blob: 51f53c311b039ca267a70a2f88f6294502c1bce5 [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
Bram Moolenaarc7875392018-09-13 14:57:41 +02003150 int extra_check = 0; // has syntax or linebreak
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#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#endif
3275#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003276 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003277# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003278 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003279# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003280 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 /* Prepare for syntax highlighting in this line. When there is an
3283 * error, stop syntax highlighting. */
3284 save_did_emsg = did_emsg;
3285 did_emsg = FALSE;
3286 syntax_start(wp, lnum);
3287 if (did_emsg)
3288 wp->w_s->b_syn_error = TRUE;
3289 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003290 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003291 did_emsg = save_did_emsg;
3292#ifdef SYN_TIME_LIMIT
3293 if (!wp->w_s->b_syn_slow)
3294#endif
3295 {
3296 has_syntax = TRUE;
3297 extra_check = TRUE;
3298 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003301
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003302 /* Check for columns to display for 'colorcolumn'. */
3303 color_cols = wp->w_p_cc_cols;
3304 if (color_cols != NULL)
3305 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003306#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003307
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003308#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003309 if (term_show_buffer(wp->w_buffer))
3310 {
3311 extra_check = TRUE;
3312 get_term_attr = TRUE;
3313 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3314 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003315#endif
3316
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003317#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003318 if (wp->w_p_spell
3319 && *wp->w_s->b_p_spl != NUL
3320 && wp->w_s->b_langp.ga_len > 0
3321 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003322 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003323 /* Prepare for spell checking. */
3324 has_spell = TRUE;
3325 extra_check = TRUE;
3326
3327 /* Get the start of the next line, so that words that wrap to the next
3328 * line are found too: "et<line-break>al.".
3329 * Trick: skip a few chars for C/shell/Vim comments */
3330 nextline[SPWORDLEN] = NUL;
3331 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3332 {
3333 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3334 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3335 }
3336
3337 /* When a word wrapped from the previous line the start of the current
3338 * line is valid. */
3339 if (lnum == checked_lnum)
3340 cur_checked_col = checked_col;
3341 checked_lnum = 0;
3342
3343 /* When there was a sentence end in the previous line may require a
3344 * word starting with capital in this line. In line 1 always check
3345 * the first word. */
3346 if (lnum != capcol_lnum)
3347 cap_col = -1;
3348 if (lnum == 1)
3349 cap_col = 0;
3350 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
3353
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003354 /*
3355 * handle visual active in this window
3356 */
3357 fromcol = -10;
3358 tocol = MAXCOL;
3359 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 /* Visual is after curwin->w_cursor */
3362 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003364 top = &curwin->w_cursor;
3365 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003369 top = &VIsual;
3370 bot = &curwin->w_cursor;
3371 }
3372 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3373 if (VIsual_mode == Ctrl_V) /* block mode */
3374 {
3375 if (lnum_in_visual_area)
3376 {
3377 fromcol = wp->w_old_cursor_fcol;
3378 tocol = wp->w_old_cursor_lcol;
3379 }
3380 }
3381 else /* non-block mode */
3382 {
3383 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 if (VIsual_mode == 'V') /* linewise */
3388 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 else
3390 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3392 if (gchar_pos(top) == NUL)
3393 tocol = fromcol + 1;
3394 }
3395 }
3396 if (VIsual_mode != 'V' && lnum == bot->lnum)
3397 {
3398 if (*p_sel == 'e' && bot->col == 0
3399#ifdef FEAT_VIRTUALEDIT
3400 && bot->coladd == 0
3401#endif
3402 )
3403 {
3404 fromcol = -10;
3405 tocol = MAXCOL;
3406 }
3407 else if (bot->col == MAXCOL)
3408 tocol = MAXCOL;
3409 else
3410 {
3411 pos = *bot;
3412 if (*p_sel == 'e')
3413 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3414 else
3415 {
3416 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3417 ++tocol;
3418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
3420 }
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 /* Check if the character under the cursor should not be inverted */
3424 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003425#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003426 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003427#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003428 )
3429 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003431 /* if inverting in this line set area_highlighting */
3432 if (fromcol >= 0)
3433 {
3434 area_highlighting = TRUE;
3435 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003437 if ((clip_star.available && !clip_star.owned
3438 && clip_isautosel_star())
3439 || (clip_plus.available && !clip_plus.owned
3440 && clip_isautosel_plus()))
3441 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003446 /*
3447 * handle 'incsearch' and ":s///c" highlighting
3448 */
3449 else if (highlight_match
3450 && wp == curwin
3451 && lnum >= curwin->w_cursor.lnum
3452 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003454 if (lnum == curwin->w_cursor.lnum)
3455 getvcol(curwin, &(curwin->w_cursor),
3456 (colnr_T *)&fromcol, NULL, NULL);
3457 else
3458 fromcol = 0;
3459 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3460 {
3461 pos.lnum = lnum;
3462 pos.col = search_match_endcol;
3463 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3464 }
3465 else
3466 tocol = MAXCOL;
3467 /* do at least one character; happens when past end of line */
3468 if (fromcol == tocol)
3469 tocol = fromcol + 1;
3470 area_highlighting = TRUE;
3471 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
3474
3475#ifdef FEAT_DIFF
3476 filler_lines = diff_check(wp, lnum);
3477 if (filler_lines < 0)
3478 {
3479 if (filler_lines == -1)
3480 {
3481 if (diff_find_change(wp, lnum, &change_start, &change_end))
3482 diff_hlf = HLF_ADD; /* added line */
3483 else if (change_start == 0)
3484 diff_hlf = HLF_TXD; /* changed text */
3485 else
3486 diff_hlf = HLF_CHD; /* changed line */
3487 }
3488 else
3489 diff_hlf = HLF_ADD; /* added line */
3490 filler_lines = 0;
3491 area_highlighting = TRUE;
3492 }
3493 if (lnum == wp->w_topline)
3494 filler_lines = wp->w_topfill;
3495 filler_todo = filler_lines;
3496#endif
3497
3498#ifdef LINE_ATTR
3499# ifdef FEAT_SIGNS
3500 /* If this line has a sign with line highlighting set line_attr. */
3501 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3502 if (v != 0)
3503 line_attr = sign_get_attr((int)v, TRUE);
3504# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003505# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003507 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003508 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509# endif
3510 if (line_attr != 0)
3511 area_highlighting = TRUE;
3512#endif
3513
3514 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3515 ptr = line;
3516
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003517#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003518 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003519 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003520 /* For checking first word with a capital skip white space. */
3521 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003522 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003523
Bram Moolenaar30abd282005-06-22 22:35:10 +00003524 /* To be able to spell-check over line boundaries copy the end of the
3525 * current line into nextline[]. Above the start of the next line was
3526 * copied to nextline[SPWORDLEN]. */
3527 if (nextline[SPWORDLEN] == NUL)
3528 {
3529 /* No next line or it is empty. */
3530 nextlinecol = MAXCOL;
3531 nextline_idx = 0;
3532 }
3533 else
3534 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003535 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003536 if (v < SPWORDLEN)
3537 {
3538 /* Short line, use it completely and append the start of the
3539 * next line. */
3540 nextlinecol = 0;
3541 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003542 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003543 nextline_idx = v + 1;
3544 }
3545 else
3546 {
3547 /* Long line, use only the last SPWORDLEN bytes. */
3548 nextlinecol = v - SPWORDLEN;
3549 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3550 nextline_idx = SPWORDLEN + 1;
3551 }
3552 }
3553 }
3554#endif
3555
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003556 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003558 if (lcs_space || lcs_trail)
3559 extra_check = TRUE;
3560 /* find start of trailing whitespace */
3561 if (lcs_trail)
3562 {
3563 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003564 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003565 --trailcol;
3566 trailcol += (colnr_T) (ptr - line);
3567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569
3570 /*
3571 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3572 * first character to be displayed.
3573 */
3574 if (wp->w_p_wrap)
3575 v = wp->w_skipcol;
3576 else
3577 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003578 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580#ifdef FEAT_MBYTE
3581 char_u *prev_ptr = ptr;
3582#endif
3583 while (vcol < v && *ptr != NUL)
3584 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003585 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 vcol += c;
3587#ifdef FEAT_MBYTE
3588 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003590 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003593 /* When:
3594 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003595 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003596 * - 'virtualedit' is set, or
3597 * - the visual mode is active,
3598 * the end of the line may be before the start of the displayed part.
3599 */
3600 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003601#ifdef FEAT_SYN_HL
3602 wp->w_p_cuc || draw_color_col ||
3603#endif
3604#ifdef FEAT_VIRTUALEDIT
3605 virtual_active() ||
3606#endif
3607 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
3612 /* Handle a character that's not completely on the screen: Put ptr at
3613 * that character but skip the first few screen characters. */
3614 if (vcol > v)
3615 {
3616 vcol -= c;
3617#ifdef FEAT_MBYTE
3618 ptr = prev_ptr;
3619#else
3620 --ptr;
3621#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003622 /* If the character fits on the screen, don't need to skip it.
3623 * Except for a TAB. */
3624 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003625#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003626 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003627#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003628 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003629 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632 /*
3633 * Adjust for when the inverted text is before the screen,
3634 * and when the start of the inverted text is before the screen.
3635 */
3636 if (tocol <= vcol)
3637 fromcol = 0;
3638 else if (fromcol >= 0 && fromcol < vcol)
3639 fromcol = vcol;
3640
3641#ifdef FEAT_LINEBREAK
3642 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3643 if (wp->w_p_wrap)
3644 need_showbreak = TRUE;
3645#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003646#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003647 /* When spell checking a word we need to figure out the start of the
3648 * word and if it's badly spelled or not. */
3649 if (has_spell)
3650 {
3651 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003652 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003653 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003654
3655 pos = wp->w_cursor;
3656 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003657 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003658 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003659
3660 /* spell_move_to() may call ml_get() and make "line" invalid */
3661 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3662 ptr = line + linecol;
3663
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003664 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003665 {
3666 /* no bad word found at line start, don't check until end of a
3667 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003668 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003669 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003670 }
3671 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003672 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003673 /* bad word found, use attributes until end of word */
3674 word_end = wp->w_cursor.col + len + 1;
3675
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003676 /* Turn index into actual attributes. */
3677 if (spell_hlf != HLF_COUNT)
3678 spell_attr = highlight_attr[spell_hlf];
3679 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003680 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003681
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003682# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003683 /* Need to restart syntax highlighting for this line. */
3684 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003685 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003686# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003687 }
3688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691 /*
3692 * Correct highlighting for cursor that can't be disabled.
3693 * Avoids having to check this for each character.
3694 */
3695 if (fromcol >= 0)
3696 {
3697 if (noinvcur)
3698 {
3699 if ((colnr_T)fromcol == wp->w_virtcol)
3700 {
3701 /* highlighting starts at cursor, let it start just after the
3702 * cursor */
3703 fromcol_prev = fromcol;
3704 fromcol = -1;
3705 }
3706 else if ((colnr_T)fromcol < wp->w_virtcol)
3707 /* restart highlighting after the cursor */
3708 fromcol_prev = wp->w_virtcol;
3709 }
3710 if (fromcol >= tocol)
3711 fromcol = -1;
3712 }
3713
3714#ifdef FEAT_SEARCH_EXTRA
3715 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003716 * Handle highlighting the last used search pattern and matches.
3717 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003719 cur = wp->w_match_head;
3720 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003721 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003723 if (shl_flag == FALSE)
3724 {
3725 shl = &search_hl;
3726 shl_flag = TRUE;
3727 }
3728 else
3729 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003730 shl->startcol = MAXCOL;
3731 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003733 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003734 v = (long)(ptr - line);
3735 if (cur != NULL)
3736 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003737 next_search_hl(wp, shl, lnum, (colnr_T)v,
3738 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003739
3740 /* Need to get the line again, a multi-line regexp may have made it
3741 * invalid. */
3742 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3743 ptr = line + v;
3744
3745 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003747 if (shl->lnum == lnum)
3748 shl->startcol = shl->rm.startpos[0].col;
3749 else
3750 shl->startcol = 0;
3751 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3752 - shl->rm.startpos[0].lnum)
3753 shl->endcol = shl->rm.endpos[0].col;
3754 else
3755 shl->endcol = MAXCOL;
3756 /* Highlight one character for an empty match. */
3757 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003760 if (has_mbyte && line[shl->endcol] != NUL)
3761 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003764 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003766 if ((long)shl->startcol < v) /* match at leftcol */
3767 {
3768 shl->attr_cur = shl->attr;
3769 search_attr = shl->attr;
3770 }
3771 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003773 if (shl != &search_hl && cur != NULL)
3774 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776#endif
3777
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003778#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003779 /* Cursor line highlighting for 'cursorline' in the current window. Not
3780 * when Visual mode is active, because it's not clear what is selected
3781 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003782 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003783 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003784 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003785 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003786 area_highlighting = TRUE;
3787 }
3788#endif
3789
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003790 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 col = 0;
3792#ifdef FEAT_RIGHTLEFT
3793 if (wp->w_p_rl)
3794 {
3795 /* Rightleft window: process the text in the normal direction, but put
3796 * it in current_ScreenLine[] from right to left. Start at the
3797 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003798 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 off += col;
3800 }
3801#endif
3802
3803 /*
3804 * Repeat for the whole displayed line.
3805 */
3806 for (;;)
3807 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003808#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003809 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 /* Skip this quickly when working on the text. */
3812 if (draw_state != WL_LINE)
3813 {
3814#ifdef FEAT_CMDWIN
3815 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3816 {
3817 draw_state = WL_CMDLINE;
3818 if (cmdwin_type != 0 && wp == curwin)
3819 {
3820 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003822 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003823 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 }
3826#endif
3827
3828#ifdef FEAT_FOLDING
3829 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3830 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003831 int fdc = compute_foldcolumn(wp, 0);
3832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003834 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003836 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003837 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003838 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003839 p_extra_free = alloc(12 + 1);
3840
3841 if (p_extra_free != NULL)
3842 {
3843 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3844 n_extra = fdc;
3845 p_extra_free[n_extra] = NUL;
3846 p_extra = p_extra_free;
3847 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003848 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 }
3852#endif
3853
3854#ifdef FEAT_SIGNS
3855 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3856 {
3857 draw_state = WL_SIGN;
3858 /* Show the sign column when there are any signs in this
3859 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003860 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003862 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003864 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865# endif
3866
3867 /* Draw two cells with the sign value or blank. */
3868 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003869 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 n_extra = 2;
3871
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003872 if (row == startrow
3873#ifdef FEAT_DIFF
3874 + filler_lines && filler_todo <= 0
3875#endif
3876 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
3878 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3879 SIGN_TEXT);
3880# ifdef FEAT_SIGN_ICONS
3881 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3882 SIGN_ICON);
3883 if (gui.in_use && icon_sign != 0)
3884 {
3885 /* Use the image in this position. */
3886 c_extra = SIGN_BYTE;
3887# ifdef FEAT_NETBEANS_INTG
3888 if (buf_signcount(wp->w_buffer, lnum) > 1)
3889 c_extra = MULTISIGN_BYTE;
3890# endif
3891 char_attr = icon_sign;
3892 }
3893 else
3894# endif
3895 if (text_sign != 0)
3896 {
3897 p_extra = sign_get_text(text_sign);
3898 if (p_extra != NULL)
3899 {
3900 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003901 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 }
3903 char_attr = sign_get_attr(text_sign, FALSE);
3904 }
3905 }
3906 }
3907 }
3908#endif
3909
3910 if (draw_state == WL_NR - 1 && n_extra == 0)
3911 {
3912 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003913 /* Display the absolute or relative line number. After the
3914 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3915 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 && (row == startrow
3917#ifdef FEAT_DIFF
3918 + filler_lines
3919#endif
3920 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3921 {
3922 /* Draw the line number (empty space after wrapping). */
3923 if (row == startrow
3924#ifdef FEAT_DIFF
3925 + filler_lines
3926#endif
3927 )
3928 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003929 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003930 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003931
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003932 if (wp->w_p_nu && !wp->w_p_rnu)
3933 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003934 num = (long)lnum;
3935 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003936 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003937 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003938 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003939 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003940 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003941 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003942 num = lnum;
3943 fmt = "%-*ld ";
3944 }
3945 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003946
Bram Moolenaar700e7342013-01-30 12:31:36 +01003947 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003948 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 if (wp->w_skipcol > 0)
3950 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3951 *p_extra = '-';
3952#ifdef FEAT_RIGHTLEFT
3953 if (wp->w_p_rl) /* reverse line numbers */
3954 rl_mirror(extra);
3955#endif
3956 p_extra = extra;
3957 c_extra = NUL;
3958 }
3959 else
3960 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003961 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003962 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003963#ifdef FEAT_SYN_HL
3964 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003965 * the current line differently.
3966 * TODO: Can we use CursorLine instead of CursorLineNr
3967 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003968 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003969 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003970 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 }
3974
Bram Moolenaar597a4222014-06-25 14:39:50 +02003975#ifdef FEAT_LINEBREAK
3976 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3977 && n_extra == 0 && *p_sbr != NUL)
3978 /* draw indent after showbreak value */
3979 draw_state = WL_BRI;
3980 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3981 /* After the showbreak, draw the breakindent */
3982 draw_state = WL_BRI - 1;
3983
3984 /* draw 'breakindent': indent wrapped text accordingly */
3985 if (draw_state == WL_BRI - 1 && n_extra == 0)
3986 {
3987 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003988 /* if need_showbreak is set, breakindent also applies */
3989 if (wp->w_p_bri && n_extra == 0
3990 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003991# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003992 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003993# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003994 )
3995 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003996 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003997# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003998 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003999 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004000 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004001# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004002 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4003 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004004 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004005# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004006 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004007# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004008 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004009 c_extra = ' ';
4010 n_extra = get_breakindent_win(wp,
4011 ml_get_buf(wp->w_buffer, lnum, FALSE));
4012 /* Correct end of highlighted area for 'breakindent',
4013 * required when 'linebreak' is also set. */
4014 if (tocol == vcol)
4015 tocol += n_extra;
4016 }
4017 }
4018#endif
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4021 if (draw_state == WL_SBR - 1 && n_extra == 0)
4022 {
4023 draw_state = WL_SBR;
4024# ifdef FEAT_DIFF
4025 if (filler_todo > 0)
4026 {
4027 /* Draw "deleted" diff line(s). */
4028 if (char2cells(fill_diff) > 1)
4029 c_extra = '-';
4030 else
4031 c_extra = fill_diff;
4032# ifdef FEAT_RIGHTLEFT
4033 if (wp->w_p_rl)
4034 n_extra = col + 1;
4035 else
4036# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004037 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004038 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040# endif
4041# ifdef FEAT_LINEBREAK
4042 if (*p_sbr != NUL && need_showbreak)
4043 {
4044 /* Draw 'showbreak' at the start of each broken line. */
4045 p_extra = p_sbr;
4046 c_extra = NUL;
4047 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004048 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004050 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /* Correct end of highlighted area for 'showbreak',
4052 * required when 'linebreak' is also set. */
4053 if (tocol == vcol)
4054 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004055#ifdef FEAT_SYN_HL
4056 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004057 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004058 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004059 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062# endif
4063 }
4064#endif
4065
4066 if (draw_state == WL_LINE - 1 && n_extra == 0)
4067 {
4068 draw_state = WL_LINE;
4069 if (saved_n_extra)
4070 {
4071 /* Continue item from end of wrapped line. */
4072 n_extra = saved_n_extra;
4073 c_extra = saved_c_extra;
4074 p_extra = saved_p_extra;
4075 char_attr = saved_char_attr;
4076 }
4077 else
4078 char_attr = 0;
4079 }
4080 }
4081
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004082 // When still displaying '$' of change command, stop at cursor.
4083 // When only displaying the (relative) line number and that's done,
4084 // stop here.
4085 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004086 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_DIFF
4088 && filler_todo <= 0
4089#endif
4090 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004091 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004093 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004094 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004095 /* Pretend we have finished updating the window. Except when
4096 * 'cursorcolumn' is set. */
4097#ifdef FEAT_SYN_HL
4098 if (wp->w_p_cuc)
4099 row = wp->w_cline_row + wp->w_cline_height;
4100 else
4101#endif
4102 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 break;
4104 }
4105
4106 if (draw_state == WL_LINE && area_highlighting)
4107 {
4108 /* handle Visual or match highlighting in this line */
4109 if (vcol == fromcol
4110#ifdef FEAT_MBYTE
4111 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4112 && (*mb_ptr2cells)(ptr) > 1)
4113#endif
4114 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004115 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 && vcol < tocol))
4117 area_attr = attr; /* start highlighting */
4118 else if (area_attr != 0
4119 && (vcol == tocol
4120 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123#ifdef FEAT_SEARCH_EXTRA
4124 if (!n_extra)
4125 {
4126 /*
4127 * Check for start/end of search pattern match.
4128 * After end, check for start/end of next match.
4129 * When another match, have to check for start again.
4130 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004131 * Do this for 'search_hl' and the match list (ordered by
4132 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004134 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004135 cur = wp->w_match_head;
4136 shl_flag = FALSE;
4137 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004139 if (shl_flag == FALSE
4140 && ((cur != NULL
4141 && cur->priority > SEARCH_HL_PRIORITY)
4142 || cur == NULL))
4143 {
4144 shl = &search_hl;
4145 shl_flag = TRUE;
4146 }
4147 else
4148 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004149 if (cur != NULL)
4150 cur->pos.cur = 0;
4151 pos_inprogress = TRUE;
4152 while (shl->rm.regprog != NULL
4153 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004155 if (shl->startcol != MAXCOL
4156 && v >= (long)shl->startcol
4157 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004159#ifdef FEAT_MBYTE
4160 int tmp_col = v + MB_PTR2LEN(ptr);
4161
4162 if (shl->endcol < tmp_col)
4163 shl->endcol = tmp_col;
4164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004166#ifdef FEAT_CONCEAL
4167 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4168 == cur->hlg_id)
4169 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004170 has_match_conc =
4171 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004172 match_conc = cur->conceal_char;
4173 }
4174 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004175 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004178 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004181 next_search_hl(wp, shl, lnum, (colnr_T)v,
4182 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004183 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004184 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 /* Need to get the line again, a multi-line regexp
4187 * may have made it invalid. */
4188 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4189 ptr = line + v;
4190
4191 if (shl->lnum == lnum)
4192 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004193 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004195 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004199 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201 /* highlight empty match, try again after
4202 * it */
4203#ifdef FEAT_MBYTE
4204 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004205 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004206 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 else
4208#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004209 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211
4212 /* Loop to check if the match starts at the
4213 * current position */
4214 continue;
4215 }
4216 }
4217 break;
4218 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004219 if (shl != &search_hl && cur != NULL)
4220 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004222
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004223 /* Use attributes from match with highest priority among
4224 * 'search_hl' and the match list. */
4225 search_attr = search_hl.attr_cur;
4226 cur = wp->w_match_head;
4227 shl_flag = FALSE;
4228 while (cur != NULL || shl_flag == FALSE)
4229 {
4230 if (shl_flag == FALSE
4231 && ((cur != NULL
4232 && cur->priority > SEARCH_HL_PRIORITY)
4233 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004234 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004235 shl = &search_hl;
4236 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004237 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004238 else
4239 shl = &cur->hl;
4240 if (shl->attr_cur != 0)
4241 search_attr = shl->attr_cur;
4242 if (shl != &search_hl && cur != NULL)
4243 cur = cur->next;
4244 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004245 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004246 if (*ptr == NUL && (did_line_attr >= 1
4247 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004248 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250#endif
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004253 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004255 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4256 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004258 if (diff_hlf == HLF_TXD && ptr - line > change_end
4259 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004261 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004262 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004263 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004266
4267 /* Decide which of the highlight attributes to use. */
4268 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004269#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004270 if (area_attr != 0)
4271 char_attr = hl_combine_attr(line_attr, area_attr);
4272 else if (search_attr != 0)
4273 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004274 /* Use line_attr when not in the Visual or 'incsearch' area
4275 * (area_attr may be 0 when "noinvcur" is set). */
4276 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004277 || vcol < fromcol || vcol_prev < fromcol_prev
4278 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004279 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004280#else
4281 if (area_attr != 0)
4282 char_attr = area_attr;
4283 else if (search_attr != 0)
4284 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004285#endif
4286 else
4287 {
4288 attr_pri = FALSE;
4289#ifdef FEAT_SYN_HL
4290 if (has_syntax)
4291 char_attr = syntax_attr;
4292 else
4293#endif
4294 char_attr = 0;
4295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297
4298 /*
4299 * Get the next character to put on the screen.
4300 */
4301 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004302 * The "p_extra" points to the extra stuff that is inserted to
4303 * represent special characters (non-printable stuff) and other
4304 * things. When all characters are the same, c_extra is used.
4305 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4306 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4308 */
4309 if (n_extra > 0)
4310 {
4311 if (c_extra != NUL)
4312 {
4313 c = c_extra;
4314#ifdef FEAT_MBYTE
4315 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004316 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
4318 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004319 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004320 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 else
4323 mb_utf8 = FALSE;
4324#endif
4325 }
4326 else
4327 {
4328 c = *p_extra;
4329#ifdef FEAT_MBYTE
4330 if (has_mbyte)
4331 {
4332 mb_c = c;
4333 if (enc_utf8)
4334 {
4335 /* If the UTF-8 character is more than one byte:
4336 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004337 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 mb_utf8 = FALSE;
4339 if (mb_l > n_extra)
4340 mb_l = 1;
4341 else if (mb_l > 1)
4342 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004343 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004345 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 }
4348 else
4349 {
4350 /* if this is a DBCS character, put it in "mb_c" */
4351 mb_l = MB_BYTE2LEN(c);
4352 if (mb_l >= n_extra)
4353 mb_l = 1;
4354 else if (mb_l > 1)
4355 mb_c = (c << 8) + p_extra[1];
4356 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004357 if (mb_l == 0) /* at the NUL at end-of-line */
4358 mb_l = 1;
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /* If a double-width char doesn't fit display a '>' in the
4361 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004362 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363# ifdef FEAT_RIGHTLEFT
4364 wp->w_p_rl ? (col <= 0) :
4365# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004366 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 && (*mb_char2cells)(mb_c) == 2)
4368 {
4369 c = '>';
4370 mb_c = c;
4371 mb_l = 1;
4372 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004373 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 /* put the pointer back to output the double-width
4375 * character at the start of the next line. */
4376 ++n_extra;
4377 --p_extra;
4378 }
4379 else
4380 {
4381 n_extra -= mb_l - 1;
4382 p_extra += mb_l - 1;
4383 }
4384 }
4385#endif
4386 ++p_extra;
4387 }
4388 --n_extra;
4389 }
4390 else
4391 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004392#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004393 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004394#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004395
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004396 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004397 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 /*
4399 * Get a character from the line itself.
4400 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004401 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004402#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004403 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_MBYTE
4406 if (has_mbyte)
4407 {
4408 mb_c = c;
4409 if (enc_utf8)
4410 {
4411 /* If the UTF-8 character is more than one byte: Decode it
4412 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004413 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 mb_utf8 = FALSE;
4415 if (mb_l > 1)
4416 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004417 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 /* Overlong encoded ASCII or ASCII with composing char
4419 * is displayed normally, except a NUL. */
4420 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004421 {
4422 c = mb_c;
4423# ifdef FEAT_LINEBREAK
4424 c0 = mb_c;
4425# endif
4426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004428
4429 /* At start of the line we can have a composing char.
4430 * Draw it as a space with a composing char. */
4431 if (utf_iscomposing(mb_c))
4432 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004433 int i;
4434
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004435 for (i = Screen_mco - 1; i > 0; --i)
4436 u8cc[i] = u8cc[i - 1];
4437 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004438 mb_c = ' ';
4439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441
4442 if ((mb_l == 1 && c >= 0x80)
4443 || (mb_l >= 1 && mb_c == 0)
4444 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004445# ifdef UNICODE16
4446 || mb_c >= 0x10000
4447# endif
4448 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
4450 /*
4451 * Illegal UTF-8 byte: display as <xx>.
4452 * Non-BMP character : display as ? or fullwidth ?.
4453 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004454# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004459# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (wp->w_p_rl) /* reverse */
4461 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004464# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else if (utf_char2cells(mb_c) != 2)
4466 STRCPY(extra, "?");
4467 else
4468 /* 0xff1f in UTF-8: full-width '?' */
4469 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 p_extra = extra;
4473 c = *p_extra;
4474 mb_c = mb_ptr2char_adv(&p_extra);
4475 mb_utf8 = (c >= 0x80);
4476 n_extra = (int)STRLEN(p_extra);
4477 c_extra = NUL;
4478 if (area_attr == 0 && search_attr == 0)
4479 {
4480 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004481 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 saved_attr2 = char_attr; /* save current attr */
4483 }
4484 }
4485 else if (mb_l == 0) /* at the NUL at end-of-line */
4486 mb_l = 1;
4487#ifdef FEAT_ARABIC
4488 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4489 {
4490 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004491 int pc, pc1, nc;
4492 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494 /* The idea of what is the previous and next
4495 * character depends on 'rightleft'. */
4496 if (wp->w_p_rl)
4497 {
4498 pc = prev_c;
4499 pc1 = prev_c1;
4500 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004501 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503 else
4504 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004505 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004507 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 prev_c = mb_c;
4510
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004511 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513 else
4514 prev_c = mb_c;
4515#endif
4516 }
4517 else /* enc_dbcs */
4518 {
4519 mb_l = MB_BYTE2LEN(c);
4520 if (mb_l == 0) /* at the NUL at end-of-line */
4521 mb_l = 1;
4522 else if (mb_l > 1)
4523 {
4524 /* We assume a second byte below 32 is illegal.
4525 * Hopefully this is OK for all double-byte encodings!
4526 */
4527 if (ptr[1] >= 32)
4528 mb_c = (c << 8) + ptr[1];
4529 else
4530 {
4531 if (ptr[1] == NUL)
4532 {
4533 /* head byte at end of line */
4534 mb_l = 1;
4535 transchar_nonprint(extra, c);
4536 }
4537 else
4538 {
4539 /* illegal tail byte */
4540 mb_l = 2;
4541 STRCPY(extra, "XX");
4542 }
4543 p_extra = extra;
4544 n_extra = (int)STRLEN(extra) - 1;
4545 c_extra = NUL;
4546 c = *p_extra++;
4547 if (area_attr == 0 && search_attr == 0)
4548 {
4549 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004550 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 saved_attr2 = char_attr; /* save current attr */
4552 }
4553 mb_c = c;
4554 }
4555 }
4556 }
4557 /* If a double-width char doesn't fit display a '>' in the
4558 * last column; the character is displayed at the start of the
4559 * next line. */
4560 if ((
4561# ifdef FEAT_RIGHTLEFT
4562 wp->w_p_rl ? (col <= 0) :
4563# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004564 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 && (*mb_char2cells)(mb_c) == 2)
4566 {
4567 c = '>';
4568 mb_c = c;
4569 mb_utf8 = FALSE;
4570 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004571 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 /* Put pointer back so that the character will be
4573 * displayed at the start of the next line. */
4574 --ptr;
4575 }
4576 else if (*ptr != NUL)
4577 ptr += mb_l - 1;
4578
4579 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004580 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004581 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004582 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004585 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 c = ' ';
4587 if (area_attr == 0 && search_attr == 0)
4588 {
4589 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004590 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 saved_attr2 = char_attr; /* save current attr */
4592 }
4593 mb_c = c;
4594 mb_utf8 = FALSE;
4595 mb_l = 1;
4596 }
4597
4598 }
4599#endif
4600 ++ptr;
4601
4602 if (extra_check)
4603 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004604#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004605 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004606#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004607
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004608#ifdef FEAT_TERMINAL
4609 if (get_term_attr)
4610 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004611 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004612
4613 if (!attr_pri)
4614 char_attr = syntax_attr;
4615 else
4616 char_attr = hl_combine_attr(syntax_attr, char_attr);
4617 }
4618#endif
4619
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004620#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 /* Get syntax attribute, unless still at the start of the line
4622 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004623 v = (long)(ptr - line);
4624 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
4626 /* Get the syntax attribute for the character. If there
4627 * is an error, disable syntax highlighting. */
4628 save_did_emsg = did_emsg;
4629 did_emsg = FALSE;
4630
Bram Moolenaar217ad922005-03-20 22:37:15 +00004631 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004632# ifdef FEAT_SPELL
4633 has_spell ? &can_spell :
4634# endif
4635 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
4637 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004638 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004639 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004640 has_syntax = FALSE;
4641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 else
4643 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004644#ifdef SYN_TIME_LIMIT
4645 if (wp->w_s->b_syn_slow)
4646 has_syntax = FALSE;
4647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 /* Need to get the line again, a multi-line regexp may
4650 * have made it invalid. */
4651 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4652 ptr = line + v;
4653
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004654 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004656 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004657 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004658# ifdef FEAT_CONCEAL
4659 /* no concealing past the end of the line, it interferes
4660 * with line highlighting */
4661 if (c == NUL)
4662 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004663 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004664 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004667#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004668
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004669#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004670 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004671 * Only do this when there is no syntax highlighting, the
4672 * @Spell cluster is not used or the current syntax item
4673 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004674 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004675 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004676 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004677# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004678 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004679 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004680# endif
4681 if (c != 0 && (
4682# ifdef FEAT_SYN_HL
4683 !has_syntax ||
4684# endif
4685 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004686 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004687 char_u *prev_ptr, *p;
4688 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004689 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004690# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004691 if (has_mbyte)
4692 {
4693 prev_ptr = ptr - mb_l;
4694 v -= mb_l - 1;
4695 }
4696 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004697# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004698 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004699
4700 /* Use nextline[] if possible, it has the start of the
4701 * next line concatenated. */
4702 if ((prev_ptr - line) - nextlinecol >= 0)
4703 p = nextline + (prev_ptr - line) - nextlinecol;
4704 else
4705 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004706 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004707 len = spell_check(wp, p, &spell_hlf, &cap_col,
4708 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004709 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004710
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004711 /* In Insert mode only highlight a word that
4712 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004713 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004714 && (State & INSERT) != 0
4715 && wp->w_cursor.lnum == lnum
4716 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004717 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004718 && wp->w_cursor.col < (colnr_T)word_end)
4719 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004720 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004721 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004722 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004723
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004724 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004725 && (p - nextline) + len > nextline_idx)
4726 {
4727 /* Remember that the good word continues at the
4728 * start of the next line. */
4729 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004730 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004731 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004732
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004733 /* Turn index into actual attributes. */
4734 if (spell_hlf != HLF_COUNT)
4735 spell_attr = highlight_attr[spell_hlf];
4736
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004737 if (cap_col > 0)
4738 {
4739 if (p != prev_ptr
4740 && (p - nextline) + cap_col >= nextline_idx)
4741 {
4742 /* Remember that the word in the next line
4743 * must start with a capital. */
4744 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004745 cap_col = (int)((p - nextline) + cap_col
4746 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004747 }
4748 else
4749 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004750 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004751 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004752 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004753 }
4754 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004755 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004756 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004757 char_attr = hl_combine_attr(char_attr, spell_attr);
4758 else
4759 char_attr = hl_combine_attr(spell_attr, char_attr);
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
4762#ifdef FEAT_LINEBREAK
4763 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004764 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004766 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004767 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004769# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004770 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004771# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004772 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004774 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004776 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004777
Bram Moolenaar597a4222014-06-25 14:39:50 +02004778 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004779 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004780 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004781 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004782# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004783 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004784 wp->w_buffer->b_p_vts_array) - 1;
4785# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004786 n_extra = (int)wp->w_buffer->b_p_ts
4787 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004788# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004789
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004790# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004791 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004792# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004794# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004795 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004796 {
4797#ifdef FEAT_CONCEAL
4798 if (c == TAB)
4799 /* See "Tab alignment" below. */
4800 FIX_FOR_BOGUSCOLS;
4801#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004802 if (!wp->w_p_list)
4803 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
4806#endif
4807
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004808 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4809 */
4810 if (wp->w_p_list
4811 && (((c == 160
4812#ifdef FEAT_MBYTE
4813 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4814#endif
4815 ) && lcs_nbsp)
4816 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4817 {
4818 c = (c == ' ') ? lcs_space : lcs_nbsp;
4819 if (area_attr == 0 && search_attr == 0)
4820 {
4821 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004822 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004823 saved_attr2 = char_attr; /* save current attr */
4824 }
4825#ifdef FEAT_MBYTE
4826 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004827 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004828 {
4829 mb_utf8 = TRUE;
4830 u8cc[0] = 0;
4831 c = 0xc0;
4832 }
4833 else
4834 mb_utf8 = FALSE;
4835#endif
4836 }
4837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4839 {
4840 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004841 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
4843 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004844 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 saved_attr2 = char_attr; /* save current attr */
4846 }
4847#ifdef FEAT_MBYTE
4848 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004849 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
4851 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004852 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004853 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 else
4856 mb_utf8 = FALSE;
4857#endif
4858 }
4859 }
4860
4861 /*
4862 * Handling of non-printable characters.
4863 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004864 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
4866 /*
4867 * when getting a character from the file, we may have to
4868 * turn it into something else on the way to putting it
4869 * into "ScreenLines".
4870 */
4871 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4872 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004873 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004874 long vcol_adjusted = vcol; /* removed showbreak length */
4875#ifdef FEAT_LINEBREAK
4876 /* only adjust the tab_len, when at the first column
4877 * after the showbreak value was drawn */
4878 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4879 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004882#ifdef FEAT_VARTABS
4883 tab_len = tabstop_padding(vcol_adjusted,
4884 wp->w_buffer->b_p_ts,
4885 wp->w_buffer->b_p_vts_array) - 1;
4886#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004887 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004888 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4889#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004890
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004891#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004892 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004893#endif
4894 /* tab amount depends on current column */
4895 n_extra = tab_len;
4896#ifdef FEAT_LINEBREAK
4897 else
4898 {
4899 char_u *p;
4900 int len = n_extra;
4901 int i;
4902 int saved_nextra = n_extra;
4903
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004904#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004905 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004906 /* there are characters to conceal */
4907 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004908 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4909 */
4910 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4911 && n_extra > tab_len)
4912 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004913#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004914
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004915 /* if n_extra > 0, it gives the number of chars, to
4916 * use for a tab, else we need to calculate the width
4917 * for a tab */
4918#ifdef FEAT_MBYTE
4919 len = (tab_len * mb_char2len(lcs_tab2));
4920 if (n_extra > 0)
4921 len += n_extra - tab_len;
4922#endif
4923 c = lcs_tab1;
4924 p = alloc((unsigned)(len + 1));
4925 vim_memset(p, ' ', len);
4926 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004927 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004928 p_extra_free = p;
4929 for (i = 0; i < tab_len; i++)
4930 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004931 if (*p == NUL)
4932 {
4933 tab_len = i;
4934 break;
4935 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004936#ifdef FEAT_MBYTE
4937 mb_char2bytes(lcs_tab2, p);
4938 p += mb_char2len(lcs_tab2);
4939 n_extra += mb_char2len(lcs_tab2)
4940 - (saved_nextra > 0 ? 1 : 0);
4941#else
4942 p[i] = lcs_tab2;
4943#endif
4944 }
4945 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004946#ifdef FEAT_CONCEAL
4947 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4948 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004949 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004950 n_extra -= vcol_off;
4951#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004952 }
4953#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004954#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004955 {
4956 int vc_saved = vcol_off;
4957
4958 /* Tab alignment should be identical regardless of
4959 * 'conceallevel' value. So tab compensates of all
4960 * previous concealed characters, and thus resets
4961 * vcol_off and boguscols accumulated so far in the
4962 * line. Note that the tab can be longer than
4963 * 'tabstop' when there are concealed characters. */
4964 FIX_FOR_BOGUSCOLS;
4965
4966 /* Make sure, the highlighting for the tab char will be
4967 * correctly set further below (effectively reverts the
4968 * FIX_FOR_BOGSUCOLS macro */
4969 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004970 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004971 tab_len += vc_saved;
4972 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#ifdef FEAT_MBYTE
4975 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4976#endif
4977 if (wp->w_p_list)
4978 {
4979 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004980#ifdef FEAT_LINEBREAK
4981 if (wp->w_p_lbr)
4982 c_extra = NUL; /* using p_extra from above */
4983 else
4984#endif
4985 c_extra = lcs_tab2;
4986 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004987 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 saved_attr2 = char_attr; /* save current attr */
4989#ifdef FEAT_MBYTE
4990 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004991 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004994 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004995 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997#endif
4998 }
4999 else
5000 {
5001 c_extra = ' ';
5002 c = ' ';
5003 }
5004 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005005 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005006 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005007 || ((fromcol >= 0 || fromcol_prev >= 0)
5008 && tocol > vcol
5009 && VIsual_mode != Ctrl_V
5010 && (
5011# ifdef FEAT_RIGHTLEFT
5012 wp->w_p_rl ? (col >= 0) :
5013# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005014 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005015 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005016 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005017 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005018 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005020 /* Display a '$' after the line or highlight an extra
5021 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5023 /* For a diff line the highlighting continues after the
5024 * "$". */
5025 if (
5026# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005027 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028# ifdef LINE_ATTR
5029 &&
5030# endif
5031# endif
5032# ifdef LINE_ATTR
5033 line_attr == 0
5034# endif
5035 )
5036#endif
5037 {
5038#ifdef FEAT_VIRTUALEDIT
5039 /* In virtualedit, visual selections may extend
5040 * beyond end of line. */
5041 if (area_highlighting && virtual_active()
5042 && tocol != MAXCOL && vcol < tocol)
5043 n_extra = 0;
5044 else
5045#endif
5046 {
5047 p_extra = at_end_str;
5048 n_extra = 1;
5049 c_extra = NUL;
5050 }
5051 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005052 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005053 c = lcs_eol;
5054 else
5055 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 lcs_eol_one = -1;
5057 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005058 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005060 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 n_attr = 1;
5062 }
5063#ifdef FEAT_MBYTE
5064 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005065 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005068 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005069 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 else
5072 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5073#endif
5074 }
5075 else if (c != NUL)
5076 {
5077 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005078 if (n_extra == 0)
5079 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080#ifdef FEAT_RIGHTLEFT
5081 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5082 rl_mirror(p_extra); /* reverse "<12>" */
5083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005085#ifdef FEAT_LINEBREAK
5086 if (wp->w_p_lbr)
5087 {
5088 char_u *p;
5089
5090 c = *p_extra;
5091 p = alloc((unsigned)n_extra + 1);
5092 vim_memset(p, ' ', n_extra);
5093 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5094 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005095 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005096 p_extra_free = p_extra = p;
5097 }
5098 else
5099#endif
5100 {
5101 n_extra = byte2cells(c) - 1;
5102 c = *p_extra++;
5103 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005104 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005107 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 saved_attr2 = char_attr; /* save current attr */
5109 }
5110#ifdef FEAT_MBYTE
5111 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5112#endif
5113 }
5114#ifdef FEAT_VIRTUALEDIT
5115 else if (VIsual_active
5116 && (VIsual_mode == Ctrl_V
5117 || VIsual_mode == 'v')
5118 && virtual_active()
5119 && tocol != MAXCOL
5120 && vcol < tocol
5121 && (
5122# ifdef FEAT_RIGHTLEFT
5123 wp->w_p_rl ? (col >= 0) :
5124# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005125 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 c = ' ';
5128 --ptr; /* put it back at the NUL */
5129 }
5130#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005131#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 else if ((
5133# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005134 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005136# ifdef FEAT_TERMINAL
5137 term_attr != 0 ||
5138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 ) && (
5141# ifdef FEAT_RIGHTLEFT
5142 wp->w_p_rl ? (col >= 0) :
5143# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005144 (col
5145# ifdef FEAT_CONCEAL
5146 - boguscols
5147# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005148 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 /* Highlight until the right side of the window */
5151 c = ' ';
5152 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005153
5154 /* Remember we do the char for line highlighting. */
5155 ++did_line_attr;
5156
5157 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005158 if (line_attr != 0 && char_attr == search_attr
5159 && (did_line_attr > 1
5160 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005161 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162# ifdef FEAT_DIFF
5163 if (diff_hlf == HLF_TXD)
5164 {
5165 diff_hlf = HLF_CHD;
5166 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005167 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005168 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005169 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5170 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005171 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005175# ifdef FEAT_TERMINAL
5176 if (term_attr != 0)
5177 {
5178 char_attr = term_attr;
5179 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5180 char_attr = hl_combine_attr(char_attr,
5181 HL_ATTR(HLF_CUL));
5182 }
5183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185#endif
5186 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005187
5188#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005189 if ( wp->w_p_cole > 0
5190 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005191 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005192 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005193 && !(lnum_in_visual_area
5194 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005195 {
5196 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005197 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005198 && (syn_get_sub_char() != NUL || match_conc
5199 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005200 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005201 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005202 /* First time at this concealed item: display one
5203 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005204 if (match_conc)
5205 c = match_conc;
5206 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005207 c = syn_get_sub_char();
5208 else if (lcs_conceal != NUL)
5209 c = lcs_conceal;
5210 else
5211 c = ' ';
5212
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005213 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005214
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005215 if (n_extra > 0)
5216 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005217 vcol += n_extra;
5218 if (wp->w_p_wrap && n_extra > 0)
5219 {
5220# ifdef FEAT_RIGHTLEFT
5221 if (wp->w_p_rl)
5222 {
5223 col -= n_extra;
5224 boguscols -= n_extra;
5225 }
5226 else
5227# endif
5228 {
5229 boguscols += n_extra;
5230 col += n_extra;
5231 }
5232 }
5233 n_extra = 0;
5234 n_attr = 0;
5235 }
5236 else if (n_skip == 0)
5237 {
5238 is_concealing = TRUE;
5239 n_skip = 1;
5240 }
5241# ifdef FEAT_MBYTE
5242 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005243 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005244 {
5245 mb_utf8 = TRUE;
5246 u8cc[0] = 0;
5247 c = 0xc0;
5248 }
5249 else
5250 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5251# endif
5252 }
5253 else
5254 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005255 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005256 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005257 }
5258#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 }
5260
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005261#ifdef FEAT_CONCEAL
5262 /* In the cursor line and we may be concealing characters: correct
5263 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005264 if (!did_wcol && draw_state == WL_LINE
5265 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005266 && conceal_cursor_line(wp)
5267 && (int)wp->w_virtcol <= vcol + n_skip)
5268 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005269# ifdef FEAT_RIGHTLEFT
5270 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005271 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005272 else
5273# endif
5274 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005275 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005276 did_wcol = TRUE;
5277 }
5278#endif
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 /* Don't override visual selection highlighting. */
5281 if (n_attr > 0
5282 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005283 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 char_attr = extra_attr;
5285
Bram Moolenaar81695252004-12-29 20:58:21 +00005286#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 /* XIM don't send preedit_start and preedit_end, but they send
5288 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5289 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005290 if (p_imst == IM_ON_THE_SPOT
5291 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005292 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 && (State & INSERT)
5294 && !p_imdisable
5295 && im_is_preediting()
5296 && draw_state == WL_LINE)
5297 {
5298 colnr_T tcol;
5299
5300 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005301 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 else
5303 tcol = preedit_end_col;
5304 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5305 {
5306 if (feedback_old_attr < 0)
5307 {
5308 feedback_col = 0;
5309 feedback_old_attr = char_attr;
5310 }
5311 char_attr = im_get_feedback_attr(feedback_col);
5312 if (char_attr < 0)
5313 char_attr = feedback_old_attr;
5314 feedback_col++;
5315 }
5316 else if (feedback_old_attr >= 0)
5317 {
5318 char_attr = feedback_old_attr;
5319 feedback_old_attr = -1;
5320 feedback_col = 0;
5321 }
5322 }
5323#endif
5324 /*
5325 * Handle the case where we are in column 0 but not on the first
5326 * character of the line and the user wants us to show us a
5327 * special character (via 'listchars' option "precedes:<char>".
5328 */
5329 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005330 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5332#ifdef FEAT_DIFF
5333 && filler_todo <= 0
5334#endif
5335 && draw_state > WL_NR
5336 && c != NUL)
5337 {
5338 c = lcs_prec;
5339 lcs_prec_todo = NUL;
5340#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005341 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5342 {
5343 /* Double-width character being overwritten by the "precedes"
5344 * character, need to fill up half the character. */
5345 c_extra = MB_FILLER_CHAR;
5346 n_extra = 1;
5347 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005348 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005351 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
5353 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005354 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005355 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357 else
5358 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5359#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005360 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005363 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 n_attr3 = 1;
5365 }
5366 }
5367
5368 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005369 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005371 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005372#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005373 || did_line_attr == 1
5374#endif
5375 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005377#ifdef FEAT_SEARCH_EXTRA
5378 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005379
5380 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005381 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005382 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005383#endif
5384
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005385 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 * highlight match at end of line. If it's beyond the last
5387 * char on the screen, just overwrite that one (tricky!) Not
5388 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005389#ifdef FEAT_SEARCH_EXTRA
5390 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005391 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005392 prevcol_hl_flag = TRUE;
5393 else
5394 {
5395 cur = wp->w_match_head;
5396 while (cur != NULL)
5397 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005398 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005399 {
5400 prevcol_hl_flag = TRUE;
5401 break;
5402 }
5403 cur = cur->next;
5404 }
5405 }
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005408 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005409 && (VIsual_mode != Ctrl_V
5410 || lnum == VIsual.lnum
5411 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005412 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#ifdef FEAT_SEARCH_EXTRA
5414 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005415 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005416# ifdef FEAT_SYN_HL
5417 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5418 && !(wp == curwin && VIsual_active))
5419# endif
5420# ifdef FEAT_DIFF
5421 && diff_hlf == (hlf_T)0
5422# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005423# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005424 && did_line_attr <= 1
5425# endif
5426 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427#endif
5428 ))
5429 {
5430 int n = 0;
5431
5432#ifdef FEAT_RIGHTLEFT
5433 if (wp->w_p_rl)
5434 {
5435 if (col < 0)
5436 n = 1;
5437 }
5438 else
5439#endif
5440 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005441 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 n = -1;
5443 }
5444 if (n != 0)
5445 {
5446 /* At the window boundary, highlight the last character
5447 * instead (better than nothing). */
5448 off += n;
5449 col += n;
5450 }
5451 else
5452 {
5453 /* Add a blank character to highlight. */
5454 ScreenLines[off] = ' ';
5455#ifdef FEAT_MBYTE
5456 if (enc_utf8)
5457 ScreenLinesUC[off] = 0;
5458#endif
5459 }
5460#ifdef FEAT_SEARCH_EXTRA
5461 if (area_attr == 0)
5462 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005463 /* Use attributes from match with highest priority among
5464 * 'search_hl' and the match list. */
5465 char_attr = search_hl.attr;
5466 cur = wp->w_match_head;
5467 shl_flag = FALSE;
5468 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005469 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005470 if (shl_flag == FALSE
5471 && ((cur != NULL
5472 && cur->priority > SEARCH_HL_PRIORITY)
5473 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005474 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005475 shl = &search_hl;
5476 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005477 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005478 else
5479 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005480 if ((ptr - line) - 1 == (long)shl->startcol
5481 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005482 char_attr = shl->attr;
5483 if (shl != &search_hl && cur != NULL)
5484 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487#endif
5488 ScreenAttrs[off] = char_attr;
5489#ifdef FEAT_RIGHTLEFT
5490 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005493 --off;
5494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 else
5496#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005499 ++off;
5500 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005501 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005502#ifdef FEAT_SYN_HL
5503 eol_hl_off = 1;
5504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
Bram Moolenaar91170f82006-05-05 21:15:17 +00005508 /*
5509 * At end of the text line.
5510 */
5511 if (c == NUL)
5512 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005513#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005514 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005515 if (wp->w_p_wrap)
5516 v = wp->w_skipcol;
5517 else
5518 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005519
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005520 /* check if line ends before left margin */
5521 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005522 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005523#ifdef FEAT_CONCEAL
5524 /* Get rid of the boguscols now, we want to draw until the right
5525 * edge for 'cursorcolumn'. */
5526 col -= boguscols;
5527 boguscols = 0;
5528#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529
5530 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005531 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532
5533 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005534 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5535 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005536 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005537 && lnum != wp->w_cursor.lnum)
5538 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005539# ifdef FEAT_RIGHTLEFT
5540 && !wp->w_p_rl
5541# endif
5542 )
5543 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005544 int rightmost_vcol = 0;
5545 int i;
5546
5547 if (wp->w_p_cuc)
5548 rightmost_vcol = wp->w_virtcol;
5549 if (draw_color_col)
5550 /* determine rightmost colorcolumn to possibly draw */
5551 for (i = 0; color_cols[i] >= 0; ++i)
5552 if (rightmost_vcol < color_cols[i])
5553 rightmost_vcol = color_cols[i];
5554
Bram Moolenaar02631462017-09-22 15:20:32 +02005555 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005556 {
5557 ScreenLines[off] = ' ';
5558#ifdef FEAT_MBYTE
5559 if (enc_utf8)
5560 ScreenLinesUC[off] = 0;
5561#endif
5562 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005563 if (draw_color_col)
5564 draw_color_col = advance_color_col(VCOL_HLC,
5565 &color_cols);
5566
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005567 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005568 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005569 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005570 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005571 else
5572 ScreenAttrs[off++] = 0;
5573
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005574 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005575 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005576
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005577 ++vcol;
5578 }
5579 }
5580#endif
5581
Bram Moolenaar53f81742017-09-22 14:35:51 +02005582 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005583 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 row++;
5585
5586 /*
5587 * Update w_cline_height and w_cline_folded if the cursor line was
5588 * updated (saves a call to plines() later).
5589 */
5590 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5591 {
5592 curwin->w_cline_row = startrow;
5593 curwin->w_cline_height = row - startrow;
5594#ifdef FEAT_FOLDING
5595 curwin->w_cline_folded = FALSE;
5596#endif
5597 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5598 }
5599
5600 break;
5601 }
5602
5603 /* line continues beyond line end */
5604 if (lcs_ext
5605 && !wp->w_p_wrap
5606#ifdef FEAT_DIFF
5607 && filler_todo <= 0
5608#endif
5609 && (
5610#ifdef FEAT_RIGHTLEFT
5611 wp->w_p_rl ? col == 0 :
5612#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005613 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005615 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5617 {
5618 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005619 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#ifdef FEAT_MBYTE
5621 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005622 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
5624 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005625 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005626 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
5628 else
5629 mb_utf8 = FALSE;
5630#endif
5631 }
5632
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005633#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634 /* advance to the next 'colorcolumn' */
5635 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005636 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005638 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639 * highlight the cursor position itself.
5640 * Also highlight the 'colorcolumn' if it is different than
5641 * 'cursorcolumn' */
5642 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005643 if (draw_state == WL_LINE && !lnum_in_visual_area
5644 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005645 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005646 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005647 && lnum != wp->w_cursor.lnum)
5648 {
5649 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005650 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005651 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005652 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005653 {
5654 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005655 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005657 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005658#endif
5659
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 /*
5661 * Store character to be displayed.
5662 * Skip characters that are left of the screen for 'nowrap'.
5663 */
5664 vcol_prev = vcol;
5665 if (draw_state < WL_LINE || n_skip <= 0)
5666 {
5667 /*
5668 * Store the character.
5669 */
5670#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5671 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5672 {
5673 /* A double-wide character is: put first halve in left cell. */
5674 --off;
5675 --col;
5676 }
5677#endif
5678 ScreenLines[off] = c;
5679#ifdef FEAT_MBYTE
5680 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005681 {
5682 if ((mb_c & 0xff00) == 0x8e00)
5683 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else if (enc_utf8)
5687 {
5688 if (mb_utf8)
5689 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005690 int i;
5691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005693 if ((c & 0xff) == 0)
5694 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005695 for (i = 0; i < Screen_mco; ++i)
5696 {
5697 ScreenLinesC[i][off] = u8cc[i];
5698 if (u8cc[i] == 0)
5699 break;
5700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 else
5703 ScreenLinesUC[off] = 0;
5704 }
5705 if (multi_attr)
5706 {
5707 ScreenAttrs[off] = multi_attr;
5708 multi_attr = 0;
5709 }
5710 else
5711#endif
5712 ScreenAttrs[off] = char_attr;
5713
5714#ifdef FEAT_MBYTE
5715 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5716 {
5717 /* Need to fill two screen columns. */
5718 ++off;
5719 ++col;
5720 if (enc_utf8)
5721 /* UTF-8: Put a 0 in the second screen char. */
5722 ScreenLines[off] = 0;
5723 else
5724 /* DBCS: Put second byte in the second screen char. */
5725 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005726 if (draw_state > WL_NR
5727#ifdef FEAT_DIFF
5728 && filler_todo <= 0
5729#endif
5730 )
5731 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 /* When "tocol" is halfway a character, set it to the end of
5733 * the character, otherwise highlighting won't stop. */
5734 if (tocol == vcol)
5735 ++tocol;
5736#ifdef FEAT_RIGHTLEFT
5737 if (wp->w_p_rl)
5738 {
5739 /* now it's time to backup one cell */
5740 --off;
5741 --col;
5742 }
5743#endif
5744 }
5745#endif
5746#ifdef FEAT_RIGHTLEFT
5747 if (wp->w_p_rl)
5748 {
5749 --off;
5750 --col;
5751 }
5752 else
5753#endif
5754 {
5755 ++off;
5756 ++col;
5757 }
5758 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005759#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005760 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005761 {
5762 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005763 ++vcol_off;
5764 if (n_extra > 0)
5765 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005766 if (wp->w_p_wrap)
5767 {
5768 /*
5769 * Special voodoo required if 'wrap' is on.
5770 *
5771 * Advance the column indicator to force the line
5772 * drawing to wrap early. This will make the line
5773 * take up the same screen space when parts are concealed,
5774 * so that cursor line computations aren't messed up.
5775 *
5776 * To avoid the fictitious advance of 'col' causing
5777 * trailing junk to be written out of the screen line
5778 * we are building, 'boguscols' keeps track of the number
5779 * of bad columns we have advanced.
5780 */
5781 if (n_extra > 0)
5782 {
5783 vcol += n_extra;
5784# ifdef FEAT_RIGHTLEFT
5785 if (wp->w_p_rl)
5786 {
5787 col -= n_extra;
5788 boguscols -= n_extra;
5789 }
5790 else
5791# endif
5792 {
5793 col += n_extra;
5794 boguscols += n_extra;
5795 }
5796 n_extra = 0;
5797 n_attr = 0;
5798 }
5799
5800
5801# ifdef FEAT_MBYTE
5802 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5803 {
5804 /* Need to fill two screen columns. */
5805# ifdef FEAT_RIGHTLEFT
5806 if (wp->w_p_rl)
5807 {
5808 --boguscols;
5809 --col;
5810 }
5811 else
5812# endif
5813 {
5814 ++boguscols;
5815 ++col;
5816 }
5817 }
5818# endif
5819
5820# ifdef FEAT_RIGHTLEFT
5821 if (wp->w_p_rl)
5822 {
5823 --boguscols;
5824 --col;
5825 }
5826 else
5827# endif
5828 {
5829 ++boguscols;
5830 ++col;
5831 }
5832 }
5833 else
5834 {
5835 if (n_extra > 0)
5836 {
5837 vcol += n_extra;
5838 n_extra = 0;
5839 n_attr = 0;
5840 }
5841 }
5842
5843 }
5844#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 else
5846 --n_skip;
5847
Bram Moolenaar64486672010-05-16 15:46:46 +02005848 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5849 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005850 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#ifdef FEAT_DIFF
5852 && filler_todo <= 0
5853#endif
5854 )
5855 ++vcol;
5856
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005857#ifdef FEAT_SYN_HL
5858 if (vcol_save_attr >= 0)
5859 char_attr = vcol_save_attr;
5860#endif
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 /* restore attributes after "predeces" in 'listchars' */
5863 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5864 char_attr = saved_attr3;
5865
5866 /* restore attributes after last 'listchars' or 'number' char */
5867 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5868 char_attr = saved_attr2;
5869
5870 /*
5871 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005872 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 */
5874 if ((
5875#ifdef FEAT_RIGHTLEFT
5876 wp->w_p_rl ? (col < 0) :
5877#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005878 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 && (*ptr != NUL
5880#ifdef FEAT_DIFF
5881 || filler_todo > 0
5882#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005883 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5885 )
5886 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005887#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005888 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005889 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005890 boguscols = 0;
5891#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005892 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005893 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 ++row;
5896 ++screen_row;
5897
5898 /* When not wrapping and finished diff lines, or when displayed
5899 * '$' and highlighting until last column, break here. */
5900 if ((!wp->w_p_wrap
5901#ifdef FEAT_DIFF
5902 && filler_todo <= 0
5903#endif
5904 ) || lcs_eol_one == -1)
5905 break;
5906
5907 /* When the window is too narrow draw all "@" lines. */
5908 if (draw_state != WL_LINE
5909#ifdef FEAT_DIFF
5910 && filler_todo <= 0
5911#endif
5912 )
5913 {
5914 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 row = endrow;
5917 }
5918
5919 /* When line got too long for screen break here. */
5920 if (row == endrow)
5921 {
5922 ++row;
5923 break;
5924 }
5925
5926 if (screen_cur_row == screen_row - 1
5927#ifdef FEAT_DIFF
5928 && filler_todo <= 0
5929#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005930 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
5932 /* Remember that the line wraps, used for modeless copy. */
5933 LineWraps[screen_row - 1] = TRUE;
5934
5935 /*
5936 * Special trick to make copy/paste of wrapped lines work with
5937 * xterm/screen: write an extra character beyond the end of
5938 * the line. This will work with all terminal types
5939 * (regardless of the xn,am settings).
5940 * Only do this on a fast tty.
5941 * Only do this if the cursor is on the current line
5942 * (something has been written in it).
5943 * Don't do this for the GUI.
5944 * Don't do this for double-width characters.
5945 * Don't do this for a window not at the right screen border.
5946 */
5947 if (p_tf
5948#ifdef FEAT_GUI
5949 && !gui.in_use
5950#endif
5951#ifdef FEAT_MBYTE
5952 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005953 && ((*mb_off2cells)(LineOffset[screen_row],
5954 LineOffset[screen_row] + screen_Columns)
5955 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005957 + (int)Columns - 2,
5958 LineOffset[screen_row] + screen_Columns)
5959 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#endif
5961 )
5962 {
5963 /* First make sure we are at the end of the screen line,
5964 * then output the same character again to let the
5965 * terminal know about the wrap. If the terminal doesn't
5966 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005967 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 screen_char(LineOffset[screen_row - 1]
5969 + (unsigned)Columns - 1,
5970 screen_row - 1, (int)(Columns - 1));
5971
5972#ifdef FEAT_MBYTE
5973 /* When there is a multi-byte character, just output a
5974 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005975 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5976 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 out_char(' ');
5978 else
5979#endif
5980 out_char(ScreenLines[LineOffset[screen_row - 1]
5981 + (Columns - 1)]);
5982 /* force a redraw of the first char on the next line */
5983 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5984 screen_start(); /* don't know where cursor is now */
5985 }
5986 }
5987
5988 col = 0;
5989 off = (unsigned)(current_ScreenLine - ScreenLines);
5990#ifdef FEAT_RIGHTLEFT
5991 if (wp->w_p_rl)
5992 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005993 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 off += col;
5995 }
5996#endif
5997
5998 /* reset the drawing state for the start of a wrapped line */
5999 draw_state = WL_START;
6000 saved_n_extra = n_extra;
6001 saved_p_extra = p_extra;
6002 saved_c_extra = c_extra;
6003 saved_char_attr = char_attr;
6004 n_extra = 0;
6005 lcs_prec_todo = lcs_prec;
6006#ifdef FEAT_LINEBREAK
6007# ifdef FEAT_DIFF
6008 if (filler_todo <= 0)
6009# endif
6010 need_showbreak = TRUE;
6011#endif
6012#ifdef FEAT_DIFF
6013 --filler_todo;
6014 /* When the filler lines are actually below the last line of the
6015 * file, don't draw the line itself, break here. */
6016 if (filler_todo == 0 && wp->w_botfill)
6017 break;
6018#endif
6019 }
6020
6021 } /* for every character in the line */
6022
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006023#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006024 /* After an empty line check first word for capital. */
6025 if (*skipwhite(line) == NUL)
6026 {
6027 capcol_lnum = lnum + 1;
6028 cap_col = 0;
6029 }
6030#endif
6031
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006032 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 return row;
6034}
6035
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006036#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006037static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038
6039/*
6040 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006041 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042 */
6043 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006044comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006045{
6046 int i;
6047
6048 for (i = 0; i < Screen_mco; ++i)
6049 {
6050 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6051 return TRUE;
6052 if (ScreenLinesC[i][off_from] == 0)
6053 break;
6054 }
6055 return FALSE;
6056}
6057#endif
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059/*
6060 * Check whether the given character needs redrawing:
6061 * - the (first byte of the) character is different
6062 * - the attributes are different
6063 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006064 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 */
6066 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006067char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 if (cols > 0
6070 && ((ScreenLines[off_from] != ScreenLines[off_to]
6071 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6072
6073#ifdef FEAT_MBYTE
6074 || (enc_dbcs != 0
6075 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6076 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6077 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6078 : (cols > 1 && ScreenLines[off_from + 1]
6079 != ScreenLines[off_to + 1])))
6080 || (enc_utf8
6081 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6082 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006083 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006084 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6085 && ScreenLines[off_from + 1]
6086 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#endif
6088 ))
6089 return TRUE;
6090 return FALSE;
6091}
6092
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006093#if defined(FEAT_TERMINAL) || defined(PROTO)
6094/*
6095 * Return the index in ScreenLines[] for the current screen line.
6096 */
6097 int
6098screen_get_current_line_off()
6099{
6100 return (int)(current_ScreenLine - ScreenLines);
6101}
6102#endif
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104/*
6105 * Move one "cooked" screen line to the screen, but only the characters that
6106 * have actually changed. Handle insert/delete character.
6107 * "coloff" gives the first column on the screen for this line.
6108 * "endcol" gives the columns where valid characters are.
6109 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6110 * needs to be cleared, negative otherwise.
6111 * "rlflag" is TRUE in a rightleft window:
6112 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6113 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6114 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006116screen_line(
6117 int row,
6118 int coloff,
6119 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006120 int clear_width,
6121 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 unsigned off_from;
6124 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006125#ifdef FEAT_MBYTE
6126 unsigned max_off_from;
6127 unsigned max_off_to;
6128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int force = FALSE; /* force update rest of the line */
6132 int redraw_this /* bool: does character need redraw? */
6133#ifdef FEAT_GUI
6134 = TRUE /* For GUI when while-loop empty */
6135#endif
6136 ;
6137 int redraw_next; /* redraw_this for next character */
6138#ifdef FEAT_MBYTE
6139 int clear_next = FALSE;
6140 int char_cells; /* 1: normal char */
6141 /* 2: occupies two display cells */
6142# define CHAR_CELLS char_cells
6143#else
6144# define CHAR_CELLS 1
6145#endif
6146
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006147 /* Check for illegal row and col, just in case. */
6148 if (row >= Rows)
6149 row = Rows - 1;
6150 if (endcol > Columns)
6151 endcol = Columns;
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153# ifdef FEAT_CLIPBOARD
6154 clip_may_clear_selection(row, row);
6155# endif
6156
6157 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6158 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006159#ifdef FEAT_MBYTE
6160 max_off_from = off_from + screen_Columns;
6161 max_off_to = LineOffset[row] + screen_Columns;
6162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164#ifdef FEAT_RIGHTLEFT
6165 if (rlflag)
6166 {
6167 /* Clear rest first, because it's left of the text. */
6168 if (clear_width > 0)
6169 {
6170 while (col <= endcol && ScreenLines[off_to] == ' '
6171 && ScreenAttrs[off_to] == 0
6172# ifdef FEAT_MBYTE
6173 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6174# endif
6175 )
6176 {
6177 ++off_to;
6178 ++col;
6179 }
6180 if (col <= endcol)
6181 screen_fill(row, row + 1, col + coloff,
6182 endcol + coloff + 1, ' ', ' ', 0);
6183 }
6184 col = endcol + 1;
6185 off_to = LineOffset[row] + col + coloff;
6186 off_from += col;
6187 endcol = (clear_width > 0 ? clear_width : -clear_width);
6188 }
6189#endif /* FEAT_RIGHTLEFT */
6190
6191 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6192
6193 while (col < endcol)
6194 {
6195#ifdef FEAT_MBYTE
6196 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006197 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 else
6199 char_cells = 1;
6200#endif
6201
6202 redraw_this = redraw_next;
6203 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6204 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6205
6206#ifdef FEAT_GUI
6207 /* If the next character was bold, then redraw the current character to
6208 * remove any pixels that might have spilt over into us. This only
6209 * happens in the GUI.
6210 */
6211 if (redraw_next && gui.in_use)
6212 {
6213 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006214 if (hl > HL_ALL)
6215 hl = syn_attr2attr(hl);
6216 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 redraw_this = TRUE;
6218 }
6219#endif
6220
6221 if (redraw_this)
6222 {
6223 /*
6224 * Special handling when 'xs' termcap flag set (hpterm):
6225 * Attributes for characters are stored at the position where the
6226 * cursor is when writing the highlighting code. The
6227 * start-highlighting code must be written with the cursor on the
6228 * first highlighted character. The stop-highlighting code must
6229 * be written with the cursor just after the last highlighted
6230 * character.
6231 * Overwriting a character doesn't remove it's highlighting. Need
6232 * to clear the rest of the line, and force redrawing it
6233 * completely.
6234 */
6235 if ( p_wiv
6236 && !force
6237#ifdef FEAT_GUI
6238 && !gui.in_use
6239#endif
6240 && ScreenAttrs[off_to] != 0
6241 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6242 {
6243 /*
6244 * Need to remove highlighting attributes here.
6245 */
6246 windgoto(row, col + coloff);
6247 out_str(T_CE); /* clear rest of this screen line */
6248 screen_start(); /* don't know where cursor is now */
6249 force = TRUE; /* force redraw of rest of the line */
6250 redraw_next = TRUE; /* or else next char would miss out */
6251
6252 /*
6253 * If the previous character was highlighted, need to stop
6254 * highlighting at this character.
6255 */
6256 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6257 {
6258 screen_attr = ScreenAttrs[off_to - 1];
6259 term_windgoto(row, col + coloff);
6260 screen_stop_highlight();
6261 }
6262 else
6263 screen_attr = 0; /* highlighting has stopped */
6264 }
6265#ifdef FEAT_MBYTE
6266 if (enc_dbcs != 0)
6267 {
6268 /* Check if overwriting a double-byte with a single-byte or
6269 * the other way around requires another character to be
6270 * redrawn. For UTF-8 this isn't needed, because comparing
6271 * ScreenLinesUC[] is sufficient. */
6272 if (char_cells == 1
6273 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006274 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
6276 /* Writing a single-cell character over a double-cell
6277 * character: need to redraw the next cell. */
6278 ScreenLines[off_to + 1] = 0;
6279 redraw_next = TRUE;
6280 }
6281 else if (char_cells == 2
6282 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006283 && (*mb_off2cells)(off_to, max_off_to) == 1
6284 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
6286 /* Writing the second half of a double-cell character over
6287 * a double-cell character: need to redraw the second
6288 * cell. */
6289 ScreenLines[off_to + 2] = 0;
6290 redraw_next = TRUE;
6291 }
6292
6293 if (enc_dbcs == DBCS_JPNU)
6294 ScreenLines2[off_to] = ScreenLines2[off_from];
6295 }
6296 /* When writing a single-width character over a double-width
6297 * character and at the end of the redrawn text, need to clear out
6298 * the right halve of the old character.
6299 * Also required when writing the right halve of a double-width
6300 * char over the left halve of an existing one. */
6301 if (has_mbyte && col + char_cells == endcol
6302 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006303 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006305 && (*mb_off2cells)(off_to, max_off_to) == 1
6306 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 clear_next = TRUE;
6308#endif
6309
6310 ScreenLines[off_to] = ScreenLines[off_from];
6311#ifdef FEAT_MBYTE
6312 if (enc_utf8)
6313 {
6314 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6315 if (ScreenLinesUC[off_from] != 0)
6316 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006317 int i;
6318
6319 for (i = 0; i < Screen_mco; ++i)
6320 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 }
6322 }
6323 if (char_cells == 2)
6324 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6325#endif
6326
6327#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006328 /* The bold trick makes a single column of pixels appear in the
6329 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 * character should be redrawn too. This happens for our own GUI
6331 * and for some xterms. */
6332 if (
6333# ifdef FEAT_GUI
6334 gui.in_use
6335# endif
6336# if defined(FEAT_GUI) && defined(UNIX)
6337 ||
6338# endif
6339# ifdef UNIX
6340 term_is_xterm
6341# endif
6342 )
6343 {
6344 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006345 if (hl > HL_ALL)
6346 hl = syn_attr2attr(hl);
6347 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 redraw_next = TRUE;
6349 }
6350#endif
6351 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6352#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006353 /* For simplicity set the attributes of second half of a
6354 * double-wide character equal to the first half. */
6355 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006357
6358 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 else
6361#endif
6362 screen_char(off_to, row, col + coloff);
6363 }
6364 else if ( p_wiv
6365#ifdef FEAT_GUI
6366 && !gui.in_use
6367#endif
6368 && col + coloff > 0)
6369 {
6370 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6371 {
6372 /*
6373 * Don't output stop-highlight when moving the cursor, it will
6374 * stop the highlighting when it should continue.
6375 */
6376 screen_attr = 0;
6377 }
6378 else if (screen_attr != 0)
6379 screen_stop_highlight();
6380 }
6381
6382 off_to += CHAR_CELLS;
6383 off_from += CHAR_CELLS;
6384 col += CHAR_CELLS;
6385 }
6386
6387#ifdef FEAT_MBYTE
6388 if (clear_next)
6389 {
6390 /* Clear the second half of a double-wide character of which the left
6391 * half was overwritten with a single-wide character. */
6392 ScreenLines[off_to] = ' ';
6393 if (enc_utf8)
6394 ScreenLinesUC[off_to] = 0;
6395 screen_char(off_to, row, col + coloff);
6396 }
6397#endif
6398
6399 if (clear_width > 0
6400#ifdef FEAT_RIGHTLEFT
6401 && !rlflag
6402#endif
6403 )
6404 {
6405#ifdef FEAT_GUI
6406 int startCol = col;
6407#endif
6408
6409 /* blank out the rest of the line */
6410 while (col < clear_width && ScreenLines[off_to] == ' '
6411 && ScreenAttrs[off_to] == 0
6412#ifdef FEAT_MBYTE
6413 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6414#endif
6415 )
6416 {
6417 ++off_to;
6418 ++col;
6419 }
6420 if (col < clear_width)
6421 {
6422#ifdef FEAT_GUI
6423 /*
6424 * In the GUI, clearing the rest of the line may leave pixels
6425 * behind if the first character cleared was bold. Some bold
6426 * fonts spill over the left. In this case we redraw the previous
6427 * character too. If we didn't skip any blanks above, then we
6428 * only redraw if the character wasn't already redrawn anyway.
6429 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006430 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
6432 hl = ScreenAttrs[off_to];
6433 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006434 {
6435 int prev_cells = 1;
6436# ifdef FEAT_MBYTE
6437 if (enc_utf8)
6438 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6439 * that its width is 2. */
6440 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6441 else if (enc_dbcs != 0)
6442 {
6443 /* find previous character by counting from first
6444 * column and get its width. */
6445 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006446 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006447
6448 while (off < off_to)
6449 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006450 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006451 off += prev_cells;
6452 }
6453 }
6454
6455 if (enc_dbcs != 0 && prev_cells > 1)
6456 screen_char_2(off_to - prev_cells, row,
6457 col + coloff - prev_cells);
6458 else
6459# endif
6460 screen_char(off_to - prev_cells, row,
6461 col + coloff - prev_cells);
6462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 }
6464#endif
6465 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6466 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 off_to += clear_width - col;
6468 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470 }
6471
6472 if (clear_width > 0)
6473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 /* For a window that's left of another, draw the separator char. */
6475 if (col + coloff < Columns)
6476 {
6477 int c;
6478
6479 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006480 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006481#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006482 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6483 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 || ScreenAttrs[off_to] != hl)
6486 {
6487 ScreenLines[off_to] = c;
6488 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006489#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (enc_utf8)
6491 {
6492 if (c >= 0x80)
6493 {
6494 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006495 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497 else
6498 ScreenLinesUC[off_to] = 0;
6499 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 screen_char(off_to, row, col + coloff);
6502 }
6503 }
6504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 LineWraps[row] = FALSE;
6506 }
6507}
6508
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006509#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006511 * Mirror text "str" for right-left displaying.
6512 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006514 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006515rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 char_u *p1, *p2;
6518 int t;
6519
6520 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6521 {
6522 t = *p1;
6523 *p1 = *p2;
6524 *p2 = t;
6525 }
6526}
6527#endif
6528
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529/*
6530 * mark all status lines for redraw; used after first :cd
6531 */
6532 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006533status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 win_T *wp;
6536
Bram Moolenaar29323592016-07-24 22:04:11 +02006537 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (wp->w_status_height)
6539 {
6540 wp->w_redr_status = TRUE;
6541 redraw_later(VALID);
6542 }
6543}
6544
6545/*
6546 * mark all status lines of the current buffer for redraw
6547 */
6548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006549status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 win_T *wp;
6552
Bram Moolenaar29323592016-07-24 22:04:11 +02006553 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6555 {
6556 wp->w_redr_status = TRUE;
6557 redraw_later(VALID);
6558 }
6559}
6560
6561/*
6562 * Redraw all status lines that need to be redrawn.
6563 */
6564 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006565redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 win_T *wp;
6568
Bram Moolenaar29323592016-07-24 22:04:11 +02006569 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006571 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006572 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006573 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaar4033c552017-09-16 20:54:51 +02006576#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577/*
6578 * Redraw all status lines at the bottom of frame "frp".
6579 */
6580 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006581win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 if (frp->fr_layout == FR_LEAF)
6584 frp->fr_win->w_redr_status = TRUE;
6585 else if (frp->fr_layout == FR_ROW)
6586 {
6587 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6588 win_redraw_last_status(frp);
6589 }
6590 else /* frp->fr_layout == FR_COL */
6591 {
6592 frp = frp->fr_child;
6593 while (frp->fr_next != NULL)
6594 frp = frp->fr_next;
6595 win_redraw_last_status(frp);
6596 }
6597}
6598#endif
6599
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600/*
6601 * Draw the verticap separator right of window "wp" starting with line "row".
6602 */
6603 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006604draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 int hl;
6607 int c;
6608
6609 if (wp->w_vsep_width)
6610 {
6611 /* draw the vertical separator right of this window */
6612 c = fillchar_vsep(&hl);
6613 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6614 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6615 c, ' ', hl);
6616 }
6617}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006620static int status_match_len(expand_T *xp, char_u *s);
6621static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622
6623/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006624 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 */
6626 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006627status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 int len = 0;
6630
6631#ifdef FEAT_MENU
6632 int emenu = (xp->xp_context == EXPAND_MENUS
6633 || xp->xp_context == EXPAND_MENUNAMES);
6634
6635 /* Check for menu separators - replace with '|'. */
6636 if (emenu && menu_is_separator(s))
6637 return 1;
6638#endif
6639
6640 while (*s != NUL)
6641 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006642 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006643 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006644 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
6646
6647 return len;
6648}
6649
6650/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006651 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006652 * These are backslashes used for escaping. Do show backslashes in help tags.
6653 */
6654 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006655skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006656{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006657 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006658#ifdef FEAT_MENU
6659 || ((xp->xp_context == EXPAND_MENUS
6660 || xp->xp_context == EXPAND_MENUNAMES)
6661 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6662#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006663 )
6664 {
6665#ifndef BACKSLASH_IN_FILENAME
6666 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6667 return 2;
6668#endif
6669 return 1;
6670 }
6671 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006672}
6673
6674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 * Show wildchar matches in the status line.
6676 * Show at least the "match" item.
6677 * We start at item 'first_match' in the list and show all matches that fit.
6678 *
6679 * If inversion is possible we use it. Else '=' characters are used.
6680 */
6681 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006682win_redr_status_matches(
6683 expand_T *xp,
6684 int num_matches,
6685 char_u **matches, /* list of matches */
6686 int match,
6687 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6690 int row;
6691 char_u *buf;
6692 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006693 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 int fillchar;
6695 int attr;
6696 int i;
6697 int highlight = TRUE;
6698 char_u *selstart = NULL;
6699 int selstart_col = 0;
6700 char_u *selend = NULL;
6701 static int first_match = 0;
6702 int add_left = FALSE;
6703 char_u *s;
6704#ifdef FEAT_MENU
6705 int emenu;
6706#endif
6707#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6708 int l;
6709#endif
6710
6711 if (matches == NULL) /* interrupted completion? */
6712 return;
6713
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006714#ifdef FEAT_MBYTE
6715 if (has_mbyte)
6716 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6717 else
6718#endif
6719 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (buf == NULL)
6721 return;
6722
6723 if (match == -1) /* don't show match but original text */
6724 {
6725 match = 0;
6726 highlight = FALSE;
6727 }
6728 /* count 1 for the ending ">" */
6729 clen = status_match_len(xp, L_MATCH(match)) + 3;
6730 if (match == 0)
6731 first_match = 0;
6732 else if (match < first_match)
6733 {
6734 /* jumping left, as far as we can go */
6735 first_match = match;
6736 add_left = TRUE;
6737 }
6738 else
6739 {
6740 /* check if match fits on the screen */
6741 for (i = first_match; i < match; ++i)
6742 clen += status_match_len(xp, L_MATCH(i)) + 2;
6743 if (first_match > 0)
6744 clen += 2;
6745 /* jumping right, put match at the left */
6746 if ((long)clen > Columns)
6747 {
6748 first_match = match;
6749 /* if showing the last match, we can add some on the left */
6750 clen = 2;
6751 for (i = match; i < num_matches; ++i)
6752 {
6753 clen += status_match_len(xp, L_MATCH(i)) + 2;
6754 if ((long)clen >= Columns)
6755 break;
6756 }
6757 if (i == num_matches)
6758 add_left = TRUE;
6759 }
6760 }
6761 if (add_left)
6762 while (first_match > 0)
6763 {
6764 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6765 if ((long)clen >= Columns)
6766 break;
6767 --first_match;
6768 }
6769
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006770 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772 if (first_match == 0)
6773 {
6774 *buf = NUL;
6775 len = 0;
6776 }
6777 else
6778 {
6779 STRCPY(buf, "< ");
6780 len = 2;
6781 }
6782 clen = len;
6783
6784 i = first_match;
6785 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6786 {
6787 if (i == match)
6788 {
6789 selstart = buf + len;
6790 selstart_col = clen;
6791 }
6792
6793 s = L_MATCH(i);
6794 /* Check for menu separators - replace with '|' */
6795#ifdef FEAT_MENU
6796 emenu = (xp->xp_context == EXPAND_MENUS
6797 || xp->xp_context == EXPAND_MENUNAMES);
6798 if (emenu && menu_is_separator(s))
6799 {
6800 STRCPY(buf + len, transchar('|'));
6801 l = (int)STRLEN(buf + len);
6802 len += l;
6803 clen += l;
6804 }
6805 else
6806#endif
6807 for ( ; *s != NUL; ++s)
6808 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006809 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 clen += ptr2cells(s);
6811#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006812 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
6814 STRNCPY(buf + len, s, l);
6815 s += l - 1;
6816 len += l;
6817 }
6818 else
6819#endif
6820 {
6821 STRCPY(buf + len, transchar_byte(*s));
6822 len += (int)STRLEN(buf + len);
6823 }
6824 }
6825 if (i == match)
6826 selend = buf + len;
6827
6828 *(buf + len++) = ' ';
6829 *(buf + len++) = ' ';
6830 clen += 2;
6831 if (++i == num_matches)
6832 break;
6833 }
6834
6835 if (i != num_matches)
6836 {
6837 *(buf + len++) = '>';
6838 ++clen;
6839 }
6840
6841 buf[len] = NUL;
6842
6843 row = cmdline_row - 1;
6844 if (row >= 0)
6845 {
6846 if (wild_menu_showing == 0)
6847 {
6848 if (msg_scrolled > 0)
6849 {
6850 /* Put the wildmenu just above the command line. If there is
6851 * no room, scroll the screen one line up. */
6852 if (cmdline_row == Rows - 1)
6853 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006854 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 ++msg_scrolled;
6856 }
6857 else
6858 {
6859 ++cmdline_row;
6860 ++row;
6861 }
6862 wild_menu_showing = WM_SCROLLED;
6863 }
6864 else
6865 {
6866 /* Create status line if needed by setting 'laststatus' to 2.
6867 * Set 'winminheight' to zero to avoid that the window is
6868 * resized. */
6869 if (lastwin->w_status_height == 0)
6870 {
6871 save_p_ls = p_ls;
6872 save_p_wmh = p_wmh;
6873 p_ls = 2;
6874 p_wmh = 0;
6875 last_status(FALSE);
6876 }
6877 wild_menu_showing = WM_SHOWN;
6878 }
6879 }
6880
6881 screen_puts(buf, row, 0, attr);
6882 if (selstart != NULL && highlight)
6883 {
6884 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006885 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
6887
6888 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6889 }
6890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 vim_free(buf);
6893}
6894#endif
6895
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896/*
6897 * Redraw the status line of window wp.
6898 *
6899 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006900 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6901 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006903 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006904win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
6906 int row;
6907 char_u *p;
6908 int len;
6909 int fillchar;
6910 int attr;
6911 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006912 static int busy = FALSE;
6913
6914 /* It's possible to get here recursively when 'statusline' (indirectly)
6915 * invokes ":redrawstatus". Simply ignore the call then. */
6916 if (busy)
6917 return;
6918 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
6920 wp->w_redr_status = FALSE;
6921 if (wp->w_status_height == 0)
6922 {
6923 /* no status line, can only be last window */
6924 redraw_cmdline = TRUE;
6925 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006926 else if (!redrawing()
6927#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006928 // don't update status line when popup menu is visible and may be
6929 // drawn over it, unless it will be redrawn later
6930 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006931#endif
6932 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
6934 /* Don't redraw right now, do it later. */
6935 wp->w_redr_status = TRUE;
6936 }
6937#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006938 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
6940 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006941 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943#endif
6944 else
6945 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006946 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006948 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 p = NameBuff;
6950 len = (int)STRLEN(p);
6951
Bram Moolenaard85f2712017-07-28 21:51:57 +02006952 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953#ifdef FEAT_QUICKFIX
6954 || wp->w_p_pvw
6955#endif
6956 || bufIsChanged(wp->w_buffer)
6957 || wp->w_buffer->b_p_ro)
6958 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006959 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006961 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 len += (int)STRLEN(p + len);
6963 }
6964#ifdef FEAT_QUICKFIX
6965 if (wp->w_p_pvw)
6966 {
6967 STRCPY(p + len, _("[Preview]"));
6968 len += (int)STRLEN(p + len);
6969 }
6970#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006971 if (bufIsChanged(wp->w_buffer)
6972#ifdef FEAT_TERMINAL
6973 && !bt_terminal(wp->w_buffer)
6974#endif
6975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {
6977 STRCPY(p + len, "[+]");
6978 len += 3;
6979 }
6980 if (wp->w_buffer->b_p_ro)
6981 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006982 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006983 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985
Bram Moolenaar02631462017-09-22 15:20:32 +02006986 this_ru_col = ru_col - (Columns - wp->w_width);
6987 if (this_ru_col < (wp->w_width + 1) / 2)
6988 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 if (this_ru_col <= 1)
6990 {
6991 p = (char_u *)"<"; /* No room for file name! */
6992 len = 1;
6993 }
6994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995#ifdef FEAT_MBYTE
6996 if (has_mbyte)
6997 {
6998 int clen = 0, i;
6999
7000 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007001 clen = mb_string2cells(p, -1);
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 /* Find first character that will fit.
7004 * Going from start to end is much faster for DBCS. */
7005 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007006 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 clen -= (*mb_ptr2cells)(p + i);
7008 len = clen;
7009 if (i > 0)
7010 {
7011 p = p + i - 1;
7012 *p = '<';
7013 ++len;
7014 }
7015
7016 }
7017 else
7018#endif
7019 if (len > this_ru_col - 1)
7020 {
7021 p += len - (this_ru_col - 1);
7022 *p = '<';
7023 len = this_ru_col - 1;
7024 }
7025
7026 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007027 screen_puts(p, row, wp->w_wincol, attr);
7028 screen_fill(row, row + 1, len + wp->w_wincol,
7029 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007031 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7033 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007034 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007037 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038#endif
7039 }
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 /*
7042 * May need to draw the character below the vertical separator.
7043 */
7044 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7045 {
7046 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007047 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 else
7049 fillchar = fillchar_vsep(&attr);
7050 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7051 attr);
7052 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007053 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
Bram Moolenaar238a5642006-02-21 22:12:05 +00007056#ifdef FEAT_STL_OPT
7057/*
7058 * Redraw the status line according to 'statusline' and take care of any
7059 * errors encountered.
7060 */
7061 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007062redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007063{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007064 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007065 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007066
7067 /* When called recursively return. This can happen when the statusline
7068 * contains an expression that triggers a redraw. */
7069 if (entered)
7070 return;
7071 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007072
Bram Moolenaara742e082016-04-05 21:10:38 +02007073 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007074 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007075 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007076 {
7077 /* When there is an error disable the statusline, otherwise the
7078 * display is messed up with errors and a redraw triggers the problem
7079 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007080 set_string_option_direct((char_u *)"statusline", -1,
7081 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007082 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007083 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007084 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007085 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007086}
7087#endif
7088
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089/*
7090 * Return TRUE if the status line of window "wp" is connected to the status
7091 * line of the window right of it. If not, then it's a vertical separator.
7092 * Only call if (wp->w_vsep_width != 0).
7093 */
7094 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007095stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 frame_T *fr;
7098
7099 fr = wp->w_frame;
7100 while (fr->fr_parent != NULL)
7101 {
7102 if (fr->fr_parent->fr_layout == FR_COL)
7103 {
7104 if (fr->fr_next != NULL)
7105 break;
7106 }
7107 else
7108 {
7109 if (fr->fr_next != NULL)
7110 return TRUE;
7111 }
7112 fr = fr->fr_parent;
7113 }
7114 return FALSE;
7115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118/*
7119 * Get the value to show for the language mappings, active 'keymap'.
7120 */
7121 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007122get_keymap_str(
7123 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007124 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007125 char_u *buf, /* buffer for the result */
7126 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128 char_u *p;
7129
7130 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7131 return FALSE;
7132
7133 {
7134#ifdef FEAT_EVAL
7135 buf_T *old_curbuf = curbuf;
7136 win_T *old_curwin = curwin;
7137 char_u *s;
7138
7139 curbuf = wp->w_buffer;
7140 curwin = wp;
7141 STRCPY(buf, "b:keymap_name"); /* must be writable */
7142 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007143 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 --emsg_skip;
7145 curbuf = old_curbuf;
7146 curwin = old_curwin;
7147 if (p == NULL || *p == NUL)
7148#endif
7149 {
7150#ifdef FEAT_KEYMAP
7151 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7152 p = wp->w_buffer->b_p_keymap;
7153 else
7154#endif
7155 p = (char_u *)"lang";
7156 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007157 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 buf[0] = NUL;
7159#ifdef FEAT_EVAL
7160 vim_free(s);
7161#endif
7162 }
7163 return buf[0] != NUL;
7164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
7166#if defined(FEAT_STL_OPT) || defined(PROTO)
7167/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007168 * Redraw the status line or ruler of window "wp".
7169 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 */
7171 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007172win_redr_custom(
7173 win_T *wp,
7174 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007176 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 int attr;
7178 int curattr;
7179 int row;
7180 int col = 0;
7181 int maxwidth;
7182 int width;
7183 int n;
7184 int len;
7185 int fillchar;
7186 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007187 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007189 struct stl_hlrec hltab[STL_MAX_ITEM];
7190 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007192 win_T *ewp;
7193 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar1d633412013-12-11 15:52:01 +01007195 /* There is a tiny chance that this gets called recursively: When
7196 * redrawing a status line triggers redrawing the ruler or tabline.
7197 * Avoid trouble by not allowing recursion. */
7198 if (entered)
7199 return;
7200 entered = TRUE;
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007203 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007205 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007206 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007208 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007209 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210 maxwidth = Columns;
7211# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007212 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007215 else
7216 {
7217 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007218 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007219 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220
7221 if (draw_ruler)
7222 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007223 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007224 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007225 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007226 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007227 if (*++stl == '-')
7228 stl++;
7229 if (atoi((char *)stl))
7230 while (VIM_ISDIGIT(*stl))
7231 stl++;
7232 if (*stl++ != '(')
7233 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007234 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007235 col = ru_col - (Columns - wp->w_width);
7236 if (col < (wp->w_width + 1) / 2)
7237 col = (wp->w_width + 1) / 2;
7238 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007239 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007240 {
7241 row = Rows - 1;
7242 --maxwidth; /* writing in last column may cause scrolling */
7243 fillchar = ' ';
7244 attr = 0;
7245 }
7246
7247# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007248 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007249# endif
7250 }
7251 else
7252 {
7253 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007254 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007256 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007257# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 use_sandbox = was_set_insecurely((char_u *)"statusline",
7259 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007260# endif
7261 }
7262
Bram Moolenaar53f81742017-09-22 14:35:51 +02007263 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007264 }
7265
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007267 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
Bram Moolenaar61452852011-02-01 18:01:11 +01007269 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7270 * the cursor away and back. */
7271 ewp = wp == NULL ? curwin : wp;
7272 p_crb_save = ewp->w_p_crb;
7273 ewp->w_p_crb = FALSE;
7274
Bram Moolenaar362f3562009-11-03 16:20:34 +00007275 /* Make a copy, because the statusline may include a function call that
7276 * might change the option value and free the memory. */
7277 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007278 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007279 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007280 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007281 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007282 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007284 /* Make all characters printable. */
7285 p = transstr(buf);
7286 if (p != NULL)
7287 {
7288 vim_strncpy(buf, p, sizeof(buf) - 1);
7289 vim_free(p);
7290 }
7291
7292 /* fill up with "fillchar" */
7293 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007294 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
7296#ifdef FEAT_MBYTE
7297 len += (*mb_char2bytes)(fillchar, buf + len);
7298#else
7299 buf[len++] = fillchar;
7300#endif
7301 ++width;
7302 }
7303 buf[len] = NUL;
7304
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 /*
7306 * Draw each snippet with the specified highlighting.
7307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 curattr = attr;
7309 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007310 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007312 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 screen_puts_len(p, len, row, col, curattr);
7314 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007315 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007317 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007319 else if (hltab[n].userhl < 0)
7320 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007321#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007322 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7323 && wp->w_status_height != 0)
7324 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007325 else if (wp != NULL && bt_terminal(wp->w_buffer)
7326 && wp->w_status_height != 0)
7327 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007328#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007329 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007330 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007332 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 }
7334 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007335
7336 if (wp == NULL)
7337 {
7338 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7339 col = 0;
7340 len = 0;
7341 p = buf;
7342 fillchar = 0;
7343 for (n = 0; tabtab[n].start != NULL; n++)
7344 {
7345 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7346 while (col < len)
7347 TabPageIdxs[col++] = fillchar;
7348 p = tabtab[n].start;
7349 fillchar = tabtab[n].userhl;
7350 }
7351 while (col < Columns)
7352 TabPageIdxs[col++] = fillchar;
7353 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007354
7355theend:
7356 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357}
7358
7359#endif /* FEAT_STL_OPT */
7360
7361/*
7362 * Output a single character directly to the screen and update ScreenLines.
7363 */
7364 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007365screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 char_u buf[MB_MAXBYTES + 1];
7368
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007369#ifdef FEAT_MBYTE
7370 if (has_mbyte)
7371 buf[(*mb_char2bytes)(c, buf)] = NUL;
7372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007374 {
7375 buf[0] = c;
7376 buf[1] = NUL;
7377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 screen_puts(buf, row, col, attr);
7379}
7380
7381/*
7382 * Get a single character directly from ScreenLines into "bytes[]".
7383 * Also return its attribute in *attrp;
7384 */
7385 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007386screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 unsigned off;
7389
7390 /* safety check */
7391 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7392 {
7393 off = LineOffset[row] + col;
7394 *attrp = ScreenAttrs[off];
7395 bytes[0] = ScreenLines[off];
7396 bytes[1] = NUL;
7397
7398#ifdef FEAT_MBYTE
7399 if (enc_utf8 && ScreenLinesUC[off] != 0)
7400 bytes[utfc_char2bytes(off, bytes)] = NUL;
7401 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7402 {
7403 bytes[0] = ScreenLines[off];
7404 bytes[1] = ScreenLines2[off];
7405 bytes[2] = NUL;
7406 }
7407 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7408 {
7409 bytes[1] = ScreenLines[off + 1];
7410 bytes[2] = NUL;
7411 }
7412#endif
7413 }
7414}
7415
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007416#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007417static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007418
7419/*
7420 * Return TRUE if composing characters for screen posn "off" differs from
7421 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007422 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007423 */
7424 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007425screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007426{
7427 int i;
7428
7429 for (i = 0; i < Screen_mco; ++i)
7430 {
7431 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7432 return TRUE;
7433 if (u8cc[i] == 0)
7434 break;
7435 }
7436 return FALSE;
7437}
7438#endif
7439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440/*
7441 * Put string '*text' on the screen at position 'row' and 'col', with
7442 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7443 * Note: only outputs within one row, message is truncated at screen boundary!
7444 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7445 */
7446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007447screen_puts(
7448 char_u *text,
7449 int row,
7450 int col,
7451 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 screen_puts_len(text, -1, row, col, attr);
7454}
7455
7456/*
7457 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7458 * a NUL.
7459 */
7460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007461screen_puts_len(
7462 char_u *text,
7463 int textlen,
7464 int row,
7465 int col,
7466 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467{
7468 unsigned off;
7469 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007470 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 int c;
7472#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007473 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 int mbyte_blen = 1;
7475 int mbyte_cells = 1;
7476 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007477 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 int clear_next_cell = FALSE;
7479# ifdef FEAT_ARABIC
7480 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007481 int pc, nc, nc1;
7482 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483# endif
7484#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007485#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7486 int force_redraw_this;
7487 int force_redraw_next = FALSE;
7488#endif
7489 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
7491 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7492 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007493 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
Bram Moolenaarc236c162008-07-13 17:41:49 +00007495#ifdef FEAT_MBYTE
7496 /* When drawing over the right halve of a double-wide char clear out the
7497 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007498 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007499# ifdef FEAT_GUI
7500 && !gui.in_use
7501# endif
7502 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007503 {
7504 ScreenLines[off - 1] = ' ';
7505 ScreenAttrs[off - 1] = 0;
7506 if (enc_utf8)
7507 {
7508 ScreenLinesUC[off - 1] = 0;
7509 ScreenLinesC[0][off - 1] = 0;
7510 }
7511 /* redraw the previous cell, make it empty */
7512 screen_char(off - 1, row, col - 1);
7513 /* force the cell at "col" to be redrawn */
7514 force_redraw_next = TRUE;
7515 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007516#endif
7517
Bram Moolenaar367329b2007-08-30 11:53:22 +00007518#ifdef FEAT_MBYTE
7519 max_off = LineOffset[row] + screen_Columns;
7520#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007521 while (col < screen_Columns
7522 && (len < 0 || (int)(ptr - text) < len)
7523 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 {
7525 c = *ptr;
7526#ifdef FEAT_MBYTE
7527 /* check if this is the first byte of a multibyte */
7528 if (has_mbyte)
7529 {
7530 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007531 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007533 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7535 mbyte_cells = 1;
7536 else if (enc_dbcs != 0)
7537 mbyte_cells = mbyte_blen;
7538 else /* enc_utf8 */
7539 {
7540 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007541 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 (int)((text + len) - ptr));
7543 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007546# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 /* Non-BMP character: display as ? or fullwidth ?. */
7548 if (u8c >= 0x10000)
7549 {
7550 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7551 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007552 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007554# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555# ifdef FEAT_ARABIC
7556 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7557 {
7558 /* Do Arabic shaping. */
7559 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7560 {
7561 /* Past end of string to be displayed. */
7562 nc = NUL;
7563 nc1 = NUL;
7564 }
7565 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007566 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007567 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7568 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007569 nc1 = pcc[0];
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 pc = prev_c;
7572 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 }
7575 else
7576 prev_c = u8c;
7577# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007578 if (col + mbyte_cells > screen_Columns)
7579 {
7580 /* Only 1 cell left, but character requires 2 cells:
7581 * display a '>' in the last column to avoid wrapping. */
7582 c = '>';
7583 mbyte_cells = 1;
7584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 }
7586 }
7587#endif
7588
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007589#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7590 force_redraw_this = force_redraw_next;
7591 force_redraw_next = FALSE;
7592#endif
7593
7594 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595#ifdef FEAT_MBYTE
7596 || (mbyte_cells == 2
7597 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7598 || (enc_dbcs == DBCS_JPNU
7599 && c == 0x8e
7600 && ScreenLines2[off] != ptr[1])
7601 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007602 && (ScreenLinesUC[off] !=
7603 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7604 || (ScreenLinesUC[off] != 0
7605 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606#endif
7607 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007608 || exmode_active;
7609
7610 if (need_redraw
7611#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7612 || force_redraw_this
7613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 )
7615 {
7616#if defined(FEAT_GUI) || defined(UNIX)
7617 /* The bold trick makes a single row of pixels appear in the next
7618 * character. When a bold character is removed, the next
7619 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007620 * and for some xterms. */
7621 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622# ifdef FEAT_GUI
7623 gui.in_use
7624# endif
7625# if defined(FEAT_GUI) && defined(UNIX)
7626 ||
7627# endif
7628# ifdef UNIX
7629 term_is_xterm
7630# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007631 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007633 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 if (n > HL_ALL)
7636 n = syn_attr2attr(n);
7637 if (n & HL_BOLD)
7638 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640#endif
7641#ifdef FEAT_MBYTE
7642 /* When at the end of the text and overwriting a two-cell
7643 * character with a one-cell character, need to clear the next
7644 * cell. Also when overwriting the left halve of a two-cell char
7645 * with the right halve of a two-cell char. Do this only once
7646 * (mb_off2cells() may return 2 on the right halve). */
7647 if (clear_next_cell)
7648 clear_next_cell = FALSE;
7649 else if (has_mbyte
7650 && (len < 0 ? ptr[mbyte_blen] == NUL
7651 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007652 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007654 && (*mb_off2cells)(off, max_off) == 1
7655 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 clear_next_cell = TRUE;
7657
7658 /* Make sure we never leave a second byte of a double-byte behind,
7659 * it confuses mb_off2cells(). */
7660 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007661 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007663 && (*mb_off2cells)(off, max_off) == 1
7664 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 ScreenLines[off + mbyte_blen] = 0;
7666#endif
7667 ScreenLines[off] = c;
7668 ScreenAttrs[off] = attr;
7669#ifdef FEAT_MBYTE
7670 if (enc_utf8)
7671 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007672 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 ScreenLinesUC[off] = 0;
7674 else
7675 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007676 int i;
7677
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007679 for (i = 0; i < Screen_mco; ++i)
7680 {
7681 ScreenLinesC[i][off] = u8cc[i];
7682 if (u8cc[i] == 0)
7683 break;
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686 if (mbyte_cells == 2)
7687 {
7688 ScreenLines[off + 1] = 0;
7689 ScreenAttrs[off + 1] = attr;
7690 }
7691 screen_char(off, row, col);
7692 }
7693 else if (mbyte_cells == 2)
7694 {
7695 ScreenLines[off + 1] = ptr[1];
7696 ScreenAttrs[off + 1] = attr;
7697 screen_char_2(off, row, col);
7698 }
7699 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7700 {
7701 ScreenLines2[off] = ptr[1];
7702 screen_char(off, row, col);
7703 }
7704 else
7705#endif
7706 screen_char(off, row, col);
7707 }
7708#ifdef FEAT_MBYTE
7709 if (has_mbyte)
7710 {
7711 off += mbyte_cells;
7712 col += mbyte_cells;
7713 ptr += mbyte_blen;
7714 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007715 {
7716 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007718 len = -1;
7719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 else
7722#endif
7723 {
7724 ++off;
7725 ++col;
7726 ++ptr;
7727 }
7728 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007729
7730#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7731 /* If we detected the next character needs to be redrawn, but the text
7732 * doesn't extend up to there, update the character here. */
7733 if (force_redraw_next && col < screen_Columns)
7734 {
7735# ifdef FEAT_MBYTE
7736 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7737 screen_char_2(off, row, col);
7738 else
7739# endif
7740 screen_char(off, row, col);
7741 }
7742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745#ifdef FEAT_SEARCH_EXTRA
7746/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007747 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 */
7749 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007750start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 if (p_hls && !no_hlsearch)
7753 {
7754 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007755 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007756# ifdef FEAT_RELTIME
7757 /* Set the time limit to 'redrawtime'. */
7758 profile_setlimit(p_rdt, &search_hl.tm);
7759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 }
7761}
7762
7763/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007764 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 */
7766 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007767end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769 if (search_hl.rm.regprog != NULL)
7770 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007771 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 search_hl.rm.regprog = NULL;
7773 }
7774}
7775
7776/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007777 * Init for calling prepare_search_hl().
7778 */
7779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007780init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007781{
7782 matchitem_T *cur;
7783
7784 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7785 * match */
7786 cur = wp->w_match_head;
7787 while (cur != NULL)
7788 {
7789 cur->hl.rm = cur->match;
7790 if (cur->hlg_id == 0)
7791 cur->hl.attr = 0;
7792 else
7793 cur->hl.attr = syn_id2attr(cur->hlg_id);
7794 cur->hl.buf = wp->w_buffer;
7795 cur->hl.lnum = 0;
7796 cur->hl.first_lnum = 0;
7797# ifdef FEAT_RELTIME
7798 /* Set the time limit to 'redrawtime'. */
7799 profile_setlimit(p_rdt, &(cur->hl.tm));
7800# endif
7801 cur = cur->next;
7802 }
7803 search_hl.buf = wp->w_buffer;
7804 search_hl.lnum = 0;
7805 search_hl.first_lnum = 0;
7806 /* time limit is set at the toplevel, for all windows */
7807}
7808
7809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 * Advance to the match in window "wp" line "lnum" or past it.
7811 */
7812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007813prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007815 matchitem_T *cur; /* points to the match list */
7816 match_T *shl; /* points to search_hl or a match */
7817 int shl_flag; /* flag to indicate whether search_hl
7818 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007819 int pos_inprogress; /* marks that position match search is
7820 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 int n;
7822
7823 /*
7824 * When using a multi-line pattern, start searching at the top
7825 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007826 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007828 cur = wp->w_match_head;
7829 shl_flag = FALSE;
7830 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007832 if (shl_flag == FALSE)
7833 {
7834 shl = &search_hl;
7835 shl_flag = TRUE;
7836 }
7837 else
7838 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (shl->rm.regprog != NULL
7840 && shl->lnum == 0
7841 && re_multiline(shl->rm.regprog))
7842 {
7843 if (shl->first_lnum == 0)
7844 {
7845# ifdef FEAT_FOLDING
7846 for (shl->first_lnum = lnum;
7847 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7848 if (hasFoldingWin(wp, shl->first_lnum - 1,
7849 NULL, NULL, TRUE, NULL))
7850 break;
7851# else
7852 shl->first_lnum = wp->w_topline;
7853# endif
7854 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007855 if (cur != NULL)
7856 cur->pos.cur = 0;
7857 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007859 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7860 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007862 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7863 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007864 pos_inprogress = cur == NULL || cur->pos.cur == 0
7865 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 if (shl->lnum != 0)
7867 {
7868 shl->first_lnum = shl->lnum
7869 + shl->rm.endpos[0].lnum
7870 - shl->rm.startpos[0].lnum;
7871 n = shl->rm.endpos[0].col;
7872 }
7873 else
7874 {
7875 ++shl->first_lnum;
7876 n = 0;
7877 }
7878 }
7879 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007880 if (shl != &search_hl && cur != NULL)
7881 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883}
7884
7885/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007886 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 * Uses shl->buf.
7888 * Sets shl->lnum and shl->rm contents.
7889 * Note: Assumes a previous match is always before "lnum", unless
7890 * shl->lnum is zero.
7891 * Careful: Any pointers for buffer lines will become invalid.
7892 */
7893 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007894next_search_hl(
7895 win_T *win,
7896 match_T *shl, /* points to search_hl or a match */
7897 linenr_T lnum,
7898 colnr_T mincol, /* minimal column for a match */
7899 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900{
7901 linenr_T l;
7902 colnr_T matchcol;
7903 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007904 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007906 // for :{range}s/pat only highlight inside the range
7907 if (lnum < search_first_line || lnum > search_last_line)
7908 {
7909 shl->lnum = 0;
7910 return;
7911 }
7912
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 if (shl->lnum != 0)
7914 {
7915 /* Check for three situations:
7916 * 1. If the "lnum" is below a previous match, start a new search.
7917 * 2. If the previous match includes "mincol", use it.
7918 * 3. Continue after the previous match.
7919 */
7920 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7921 if (lnum > l)
7922 shl->lnum = 0;
7923 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7924 return;
7925 }
7926
7927 /*
7928 * Repeat searching for a match until one is found that includes "mincol"
7929 * or none is found in this line.
7930 */
7931 called_emsg = FALSE;
7932 for (;;)
7933 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007934#ifdef FEAT_RELTIME
7935 /* Stop searching after passing the time limit. */
7936 if (profile_passed_limit(&(shl->tm)))
7937 {
7938 shl->lnum = 0; /* no match found in time */
7939 break;
7940 }
7941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 /* Three situations:
7943 * 1. No useful previous match: search from start of line.
7944 * 2. Not Vi compatible or empty match: continue at next character.
7945 * Break the loop if this is beyond the end of the line.
7946 * 3. Vi compatible searching: continue at end of previous match.
7947 */
7948 if (shl->lnum == 0)
7949 matchcol = 0;
7950 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7951 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007952 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007954 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007955
7956 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007957 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007958 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007960 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 shl->lnum = 0;
7962 break;
7963 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007964#ifdef FEAT_MBYTE
7965 if (has_mbyte)
7966 matchcol += mb_ptr2len(ml);
7967 else
7968#endif
7969 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 }
7971 else
7972 matchcol = shl->rm.endpos[0].col;
7973
7974 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007977 /* Remember whether shl->rm is using a copy of the regprog in
7978 * cur->match. */
7979 int regprog_is_copy = (shl != &search_hl && cur != NULL
7980 && shl == &cur->hl
7981 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007982 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007983
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7985 matchcol,
7986#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007987 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007988#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007989 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990#endif
7991 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007992 /* Copy the regprog, in case it got freed and recompiled. */
7993 if (regprog_is_copy)
7994 cur->match.regprog = cur->hl.rm.regprog;
7995
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007996 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007997 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007998 /* Error while handling regexp: stop using this regexp. */
7999 if (shl == &search_hl)
8000 {
8001 /* don't free regprog in the match list, it's a copy */
8002 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008003 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 }
8005 shl->rm.regprog = NULL;
8006 shl->lnum = 0;
8007 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8008 message */
8009 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008010 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011 }
8012 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008013 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008014 else
8015 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (nmatched == 0)
8017 {
8018 shl->lnum = 0; /* no match found */
8019 break;
8020 }
8021 if (shl->rm.startpos[0].lnum > 0
8022 || shl->rm.startpos[0].col >= mincol
8023 || nmatched > 1
8024 || shl->rm.endpos[0].col > mincol)
8025 {
8026 shl->lnum += shl->rm.startpos[0].lnum;
8027 break; /* useful match found */
8028 }
8029 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008030
8031 // Restore called_emsg for assert_fails().
8032 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
Bram Moolenaar85077472016-10-16 14:35:48 +02008035/*
8036 * If there is a match fill "shl" and return one.
8037 * Return zero otherwise.
8038 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008040next_search_hl_pos(
8041 match_T *shl, /* points to a match */
8042 linenr_T lnum,
8043 posmatch_T *posmatch, /* match positions */
8044 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008045{
8046 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008047 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008048
Bram Moolenaarb3414592014-06-17 17:48:32 +02008049 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8050 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008051 llpos_T *pos = &posmatch->pos[i];
8052
8053 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008054 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008055 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008056 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008057 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008059 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008060 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008061 /* if this match comes before the one at "found" then swap
8062 * them */
8063 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008064 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008065 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008066
Bram Moolenaar85077472016-10-16 14:35:48 +02008067 *pos = posmatch->pos[found];
8068 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008069 }
8070 }
8071 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008072 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008073 }
8074 }
8075 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008076 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008078 colnr_T start = posmatch->pos[found].col == 0
8079 ? 0 : posmatch->pos[found].col - 1;
8080 colnr_T end = posmatch->pos[found].col == 0
8081 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082
Bram Moolenaar85077472016-10-16 14:35:48 +02008083 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 shl->rm.startpos[0].lnum = 0;
8085 shl->rm.startpos[0].col = start;
8086 shl->rm.endpos[0].lnum = 0;
8087 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008088 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008089 posmatch->cur = found + 1;
8090 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008091 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008092 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008093}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008094#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008095
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008097screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 attrentry_T *aep = NULL;
8100
8101 screen_attr = attr;
8102 if (full_screen
8103#ifdef WIN3264
8104 && termcap_active
8105#endif
8106 )
8107 {
8108#ifdef FEAT_GUI
8109 if (gui.in_use)
8110 {
8111 char buf[20];
8112
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008113 /* The GUI handles this internally. */
8114 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 OUT_STR(buf);
8116 }
8117 else
8118#endif
8119 {
8120 if (attr > HL_ALL) /* special HL attr. */
8121 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008122 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 aep = syn_cterm_attr2entry(attr);
8124 else
8125 aep = syn_term_attr2entry(attr);
8126 if (aep == NULL) /* did ":syntax clear" */
8127 attr = 0;
8128 else
8129 attr = aep->ae_attr;
8130 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008131 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008133 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008134#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008135 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8136 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8137 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008139 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008140 /* If the Normal FG color has BOLD attribute and the new HL
8141 * has a FG color defined, clear BOLD. */
8142 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008143 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008145 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008146 out_str(T_UCS);
8147 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008148 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8149 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008151 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008153 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008155 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008156 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157
8158 /*
8159 * Output the color or start string after bold etc., in case the
8160 * bold etc. override the color setting.
8161 */
8162 if (aep != NULL)
8163 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008164#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008165 /* When 'termguicolors' is set but fg or bg is unset,
8166 * fall back to the cterm colors. This helps for SpellBad,
8167 * where the GUI uses a red undercurl. */
8168 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008170 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008171 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008172 }
8173 else
8174#endif
8175 if (t_colors > 1)
8176 {
8177 if (aep->ae_u.cterm.fg_color)
8178 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8179 }
8180#ifdef FEAT_TERMGUICOLORS
8181 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8182 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008183 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008184 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 }
8186 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008187#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008188 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008190 if (aep->ae_u.cterm.bg_color)
8191 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8192 }
8193
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008194 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008195 {
8196 if (aep->ae_u.term.start != NULL)
8197 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199 }
8200 }
8201 }
8202}
8203
8204 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008205screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 int do_ME = FALSE; /* output T_ME code */
8208
8209 if (screen_attr != 0
8210#ifdef WIN3264
8211 && termcap_active
8212#endif
8213 )
8214 {
8215#ifdef FEAT_GUI
8216 if (gui.in_use)
8217 {
8218 char buf[20];
8219
8220 /* use internal GUI code */
8221 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8222 OUT_STR(buf);
8223 }
8224 else
8225#endif
8226 {
8227 if (screen_attr > HL_ALL) /* special HL attr. */
8228 {
8229 attrentry_T *aep;
8230
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008231 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
8233 /*
8234 * Assume that t_me restores the original colors!
8235 */
8236 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008237 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008238#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008239 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8240 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8241 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008242#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008243 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008244#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008245 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8246 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8247 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008248#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008249 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 do_ME = TRUE;
8251 }
8252 else
8253 {
8254 aep = syn_term_attr2entry(screen_attr);
8255 if (aep != NULL && aep->ae_u.term.stop != NULL)
8256 {
8257 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8258 do_ME = TRUE;
8259 else
8260 out_str(aep->ae_u.term.stop);
8261 }
8262 }
8263 if (aep == NULL) /* did ":syntax clear" */
8264 screen_attr = 0;
8265 else
8266 screen_attr = aep->ae_attr;
8267 }
8268
8269 /*
8270 * Often all ending-codes are equal to T_ME. Avoid outputting the
8271 * same sequence several times.
8272 */
8273 if (screen_attr & HL_STANDOUT)
8274 {
8275 if (STRCMP(T_SE, T_ME) == 0)
8276 do_ME = TRUE;
8277 else
8278 out_str(T_SE);
8279 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008280 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008281 {
8282 if (STRCMP(T_UCE, T_ME) == 0)
8283 do_ME = TRUE;
8284 else
8285 out_str(T_UCE);
8286 }
8287 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008288 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
8290 if (STRCMP(T_UE, T_ME) == 0)
8291 do_ME = TRUE;
8292 else
8293 out_str(T_UE);
8294 }
8295 if (screen_attr & HL_ITALIC)
8296 {
8297 if (STRCMP(T_CZR, T_ME) == 0)
8298 do_ME = TRUE;
8299 else
8300 out_str(T_CZR);
8301 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008302 if (screen_attr & HL_STRIKETHROUGH)
8303 {
8304 if (STRCMP(T_STE, T_ME) == 0)
8305 do_ME = TRUE;
8306 else
8307 out_str(T_STE);
8308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8310 out_str(T_ME);
8311
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008312#ifdef FEAT_TERMGUICOLORS
8313 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008315 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008316 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008317 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008318 term_bg_rgb_color(cterm_normal_bg_gui_color);
8319 }
8320 else
8321#endif
8322 {
8323 if (t_colors > 1)
8324 {
8325 /* set Normal cterm colors */
8326 if (cterm_normal_fg_color != 0)
8327 term_fg_color(cterm_normal_fg_color - 1);
8328 if (cterm_normal_bg_color != 0)
8329 term_bg_color(cterm_normal_bg_color - 1);
8330 if (cterm_normal_fg_bold)
8331 out_str(T_MD);
8332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334 }
8335 }
8336 screen_attr = 0;
8337}
8338
8339/*
8340 * Reset the colors for a cterm. Used when leaving Vim.
8341 * The machine specific code may override this again.
8342 */
8343 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008344reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008346 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {
8348 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008349#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008350 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8351 || cterm_normal_bg_gui_color != INVALCOLOR)
8352 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008353#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {
8357 out_str(T_OP);
8358 screen_attr = -1;
8359 }
8360 if (cterm_normal_fg_bold)
8361 {
8362 out_str(T_ME);
8363 screen_attr = -1;
8364 }
8365 }
8366}
8367
8368/*
8369 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8370 * using the attributes from ScreenAttrs["off"].
8371 */
8372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008373screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
8375 int attr;
8376
8377 /* Check for illegal values, just in case (could happen just after
8378 * resizing). */
8379 if (row >= screen_Rows || col >= screen_Columns)
8380 return;
8381
Bram Moolenaar494838a2015-02-10 19:20:37 +01008382 /* Outputting a character in the last cell on the screen may scroll the
8383 * screen up. Only do it when the "xn" termcap property is set, otherwise
8384 * mark the character invalid (update it when scrolled up). */
8385 if (*T_XN == NUL
8386 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387#ifdef FEAT_RIGHTLEFT
8388 /* account for first command-line character in rightleft mode */
8389 && !cmdmsg_rl
8390#endif
8391 )
8392 {
8393 ScreenAttrs[off] = (sattr_T)-1;
8394 return;
8395 }
8396
8397 /*
8398 * Stop highlighting first, so it's easier to move the cursor.
8399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 if (screen_char_attr != 0)
8401 attr = screen_char_attr;
8402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 attr = ScreenAttrs[off];
8404 if (screen_attr != attr)
8405 screen_stop_highlight();
8406
8407 windgoto(row, col);
8408
8409 if (screen_attr != attr)
8410 screen_start_highlight(attr);
8411
8412#ifdef FEAT_MBYTE
8413 if (enc_utf8 && ScreenLinesUC[off] != 0)
8414 {
8415 char_u buf[MB_MAXBYTES + 1];
8416
Bram Moolenaarcb070082016-04-02 22:14:51 +02008417 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008418 {
8419 if (*p_ambw == 'd'
8420# ifdef FEAT_GUI
8421 && !gui.in_use
8422# endif
8423 )
8424 {
8425 /* Clear the two screen cells. If the character is actually
8426 * single width it won't change the second cell. */
8427 out_str((char_u *)" ");
8428 term_windgoto(row, col);
8429 }
8430 /* not sure where the cursor is after drawing the ambiguous width
8431 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008432 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008433 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008434 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008436
8437 /* Convert the UTF-8 character to bytes and write it. */
8438 buf[utfc_char2bytes(off, buf)] = NUL;
8439 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 }
8441 else
8442#endif
8443 {
8444#ifdef FEAT_MBYTE
8445 out_flush_check();
8446#endif
8447 out_char(ScreenLines[off]);
8448#ifdef FEAT_MBYTE
8449 /* double-byte character in single-width cell */
8450 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8451 out_char(ScreenLines2[off]);
8452#endif
8453 }
8454
8455 screen_cur_col++;
8456}
8457
8458#ifdef FEAT_MBYTE
8459
8460/*
8461 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8462 * on the screen at position 'row' and 'col'.
8463 * The attributes of the first byte is used for all. This is required to
8464 * output the two bytes of a double-byte character with nothing in between.
8465 */
8466 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008467screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 /* Check for illegal values (could be wrong when screen was resized). */
8470 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8471 return;
8472
8473 /* Outputting the last character on the screen may scrollup the screen.
8474 * Don't to it! Mark the character invalid (update it when scrolled up) */
8475 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8476 {
8477 ScreenAttrs[off] = (sattr_T)-1;
8478 return;
8479 }
8480
8481 /* Output the first byte normally (positions the cursor), then write the
8482 * second byte directly. */
8483 screen_char(off, row, col);
8484 out_char(ScreenLines[off + 1]);
8485 ++screen_cur_col;
8486}
8487#endif
8488
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489/*
8490 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8491 * This uses the contents of ScreenLines[] and doesn't change it.
8492 */
8493 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008494screen_draw_rectangle(
8495 int row,
8496 int col,
8497 int height,
8498 int width,
8499 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 int r, c;
8502 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008503#ifdef FEAT_MBYTE
8504 int max_off;
8505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008507 /* Can't use ScreenLines unless initialized */
8508 if (ScreenLines == NULL)
8509 return;
8510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 if (invert)
8512 screen_char_attr = HL_INVERSE;
8513 for (r = row; r < row + height; ++r)
8514 {
8515 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008516#ifdef FEAT_MBYTE
8517 max_off = off + screen_Columns;
8518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 for (c = col; c < col + width; ++c)
8520 {
8521#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008522 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {
8524 screen_char_2(off + c, r, c);
8525 ++c;
8526 }
8527 else
8528#endif
8529 {
8530 screen_char(off + c, r, c);
8531#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008532 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 ++c;
8534#endif
8535 }
8536 }
8537 }
8538 screen_char_attr = 0;
8539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541/*
8542 * Redraw the characters for a vertically split window.
8543 */
8544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008545redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 int col;
8548 int width;
8549
8550# ifdef FEAT_CLIPBOARD
8551 clip_may_clear_selection(row, end - 1);
8552# endif
8553
8554 if (wp == NULL)
8555 {
8556 col = 0;
8557 width = Columns;
8558 }
8559 else
8560 {
8561 col = wp->w_wincol;
8562 width = wp->w_width;
8563 }
8564 screen_draw_rectangle(row, col, end - row, width, FALSE);
8565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008567 static void
8568space_to_screenline(int off, int attr)
8569{
8570 ScreenLines[off] = ' ';
8571 ScreenAttrs[off] = attr;
8572# ifdef FEAT_MBYTE
8573 if (enc_utf8)
8574 ScreenLinesUC[off] = 0;
8575# endif
8576}
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578/*
8579 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8580 * with character 'c1' in first column followed by 'c2' in the other columns.
8581 * Use attributes 'attr'.
8582 */
8583 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008584screen_fill(
8585 int start_row,
8586 int end_row,
8587 int start_col,
8588 int end_col,
8589 int c1,
8590 int c2,
8591 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 int row;
8594 int col;
8595 int off;
8596 int end_off;
8597 int did_delete;
8598 int c;
8599 int norm_term;
8600#if defined(FEAT_GUI) || defined(UNIX)
8601 int force_next = FALSE;
8602#endif
8603
8604 if (end_row > screen_Rows) /* safety check */
8605 end_row = screen_Rows;
8606 if (end_col > screen_Columns) /* safety check */
8607 end_col = screen_Columns;
8608 if (ScreenLines == NULL
8609 || start_row >= end_row
8610 || start_col >= end_col) /* nothing to do */
8611 return;
8612
8613 /* it's a "normal" terminal when not in a GUI or cterm */
8614 norm_term = (
8615#ifdef FEAT_GUI
8616 !gui.in_use &&
8617#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008618 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 for (row = start_row; row < end_row; ++row)
8620 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008621#ifdef FEAT_MBYTE
8622 if (has_mbyte
8623# ifdef FEAT_GUI
8624 && !gui.in_use
8625# endif
8626 )
8627 {
8628 /* When drawing over the right halve of a double-wide char clear
8629 * out the left halve. When drawing over the left halve of a
8630 * double wide-char clear out the right halve. Only needed in a
8631 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008632 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008633 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008634 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008635 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008636 }
8637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 /*
8639 * Try to use delete-line termcap code, when no attributes or in a
8640 * "normal" terminal, where a bold/italic space is just a
8641 * space.
8642 */
8643 did_delete = FALSE;
8644 if (c2 == ' '
8645 && end_col == Columns
8646 && can_clear(T_CE)
8647 && (attr == 0
8648 || (norm_term
8649 && attr <= HL_ALL
8650 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8651 {
8652 /*
8653 * check if we really need to clear something
8654 */
8655 col = start_col;
8656 if (c1 != ' ') /* don't clear first char */
8657 ++col;
8658
8659 off = LineOffset[row] + col;
8660 end_off = LineOffset[row] + end_col;
8661
8662 /* skip blanks (used often, keep it fast!) */
8663#ifdef FEAT_MBYTE
8664 if (enc_utf8)
8665 while (off < end_off && ScreenLines[off] == ' '
8666 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8667 ++off;
8668 else
8669#endif
8670 while (off < end_off && ScreenLines[off] == ' '
8671 && ScreenAttrs[off] == 0)
8672 ++off;
8673 if (off < end_off) /* something to be cleared */
8674 {
8675 col = off - LineOffset[row];
8676 screen_stop_highlight();
8677 term_windgoto(row, col);/* clear rest of this screen line */
8678 out_str(T_CE);
8679 screen_start(); /* don't know where cursor is now */
8680 col = end_col - col;
8681 while (col--) /* clear chars in ScreenLines */
8682 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008683 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 ++off;
8685 }
8686 }
8687 did_delete = TRUE; /* the chars are cleared now */
8688 }
8689
8690 off = LineOffset[row] + start_col;
8691 c = c1;
8692 for (col = start_col; col < end_col; ++col)
8693 {
8694 if (ScreenLines[off] != c
8695#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008696 || (enc_utf8 && (int)ScreenLinesUC[off]
8697 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698#endif
8699 || ScreenAttrs[off] != attr
8700#if defined(FEAT_GUI) || defined(UNIX)
8701 || force_next
8702#endif
8703 )
8704 {
8705#if defined(FEAT_GUI) || defined(UNIX)
8706 /* The bold trick may make a single row of pixels appear in
8707 * the next character. When a bold character is removed, the
8708 * next character should be redrawn too. This happens for our
8709 * own GUI and for some xterms. */
8710 if (
8711# ifdef FEAT_GUI
8712 gui.in_use
8713# endif
8714# if defined(FEAT_GUI) && defined(UNIX)
8715 ||
8716# endif
8717# ifdef UNIX
8718 term_is_xterm
8719# endif
8720 )
8721 {
8722 if (ScreenLines[off] != ' '
8723 && (ScreenAttrs[off] > HL_ALL
8724 || ScreenAttrs[off] & HL_BOLD))
8725 force_next = TRUE;
8726 else
8727 force_next = FALSE;
8728 }
8729#endif
8730 ScreenLines[off] = c;
8731#ifdef FEAT_MBYTE
8732 if (enc_utf8)
8733 {
8734 if (c >= 0x80)
8735 {
8736 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008737 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
8739 else
8740 ScreenLinesUC[off] = 0;
8741 }
8742#endif
8743 ScreenAttrs[off] = attr;
8744 if (!did_delete || c != ' ')
8745 screen_char(off, row, col);
8746 }
8747 ++off;
8748 if (col == start_col)
8749 {
8750 if (did_delete)
8751 break;
8752 c = c2;
8753 }
8754 }
8755 if (end_col == Columns)
8756 LineWraps[row] = FALSE;
8757 if (row == Rows - 1) /* overwritten the command line */
8758 {
8759 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008760 if (start_col == 0 && end_col == Columns
8761 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008763 if (start_col == 0)
8764 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 }
8766 }
8767}
8768
8769/*
8770 * Check if there should be a delay. Used before clearing or redrawing the
8771 * screen or the command line.
8772 */
8773 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008774check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
8776 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8777 && !did_wait_return
8778 && emsg_silent == 0)
8779 {
8780 out_flush();
8781 ui_delay(1000L, TRUE);
8782 emsg_on_display = FALSE;
8783 if (check_msg_scroll)
8784 msg_scroll = FALSE;
8785 }
8786}
8787
8788/*
8789 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008790 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 * Returns TRUE if there is a valid screen to write to.
8792 * Returns FALSE when starting up and screen not initialized yet.
8793 */
8794 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008795screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008797 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 return (ScreenLines != NULL);
8799}
8800
8801/*
8802 * Resize the shell to Rows and Columns.
8803 * Allocate ScreenLines[] and associated items.
8804 *
8805 * There may be some time between setting Rows and Columns and (re)allocating
8806 * ScreenLines[]. This happens when starting up and when (manually) changing
8807 * the shell size. Always use screen_Rows and screen_Columns to access items
8808 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8809 * final size of the shell is needed.
8810 */
8811 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008812screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
8814 int new_row, old_row;
8815#ifdef FEAT_GUI
8816 int old_Rows;
8817#endif
8818 win_T *wp;
8819 int outofmem = FALSE;
8820 int len;
8821 schar_T *new_ScreenLines;
8822#ifdef FEAT_MBYTE
8823 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008824 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008826 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827#endif
8828 sattr_T *new_ScreenAttrs;
8829 unsigned *new_LineOffset;
8830 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008831 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008832 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008834 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008835 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008837retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 /*
8839 * Allocation of the screen buffers is done only when the size changes and
8840 * when Rows and Columns have been set and we have started doing full
8841 * screen stuff.
8842 */
8843 if ((ScreenLines != NULL
8844 && Rows == screen_Rows
8845 && Columns == screen_Columns
8846#ifdef FEAT_MBYTE
8847 && enc_utf8 == (ScreenLinesUC != NULL)
8848 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008849 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850#endif
8851 )
8852 || Rows == 0
8853 || Columns == 0
8854 || (!full_screen && ScreenLines == NULL))
8855 return;
8856
8857 /*
8858 * It's possible that we produce an out-of-memory message below, which
8859 * will cause this function to be called again. To break the loop, just
8860 * return here.
8861 */
8862 if (entered)
8863 return;
8864 entered = TRUE;
8865
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008866 /*
8867 * Note that the window sizes are updated before reallocating the arrays,
8868 * thus we must not redraw here!
8869 */
8870 ++RedrawingDisabled;
8871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 win_new_shellsize(); /* fit the windows in the new sized shell */
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 comp_col(); /* recompute columns for shown command and ruler */
8875
8876 /*
8877 * We're changing the size of the screen.
8878 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8879 * - Move lines from the old arrays into the new arrays, clear extra
8880 * lines (unless the screen is going to be cleared).
8881 * - Free the old arrays.
8882 *
8883 * If anything fails, make ScreenLines NULL, so we don't do anything!
8884 * Continuing with the old ScreenLines may result in a crash, because the
8885 * size is wrong.
8886 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008887 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008889 if (aucmd_win != NULL)
8890 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
8892 new_ScreenLines = (schar_T *)lalloc((long_u)(
8893 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8894#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008895 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 if (enc_utf8)
8897 {
8898 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8899 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008900 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008901 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8903 }
8904 if (enc_dbcs == DBCS_JPNU)
8905 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8906 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8907#endif
8908 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8909 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8910 new_LineOffset = (unsigned *)lalloc((long_u)(
8911 Rows * sizeof(unsigned)), FALSE);
8912 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008913 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008915 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 {
8917 if (win_alloc_lines(wp) == FAIL)
8918 {
8919 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008920 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
8922 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008923 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8924 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008925 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008926give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008928#ifdef FEAT_MBYTE
8929 for (i = 0; i < p_mco; ++i)
8930 if (new_ScreenLinesC[i] == NULL)
8931 break;
8932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 if (new_ScreenLines == NULL
8934#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008935 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8937#endif
8938 || new_ScreenAttrs == NULL
8939 || new_LineOffset == NULL
8940 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008941 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 || outofmem)
8943 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008944 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008945 {
8946 /* guess the size */
8947 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8948
8949 /* Remember we did this to avoid getting outofmem messages over
8950 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008951 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008952 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008953 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008955 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008956 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008957 VIM_CLEAR(new_ScreenLinesC[i]);
8958 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008960 VIM_CLEAR(new_ScreenAttrs);
8961 VIM_CLEAR(new_LineOffset);
8962 VIM_CLEAR(new_LineWraps);
8963 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 else
8966 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008967 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 for (new_row = 0; new_row < Rows; ++new_row)
8970 {
8971 new_LineOffset[new_row] = new_row * Columns;
8972 new_LineWraps[new_row] = FALSE;
8973
8974 /*
8975 * If the screen is not going to be cleared, copy as much as
8976 * possible from the old screen to the new one and clear the rest
8977 * (used when resizing the window at the "--more--" prompt or when
8978 * executing an external command, for the GUI).
8979 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008980 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
8982 (void)vim_memset(new_ScreenLines + new_row * Columns,
8983 ' ', (size_t)Columns * sizeof(schar_T));
8984#ifdef FEAT_MBYTE
8985 if (enc_utf8)
8986 {
8987 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8988 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 for (i = 0; i < p_mco; ++i)
8990 (void)vim_memset(new_ScreenLinesC[i]
8991 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 0, (size_t)Columns * sizeof(u8char_T));
8993 }
8994 if (enc_dbcs == DBCS_JPNU)
8995 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8996 0, (size_t)Columns * sizeof(schar_T));
8997#endif
8998 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8999 0, (size_t)Columns * sizeof(sattr_T));
9000 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009001 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 {
9003 if (screen_Columns < Columns)
9004 len = screen_Columns;
9005 else
9006 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009007#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009008 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 * may be invalid now. Also when p_mco changes. */
9010 if (!(enc_utf8 && ScreenLinesUC == NULL)
9011 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009012#endif
9013 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9014 ScreenLines + LineOffset[old_row],
9015 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009017 if (enc_utf8 && ScreenLinesUC != NULL
9018 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 {
9020 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9021 ScreenLinesUC + LineOffset[old_row],
9022 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009023 for (i = 0; i < p_mco; ++i)
9024 mch_memmove(new_ScreenLinesC[i]
9025 + new_LineOffset[new_row],
9026 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 (size_t)len * sizeof(u8char_T));
9028 }
9029 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9030 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9031 ScreenLines2 + LineOffset[old_row],
9032 (size_t)len * sizeof(schar_T));
9033#endif
9034 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9035 ScreenAttrs + LineOffset[old_row],
9036 (size_t)len * sizeof(sattr_T));
9037 }
9038 }
9039 }
9040 /* Use the last line of the screen for the current line. */
9041 current_ScreenLine = new_ScreenLines + Rows * Columns;
9042 }
9043
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009044 free_screenlines();
9045
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 ScreenLines = new_ScreenLines;
9047#ifdef FEAT_MBYTE
9048 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009049 for (i = 0; i < p_mco; ++i)
9050 ScreenLinesC[i] = new_ScreenLinesC[i];
9051 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 ScreenLines2 = new_ScreenLines2;
9053#endif
9054 ScreenAttrs = new_ScreenAttrs;
9055 LineOffset = new_LineOffset;
9056 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009057 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 /* It's important that screen_Rows and screen_Columns reflect the actual
9060 * size of ScreenLines[]. Set them before calling anything. */
9061#ifdef FEAT_GUI
9062 old_Rows = screen_Rows;
9063#endif
9064 screen_Rows = Rows;
9065 screen_Columns = Columns;
9066
9067 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009068 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 screenclear2();
9070
9071#ifdef FEAT_GUI
9072 else if (gui.in_use
9073 && !gui.starting
9074 && ScreenLines != NULL
9075 && old_Rows != Rows)
9076 {
9077 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9078 /*
9079 * Adjust the position of the cursor, for when executing an external
9080 * command.
9081 */
9082 if (msg_row >= Rows) /* Rows got smaller */
9083 msg_row = Rows - 1; /* put cursor at last row */
9084 else if (Rows > old_Rows) /* Rows got bigger */
9085 msg_row += Rows - old_Rows; /* put cursor in same place */
9086 if (msg_col >= Columns) /* Columns got smaller */
9087 msg_col = Columns - 1; /* put cursor at last column */
9088 }
9089#endif
9090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009092 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009093
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009094 /*
9095 * Do not apply autocommands more than 3 times to avoid an endless loop
9096 * in case applying autocommands always changes Rows or Columns.
9097 */
9098 if (starting == 0 && ++retry_count <= 3)
9099 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009100 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009101 /* In rare cases, autocommands may have altered Rows or Columns,
9102 * jump back to check if we need to allocate the screen again. */
9103 goto retry;
9104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105}
9106
9107 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009108free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009109{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009110#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009111 int i;
9112
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009113 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009114 for (i = 0; i < Screen_mco; ++i)
9115 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009116 vim_free(ScreenLines2);
9117#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009118 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009119 vim_free(ScreenAttrs);
9120 vim_free(LineOffset);
9121 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009122 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009123}
9124
9125 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009126screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 check_for_delay(FALSE);
9129 screenalloc(FALSE); /* allocate screen buffers if size changed */
9130 screenclear2(); /* clear the screen */
9131}
9132
9133 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009134screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 int i;
9137
9138 if (starting == NO_SCREEN || ScreenLines == NULL
9139#ifdef FEAT_GUI
9140 || (gui.in_use && gui.starting)
9141#endif
9142 )
9143 return;
9144
9145#ifdef FEAT_GUI
9146 if (!gui.in_use)
9147#endif
9148 screen_attr = -1; /* force setting the Normal colors */
9149 screen_stop_highlight(); /* don't want highlighting here */
9150
9151#ifdef FEAT_CLIPBOARD
9152 /* disable selection without redrawing it */
9153 clip_scroll_selection(9999);
9154#endif
9155
9156 /* blank out ScreenLines */
9157 for (i = 0; i < Rows; ++i)
9158 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009159 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 LineWraps[i] = FALSE;
9161 }
9162
9163 if (can_clear(T_CL))
9164 {
9165 out_str(T_CL); /* clear the display */
9166 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009167 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 }
9169 else
9170 {
9171 /* can't clear the screen, mark all chars with invalid attributes */
9172 for (i = 0; i < Rows; ++i)
9173 lineinvalid(LineOffset[i], (int)Columns);
9174 clear_cmdline = TRUE;
9175 }
9176
9177 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9178
9179 win_rest_invalid(firstwin);
9180 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009181 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 if (must_redraw == CLEAR) /* no need to clear again */
9183 must_redraw = NOT_VALID;
9184 compute_cmdrow();
9185 msg_row = cmdline_row; /* put cursor on last line for messages */
9186 msg_col = 0;
9187 screen_start(); /* don't know where cursor is now */
9188 msg_scrolled = 0; /* can't scroll back */
9189 msg_didany = FALSE;
9190 msg_didout = FALSE;
9191}
9192
9193/*
9194 * Clear one line in ScreenLines.
9195 */
9196 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009197lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9200#ifdef FEAT_MBYTE
9201 if (enc_utf8)
9202 (void)vim_memset(ScreenLinesUC + off, 0,
9203 (size_t)width * sizeof(u8char_T));
9204#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009205 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206}
9207
9208/*
9209 * Mark one line in ScreenLines invalid by setting the attributes to an
9210 * invalid value.
9211 */
9212 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009213lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9216}
9217
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218/*
9219 * Copy part of a Screenline for vertically split window "wp".
9220 */
9221 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009222linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 unsigned off_to = LineOffset[to] + wp->w_wincol;
9225 unsigned off_from = LineOffset[from] + wp->w_wincol;
9226
9227 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9228 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009229#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 if (enc_utf8)
9231 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009232 int i;
9233
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9235 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009236 for (i = 0; i < p_mco; ++i)
9237 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9238 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 }
9240 if (enc_dbcs == DBCS_JPNU)
9241 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9242 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9245 wp->w_width * sizeof(sattr_T));
9246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247
9248/*
9249 * Return TRUE if clearing with term string "p" would work.
9250 * It can't work when the string is empty or it won't set the right background.
9251 */
9252 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009253can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255 return (*p != NUL && (t_colors <= 1
9256#ifdef FEAT_GUI
9257 || gui.in_use
9258#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009259#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009260 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009261 || (!p_tgc && cterm_normal_bg_color == 0)
9262#else
9263 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009264#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009265 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266}
9267
9268/*
9269 * Reset cursor position. Use whenever cursor was moved because of outputting
9270 * something directly to the screen (shell commands) or a terminal control
9271 * code.
9272 */
9273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009274screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
9276 screen_cur_row = screen_cur_col = 9999;
9277}
9278
9279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 * Move the cursor to position "row","col" in the screen.
9281 * This tries to find the most efficient way to move, minimizing the number of
9282 * characters sent to the terminal.
9283 */
9284 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009285windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009287 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 int i;
9289 int plan;
9290 int cost;
9291 int wouldbe_col;
9292 int noinvcurs;
9293 char_u *bs;
9294 int goto_cost;
9295 int attr;
9296
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009297#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9299
9300#define PLAN_LE 1
9301#define PLAN_CR 2
9302#define PLAN_NL 3
9303#define PLAN_WRITE 4
9304 /* Can't use ScreenLines unless initialized */
9305 if (ScreenLines == NULL)
9306 return;
9307
9308 if (col != screen_cur_col || row != screen_cur_row)
9309 {
9310 /* Check for valid position. */
9311 if (row < 0) /* window without text lines? */
9312 row = 0;
9313 if (row >= screen_Rows)
9314 row = screen_Rows - 1;
9315 if (col >= screen_Columns)
9316 col = screen_Columns - 1;
9317
9318 /* check if no cursor movement is allowed in highlight mode */
9319 if (screen_attr && *T_MS == NUL)
9320 noinvcurs = HIGHL_COST;
9321 else
9322 noinvcurs = 0;
9323 goto_cost = GOTO_COST + noinvcurs;
9324
9325 /*
9326 * Plan how to do the positioning:
9327 * 1. Use CR to move it to column 0, same row.
9328 * 2. Use T_LE to move it a few columns to the left.
9329 * 3. Use NL to move a few lines down, column 0.
9330 * 4. Move a few columns to the right with T_ND or by writing chars.
9331 *
9332 * Don't do this if the cursor went beyond the last column, the cursor
9333 * position is unknown then (some terminals wrap, some don't )
9334 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009335 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 * characters to move the cursor to the right.
9337 */
9338 if (row >= screen_cur_row && screen_cur_col < Columns)
9339 {
9340 /*
9341 * If the cursor is in the same row, bigger col, we can use CR
9342 * or T_LE.
9343 */
9344 bs = NULL; /* init for GCC */
9345 attr = screen_attr;
9346 if (row == screen_cur_row && col < screen_cur_col)
9347 {
9348 /* "le" is preferred over "bc", because "bc" is obsolete */
9349 if (*T_LE)
9350 bs = T_LE; /* "cursor left" */
9351 else
9352 bs = T_BC; /* "backspace character (old) */
9353 if (*bs)
9354 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9355 else
9356 cost = 999;
9357 if (col + 1 < cost) /* using CR is less characters */
9358 {
9359 plan = PLAN_CR;
9360 wouldbe_col = 0;
9361 cost = 1; /* CR is just one character */
9362 }
9363 else
9364 {
9365 plan = PLAN_LE;
9366 wouldbe_col = col;
9367 }
9368 if (noinvcurs) /* will stop highlighting */
9369 {
9370 cost += noinvcurs;
9371 attr = 0;
9372 }
9373 }
9374
9375 /*
9376 * If the cursor is above where we want to be, we can use CR LF.
9377 */
9378 else if (row > screen_cur_row)
9379 {
9380 plan = PLAN_NL;
9381 wouldbe_col = 0;
9382 cost = (row - screen_cur_row) * 2; /* CR LF */
9383 if (noinvcurs) /* will stop highlighting */
9384 {
9385 cost += noinvcurs;
9386 attr = 0;
9387 }
9388 }
9389
9390 /*
9391 * If the cursor is in the same row, smaller col, just use write.
9392 */
9393 else
9394 {
9395 plan = PLAN_WRITE;
9396 wouldbe_col = screen_cur_col;
9397 cost = 0;
9398 }
9399
9400 /*
9401 * Check if any characters that need to be written have the
9402 * correct attributes. Also avoid UTF-8 characters.
9403 */
9404 i = col - wouldbe_col;
9405 if (i > 0)
9406 cost += i;
9407 if (cost < goto_cost && i > 0)
9408 {
9409 /*
9410 * Check if the attributes are correct without additionally
9411 * stopping highlighting.
9412 */
9413 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9414 while (i && *p++ == attr)
9415 --i;
9416 if (i != 0)
9417 {
9418 /*
9419 * Try if it works when highlighting is stopped here.
9420 */
9421 if (*--p == 0)
9422 {
9423 cost += noinvcurs;
9424 while (i && *p++ == 0)
9425 --i;
9426 }
9427 if (i != 0)
9428 cost = 999; /* different attributes, don't do it */
9429 }
9430#ifdef FEAT_MBYTE
9431 if (enc_utf8)
9432 {
9433 /* Don't use an UTF-8 char for positioning, it's slow. */
9434 for (i = wouldbe_col; i < col; ++i)
9435 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9436 {
9437 cost = 999;
9438 break;
9439 }
9440 }
9441#endif
9442 }
9443
9444 /*
9445 * We can do it without term_windgoto()!
9446 */
9447 if (cost < goto_cost)
9448 {
9449 if (plan == PLAN_LE)
9450 {
9451 if (noinvcurs)
9452 screen_stop_highlight();
9453 while (screen_cur_col > col)
9454 {
9455 out_str(bs);
9456 --screen_cur_col;
9457 }
9458 }
9459 else if (plan == PLAN_CR)
9460 {
9461 if (noinvcurs)
9462 screen_stop_highlight();
9463 out_char('\r');
9464 screen_cur_col = 0;
9465 }
9466 else if (plan == PLAN_NL)
9467 {
9468 if (noinvcurs)
9469 screen_stop_highlight();
9470 while (screen_cur_row < row)
9471 {
9472 out_char('\n');
9473 ++screen_cur_row;
9474 }
9475 screen_cur_col = 0;
9476 }
9477
9478 i = col - screen_cur_col;
9479 if (i > 0)
9480 {
9481 /*
9482 * Use cursor-right if it's one character only. Avoids
9483 * removing a line of pixels from the last bold char, when
9484 * using the bold trick in the GUI.
9485 */
9486 if (T_ND[0] != NUL && T_ND[1] == NUL)
9487 {
9488 while (i-- > 0)
9489 out_char(*T_ND);
9490 }
9491 else
9492 {
9493 int off;
9494
9495 off = LineOffset[row] + screen_cur_col;
9496 while (i-- > 0)
9497 {
9498 if (ScreenAttrs[off] != screen_attr)
9499 screen_stop_highlight();
9500#ifdef FEAT_MBYTE
9501 out_flush_check();
9502#endif
9503 out_char(ScreenLines[off]);
9504#ifdef FEAT_MBYTE
9505 if (enc_dbcs == DBCS_JPNU
9506 && ScreenLines[off] == 0x8e)
9507 out_char(ScreenLines2[off]);
9508#endif
9509 ++off;
9510 }
9511 }
9512 }
9513 }
9514 }
9515 else
9516 cost = 999;
9517
9518 if (cost >= goto_cost)
9519 {
9520 if (noinvcurs)
9521 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009522 if (row == screen_cur_row && (col > screen_cur_col)
9523 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 term_cursor_right(col - screen_cur_col);
9525 else
9526 term_windgoto(row, col);
9527 }
9528 screen_cur_row = row;
9529 screen_cur_col = col;
9530 }
9531}
9532
9533/*
9534 * Set cursor to its position in the current window.
9535 */
9536 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009537setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009539 setcursor_mayforce(FALSE);
9540}
9541
9542/*
9543 * Set cursor to its position in the current window.
9544 * When "force" is TRUE also when not redrawing.
9545 */
9546 void
9547setcursor_mayforce(int force)
9548{
9549 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 {
9551 validate_cursor();
9552 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009553 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009555 /* With 'rightleft' set and the cursor on a double-wide
9556 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009557 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009559 (has_mbyte
9560 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9561 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562# endif
9563 1)) :
9564#endif
9565 curwin->w_wcol));
9566 }
9567}
9568
9569
9570/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009571 * Insert 'line_count' lines at 'row' in window 'wp'.
9572 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9573 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 * scrolling.
9575 * Returns FAIL if the lines are not inserted, OK for success.
9576 */
9577 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009578win_ins_lines(
9579 win_T *wp,
9580 int row,
9581 int line_count,
9582 int invalid,
9583 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585 int did_delete;
9586 int nextrow;
9587 int lastrow;
9588 int retval;
9589
9590 if (invalid)
9591 wp->w_lines_valid = 0;
9592
9593 if (wp->w_height < 5)
9594 return FAIL;
9595
9596 if (line_count > wp->w_height - row)
9597 line_count = wp->w_height - row;
9598
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009599 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 if (retval != MAYBE)
9601 return retval;
9602
9603 /*
9604 * If there is a next window or a status line, we first try to delete the
9605 * lines at the bottom to avoid messing what is after the window.
9606 * If this fails and there are following windows, don't do anything to avoid
9607 * messing up those windows, better just redraw.
9608 */
9609 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 if (wp->w_next != NULL || wp->w_status_height)
9611 {
9612 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009613 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 did_delete = TRUE;
9615 else if (wp->w_next)
9616 return FAIL;
9617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 /*
9619 * if no lines deleted, blank the lines that will end up below the window
9620 */
9621 if (!did_delete)
9622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009625 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 lastrow = nextrow + line_count;
9627 if (lastrow > Rows)
9628 lastrow = Rows;
9629 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009630 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 ' ', ' ', 0);
9632 }
9633
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009634 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 == FAIL)
9636 {
9637 /* deletion will have messed up other windows */
9638 if (did_delete)
9639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 win_rest_invalid(W_NEXT(wp));
9642 }
9643 return FAIL;
9644 }
9645
9646 return OK;
9647}
9648
9649/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009650 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9652 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9653 * scrolling
9654 * Return OK for success, FAIL if the lines are not deleted.
9655 */
9656 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009657win_del_lines(
9658 win_T *wp,
9659 int row,
9660 int line_count,
9661 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009662 int mayclear,
9663 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 int retval;
9666
9667 if (invalid)
9668 wp->w_lines_valid = 0;
9669
9670 if (line_count > wp->w_height - row)
9671 line_count = wp->w_height - row;
9672
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009673 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 if (retval != MAYBE)
9675 return retval;
9676
9677 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009678 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 return FAIL;
9680
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 /*
9682 * If there are windows or status lines below, try to put them at the
9683 * correct place. If we can't do that, they have to be redrawn.
9684 */
9685 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9686 {
9687 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009688 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 {
9690 wp->w_redr_status = TRUE;
9691 win_rest_invalid(wp->w_next);
9692 }
9693 }
9694 /*
9695 * If this is the last window and there is no status line, redraw the
9696 * command line later.
9697 */
9698 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 redraw_cmdline = TRUE;
9700 return OK;
9701}
9702
9703/*
9704 * Common code for win_ins_lines() and win_del_lines().
9705 * Returns OK or FAIL when the work has been done.
9706 * Returns MAYBE when not finished yet.
9707 */
9708 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009709win_do_lines(
9710 win_T *wp,
9711 int row,
9712 int line_count,
9713 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009714 int del,
9715 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
9717 int retval;
9718
9719 if (!redrawing() || line_count <= 0)
9720 return FAIL;
9721
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009722 /* When inserting lines would result in loss of command output, just redraw
9723 * the lines. */
9724 if (no_win_do_lines_ins && !del)
9725 return FAIL;
9726
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009728 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009730 if (!no_win_do_lines_ins)
9731 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 return FAIL;
9733 }
9734
9735 /*
9736 * Delete all remaining lines
9737 */
9738 if (row + line_count >= wp->w_height)
9739 {
9740 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009741 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 ' ', ' ', 0);
9743 return OK;
9744 }
9745
9746 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009747 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009749 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009751 if (!no_win_do_lines_ins)
9752 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753
9754 /*
9755 * If the terminal can set a scroll region, use that.
9756 * Always do this in a vertically split window. This will redraw from
9757 * ScreenLines[] when t_CV isn't defined. That's faster than using
9758 * win_line().
9759 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009760 * a character in the lower right corner of the scroll region may cause a
9761 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009763 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 scroll_region_set(wp, row);
9767 if (del)
9768 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009769 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 else
9771 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009772 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 scroll_region_reset();
9775 return retval;
9776 }
9777
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9779 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
9781 return MAYBE;
9782}
9783
9784/*
9785 * window 'wp' and everything after it is messed up, mark it for redraw
9786 */
9787 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009788win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 {
9792 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 wp->w_redr_status = TRUE;
9794 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 }
9796 redraw_cmdline = TRUE;
9797}
9798
9799/*
9800 * The rest of the routines in this file perform screen manipulations. The
9801 * given operation is performed physically on the screen. The corresponding
9802 * change is also made to the internal screen image. In this way, the editor
9803 * anticipates the effect of editing changes on the appearance of the screen.
9804 * That way, when we call screenupdate a complete redraw isn't usually
9805 * necessary. Another advantage is that we can keep adding code to anticipate
9806 * screen changes, and in the meantime, everything still works.
9807 */
9808
9809/*
9810 * types for inserting or deleting lines
9811 */
9812#define USE_T_CAL 1
9813#define USE_T_CDL 2
9814#define USE_T_AL 3
9815#define USE_T_CE 4
9816#define USE_T_DL 5
9817#define USE_T_SR 6
9818#define USE_NL 7
9819#define USE_T_CD 8
9820#define USE_REDRAW 9
9821
9822/*
9823 * insert lines on the screen and update ScreenLines[]
9824 * 'end' is the line after the scrolled part. Normally it is Rows.
9825 * When scrolling region used 'off' is the offset from the top for the region.
9826 * 'row' and 'end' are relative to the start of the region.
9827 *
9828 * return FAIL for failure, OK for success.
9829 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009830 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009831screen_ins_lines(
9832 int off,
9833 int row,
9834 int line_count,
9835 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009836 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009837 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 int i;
9840 int j;
9841 unsigned temp;
9842 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009843 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 int type;
9845 int result_empty;
9846 int can_ce = can_clear(T_CE);
9847
9848 /*
9849 * FAIL if
9850 * - there is no valid screen
9851 * - the screen has to be redrawn completely
9852 * - the line count is less than one
9853 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009854 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009856 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9857#ifdef FEAT_CLIPBOARD
9858 || (clip_star.state != SELECT_CLEARED
9859 && redrawing_for_callback > 0)
9860#endif
9861 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 return FAIL;
9863
9864 /*
9865 * There are seven ways to insert lines:
9866 * 0. When in a vertically split window and t_CV isn't set, redraw the
9867 * characters from ScreenLines[].
9868 * 1. Use T_CD (clear to end of display) if it exists and the result of
9869 * the insert is just empty lines
9870 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9871 * present or line_count > 1. It looks better if we do all the inserts
9872 * at once.
9873 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9874 * insert is just empty lines and T_CE is not present or line_count >
9875 * 1.
9876 * 4. Use T_AL (insert line) if it exists.
9877 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9878 * just empty lines.
9879 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9880 * just empty lines.
9881 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9882 * the 'da' flag is not set or we have clear line capability.
9883 * 8. redraw the characters from ScreenLines[].
9884 *
9885 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9886 * the scrollbar for the window. It does have insert line, use that if it
9887 * exists.
9888 */
9889 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9891 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009892 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 type = USE_T_CD;
9894 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9895 type = USE_T_CAL;
9896 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9897 type = USE_T_CDL;
9898 else if (*T_AL != NUL)
9899 type = USE_T_AL;
9900 else if (can_ce && result_empty)
9901 type = USE_T_CE;
9902 else if (*T_DL != NUL && result_empty)
9903 type = USE_T_DL;
9904 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9905 type = USE_T_SR;
9906 else
9907 return FAIL;
9908
9909 /*
9910 * For clearing the lines screen_del_lines() is used. This will also take
9911 * care of t_db if necessary.
9912 */
9913 if (type == USE_T_CD || type == USE_T_CDL ||
9914 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009915 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916
9917 /*
9918 * If text is retained below the screen, first clear or delete as many
9919 * lines at the bottom of the window as are about to be inserted so that
9920 * the deleted lines won't later surface during a screen_del_lines.
9921 */
9922 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009923 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
9925#ifdef FEAT_CLIPBOARD
9926 /* Remove a modeless selection when inserting lines halfway the screen
9927 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009928 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009929 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 else
9931 clip_scroll_selection(-line_count);
9932#endif
9933
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934#ifdef FEAT_GUI
9935 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9936 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009937 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938#endif
9939
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009940 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9941 cursor_col = wp->w_wincol;
9942
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 if (*T_CCS != NUL) /* cursor relative to region */
9944 cursor_row = row;
9945 else
9946 cursor_row = row + off;
9947
9948 /*
9949 * Shift LineOffset[] line_count down to reflect the inserted lines.
9950 * Clear the inserted lines in ScreenLines[].
9951 */
9952 row += off;
9953 end += off;
9954 for (i = 0; i < line_count; ++i)
9955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 if (wp != NULL && wp->w_width != Columns)
9957 {
9958 /* need to copy part of a line */
9959 j = end - 1 - i;
9960 while ((j -= line_count) >= row)
9961 linecopy(j + line_count, j, wp);
9962 j += line_count;
9963 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009964 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9965 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 else
9967 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9968 LineWraps[j] = FALSE;
9969 }
9970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
9972 j = end - 1 - i;
9973 temp = LineOffset[j];
9974 while ((j -= line_count) >= row)
9975 {
9976 LineOffset[j + line_count] = LineOffset[j];
9977 LineWraps[j + line_count] = LineWraps[j];
9978 }
9979 LineOffset[j + line_count] = temp;
9980 LineWraps[j + line_count] = FALSE;
9981 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009982 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 else
9984 lineinvalid(temp, (int)Columns);
9985 }
9986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987
9988 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009989 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009990 if (clear_attr != 0)
9991 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 /* redraw the characters */
9994 if (type == USE_REDRAW)
9995 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009996 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
9998 term_append_lines(line_count);
9999 screen_start(); /* don't know where cursor is now */
10000 }
10001 else
10002 {
10003 for (i = 0; i < line_count; i++)
10004 {
10005 if (type == USE_T_AL)
10006 {
10007 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010008 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 out_str(T_AL);
10010 }
10011 else /* type == USE_T_SR */
10012 out_str(T_SR);
10013 screen_start(); /* don't know where cursor is now */
10014 }
10015 }
10016
10017 /*
10018 * With scroll-reverse and 'da' flag set we need to clear the lines that
10019 * have been scrolled down into the region.
10020 */
10021 if (type == USE_T_SR && *T_DA)
10022 {
10023 for (i = 0; i < line_count; ++i)
10024 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010025 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 out_str(T_CE);
10027 screen_start(); /* don't know where cursor is now */
10028 }
10029 }
10030
10031#ifdef FEAT_GUI
10032 gui_can_update_cursor();
10033 if (gui.in_use)
10034 out_flush(); /* always flush after a scroll */
10035#endif
10036 return OK;
10037}
10038
10039/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010040 * Delete lines on the screen and update ScreenLines[].
10041 * "end" is the line after the scrolled part. Normally it is Rows.
10042 * When scrolling region used "off" is the offset from the top for the region.
10043 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 *
10045 * Return OK for success, FAIL if the lines are not deleted.
10046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010048screen_del_lines(
10049 int off,
10050 int row,
10051 int line_count,
10052 int end,
10053 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010054 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010055 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
10057 int j;
10058 int i;
10059 unsigned temp;
10060 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010061 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 int cursor_end;
10063 int result_empty; /* result is empty until end of region */
10064 int can_delete; /* deleting line codes can be used */
10065 int type;
10066
10067 /*
10068 * FAIL if
10069 * - there is no valid screen
10070 * - the screen has to be redrawn completely
10071 * - the line count is less than one
10072 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010073 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010075 if (!screen_valid(TRUE) || line_count <= 0
10076 || (!force && line_count > p_ttyscroll)
10077#ifdef FEAT_CLIPBOARD
10078 || (clip_star.state != SELECT_CLEARED
10079 && redrawing_for_callback > 0)
10080#endif
10081 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 return FAIL;
10083
10084 /*
10085 * Check if the rest of the current region will become empty.
10086 */
10087 result_empty = row + line_count >= end;
10088
10089 /*
10090 * We can delete lines only when 'db' flag not set or when 'ce' option
10091 * available.
10092 */
10093 can_delete = (*T_DB == NUL || can_clear(T_CE));
10094
10095 /*
10096 * There are six ways to delete lines:
10097 * 0. When in a vertically split window and t_CV isn't set, redraw the
10098 * characters from ScreenLines[].
10099 * 1. Use T_CD if it exists and the result is empty.
10100 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10101 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10102 * none of the other ways work.
10103 * 4. Use T_CE (erase line) if the result is empty.
10104 * 5. Use T_DL (delete line) if it exists.
10105 * 6. redraw the characters from ScreenLines[].
10106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10108 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010109 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 type = USE_T_CD;
10111#if defined(__BEOS__) && defined(BEOS_DR8)
10112 /*
10113 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10114 * its internal termcap... this works okay for tests which test *T_DB !=
10115 * NUL. It has the disadvantage that the user cannot use any :set t_*
10116 * command to get T_DB (back) to empty_option, only :set term=... will do
10117 * the trick...
10118 * Anyway, this hack will hopefully go away with the next OS release.
10119 * (Olaf Seibert)
10120 */
10121 else if (row == 0 && T_DB == empty_option
10122 && (line_count == 1 || *T_CDL == NUL))
10123#else
10124 else if (row == 0 && (
10125#ifndef AMIGA
10126 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10127 * up, so use delete-line command */
10128 line_count == 1 ||
10129#endif
10130 *T_CDL == NUL))
10131#endif
10132 type = USE_NL;
10133 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10134 type = USE_T_CDL;
10135 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010136 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 type = USE_T_CE;
10138 else if (*T_DL != NUL && can_delete)
10139 type = USE_T_DL;
10140 else if (*T_CDL != NUL && can_delete)
10141 type = USE_T_CDL;
10142 else
10143 return FAIL;
10144
10145#ifdef FEAT_CLIPBOARD
10146 /* Remove a modeless selection when deleting lines halfway the screen or
10147 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010148 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010149 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else
10151 clip_scroll_selection(line_count);
10152#endif
10153
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154#ifdef FEAT_GUI
10155 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10156 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010157 gui_dont_update_cursor(gui.cursor_row >= row + off
10158 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159#endif
10160
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010161 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10162 cursor_col = wp->w_wincol;
10163
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 if (*T_CCS != NUL) /* cursor relative to region */
10165 {
10166 cursor_row = row;
10167 cursor_end = end;
10168 }
10169 else
10170 {
10171 cursor_row = row + off;
10172 cursor_end = end + off;
10173 }
10174
10175 /*
10176 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10177 * Clear the inserted lines in ScreenLines[].
10178 */
10179 row += off;
10180 end += off;
10181 for (i = 0; i < line_count; ++i)
10182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 if (wp != NULL && wp->w_width != Columns)
10184 {
10185 /* need to copy part of a line */
10186 j = row + i;
10187 while ((j += line_count) <= end - 1)
10188 linecopy(j - line_count, j, wp);
10189 j -= line_count;
10190 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010191 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10192 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 else
10194 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10195 LineWraps[j] = FALSE;
10196 }
10197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
10199 /* whole width, moving the line pointers is faster */
10200 j = row + i;
10201 temp = LineOffset[j];
10202 while ((j += line_count) <= end - 1)
10203 {
10204 LineOffset[j - line_count] = LineOffset[j];
10205 LineWraps[j - line_count] = LineWraps[j];
10206 }
10207 LineOffset[j - line_count] = temp;
10208 LineWraps[j - line_count] = FALSE;
10209 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010210 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 else
10212 lineinvalid(temp, (int)Columns);
10213 }
10214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010216 if (screen_attr != clear_attr)
10217 screen_stop_highlight();
10218 if (clear_attr != 0)
10219 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 /* redraw the characters */
10222 if (type == USE_REDRAW)
10223 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010224 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010226 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 out_str(T_CD);
10228 screen_start(); /* don't know where cursor is now */
10229 }
10230 else if (type == USE_T_CDL)
10231 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010232 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 term_delete_lines(line_count);
10234 screen_start(); /* don't know where cursor is now */
10235 }
10236 /*
10237 * Deleting lines at top of the screen or scroll region: Just scroll
10238 * the whole screen (scroll region) up by outputting newlines on the
10239 * last line.
10240 */
10241 else if (type == USE_NL)
10242 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010243 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 for (i = line_count; --i >= 0; )
10245 out_char('\n'); /* cursor will remain on same line */
10246 }
10247 else
10248 {
10249 for (i = line_count; --i >= 0; )
10250 {
10251 if (type == USE_T_DL)
10252 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010253 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 out_str(T_DL); /* delete a line */
10255 }
10256 else /* type == USE_T_CE */
10257 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010258 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 out_str(T_CE); /* erase a line */
10260 }
10261 screen_start(); /* don't know where cursor is now */
10262 }
10263 }
10264
10265 /*
10266 * If the 'db' flag is set, we need to clear the lines that have been
10267 * scrolled up at the bottom of the region.
10268 */
10269 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10270 {
10271 for (i = line_count; i > 0; --i)
10272 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010273 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 out_str(T_CE); /* erase a line */
10275 screen_start(); /* don't know where cursor is now */
10276 }
10277 }
10278
10279#ifdef FEAT_GUI
10280 gui_can_update_cursor();
10281 if (gui.in_use)
10282 out_flush(); /* always flush after a scroll */
10283#endif
10284
10285 return OK;
10286}
10287
10288/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010289 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 *
10291 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10292 * If clear_cmdline is FALSE there may be a message there that needs to be
10293 * cleared only if a mode is shown.
10294 * Return the length of the message (0 if no message).
10295 */
10296 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010297showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298{
10299 int need_clear;
10300 int length = 0;
10301 int do_mode;
10302 int attr;
10303 int nwr_save;
10304#ifdef FEAT_INS_EXPAND
10305 int sub_attr;
10306#endif
10307
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010308 do_mode = ((p_smd && msg_silent == 0)
10309 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010310 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010311 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010312 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 {
10314 /*
10315 * Don't show mode right now, when not redrawing or inside a mapping.
10316 * Call char_avail() only when we are going to show something, because
10317 * it takes a bit of time.
10318 */
10319 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10320 {
10321 redraw_cmdline = TRUE; /* show mode later */
10322 return 0;
10323 }
10324
10325 nwr_save = need_wait_return;
10326
10327 /* wait a bit before overwriting an important message */
10328 check_for_delay(FALSE);
10329
10330 /* if the cmdline is more than one line high, erase top lines */
10331 need_clear = clear_cmdline;
10332 if (clear_cmdline && cmdline_row < Rows - 1)
10333 msg_clr_cmdline(); /* will reset clear_cmdline */
10334
10335 /* Position on the last line in the window, column 0 */
10336 msg_pos_mode();
10337 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010338 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 if (do_mode)
10340 {
10341 MSG_PUTS_ATTR("--", attr);
10342#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010343 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010344# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010345 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010346# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010347 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010348# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010349 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010350# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 MSG_PUTS_ATTR(" IM", attr);
10352# else
10353 MSG_PUTS_ATTR(" XIM", attr);
10354# endif
10355#endif
10356#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10357 if (gui.in_use)
10358 {
10359 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010360 {
10361 /* HANGUL */
10362 if (enc_utf8)
10363 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10364 else
10365 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 }
10368#endif
10369#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010370 /* CTRL-X in Insert mode */
10371 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 {
10373 /* These messages can get long, avoid a wrap in a narrow
10374 * window. Prefer showing edit_submode_extra. */
10375 length = (Rows - msg_row) * Columns - 3;
10376 if (edit_submode_extra != NULL)
10377 length -= vim_strsize(edit_submode_extra);
10378 if (length > 0)
10379 {
10380 if (edit_submode_pre != NULL)
10381 length -= vim_strsize(edit_submode_pre);
10382 if (length - vim_strsize(edit_submode) > 0)
10383 {
10384 if (edit_submode_pre != NULL)
10385 msg_puts_attr(edit_submode_pre, attr);
10386 msg_puts_attr(edit_submode, attr);
10387 }
10388 if (edit_submode_extra != NULL)
10389 {
10390 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10391 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010392 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 else
10394 sub_attr = attr;
10395 msg_puts_attr(edit_submode_extra, sub_attr);
10396 }
10397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 }
10399 else
10400#endif
10401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 if (State & VREPLACE_FLAG)
10403 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010404 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10406 else if (State & INSERT)
10407 {
10408#ifdef FEAT_RIGHTLEFT
10409 if (p_ri)
10410 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10411#endif
10412 MSG_PUTS_ATTR(_(" INSERT"), attr);
10413 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010414 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 MSG_PUTS_ATTR(_(" (insert)"), attr);
10416 else if (restart_edit == 'R')
10417 MSG_PUTS_ATTR(_(" (replace)"), attr);
10418 else if (restart_edit == 'V')
10419 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10420#ifdef FEAT_RIGHTLEFT
10421 if (p_hkmap)
10422 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10423# ifdef FEAT_FKMAP
10424 if (p_fkmap)
10425 MSG_PUTS_ATTR(farsi_text_5, attr);
10426# endif
10427#endif
10428#ifdef FEAT_KEYMAP
10429 if (State & LANGMAP)
10430 {
10431# ifdef FEAT_ARABIC
10432 if (curwin->w_p_arab)
10433 MSG_PUTS_ATTR(_(" Arabic"), attr);
10434 else
10435# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010436 if (get_keymap_str(curwin, (char_u *)" (%s)",
10437 NameBuff, MAXPATHL))
10438 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 }
10440#endif
10441 if ((State & INSERT) && p_paste)
10442 MSG_PUTS_ATTR(_(" (paste)"), attr);
10443
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 if (VIsual_active)
10445 {
10446 char *p;
10447
10448 /* Don't concatenate separate words to avoid translation
10449 * problems. */
10450 switch ((VIsual_select ? 4 : 0)
10451 + (VIsual_mode == Ctrl_V) * 2
10452 + (VIsual_mode == 'V'))
10453 {
10454 case 0: p = N_(" VISUAL"); break;
10455 case 1: p = N_(" VISUAL LINE"); break;
10456 case 2: p = N_(" VISUAL BLOCK"); break;
10457 case 4: p = N_(" SELECT"); break;
10458 case 5: p = N_(" SELECT LINE"); break;
10459 default: p = N_(" SELECT BLOCK"); break;
10460 }
10461 MSG_PUTS_ATTR(_(p), attr);
10462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 MSG_PUTS_ATTR(" --", attr);
10464 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010465
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 need_clear = TRUE;
10467 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010468 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469#ifdef FEAT_INS_EXPAND
10470 && edit_submode == NULL /* otherwise it gets too long */
10471#endif
10472 )
10473 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010474 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 need_clear = TRUE;
10476 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010477
10478 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 if (need_clear || clear_cmdline)
10480 msg_clr_eos();
10481 msg_didout = FALSE; /* overwrite this message */
10482 length = msg_col;
10483 msg_col = 0;
10484 need_wait_return = nwr_save; /* never ask for hit-return for this */
10485 }
10486 else if (clear_cmdline && msg_silent == 0)
10487 /* Clear the whole command line. Will reset "clear_cmdline". */
10488 msg_clr_cmdline();
10489
10490#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 /* In Visual mode the size of the selected area must be redrawn. */
10492 if (VIsual_active)
10493 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494
10495 /* If the last window has no status line, the ruler is after the mode
10496 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010497 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010498 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499#endif
10500 redraw_cmdline = FALSE;
10501 clear_cmdline = FALSE;
10502
10503 return length;
10504}
10505
10506/*
10507 * Position for a mode message.
10508 */
10509 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010510msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512 msg_col = 0;
10513 msg_row = Rows - 1;
10514}
10515
10516/*
10517 * Delete mode message. Used when ESC is typed which is expected to end
10518 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010519 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 */
10521 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010522unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010525 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 */
10527 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10528 redraw_cmdline = TRUE; /* delete mode later */
10529 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010530 clearmode();
10531}
10532
10533/*
10534 * Clear the mode message.
10535 */
10536 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010537clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010538{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010539 int save_msg_row = msg_row;
10540 int save_msg_col = msg_col;
10541
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010542 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010543 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010544 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010545 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010546
10547 msg_col = save_msg_col;
10548 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549}
10550
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010551 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010552recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010553{
10554 MSG_PUTS_ATTR(_("recording"), attr);
10555 if (!shortmess(SHM_RECORDING))
10556 {
10557 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010558 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010559 MSG_PUTS_ATTR(s, attr);
10560 }
10561}
10562
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010563/*
10564 * Draw the tab pages line at the top of the Vim window.
10565 */
10566 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010567draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010568{
10569 int tabcount = 0;
10570 tabpage_T *tp;
10571 int tabwidth;
10572 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010573 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010574 int attr;
10575 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010576 win_T *cwp;
10577 int wincount;
10578 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010579 int c;
10580 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010581 int attr_sel = HL_ATTR(HLF_TPS);
10582 int attr_nosel = HL_ATTR(HLF_TP);
10583 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010584 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010585 int room;
10586 int use_sep_chars = (t_colors < 8
10587#ifdef FEAT_GUI
10588 && !gui.in_use
10589#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010590#ifdef FEAT_TERMGUICOLORS
10591 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010592#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010593 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010594
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010595 if (ScreenLines == NULL)
10596 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010597 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010598
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010599#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010600 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010601 if (gui_use_tabline())
10602 {
10603 gui_update_tabline();
10604 return;
10605 }
10606#endif
10607
10608 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010609 return;
10610
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010611#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010612
10613 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10614 for (scol = 0; scol < Columns; ++scol)
10615 TabPageIdxs[scol] = 0;
10616
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010617 /* Use the 'tabline' option if it's set. */
10618 if (*p_tal != NUL)
10619 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010620 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621
10622 /* Check for an error. If there is one we would loop in redrawing the
10623 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010624 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010625 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010626 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010628 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010629 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010630 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010631 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010632#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010633 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010634 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010636
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10638 if (tabwidth < 6)
10639 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010640
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 attr = attr_nosel;
10642 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010643 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010644 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10645 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010646 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010648
Bram Moolenaar238a5642006-02-21 22:12:05 +000010649 if (tp->tp_topframe == topframe)
10650 attr = attr_sel;
10651 if (use_sep_chars && col > 0)
10652 screen_putchar('|', 0, col++, attr);
10653
10654 if (tp->tp_topframe != topframe)
10655 attr = attr_nosel;
10656
10657 screen_putchar(' ', 0, col++, attr);
10658
10659 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010660 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010661 cwp = curwin;
10662 wp = firstwin;
10663 }
10664 else
10665 {
10666 cwp = tp->tp_curwin;
10667 wp = tp->tp_firstwin;
10668 }
10669
10670 modified = FALSE;
10671 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10672 if (bufIsChanged(wp->w_buffer))
10673 modified = TRUE;
10674 if (modified || wincount > 1)
10675 {
10676 if (wincount > 1)
10677 {
10678 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010679 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010680 if (col + len >= Columns - 3)
10681 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010682 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010683#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010684 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010685#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010686 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010687#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 );
10689 col += len;
10690 }
10691 if (modified)
10692 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10693 screen_putchar(' ', 0, col++, attr);
10694 }
10695
10696 room = scol - col + tabwidth - 1;
10697 if (room > 0)
10698 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010699 /* Get buffer name in NameBuff[] */
10700 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010701 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010702 len = vim_strsize(NameBuff);
10703 p = NameBuff;
10704#ifdef FEAT_MBYTE
10705 if (has_mbyte)
10706 while (len > room)
10707 {
10708 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010709 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710 }
10711 else
10712#endif
10713 if (len > room)
10714 {
10715 p += len - room;
10716 len = room;
10717 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010718 if (len > Columns - col - 1)
10719 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010720
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010721 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010722 col += len;
10723 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010724 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010725
10726 /* Store the tab page number in TabPageIdxs[], so that
10727 * jump_to_mouse() knows where each one is. */
10728 ++tabcount;
10729 while (scol < col)
10730 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010731 }
10732
Bram Moolenaar238a5642006-02-21 22:12:05 +000010733 if (use_sep_chars)
10734 c = '_';
10735 else
10736 c = ' ';
10737 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010738
10739 /* Put an "X" for closing the current tab if there are several. */
10740 if (first_tabpage->tp_next != NULL)
10741 {
10742 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10743 TabPageIdxs[Columns - 1] = -999;
10744 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010745 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010746
10747 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10748 * set. */
10749 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010750}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010751
10752/*
10753 * Get buffer name for "buf" into NameBuff[].
10754 * Takes care of special buffer names and translates special characters.
10755 */
10756 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010757get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010758{
10759 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010760 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010761 else
10762 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10763 trans_characters(NameBuff, MAXPATHL);
10764}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010765
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766/*
10767 * Get the character to use in a status line. Get its attributes in "*attr".
10768 */
10769 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010770fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771{
10772 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773
10774#ifdef FEAT_TERMINAL
10775 if (bt_terminal(wp->w_buffer))
10776 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010777 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010778 {
10779 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010780 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010781 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010782 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010783 {
10784 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010785 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010786 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010787 }
10788 else
10789#endif
10790 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010792 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 fill = fill_stl;
10794 }
10795 else
10796 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010797 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 fill = fill_stlnc;
10799 }
10800 /* Use fill when there is highlighting, and highlighting of current
10801 * window differs, or the fillchars differ, or this is not the
10802 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010803 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010804 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 || (fill_stl != fill_stlnc)))
10806 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010807 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 return '^';
10809 return '=';
10810}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812/*
10813 * Get the character to use in a separator between vertically split windows.
10814 * Get its attributes in "*attr".
10815 */
10816 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010817fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010819 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (*attr == 0 && fill_vert == ' ')
10821 return '|';
10822 else
10823 return fill_vert;
10824}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825
10826/*
10827 * Return TRUE if redrawing should currently be done.
10828 */
10829 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010830redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010832#ifdef FEAT_EVAL
10833 if (disable_redraw_for_testing)
10834 return 0;
10835 else
10836#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010837 return ((!RedrawingDisabled
10838#ifdef FEAT_EVAL
10839 || ignore_redraw_flag_for_testing
10840#endif
10841 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842}
10843
10844/*
10845 * Return TRUE if printing messages should currently be done.
10846 */
10847 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010848messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849{
10850 return (!(p_lz && char_avail() && !KeyTyped));
10851}
10852
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010853#ifdef FEAT_MENU
10854/*
10855 * Draw the window toolbar.
10856 */
10857 static void
10858redraw_win_toolbar(win_T *wp)
10859{
10860 vimmenu_T *menu;
10861 int item_idx = 0;
10862 int item_count = 0;
10863 int col = 0;
10864 int next_col;
10865 int off = (int)(current_ScreenLine - ScreenLines);
10866 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10867 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10868
10869 vim_free(wp->w_winbar_items);
10870 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10871 ++item_count;
10872 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10873 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10874
10875 /* TODO: use fewer spaces if there is not enough room */
10876 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010877 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010878 {
10879 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010880 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010881 break;
10882 if (col > 1)
10883 {
10884 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010885 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010886 break;
10887 }
10888
10889 wp->w_winbar_items[item_idx].wb_startcol = col;
10890 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010891 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010892 break;
10893
10894 next_col = text_to_screenline(wp, menu->name, col);
10895 while (col < next_col)
10896 {
10897 ScreenAttrs[off + col] = button_attr;
10898 ++col;
10899 }
10900 wp->w_winbar_items[item_idx].wb_endcol = col;
10901 wp->w_winbar_items[item_idx].wb_menu = menu;
10902 ++item_idx;
10903
Bram Moolenaar02631462017-09-22 15:20:32 +020010904 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010905 break;
10906 space_to_screenline(off + col, button_attr);
10907 ++col;
10908 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010909 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010910 {
10911 space_to_screenline(off + col, fill_attr);
10912 ++col;
10913 }
10914 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10915
Bram Moolenaar02631462017-09-22 15:20:32 +020010916 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10917 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010918}
10919#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010920
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921/*
10922 * Show current status info in ruler and various other places
10923 * If always is FALSE, only show ruler if position has changed.
10924 */
10925 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010926showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927{
10928 if (!always && !redrawing())
10929 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010930#ifdef FEAT_INS_EXPAND
10931 if (pum_visible())
10932 {
10933 /* Don't redraw right now, do it later. */
10934 curwin->w_redr_status = TRUE;
10935 return;
10936 }
10937#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010938#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010939 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010940 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010941 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 else
10944#endif
10945#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010946 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947#endif
10948
10949#ifdef FEAT_TITLE
10950 if (need_maketitle
10951# ifdef FEAT_STL_OPT
10952 || (p_icon && (stl_syntax & STL_IN_ICON))
10953 || (p_title && (stl_syntax & STL_IN_TITLE))
10954# endif
10955 )
10956 maketitle();
10957#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010958 /* Redraw the tab pages line if needed. */
10959 if (redraw_tabline)
10960 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961}
10962
10963#ifdef FEAT_CMDL_INFO
10964 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010965win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010967#define RULER_BUF_LEN 70
10968 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 int row;
10970 int fillchar;
10971 int attr;
10972 int empty_line = FALSE;
10973 colnr_T virtcol;
10974 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010975 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 int this_ru_col;
10978 int off = 0;
10979 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980
10981 /* If 'ruler' off or redrawing disabled, don't do anything */
10982 if (!p_ru)
10983 return;
10984
10985 /*
10986 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10987 * after deleting lines, before cursor.lnum is corrected.
10988 */
10989 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10990 return;
10991
10992#ifdef FEAT_INS_EXPAND
10993 /* Don't draw the ruler while doing insert-completion, it might overwrite
10994 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 if (edit_submode != NULL)
10997 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010998 // Don't draw the ruler when the popup menu is visible, it may overlap.
10999 // Except when the popup menu will be redrawn anyway.
11000 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011001 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002#endif
11003
11004#ifdef FEAT_STL_OPT
11005 if (*p_ruf)
11006 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011007 int save_called_emsg = called_emsg;
11008
11009 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011011 if (called_emsg)
11012 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011013 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011014 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 return;
11016 }
11017#endif
11018
11019 /*
11020 * Check if not in Insert mode and the line is empty (will show "0-1").
11021 */
11022 if (!(State & INSERT)
11023 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11024 empty_line = TRUE;
11025
11026 /*
11027 * Only draw the ruler when something changed.
11028 */
11029 validate_virtcol_win(wp);
11030 if ( redraw_cmdline
11031 || always
11032 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11033 || wp->w_cursor.col != wp->w_ru_cursor.col
11034 || wp->w_virtcol != wp->w_ru_virtcol
11035#ifdef FEAT_VIRTUALEDIT
11036 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11037#endif
11038 || wp->w_topline != wp->w_ru_topline
11039 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11040#ifdef FEAT_DIFF
11041 || wp->w_topfill != wp->w_ru_topfill
11042#endif
11043 || empty_line != wp->w_ru_empty)
11044 {
11045 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 if (wp->w_status_height)
11047 {
11048 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011049 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011050 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011051 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 }
11053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 {
11055 row = Rows - 1;
11056 fillchar = ' ';
11057 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 width = Columns;
11059 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 }
11061
11062 /* In list mode virtcol needs to be recomputed */
11063 virtcol = wp->w_virtcol;
11064 if (wp->w_p_list && lcs_tab1 == NUL)
11065 {
11066 wp->w_p_list = FALSE;
11067 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11068 wp->w_p_list = TRUE;
11069 }
11070
11071 /*
11072 * Some sprintfs return the length, some return a pointer.
11073 * To avoid portability problems we use strlen() here.
11074 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011075 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11077 ? 0L
11078 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011079 len = STRLEN(buffer);
11080 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11082 (int)virtcol + 1);
11083
11084 /*
11085 * Add a "50%" if there is room for it.
11086 * On the last line, don't print in the last column (scrolls the
11087 * screen up on some terminals).
11088 */
11089 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011090 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 this_ru_col = ru_col - (Columns - width);
11095 if (this_ru_col < 0)
11096 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 /* Never use more than half the window/screen width, leave the other
11098 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011099 if (this_ru_col < (width + 1) / 2)
11100 this_ru_col = (width + 1) / 2;
11101 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011103 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011104 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 {
11106#ifdef FEAT_MBYTE
11107 if (has_mbyte)
11108 i += (*mb_char2bytes)(fillchar, buffer + i);
11109 else
11110#endif
11111 buffer[i++] = fillchar;
11112 ++o;
11113 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011114 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 }
11116 /* Truncate at window boundary. */
11117#ifdef FEAT_MBYTE
11118 if (has_mbyte)
11119 {
11120 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011121 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 {
11123 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011124 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 {
11126 buffer[i] = NUL;
11127 break;
11128 }
11129 }
11130 }
11131 else
11132#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011133 if (this_ru_col + (int)STRLEN(buffer) > width)
11134 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135
Bram Moolenaar4033c552017-09-16 20:54:51 +020011136 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 i = redraw_cmdline;
11138 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011139 this_ru_col + off + (int)STRLEN(buffer),
11140 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 fillchar, fillchar, attr);
11142 /* don't redraw the cmdline because of showing the ruler */
11143 redraw_cmdline = i;
11144 wp->w_ru_cursor = wp->w_cursor;
11145 wp->w_ru_virtcol = wp->w_virtcol;
11146 wp->w_ru_empty = empty_line;
11147 wp->w_ru_topline = wp->w_topline;
11148 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11149#ifdef FEAT_DIFF
11150 wp->w_ru_topfill = wp->w_topfill;
11151#endif
11152 }
11153}
11154#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011155
11156#if defined(FEAT_LINEBREAK) || defined(PROTO)
11157/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011158 * Return the width of the 'number' and 'relativenumber' column.
11159 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011160 * Otherwise it depends on 'numberwidth' and the line count.
11161 */
11162 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011163number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011164{
11165 int n;
11166 linenr_T lnum;
11167
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011168 if (wp->w_p_rnu && !wp->w_p_nu)
11169 /* cursor line shows "0" */
11170 lnum = wp->w_height;
11171 else
11172 /* cursor line shows absolute line number */
11173 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011174
Bram Moolenaar6b314672015-03-20 15:42:10 +010011175 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011176 return wp->w_nrwidth_width;
11177 wp->w_nrwidth_line_count = lnum;
11178
11179 n = 0;
11180 do
11181 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011182 lnum /= 10;
11183 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011184 } while (lnum > 0);
11185
11186 /* 'numberwidth' gives the minimal width plus one */
11187 if (n < wp->w_p_nuw - 1)
11188 n = wp->w_p_nuw - 1;
11189
11190 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011191 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011192 return n;
11193}
11194#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011195
11196/*
11197 * Return the current cursor column. This is the actual position on the
11198 * screen. First column is 0.
11199 */
11200 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011201screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011202{
11203 return screen_cur_col;
11204}
11205
11206/*
11207 * Return the current cursor row. This is the actual position on the screen.
11208 * First row is 0.
11209 */
11210 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011211screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011212{
11213 return screen_cur_row;
11214}