blob: c03443fecd4a69757efdbaf4547125056eb1dc6a [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 Moolenaara87b72c2018-06-25 21:24:51 +02004756#ifdef FEAT_VARTABS
4757 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
4758 wp->w_buffer->b_p_vts_array) - 1;
4759#else
Bram Moolenaara3650912014-11-19 13:21:57 +01004760 n_extra = (int)wp->w_buffer->b_p_ts
4761 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004762 #endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004763
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004764# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004765 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004766# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004768# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004769 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004770 {
4771#ifdef FEAT_CONCEAL
4772 if (c == TAB)
4773 /* See "Tab alignment" below. */
4774 FIX_FOR_BOGUSCOLS;
4775#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004776 if (!wp->w_p_list)
4777 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780#endif
4781
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004782 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4783 */
4784 if (wp->w_p_list
4785 && (((c == 160
4786#ifdef FEAT_MBYTE
4787 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4788#endif
4789 ) && lcs_nbsp)
4790 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4791 {
4792 c = (c == ' ') ? lcs_space : lcs_nbsp;
4793 if (area_attr == 0 && search_attr == 0)
4794 {
4795 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004796 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004797 saved_attr2 = char_attr; /* save current attr */
4798 }
4799#ifdef FEAT_MBYTE
4800 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004801 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004802 {
4803 mb_utf8 = TRUE;
4804 u8cc[0] = 0;
4805 c = 0xc0;
4806 }
4807 else
4808 mb_utf8 = FALSE;
4809#endif
4810 }
4811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4813 {
4814 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004815 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004818 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 saved_attr2 = char_attr; /* save current attr */
4820 }
4821#ifdef FEAT_MBYTE
4822 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004823 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
4825 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004826 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004827 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 else
4830 mb_utf8 = FALSE;
4831#endif
4832 }
4833 }
4834
4835 /*
4836 * Handling of non-printable characters.
4837 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004838 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 /*
4841 * when getting a character from the file, we may have to
4842 * turn it into something else on the way to putting it
4843 * into "ScreenLines".
4844 */
4845 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4846 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004847 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004848 long vcol_adjusted = vcol; /* removed showbreak length */
4849#ifdef FEAT_LINEBREAK
4850 /* only adjust the tab_len, when at the first column
4851 * after the showbreak value was drawn */
4852 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4853 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004856#ifdef FEAT_VARTABS
4857 tab_len = tabstop_padding(vcol_adjusted,
4858 wp->w_buffer->b_p_ts,
4859 wp->w_buffer->b_p_vts_array) - 1;
4860#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004861 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004862 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4863#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004864
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004865#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004866 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004867#endif
4868 /* tab amount depends on current column */
4869 n_extra = tab_len;
4870#ifdef FEAT_LINEBREAK
4871 else
4872 {
4873 char_u *p;
4874 int len = n_extra;
4875 int i;
4876 int saved_nextra = n_extra;
4877
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004878#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004879 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004880 /* there are characters to conceal */
4881 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004882 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4883 */
4884 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4885 && n_extra > tab_len)
4886 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004887#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004888
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004889 /* if n_extra > 0, it gives the number of chars, to
4890 * use for a tab, else we need to calculate the width
4891 * for a tab */
4892#ifdef FEAT_MBYTE
4893 len = (tab_len * mb_char2len(lcs_tab2));
4894 if (n_extra > 0)
4895 len += n_extra - tab_len;
4896#endif
4897 c = lcs_tab1;
4898 p = alloc((unsigned)(len + 1));
4899 vim_memset(p, ' ', len);
4900 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004901 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004902 p_extra_free = p;
4903 for (i = 0; i < tab_len; i++)
4904 {
4905#ifdef FEAT_MBYTE
4906 mb_char2bytes(lcs_tab2, p);
4907 p += mb_char2len(lcs_tab2);
4908 n_extra += mb_char2len(lcs_tab2)
4909 - (saved_nextra > 0 ? 1 : 0);
4910#else
4911 p[i] = lcs_tab2;
4912#endif
4913 }
4914 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004915#ifdef FEAT_CONCEAL
4916 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4917 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004918 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004919 n_extra -= vcol_off;
4920#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004921 }
4922#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004923#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004924 {
4925 int vc_saved = vcol_off;
4926
4927 /* Tab alignment should be identical regardless of
4928 * 'conceallevel' value. So tab compensates of all
4929 * previous concealed characters, and thus resets
4930 * vcol_off and boguscols accumulated so far in the
4931 * line. Note that the tab can be longer than
4932 * 'tabstop' when there are concealed characters. */
4933 FIX_FOR_BOGUSCOLS;
4934
4935 /* Make sure, the highlighting for the tab char will be
4936 * correctly set further below (effectively reverts the
4937 * FIX_FOR_BOGSUCOLS macro */
4938 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004939 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004940 tab_len += vc_saved;
4941 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943#ifdef FEAT_MBYTE
4944 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4945#endif
4946 if (wp->w_p_list)
4947 {
4948 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004949#ifdef FEAT_LINEBREAK
4950 if (wp->w_p_lbr)
4951 c_extra = NUL; /* using p_extra from above */
4952 else
4953#endif
4954 c_extra = lcs_tab2;
4955 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004956 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 saved_attr2 = char_attr; /* save current attr */
4958#ifdef FEAT_MBYTE
4959 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004960 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
4962 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004963 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004964 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966#endif
4967 }
4968 else
4969 {
4970 c_extra = ' ';
4971 c = ' ';
4972 }
4973 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004974 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004975 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004976 || ((fromcol >= 0 || fromcol_prev >= 0)
4977 && tocol > vcol
4978 && VIsual_mode != Ctrl_V
4979 && (
4980# ifdef FEAT_RIGHTLEFT
4981 wp->w_p_rl ? (col >= 0) :
4982# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004983 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004984 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00004985 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004986 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02004987 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004989 /* Display a '$' after the line or highlight an extra
4990 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#if defined(FEAT_DIFF) || defined(LINE_ATTR)
4992 /* For a diff line the highlighting continues after the
4993 * "$". */
4994 if (
4995# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004996 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997# ifdef LINE_ATTR
4998 &&
4999# endif
5000# endif
5001# ifdef LINE_ATTR
5002 line_attr == 0
5003# endif
5004 )
5005#endif
5006 {
5007#ifdef FEAT_VIRTUALEDIT
5008 /* In virtualedit, visual selections may extend
5009 * beyond end of line. */
5010 if (area_highlighting && virtual_active()
5011 && tocol != MAXCOL && vcol < tocol)
5012 n_extra = 0;
5013 else
5014#endif
5015 {
5016 p_extra = at_end_str;
5017 n_extra = 1;
5018 c_extra = NUL;
5019 }
5020 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005021 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005022 c = lcs_eol;
5023 else
5024 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 lcs_eol_one = -1;
5026 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005027 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005029 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 n_attr = 1;
5031 }
5032#ifdef FEAT_MBYTE
5033 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005034 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
5036 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005037 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005038 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040 else
5041 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5042#endif
5043 }
5044 else if (c != NUL)
5045 {
5046 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005047 if (n_extra == 0)
5048 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049#ifdef FEAT_RIGHTLEFT
5050 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5051 rl_mirror(p_extra); /* reverse "<12>" */
5052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005054#ifdef FEAT_LINEBREAK
5055 if (wp->w_p_lbr)
5056 {
5057 char_u *p;
5058
5059 c = *p_extra;
5060 p = alloc((unsigned)n_extra + 1);
5061 vim_memset(p, ' ', n_extra);
5062 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5063 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005064 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005065 p_extra_free = p_extra = p;
5066 }
5067 else
5068#endif
5069 {
5070 n_extra = byte2cells(c) - 1;
5071 c = *p_extra++;
5072 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005073 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005076 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 saved_attr2 = char_attr; /* save current attr */
5078 }
5079#ifdef FEAT_MBYTE
5080 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5081#endif
5082 }
5083#ifdef FEAT_VIRTUALEDIT
5084 else if (VIsual_active
5085 && (VIsual_mode == Ctrl_V
5086 || VIsual_mode == 'v')
5087 && virtual_active()
5088 && tocol != MAXCOL
5089 && vcol < tocol
5090 && (
5091# ifdef FEAT_RIGHTLEFT
5092 wp->w_p_rl ? (col >= 0) :
5093# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005094 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 c = ' ';
5097 --ptr; /* put it back at the NUL */
5098 }
5099#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005100#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 else if ((
5102# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005103 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005105# ifdef FEAT_TERMINAL
5106 term_attr != 0 ||
5107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 ) && (
5110# ifdef FEAT_RIGHTLEFT
5111 wp->w_p_rl ? (col >= 0) :
5112# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005113 (col
5114# ifdef FEAT_CONCEAL
5115 - boguscols
5116# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005117 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 /* Highlight until the right side of the window */
5120 c = ' ';
5121 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005122
5123 /* Remember we do the char for line highlighting. */
5124 ++did_line_attr;
5125
5126 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005127 if (line_attr != 0 && char_attr == search_attr
5128 && (did_line_attr > 1
5129 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005130 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131# ifdef FEAT_DIFF
5132 if (diff_hlf == HLF_TXD)
5133 {
5134 diff_hlf = HLF_CHD;
5135 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005136 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005137 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005138 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5139 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005140 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005144# ifdef FEAT_TERMINAL
5145 if (term_attr != 0)
5146 {
5147 char_attr = term_attr;
5148 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5149 char_attr = hl_combine_attr(char_attr,
5150 HL_ATTR(HLF_CUL));
5151 }
5152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154#endif
5155 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005156
5157#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005158 if ( wp->w_p_cole > 0
5159 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005160 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005161 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005162 && !(lnum_in_visual_area
5163 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005164 {
5165 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005166 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005167 && (syn_get_sub_char() != NUL || match_conc
5168 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005169 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005170 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005171 /* First time at this concealed item: display one
5172 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005173 if (match_conc)
5174 c = match_conc;
5175 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005176 c = syn_get_sub_char();
5177 else if (lcs_conceal != NUL)
5178 c = lcs_conceal;
5179 else
5180 c = ' ';
5181
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005182 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005183
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005184 if (n_extra > 0)
5185 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005186 vcol += n_extra;
5187 if (wp->w_p_wrap && n_extra > 0)
5188 {
5189# ifdef FEAT_RIGHTLEFT
5190 if (wp->w_p_rl)
5191 {
5192 col -= n_extra;
5193 boguscols -= n_extra;
5194 }
5195 else
5196# endif
5197 {
5198 boguscols += n_extra;
5199 col += n_extra;
5200 }
5201 }
5202 n_extra = 0;
5203 n_attr = 0;
5204 }
5205 else if (n_skip == 0)
5206 {
5207 is_concealing = TRUE;
5208 n_skip = 1;
5209 }
5210# ifdef FEAT_MBYTE
5211 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005212 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005213 {
5214 mb_utf8 = TRUE;
5215 u8cc[0] = 0;
5216 c = 0xc0;
5217 }
5218 else
5219 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5220# endif
5221 }
5222 else
5223 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005224 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005225 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005226 }
5227#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005230#ifdef FEAT_CONCEAL
5231 /* In the cursor line and we may be concealing characters: correct
5232 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005233 if (!did_wcol && draw_state == WL_LINE
5234 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005235 && conceal_cursor_line(wp)
5236 && (int)wp->w_virtcol <= vcol + n_skip)
5237 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005238# ifdef FEAT_RIGHTLEFT
5239 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005240 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005241 else
5242# endif
5243 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005244 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005245 did_wcol = TRUE;
5246 }
5247#endif
5248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 /* Don't override visual selection highlighting. */
5250 if (n_attr > 0
5251 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005252 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 char_attr = extra_attr;
5254
Bram Moolenaar81695252004-12-29 20:58:21 +00005255#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /* XIM don't send preedit_start and preedit_end, but they send
5257 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5258 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005259 if (p_imst == IM_ON_THE_SPOT
5260 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005261 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 && (State & INSERT)
5263 && !p_imdisable
5264 && im_is_preediting()
5265 && draw_state == WL_LINE)
5266 {
5267 colnr_T tcol;
5268
5269 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005270 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 else
5272 tcol = preedit_end_col;
5273 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5274 {
5275 if (feedback_old_attr < 0)
5276 {
5277 feedback_col = 0;
5278 feedback_old_attr = char_attr;
5279 }
5280 char_attr = im_get_feedback_attr(feedback_col);
5281 if (char_attr < 0)
5282 char_attr = feedback_old_attr;
5283 feedback_col++;
5284 }
5285 else if (feedback_old_attr >= 0)
5286 {
5287 char_attr = feedback_old_attr;
5288 feedback_old_attr = -1;
5289 feedback_col = 0;
5290 }
5291 }
5292#endif
5293 /*
5294 * Handle the case where we are in column 0 but not on the first
5295 * character of the line and the user wants us to show us a
5296 * special character (via 'listchars' option "precedes:<char>".
5297 */
5298 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005299 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5301#ifdef FEAT_DIFF
5302 && filler_todo <= 0
5303#endif
5304 && draw_state > WL_NR
5305 && c != NUL)
5306 {
5307 c = lcs_prec;
5308 lcs_prec_todo = NUL;
5309#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005310 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5311 {
5312 /* Double-width character being overwritten by the "precedes"
5313 * character, need to fill up half the character. */
5314 c_extra = MB_FILLER_CHAR;
5315 n_extra = 1;
5316 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005317 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005320 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005323 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005324 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
5326 else
5327 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5328#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005329 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005332 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 n_attr3 = 1;
5334 }
5335 }
5336
5337 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005338 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005340 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005341#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005342 || did_line_attr == 1
5343#endif
5344 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005346#ifdef FEAT_SEARCH_EXTRA
5347 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005348
5349 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005350 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005351 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005352#endif
5353
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005354 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 * highlight match at end of line. If it's beyond the last
5356 * char on the screen, just overwrite that one (tricky!) Not
5357 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005358#ifdef FEAT_SEARCH_EXTRA
5359 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005360 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005361 prevcol_hl_flag = TRUE;
5362 else
5363 {
5364 cur = wp->w_match_head;
5365 while (cur != NULL)
5366 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005367 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005368 {
5369 prevcol_hl_flag = TRUE;
5370 break;
5371 }
5372 cur = cur->next;
5373 }
5374 }
5375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005377 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005378 && (VIsual_mode != Ctrl_V
5379 || lnum == VIsual.lnum
5380 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005381 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382#ifdef FEAT_SEARCH_EXTRA
5383 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005384 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005385# ifdef FEAT_SYN_HL
5386 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5387 && !(wp == curwin && VIsual_active))
5388# endif
5389# ifdef FEAT_DIFF
5390 && diff_hlf == (hlf_T)0
5391# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005392# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005393 && did_line_attr <= 1
5394# endif
5395 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#endif
5397 ))
5398 {
5399 int n = 0;
5400
5401#ifdef FEAT_RIGHTLEFT
5402 if (wp->w_p_rl)
5403 {
5404 if (col < 0)
5405 n = 1;
5406 }
5407 else
5408#endif
5409 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005410 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 n = -1;
5412 }
5413 if (n != 0)
5414 {
5415 /* At the window boundary, highlight the last character
5416 * instead (better than nothing). */
5417 off += n;
5418 col += n;
5419 }
5420 else
5421 {
5422 /* Add a blank character to highlight. */
5423 ScreenLines[off] = ' ';
5424#ifdef FEAT_MBYTE
5425 if (enc_utf8)
5426 ScreenLinesUC[off] = 0;
5427#endif
5428 }
5429#ifdef FEAT_SEARCH_EXTRA
5430 if (area_attr == 0)
5431 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005432 /* Use attributes from match with highest priority among
5433 * 'search_hl' and the match list. */
5434 char_attr = search_hl.attr;
5435 cur = wp->w_match_head;
5436 shl_flag = FALSE;
5437 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005438 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005439 if (shl_flag == FALSE
5440 && ((cur != NULL
5441 && cur->priority > SEARCH_HL_PRIORITY)
5442 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005443 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005444 shl = &search_hl;
5445 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005446 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005447 else
5448 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005449 if ((ptr - line) - 1 == (long)shl->startcol
5450 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005451 char_attr = shl->attr;
5452 if (shl != &search_hl && cur != NULL)
5453 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456#endif
5457 ScreenAttrs[off] = char_attr;
5458#ifdef FEAT_RIGHTLEFT
5459 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005462 --off;
5463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 else
5465#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005468 ++off;
5469 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005470 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005471#ifdef FEAT_SYN_HL
5472 eol_hl_off = 1;
5473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar91170f82006-05-05 21:15:17 +00005477 /*
5478 * At end of the text line.
5479 */
5480 if (c == NUL)
5481 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005482#ifdef FEAT_SYN_HL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005483 if (eol_hl_off > 0 && vcol - eol_hl_off == (long)wp->w_virtcol
5484 && lnum == wp->w_cursor.lnum)
Bram Moolenaara443af82007-11-08 13:51:42 +00005485 {
5486 /* highlight last char after line */
5487 --col;
5488 --off;
5489 --vcol;
5490 }
5491
Bram Moolenaar1a384422010-07-14 19:53:30 +02005492 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005493 if (wp->w_p_wrap)
5494 v = wp->w_skipcol;
5495 else
5496 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005497
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005498 /* check if line ends before left margin */
5499 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005500 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005501#ifdef FEAT_CONCEAL
5502 /* Get rid of the boguscols now, we want to draw until the right
5503 * edge for 'cursorcolumn'. */
5504 col -= boguscols;
5505 boguscols = 0;
5506#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005507
5508 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005509 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005510
5511 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005512 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5513 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005514 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515 && lnum != wp->w_cursor.lnum)
5516 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005517# ifdef FEAT_RIGHTLEFT
5518 && !wp->w_p_rl
5519# endif
5520 )
5521 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005522 int rightmost_vcol = 0;
5523 int i;
5524
5525 if (wp->w_p_cuc)
5526 rightmost_vcol = wp->w_virtcol;
5527 if (draw_color_col)
5528 /* determine rightmost colorcolumn to possibly draw */
5529 for (i = 0; color_cols[i] >= 0; ++i)
5530 if (rightmost_vcol < color_cols[i])
5531 rightmost_vcol = color_cols[i];
5532
Bram Moolenaar02631462017-09-22 15:20:32 +02005533 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005534 {
5535 ScreenLines[off] = ' ';
5536#ifdef FEAT_MBYTE
5537 if (enc_utf8)
5538 ScreenLinesUC[off] = 0;
5539#endif
5540 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005541 if (draw_color_col)
5542 draw_color_col = advance_color_col(VCOL_HLC,
5543 &color_cols);
5544
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005545 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005546 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005547 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005548 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005549 else
5550 ScreenAttrs[off++] = 0;
5551
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005552 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005553 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005554
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005555 ++vcol;
5556 }
5557 }
5558#endif
5559
Bram Moolenaar53f81742017-09-22 14:35:51 +02005560 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005561 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 row++;
5563
5564 /*
5565 * Update w_cline_height and w_cline_folded if the cursor line was
5566 * updated (saves a call to plines() later).
5567 */
5568 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5569 {
5570 curwin->w_cline_row = startrow;
5571 curwin->w_cline_height = row - startrow;
5572#ifdef FEAT_FOLDING
5573 curwin->w_cline_folded = FALSE;
5574#endif
5575 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5576 }
5577
5578 break;
5579 }
5580
5581 /* line continues beyond line end */
5582 if (lcs_ext
5583 && !wp->w_p_wrap
5584#ifdef FEAT_DIFF
5585 && filler_todo <= 0
5586#endif
5587 && (
5588#ifdef FEAT_RIGHTLEFT
5589 wp->w_p_rl ? col == 0 :
5590#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005591 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005593 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5595 {
5596 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005597 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598#ifdef FEAT_MBYTE
5599 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005600 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
5602 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005603 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005604 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606 else
5607 mb_utf8 = FALSE;
5608#endif
5609 }
5610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005611#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 /* advance to the next 'colorcolumn' */
5613 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005614 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005615
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005616 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005617 * highlight the cursor position itself.
5618 * Also highlight the 'colorcolumn' if it is different than
5619 * 'cursorcolumn' */
5620 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005621 if (draw_state == WL_LINE && !lnum_in_visual_area
5622 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005623 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005624 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625 && lnum != wp->w_cursor.lnum)
5626 {
5627 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005628 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005629 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005630 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005631 {
5632 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005633 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005635 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636#endif
5637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 /*
5639 * Store character to be displayed.
5640 * Skip characters that are left of the screen for 'nowrap'.
5641 */
5642 vcol_prev = vcol;
5643 if (draw_state < WL_LINE || n_skip <= 0)
5644 {
5645 /*
5646 * Store the character.
5647 */
5648#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5649 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5650 {
5651 /* A double-wide character is: put first halve in left cell. */
5652 --off;
5653 --col;
5654 }
5655#endif
5656 ScreenLines[off] = c;
5657#ifdef FEAT_MBYTE
5658 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005659 {
5660 if ((mb_c & 0xff00) == 0x8e00)
5661 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 else if (enc_utf8)
5665 {
5666 if (mb_utf8)
5667 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005668 int i;
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005671 if ((c & 0xff) == 0)
5672 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005673 for (i = 0; i < Screen_mco; ++i)
5674 {
5675 ScreenLinesC[i][off] = u8cc[i];
5676 if (u8cc[i] == 0)
5677 break;
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else
5681 ScreenLinesUC[off] = 0;
5682 }
5683 if (multi_attr)
5684 {
5685 ScreenAttrs[off] = multi_attr;
5686 multi_attr = 0;
5687 }
5688 else
5689#endif
5690 ScreenAttrs[off] = char_attr;
5691
5692#ifdef FEAT_MBYTE
5693 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5694 {
5695 /* Need to fill two screen columns. */
5696 ++off;
5697 ++col;
5698 if (enc_utf8)
5699 /* UTF-8: Put a 0 in the second screen char. */
5700 ScreenLines[off] = 0;
5701 else
5702 /* DBCS: Put second byte in the second screen char. */
5703 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005704 if (draw_state > WL_NR
5705#ifdef FEAT_DIFF
5706 && filler_todo <= 0
5707#endif
5708 )
5709 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 /* When "tocol" is halfway a character, set it to the end of
5711 * the character, otherwise highlighting won't stop. */
5712 if (tocol == vcol)
5713 ++tocol;
5714#ifdef FEAT_RIGHTLEFT
5715 if (wp->w_p_rl)
5716 {
5717 /* now it's time to backup one cell */
5718 --off;
5719 --col;
5720 }
5721#endif
5722 }
5723#endif
5724#ifdef FEAT_RIGHTLEFT
5725 if (wp->w_p_rl)
5726 {
5727 --off;
5728 --col;
5729 }
5730 else
5731#endif
5732 {
5733 ++off;
5734 ++col;
5735 }
5736 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005737#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005738 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005739 {
5740 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005741 ++vcol_off;
5742 if (n_extra > 0)
5743 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005744 if (wp->w_p_wrap)
5745 {
5746 /*
5747 * Special voodoo required if 'wrap' is on.
5748 *
5749 * Advance the column indicator to force the line
5750 * drawing to wrap early. This will make the line
5751 * take up the same screen space when parts are concealed,
5752 * so that cursor line computations aren't messed up.
5753 *
5754 * To avoid the fictitious advance of 'col' causing
5755 * trailing junk to be written out of the screen line
5756 * we are building, 'boguscols' keeps track of the number
5757 * of bad columns we have advanced.
5758 */
5759 if (n_extra > 0)
5760 {
5761 vcol += n_extra;
5762# ifdef FEAT_RIGHTLEFT
5763 if (wp->w_p_rl)
5764 {
5765 col -= n_extra;
5766 boguscols -= n_extra;
5767 }
5768 else
5769# endif
5770 {
5771 col += n_extra;
5772 boguscols += n_extra;
5773 }
5774 n_extra = 0;
5775 n_attr = 0;
5776 }
5777
5778
5779# ifdef FEAT_MBYTE
5780 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5781 {
5782 /* Need to fill two screen columns. */
5783# ifdef FEAT_RIGHTLEFT
5784 if (wp->w_p_rl)
5785 {
5786 --boguscols;
5787 --col;
5788 }
5789 else
5790# endif
5791 {
5792 ++boguscols;
5793 ++col;
5794 }
5795 }
5796# endif
5797
5798# ifdef FEAT_RIGHTLEFT
5799 if (wp->w_p_rl)
5800 {
5801 --boguscols;
5802 --col;
5803 }
5804 else
5805# endif
5806 {
5807 ++boguscols;
5808 ++col;
5809 }
5810 }
5811 else
5812 {
5813 if (n_extra > 0)
5814 {
5815 vcol += n_extra;
5816 n_extra = 0;
5817 n_attr = 0;
5818 }
5819 }
5820
5821 }
5822#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 else
5824 --n_skip;
5825
Bram Moolenaar64486672010-05-16 15:46:46 +02005826 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5827 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005828 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#ifdef FEAT_DIFF
5830 && filler_todo <= 0
5831#endif
5832 )
5833 ++vcol;
5834
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005835#ifdef FEAT_SYN_HL
5836 if (vcol_save_attr >= 0)
5837 char_attr = vcol_save_attr;
5838#endif
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 /* restore attributes after "predeces" in 'listchars' */
5841 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5842 char_attr = saved_attr3;
5843
5844 /* restore attributes after last 'listchars' or 'number' char */
5845 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5846 char_attr = saved_attr2;
5847
5848 /*
5849 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005850 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 */
5852 if ((
5853#ifdef FEAT_RIGHTLEFT
5854 wp->w_p_rl ? (col < 0) :
5855#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005856 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 && (*ptr != NUL
5858#ifdef FEAT_DIFF
5859 || filler_todo > 0
5860#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005861 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5863 )
5864 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005865#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005866 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005867 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005868 boguscols = 0;
5869#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005870 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005871 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 ++row;
5874 ++screen_row;
5875
5876 /* When not wrapping and finished diff lines, or when displayed
5877 * '$' and highlighting until last column, break here. */
5878 if ((!wp->w_p_wrap
5879#ifdef FEAT_DIFF
5880 && filler_todo <= 0
5881#endif
5882 ) || lcs_eol_one == -1)
5883 break;
5884
5885 /* When the window is too narrow draw all "@" lines. */
5886 if (draw_state != WL_LINE
5887#ifdef FEAT_DIFF
5888 && filler_todo <= 0
5889#endif
5890 )
5891 {
5892 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 row = endrow;
5895 }
5896
5897 /* When line got too long for screen break here. */
5898 if (row == endrow)
5899 {
5900 ++row;
5901 break;
5902 }
5903
5904 if (screen_cur_row == screen_row - 1
5905#ifdef FEAT_DIFF
5906 && filler_todo <= 0
5907#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005908 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 /* Remember that the line wraps, used for modeless copy. */
5911 LineWraps[screen_row - 1] = TRUE;
5912
5913 /*
5914 * Special trick to make copy/paste of wrapped lines work with
5915 * xterm/screen: write an extra character beyond the end of
5916 * the line. This will work with all terminal types
5917 * (regardless of the xn,am settings).
5918 * Only do this on a fast tty.
5919 * Only do this if the cursor is on the current line
5920 * (something has been written in it).
5921 * Don't do this for the GUI.
5922 * Don't do this for double-width characters.
5923 * Don't do this for a window not at the right screen border.
5924 */
5925 if (p_tf
5926#ifdef FEAT_GUI
5927 && !gui.in_use
5928#endif
5929#ifdef FEAT_MBYTE
5930 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005931 && ((*mb_off2cells)(LineOffset[screen_row],
5932 LineOffset[screen_row] + screen_Columns)
5933 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005935 + (int)Columns - 2,
5936 LineOffset[screen_row] + screen_Columns)
5937 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938#endif
5939 )
5940 {
5941 /* First make sure we are at the end of the screen line,
5942 * then output the same character again to let the
5943 * terminal know about the wrap. If the terminal doesn't
5944 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005945 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 screen_char(LineOffset[screen_row - 1]
5947 + (unsigned)Columns - 1,
5948 screen_row - 1, (int)(Columns - 1));
5949
5950#ifdef FEAT_MBYTE
5951 /* When there is a multi-byte character, just output a
5952 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005953 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5954 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 out_char(' ');
5956 else
5957#endif
5958 out_char(ScreenLines[LineOffset[screen_row - 1]
5959 + (Columns - 1)]);
5960 /* force a redraw of the first char on the next line */
5961 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5962 screen_start(); /* don't know where cursor is now */
5963 }
5964 }
5965
5966 col = 0;
5967 off = (unsigned)(current_ScreenLine - ScreenLines);
5968#ifdef FEAT_RIGHTLEFT
5969 if (wp->w_p_rl)
5970 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005971 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 off += col;
5973 }
5974#endif
5975
5976 /* reset the drawing state for the start of a wrapped line */
5977 draw_state = WL_START;
5978 saved_n_extra = n_extra;
5979 saved_p_extra = p_extra;
5980 saved_c_extra = c_extra;
5981 saved_char_attr = char_attr;
5982 n_extra = 0;
5983 lcs_prec_todo = lcs_prec;
5984#ifdef FEAT_LINEBREAK
5985# ifdef FEAT_DIFF
5986 if (filler_todo <= 0)
5987# endif
5988 need_showbreak = TRUE;
5989#endif
5990#ifdef FEAT_DIFF
5991 --filler_todo;
5992 /* When the filler lines are actually below the last line of the
5993 * file, don't draw the line itself, break here. */
5994 if (filler_todo == 0 && wp->w_botfill)
5995 break;
5996#endif
5997 }
5998
5999 } /* for every character in the line */
6000
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006001#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006002 /* After an empty line check first word for capital. */
6003 if (*skipwhite(line) == NUL)
6004 {
6005 capcol_lnum = lnum + 1;
6006 cap_col = 0;
6007 }
6008#endif
6009
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006010 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 return row;
6012}
6013
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006014#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006015static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006016
6017/*
6018 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006019 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006020 */
6021 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006022comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006023{
6024 int i;
6025
6026 for (i = 0; i < Screen_mco; ++i)
6027 {
6028 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6029 return TRUE;
6030 if (ScreenLinesC[i][off_from] == 0)
6031 break;
6032 }
6033 return FALSE;
6034}
6035#endif
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037/*
6038 * Check whether the given character needs redrawing:
6039 * - the (first byte of the) character is different
6040 * - the attributes are different
6041 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006042 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 */
6044 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006045char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 if (cols > 0
6048 && ((ScreenLines[off_from] != ScreenLines[off_to]
6049 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6050
6051#ifdef FEAT_MBYTE
6052 || (enc_dbcs != 0
6053 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6054 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6055 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6056 : (cols > 1 && ScreenLines[off_from + 1]
6057 != ScreenLines[off_to + 1])))
6058 || (enc_utf8
6059 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6060 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006061 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006062 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6063 && ScreenLines[off_from + 1]
6064 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065#endif
6066 ))
6067 return TRUE;
6068 return FALSE;
6069}
6070
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006071#if defined(FEAT_TERMINAL) || defined(PROTO)
6072/*
6073 * Return the index in ScreenLines[] for the current screen line.
6074 */
6075 int
6076screen_get_current_line_off()
6077{
6078 return (int)(current_ScreenLine - ScreenLines);
6079}
6080#endif
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082/*
6083 * Move one "cooked" screen line to the screen, but only the characters that
6084 * have actually changed. Handle insert/delete character.
6085 * "coloff" gives the first column on the screen for this line.
6086 * "endcol" gives the columns where valid characters are.
6087 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6088 * needs to be cleared, negative otherwise.
6089 * "rlflag" is TRUE in a rightleft window:
6090 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6091 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6092 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006093 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006094screen_line(
6095 int row,
6096 int coloff,
6097 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006098 int clear_width,
6099 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 unsigned off_from;
6102 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006103#ifdef FEAT_MBYTE
6104 unsigned max_off_from;
6105 unsigned max_off_to;
6106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int force = FALSE; /* force update rest of the line */
6110 int redraw_this /* bool: does character need redraw? */
6111#ifdef FEAT_GUI
6112 = TRUE /* For GUI when while-loop empty */
6113#endif
6114 ;
6115 int redraw_next; /* redraw_this for next character */
6116#ifdef FEAT_MBYTE
6117 int clear_next = FALSE;
6118 int char_cells; /* 1: normal char */
6119 /* 2: occupies two display cells */
6120# define CHAR_CELLS char_cells
6121#else
6122# define CHAR_CELLS 1
6123#endif
6124
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006125 /* Check for illegal row and col, just in case. */
6126 if (row >= Rows)
6127 row = Rows - 1;
6128 if (endcol > Columns)
6129 endcol = Columns;
6130
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131# ifdef FEAT_CLIPBOARD
6132 clip_may_clear_selection(row, row);
6133# endif
6134
6135 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6136 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006137#ifdef FEAT_MBYTE
6138 max_off_from = off_from + screen_Columns;
6139 max_off_to = LineOffset[row] + screen_Columns;
6140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142#ifdef FEAT_RIGHTLEFT
6143 if (rlflag)
6144 {
6145 /* Clear rest first, because it's left of the text. */
6146 if (clear_width > 0)
6147 {
6148 while (col <= endcol && ScreenLines[off_to] == ' '
6149 && ScreenAttrs[off_to] == 0
6150# ifdef FEAT_MBYTE
6151 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6152# endif
6153 )
6154 {
6155 ++off_to;
6156 ++col;
6157 }
6158 if (col <= endcol)
6159 screen_fill(row, row + 1, col + coloff,
6160 endcol + coloff + 1, ' ', ' ', 0);
6161 }
6162 col = endcol + 1;
6163 off_to = LineOffset[row] + col + coloff;
6164 off_from += col;
6165 endcol = (clear_width > 0 ? clear_width : -clear_width);
6166 }
6167#endif /* FEAT_RIGHTLEFT */
6168
6169 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6170
6171 while (col < endcol)
6172 {
6173#ifdef FEAT_MBYTE
6174 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006175 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 else
6177 char_cells = 1;
6178#endif
6179
6180 redraw_this = redraw_next;
6181 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6182 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6183
6184#ifdef FEAT_GUI
6185 /* If the next character was bold, then redraw the current character to
6186 * remove any pixels that might have spilt over into us. This only
6187 * happens in the GUI.
6188 */
6189 if (redraw_next && gui.in_use)
6190 {
6191 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006192 if (hl > HL_ALL)
6193 hl = syn_attr2attr(hl);
6194 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 redraw_this = TRUE;
6196 }
6197#endif
6198
6199 if (redraw_this)
6200 {
6201 /*
6202 * Special handling when 'xs' termcap flag set (hpterm):
6203 * Attributes for characters are stored at the position where the
6204 * cursor is when writing the highlighting code. The
6205 * start-highlighting code must be written with the cursor on the
6206 * first highlighted character. The stop-highlighting code must
6207 * be written with the cursor just after the last highlighted
6208 * character.
6209 * Overwriting a character doesn't remove it's highlighting. Need
6210 * to clear the rest of the line, and force redrawing it
6211 * completely.
6212 */
6213 if ( p_wiv
6214 && !force
6215#ifdef FEAT_GUI
6216 && !gui.in_use
6217#endif
6218 && ScreenAttrs[off_to] != 0
6219 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6220 {
6221 /*
6222 * Need to remove highlighting attributes here.
6223 */
6224 windgoto(row, col + coloff);
6225 out_str(T_CE); /* clear rest of this screen line */
6226 screen_start(); /* don't know where cursor is now */
6227 force = TRUE; /* force redraw of rest of the line */
6228 redraw_next = TRUE; /* or else next char would miss out */
6229
6230 /*
6231 * If the previous character was highlighted, need to stop
6232 * highlighting at this character.
6233 */
6234 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6235 {
6236 screen_attr = ScreenAttrs[off_to - 1];
6237 term_windgoto(row, col + coloff);
6238 screen_stop_highlight();
6239 }
6240 else
6241 screen_attr = 0; /* highlighting has stopped */
6242 }
6243#ifdef FEAT_MBYTE
6244 if (enc_dbcs != 0)
6245 {
6246 /* Check if overwriting a double-byte with a single-byte or
6247 * the other way around requires another character to be
6248 * redrawn. For UTF-8 this isn't needed, because comparing
6249 * ScreenLinesUC[] is sufficient. */
6250 if (char_cells == 1
6251 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006252 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
6254 /* Writing a single-cell character over a double-cell
6255 * character: need to redraw the next cell. */
6256 ScreenLines[off_to + 1] = 0;
6257 redraw_next = TRUE;
6258 }
6259 else if (char_cells == 2
6260 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006261 && (*mb_off2cells)(off_to, max_off_to) == 1
6262 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
6264 /* Writing the second half of a double-cell character over
6265 * a double-cell character: need to redraw the second
6266 * cell. */
6267 ScreenLines[off_to + 2] = 0;
6268 redraw_next = TRUE;
6269 }
6270
6271 if (enc_dbcs == DBCS_JPNU)
6272 ScreenLines2[off_to] = ScreenLines2[off_from];
6273 }
6274 /* When writing a single-width character over a double-width
6275 * character and at the end of the redrawn text, need to clear out
6276 * the right halve of the old character.
6277 * Also required when writing the right halve of a double-width
6278 * char over the left halve of an existing one. */
6279 if (has_mbyte && col + char_cells == endcol
6280 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006281 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006283 && (*mb_off2cells)(off_to, max_off_to) == 1
6284 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 clear_next = TRUE;
6286#endif
6287
6288 ScreenLines[off_to] = ScreenLines[off_from];
6289#ifdef FEAT_MBYTE
6290 if (enc_utf8)
6291 {
6292 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6293 if (ScreenLinesUC[off_from] != 0)
6294 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006295 int i;
6296
6297 for (i = 0; i < Screen_mco; ++i)
6298 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
6300 }
6301 if (char_cells == 2)
6302 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6303#endif
6304
6305#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006306 /* The bold trick makes a single column of pixels appear in the
6307 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 * character should be redrawn too. This happens for our own GUI
6309 * and for some xterms. */
6310 if (
6311# ifdef FEAT_GUI
6312 gui.in_use
6313# endif
6314# if defined(FEAT_GUI) && defined(UNIX)
6315 ||
6316# endif
6317# ifdef UNIX
6318 term_is_xterm
6319# endif
6320 )
6321 {
6322 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006323 if (hl > HL_ALL)
6324 hl = syn_attr2attr(hl);
6325 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 redraw_next = TRUE;
6327 }
6328#endif
6329 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6330#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006331 /* For simplicity set the attributes of second half of a
6332 * double-wide character equal to the first half. */
6333 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006335
6336 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 else
6339#endif
6340 screen_char(off_to, row, col + coloff);
6341 }
6342 else if ( p_wiv
6343#ifdef FEAT_GUI
6344 && !gui.in_use
6345#endif
6346 && col + coloff > 0)
6347 {
6348 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6349 {
6350 /*
6351 * Don't output stop-highlight when moving the cursor, it will
6352 * stop the highlighting when it should continue.
6353 */
6354 screen_attr = 0;
6355 }
6356 else if (screen_attr != 0)
6357 screen_stop_highlight();
6358 }
6359
6360 off_to += CHAR_CELLS;
6361 off_from += CHAR_CELLS;
6362 col += CHAR_CELLS;
6363 }
6364
6365#ifdef FEAT_MBYTE
6366 if (clear_next)
6367 {
6368 /* Clear the second half of a double-wide character of which the left
6369 * half was overwritten with a single-wide character. */
6370 ScreenLines[off_to] = ' ';
6371 if (enc_utf8)
6372 ScreenLinesUC[off_to] = 0;
6373 screen_char(off_to, row, col + coloff);
6374 }
6375#endif
6376
6377 if (clear_width > 0
6378#ifdef FEAT_RIGHTLEFT
6379 && !rlflag
6380#endif
6381 )
6382 {
6383#ifdef FEAT_GUI
6384 int startCol = col;
6385#endif
6386
6387 /* blank out the rest of the line */
6388 while (col < clear_width && ScreenLines[off_to] == ' '
6389 && ScreenAttrs[off_to] == 0
6390#ifdef FEAT_MBYTE
6391 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6392#endif
6393 )
6394 {
6395 ++off_to;
6396 ++col;
6397 }
6398 if (col < clear_width)
6399 {
6400#ifdef FEAT_GUI
6401 /*
6402 * In the GUI, clearing the rest of the line may leave pixels
6403 * behind if the first character cleared was bold. Some bold
6404 * fonts spill over the left. In this case we redraw the previous
6405 * character too. If we didn't skip any blanks above, then we
6406 * only redraw if the character wasn't already redrawn anyway.
6407 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006408 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 {
6410 hl = ScreenAttrs[off_to];
6411 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006412 {
6413 int prev_cells = 1;
6414# ifdef FEAT_MBYTE
6415 if (enc_utf8)
6416 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6417 * that its width is 2. */
6418 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6419 else if (enc_dbcs != 0)
6420 {
6421 /* find previous character by counting from first
6422 * column and get its width. */
6423 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006424 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006425
6426 while (off < off_to)
6427 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006428 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006429 off += prev_cells;
6430 }
6431 }
6432
6433 if (enc_dbcs != 0 && prev_cells > 1)
6434 screen_char_2(off_to - prev_cells, row,
6435 col + coloff - prev_cells);
6436 else
6437# endif
6438 screen_char(off_to - prev_cells, row,
6439 col + coloff - prev_cells);
6440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 }
6442#endif
6443 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6444 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 off_to += clear_width - col;
6446 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
6448 }
6449
6450 if (clear_width > 0)
6451 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 /* For a window that's left of another, draw the separator char. */
6453 if (col + coloff < Columns)
6454 {
6455 int c;
6456
6457 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006458 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006459#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006460 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6461 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 || ScreenAttrs[off_to] != hl)
6464 {
6465 ScreenLines[off_to] = c;
6466 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006467#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (enc_utf8)
6469 {
6470 if (c >= 0x80)
6471 {
6472 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006473 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475 else
6476 ScreenLinesUC[off_to] = 0;
6477 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 screen_char(off_to, row, col + coloff);
6480 }
6481 }
6482 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 LineWraps[row] = FALSE;
6484 }
6485}
6486
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006487#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006489 * Mirror text "str" for right-left displaying.
6490 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006492 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006493rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 char_u *p1, *p2;
6496 int t;
6497
6498 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6499 {
6500 t = *p1;
6501 *p1 = *p2;
6502 *p2 = t;
6503 }
6504}
6505#endif
6506
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507/*
6508 * mark all status lines for redraw; used after first :cd
6509 */
6510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006511status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 win_T *wp;
6514
Bram Moolenaar29323592016-07-24 22:04:11 +02006515 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (wp->w_status_height)
6517 {
6518 wp->w_redr_status = TRUE;
6519 redraw_later(VALID);
6520 }
6521}
6522
6523/*
6524 * mark all status lines of the current buffer for redraw
6525 */
6526 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006527status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 win_T *wp;
6530
Bram Moolenaar29323592016-07-24 22:04:11 +02006531 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6533 {
6534 wp->w_redr_status = TRUE;
6535 redraw_later(VALID);
6536 }
6537}
6538
6539/*
6540 * Redraw all status lines that need to be redrawn.
6541 */
6542 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006543redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 win_T *wp;
6546
Bram Moolenaar29323592016-07-24 22:04:11 +02006547 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006549 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006550 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006551 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553
Bram Moolenaar4033c552017-09-16 20:54:51 +02006554#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Redraw all status lines at the bottom of frame "frp".
6557 */
6558 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006559win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 if (frp->fr_layout == FR_LEAF)
6562 frp->fr_win->w_redr_status = TRUE;
6563 else if (frp->fr_layout == FR_ROW)
6564 {
6565 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6566 win_redraw_last_status(frp);
6567 }
6568 else /* frp->fr_layout == FR_COL */
6569 {
6570 frp = frp->fr_child;
6571 while (frp->fr_next != NULL)
6572 frp = frp->fr_next;
6573 win_redraw_last_status(frp);
6574 }
6575}
6576#endif
6577
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578/*
6579 * Draw the verticap separator right of window "wp" starting with line "row".
6580 */
6581 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006582draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 int hl;
6585 int c;
6586
6587 if (wp->w_vsep_width)
6588 {
6589 /* draw the vertical separator right of this window */
6590 c = fillchar_vsep(&hl);
6591 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6592 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6593 c, ' ', hl);
6594 }
6595}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
6597#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006598static int status_match_len(expand_T *xp, char_u *s);
6599static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006602 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 */
6604 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006605status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 int len = 0;
6608
6609#ifdef FEAT_MENU
6610 int emenu = (xp->xp_context == EXPAND_MENUS
6611 || xp->xp_context == EXPAND_MENUNAMES);
6612
6613 /* Check for menu separators - replace with '|'. */
6614 if (emenu && menu_is_separator(s))
6615 return 1;
6616#endif
6617
6618 while (*s != NUL)
6619 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006620 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006621 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006622 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 }
6624
6625 return len;
6626}
6627
6628/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006629 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006630 * These are backslashes used for escaping. Do show backslashes in help tags.
6631 */
6632 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006633skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006634{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006635 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006636#ifdef FEAT_MENU
6637 || ((xp->xp_context == EXPAND_MENUS
6638 || xp->xp_context == EXPAND_MENUNAMES)
6639 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6640#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006641 )
6642 {
6643#ifndef BACKSLASH_IN_FILENAME
6644 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6645 return 2;
6646#endif
6647 return 1;
6648 }
6649 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006650}
6651
6652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 * Show wildchar matches in the status line.
6654 * Show at least the "match" item.
6655 * We start at item 'first_match' in the list and show all matches that fit.
6656 *
6657 * If inversion is possible we use it. Else '=' characters are used.
6658 */
6659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006660win_redr_status_matches(
6661 expand_T *xp,
6662 int num_matches,
6663 char_u **matches, /* list of matches */
6664 int match,
6665 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6668 int row;
6669 char_u *buf;
6670 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006671 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 int fillchar;
6673 int attr;
6674 int i;
6675 int highlight = TRUE;
6676 char_u *selstart = NULL;
6677 int selstart_col = 0;
6678 char_u *selend = NULL;
6679 static int first_match = 0;
6680 int add_left = FALSE;
6681 char_u *s;
6682#ifdef FEAT_MENU
6683 int emenu;
6684#endif
6685#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6686 int l;
6687#endif
6688
6689 if (matches == NULL) /* interrupted completion? */
6690 return;
6691
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006692#ifdef FEAT_MBYTE
6693 if (has_mbyte)
6694 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6695 else
6696#endif
6697 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (buf == NULL)
6699 return;
6700
6701 if (match == -1) /* don't show match but original text */
6702 {
6703 match = 0;
6704 highlight = FALSE;
6705 }
6706 /* count 1 for the ending ">" */
6707 clen = status_match_len(xp, L_MATCH(match)) + 3;
6708 if (match == 0)
6709 first_match = 0;
6710 else if (match < first_match)
6711 {
6712 /* jumping left, as far as we can go */
6713 first_match = match;
6714 add_left = TRUE;
6715 }
6716 else
6717 {
6718 /* check if match fits on the screen */
6719 for (i = first_match; i < match; ++i)
6720 clen += status_match_len(xp, L_MATCH(i)) + 2;
6721 if (first_match > 0)
6722 clen += 2;
6723 /* jumping right, put match at the left */
6724 if ((long)clen > Columns)
6725 {
6726 first_match = match;
6727 /* if showing the last match, we can add some on the left */
6728 clen = 2;
6729 for (i = match; i < num_matches; ++i)
6730 {
6731 clen += status_match_len(xp, L_MATCH(i)) + 2;
6732 if ((long)clen >= Columns)
6733 break;
6734 }
6735 if (i == num_matches)
6736 add_left = TRUE;
6737 }
6738 }
6739 if (add_left)
6740 while (first_match > 0)
6741 {
6742 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6743 if ((long)clen >= Columns)
6744 break;
6745 --first_match;
6746 }
6747
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006748 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
6750 if (first_match == 0)
6751 {
6752 *buf = NUL;
6753 len = 0;
6754 }
6755 else
6756 {
6757 STRCPY(buf, "< ");
6758 len = 2;
6759 }
6760 clen = len;
6761
6762 i = first_match;
6763 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6764 {
6765 if (i == match)
6766 {
6767 selstart = buf + len;
6768 selstart_col = clen;
6769 }
6770
6771 s = L_MATCH(i);
6772 /* Check for menu separators - replace with '|' */
6773#ifdef FEAT_MENU
6774 emenu = (xp->xp_context == EXPAND_MENUS
6775 || xp->xp_context == EXPAND_MENUNAMES);
6776 if (emenu && menu_is_separator(s))
6777 {
6778 STRCPY(buf + len, transchar('|'));
6779 l = (int)STRLEN(buf + len);
6780 len += l;
6781 clen += l;
6782 }
6783 else
6784#endif
6785 for ( ; *s != NUL; ++s)
6786 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006787 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 clen += ptr2cells(s);
6789#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006790 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 STRNCPY(buf + len, s, l);
6793 s += l - 1;
6794 len += l;
6795 }
6796 else
6797#endif
6798 {
6799 STRCPY(buf + len, transchar_byte(*s));
6800 len += (int)STRLEN(buf + len);
6801 }
6802 }
6803 if (i == match)
6804 selend = buf + len;
6805
6806 *(buf + len++) = ' ';
6807 *(buf + len++) = ' ';
6808 clen += 2;
6809 if (++i == num_matches)
6810 break;
6811 }
6812
6813 if (i != num_matches)
6814 {
6815 *(buf + len++) = '>';
6816 ++clen;
6817 }
6818
6819 buf[len] = NUL;
6820
6821 row = cmdline_row - 1;
6822 if (row >= 0)
6823 {
6824 if (wild_menu_showing == 0)
6825 {
6826 if (msg_scrolled > 0)
6827 {
6828 /* Put the wildmenu just above the command line. If there is
6829 * no room, scroll the screen one line up. */
6830 if (cmdline_row == Rows - 1)
6831 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006832 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 ++msg_scrolled;
6834 }
6835 else
6836 {
6837 ++cmdline_row;
6838 ++row;
6839 }
6840 wild_menu_showing = WM_SCROLLED;
6841 }
6842 else
6843 {
6844 /* Create status line if needed by setting 'laststatus' to 2.
6845 * Set 'winminheight' to zero to avoid that the window is
6846 * resized. */
6847 if (lastwin->w_status_height == 0)
6848 {
6849 save_p_ls = p_ls;
6850 save_p_wmh = p_wmh;
6851 p_ls = 2;
6852 p_wmh = 0;
6853 last_status(FALSE);
6854 }
6855 wild_menu_showing = WM_SHOWN;
6856 }
6857 }
6858
6859 screen_puts(buf, row, 0, attr);
6860 if (selstart != NULL && highlight)
6861 {
6862 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006863 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 }
6865
6866 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6867 }
6868
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 vim_free(buf);
6871}
6872#endif
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874/*
6875 * Redraw the status line of window wp.
6876 *
6877 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006878 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6879 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006881 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006882win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
6884 int row;
6885 char_u *p;
6886 int len;
6887 int fillchar;
6888 int attr;
6889 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006890 static int busy = FALSE;
6891
6892 /* It's possible to get here recursively when 'statusline' (indirectly)
6893 * invokes ":redrawstatus". Simply ignore the call then. */
6894 if (busy)
6895 return;
6896 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898 wp->w_redr_status = FALSE;
6899 if (wp->w_status_height == 0)
6900 {
6901 /* no status line, can only be last window */
6902 redraw_cmdline = TRUE;
6903 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006904 else if (!redrawing()
6905#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006906 // don't update status line when popup menu is visible and may be
6907 // drawn over it, unless it will be redrawn later
6908 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006909#endif
6910 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
6912 /* Don't redraw right now, do it later. */
6913 wp->w_redr_status = TRUE;
6914 }
6915#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006916 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
6918 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006919 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 }
6921#endif
6922 else
6923 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006924 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006926 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 p = NameBuff;
6928 len = (int)STRLEN(p);
6929
Bram Moolenaard85f2712017-07-28 21:51:57 +02006930 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931#ifdef FEAT_QUICKFIX
6932 || wp->w_p_pvw
6933#endif
6934 || bufIsChanged(wp->w_buffer)
6935 || wp->w_buffer->b_p_ro)
6936 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006937 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006939 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 len += (int)STRLEN(p + len);
6941 }
6942#ifdef FEAT_QUICKFIX
6943 if (wp->w_p_pvw)
6944 {
6945 STRCPY(p + len, _("[Preview]"));
6946 len += (int)STRLEN(p + len);
6947 }
6948#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006949 if (bufIsChanged(wp->w_buffer)
6950#ifdef FEAT_TERMINAL
6951 && !bt_terminal(wp->w_buffer)
6952#endif
6953 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {
6955 STRCPY(p + len, "[+]");
6956 len += 3;
6957 }
6958 if (wp->w_buffer->b_p_ro)
6959 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006960 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006961 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 }
6963
Bram Moolenaar02631462017-09-22 15:20:32 +02006964 this_ru_col = ru_col - (Columns - wp->w_width);
6965 if (this_ru_col < (wp->w_width + 1) / 2)
6966 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (this_ru_col <= 1)
6968 {
6969 p = (char_u *)"<"; /* No room for file name! */
6970 len = 1;
6971 }
6972 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#ifdef FEAT_MBYTE
6974 if (has_mbyte)
6975 {
6976 int clen = 0, i;
6977
6978 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006979 clen = mb_string2cells(p, -1);
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 /* Find first character that will fit.
6982 * Going from start to end is much faster for DBCS. */
6983 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006984 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 clen -= (*mb_ptr2cells)(p + i);
6986 len = clen;
6987 if (i > 0)
6988 {
6989 p = p + i - 1;
6990 *p = '<';
6991 ++len;
6992 }
6993
6994 }
6995 else
6996#endif
6997 if (len > this_ru_col - 1)
6998 {
6999 p += len - (this_ru_col - 1);
7000 *p = '<';
7001 len = this_ru_col - 1;
7002 }
7003
7004 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007005 screen_puts(p, row, wp->w_wincol, attr);
7006 screen_fill(row, row + 1, len + wp->w_wincol,
7007 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007009 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7011 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007012 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
7014#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007015 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016#endif
7017 }
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 /*
7020 * May need to draw the character below the vertical separator.
7021 */
7022 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7023 {
7024 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007025 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 else
7027 fillchar = fillchar_vsep(&attr);
7028 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7029 attr);
7030 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007031 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032}
7033
Bram Moolenaar238a5642006-02-21 22:12:05 +00007034#ifdef FEAT_STL_OPT
7035/*
7036 * Redraw the status line according to 'statusline' and take care of any
7037 * errors encountered.
7038 */
7039 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007040redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007041{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007042 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007043 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007044
7045 /* When called recursively return. This can happen when the statusline
7046 * contains an expression that triggers a redraw. */
7047 if (entered)
7048 return;
7049 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007050
Bram Moolenaara742e082016-04-05 21:10:38 +02007051 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007052 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007053 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007054 {
7055 /* When there is an error disable the statusline, otherwise the
7056 * display is messed up with errors and a redraw triggers the problem
7057 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007058 set_string_option_direct((char_u *)"statusline", -1,
7059 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007060 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007061 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007062 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007063 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007064}
7065#endif
7066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067/*
7068 * Return TRUE if the status line of window "wp" is connected to the status
7069 * line of the window right of it. If not, then it's a vertical separator.
7070 * Only call if (wp->w_vsep_width != 0).
7071 */
7072 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007073stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 frame_T *fr;
7076
7077 fr = wp->w_frame;
7078 while (fr->fr_parent != NULL)
7079 {
7080 if (fr->fr_parent->fr_layout == FR_COL)
7081 {
7082 if (fr->fr_next != NULL)
7083 break;
7084 }
7085 else
7086 {
7087 if (fr->fr_next != NULL)
7088 return TRUE;
7089 }
7090 fr = fr->fr_parent;
7091 }
7092 return FALSE;
7093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096/*
7097 * Get the value to show for the language mappings, active 'keymap'.
7098 */
7099 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007100get_keymap_str(
7101 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007102 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007103 char_u *buf, /* buffer for the result */
7104 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 char_u *p;
7107
7108 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7109 return FALSE;
7110
7111 {
7112#ifdef FEAT_EVAL
7113 buf_T *old_curbuf = curbuf;
7114 win_T *old_curwin = curwin;
7115 char_u *s;
7116
7117 curbuf = wp->w_buffer;
7118 curwin = wp;
7119 STRCPY(buf, "b:keymap_name"); /* must be writable */
7120 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007121 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 --emsg_skip;
7123 curbuf = old_curbuf;
7124 curwin = old_curwin;
7125 if (p == NULL || *p == NUL)
7126#endif
7127 {
7128#ifdef FEAT_KEYMAP
7129 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7130 p = wp->w_buffer->b_p_keymap;
7131 else
7132#endif
7133 p = (char_u *)"lang";
7134 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007135 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 buf[0] = NUL;
7137#ifdef FEAT_EVAL
7138 vim_free(s);
7139#endif
7140 }
7141 return buf[0] != NUL;
7142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
7144#if defined(FEAT_STL_OPT) || defined(PROTO)
7145/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007146 * Redraw the status line or ruler of window "wp".
7147 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 */
7149 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007150win_redr_custom(
7151 win_T *wp,
7152 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007154 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 int attr;
7156 int curattr;
7157 int row;
7158 int col = 0;
7159 int maxwidth;
7160 int width;
7161 int n;
7162 int len;
7163 int fillchar;
7164 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007165 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007167 struct stl_hlrec hltab[STL_MAX_ITEM];
7168 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007170 win_T *ewp;
7171 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar1d633412013-12-11 15:52:01 +01007173 /* There is a tiny chance that this gets called recursively: When
7174 * redrawing a status line triggers redrawing the ruler or tabline.
7175 * Avoid trouble by not allowing recursion. */
7176 if (entered)
7177 return;
7178 entered = TRUE;
7179
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007181 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007183 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007184 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007185 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007186 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007187 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007188 maxwidth = Columns;
7189# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007190 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 else
7194 {
7195 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007196 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007197 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007198
7199 if (draw_ruler)
7200 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007201 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007202 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007203 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007204 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007205 if (*++stl == '-')
7206 stl++;
7207 if (atoi((char *)stl))
7208 while (VIM_ISDIGIT(*stl))
7209 stl++;
7210 if (*stl++ != '(')
7211 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007213 col = ru_col - (Columns - wp->w_width);
7214 if (col < (wp->w_width + 1) / 2)
7215 col = (wp->w_width + 1) / 2;
7216 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007217 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007218 {
7219 row = Rows - 1;
7220 --maxwidth; /* writing in last column may cause scrolling */
7221 fillchar = ' ';
7222 attr = 0;
7223 }
7224
7225# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007226 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007227# endif
7228 }
7229 else
7230 {
7231 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007232 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007233 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007234 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007235# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 use_sandbox = was_set_insecurely((char_u *)"statusline",
7237 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007238# endif
7239 }
7240
Bram Moolenaar53f81742017-09-22 14:35:51 +02007241 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007242 }
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007245 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaar61452852011-02-01 18:01:11 +01007247 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7248 * the cursor away and back. */
7249 ewp = wp == NULL ? curwin : wp;
7250 p_crb_save = ewp->w_p_crb;
7251 ewp->w_p_crb = FALSE;
7252
Bram Moolenaar362f3562009-11-03 16:20:34 +00007253 /* Make a copy, because the statusline may include a function call that
7254 * might change the option value and free the memory. */
7255 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007256 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007257 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007258 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007259 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007260 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007262 /* Make all characters printable. */
7263 p = transstr(buf);
7264 if (p != NULL)
7265 {
7266 vim_strncpy(buf, p, sizeof(buf) - 1);
7267 vim_free(p);
7268 }
7269
7270 /* fill up with "fillchar" */
7271 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007272 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 {
7274#ifdef FEAT_MBYTE
7275 len += (*mb_char2bytes)(fillchar, buf + len);
7276#else
7277 buf[len++] = fillchar;
7278#endif
7279 ++width;
7280 }
7281 buf[len] = NUL;
7282
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007283 /*
7284 * Draw each snippet with the specified highlighting.
7285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 curattr = attr;
7287 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007290 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 screen_puts_len(p, len, row, col, curattr);
7292 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007293 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007295 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007297 else if (hltab[n].userhl < 0)
7298 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007299#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007300 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7301 && wp->w_status_height != 0)
7302 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007303 else if (wp != NULL && bt_terminal(wp->w_buffer)
7304 && wp->w_status_height != 0)
7305 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007306#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007307 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007308 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007310 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 }
7312 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007313
7314 if (wp == NULL)
7315 {
7316 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7317 col = 0;
7318 len = 0;
7319 p = buf;
7320 fillchar = 0;
7321 for (n = 0; tabtab[n].start != NULL; n++)
7322 {
7323 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7324 while (col < len)
7325 TabPageIdxs[col++] = fillchar;
7326 p = tabtab[n].start;
7327 fillchar = tabtab[n].userhl;
7328 }
7329 while (col < Columns)
7330 TabPageIdxs[col++] = fillchar;
7331 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007332
7333theend:
7334 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335}
7336
7337#endif /* FEAT_STL_OPT */
7338
7339/*
7340 * Output a single character directly to the screen and update ScreenLines.
7341 */
7342 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007343screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 char_u buf[MB_MAXBYTES + 1];
7346
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007347#ifdef FEAT_MBYTE
7348 if (has_mbyte)
7349 buf[(*mb_char2bytes)(c, buf)] = NUL;
7350 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007352 {
7353 buf[0] = c;
7354 buf[1] = NUL;
7355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 screen_puts(buf, row, col, attr);
7357}
7358
7359/*
7360 * Get a single character directly from ScreenLines into "bytes[]".
7361 * Also return its attribute in *attrp;
7362 */
7363 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007364screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366 unsigned off;
7367
7368 /* safety check */
7369 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7370 {
7371 off = LineOffset[row] + col;
7372 *attrp = ScreenAttrs[off];
7373 bytes[0] = ScreenLines[off];
7374 bytes[1] = NUL;
7375
7376#ifdef FEAT_MBYTE
7377 if (enc_utf8 && ScreenLinesUC[off] != 0)
7378 bytes[utfc_char2bytes(off, bytes)] = NUL;
7379 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7380 {
7381 bytes[0] = ScreenLines[off];
7382 bytes[1] = ScreenLines2[off];
7383 bytes[2] = NUL;
7384 }
7385 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7386 {
7387 bytes[1] = ScreenLines[off + 1];
7388 bytes[2] = NUL;
7389 }
7390#endif
7391 }
7392}
7393
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007394#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007395static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007396
7397/*
7398 * Return TRUE if composing characters for screen posn "off" differs from
7399 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007400 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007401 */
7402 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007403screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404{
7405 int i;
7406
7407 for (i = 0; i < Screen_mco; ++i)
7408 {
7409 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7410 return TRUE;
7411 if (u8cc[i] == 0)
7412 break;
7413 }
7414 return FALSE;
7415}
7416#endif
7417
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418/*
7419 * Put string '*text' on the screen at position 'row' and 'col', with
7420 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7421 * Note: only outputs within one row, message is truncated at screen boundary!
7422 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7423 */
7424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007425screen_puts(
7426 char_u *text,
7427 int row,
7428 int col,
7429 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
7431 screen_puts_len(text, -1, row, col, attr);
7432}
7433
7434/*
7435 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7436 * a NUL.
7437 */
7438 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007439screen_puts_len(
7440 char_u *text,
7441 int textlen,
7442 int row,
7443 int col,
7444 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 unsigned off;
7447 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007448 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 int c;
7450#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007451 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 int mbyte_blen = 1;
7453 int mbyte_cells = 1;
7454 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007455 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 int clear_next_cell = FALSE;
7457# ifdef FEAT_ARABIC
7458 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007459 int pc, nc, nc1;
7460 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461# endif
7462#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007463#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7464 int force_redraw_this;
7465 int force_redraw_next = FALSE;
7466#endif
7467 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
7469 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7470 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007471 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
Bram Moolenaarc236c162008-07-13 17:41:49 +00007473#ifdef FEAT_MBYTE
7474 /* When drawing over the right halve of a double-wide char clear out the
7475 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007476 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007477# ifdef FEAT_GUI
7478 && !gui.in_use
7479# endif
7480 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007481 {
7482 ScreenLines[off - 1] = ' ';
7483 ScreenAttrs[off - 1] = 0;
7484 if (enc_utf8)
7485 {
7486 ScreenLinesUC[off - 1] = 0;
7487 ScreenLinesC[0][off - 1] = 0;
7488 }
7489 /* redraw the previous cell, make it empty */
7490 screen_char(off - 1, row, col - 1);
7491 /* force the cell at "col" to be redrawn */
7492 force_redraw_next = TRUE;
7493 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007494#endif
7495
Bram Moolenaar367329b2007-08-30 11:53:22 +00007496#ifdef FEAT_MBYTE
7497 max_off = LineOffset[row] + screen_Columns;
7498#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007499 while (col < screen_Columns
7500 && (len < 0 || (int)(ptr - text) < len)
7501 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 {
7503 c = *ptr;
7504#ifdef FEAT_MBYTE
7505 /* check if this is the first byte of a multibyte */
7506 if (has_mbyte)
7507 {
7508 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007509 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007511 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7513 mbyte_cells = 1;
7514 else if (enc_dbcs != 0)
7515 mbyte_cells = mbyte_blen;
7516 else /* enc_utf8 */
7517 {
7518 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007519 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 (int)((text + len) - ptr));
7521 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007522 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007524# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 /* Non-BMP character: display as ? or fullwidth ?. */
7526 if (u8c >= 0x10000)
7527 {
7528 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7529 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007530 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533# ifdef FEAT_ARABIC
7534 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7535 {
7536 /* Do Arabic shaping. */
7537 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7538 {
7539 /* Past end of string to be displayed. */
7540 nc = NUL;
7541 nc1 = NUL;
7542 }
7543 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007545 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7546 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007547 nc1 = pcc[0];
7548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 pc = prev_c;
7550 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007551 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 }
7553 else
7554 prev_c = u8c;
7555# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007556 if (col + mbyte_cells > screen_Columns)
7557 {
7558 /* Only 1 cell left, but character requires 2 cells:
7559 * display a '>' in the last column to avoid wrapping. */
7560 c = '>';
7561 mbyte_cells = 1;
7562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 }
7564 }
7565#endif
7566
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007567#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7568 force_redraw_this = force_redraw_next;
7569 force_redraw_next = FALSE;
7570#endif
7571
7572 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573#ifdef FEAT_MBYTE
7574 || (mbyte_cells == 2
7575 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7576 || (enc_dbcs == DBCS_JPNU
7577 && c == 0x8e
7578 && ScreenLines2[off] != ptr[1])
7579 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007580 && (ScreenLinesUC[off] !=
7581 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7582 || (ScreenLinesUC[off] != 0
7583 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584#endif
7585 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007586 || exmode_active;
7587
7588 if (need_redraw
7589#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7590 || force_redraw_this
7591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 )
7593 {
7594#if defined(FEAT_GUI) || defined(UNIX)
7595 /* The bold trick makes a single row of pixels appear in the next
7596 * character. When a bold character is removed, the next
7597 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007598 * and for some xterms. */
7599 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600# ifdef FEAT_GUI
7601 gui.in_use
7602# endif
7603# if defined(FEAT_GUI) && defined(UNIX)
7604 ||
7605# endif
7606# ifdef UNIX
7607 term_is_xterm
7608# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007609 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007611 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007613 if (n > HL_ALL)
7614 n = syn_attr2attr(n);
7615 if (n & HL_BOLD)
7616 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618#endif
7619#ifdef FEAT_MBYTE
7620 /* When at the end of the text and overwriting a two-cell
7621 * character with a one-cell character, need to clear the next
7622 * cell. Also when overwriting the left halve of a two-cell char
7623 * with the right halve of a two-cell char. Do this only once
7624 * (mb_off2cells() may return 2 on the right halve). */
7625 if (clear_next_cell)
7626 clear_next_cell = FALSE;
7627 else if (has_mbyte
7628 && (len < 0 ? ptr[mbyte_blen] == NUL
7629 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007630 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007632 && (*mb_off2cells)(off, max_off) == 1
7633 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 clear_next_cell = TRUE;
7635
7636 /* Make sure we never leave a second byte of a double-byte behind,
7637 * it confuses mb_off2cells(). */
7638 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007639 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007641 && (*mb_off2cells)(off, max_off) == 1
7642 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 ScreenLines[off + mbyte_blen] = 0;
7644#endif
7645 ScreenLines[off] = c;
7646 ScreenAttrs[off] = attr;
7647#ifdef FEAT_MBYTE
7648 if (enc_utf8)
7649 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007650 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 ScreenLinesUC[off] = 0;
7652 else
7653 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007654 int i;
7655
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007657 for (i = 0; i < Screen_mco; ++i)
7658 {
7659 ScreenLinesC[i][off] = u8cc[i];
7660 if (u8cc[i] == 0)
7661 break;
7662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664 if (mbyte_cells == 2)
7665 {
7666 ScreenLines[off + 1] = 0;
7667 ScreenAttrs[off + 1] = attr;
7668 }
7669 screen_char(off, row, col);
7670 }
7671 else if (mbyte_cells == 2)
7672 {
7673 ScreenLines[off + 1] = ptr[1];
7674 ScreenAttrs[off + 1] = attr;
7675 screen_char_2(off, row, col);
7676 }
7677 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7678 {
7679 ScreenLines2[off] = ptr[1];
7680 screen_char(off, row, col);
7681 }
7682 else
7683#endif
7684 screen_char(off, row, col);
7685 }
7686#ifdef FEAT_MBYTE
7687 if (has_mbyte)
7688 {
7689 off += mbyte_cells;
7690 col += mbyte_cells;
7691 ptr += mbyte_blen;
7692 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007693 {
7694 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007696 len = -1;
7697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699 else
7700#endif
7701 {
7702 ++off;
7703 ++col;
7704 ++ptr;
7705 }
7706 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007707
7708#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7709 /* If we detected the next character needs to be redrawn, but the text
7710 * doesn't extend up to there, update the character here. */
7711 if (force_redraw_next && col < screen_Columns)
7712 {
7713# ifdef FEAT_MBYTE
7714 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7715 screen_char_2(off, row, col);
7716 else
7717# endif
7718 screen_char(off, row, col);
7719 }
7720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721}
7722
7723#ifdef FEAT_SEARCH_EXTRA
7724/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007725 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 */
7727 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007728start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 if (p_hls && !no_hlsearch)
7731 {
7732 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007733 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007734# ifdef FEAT_RELTIME
7735 /* Set the time limit to 'redrawtime'. */
7736 profile_setlimit(p_rdt, &search_hl.tm);
7737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 }
7739}
7740
7741/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007742 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 */
7744 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007745end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 if (search_hl.rm.regprog != NULL)
7748 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007749 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 search_hl.rm.regprog = NULL;
7751 }
7752}
7753
7754/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007755 * Init for calling prepare_search_hl().
7756 */
7757 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007758init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007759{
7760 matchitem_T *cur;
7761
7762 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7763 * match */
7764 cur = wp->w_match_head;
7765 while (cur != NULL)
7766 {
7767 cur->hl.rm = cur->match;
7768 if (cur->hlg_id == 0)
7769 cur->hl.attr = 0;
7770 else
7771 cur->hl.attr = syn_id2attr(cur->hlg_id);
7772 cur->hl.buf = wp->w_buffer;
7773 cur->hl.lnum = 0;
7774 cur->hl.first_lnum = 0;
7775# ifdef FEAT_RELTIME
7776 /* Set the time limit to 'redrawtime'. */
7777 profile_setlimit(p_rdt, &(cur->hl.tm));
7778# endif
7779 cur = cur->next;
7780 }
7781 search_hl.buf = wp->w_buffer;
7782 search_hl.lnum = 0;
7783 search_hl.first_lnum = 0;
7784 /* time limit is set at the toplevel, for all windows */
7785}
7786
7787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * Advance to the match in window "wp" line "lnum" or past it.
7789 */
7790 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007791prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007793 matchitem_T *cur; /* points to the match list */
7794 match_T *shl; /* points to search_hl or a match */
7795 int shl_flag; /* flag to indicate whether search_hl
7796 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007797 int pos_inprogress; /* marks that position match search is
7798 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 int n;
7800
7801 /*
7802 * When using a multi-line pattern, start searching at the top
7803 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007804 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007806 cur = wp->w_match_head;
7807 shl_flag = FALSE;
7808 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007810 if (shl_flag == FALSE)
7811 {
7812 shl = &search_hl;
7813 shl_flag = TRUE;
7814 }
7815 else
7816 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 if (shl->rm.regprog != NULL
7818 && shl->lnum == 0
7819 && re_multiline(shl->rm.regprog))
7820 {
7821 if (shl->first_lnum == 0)
7822 {
7823# ifdef FEAT_FOLDING
7824 for (shl->first_lnum = lnum;
7825 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7826 if (hasFoldingWin(wp, shl->first_lnum - 1,
7827 NULL, NULL, TRUE, NULL))
7828 break;
7829# else
7830 shl->first_lnum = wp->w_topline;
7831# endif
7832 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007833 if (cur != NULL)
7834 cur->pos.cur = 0;
7835 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007837 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7838 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007840 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7841 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007842 pos_inprogress = cur == NULL || cur->pos.cur == 0
7843 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 if (shl->lnum != 0)
7845 {
7846 shl->first_lnum = shl->lnum
7847 + shl->rm.endpos[0].lnum
7848 - shl->rm.startpos[0].lnum;
7849 n = shl->rm.endpos[0].col;
7850 }
7851 else
7852 {
7853 ++shl->first_lnum;
7854 n = 0;
7855 }
7856 }
7857 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007858 if (shl != &search_hl && cur != NULL)
7859 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 }
7861}
7862
7863/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007864 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 * Uses shl->buf.
7866 * Sets shl->lnum and shl->rm contents.
7867 * Note: Assumes a previous match is always before "lnum", unless
7868 * shl->lnum is zero.
7869 * Careful: Any pointers for buffer lines will become invalid.
7870 */
7871 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007872next_search_hl(
7873 win_T *win,
7874 match_T *shl, /* points to search_hl or a match */
7875 linenr_T lnum,
7876 colnr_T mincol, /* minimal column for a match */
7877 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
7879 linenr_T l;
7880 colnr_T matchcol;
7881 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007882 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883
7884 if (shl->lnum != 0)
7885 {
7886 /* Check for three situations:
7887 * 1. If the "lnum" is below a previous match, start a new search.
7888 * 2. If the previous match includes "mincol", use it.
7889 * 3. Continue after the previous match.
7890 */
7891 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7892 if (lnum > l)
7893 shl->lnum = 0;
7894 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7895 return;
7896 }
7897
7898 /*
7899 * Repeat searching for a match until one is found that includes "mincol"
7900 * or none is found in this line.
7901 */
7902 called_emsg = FALSE;
7903 for (;;)
7904 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007905#ifdef FEAT_RELTIME
7906 /* Stop searching after passing the time limit. */
7907 if (profile_passed_limit(&(shl->tm)))
7908 {
7909 shl->lnum = 0; /* no match found in time */
7910 break;
7911 }
7912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 /* Three situations:
7914 * 1. No useful previous match: search from start of line.
7915 * 2. Not Vi compatible or empty match: continue at next character.
7916 * Break the loop if this is beyond the end of the line.
7917 * 3. Vi compatible searching: continue at end of previous match.
7918 */
7919 if (shl->lnum == 0)
7920 matchcol = 0;
7921 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7922 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007923 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007925 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007926
7927 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007928 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007929 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007931 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 shl->lnum = 0;
7933 break;
7934 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007935#ifdef FEAT_MBYTE
7936 if (has_mbyte)
7937 matchcol += mb_ptr2len(ml);
7938 else
7939#endif
7940 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 }
7942 else
7943 matchcol = shl->rm.endpos[0].col;
7944
7945 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007946 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007948 /* Remember whether shl->rm is using a copy of the regprog in
7949 * cur->match. */
7950 int regprog_is_copy = (shl != &search_hl && cur != NULL
7951 && shl == &cur->hl
7952 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007953 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007954
Bram Moolenaarb3414592014-06-17 17:48:32 +02007955 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7956 matchcol,
7957#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007958 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007959#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007960 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961#endif
7962 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007963 /* Copy the regprog, in case it got freed and recompiled. */
7964 if (regprog_is_copy)
7965 cur->match.regprog = cur->hl.rm.regprog;
7966
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007967 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007968 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007969 /* Error while handling regexp: stop using this regexp. */
7970 if (shl == &search_hl)
7971 {
7972 /* don't free regprog in the match list, it's a copy */
7973 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007974 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007975 }
7976 shl->rm.regprog = NULL;
7977 shl->lnum = 0;
7978 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7979 message */
7980 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007981 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007982 }
7983 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02007985 else
7986 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (nmatched == 0)
7988 {
7989 shl->lnum = 0; /* no match found */
7990 break;
7991 }
7992 if (shl->rm.startpos[0].lnum > 0
7993 || shl->rm.startpos[0].col >= mincol
7994 || nmatched > 1
7995 || shl->rm.endpos[0].col > mincol)
7996 {
7997 shl->lnum += shl->rm.startpos[0].lnum;
7998 break; /* useful match found */
7999 }
8000 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008001
8002 // Restore called_emsg for assert_fails().
8003 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005
Bram Moolenaar85077472016-10-16 14:35:48 +02008006/*
8007 * If there is a match fill "shl" and return one.
8008 * Return zero otherwise.
8009 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008011next_search_hl_pos(
8012 match_T *shl, /* points to a match */
8013 linenr_T lnum,
8014 posmatch_T *posmatch, /* match positions */
8015 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008016{
8017 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008018 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008019
Bram Moolenaarb3414592014-06-17 17:48:32 +02008020 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8021 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008022 llpos_T *pos = &posmatch->pos[i];
8023
8024 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008025 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008026 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008028 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008029 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008030 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008032 /* if this match comes before the one at "found" then swap
8033 * them */
8034 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008035 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008036 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037
Bram Moolenaar85077472016-10-16 14:35:48 +02008038 *pos = posmatch->pos[found];
8039 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008040 }
8041 }
8042 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008043 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008044 }
8045 }
8046 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008047 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008048 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008049 colnr_T start = posmatch->pos[found].col == 0
8050 ? 0 : posmatch->pos[found].col - 1;
8051 colnr_T end = posmatch->pos[found].col == 0
8052 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008053
Bram Moolenaar85077472016-10-16 14:35:48 +02008054 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008055 shl->rm.startpos[0].lnum = 0;
8056 shl->rm.startpos[0].col = start;
8057 shl->rm.endpos[0].lnum = 0;
8058 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008059 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008060 posmatch->cur = found + 1;
8061 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008063 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008064}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008065#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008066
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008068screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 attrentry_T *aep = NULL;
8071
8072 screen_attr = attr;
8073 if (full_screen
8074#ifdef WIN3264
8075 && termcap_active
8076#endif
8077 )
8078 {
8079#ifdef FEAT_GUI
8080 if (gui.in_use)
8081 {
8082 char buf[20];
8083
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008084 /* The GUI handles this internally. */
8085 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 OUT_STR(buf);
8087 }
8088 else
8089#endif
8090 {
8091 if (attr > HL_ALL) /* special HL attr. */
8092 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008093 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 aep = syn_cterm_attr2entry(attr);
8095 else
8096 aep = syn_term_attr2entry(attr);
8097 if (aep == NULL) /* did ":syntax clear" */
8098 attr = 0;
8099 else
8100 attr = aep->ae_attr;
8101 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008102 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008104 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008105#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008106 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8107 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8108 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008109#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008110 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008111 /* If the Normal FG color has BOLD attribute and the new HL
8112 * has a FG color defined, clear BOLD. */
8113 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008114 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008116 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008117 out_str(T_UCS);
8118 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008119 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8120 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008122 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008124 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008126 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008127 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128
8129 /*
8130 * Output the color or start string after bold etc., in case the
8131 * bold etc. override the color setting.
8132 */
8133 if (aep != NULL)
8134 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008135#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008136 /* When 'termguicolors' is set but fg or bg is unset,
8137 * fall back to the cterm colors. This helps for SpellBad,
8138 * where the GUI uses a red undercurl. */
8139 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008141 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008142 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008143 }
8144 else
8145#endif
8146 if (t_colors > 1)
8147 {
8148 if (aep->ae_u.cterm.fg_color)
8149 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8150 }
8151#ifdef FEAT_TERMGUICOLORS
8152 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8153 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008154 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008155 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008158#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008159 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 if (aep->ae_u.cterm.bg_color)
8162 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8163 }
8164
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008165 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008166 {
8167 if (aep->ae_u.term.start != NULL)
8168 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 }
8170 }
8171 }
8172 }
8173}
8174
8175 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008176screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
8178 int do_ME = FALSE; /* output T_ME code */
8179
8180 if (screen_attr != 0
8181#ifdef WIN3264
8182 && termcap_active
8183#endif
8184 )
8185 {
8186#ifdef FEAT_GUI
8187 if (gui.in_use)
8188 {
8189 char buf[20];
8190
8191 /* use internal GUI code */
8192 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8193 OUT_STR(buf);
8194 }
8195 else
8196#endif
8197 {
8198 if (screen_attr > HL_ALL) /* special HL attr. */
8199 {
8200 attrentry_T *aep;
8201
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008202 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 /*
8205 * Assume that t_me restores the original colors!
8206 */
8207 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008208 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008209#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008210 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8211 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8212 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008213#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008214 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008215#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008216 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8217 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8218 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008219#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008220 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 do_ME = TRUE;
8222 }
8223 else
8224 {
8225 aep = syn_term_attr2entry(screen_attr);
8226 if (aep != NULL && aep->ae_u.term.stop != NULL)
8227 {
8228 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8229 do_ME = TRUE;
8230 else
8231 out_str(aep->ae_u.term.stop);
8232 }
8233 }
8234 if (aep == NULL) /* did ":syntax clear" */
8235 screen_attr = 0;
8236 else
8237 screen_attr = aep->ae_attr;
8238 }
8239
8240 /*
8241 * Often all ending-codes are equal to T_ME. Avoid outputting the
8242 * same sequence several times.
8243 */
8244 if (screen_attr & HL_STANDOUT)
8245 {
8246 if (STRCMP(T_SE, T_ME) == 0)
8247 do_ME = TRUE;
8248 else
8249 out_str(T_SE);
8250 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008251 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008252 {
8253 if (STRCMP(T_UCE, T_ME) == 0)
8254 do_ME = TRUE;
8255 else
8256 out_str(T_UCE);
8257 }
8258 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008259 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
8261 if (STRCMP(T_UE, T_ME) == 0)
8262 do_ME = TRUE;
8263 else
8264 out_str(T_UE);
8265 }
8266 if (screen_attr & HL_ITALIC)
8267 {
8268 if (STRCMP(T_CZR, T_ME) == 0)
8269 do_ME = TRUE;
8270 else
8271 out_str(T_CZR);
8272 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008273 if (screen_attr & HL_STRIKETHROUGH)
8274 {
8275 if (STRCMP(T_STE, T_ME) == 0)
8276 do_ME = TRUE;
8277 else
8278 out_str(T_STE);
8279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8281 out_str(T_ME);
8282
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008283#ifdef FEAT_TERMGUICOLORS
8284 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008286 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008287 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008288 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008289 term_bg_rgb_color(cterm_normal_bg_gui_color);
8290 }
8291 else
8292#endif
8293 {
8294 if (t_colors > 1)
8295 {
8296 /* set Normal cterm colors */
8297 if (cterm_normal_fg_color != 0)
8298 term_fg_color(cterm_normal_fg_color - 1);
8299 if (cterm_normal_bg_color != 0)
8300 term_bg_color(cterm_normal_bg_color - 1);
8301 if (cterm_normal_fg_bold)
8302 out_str(T_MD);
8303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305 }
8306 }
8307 screen_attr = 0;
8308}
8309
8310/*
8311 * Reset the colors for a cterm. Used when leaving Vim.
8312 * The machine specific code may override this again.
8313 */
8314 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008315reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008317 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
8319 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008320#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008321 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8322 || cterm_normal_bg_gui_color != INVALCOLOR)
8323 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008324#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
8328 out_str(T_OP);
8329 screen_attr = -1;
8330 }
8331 if (cterm_normal_fg_bold)
8332 {
8333 out_str(T_ME);
8334 screen_attr = -1;
8335 }
8336 }
8337}
8338
8339/*
8340 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8341 * using the attributes from ScreenAttrs["off"].
8342 */
8343 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008344screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 int attr;
8347
8348 /* Check for illegal values, just in case (could happen just after
8349 * resizing). */
8350 if (row >= screen_Rows || col >= screen_Columns)
8351 return;
8352
Bram Moolenaar494838a2015-02-10 19:20:37 +01008353 /* Outputting a character in the last cell on the screen may scroll the
8354 * screen up. Only do it when the "xn" termcap property is set, otherwise
8355 * mark the character invalid (update it when scrolled up). */
8356 if (*T_XN == NUL
8357 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358#ifdef FEAT_RIGHTLEFT
8359 /* account for first command-line character in rightleft mode */
8360 && !cmdmsg_rl
8361#endif
8362 )
8363 {
8364 ScreenAttrs[off] = (sattr_T)-1;
8365 return;
8366 }
8367
8368 /*
8369 * Stop highlighting first, so it's easier to move the cursor.
8370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 if (screen_char_attr != 0)
8372 attr = screen_char_attr;
8373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 attr = ScreenAttrs[off];
8375 if (screen_attr != attr)
8376 screen_stop_highlight();
8377
8378 windgoto(row, col);
8379
8380 if (screen_attr != attr)
8381 screen_start_highlight(attr);
8382
8383#ifdef FEAT_MBYTE
8384 if (enc_utf8 && ScreenLinesUC[off] != 0)
8385 {
8386 char_u buf[MB_MAXBYTES + 1];
8387
Bram Moolenaarcb070082016-04-02 22:14:51 +02008388 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008389 {
8390 if (*p_ambw == 'd'
8391# ifdef FEAT_GUI
8392 && !gui.in_use
8393# endif
8394 )
8395 {
8396 /* Clear the two screen cells. If the character is actually
8397 * single width it won't change the second cell. */
8398 out_str((char_u *)" ");
8399 term_windgoto(row, col);
8400 }
8401 /* not sure where the cursor is after drawing the ambiguous width
8402 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008403 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008404 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008405 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008407
8408 /* Convert the UTF-8 character to bytes and write it. */
8409 buf[utfc_char2bytes(off, buf)] = NUL;
8410 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 }
8412 else
8413#endif
8414 {
8415#ifdef FEAT_MBYTE
8416 out_flush_check();
8417#endif
8418 out_char(ScreenLines[off]);
8419#ifdef FEAT_MBYTE
8420 /* double-byte character in single-width cell */
8421 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8422 out_char(ScreenLines2[off]);
8423#endif
8424 }
8425
8426 screen_cur_col++;
8427}
8428
8429#ifdef FEAT_MBYTE
8430
8431/*
8432 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8433 * on the screen at position 'row' and 'col'.
8434 * The attributes of the first byte is used for all. This is required to
8435 * output the two bytes of a double-byte character with nothing in between.
8436 */
8437 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008438screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439{
8440 /* Check for illegal values (could be wrong when screen was resized). */
8441 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8442 return;
8443
8444 /* Outputting the last character on the screen may scrollup the screen.
8445 * Don't to it! Mark the character invalid (update it when scrolled up) */
8446 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8447 {
8448 ScreenAttrs[off] = (sattr_T)-1;
8449 return;
8450 }
8451
8452 /* Output the first byte normally (positions the cursor), then write the
8453 * second byte directly. */
8454 screen_char(off, row, col);
8455 out_char(ScreenLines[off + 1]);
8456 ++screen_cur_col;
8457}
8458#endif
8459
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460/*
8461 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8462 * This uses the contents of ScreenLines[] and doesn't change it.
8463 */
8464 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008465screen_draw_rectangle(
8466 int row,
8467 int col,
8468 int height,
8469 int width,
8470 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 int r, c;
8473 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008474#ifdef FEAT_MBYTE
8475 int max_off;
8476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008478 /* Can't use ScreenLines unless initialized */
8479 if (ScreenLines == NULL)
8480 return;
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (invert)
8483 screen_char_attr = HL_INVERSE;
8484 for (r = row; r < row + height; ++r)
8485 {
8486 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008487#ifdef FEAT_MBYTE
8488 max_off = off + screen_Columns;
8489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 for (c = col; c < col + width; ++c)
8491 {
8492#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008493 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {
8495 screen_char_2(off + c, r, c);
8496 ++c;
8497 }
8498 else
8499#endif
8500 {
8501 screen_char(off + c, r, c);
8502#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008503 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 ++c;
8505#endif
8506 }
8507 }
8508 }
8509 screen_char_attr = 0;
8510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512/*
8513 * Redraw the characters for a vertically split window.
8514 */
8515 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008516redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 int col;
8519 int width;
8520
8521# ifdef FEAT_CLIPBOARD
8522 clip_may_clear_selection(row, end - 1);
8523# endif
8524
8525 if (wp == NULL)
8526 {
8527 col = 0;
8528 width = Columns;
8529 }
8530 else
8531 {
8532 col = wp->w_wincol;
8533 width = wp->w_width;
8534 }
8535 screen_draw_rectangle(row, col, end - row, width, FALSE);
8536}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008538 static void
8539space_to_screenline(int off, int attr)
8540{
8541 ScreenLines[off] = ' ';
8542 ScreenAttrs[off] = attr;
8543# ifdef FEAT_MBYTE
8544 if (enc_utf8)
8545 ScreenLinesUC[off] = 0;
8546# endif
8547}
8548
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549/*
8550 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8551 * with character 'c1' in first column followed by 'c2' in the other columns.
8552 * Use attributes 'attr'.
8553 */
8554 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008555screen_fill(
8556 int start_row,
8557 int end_row,
8558 int start_col,
8559 int end_col,
8560 int c1,
8561 int c2,
8562 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 int row;
8565 int col;
8566 int off;
8567 int end_off;
8568 int did_delete;
8569 int c;
8570 int norm_term;
8571#if defined(FEAT_GUI) || defined(UNIX)
8572 int force_next = FALSE;
8573#endif
8574
8575 if (end_row > screen_Rows) /* safety check */
8576 end_row = screen_Rows;
8577 if (end_col > screen_Columns) /* safety check */
8578 end_col = screen_Columns;
8579 if (ScreenLines == NULL
8580 || start_row >= end_row
8581 || start_col >= end_col) /* nothing to do */
8582 return;
8583
8584 /* it's a "normal" terminal when not in a GUI or cterm */
8585 norm_term = (
8586#ifdef FEAT_GUI
8587 !gui.in_use &&
8588#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008589 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 for (row = start_row; row < end_row; ++row)
8591 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008592#ifdef FEAT_MBYTE
8593 if (has_mbyte
8594# ifdef FEAT_GUI
8595 && !gui.in_use
8596# endif
8597 )
8598 {
8599 /* When drawing over the right halve of a double-wide char clear
8600 * out the left halve. When drawing over the left halve of a
8601 * double wide-char clear out the right halve. Only needed in a
8602 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008603 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008604 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008605 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008606 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008607 }
8608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 /*
8610 * Try to use delete-line termcap code, when no attributes or in a
8611 * "normal" terminal, where a bold/italic space is just a
8612 * space.
8613 */
8614 did_delete = FALSE;
8615 if (c2 == ' '
8616 && end_col == Columns
8617 && can_clear(T_CE)
8618 && (attr == 0
8619 || (norm_term
8620 && attr <= HL_ALL
8621 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8622 {
8623 /*
8624 * check if we really need to clear something
8625 */
8626 col = start_col;
8627 if (c1 != ' ') /* don't clear first char */
8628 ++col;
8629
8630 off = LineOffset[row] + col;
8631 end_off = LineOffset[row] + end_col;
8632
8633 /* skip blanks (used often, keep it fast!) */
8634#ifdef FEAT_MBYTE
8635 if (enc_utf8)
8636 while (off < end_off && ScreenLines[off] == ' '
8637 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8638 ++off;
8639 else
8640#endif
8641 while (off < end_off && ScreenLines[off] == ' '
8642 && ScreenAttrs[off] == 0)
8643 ++off;
8644 if (off < end_off) /* something to be cleared */
8645 {
8646 col = off - LineOffset[row];
8647 screen_stop_highlight();
8648 term_windgoto(row, col);/* clear rest of this screen line */
8649 out_str(T_CE);
8650 screen_start(); /* don't know where cursor is now */
8651 col = end_col - col;
8652 while (col--) /* clear chars in ScreenLines */
8653 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008654 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 ++off;
8656 }
8657 }
8658 did_delete = TRUE; /* the chars are cleared now */
8659 }
8660
8661 off = LineOffset[row] + start_col;
8662 c = c1;
8663 for (col = start_col; col < end_col; ++col)
8664 {
8665 if (ScreenLines[off] != c
8666#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008667 || (enc_utf8 && (int)ScreenLinesUC[off]
8668 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669#endif
8670 || ScreenAttrs[off] != attr
8671#if defined(FEAT_GUI) || defined(UNIX)
8672 || force_next
8673#endif
8674 )
8675 {
8676#if defined(FEAT_GUI) || defined(UNIX)
8677 /* The bold trick may make a single row of pixels appear in
8678 * the next character. When a bold character is removed, the
8679 * next character should be redrawn too. This happens for our
8680 * own GUI and for some xterms. */
8681 if (
8682# ifdef FEAT_GUI
8683 gui.in_use
8684# endif
8685# if defined(FEAT_GUI) && defined(UNIX)
8686 ||
8687# endif
8688# ifdef UNIX
8689 term_is_xterm
8690# endif
8691 )
8692 {
8693 if (ScreenLines[off] != ' '
8694 && (ScreenAttrs[off] > HL_ALL
8695 || ScreenAttrs[off] & HL_BOLD))
8696 force_next = TRUE;
8697 else
8698 force_next = FALSE;
8699 }
8700#endif
8701 ScreenLines[off] = c;
8702#ifdef FEAT_MBYTE
8703 if (enc_utf8)
8704 {
8705 if (c >= 0x80)
8706 {
8707 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008708 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710 else
8711 ScreenLinesUC[off] = 0;
8712 }
8713#endif
8714 ScreenAttrs[off] = attr;
8715 if (!did_delete || c != ' ')
8716 screen_char(off, row, col);
8717 }
8718 ++off;
8719 if (col == start_col)
8720 {
8721 if (did_delete)
8722 break;
8723 c = c2;
8724 }
8725 }
8726 if (end_col == Columns)
8727 LineWraps[row] = FALSE;
8728 if (row == Rows - 1) /* overwritten the command line */
8729 {
8730 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008731 if (start_col == 0 && end_col == Columns
8732 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008734 if (start_col == 0)
8735 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 }
8737 }
8738}
8739
8740/*
8741 * Check if there should be a delay. Used before clearing or redrawing the
8742 * screen or the command line.
8743 */
8744 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008745check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746{
8747 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8748 && !did_wait_return
8749 && emsg_silent == 0)
8750 {
8751 out_flush();
8752 ui_delay(1000L, TRUE);
8753 emsg_on_display = FALSE;
8754 if (check_msg_scroll)
8755 msg_scroll = FALSE;
8756 }
8757}
8758
8759/*
8760 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008761 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 * Returns TRUE if there is a valid screen to write to.
8763 * Returns FALSE when starting up and screen not initialized yet.
8764 */
8765 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008766screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008768 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 return (ScreenLines != NULL);
8770}
8771
8772/*
8773 * Resize the shell to Rows and Columns.
8774 * Allocate ScreenLines[] and associated items.
8775 *
8776 * There may be some time between setting Rows and Columns and (re)allocating
8777 * ScreenLines[]. This happens when starting up and when (manually) changing
8778 * the shell size. Always use screen_Rows and screen_Columns to access items
8779 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8780 * final size of the shell is needed.
8781 */
8782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008783screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 int new_row, old_row;
8786#ifdef FEAT_GUI
8787 int old_Rows;
8788#endif
8789 win_T *wp;
8790 int outofmem = FALSE;
8791 int len;
8792 schar_T *new_ScreenLines;
8793#ifdef FEAT_MBYTE
8794 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008795 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008797 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#endif
8799 sattr_T *new_ScreenAttrs;
8800 unsigned *new_LineOffset;
8801 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008802 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008803 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008805 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008806 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008808retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 /*
8810 * Allocation of the screen buffers is done only when the size changes and
8811 * when Rows and Columns have been set and we have started doing full
8812 * screen stuff.
8813 */
8814 if ((ScreenLines != NULL
8815 && Rows == screen_Rows
8816 && Columns == screen_Columns
8817#ifdef FEAT_MBYTE
8818 && enc_utf8 == (ScreenLinesUC != NULL)
8819 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008820 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821#endif
8822 )
8823 || Rows == 0
8824 || Columns == 0
8825 || (!full_screen && ScreenLines == NULL))
8826 return;
8827
8828 /*
8829 * It's possible that we produce an out-of-memory message below, which
8830 * will cause this function to be called again. To break the loop, just
8831 * return here.
8832 */
8833 if (entered)
8834 return;
8835 entered = TRUE;
8836
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008837 /*
8838 * Note that the window sizes are updated before reallocating the arrays,
8839 * thus we must not redraw here!
8840 */
8841 ++RedrawingDisabled;
8842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 win_new_shellsize(); /* fit the windows in the new sized shell */
8844
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 comp_col(); /* recompute columns for shown command and ruler */
8846
8847 /*
8848 * We're changing the size of the screen.
8849 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8850 * - Move lines from the old arrays into the new arrays, clear extra
8851 * lines (unless the screen is going to be cleared).
8852 * - Free the old arrays.
8853 *
8854 * If anything fails, make ScreenLines NULL, so we don't do anything!
8855 * Continuing with the old ScreenLines may result in a crash, because the
8856 * size is wrong.
8857 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008858 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008860 if (aucmd_win != NULL)
8861 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863 new_ScreenLines = (schar_T *)lalloc((long_u)(
8864 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8865#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008866 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 if (enc_utf8)
8868 {
8869 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8870 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008871 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008872 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8874 }
8875 if (enc_dbcs == DBCS_JPNU)
8876 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8877 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8878#endif
8879 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8880 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8881 new_LineOffset = (unsigned *)lalloc((long_u)(
8882 Rows * sizeof(unsigned)), FALSE);
8883 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008884 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008886 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 {
8888 if (win_alloc_lines(wp) == FAIL)
8889 {
8890 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008891 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008894 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8895 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008896 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008897give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008899#ifdef FEAT_MBYTE
8900 for (i = 0; i < p_mco; ++i)
8901 if (new_ScreenLinesC[i] == NULL)
8902 break;
8903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (new_ScreenLines == NULL
8905#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008906 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8908#endif
8909 || new_ScreenAttrs == NULL
8910 || new_LineOffset == NULL
8911 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008912 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 || outofmem)
8914 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008915 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008916 {
8917 /* guess the size */
8918 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8919
8920 /* Remember we did this to avoid getting outofmem messages over
8921 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008922 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008923 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008924 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008926 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008927 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008928 VIM_CLEAR(new_ScreenLinesC[i]);
8929 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008931 VIM_CLEAR(new_ScreenAttrs);
8932 VIM_CLEAR(new_LineOffset);
8933 VIM_CLEAR(new_LineWraps);
8934 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936 else
8937 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008938 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008939
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 for (new_row = 0; new_row < Rows; ++new_row)
8941 {
8942 new_LineOffset[new_row] = new_row * Columns;
8943 new_LineWraps[new_row] = FALSE;
8944
8945 /*
8946 * If the screen is not going to be cleared, copy as much as
8947 * possible from the old screen to the new one and clear the rest
8948 * (used when resizing the window at the "--more--" prompt or when
8949 * executing an external command, for the GUI).
8950 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008951 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
8953 (void)vim_memset(new_ScreenLines + new_row * Columns,
8954 ' ', (size_t)Columns * sizeof(schar_T));
8955#ifdef FEAT_MBYTE
8956 if (enc_utf8)
8957 {
8958 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8959 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008960 for (i = 0; i < p_mco; ++i)
8961 (void)vim_memset(new_ScreenLinesC[i]
8962 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 0, (size_t)Columns * sizeof(u8char_T));
8964 }
8965 if (enc_dbcs == DBCS_JPNU)
8966 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8967 0, (size_t)Columns * sizeof(schar_T));
8968#endif
8969 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8970 0, (size_t)Columns * sizeof(sattr_T));
8971 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008972 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 {
8974 if (screen_Columns < Columns)
8975 len = screen_Columns;
8976 else
8977 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008978#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008979 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008980 * may be invalid now. Also when p_mco changes. */
8981 if (!(enc_utf8 && ScreenLinesUC == NULL)
8982 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008983#endif
8984 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8985 ScreenLines + LineOffset[old_row],
8986 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008988 if (enc_utf8 && ScreenLinesUC != NULL
8989 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 {
8991 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8992 ScreenLinesUC + LineOffset[old_row],
8993 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008994 for (i = 0; i < p_mco; ++i)
8995 mch_memmove(new_ScreenLinesC[i]
8996 + new_LineOffset[new_row],
8997 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 (size_t)len * sizeof(u8char_T));
8999 }
9000 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9001 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9002 ScreenLines2 + LineOffset[old_row],
9003 (size_t)len * sizeof(schar_T));
9004#endif
9005 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9006 ScreenAttrs + LineOffset[old_row],
9007 (size_t)len * sizeof(sattr_T));
9008 }
9009 }
9010 }
9011 /* Use the last line of the screen for the current line. */
9012 current_ScreenLine = new_ScreenLines + Rows * Columns;
9013 }
9014
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009015 free_screenlines();
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 ScreenLines = new_ScreenLines;
9018#ifdef FEAT_MBYTE
9019 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009020 for (i = 0; i < p_mco; ++i)
9021 ScreenLinesC[i] = new_ScreenLinesC[i];
9022 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 ScreenLines2 = new_ScreenLines2;
9024#endif
9025 ScreenAttrs = new_ScreenAttrs;
9026 LineOffset = new_LineOffset;
9027 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009028 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029
9030 /* It's important that screen_Rows and screen_Columns reflect the actual
9031 * size of ScreenLines[]. Set them before calling anything. */
9032#ifdef FEAT_GUI
9033 old_Rows = screen_Rows;
9034#endif
9035 screen_Rows = Rows;
9036 screen_Columns = Columns;
9037
9038 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009039 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 screenclear2();
9041
9042#ifdef FEAT_GUI
9043 else if (gui.in_use
9044 && !gui.starting
9045 && ScreenLines != NULL
9046 && old_Rows != Rows)
9047 {
9048 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9049 /*
9050 * Adjust the position of the cursor, for when executing an external
9051 * command.
9052 */
9053 if (msg_row >= Rows) /* Rows got smaller */
9054 msg_row = Rows - 1; /* put cursor at last row */
9055 else if (Rows > old_Rows) /* Rows got bigger */
9056 msg_row += Rows - old_Rows; /* put cursor in same place */
9057 if (msg_col >= Columns) /* Columns got smaller */
9058 msg_col = Columns - 1; /* put cursor at last column */
9059 }
9060#endif
9061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009063 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009064
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009065 /*
9066 * Do not apply autocommands more than 3 times to avoid an endless loop
9067 * in case applying autocommands always changes Rows or Columns.
9068 */
9069 if (starting == 0 && ++retry_count <= 3)
9070 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009071 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009072 /* In rare cases, autocommands may have altered Rows or Columns,
9073 * jump back to check if we need to allocate the screen again. */
9074 goto retry;
9075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076}
9077
9078 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009079free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009080{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009081#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009082 int i;
9083
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009084 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009085 for (i = 0; i < Screen_mco; ++i)
9086 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009087 vim_free(ScreenLines2);
9088#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009089 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009090 vim_free(ScreenAttrs);
9091 vim_free(LineOffset);
9092 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009093 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009094}
9095
9096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009097screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
9099 check_for_delay(FALSE);
9100 screenalloc(FALSE); /* allocate screen buffers if size changed */
9101 screenclear2(); /* clear the screen */
9102}
9103
9104 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009105screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 int i;
9108
9109 if (starting == NO_SCREEN || ScreenLines == NULL
9110#ifdef FEAT_GUI
9111 || (gui.in_use && gui.starting)
9112#endif
9113 )
9114 return;
9115
9116#ifdef FEAT_GUI
9117 if (!gui.in_use)
9118#endif
9119 screen_attr = -1; /* force setting the Normal colors */
9120 screen_stop_highlight(); /* don't want highlighting here */
9121
9122#ifdef FEAT_CLIPBOARD
9123 /* disable selection without redrawing it */
9124 clip_scroll_selection(9999);
9125#endif
9126
9127 /* blank out ScreenLines */
9128 for (i = 0; i < Rows; ++i)
9129 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009130 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 LineWraps[i] = FALSE;
9132 }
9133
9134 if (can_clear(T_CL))
9135 {
9136 out_str(T_CL); /* clear the display */
9137 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009138 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
9140 else
9141 {
9142 /* can't clear the screen, mark all chars with invalid attributes */
9143 for (i = 0; i < Rows; ++i)
9144 lineinvalid(LineOffset[i], (int)Columns);
9145 clear_cmdline = TRUE;
9146 }
9147
9148 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9149
9150 win_rest_invalid(firstwin);
9151 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009152 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 if (must_redraw == CLEAR) /* no need to clear again */
9154 must_redraw = NOT_VALID;
9155 compute_cmdrow();
9156 msg_row = cmdline_row; /* put cursor on last line for messages */
9157 msg_col = 0;
9158 screen_start(); /* don't know where cursor is now */
9159 msg_scrolled = 0; /* can't scroll back */
9160 msg_didany = FALSE;
9161 msg_didout = FALSE;
9162}
9163
9164/*
9165 * Clear one line in ScreenLines.
9166 */
9167 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009168lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9171#ifdef FEAT_MBYTE
9172 if (enc_utf8)
9173 (void)vim_memset(ScreenLinesUC + off, 0,
9174 (size_t)width * sizeof(u8char_T));
9175#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009176 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177}
9178
9179/*
9180 * Mark one line in ScreenLines invalid by setting the attributes to an
9181 * invalid value.
9182 */
9183 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009184lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9187}
9188
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189/*
9190 * Copy part of a Screenline for vertically split window "wp".
9191 */
9192 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009193linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195 unsigned off_to = LineOffset[to] + wp->w_wincol;
9196 unsigned off_from = LineOffset[from] + wp->w_wincol;
9197
9198 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9199 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009200#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 if (enc_utf8)
9202 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009203 int i;
9204
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9206 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009207 for (i = 0; i < p_mco; ++i)
9208 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9209 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 }
9211 if (enc_dbcs == DBCS_JPNU)
9212 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9213 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9216 wp->w_width * sizeof(sattr_T));
9217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
9219/*
9220 * Return TRUE if clearing with term string "p" would work.
9221 * It can't work when the string is empty or it won't set the right background.
9222 */
9223 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009224can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 return (*p != NUL && (t_colors <= 1
9227#ifdef FEAT_GUI
9228 || gui.in_use
9229#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009230#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009231 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009232 || (!p_tgc && cterm_normal_bg_color == 0)
9233#else
9234 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009235#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009236 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237}
9238
9239/*
9240 * Reset cursor position. Use whenever cursor was moved because of outputting
9241 * something directly to the screen (shell commands) or a terminal control
9242 * code.
9243 */
9244 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009245screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
9247 screen_cur_row = screen_cur_col = 9999;
9248}
9249
9250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 * Move the cursor to position "row","col" in the screen.
9252 * This tries to find the most efficient way to move, minimizing the number of
9253 * characters sent to the terminal.
9254 */
9255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009256windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009258 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 int i;
9260 int plan;
9261 int cost;
9262 int wouldbe_col;
9263 int noinvcurs;
9264 char_u *bs;
9265 int goto_cost;
9266 int attr;
9267
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009268#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9270
9271#define PLAN_LE 1
9272#define PLAN_CR 2
9273#define PLAN_NL 3
9274#define PLAN_WRITE 4
9275 /* Can't use ScreenLines unless initialized */
9276 if (ScreenLines == NULL)
9277 return;
9278
9279 if (col != screen_cur_col || row != screen_cur_row)
9280 {
9281 /* Check for valid position. */
9282 if (row < 0) /* window without text lines? */
9283 row = 0;
9284 if (row >= screen_Rows)
9285 row = screen_Rows - 1;
9286 if (col >= screen_Columns)
9287 col = screen_Columns - 1;
9288
9289 /* check if no cursor movement is allowed in highlight mode */
9290 if (screen_attr && *T_MS == NUL)
9291 noinvcurs = HIGHL_COST;
9292 else
9293 noinvcurs = 0;
9294 goto_cost = GOTO_COST + noinvcurs;
9295
9296 /*
9297 * Plan how to do the positioning:
9298 * 1. Use CR to move it to column 0, same row.
9299 * 2. Use T_LE to move it a few columns to the left.
9300 * 3. Use NL to move a few lines down, column 0.
9301 * 4. Move a few columns to the right with T_ND or by writing chars.
9302 *
9303 * Don't do this if the cursor went beyond the last column, the cursor
9304 * position is unknown then (some terminals wrap, some don't )
9305 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009306 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 * characters to move the cursor to the right.
9308 */
9309 if (row >= screen_cur_row && screen_cur_col < Columns)
9310 {
9311 /*
9312 * If the cursor is in the same row, bigger col, we can use CR
9313 * or T_LE.
9314 */
9315 bs = NULL; /* init for GCC */
9316 attr = screen_attr;
9317 if (row == screen_cur_row && col < screen_cur_col)
9318 {
9319 /* "le" is preferred over "bc", because "bc" is obsolete */
9320 if (*T_LE)
9321 bs = T_LE; /* "cursor left" */
9322 else
9323 bs = T_BC; /* "backspace character (old) */
9324 if (*bs)
9325 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9326 else
9327 cost = 999;
9328 if (col + 1 < cost) /* using CR is less characters */
9329 {
9330 plan = PLAN_CR;
9331 wouldbe_col = 0;
9332 cost = 1; /* CR is just one character */
9333 }
9334 else
9335 {
9336 plan = PLAN_LE;
9337 wouldbe_col = col;
9338 }
9339 if (noinvcurs) /* will stop highlighting */
9340 {
9341 cost += noinvcurs;
9342 attr = 0;
9343 }
9344 }
9345
9346 /*
9347 * If the cursor is above where we want to be, we can use CR LF.
9348 */
9349 else if (row > screen_cur_row)
9350 {
9351 plan = PLAN_NL;
9352 wouldbe_col = 0;
9353 cost = (row - screen_cur_row) * 2; /* CR LF */
9354 if (noinvcurs) /* will stop highlighting */
9355 {
9356 cost += noinvcurs;
9357 attr = 0;
9358 }
9359 }
9360
9361 /*
9362 * If the cursor is in the same row, smaller col, just use write.
9363 */
9364 else
9365 {
9366 plan = PLAN_WRITE;
9367 wouldbe_col = screen_cur_col;
9368 cost = 0;
9369 }
9370
9371 /*
9372 * Check if any characters that need to be written have the
9373 * correct attributes. Also avoid UTF-8 characters.
9374 */
9375 i = col - wouldbe_col;
9376 if (i > 0)
9377 cost += i;
9378 if (cost < goto_cost && i > 0)
9379 {
9380 /*
9381 * Check if the attributes are correct without additionally
9382 * stopping highlighting.
9383 */
9384 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9385 while (i && *p++ == attr)
9386 --i;
9387 if (i != 0)
9388 {
9389 /*
9390 * Try if it works when highlighting is stopped here.
9391 */
9392 if (*--p == 0)
9393 {
9394 cost += noinvcurs;
9395 while (i && *p++ == 0)
9396 --i;
9397 }
9398 if (i != 0)
9399 cost = 999; /* different attributes, don't do it */
9400 }
9401#ifdef FEAT_MBYTE
9402 if (enc_utf8)
9403 {
9404 /* Don't use an UTF-8 char for positioning, it's slow. */
9405 for (i = wouldbe_col; i < col; ++i)
9406 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9407 {
9408 cost = 999;
9409 break;
9410 }
9411 }
9412#endif
9413 }
9414
9415 /*
9416 * We can do it without term_windgoto()!
9417 */
9418 if (cost < goto_cost)
9419 {
9420 if (plan == PLAN_LE)
9421 {
9422 if (noinvcurs)
9423 screen_stop_highlight();
9424 while (screen_cur_col > col)
9425 {
9426 out_str(bs);
9427 --screen_cur_col;
9428 }
9429 }
9430 else if (plan == PLAN_CR)
9431 {
9432 if (noinvcurs)
9433 screen_stop_highlight();
9434 out_char('\r');
9435 screen_cur_col = 0;
9436 }
9437 else if (plan == PLAN_NL)
9438 {
9439 if (noinvcurs)
9440 screen_stop_highlight();
9441 while (screen_cur_row < row)
9442 {
9443 out_char('\n');
9444 ++screen_cur_row;
9445 }
9446 screen_cur_col = 0;
9447 }
9448
9449 i = col - screen_cur_col;
9450 if (i > 0)
9451 {
9452 /*
9453 * Use cursor-right if it's one character only. Avoids
9454 * removing a line of pixels from the last bold char, when
9455 * using the bold trick in the GUI.
9456 */
9457 if (T_ND[0] != NUL && T_ND[1] == NUL)
9458 {
9459 while (i-- > 0)
9460 out_char(*T_ND);
9461 }
9462 else
9463 {
9464 int off;
9465
9466 off = LineOffset[row] + screen_cur_col;
9467 while (i-- > 0)
9468 {
9469 if (ScreenAttrs[off] != screen_attr)
9470 screen_stop_highlight();
9471#ifdef FEAT_MBYTE
9472 out_flush_check();
9473#endif
9474 out_char(ScreenLines[off]);
9475#ifdef FEAT_MBYTE
9476 if (enc_dbcs == DBCS_JPNU
9477 && ScreenLines[off] == 0x8e)
9478 out_char(ScreenLines2[off]);
9479#endif
9480 ++off;
9481 }
9482 }
9483 }
9484 }
9485 }
9486 else
9487 cost = 999;
9488
9489 if (cost >= goto_cost)
9490 {
9491 if (noinvcurs)
9492 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009493 if (row == screen_cur_row && (col > screen_cur_col)
9494 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 term_cursor_right(col - screen_cur_col);
9496 else
9497 term_windgoto(row, col);
9498 }
9499 screen_cur_row = row;
9500 screen_cur_col = col;
9501 }
9502}
9503
9504/*
9505 * Set cursor to its position in the current window.
9506 */
9507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009508setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009510 setcursor_mayforce(FALSE);
9511}
9512
9513/*
9514 * Set cursor to its position in the current window.
9515 * When "force" is TRUE also when not redrawing.
9516 */
9517 void
9518setcursor_mayforce(int force)
9519{
9520 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
9522 validate_cursor();
9523 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009524 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009526 /* With 'rightleft' set and the cursor on a double-wide
9527 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009528 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009530 (has_mbyte
9531 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9532 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533# endif
9534 1)) :
9535#endif
9536 curwin->w_wcol));
9537 }
9538}
9539
9540
9541/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009542 * Insert 'line_count' lines at 'row' in window 'wp'.
9543 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9544 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 * scrolling.
9546 * Returns FAIL if the lines are not inserted, OK for success.
9547 */
9548 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009549win_ins_lines(
9550 win_T *wp,
9551 int row,
9552 int line_count,
9553 int invalid,
9554 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556 int did_delete;
9557 int nextrow;
9558 int lastrow;
9559 int retval;
9560
9561 if (invalid)
9562 wp->w_lines_valid = 0;
9563
9564 if (wp->w_height < 5)
9565 return FAIL;
9566
9567 if (line_count > wp->w_height - row)
9568 line_count = wp->w_height - row;
9569
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009570 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 if (retval != MAYBE)
9572 return retval;
9573
9574 /*
9575 * If there is a next window or a status line, we first try to delete the
9576 * lines at the bottom to avoid messing what is after the window.
9577 * If this fails and there are following windows, don't do anything to avoid
9578 * messing up those windows, better just redraw.
9579 */
9580 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 if (wp->w_next != NULL || wp->w_status_height)
9582 {
9583 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009584 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 did_delete = TRUE;
9586 else if (wp->w_next)
9587 return FAIL;
9588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 /*
9590 * if no lines deleted, blank the lines that will end up below the window
9591 */
9592 if (!did_delete)
9593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009596 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 lastrow = nextrow + line_count;
9598 if (lastrow > Rows)
9599 lastrow = Rows;
9600 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009601 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 ' ', ' ', 0);
9603 }
9604
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009605 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 == FAIL)
9607 {
9608 /* deletion will have messed up other windows */
9609 if (did_delete)
9610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 win_rest_invalid(W_NEXT(wp));
9613 }
9614 return FAIL;
9615 }
9616
9617 return OK;
9618}
9619
9620/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009621 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9623 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9624 * scrolling
9625 * Return OK for success, FAIL if the lines are not deleted.
9626 */
9627 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009628win_del_lines(
9629 win_T *wp,
9630 int row,
9631 int line_count,
9632 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009633 int mayclear,
9634 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
9636 int retval;
9637
9638 if (invalid)
9639 wp->w_lines_valid = 0;
9640
9641 if (line_count > wp->w_height - row)
9642 line_count = wp->w_height - row;
9643
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009644 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 if (retval != MAYBE)
9646 return retval;
9647
9648 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009649 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 return FAIL;
9651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 /*
9653 * If there are windows or status lines below, try to put them at the
9654 * correct place. If we can't do that, they have to be redrawn.
9655 */
9656 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9657 {
9658 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009659 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 {
9661 wp->w_redr_status = TRUE;
9662 win_rest_invalid(wp->w_next);
9663 }
9664 }
9665 /*
9666 * If this is the last window and there is no status line, redraw the
9667 * command line later.
9668 */
9669 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 redraw_cmdline = TRUE;
9671 return OK;
9672}
9673
9674/*
9675 * Common code for win_ins_lines() and win_del_lines().
9676 * Returns OK or FAIL when the work has been done.
9677 * Returns MAYBE when not finished yet.
9678 */
9679 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009680win_do_lines(
9681 win_T *wp,
9682 int row,
9683 int line_count,
9684 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009685 int del,
9686 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 int retval;
9689
9690 if (!redrawing() || line_count <= 0)
9691 return FAIL;
9692
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009693 /* When inserting lines would result in loss of command output, just redraw
9694 * the lines. */
9695 if (no_win_do_lines_ins && !del)
9696 return FAIL;
9697
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009699 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009701 if (!no_win_do_lines_ins)
9702 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 return FAIL;
9704 }
9705
9706 /*
9707 * Delete all remaining lines
9708 */
9709 if (row + line_count >= wp->w_height)
9710 {
9711 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009712 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 ' ', ' ', 0);
9714 return OK;
9715 }
9716
9717 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009718 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009720 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009722 if (!no_win_do_lines_ins)
9723 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724
9725 /*
9726 * If the terminal can set a scroll region, use that.
9727 * Always do this in a vertically split window. This will redraw from
9728 * ScreenLines[] when t_CV isn't defined. That's faster than using
9729 * win_line().
9730 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009731 * a character in the lower right corner of the scroll region may cause a
9732 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009734 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 scroll_region_set(wp, row);
9738 if (del)
9739 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009740 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 else
9742 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009743 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 scroll_region_reset();
9746 return retval;
9747 }
9748
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9750 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
9752 return MAYBE;
9753}
9754
9755/*
9756 * window 'wp' and everything after it is messed up, mark it for redraw
9757 */
9758 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009759win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 {
9763 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 wp->w_redr_status = TRUE;
9765 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 }
9767 redraw_cmdline = TRUE;
9768}
9769
9770/*
9771 * The rest of the routines in this file perform screen manipulations. The
9772 * given operation is performed physically on the screen. The corresponding
9773 * change is also made to the internal screen image. In this way, the editor
9774 * anticipates the effect of editing changes on the appearance of the screen.
9775 * That way, when we call screenupdate a complete redraw isn't usually
9776 * necessary. Another advantage is that we can keep adding code to anticipate
9777 * screen changes, and in the meantime, everything still works.
9778 */
9779
9780/*
9781 * types for inserting or deleting lines
9782 */
9783#define USE_T_CAL 1
9784#define USE_T_CDL 2
9785#define USE_T_AL 3
9786#define USE_T_CE 4
9787#define USE_T_DL 5
9788#define USE_T_SR 6
9789#define USE_NL 7
9790#define USE_T_CD 8
9791#define USE_REDRAW 9
9792
9793/*
9794 * insert lines on the screen and update ScreenLines[]
9795 * 'end' is the line after the scrolled part. Normally it is Rows.
9796 * When scrolling region used 'off' is the offset from the top for the region.
9797 * 'row' and 'end' are relative to the start of the region.
9798 *
9799 * return FAIL for failure, OK for success.
9800 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009801 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009802screen_ins_lines(
9803 int off,
9804 int row,
9805 int line_count,
9806 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009807 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009808 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809{
9810 int i;
9811 int j;
9812 unsigned temp;
9813 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009814 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 int type;
9816 int result_empty;
9817 int can_ce = can_clear(T_CE);
9818
9819 /*
9820 * FAIL if
9821 * - there is no valid screen
9822 * - the screen has to be redrawn completely
9823 * - the line count is less than one
9824 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009825 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009827 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9828#ifdef FEAT_CLIPBOARD
9829 || (clip_star.state != SELECT_CLEARED
9830 && redrawing_for_callback > 0)
9831#endif
9832 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 return FAIL;
9834
9835 /*
9836 * There are seven ways to insert lines:
9837 * 0. When in a vertically split window and t_CV isn't set, redraw the
9838 * characters from ScreenLines[].
9839 * 1. Use T_CD (clear to end of display) if it exists and the result of
9840 * the insert is just empty lines
9841 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9842 * present or line_count > 1. It looks better if we do all the inserts
9843 * at once.
9844 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9845 * insert is just empty lines and T_CE is not present or line_count >
9846 * 1.
9847 * 4. Use T_AL (insert line) if it exists.
9848 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9849 * just empty lines.
9850 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9851 * just empty lines.
9852 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9853 * the 'da' flag is not set or we have clear line capability.
9854 * 8. redraw the characters from ScreenLines[].
9855 *
9856 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9857 * the scrollbar for the window. It does have insert line, use that if it
9858 * exists.
9859 */
9860 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9862 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009863 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 type = USE_T_CD;
9865 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9866 type = USE_T_CAL;
9867 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9868 type = USE_T_CDL;
9869 else if (*T_AL != NUL)
9870 type = USE_T_AL;
9871 else if (can_ce && result_empty)
9872 type = USE_T_CE;
9873 else if (*T_DL != NUL && result_empty)
9874 type = USE_T_DL;
9875 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9876 type = USE_T_SR;
9877 else
9878 return FAIL;
9879
9880 /*
9881 * For clearing the lines screen_del_lines() is used. This will also take
9882 * care of t_db if necessary.
9883 */
9884 if (type == USE_T_CD || type == USE_T_CDL ||
9885 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009886 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
9888 /*
9889 * If text is retained below the screen, first clear or delete as many
9890 * lines at the bottom of the window as are about to be inserted so that
9891 * the deleted lines won't later surface during a screen_del_lines.
9892 */
9893 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009894 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896#ifdef FEAT_CLIPBOARD
9897 /* Remove a modeless selection when inserting lines halfway the screen
9898 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009899 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009900 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 else
9902 clip_scroll_selection(-line_count);
9903#endif
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905#ifdef FEAT_GUI
9906 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9907 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009908 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909#endif
9910
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009911 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9912 cursor_col = wp->w_wincol;
9913
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (*T_CCS != NUL) /* cursor relative to region */
9915 cursor_row = row;
9916 else
9917 cursor_row = row + off;
9918
9919 /*
9920 * Shift LineOffset[] line_count down to reflect the inserted lines.
9921 * Clear the inserted lines in ScreenLines[].
9922 */
9923 row += off;
9924 end += off;
9925 for (i = 0; i < line_count; ++i)
9926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 if (wp != NULL && wp->w_width != Columns)
9928 {
9929 /* need to copy part of a line */
9930 j = end - 1 - i;
9931 while ((j -= line_count) >= row)
9932 linecopy(j + line_count, j, wp);
9933 j += line_count;
9934 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009935 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9936 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9939 LineWraps[j] = FALSE;
9940 }
9941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 {
9943 j = end - 1 - i;
9944 temp = LineOffset[j];
9945 while ((j -= line_count) >= row)
9946 {
9947 LineOffset[j + line_count] = LineOffset[j];
9948 LineWraps[j + line_count] = LineWraps[j];
9949 }
9950 LineOffset[j + line_count] = temp;
9951 LineWraps[j + line_count] = FALSE;
9952 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009953 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 else
9955 lineinvalid(temp, (int)Columns);
9956 }
9957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
9959 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009960 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009961 if (clear_attr != 0)
9962 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 /* redraw the characters */
9965 if (type == USE_REDRAW)
9966 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009967 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
9969 term_append_lines(line_count);
9970 screen_start(); /* don't know where cursor is now */
9971 }
9972 else
9973 {
9974 for (i = 0; i < line_count; i++)
9975 {
9976 if (type == USE_T_AL)
9977 {
9978 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009979 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 out_str(T_AL);
9981 }
9982 else /* type == USE_T_SR */
9983 out_str(T_SR);
9984 screen_start(); /* don't know where cursor is now */
9985 }
9986 }
9987
9988 /*
9989 * With scroll-reverse and 'da' flag set we need to clear the lines that
9990 * have been scrolled down into the region.
9991 */
9992 if (type == USE_T_SR && *T_DA)
9993 {
9994 for (i = 0; i < line_count; ++i)
9995 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009996 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 out_str(T_CE);
9998 screen_start(); /* don't know where cursor is now */
9999 }
10000 }
10001
10002#ifdef FEAT_GUI
10003 gui_can_update_cursor();
10004 if (gui.in_use)
10005 out_flush(); /* always flush after a scroll */
10006#endif
10007 return OK;
10008}
10009
10010/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010011 * Delete lines on the screen and update ScreenLines[].
10012 * "end" is the line after the scrolled part. Normally it is Rows.
10013 * When scrolling region used "off" is the offset from the top for the region.
10014 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 *
10016 * Return OK for success, FAIL if the lines are not deleted.
10017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010019screen_del_lines(
10020 int off,
10021 int row,
10022 int line_count,
10023 int end,
10024 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010025 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010026 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027{
10028 int j;
10029 int i;
10030 unsigned temp;
10031 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010032 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 int cursor_end;
10034 int result_empty; /* result is empty until end of region */
10035 int can_delete; /* deleting line codes can be used */
10036 int type;
10037
10038 /*
10039 * FAIL if
10040 * - there is no valid screen
10041 * - the screen has to be redrawn completely
10042 * - the line count is less than one
10043 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010044 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010046 if (!screen_valid(TRUE) || line_count <= 0
10047 || (!force && line_count > p_ttyscroll)
10048#ifdef FEAT_CLIPBOARD
10049 || (clip_star.state != SELECT_CLEARED
10050 && redrawing_for_callback > 0)
10051#endif
10052 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 return FAIL;
10054
10055 /*
10056 * Check if the rest of the current region will become empty.
10057 */
10058 result_empty = row + line_count >= end;
10059
10060 /*
10061 * We can delete lines only when 'db' flag not set or when 'ce' option
10062 * available.
10063 */
10064 can_delete = (*T_DB == NUL || can_clear(T_CE));
10065
10066 /*
10067 * There are six ways to delete lines:
10068 * 0. When in a vertically split window and t_CV isn't set, redraw the
10069 * characters from ScreenLines[].
10070 * 1. Use T_CD if it exists and the result is empty.
10071 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10072 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10073 * none of the other ways work.
10074 * 4. Use T_CE (erase line) if the result is empty.
10075 * 5. Use T_DL (delete line) if it exists.
10076 * 6. redraw the characters from ScreenLines[].
10077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10079 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010080 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 type = USE_T_CD;
10082#if defined(__BEOS__) && defined(BEOS_DR8)
10083 /*
10084 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10085 * its internal termcap... this works okay for tests which test *T_DB !=
10086 * NUL. It has the disadvantage that the user cannot use any :set t_*
10087 * command to get T_DB (back) to empty_option, only :set term=... will do
10088 * the trick...
10089 * Anyway, this hack will hopefully go away with the next OS release.
10090 * (Olaf Seibert)
10091 */
10092 else if (row == 0 && T_DB == empty_option
10093 && (line_count == 1 || *T_CDL == NUL))
10094#else
10095 else if (row == 0 && (
10096#ifndef AMIGA
10097 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10098 * up, so use delete-line command */
10099 line_count == 1 ||
10100#endif
10101 *T_CDL == NUL))
10102#endif
10103 type = USE_NL;
10104 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10105 type = USE_T_CDL;
10106 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010107 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 type = USE_T_CE;
10109 else if (*T_DL != NUL && can_delete)
10110 type = USE_T_DL;
10111 else if (*T_CDL != NUL && can_delete)
10112 type = USE_T_CDL;
10113 else
10114 return FAIL;
10115
10116#ifdef FEAT_CLIPBOARD
10117 /* Remove a modeless selection when deleting lines halfway the screen or
10118 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010119 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010120 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 else
10122 clip_scroll_selection(line_count);
10123#endif
10124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125#ifdef FEAT_GUI
10126 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10127 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010128 gui_dont_update_cursor(gui.cursor_row >= row + off
10129 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#endif
10131
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010132 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10133 cursor_col = wp->w_wincol;
10134
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 if (*T_CCS != NUL) /* cursor relative to region */
10136 {
10137 cursor_row = row;
10138 cursor_end = end;
10139 }
10140 else
10141 {
10142 cursor_row = row + off;
10143 cursor_end = end + off;
10144 }
10145
10146 /*
10147 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10148 * Clear the inserted lines in ScreenLines[].
10149 */
10150 row += off;
10151 end += off;
10152 for (i = 0; i < line_count; ++i)
10153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 if (wp != NULL && wp->w_width != Columns)
10155 {
10156 /* need to copy part of a line */
10157 j = row + i;
10158 while ((j += line_count) <= end - 1)
10159 linecopy(j - line_count, j, wp);
10160 j -= line_count;
10161 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010162 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10163 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 else
10165 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10166 LineWraps[j] = FALSE;
10167 }
10168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 {
10170 /* whole width, moving the line pointers is faster */
10171 j = row + i;
10172 temp = LineOffset[j];
10173 while ((j += line_count) <= end - 1)
10174 {
10175 LineOffset[j - line_count] = LineOffset[j];
10176 LineWraps[j - line_count] = LineWraps[j];
10177 }
10178 LineOffset[j - line_count] = temp;
10179 LineWraps[j - line_count] = FALSE;
10180 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010181 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 else
10183 lineinvalid(temp, (int)Columns);
10184 }
10185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010187 if (screen_attr != clear_attr)
10188 screen_stop_highlight();
10189 if (clear_attr != 0)
10190 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 /* redraw the characters */
10193 if (type == USE_REDRAW)
10194 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010195 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010197 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 out_str(T_CD);
10199 screen_start(); /* don't know where cursor is now */
10200 }
10201 else if (type == USE_T_CDL)
10202 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010203 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 term_delete_lines(line_count);
10205 screen_start(); /* don't know where cursor is now */
10206 }
10207 /*
10208 * Deleting lines at top of the screen or scroll region: Just scroll
10209 * the whole screen (scroll region) up by outputting newlines on the
10210 * last line.
10211 */
10212 else if (type == USE_NL)
10213 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010214 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 for (i = line_count; --i >= 0; )
10216 out_char('\n'); /* cursor will remain on same line */
10217 }
10218 else
10219 {
10220 for (i = line_count; --i >= 0; )
10221 {
10222 if (type == USE_T_DL)
10223 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010224 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 out_str(T_DL); /* delete a line */
10226 }
10227 else /* type == USE_T_CE */
10228 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010229 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 out_str(T_CE); /* erase a line */
10231 }
10232 screen_start(); /* don't know where cursor is now */
10233 }
10234 }
10235
10236 /*
10237 * If the 'db' flag is set, we need to clear the lines that have been
10238 * scrolled up at the bottom of the region.
10239 */
10240 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10241 {
10242 for (i = line_count; i > 0; --i)
10243 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010244 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 out_str(T_CE); /* erase a line */
10246 screen_start(); /* don't know where cursor is now */
10247 }
10248 }
10249
10250#ifdef FEAT_GUI
10251 gui_can_update_cursor();
10252 if (gui.in_use)
10253 out_flush(); /* always flush after a scroll */
10254#endif
10255
10256 return OK;
10257}
10258
10259/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010260 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 *
10262 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10263 * If clear_cmdline is FALSE there may be a message there that needs to be
10264 * cleared only if a mode is shown.
10265 * Return the length of the message (0 if no message).
10266 */
10267 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010268showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269{
10270 int need_clear;
10271 int length = 0;
10272 int do_mode;
10273 int attr;
10274 int nwr_save;
10275#ifdef FEAT_INS_EXPAND
10276 int sub_attr;
10277#endif
10278
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010279 do_mode = ((p_smd && msg_silent == 0)
10280 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010281 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010282 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010283 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 {
10285 /*
10286 * Don't show mode right now, when not redrawing or inside a mapping.
10287 * Call char_avail() only when we are going to show something, because
10288 * it takes a bit of time.
10289 */
10290 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10291 {
10292 redraw_cmdline = TRUE; /* show mode later */
10293 return 0;
10294 }
10295
10296 nwr_save = need_wait_return;
10297
10298 /* wait a bit before overwriting an important message */
10299 check_for_delay(FALSE);
10300
10301 /* if the cmdline is more than one line high, erase top lines */
10302 need_clear = clear_cmdline;
10303 if (clear_cmdline && cmdline_row < Rows - 1)
10304 msg_clr_cmdline(); /* will reset clear_cmdline */
10305
10306 /* Position on the last line in the window, column 0 */
10307 msg_pos_mode();
10308 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010309 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 if (do_mode)
10311 {
10312 MSG_PUTS_ATTR("--", attr);
10313#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010314 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010315# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010316 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010317# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010318 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010319# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010320 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010321# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 MSG_PUTS_ATTR(" IM", attr);
10323# else
10324 MSG_PUTS_ATTR(" XIM", attr);
10325# endif
10326#endif
10327#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10328 if (gui.in_use)
10329 {
10330 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010331 {
10332 /* HANGUL */
10333 if (enc_utf8)
10334 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10335 else
10336 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 }
10339#endif
10340#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010341 /* CTRL-X in Insert mode */
10342 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 {
10344 /* These messages can get long, avoid a wrap in a narrow
10345 * window. Prefer showing edit_submode_extra. */
10346 length = (Rows - msg_row) * Columns - 3;
10347 if (edit_submode_extra != NULL)
10348 length -= vim_strsize(edit_submode_extra);
10349 if (length > 0)
10350 {
10351 if (edit_submode_pre != NULL)
10352 length -= vim_strsize(edit_submode_pre);
10353 if (length - vim_strsize(edit_submode) > 0)
10354 {
10355 if (edit_submode_pre != NULL)
10356 msg_puts_attr(edit_submode_pre, attr);
10357 msg_puts_attr(edit_submode, attr);
10358 }
10359 if (edit_submode_extra != NULL)
10360 {
10361 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10362 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010363 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 else
10365 sub_attr = attr;
10366 msg_puts_attr(edit_submode_extra, sub_attr);
10367 }
10368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 }
10370 else
10371#endif
10372 {
10373#ifdef FEAT_VREPLACE
10374 if (State & VREPLACE_FLAG)
10375 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
10376 else
10377#endif
10378 if (State & REPLACE_FLAG)
10379 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10380 else if (State & INSERT)
10381 {
10382#ifdef FEAT_RIGHTLEFT
10383 if (p_ri)
10384 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10385#endif
10386 MSG_PUTS_ATTR(_(" INSERT"), attr);
10387 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010388 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 MSG_PUTS_ATTR(_(" (insert)"), attr);
10390 else if (restart_edit == 'R')
10391 MSG_PUTS_ATTR(_(" (replace)"), attr);
10392 else if (restart_edit == 'V')
10393 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10394#ifdef FEAT_RIGHTLEFT
10395 if (p_hkmap)
10396 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10397# ifdef FEAT_FKMAP
10398 if (p_fkmap)
10399 MSG_PUTS_ATTR(farsi_text_5, attr);
10400# endif
10401#endif
10402#ifdef FEAT_KEYMAP
10403 if (State & LANGMAP)
10404 {
10405# ifdef FEAT_ARABIC
10406 if (curwin->w_p_arab)
10407 MSG_PUTS_ATTR(_(" Arabic"), attr);
10408 else
10409# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010410 if (get_keymap_str(curwin, (char_u *)" (%s)",
10411 NameBuff, MAXPATHL))
10412 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 }
10414#endif
10415 if ((State & INSERT) && p_paste)
10416 MSG_PUTS_ATTR(_(" (paste)"), attr);
10417
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 if (VIsual_active)
10419 {
10420 char *p;
10421
10422 /* Don't concatenate separate words to avoid translation
10423 * problems. */
10424 switch ((VIsual_select ? 4 : 0)
10425 + (VIsual_mode == Ctrl_V) * 2
10426 + (VIsual_mode == 'V'))
10427 {
10428 case 0: p = N_(" VISUAL"); break;
10429 case 1: p = N_(" VISUAL LINE"); break;
10430 case 2: p = N_(" VISUAL BLOCK"); break;
10431 case 4: p = N_(" SELECT"); break;
10432 case 5: p = N_(" SELECT LINE"); break;
10433 default: p = N_(" SELECT BLOCK"); break;
10434 }
10435 MSG_PUTS_ATTR(_(p), attr);
10436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 MSG_PUTS_ATTR(" --", attr);
10438 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010439
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 need_clear = TRUE;
10441 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010442 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443#ifdef FEAT_INS_EXPAND
10444 && edit_submode == NULL /* otherwise it gets too long */
10445#endif
10446 )
10447 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010448 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 need_clear = TRUE;
10450 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010451
10452 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 if (need_clear || clear_cmdline)
10454 msg_clr_eos();
10455 msg_didout = FALSE; /* overwrite this message */
10456 length = msg_col;
10457 msg_col = 0;
10458 need_wait_return = nwr_save; /* never ask for hit-return for this */
10459 }
10460 else if (clear_cmdline && msg_silent == 0)
10461 /* Clear the whole command line. Will reset "clear_cmdline". */
10462 msg_clr_cmdline();
10463
10464#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 /* In Visual mode the size of the selected area must be redrawn. */
10466 if (VIsual_active)
10467 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468
10469 /* If the last window has no status line, the ruler is after the mode
10470 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010471 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010472 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473#endif
10474 redraw_cmdline = FALSE;
10475 clear_cmdline = FALSE;
10476
10477 return length;
10478}
10479
10480/*
10481 * Position for a mode message.
10482 */
10483 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010484msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485{
10486 msg_col = 0;
10487 msg_row = Rows - 1;
10488}
10489
10490/*
10491 * Delete mode message. Used when ESC is typed which is expected to end
10492 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010493 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 */
10495 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010496unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010499 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 */
10501 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10502 redraw_cmdline = TRUE; /* delete mode later */
10503 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010504 clearmode();
10505}
10506
10507/*
10508 * Clear the mode message.
10509 */
10510 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010511clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010512{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010513 int save_msg_row = msg_row;
10514 int save_msg_col = msg_col;
10515
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010516 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010517 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010518 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010519 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010520
10521 msg_col = save_msg_col;
10522 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523}
10524
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010525 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010526recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010527{
10528 MSG_PUTS_ATTR(_("recording"), attr);
10529 if (!shortmess(SHM_RECORDING))
10530 {
10531 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010532 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010533 MSG_PUTS_ATTR(s, attr);
10534 }
10535}
10536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010537/*
10538 * Draw the tab pages line at the top of the Vim window.
10539 */
10540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010541draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010542{
10543 int tabcount = 0;
10544 tabpage_T *tp;
10545 int tabwidth;
10546 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010547 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010548 int attr;
10549 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010550 win_T *cwp;
10551 int wincount;
10552 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010553 int c;
10554 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010555 int attr_sel = HL_ATTR(HLF_TPS);
10556 int attr_nosel = HL_ATTR(HLF_TP);
10557 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010558 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010559 int room;
10560 int use_sep_chars = (t_colors < 8
10561#ifdef FEAT_GUI
10562 && !gui.in_use
10563#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010564#ifdef FEAT_TERMGUICOLORS
10565 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010566#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010567 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010568
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010569 if (ScreenLines == NULL)
10570 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010571 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010572
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010573#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010574 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010575 if (gui_use_tabline())
10576 {
10577 gui_update_tabline();
10578 return;
10579 }
10580#endif
10581
10582 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010583 return;
10584
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010585#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010586
10587 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10588 for (scol = 0; scol < Columns; ++scol)
10589 TabPageIdxs[scol] = 0;
10590
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010591 /* Use the 'tabline' option if it's set. */
10592 if (*p_tal != NUL)
10593 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010594 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010595
10596 /* Check for an error. If there is one we would loop in redrawing the
10597 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010598 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010599 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010600 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010601 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010602 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010603 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010604 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010605 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010606#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010607 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010608 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010609 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010610
Bram Moolenaar238a5642006-02-21 22:12:05 +000010611 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10612 if (tabwidth < 6)
10613 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010614
Bram Moolenaar238a5642006-02-21 22:12:05 +000010615 attr = attr_nosel;
10616 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010617 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010618 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10619 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010620 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010622
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 if (tp->tp_topframe == topframe)
10624 attr = attr_sel;
10625 if (use_sep_chars && col > 0)
10626 screen_putchar('|', 0, col++, attr);
10627
10628 if (tp->tp_topframe != topframe)
10629 attr = attr_nosel;
10630
10631 screen_putchar(' ', 0, col++, attr);
10632
10633 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010634 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 cwp = curwin;
10636 wp = firstwin;
10637 }
10638 else
10639 {
10640 cwp = tp->tp_curwin;
10641 wp = tp->tp_firstwin;
10642 }
10643
10644 modified = FALSE;
10645 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10646 if (bufIsChanged(wp->w_buffer))
10647 modified = TRUE;
10648 if (modified || wincount > 1)
10649 {
10650 if (wincount > 1)
10651 {
10652 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010653 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010654 if (col + len >= Columns - 3)
10655 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010656 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010657#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010658 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010659#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010660 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010661#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010662 );
10663 col += len;
10664 }
10665 if (modified)
10666 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10667 screen_putchar(' ', 0, col++, attr);
10668 }
10669
10670 room = scol - col + tabwidth - 1;
10671 if (room > 0)
10672 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010673 /* Get buffer name in NameBuff[] */
10674 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010675 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010676 len = vim_strsize(NameBuff);
10677 p = NameBuff;
10678#ifdef FEAT_MBYTE
10679 if (has_mbyte)
10680 while (len > room)
10681 {
10682 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010683 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010684 }
10685 else
10686#endif
10687 if (len > room)
10688 {
10689 p += len - room;
10690 len = room;
10691 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010692 if (len > Columns - col - 1)
10693 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010695 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010696 col += len;
10697 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010698 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010699
10700 /* Store the tab page number in TabPageIdxs[], so that
10701 * jump_to_mouse() knows where each one is. */
10702 ++tabcount;
10703 while (scol < col)
10704 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010705 }
10706
Bram Moolenaar238a5642006-02-21 22:12:05 +000010707 if (use_sep_chars)
10708 c = '_';
10709 else
10710 c = ' ';
10711 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010712
10713 /* Put an "X" for closing the current tab if there are several. */
10714 if (first_tabpage->tp_next != NULL)
10715 {
10716 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10717 TabPageIdxs[Columns - 1] = -999;
10718 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010719 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010720
10721 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10722 * set. */
10723 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010724}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010725
10726/*
10727 * Get buffer name for "buf" into NameBuff[].
10728 * Takes care of special buffer names and translates special characters.
10729 */
10730 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010731get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010732{
10733 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010734 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010735 else
10736 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10737 trans_characters(NameBuff, MAXPATHL);
10738}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010739
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740/*
10741 * Get the character to use in a status line. Get its attributes in "*attr".
10742 */
10743 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010744fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
10746 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010747
10748#ifdef FEAT_TERMINAL
10749 if (bt_terminal(wp->w_buffer))
10750 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010751 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010752 {
10753 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010754 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010755 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010757 {
10758 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010760 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010761 }
10762 else
10763#endif
10764 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010766 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 fill = fill_stl;
10768 }
10769 else
10770 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010771 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 fill = fill_stlnc;
10773 }
10774 /* Use fill when there is highlighting, and highlighting of current
10775 * window differs, or the fillchars differ, or this is not the
10776 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010777 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010778 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 || (fill_stl != fill_stlnc)))
10780 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010781 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 return '^';
10783 return '=';
10784}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786/*
10787 * Get the character to use in a separator between vertically split windows.
10788 * Get its attributes in "*attr".
10789 */
10790 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010791fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010793 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 if (*attr == 0 && fill_vert == ' ')
10795 return '|';
10796 else
10797 return fill_vert;
10798}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799
10800/*
10801 * Return TRUE if redrawing should currently be done.
10802 */
10803 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010804redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010806#ifdef FEAT_EVAL
10807 if (disable_redraw_for_testing)
10808 return 0;
10809 else
10810#endif
10811 return (!RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
10813}
10814
10815/*
10816 * Return TRUE if printing messages should currently be done.
10817 */
10818 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010819messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 return (!(p_lz && char_avail() && !KeyTyped));
10822}
10823
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010824#ifdef FEAT_MENU
10825/*
10826 * Draw the window toolbar.
10827 */
10828 static void
10829redraw_win_toolbar(win_T *wp)
10830{
10831 vimmenu_T *menu;
10832 int item_idx = 0;
10833 int item_count = 0;
10834 int col = 0;
10835 int next_col;
10836 int off = (int)(current_ScreenLine - ScreenLines);
10837 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10838 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10839
10840 vim_free(wp->w_winbar_items);
10841 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10842 ++item_count;
10843 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10844 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10845
10846 /* TODO: use fewer spaces if there is not enough room */
10847 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010848 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010849 {
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 if (col > 1)
10854 {
10855 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010856 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010857 break;
10858 }
10859
10860 wp->w_winbar_items[item_idx].wb_startcol = col;
10861 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010862 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010863 break;
10864
10865 next_col = text_to_screenline(wp, menu->name, col);
10866 while (col < next_col)
10867 {
10868 ScreenAttrs[off + col] = button_attr;
10869 ++col;
10870 }
10871 wp->w_winbar_items[item_idx].wb_endcol = col;
10872 wp->w_winbar_items[item_idx].wb_menu = menu;
10873 ++item_idx;
10874
Bram Moolenaar02631462017-09-22 15:20:32 +020010875 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010876 break;
10877 space_to_screenline(off + col, button_attr);
10878 ++col;
10879 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010880 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010881 {
10882 space_to_screenline(off + col, fill_attr);
10883 ++col;
10884 }
10885 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10886
Bram Moolenaar02631462017-09-22 15:20:32 +020010887 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10888 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010889}
10890#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010891
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892/*
10893 * Show current status info in ruler and various other places
10894 * If always is FALSE, only show ruler if position has changed.
10895 */
10896 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010897showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898{
10899 if (!always && !redrawing())
10900 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010901#ifdef FEAT_INS_EXPAND
10902 if (pum_visible())
10903 {
10904 /* Don't redraw right now, do it later. */
10905 curwin->w_redr_status = TRUE;
10906 return;
10907 }
10908#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010909#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010910 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010911 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010912 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 else
10915#endif
10916#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010917 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918#endif
10919
10920#ifdef FEAT_TITLE
10921 if (need_maketitle
10922# ifdef FEAT_STL_OPT
10923 || (p_icon && (stl_syntax & STL_IN_ICON))
10924 || (p_title && (stl_syntax & STL_IN_TITLE))
10925# endif
10926 )
10927 maketitle();
10928#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010929 /* Redraw the tab pages line if needed. */
10930 if (redraw_tabline)
10931 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932}
10933
10934#ifdef FEAT_CMDL_INFO
10935 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010936win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010938#define RULER_BUF_LEN 70
10939 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 int row;
10941 int fillchar;
10942 int attr;
10943 int empty_line = FALSE;
10944 colnr_T virtcol;
10945 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010946 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 int this_ru_col;
10949 int off = 0;
10950 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951
10952 /* If 'ruler' off or redrawing disabled, don't do anything */
10953 if (!p_ru)
10954 return;
10955
10956 /*
10957 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10958 * after deleting lines, before cursor.lnum is corrected.
10959 */
10960 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10961 return;
10962
10963#ifdef FEAT_INS_EXPAND
10964 /* Don't draw the ruler while doing insert-completion, it might overwrite
10965 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 if (edit_submode != NULL)
10968 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010969 // Don't draw the ruler when the popup menu is visible, it may overlap.
10970 // Except when the popup menu will be redrawn anyway.
10971 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010972 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973#endif
10974
10975#ifdef FEAT_STL_OPT
10976 if (*p_ruf)
10977 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010978 int save_called_emsg = called_emsg;
10979
10980 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010982 if (called_emsg)
10983 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010984 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010985 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 return;
10987 }
10988#endif
10989
10990 /*
10991 * Check if not in Insert mode and the line is empty (will show "0-1").
10992 */
10993 if (!(State & INSERT)
10994 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10995 empty_line = TRUE;
10996
10997 /*
10998 * Only draw the ruler when something changed.
10999 */
11000 validate_virtcol_win(wp);
11001 if ( redraw_cmdline
11002 || always
11003 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11004 || wp->w_cursor.col != wp->w_ru_cursor.col
11005 || wp->w_virtcol != wp->w_ru_virtcol
11006#ifdef FEAT_VIRTUALEDIT
11007 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11008#endif
11009 || wp->w_topline != wp->w_ru_topline
11010 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11011#ifdef FEAT_DIFF
11012 || wp->w_topfill != wp->w_ru_topfill
11013#endif
11014 || empty_line != wp->w_ru_empty)
11015 {
11016 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 if (wp->w_status_height)
11018 {
11019 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011020 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011021 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011022 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 }
11024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 {
11026 row = Rows - 1;
11027 fillchar = ' ';
11028 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 width = Columns;
11030 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 }
11032
11033 /* In list mode virtcol needs to be recomputed */
11034 virtcol = wp->w_virtcol;
11035 if (wp->w_p_list && lcs_tab1 == NUL)
11036 {
11037 wp->w_p_list = FALSE;
11038 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11039 wp->w_p_list = TRUE;
11040 }
11041
11042 /*
11043 * Some sprintfs return the length, some return a pointer.
11044 * To avoid portability problems we use strlen() here.
11045 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011046 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11048 ? 0L
11049 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011050 len = STRLEN(buffer);
11051 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11053 (int)virtcol + 1);
11054
11055 /*
11056 * Add a "50%" if there is room for it.
11057 * On the last line, don't print in the last column (scrolls the
11058 * screen up on some terminals).
11059 */
11060 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011061 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 this_ru_col = ru_col - (Columns - width);
11066 if (this_ru_col < 0)
11067 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 /* Never use more than half the window/screen width, leave the other
11069 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011070 if (this_ru_col < (width + 1) / 2)
11071 this_ru_col = (width + 1) / 2;
11072 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011074 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011075 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 {
11077#ifdef FEAT_MBYTE
11078 if (has_mbyte)
11079 i += (*mb_char2bytes)(fillchar, buffer + i);
11080 else
11081#endif
11082 buffer[i++] = fillchar;
11083 ++o;
11084 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011085 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 }
11087 /* Truncate at window boundary. */
11088#ifdef FEAT_MBYTE
11089 if (has_mbyte)
11090 {
11091 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011092 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 {
11094 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011095 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 {
11097 buffer[i] = NUL;
11098 break;
11099 }
11100 }
11101 }
11102 else
11103#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011104 if (this_ru_col + (int)STRLEN(buffer) > width)
11105 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
Bram Moolenaar4033c552017-09-16 20:54:51 +020011107 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 i = redraw_cmdline;
11109 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011110 this_ru_col + off + (int)STRLEN(buffer),
11111 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 fillchar, fillchar, attr);
11113 /* don't redraw the cmdline because of showing the ruler */
11114 redraw_cmdline = i;
11115 wp->w_ru_cursor = wp->w_cursor;
11116 wp->w_ru_virtcol = wp->w_virtcol;
11117 wp->w_ru_empty = empty_line;
11118 wp->w_ru_topline = wp->w_topline;
11119 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11120#ifdef FEAT_DIFF
11121 wp->w_ru_topfill = wp->w_topfill;
11122#endif
11123 }
11124}
11125#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011126
11127#if defined(FEAT_LINEBREAK) || defined(PROTO)
11128/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011129 * Return the width of the 'number' and 'relativenumber' column.
11130 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011131 * Otherwise it depends on 'numberwidth' and the line count.
11132 */
11133 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011134number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011135{
11136 int n;
11137 linenr_T lnum;
11138
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011139 if (wp->w_p_rnu && !wp->w_p_nu)
11140 /* cursor line shows "0" */
11141 lnum = wp->w_height;
11142 else
11143 /* cursor line shows absolute line number */
11144 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011145
Bram Moolenaar6b314672015-03-20 15:42:10 +010011146 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011147 return wp->w_nrwidth_width;
11148 wp->w_nrwidth_line_count = lnum;
11149
11150 n = 0;
11151 do
11152 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011153 lnum /= 10;
11154 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011155 } while (lnum > 0);
11156
11157 /* 'numberwidth' gives the minimal width plus one */
11158 if (n < wp->w_p_nuw - 1)
11159 n = wp->w_p_nuw - 1;
11160
11161 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011162 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011163 return n;
11164}
11165#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011166
11167/*
11168 * Return the current cursor column. This is the actual position on the
11169 * screen. First column is 0.
11170 */
11171 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011172screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011173{
11174 return screen_cur_col;
11175}
11176
11177/*
11178 * Return the current cursor row. This is the actual position on the screen.
11179 * First row is 0.
11180 */
11181 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011182screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011183{
11184 return screen_cur_row;
11185}