blob: eac50c52c81510868d02b413d194709fa4823f70 [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 Moolenaarf3d769a2017-09-22 13:44:56 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange);
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)
450 ; /* do nothing */
451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar86033562017-07-12 20:24:41 +0200453 /* Redrawing only works when the screen didn't scroll. Don't clear
454 * wildmenu entries. */
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200455 if (msg_scrolled == 0
456#ifdef FEAT_WILDMENU
457 && wild_menu_showing == 0
458#endif
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200459 && call_update_screen)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 update_screen(0);
Bram Moolenaar86033562017-07-12 20:24:41 +0200461 /* Redraw in the same position, so that the user can continue
462 * editing the command. */
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200463 redrawcmdline_ex(FALSE);
464 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200465 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100466 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200467 /* keep the command line if possible */
468 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100469 setcursor();
470 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100472#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100473 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200474 /* Don't update the cursor when it is blinking and off to avoid
475 * flicker. */
Bram Moolenaara338adc2018-01-31 20:51:47 +0100476 out_flush_cursor(FALSE, FALSE);
477 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100479 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200480
481 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482}
483
484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * Changed something in the current window, at buffer line "lnum", that
486 * requires that line and possibly other lines to be redrawn.
487 * Used when entering/leaving Insert mode with the cursor on a folded line.
488 * Used to remove the "$" from a change command.
489 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
490 * may become invalid and the whole window will have to be redrawn.
491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493redrawWinline(
494 linenr_T lnum,
495 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
497#ifdef FEAT_FOLDING
498 int i;
499#endif
500
501 if (curwin->w_redraw_top == 0 || curwin->w_redraw_top > lnum)
502 curwin->w_redraw_top = lnum;
503 if (curwin->w_redraw_bot == 0 || curwin->w_redraw_bot < lnum)
504 curwin->w_redraw_bot = lnum;
505 redraw_later(VALID);
506
507#ifdef FEAT_FOLDING
508 if (invalid)
509 {
510 /* A w_lines[] entry for this lnum has become invalid. */
511 i = find_wl_entry(curwin, lnum);
512 if (i >= 0)
513 curwin->w_lines[i].wl_valid = FALSE;
514 }
515#endif
516}
517
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200518 void
519reset_updating_screen(int may_resize_shell UNUSED)
520{
521 updating_screen = FALSE;
522#ifdef FEAT_GUI
523 if (may_resize_shell)
524 gui_may_resize_shell();
525#endif
526#ifdef FEAT_TERMINAL
527 term_check_channel_closed_recently();
528#endif
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200532 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 */
534 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100535update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
537 redraw_curbuf_later(type);
538 update_screen(type);
539}
540
541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * Based on the current value of curwin->w_topline, transfer a screenfull
543 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200544 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200546 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200547update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200549 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 win_T *wp;
551 static int did_intro = FALSE;
552#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
553 int did_one;
554#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200555#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200556 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200557 int gui_cursor_col;
558 int gui_cursor_row;
559#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200560 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000562 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200564 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200566 if (type == VALID_NO_UPDATE)
567 {
568 no_update = TRUE;
569 type = 0;
570 }
571
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (must_redraw)
573 {
574 if (type < must_redraw) /* use maximal type */
575 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000576
577 /* must_redraw is reset here, so that when we run into some weird
578 * reason to redraw while busy redrawing (e.g., asynchronous
579 * scrolling), or update_topline() in win_update() will cause a
580 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 must_redraw = 0;
582 }
583
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200584 /* May need to update w_lines[]. */
585 if (curwin->w_lines_valid == 0 && type < NOT_VALID
586#ifdef FEAT_TERMINAL
587 && !term_do_update_window(curwin)
588#endif
589 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 type = NOT_VALID;
591
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000592 /* Postpone the redrawing when it's not needed and when being called
593 * recursively. */
594 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
596 redraw_later(type); /* remember type for next time */
597 must_redraw = type;
598 if (type > INVERTED_ALL)
599 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200600 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602
603 updating_screen = TRUE;
604#ifdef FEAT_SYN_HL
605 ++display_tick; /* let syntax code know we're in a next round of
606 * display updating */
607#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200608 if (no_update)
609 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /*
612 * if the screen was scrolled up when displaying a message, scroll it down
613 */
614 if (msg_scrolled)
615 {
616 clear_cmdline = TRUE;
617 if (msg_scrolled > Rows - 5) /* clearing is faster */
618 type = CLEAR;
619 else if (type != CLEAR)
620 {
621 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200622 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
623 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 type = CLEAR;
625 FOR_ALL_WINDOWS(wp)
626 {
627 if (W_WINROW(wp) < msg_scrolled)
628 {
629 if (W_WINROW(wp) + wp->w_height > msg_scrolled
630 && wp->w_redr_type < REDRAW_TOP
631 && wp->w_lines_valid > 0
632 && wp->w_topline == wp->w_lines[0].wl_lnum)
633 {
634 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
635 wp->w_redr_type = REDRAW_TOP;
636 }
637 else
638 {
639 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200640 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
641 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
644 }
645 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200646 if (!no_update)
647 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000648 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 }
650 msg_scrolled = 0;
651 need_wait_return = FALSE;
652 }
653
654 /* reset cmdline_row now (may have been changed temporarily) */
655 compute_cmdrow();
656
657 /* Check for changed highlighting */
658 if (need_highlight_changed)
659 highlight_changed();
660
661 if (type == CLEAR) /* first clear screen */
662 {
663 screenclear(); /* will reset clear_cmdline */
664 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200665 /* must_redraw may be set indirectly, avoid another redraw later */
666 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668
669 if (clear_cmdline) /* going to clear cmdline (done below) */
670 check_for_delay(FALSE);
671
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000672#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200673 /* Force redraw when width of 'number' or 'relativenumber' column
674 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200676 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
677 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000678 curwin->w_redr_type = NOT_VALID;
679#endif
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 /*
682 * Only start redrawing if there is really something to do.
683 */
684 if (type == INVERTED)
685 update_curswant();
686 if (curwin->w_redr_type < type
687 && !((type == VALID
688 && curwin->w_lines[0].wl_valid
689#ifdef FEAT_DIFF
690 && curwin->w_topfill == curwin->w_old_topfill
691 && curwin->w_botfill == curwin->w_old_botfill
692#endif
693 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000695 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
697 && curwin->w_old_visual_mode == VIsual_mode
698 && (curwin->w_valid & VALID_VIRTCOL)
699 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ))
701 curwin->w_redr_type = type;
702
Bram Moolenaar5a305422006-04-28 22:38:25 +0000703 /* Redraw the tab pages line if needed. */
704 if (redraw_tabline || type >= NOT_VALID)
705 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#ifdef FEAT_SYN_HL
708 /*
709 * Correct stored syntax highlighting info for changes in each displayed
710 * buffer. Each buffer must only be done once.
711 */
712 FOR_ALL_WINDOWS(wp)
713 {
714 if (wp->w_buffer->b_mod_set)
715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 win_T *wwp;
717
718 /* Check if we already did this buffer. */
719 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
720 if (wwp->w_buffer == wp->w_buffer)
721 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200722 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 syn_stack_apply_changes(wp->w_buffer);
724 }
725 }
726#endif
727
728 /*
729 * Go from top to bottom through the windows, redrawing the ones that need
730 * it.
731 */
732#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
733 did_one = FALSE;
734#endif
735#ifdef FEAT_SEARCH_EXTRA
736 search_hl.rm.regprog = NULL;
737#endif
738 FOR_ALL_WINDOWS(wp)
739 {
740 if (wp->w_redr_type != 0)
741 {
742 cursor_off();
743#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
744 if (!did_one)
745 {
746 did_one = TRUE;
747# ifdef FEAT_SEARCH_EXTRA
748 start_search_hl();
749# endif
750# ifdef FEAT_CLIPBOARD
751 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200752 if (clip_star.available && clip_isautosel_star())
753 clip_update_selection(&clip_star);
754 if (clip_plus.available && clip_isautosel_plus())
755 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756# endif
757#ifdef FEAT_GUI
758 /* Remove the cursor before starting to do anything, because
759 * scrolling may make it difficult to redraw the text under
760 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200761 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200762 {
763 gui_cursor_col = gui.cursor_col;
764 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200766 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#endif
769 }
770#endif
771 win_update(wp);
772 }
773
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 /* redraw status line after the window to minimize cursor movement */
775 if (wp->w_redr_status)
776 {
777 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200778 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781#if defined(FEAT_SEARCH_EXTRA)
782 end_search_hl();
783#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100784#ifdef FEAT_INS_EXPAND
785 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200786 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 /* Reset b_mod_set flags. Going through all windows is probably faster
790 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200791 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200794 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /* Clear or redraw the command line. Done last, because scrolling may
797 * mess up the command line. */
798 if (clear_cmdline || redraw_cmdline)
799 showmode();
800
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200801 if (no_update)
802 --no_win_do_lines_ins;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200805 if (!did_intro)
806 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 did_intro = TRUE;
808
809#ifdef FEAT_GUI
810 /* Redraw the cursor and update the scrollbars when all screen updating is
811 * done. */
812 if (gui.in_use)
813 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200814 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200815 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100816 mch_disable_flush();
817 out_flush(); /* required before updating the cursor */
818 mch_enable_flush();
819
Bram Moolenaar144445d2016-07-08 21:41:54 +0200820 /* Put the GUI position where the cursor was, gui_update_cursor()
821 * uses that. */
822 gui.col = gui_cursor_col;
823 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200824# ifdef FEAT_MBYTE
825 gui.col = mb_fix_col(gui.col, gui.row);
826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100828 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200829 screen_cur_col = gui.col;
830 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200831 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100832 else
833 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 gui_update_scrollbars(FALSE);
835 }
836#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200837 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838}
839
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100840#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
841/*
842 * Prepare for updating one or more windows.
843 * Caller must check for "updating_screen" already set to avoid recursiveness.
844 */
845 static void
846update_prepare(void)
847{
848 cursor_off();
849 updating_screen = TRUE;
850#ifdef FEAT_GUI
851 /* Remove the cursor before starting to do anything, because scrolling may
852 * make it difficult to redraw the text under it. */
853 if (gui.in_use)
854 gui_undraw_cursor();
855#endif
856#ifdef FEAT_SEARCH_EXTRA
857 start_search_hl();
858#endif
859}
860
861/*
862 * Finish updating one or more windows.
863 */
864 static void
865update_finish(void)
866{
867 if (redraw_cmdline)
868 showmode();
869
870# ifdef FEAT_SEARCH_EXTRA
871 end_search_hl();
872# endif
873
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200874 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100875
876# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100877 /* Redraw the cursor and update the scrollbars when all screen updating is
878 * done. */
879 if (gui.in_use)
880 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100881 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882 gui_update_scrollbars(FALSE);
883 }
884# endif
885}
886#endif
887
Bram Moolenaar860cae12010-06-05 23:22:07 +0200888#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200889/*
890 * Return TRUE if the cursor line in window "wp" may be concealed, according
891 * to the 'concealcursor' option.
892 */
893 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100894conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200895{
896 int c;
897
898 if (*wp->w_p_cocu == NUL)
899 return FALSE;
900 if (get_real_state() & VISUAL)
901 c = 'v';
902 else if (State & INSERT)
903 c = 'i';
904 else if (State & NORMAL)
905 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200906 else if (State & CMDLINE)
907 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908 else
909 return FALSE;
910 return vim_strchr(wp->w_p_cocu, c) != NULL;
911}
912
913/*
914 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
915 */
916 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200917conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200918{
919 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
920 {
921 need_cursor_line_redraw = TRUE;
922 /* Need to recompute cursor column, e.g., when starting Visual mode
923 * without concealing. */
924 curs_columns(TRUE);
925 }
926}
927
Bram Moolenaar860cae12010-06-05 23:22:07 +0200928 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100929update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200930{
931 int row;
932 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200933#ifdef SYN_TIME_LIMIT
934 proftime_T syntax_tm;
935#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200936
Bram Moolenaar908be432016-05-24 10:51:30 +0200937 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100938 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200939 return;
940
Bram Moolenaar860cae12010-06-05 23:22:07 +0200941 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200942 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200944#ifdef SYN_TIME_LIMIT
945 /* Set the time limit to 'redrawtime'. */
946 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200947 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200948#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100949 update_prepare();
950
Bram Moolenaar860cae12010-06-05 23:22:07 +0200951 row = 0;
952 for (j = 0; j < wp->w_lines_valid; ++j)
953 {
954 if (lnum == wp->w_lines[j].wl_lnum)
955 {
956 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200957# ifdef FEAT_SEARCH_EXTRA
958 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200959 start_search_hl();
960 prepare_search_hl(wp, lnum);
961# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200962 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200963# if defined(FEAT_SEARCH_EXTRA)
964 end_search_hl();
965# endif
966 break;
967 }
968 row += wp->w_lines[j].wl_size;
969 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100970
971 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200972
973#ifdef SYN_TIME_LIMIT
974 syn_set_timeout(NULL);
975#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200976 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200977 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978}
979#endif
980
981#if defined(FEAT_SIGNS) || defined(PROTO)
982 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100983update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984{
985 win_T *wp;
986 int doit = FALSE;
987
988# ifdef FEAT_FOLDING
989 win_foldinfo.fi_level = 0;
990# endif
991
992 /* update/delete a specific mark */
993 FOR_ALL_WINDOWS(wp)
994 {
995 if (buf != NULL && lnum > 0)
996 {
997 if (wp->w_buffer == buf && lnum >= wp->w_topline
998 && lnum < wp->w_botline)
999 {
1000 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1001 wp->w_redraw_top = lnum;
1002 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1003 wp->w_redraw_bot = lnum;
1004 redraw_win_later(wp, VALID);
1005 }
1006 }
1007 else
1008 redraw_win_later(wp, VALID);
1009 if (wp->w_redr_type != 0)
1010 doit = TRUE;
1011 }
1012
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001013 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001014 * happening (recursive call), messages on the screen or still starting up.
1015 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001016 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001017 || State == ASKMORE || State == HITRETURN
1018 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001019#ifdef FEAT_GUI
1020 || gui.starting
1021#endif
1022 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 return;
1024
1025 /* update all windows that need updating */
1026 update_prepare();
1027
Bram Moolenaar29323592016-07-24 22:04:11 +02001028 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 if (wp->w_redr_type != 0)
1031 win_update(wp);
1032 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001033 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
1036 update_finish();
1037}
1038#endif
1039
1040
1041#if defined(FEAT_GUI) || defined(PROTO)
1042/*
1043 * Update a single window, its status line and maybe the command line msg.
1044 * Used for the GUI scrollbar.
1045 */
1046 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001047updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001049 /* return if already busy updating */
1050 if (updating_screen)
1051 return;
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 update_prepare();
1054
1055#ifdef FEAT_CLIPBOARD
1056 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001057 if (clip_star.available && clip_isautosel_star())
1058 clip_update_selection(&clip_star);
1059 if (clip_plus.available && clip_isautosel_plus())
1060 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001065 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001066 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001067 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (wp->w_redr_status
1070# ifdef FEAT_CMDL_INFO
1071 || p_ru
1072# endif
1073# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001074 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075# endif
1076 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001077 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 update_finish();
1080}
1081#endif
1082
1083/*
1084 * Update a single window.
1085 *
1086 * This may cause the windows below it also to be redrawn (when clearing the
1087 * screen or scrolling lines).
1088 *
1089 * How the window is redrawn depends on wp->w_redr_type. Each type also
1090 * implies the one below it.
1091 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001092 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1094 * INVERTED redraw the changed part of the Visual area
1095 * INVERTED_ALL redraw the whole Visual area
1096 * VALID 1. scroll up/down to adjust for a changed w_topline
1097 * 2. update lines at the top when scrolled down
1098 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001099 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * b_mod_top and b_mod_bot.
1101 * - if wp->w_redraw_top non-zero, redraw lines between
1102 * wp->w_redraw_top and wp->w_redr_bot.
1103 * - continue redrawing when syntax status is invalid.
1104 * 4. if scrolled up, update lines at the bottom.
1105 * This results in three areas that may need updating:
1106 * top: from first row to top_end (when scrolled down)
1107 * mid: from mid_start to mid_end (update inversion or changed text)
1108 * bot: from bot_start to last row (when scrolled up)
1109 */
1110 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001111win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112{
1113 buf_T *buf = wp->w_buffer;
1114 int type;
1115 int top_end = 0; /* Below last row of the top area that needs
1116 updating. 0 when no top area updating. */
1117 int mid_start = 999;/* first row of the mid area that needs
1118 updating. 999 when no mid area updating. */
1119 int mid_end = 0; /* Below last row of the mid area that needs
1120 updating. 0 when no mid area updating. */
1121 int bot_start = 999;/* first row of the bot area that needs
1122 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int scrolled_down = FALSE; /* TRUE when scrolled down when
1124 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001126 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 int top_to_mod = FALSE; /* redraw above mod_top */
1128#endif
1129
1130 int row; /* current window row to display */
1131 linenr_T lnum; /* current buffer lnum to display */
1132 int idx; /* current index in w_lines[] */
1133 int srow; /* starting row of the current line */
1134
1135 int eof = FALSE; /* if TRUE, we hit the end of the file */
1136 int didline = FALSE; /* if TRUE, we finished the last line */
1137 int i;
1138 long j;
1139 static int recursive = FALSE; /* being called recursively */
1140 int old_botline = wp->w_botline;
1141#ifdef FEAT_FOLDING
1142 long fold_count;
1143#endif
1144#ifdef FEAT_SYN_HL
1145 /* remember what happened to the previous line, to know if
1146 * check_visual_highlight() can be used */
1147#define DID_NONE 1 /* didn't update a line */
1148#define DID_LINE 2 /* updated a normal line */
1149#define DID_FOLD 3 /* updated a folded line */
1150 int did_update = DID_NONE;
1151 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1152#endif
1153 linenr_T mod_top = 0;
1154 linenr_T mod_bot = 0;
1155#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1156 int save_got_int;
1157#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001158#ifdef SYN_TIME_LIMIT
1159 proftime_T syntax_tm;
1160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
1162 type = wp->w_redr_type;
1163
1164 if (type == NOT_VALID)
1165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 wp->w_lines_valid = 0;
1168 }
1169
1170 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001171 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 {
1173 wp->w_redr_type = 0;
1174 return;
1175 }
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 /* Window is zero-width: Only need to draw the separator. */
1178 if (wp->w_width == 0)
1179 {
1180 /* draw the vertical separator right of this window */
1181 draw_vsep_win(wp, 0);
1182 wp->w_redr_type = 0;
1183 return;
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001186#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001187 // If this window contains a terminal, redraw works completely differently.
1188 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001189 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001190 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001191# ifdef FEAT_MENU
1192 /* Draw the window toolbar, if there is one. */
1193 if (winbar_height(wp) > 0)
1194 redraw_win_toolbar(wp);
1195# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001196 wp->w_redr_type = 0;
1197 return;
1198 }
1199#endif
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001202 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
1204
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001205#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001206 /* Force redraw when width of 'number' or 'relativenumber' column
1207 * changes. */
1208 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001209 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001210 {
1211 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001212 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001213 }
1214 else
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1218 {
1219 /*
1220 * When there are both inserted/deleted lines and specific lines to be
1221 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1222 * everything (only happens when redrawing is off for while).
1223 */
1224 type = NOT_VALID;
1225 }
1226 else
1227 {
1228 /*
1229 * Set mod_top to the first line that needs displaying because of
1230 * changes. Set mod_bot to the first line after the changes.
1231 */
1232 mod_top = wp->w_redraw_top;
1233 if (wp->w_redraw_bot != 0)
1234 mod_bot = wp->w_redraw_bot + 1;
1235 else
1236 mod_bot = 0;
1237 wp->w_redraw_top = 0; /* reset for next time */
1238 wp->w_redraw_bot = 0;
1239 if (buf->b_mod_set)
1240 {
1241 if (mod_top == 0 || mod_top > buf->b_mod_top)
1242 {
1243 mod_top = buf->b_mod_top;
1244#ifdef FEAT_SYN_HL
1245 /* Need to redraw lines above the change that may be included
1246 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001247 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001249 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (mod_top < 1)
1251 mod_top = 1;
1252 }
1253#endif
1254 }
1255 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1256 mod_bot = buf->b_mod_bot;
1257
1258#ifdef FEAT_SEARCH_EXTRA
1259 /* When 'hlsearch' is on and using a multi-line search pattern, a
1260 * change in one line may make the Search highlighting in a
1261 * previous line invalid. Simple solution: redraw all visible
1262 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001263 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001265 if (search_hl.rm.regprog != NULL
1266 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001268 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001269 {
1270 cur = wp->w_match_head;
1271 while (cur != NULL)
1272 {
1273 if (cur->match.regprog != NULL
1274 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001275 {
1276 top_to_mod = TRUE;
1277 break;
1278 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001279 cur = cur->next;
1280 }
1281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#endif
1283 }
1284#ifdef FEAT_FOLDING
1285 if (mod_top != 0 && hasAnyFolding(wp))
1286 {
1287 linenr_T lnumt, lnumb;
1288
1289 /*
1290 * A change in a line can cause lines above it to become folded or
1291 * unfolded. Find the top most buffer line that may be affected.
1292 * If the line was previously folded and displayed, get the first
1293 * line of that fold. If the line is folded now, get the first
1294 * folded line. Use the minimum of these two.
1295 */
1296
1297 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1298 * the line below it. If there is no valid entry, use w_topline.
1299 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1300 * to this line. If there is no valid entry, use MAXLNUM. */
1301 lnumt = wp->w_topline;
1302 lnumb = MAXLNUM;
1303 for (i = 0; i < wp->w_lines_valid; ++i)
1304 if (wp->w_lines[i].wl_valid)
1305 {
1306 if (wp->w_lines[i].wl_lastlnum < mod_top)
1307 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1308 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1309 {
1310 lnumb = wp->w_lines[i].wl_lnum;
1311 /* When there is a fold column it might need updating
1312 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001313 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ++lnumb;
1315 }
1316 }
1317
1318 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1319 if (mod_top > lnumt)
1320 mod_top = lnumt;
1321
1322 /* Now do the same for the bottom line (one above mod_bot). */
1323 --mod_bot;
1324 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1325 ++mod_bot;
1326 if (mod_bot < lnumb)
1327 mod_bot = lnumb;
1328 }
1329#endif
1330
1331 /* When a change starts above w_topline and the end is below
1332 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001333 * If the end of the change is above w_topline: do like no change was
1334 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (mod_top != 0 && mod_top < wp->w_topline)
1336 {
1337 if (mod_bot > wp->w_topline)
1338 mod_top = wp->w_topline;
1339#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001340 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 top_end = 1;
1342#endif
1343 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001344
1345 /* When line numbers are displayed need to redraw all lines below
1346 * inserted/deleted lines. */
1347 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1348 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350
1351 /*
1352 * When only displaying the lines at the top, set top_end. Used when
1353 * window has scrolled down for msg_scrolled.
1354 */
1355 if (type == REDRAW_TOP)
1356 {
1357 j = 0;
1358 for (i = 0; i < wp->w_lines_valid; ++i)
1359 {
1360 j += wp->w_lines[i].wl_size;
1361 if (j >= wp->w_upd_rows)
1362 {
1363 top_end = j;
1364 break;
1365 }
1366 }
1367 if (top_end == 0)
1368 /* not found (cannot happen?): redraw everything */
1369 type = NOT_VALID;
1370 else
1371 /* top area defined, the rest is VALID */
1372 type = VALID;
1373 }
1374
Bram Moolenaar367329b2007-08-30 11:53:22 +00001375 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001376 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1377 * non-zero and thus not FALSE) will indicate that screenclear() was not
1378 * called. */
1379 if (screen_cleared)
1380 screen_cleared = MAYBE;
1381
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /*
1383 * If there are no changes on the screen that require a complete redraw,
1384 * handle three cases:
1385 * 1: we are off the top of the screen by a few lines: scroll down
1386 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1387 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1388 * w_lines[] that needs updating.
1389 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001390 if ((type == VALID || type == SOME_VALID
1391 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#ifdef FEAT_DIFF
1393 && !wp->w_botfill && !wp->w_old_botfill
1394#endif
1395 )
1396 {
1397 if (mod_top != 0 && wp->w_topline == mod_top)
1398 {
1399 /*
1400 * w_topline is the first changed line, the scrolling will be done
1401 * further down.
1402 */
1403 }
1404 else if (wp->w_lines[0].wl_valid
1405 && (wp->w_topline < wp->w_lines[0].wl_lnum
1406#ifdef FEAT_DIFF
1407 || (wp->w_topline == wp->w_lines[0].wl_lnum
1408 && wp->w_topfill > wp->w_old_topfill)
1409#endif
1410 ))
1411 {
1412 /*
1413 * New topline is above old topline: May scroll down.
1414 */
1415#ifdef FEAT_FOLDING
1416 if (hasAnyFolding(wp))
1417 {
1418 linenr_T ln;
1419
1420 /* count the number of lines we are off, counting a sequence
1421 * of folded lines as one */
1422 j = 0;
1423 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1424 {
1425 ++j;
1426 if (j >= wp->w_height - 2)
1427 break;
1428 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1429 }
1430 }
1431 else
1432#endif
1433 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1434 if (j < wp->w_height - 2) /* not too far off */
1435 {
1436 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1437#ifdef FEAT_DIFF
1438 /* insert extra lines for previously invisible filler lines */
1439 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1440 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1441 - wp->w_old_topfill;
1442#endif
1443 if (i < wp->w_height - 2) /* less than a screen off */
1444 {
1445 /*
1446 * Try to insert the correct number of lines.
1447 * If not the last window, delete the lines at the bottom.
1448 * win_ins_lines may fail when the terminal can't do it.
1449 */
1450 if (i > 0)
1451 check_for_delay(FALSE);
1452 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1453 {
1454 if (wp->w_lines_valid != 0)
1455 {
1456 /* Need to update rows that are new, stop at the
1457 * first one that scrolled down. */
1458 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
1461 /* Move the entries that were scrolled, disable
1462 * the entries for the lines to be redrawn. */
1463 if ((wp->w_lines_valid += j) > wp->w_height)
1464 wp->w_lines_valid = wp->w_height;
1465 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1466 wp->w_lines[idx] = wp->w_lines[idx - j];
1467 while (idx >= 0)
1468 wp->w_lines[idx--].wl_valid = FALSE;
1469 }
1470 }
1471 else
1472 mid_start = 0; /* redraw all lines */
1473 }
1474 else
1475 mid_start = 0; /* redraw all lines */
1476 }
1477 else
1478 mid_start = 0; /* redraw all lines */
1479 }
1480 else
1481 {
1482 /*
1483 * New topline is at or below old topline: May scroll up.
1484 * When topline didn't change, find first entry in w_lines[] that
1485 * needs updating.
1486 */
1487
1488 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1489 j = -1;
1490 row = 0;
1491 for (i = 0; i < wp->w_lines_valid; i++)
1492 {
1493 if (wp->w_lines[i].wl_valid
1494 && wp->w_lines[i].wl_lnum == wp->w_topline)
1495 {
1496 j = i;
1497 break;
1498 }
1499 row += wp->w_lines[i].wl_size;
1500 }
1501 if (j == -1)
1502 {
1503 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1504 * lines */
1505 mid_start = 0;
1506 }
1507 else
1508 {
1509 /*
1510 * Try to delete the correct number of lines.
1511 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1512 */
1513#ifdef FEAT_DIFF
1514 /* If the topline didn't change, delete old filler lines,
1515 * otherwise delete filler lines of the new topline... */
1516 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1517 row += wp->w_old_topfill;
1518 else
1519 row += diff_check_fill(wp, wp->w_topline);
1520 /* ... but don't delete new filler lines. */
1521 row -= wp->w_topfill;
1522#endif
1523 if (row > 0)
1524 {
1525 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001526 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1527 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 bot_start = wp->w_height - row;
1529 else
1530 mid_start = 0; /* redraw all lines */
1531 }
1532 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1533 {
1534 /*
1535 * Skip the lines (below the deleted lines) that are still
1536 * valid and don't need redrawing. Copy their info
1537 * upwards, to compensate for the deleted lines. Set
1538 * bot_start to the first row that needs redrawing.
1539 */
1540 bot_start = 0;
1541 idx = 0;
1542 for (;;)
1543 {
1544 wp->w_lines[idx] = wp->w_lines[j];
1545 /* stop at line that didn't fit, unless it is still
1546 * valid (no lines deleted) */
1547 if (row > 0 && bot_start + row
1548 + (int)wp->w_lines[j].wl_size > wp->w_height)
1549 {
1550 wp->w_lines_valid = idx + 1;
1551 break;
1552 }
1553 bot_start += wp->w_lines[idx++].wl_size;
1554
1555 /* stop at the last valid entry in w_lines[].wl_size */
1556 if (++j >= wp->w_lines_valid)
1557 {
1558 wp->w_lines_valid = idx;
1559 break;
1560 }
1561 }
1562#ifdef FEAT_DIFF
1563 /* Correct the first entry for filler lines at the top
1564 * when it won't get updated below. */
1565 if (wp->w_p_diff && bot_start > 0)
1566 wp->w_lines[0].wl_size =
1567 plines_win_nofill(wp, wp->w_topline, TRUE)
1568 + wp->w_topfill;
1569#endif
1570 }
1571 }
1572 }
1573
1574 /* When starting redraw in the first line, redraw all lines. When
1575 * there is only one window it's probably faster to clear the screen
1576 * first. */
1577 if (mid_start == 0)
1578 {
1579 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001580 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001581 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001582 /* Clear the screen when it was not done by win_del_lines() or
1583 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1584 * then. */
1585 if (screen_cleared != TRUE)
1586 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001587 /* The screen was cleared, redraw the tab pages line. */
1588 if (redraw_tabline)
1589 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001592
1593 /* When win_del_lines() or win_ins_lines() caused the screen to be
1594 * cleared (only happens for the first window) or when screenclear()
1595 * was called directly above, "must_redraw" will have been set to
1596 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1597 if (screen_cleared == TRUE)
1598 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600 else
1601 {
1602 /* Not VALID or INVERTED: redraw all lines. */
1603 mid_start = 0;
1604 mid_end = wp->w_height;
1605 }
1606
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001607 if (type == SOME_VALID)
1608 {
1609 /* SOME_VALID: redraw all lines. */
1610 mid_start = 0;
1611 mid_end = wp->w_height;
1612 type = NOT_VALID;
1613 }
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 /* check if we are updating or removing the inverted part */
1616 if ((VIsual_active && buf == curwin->w_buffer)
1617 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1618 {
1619 linenr_T from, to;
1620
1621 if (VIsual_active)
1622 {
1623 if (VIsual_active
1624 && (VIsual_mode != wp->w_old_visual_mode
1625 || type == INVERTED_ALL))
1626 {
1627 /*
1628 * If the type of Visual selection changed, redraw the whole
1629 * selection. Also when the ownership of the X selection is
1630 * gained or lost.
1631 */
1632 if (curwin->w_cursor.lnum < VIsual.lnum)
1633 {
1634 from = curwin->w_cursor.lnum;
1635 to = VIsual.lnum;
1636 }
1637 else
1638 {
1639 from = VIsual.lnum;
1640 to = curwin->w_cursor.lnum;
1641 }
1642 /* redraw more when the cursor moved as well */
1643 if (wp->w_old_cursor_lnum < from)
1644 from = wp->w_old_cursor_lnum;
1645 if (wp->w_old_cursor_lnum > to)
1646 to = wp->w_old_cursor_lnum;
1647 if (wp->w_old_visual_lnum < from)
1648 from = wp->w_old_visual_lnum;
1649 if (wp->w_old_visual_lnum > to)
1650 to = wp->w_old_visual_lnum;
1651 }
1652 else
1653 {
1654 /*
1655 * Find the line numbers that need to be updated: The lines
1656 * between the old cursor position and the current cursor
1657 * position. Also check if the Visual position changed.
1658 */
1659 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1660 {
1661 from = curwin->w_cursor.lnum;
1662 to = wp->w_old_cursor_lnum;
1663 }
1664 else
1665 {
1666 from = wp->w_old_cursor_lnum;
1667 to = curwin->w_cursor.lnum;
1668 if (from == 0) /* Visual mode just started */
1669 from = to;
1670 }
1671
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001672 if (VIsual.lnum != wp->w_old_visual_lnum
1673 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
1675 if (wp->w_old_visual_lnum < from
1676 && wp->w_old_visual_lnum != 0)
1677 from = wp->w_old_visual_lnum;
1678 if (wp->w_old_visual_lnum > to)
1679 to = wp->w_old_visual_lnum;
1680 if (VIsual.lnum < from)
1681 from = VIsual.lnum;
1682 if (VIsual.lnum > to)
1683 to = VIsual.lnum;
1684 }
1685 }
1686
1687 /*
1688 * If in block mode and changed column or curwin->w_curswant:
1689 * update all lines.
1690 * First compute the actual start and end column.
1691 */
1692 if (VIsual_mode == Ctrl_V)
1693 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001694 colnr_T fromc, toc;
1695#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1696 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaar404406a2014-10-09 13:24:43 +02001698 if (curwin->w_p_lbr)
1699 ve_flags = VE_ALL;
1700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001702#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1703 ve_flags = save_ve_flags;
1704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 ++toc;
1706 if (curwin->w_curswant == MAXCOL)
1707 toc = MAXCOL;
1708
1709 if (fromc != wp->w_old_cursor_fcol
1710 || toc != wp->w_old_cursor_lcol)
1711 {
1712 if (from > VIsual.lnum)
1713 from = VIsual.lnum;
1714 if (to < VIsual.lnum)
1715 to = VIsual.lnum;
1716 }
1717 wp->w_old_cursor_fcol = fromc;
1718 wp->w_old_cursor_lcol = toc;
1719 }
1720 }
1721 else
1722 {
1723 /* Use the line numbers of the old Visual area. */
1724 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1725 {
1726 from = wp->w_old_cursor_lnum;
1727 to = wp->w_old_visual_lnum;
1728 }
1729 else
1730 {
1731 from = wp->w_old_visual_lnum;
1732 to = wp->w_old_cursor_lnum;
1733 }
1734 }
1735
1736 /*
1737 * There is no need to update lines above the top of the window.
1738 */
1739 if (from < wp->w_topline)
1740 from = wp->w_topline;
1741
1742 /*
1743 * If we know the value of w_botline, use it to restrict the update to
1744 * the lines that are visible in the window.
1745 */
1746 if (wp->w_valid & VALID_BOTLINE)
1747 {
1748 if (from >= wp->w_botline)
1749 from = wp->w_botline - 1;
1750 if (to >= wp->w_botline)
1751 to = wp->w_botline - 1;
1752 }
1753
1754 /*
1755 * Find the minimal part to be updated.
1756 * Watch out for scrolling that made entries in w_lines[] invalid.
1757 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1758 * top_end; need to redraw from top_end to the "to" line.
1759 * A middle mouse click with a Visual selection may change the text
1760 * above the Visual area and reset wl_valid, do count these for
1761 * mid_end (in srow).
1762 */
1763 if (mid_start > 0)
1764 {
1765 lnum = wp->w_topline;
1766 idx = 0;
1767 srow = 0;
1768 if (scrolled_down)
1769 mid_start = top_end;
1770 else
1771 mid_start = 0;
1772 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1773 {
1774 if (wp->w_lines[idx].wl_valid)
1775 mid_start += wp->w_lines[idx].wl_size;
1776 else if (!scrolled_down)
1777 srow += wp->w_lines[idx].wl_size;
1778 ++idx;
1779# ifdef FEAT_FOLDING
1780 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1781 lnum = wp->w_lines[idx].wl_lnum;
1782 else
1783# endif
1784 ++lnum;
1785 }
1786 srow += mid_start;
1787 mid_end = wp->w_height;
1788 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1789 {
1790 if (wp->w_lines[idx].wl_valid
1791 && wp->w_lines[idx].wl_lnum >= to + 1)
1792 {
1793 /* Only update until first row of this line */
1794 mid_end = srow;
1795 break;
1796 }
1797 srow += wp->w_lines[idx].wl_size;
1798 }
1799 }
1800 }
1801
1802 if (VIsual_active && buf == curwin->w_buffer)
1803 {
1804 wp->w_old_visual_mode = VIsual_mode;
1805 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1806 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001807 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 wp->w_old_curswant = curwin->w_curswant;
1809 }
1810 else
1811 {
1812 wp->w_old_visual_mode = 0;
1813 wp->w_old_cursor_lnum = 0;
1814 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001815 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1819 /* reset got_int, otherwise regexp won't work */
1820 save_got_int = got_int;
1821 got_int = 0;
1822#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001823#ifdef SYN_TIME_LIMIT
1824 /* Set the time limit to 'redrawtime'. */
1825 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001826 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_FOLDING
1829 win_foldinfo.fi_level = 0;
1830#endif
1831
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001832#ifdef FEAT_MENU
1833 /*
1834 * Draw the window toolbar, if there is one.
1835 * TODO: only when needed.
1836 */
1837 if (winbar_height(wp) > 0)
1838 redraw_win_toolbar(wp);
1839#endif
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 /*
1842 * Update all the window rows.
1843 */
1844 idx = 0; /* first entry in w_lines[].wl_size */
1845 row = 0;
1846 srow = 0;
1847 lnum = wp->w_topline; /* first line shown in window */
1848 for (;;)
1849 {
1850 /* stop updating when reached the end of the window (check for _past_
1851 * the end of the window is at the end of the loop) */
1852 if (row == wp->w_height)
1853 {
1854 didline = TRUE;
1855 break;
1856 }
1857
1858 /* stop updating when hit the end of the file */
1859 if (lnum > buf->b_ml.ml_line_count)
1860 {
1861 eof = TRUE;
1862 break;
1863 }
1864
1865 /* Remember the starting row of the line that is going to be dealt
1866 * with. It is used further down when the line doesn't fit. */
1867 srow = row;
1868
1869 /*
1870 * Update a line when it is in an area that needs updating, when it
1871 * has changes or w_lines[idx] is invalid.
1872 * bot_start may be halfway a wrapped line after using
1873 * win_del_lines(), check if the current line includes it.
1874 * When syntax folding is being used, the saved syntax states will
1875 * already have been updated, we can't see where the syntax state is
1876 * the same again, just update until the end of the window.
1877 */
1878 if (row < top_end
1879 || (row >= mid_start && row < mid_end)
1880#ifdef FEAT_SEARCH_EXTRA
1881 || top_to_mod
1882#endif
1883 || idx >= wp->w_lines_valid
1884 || (row + wp->w_lines[idx].wl_size > bot_start)
1885 || (mod_top != 0
1886 && (lnum == mod_top
1887 || (lnum >= mod_top
1888 && (lnum < mod_bot
1889#ifdef FEAT_SYN_HL
1890 || did_update == DID_FOLD
1891 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001892 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 && (
1894# ifdef FEAT_FOLDING
1895 (foldmethodIsSyntax(wp)
1896 && hasAnyFolding(wp)) ||
1897# endif
1898 syntax_check_changed(lnum)))
1899#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001900#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001901 /* match in fixed position might need redraw
1902 * if lines were inserted or deleted */
1903 || (wp->w_match_head != NULL
1904 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 )))))
1907 {
1908#ifdef FEAT_SEARCH_EXTRA
1909 if (lnum == mod_top)
1910 top_to_mod = FALSE;
1911#endif
1912
1913 /*
1914 * When at start of changed lines: May scroll following lines
1915 * up or down to minimize redrawing.
1916 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001917 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 */
1919 if (lnum == mod_top
1920 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001921 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 int old_rows = 0;
1924 int new_rows = 0;
1925 int xtra_rows;
1926 linenr_T l;
1927
1928 /* Count the old number of window rows, using w_lines[], which
1929 * should still contain the sizes for the lines as they are
1930 * currently displayed. */
1931 for (i = idx; i < wp->w_lines_valid; ++i)
1932 {
1933 /* Only valid lines have a meaningful wl_lnum. Invalid
1934 * lines are part of the changed area. */
1935 if (wp->w_lines[i].wl_valid
1936 && wp->w_lines[i].wl_lnum == mod_bot)
1937 break;
1938 old_rows += wp->w_lines[i].wl_size;
1939#ifdef FEAT_FOLDING
1940 if (wp->w_lines[i].wl_valid
1941 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1942 {
1943 /* Must have found the last valid entry above mod_bot.
1944 * Add following invalid entries. */
1945 ++i;
1946 while (i < wp->w_lines_valid
1947 && !wp->w_lines[i].wl_valid)
1948 old_rows += wp->w_lines[i++].wl_size;
1949 break;
1950 }
1951#endif
1952 }
1953
1954 if (i >= wp->w_lines_valid)
1955 {
1956 /* We can't find a valid line below the changed lines,
1957 * need to redraw until the end of the window.
1958 * Inserting/deleting lines has no use. */
1959 bot_start = 0;
1960 }
1961 else
1962 {
1963 /* Able to count old number of rows: Count new window
1964 * rows, and may insert/delete lines */
1965 j = idx;
1966 for (l = lnum; l < mod_bot; ++l)
1967 {
1968#ifdef FEAT_FOLDING
1969 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1970 ++new_rows;
1971 else
1972#endif
1973#ifdef FEAT_DIFF
1974 if (l == wp->w_topline)
1975 new_rows += plines_win_nofill(wp, l, TRUE)
1976 + wp->w_topfill;
1977 else
1978#endif
1979 new_rows += plines_win(wp, l, TRUE);
1980 ++j;
1981 if (new_rows > wp->w_height - row - 2)
1982 {
1983 /* it's getting too much, must redraw the rest */
1984 new_rows = 9999;
1985 break;
1986 }
1987 }
1988 xtra_rows = new_rows - old_rows;
1989 if (xtra_rows < 0)
1990 {
1991 /* May scroll text up. If there is not enough
1992 * remaining text or scrolling fails, must redraw the
1993 * rest. If scrolling works, must redraw the text
1994 * below the scrolled text. */
1995 if (row - xtra_rows >= wp->w_height - 2)
1996 mod_bot = MAXLNUM;
1997 else
1998 {
1999 check_for_delay(FALSE);
2000 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002001 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 mod_bot = MAXLNUM;
2003 else
2004 bot_start = wp->w_height + xtra_rows;
2005 }
2006 }
2007 else if (xtra_rows > 0)
2008 {
2009 /* May scroll text down. If there is not enough
2010 * remaining text of scrolling fails, must redraw the
2011 * rest. */
2012 if (row + xtra_rows >= wp->w_height - 2)
2013 mod_bot = MAXLNUM;
2014 else
2015 {
2016 check_for_delay(FALSE);
2017 if (win_ins_lines(wp, row + old_rows,
2018 xtra_rows, FALSE, FALSE) == FAIL)
2019 mod_bot = MAXLNUM;
2020 else if (top_end > row + old_rows)
2021 /* Scrolled the part at the top that requires
2022 * updating down. */
2023 top_end += xtra_rows;
2024 }
2025 }
2026
2027 /* When not updating the rest, may need to move w_lines[]
2028 * entries. */
2029 if (mod_bot != MAXLNUM && i != j)
2030 {
2031 if (j < i)
2032 {
2033 int x = row + new_rows;
2034
2035 /* move entries in w_lines[] upwards */
2036 for (;;)
2037 {
2038 /* stop at last valid entry in w_lines[] */
2039 if (i >= wp->w_lines_valid)
2040 {
2041 wp->w_lines_valid = j;
2042 break;
2043 }
2044 wp->w_lines[j] = wp->w_lines[i];
2045 /* stop at a line that won't fit */
2046 if (x + (int)wp->w_lines[j].wl_size
2047 > wp->w_height)
2048 {
2049 wp->w_lines_valid = j + 1;
2050 break;
2051 }
2052 x += wp->w_lines[j++].wl_size;
2053 ++i;
2054 }
2055 if (bot_start > x)
2056 bot_start = x;
2057 }
2058 else /* j > i */
2059 {
2060 /* move entries in w_lines[] downwards */
2061 j -= i;
2062 wp->w_lines_valid += j;
2063 if (wp->w_lines_valid > wp->w_height)
2064 wp->w_lines_valid = wp->w_height;
2065 for (i = wp->w_lines_valid; i - j >= idx; --i)
2066 wp->w_lines[i] = wp->w_lines[i - j];
2067
2068 /* The w_lines[] entries for inserted lines are
2069 * now invalid, but wl_size may be used above.
2070 * Reset to zero. */
2071 while (i >= idx)
2072 {
2073 wp->w_lines[i].wl_size = 0;
2074 wp->w_lines[i--].wl_valid = FALSE;
2075 }
2076 }
2077 }
2078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080
2081#ifdef FEAT_FOLDING
2082 /*
2083 * When lines are folded, display one line for all of them.
2084 * Otherwise, display normally (can be several display lines when
2085 * 'wrap' is on).
2086 */
2087 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2088 if (fold_count != 0)
2089 {
2090 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2091 ++row;
2092 --fold_count;
2093 wp->w_lines[idx].wl_folded = TRUE;
2094 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2095# ifdef FEAT_SYN_HL
2096 did_update = DID_FOLD;
2097# endif
2098 }
2099 else
2100#endif
2101 if (idx < wp->w_lines_valid
2102 && wp->w_lines[idx].wl_valid
2103 && wp->w_lines[idx].wl_lnum == lnum
2104 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002105 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 && srow + wp->w_lines[idx].wl_size > wp->w_height
2107#ifdef FEAT_DIFF
2108 && diff_check_fill(wp, lnum) == 0
2109#endif
2110 )
2111 {
2112 /* This line is not going to fit. Don't draw anything here,
2113 * will draw "@ " lines below. */
2114 row = wp->w_height + 1;
2115 }
2116 else
2117 {
2118#ifdef FEAT_SEARCH_EXTRA
2119 prepare_search_hl(wp, lnum);
2120#endif
2121#ifdef FEAT_SYN_HL
2122 /* Let the syntax stuff know we skipped a few lines. */
2123 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002124 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 syntax_end_parsing(syntax_last_parsed + 1);
2126#endif
2127
2128 /*
2129 * Display one line.
2130 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002131 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133#ifdef FEAT_FOLDING
2134 wp->w_lines[idx].wl_folded = FALSE;
2135 wp->w_lines[idx].wl_lastlnum = lnum;
2136#endif
2137#ifdef FEAT_SYN_HL
2138 did_update = DID_LINE;
2139 syntax_last_parsed = lnum;
2140#endif
2141 }
2142
2143 wp->w_lines[idx].wl_lnum = lnum;
2144 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002145
2146 /* Past end of the window or end of the screen. Note that after
2147 * resizing wp->w_height may be end up too big. That's a problem
2148 * elsewhere, but prevent a crash here. */
2149 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002152 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2154 ++idx;
2155 break;
2156 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002157 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 wp->w_lines[idx].wl_size = row - srow;
2159 ++idx;
2160#ifdef FEAT_FOLDING
2161 lnum += fold_count + 1;
2162#else
2163 ++lnum;
2164#endif
2165 }
2166 else
2167 {
2168 /* This line does not need updating, advance to the next one */
2169 row += wp->w_lines[idx++].wl_size;
2170 if (row > wp->w_height) /* past end of screen */
2171 break;
2172#ifdef FEAT_FOLDING
2173 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2174#else
2175 ++lnum;
2176#endif
2177#ifdef FEAT_SYN_HL
2178 did_update = DID_NONE;
2179#endif
2180 }
2181
2182 if (lnum > buf->b_ml.ml_line_count)
2183 {
2184 eof = TRUE;
2185 break;
2186 }
2187 }
2188 /*
2189 * End of loop over all window lines.
2190 */
2191
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002192#ifdef FEAT_VTP
2193 /* Rewrite the character at the end of the screen line. */
2194 if (use_vtp())
2195 {
2196 int i;
2197
2198 for (i = 0; i < Rows; ++i)
2199# ifdef FEAT_MBYTE
2200 if (enc_utf8)
2201 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2202 LineOffset[i] + screen_Columns) > 1)
2203 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2204 else
2205 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2206 else
2207# endif
2208 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2209 }
2210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212 if (idx > wp->w_lines_valid)
2213 wp->w_lines_valid = idx;
2214
2215#ifdef FEAT_SYN_HL
2216 /*
2217 * Let the syntax stuff know we stop parsing here.
2218 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002219 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 syntax_end_parsing(syntax_last_parsed + 1);
2221#endif
2222
2223 /*
2224 * If we didn't hit the end of the file, and we didn't finish the last
2225 * line we were working on, then the line didn't fit.
2226 */
2227 wp->w_empty_rows = 0;
2228#ifdef FEAT_DIFF
2229 wp->w_filler_rows = 0;
2230#endif
2231 if (!eof && !didline)
2232 {
2233 if (lnum == wp->w_topline)
2234 {
2235 /*
2236 * Single line that does not fit!
2237 * Don't overwrite it, it can be edited.
2238 */
2239 wp->w_botline = lnum + 1;
2240 }
2241#ifdef FEAT_DIFF
2242 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2243 {
2244 /* Window ends in filler lines. */
2245 wp->w_botline = lnum;
2246 wp->w_filler_rows = wp->w_height - srow;
2247 }
2248#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002249 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2250 {
2251 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2252
2253 /*
2254 * Last line isn't finished: Display "@@@" in the last screen line.
2255 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002256 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002257 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002258 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002259 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002261 set_empty_rows(wp, srow);
2262 wp->w_botline = lnum;
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2265 {
2266 /*
2267 * Last line isn't finished: Display "@@@" at the end.
2268 */
2269 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2270 W_WINROW(wp) + wp->w_height,
2271 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002272 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 set_empty_rows(wp, srow);
2274 wp->w_botline = lnum;
2275 }
2276 else
2277 {
2278 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2279 wp->w_botline = lnum;
2280 }
2281 }
2282 else
2283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (eof) /* we hit the end of the file */
2286 {
2287 wp->w_botline = buf->b_ml.ml_line_count + 1;
2288#ifdef FEAT_DIFF
2289 j = diff_check_fill(wp, wp->w_botline);
2290 if (j > 0 && !wp->w_botfill)
2291 {
2292 /*
2293 * Display filler lines at the end of the file
2294 */
2295 if (char2cells(fill_diff) > 1)
2296 i = '-';
2297 else
2298 i = fill_diff;
2299 if (row + j > wp->w_height)
2300 j = wp->w_height - row;
2301 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2302 row += j;
2303 }
2304#endif
2305 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002306 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 wp->w_botline = lnum;
2308
2309 /* make sure the rest of the screen is blank */
2310 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002311 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002314#ifdef SYN_TIME_LIMIT
2315 syn_set_timeout(NULL);
2316#endif
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /* Reset the type of redrawing required, the window has been updated. */
2319 wp->w_redr_type = 0;
2320#ifdef FEAT_DIFF
2321 wp->w_old_topfill = wp->w_topfill;
2322 wp->w_old_botfill = wp->w_botfill;
2323#endif
2324
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002325 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 /*
2328 * There is a trick with w_botline. If we invalidate it on each
2329 * change that might modify it, this will cause a lot of expensive
2330 * calls to plines() in update_topline() each time. Therefore the
2331 * value of w_botline is often approximated, and this value is used to
2332 * compute the value of w_topline. If the value of w_botline was
2333 * wrong, check that the value of w_topline is correct (cursor is on
2334 * the visible part of the text). If it's not, we need to redraw
2335 * again. Mostly this just means scrolling up a few lines, so it
2336 * doesn't look too bad. Only do this for the current window (where
2337 * changes are relevant).
2338 */
2339 wp->w_valid |= VALID_BOTLINE;
2340 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2341 {
2342 recursive = TRUE;
2343 curwin->w_valid &= ~VALID_TOPLINE;
2344 update_topline(); /* may invalidate w_botline again */
2345 if (must_redraw != 0)
2346 {
2347 /* Don't update for changes in buffer again. */
2348 i = curbuf->b_mod_set;
2349 curbuf->b_mod_set = FALSE;
2350 win_update(curwin);
2351 must_redraw = 0;
2352 curbuf->b_mod_set = i;
2353 }
2354 recursive = FALSE;
2355 }
2356 }
2357
2358#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2359 /* restore got_int, unless CTRL-C was hit while redrawing */
2360 if (!got_int)
2361 got_int = save_got_int;
2362#endif
2363}
2364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365/*
2366 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2367 * as the filler character.
2368 */
2369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002370win_draw_end(
2371 win_T *wp,
2372 int c1,
2373 int c2,
2374 int row,
2375 int endrow,
2376 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
2378#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2379 int n = 0;
2380# define FDC_OFF n
2381#else
2382# define FDC_OFF 0
2383#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002384#ifdef FEAT_FOLDING
2385 int fdc = compute_foldcolumn(wp, 0);
2386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388#ifdef FEAT_RIGHTLEFT
2389 if (wp->w_p_rl)
2390 {
2391 /* No check for cmdline window: should never be right-left. */
2392# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002393 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 if (n > 0)
2396 {
2397 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002398 if (n > wp->w_width)
2399 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2401 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002402 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404# endif
2405# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002406 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 int nn = n + 2;
2409
2410 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002411 if (nn > wp->w_width)
2412 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2414 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002415 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 n = nn;
2417 }
2418# endif
2419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002420 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002421 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2423 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002424 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426 else
2427#endif
2428 {
2429#ifdef FEAT_CMDWIN
2430 if (cmdwin_type != 0 && wp == curwin)
2431 {
2432 /* draw the cmdline character in the leftmost column */
2433 n = 1;
2434 if (n > wp->w_width)
2435 n = wp->w_width;
2436 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002437 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002438 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440#endif
2441#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002442 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002444 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002447 if (nn > wp->w_width)
2448 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002450 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002451 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 n = nn;
2453 }
2454#endif
2455#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002456 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 int nn = n + 2;
2459
2460 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002461 if (nn > wp->w_width)
2462 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002464 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002465 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 n = nn;
2467 }
2468#endif
2469 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002470 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002471 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 }
2473 set_empty_rows(wp, row);
2474}
2475
Bram Moolenaar1a384422010-07-14 19:53:30 +02002476#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002477static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002478
2479/*
2480 * Advance **color_cols and return TRUE when there are columns to draw.
2481 */
2482 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002483advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002484{
2485 while (**color_cols >= 0 && vcol > **color_cols)
2486 ++*color_cols;
2487 return (**color_cols >= 0);
2488}
2489#endif
2490
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002491#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2492/*
2493 * Copy "text" to ScreenLines using "attr".
2494 * Returns the next screen column.
2495 */
2496 static int
2497text_to_screenline(win_T *wp, char_u *text, int col)
2498{
2499 int off = (int)(current_ScreenLine - ScreenLines);
2500
2501#ifdef FEAT_MBYTE
2502 if (has_mbyte)
2503 {
2504 int cells;
2505 int u8c, u8cc[MAX_MCO];
2506 int i;
2507 int idx;
2508 int c_len;
2509 char_u *p;
2510# ifdef FEAT_ARABIC
2511 int prev_c = 0; /* previous Arabic character */
2512 int prev_c1 = 0; /* first composing char for prev_c */
2513# endif
2514
2515# ifdef FEAT_RIGHTLEFT
2516 if (wp->w_p_rl)
2517 idx = off;
2518 else
2519# endif
2520 idx = off + col;
2521
2522 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2523 for (p = text; *p != NUL; )
2524 {
2525 cells = (*mb_ptr2cells)(p);
2526 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002527 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002528# ifdef FEAT_RIGHTLEFT
2529 - (wp->w_p_rl ? col : 0)
2530# endif
2531 )
2532 break;
2533 ScreenLines[idx] = *p;
2534 if (enc_utf8)
2535 {
2536 u8c = utfc_ptr2char(p, u8cc);
2537 if (*p < 0x80 && u8cc[0] == 0)
2538 {
2539 ScreenLinesUC[idx] = 0;
2540#ifdef FEAT_ARABIC
2541 prev_c = u8c;
2542#endif
2543 }
2544 else
2545 {
2546#ifdef FEAT_ARABIC
2547 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2548 {
2549 /* Do Arabic shaping. */
2550 int pc, pc1, nc;
2551 int pcc[MAX_MCO];
2552 int firstbyte = *p;
2553
2554 /* The idea of what is the previous and next
2555 * character depends on 'rightleft'. */
2556 if (wp->w_p_rl)
2557 {
2558 pc = prev_c;
2559 pc1 = prev_c1;
2560 nc = utf_ptr2char(p + c_len);
2561 prev_c1 = u8cc[0];
2562 }
2563 else
2564 {
2565 pc = utfc_ptr2char(p + c_len, pcc);
2566 nc = prev_c;
2567 pc1 = pcc[0];
2568 }
2569 prev_c = u8c;
2570
2571 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2572 pc, pc1, nc);
2573 ScreenLines[idx] = firstbyte;
2574 }
2575 else
2576 prev_c = u8c;
2577#endif
2578 /* Non-BMP character: display as ? or fullwidth ?. */
2579#ifdef UNICODE16
2580 if (u8c >= 0x10000)
2581 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2582 else
2583#endif
2584 ScreenLinesUC[idx] = u8c;
2585 for (i = 0; i < Screen_mco; ++i)
2586 {
2587 ScreenLinesC[i][idx] = u8cc[i];
2588 if (u8cc[i] == 0)
2589 break;
2590 }
2591 }
2592 if (cells > 1)
2593 ScreenLines[idx + 1] = 0;
2594 }
2595 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2596 /* double-byte single width character */
2597 ScreenLines2[idx] = p[1];
2598 else if (cells > 1)
2599 /* double-width character */
2600 ScreenLines[idx + 1] = p[1];
2601 col += cells;
2602 idx += cells;
2603 p += c_len;
2604 }
2605 }
2606 else
2607#endif
2608 {
2609 int len = (int)STRLEN(text);
2610
Bram Moolenaar02631462017-09-22 15:20:32 +02002611 if (len > wp->w_width - col)
2612 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002613 if (len > 0)
2614 {
2615#ifdef FEAT_RIGHTLEFT
2616 if (wp->w_p_rl)
2617 STRNCPY(current_ScreenLine, text, len);
2618 else
2619#endif
2620 STRNCPY(current_ScreenLine + col, text, len);
2621 col += len;
2622 }
2623 }
2624 return col;
2625}
2626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628#ifdef FEAT_FOLDING
2629/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002630 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2631 * space is available for window "wp", minus "col".
2632 */
2633 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002634compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002635{
2636 int fdc = wp->w_p_fdc;
2637 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002638 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002639
2640 if (fdc > wwidth - (col + wmw))
2641 fdc = wwidth - (col + wmw);
2642 return fdc;
2643}
2644
2645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 * Display one folded line.
2647 */
2648 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002649fold_line(
2650 win_T *wp,
2651 long fold_count,
2652 foldinfo_T *foldinfo,
2653 linenr_T lnum,
2654 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002656 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 pos_T *top, *bot;
2658 linenr_T lnume = lnum + fold_count - 1;
2659 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002660 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int col;
2663 int txtcol;
2664 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002665 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666
2667 /* Build the fold line:
2668 * 1. Add the cmdwin_type for the command-line window
2669 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002670 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 * 4. Compose the text
2672 * 5. Add the text
2673 * 6. set highlighting for the Visual area an other text
2674 */
2675 col = 0;
2676
2677 /*
2678 * 1. Add the cmdwin_type for the command-line window
2679 * Ignores 'rightleft', this window is never right-left.
2680 */
2681#ifdef FEAT_CMDWIN
2682 if (cmdwin_type != 0 && wp == curwin)
2683 {
2684 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002685 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686#ifdef FEAT_MBYTE
2687 if (enc_utf8)
2688 ScreenLinesUC[off] = 0;
2689#endif
2690 ++col;
2691 }
2692#endif
2693
2694 /*
2695 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002696 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002698 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (fdc > 0)
2700 {
2701 fill_foldcolumn(buf, wp, TRUE, lnum);
2702#ifdef FEAT_RIGHTLEFT
2703 if (wp->w_p_rl)
2704 {
2705 int i;
2706
Bram Moolenaar02631462017-09-22 15:20:32 +02002707 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002708 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /* reverse the fold column */
2710 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 else
2714#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002715 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 col += fdc;
2717 }
2718
2719#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002720# define RL_MEMSET(p, v, l) \
2721 do { \
2722 if (wp->w_p_rl) \
2723 for (ri = 0; ri < l; ++ri) \
2724 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2725 else \
2726 for (ri = 0; ri < l; ++ri) \
2727 ScreenAttrs[off + (p) + ri] = v; \
2728 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002730# define RL_MEMSET(p, v, l) \
2731 do { \
2732 for (ri = 0; ri < l; ++ri) \
2733 ScreenAttrs[off + (p) + ri] = v; \
2734 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#endif
2736
Bram Moolenaar64486672010-05-16 15:46:46 +02002737 /* Set all attributes of the 'number' or 'relativenumber' column and the
2738 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002739 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741#ifdef FEAT_SIGNS
2742 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002743 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002745 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (len > 0)
2747 {
2748 if (len > 2)
2749 len = 2;
2750# ifdef FEAT_RIGHTLEFT
2751 if (wp->w_p_rl)
2752 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002753 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002754 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 else
2756# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002757 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 col += len;
2759 }
2760 }
2761#endif
2762
2763 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002764 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002766 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002768 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (len > 0)
2770 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002771 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002772 long num;
2773 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002774
2775 if (len > w + 1)
2776 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002777
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002778 if (wp->w_p_nu && !wp->w_p_rnu)
2779 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002780 num = (long)lnum;
2781 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002782 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002783 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002784 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002785 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002786 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002787 /* 'number' + 'relativenumber': cursor line shows absolute
2788 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002789 num = lnum;
2790 fmt = "%-*ld ";
2791 }
2792 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002793
Bram Moolenaar700e7342013-01-30 12:31:36 +01002794 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#ifdef FEAT_RIGHTLEFT
2796 if (wp->w_p_rl)
2797 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002798 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002799 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else
2801#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002802 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 col += len;
2804 }
2805 }
2806
2807 /*
2808 * 4. Compose the folded-line string with 'foldtext', if set.
2809 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002810 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 txtcol = col; /* remember where text starts */
2813
2814 /*
2815 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2816 * Right-left text is put in columns 0 - number-col, normal text is put
2817 * in columns number-col - window-width.
2818 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002819 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
2821 /* Fill the rest of the line with the fold filler */
2822#ifdef FEAT_RIGHTLEFT
2823 if (wp->w_p_rl)
2824 col -= txtcol;
2825#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002826 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#ifdef FEAT_RIGHTLEFT
2828 - (wp->w_p_rl ? txtcol : 0)
2829#endif
2830 )
2831 {
2832#ifdef FEAT_MBYTE
2833 if (enc_utf8)
2834 {
2835 if (fill_fold >= 0x80)
2836 {
2837 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002838 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002839 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002844 ScreenLines[off + col] = fill_fold;
2845 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002846 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002850 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852
2853 if (text != buf)
2854 vim_free(text);
2855
2856 /*
2857 * 6. set highlighting for the Visual area an other text.
2858 * If all folded lines are in the Visual area, highlight the line.
2859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2861 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002862 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
2864 /* Visual is after curwin->w_cursor */
2865 top = &curwin->w_cursor;
2866 bot = &VIsual;
2867 }
2868 else
2869 {
2870 /* Visual is before curwin->w_cursor */
2871 top = &VIsual;
2872 bot = &curwin->w_cursor;
2873 }
2874 if (lnum >= top->lnum
2875 && lnume <= bot->lnum
2876 && (VIsual_mode != 'v'
2877 || ((lnum > top->lnum
2878 || (lnum == top->lnum
2879 && top->col == 0))
2880 && (lnume < bot->lnum
2881 || (lnume == bot->lnum
2882 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002883 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 if (VIsual_mode == Ctrl_V)
2886 {
2887 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002888 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002890 if (wp->w_old_cursor_lcol != MAXCOL
2891 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002892 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 len = wp->w_old_cursor_lcol;
2894 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002896 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002897 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
2899 }
2900 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002908#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002909 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2910 if (wp->w_p_cc_cols)
2911 {
2912 int i = 0;
2913 int j = wp->w_p_cc_cols[i];
2914 int old_txtcol = txtcol;
2915
2916 while (j > -1)
2917 {
2918 txtcol += j;
2919 if (wp->w_p_wrap)
2920 txtcol -= wp->w_skipcol;
2921 else
2922 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002923 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002924 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002925 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002926 txtcol = old_txtcol;
2927 j = wp->w_p_cc_cols[++i];
2928 }
2929 }
2930
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002931 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002932 if (wp->w_p_cuc)
2933 {
2934 txtcol += wp->w_virtcol;
2935 if (wp->w_p_wrap)
2936 txtcol -= wp->w_skipcol;
2937 else
2938 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002939 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002940 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002941 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002942 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
Bram Moolenaar02631462017-09-22 15:20:32 +02002945 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2946 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 /*
2949 * Update w_cline_height and w_cline_folded if the cursor line was
2950 * updated (saves a call to plines() later).
2951 */
2952 if (wp == curwin
2953 && lnum <= curwin->w_cursor.lnum
2954 && lnume >= curwin->w_cursor.lnum)
2955 {
2956 curwin->w_cline_row = row;
2957 curwin->w_cline_height = 1;
2958 curwin->w_cline_folded = TRUE;
2959 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2960 }
2961}
2962
2963/*
2964 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2965 */
2966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002967copy_text_attr(
2968 int off,
2969 char_u *buf,
2970 int len,
2971 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002973 int i;
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 mch_memmove(ScreenLines + off, buf, (size_t)len);
2976# ifdef FEAT_MBYTE
2977 if (enc_utf8)
2978 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2979# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002980 for (i = 0; i < len; ++i)
2981 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982}
2983
2984/*
2985 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002986 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
2988 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002989fill_foldcolumn(
2990 char_u *p,
2991 win_T *wp,
2992 int closed, /* TRUE of FALSE */
2993 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 int i = 0;
2996 int level;
2997 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002998 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002999 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003002 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 level = win_foldinfo.fi_level;
3005 if (level > 0)
3006 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003007 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003008 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 /* If the column is too narrow, we start at the lowest level that
3011 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003012 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (first_level < 1)
3014 first_level = 1;
3015
Bram Moolenaar1c934292015-01-27 16:39:29 +01003016 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
3018 if (win_foldinfo.fi_lnum == lnum
3019 && first_level + i >= win_foldinfo.fi_low_level)
3020 p[i] = '-';
3021 else if (first_level == 1)
3022 p[i] = '|';
3023 else if (first_level + i <= 9)
3024 p[i] = '0' + first_level + i;
3025 else
3026 p[i] = '>';
3027 if (first_level + i == level)
3028 break;
3029 }
3030 }
3031 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003032 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033}
3034#endif /* FEAT_FOLDING */
3035
3036/*
3037 * Display line "lnum" of window 'wp' on the screen.
3038 * Start at row "startrow", stop when "endrow" is reached.
3039 * wp->w_virtcol needs to be valid.
3040 *
3041 * Return the number of last row the line occupies.
3042 */
3043 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003044win_line(
3045 win_T *wp,
3046 linenr_T lnum,
3047 int startrow,
3048 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003049 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003051 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3053 int c = 0; /* init for GCC */
3054 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003055#ifdef FEAT_LINEBREAK
3056 long vcol_sbr = -1; /* virtual column after showbreak */
3057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 long vcol_prev = -1; /* "vcol" of previous character */
3059 char_u *line; /* current line */
3060 char_u *ptr; /* current position in "line" */
3061 int row; /* row in the window, excl w_winrow */
3062 int screen_row; /* row on the screen, incl w_winrow */
3063
3064 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3065 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003066 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003067 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 int c_extra = NUL; /* extra chars, all the same */
3069 int extra_attr = 0; /* attributes when n_extra != 0 */
3070 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3071 displaying lcs_eol at end-of-line */
3072 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3073 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3074
3075 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3076 int saved_n_extra = 0;
3077 char_u *saved_p_extra = NULL;
3078 int saved_c_extra = 0;
3079 int saved_char_attr = 0;
3080
3081 int n_attr = 0; /* chars with special attr */
3082 int saved_attr2 = 0; /* char_attr saved for n_attr */
3083 int n_attr3 = 0; /* chars with overruling special attr */
3084 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3085
3086 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3087
3088 int fromcol, tocol; /* start/end of inverting */
3089 int fromcol_prev = -2; /* start of inverting after cursor */
3090 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003092 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 pos_T pos;
3094 long v;
3095
3096 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003097 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3099 in this line */
3100 int attr = 0; /* attributes for area highlighting */
3101 int area_attr = 0; /* attributes desired by highlighting */
3102 int search_attr = 0; /* attributes desired by 'hlsearch' */
3103#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003104 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 int syntax_attr = 0; /* attributes desired by syntax */
3106 int has_syntax = FALSE; /* this buffer has syntax highl. */
3107 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003108 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003109 int draw_color_col = FALSE; /* highlight colorcolumn */
3110 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003111#endif
3112#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003113 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003114# define SPWORDLEN 150
3115 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003116 int nextlinecol = 0; /* column where nextline[] starts */
3117 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003118 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003119 int spell_attr = 0; /* attributes desired by spelling */
3120 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003121 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3122 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003123 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003124 static int cap_col = -1; /* column to check for Cap word */
3125 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003126 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128 int extra_check; /* has syntax or linebreak */
3129#ifdef FEAT_MBYTE
3130 int multi_attr = 0; /* attributes desired by multibyte */
3131 int mb_l = 1; /* multi-byte byte length */
3132 int mb_c = 0; /* decoded multi-byte character */
3133 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003134 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#endif
3136#ifdef FEAT_DIFF
3137 int filler_lines; /* nr of filler lines to be drawn */
3138 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003139 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int change_start = MAXCOL; /* first col of changed area */
3141 int change_end = -1; /* last col of changed area */
3142#endif
3143 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3144#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003145 int need_showbreak = FALSE; /* overlong line, skipping first x
3146 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003148#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003149 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003151 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
3153#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003154 matchitem_T *cur; /* points to the match list */
3155 match_T *shl; /* points to search_hl or a match */
3156 int shl_flag; /* flag to indicate whether search_hl
3157 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003158 int pos_inprogress; /* marks that position match search is
3159 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003160 int prevcol_hl_flag; /* flag to indicate whether prevcol
3161 equals startcol of search_hl or one
3162 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164#ifdef FEAT_ARABIC
3165 int prev_c = 0; /* previous Arabic character */
3166 int prev_c1 = 0; /* first composing char for prev_c */
3167#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003168#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003169 int did_line_attr = 0;
3170#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003171#ifdef FEAT_TERMINAL
3172 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003173 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /* draw_state: items that are drawn in sequence: */
3177#define WL_START 0 /* nothing done yet */
3178#ifdef FEAT_CMDWIN
3179# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3180#else
3181# define WL_CMDLINE WL_START
3182#endif
3183#ifdef FEAT_FOLDING
3184# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3185#else
3186# define WL_FOLD WL_CMDLINE
3187#endif
3188#ifdef FEAT_SIGNS
3189# define WL_SIGN WL_FOLD + 1 /* column for signs */
3190#else
3191# define WL_SIGN WL_FOLD /* column for signs */
3192#endif
3193#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003194#ifdef FEAT_LINEBREAK
3195# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003197# define WL_BRI WL_NR
3198#endif
3199#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3200# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3201#else
3202# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif
3204#define WL_LINE WL_SBR + 1 /* text in the line */
3205 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003206#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 int feedback_col = 0;
3208 int feedback_old_attr = -1;
3209#endif
3210
Bram Moolenaar860cae12010-06-05 23:22:07 +02003211#ifdef FEAT_CONCEAL
3212 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003213 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003214 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003215 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003216 int is_concealing = FALSE;
3217 int boguscols = 0; /* nonexistent columns added to force
3218 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003219 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003220 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003221 int match_conc = 0; /* cchar for match functions */
3222 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003223 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003224# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003225# define FIX_FOR_BOGUSCOLS \
3226 { \
3227 n_extra += vcol_off; \
3228 vcol -= vcol_off; \
3229 vcol_off = 0; \
3230 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003231 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003232 boguscols = 0; \
3233 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003234#else
3235# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 if (startrow > endrow) /* past the end already! */
3239 return startrow;
3240
3241 row = startrow;
3242 screen_row = row + W_WINROW(wp);
3243
3244 /*
3245 * To speed up the loop below, set extra_check when there is linebreak,
3246 * trailing white space and/or syntax processing to be done.
3247 */
3248#ifdef FEAT_LINEBREAK
3249 extra_check = wp->w_p_lbr;
3250#else
3251 extra_check = 0;
3252#endif
3253#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003254 if (syntax_present(wp) && !wp->w_s->b_syn_error
3255# ifdef SYN_TIME_LIMIT
3256 && !wp->w_s->b_syn_slow
3257# endif
3258 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
3260 /* Prepare for syntax highlighting in this line. When there is an
3261 * error, stop syntax highlighting. */
3262 save_did_emsg = did_emsg;
3263 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003264 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003266 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 else
3268 {
3269 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003270#ifdef SYN_TIME_LIMIT
3271 if (!wp->w_s->b_syn_slow)
3272#endif
3273 {
3274 has_syntax = TRUE;
3275 extra_check = TRUE;
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 }
3278 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003279
3280 /* Check for columns to display for 'colorcolumn'. */
3281 color_cols = wp->w_p_cc_cols;
3282 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003283 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003284#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003285
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003286#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003287 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003288 {
3289 extra_check = TRUE;
3290 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003291 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003292 }
3293#endif
3294
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003295#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003296 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003297 && *wp->w_s->b_p_spl != NUL
3298 && wp->w_s->b_langp.ga_len > 0
3299 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003300 {
3301 /* Prepare for spell checking. */
3302 has_spell = TRUE;
3303 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003304
3305 /* Get the start of the next line, so that words that wrap to the next
3306 * line are found too: "et<line-break>al.".
3307 * Trick: skip a few chars for C/shell/Vim comments */
3308 nextline[SPWORDLEN] = NUL;
3309 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3310 {
3311 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3312 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3313 }
3314
3315 /* When a word wrapped from the previous line the start of the current
3316 * line is valid. */
3317 if (lnum == checked_lnum)
3318 cur_checked_col = checked_col;
3319 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003320
3321 /* When there was a sentence end in the previous line may require a
3322 * word starting with capital in this line. In line 1 always check
3323 * the first word. */
3324 if (lnum != capcol_lnum)
3325 cap_col = -1;
3326 if (lnum == 1)
3327 cap_col = 0;
3328 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
3331
3332 /*
3333 * handle visual active in this window
3334 */
3335 fromcol = -10;
3336 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3338 {
3339 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003340 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342 top = &curwin->w_cursor;
3343 bot = &VIsual;
3344 }
3345 else /* Visual is before curwin->w_cursor */
3346 {
3347 top = &VIsual;
3348 bot = &curwin->w_cursor;
3349 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003350 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 if (VIsual_mode == Ctrl_V) /* block mode */
3352 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003353 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
3355 fromcol = wp->w_old_cursor_fcol;
3356 tocol = wp->w_old_cursor_lcol;
3357 }
3358 }
3359 else /* non-block mode */
3360 {
3361 if (lnum > top->lnum && lnum <= bot->lnum)
3362 fromcol = 0;
3363 else if (lnum == top->lnum)
3364 {
3365 if (VIsual_mode == 'V') /* linewise */
3366 fromcol = 0;
3367 else
3368 {
3369 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3370 if (gchar_pos(top) == NUL)
3371 tocol = fromcol + 1;
3372 }
3373 }
3374 if (VIsual_mode != 'V' && lnum == bot->lnum)
3375 {
3376 if (*p_sel == 'e' && bot->col == 0
3377#ifdef FEAT_VIRTUALEDIT
3378 && bot->coladd == 0
3379#endif
3380 )
3381 {
3382 fromcol = -10;
3383 tocol = MAXCOL;
3384 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003385 else if (bot->col == MAXCOL)
3386 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 else
3388 {
3389 pos = *bot;
3390 if (*p_sel == 'e')
3391 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3392 else
3393 {
3394 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3395 ++tocol;
3396 }
3397 }
3398 }
3399 }
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /* Check if the character under the cursor should not be inverted */
3402 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003403#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 )
3407 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
3409 /* if inverting in this line set area_highlighting */
3410 if (fromcol >= 0)
3411 {
3412 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003413 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003415 if ((clip_star.available && !clip_star.owned
3416 && clip_isautosel_star())
3417 || (clip_plus.available && !clip_plus.owned
3418 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003419 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#endif
3421 }
3422 }
3423
3424 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003425 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003427 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 && wp == curwin
3429 && lnum >= curwin->w_cursor.lnum
3430 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3431 {
3432 if (lnum == curwin->w_cursor.lnum)
3433 getvcol(curwin, &(curwin->w_cursor),
3434 (colnr_T *)&fromcol, NULL, NULL);
3435 else
3436 fromcol = 0;
3437 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3438 {
3439 pos.lnum = lnum;
3440 pos.col = search_match_endcol;
3441 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3442 }
3443 else
3444 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003445 /* do at least one character; happens when past end of line */
3446 if (fromcol == tocol)
3447 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003449 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
3451
3452#ifdef FEAT_DIFF
3453 filler_lines = diff_check(wp, lnum);
3454 if (filler_lines < 0)
3455 {
3456 if (filler_lines == -1)
3457 {
3458 if (diff_find_change(wp, lnum, &change_start, &change_end))
3459 diff_hlf = HLF_ADD; /* added line */
3460 else if (change_start == 0)
3461 diff_hlf = HLF_TXD; /* changed text */
3462 else
3463 diff_hlf = HLF_CHD; /* changed line */
3464 }
3465 else
3466 diff_hlf = HLF_ADD; /* added line */
3467 filler_lines = 0;
3468 area_highlighting = TRUE;
3469 }
3470 if (lnum == wp->w_topline)
3471 filler_lines = wp->w_topfill;
3472 filler_todo = filler_lines;
3473#endif
3474
3475#ifdef LINE_ATTR
3476# ifdef FEAT_SIGNS
3477 /* If this line has a sign with line highlighting set line_attr. */
3478 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3479 if (v != 0)
3480 line_attr = sign_get_attr((int)v, TRUE);
3481# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003482# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003484 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003485 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486# endif
3487 if (line_attr != 0)
3488 area_highlighting = TRUE;
3489#endif
3490
3491 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3492 ptr = line;
3493
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003494#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003495 if (has_spell)
3496 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003497 /* For checking first word with a capital skip white space. */
3498 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003499 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003500
Bram Moolenaar30abd282005-06-22 22:35:10 +00003501 /* To be able to spell-check over line boundaries copy the end of the
3502 * current line into nextline[]. Above the start of the next line was
3503 * copied to nextline[SPWORDLEN]. */
3504 if (nextline[SPWORDLEN] == NUL)
3505 {
3506 /* No next line or it is empty. */
3507 nextlinecol = MAXCOL;
3508 nextline_idx = 0;
3509 }
3510 else
3511 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003513 if (v < SPWORDLEN)
3514 {
3515 /* Short line, use it completely and append the start of the
3516 * next line. */
3517 nextlinecol = 0;
3518 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003519 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003520 nextline_idx = v + 1;
3521 }
3522 else
3523 {
3524 /* Long line, use only the last SPWORDLEN bytes. */
3525 nextlinecol = v - SPWORDLEN;
3526 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3527 nextline_idx = SPWORDLEN + 1;
3528 }
3529 }
3530 }
3531#endif
3532
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003533 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003535 if (lcs_space || lcs_trail)
3536 extra_check = TRUE;
3537 /* find start of trailing whitespace */
3538 if (lcs_trail)
3539 {
3540 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003541 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003542 --trailcol;
3543 trailcol += (colnr_T) (ptr - line);
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
3546
3547 /*
3548 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3549 * first character to be displayed.
3550 */
3551 if (wp->w_p_wrap)
3552 v = wp->w_skipcol;
3553 else
3554 v = wp->w_leftcol;
3555 if (v > 0)
3556 {
3557#ifdef FEAT_MBYTE
3558 char_u *prev_ptr = ptr;
3559#endif
3560 while (vcol < v && *ptr != NUL)
3561 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003562 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 vcol += c;
3564#ifdef FEAT_MBYTE
3565 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003567 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003570 /* When:
3571 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003572 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003573 * - 'virtualedit' is set, or
3574 * - the visual mode is active,
3575 * the end of the line may be before the start of the displayed part.
3576 */
3577 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003578#ifdef FEAT_SYN_HL
3579 wp->w_p_cuc || draw_color_col ||
3580#endif
3581#ifdef FEAT_VIRTUALEDIT
3582 virtual_active() ||
3583#endif
3584 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 /* Handle a character that's not completely on the screen: Put ptr at
3590 * that character but skip the first few screen characters. */
3591 if (vcol > v)
3592 {
3593 vcol -= c;
3594#ifdef FEAT_MBYTE
3595 ptr = prev_ptr;
3596#else
3597 --ptr;
3598#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003599 /* If the character fits on the screen, don't need to skip it.
3600 * Except for a TAB. */
3601 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003602#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003603 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003604#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003605 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003606 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
3608
3609 /*
3610 * Adjust for when the inverted text is before the screen,
3611 * and when the start of the inverted text is before the screen.
3612 */
3613 if (tocol <= vcol)
3614 fromcol = 0;
3615 else if (fromcol >= 0 && fromcol < vcol)
3616 fromcol = vcol;
3617
3618#ifdef FEAT_LINEBREAK
3619 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3620 if (wp->w_p_wrap)
3621 need_showbreak = TRUE;
3622#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003623#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624 /* When spell checking a word we need to figure out the start of the
3625 * word and if it's badly spelled or not. */
3626 if (has_spell)
3627 {
3628 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003629 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003630 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003631
3632 pos = wp->w_cursor;
3633 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003634 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003635 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003636
3637 /* spell_move_to() may call ml_get() and make "line" invalid */
3638 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3639 ptr = line + linecol;
3640
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003641 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003642 {
3643 /* no bad word found at line start, don't check until end of a
3644 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003645 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003646 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003647 }
3648 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003649 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003650 /* bad word found, use attributes until end of word */
3651 word_end = wp->w_cursor.col + len + 1;
3652
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003653 /* Turn index into actual attributes. */
3654 if (spell_hlf != HLF_COUNT)
3655 spell_attr = highlight_attr[spell_hlf];
3656 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003657 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003658
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003659# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003660 /* Need to restart syntax highlighting for this line. */
3661 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003662 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003663# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003664 }
3665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 /*
3669 * Correct highlighting for cursor that can't be disabled.
3670 * Avoids having to check this for each character.
3671 */
3672 if (fromcol >= 0)
3673 {
3674 if (noinvcur)
3675 {
3676 if ((colnr_T)fromcol == wp->w_virtcol)
3677 {
3678 /* highlighting starts at cursor, let it start just after the
3679 * cursor */
3680 fromcol_prev = fromcol;
3681 fromcol = -1;
3682 }
3683 else if ((colnr_T)fromcol < wp->w_virtcol)
3684 /* restart highlighting after the cursor */
3685 fromcol_prev = wp->w_virtcol;
3686 }
3687 if (fromcol >= tocol)
3688 fromcol = -1;
3689 }
3690
3691#ifdef FEAT_SEARCH_EXTRA
3692 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003693 * Handle highlighting the last used search pattern and matches.
3694 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003696 cur = wp->w_match_head;
3697 shl_flag = FALSE;
3698 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003700 if (shl_flag == FALSE)
3701 {
3702 shl = &search_hl;
3703 shl_flag = TRUE;
3704 }
3705 else
3706 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003707 shl->startcol = MAXCOL;
3708 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003710 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003711 v = (long)(ptr - line);
3712 if (cur != NULL)
3713 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003714 next_search_hl(wp, shl, lnum, (colnr_T)v,
3715 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003716
3717 /* Need to get the line again, a multi-line regexp may have made it
3718 * invalid. */
3719 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3720 ptr = line + v;
3721
3722 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003724 if (shl->lnum == lnum)
3725 shl->startcol = shl->rm.startpos[0].col;
3726 else
3727 shl->startcol = 0;
3728 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3729 - shl->rm.startpos[0].lnum)
3730 shl->endcol = shl->rm.endpos[0].col;
3731 else
3732 shl->endcol = MAXCOL;
3733 /* Highlight one character for an empty match. */
3734 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003737 if (has_mbyte && line[shl->endcol] != NUL)
3738 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003741 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003743 if ((long)shl->startcol < v) /* match at leftcol */
3744 {
3745 shl->attr_cur = shl->attr;
3746 search_attr = shl->attr;
3747 }
3748 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003750 if (shl != &search_hl && cur != NULL)
3751 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753#endif
3754
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003755#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003756 /* Cursor line highlighting for 'cursorline' in the current window. Not
3757 * when Visual mode is active, because it's not clear what is selected
3758 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003759 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003760 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003761 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003762 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003763 area_highlighting = TRUE;
3764 }
3765#endif
3766
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003767 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 col = 0;
3769#ifdef FEAT_RIGHTLEFT
3770 if (wp->w_p_rl)
3771 {
3772 /* Rightleft window: process the text in the normal direction, but put
3773 * it in current_ScreenLine[] from right to left. Start at the
3774 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003775 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 off += col;
3777 }
3778#endif
3779
3780 /*
3781 * Repeat for the whole displayed line.
3782 */
3783 for (;;)
3784 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003785#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003786 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 /* Skip this quickly when working on the text. */
3789 if (draw_state != WL_LINE)
3790 {
3791#ifdef FEAT_CMDWIN
3792 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3793 {
3794 draw_state = WL_CMDLINE;
3795 if (cmdwin_type != 0 && wp == curwin)
3796 {
3797 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003799 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003800 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 }
3803#endif
3804
3805#ifdef FEAT_FOLDING
3806 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3807 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003808 int fdc = compute_foldcolumn(wp, 0);
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003811 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003813 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003814 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003815 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003816 p_extra_free = alloc(12 + 1);
3817
3818 if (p_extra_free != NULL)
3819 {
3820 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3821 n_extra = fdc;
3822 p_extra_free[n_extra] = NUL;
3823 p_extra = p_extra_free;
3824 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003825 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829#endif
3830
3831#ifdef FEAT_SIGNS
3832 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3833 {
3834 draw_state = WL_SIGN;
3835 /* Show the sign column when there are any signs in this
3836 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003837 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003839 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003841 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842# endif
3843
3844 /* Draw two cells with the sign value or blank. */
3845 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003846 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 n_extra = 2;
3848
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003849 if (row == startrow
3850#ifdef FEAT_DIFF
3851 + filler_lines && filler_todo <= 0
3852#endif
3853 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3856 SIGN_TEXT);
3857# ifdef FEAT_SIGN_ICONS
3858 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3859 SIGN_ICON);
3860 if (gui.in_use && icon_sign != 0)
3861 {
3862 /* Use the image in this position. */
3863 c_extra = SIGN_BYTE;
3864# ifdef FEAT_NETBEANS_INTG
3865 if (buf_signcount(wp->w_buffer, lnum) > 1)
3866 c_extra = MULTISIGN_BYTE;
3867# endif
3868 char_attr = icon_sign;
3869 }
3870 else
3871# endif
3872 if (text_sign != 0)
3873 {
3874 p_extra = sign_get_text(text_sign);
3875 if (p_extra != NULL)
3876 {
3877 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003878 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 char_attr = sign_get_attr(text_sign, FALSE);
3881 }
3882 }
3883 }
3884 }
3885#endif
3886
3887 if (draw_state == WL_NR - 1 && n_extra == 0)
3888 {
3889 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003890 /* Display the absolute or relative line number. After the
3891 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3892 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 && (row == startrow
3894#ifdef FEAT_DIFF
3895 + filler_lines
3896#endif
3897 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3898 {
3899 /* Draw the line number (empty space after wrapping). */
3900 if (row == startrow
3901#ifdef FEAT_DIFF
3902 + filler_lines
3903#endif
3904 )
3905 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003906 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003907 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003908
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003909 if (wp->w_p_nu && !wp->w_p_rnu)
3910 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003911 num = (long)lnum;
3912 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003913 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003914 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003915 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003916 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003917 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003918 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003919 num = lnum;
3920 fmt = "%-*ld ";
3921 }
3922 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003923
Bram Moolenaar700e7342013-01-30 12:31:36 +01003924 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003925 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (wp->w_skipcol > 0)
3927 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3928 *p_extra = '-';
3929#ifdef FEAT_RIGHTLEFT
3930 if (wp->w_p_rl) /* reverse line numbers */
3931 rl_mirror(extra);
3932#endif
3933 p_extra = extra;
3934 c_extra = NUL;
3935 }
3936 else
3937 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003938 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003940#ifdef FEAT_SYN_HL
3941 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003942 * the current line differently.
3943 * TODO: Can we use CursorLine instead of CursorLineNr
3944 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003945 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003946 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003947 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
3951
Bram Moolenaar597a4222014-06-25 14:39:50 +02003952#ifdef FEAT_LINEBREAK
3953 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3954 && n_extra == 0 && *p_sbr != NUL)
3955 /* draw indent after showbreak value */
3956 draw_state = WL_BRI;
3957 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3958 /* After the showbreak, draw the breakindent */
3959 draw_state = WL_BRI - 1;
3960
3961 /* draw 'breakindent': indent wrapped text accordingly */
3962 if (draw_state == WL_BRI - 1 && n_extra == 0)
3963 {
3964 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003965 /* if need_showbreak is set, breakindent also applies */
3966 if (wp->w_p_bri && n_extra == 0
3967 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003968# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003969 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003970# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003971 )
3972 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003973 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003974# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003975 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003976 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003977 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003978# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003979 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3980 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003981 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003982# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003983 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003984# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003985 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003986 c_extra = ' ';
3987 n_extra = get_breakindent_win(wp,
3988 ml_get_buf(wp->w_buffer, lnum, FALSE));
3989 /* Correct end of highlighted area for 'breakindent',
3990 * required when 'linebreak' is also set. */
3991 if (tocol == vcol)
3992 tocol += n_extra;
3993 }
3994 }
3995#endif
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3998 if (draw_state == WL_SBR - 1 && n_extra == 0)
3999 {
4000 draw_state = WL_SBR;
4001# ifdef FEAT_DIFF
4002 if (filler_todo > 0)
4003 {
4004 /* Draw "deleted" diff line(s). */
4005 if (char2cells(fill_diff) > 1)
4006 c_extra = '-';
4007 else
4008 c_extra = fill_diff;
4009# ifdef FEAT_RIGHTLEFT
4010 if (wp->w_p_rl)
4011 n_extra = col + 1;
4012 else
4013# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004014 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004015 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017# endif
4018# ifdef FEAT_LINEBREAK
4019 if (*p_sbr != NUL && need_showbreak)
4020 {
4021 /* Draw 'showbreak' at the start of each broken line. */
4022 p_extra = p_sbr;
4023 c_extra = NUL;
4024 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004025 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004027 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 /* Correct end of highlighted area for 'showbreak',
4029 * required when 'linebreak' is also set. */
4030 if (tocol == vcol)
4031 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004032#ifdef FEAT_SYN_HL
4033 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004034 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004035 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004036 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039# endif
4040 }
4041#endif
4042
4043 if (draw_state == WL_LINE - 1 && n_extra == 0)
4044 {
4045 draw_state = WL_LINE;
4046 if (saved_n_extra)
4047 {
4048 /* Continue item from end of wrapped line. */
4049 n_extra = saved_n_extra;
4050 c_extra = saved_c_extra;
4051 p_extra = saved_p_extra;
4052 char_attr = saved_char_attr;
4053 }
4054 else
4055 char_attr = 0;
4056 }
4057 }
4058
4059 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004060 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062#ifdef FEAT_DIFF
4063 && filler_todo <= 0
4064#endif
4065 )
4066 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004067 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004068 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 /* Pretend we have finished updating the window. Except when
4070 * 'cursorcolumn' is set. */
4071#ifdef FEAT_SYN_HL
4072 if (wp->w_p_cuc)
4073 row = wp->w_cline_row + wp->w_cline_height;
4074 else
4075#endif
4076 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 break;
4078 }
4079
4080 if (draw_state == WL_LINE && area_highlighting)
4081 {
4082 /* handle Visual or match highlighting in this line */
4083 if (vcol == fromcol
4084#ifdef FEAT_MBYTE
4085 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4086 && (*mb_ptr2cells)(ptr) > 1)
4087#endif
4088 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004089 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 && vcol < tocol))
4091 area_attr = attr; /* start highlighting */
4092 else if (area_attr != 0
4093 && (vcol == tocol
4094 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097#ifdef FEAT_SEARCH_EXTRA
4098 if (!n_extra)
4099 {
4100 /*
4101 * Check for start/end of search pattern match.
4102 * After end, check for start/end of next match.
4103 * When another match, have to check for start again.
4104 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004105 * Do this for 'search_hl' and the match list (ordered by
4106 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004108 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004109 cur = wp->w_match_head;
4110 shl_flag = FALSE;
4111 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004113 if (shl_flag == FALSE
4114 && ((cur != NULL
4115 && cur->priority > SEARCH_HL_PRIORITY)
4116 || cur == NULL))
4117 {
4118 shl = &search_hl;
4119 shl_flag = TRUE;
4120 }
4121 else
4122 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004123 if (cur != NULL)
4124 cur->pos.cur = 0;
4125 pos_inprogress = TRUE;
4126 while (shl->rm.regprog != NULL
4127 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004129 if (shl->startcol != MAXCOL
4130 && v >= (long)shl->startcol
4131 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004133#ifdef FEAT_MBYTE
4134 int tmp_col = v + MB_PTR2LEN(ptr);
4135
4136 if (shl->endcol < tmp_col)
4137 shl->endcol = tmp_col;
4138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004140#ifdef FEAT_CONCEAL
4141 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4142 == cur->hlg_id)
4143 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004144 has_match_conc =
4145 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004146 match_conc = cur->conceal_char;
4147 }
4148 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004149 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004152 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
4154 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004155 next_search_hl(wp, shl, lnum, (colnr_T)v,
4156 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004157 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004158 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 /* Need to get the line again, a multi-line regexp
4161 * may have made it invalid. */
4162 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4163 ptr = line + v;
4164
4165 if (shl->lnum == lnum)
4166 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004167 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004169 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004171 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004173 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 /* highlight empty match, try again after
4176 * it */
4177#ifdef FEAT_MBYTE
4178 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004179 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004180 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else
4182#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004183 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 /* Loop to check if the match starts at the
4187 * current position */
4188 continue;
4189 }
4190 }
4191 break;
4192 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004193 if (shl != &search_hl && cur != NULL)
4194 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004196
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 /* Use attributes from match with highest priority among
4198 * 'search_hl' and the match list. */
4199 search_attr = search_hl.attr_cur;
4200 cur = wp->w_match_head;
4201 shl_flag = FALSE;
4202 while (cur != NULL || shl_flag == FALSE)
4203 {
4204 if (shl_flag == FALSE
4205 && ((cur != NULL
4206 && cur->priority > SEARCH_HL_PRIORITY)
4207 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004208 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004209 shl = &search_hl;
4210 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004211 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004212 else
4213 shl = &cur->hl;
4214 if (shl->attr_cur != 0)
4215 search_attr = shl->attr_cur;
4216 if (shl != &search_hl && cur != NULL)
4217 cur = cur->next;
4218 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004219 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004220 if (*ptr == NUL && (did_line_attr >= 1
4221 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004222 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224#endif
4225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004227 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004229 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4230 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004232 if (diff_hlf == HLF_TXD && ptr - line > change_end
4233 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004235 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004236 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004237 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004240
4241 /* Decide which of the highlight attributes to use. */
4242 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004243#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004244 if (area_attr != 0)
4245 char_attr = hl_combine_attr(line_attr, area_attr);
4246 else if (search_attr != 0)
4247 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004248 /* Use line_attr when not in the Visual or 'incsearch' area
4249 * (area_attr may be 0 when "noinvcur" is set). */
4250 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004251 || vcol < fromcol || vcol_prev < fromcol_prev
4252 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004253 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004254#else
4255 if (area_attr != 0)
4256 char_attr = area_attr;
4257 else if (search_attr != 0)
4258 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004259#endif
4260 else
4261 {
4262 attr_pri = FALSE;
4263#ifdef FEAT_SYN_HL
4264 if (has_syntax)
4265 char_attr = syntax_attr;
4266 else
4267#endif
4268 char_attr = 0;
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271
4272 /*
4273 * Get the next character to put on the screen.
4274 */
4275 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004276 * The "p_extra" points to the extra stuff that is inserted to
4277 * represent special characters (non-printable stuff) and other
4278 * things. When all characters are the same, c_extra is used.
4279 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4280 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4282 */
4283 if (n_extra > 0)
4284 {
4285 if (c_extra != NUL)
4286 {
4287 c = c_extra;
4288#ifdef FEAT_MBYTE
4289 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004290 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
4292 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004293 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004294 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 else
4297 mb_utf8 = FALSE;
4298#endif
4299 }
4300 else
4301 {
4302 c = *p_extra;
4303#ifdef FEAT_MBYTE
4304 if (has_mbyte)
4305 {
4306 mb_c = c;
4307 if (enc_utf8)
4308 {
4309 /* If the UTF-8 character is more than one byte:
4310 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004311 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 mb_utf8 = FALSE;
4313 if (mb_l > n_extra)
4314 mb_l = 1;
4315 else if (mb_l > 1)
4316 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004317 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004319 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321 }
4322 else
4323 {
4324 /* if this is a DBCS character, put it in "mb_c" */
4325 mb_l = MB_BYTE2LEN(c);
4326 if (mb_l >= n_extra)
4327 mb_l = 1;
4328 else if (mb_l > 1)
4329 mb_c = (c << 8) + p_extra[1];
4330 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004331 if (mb_l == 0) /* at the NUL at end-of-line */
4332 mb_l = 1;
4333
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* If a double-width char doesn't fit display a '>' in the
4335 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004336 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337# ifdef FEAT_RIGHTLEFT
4338 wp->w_p_rl ? (col <= 0) :
4339# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004340 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 && (*mb_char2cells)(mb_c) == 2)
4342 {
4343 c = '>';
4344 mb_c = c;
4345 mb_l = 1;
4346 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004347 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 /* put the pointer back to output the double-width
4349 * character at the start of the next line. */
4350 ++n_extra;
4351 --p_extra;
4352 }
4353 else
4354 {
4355 n_extra -= mb_l - 1;
4356 p_extra += mb_l - 1;
4357 }
4358 }
4359#endif
4360 ++p_extra;
4361 }
4362 --n_extra;
4363 }
4364 else
4365 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004366#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004367 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004368#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004369
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004370 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004371 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 /*
4373 * Get a character from the line itself.
4374 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004375 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004376#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004377 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_MBYTE
4380 if (has_mbyte)
4381 {
4382 mb_c = c;
4383 if (enc_utf8)
4384 {
4385 /* If the UTF-8 character is more than one byte: Decode it
4386 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004387 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 mb_utf8 = FALSE;
4389 if (mb_l > 1)
4390 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004391 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 /* Overlong encoded ASCII or ASCII with composing char
4393 * is displayed normally, except a NUL. */
4394 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004395 {
4396 c = mb_c;
4397# ifdef FEAT_LINEBREAK
4398 c0 = mb_c;
4399# endif
4400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004402
4403 /* At start of the line we can have a composing char.
4404 * Draw it as a space with a composing char. */
4405 if (utf_iscomposing(mb_c))
4406 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004407 int i;
4408
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004409 for (i = Screen_mco - 1; i > 0; --i)
4410 u8cc[i] = u8cc[i - 1];
4411 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004412 mb_c = ' ';
4413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415
4416 if ((mb_l == 1 && c >= 0x80)
4417 || (mb_l >= 1 && mb_c == 0)
4418 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004419# ifdef UNICODE16
4420 || mb_c >= 0x10000
4421# endif
4422 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 /*
4425 * Illegal UTF-8 byte: display as <xx>.
4426 * Non-BMP character : display as ? or fullwidth ?.
4427 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004428# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
4432 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004433# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (wp->w_p_rl) /* reverse */
4435 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004438# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 else if (utf_char2cells(mb_c) != 2)
4440 STRCPY(extra, "?");
4441 else
4442 /* 0xff1f in UTF-8: full-width '?' */
4443 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 p_extra = extra;
4447 c = *p_extra;
4448 mb_c = mb_ptr2char_adv(&p_extra);
4449 mb_utf8 = (c >= 0x80);
4450 n_extra = (int)STRLEN(p_extra);
4451 c_extra = NUL;
4452 if (area_attr == 0 && search_attr == 0)
4453 {
4454 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004455 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 saved_attr2 = char_attr; /* save current attr */
4457 }
4458 }
4459 else if (mb_l == 0) /* at the NUL at end-of-line */
4460 mb_l = 1;
4461#ifdef FEAT_ARABIC
4462 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4463 {
4464 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004465 int pc, pc1, nc;
4466 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 /* The idea of what is the previous and next
4469 * character depends on 'rightleft'. */
4470 if (wp->w_p_rl)
4471 {
4472 pc = prev_c;
4473 pc1 = prev_c1;
4474 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004475 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else
4478 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004479 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 prev_c = mb_c;
4484
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004485 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else
4488 prev_c = mb_c;
4489#endif
4490 }
4491 else /* enc_dbcs */
4492 {
4493 mb_l = MB_BYTE2LEN(c);
4494 if (mb_l == 0) /* at the NUL at end-of-line */
4495 mb_l = 1;
4496 else if (mb_l > 1)
4497 {
4498 /* We assume a second byte below 32 is illegal.
4499 * Hopefully this is OK for all double-byte encodings!
4500 */
4501 if (ptr[1] >= 32)
4502 mb_c = (c << 8) + ptr[1];
4503 else
4504 {
4505 if (ptr[1] == NUL)
4506 {
4507 /* head byte at end of line */
4508 mb_l = 1;
4509 transchar_nonprint(extra, c);
4510 }
4511 else
4512 {
4513 /* illegal tail byte */
4514 mb_l = 2;
4515 STRCPY(extra, "XX");
4516 }
4517 p_extra = extra;
4518 n_extra = (int)STRLEN(extra) - 1;
4519 c_extra = NUL;
4520 c = *p_extra++;
4521 if (area_attr == 0 && search_attr == 0)
4522 {
4523 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004524 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 saved_attr2 = char_attr; /* save current attr */
4526 }
4527 mb_c = c;
4528 }
4529 }
4530 }
4531 /* If a double-width char doesn't fit display a '>' in the
4532 * last column; the character is displayed at the start of the
4533 * next line. */
4534 if ((
4535# ifdef FEAT_RIGHTLEFT
4536 wp->w_p_rl ? (col <= 0) :
4537# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004538 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 && (*mb_char2cells)(mb_c) == 2)
4540 {
4541 c = '>';
4542 mb_c = c;
4543 mb_utf8 = FALSE;
4544 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004545 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 /* Put pointer back so that the character will be
4547 * displayed at the start of the next line. */
4548 --ptr;
4549 }
4550 else if (*ptr != NUL)
4551 ptr += mb_l - 1;
4552
4553 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004554 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004555 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004556 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004559 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 c = ' ';
4561 if (area_attr == 0 && search_attr == 0)
4562 {
4563 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004564 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 saved_attr2 = char_attr; /* save current attr */
4566 }
4567 mb_c = c;
4568 mb_utf8 = FALSE;
4569 mb_l = 1;
4570 }
4571
4572 }
4573#endif
4574 ++ptr;
4575
4576 if (extra_check)
4577 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004578#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004579 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004580#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004581
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004582#ifdef FEAT_TERMINAL
4583 if (get_term_attr)
4584 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004585 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004586
4587 if (!attr_pri)
4588 char_attr = syntax_attr;
4589 else
4590 char_attr = hl_combine_attr(syntax_attr, char_attr);
4591 }
4592#endif
4593
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004594#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /* Get syntax attribute, unless still at the start of the line
4596 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004597 v = (long)(ptr - line);
4598 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
4600 /* Get the syntax attribute for the character. If there
4601 * is an error, disable syntax highlighting. */
4602 save_did_emsg = did_emsg;
4603 did_emsg = FALSE;
4604
Bram Moolenaar217ad922005-03-20 22:37:15 +00004605 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004606# ifdef FEAT_SPELL
4607 has_spell ? &can_spell :
4608# endif
4609 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004612 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004613 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004614 has_syntax = FALSE;
4615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 else
4617 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004618#ifdef SYN_TIME_LIMIT
4619 if (wp->w_s->b_syn_slow)
4620 has_syntax = FALSE;
4621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 /* Need to get the line again, a multi-line regexp may
4624 * have made it invalid. */
4625 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4626 ptr = line + v;
4627
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004628 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004630 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004631 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004632# ifdef FEAT_CONCEAL
4633 /* no concealing past the end of the line, it interferes
4634 * with line highlighting */
4635 if (c == NUL)
4636 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004637 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004638 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004641#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004642
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004643#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004644 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004645 * Only do this when there is no syntax highlighting, the
4646 * @Spell cluster is not used or the current syntax item
4647 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004648 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004649 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004650 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004651# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004652 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004653 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004654# endif
4655 if (c != 0 && (
4656# ifdef FEAT_SYN_HL
4657 !has_syntax ||
4658# endif
4659 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004660 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004661 char_u *prev_ptr, *p;
4662 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004663 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004664# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004665 if (has_mbyte)
4666 {
4667 prev_ptr = ptr - mb_l;
4668 v -= mb_l - 1;
4669 }
4670 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004671# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004672 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004673
4674 /* Use nextline[] if possible, it has the start of the
4675 * next line concatenated. */
4676 if ((prev_ptr - line) - nextlinecol >= 0)
4677 p = nextline + (prev_ptr - line) - nextlinecol;
4678 else
4679 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004680 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004681 len = spell_check(wp, p, &spell_hlf, &cap_col,
4682 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004683 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004684
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004685 /* In Insert mode only highlight a word that
4686 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004687 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004688 && (State & INSERT) != 0
4689 && wp->w_cursor.lnum == lnum
4690 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004691 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004692 && wp->w_cursor.col < (colnr_T)word_end)
4693 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004694 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004695 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004697
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004698 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004699 && (p - nextline) + len > nextline_idx)
4700 {
4701 /* Remember that the good word continues at the
4702 * start of the next line. */
4703 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004704 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004705 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004706
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004707 /* Turn index into actual attributes. */
4708 if (spell_hlf != HLF_COUNT)
4709 spell_attr = highlight_attr[spell_hlf];
4710
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004711 if (cap_col > 0)
4712 {
4713 if (p != prev_ptr
4714 && (p - nextline) + cap_col >= nextline_idx)
4715 {
4716 /* Remember that the word in the next line
4717 * must start with a capital. */
4718 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004719 cap_col = (int)((p - nextline) + cap_col
4720 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004721 }
4722 else
4723 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004724 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004725 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004726 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004727 }
4728 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004729 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004730 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004731 char_attr = hl_combine_attr(char_attr, spell_attr);
4732 else
4733 char_attr = hl_combine_attr(spell_attr, char_attr);
4734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735#endif
4736#ifdef FEAT_LINEBREAK
4737 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004738 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004740 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004741 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004743# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004744 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004745# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004746 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004748 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004750 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004751
Bram Moolenaar597a4222014-06-25 14:39:50 +02004752 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004753 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004754 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004755 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaara3650912014-11-19 13:21:57 +01004756 n_extra = (int)wp->w_buffer->b_p_ts
4757 - vcol % (int)wp->w_buffer->b_p_ts - 1;
4758
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004759# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004760 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004761# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004763# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004764 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004765 {
4766#ifdef FEAT_CONCEAL
4767 if (c == TAB)
4768 /* See "Tab alignment" below. */
4769 FIX_FOR_BOGUSCOLS;
4770#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004771 if (!wp->w_p_list)
4772 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775#endif
4776
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004777 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4778 */
4779 if (wp->w_p_list
4780 && (((c == 160
4781#ifdef FEAT_MBYTE
4782 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4783#endif
4784 ) && lcs_nbsp)
4785 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4786 {
4787 c = (c == ' ') ? lcs_space : lcs_nbsp;
4788 if (area_attr == 0 && search_attr == 0)
4789 {
4790 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004791 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004792 saved_attr2 = char_attr; /* save current attr */
4793 }
4794#ifdef FEAT_MBYTE
4795 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004796 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004797 {
4798 mb_utf8 = TRUE;
4799 u8cc[0] = 0;
4800 c = 0xc0;
4801 }
4802 else
4803 mb_utf8 = FALSE;
4804#endif
4805 }
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4808 {
4809 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004810 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004813 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 saved_attr2 = char_attr; /* save current attr */
4815 }
4816#ifdef FEAT_MBYTE
4817 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004818 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 {
4820 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004821 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004822 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824 else
4825 mb_utf8 = FALSE;
4826#endif
4827 }
4828 }
4829
4830 /*
4831 * Handling of non-printable characters.
4832 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004833 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 /*
4836 * when getting a character from the file, we may have to
4837 * turn it into something else on the way to putting it
4838 * into "ScreenLines".
4839 */
4840 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4841 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004842 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004843 long vcol_adjusted = vcol; /* removed showbreak length */
4844#ifdef FEAT_LINEBREAK
4845 /* only adjust the tab_len, when at the first column
4846 * after the showbreak value was drawn */
4847 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4848 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004851#ifdef FEAT_VARTABS
4852 tab_len = tabstop_padding(vcol_adjusted,
4853 wp->w_buffer->b_p_ts,
4854 wp->w_buffer->b_p_vts_array) - 1;
4855#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004856 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004857 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4858#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004859
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004860#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004861 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004862#endif
4863 /* tab amount depends on current column */
4864 n_extra = tab_len;
4865#ifdef FEAT_LINEBREAK
4866 else
4867 {
4868 char_u *p;
4869 int len = n_extra;
4870 int i;
4871 int saved_nextra = n_extra;
4872
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004873#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004874 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004875 /* there are characters to conceal */
4876 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004877 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4878 */
4879 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4880 && n_extra > tab_len)
4881 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004882#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004883
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004884 /* if n_extra > 0, it gives the number of chars, to
4885 * use for a tab, else we need to calculate the width
4886 * for a tab */
4887#ifdef FEAT_MBYTE
4888 len = (tab_len * mb_char2len(lcs_tab2));
4889 if (n_extra > 0)
4890 len += n_extra - tab_len;
4891#endif
4892 c = lcs_tab1;
4893 p = alloc((unsigned)(len + 1));
4894 vim_memset(p, ' ', len);
4895 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004896 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004897 p_extra_free = p;
4898 for (i = 0; i < tab_len; i++)
4899 {
4900#ifdef FEAT_MBYTE
4901 mb_char2bytes(lcs_tab2, p);
4902 p += mb_char2len(lcs_tab2);
4903 n_extra += mb_char2len(lcs_tab2)
4904 - (saved_nextra > 0 ? 1 : 0);
4905#else
4906 p[i] = lcs_tab2;
4907#endif
4908 }
4909 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004910#ifdef FEAT_CONCEAL
4911 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4912 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004913 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004914 n_extra -= vcol_off;
4915#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004916 }
4917#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004918#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004919 {
4920 int vc_saved = vcol_off;
4921
4922 /* Tab alignment should be identical regardless of
4923 * 'conceallevel' value. So tab compensates of all
4924 * previous concealed characters, and thus resets
4925 * vcol_off and boguscols accumulated so far in the
4926 * line. Note that the tab can be longer than
4927 * 'tabstop' when there are concealed characters. */
4928 FIX_FOR_BOGUSCOLS;
4929
4930 /* Make sure, the highlighting for the tab char will be
4931 * correctly set further below (effectively reverts the
4932 * FIX_FOR_BOGSUCOLS macro */
4933 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004934 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004935 tab_len += vc_saved;
4936 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#ifdef FEAT_MBYTE
4939 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4940#endif
4941 if (wp->w_p_list)
4942 {
4943 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004944#ifdef FEAT_LINEBREAK
4945 if (wp->w_p_lbr)
4946 c_extra = NUL; /* using p_extra from above */
4947 else
4948#endif
4949 c_extra = lcs_tab2;
4950 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004951 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 saved_attr2 = char_attr; /* save current attr */
4953#ifdef FEAT_MBYTE
4954 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004955 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004958 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004959 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 }
4961#endif
4962 }
4963 else
4964 {
4965 c_extra = ' ';
4966 c = ' ';
4967 }
4968 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004969 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004970 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004971 || ((fromcol >= 0 || fromcol_prev >= 0)
4972 && tocol > vcol
4973 && VIsual_mode != Ctrl_V
4974 && (
4975# ifdef FEAT_RIGHTLEFT
4976 wp->w_p_rl ? (col >= 0) :
4977# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004978 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004979 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004980 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004981 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004982 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004984 /* Display a '$' after the line or highlight an extra
4985 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4987 /* For a diff line the highlighting continues after the
4988 * "$". */
4989 if (
4990# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004991 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992# ifdef LINE_ATTR
4993 &&
4994# endif
4995# endif
4996# ifdef LINE_ATTR
4997 line_attr == 0
4998# endif
4999 )
5000#endif
5001 {
5002#ifdef FEAT_VIRTUALEDIT
5003 /* In virtualedit, visual selections may extend
5004 * beyond end of line. */
5005 if (area_highlighting && virtual_active()
5006 && tocol != MAXCOL && vcol < tocol)
5007 n_extra = 0;
5008 else
5009#endif
5010 {
5011 p_extra = at_end_str;
5012 n_extra = 1;
5013 c_extra = NUL;
5014 }
5015 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005016 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005017 c = lcs_eol;
5018 else
5019 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 lcs_eol_one = -1;
5021 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005022 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005024 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 n_attr = 1;
5026 }
5027#ifdef FEAT_MBYTE
5028 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005029 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005032 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005033 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 else
5036 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5037#endif
5038 }
5039 else if (c != NUL)
5040 {
5041 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005042 if (n_extra == 0)
5043 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#ifdef FEAT_RIGHTLEFT
5045 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5046 rl_mirror(p_extra); /* reverse "<12>" */
5047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005049#ifdef FEAT_LINEBREAK
5050 if (wp->w_p_lbr)
5051 {
5052 char_u *p;
5053
5054 c = *p_extra;
5055 p = alloc((unsigned)n_extra + 1);
5056 vim_memset(p, ' ', n_extra);
5057 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5058 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005059 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005060 p_extra_free = p_extra = p;
5061 }
5062 else
5063#endif
5064 {
5065 n_extra = byte2cells(c) - 1;
5066 c = *p_extra++;
5067 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005068 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005071 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 saved_attr2 = char_attr; /* save current attr */
5073 }
5074#ifdef FEAT_MBYTE
5075 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5076#endif
5077 }
5078#ifdef FEAT_VIRTUALEDIT
5079 else if (VIsual_active
5080 && (VIsual_mode == Ctrl_V
5081 || VIsual_mode == 'v')
5082 && virtual_active()
5083 && tocol != MAXCOL
5084 && vcol < tocol
5085 && (
5086# ifdef FEAT_RIGHTLEFT
5087 wp->w_p_rl ? (col >= 0) :
5088# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005089 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 c = ' ';
5092 --ptr; /* put it back at the NUL */
5093 }
5094#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005095#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else if ((
5097# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005098 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005100# ifdef FEAT_TERMINAL
5101 term_attr != 0 ||
5102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 ) && (
5105# ifdef FEAT_RIGHTLEFT
5106 wp->w_p_rl ? (col >= 0) :
5107# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005108 (col
5109# ifdef FEAT_CONCEAL
5110 - boguscols
5111# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005112 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 /* Highlight until the right side of the window */
5115 c = ' ';
5116 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005117
5118 /* Remember we do the char for line highlighting. */
5119 ++did_line_attr;
5120
5121 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005122 if (line_attr != 0 && char_attr == search_attr
5123 && (did_line_attr > 1
5124 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005125 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126# ifdef FEAT_DIFF
5127 if (diff_hlf == HLF_TXD)
5128 {
5129 diff_hlf = HLF_CHD;
5130 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005131 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005132 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005133 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5134 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005135 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005139# ifdef FEAT_TERMINAL
5140 if (term_attr != 0)
5141 {
5142 char_attr = term_attr;
5143 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5144 char_attr = hl_combine_attr(char_attr,
5145 HL_ATTR(HLF_CUL));
5146 }
5147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149#endif
5150 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005151
5152#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005153 if ( wp->w_p_cole > 0
5154 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005155 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005156 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005157 && !(lnum_in_visual_area
5158 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005159 {
5160 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005161 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005162 && (syn_get_sub_char() != NUL || match_conc
5163 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005164 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005165 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005166 /* First time at this concealed item: display one
5167 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005168 if (match_conc)
5169 c = match_conc;
5170 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005171 c = syn_get_sub_char();
5172 else if (lcs_conceal != NUL)
5173 c = lcs_conceal;
5174 else
5175 c = ' ';
5176
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005177 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005178
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005179 if (n_extra > 0)
5180 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005181 vcol += n_extra;
5182 if (wp->w_p_wrap && n_extra > 0)
5183 {
5184# ifdef FEAT_RIGHTLEFT
5185 if (wp->w_p_rl)
5186 {
5187 col -= n_extra;
5188 boguscols -= n_extra;
5189 }
5190 else
5191# endif
5192 {
5193 boguscols += n_extra;
5194 col += n_extra;
5195 }
5196 }
5197 n_extra = 0;
5198 n_attr = 0;
5199 }
5200 else if (n_skip == 0)
5201 {
5202 is_concealing = TRUE;
5203 n_skip = 1;
5204 }
5205# ifdef FEAT_MBYTE
5206 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005207 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005208 {
5209 mb_utf8 = TRUE;
5210 u8cc[0] = 0;
5211 c = 0xc0;
5212 }
5213 else
5214 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5215# endif
5216 }
5217 else
5218 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005219 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005220 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005221 }
5222#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005225#ifdef FEAT_CONCEAL
5226 /* In the cursor line and we may be concealing characters: correct
5227 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005228 if (!did_wcol && draw_state == WL_LINE
5229 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005230 && conceal_cursor_line(wp)
5231 && (int)wp->w_virtcol <= vcol + n_skip)
5232 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005233# ifdef FEAT_RIGHTLEFT
5234 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005235 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005236 else
5237# endif
5238 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005239 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005240 did_wcol = TRUE;
5241 }
5242#endif
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 /* Don't override visual selection highlighting. */
5245 if (n_attr > 0
5246 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005247 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 char_attr = extra_attr;
5249
Bram Moolenaar81695252004-12-29 20:58:21 +00005250#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 /* XIM don't send preedit_start and preedit_end, but they send
5252 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5253 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005254 if (p_imst == IM_ON_THE_SPOT
5255 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005256 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 && (State & INSERT)
5258 && !p_imdisable
5259 && im_is_preediting()
5260 && draw_state == WL_LINE)
5261 {
5262 colnr_T tcol;
5263
5264 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005265 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 else
5267 tcol = preedit_end_col;
5268 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5269 {
5270 if (feedback_old_attr < 0)
5271 {
5272 feedback_col = 0;
5273 feedback_old_attr = char_attr;
5274 }
5275 char_attr = im_get_feedback_attr(feedback_col);
5276 if (char_attr < 0)
5277 char_attr = feedback_old_attr;
5278 feedback_col++;
5279 }
5280 else if (feedback_old_attr >= 0)
5281 {
5282 char_attr = feedback_old_attr;
5283 feedback_old_attr = -1;
5284 feedback_col = 0;
5285 }
5286 }
5287#endif
5288 /*
5289 * Handle the case where we are in column 0 but not on the first
5290 * character of the line and the user wants us to show us a
5291 * special character (via 'listchars' option "precedes:<char>".
5292 */
5293 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005294 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5296#ifdef FEAT_DIFF
5297 && filler_todo <= 0
5298#endif
5299 && draw_state > WL_NR
5300 && c != NUL)
5301 {
5302 c = lcs_prec;
5303 lcs_prec_todo = NUL;
5304#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005305 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5306 {
5307 /* Double-width character being overwritten by the "precedes"
5308 * character, need to fill up half the character. */
5309 c_extra = MB_FILLER_CHAR;
5310 n_extra = 1;
5311 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005312 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005315 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
5317 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005318 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005319 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 else
5322 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5323#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005324 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005327 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 n_attr3 = 1;
5329 }
5330 }
5331
5332 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005333 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005335 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005336#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005337 || did_line_attr == 1
5338#endif
5339 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005341#ifdef FEAT_SEARCH_EXTRA
5342 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005343
5344 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005345 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005346 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005347#endif
5348
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005349 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 * highlight match at end of line. If it's beyond the last
5351 * char on the screen, just overwrite that one (tricky!) Not
5352 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005353#ifdef FEAT_SEARCH_EXTRA
5354 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005355 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005356 prevcol_hl_flag = TRUE;
5357 else
5358 {
5359 cur = wp->w_match_head;
5360 while (cur != NULL)
5361 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005362 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005363 {
5364 prevcol_hl_flag = TRUE;
5365 break;
5366 }
5367 cur = cur->next;
5368 }
5369 }
5370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005372 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005373 && (VIsual_mode != Ctrl_V
5374 || lnum == VIsual.lnum
5375 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005376 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377#ifdef FEAT_SEARCH_EXTRA
5378 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005379 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005380# ifdef FEAT_SYN_HL
5381 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5382 && !(wp == curwin && VIsual_active))
5383# endif
5384# ifdef FEAT_DIFF
5385 && diff_hlf == (hlf_T)0
5386# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005387# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005388 && did_line_attr <= 1
5389# endif
5390 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#endif
5392 ))
5393 {
5394 int n = 0;
5395
5396#ifdef FEAT_RIGHTLEFT
5397 if (wp->w_p_rl)
5398 {
5399 if (col < 0)
5400 n = 1;
5401 }
5402 else
5403#endif
5404 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005405 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 n = -1;
5407 }
5408 if (n != 0)
5409 {
5410 /* At the window boundary, highlight the last character
5411 * instead (better than nothing). */
5412 off += n;
5413 col += n;
5414 }
5415 else
5416 {
5417 /* Add a blank character to highlight. */
5418 ScreenLines[off] = ' ';
5419#ifdef FEAT_MBYTE
5420 if (enc_utf8)
5421 ScreenLinesUC[off] = 0;
5422#endif
5423 }
5424#ifdef FEAT_SEARCH_EXTRA
5425 if (area_attr == 0)
5426 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005427 /* Use attributes from match with highest priority among
5428 * 'search_hl' and the match list. */
5429 char_attr = search_hl.attr;
5430 cur = wp->w_match_head;
5431 shl_flag = FALSE;
5432 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005433 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005434 if (shl_flag == FALSE
5435 && ((cur != NULL
5436 && cur->priority > SEARCH_HL_PRIORITY)
5437 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005438 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005439 shl = &search_hl;
5440 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005441 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005442 else
5443 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005444 if ((ptr - line) - 1 == (long)shl->startcol
5445 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005446 char_attr = shl->attr;
5447 if (shl != &search_hl && cur != NULL)
5448 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451#endif
5452 ScreenAttrs[off] = char_attr;
5453#ifdef FEAT_RIGHTLEFT
5454 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005457 --off;
5458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 else
5460#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005463 ++off;
5464 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005465 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005466#ifdef FEAT_SYN_HL
5467 eol_hl_off = 1;
5468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaar91170f82006-05-05 21:15:17 +00005472 /*
5473 * At end of the text line.
5474 */
5475 if (c == NUL)
5476 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005477#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005478 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5479 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005480 {
5481 /* highlight last char after line */
5482 --col;
5483 --off;
5484 --vcol;
5485 }
5486
Bram Moolenaar1a384422010-07-14 19:53:30 +02005487 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005488 if (wp->w_p_wrap)
5489 v = wp->w_skipcol;
5490 else
5491 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005493 /* check if line ends before left margin */
5494 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005495 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005496#ifdef FEAT_CONCEAL
5497 /* Get rid of the boguscols now, we want to draw until the right
5498 * edge for 'cursorcolumn'. */
5499 col -= boguscols;
5500 boguscols = 0;
5501#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005502
5503 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005504 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505
5506 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005507 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5508 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005509 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005510 && lnum != wp->w_cursor.lnum)
5511 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005512# ifdef FEAT_RIGHTLEFT
5513 && !wp->w_p_rl
5514# endif
5515 )
5516 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005517 int rightmost_vcol = 0;
5518 int i;
5519
5520 if (wp->w_p_cuc)
5521 rightmost_vcol = wp->w_virtcol;
5522 if (draw_color_col)
5523 /* determine rightmost colorcolumn to possibly draw */
5524 for (i = 0; color_cols[i] >= 0; ++i)
5525 if (rightmost_vcol < color_cols[i])
5526 rightmost_vcol = color_cols[i];
5527
Bram Moolenaar02631462017-09-22 15:20:32 +02005528 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005529 {
5530 ScreenLines[off] = ' ';
5531#ifdef FEAT_MBYTE
5532 if (enc_utf8)
5533 ScreenLinesUC[off] = 0;
5534#endif
5535 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005536 if (draw_color_col)
5537 draw_color_col = advance_color_col(VCOL_HLC,
5538 &color_cols);
5539
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005540 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005541 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005542 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005543 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005544 else
5545 ScreenAttrs[off++] = 0;
5546
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005547 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005548 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005549
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005550 ++vcol;
5551 }
5552 }
5553#endif
5554
Bram Moolenaar53f81742017-09-22 14:35:51 +02005555 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005556 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 row++;
5558
5559 /*
5560 * Update w_cline_height and w_cline_folded if the cursor line was
5561 * updated (saves a call to plines() later).
5562 */
5563 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5564 {
5565 curwin->w_cline_row = startrow;
5566 curwin->w_cline_height = row - startrow;
5567#ifdef FEAT_FOLDING
5568 curwin->w_cline_folded = FALSE;
5569#endif
5570 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5571 }
5572
5573 break;
5574 }
5575
5576 /* line continues beyond line end */
5577 if (lcs_ext
5578 && !wp->w_p_wrap
5579#ifdef FEAT_DIFF
5580 && filler_todo <= 0
5581#endif
5582 && (
5583#ifdef FEAT_RIGHTLEFT
5584 wp->w_p_rl ? col == 0 :
5585#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005586 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005588 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5590 {
5591 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005592 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#ifdef FEAT_MBYTE
5594 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005595 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 {
5597 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005598 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005599 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
5601 else
5602 mb_utf8 = FALSE;
5603#endif
5604 }
5605
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005606#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005607 /* advance to the next 'colorcolumn' */
5608 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005609 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005611 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 * highlight the cursor position itself.
5613 * Also highlight the 'colorcolumn' if it is different than
5614 * 'cursorcolumn' */
5615 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005616 if (draw_state == WL_LINE && !lnum_in_visual_area
5617 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005618 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005619 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005620 && lnum != wp->w_cursor.lnum)
5621 {
5622 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005623 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005624 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005625 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005626 {
5627 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005628 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005629 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005630 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631#endif
5632
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 /*
5634 * Store character to be displayed.
5635 * Skip characters that are left of the screen for 'nowrap'.
5636 */
5637 vcol_prev = vcol;
5638 if (draw_state < WL_LINE || n_skip <= 0)
5639 {
5640 /*
5641 * Store the character.
5642 */
5643#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5644 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5645 {
5646 /* A double-wide character is: put first halve in left cell. */
5647 --off;
5648 --col;
5649 }
5650#endif
5651 ScreenLines[off] = c;
5652#ifdef FEAT_MBYTE
5653 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005654 {
5655 if ((mb_c & 0xff00) == 0x8e00)
5656 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 else if (enc_utf8)
5660 {
5661 if (mb_utf8)
5662 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005663 int i;
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005666 if ((c & 0xff) == 0)
5667 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005668 for (i = 0; i < Screen_mco; ++i)
5669 {
5670 ScreenLinesC[i][off] = u8cc[i];
5671 if (u8cc[i] == 0)
5672 break;
5673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else
5676 ScreenLinesUC[off] = 0;
5677 }
5678 if (multi_attr)
5679 {
5680 ScreenAttrs[off] = multi_attr;
5681 multi_attr = 0;
5682 }
5683 else
5684#endif
5685 ScreenAttrs[off] = char_attr;
5686
5687#ifdef FEAT_MBYTE
5688 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5689 {
5690 /* Need to fill two screen columns. */
5691 ++off;
5692 ++col;
5693 if (enc_utf8)
5694 /* UTF-8: Put a 0 in the second screen char. */
5695 ScreenLines[off] = 0;
5696 else
5697 /* DBCS: Put second byte in the second screen char. */
5698 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005699 if (draw_state > WL_NR
5700#ifdef FEAT_DIFF
5701 && filler_todo <= 0
5702#endif
5703 )
5704 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 /* When "tocol" is halfway a character, set it to the end of
5706 * the character, otherwise highlighting won't stop. */
5707 if (tocol == vcol)
5708 ++tocol;
5709#ifdef FEAT_RIGHTLEFT
5710 if (wp->w_p_rl)
5711 {
5712 /* now it's time to backup one cell */
5713 --off;
5714 --col;
5715 }
5716#endif
5717 }
5718#endif
5719#ifdef FEAT_RIGHTLEFT
5720 if (wp->w_p_rl)
5721 {
5722 --off;
5723 --col;
5724 }
5725 else
5726#endif
5727 {
5728 ++off;
5729 ++col;
5730 }
5731 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005732#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005733 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005734 {
5735 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005736 ++vcol_off;
5737 if (n_extra > 0)
5738 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005739 if (wp->w_p_wrap)
5740 {
5741 /*
5742 * Special voodoo required if 'wrap' is on.
5743 *
5744 * Advance the column indicator to force the line
5745 * drawing to wrap early. This will make the line
5746 * take up the same screen space when parts are concealed,
5747 * so that cursor line computations aren't messed up.
5748 *
5749 * To avoid the fictitious advance of 'col' causing
5750 * trailing junk to be written out of the screen line
5751 * we are building, 'boguscols' keeps track of the number
5752 * of bad columns we have advanced.
5753 */
5754 if (n_extra > 0)
5755 {
5756 vcol += n_extra;
5757# ifdef FEAT_RIGHTLEFT
5758 if (wp->w_p_rl)
5759 {
5760 col -= n_extra;
5761 boguscols -= n_extra;
5762 }
5763 else
5764# endif
5765 {
5766 col += n_extra;
5767 boguscols += n_extra;
5768 }
5769 n_extra = 0;
5770 n_attr = 0;
5771 }
5772
5773
5774# ifdef FEAT_MBYTE
5775 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5776 {
5777 /* Need to fill two screen columns. */
5778# ifdef FEAT_RIGHTLEFT
5779 if (wp->w_p_rl)
5780 {
5781 --boguscols;
5782 --col;
5783 }
5784 else
5785# endif
5786 {
5787 ++boguscols;
5788 ++col;
5789 }
5790 }
5791# endif
5792
5793# ifdef FEAT_RIGHTLEFT
5794 if (wp->w_p_rl)
5795 {
5796 --boguscols;
5797 --col;
5798 }
5799 else
5800# endif
5801 {
5802 ++boguscols;
5803 ++col;
5804 }
5805 }
5806 else
5807 {
5808 if (n_extra > 0)
5809 {
5810 vcol += n_extra;
5811 n_extra = 0;
5812 n_attr = 0;
5813 }
5814 }
5815
5816 }
5817#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 else
5819 --n_skip;
5820
Bram Moolenaar64486672010-05-16 15:46:46 +02005821 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5822 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005823 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#ifdef FEAT_DIFF
5825 && filler_todo <= 0
5826#endif
5827 )
5828 ++vcol;
5829
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005830#ifdef FEAT_SYN_HL
5831 if (vcol_save_attr >= 0)
5832 char_attr = vcol_save_attr;
5833#endif
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 /* restore attributes after "predeces" in 'listchars' */
5836 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5837 char_attr = saved_attr3;
5838
5839 /* restore attributes after last 'listchars' or 'number' char */
5840 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5841 char_attr = saved_attr2;
5842
5843 /*
5844 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005845 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 */
5847 if ((
5848#ifdef FEAT_RIGHTLEFT
5849 wp->w_p_rl ? (col < 0) :
5850#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005851 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 && (*ptr != NUL
5853#ifdef FEAT_DIFF
5854 || filler_todo > 0
5855#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005856 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5858 )
5859 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005860#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005861 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005862 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005863 boguscols = 0;
5864#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005865 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005866 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 ++row;
5869 ++screen_row;
5870
5871 /* When not wrapping and finished diff lines, or when displayed
5872 * '$' and highlighting until last column, break here. */
5873 if ((!wp->w_p_wrap
5874#ifdef FEAT_DIFF
5875 && filler_todo <= 0
5876#endif
5877 ) || lcs_eol_one == -1)
5878 break;
5879
5880 /* When the window is too narrow draw all "@" lines. */
5881 if (draw_state != WL_LINE
5882#ifdef FEAT_DIFF
5883 && filler_todo <= 0
5884#endif
5885 )
5886 {
5887 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 row = endrow;
5890 }
5891
5892 /* When line got too long for screen break here. */
5893 if (row == endrow)
5894 {
5895 ++row;
5896 break;
5897 }
5898
5899 if (screen_cur_row == screen_row - 1
5900#ifdef FEAT_DIFF
5901 && filler_todo <= 0
5902#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005903 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
5905 /* Remember that the line wraps, used for modeless copy. */
5906 LineWraps[screen_row - 1] = TRUE;
5907
5908 /*
5909 * Special trick to make copy/paste of wrapped lines work with
5910 * xterm/screen: write an extra character beyond the end of
5911 * the line. This will work with all terminal types
5912 * (regardless of the xn,am settings).
5913 * Only do this on a fast tty.
5914 * Only do this if the cursor is on the current line
5915 * (something has been written in it).
5916 * Don't do this for the GUI.
5917 * Don't do this for double-width characters.
5918 * Don't do this for a window not at the right screen border.
5919 */
5920 if (p_tf
5921#ifdef FEAT_GUI
5922 && !gui.in_use
5923#endif
5924#ifdef FEAT_MBYTE
5925 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005926 && ((*mb_off2cells)(LineOffset[screen_row],
5927 LineOffset[screen_row] + screen_Columns)
5928 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005930 + (int)Columns - 2,
5931 LineOffset[screen_row] + screen_Columns)
5932 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933#endif
5934 )
5935 {
5936 /* First make sure we are at the end of the screen line,
5937 * then output the same character again to let the
5938 * terminal know about the wrap. If the terminal doesn't
5939 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005940 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 screen_char(LineOffset[screen_row - 1]
5942 + (unsigned)Columns - 1,
5943 screen_row - 1, (int)(Columns - 1));
5944
5945#ifdef FEAT_MBYTE
5946 /* When there is a multi-byte character, just output a
5947 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005948 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5949 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 out_char(' ');
5951 else
5952#endif
5953 out_char(ScreenLines[LineOffset[screen_row - 1]
5954 + (Columns - 1)]);
5955 /* force a redraw of the first char on the next line */
5956 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5957 screen_start(); /* don't know where cursor is now */
5958 }
5959 }
5960
5961 col = 0;
5962 off = (unsigned)(current_ScreenLine - ScreenLines);
5963#ifdef FEAT_RIGHTLEFT
5964 if (wp->w_p_rl)
5965 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005966 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 off += col;
5968 }
5969#endif
5970
5971 /* reset the drawing state for the start of a wrapped line */
5972 draw_state = WL_START;
5973 saved_n_extra = n_extra;
5974 saved_p_extra = p_extra;
5975 saved_c_extra = c_extra;
5976 saved_char_attr = char_attr;
5977 n_extra = 0;
5978 lcs_prec_todo = lcs_prec;
5979#ifdef FEAT_LINEBREAK
5980# ifdef FEAT_DIFF
5981 if (filler_todo <= 0)
5982# endif
5983 need_showbreak = TRUE;
5984#endif
5985#ifdef FEAT_DIFF
5986 --filler_todo;
5987 /* When the filler lines are actually below the last line of the
5988 * file, don't draw the line itself, break here. */
5989 if (filler_todo == 0 && wp->w_botfill)
5990 break;
5991#endif
5992 }
5993
5994 } /* for every character in the line */
5995
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005996#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005997 /* After an empty line check first word for capital. */
5998 if (*skipwhite(line) == NUL)
5999 {
6000 capcol_lnum = lnum + 1;
6001 cap_col = 0;
6002 }
6003#endif
6004
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006005 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 return row;
6007}
6008
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006009#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006010static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006011
6012/*
6013 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006014 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006015 */
6016 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006017comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006018{
6019 int i;
6020
6021 for (i = 0; i < Screen_mco; ++i)
6022 {
6023 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6024 return TRUE;
6025 if (ScreenLinesC[i][off_from] == 0)
6026 break;
6027 }
6028 return FALSE;
6029}
6030#endif
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032/*
6033 * Check whether the given character needs redrawing:
6034 * - the (first byte of the) character is different
6035 * - the attributes are different
6036 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006037 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 */
6039 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006040char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 if (cols > 0
6043 && ((ScreenLines[off_from] != ScreenLines[off_to]
6044 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6045
6046#ifdef FEAT_MBYTE
6047 || (enc_dbcs != 0
6048 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6049 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6050 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6051 : (cols > 1 && ScreenLines[off_from + 1]
6052 != ScreenLines[off_to + 1])))
6053 || (enc_utf8
6054 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6055 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006056 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006057 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6058 && ScreenLines[off_from + 1]
6059 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060#endif
6061 ))
6062 return TRUE;
6063 return FALSE;
6064}
6065
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006066#if defined(FEAT_TERMINAL) || defined(PROTO)
6067/*
6068 * Return the index in ScreenLines[] for the current screen line.
6069 */
6070 int
6071screen_get_current_line_off()
6072{
6073 return (int)(current_ScreenLine - ScreenLines);
6074}
6075#endif
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077/*
6078 * Move one "cooked" screen line to the screen, but only the characters that
6079 * have actually changed. Handle insert/delete character.
6080 * "coloff" gives the first column on the screen for this line.
6081 * "endcol" gives the columns where valid characters are.
6082 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6083 * needs to be cleared, negative otherwise.
6084 * "rlflag" is TRUE in a rightleft window:
6085 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6086 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6087 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006088 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006089screen_line(
6090 int row,
6091 int coloff,
6092 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006093 int clear_width,
6094 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 unsigned off_from;
6097 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006098#ifdef FEAT_MBYTE
6099 unsigned max_off_from;
6100 unsigned max_off_to;
6101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 int force = FALSE; /* force update rest of the line */
6105 int redraw_this /* bool: does character need redraw? */
6106#ifdef FEAT_GUI
6107 = TRUE /* For GUI when while-loop empty */
6108#endif
6109 ;
6110 int redraw_next; /* redraw_this for next character */
6111#ifdef FEAT_MBYTE
6112 int clear_next = FALSE;
6113 int char_cells; /* 1: normal char */
6114 /* 2: occupies two display cells */
6115# define CHAR_CELLS char_cells
6116#else
6117# define CHAR_CELLS 1
6118#endif
6119
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006120 /* Check for illegal row and col, just in case. */
6121 if (row >= Rows)
6122 row = Rows - 1;
6123 if (endcol > Columns)
6124 endcol = Columns;
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126# ifdef FEAT_CLIPBOARD
6127 clip_may_clear_selection(row, row);
6128# endif
6129
6130 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6131 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006132#ifdef FEAT_MBYTE
6133 max_off_from = off_from + screen_Columns;
6134 max_off_to = LineOffset[row] + screen_Columns;
6135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137#ifdef FEAT_RIGHTLEFT
6138 if (rlflag)
6139 {
6140 /* Clear rest first, because it's left of the text. */
6141 if (clear_width > 0)
6142 {
6143 while (col <= endcol && ScreenLines[off_to] == ' '
6144 && ScreenAttrs[off_to] == 0
6145# ifdef FEAT_MBYTE
6146 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6147# endif
6148 )
6149 {
6150 ++off_to;
6151 ++col;
6152 }
6153 if (col <= endcol)
6154 screen_fill(row, row + 1, col + coloff,
6155 endcol + coloff + 1, ' ', ' ', 0);
6156 }
6157 col = endcol + 1;
6158 off_to = LineOffset[row] + col + coloff;
6159 off_from += col;
6160 endcol = (clear_width > 0 ? clear_width : -clear_width);
6161 }
6162#endif /* FEAT_RIGHTLEFT */
6163
6164 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6165
6166 while (col < endcol)
6167 {
6168#ifdef FEAT_MBYTE
6169 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006170 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 else
6172 char_cells = 1;
6173#endif
6174
6175 redraw_this = redraw_next;
6176 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6177 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6178
6179#ifdef FEAT_GUI
6180 /* If the next character was bold, then redraw the current character to
6181 * remove any pixels that might have spilt over into us. This only
6182 * happens in the GUI.
6183 */
6184 if (redraw_next && gui.in_use)
6185 {
6186 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006187 if (hl > HL_ALL)
6188 hl = syn_attr2attr(hl);
6189 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 redraw_this = TRUE;
6191 }
6192#endif
6193
6194 if (redraw_this)
6195 {
6196 /*
6197 * Special handling when 'xs' termcap flag set (hpterm):
6198 * Attributes for characters are stored at the position where the
6199 * cursor is when writing the highlighting code. The
6200 * start-highlighting code must be written with the cursor on the
6201 * first highlighted character. The stop-highlighting code must
6202 * be written with the cursor just after the last highlighted
6203 * character.
6204 * Overwriting a character doesn't remove it's highlighting. Need
6205 * to clear the rest of the line, and force redrawing it
6206 * completely.
6207 */
6208 if ( p_wiv
6209 && !force
6210#ifdef FEAT_GUI
6211 && !gui.in_use
6212#endif
6213 && ScreenAttrs[off_to] != 0
6214 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6215 {
6216 /*
6217 * Need to remove highlighting attributes here.
6218 */
6219 windgoto(row, col + coloff);
6220 out_str(T_CE); /* clear rest of this screen line */
6221 screen_start(); /* don't know where cursor is now */
6222 force = TRUE; /* force redraw of rest of the line */
6223 redraw_next = TRUE; /* or else next char would miss out */
6224
6225 /*
6226 * If the previous character was highlighted, need to stop
6227 * highlighting at this character.
6228 */
6229 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6230 {
6231 screen_attr = ScreenAttrs[off_to - 1];
6232 term_windgoto(row, col + coloff);
6233 screen_stop_highlight();
6234 }
6235 else
6236 screen_attr = 0; /* highlighting has stopped */
6237 }
6238#ifdef FEAT_MBYTE
6239 if (enc_dbcs != 0)
6240 {
6241 /* Check if overwriting a double-byte with a single-byte or
6242 * the other way around requires another character to be
6243 * redrawn. For UTF-8 this isn't needed, because comparing
6244 * ScreenLinesUC[] is sufficient. */
6245 if (char_cells == 1
6246 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006247 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
6249 /* Writing a single-cell character over a double-cell
6250 * character: need to redraw the next cell. */
6251 ScreenLines[off_to + 1] = 0;
6252 redraw_next = TRUE;
6253 }
6254 else if (char_cells == 2
6255 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006256 && (*mb_off2cells)(off_to, max_off_to) == 1
6257 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
6259 /* Writing the second half of a double-cell character over
6260 * a double-cell character: need to redraw the second
6261 * cell. */
6262 ScreenLines[off_to + 2] = 0;
6263 redraw_next = TRUE;
6264 }
6265
6266 if (enc_dbcs == DBCS_JPNU)
6267 ScreenLines2[off_to] = ScreenLines2[off_from];
6268 }
6269 /* When writing a single-width character over a double-width
6270 * character and at the end of the redrawn text, need to clear out
6271 * the right halve of the old character.
6272 * Also required when writing the right halve of a double-width
6273 * char over the left halve of an existing one. */
6274 if (has_mbyte && col + char_cells == endcol
6275 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006276 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006278 && (*mb_off2cells)(off_to, max_off_to) == 1
6279 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 clear_next = TRUE;
6281#endif
6282
6283 ScreenLines[off_to] = ScreenLines[off_from];
6284#ifdef FEAT_MBYTE
6285 if (enc_utf8)
6286 {
6287 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6288 if (ScreenLinesUC[off_from] != 0)
6289 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006290 int i;
6291
6292 for (i = 0; i < Screen_mco; ++i)
6293 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 }
6295 }
6296 if (char_cells == 2)
6297 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6298#endif
6299
6300#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006301 /* The bold trick makes a single column of pixels appear in the
6302 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 * character should be redrawn too. This happens for our own GUI
6304 * and for some xterms. */
6305 if (
6306# ifdef FEAT_GUI
6307 gui.in_use
6308# endif
6309# if defined(FEAT_GUI) && defined(UNIX)
6310 ||
6311# endif
6312# ifdef UNIX
6313 term_is_xterm
6314# endif
6315 )
6316 {
6317 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006318 if (hl > HL_ALL)
6319 hl = syn_attr2attr(hl);
6320 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 redraw_next = TRUE;
6322 }
6323#endif
6324 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6325#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006326 /* For simplicity set the attributes of second half of a
6327 * double-wide character equal to the first half. */
6328 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006330
6331 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 else
6334#endif
6335 screen_char(off_to, row, col + coloff);
6336 }
6337 else if ( p_wiv
6338#ifdef FEAT_GUI
6339 && !gui.in_use
6340#endif
6341 && col + coloff > 0)
6342 {
6343 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6344 {
6345 /*
6346 * Don't output stop-highlight when moving the cursor, it will
6347 * stop the highlighting when it should continue.
6348 */
6349 screen_attr = 0;
6350 }
6351 else if (screen_attr != 0)
6352 screen_stop_highlight();
6353 }
6354
6355 off_to += CHAR_CELLS;
6356 off_from += CHAR_CELLS;
6357 col += CHAR_CELLS;
6358 }
6359
6360#ifdef FEAT_MBYTE
6361 if (clear_next)
6362 {
6363 /* Clear the second half of a double-wide character of which the left
6364 * half was overwritten with a single-wide character. */
6365 ScreenLines[off_to] = ' ';
6366 if (enc_utf8)
6367 ScreenLinesUC[off_to] = 0;
6368 screen_char(off_to, row, col + coloff);
6369 }
6370#endif
6371
6372 if (clear_width > 0
6373#ifdef FEAT_RIGHTLEFT
6374 && !rlflag
6375#endif
6376 )
6377 {
6378#ifdef FEAT_GUI
6379 int startCol = col;
6380#endif
6381
6382 /* blank out the rest of the line */
6383 while (col < clear_width && ScreenLines[off_to] == ' '
6384 && ScreenAttrs[off_to] == 0
6385#ifdef FEAT_MBYTE
6386 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6387#endif
6388 )
6389 {
6390 ++off_to;
6391 ++col;
6392 }
6393 if (col < clear_width)
6394 {
6395#ifdef FEAT_GUI
6396 /*
6397 * In the GUI, clearing the rest of the line may leave pixels
6398 * behind if the first character cleared was bold. Some bold
6399 * fonts spill over the left. In this case we redraw the previous
6400 * character too. If we didn't skip any blanks above, then we
6401 * only redraw if the character wasn't already redrawn anyway.
6402 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006403 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
6405 hl = ScreenAttrs[off_to];
6406 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006407 {
6408 int prev_cells = 1;
6409# ifdef FEAT_MBYTE
6410 if (enc_utf8)
6411 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6412 * that its width is 2. */
6413 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6414 else if (enc_dbcs != 0)
6415 {
6416 /* find previous character by counting from first
6417 * column and get its width. */
6418 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006419 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006420
6421 while (off < off_to)
6422 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006423 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006424 off += prev_cells;
6425 }
6426 }
6427
6428 if (enc_dbcs != 0 && prev_cells > 1)
6429 screen_char_2(off_to - prev_cells, row,
6430 col + coloff - prev_cells);
6431 else
6432# endif
6433 screen_char(off_to - prev_cells, row,
6434 col + coloff - prev_cells);
6435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437#endif
6438 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6439 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 off_to += clear_width - col;
6441 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 }
6443 }
6444
6445 if (clear_width > 0)
6446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 /* For a window that's left of another, draw the separator char. */
6448 if (col + coloff < Columns)
6449 {
6450 int c;
6451
6452 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006453 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006454#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006455 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6456 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 || ScreenAttrs[off_to] != hl)
6459 {
6460 ScreenLines[off_to] = c;
6461 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006462#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (enc_utf8)
6464 {
6465 if (c >= 0x80)
6466 {
6467 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006468 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 }
6470 else
6471 ScreenLinesUC[off_to] = 0;
6472 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 screen_char(off_to, row, col + coloff);
6475 }
6476 }
6477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 LineWraps[row] = FALSE;
6479 }
6480}
6481
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006482#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006484 * Mirror text "str" for right-left displaying.
6485 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006487 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006488rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 char_u *p1, *p2;
6491 int t;
6492
6493 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6494 {
6495 t = *p1;
6496 *p1 = *p2;
6497 *p2 = t;
6498 }
6499}
6500#endif
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502/*
6503 * mark all status lines for redraw; used after first :cd
6504 */
6505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006506status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 win_T *wp;
6509
Bram Moolenaar29323592016-07-24 22:04:11 +02006510 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (wp->w_status_height)
6512 {
6513 wp->w_redr_status = TRUE;
6514 redraw_later(VALID);
6515 }
6516}
6517
6518/*
6519 * mark all status lines of the current buffer for redraw
6520 */
6521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006522status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 win_T *wp;
6525
Bram Moolenaar29323592016-07-24 22:04:11 +02006526 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6528 {
6529 wp->w_redr_status = TRUE;
6530 redraw_later(VALID);
6531 }
6532}
6533
6534/*
6535 * Redraw all status lines that need to be redrawn.
6536 */
6537 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006538redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 win_T *wp;
6541
Bram Moolenaar29323592016-07-24 22:04:11 +02006542 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006544 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006545 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006546 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
Bram Moolenaar4033c552017-09-16 20:54:51 +02006549#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550/*
6551 * Redraw all status lines at the bottom of frame "frp".
6552 */
6553 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006554win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 if (frp->fr_layout == FR_LEAF)
6557 frp->fr_win->w_redr_status = TRUE;
6558 else if (frp->fr_layout == FR_ROW)
6559 {
6560 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6561 win_redraw_last_status(frp);
6562 }
6563 else /* frp->fr_layout == FR_COL */
6564 {
6565 frp = frp->fr_child;
6566 while (frp->fr_next != NULL)
6567 frp = frp->fr_next;
6568 win_redraw_last_status(frp);
6569 }
6570}
6571#endif
6572
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573/*
6574 * Draw the verticap separator right of window "wp" starting with line "row".
6575 */
6576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006577draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 int hl;
6580 int c;
6581
6582 if (wp->w_vsep_width)
6583 {
6584 /* draw the vertical separator right of this window */
6585 c = fillchar_vsep(&hl);
6586 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6587 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6588 c, ' ', hl);
6589 }
6590}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591
6592#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006593static int status_match_len(expand_T *xp, char_u *s);
6594static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006597 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 */
6599 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006600status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 int len = 0;
6603
6604#ifdef FEAT_MENU
6605 int emenu = (xp->xp_context == EXPAND_MENUS
6606 || xp->xp_context == EXPAND_MENUNAMES);
6607
6608 /* Check for menu separators - replace with '|'. */
6609 if (emenu && menu_is_separator(s))
6610 return 1;
6611#endif
6612
6613 while (*s != NUL)
6614 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006615 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006616 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006617 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 }
6619
6620 return len;
6621}
6622
6623/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006624 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006625 * These are backslashes used for escaping. Do show backslashes in help tags.
6626 */
6627 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006628skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006629{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006630 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006631#ifdef FEAT_MENU
6632 || ((xp->xp_context == EXPAND_MENUS
6633 || xp->xp_context == EXPAND_MENUNAMES)
6634 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6635#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006636 )
6637 {
6638#ifndef BACKSLASH_IN_FILENAME
6639 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6640 return 2;
6641#endif
6642 return 1;
6643 }
6644 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006645}
6646
6647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 * Show wildchar matches in the status line.
6649 * Show at least the "match" item.
6650 * We start at item 'first_match' in the list and show all matches that fit.
6651 *
6652 * If inversion is possible we use it. Else '=' characters are used.
6653 */
6654 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006655win_redr_status_matches(
6656 expand_T *xp,
6657 int num_matches,
6658 char_u **matches, /* list of matches */
6659 int match,
6660 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6663 int row;
6664 char_u *buf;
6665 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006666 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 int fillchar;
6668 int attr;
6669 int i;
6670 int highlight = TRUE;
6671 char_u *selstart = NULL;
6672 int selstart_col = 0;
6673 char_u *selend = NULL;
6674 static int first_match = 0;
6675 int add_left = FALSE;
6676 char_u *s;
6677#ifdef FEAT_MENU
6678 int emenu;
6679#endif
6680#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6681 int l;
6682#endif
6683
6684 if (matches == NULL) /* interrupted completion? */
6685 return;
6686
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006687#ifdef FEAT_MBYTE
6688 if (has_mbyte)
6689 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6690 else
6691#endif
6692 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (buf == NULL)
6694 return;
6695
6696 if (match == -1) /* don't show match but original text */
6697 {
6698 match = 0;
6699 highlight = FALSE;
6700 }
6701 /* count 1 for the ending ">" */
6702 clen = status_match_len(xp, L_MATCH(match)) + 3;
6703 if (match == 0)
6704 first_match = 0;
6705 else if (match < first_match)
6706 {
6707 /* jumping left, as far as we can go */
6708 first_match = match;
6709 add_left = TRUE;
6710 }
6711 else
6712 {
6713 /* check if match fits on the screen */
6714 for (i = first_match; i < match; ++i)
6715 clen += status_match_len(xp, L_MATCH(i)) + 2;
6716 if (first_match > 0)
6717 clen += 2;
6718 /* jumping right, put match at the left */
6719 if ((long)clen > Columns)
6720 {
6721 first_match = match;
6722 /* if showing the last match, we can add some on the left */
6723 clen = 2;
6724 for (i = match; i < num_matches; ++i)
6725 {
6726 clen += status_match_len(xp, L_MATCH(i)) + 2;
6727 if ((long)clen >= Columns)
6728 break;
6729 }
6730 if (i == num_matches)
6731 add_left = TRUE;
6732 }
6733 }
6734 if (add_left)
6735 while (first_match > 0)
6736 {
6737 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6738 if ((long)clen >= Columns)
6739 break;
6740 --first_match;
6741 }
6742
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006743 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
6745 if (first_match == 0)
6746 {
6747 *buf = NUL;
6748 len = 0;
6749 }
6750 else
6751 {
6752 STRCPY(buf, "< ");
6753 len = 2;
6754 }
6755 clen = len;
6756
6757 i = first_match;
6758 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6759 {
6760 if (i == match)
6761 {
6762 selstart = buf + len;
6763 selstart_col = clen;
6764 }
6765
6766 s = L_MATCH(i);
6767 /* Check for menu separators - replace with '|' */
6768#ifdef FEAT_MENU
6769 emenu = (xp->xp_context == EXPAND_MENUS
6770 || xp->xp_context == EXPAND_MENUNAMES);
6771 if (emenu && menu_is_separator(s))
6772 {
6773 STRCPY(buf + len, transchar('|'));
6774 l = (int)STRLEN(buf + len);
6775 len += l;
6776 clen += l;
6777 }
6778 else
6779#endif
6780 for ( ; *s != NUL; ++s)
6781 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006782 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 clen += ptr2cells(s);
6784#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006785 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
6787 STRNCPY(buf + len, s, l);
6788 s += l - 1;
6789 len += l;
6790 }
6791 else
6792#endif
6793 {
6794 STRCPY(buf + len, transchar_byte(*s));
6795 len += (int)STRLEN(buf + len);
6796 }
6797 }
6798 if (i == match)
6799 selend = buf + len;
6800
6801 *(buf + len++) = ' ';
6802 *(buf + len++) = ' ';
6803 clen += 2;
6804 if (++i == num_matches)
6805 break;
6806 }
6807
6808 if (i != num_matches)
6809 {
6810 *(buf + len++) = '>';
6811 ++clen;
6812 }
6813
6814 buf[len] = NUL;
6815
6816 row = cmdline_row - 1;
6817 if (row >= 0)
6818 {
6819 if (wild_menu_showing == 0)
6820 {
6821 if (msg_scrolled > 0)
6822 {
6823 /* Put the wildmenu just above the command line. If there is
6824 * no room, scroll the screen one line up. */
6825 if (cmdline_row == Rows - 1)
6826 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006827 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 ++msg_scrolled;
6829 }
6830 else
6831 {
6832 ++cmdline_row;
6833 ++row;
6834 }
6835 wild_menu_showing = WM_SCROLLED;
6836 }
6837 else
6838 {
6839 /* Create status line if needed by setting 'laststatus' to 2.
6840 * Set 'winminheight' to zero to avoid that the window is
6841 * resized. */
6842 if (lastwin->w_status_height == 0)
6843 {
6844 save_p_ls = p_ls;
6845 save_p_wmh = p_wmh;
6846 p_ls = 2;
6847 p_wmh = 0;
6848 last_status(FALSE);
6849 }
6850 wild_menu_showing = WM_SHOWN;
6851 }
6852 }
6853
6854 screen_puts(buf, row, 0, attr);
6855 if (selstart != NULL && highlight)
6856 {
6857 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006858 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 }
6860
6861 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6862 }
6863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 vim_free(buf);
6866}
6867#endif
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869/*
6870 * Redraw the status line of window wp.
6871 *
6872 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006873 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6874 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006876 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006877win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878{
6879 int row;
6880 char_u *p;
6881 int len;
6882 int fillchar;
6883 int attr;
6884 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006885 static int busy = FALSE;
6886
6887 /* It's possible to get here recursively when 'statusline' (indirectly)
6888 * invokes ":redrawstatus". Simply ignore the call then. */
6889 if (busy)
6890 return;
6891 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892
6893 wp->w_redr_status = FALSE;
6894 if (wp->w_status_height == 0)
6895 {
6896 /* no status line, can only be last window */
6897 redraw_cmdline = TRUE;
6898 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006899 else if (!redrawing()
6900#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006901 // don't update status line when popup menu is visible and may be
6902 // drawn over it, unless it will be redrawn later
6903 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006904#endif
6905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
6907 /* Don't redraw right now, do it later. */
6908 wp->w_redr_status = TRUE;
6909 }
6910#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006911 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
6913 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006914 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916#endif
6917 else
6918 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006919 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006921 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 p = NameBuff;
6923 len = (int)STRLEN(p);
6924
Bram Moolenaard85f2712017-07-28 21:51:57 +02006925 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926#ifdef FEAT_QUICKFIX
6927 || wp->w_p_pvw
6928#endif
6929 || bufIsChanged(wp->w_buffer)
6930 || wp->w_buffer->b_p_ro)
6931 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006932 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006934 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 len += (int)STRLEN(p + len);
6936 }
6937#ifdef FEAT_QUICKFIX
6938 if (wp->w_p_pvw)
6939 {
6940 STRCPY(p + len, _("[Preview]"));
6941 len += (int)STRLEN(p + len);
6942 }
6943#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006944 if (bufIsChanged(wp->w_buffer)
6945#ifdef FEAT_TERMINAL
6946 && !bt_terminal(wp->w_buffer)
6947#endif
6948 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
6950 STRCPY(p + len, "[+]");
6951 len += 3;
6952 }
6953 if (wp->w_buffer->b_p_ro)
6954 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006955 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006956 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958
Bram Moolenaar02631462017-09-22 15:20:32 +02006959 this_ru_col = ru_col - (Columns - wp->w_width);
6960 if (this_ru_col < (wp->w_width + 1) / 2)
6961 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (this_ru_col <= 1)
6963 {
6964 p = (char_u *)"<"; /* No room for file name! */
6965 len = 1;
6966 }
6967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968#ifdef FEAT_MBYTE
6969 if (has_mbyte)
6970 {
6971 int clen = 0, i;
6972
6973 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006974 clen = mb_string2cells(p, -1);
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 /* Find first character that will fit.
6977 * Going from start to end is much faster for DBCS. */
6978 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006979 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 clen -= (*mb_ptr2cells)(p + i);
6981 len = clen;
6982 if (i > 0)
6983 {
6984 p = p + i - 1;
6985 *p = '<';
6986 ++len;
6987 }
6988
6989 }
6990 else
6991#endif
6992 if (len > this_ru_col - 1)
6993 {
6994 p += len - (this_ru_col - 1);
6995 *p = '<';
6996 len = this_ru_col - 1;
6997 }
6998
6999 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007000 screen_puts(p, row, wp->w_wincol, attr);
7001 screen_fill(row, row + 1, len + wp->w_wincol,
7002 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007004 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7006 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007007 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007010 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011#endif
7012 }
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 /*
7015 * May need to draw the character below the vertical separator.
7016 */
7017 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7018 {
7019 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007020 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
7022 fillchar = fillchar_vsep(&attr);
7023 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7024 attr);
7025 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007026 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027}
7028
Bram Moolenaar238a5642006-02-21 22:12:05 +00007029#ifdef FEAT_STL_OPT
7030/*
7031 * Redraw the status line according to 'statusline' and take care of any
7032 * errors encountered.
7033 */
7034 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007035redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007036{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007037 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007038 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007039
7040 /* When called recursively return. This can happen when the statusline
7041 * contains an expression that triggers a redraw. */
7042 if (entered)
7043 return;
7044 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007045
Bram Moolenaara742e082016-04-05 21:10:38 +02007046 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007047 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007048 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007049 {
7050 /* When there is an error disable the statusline, otherwise the
7051 * display is messed up with errors and a redraw triggers the problem
7052 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007053 set_string_option_direct((char_u *)"statusline", -1,
7054 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007055 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007056 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007057 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007058 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007059}
7060#endif
7061
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062/*
7063 * Return TRUE if the status line of window "wp" is connected to the status
7064 * line of the window right of it. If not, then it's a vertical separator.
7065 * Only call if (wp->w_vsep_width != 0).
7066 */
7067 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007068stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
7070 frame_T *fr;
7071
7072 fr = wp->w_frame;
7073 while (fr->fr_parent != NULL)
7074 {
7075 if (fr->fr_parent->fr_layout == FR_COL)
7076 {
7077 if (fr->fr_next != NULL)
7078 break;
7079 }
7080 else
7081 {
7082 if (fr->fr_next != NULL)
7083 return TRUE;
7084 }
7085 fr = fr->fr_parent;
7086 }
7087 return FALSE;
7088}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091/*
7092 * Get the value to show for the language mappings, active 'keymap'.
7093 */
7094 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007095get_keymap_str(
7096 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007097 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007098 char_u *buf, /* buffer for the result */
7099 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 char_u *p;
7102
7103 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7104 return FALSE;
7105
7106 {
7107#ifdef FEAT_EVAL
7108 buf_T *old_curbuf = curbuf;
7109 win_T *old_curwin = curwin;
7110 char_u *s;
7111
7112 curbuf = wp->w_buffer;
7113 curwin = wp;
7114 STRCPY(buf, "b:keymap_name"); /* must be writable */
7115 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007116 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 --emsg_skip;
7118 curbuf = old_curbuf;
7119 curwin = old_curwin;
7120 if (p == NULL || *p == NUL)
7121#endif
7122 {
7123#ifdef FEAT_KEYMAP
7124 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7125 p = wp->w_buffer->b_p_keymap;
7126 else
7127#endif
7128 p = (char_u *)"lang";
7129 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007130 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 buf[0] = NUL;
7132#ifdef FEAT_EVAL
7133 vim_free(s);
7134#endif
7135 }
7136 return buf[0] != NUL;
7137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139#if defined(FEAT_STL_OPT) || defined(PROTO)
7140/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007141 * Redraw the status line or ruler of window "wp".
7142 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 */
7144 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007145win_redr_custom(
7146 win_T *wp,
7147 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007149 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 int attr;
7151 int curattr;
7152 int row;
7153 int col = 0;
7154 int maxwidth;
7155 int width;
7156 int n;
7157 int len;
7158 int fillchar;
7159 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007162 struct stl_hlrec hltab[STL_MAX_ITEM];
7163 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007164 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007165 win_T *ewp;
7166 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
Bram Moolenaar1d633412013-12-11 15:52:01 +01007168 /* There is a tiny chance that this gets called recursively: When
7169 * redrawing a status line triggers redrawing the ruler or tabline.
7170 * Avoid trouble by not allowing recursion. */
7171 if (entered)
7172 return;
7173 entered = TRUE;
7174
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007176 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007178 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007179 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007180 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007181 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007182 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 maxwidth = Columns;
7184# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007185 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007188 else
7189 {
7190 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007191 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007192 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193
7194 if (draw_ruler)
7195 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007196 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007197 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007198 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007200 if (*++stl == '-')
7201 stl++;
7202 if (atoi((char *)stl))
7203 while (VIM_ISDIGIT(*stl))
7204 stl++;
7205 if (*stl++ != '(')
7206 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007207 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007208 col = ru_col - (Columns - wp->w_width);
7209 if (col < (wp->w_width + 1) / 2)
7210 col = (wp->w_width + 1) / 2;
7211 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007213 {
7214 row = Rows - 1;
7215 --maxwidth; /* writing in last column may cause scrolling */
7216 fillchar = ' ';
7217 attr = 0;
7218 }
7219
7220# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007221 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007222# endif
7223 }
7224 else
7225 {
7226 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007227 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007228 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007229 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007230# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007231 use_sandbox = was_set_insecurely((char_u *)"statusline",
7232 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007233# endif
7234 }
7235
Bram Moolenaar53f81742017-09-22 14:35:51 +02007236 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007237 }
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007240 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar61452852011-02-01 18:01:11 +01007242 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7243 * the cursor away and back. */
7244 ewp = wp == NULL ? curwin : wp;
7245 p_crb_save = ewp->w_p_crb;
7246 ewp->w_p_crb = FALSE;
7247
Bram Moolenaar362f3562009-11-03 16:20:34 +00007248 /* Make a copy, because the statusline may include a function call that
7249 * might change the option value and free the memory. */
7250 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007251 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007252 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007253 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007254 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007255 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007257 /* Make all characters printable. */
7258 p = transstr(buf);
7259 if (p != NULL)
7260 {
7261 vim_strncpy(buf, p, sizeof(buf) - 1);
7262 vim_free(p);
7263 }
7264
7265 /* fill up with "fillchar" */
7266 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007267 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
7269#ifdef FEAT_MBYTE
7270 len += (*mb_char2bytes)(fillchar, buf + len);
7271#else
7272 buf[len++] = fillchar;
7273#endif
7274 ++width;
7275 }
7276 buf[len] = NUL;
7277
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007278 /*
7279 * Draw each snippet with the specified highlighting.
7280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 curattr = attr;
7282 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007283 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007285 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 screen_puts_len(p, len, row, col, curattr);
7287 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007290 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007292 else if (hltab[n].userhl < 0)
7293 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007294#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007295 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7296 && wp->w_status_height != 0)
7297 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007298 else if (wp != NULL && bt_terminal(wp->w_buffer)
7299 && wp->w_status_height != 0)
7300 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007301#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007302 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007303 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 }
7307 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007308
7309 if (wp == NULL)
7310 {
7311 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7312 col = 0;
7313 len = 0;
7314 p = buf;
7315 fillchar = 0;
7316 for (n = 0; tabtab[n].start != NULL; n++)
7317 {
7318 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7319 while (col < len)
7320 TabPageIdxs[col++] = fillchar;
7321 p = tabtab[n].start;
7322 fillchar = tabtab[n].userhl;
7323 }
7324 while (col < Columns)
7325 TabPageIdxs[col++] = fillchar;
7326 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007327
7328theend:
7329 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330}
7331
7332#endif /* FEAT_STL_OPT */
7333
7334/*
7335 * Output a single character directly to the screen and update ScreenLines.
7336 */
7337 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007338screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 char_u buf[MB_MAXBYTES + 1];
7341
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007342#ifdef FEAT_MBYTE
7343 if (has_mbyte)
7344 buf[(*mb_char2bytes)(c, buf)] = NUL;
7345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007347 {
7348 buf[0] = c;
7349 buf[1] = NUL;
7350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 screen_puts(buf, row, col, attr);
7352}
7353
7354/*
7355 * Get a single character directly from ScreenLines into "bytes[]".
7356 * Also return its attribute in *attrp;
7357 */
7358 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007359screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
7361 unsigned off;
7362
7363 /* safety check */
7364 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7365 {
7366 off = LineOffset[row] + col;
7367 *attrp = ScreenAttrs[off];
7368 bytes[0] = ScreenLines[off];
7369 bytes[1] = NUL;
7370
7371#ifdef FEAT_MBYTE
7372 if (enc_utf8 && ScreenLinesUC[off] != 0)
7373 bytes[utfc_char2bytes(off, bytes)] = NUL;
7374 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7375 {
7376 bytes[0] = ScreenLines[off];
7377 bytes[1] = ScreenLines2[off];
7378 bytes[2] = NUL;
7379 }
7380 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7381 {
7382 bytes[1] = ScreenLines[off + 1];
7383 bytes[2] = NUL;
7384 }
7385#endif
7386 }
7387}
7388
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007389#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007390static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007391
7392/*
7393 * Return TRUE if composing characters for screen posn "off" differs from
7394 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007395 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007396 */
7397 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007398screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007399{
7400 int i;
7401
7402 for (i = 0; i < Screen_mco; ++i)
7403 {
7404 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7405 return TRUE;
7406 if (u8cc[i] == 0)
7407 break;
7408 }
7409 return FALSE;
7410}
7411#endif
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413/*
7414 * Put string '*text' on the screen at position 'row' and 'col', with
7415 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7416 * Note: only outputs within one row, message is truncated at screen boundary!
7417 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7418 */
7419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007420screen_puts(
7421 char_u *text,
7422 int row,
7423 int col,
7424 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 screen_puts_len(text, -1, row, col, attr);
7427}
7428
7429/*
7430 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7431 * a NUL.
7432 */
7433 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007434screen_puts_len(
7435 char_u *text,
7436 int textlen,
7437 int row,
7438 int col,
7439 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 unsigned off;
7442 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007443 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 int c;
7445#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007446 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 int mbyte_blen = 1;
7448 int mbyte_cells = 1;
7449 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007450 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 int clear_next_cell = FALSE;
7452# ifdef FEAT_ARABIC
7453 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007454 int pc, nc, nc1;
7455 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456# endif
7457#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007458#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7459 int force_redraw_this;
7460 int force_redraw_next = FALSE;
7461#endif
7462 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
7464 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7465 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007466 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaarc236c162008-07-13 17:41:49 +00007468#ifdef FEAT_MBYTE
7469 /* When drawing over the right halve of a double-wide char clear out the
7470 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007471 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007472# ifdef FEAT_GUI
7473 && !gui.in_use
7474# endif
7475 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007476 {
7477 ScreenLines[off - 1] = ' ';
7478 ScreenAttrs[off - 1] = 0;
7479 if (enc_utf8)
7480 {
7481 ScreenLinesUC[off - 1] = 0;
7482 ScreenLinesC[0][off - 1] = 0;
7483 }
7484 /* redraw the previous cell, make it empty */
7485 screen_char(off - 1, row, col - 1);
7486 /* force the cell at "col" to be redrawn */
7487 force_redraw_next = TRUE;
7488 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007489#endif
7490
Bram Moolenaar367329b2007-08-30 11:53:22 +00007491#ifdef FEAT_MBYTE
7492 max_off = LineOffset[row] + screen_Columns;
7493#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007494 while (col < screen_Columns
7495 && (len < 0 || (int)(ptr - text) < len)
7496 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
7498 c = *ptr;
7499#ifdef FEAT_MBYTE
7500 /* check if this is the first byte of a multibyte */
7501 if (has_mbyte)
7502 {
7503 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007504 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007506 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7508 mbyte_cells = 1;
7509 else if (enc_dbcs != 0)
7510 mbyte_cells = mbyte_blen;
7511 else /* enc_utf8 */
7512 {
7513 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007514 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 (int)((text + len) - ptr));
7516 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007517 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007519# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 /* Non-BMP character: display as ? or fullwidth ?. */
7521 if (u8c >= 0x10000)
7522 {
7523 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7524 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007525 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528# ifdef FEAT_ARABIC
7529 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7530 {
7531 /* Do Arabic shaping. */
7532 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7533 {
7534 /* Past end of string to be displayed. */
7535 nc = NUL;
7536 nc1 = NUL;
7537 }
7538 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007539 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007540 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7541 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007542 nc1 = pcc[0];
7543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 pc = prev_c;
7545 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007546 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
7548 else
7549 prev_c = u8c;
7550# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007551 if (col + mbyte_cells > screen_Columns)
7552 {
7553 /* Only 1 cell left, but character requires 2 cells:
7554 * display a '>' in the last column to avoid wrapping. */
7555 c = '>';
7556 mbyte_cells = 1;
7557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 }
7559 }
7560#endif
7561
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007562#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7563 force_redraw_this = force_redraw_next;
7564 force_redraw_next = FALSE;
7565#endif
7566
7567 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568#ifdef FEAT_MBYTE
7569 || (mbyte_cells == 2
7570 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7571 || (enc_dbcs == DBCS_JPNU
7572 && c == 0x8e
7573 && ScreenLines2[off] != ptr[1])
7574 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007575 && (ScreenLinesUC[off] !=
7576 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7577 || (ScreenLinesUC[off] != 0
7578 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579#endif
7580 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007581 || exmode_active;
7582
7583 if (need_redraw
7584#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7585 || force_redraw_this
7586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 )
7588 {
7589#if defined(FEAT_GUI) || defined(UNIX)
7590 /* The bold trick makes a single row of pixels appear in the next
7591 * character. When a bold character is removed, the next
7592 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007593 * and for some xterms. */
7594 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595# ifdef FEAT_GUI
7596 gui.in_use
7597# endif
7598# if defined(FEAT_GUI) && defined(UNIX)
7599 ||
7600# endif
7601# ifdef UNIX
7602 term_is_xterm
7603# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007604 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007606 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007608 if (n > HL_ALL)
7609 n = syn_attr2attr(n);
7610 if (n & HL_BOLD)
7611 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613#endif
7614#ifdef FEAT_MBYTE
7615 /* When at the end of the text and overwriting a two-cell
7616 * character with a one-cell character, need to clear the next
7617 * cell. Also when overwriting the left halve of a two-cell char
7618 * with the right halve of a two-cell char. Do this only once
7619 * (mb_off2cells() may return 2 on the right halve). */
7620 if (clear_next_cell)
7621 clear_next_cell = FALSE;
7622 else if (has_mbyte
7623 && (len < 0 ? ptr[mbyte_blen] == NUL
7624 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007625 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007627 && (*mb_off2cells)(off, max_off) == 1
7628 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 clear_next_cell = TRUE;
7630
7631 /* Make sure we never leave a second byte of a double-byte behind,
7632 * it confuses mb_off2cells(). */
7633 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007634 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007636 && (*mb_off2cells)(off, max_off) == 1
7637 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 ScreenLines[off + mbyte_blen] = 0;
7639#endif
7640 ScreenLines[off] = c;
7641 ScreenAttrs[off] = attr;
7642#ifdef FEAT_MBYTE
7643 if (enc_utf8)
7644 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007645 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 ScreenLinesUC[off] = 0;
7647 else
7648 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007649 int i;
7650
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007652 for (i = 0; i < Screen_mco; ++i)
7653 {
7654 ScreenLinesC[i][off] = u8cc[i];
7655 if (u8cc[i] == 0)
7656 break;
7657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659 if (mbyte_cells == 2)
7660 {
7661 ScreenLines[off + 1] = 0;
7662 ScreenAttrs[off + 1] = attr;
7663 }
7664 screen_char(off, row, col);
7665 }
7666 else if (mbyte_cells == 2)
7667 {
7668 ScreenLines[off + 1] = ptr[1];
7669 ScreenAttrs[off + 1] = attr;
7670 screen_char_2(off, row, col);
7671 }
7672 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7673 {
7674 ScreenLines2[off] = ptr[1];
7675 screen_char(off, row, col);
7676 }
7677 else
7678#endif
7679 screen_char(off, row, col);
7680 }
7681#ifdef FEAT_MBYTE
7682 if (has_mbyte)
7683 {
7684 off += mbyte_cells;
7685 col += mbyte_cells;
7686 ptr += mbyte_blen;
7687 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007688 {
7689 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007691 len = -1;
7692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694 else
7695#endif
7696 {
7697 ++off;
7698 ++col;
7699 ++ptr;
7700 }
7701 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007702
7703#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7704 /* If we detected the next character needs to be redrawn, but the text
7705 * doesn't extend up to there, update the character here. */
7706 if (force_redraw_next && col < screen_Columns)
7707 {
7708# ifdef FEAT_MBYTE
7709 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7710 screen_char_2(off, row, col);
7711 else
7712# endif
7713 screen_char(off, row, col);
7714 }
7715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
7718#ifdef FEAT_SEARCH_EXTRA
7719/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007720 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 */
7722 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007723start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
7725 if (p_hls && !no_hlsearch)
7726 {
7727 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007728 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007729# ifdef FEAT_RELTIME
7730 /* Set the time limit to 'redrawtime'. */
7731 profile_setlimit(p_rdt, &search_hl.tm);
7732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734}
7735
7736/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007737 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 */
7739 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007740end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
7742 if (search_hl.rm.regprog != NULL)
7743 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007744 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 search_hl.rm.regprog = NULL;
7746 }
7747}
7748
7749/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007750 * Init for calling prepare_search_hl().
7751 */
7752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007753init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007754{
7755 matchitem_T *cur;
7756
7757 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7758 * match */
7759 cur = wp->w_match_head;
7760 while (cur != NULL)
7761 {
7762 cur->hl.rm = cur->match;
7763 if (cur->hlg_id == 0)
7764 cur->hl.attr = 0;
7765 else
7766 cur->hl.attr = syn_id2attr(cur->hlg_id);
7767 cur->hl.buf = wp->w_buffer;
7768 cur->hl.lnum = 0;
7769 cur->hl.first_lnum = 0;
7770# ifdef FEAT_RELTIME
7771 /* Set the time limit to 'redrawtime'. */
7772 profile_setlimit(p_rdt, &(cur->hl.tm));
7773# endif
7774 cur = cur->next;
7775 }
7776 search_hl.buf = wp->w_buffer;
7777 search_hl.lnum = 0;
7778 search_hl.first_lnum = 0;
7779 /* time limit is set at the toplevel, for all windows */
7780}
7781
7782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 * Advance to the match in window "wp" line "lnum" or past it.
7784 */
7785 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007786prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007788 matchitem_T *cur; /* points to the match list */
7789 match_T *shl; /* points to search_hl or a match */
7790 int shl_flag; /* flag to indicate whether search_hl
7791 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007792 int pos_inprogress; /* marks that position match search is
7793 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 int n;
7795
7796 /*
7797 * When using a multi-line pattern, start searching at the top
7798 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007799 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007801 cur = wp->w_match_head;
7802 shl_flag = FALSE;
7803 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007805 if (shl_flag == FALSE)
7806 {
7807 shl = &search_hl;
7808 shl_flag = TRUE;
7809 }
7810 else
7811 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 if (shl->rm.regprog != NULL
7813 && shl->lnum == 0
7814 && re_multiline(shl->rm.regprog))
7815 {
7816 if (shl->first_lnum == 0)
7817 {
7818# ifdef FEAT_FOLDING
7819 for (shl->first_lnum = lnum;
7820 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7821 if (hasFoldingWin(wp, shl->first_lnum - 1,
7822 NULL, NULL, TRUE, NULL))
7823 break;
7824# else
7825 shl->first_lnum = wp->w_topline;
7826# endif
7827 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007828 if (cur != NULL)
7829 cur->pos.cur = 0;
7830 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007832 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7833 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007835 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7836 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007837 pos_inprogress = cur == NULL || cur->pos.cur == 0
7838 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (shl->lnum != 0)
7840 {
7841 shl->first_lnum = shl->lnum
7842 + shl->rm.endpos[0].lnum
7843 - shl->rm.startpos[0].lnum;
7844 n = shl->rm.endpos[0].col;
7845 }
7846 else
7847 {
7848 ++shl->first_lnum;
7849 n = 0;
7850 }
7851 }
7852 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007853 if (shl != &search_hl && cur != NULL)
7854 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 }
7856}
7857
7858/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 * Uses shl->buf.
7861 * Sets shl->lnum and shl->rm contents.
7862 * Note: Assumes a previous match is always before "lnum", unless
7863 * shl->lnum is zero.
7864 * Careful: Any pointers for buffer lines will become invalid.
7865 */
7866 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007867next_search_hl(
7868 win_T *win,
7869 match_T *shl, /* points to search_hl or a match */
7870 linenr_T lnum,
7871 colnr_T mincol, /* minimal column for a match */
7872 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 linenr_T l;
7875 colnr_T matchcol;
7876 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007877 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878
7879 if (shl->lnum != 0)
7880 {
7881 /* Check for three situations:
7882 * 1. If the "lnum" is below a previous match, start a new search.
7883 * 2. If the previous match includes "mincol", use it.
7884 * 3. Continue after the previous match.
7885 */
7886 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7887 if (lnum > l)
7888 shl->lnum = 0;
7889 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7890 return;
7891 }
7892
7893 /*
7894 * Repeat searching for a match until one is found that includes "mincol"
7895 * or none is found in this line.
7896 */
7897 called_emsg = FALSE;
7898 for (;;)
7899 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007900#ifdef FEAT_RELTIME
7901 /* Stop searching after passing the time limit. */
7902 if (profile_passed_limit(&(shl->tm)))
7903 {
7904 shl->lnum = 0; /* no match found in time */
7905 break;
7906 }
7907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 /* Three situations:
7909 * 1. No useful previous match: search from start of line.
7910 * 2. Not Vi compatible or empty match: continue at next character.
7911 * Break the loop if this is beyond the end of the line.
7912 * 3. Vi compatible searching: continue at end of previous match.
7913 */
7914 if (shl->lnum == 0)
7915 matchcol = 0;
7916 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7917 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007918 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007920 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007921
7922 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007923 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007924 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007926 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 shl->lnum = 0;
7928 break;
7929 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007930#ifdef FEAT_MBYTE
7931 if (has_mbyte)
7932 matchcol += mb_ptr2len(ml);
7933 else
7934#endif
7935 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 }
7937 else
7938 matchcol = shl->rm.endpos[0].col;
7939
7940 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007941 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007943 /* Remember whether shl->rm is using a copy of the regprog in
7944 * cur->match. */
7945 int regprog_is_copy = (shl != &search_hl && cur != NULL
7946 && shl == &cur->hl
7947 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007948 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007949
Bram Moolenaarb3414592014-06-17 17:48:32 +02007950 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7951 matchcol,
7952#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007953 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007954#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007955 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007956#endif
7957 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007958 /* Copy the regprog, in case it got freed and recompiled. */
7959 if (regprog_is_copy)
7960 cur->match.regprog = cur->hl.rm.regprog;
7961
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007962 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007963 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007964 /* Error while handling regexp: stop using this regexp. */
7965 if (shl == &search_hl)
7966 {
7967 /* don't free regprog in the match list, it's a copy */
7968 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007969 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 }
7971 shl->rm.regprog = NULL;
7972 shl->lnum = 0;
7973 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7974 message */
7975 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007976 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007977 }
7978 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007979 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007980 else
7981 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 if (nmatched == 0)
7983 {
7984 shl->lnum = 0; /* no match found */
7985 break;
7986 }
7987 if (shl->rm.startpos[0].lnum > 0
7988 || shl->rm.startpos[0].col >= mincol
7989 || nmatched > 1
7990 || shl->rm.endpos[0].col > mincol)
7991 {
7992 shl->lnum += shl->rm.startpos[0].lnum;
7993 break; /* useful match found */
7994 }
7995 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007996
7997 // Restore called_emsg for assert_fails().
7998 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
Bram Moolenaar85077472016-10-16 14:35:48 +02008001/*
8002 * If there is a match fill "shl" and return one.
8003 * Return zero otherwise.
8004 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008005 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008006next_search_hl_pos(
8007 match_T *shl, /* points to a match */
8008 linenr_T lnum,
8009 posmatch_T *posmatch, /* match positions */
8010 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011{
8012 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008013 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008014
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8016 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008017 llpos_T *pos = &posmatch->pos[i];
8018
8019 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008020 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008021 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008022 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008023 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008025 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008027 /* if this match comes before the one at "found" then swap
8028 * them */
8029 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008030 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008031 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008032
Bram Moolenaar85077472016-10-16 14:35:48 +02008033 *pos = posmatch->pos[found];
8034 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008035 }
8036 }
8037 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008038 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 }
8040 }
8041 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008042 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008043 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008044 colnr_T start = posmatch->pos[found].col == 0
8045 ? 0 : posmatch->pos[found].col - 1;
8046 colnr_T end = posmatch->pos[found].col == 0
8047 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008048
Bram Moolenaar85077472016-10-16 14:35:48 +02008049 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008050 shl->rm.startpos[0].lnum = 0;
8051 shl->rm.startpos[0].col = start;
8052 shl->rm.endpos[0].lnum = 0;
8053 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008054 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008055 posmatch->cur = found + 1;
8056 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008057 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008058 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008059}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008060#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008061
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008063screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 attrentry_T *aep = NULL;
8066
8067 screen_attr = attr;
8068 if (full_screen
8069#ifdef WIN3264
8070 && termcap_active
8071#endif
8072 )
8073 {
8074#ifdef FEAT_GUI
8075 if (gui.in_use)
8076 {
8077 char buf[20];
8078
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008079 /* The GUI handles this internally. */
8080 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 OUT_STR(buf);
8082 }
8083 else
8084#endif
8085 {
8086 if (attr > HL_ALL) /* special HL attr. */
8087 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008088 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 aep = syn_cterm_attr2entry(attr);
8090 else
8091 aep = syn_term_attr2entry(attr);
8092 if (aep == NULL) /* did ":syntax clear" */
8093 attr = 0;
8094 else
8095 attr = aep->ae_attr;
8096 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008097 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008099 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008100#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008101 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8102 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8103 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008104#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008105 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008106 /* If the Normal FG color has BOLD attribute and the new HL
8107 * has a FG color defined, clear BOLD. */
8108 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008109 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008111 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008112 out_str(T_UCS);
8113 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008114 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8115 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008117 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008119 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008121 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008122 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123
8124 /*
8125 * Output the color or start string after bold etc., in case the
8126 * bold etc. override the color setting.
8127 */
8128 if (aep != NULL)
8129 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008130#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008131 /* When 'termguicolors' is set but fg or bg is unset,
8132 * fall back to the cterm colors. This helps for SpellBad,
8133 * where the GUI uses a red undercurl. */
8134 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008136 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008137 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008138 }
8139 else
8140#endif
8141 if (t_colors > 1)
8142 {
8143 if (aep->ae_u.cterm.fg_color)
8144 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8145 }
8146#ifdef FEAT_TERMGUICOLORS
8147 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8148 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008149 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008150 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 }
8152 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008153#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008154 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008156 if (aep->ae_u.cterm.bg_color)
8157 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8158 }
8159
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008160 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 {
8162 if (aep->ae_u.term.start != NULL)
8163 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 }
8166 }
8167 }
8168}
8169
8170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008171screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 int do_ME = FALSE; /* output T_ME code */
8174
8175 if (screen_attr != 0
8176#ifdef WIN3264
8177 && termcap_active
8178#endif
8179 )
8180 {
8181#ifdef FEAT_GUI
8182 if (gui.in_use)
8183 {
8184 char buf[20];
8185
8186 /* use internal GUI code */
8187 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8188 OUT_STR(buf);
8189 }
8190 else
8191#endif
8192 {
8193 if (screen_attr > HL_ALL) /* special HL attr. */
8194 {
8195 attrentry_T *aep;
8196
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008197 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
8199 /*
8200 * Assume that t_me restores the original colors!
8201 */
8202 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008203 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008204#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008205 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8206 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8207 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008208#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008209 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008210#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008211 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8212 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8213 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008214#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008215 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 do_ME = TRUE;
8217 }
8218 else
8219 {
8220 aep = syn_term_attr2entry(screen_attr);
8221 if (aep != NULL && aep->ae_u.term.stop != NULL)
8222 {
8223 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8224 do_ME = TRUE;
8225 else
8226 out_str(aep->ae_u.term.stop);
8227 }
8228 }
8229 if (aep == NULL) /* did ":syntax clear" */
8230 screen_attr = 0;
8231 else
8232 screen_attr = aep->ae_attr;
8233 }
8234
8235 /*
8236 * Often all ending-codes are equal to T_ME. Avoid outputting the
8237 * same sequence several times.
8238 */
8239 if (screen_attr & HL_STANDOUT)
8240 {
8241 if (STRCMP(T_SE, T_ME) == 0)
8242 do_ME = TRUE;
8243 else
8244 out_str(T_SE);
8245 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008246 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008247 {
8248 if (STRCMP(T_UCE, T_ME) == 0)
8249 do_ME = TRUE;
8250 else
8251 out_str(T_UCE);
8252 }
8253 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008254 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {
8256 if (STRCMP(T_UE, T_ME) == 0)
8257 do_ME = TRUE;
8258 else
8259 out_str(T_UE);
8260 }
8261 if (screen_attr & HL_ITALIC)
8262 {
8263 if (STRCMP(T_CZR, T_ME) == 0)
8264 do_ME = TRUE;
8265 else
8266 out_str(T_CZR);
8267 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008268 if (screen_attr & HL_STRIKETHROUGH)
8269 {
8270 if (STRCMP(T_STE, T_ME) == 0)
8271 do_ME = TRUE;
8272 else
8273 out_str(T_STE);
8274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8276 out_str(T_ME);
8277
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008278#ifdef FEAT_TERMGUICOLORS
8279 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008281 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008282 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008283 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008284 term_bg_rgb_color(cterm_normal_bg_gui_color);
8285 }
8286 else
8287#endif
8288 {
8289 if (t_colors > 1)
8290 {
8291 /* set Normal cterm colors */
8292 if (cterm_normal_fg_color != 0)
8293 term_fg_color(cterm_normal_fg_color - 1);
8294 if (cterm_normal_bg_color != 0)
8295 term_bg_color(cterm_normal_bg_color - 1);
8296 if (cterm_normal_fg_bold)
8297 out_str(T_MD);
8298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 }
8300 }
8301 }
8302 screen_attr = 0;
8303}
8304
8305/*
8306 * Reset the colors for a cterm. Used when leaving Vim.
8307 * The machine specific code may override this again.
8308 */
8309 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008310reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008312 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
8314 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008315#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008316 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8317 || cterm_normal_bg_gui_color != INVALCOLOR)
8318 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008319#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {
8323 out_str(T_OP);
8324 screen_attr = -1;
8325 }
8326 if (cterm_normal_fg_bold)
8327 {
8328 out_str(T_ME);
8329 screen_attr = -1;
8330 }
8331 }
8332}
8333
8334/*
8335 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8336 * using the attributes from ScreenAttrs["off"].
8337 */
8338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008339screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 int attr;
8342
8343 /* Check for illegal values, just in case (could happen just after
8344 * resizing). */
8345 if (row >= screen_Rows || col >= screen_Columns)
8346 return;
8347
Bram Moolenaar494838a2015-02-10 19:20:37 +01008348 /* Outputting a character in the last cell on the screen may scroll the
8349 * screen up. Only do it when the "xn" termcap property is set, otherwise
8350 * mark the character invalid (update it when scrolled up). */
8351 if (*T_XN == NUL
8352 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353#ifdef FEAT_RIGHTLEFT
8354 /* account for first command-line character in rightleft mode */
8355 && !cmdmsg_rl
8356#endif
8357 )
8358 {
8359 ScreenAttrs[off] = (sattr_T)-1;
8360 return;
8361 }
8362
8363 /*
8364 * Stop highlighting first, so it's easier to move the cursor.
8365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 if (screen_char_attr != 0)
8367 attr = screen_char_attr;
8368 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 attr = ScreenAttrs[off];
8370 if (screen_attr != attr)
8371 screen_stop_highlight();
8372
8373 windgoto(row, col);
8374
8375 if (screen_attr != attr)
8376 screen_start_highlight(attr);
8377
8378#ifdef FEAT_MBYTE
8379 if (enc_utf8 && ScreenLinesUC[off] != 0)
8380 {
8381 char_u buf[MB_MAXBYTES + 1];
8382
Bram Moolenaarcb070082016-04-02 22:14:51 +02008383 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008384 {
8385 if (*p_ambw == 'd'
8386# ifdef FEAT_GUI
8387 && !gui.in_use
8388# endif
8389 )
8390 {
8391 /* Clear the two screen cells. If the character is actually
8392 * single width it won't change the second cell. */
8393 out_str((char_u *)" ");
8394 term_windgoto(row, col);
8395 }
8396 /* not sure where the cursor is after drawing the ambiguous width
8397 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008398 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008399 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008400 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008402
8403 /* Convert the UTF-8 character to bytes and write it. */
8404 buf[utfc_char2bytes(off, buf)] = NUL;
8405 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407 else
8408#endif
8409 {
8410#ifdef FEAT_MBYTE
8411 out_flush_check();
8412#endif
8413 out_char(ScreenLines[off]);
8414#ifdef FEAT_MBYTE
8415 /* double-byte character in single-width cell */
8416 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8417 out_char(ScreenLines2[off]);
8418#endif
8419 }
8420
8421 screen_cur_col++;
8422}
8423
8424#ifdef FEAT_MBYTE
8425
8426/*
8427 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8428 * on the screen at position 'row' and 'col'.
8429 * The attributes of the first byte is used for all. This is required to
8430 * output the two bytes of a double-byte character with nothing in between.
8431 */
8432 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008433screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434{
8435 /* Check for illegal values (could be wrong when screen was resized). */
8436 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8437 return;
8438
8439 /* Outputting the last character on the screen may scrollup the screen.
8440 * Don't to it! Mark the character invalid (update it when scrolled up) */
8441 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8442 {
8443 ScreenAttrs[off] = (sattr_T)-1;
8444 return;
8445 }
8446
8447 /* Output the first byte normally (positions the cursor), then write the
8448 * second byte directly. */
8449 screen_char(off, row, col);
8450 out_char(ScreenLines[off + 1]);
8451 ++screen_cur_col;
8452}
8453#endif
8454
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455/*
8456 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8457 * This uses the contents of ScreenLines[] and doesn't change it.
8458 */
8459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008460screen_draw_rectangle(
8461 int row,
8462 int col,
8463 int height,
8464 int width,
8465 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466{
8467 int r, c;
8468 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008469#ifdef FEAT_MBYTE
8470 int max_off;
8471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008473 /* Can't use ScreenLines unless initialized */
8474 if (ScreenLines == NULL)
8475 return;
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (invert)
8478 screen_char_attr = HL_INVERSE;
8479 for (r = row; r < row + height; ++r)
8480 {
8481 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008482#ifdef FEAT_MBYTE
8483 max_off = off + screen_Columns;
8484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 for (c = col; c < col + width; ++c)
8486 {
8487#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008488 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {
8490 screen_char_2(off + c, r, c);
8491 ++c;
8492 }
8493 else
8494#endif
8495 {
8496 screen_char(off + c, r, c);
8497#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008498 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 ++c;
8500#endif
8501 }
8502 }
8503 }
8504 screen_char_attr = 0;
8505}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507/*
8508 * Redraw the characters for a vertically split window.
8509 */
8510 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008511redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
8513 int col;
8514 int width;
8515
8516# ifdef FEAT_CLIPBOARD
8517 clip_may_clear_selection(row, end - 1);
8518# endif
8519
8520 if (wp == NULL)
8521 {
8522 col = 0;
8523 width = Columns;
8524 }
8525 else
8526 {
8527 col = wp->w_wincol;
8528 width = wp->w_width;
8529 }
8530 screen_draw_rectangle(row, col, end - row, width, FALSE);
8531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008533 static void
8534space_to_screenline(int off, int attr)
8535{
8536 ScreenLines[off] = ' ';
8537 ScreenAttrs[off] = attr;
8538# ifdef FEAT_MBYTE
8539 if (enc_utf8)
8540 ScreenLinesUC[off] = 0;
8541# endif
8542}
8543
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544/*
8545 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8546 * with character 'c1' in first column followed by 'c2' in the other columns.
8547 * Use attributes 'attr'.
8548 */
8549 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008550screen_fill(
8551 int start_row,
8552 int end_row,
8553 int start_col,
8554 int end_col,
8555 int c1,
8556 int c2,
8557 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 int row;
8560 int col;
8561 int off;
8562 int end_off;
8563 int did_delete;
8564 int c;
8565 int norm_term;
8566#if defined(FEAT_GUI) || defined(UNIX)
8567 int force_next = FALSE;
8568#endif
8569
8570 if (end_row > screen_Rows) /* safety check */
8571 end_row = screen_Rows;
8572 if (end_col > screen_Columns) /* safety check */
8573 end_col = screen_Columns;
8574 if (ScreenLines == NULL
8575 || start_row >= end_row
8576 || start_col >= end_col) /* nothing to do */
8577 return;
8578
8579 /* it's a "normal" terminal when not in a GUI or cterm */
8580 norm_term = (
8581#ifdef FEAT_GUI
8582 !gui.in_use &&
8583#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008584 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 for (row = start_row; row < end_row; ++row)
8586 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008587#ifdef FEAT_MBYTE
8588 if (has_mbyte
8589# ifdef FEAT_GUI
8590 && !gui.in_use
8591# endif
8592 )
8593 {
8594 /* When drawing over the right halve of a double-wide char clear
8595 * out the left halve. When drawing over the left halve of a
8596 * double wide-char clear out the right halve. Only needed in a
8597 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008598 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008599 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008600 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008601 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008602 }
8603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 /*
8605 * Try to use delete-line termcap code, when no attributes or in a
8606 * "normal" terminal, where a bold/italic space is just a
8607 * space.
8608 */
8609 did_delete = FALSE;
8610 if (c2 == ' '
8611 && end_col == Columns
8612 && can_clear(T_CE)
8613 && (attr == 0
8614 || (norm_term
8615 && attr <= HL_ALL
8616 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8617 {
8618 /*
8619 * check if we really need to clear something
8620 */
8621 col = start_col;
8622 if (c1 != ' ') /* don't clear first char */
8623 ++col;
8624
8625 off = LineOffset[row] + col;
8626 end_off = LineOffset[row] + end_col;
8627
8628 /* skip blanks (used often, keep it fast!) */
8629#ifdef FEAT_MBYTE
8630 if (enc_utf8)
8631 while (off < end_off && ScreenLines[off] == ' '
8632 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8633 ++off;
8634 else
8635#endif
8636 while (off < end_off && ScreenLines[off] == ' '
8637 && ScreenAttrs[off] == 0)
8638 ++off;
8639 if (off < end_off) /* something to be cleared */
8640 {
8641 col = off - LineOffset[row];
8642 screen_stop_highlight();
8643 term_windgoto(row, col);/* clear rest of this screen line */
8644 out_str(T_CE);
8645 screen_start(); /* don't know where cursor is now */
8646 col = end_col - col;
8647 while (col--) /* clear chars in ScreenLines */
8648 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008649 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 ++off;
8651 }
8652 }
8653 did_delete = TRUE; /* the chars are cleared now */
8654 }
8655
8656 off = LineOffset[row] + start_col;
8657 c = c1;
8658 for (col = start_col; col < end_col; ++col)
8659 {
8660 if (ScreenLines[off] != c
8661#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008662 || (enc_utf8 && (int)ScreenLinesUC[off]
8663 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664#endif
8665 || ScreenAttrs[off] != attr
8666#if defined(FEAT_GUI) || defined(UNIX)
8667 || force_next
8668#endif
8669 )
8670 {
8671#if defined(FEAT_GUI) || defined(UNIX)
8672 /* The bold trick may make a single row of pixels appear in
8673 * the next character. When a bold character is removed, the
8674 * next character should be redrawn too. This happens for our
8675 * own GUI and for some xterms. */
8676 if (
8677# ifdef FEAT_GUI
8678 gui.in_use
8679# endif
8680# if defined(FEAT_GUI) && defined(UNIX)
8681 ||
8682# endif
8683# ifdef UNIX
8684 term_is_xterm
8685# endif
8686 )
8687 {
8688 if (ScreenLines[off] != ' '
8689 && (ScreenAttrs[off] > HL_ALL
8690 || ScreenAttrs[off] & HL_BOLD))
8691 force_next = TRUE;
8692 else
8693 force_next = FALSE;
8694 }
8695#endif
8696 ScreenLines[off] = c;
8697#ifdef FEAT_MBYTE
8698 if (enc_utf8)
8699 {
8700 if (c >= 0x80)
8701 {
8702 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008703 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 }
8705 else
8706 ScreenLinesUC[off] = 0;
8707 }
8708#endif
8709 ScreenAttrs[off] = attr;
8710 if (!did_delete || c != ' ')
8711 screen_char(off, row, col);
8712 }
8713 ++off;
8714 if (col == start_col)
8715 {
8716 if (did_delete)
8717 break;
8718 c = c2;
8719 }
8720 }
8721 if (end_col == Columns)
8722 LineWraps[row] = FALSE;
8723 if (row == Rows - 1) /* overwritten the command line */
8724 {
8725 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008726 if (start_col == 0 && end_col == Columns
8727 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008729 if (start_col == 0)
8730 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
8732 }
8733}
8734
8735/*
8736 * Check if there should be a delay. Used before clearing or redrawing the
8737 * screen or the command line.
8738 */
8739 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008740check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
8742 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8743 && !did_wait_return
8744 && emsg_silent == 0)
8745 {
8746 out_flush();
8747 ui_delay(1000L, TRUE);
8748 emsg_on_display = FALSE;
8749 if (check_msg_scroll)
8750 msg_scroll = FALSE;
8751 }
8752}
8753
8754/*
8755 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008756 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * Returns TRUE if there is a valid screen to write to.
8758 * Returns FALSE when starting up and screen not initialized yet.
8759 */
8760 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008761screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008763 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 return (ScreenLines != NULL);
8765}
8766
8767/*
8768 * Resize the shell to Rows and Columns.
8769 * Allocate ScreenLines[] and associated items.
8770 *
8771 * There may be some time between setting Rows and Columns and (re)allocating
8772 * ScreenLines[]. This happens when starting up and when (manually) changing
8773 * the shell size. Always use screen_Rows and screen_Columns to access items
8774 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8775 * final size of the shell is needed.
8776 */
8777 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008778screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 int new_row, old_row;
8781#ifdef FEAT_GUI
8782 int old_Rows;
8783#endif
8784 win_T *wp;
8785 int outofmem = FALSE;
8786 int len;
8787 schar_T *new_ScreenLines;
8788#ifdef FEAT_MBYTE
8789 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008790 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008792 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793#endif
8794 sattr_T *new_ScreenAttrs;
8795 unsigned *new_LineOffset;
8796 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008797 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008798 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008800 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008801 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008803retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 /*
8805 * Allocation of the screen buffers is done only when the size changes and
8806 * when Rows and Columns have been set and we have started doing full
8807 * screen stuff.
8808 */
8809 if ((ScreenLines != NULL
8810 && Rows == screen_Rows
8811 && Columns == screen_Columns
8812#ifdef FEAT_MBYTE
8813 && enc_utf8 == (ScreenLinesUC != NULL)
8814 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008815 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816#endif
8817 )
8818 || Rows == 0
8819 || Columns == 0
8820 || (!full_screen && ScreenLines == NULL))
8821 return;
8822
8823 /*
8824 * It's possible that we produce an out-of-memory message below, which
8825 * will cause this function to be called again. To break the loop, just
8826 * return here.
8827 */
8828 if (entered)
8829 return;
8830 entered = TRUE;
8831
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008832 /*
8833 * Note that the window sizes are updated before reallocating the arrays,
8834 * thus we must not redraw here!
8835 */
8836 ++RedrawingDisabled;
8837
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 win_new_shellsize(); /* fit the windows in the new sized shell */
8839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 comp_col(); /* recompute columns for shown command and ruler */
8841
8842 /*
8843 * We're changing the size of the screen.
8844 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8845 * - Move lines from the old arrays into the new arrays, clear extra
8846 * lines (unless the screen is going to be cleared).
8847 * - Free the old arrays.
8848 *
8849 * If anything fails, make ScreenLines NULL, so we don't do anything!
8850 * Continuing with the old ScreenLines may result in a crash, because the
8851 * size is wrong.
8852 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008853 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008855 if (aucmd_win != NULL)
8856 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857
8858 new_ScreenLines = (schar_T *)lalloc((long_u)(
8859 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8860#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008861 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (enc_utf8)
8863 {
8864 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8865 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008866 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008867 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8869 }
8870 if (enc_dbcs == DBCS_JPNU)
8871 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8872 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8873#endif
8874 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8875 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8876 new_LineOffset = (unsigned *)lalloc((long_u)(
8877 Rows * sizeof(unsigned)), FALSE);
8878 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008879 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008881 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 {
8883 if (win_alloc_lines(wp) == FAIL)
8884 {
8885 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008886 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 }
8888 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008889 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8890 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008891 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008892give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008894#ifdef FEAT_MBYTE
8895 for (i = 0; i < p_mco; ++i)
8896 if (new_ScreenLinesC[i] == NULL)
8897 break;
8898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 if (new_ScreenLines == NULL
8900#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008901 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8903#endif
8904 || new_ScreenAttrs == NULL
8905 || new_LineOffset == NULL
8906 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008907 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 || outofmem)
8909 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008910 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008911 {
8912 /* guess the size */
8913 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8914
8915 /* Remember we did this to avoid getting outofmem messages over
8916 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008917 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008918 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008919 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008921 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008922 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008923 VIM_CLEAR(new_ScreenLinesC[i]);
8924 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008926 VIM_CLEAR(new_ScreenAttrs);
8927 VIM_CLEAR(new_LineOffset);
8928 VIM_CLEAR(new_LineWraps);
8929 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
8931 else
8932 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008933 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 for (new_row = 0; new_row < Rows; ++new_row)
8936 {
8937 new_LineOffset[new_row] = new_row * Columns;
8938 new_LineWraps[new_row] = FALSE;
8939
8940 /*
8941 * If the screen is not going to be cleared, copy as much as
8942 * possible from the old screen to the new one and clear the rest
8943 * (used when resizing the window at the "--more--" prompt or when
8944 * executing an external command, for the GUI).
8945 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008946 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 {
8948 (void)vim_memset(new_ScreenLines + new_row * Columns,
8949 ' ', (size_t)Columns * sizeof(schar_T));
8950#ifdef FEAT_MBYTE
8951 if (enc_utf8)
8952 {
8953 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8954 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008955 for (i = 0; i < p_mco; ++i)
8956 (void)vim_memset(new_ScreenLinesC[i]
8957 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 0, (size_t)Columns * sizeof(u8char_T));
8959 }
8960 if (enc_dbcs == DBCS_JPNU)
8961 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8962 0, (size_t)Columns * sizeof(schar_T));
8963#endif
8964 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8965 0, (size_t)Columns * sizeof(sattr_T));
8966 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008967 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
8969 if (screen_Columns < Columns)
8970 len = screen_Columns;
8971 else
8972 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008973#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008974 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 * may be invalid now. Also when p_mco changes. */
8976 if (!(enc_utf8 && ScreenLinesUC == NULL)
8977 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008978#endif
8979 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8980 ScreenLines + LineOffset[old_row],
8981 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008983 if (enc_utf8 && ScreenLinesUC != NULL
8984 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 {
8986 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8987 ScreenLinesUC + LineOffset[old_row],
8988 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 for (i = 0; i < p_mco; ++i)
8990 mch_memmove(new_ScreenLinesC[i]
8991 + new_LineOffset[new_row],
8992 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 (size_t)len * sizeof(u8char_T));
8994 }
8995 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8996 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8997 ScreenLines2 + LineOffset[old_row],
8998 (size_t)len * sizeof(schar_T));
8999#endif
9000 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9001 ScreenAttrs + LineOffset[old_row],
9002 (size_t)len * sizeof(sattr_T));
9003 }
9004 }
9005 }
9006 /* Use the last line of the screen for the current line. */
9007 current_ScreenLine = new_ScreenLines + Rows * Columns;
9008 }
9009
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009010 free_screenlines();
9011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 ScreenLines = new_ScreenLines;
9013#ifdef FEAT_MBYTE
9014 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009015 for (i = 0; i < p_mco; ++i)
9016 ScreenLinesC[i] = new_ScreenLinesC[i];
9017 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 ScreenLines2 = new_ScreenLines2;
9019#endif
9020 ScreenAttrs = new_ScreenAttrs;
9021 LineOffset = new_LineOffset;
9022 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009023 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
9025 /* It's important that screen_Rows and screen_Columns reflect the actual
9026 * size of ScreenLines[]. Set them before calling anything. */
9027#ifdef FEAT_GUI
9028 old_Rows = screen_Rows;
9029#endif
9030 screen_Rows = Rows;
9031 screen_Columns = Columns;
9032
9033 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009034 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 screenclear2();
9036
9037#ifdef FEAT_GUI
9038 else if (gui.in_use
9039 && !gui.starting
9040 && ScreenLines != NULL
9041 && old_Rows != Rows)
9042 {
9043 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9044 /*
9045 * Adjust the position of the cursor, for when executing an external
9046 * command.
9047 */
9048 if (msg_row >= Rows) /* Rows got smaller */
9049 msg_row = Rows - 1; /* put cursor at last row */
9050 else if (Rows > old_Rows) /* Rows got bigger */
9051 msg_row += Rows - old_Rows; /* put cursor in same place */
9052 if (msg_col >= Columns) /* Columns got smaller */
9053 msg_col = Columns - 1; /* put cursor at last column */
9054 }
9055#endif
9056
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009058 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009059
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009060 /*
9061 * Do not apply autocommands more than 3 times to avoid an endless loop
9062 * in case applying autocommands always changes Rows or Columns.
9063 */
9064 if (starting == 0 && ++retry_count <= 3)
9065 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009066 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009067 /* In rare cases, autocommands may have altered Rows or Columns,
9068 * jump back to check if we need to allocate the screen again. */
9069 goto retry;
9070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071}
9072
9073 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009074free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009075{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009076#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009077 int i;
9078
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009079 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009080 for (i = 0; i < Screen_mco; ++i)
9081 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009082 vim_free(ScreenLines2);
9083#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009084 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009085 vim_free(ScreenAttrs);
9086 vim_free(LineOffset);
9087 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009088 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009089}
9090
9091 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009092screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093{
9094 check_for_delay(FALSE);
9095 screenalloc(FALSE); /* allocate screen buffers if size changed */
9096 screenclear2(); /* clear the screen */
9097}
9098
9099 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009100screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 int i;
9103
9104 if (starting == NO_SCREEN || ScreenLines == NULL
9105#ifdef FEAT_GUI
9106 || (gui.in_use && gui.starting)
9107#endif
9108 )
9109 return;
9110
9111#ifdef FEAT_GUI
9112 if (!gui.in_use)
9113#endif
9114 screen_attr = -1; /* force setting the Normal colors */
9115 screen_stop_highlight(); /* don't want highlighting here */
9116
9117#ifdef FEAT_CLIPBOARD
9118 /* disable selection without redrawing it */
9119 clip_scroll_selection(9999);
9120#endif
9121
9122 /* blank out ScreenLines */
9123 for (i = 0; i < Rows; ++i)
9124 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009125 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 LineWraps[i] = FALSE;
9127 }
9128
9129 if (can_clear(T_CL))
9130 {
9131 out_str(T_CL); /* clear the display */
9132 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009133 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 }
9135 else
9136 {
9137 /* can't clear the screen, mark all chars with invalid attributes */
9138 for (i = 0; i < Rows; ++i)
9139 lineinvalid(LineOffset[i], (int)Columns);
9140 clear_cmdline = TRUE;
9141 }
9142
9143 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9144
9145 win_rest_invalid(firstwin);
9146 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009147 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 if (must_redraw == CLEAR) /* no need to clear again */
9149 must_redraw = NOT_VALID;
9150 compute_cmdrow();
9151 msg_row = cmdline_row; /* put cursor on last line for messages */
9152 msg_col = 0;
9153 screen_start(); /* don't know where cursor is now */
9154 msg_scrolled = 0; /* can't scroll back */
9155 msg_didany = FALSE;
9156 msg_didout = FALSE;
9157}
9158
9159/*
9160 * Clear one line in ScreenLines.
9161 */
9162 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009163lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9166#ifdef FEAT_MBYTE
9167 if (enc_utf8)
9168 (void)vim_memset(ScreenLinesUC + off, 0,
9169 (size_t)width * sizeof(u8char_T));
9170#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009171 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
9175 * Mark one line in ScreenLines invalid by setting the attributes to an
9176 * invalid value.
9177 */
9178 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009179lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180{
9181 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9182}
9183
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184/*
9185 * Copy part of a Screenline for vertically split window "wp".
9186 */
9187 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009188linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189{
9190 unsigned off_to = LineOffset[to] + wp->w_wincol;
9191 unsigned off_from = LineOffset[from] + wp->w_wincol;
9192
9193 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9194 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009195#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 if (enc_utf8)
9197 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009198 int i;
9199
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9201 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009202 for (i = 0; i < p_mco; ++i)
9203 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9204 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 }
9206 if (enc_dbcs == DBCS_JPNU)
9207 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9208 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9211 wp->w_width * sizeof(sattr_T));
9212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213
9214/*
9215 * Return TRUE if clearing with term string "p" would work.
9216 * It can't work when the string is empty or it won't set the right background.
9217 */
9218 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009219can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220{
9221 return (*p != NUL && (t_colors <= 1
9222#ifdef FEAT_GUI
9223 || gui.in_use
9224#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009225#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009226 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009227 || (!p_tgc && cterm_normal_bg_color == 0)
9228#else
9229 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009230#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009231 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232}
9233
9234/*
9235 * Reset cursor position. Use whenever cursor was moved because of outputting
9236 * something directly to the screen (shell commands) or a terminal control
9237 * code.
9238 */
9239 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009240screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241{
9242 screen_cur_row = screen_cur_col = 9999;
9243}
9244
9245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 * Move the cursor to position "row","col" in the screen.
9247 * This tries to find the most efficient way to move, minimizing the number of
9248 * characters sent to the terminal.
9249 */
9250 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009251windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009253 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 int i;
9255 int plan;
9256 int cost;
9257 int wouldbe_col;
9258 int noinvcurs;
9259 char_u *bs;
9260 int goto_cost;
9261 int attr;
9262
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009263#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9265
9266#define PLAN_LE 1
9267#define PLAN_CR 2
9268#define PLAN_NL 3
9269#define PLAN_WRITE 4
9270 /* Can't use ScreenLines unless initialized */
9271 if (ScreenLines == NULL)
9272 return;
9273
9274 if (col != screen_cur_col || row != screen_cur_row)
9275 {
9276 /* Check for valid position. */
9277 if (row < 0) /* window without text lines? */
9278 row = 0;
9279 if (row >= screen_Rows)
9280 row = screen_Rows - 1;
9281 if (col >= screen_Columns)
9282 col = screen_Columns - 1;
9283
9284 /* check if no cursor movement is allowed in highlight mode */
9285 if (screen_attr && *T_MS == NUL)
9286 noinvcurs = HIGHL_COST;
9287 else
9288 noinvcurs = 0;
9289 goto_cost = GOTO_COST + noinvcurs;
9290
9291 /*
9292 * Plan how to do the positioning:
9293 * 1. Use CR to move it to column 0, same row.
9294 * 2. Use T_LE to move it a few columns to the left.
9295 * 3. Use NL to move a few lines down, column 0.
9296 * 4. Move a few columns to the right with T_ND or by writing chars.
9297 *
9298 * Don't do this if the cursor went beyond the last column, the cursor
9299 * position is unknown then (some terminals wrap, some don't )
9300 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009301 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * characters to move the cursor to the right.
9303 */
9304 if (row >= screen_cur_row && screen_cur_col < Columns)
9305 {
9306 /*
9307 * If the cursor is in the same row, bigger col, we can use CR
9308 * or T_LE.
9309 */
9310 bs = NULL; /* init for GCC */
9311 attr = screen_attr;
9312 if (row == screen_cur_row && col < screen_cur_col)
9313 {
9314 /* "le" is preferred over "bc", because "bc" is obsolete */
9315 if (*T_LE)
9316 bs = T_LE; /* "cursor left" */
9317 else
9318 bs = T_BC; /* "backspace character (old) */
9319 if (*bs)
9320 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9321 else
9322 cost = 999;
9323 if (col + 1 < cost) /* using CR is less characters */
9324 {
9325 plan = PLAN_CR;
9326 wouldbe_col = 0;
9327 cost = 1; /* CR is just one character */
9328 }
9329 else
9330 {
9331 plan = PLAN_LE;
9332 wouldbe_col = col;
9333 }
9334 if (noinvcurs) /* will stop highlighting */
9335 {
9336 cost += noinvcurs;
9337 attr = 0;
9338 }
9339 }
9340
9341 /*
9342 * If the cursor is above where we want to be, we can use CR LF.
9343 */
9344 else if (row > screen_cur_row)
9345 {
9346 plan = PLAN_NL;
9347 wouldbe_col = 0;
9348 cost = (row - screen_cur_row) * 2; /* CR LF */
9349 if (noinvcurs) /* will stop highlighting */
9350 {
9351 cost += noinvcurs;
9352 attr = 0;
9353 }
9354 }
9355
9356 /*
9357 * If the cursor is in the same row, smaller col, just use write.
9358 */
9359 else
9360 {
9361 plan = PLAN_WRITE;
9362 wouldbe_col = screen_cur_col;
9363 cost = 0;
9364 }
9365
9366 /*
9367 * Check if any characters that need to be written have the
9368 * correct attributes. Also avoid UTF-8 characters.
9369 */
9370 i = col - wouldbe_col;
9371 if (i > 0)
9372 cost += i;
9373 if (cost < goto_cost && i > 0)
9374 {
9375 /*
9376 * Check if the attributes are correct without additionally
9377 * stopping highlighting.
9378 */
9379 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9380 while (i && *p++ == attr)
9381 --i;
9382 if (i != 0)
9383 {
9384 /*
9385 * Try if it works when highlighting is stopped here.
9386 */
9387 if (*--p == 0)
9388 {
9389 cost += noinvcurs;
9390 while (i && *p++ == 0)
9391 --i;
9392 }
9393 if (i != 0)
9394 cost = 999; /* different attributes, don't do it */
9395 }
9396#ifdef FEAT_MBYTE
9397 if (enc_utf8)
9398 {
9399 /* Don't use an UTF-8 char for positioning, it's slow. */
9400 for (i = wouldbe_col; i < col; ++i)
9401 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9402 {
9403 cost = 999;
9404 break;
9405 }
9406 }
9407#endif
9408 }
9409
9410 /*
9411 * We can do it without term_windgoto()!
9412 */
9413 if (cost < goto_cost)
9414 {
9415 if (plan == PLAN_LE)
9416 {
9417 if (noinvcurs)
9418 screen_stop_highlight();
9419 while (screen_cur_col > col)
9420 {
9421 out_str(bs);
9422 --screen_cur_col;
9423 }
9424 }
9425 else if (plan == PLAN_CR)
9426 {
9427 if (noinvcurs)
9428 screen_stop_highlight();
9429 out_char('\r');
9430 screen_cur_col = 0;
9431 }
9432 else if (plan == PLAN_NL)
9433 {
9434 if (noinvcurs)
9435 screen_stop_highlight();
9436 while (screen_cur_row < row)
9437 {
9438 out_char('\n');
9439 ++screen_cur_row;
9440 }
9441 screen_cur_col = 0;
9442 }
9443
9444 i = col - screen_cur_col;
9445 if (i > 0)
9446 {
9447 /*
9448 * Use cursor-right if it's one character only. Avoids
9449 * removing a line of pixels from the last bold char, when
9450 * using the bold trick in the GUI.
9451 */
9452 if (T_ND[0] != NUL && T_ND[1] == NUL)
9453 {
9454 while (i-- > 0)
9455 out_char(*T_ND);
9456 }
9457 else
9458 {
9459 int off;
9460
9461 off = LineOffset[row] + screen_cur_col;
9462 while (i-- > 0)
9463 {
9464 if (ScreenAttrs[off] != screen_attr)
9465 screen_stop_highlight();
9466#ifdef FEAT_MBYTE
9467 out_flush_check();
9468#endif
9469 out_char(ScreenLines[off]);
9470#ifdef FEAT_MBYTE
9471 if (enc_dbcs == DBCS_JPNU
9472 && ScreenLines[off] == 0x8e)
9473 out_char(ScreenLines2[off]);
9474#endif
9475 ++off;
9476 }
9477 }
9478 }
9479 }
9480 }
9481 else
9482 cost = 999;
9483
9484 if (cost >= goto_cost)
9485 {
9486 if (noinvcurs)
9487 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009488 if (row == screen_cur_row && (col > screen_cur_col)
9489 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 term_cursor_right(col - screen_cur_col);
9491 else
9492 term_windgoto(row, col);
9493 }
9494 screen_cur_row = row;
9495 screen_cur_col = col;
9496 }
9497}
9498
9499/*
9500 * Set cursor to its position in the current window.
9501 */
9502 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009503setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009505 setcursor_mayforce(FALSE);
9506}
9507
9508/*
9509 * Set cursor to its position in the current window.
9510 * When "force" is TRUE also when not redrawing.
9511 */
9512 void
9513setcursor_mayforce(int force)
9514{
9515 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
9517 validate_cursor();
9518 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009519 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009521 /* With 'rightleft' set and the cursor on a double-wide
9522 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009523 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009525 (has_mbyte
9526 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9527 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528# endif
9529 1)) :
9530#endif
9531 curwin->w_wcol));
9532 }
9533}
9534
9535
9536/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009537 * Insert 'line_count' lines at 'row' in window 'wp'.
9538 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9539 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 * scrolling.
9541 * Returns FAIL if the lines are not inserted, OK for success.
9542 */
9543 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009544win_ins_lines(
9545 win_T *wp,
9546 int row,
9547 int line_count,
9548 int invalid,
9549 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 int did_delete;
9552 int nextrow;
9553 int lastrow;
9554 int retval;
9555
9556 if (invalid)
9557 wp->w_lines_valid = 0;
9558
9559 if (wp->w_height < 5)
9560 return FAIL;
9561
9562 if (line_count > wp->w_height - row)
9563 line_count = wp->w_height - row;
9564
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009565 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 if (retval != MAYBE)
9567 return retval;
9568
9569 /*
9570 * If there is a next window or a status line, we first try to delete the
9571 * lines at the bottom to avoid messing what is after the window.
9572 * If this fails and there are following windows, don't do anything to avoid
9573 * messing up those windows, better just redraw.
9574 */
9575 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 if (wp->w_next != NULL || wp->w_status_height)
9577 {
9578 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009579 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 did_delete = TRUE;
9581 else if (wp->w_next)
9582 return FAIL;
9583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 /*
9585 * if no lines deleted, blank the lines that will end up below the window
9586 */
9587 if (!did_delete)
9588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009591 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 lastrow = nextrow + line_count;
9593 if (lastrow > Rows)
9594 lastrow = Rows;
9595 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009596 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 ' ', ' ', 0);
9598 }
9599
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009600 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 == FAIL)
9602 {
9603 /* deletion will have messed up other windows */
9604 if (did_delete)
9605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 win_rest_invalid(W_NEXT(wp));
9608 }
9609 return FAIL;
9610 }
9611
9612 return OK;
9613}
9614
9615/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009616 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9618 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9619 * scrolling
9620 * Return OK for success, FAIL if the lines are not deleted.
9621 */
9622 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009623win_del_lines(
9624 win_T *wp,
9625 int row,
9626 int line_count,
9627 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009628 int mayclear,
9629 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 int retval;
9632
9633 if (invalid)
9634 wp->w_lines_valid = 0;
9635
9636 if (line_count > wp->w_height - row)
9637 line_count = wp->w_height - row;
9638
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009639 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 if (retval != MAYBE)
9641 return retval;
9642
9643 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009644 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 return FAIL;
9646
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 /*
9648 * If there are windows or status lines below, try to put them at the
9649 * correct place. If we can't do that, they have to be redrawn.
9650 */
9651 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9652 {
9653 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009654 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 {
9656 wp->w_redr_status = TRUE;
9657 win_rest_invalid(wp->w_next);
9658 }
9659 }
9660 /*
9661 * If this is the last window and there is no status line, redraw the
9662 * command line later.
9663 */
9664 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 redraw_cmdline = TRUE;
9666 return OK;
9667}
9668
9669/*
9670 * Common code for win_ins_lines() and win_del_lines().
9671 * Returns OK or FAIL when the work has been done.
9672 * Returns MAYBE when not finished yet.
9673 */
9674 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009675win_do_lines(
9676 win_T *wp,
9677 int row,
9678 int line_count,
9679 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009680 int del,
9681 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 int retval;
9684
9685 if (!redrawing() || line_count <= 0)
9686 return FAIL;
9687
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009688 /* When inserting lines would result in loss of command output, just redraw
9689 * the lines. */
9690 if (no_win_do_lines_ins && !del)
9691 return FAIL;
9692
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009694 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009696 if (!no_win_do_lines_ins)
9697 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 return FAIL;
9699 }
9700
9701 /*
9702 * Delete all remaining lines
9703 */
9704 if (row + line_count >= wp->w_height)
9705 {
9706 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009707 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 ' ', ' ', 0);
9709 return OK;
9710 }
9711
9712 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009713 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009715 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009717 if (!no_win_do_lines_ins)
9718 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
9720 /*
9721 * If the terminal can set a scroll region, use that.
9722 * Always do this in a vertically split window. This will redraw from
9723 * ScreenLines[] when t_CV isn't defined. That's faster than using
9724 * win_line().
9725 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009726 * a character in the lower right corner of the scroll region may cause a
9727 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009729 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 scroll_region_set(wp, row);
9733 if (del)
9734 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009735 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 else
9737 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009738 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 scroll_region_reset();
9741 return retval;
9742 }
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746
9747 return MAYBE;
9748}
9749
9750/*
9751 * window 'wp' and everything after it is messed up, mark it for redraw
9752 */
9753 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009754win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 {
9758 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 wp->w_redr_status = TRUE;
9760 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 }
9762 redraw_cmdline = TRUE;
9763}
9764
9765/*
9766 * The rest of the routines in this file perform screen manipulations. The
9767 * given operation is performed physically on the screen. The corresponding
9768 * change is also made to the internal screen image. In this way, the editor
9769 * anticipates the effect of editing changes on the appearance of the screen.
9770 * That way, when we call screenupdate a complete redraw isn't usually
9771 * necessary. Another advantage is that we can keep adding code to anticipate
9772 * screen changes, and in the meantime, everything still works.
9773 */
9774
9775/*
9776 * types for inserting or deleting lines
9777 */
9778#define USE_T_CAL 1
9779#define USE_T_CDL 2
9780#define USE_T_AL 3
9781#define USE_T_CE 4
9782#define USE_T_DL 5
9783#define USE_T_SR 6
9784#define USE_NL 7
9785#define USE_T_CD 8
9786#define USE_REDRAW 9
9787
9788/*
9789 * insert lines on the screen and update ScreenLines[]
9790 * 'end' is the line after the scrolled part. Normally it is Rows.
9791 * When scrolling region used 'off' is the offset from the top for the region.
9792 * 'row' and 'end' are relative to the start of the region.
9793 *
9794 * return FAIL for failure, OK for success.
9795 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009796 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009797screen_ins_lines(
9798 int off,
9799 int row,
9800 int line_count,
9801 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009802 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009803 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804{
9805 int i;
9806 int j;
9807 unsigned temp;
9808 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009809 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 int type;
9811 int result_empty;
9812 int can_ce = can_clear(T_CE);
9813
9814 /*
9815 * FAIL if
9816 * - there is no valid screen
9817 * - the screen has to be redrawn completely
9818 * - the line count is less than one
9819 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009820 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009822 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9823#ifdef FEAT_CLIPBOARD
9824 || (clip_star.state != SELECT_CLEARED
9825 && redrawing_for_callback > 0)
9826#endif
9827 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 return FAIL;
9829
9830 /*
9831 * There are seven ways to insert lines:
9832 * 0. When in a vertically split window and t_CV isn't set, redraw the
9833 * characters from ScreenLines[].
9834 * 1. Use T_CD (clear to end of display) if it exists and the result of
9835 * the insert is just empty lines
9836 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9837 * present or line_count > 1. It looks better if we do all the inserts
9838 * at once.
9839 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9840 * insert is just empty lines and T_CE is not present or line_count >
9841 * 1.
9842 * 4. Use T_AL (insert line) if it exists.
9843 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9844 * just empty lines.
9845 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9846 * just empty lines.
9847 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9848 * the 'da' flag is not set or we have clear line capability.
9849 * 8. redraw the characters from ScreenLines[].
9850 *
9851 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9852 * the scrollbar for the window. It does have insert line, use that if it
9853 * exists.
9854 */
9855 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9857 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009858 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 type = USE_T_CD;
9860 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9861 type = USE_T_CAL;
9862 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9863 type = USE_T_CDL;
9864 else if (*T_AL != NUL)
9865 type = USE_T_AL;
9866 else if (can_ce && result_empty)
9867 type = USE_T_CE;
9868 else if (*T_DL != NUL && result_empty)
9869 type = USE_T_DL;
9870 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9871 type = USE_T_SR;
9872 else
9873 return FAIL;
9874
9875 /*
9876 * For clearing the lines screen_del_lines() is used. This will also take
9877 * care of t_db if necessary.
9878 */
9879 if (type == USE_T_CD || type == USE_T_CDL ||
9880 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009881 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
9883 /*
9884 * If text is retained below the screen, first clear or delete as many
9885 * lines at the bottom of the window as are about to be inserted so that
9886 * the deleted lines won't later surface during a screen_del_lines.
9887 */
9888 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009889 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891#ifdef FEAT_CLIPBOARD
9892 /* Remove a modeless selection when inserting lines halfway the screen
9893 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009894 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009895 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 else
9897 clip_scroll_selection(-line_count);
9898#endif
9899
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900#ifdef FEAT_GUI
9901 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9902 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009903 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904#endif
9905
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009906 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9907 cursor_col = wp->w_wincol;
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 if (*T_CCS != NUL) /* cursor relative to region */
9910 cursor_row = row;
9911 else
9912 cursor_row = row + off;
9913
9914 /*
9915 * Shift LineOffset[] line_count down to reflect the inserted lines.
9916 * Clear the inserted lines in ScreenLines[].
9917 */
9918 row += off;
9919 end += off;
9920 for (i = 0; i < line_count; ++i)
9921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 if (wp != NULL && wp->w_width != Columns)
9923 {
9924 /* need to copy part of a line */
9925 j = end - 1 - i;
9926 while ((j -= line_count) >= row)
9927 linecopy(j + line_count, j, wp);
9928 j += line_count;
9929 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009930 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9931 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 else
9933 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9934 LineWraps[j] = FALSE;
9935 }
9936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 {
9938 j = end - 1 - i;
9939 temp = LineOffset[j];
9940 while ((j -= line_count) >= row)
9941 {
9942 LineOffset[j + line_count] = LineOffset[j];
9943 LineWraps[j + line_count] = LineWraps[j];
9944 }
9945 LineOffset[j + line_count] = temp;
9946 LineWraps[j + line_count] = FALSE;
9947 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009948 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 else
9950 lineinvalid(temp, (int)Columns);
9951 }
9952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953
9954 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009955 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009956 if (clear_attr != 0)
9957 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 /* redraw the characters */
9960 if (type == USE_REDRAW)
9961 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009962 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 {
9964 term_append_lines(line_count);
9965 screen_start(); /* don't know where cursor is now */
9966 }
9967 else
9968 {
9969 for (i = 0; i < line_count; i++)
9970 {
9971 if (type == USE_T_AL)
9972 {
9973 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009974 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 out_str(T_AL);
9976 }
9977 else /* type == USE_T_SR */
9978 out_str(T_SR);
9979 screen_start(); /* don't know where cursor is now */
9980 }
9981 }
9982
9983 /*
9984 * With scroll-reverse and 'da' flag set we need to clear the lines that
9985 * have been scrolled down into the region.
9986 */
9987 if (type == USE_T_SR && *T_DA)
9988 {
9989 for (i = 0; i < line_count; ++i)
9990 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009991 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 out_str(T_CE);
9993 screen_start(); /* don't know where cursor is now */
9994 }
9995 }
9996
9997#ifdef FEAT_GUI
9998 gui_can_update_cursor();
9999 if (gui.in_use)
10000 out_flush(); /* always flush after a scroll */
10001#endif
10002 return OK;
10003}
10004
10005/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010006 * Delete lines on the screen and update ScreenLines[].
10007 * "end" is the line after the scrolled part. Normally it is Rows.
10008 * When scrolling region used "off" is the offset from the top for the region.
10009 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 *
10011 * Return OK for success, FAIL if the lines are not deleted.
10012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010014screen_del_lines(
10015 int off,
10016 int row,
10017 int line_count,
10018 int end,
10019 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010020 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010021 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
10023 int j;
10024 int i;
10025 unsigned temp;
10026 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010027 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 int cursor_end;
10029 int result_empty; /* result is empty until end of region */
10030 int can_delete; /* deleting line codes can be used */
10031 int type;
10032
10033 /*
10034 * FAIL if
10035 * - there is no valid screen
10036 * - the screen has to be redrawn completely
10037 * - the line count is less than one
10038 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010039 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010041 if (!screen_valid(TRUE) || line_count <= 0
10042 || (!force && line_count > p_ttyscroll)
10043#ifdef FEAT_CLIPBOARD
10044 || (clip_star.state != SELECT_CLEARED
10045 && redrawing_for_callback > 0)
10046#endif
10047 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 return FAIL;
10049
10050 /*
10051 * Check if the rest of the current region will become empty.
10052 */
10053 result_empty = row + line_count >= end;
10054
10055 /*
10056 * We can delete lines only when 'db' flag not set or when 'ce' option
10057 * available.
10058 */
10059 can_delete = (*T_DB == NUL || can_clear(T_CE));
10060
10061 /*
10062 * There are six ways to delete lines:
10063 * 0. When in a vertically split window and t_CV isn't set, redraw the
10064 * characters from ScreenLines[].
10065 * 1. Use T_CD if it exists and the result is empty.
10066 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10067 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10068 * none of the other ways work.
10069 * 4. Use T_CE (erase line) if the result is empty.
10070 * 5. Use T_DL (delete line) if it exists.
10071 * 6. redraw the characters from ScreenLines[].
10072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10074 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010075 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 type = USE_T_CD;
10077#if defined(__BEOS__) && defined(BEOS_DR8)
10078 /*
10079 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10080 * its internal termcap... this works okay for tests which test *T_DB !=
10081 * NUL. It has the disadvantage that the user cannot use any :set t_*
10082 * command to get T_DB (back) to empty_option, only :set term=... will do
10083 * the trick...
10084 * Anyway, this hack will hopefully go away with the next OS release.
10085 * (Olaf Seibert)
10086 */
10087 else if (row == 0 && T_DB == empty_option
10088 && (line_count == 1 || *T_CDL == NUL))
10089#else
10090 else if (row == 0 && (
10091#ifndef AMIGA
10092 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10093 * up, so use delete-line command */
10094 line_count == 1 ||
10095#endif
10096 *T_CDL == NUL))
10097#endif
10098 type = USE_NL;
10099 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10100 type = USE_T_CDL;
10101 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010102 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 type = USE_T_CE;
10104 else if (*T_DL != NUL && can_delete)
10105 type = USE_T_DL;
10106 else if (*T_CDL != NUL && can_delete)
10107 type = USE_T_CDL;
10108 else
10109 return FAIL;
10110
10111#ifdef FEAT_CLIPBOARD
10112 /* Remove a modeless selection when deleting lines halfway the screen or
10113 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010114 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010115 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 else
10117 clip_scroll_selection(line_count);
10118#endif
10119
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#ifdef FEAT_GUI
10121 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10122 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010123 gui_dont_update_cursor(gui.cursor_row >= row + off
10124 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125#endif
10126
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010127 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10128 cursor_col = wp->w_wincol;
10129
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 if (*T_CCS != NUL) /* cursor relative to region */
10131 {
10132 cursor_row = row;
10133 cursor_end = end;
10134 }
10135 else
10136 {
10137 cursor_row = row + off;
10138 cursor_end = end + off;
10139 }
10140
10141 /*
10142 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10143 * Clear the inserted lines in ScreenLines[].
10144 */
10145 row += off;
10146 end += off;
10147 for (i = 0; i < line_count; ++i)
10148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 if (wp != NULL && wp->w_width != Columns)
10150 {
10151 /* need to copy part of a line */
10152 j = row + i;
10153 while ((j += line_count) <= end - 1)
10154 linecopy(j - line_count, j, wp);
10155 j -= line_count;
10156 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010157 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10158 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 else
10160 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10161 LineWraps[j] = FALSE;
10162 }
10163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 {
10165 /* whole width, moving the line pointers is faster */
10166 j = row + i;
10167 temp = LineOffset[j];
10168 while ((j += line_count) <= end - 1)
10169 {
10170 LineOffset[j - line_count] = LineOffset[j];
10171 LineWraps[j - line_count] = LineWraps[j];
10172 }
10173 LineOffset[j - line_count] = temp;
10174 LineWraps[j - line_count] = FALSE;
10175 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010176 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 else
10178 lineinvalid(temp, (int)Columns);
10179 }
10180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010182 if (screen_attr != clear_attr)
10183 screen_stop_highlight();
10184 if (clear_attr != 0)
10185 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 /* redraw the characters */
10188 if (type == USE_REDRAW)
10189 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010190 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010192 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 out_str(T_CD);
10194 screen_start(); /* don't know where cursor is now */
10195 }
10196 else if (type == USE_T_CDL)
10197 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010198 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 term_delete_lines(line_count);
10200 screen_start(); /* don't know where cursor is now */
10201 }
10202 /*
10203 * Deleting lines at top of the screen or scroll region: Just scroll
10204 * the whole screen (scroll region) up by outputting newlines on the
10205 * last line.
10206 */
10207 else if (type == USE_NL)
10208 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010209 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 for (i = line_count; --i >= 0; )
10211 out_char('\n'); /* cursor will remain on same line */
10212 }
10213 else
10214 {
10215 for (i = line_count; --i >= 0; )
10216 {
10217 if (type == USE_T_DL)
10218 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010219 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 out_str(T_DL); /* delete a line */
10221 }
10222 else /* type == USE_T_CE */
10223 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010224 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 out_str(T_CE); /* erase a line */
10226 }
10227 screen_start(); /* don't know where cursor is now */
10228 }
10229 }
10230
10231 /*
10232 * If the 'db' flag is set, we need to clear the lines that have been
10233 * scrolled up at the bottom of the region.
10234 */
10235 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10236 {
10237 for (i = line_count; i > 0; --i)
10238 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010239 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 out_str(T_CE); /* erase a line */
10241 screen_start(); /* don't know where cursor is now */
10242 }
10243 }
10244
10245#ifdef FEAT_GUI
10246 gui_can_update_cursor();
10247 if (gui.in_use)
10248 out_flush(); /* always flush after a scroll */
10249#endif
10250
10251 return OK;
10252}
10253
10254/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010255 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 *
10257 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10258 * If clear_cmdline is FALSE there may be a message there that needs to be
10259 * cleared only if a mode is shown.
10260 * Return the length of the message (0 if no message).
10261 */
10262 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010263showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264{
10265 int need_clear;
10266 int length = 0;
10267 int do_mode;
10268 int attr;
10269 int nwr_save;
10270#ifdef FEAT_INS_EXPAND
10271 int sub_attr;
10272#endif
10273
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010274 do_mode = ((p_smd && msg_silent == 0)
10275 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010276 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010277 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010278 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 {
10280 /*
10281 * Don't show mode right now, when not redrawing or inside a mapping.
10282 * Call char_avail() only when we are going to show something, because
10283 * it takes a bit of time.
10284 */
10285 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10286 {
10287 redraw_cmdline = TRUE; /* show mode later */
10288 return 0;
10289 }
10290
10291 nwr_save = need_wait_return;
10292
10293 /* wait a bit before overwriting an important message */
10294 check_for_delay(FALSE);
10295
10296 /* if the cmdline is more than one line high, erase top lines */
10297 need_clear = clear_cmdline;
10298 if (clear_cmdline && cmdline_row < Rows - 1)
10299 msg_clr_cmdline(); /* will reset clear_cmdline */
10300
10301 /* Position on the last line in the window, column 0 */
10302 msg_pos_mode();
10303 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010304 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 if (do_mode)
10306 {
10307 MSG_PUTS_ATTR("--", attr);
10308#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010309 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010310# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010311 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010312# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010313 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010314# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010315 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010316# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 MSG_PUTS_ATTR(" IM", attr);
10318# else
10319 MSG_PUTS_ATTR(" XIM", attr);
10320# endif
10321#endif
10322#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10323 if (gui.in_use)
10324 {
10325 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010326 {
10327 /* HANGUL */
10328 if (enc_utf8)
10329 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10330 else
10331 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 }
10334#endif
10335#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010336 /* CTRL-X in Insert mode */
10337 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 {
10339 /* These messages can get long, avoid a wrap in a narrow
10340 * window. Prefer showing edit_submode_extra. */
10341 length = (Rows - msg_row) * Columns - 3;
10342 if (edit_submode_extra != NULL)
10343 length -= vim_strsize(edit_submode_extra);
10344 if (length > 0)
10345 {
10346 if (edit_submode_pre != NULL)
10347 length -= vim_strsize(edit_submode_pre);
10348 if (length - vim_strsize(edit_submode) > 0)
10349 {
10350 if (edit_submode_pre != NULL)
10351 msg_puts_attr(edit_submode_pre, attr);
10352 msg_puts_attr(edit_submode, attr);
10353 }
10354 if (edit_submode_extra != NULL)
10355 {
10356 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10357 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010358 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 else
10360 sub_attr = attr;
10361 msg_puts_attr(edit_submode_extra, sub_attr);
10362 }
10363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 }
10365 else
10366#endif
10367 {
10368#ifdef FEAT_VREPLACE
10369 if (State & VREPLACE_FLAG)
10370 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10371 else
10372#endif
10373 if (State & REPLACE_FLAG)
10374 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10375 else if (State & INSERT)
10376 {
10377#ifdef FEAT_RIGHTLEFT
10378 if (p_ri)
10379 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10380#endif
10381 MSG_PUTS_ATTR(_(" INSERT"), attr);
10382 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010383 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 MSG_PUTS_ATTR(_(" (insert)"), attr);
10385 else if (restart_edit == 'R')
10386 MSG_PUTS_ATTR(_(" (replace)"), attr);
10387 else if (restart_edit == 'V')
10388 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10389#ifdef FEAT_RIGHTLEFT
10390 if (p_hkmap)
10391 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10392# ifdef FEAT_FKMAP
10393 if (p_fkmap)
10394 MSG_PUTS_ATTR(farsi_text_5, attr);
10395# endif
10396#endif
10397#ifdef FEAT_KEYMAP
10398 if (State & LANGMAP)
10399 {
10400# ifdef FEAT_ARABIC
10401 if (curwin->w_p_arab)
10402 MSG_PUTS_ATTR(_(" Arabic"), attr);
10403 else
10404# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010405 if (get_keymap_str(curwin, (char_u *)" (%s)",
10406 NameBuff, MAXPATHL))
10407 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 }
10409#endif
10410 if ((State & INSERT) && p_paste)
10411 MSG_PUTS_ATTR(_(" (paste)"), attr);
10412
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 if (VIsual_active)
10414 {
10415 char *p;
10416
10417 /* Don't concatenate separate words to avoid translation
10418 * problems. */
10419 switch ((VIsual_select ? 4 : 0)
10420 + (VIsual_mode == Ctrl_V) * 2
10421 + (VIsual_mode == 'V'))
10422 {
10423 case 0: p = N_(" VISUAL"); break;
10424 case 1: p = N_(" VISUAL LINE"); break;
10425 case 2: p = N_(" VISUAL BLOCK"); break;
10426 case 4: p = N_(" SELECT"); break;
10427 case 5: p = N_(" SELECT LINE"); break;
10428 default: p = N_(" SELECT BLOCK"); break;
10429 }
10430 MSG_PUTS_ATTR(_(p), attr);
10431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 MSG_PUTS_ATTR(" --", attr);
10433 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010434
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 need_clear = TRUE;
10436 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010437 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438#ifdef FEAT_INS_EXPAND
10439 && edit_submode == NULL /* otherwise it gets too long */
10440#endif
10441 )
10442 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010443 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 need_clear = TRUE;
10445 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010446
10447 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 if (need_clear || clear_cmdline)
10449 msg_clr_eos();
10450 msg_didout = FALSE; /* overwrite this message */
10451 length = msg_col;
10452 msg_col = 0;
10453 need_wait_return = nwr_save; /* never ask for hit-return for this */
10454 }
10455 else if (clear_cmdline && msg_silent == 0)
10456 /* Clear the whole command line. Will reset "clear_cmdline". */
10457 msg_clr_cmdline();
10458
10459#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 /* In Visual mode the size of the selected area must be redrawn. */
10461 if (VIsual_active)
10462 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463
10464 /* If the last window has no status line, the ruler is after the mode
10465 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010466 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010467 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468#endif
10469 redraw_cmdline = FALSE;
10470 clear_cmdline = FALSE;
10471
10472 return length;
10473}
10474
10475/*
10476 * Position for a mode message.
10477 */
10478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010479msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480{
10481 msg_col = 0;
10482 msg_row = Rows - 1;
10483}
10484
10485/*
10486 * Delete mode message. Used when ESC is typed which is expected to end
10487 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010488 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 */
10490 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010491unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010494 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 */
10496 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10497 redraw_cmdline = TRUE; /* delete mode later */
10498 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010499 clearmode();
10500}
10501
10502/*
10503 * Clear the mode message.
10504 */
10505 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010506clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010507{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010508 int save_msg_row = msg_row;
10509 int save_msg_col = msg_col;
10510
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010511 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010512 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010513 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010514 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010515
10516 msg_col = save_msg_col;
10517 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518}
10519
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010520 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010521recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010522{
10523 MSG_PUTS_ATTR(_("recording"), attr);
10524 if (!shortmess(SHM_RECORDING))
10525 {
10526 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010527 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010528 MSG_PUTS_ATTR(s, attr);
10529 }
10530}
10531
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010532/*
10533 * Draw the tab pages line at the top of the Vim window.
10534 */
10535 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010536draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010537{
10538 int tabcount = 0;
10539 tabpage_T *tp;
10540 int tabwidth;
10541 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010542 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010543 int attr;
10544 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010545 win_T *cwp;
10546 int wincount;
10547 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010548 int c;
10549 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010550 int attr_sel = HL_ATTR(HLF_TPS);
10551 int attr_nosel = HL_ATTR(HLF_TP);
10552 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010553 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010554 int room;
10555 int use_sep_chars = (t_colors < 8
10556#ifdef FEAT_GUI
10557 && !gui.in_use
10558#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010559#ifdef FEAT_TERMGUICOLORS
10560 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010561#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010562 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010563
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010564 if (ScreenLines == NULL)
10565 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010566 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010567
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010568#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010569 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010570 if (gui_use_tabline())
10571 {
10572 gui_update_tabline();
10573 return;
10574 }
10575#endif
10576
10577 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010578 return;
10579
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010580#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010581
10582 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10583 for (scol = 0; scol < Columns; ++scol)
10584 TabPageIdxs[scol] = 0;
10585
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010586 /* Use the 'tabline' option if it's set. */
10587 if (*p_tal != NUL)
10588 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010589 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010590
10591 /* Check for an error. If there is one we would loop in redrawing the
10592 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010593 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010594 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010595 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010596 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010597 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010598 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010599 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010600 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010601#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010602 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010603 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010604 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010605
Bram Moolenaar238a5642006-02-21 22:12:05 +000010606 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10607 if (tabwidth < 6)
10608 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010609
Bram Moolenaar238a5642006-02-21 22:12:05 +000010610 attr = attr_nosel;
10611 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010612 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010613 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10614 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010615 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010616 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010617
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618 if (tp->tp_topframe == topframe)
10619 attr = attr_sel;
10620 if (use_sep_chars && col > 0)
10621 screen_putchar('|', 0, col++, attr);
10622
10623 if (tp->tp_topframe != topframe)
10624 attr = attr_nosel;
10625
10626 screen_putchar(' ', 0, col++, attr);
10627
10628 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010629 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 cwp = curwin;
10631 wp = firstwin;
10632 }
10633 else
10634 {
10635 cwp = tp->tp_curwin;
10636 wp = tp->tp_firstwin;
10637 }
10638
10639 modified = FALSE;
10640 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10641 if (bufIsChanged(wp->w_buffer))
10642 modified = TRUE;
10643 if (modified || wincount > 1)
10644 {
10645 if (wincount > 1)
10646 {
10647 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010648 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010649 if (col + len >= Columns - 3)
10650 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010651 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010652#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010653 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010654#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010655 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010656#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010657 );
10658 col += len;
10659 }
10660 if (modified)
10661 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10662 screen_putchar(' ', 0, col++, attr);
10663 }
10664
10665 room = scol - col + tabwidth - 1;
10666 if (room > 0)
10667 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010668 /* Get buffer name in NameBuff[] */
10669 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010670 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010671 len = vim_strsize(NameBuff);
10672 p = NameBuff;
10673#ifdef FEAT_MBYTE
10674 if (has_mbyte)
10675 while (len > room)
10676 {
10677 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010678 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010679 }
10680 else
10681#endif
10682 if (len > room)
10683 {
10684 p += len - room;
10685 len = room;
10686 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010687 if (len > Columns - col - 1)
10688 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010690 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010691 col += len;
10692 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010693 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694
10695 /* Store the tab page number in TabPageIdxs[], so that
10696 * jump_to_mouse() knows where each one is. */
10697 ++tabcount;
10698 while (scol < col)
10699 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010700 }
10701
Bram Moolenaar238a5642006-02-21 22:12:05 +000010702 if (use_sep_chars)
10703 c = '_';
10704 else
10705 c = ' ';
10706 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010707
10708 /* Put an "X" for closing the current tab if there are several. */
10709 if (first_tabpage->tp_next != NULL)
10710 {
10711 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10712 TabPageIdxs[Columns - 1] = -999;
10713 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010714 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010715
10716 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10717 * set. */
10718 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010719}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010720
10721/*
10722 * Get buffer name for "buf" into NameBuff[].
10723 * Takes care of special buffer names and translates special characters.
10724 */
10725 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010726get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010727{
10728 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010729 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010730 else
10731 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10732 trans_characters(NameBuff, MAXPATHL);
10733}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010734
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735/*
10736 * Get the character to use in a status line. Get its attributes in "*attr".
10737 */
10738 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010739fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740{
10741 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010742
10743#ifdef FEAT_TERMINAL
10744 if (bt_terminal(wp->w_buffer))
10745 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010746 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010747 {
10748 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010749 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010750 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010751 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010752 {
10753 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010754 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010755 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756 }
10757 else
10758#endif
10759 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010761 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 fill = fill_stl;
10763 }
10764 else
10765 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010766 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 fill = fill_stlnc;
10768 }
10769 /* Use fill when there is highlighting, and highlighting of current
10770 * window differs, or the fillchars differ, or this is not the
10771 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010772 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 || (fill_stl != fill_stlnc)))
10775 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010776 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 return '^';
10778 return '=';
10779}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781/*
10782 * Get the character to use in a separator between vertically split windows.
10783 * Get its attributes in "*attr".
10784 */
10785 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010786fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010788 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 if (*attr == 0 && fill_vert == ' ')
10790 return '|';
10791 else
10792 return fill_vert;
10793}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794
10795/*
10796 * Return TRUE if redrawing should currently be done.
10797 */
10798 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010799redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010801#ifdef FEAT_EVAL
10802 if (disable_redraw_for_testing)
10803 return 0;
10804 else
10805#endif
10806 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10808}
10809
10810/*
10811 * Return TRUE if printing messages should currently be done.
10812 */
10813 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010814messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815{
10816 return (!(p_lz && char_avail() && !KeyTyped));
10817}
10818
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010819#ifdef FEAT_MENU
10820/*
10821 * Draw the window toolbar.
10822 */
10823 static void
10824redraw_win_toolbar(win_T *wp)
10825{
10826 vimmenu_T *menu;
10827 int item_idx = 0;
10828 int item_count = 0;
10829 int col = 0;
10830 int next_col;
10831 int off = (int)(current_ScreenLine - ScreenLines);
10832 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10833 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10834
10835 vim_free(wp->w_winbar_items);
10836 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10837 ++item_count;
10838 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10839 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10840
10841 /* TODO: use fewer spaces if there is not enough room */
10842 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010843 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010844 {
10845 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010846 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010847 break;
10848 if (col > 1)
10849 {
10850 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010851 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010852 break;
10853 }
10854
10855 wp->w_winbar_items[item_idx].wb_startcol = col;
10856 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010857 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010858 break;
10859
10860 next_col = text_to_screenline(wp, menu->name, col);
10861 while (col < next_col)
10862 {
10863 ScreenAttrs[off + col] = button_attr;
10864 ++col;
10865 }
10866 wp->w_winbar_items[item_idx].wb_endcol = col;
10867 wp->w_winbar_items[item_idx].wb_menu = menu;
10868 ++item_idx;
10869
Bram Moolenaar02631462017-09-22 15:20:32 +020010870 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010871 break;
10872 space_to_screenline(off + col, button_attr);
10873 ++col;
10874 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010875 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010876 {
10877 space_to_screenline(off + col, fill_attr);
10878 ++col;
10879 }
10880 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10881
Bram Moolenaar02631462017-09-22 15:20:32 +020010882 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10883 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010884}
10885#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010886
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887/*
10888 * Show current status info in ruler and various other places
10889 * If always is FALSE, only show ruler if position has changed.
10890 */
10891 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010892showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
10894 if (!always && !redrawing())
10895 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010896#ifdef FEAT_INS_EXPAND
10897 if (pum_visible())
10898 {
10899 /* Don't redraw right now, do it later. */
10900 curwin->w_redr_status = TRUE;
10901 return;
10902 }
10903#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010904#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010905 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010906 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010907 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 else
10910#endif
10911#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010912 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913#endif
10914
10915#ifdef FEAT_TITLE
10916 if (need_maketitle
10917# ifdef FEAT_STL_OPT
10918 || (p_icon && (stl_syntax & STL_IN_ICON))
10919 || (p_title && (stl_syntax & STL_IN_TITLE))
10920# endif
10921 )
10922 maketitle();
10923#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010924 /* Redraw the tab pages line if needed. */
10925 if (redraw_tabline)
10926 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927}
10928
10929#ifdef FEAT_CMDL_INFO
10930 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010931win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010933#define RULER_BUF_LEN 70
10934 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 int row;
10936 int fillchar;
10937 int attr;
10938 int empty_line = FALSE;
10939 colnr_T virtcol;
10940 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010941 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 int this_ru_col;
10944 int off = 0;
10945 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946
10947 /* If 'ruler' off or redrawing disabled, don't do anything */
10948 if (!p_ru)
10949 return;
10950
10951 /*
10952 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10953 * after deleting lines, before cursor.lnum is corrected.
10954 */
10955 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10956 return;
10957
10958#ifdef FEAT_INS_EXPAND
10959 /* Don't draw the ruler while doing insert-completion, it might overwrite
10960 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 if (edit_submode != NULL)
10963 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010964 // Don't draw the ruler when the popup menu is visible, it may overlap.
10965 // Except when the popup menu will be redrawn anyway.
10966 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010967 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968#endif
10969
10970#ifdef FEAT_STL_OPT
10971 if (*p_ruf)
10972 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010973 int save_called_emsg = called_emsg;
10974
10975 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010977 if (called_emsg)
10978 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010979 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010980 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 return;
10982 }
10983#endif
10984
10985 /*
10986 * Check if not in Insert mode and the line is empty (will show "0-1").
10987 */
10988 if (!(State & INSERT)
10989 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10990 empty_line = TRUE;
10991
10992 /*
10993 * Only draw the ruler when something changed.
10994 */
10995 validate_virtcol_win(wp);
10996 if ( redraw_cmdline
10997 || always
10998 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10999 || wp->w_cursor.col != wp->w_ru_cursor.col
11000 || wp->w_virtcol != wp->w_ru_virtcol
11001#ifdef FEAT_VIRTUALEDIT
11002 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11003#endif
11004 || wp->w_topline != wp->w_ru_topline
11005 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11006#ifdef FEAT_DIFF
11007 || wp->w_topfill != wp->w_ru_topfill
11008#endif
11009 || empty_line != wp->w_ru_empty)
11010 {
11011 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 if (wp->w_status_height)
11013 {
11014 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011015 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011016 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011017 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 }
11019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 {
11021 row = Rows - 1;
11022 fillchar = ' ';
11023 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 width = Columns;
11025 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 }
11027
11028 /* In list mode virtcol needs to be recomputed */
11029 virtcol = wp->w_virtcol;
11030 if (wp->w_p_list && lcs_tab1 == NUL)
11031 {
11032 wp->w_p_list = FALSE;
11033 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11034 wp->w_p_list = TRUE;
11035 }
11036
11037 /*
11038 * Some sprintfs return the length, some return a pointer.
11039 * To avoid portability problems we use strlen() here.
11040 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011041 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11043 ? 0L
11044 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011045 len = STRLEN(buffer);
11046 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11048 (int)virtcol + 1);
11049
11050 /*
11051 * Add a "50%" if there is room for it.
11052 * On the last line, don't print in the last column (scrolls the
11053 * screen up on some terminals).
11054 */
11055 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011056 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 this_ru_col = ru_col - (Columns - width);
11061 if (this_ru_col < 0)
11062 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 /* Never use more than half the window/screen width, leave the other
11064 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011065 if (this_ru_col < (width + 1) / 2)
11066 this_ru_col = (width + 1) / 2;
11067 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011069 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011070 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 {
11072#ifdef FEAT_MBYTE
11073 if (has_mbyte)
11074 i += (*mb_char2bytes)(fillchar, buffer + i);
11075 else
11076#endif
11077 buffer[i++] = fillchar;
11078 ++o;
11079 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011080 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 }
11082 /* Truncate at window boundary. */
11083#ifdef FEAT_MBYTE
11084 if (has_mbyte)
11085 {
11086 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011087 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
11089 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011090 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092 buffer[i] = NUL;
11093 break;
11094 }
11095 }
11096 }
11097 else
11098#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011099 if (this_ru_col + (int)STRLEN(buffer) > width)
11100 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101
Bram Moolenaar4033c552017-09-16 20:54:51 +020011102 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 i = redraw_cmdline;
11104 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011105 this_ru_col + off + (int)STRLEN(buffer),
11106 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 fillchar, fillchar, attr);
11108 /* don't redraw the cmdline because of showing the ruler */
11109 redraw_cmdline = i;
11110 wp->w_ru_cursor = wp->w_cursor;
11111 wp->w_ru_virtcol = wp->w_virtcol;
11112 wp->w_ru_empty = empty_line;
11113 wp->w_ru_topline = wp->w_topline;
11114 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11115#ifdef FEAT_DIFF
11116 wp->w_ru_topfill = wp->w_topfill;
11117#endif
11118 }
11119}
11120#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011121
11122#if defined(FEAT_LINEBREAK) || defined(PROTO)
11123/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011124 * Return the width of the 'number' and 'relativenumber' column.
11125 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011126 * Otherwise it depends on 'numberwidth' and the line count.
11127 */
11128 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011129number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011130{
11131 int n;
11132 linenr_T lnum;
11133
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011134 if (wp->w_p_rnu && !wp->w_p_nu)
11135 /* cursor line shows "0" */
11136 lnum = wp->w_height;
11137 else
11138 /* cursor line shows absolute line number */
11139 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011140
Bram Moolenaar6b314672015-03-20 15:42:10 +010011141 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011142 return wp->w_nrwidth_width;
11143 wp->w_nrwidth_line_count = lnum;
11144
11145 n = 0;
11146 do
11147 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011148 lnum /= 10;
11149 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011150 } while (lnum > 0);
11151
11152 /* 'numberwidth' gives the minimal width plus one */
11153 if (n < wp->w_p_nuw - 1)
11154 n = wp->w_p_nuw - 1;
11155
11156 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011157 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011158 return n;
11159}
11160#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011161
11162/*
11163 * Return the current cursor column. This is the actual position on the
11164 * screen. First column is 0.
11165 */
11166 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011167screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011168{
11169 return screen_cur_col;
11170}
11171
11172/*
11173 * Return the current cursor row. This is the actual position on the screen.
11174 * First row is 0.
11175 */
11176 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011177screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011178{
11179 return screen_cur_row;
11180}