blob: f82e1d60c46dce6ccf36e65c7953457743615e9f [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)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100500 linenr_T lnum,
501 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
503#ifdef FEAT_FOLDING
504 int i;
505#endif
506
Bram Moolenaar90a99792018-09-12 21:52:18 +0200507 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
508 wp->w_redraw_top = lnum;
509 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
510 wp->w_redraw_bot = lnum;
511 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512
513#ifdef FEAT_FOLDING
514 if (invalid)
515 {
516 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200517 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200519 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521#endif
522}
523
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200524 void
525reset_updating_screen(int may_resize_shell UNUSED)
526{
527 updating_screen = FALSE;
528#ifdef FEAT_GUI
529 if (may_resize_shell)
530 gui_may_resize_shell();
531#endif
532#ifdef FEAT_TERMINAL
533 term_check_channel_closed_recently();
534#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200535
536#ifdef HAVE_DROP_FILE
537 // If handle_drop() was called while updating_screen was TRUE need to
538 // handle the drop now.
539 handle_any_postponed_drop();
540#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200541}
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 */
546 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100547update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548{
549 redraw_curbuf_later(type);
550 update_screen(type);
551}
552
553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 * Based on the current value of curwin->w_topline, transfer a screenfull
555 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200556 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200558 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 win_T *wp;
563 static int did_intro = FALSE;
564#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
565 int did_one;
566#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200567#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200568 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200569 int gui_cursor_col;
570 int gui_cursor_row;
571#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200572 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000574 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200578 if (type == VALID_NO_UPDATE)
579 {
580 no_update = TRUE;
581 type = 0;
582 }
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (must_redraw)
585 {
586 if (type < must_redraw) /* use maximal type */
587 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000588
589 /* must_redraw is reset here, so that when we run into some weird
590 * reason to redraw while busy redrawing (e.g., asynchronous
591 * scrolling), or update_topline() in win_update() will cause a
592 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 must_redraw = 0;
594 }
595
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200596 /* May need to update w_lines[]. */
597 if (curwin->w_lines_valid == 0 && type < NOT_VALID
598#ifdef FEAT_TERMINAL
599 && !term_do_update_window(curwin)
600#endif
601 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 type = NOT_VALID;
603
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000604 /* Postpone the redrawing when it's not needed and when being called
605 * recursively. */
606 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {
608 redraw_later(type); /* remember type for next time */
609 must_redraw = type;
610 if (type > INVERTED_ALL)
611 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
614
615 updating_screen = TRUE;
616#ifdef FEAT_SYN_HL
617 ++display_tick; /* let syntax code know we're in a next round of
618 * display updating */
619#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200620 if (no_update)
621 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 /*
624 * if the screen was scrolled up when displaying a message, scroll it down
625 */
626 if (msg_scrolled)
627 {
628 clear_cmdline = TRUE;
629 if (msg_scrolled > Rows - 5) /* clearing is faster */
630 type = CLEAR;
631 else if (type != CLEAR)
632 {
633 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200634 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
635 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 type = CLEAR;
637 FOR_ALL_WINDOWS(wp)
638 {
639 if (W_WINROW(wp) < msg_scrolled)
640 {
641 if (W_WINROW(wp) + wp->w_height > msg_scrolled
642 && wp->w_redr_type < REDRAW_TOP
643 && wp->w_lines_valid > 0
644 && wp->w_topline == wp->w_lines[0].wl_lnum)
645 {
646 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
647 wp->w_redr_type = REDRAW_TOP;
648 }
649 else
650 {
651 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200652 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
653 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 }
657 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200658 if (!no_update)
659 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000660 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 msg_scrolled = 0;
663 need_wait_return = FALSE;
664 }
665
666 /* reset cmdline_row now (may have been changed temporarily) */
667 compute_cmdrow();
668
669 /* Check for changed highlighting */
670 if (need_highlight_changed)
671 highlight_changed();
672
673 if (type == CLEAR) /* first clear screen */
674 {
675 screenclear(); /* will reset clear_cmdline */
676 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200677 /* must_redraw may be set indirectly, avoid another redraw later */
678 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680
681 if (clear_cmdline) /* going to clear cmdline (done below) */
682 check_for_delay(FALSE);
683
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000684#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200685 /* Force redraw when width of 'number' or 'relativenumber' column
686 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000687 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200688 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
689 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000690 curwin->w_redr_type = NOT_VALID;
691#endif
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 /*
694 * Only start redrawing if there is really something to do.
695 */
696 if (type == INVERTED)
697 update_curswant();
698 if (curwin->w_redr_type < type
699 && !((type == VALID
700 && curwin->w_lines[0].wl_valid
701#ifdef FEAT_DIFF
702 && curwin->w_topfill == curwin->w_old_topfill
703 && curwin->w_botfill == curwin->w_old_botfill
704#endif
705 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000707 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
709 && curwin->w_old_visual_mode == VIsual_mode
710 && (curwin->w_valid & VALID_VIRTCOL)
711 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 ))
713 curwin->w_redr_type = type;
714
Bram Moolenaar5a305422006-04-28 22:38:25 +0000715 /* Redraw the tab pages line if needed. */
716 if (redraw_tabline || type >= NOT_VALID)
717 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef FEAT_SYN_HL
720 /*
721 * Correct stored syntax highlighting info for changes in each displayed
722 * buffer. Each buffer must only be done once.
723 */
724 FOR_ALL_WINDOWS(wp)
725 {
726 if (wp->w_buffer->b_mod_set)
727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 win_T *wwp;
729
730 /* Check if we already did this buffer. */
731 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
732 if (wwp->w_buffer == wp->w_buffer)
733 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200734 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 syn_stack_apply_changes(wp->w_buffer);
736 }
737 }
738#endif
739
740 /*
741 * Go from top to bottom through the windows, redrawing the ones that need
742 * it.
743 */
744#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
745 did_one = FALSE;
746#endif
747#ifdef FEAT_SEARCH_EXTRA
748 search_hl.rm.regprog = NULL;
749#endif
750 FOR_ALL_WINDOWS(wp)
751 {
752 if (wp->w_redr_type != 0)
753 {
754 cursor_off();
755#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
756 if (!did_one)
757 {
758 did_one = TRUE;
759# ifdef FEAT_SEARCH_EXTRA
760 start_search_hl();
761# endif
762# ifdef FEAT_CLIPBOARD
763 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200764 if (clip_star.available && clip_isautosel_star())
765 clip_update_selection(&clip_star);
766 if (clip_plus.available && clip_isautosel_plus())
767 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768# endif
769#ifdef FEAT_GUI
770 /* Remove the cursor before starting to do anything, because
771 * scrolling may make it difficult to redraw the text under
772 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200773 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 {
775 gui_cursor_col = gui.cursor_col;
776 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200778 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 }
782#endif
783 win_update(wp);
784 }
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* redraw status line after the window to minimize cursor movement */
787 if (wp->w_redr_status)
788 {
789 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200790 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793#if defined(FEAT_SEARCH_EXTRA)
794 end_search_hl();
795#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100796#ifdef FEAT_INS_EXPAND
797 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200798 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* Reset b_mod_set flags. Going through all windows is probably faster
802 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200803 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200806 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
808 /* Clear or redraw the command line. Done last, because scrolling may
809 * mess up the command line. */
810 if (clear_cmdline || redraw_cmdline)
811 showmode();
812
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200813 if (no_update)
814 --no_win_do_lines_ins;
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200817 if (!did_intro)
818 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 did_intro = TRUE;
820
821#ifdef FEAT_GUI
822 /* Redraw the cursor and update the scrollbars when all screen updating is
823 * done. */
824 if (gui.in_use)
825 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200826 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200827 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100828 mch_disable_flush();
829 out_flush(); /* required before updating the cursor */
830 mch_enable_flush();
831
Bram Moolenaar144445d2016-07-08 21:41:54 +0200832 /* Put the GUI position where the cursor was, gui_update_cursor()
833 * uses that. */
834 gui.col = gui_cursor_col;
835 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200836# ifdef FEAT_MBYTE
837 gui.col = mb_fix_col(gui.col, gui.row);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100840 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200841 screen_cur_col = gui.col;
842 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200843 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100844 else
845 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_scrollbars(FALSE);
847 }
848#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200849 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850}
851
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100852#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
853/*
854 * Prepare for updating one or more windows.
855 * Caller must check for "updating_screen" already set to avoid recursiveness.
856 */
857 static void
858update_prepare(void)
859{
860 cursor_off();
861 updating_screen = TRUE;
862#ifdef FEAT_GUI
863 /* Remove the cursor before starting to do anything, because scrolling may
864 * make it difficult to redraw the text under it. */
865 if (gui.in_use)
866 gui_undraw_cursor();
867#endif
868#ifdef FEAT_SEARCH_EXTRA
869 start_search_hl();
870#endif
871}
872
873/*
874 * Finish updating one or more windows.
875 */
876 static void
877update_finish(void)
878{
879 if (redraw_cmdline)
880 showmode();
881
882# ifdef FEAT_SEARCH_EXTRA
883 end_search_hl();
884# endif
885
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200886 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887
888# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100889 /* Redraw the cursor and update the scrollbars when all screen updating is
890 * done. */
891 if (gui.in_use)
892 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100893 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894 gui_update_scrollbars(FALSE);
895 }
896# endif
897}
898#endif
899
Bram Moolenaar860cae12010-06-05 23:22:07 +0200900#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200901/*
902 * Return TRUE if the cursor line in window "wp" may be concealed, according
903 * to the 'concealcursor' option.
904 */
905 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100906conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200907{
908 int c;
909
910 if (*wp->w_p_cocu == NUL)
911 return FALSE;
912 if (get_real_state() & VISUAL)
913 c = 'v';
914 else if (State & INSERT)
915 c = 'i';
916 else if (State & NORMAL)
917 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200918 else if (State & CMDLINE)
919 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200920 else
921 return FALSE;
922 return vim_strchr(wp->w_p_cocu, c) != NULL;
923}
924
925/*
926 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
927 */
928 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200929conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200930{
931 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
932 {
933 need_cursor_line_redraw = TRUE;
934 /* Need to recompute cursor column, e.g., when starting Visual mode
935 * without concealing. */
936 curs_columns(TRUE);
937 }
938}
939
Bram Moolenaar860cae12010-06-05 23:22:07 +0200940 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100941update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200942{
943 int row;
944 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200945#ifdef SYN_TIME_LIMIT
946 proftime_T syntax_tm;
947#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948
Bram Moolenaar908be432016-05-24 10:51:30 +0200949 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100950 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200951 return;
952
Bram Moolenaar860cae12010-06-05 23:22:07 +0200953 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200954 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200955 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200956#ifdef SYN_TIME_LIMIT
957 /* Set the time limit to 'redrawtime'. */
958 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200959 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200960#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100961 update_prepare();
962
Bram Moolenaar860cae12010-06-05 23:22:07 +0200963 row = 0;
964 for (j = 0; j < wp->w_lines_valid; ++j)
965 {
966 if (lnum == wp->w_lines[j].wl_lnum)
967 {
968 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200969# ifdef FEAT_SEARCH_EXTRA
970 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200971 start_search_hl();
972 prepare_search_hl(wp, lnum);
973# endif
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200974 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200975# if defined(FEAT_SEARCH_EXTRA)
976 end_search_hl();
977# endif
978 break;
979 }
980 row += wp->w_lines[j].wl_size;
981 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100982
983 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200984
985#ifdef SYN_TIME_LIMIT
986 syn_set_timeout(NULL);
987#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200988 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200989 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990}
991#endif
992
993#if defined(FEAT_SIGNS) || defined(PROTO)
994 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100995update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996{
997 win_T *wp;
998 int doit = FALSE;
999
1000# ifdef FEAT_FOLDING
1001 win_foldinfo.fi_level = 0;
1002# endif
1003
1004 /* update/delete a specific mark */
1005 FOR_ALL_WINDOWS(wp)
1006 {
1007 if (buf != NULL && lnum > 0)
1008 {
1009 if (wp->w_buffer == buf && lnum >= wp->w_topline
1010 && lnum < wp->w_botline)
1011 {
1012 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1013 wp->w_redraw_top = lnum;
1014 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1015 wp->w_redraw_bot = lnum;
1016 redraw_win_later(wp, VALID);
1017 }
1018 }
1019 else
1020 redraw_win_later(wp, VALID);
1021 if (wp->w_redr_type != 0)
1022 doit = TRUE;
1023 }
1024
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001025 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001026 * happening (recursive call), messages on the screen or still starting up.
1027 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001028 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001029 || State == ASKMORE || State == HITRETURN
1030 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001031#ifdef FEAT_GUI
1032 || gui.starting
1033#endif
1034 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 return;
1036
1037 /* update all windows that need updating */
1038 update_prepare();
1039
Bram Moolenaar29323592016-07-24 22:04:11 +02001040 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 if (wp->w_redr_type != 0)
1043 win_update(wp);
1044 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001045 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
1048 update_finish();
1049}
1050#endif
1051
1052
1053#if defined(FEAT_GUI) || defined(PROTO)
1054/*
1055 * Update a single window, its status line and maybe the command line msg.
1056 * Used for the GUI scrollbar.
1057 */
1058 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001059updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001061 /* return if already busy updating */
1062 if (updating_screen)
1063 return;
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 update_prepare();
1066
1067#ifdef FEAT_CLIPBOARD
1068 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001069 if (clip_star.available && clip_isautosel_star())
1070 clip_update_selection(&clip_star);
1071 if (clip_plus.available && clip_isautosel_plus())
1072 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001076
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001077 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001078 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001079 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (wp->w_redr_status
1082# ifdef FEAT_CMDL_INFO
1083 || p_ru
1084# endif
1085# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001086 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087# endif
1088 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001089 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 update_finish();
1092}
1093#endif
1094
1095/*
1096 * Update a single window.
1097 *
1098 * This may cause the windows below it also to be redrawn (when clearing the
1099 * screen or scrolling lines).
1100 *
1101 * How the window is redrawn depends on wp->w_redr_type. Each type also
1102 * implies the one below it.
1103 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001104 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1106 * INVERTED redraw the changed part of the Visual area
1107 * INVERTED_ALL redraw the whole Visual area
1108 * VALID 1. scroll up/down to adjust for a changed w_topline
1109 * 2. update lines at the top when scrolled down
1110 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001111 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 * b_mod_top and b_mod_bot.
1113 * - if wp->w_redraw_top non-zero, redraw lines between
1114 * wp->w_redraw_top and wp->w_redr_bot.
1115 * - continue redrawing when syntax status is invalid.
1116 * 4. if scrolled up, update lines at the bottom.
1117 * This results in three areas that may need updating:
1118 * top: from first row to top_end (when scrolled down)
1119 * mid: from mid_start to mid_end (update inversion or changed text)
1120 * bot: from bot_start to last row (when scrolled up)
1121 */
1122 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001123win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 buf_T *buf = wp->w_buffer;
1126 int type;
1127 int top_end = 0; /* Below last row of the top area that needs
1128 updating. 0 when no top area updating. */
1129 int mid_start = 999;/* first row of the mid area that needs
1130 updating. 999 when no mid area updating. */
1131 int mid_end = 0; /* Below last row of the mid area that needs
1132 updating. 0 when no mid area updating. */
1133 int bot_start = 999;/* first row of the bot area that needs
1134 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 int scrolled_down = FALSE; /* TRUE when scrolled down when
1136 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001138 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 int top_to_mod = FALSE; /* redraw above mod_top */
1140#endif
1141
1142 int row; /* current window row to display */
1143 linenr_T lnum; /* current buffer lnum to display */
1144 int idx; /* current index in w_lines[] */
1145 int srow; /* starting row of the current line */
1146
1147 int eof = FALSE; /* if TRUE, we hit the end of the file */
1148 int didline = FALSE; /* if TRUE, we finished the last line */
1149 int i;
1150 long j;
1151 static int recursive = FALSE; /* being called recursively */
1152 int old_botline = wp->w_botline;
1153#ifdef FEAT_FOLDING
1154 long fold_count;
1155#endif
1156#ifdef FEAT_SYN_HL
1157 /* remember what happened to the previous line, to know if
1158 * check_visual_highlight() can be used */
1159#define DID_NONE 1 /* didn't update a line */
1160#define DID_LINE 2 /* updated a normal line */
1161#define DID_FOLD 3 /* updated a folded line */
1162 int did_update = DID_NONE;
1163 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1164#endif
1165 linenr_T mod_top = 0;
1166 linenr_T mod_bot = 0;
1167#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1168 int save_got_int;
1169#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001170#ifdef SYN_TIME_LIMIT
1171 proftime_T syntax_tm;
1172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 type = wp->w_redr_type;
1175
1176 if (type == NOT_VALID)
1177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 wp->w_lines_valid = 0;
1180 }
1181
1182 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001183 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
1185 wp->w_redr_type = 0;
1186 return;
1187 }
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 /* Window is zero-width: Only need to draw the separator. */
1190 if (wp->w_width == 0)
1191 {
1192 /* draw the vertical separator right of this window */
1193 draw_vsep_win(wp, 0);
1194 wp->w_redr_type = 0;
1195 return;
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001198#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001199 // If this window contains a terminal, redraw works completely differently.
1200 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001201 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001202 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001203# ifdef FEAT_MENU
1204 /* Draw the window toolbar, if there is one. */
1205 if (winbar_height(wp) > 0)
1206 redraw_win_toolbar(wp);
1207# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001208 wp->w_redr_type = 0;
1209 return;
1210 }
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001214 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001217#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001218 /* Force redraw when width of 'number' or 'relativenumber' column
1219 * changes. */
1220 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001221 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001222 {
1223 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001224 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001225 }
1226 else
1227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1230 {
1231 /*
1232 * When there are both inserted/deleted lines and specific lines to be
1233 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1234 * everything (only happens when redrawing is off for while).
1235 */
1236 type = NOT_VALID;
1237 }
1238 else
1239 {
1240 /*
1241 * Set mod_top to the first line that needs displaying because of
1242 * changes. Set mod_bot to the first line after the changes.
1243 */
1244 mod_top = wp->w_redraw_top;
1245 if (wp->w_redraw_bot != 0)
1246 mod_bot = wp->w_redraw_bot + 1;
1247 else
1248 mod_bot = 0;
1249 wp->w_redraw_top = 0; /* reset for next time */
1250 wp->w_redraw_bot = 0;
1251 if (buf->b_mod_set)
1252 {
1253 if (mod_top == 0 || mod_top > buf->b_mod_top)
1254 {
1255 mod_top = buf->b_mod_top;
1256#ifdef FEAT_SYN_HL
1257 /* Need to redraw lines above the change that may be included
1258 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (mod_top < 1)
1263 mod_top = 1;
1264 }
1265#endif
1266 }
1267 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1268 mod_bot = buf->b_mod_bot;
1269
1270#ifdef FEAT_SEARCH_EXTRA
1271 /* When 'hlsearch' is on and using a multi-line search pattern, a
1272 * change in one line may make the Search highlighting in a
1273 * previous line invalid. Simple solution: redraw all visible
1274 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001277 if (search_hl.rm.regprog != NULL
1278 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001280 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001281 {
1282 cur = wp->w_match_head;
1283 while (cur != NULL)
1284 {
1285 if (cur->match.regprog != NULL
1286 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001287 {
1288 top_to_mod = TRUE;
1289 break;
1290 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001291 cur = cur->next;
1292 }
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294#endif
1295 }
1296#ifdef FEAT_FOLDING
1297 if (mod_top != 0 && hasAnyFolding(wp))
1298 {
1299 linenr_T lnumt, lnumb;
1300
1301 /*
1302 * A change in a line can cause lines above it to become folded or
1303 * unfolded. Find the top most buffer line that may be affected.
1304 * If the line was previously folded and displayed, get the first
1305 * line of that fold. If the line is folded now, get the first
1306 * folded line. Use the minimum of these two.
1307 */
1308
1309 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1310 * the line below it. If there is no valid entry, use w_topline.
1311 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1312 * to this line. If there is no valid entry, use MAXLNUM. */
1313 lnumt = wp->w_topline;
1314 lnumb = MAXLNUM;
1315 for (i = 0; i < wp->w_lines_valid; ++i)
1316 if (wp->w_lines[i].wl_valid)
1317 {
1318 if (wp->w_lines[i].wl_lastlnum < mod_top)
1319 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1320 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1321 {
1322 lnumb = wp->w_lines[i].wl_lnum;
1323 /* When there is a fold column it might need updating
1324 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001325 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 ++lnumb;
1327 }
1328 }
1329
1330 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1331 if (mod_top > lnumt)
1332 mod_top = lnumt;
1333
1334 /* Now do the same for the bottom line (one above mod_bot). */
1335 --mod_bot;
1336 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1337 ++mod_bot;
1338 if (mod_bot < lnumb)
1339 mod_bot = lnumb;
1340 }
1341#endif
1342
1343 /* When a change starts above w_topline and the end is below
1344 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001345 * If the end of the change is above w_topline: do like no change was
1346 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (mod_top != 0 && mod_top < wp->w_topline)
1348 {
1349 if (mod_bot > wp->w_topline)
1350 mod_top = wp->w_topline;
1351#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001352 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 top_end = 1;
1354#endif
1355 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001356
1357 /* When line numbers are displayed need to redraw all lines below
1358 * inserted/deleted lines. */
1359 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1360 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362
1363 /*
1364 * When only displaying the lines at the top, set top_end. Used when
1365 * window has scrolled down for msg_scrolled.
1366 */
1367 if (type == REDRAW_TOP)
1368 {
1369 j = 0;
1370 for (i = 0; i < wp->w_lines_valid; ++i)
1371 {
1372 j += wp->w_lines[i].wl_size;
1373 if (j >= wp->w_upd_rows)
1374 {
1375 top_end = j;
1376 break;
1377 }
1378 }
1379 if (top_end == 0)
1380 /* not found (cannot happen?): redraw everything */
1381 type = NOT_VALID;
1382 else
1383 /* top area defined, the rest is VALID */
1384 type = VALID;
1385 }
1386
Bram Moolenaar367329b2007-08-30 11:53:22 +00001387 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001388 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1389 * non-zero and thus not FALSE) will indicate that screenclear() was not
1390 * called. */
1391 if (screen_cleared)
1392 screen_cleared = MAYBE;
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /*
1395 * If there are no changes on the screen that require a complete redraw,
1396 * handle three cases:
1397 * 1: we are off the top of the screen by a few lines: scroll down
1398 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1399 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1400 * w_lines[] that needs updating.
1401 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001402 if ((type == VALID || type == SOME_VALID
1403 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_DIFF
1405 && !wp->w_botfill && !wp->w_old_botfill
1406#endif
1407 )
1408 {
1409 if (mod_top != 0 && wp->w_topline == mod_top)
1410 {
1411 /*
1412 * w_topline is the first changed line, the scrolling will be done
1413 * further down.
1414 */
1415 }
1416 else if (wp->w_lines[0].wl_valid
1417 && (wp->w_topline < wp->w_lines[0].wl_lnum
1418#ifdef FEAT_DIFF
1419 || (wp->w_topline == wp->w_lines[0].wl_lnum
1420 && wp->w_topfill > wp->w_old_topfill)
1421#endif
1422 ))
1423 {
1424 /*
1425 * New topline is above old topline: May scroll down.
1426 */
1427#ifdef FEAT_FOLDING
1428 if (hasAnyFolding(wp))
1429 {
1430 linenr_T ln;
1431
1432 /* count the number of lines we are off, counting a sequence
1433 * of folded lines as one */
1434 j = 0;
1435 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1436 {
1437 ++j;
1438 if (j >= wp->w_height - 2)
1439 break;
1440 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1441 }
1442 }
1443 else
1444#endif
1445 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1446 if (j < wp->w_height - 2) /* not too far off */
1447 {
1448 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1449#ifdef FEAT_DIFF
1450 /* insert extra lines for previously invisible filler lines */
1451 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1452 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1453 - wp->w_old_topfill;
1454#endif
1455 if (i < wp->w_height - 2) /* less than a screen off */
1456 {
1457 /*
1458 * Try to insert the correct number of lines.
1459 * If not the last window, delete the lines at the bottom.
1460 * win_ins_lines may fail when the terminal can't do it.
1461 */
1462 if (i > 0)
1463 check_for_delay(FALSE);
1464 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1465 {
1466 if (wp->w_lines_valid != 0)
1467 {
1468 /* Need to update rows that are new, stop at the
1469 * first one that scrolled down. */
1470 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 /* Move the entries that were scrolled, disable
1474 * the entries for the lines to be redrawn. */
1475 if ((wp->w_lines_valid += j) > wp->w_height)
1476 wp->w_lines_valid = wp->w_height;
1477 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1478 wp->w_lines[idx] = wp->w_lines[idx - j];
1479 while (idx >= 0)
1480 wp->w_lines[idx--].wl_valid = FALSE;
1481 }
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 mid_start = 0; /* redraw all lines */
1488 }
1489 else
1490 mid_start = 0; /* redraw all lines */
1491 }
1492 else
1493 {
1494 /*
1495 * New topline is at or below old topline: May scroll up.
1496 * When topline didn't change, find first entry in w_lines[] that
1497 * needs updating.
1498 */
1499
1500 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1501 j = -1;
1502 row = 0;
1503 for (i = 0; i < wp->w_lines_valid; i++)
1504 {
1505 if (wp->w_lines[i].wl_valid
1506 && wp->w_lines[i].wl_lnum == wp->w_topline)
1507 {
1508 j = i;
1509 break;
1510 }
1511 row += wp->w_lines[i].wl_size;
1512 }
1513 if (j == -1)
1514 {
1515 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1516 * lines */
1517 mid_start = 0;
1518 }
1519 else
1520 {
1521 /*
1522 * Try to delete the correct number of lines.
1523 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1524 */
1525#ifdef FEAT_DIFF
1526 /* If the topline didn't change, delete old filler lines,
1527 * otherwise delete filler lines of the new topline... */
1528 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1529 row += wp->w_old_topfill;
1530 else
1531 row += diff_check_fill(wp, wp->w_topline);
1532 /* ... but don't delete new filler lines. */
1533 row -= wp->w_topfill;
1534#endif
1535 if (row > 0)
1536 {
1537 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001538 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1539 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 bot_start = wp->w_height - row;
1541 else
1542 mid_start = 0; /* redraw all lines */
1543 }
1544 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1545 {
1546 /*
1547 * Skip the lines (below the deleted lines) that are still
1548 * valid and don't need redrawing. Copy their info
1549 * upwards, to compensate for the deleted lines. Set
1550 * bot_start to the first row that needs redrawing.
1551 */
1552 bot_start = 0;
1553 idx = 0;
1554 for (;;)
1555 {
1556 wp->w_lines[idx] = wp->w_lines[j];
1557 /* stop at line that didn't fit, unless it is still
1558 * valid (no lines deleted) */
1559 if (row > 0 && bot_start + row
1560 + (int)wp->w_lines[j].wl_size > wp->w_height)
1561 {
1562 wp->w_lines_valid = idx + 1;
1563 break;
1564 }
1565 bot_start += wp->w_lines[idx++].wl_size;
1566
1567 /* stop at the last valid entry in w_lines[].wl_size */
1568 if (++j >= wp->w_lines_valid)
1569 {
1570 wp->w_lines_valid = idx;
1571 break;
1572 }
1573 }
1574#ifdef FEAT_DIFF
1575 /* Correct the first entry for filler lines at the top
1576 * when it won't get updated below. */
1577 if (wp->w_p_diff && bot_start > 0)
1578 wp->w_lines[0].wl_size =
1579 plines_win_nofill(wp, wp->w_topline, TRUE)
1580 + wp->w_topfill;
1581#endif
1582 }
1583 }
1584 }
1585
1586 /* When starting redraw in the first line, redraw all lines. When
1587 * there is only one window it's probably faster to clear the screen
1588 * first. */
1589 if (mid_start == 0)
1590 {
1591 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001592 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001594 /* Clear the screen when it was not done by win_del_lines() or
1595 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1596 * then. */
1597 if (screen_cleared != TRUE)
1598 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001599 /* The screen was cleared, redraw the tab pages line. */
1600 if (redraw_tabline)
1601 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001604
1605 /* When win_del_lines() or win_ins_lines() caused the screen to be
1606 * cleared (only happens for the first window) or when screenclear()
1607 * was called directly above, "must_redraw" will have been set to
1608 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1609 if (screen_cleared == TRUE)
1610 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 else
1613 {
1614 /* Not VALID or INVERTED: redraw all lines. */
1615 mid_start = 0;
1616 mid_end = wp->w_height;
1617 }
1618
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001619 if (type == SOME_VALID)
1620 {
1621 /* SOME_VALID: redraw all lines. */
1622 mid_start = 0;
1623 mid_end = wp->w_height;
1624 type = NOT_VALID;
1625 }
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 /* check if we are updating or removing the inverted part */
1628 if ((VIsual_active && buf == curwin->w_buffer)
1629 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1630 {
1631 linenr_T from, to;
1632
1633 if (VIsual_active)
1634 {
1635 if (VIsual_active
1636 && (VIsual_mode != wp->w_old_visual_mode
1637 || type == INVERTED_ALL))
1638 {
1639 /*
1640 * If the type of Visual selection changed, redraw the whole
1641 * selection. Also when the ownership of the X selection is
1642 * gained or lost.
1643 */
1644 if (curwin->w_cursor.lnum < VIsual.lnum)
1645 {
1646 from = curwin->w_cursor.lnum;
1647 to = VIsual.lnum;
1648 }
1649 else
1650 {
1651 from = VIsual.lnum;
1652 to = curwin->w_cursor.lnum;
1653 }
1654 /* redraw more when the cursor moved as well */
1655 if (wp->w_old_cursor_lnum < from)
1656 from = wp->w_old_cursor_lnum;
1657 if (wp->w_old_cursor_lnum > to)
1658 to = wp->w_old_cursor_lnum;
1659 if (wp->w_old_visual_lnum < from)
1660 from = wp->w_old_visual_lnum;
1661 if (wp->w_old_visual_lnum > to)
1662 to = wp->w_old_visual_lnum;
1663 }
1664 else
1665 {
1666 /*
1667 * Find the line numbers that need to be updated: The lines
1668 * between the old cursor position and the current cursor
1669 * position. Also check if the Visual position changed.
1670 */
1671 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1672 {
1673 from = curwin->w_cursor.lnum;
1674 to = wp->w_old_cursor_lnum;
1675 }
1676 else
1677 {
1678 from = wp->w_old_cursor_lnum;
1679 to = curwin->w_cursor.lnum;
1680 if (from == 0) /* Visual mode just started */
1681 from = to;
1682 }
1683
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001684 if (VIsual.lnum != wp->w_old_visual_lnum
1685 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 if (wp->w_old_visual_lnum < from
1688 && wp->w_old_visual_lnum != 0)
1689 from = wp->w_old_visual_lnum;
1690 if (wp->w_old_visual_lnum > to)
1691 to = wp->w_old_visual_lnum;
1692 if (VIsual.lnum < from)
1693 from = VIsual.lnum;
1694 if (VIsual.lnum > to)
1695 to = VIsual.lnum;
1696 }
1697 }
1698
1699 /*
1700 * If in block mode and changed column or curwin->w_curswant:
1701 * update all lines.
1702 * First compute the actual start and end column.
1703 */
1704 if (VIsual_mode == Ctrl_V)
1705 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001706 colnr_T fromc, toc;
1707#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1708 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar404406a2014-10-09 13:24:43 +02001710 if (curwin->w_p_lbr)
1711 ve_flags = VE_ALL;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001714#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1715 ve_flags = save_ve_flags;
1716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 ++toc;
1718 if (curwin->w_curswant == MAXCOL)
1719 toc = MAXCOL;
1720
1721 if (fromc != wp->w_old_cursor_fcol
1722 || toc != wp->w_old_cursor_lcol)
1723 {
1724 if (from > VIsual.lnum)
1725 from = VIsual.lnum;
1726 if (to < VIsual.lnum)
1727 to = VIsual.lnum;
1728 }
1729 wp->w_old_cursor_fcol = fromc;
1730 wp->w_old_cursor_lcol = toc;
1731 }
1732 }
1733 else
1734 {
1735 /* Use the line numbers of the old Visual area. */
1736 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1737 {
1738 from = wp->w_old_cursor_lnum;
1739 to = wp->w_old_visual_lnum;
1740 }
1741 else
1742 {
1743 from = wp->w_old_visual_lnum;
1744 to = wp->w_old_cursor_lnum;
1745 }
1746 }
1747
1748 /*
1749 * There is no need to update lines above the top of the window.
1750 */
1751 if (from < wp->w_topline)
1752 from = wp->w_topline;
1753
1754 /*
1755 * If we know the value of w_botline, use it to restrict the update to
1756 * the lines that are visible in the window.
1757 */
1758 if (wp->w_valid & VALID_BOTLINE)
1759 {
1760 if (from >= wp->w_botline)
1761 from = wp->w_botline - 1;
1762 if (to >= wp->w_botline)
1763 to = wp->w_botline - 1;
1764 }
1765
1766 /*
1767 * Find the minimal part to be updated.
1768 * Watch out for scrolling that made entries in w_lines[] invalid.
1769 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1770 * top_end; need to redraw from top_end to the "to" line.
1771 * A middle mouse click with a Visual selection may change the text
1772 * above the Visual area and reset wl_valid, do count these for
1773 * mid_end (in srow).
1774 */
1775 if (mid_start > 0)
1776 {
1777 lnum = wp->w_topline;
1778 idx = 0;
1779 srow = 0;
1780 if (scrolled_down)
1781 mid_start = top_end;
1782 else
1783 mid_start = 0;
1784 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1785 {
1786 if (wp->w_lines[idx].wl_valid)
1787 mid_start += wp->w_lines[idx].wl_size;
1788 else if (!scrolled_down)
1789 srow += wp->w_lines[idx].wl_size;
1790 ++idx;
1791# ifdef FEAT_FOLDING
1792 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1793 lnum = wp->w_lines[idx].wl_lnum;
1794 else
1795# endif
1796 ++lnum;
1797 }
1798 srow += mid_start;
1799 mid_end = wp->w_height;
1800 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1801 {
1802 if (wp->w_lines[idx].wl_valid
1803 && wp->w_lines[idx].wl_lnum >= to + 1)
1804 {
1805 /* Only update until first row of this line */
1806 mid_end = srow;
1807 break;
1808 }
1809 srow += wp->w_lines[idx].wl_size;
1810 }
1811 }
1812 }
1813
1814 if (VIsual_active && buf == curwin->w_buffer)
1815 {
1816 wp->w_old_visual_mode = VIsual_mode;
1817 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1818 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001819 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 wp->w_old_curswant = curwin->w_curswant;
1821 }
1822 else
1823 {
1824 wp->w_old_visual_mode = 0;
1825 wp->w_old_cursor_lnum = 0;
1826 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001827 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1831 /* reset got_int, otherwise regexp won't work */
1832 save_got_int = got_int;
1833 got_int = 0;
1834#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001835#ifdef SYN_TIME_LIMIT
1836 /* Set the time limit to 'redrawtime'. */
1837 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001838 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_FOLDING
1841 win_foldinfo.fi_level = 0;
1842#endif
1843
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001844#ifdef FEAT_MENU
1845 /*
1846 * Draw the window toolbar, if there is one.
1847 * TODO: only when needed.
1848 */
1849 if (winbar_height(wp) > 0)
1850 redraw_win_toolbar(wp);
1851#endif
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 /*
1854 * Update all the window rows.
1855 */
1856 idx = 0; /* first entry in w_lines[].wl_size */
1857 row = 0;
1858 srow = 0;
1859 lnum = wp->w_topline; /* first line shown in window */
1860 for (;;)
1861 {
1862 /* stop updating when reached the end of the window (check for _past_
1863 * the end of the window is at the end of the loop) */
1864 if (row == wp->w_height)
1865 {
1866 didline = TRUE;
1867 break;
1868 }
1869
1870 /* stop updating when hit the end of the file */
1871 if (lnum > buf->b_ml.ml_line_count)
1872 {
1873 eof = TRUE;
1874 break;
1875 }
1876
1877 /* Remember the starting row of the line that is going to be dealt
1878 * with. It is used further down when the line doesn't fit. */
1879 srow = row;
1880
1881 /*
1882 * Update a line when it is in an area that needs updating, when it
1883 * has changes or w_lines[idx] is invalid.
1884 * bot_start may be halfway a wrapped line after using
1885 * win_del_lines(), check if the current line includes it.
1886 * When syntax folding is being used, the saved syntax states will
1887 * already have been updated, we can't see where the syntax state is
1888 * the same again, just update until the end of the window.
1889 */
1890 if (row < top_end
1891 || (row >= mid_start && row < mid_end)
1892#ifdef FEAT_SEARCH_EXTRA
1893 || top_to_mod
1894#endif
1895 || idx >= wp->w_lines_valid
1896 || (row + wp->w_lines[idx].wl_size > bot_start)
1897 || (mod_top != 0
1898 && (lnum == mod_top
1899 || (lnum >= mod_top
1900 && (lnum < mod_bot
1901#ifdef FEAT_SYN_HL
1902 || did_update == DID_FOLD
1903 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001904 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 && (
1906# ifdef FEAT_FOLDING
1907 (foldmethodIsSyntax(wp)
1908 && hasAnyFolding(wp)) ||
1909# endif
1910 syntax_check_changed(lnum)))
1911#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001912#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001913 /* match in fixed position might need redraw
1914 * if lines were inserted or deleted */
1915 || (wp->w_match_head != NULL
1916 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 )))))
1919 {
1920#ifdef FEAT_SEARCH_EXTRA
1921 if (lnum == mod_top)
1922 top_to_mod = FALSE;
1923#endif
1924
1925 /*
1926 * When at start of changed lines: May scroll following lines
1927 * up or down to minimize redrawing.
1928 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001929 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 if (lnum == mod_top
1932 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001933 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
1935 int old_rows = 0;
1936 int new_rows = 0;
1937 int xtra_rows;
1938 linenr_T l;
1939
1940 /* Count the old number of window rows, using w_lines[], which
1941 * should still contain the sizes for the lines as they are
1942 * currently displayed. */
1943 for (i = idx; i < wp->w_lines_valid; ++i)
1944 {
1945 /* Only valid lines have a meaningful wl_lnum. Invalid
1946 * lines are part of the changed area. */
1947 if (wp->w_lines[i].wl_valid
1948 && wp->w_lines[i].wl_lnum == mod_bot)
1949 break;
1950 old_rows += wp->w_lines[i].wl_size;
1951#ifdef FEAT_FOLDING
1952 if (wp->w_lines[i].wl_valid
1953 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1954 {
1955 /* Must have found the last valid entry above mod_bot.
1956 * Add following invalid entries. */
1957 ++i;
1958 while (i < wp->w_lines_valid
1959 && !wp->w_lines[i].wl_valid)
1960 old_rows += wp->w_lines[i++].wl_size;
1961 break;
1962 }
1963#endif
1964 }
1965
1966 if (i >= wp->w_lines_valid)
1967 {
1968 /* We can't find a valid line below the changed lines,
1969 * need to redraw until the end of the window.
1970 * Inserting/deleting lines has no use. */
1971 bot_start = 0;
1972 }
1973 else
1974 {
1975 /* Able to count old number of rows: Count new window
1976 * rows, and may insert/delete lines */
1977 j = idx;
1978 for (l = lnum; l < mod_bot; ++l)
1979 {
1980#ifdef FEAT_FOLDING
1981 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1982 ++new_rows;
1983 else
1984#endif
1985#ifdef FEAT_DIFF
1986 if (l == wp->w_topline)
1987 new_rows += plines_win_nofill(wp, l, TRUE)
1988 + wp->w_topfill;
1989 else
1990#endif
1991 new_rows += plines_win(wp, l, TRUE);
1992 ++j;
1993 if (new_rows > wp->w_height - row - 2)
1994 {
1995 /* it's getting too much, must redraw the rest */
1996 new_rows = 9999;
1997 break;
1998 }
1999 }
2000 xtra_rows = new_rows - old_rows;
2001 if (xtra_rows < 0)
2002 {
2003 /* May scroll text up. If there is not enough
2004 * remaining text or scrolling fails, must redraw the
2005 * rest. If scrolling works, must redraw the text
2006 * below the scrolled text. */
2007 if (row - xtra_rows >= wp->w_height - 2)
2008 mod_bot = MAXLNUM;
2009 else
2010 {
2011 check_for_delay(FALSE);
2012 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002013 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 mod_bot = MAXLNUM;
2015 else
2016 bot_start = wp->w_height + xtra_rows;
2017 }
2018 }
2019 else if (xtra_rows > 0)
2020 {
2021 /* May scroll text down. If there is not enough
2022 * remaining text of scrolling fails, must redraw the
2023 * rest. */
2024 if (row + xtra_rows >= wp->w_height - 2)
2025 mod_bot = MAXLNUM;
2026 else
2027 {
2028 check_for_delay(FALSE);
2029 if (win_ins_lines(wp, row + old_rows,
2030 xtra_rows, FALSE, FALSE) == FAIL)
2031 mod_bot = MAXLNUM;
2032 else if (top_end > row + old_rows)
2033 /* Scrolled the part at the top that requires
2034 * updating down. */
2035 top_end += xtra_rows;
2036 }
2037 }
2038
2039 /* When not updating the rest, may need to move w_lines[]
2040 * entries. */
2041 if (mod_bot != MAXLNUM && i != j)
2042 {
2043 if (j < i)
2044 {
2045 int x = row + new_rows;
2046
2047 /* move entries in w_lines[] upwards */
2048 for (;;)
2049 {
2050 /* stop at last valid entry in w_lines[] */
2051 if (i >= wp->w_lines_valid)
2052 {
2053 wp->w_lines_valid = j;
2054 break;
2055 }
2056 wp->w_lines[j] = wp->w_lines[i];
2057 /* stop at a line that won't fit */
2058 if (x + (int)wp->w_lines[j].wl_size
2059 > wp->w_height)
2060 {
2061 wp->w_lines_valid = j + 1;
2062 break;
2063 }
2064 x += wp->w_lines[j++].wl_size;
2065 ++i;
2066 }
2067 if (bot_start > x)
2068 bot_start = x;
2069 }
2070 else /* j > i */
2071 {
2072 /* move entries in w_lines[] downwards */
2073 j -= i;
2074 wp->w_lines_valid += j;
2075 if (wp->w_lines_valid > wp->w_height)
2076 wp->w_lines_valid = wp->w_height;
2077 for (i = wp->w_lines_valid; i - j >= idx; --i)
2078 wp->w_lines[i] = wp->w_lines[i - j];
2079
2080 /* The w_lines[] entries for inserted lines are
2081 * now invalid, but wl_size may be used above.
2082 * Reset to zero. */
2083 while (i >= idx)
2084 {
2085 wp->w_lines[i].wl_size = 0;
2086 wp->w_lines[i--].wl_valid = FALSE;
2087 }
2088 }
2089 }
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092
2093#ifdef FEAT_FOLDING
2094 /*
2095 * When lines are folded, display one line for all of them.
2096 * Otherwise, display normally (can be several display lines when
2097 * 'wrap' is on).
2098 */
2099 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2100 if (fold_count != 0)
2101 {
2102 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2103 ++row;
2104 --fold_count;
2105 wp->w_lines[idx].wl_folded = TRUE;
2106 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2107# ifdef FEAT_SYN_HL
2108 did_update = DID_FOLD;
2109# endif
2110 }
2111 else
2112#endif
2113 if (idx < wp->w_lines_valid
2114 && wp->w_lines[idx].wl_valid
2115 && wp->w_lines[idx].wl_lnum == lnum
2116 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002117 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 && srow + wp->w_lines[idx].wl_size > wp->w_height
2119#ifdef FEAT_DIFF
2120 && diff_check_fill(wp, lnum) == 0
2121#endif
2122 )
2123 {
2124 /* This line is not going to fit. Don't draw anything here,
2125 * will draw "@ " lines below. */
2126 row = wp->w_height + 1;
2127 }
2128 else
2129 {
2130#ifdef FEAT_SEARCH_EXTRA
2131 prepare_search_hl(wp, lnum);
2132#endif
2133#ifdef FEAT_SYN_HL
2134 /* Let the syntax stuff know we skipped a few lines. */
2135 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002136 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 syntax_end_parsing(syntax_last_parsed + 1);
2138#endif
2139
2140 /*
2141 * Display one line.
2142 */
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002143 row = win_line(wp, lnum, srow, wp->w_height, mod_top == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145#ifdef FEAT_FOLDING
2146 wp->w_lines[idx].wl_folded = FALSE;
2147 wp->w_lines[idx].wl_lastlnum = lnum;
2148#endif
2149#ifdef FEAT_SYN_HL
2150 did_update = DID_LINE;
2151 syntax_last_parsed = lnum;
2152#endif
2153 }
2154
2155 wp->w_lines[idx].wl_lnum = lnum;
2156 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002157
2158 /* Past end of the window or end of the screen. Note that after
2159 * resizing wp->w_height may be end up too big. That's a problem
2160 * elsewhere, but prevent a crash here. */
2161 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002164 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2166 ++idx;
2167 break;
2168 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002169 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 wp->w_lines[idx].wl_size = row - srow;
2171 ++idx;
2172#ifdef FEAT_FOLDING
2173 lnum += fold_count + 1;
2174#else
2175 ++lnum;
2176#endif
2177 }
2178 else
2179 {
2180 /* This line does not need updating, advance to the next one */
2181 row += wp->w_lines[idx++].wl_size;
2182 if (row > wp->w_height) /* past end of screen */
2183 break;
2184#ifdef FEAT_FOLDING
2185 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2186#else
2187 ++lnum;
2188#endif
2189#ifdef FEAT_SYN_HL
2190 did_update = DID_NONE;
2191#endif
2192 }
2193
2194 if (lnum > buf->b_ml.ml_line_count)
2195 {
2196 eof = TRUE;
2197 break;
2198 }
2199 }
2200 /*
2201 * End of loop over all window lines.
2202 */
2203
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002204#ifdef FEAT_VTP
2205 /* Rewrite the character at the end of the screen line. */
2206 if (use_vtp())
2207 {
2208 int i;
2209
2210 for (i = 0; i < Rows; ++i)
2211# ifdef FEAT_MBYTE
2212 if (enc_utf8)
2213 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2214 LineOffset[i] + screen_Columns) > 1)
2215 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2216 else
2217 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2218 else
2219# endif
2220 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2221 }
2222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 if (idx > wp->w_lines_valid)
2225 wp->w_lines_valid = idx;
2226
2227#ifdef FEAT_SYN_HL
2228 /*
2229 * Let the syntax stuff know we stop parsing here.
2230 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002231 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 syntax_end_parsing(syntax_last_parsed + 1);
2233#endif
2234
2235 /*
2236 * If we didn't hit the end of the file, and we didn't finish the last
2237 * line we were working on, then the line didn't fit.
2238 */
2239 wp->w_empty_rows = 0;
2240#ifdef FEAT_DIFF
2241 wp->w_filler_rows = 0;
2242#endif
2243 if (!eof && !didline)
2244 {
2245 if (lnum == wp->w_topline)
2246 {
2247 /*
2248 * Single line that does not fit!
2249 * Don't overwrite it, it can be edited.
2250 */
2251 wp->w_botline = lnum + 1;
2252 }
2253#ifdef FEAT_DIFF
2254 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2255 {
2256 /* Window ends in filler lines. */
2257 wp->w_botline = lnum;
2258 wp->w_filler_rows = wp->w_height - srow;
2259 }
2260#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002261 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2262 {
2263 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2264
2265 /*
2266 * Last line isn't finished: Display "@@@" in the last screen line.
2267 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002268 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002269 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002270 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002271 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002272 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002273 set_empty_rows(wp, srow);
2274 wp->w_botline = lnum;
2275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2277 {
2278 /*
2279 * Last line isn't finished: Display "@@@" at the end.
2280 */
2281 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2282 W_WINROW(wp) + wp->w_height,
2283 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002284 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 set_empty_rows(wp, srow);
2286 wp->w_botline = lnum;
2287 }
2288 else
2289 {
2290 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2291 wp->w_botline = lnum;
2292 }
2293 }
2294 else
2295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (eof) /* we hit the end of the file */
2298 {
2299 wp->w_botline = buf->b_ml.ml_line_count + 1;
2300#ifdef FEAT_DIFF
2301 j = diff_check_fill(wp, wp->w_botline);
2302 if (j > 0 && !wp->w_botfill)
2303 {
2304 /*
2305 * Display filler lines at the end of the file
2306 */
2307 if (char2cells(fill_diff) > 1)
2308 i = '-';
2309 else
2310 i = fill_diff;
2311 if (row + j > wp->w_height)
2312 j = wp->w_height - row;
2313 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2314 row += j;
2315 }
2316#endif
2317 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002318 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 wp->w_botline = lnum;
2320
2321 /* make sure the rest of the screen is blank */
2322 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002323 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002326#ifdef SYN_TIME_LIMIT
2327 syn_set_timeout(NULL);
2328#endif
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 /* Reset the type of redrawing required, the window has been updated. */
2331 wp->w_redr_type = 0;
2332#ifdef FEAT_DIFF
2333 wp->w_old_topfill = wp->w_topfill;
2334 wp->w_old_botfill = wp->w_botfill;
2335#endif
2336
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002337 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 /*
2340 * There is a trick with w_botline. If we invalidate it on each
2341 * change that might modify it, this will cause a lot of expensive
2342 * calls to plines() in update_topline() each time. Therefore the
2343 * value of w_botline is often approximated, and this value is used to
2344 * compute the value of w_topline. If the value of w_botline was
2345 * wrong, check that the value of w_topline is correct (cursor is on
2346 * the visible part of the text). If it's not, we need to redraw
2347 * again. Mostly this just means scrolling up a few lines, so it
2348 * doesn't look too bad. Only do this for the current window (where
2349 * changes are relevant).
2350 */
2351 wp->w_valid |= VALID_BOTLINE;
2352 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2353 {
2354 recursive = TRUE;
2355 curwin->w_valid &= ~VALID_TOPLINE;
2356 update_topline(); /* may invalidate w_botline again */
2357 if (must_redraw != 0)
2358 {
2359 /* Don't update for changes in buffer again. */
2360 i = curbuf->b_mod_set;
2361 curbuf->b_mod_set = FALSE;
2362 win_update(curwin);
2363 must_redraw = 0;
2364 curbuf->b_mod_set = i;
2365 }
2366 recursive = FALSE;
2367 }
2368 }
2369
2370#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2371 /* restore got_int, unless CTRL-C was hit while redrawing */
2372 if (!got_int)
2373 got_int = save_got_int;
2374#endif
2375}
2376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377/*
2378 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2379 * as the filler character.
2380 */
2381 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002382win_draw_end(
2383 win_T *wp,
2384 int c1,
2385 int c2,
2386 int row,
2387 int endrow,
2388 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
2390#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2391 int n = 0;
2392# define FDC_OFF n
2393#else
2394# define FDC_OFF 0
2395#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002396#ifdef FEAT_FOLDING
2397 int fdc = compute_foldcolumn(wp, 0);
2398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400#ifdef FEAT_RIGHTLEFT
2401 if (wp->w_p_rl)
2402 {
2403 /* No check for cmdline window: should never be right-left. */
2404# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002405 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 if (n > 0)
2408 {
2409 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002410 if (n > wp->w_width)
2411 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2413 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002414 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416# endif
2417# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002418 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 int nn = n + 2;
2421
2422 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002423 if (nn > wp->w_width)
2424 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2426 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002427 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 n = nn;
2429 }
2430# endif
2431 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002432 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002433 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2435 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002436 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438 else
2439#endif
2440 {
2441#ifdef FEAT_CMDWIN
2442 if (cmdwin_type != 0 && wp == curwin)
2443 {
2444 /* draw the cmdline character in the leftmost column */
2445 n = 1;
2446 if (n > wp->w_width)
2447 n = wp->w_width;
2448 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002449 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002450 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
2452#endif
2453#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002454 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002456 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002459 if (nn > wp->w_width)
2460 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002462 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002463 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 n = nn;
2465 }
2466#endif
2467#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002468 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 int nn = n + 2;
2471
2472 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002473 if (nn > wp->w_width)
2474 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002476 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002477 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 n = nn;
2479 }
2480#endif
2481 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002482 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002483 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
2485 set_empty_rows(wp, row);
2486}
2487
Bram Moolenaar1a384422010-07-14 19:53:30 +02002488#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002489static int advance_color_col(int vcol, int **color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02002490
2491/*
2492 * Advance **color_cols and return TRUE when there are columns to draw.
2493 */
2494 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002495advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496{
2497 while (**color_cols >= 0 && vcol > **color_cols)
2498 ++*color_cols;
2499 return (**color_cols >= 0);
2500}
2501#endif
2502
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002503#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2504/*
2505 * Copy "text" to ScreenLines using "attr".
2506 * Returns the next screen column.
2507 */
2508 static int
2509text_to_screenline(win_T *wp, char_u *text, int col)
2510{
2511 int off = (int)(current_ScreenLine - ScreenLines);
2512
2513#ifdef FEAT_MBYTE
2514 if (has_mbyte)
2515 {
2516 int cells;
2517 int u8c, u8cc[MAX_MCO];
2518 int i;
2519 int idx;
2520 int c_len;
2521 char_u *p;
2522# ifdef FEAT_ARABIC
2523 int prev_c = 0; /* previous Arabic character */
2524 int prev_c1 = 0; /* first composing char for prev_c */
2525# endif
2526
2527# ifdef FEAT_RIGHTLEFT
2528 if (wp->w_p_rl)
2529 idx = off;
2530 else
2531# endif
2532 idx = off + col;
2533
2534 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2535 for (p = text; *p != NUL; )
2536 {
2537 cells = (*mb_ptr2cells)(p);
2538 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002539 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002540# ifdef FEAT_RIGHTLEFT
2541 - (wp->w_p_rl ? col : 0)
2542# endif
2543 )
2544 break;
2545 ScreenLines[idx] = *p;
2546 if (enc_utf8)
2547 {
2548 u8c = utfc_ptr2char(p, u8cc);
2549 if (*p < 0x80 && u8cc[0] == 0)
2550 {
2551 ScreenLinesUC[idx] = 0;
2552#ifdef FEAT_ARABIC
2553 prev_c = u8c;
2554#endif
2555 }
2556 else
2557 {
2558#ifdef FEAT_ARABIC
2559 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2560 {
2561 /* Do Arabic shaping. */
2562 int pc, pc1, nc;
2563 int pcc[MAX_MCO];
2564 int firstbyte = *p;
2565
2566 /* The idea of what is the previous and next
2567 * character depends on 'rightleft'. */
2568 if (wp->w_p_rl)
2569 {
2570 pc = prev_c;
2571 pc1 = prev_c1;
2572 nc = utf_ptr2char(p + c_len);
2573 prev_c1 = u8cc[0];
2574 }
2575 else
2576 {
2577 pc = utfc_ptr2char(p + c_len, pcc);
2578 nc = prev_c;
2579 pc1 = pcc[0];
2580 }
2581 prev_c = u8c;
2582
2583 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2584 pc, pc1, nc);
2585 ScreenLines[idx] = firstbyte;
2586 }
2587 else
2588 prev_c = u8c;
2589#endif
2590 /* Non-BMP character: display as ? or fullwidth ?. */
2591#ifdef UNICODE16
2592 if (u8c >= 0x10000)
2593 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2594 else
2595#endif
2596 ScreenLinesUC[idx] = u8c;
2597 for (i = 0; i < Screen_mco; ++i)
2598 {
2599 ScreenLinesC[i][idx] = u8cc[i];
2600 if (u8cc[i] == 0)
2601 break;
2602 }
2603 }
2604 if (cells > 1)
2605 ScreenLines[idx + 1] = 0;
2606 }
2607 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2608 /* double-byte single width character */
2609 ScreenLines2[idx] = p[1];
2610 else if (cells > 1)
2611 /* double-width character */
2612 ScreenLines[idx + 1] = p[1];
2613 col += cells;
2614 idx += cells;
2615 p += c_len;
2616 }
2617 }
2618 else
2619#endif
2620 {
2621 int len = (int)STRLEN(text);
2622
Bram Moolenaar02631462017-09-22 15:20:32 +02002623 if (len > wp->w_width - col)
2624 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002625 if (len > 0)
2626 {
2627#ifdef FEAT_RIGHTLEFT
2628 if (wp->w_p_rl)
2629 STRNCPY(current_ScreenLine, text, len);
2630 else
2631#endif
2632 STRNCPY(current_ScreenLine + col, text, len);
2633 col += len;
2634 }
2635 }
2636 return col;
2637}
2638#endif
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640#ifdef FEAT_FOLDING
2641/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002642 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2643 * space is available for window "wp", minus "col".
2644 */
2645 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002646compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002647{
2648 int fdc = wp->w_p_fdc;
2649 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002650 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002651
2652 if (fdc > wwidth - (col + wmw))
2653 fdc = wwidth - (col + wmw);
2654 return fdc;
2655}
2656
2657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 * Display one folded line.
2659 */
2660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002661fold_line(
2662 win_T *wp,
2663 long fold_count,
2664 foldinfo_T *foldinfo,
2665 linenr_T lnum,
2666 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002668 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 pos_T *top, *bot;
2670 linenr_T lnume = lnum + fold_count - 1;
2671 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002672 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 int col;
2675 int txtcol;
2676 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002677 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 /* Build the fold line:
2680 * 1. Add the cmdwin_type for the command-line window
2681 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002682 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 * 4. Compose the text
2684 * 5. Add the text
2685 * 6. set highlighting for the Visual area an other text
2686 */
2687 col = 0;
2688
2689 /*
2690 * 1. Add the cmdwin_type for the command-line window
2691 * Ignores 'rightleft', this window is never right-left.
2692 */
2693#ifdef FEAT_CMDWIN
2694 if (cmdwin_type != 0 && wp == curwin)
2695 {
2696 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002697 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#ifdef FEAT_MBYTE
2699 if (enc_utf8)
2700 ScreenLinesUC[off] = 0;
2701#endif
2702 ++col;
2703 }
2704#endif
2705
2706 /*
2707 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002708 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002710 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (fdc > 0)
2712 {
2713 fill_foldcolumn(buf, wp, TRUE, lnum);
2714#ifdef FEAT_RIGHTLEFT
2715 if (wp->w_p_rl)
2716 {
2717 int i;
2718
Bram Moolenaar02631462017-09-22 15:20:32 +02002719 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002720 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 /* reverse the fold column */
2722 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002723 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 else
2726#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002727 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 col += fdc;
2729 }
2730
2731#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002732# define RL_MEMSET(p, v, l) \
2733 do { \
2734 if (wp->w_p_rl) \
2735 for (ri = 0; ri < l; ++ri) \
2736 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2737 else \
2738 for (ri = 0; ri < l; ++ri) \
2739 ScreenAttrs[off + (p) + ri] = v; \
2740 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002742# define RL_MEMSET(p, v, l) \
2743 do { \
2744 for (ri = 0; ri < l; ++ri) \
2745 ScreenAttrs[off + (p) + ri] = v; \
2746 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747#endif
2748
Bram Moolenaar64486672010-05-16 15:46:46 +02002749 /* Set all attributes of the 'number' or 'relativenumber' column and the
2750 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002751 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753#ifdef FEAT_SIGNS
2754 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002755 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002757 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if (len > 0)
2759 {
2760 if (len > 2)
2761 len = 2;
2762# ifdef FEAT_RIGHTLEFT
2763 if (wp->w_p_rl)
2764 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002765 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002766 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 else
2768# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002769 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 col += len;
2771 }
2772 }
2773#endif
2774
2775 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002776 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002778 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002780 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 if (len > 0)
2782 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002783 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002784 long num;
2785 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002786
2787 if (len > w + 1)
2788 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002789
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002790 if (wp->w_p_nu && !wp->w_p_rnu)
2791 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002792 num = (long)lnum;
2793 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002794 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002795 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002796 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002797 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002798 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002799 /* 'number' + 'relativenumber': cursor line shows absolute
2800 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002801 num = lnum;
2802 fmt = "%-*ld ";
2803 }
2804 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002805
Bram Moolenaar700e7342013-01-30 12:31:36 +01002806 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#ifdef FEAT_RIGHTLEFT
2808 if (wp->w_p_rl)
2809 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002810 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002811 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
2813#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002814 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 col += len;
2816 }
2817 }
2818
2819 /*
2820 * 4. Compose the folded-line string with 'foldtext', if set.
2821 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002822 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 txtcol = col; /* remember where text starts */
2825
2826 /*
2827 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2828 * Right-left text is put in columns 0 - number-col, normal text is put
2829 * in columns number-col - window-width.
2830 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002831 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 /* Fill the rest of the line with the fold filler */
2834#ifdef FEAT_RIGHTLEFT
2835 if (wp->w_p_rl)
2836 col -= txtcol;
2837#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002838 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839#ifdef FEAT_RIGHTLEFT
2840 - (wp->w_p_rl ? txtcol : 0)
2841#endif
2842 )
2843 {
2844#ifdef FEAT_MBYTE
2845 if (enc_utf8)
2846 {
2847 if (fill_fold >= 0x80)
2848 {
2849 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002850 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002851 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002856 ScreenLines[off + col] = fill_fold;
2857 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002858 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002862 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
2864
2865 if (text != buf)
2866 vim_free(text);
2867
2868 /*
2869 * 6. set highlighting for the Visual area an other text.
2870 * If all folded lines are in the Visual area, highlight the line.
2871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2873 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002874 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 /* Visual is after curwin->w_cursor */
2877 top = &curwin->w_cursor;
2878 bot = &VIsual;
2879 }
2880 else
2881 {
2882 /* Visual is before curwin->w_cursor */
2883 top = &VIsual;
2884 bot = &curwin->w_cursor;
2885 }
2886 if (lnum >= top->lnum
2887 && lnume <= bot->lnum
2888 && (VIsual_mode != 'v'
2889 || ((lnum > top->lnum
2890 || (lnum == top->lnum
2891 && top->col == 0))
2892 && (lnume < bot->lnum
2893 || (lnume == bot->lnum
2894 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002895 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 {
2897 if (VIsual_mode == Ctrl_V)
2898 {
2899 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002902 if (wp->w_old_cursor_lcol != MAXCOL
2903 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 len = wp->w_old_cursor_lcol;
2906 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002907 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002908 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002909 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
2911 }
2912 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002920#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002921 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2922 if (wp->w_p_cc_cols)
2923 {
2924 int i = 0;
2925 int j = wp->w_p_cc_cols[i];
2926 int old_txtcol = txtcol;
2927
2928 while (j > -1)
2929 {
2930 txtcol += j;
2931 if (wp->w_p_wrap)
2932 txtcol -= wp->w_skipcol;
2933 else
2934 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002935 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002936 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002937 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002938 txtcol = old_txtcol;
2939 j = wp->w_p_cc_cols[++i];
2940 }
2941 }
2942
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002943 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002944 if (wp->w_p_cuc)
2945 {
2946 txtcol += wp->w_virtcol;
2947 if (wp->w_p_wrap)
2948 txtcol -= wp->w_skipcol;
2949 else
2950 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002951 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002952 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002953 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002954 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaar02631462017-09-22 15:20:32 +02002957 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2958 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /*
2961 * Update w_cline_height and w_cline_folded if the cursor line was
2962 * updated (saves a call to plines() later).
2963 */
2964 if (wp == curwin
2965 && lnum <= curwin->w_cursor.lnum
2966 && lnume >= curwin->w_cursor.lnum)
2967 {
2968 curwin->w_cline_row = row;
2969 curwin->w_cline_height = 1;
2970 curwin->w_cline_folded = TRUE;
2971 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2972 }
2973}
2974
2975/*
2976 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2977 */
2978 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002979copy_text_attr(
2980 int off,
2981 char_u *buf,
2982 int len,
2983 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002985 int i;
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 mch_memmove(ScreenLines + off, buf, (size_t)len);
2988# ifdef FEAT_MBYTE
2989 if (enc_utf8)
2990 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2991# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002992 for (i = 0; i < len; ++i)
2993 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996/*
2997 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002998 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
3000 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003001fill_foldcolumn(
3002 char_u *p,
3003 win_T *wp,
3004 int closed, /* TRUE of FALSE */
3005 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 int i = 0;
3008 int level;
3009 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003010 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003011 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003014 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
3016 level = win_foldinfo.fi_level;
3017 if (level > 0)
3018 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003019 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003020 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 /* If the column is too narrow, we start at the lowest level that
3023 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003024 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (first_level < 1)
3026 first_level = 1;
3027
Bram Moolenaar1c934292015-01-27 16:39:29 +01003028 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 {
3030 if (win_foldinfo.fi_lnum == lnum
3031 && first_level + i >= win_foldinfo.fi_low_level)
3032 p[i] = '-';
3033 else if (first_level == 1)
3034 p[i] = '|';
3035 else if (first_level + i <= 9)
3036 p[i] = '0' + first_level + i;
3037 else
3038 p[i] = '>';
3039 if (first_level + i == level)
3040 break;
3041 }
3042 }
3043 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003044 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045}
3046#endif /* FEAT_FOLDING */
3047
3048/*
3049 * Display line "lnum" of window 'wp' on the screen.
3050 * Start at row "startrow", stop when "endrow" is reached.
3051 * wp->w_virtcol needs to be valid.
3052 *
3053 * Return the number of last row the line occupies.
3054 */
3055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003056win_line(
3057 win_T *wp,
3058 linenr_T lnum,
3059 int startrow,
3060 int endrow,
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003061 int nochange UNUSED) /* not updating for changed text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003063 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3065 int c = 0; /* init for GCC */
3066 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003067#ifdef FEAT_LINEBREAK
3068 long vcol_sbr = -1; /* virtual column after showbreak */
3069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 long vcol_prev = -1; /* "vcol" of previous character */
3071 char_u *line; /* current line */
3072 char_u *ptr; /* current position in "line" */
3073 int row; /* row in the window, excl w_winrow */
3074 int screen_row; /* row on the screen, incl w_winrow */
3075
3076 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3077 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003078 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003079 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 int c_extra = NUL; /* extra chars, all the same */
3081 int extra_attr = 0; /* attributes when n_extra != 0 */
3082 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3083 displaying lcs_eol at end-of-line */
3084 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3085 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3086
3087 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3088 int saved_n_extra = 0;
3089 char_u *saved_p_extra = NULL;
3090 int saved_c_extra = 0;
3091 int saved_char_attr = 0;
3092
3093 int n_attr = 0; /* chars with special attr */
3094 int saved_attr2 = 0; /* char_attr saved for n_attr */
3095 int n_attr3 = 0; /* chars with overruling special attr */
3096 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3097
3098 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3099
3100 int fromcol, tocol; /* start/end of inverting */
3101 int fromcol_prev = -2; /* start of inverting after cursor */
3102 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003104 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 pos_T pos;
3106 long v;
3107
3108 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003109 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3111 in this line */
3112 int attr = 0; /* attributes for area highlighting */
3113 int area_attr = 0; /* attributes desired by highlighting */
3114 int search_attr = 0; /* attributes desired by 'hlsearch' */
3115#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003116 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int syntax_attr = 0; /* attributes desired by syntax */
3118 int has_syntax = FALSE; /* this buffer has syntax highl. */
3119 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003120 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003121 int draw_color_col = FALSE; /* highlight colorcolumn */
3122 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003123#endif
3124#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003125 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003126# define SPWORDLEN 150
3127 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003128 int nextlinecol = 0; /* column where nextline[] starts */
3129 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003130 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003131 int spell_attr = 0; /* attributes desired by spelling */
3132 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003133 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3134 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003135 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003136 static int cap_col = -1; /* column to check for Cap word */
3137 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003138 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#endif
3140 int extra_check; /* has syntax or linebreak */
3141#ifdef FEAT_MBYTE
3142 int multi_attr = 0; /* attributes desired by multibyte */
3143 int mb_l = 1; /* multi-byte byte length */
3144 int mb_c = 0; /* decoded multi-byte character */
3145 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003146 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#endif
3148#ifdef FEAT_DIFF
3149 int filler_lines; /* nr of filler lines to be drawn */
3150 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003151 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 int change_start = MAXCOL; /* first col of changed area */
3153 int change_end = -1; /* last col of changed area */
3154#endif
3155 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3156#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003157 int need_showbreak = FALSE; /* overlong line, skipping first x
3158 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003160#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003161 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003163 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#endif
3165#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003166 matchitem_T *cur; /* points to the match list */
3167 match_T *shl; /* points to search_hl or a match */
3168 int shl_flag; /* flag to indicate whether search_hl
3169 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003170 int pos_inprogress; /* marks that position match search is
3171 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003172 int prevcol_hl_flag; /* flag to indicate whether prevcol
3173 equals startcol of search_hl or one
3174 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#endif
3176#ifdef FEAT_ARABIC
3177 int prev_c = 0; /* previous Arabic character */
3178 int prev_c1 = 0; /* first composing char for prev_c */
3179#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003180#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003181 int did_line_attr = 0;
3182#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003183#ifdef FEAT_TERMINAL
3184 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003185 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 /* draw_state: items that are drawn in sequence: */
3189#define WL_START 0 /* nothing done yet */
3190#ifdef FEAT_CMDWIN
3191# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3192#else
3193# define WL_CMDLINE WL_START
3194#endif
3195#ifdef FEAT_FOLDING
3196# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3197#else
3198# define WL_FOLD WL_CMDLINE
3199#endif
3200#ifdef FEAT_SIGNS
3201# define WL_SIGN WL_FOLD + 1 /* column for signs */
3202#else
3203# define WL_SIGN WL_FOLD /* column for signs */
3204#endif
3205#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003206#ifdef FEAT_LINEBREAK
3207# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003209# define WL_BRI WL_NR
3210#endif
3211#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3212# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3213#else
3214# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#endif
3216#define WL_LINE WL_SBR + 1 /* text in the line */
3217 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003218#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 int feedback_col = 0;
3220 int feedback_old_attr = -1;
3221#endif
3222
Bram Moolenaar860cae12010-06-05 23:22:07 +02003223#ifdef FEAT_CONCEAL
3224 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003225 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003226 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003227 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003228 int is_concealing = FALSE;
3229 int boguscols = 0; /* nonexistent columns added to force
3230 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003231 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003232 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003233 int match_conc = 0; /* cchar for match functions */
3234 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003235 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003236# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003237# define FIX_FOR_BOGUSCOLS \
3238 { \
3239 n_extra += vcol_off; \
3240 vcol -= vcol_off; \
3241 vcol_off = 0; \
3242 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003243 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003244 boguscols = 0; \
3245 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003246#else
3247# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
3250 if (startrow > endrow) /* past the end already! */
3251 return startrow;
3252
3253 row = startrow;
3254 screen_row = row + W_WINROW(wp);
3255
3256 /*
3257 * To speed up the loop below, set extra_check when there is linebreak,
3258 * trailing white space and/or syntax processing to be done.
3259 */
3260#ifdef FEAT_LINEBREAK
3261 extra_check = wp->w_p_lbr;
3262#else
3263 extra_check = 0;
3264#endif
3265#ifdef FEAT_SYN_HL
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003266 if (syntax_present(wp) && !wp->w_s->b_syn_error
3267# ifdef SYN_TIME_LIMIT
3268 && !wp->w_s->b_syn_slow
3269# endif
3270 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 /* Prepare for syntax highlighting in this line. When there is an
3273 * error, stop syntax highlighting. */
3274 save_did_emsg = did_emsg;
3275 did_emsg = FALSE;
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003276 syntax_start(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (did_emsg)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003278 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 else
3280 {
3281 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003282#ifdef SYN_TIME_LIMIT
3283 if (!wp->w_s->b_syn_slow)
3284#endif
3285 {
3286 has_syntax = TRUE;
3287 extra_check = TRUE;
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 }
3290 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003291
3292 /* Check for columns to display for 'colorcolumn'. */
3293 color_cols = wp->w_p_cc_cols;
3294 if (color_cols != NULL)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003295 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003296#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003297
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003298#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02003299 if (term_show_buffer(wp->w_buffer))
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003300 {
3301 extra_check = TRUE;
3302 get_term_attr = TRUE;
Bram Moolenaar49a613f2017-09-11 23:05:44 +02003303 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003304 }
3305#endif
3306
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003307#ifdef FEAT_SPELL
Bram Moolenaar0cb032e2005-04-23 20:52:00 +00003308 if (wp->w_p_spell
Bram Moolenaar860cae12010-06-05 23:22:07 +02003309 && *wp->w_s->b_p_spl != NUL
3310 && wp->w_s->b_langp.ga_len > 0
3311 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar217ad922005-03-20 22:37:15 +00003312 {
3313 /* Prepare for spell checking. */
3314 has_spell = TRUE;
3315 extra_check = TRUE;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003316
3317 /* Get the start of the next line, so that words that wrap to the next
3318 * line are found too: "et<line-break>al.".
3319 * Trick: skip a few chars for C/shell/Vim comments */
3320 nextline[SPWORDLEN] = NUL;
3321 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3322 {
3323 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3324 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3325 }
3326
3327 /* When a word wrapped from the previous line the start of the current
3328 * line is valid. */
3329 if (lnum == checked_lnum)
3330 cur_checked_col = checked_col;
3331 checked_lnum = 0;
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003332
3333 /* When there was a sentence end in the previous line may require a
3334 * word starting with capital in this line. In line 1 always check
3335 * the first word. */
3336 if (lnum != capcol_lnum)
3337 cap_col = -1;
3338 if (lnum == 1)
3339 cap_col = 0;
3340 capcol_lnum = 0;
Bram Moolenaar217ad922005-03-20 22:37:15 +00003341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#endif
3343
3344 /*
3345 * handle visual active in this window
3346 */
3347 fromcol = -10;
3348 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3350 {
3351 /* Visual is after curwin->w_cursor */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003352 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
3354 top = &curwin->w_cursor;
3355 bot = &VIsual;
3356 }
3357 else /* Visual is before curwin->w_cursor */
3358 {
3359 top = &VIsual;
3360 bot = &curwin->w_cursor;
3361 }
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003362 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (VIsual_mode == Ctrl_V) /* block mode */
3364 {
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003365 if (lnum_in_visual_area)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 fromcol = wp->w_old_cursor_fcol;
3368 tocol = wp->w_old_cursor_lcol;
3369 }
3370 }
3371 else /* non-block mode */
3372 {
3373 if (lnum > top->lnum && lnum <= bot->lnum)
3374 fromcol = 0;
3375 else if (lnum == top->lnum)
3376 {
3377 if (VIsual_mode == 'V') /* linewise */
3378 fromcol = 0;
3379 else
3380 {
3381 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3382 if (gchar_pos(top) == NUL)
3383 tocol = fromcol + 1;
3384 }
3385 }
3386 if (VIsual_mode != 'V' && lnum == bot->lnum)
3387 {
3388 if (*p_sel == 'e' && bot->col == 0
3389#ifdef FEAT_VIRTUALEDIT
3390 && bot->coladd == 0
3391#endif
3392 )
3393 {
3394 fromcol = -10;
3395 tocol = MAXCOL;
3396 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003397 else if (bot->col == MAXCOL)
3398 tocol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
3400 {
3401 pos = *bot;
3402 if (*p_sel == 'e')
3403 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3404 else
3405 {
3406 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3407 ++tocol;
3408 }
3409 }
3410 }
3411 }
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 /* Check if the character under the cursor should not be inverted */
3414 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003415#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 )
3419 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
3421 /* if inverting in this line set area_highlighting */
3422 if (fromcol >= 0)
3423 {
3424 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003425 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02003427 if ((clip_star.available && !clip_star.owned
3428 && clip_isautosel_star())
3429 || (clip_plus.available && !clip_plus.owned
3430 && clip_isautosel_plus()))
Bram Moolenaar8820b482017-03-16 17:23:31 +01003431 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432#endif
3433 }
3434 }
3435
3436 /*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003437 * handle 'incsearch' and ":s///c" highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003439 else if (highlight_match
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && wp == curwin
3441 && lnum >= curwin->w_cursor.lnum
3442 && lnum <= curwin->w_cursor.lnum + search_match_lines)
3443 {
3444 if (lnum == curwin->w_cursor.lnum)
3445 getvcol(curwin, &(curwin->w_cursor),
3446 (colnr_T *)&fromcol, NULL, NULL);
3447 else
3448 fromcol = 0;
3449 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3450 {
3451 pos.lnum = lnum;
3452 pos.col = search_match_endcol;
3453 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3454 }
3455 else
3456 tocol = MAXCOL;
Bram Moolenaarf3205d12009-03-18 18:09:03 +00003457 /* do at least one character; happens when past end of line */
3458 if (fromcol == tocol)
3459 tocol = fromcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 area_highlighting = TRUE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003461 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
3463
3464#ifdef FEAT_DIFF
3465 filler_lines = diff_check(wp, lnum);
3466 if (filler_lines < 0)
3467 {
3468 if (filler_lines == -1)
3469 {
3470 if (diff_find_change(wp, lnum, &change_start, &change_end))
3471 diff_hlf = HLF_ADD; /* added line */
3472 else if (change_start == 0)
3473 diff_hlf = HLF_TXD; /* changed text */
3474 else
3475 diff_hlf = HLF_CHD; /* changed line */
3476 }
3477 else
3478 diff_hlf = HLF_ADD; /* added line */
3479 filler_lines = 0;
3480 area_highlighting = TRUE;
3481 }
3482 if (lnum == wp->w_topline)
3483 filler_lines = wp->w_topfill;
3484 filler_todo = filler_lines;
3485#endif
3486
3487#ifdef LINE_ATTR
3488# ifdef FEAT_SIGNS
3489 /* If this line has a sign with line highlighting set line_attr. */
3490 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3491 if (v != 0)
3492 line_attr = sign_get_attr((int)v, TRUE);
3493# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003494# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003496 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003497 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498# endif
3499 if (line_attr != 0)
3500 area_highlighting = TRUE;
3501#endif
3502
3503 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3504 ptr = line;
3505
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003506#ifdef FEAT_SPELL
Bram Moolenaar30abd282005-06-22 22:35:10 +00003507 if (has_spell)
3508 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003509 /* For checking first word with a capital skip white space. */
3510 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003511 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003512
Bram Moolenaar30abd282005-06-22 22:35:10 +00003513 /* To be able to spell-check over line boundaries copy the end of the
3514 * current line into nextline[]. Above the start of the next line was
3515 * copied to nextline[SPWORDLEN]. */
3516 if (nextline[SPWORDLEN] == NUL)
3517 {
3518 /* No next line or it is empty. */
3519 nextlinecol = MAXCOL;
3520 nextline_idx = 0;
3521 }
3522 else
3523 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003525 if (v < SPWORDLEN)
3526 {
3527 /* Short line, use it completely and append the start of the
3528 * next line. */
3529 nextlinecol = 0;
3530 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003531 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003532 nextline_idx = v + 1;
3533 }
3534 else
3535 {
3536 /* Long line, use only the last SPWORDLEN bytes. */
3537 nextlinecol = v - SPWORDLEN;
3538 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3539 nextline_idx = SPWORDLEN + 1;
3540 }
3541 }
3542 }
3543#endif
3544
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003545 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003547 if (lcs_space || lcs_trail)
3548 extra_check = TRUE;
3549 /* find start of trailing whitespace */
3550 if (lcs_trail)
3551 {
3552 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003553 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003554 --trailcol;
3555 trailcol += (colnr_T) (ptr - line);
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558
3559 /*
3560 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3561 * first character to be displayed.
3562 */
3563 if (wp->w_p_wrap)
3564 v = wp->w_skipcol;
3565 else
3566 v = wp->w_leftcol;
3567 if (v > 0)
3568 {
3569#ifdef FEAT_MBYTE
3570 char_u *prev_ptr = ptr;
3571#endif
3572 while (vcol < v && *ptr != NUL)
3573 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003574 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 vcol += c;
3576#ifdef FEAT_MBYTE
3577 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003579 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003582 /* When:
3583 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003584 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003585 * - 'virtualedit' is set, or
3586 * - the visual mode is active,
3587 * the end of the line may be before the start of the displayed part.
3588 */
3589 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003590#ifdef FEAT_SYN_HL
3591 wp->w_p_cuc || draw_color_col ||
3592#endif
3593#ifdef FEAT_VIRTUALEDIT
3594 virtual_active() ||
3595#endif
3596 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
3601 /* Handle a character that's not completely on the screen: Put ptr at
3602 * that character but skip the first few screen characters. */
3603 if (vcol > v)
3604 {
3605 vcol -= c;
3606#ifdef FEAT_MBYTE
3607 ptr = prev_ptr;
3608#else
3609 --ptr;
3610#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003611 /* If the character fits on the screen, don't need to skip it.
3612 * Except for a TAB. */
3613 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003614#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003615 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003616#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003617 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003618 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
3620
3621 /*
3622 * Adjust for when the inverted text is before the screen,
3623 * and when the start of the inverted text is before the screen.
3624 */
3625 if (tocol <= vcol)
3626 fromcol = 0;
3627 else if (fromcol >= 0 && fromcol < vcol)
3628 fromcol = vcol;
3629
3630#ifdef FEAT_LINEBREAK
3631 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3632 if (wp->w_p_wrap)
3633 need_showbreak = TRUE;
3634#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003635#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003636 /* When spell checking a word we need to figure out the start of the
3637 * word and if it's badly spelled or not. */
3638 if (has_spell)
3639 {
3640 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003641 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003642 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003643
3644 pos = wp->w_cursor;
3645 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003646 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003647 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003648
3649 /* spell_move_to() may call ml_get() and make "line" invalid */
3650 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3651 ptr = line + linecol;
3652
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003653 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003654 {
3655 /* no bad word found at line start, don't check until end of a
3656 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003657 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003658 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003659 }
3660 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003661 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003662 /* bad word found, use attributes until end of word */
3663 word_end = wp->w_cursor.col + len + 1;
3664
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003665 /* Turn index into actual attributes. */
3666 if (spell_hlf != HLF_COUNT)
3667 spell_attr = highlight_attr[spell_hlf];
3668 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003669 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003670
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003671# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003672 /* Need to restart syntax highlighting for this line. */
3673 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003674 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003675# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003676 }
3677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679
3680 /*
3681 * Correct highlighting for cursor that can't be disabled.
3682 * Avoids having to check this for each character.
3683 */
3684 if (fromcol >= 0)
3685 {
3686 if (noinvcur)
3687 {
3688 if ((colnr_T)fromcol == wp->w_virtcol)
3689 {
3690 /* highlighting starts at cursor, let it start just after the
3691 * cursor */
3692 fromcol_prev = fromcol;
3693 fromcol = -1;
3694 }
3695 else if ((colnr_T)fromcol < wp->w_virtcol)
3696 /* restart highlighting after the cursor */
3697 fromcol_prev = wp->w_virtcol;
3698 }
3699 if (fromcol >= tocol)
3700 fromcol = -1;
3701 }
3702
3703#ifdef FEAT_SEARCH_EXTRA
3704 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003705 * Handle highlighting the last used search pattern and matches.
3706 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003708 cur = wp->w_match_head;
3709 shl_flag = FALSE;
3710 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003712 if (shl_flag == FALSE)
3713 {
3714 shl = &search_hl;
3715 shl_flag = TRUE;
3716 }
3717 else
3718 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003719 shl->startcol = MAXCOL;
3720 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003722 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003723 v = (long)(ptr - line);
3724 if (cur != NULL)
3725 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003726 next_search_hl(wp, shl, lnum, (colnr_T)v,
3727 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003728
3729 /* Need to get the line again, a multi-line regexp may have made it
3730 * invalid. */
3731 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3732 ptr = line + v;
3733
3734 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003736 if (shl->lnum == lnum)
3737 shl->startcol = shl->rm.startpos[0].col;
3738 else
3739 shl->startcol = 0;
3740 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3741 - shl->rm.startpos[0].lnum)
3742 shl->endcol = shl->rm.endpos[0].col;
3743 else
3744 shl->endcol = MAXCOL;
3745 /* Highlight one character for an empty match. */
3746 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003749 if (has_mbyte && line[shl->endcol] != NUL)
3750 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3751 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003753 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003755 if ((long)shl->startcol < v) /* match at leftcol */
3756 {
3757 shl->attr_cur = shl->attr;
3758 search_attr = shl->attr;
3759 }
3760 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003762 if (shl != &search_hl && cur != NULL)
3763 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
3765#endif
3766
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003767#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003768 /* Cursor line highlighting for 'cursorline' in the current window. Not
3769 * when Visual mode is active, because it's not clear what is selected
3770 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003771 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003772 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003773 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003774 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003775 area_highlighting = TRUE;
3776 }
3777#endif
3778
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003779 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 col = 0;
3781#ifdef FEAT_RIGHTLEFT
3782 if (wp->w_p_rl)
3783 {
3784 /* Rightleft window: process the text in the normal direction, but put
3785 * it in current_ScreenLine[] from right to left. Start at the
3786 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003787 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 off += col;
3789 }
3790#endif
3791
3792 /*
3793 * Repeat for the whole displayed line.
3794 */
3795 for (;;)
3796 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003797#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003798 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 /* Skip this quickly when working on the text. */
3801 if (draw_state != WL_LINE)
3802 {
3803#ifdef FEAT_CMDWIN
3804 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3805 {
3806 draw_state = WL_CMDLINE;
3807 if (cmdwin_type != 0 && wp == curwin)
3808 {
3809 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003811 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003812 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 }
3815#endif
3816
3817#ifdef FEAT_FOLDING
3818 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3819 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003820 int fdc = compute_foldcolumn(wp, 0);
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003823 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003825 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003826 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003827 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003828 p_extra_free = alloc(12 + 1);
3829
3830 if (p_extra_free != NULL)
3831 {
3832 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3833 n_extra = fdc;
3834 p_extra_free[n_extra] = NUL;
3835 p_extra = p_extra_free;
3836 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003837 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 }
3841#endif
3842
3843#ifdef FEAT_SIGNS
3844 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3845 {
3846 draw_state = WL_SIGN;
3847 /* Show the sign column when there are any signs in this
3848 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003849 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003851 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003853 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854# endif
3855
3856 /* Draw two cells with the sign value or blank. */
3857 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003858 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 n_extra = 2;
3860
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003861 if (row == startrow
3862#ifdef FEAT_DIFF
3863 + filler_lines && filler_todo <= 0
3864#endif
3865 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 {
3867 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3868 SIGN_TEXT);
3869# ifdef FEAT_SIGN_ICONS
3870 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3871 SIGN_ICON);
3872 if (gui.in_use && icon_sign != 0)
3873 {
3874 /* Use the image in this position. */
3875 c_extra = SIGN_BYTE;
3876# ifdef FEAT_NETBEANS_INTG
3877 if (buf_signcount(wp->w_buffer, lnum) > 1)
3878 c_extra = MULTISIGN_BYTE;
3879# endif
3880 char_attr = icon_sign;
3881 }
3882 else
3883# endif
3884 if (text_sign != 0)
3885 {
3886 p_extra = sign_get_text(text_sign);
3887 if (p_extra != NULL)
3888 {
3889 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003890 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 }
3892 char_attr = sign_get_attr(text_sign, FALSE);
3893 }
3894 }
3895 }
3896 }
3897#endif
3898
3899 if (draw_state == WL_NR - 1 && n_extra == 0)
3900 {
3901 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003902 /* Display the absolute or relative line number. After the
3903 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3904 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 && (row == startrow
3906#ifdef FEAT_DIFF
3907 + filler_lines
3908#endif
3909 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3910 {
3911 /* Draw the line number (empty space after wrapping). */
3912 if (row == startrow
3913#ifdef FEAT_DIFF
3914 + filler_lines
3915#endif
3916 )
3917 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003918 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003919 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003920
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003921 if (wp->w_p_nu && !wp->w_p_rnu)
3922 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003923 num = (long)lnum;
3924 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003925 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003926 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003927 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003928 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003929 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003930 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003931 num = lnum;
3932 fmt = "%-*ld ";
3933 }
3934 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003935
Bram Moolenaar700e7342013-01-30 12:31:36 +01003936 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003937 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (wp->w_skipcol > 0)
3939 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3940 *p_extra = '-';
3941#ifdef FEAT_RIGHTLEFT
3942 if (wp->w_p_rl) /* reverse line numbers */
3943 rl_mirror(extra);
3944#endif
3945 p_extra = extra;
3946 c_extra = NUL;
3947 }
3948 else
3949 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003950 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003951 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003952#ifdef FEAT_SYN_HL
3953 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003954 * the current line differently.
3955 * TODO: Can we use CursorLine instead of CursorLineNr
3956 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003957 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003958 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003959 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 }
3963
Bram Moolenaar597a4222014-06-25 14:39:50 +02003964#ifdef FEAT_LINEBREAK
3965 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3966 && n_extra == 0 && *p_sbr != NUL)
3967 /* draw indent after showbreak value */
3968 draw_state = WL_BRI;
3969 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3970 /* After the showbreak, draw the breakindent */
3971 draw_state = WL_BRI - 1;
3972
3973 /* draw 'breakindent': indent wrapped text accordingly */
3974 if (draw_state == WL_BRI - 1 && n_extra == 0)
3975 {
3976 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003977 /* if need_showbreak is set, breakindent also applies */
3978 if (wp->w_p_bri && n_extra == 0
3979 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003980# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003981 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003982# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003983 )
3984 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003985 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003986# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003987 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003988 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003989 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003990# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003991 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3992 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003993 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003994# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02003995 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003996# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02003997 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02003998 c_extra = ' ';
3999 n_extra = get_breakindent_win(wp,
4000 ml_get_buf(wp->w_buffer, lnum, FALSE));
4001 /* Correct end of highlighted area for 'breakindent',
4002 * required when 'linebreak' is also set. */
4003 if (tocol == vcol)
4004 tocol += n_extra;
4005 }
4006 }
4007#endif
4008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4010 if (draw_state == WL_SBR - 1 && n_extra == 0)
4011 {
4012 draw_state = WL_SBR;
4013# ifdef FEAT_DIFF
4014 if (filler_todo > 0)
4015 {
4016 /* Draw "deleted" diff line(s). */
4017 if (char2cells(fill_diff) > 1)
4018 c_extra = '-';
4019 else
4020 c_extra = fill_diff;
4021# ifdef FEAT_RIGHTLEFT
4022 if (wp->w_p_rl)
4023 n_extra = col + 1;
4024 else
4025# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004026 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004027 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029# endif
4030# ifdef FEAT_LINEBREAK
4031 if (*p_sbr != NUL && need_showbreak)
4032 {
4033 /* Draw 'showbreak' at the start of each broken line. */
4034 p_extra = p_sbr;
4035 c_extra = NUL;
4036 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004037 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004039 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /* Correct end of highlighted area for 'showbreak',
4041 * required when 'linebreak' is also set. */
4042 if (tocol == vcol)
4043 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004044#ifdef FEAT_SYN_HL
4045 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004046 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004047 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004048 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
4051# endif
4052 }
4053#endif
4054
4055 if (draw_state == WL_LINE - 1 && n_extra == 0)
4056 {
4057 draw_state = WL_LINE;
4058 if (saved_n_extra)
4059 {
4060 /* Continue item from end of wrapped line. */
4061 n_extra = saved_n_extra;
4062 c_extra = saved_c_extra;
4063 p_extra = saved_p_extra;
4064 char_attr = saved_char_attr;
4065 }
4066 else
4067 char_attr = 0;
4068 }
4069 }
4070
4071 /* When still displaying '$' of change command, stop at cursor */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01004072 if (dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074#ifdef FEAT_DIFF
4075 && filler_todo <= 0
4076#endif
4077 )
4078 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004079 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004080 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004081 /* Pretend we have finished updating the window. Except when
4082 * 'cursorcolumn' is set. */
4083#ifdef FEAT_SYN_HL
4084 if (wp->w_p_cuc)
4085 row = wp->w_cline_row + wp->w_cline_height;
4086 else
4087#endif
4088 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 break;
4090 }
4091
4092 if (draw_state == WL_LINE && area_highlighting)
4093 {
4094 /* handle Visual or match highlighting in this line */
4095 if (vcol == fromcol
4096#ifdef FEAT_MBYTE
4097 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4098 && (*mb_ptr2cells)(ptr) > 1)
4099#endif
4100 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004101 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 && vcol < tocol))
4103 area_attr = attr; /* start highlighting */
4104 else if (area_attr != 0
4105 && (vcol == tocol
4106 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
4109#ifdef FEAT_SEARCH_EXTRA
4110 if (!n_extra)
4111 {
4112 /*
4113 * Check for start/end of search pattern match.
4114 * After end, check for start/end of next match.
4115 * When another match, have to check for start again.
4116 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004117 * Do this for 'search_hl' and the match list (ordered by
4118 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004120 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004121 cur = wp->w_match_head;
4122 shl_flag = FALSE;
4123 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004125 if (shl_flag == FALSE
4126 && ((cur != NULL
4127 && cur->priority > SEARCH_HL_PRIORITY)
4128 || cur == NULL))
4129 {
4130 shl = &search_hl;
4131 shl_flag = TRUE;
4132 }
4133 else
4134 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004135 if (cur != NULL)
4136 cur->pos.cur = 0;
4137 pos_inprogress = TRUE;
4138 while (shl->rm.regprog != NULL
4139 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004141 if (shl->startcol != MAXCOL
4142 && v >= (long)shl->startcol
4143 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004145#ifdef FEAT_MBYTE
4146 int tmp_col = v + MB_PTR2LEN(ptr);
4147
4148 if (shl->endcol < tmp_col)
4149 shl->endcol = tmp_col;
4150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004152#ifdef FEAT_CONCEAL
4153 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4154 == cur->hlg_id)
4155 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004156 has_match_conc =
4157 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004158 match_conc = cur->conceal_char;
4159 }
4160 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004161 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004164 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004167 next_search_hl(wp, shl, lnum, (colnr_T)v,
4168 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004169 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004170 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
4172 /* Need to get the line again, a multi-line regexp
4173 * may have made it invalid. */
4174 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4175 ptr = line + v;
4176
4177 if (shl->lnum == lnum)
4178 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004179 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004181 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004183 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004185 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 /* highlight empty match, try again after
4188 * it */
4189#ifdef FEAT_MBYTE
4190 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004191 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004192 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
4194#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004195 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197
4198 /* Loop to check if the match starts at the
4199 * current position */
4200 continue;
4201 }
4202 }
4203 break;
4204 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004205 if (shl != &search_hl && cur != NULL)
4206 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004208
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004209 /* Use attributes from match with highest priority among
4210 * 'search_hl' and the match list. */
4211 search_attr = search_hl.attr_cur;
4212 cur = wp->w_match_head;
4213 shl_flag = FALSE;
4214 while (cur != NULL || shl_flag == FALSE)
4215 {
4216 if (shl_flag == FALSE
4217 && ((cur != NULL
4218 && cur->priority > SEARCH_HL_PRIORITY)
4219 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004220 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004221 shl = &search_hl;
4222 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004223 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004224 else
4225 shl = &cur->hl;
4226 if (shl->attr_cur != 0)
4227 search_attr = shl->attr_cur;
4228 if (shl != &search_hl && cur != NULL)
4229 cur = cur->next;
4230 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004231 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004232 if (*ptr == NUL && (did_line_attr >= 1
4233 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004234 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236#endif
4237
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004239 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004241 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4242 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004244 if (diff_hlf == HLF_TXD && ptr - line > change_end
4245 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004247 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004248 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004249 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004252
4253 /* Decide which of the highlight attributes to use. */
4254 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004255#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004256 if (area_attr != 0)
4257 char_attr = hl_combine_attr(line_attr, area_attr);
4258 else if (search_attr != 0)
4259 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004260 /* Use line_attr when not in the Visual or 'incsearch' area
4261 * (area_attr may be 0 when "noinvcur" is set). */
4262 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004263 || vcol < fromcol || vcol_prev < fromcol_prev
4264 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004265 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004266#else
4267 if (area_attr != 0)
4268 char_attr = area_attr;
4269 else if (search_attr != 0)
4270 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004271#endif
4272 else
4273 {
4274 attr_pri = FALSE;
4275#ifdef FEAT_SYN_HL
4276 if (has_syntax)
4277 char_attr = syntax_attr;
4278 else
4279#endif
4280 char_attr = 0;
4281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283
4284 /*
4285 * Get the next character to put on the screen.
4286 */
4287 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004288 * The "p_extra" points to the extra stuff that is inserted to
4289 * represent special characters (non-printable stuff) and other
4290 * things. When all characters are the same, c_extra is used.
4291 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4292 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4294 */
4295 if (n_extra > 0)
4296 {
4297 if (c_extra != NUL)
4298 {
4299 c = c_extra;
4300#ifdef FEAT_MBYTE
4301 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004302 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
4304 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004305 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004306 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 else
4309 mb_utf8 = FALSE;
4310#endif
4311 }
4312 else
4313 {
4314 c = *p_extra;
4315#ifdef FEAT_MBYTE
4316 if (has_mbyte)
4317 {
4318 mb_c = c;
4319 if (enc_utf8)
4320 {
4321 /* If the UTF-8 character is more than one byte:
4322 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004323 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 mb_utf8 = FALSE;
4325 if (mb_l > n_extra)
4326 mb_l = 1;
4327 else if (mb_l > 1)
4328 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004329 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004331 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 }
4333 }
4334 else
4335 {
4336 /* if this is a DBCS character, put it in "mb_c" */
4337 mb_l = MB_BYTE2LEN(c);
4338 if (mb_l >= n_extra)
4339 mb_l = 1;
4340 else if (mb_l > 1)
4341 mb_c = (c << 8) + p_extra[1];
4342 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004343 if (mb_l == 0) /* at the NUL at end-of-line */
4344 mb_l = 1;
4345
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 /* If a double-width char doesn't fit display a '>' in the
4347 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004348 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349# ifdef FEAT_RIGHTLEFT
4350 wp->w_p_rl ? (col <= 0) :
4351# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004352 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 && (*mb_char2cells)(mb_c) == 2)
4354 {
4355 c = '>';
4356 mb_c = c;
4357 mb_l = 1;
4358 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004359 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /* put the pointer back to output the double-width
4361 * character at the start of the next line. */
4362 ++n_extra;
4363 --p_extra;
4364 }
4365 else
4366 {
4367 n_extra -= mb_l - 1;
4368 p_extra += mb_l - 1;
4369 }
4370 }
4371#endif
4372 ++p_extra;
4373 }
4374 --n_extra;
4375 }
4376 else
4377 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004378#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004379 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004380#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004381
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004382 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004383 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 /*
4385 * Get a character from the line itself.
4386 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004387 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004388#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004389 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#ifdef FEAT_MBYTE
4392 if (has_mbyte)
4393 {
4394 mb_c = c;
4395 if (enc_utf8)
4396 {
4397 /* If the UTF-8 character is more than one byte: Decode it
4398 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004399 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 mb_utf8 = FALSE;
4401 if (mb_l > 1)
4402 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004403 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 /* Overlong encoded ASCII or ASCII with composing char
4405 * is displayed normally, except a NUL. */
4406 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004407 {
4408 c = mb_c;
4409# ifdef FEAT_LINEBREAK
4410 c0 = mb_c;
4411# endif
4412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004414
4415 /* At start of the line we can have a composing char.
4416 * Draw it as a space with a composing char. */
4417 if (utf_iscomposing(mb_c))
4418 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004419 int i;
4420
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004421 for (i = Screen_mco - 1; i > 0; --i)
4422 u8cc[i] = u8cc[i - 1];
4423 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004424 mb_c = ' ';
4425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427
4428 if ((mb_l == 1 && c >= 0x80)
4429 || (mb_l >= 1 && mb_c == 0)
4430 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004431# ifdef UNICODE16
4432 || mb_c >= 0x10000
4433# endif
4434 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 {
4436 /*
4437 * Illegal UTF-8 byte: display as <xx>.
4438 * Non-BMP character : display as ? or fullwidth ?.
4439 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004440# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004445# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (wp->w_p_rl) /* reverse */
4447 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004448# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004450# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 else if (utf_char2cells(mb_c) != 2)
4452 STRCPY(extra, "?");
4453 else
4454 /* 0xff1f in UTF-8: full-width '?' */
4455 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 p_extra = extra;
4459 c = *p_extra;
4460 mb_c = mb_ptr2char_adv(&p_extra);
4461 mb_utf8 = (c >= 0x80);
4462 n_extra = (int)STRLEN(p_extra);
4463 c_extra = NUL;
4464 if (area_attr == 0 && search_attr == 0)
4465 {
4466 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004467 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 saved_attr2 = char_attr; /* save current attr */
4469 }
4470 }
4471 else if (mb_l == 0) /* at the NUL at end-of-line */
4472 mb_l = 1;
4473#ifdef FEAT_ARABIC
4474 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4475 {
4476 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004477 int pc, pc1, nc;
4478 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 /* The idea of what is the previous and next
4481 * character depends on 'rightleft'. */
4482 if (wp->w_p_rl)
4483 {
4484 pc = prev_c;
4485 pc1 = prev_c1;
4486 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004487 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 else
4490 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004491 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004493 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 prev_c = mb_c;
4496
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 else
4500 prev_c = mb_c;
4501#endif
4502 }
4503 else /* enc_dbcs */
4504 {
4505 mb_l = MB_BYTE2LEN(c);
4506 if (mb_l == 0) /* at the NUL at end-of-line */
4507 mb_l = 1;
4508 else if (mb_l > 1)
4509 {
4510 /* We assume a second byte below 32 is illegal.
4511 * Hopefully this is OK for all double-byte encodings!
4512 */
4513 if (ptr[1] >= 32)
4514 mb_c = (c << 8) + ptr[1];
4515 else
4516 {
4517 if (ptr[1] == NUL)
4518 {
4519 /* head byte at end of line */
4520 mb_l = 1;
4521 transchar_nonprint(extra, c);
4522 }
4523 else
4524 {
4525 /* illegal tail byte */
4526 mb_l = 2;
4527 STRCPY(extra, "XX");
4528 }
4529 p_extra = extra;
4530 n_extra = (int)STRLEN(extra) - 1;
4531 c_extra = NUL;
4532 c = *p_extra++;
4533 if (area_attr == 0 && search_attr == 0)
4534 {
4535 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004536 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 saved_attr2 = char_attr; /* save current attr */
4538 }
4539 mb_c = c;
4540 }
4541 }
4542 }
4543 /* If a double-width char doesn't fit display a '>' in the
4544 * last column; the character is displayed at the start of the
4545 * next line. */
4546 if ((
4547# ifdef FEAT_RIGHTLEFT
4548 wp->w_p_rl ? (col <= 0) :
4549# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004550 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 && (*mb_char2cells)(mb_c) == 2)
4552 {
4553 c = '>';
4554 mb_c = c;
4555 mb_utf8 = FALSE;
4556 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004557 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 /* Put pointer back so that the character will be
4559 * displayed at the start of the next line. */
4560 --ptr;
4561 }
4562 else if (*ptr != NUL)
4563 ptr += mb_l - 1;
4564
4565 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004566 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004567 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004568 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004571 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 c = ' ';
4573 if (area_attr == 0 && search_attr == 0)
4574 {
4575 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004576 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 saved_attr2 = char_attr; /* save current attr */
4578 }
4579 mb_c = c;
4580 mb_utf8 = FALSE;
4581 mb_l = 1;
4582 }
4583
4584 }
4585#endif
4586 ++ptr;
4587
4588 if (extra_check)
4589 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004590#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004591 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004592#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004593
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004594#ifdef FEAT_TERMINAL
4595 if (get_term_attr)
4596 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004597 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004598
4599 if (!attr_pri)
4600 char_attr = syntax_attr;
4601 else
4602 char_attr = hl_combine_attr(syntax_attr, char_attr);
4603 }
4604#endif
4605
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004606#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /* Get syntax attribute, unless still at the start of the line
4608 * (double-wide char that doesn't fit). */
Bram Moolenaar217ad922005-03-20 22:37:15 +00004609 v = (long)(ptr - line);
4610 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612 /* Get the syntax attribute for the character. If there
4613 * is an error, disable syntax highlighting. */
4614 save_did_emsg = did_emsg;
4615 did_emsg = FALSE;
4616
Bram Moolenaar217ad922005-03-20 22:37:15 +00004617 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004618# ifdef FEAT_SPELL
4619 has_spell ? &can_spell :
4620# endif
4621 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004624 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004625 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004626 has_syntax = FALSE;
4627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 else
4629 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004630#ifdef SYN_TIME_LIMIT
4631 if (wp->w_s->b_syn_slow)
4632 has_syntax = FALSE;
4633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 /* Need to get the line again, a multi-line regexp may
4636 * have made it invalid. */
4637 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4638 ptr = line + v;
4639
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004640 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 char_attr = syntax_attr;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004642 else
Bram Moolenaarbc045ea2005-06-05 22:01:26 +00004643 char_attr = hl_combine_attr(syntax_attr, char_attr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004644# ifdef FEAT_CONCEAL
4645 /* no concealing past the end of the line, it interferes
4646 * with line highlighting */
4647 if (c == NUL)
4648 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004649 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004650 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004653#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004654
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004655#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004656 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004657 * Only do this when there is no syntax highlighting, the
4658 * @Spell cluster is not used or the current syntax item
4659 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004660 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004661 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004662 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004663# ifdef FEAT_SYN_HL
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004664 if (!attr_pri)
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004665 char_attr = syntax_attr;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004666# endif
4667 if (c != 0 && (
4668# ifdef FEAT_SYN_HL
4669 !has_syntax ||
4670# endif
4671 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004672 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004673 char_u *prev_ptr, *p;
4674 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004675 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004676# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004677 if (has_mbyte)
4678 {
4679 prev_ptr = ptr - mb_l;
4680 v -= mb_l - 1;
4681 }
4682 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004683# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004684 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004685
4686 /* Use nextline[] if possible, it has the start of the
4687 * next line concatenated. */
4688 if ((prev_ptr - line) - nextlinecol >= 0)
4689 p = nextline + (prev_ptr - line) - nextlinecol;
4690 else
4691 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004692 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004693 len = spell_check(wp, p, &spell_hlf, &cap_col,
4694 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004695 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004696
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004697 /* In Insert mode only highlight a word that
4698 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004699 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004700 && (State & INSERT) != 0
4701 && wp->w_cursor.lnum == lnum
4702 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004703 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004704 && wp->w_cursor.col < (colnr_T)word_end)
4705 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004706 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004707 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004708 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004709
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004710 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004711 && (p - nextline) + len > nextline_idx)
4712 {
4713 /* Remember that the good word continues at the
4714 * start of the next line. */
4715 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004716 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004717 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004718
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004719 /* Turn index into actual attributes. */
4720 if (spell_hlf != HLF_COUNT)
4721 spell_attr = highlight_attr[spell_hlf];
4722
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004723 if (cap_col > 0)
4724 {
4725 if (p != prev_ptr
4726 && (p - nextline) + cap_col >= nextline_idx)
4727 {
4728 /* Remember that the word in the next line
4729 * must start with a capital. */
4730 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004731 cap_col = (int)((p - nextline) + cap_col
4732 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004733 }
4734 else
4735 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004736 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004737 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004738 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004739 }
4740 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004741 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004742 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004743 char_attr = hl_combine_attr(char_attr, spell_attr);
4744 else
4745 char_attr = hl_combine_attr(spell_attr, char_attr);
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#endif
4748#ifdef FEAT_LINEBREAK
4749 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004750 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004752 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004753 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004755# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004756 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004757# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004758 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004760 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004762 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004763
Bram Moolenaar597a4222014-06-25 14:39:50 +02004764 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004765 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004766 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004767 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004768# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004769 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004770 wp->w_buffer->b_p_vts_array) - 1;
4771# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004772 n_extra = (int)wp->w_buffer->b_p_ts
4773 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004774# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004775
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004776# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004777 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004778# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004780# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004781 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004782 {
4783#ifdef FEAT_CONCEAL
4784 if (c == TAB)
4785 /* See "Tab alignment" below. */
4786 FIX_FOR_BOGUSCOLS;
4787#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004788 if (!wp->w_p_list)
4789 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 }
4792#endif
4793
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004794 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4795 */
4796 if (wp->w_p_list
4797 && (((c == 160
4798#ifdef FEAT_MBYTE
4799 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4800#endif
4801 ) && lcs_nbsp)
4802 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4803 {
4804 c = (c == ' ') ? lcs_space : lcs_nbsp;
4805 if (area_attr == 0 && search_attr == 0)
4806 {
4807 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004808 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004809 saved_attr2 = char_attr; /* save current attr */
4810 }
4811#ifdef FEAT_MBYTE
4812 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004813 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004814 {
4815 mb_utf8 = TRUE;
4816 u8cc[0] = 0;
4817 c = 0xc0;
4818 }
4819 else
4820 mb_utf8 = FALSE;
4821#endif
4822 }
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4825 {
4826 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004827 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
4829 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004830 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 saved_attr2 = char_attr; /* save current attr */
4832 }
4833#ifdef FEAT_MBYTE
4834 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004835 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
4837 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004838 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004839 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 else
4842 mb_utf8 = FALSE;
4843#endif
4844 }
4845 }
4846
4847 /*
4848 * Handling of non-printable characters.
4849 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004850 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
4852 /*
4853 * when getting a character from the file, we may have to
4854 * turn it into something else on the way to putting it
4855 * into "ScreenLines".
4856 */
4857 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4858 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004859 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004860 long vcol_adjusted = vcol; /* removed showbreak length */
4861#ifdef FEAT_LINEBREAK
4862 /* only adjust the tab_len, when at the first column
4863 * after the showbreak value was drawn */
4864 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4865 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004868#ifdef FEAT_VARTABS
4869 tab_len = tabstop_padding(vcol_adjusted,
4870 wp->w_buffer->b_p_ts,
4871 wp->w_buffer->b_p_vts_array) - 1;
4872#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004873 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004874 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4875#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004876
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004877#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004878 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004879#endif
4880 /* tab amount depends on current column */
4881 n_extra = tab_len;
4882#ifdef FEAT_LINEBREAK
4883 else
4884 {
4885 char_u *p;
4886 int len = n_extra;
4887 int i;
4888 int saved_nextra = n_extra;
4889
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004890#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004891 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004892 /* there are characters to conceal */
4893 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004894 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4895 */
4896 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4897 && n_extra > tab_len)
4898 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004899#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004900
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004901 /* if n_extra > 0, it gives the number of chars, to
4902 * use for a tab, else we need to calculate the width
4903 * for a tab */
4904#ifdef FEAT_MBYTE
4905 len = (tab_len * mb_char2len(lcs_tab2));
4906 if (n_extra > 0)
4907 len += n_extra - tab_len;
4908#endif
4909 c = lcs_tab1;
4910 p = alloc((unsigned)(len + 1));
4911 vim_memset(p, ' ', len);
4912 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004913 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004914 p_extra_free = p;
4915 for (i = 0; i < tab_len; i++)
4916 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004917 if (*p == NUL)
4918 {
4919 tab_len = i;
4920 break;
4921 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004922#ifdef FEAT_MBYTE
4923 mb_char2bytes(lcs_tab2, p);
4924 p += mb_char2len(lcs_tab2);
4925 n_extra += mb_char2len(lcs_tab2)
4926 - (saved_nextra > 0 ? 1 : 0);
4927#else
4928 p[i] = lcs_tab2;
4929#endif
4930 }
4931 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004932#ifdef FEAT_CONCEAL
4933 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4934 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004935 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004936 n_extra -= vcol_off;
4937#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004938 }
4939#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004940#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004941 {
4942 int vc_saved = vcol_off;
4943
4944 /* Tab alignment should be identical regardless of
4945 * 'conceallevel' value. So tab compensates of all
4946 * previous concealed characters, and thus resets
4947 * vcol_off and boguscols accumulated so far in the
4948 * line. Note that the tab can be longer than
4949 * 'tabstop' when there are concealed characters. */
4950 FIX_FOR_BOGUSCOLS;
4951
4952 /* Make sure, the highlighting for the tab char will be
4953 * correctly set further below (effectively reverts the
4954 * FIX_FOR_BOGSUCOLS macro */
4955 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004956 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004957 tab_len += vc_saved;
4958 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#ifdef FEAT_MBYTE
4961 mb_utf8 = FALSE; /* don't draw as UTF-8 */
4962#endif
4963 if (wp->w_p_list)
4964 {
4965 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004966#ifdef FEAT_LINEBREAK
4967 if (wp->w_p_lbr)
4968 c_extra = NUL; /* using p_extra from above */
4969 else
4970#endif
4971 c_extra = lcs_tab2;
4972 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004973 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 saved_attr2 = char_attr; /* save current attr */
4975#ifdef FEAT_MBYTE
4976 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004977 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004980 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004981 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
4983#endif
4984 }
4985 else
4986 {
4987 c_extra = ' ';
4988 c = ' ';
4989 }
4990 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004991 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02004992 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004993 || ((fromcol >= 0 || fromcol_prev >= 0)
4994 && tocol > vcol
4995 && VIsual_mode != Ctrl_V
4996 && (
4997# ifdef FEAT_RIGHTLEFT
4998 wp->w_p_rl ? (col >= 0) :
4999# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005000 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005001 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005002 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005003 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005004 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005006 /* Display a '$' after the line or highlight an extra
5007 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5009 /* For a diff line the highlighting continues after the
5010 * "$". */
5011 if (
5012# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005013 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014# ifdef LINE_ATTR
5015 &&
5016# endif
5017# endif
5018# ifdef LINE_ATTR
5019 line_attr == 0
5020# endif
5021 )
5022#endif
5023 {
5024#ifdef FEAT_VIRTUALEDIT
5025 /* In virtualedit, visual selections may extend
5026 * beyond end of line. */
5027 if (area_highlighting && virtual_active()
5028 && tocol != MAXCOL && vcol < tocol)
5029 n_extra = 0;
5030 else
5031#endif
5032 {
5033 p_extra = at_end_str;
5034 n_extra = 1;
5035 c_extra = NUL;
5036 }
5037 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005038 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005039 c = lcs_eol;
5040 else
5041 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 lcs_eol_one = -1;
5043 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005044 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005046 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 n_attr = 1;
5048 }
5049#ifdef FEAT_MBYTE
5050 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005051 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005054 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005055 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 else
5058 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5059#endif
5060 }
5061 else if (c != NUL)
5062 {
5063 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005064 if (n_extra == 0)
5065 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066#ifdef FEAT_RIGHTLEFT
5067 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5068 rl_mirror(p_extra); /* reverse "<12>" */
5069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005071#ifdef FEAT_LINEBREAK
5072 if (wp->w_p_lbr)
5073 {
5074 char_u *p;
5075
5076 c = *p_extra;
5077 p = alloc((unsigned)n_extra + 1);
5078 vim_memset(p, ' ', n_extra);
5079 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5080 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005081 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005082 p_extra_free = p_extra = p;
5083 }
5084 else
5085#endif
5086 {
5087 n_extra = byte2cells(c) - 1;
5088 c = *p_extra++;
5089 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005090 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005093 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 saved_attr2 = char_attr; /* save current attr */
5095 }
5096#ifdef FEAT_MBYTE
5097 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5098#endif
5099 }
5100#ifdef FEAT_VIRTUALEDIT
5101 else if (VIsual_active
5102 && (VIsual_mode == Ctrl_V
5103 || VIsual_mode == 'v')
5104 && virtual_active()
5105 && tocol != MAXCOL
5106 && vcol < tocol
5107 && (
5108# ifdef FEAT_RIGHTLEFT
5109 wp->w_p_rl ? (col >= 0) :
5110# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005111 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 c = ' ';
5114 --ptr; /* put it back at the NUL */
5115 }
5116#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005117#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else if ((
5119# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005120 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005122# ifdef FEAT_TERMINAL
5123 term_attr != 0 ||
5124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 ) && (
5127# ifdef FEAT_RIGHTLEFT
5128 wp->w_p_rl ? (col >= 0) :
5129# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005130 (col
5131# ifdef FEAT_CONCEAL
5132 - boguscols
5133# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005134 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 /* Highlight until the right side of the window */
5137 c = ' ';
5138 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005139
5140 /* Remember we do the char for line highlighting. */
5141 ++did_line_attr;
5142
5143 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005144 if (line_attr != 0 && char_attr == search_attr
5145 && (did_line_attr > 1
5146 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005147 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148# ifdef FEAT_DIFF
5149 if (diff_hlf == HLF_TXD)
5150 {
5151 diff_hlf = HLF_CHD;
5152 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005153 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005154 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005155 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5156 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005157 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005161# ifdef FEAT_TERMINAL
5162 if (term_attr != 0)
5163 {
5164 char_attr = term_attr;
5165 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5166 char_attr = hl_combine_attr(char_attr,
5167 HL_ATTR(HLF_CUL));
5168 }
5169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171#endif
5172 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005173
5174#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005175 if ( wp->w_p_cole > 0
5176 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005177 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005178 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005179 && !(lnum_in_visual_area
5180 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005181 {
5182 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005183 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005184 && (syn_get_sub_char() != NUL || match_conc
5185 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005186 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005187 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005188 /* First time at this concealed item: display one
5189 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005190 if (match_conc)
5191 c = match_conc;
5192 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005193 c = syn_get_sub_char();
5194 else if (lcs_conceal != NUL)
5195 c = lcs_conceal;
5196 else
5197 c = ' ';
5198
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005199 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005200
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005201 if (n_extra > 0)
5202 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005203 vcol += n_extra;
5204 if (wp->w_p_wrap && n_extra > 0)
5205 {
5206# ifdef FEAT_RIGHTLEFT
5207 if (wp->w_p_rl)
5208 {
5209 col -= n_extra;
5210 boguscols -= n_extra;
5211 }
5212 else
5213# endif
5214 {
5215 boguscols += n_extra;
5216 col += n_extra;
5217 }
5218 }
5219 n_extra = 0;
5220 n_attr = 0;
5221 }
5222 else if (n_skip == 0)
5223 {
5224 is_concealing = TRUE;
5225 n_skip = 1;
5226 }
5227# ifdef FEAT_MBYTE
5228 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005229 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005230 {
5231 mb_utf8 = TRUE;
5232 u8cc[0] = 0;
5233 c = 0xc0;
5234 }
5235 else
5236 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5237# endif
5238 }
5239 else
5240 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005241 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005242 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005243 }
5244#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005247#ifdef FEAT_CONCEAL
5248 /* In the cursor line and we may be concealing characters: correct
5249 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005250 if (!did_wcol && draw_state == WL_LINE
5251 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005252 && conceal_cursor_line(wp)
5253 && (int)wp->w_virtcol <= vcol + n_skip)
5254 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005255# ifdef FEAT_RIGHTLEFT
5256 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005257 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005258 else
5259# endif
5260 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005261 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005262 did_wcol = TRUE;
5263 }
5264#endif
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /* Don't override visual selection highlighting. */
5267 if (n_attr > 0
5268 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005269 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 char_attr = extra_attr;
5271
Bram Moolenaar81695252004-12-29 20:58:21 +00005272#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 /* XIM don't send preedit_start and preedit_end, but they send
5274 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5275 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005276 if (p_imst == IM_ON_THE_SPOT
5277 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005278 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 && (State & INSERT)
5280 && !p_imdisable
5281 && im_is_preediting()
5282 && draw_state == WL_LINE)
5283 {
5284 colnr_T tcol;
5285
5286 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005287 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 else
5289 tcol = preedit_end_col;
5290 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5291 {
5292 if (feedback_old_attr < 0)
5293 {
5294 feedback_col = 0;
5295 feedback_old_attr = char_attr;
5296 }
5297 char_attr = im_get_feedback_attr(feedback_col);
5298 if (char_attr < 0)
5299 char_attr = feedback_old_attr;
5300 feedback_col++;
5301 }
5302 else if (feedback_old_attr >= 0)
5303 {
5304 char_attr = feedback_old_attr;
5305 feedback_old_attr = -1;
5306 feedback_col = 0;
5307 }
5308 }
5309#endif
5310 /*
5311 * Handle the case where we are in column 0 but not on the first
5312 * character of the line and the user wants us to show us a
5313 * special character (via 'listchars' option "precedes:<char>".
5314 */
5315 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005316 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5318#ifdef FEAT_DIFF
5319 && filler_todo <= 0
5320#endif
5321 && draw_state > WL_NR
5322 && c != NUL)
5323 {
5324 c = lcs_prec;
5325 lcs_prec_todo = NUL;
5326#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005327 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5328 {
5329 /* Double-width character being overwritten by the "precedes"
5330 * character, need to fill up half the character. */
5331 c_extra = MB_FILLER_CHAR;
5332 n_extra = 1;
5333 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005334 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005337 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
5339 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005340 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005341 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343 else
5344 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5345#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005346 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
5348 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005349 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 n_attr3 = 1;
5351 }
5352 }
5353
5354 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005355 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005357 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005358#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005359 || did_line_attr == 1
5360#endif
5361 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005363#ifdef FEAT_SEARCH_EXTRA
5364 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005365
5366 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005367 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005368 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005369#endif
5370
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005371 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 * highlight match at end of line. If it's beyond the last
5373 * char on the screen, just overwrite that one (tricky!) Not
5374 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005375#ifdef FEAT_SEARCH_EXTRA
5376 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005377 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005378 prevcol_hl_flag = TRUE;
5379 else
5380 {
5381 cur = wp->w_match_head;
5382 while (cur != NULL)
5383 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005384 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005385 {
5386 prevcol_hl_flag = TRUE;
5387 break;
5388 }
5389 cur = cur->next;
5390 }
5391 }
5392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005394 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005395 && (VIsual_mode != Ctrl_V
5396 || lnum == VIsual.lnum
5397 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005398 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#ifdef FEAT_SEARCH_EXTRA
5400 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005401 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005402# ifdef FEAT_SYN_HL
5403 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5404 && !(wp == curwin && VIsual_active))
5405# endif
5406# ifdef FEAT_DIFF
5407 && diff_hlf == (hlf_T)0
5408# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005409# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005410 && did_line_attr <= 1
5411# endif
5412 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#endif
5414 ))
5415 {
5416 int n = 0;
5417
5418#ifdef FEAT_RIGHTLEFT
5419 if (wp->w_p_rl)
5420 {
5421 if (col < 0)
5422 n = 1;
5423 }
5424 else
5425#endif
5426 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005427 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 n = -1;
5429 }
5430 if (n != 0)
5431 {
5432 /* At the window boundary, highlight the last character
5433 * instead (better than nothing). */
5434 off += n;
5435 col += n;
5436 }
5437 else
5438 {
5439 /* Add a blank character to highlight. */
5440 ScreenLines[off] = ' ';
5441#ifdef FEAT_MBYTE
5442 if (enc_utf8)
5443 ScreenLinesUC[off] = 0;
5444#endif
5445 }
5446#ifdef FEAT_SEARCH_EXTRA
5447 if (area_attr == 0)
5448 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005449 /* Use attributes from match with highest priority among
5450 * 'search_hl' and the match list. */
5451 char_attr = search_hl.attr;
5452 cur = wp->w_match_head;
5453 shl_flag = FALSE;
5454 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005455 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005456 if (shl_flag == FALSE
5457 && ((cur != NULL
5458 && cur->priority > SEARCH_HL_PRIORITY)
5459 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005460 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005461 shl = &search_hl;
5462 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005463 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005464 else
5465 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005466 if ((ptr - line) - 1 == (long)shl->startcol
5467 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005468 char_attr = shl->attr;
5469 if (shl != &search_hl && cur != NULL)
5470 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473#endif
5474 ScreenAttrs[off] = char_attr;
5475#ifdef FEAT_RIGHTLEFT
5476 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005479 --off;
5480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 else
5482#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005485 ++off;
5486 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005487 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005488#ifdef FEAT_SYN_HL
5489 eol_hl_off = 1;
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar91170f82006-05-05 21:15:17 +00005494 /*
5495 * At end of the text line.
5496 */
5497 if (c == NUL)
5498 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005499#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005500 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005501 if (wp->w_p_wrap)
5502 v = wp->w_skipcol;
5503 else
5504 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005505
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005506 /* check if line ends before left margin */
5507 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005508 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005509#ifdef FEAT_CONCEAL
5510 /* Get rid of the boguscols now, we want to draw until the right
5511 * edge for 'cursorcolumn'. */
5512 col -= boguscols;
5513 boguscols = 0;
5514#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005515
5516 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005517 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005518
5519 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005520 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5521 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005522 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005523 && lnum != wp->w_cursor.lnum)
5524 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005525# ifdef FEAT_RIGHTLEFT
5526 && !wp->w_p_rl
5527# endif
5528 )
5529 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005530 int rightmost_vcol = 0;
5531 int i;
5532
5533 if (wp->w_p_cuc)
5534 rightmost_vcol = wp->w_virtcol;
5535 if (draw_color_col)
5536 /* determine rightmost colorcolumn to possibly draw */
5537 for (i = 0; color_cols[i] >= 0; ++i)
5538 if (rightmost_vcol < color_cols[i])
5539 rightmost_vcol = color_cols[i];
5540
Bram Moolenaar02631462017-09-22 15:20:32 +02005541 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005542 {
5543 ScreenLines[off] = ' ';
5544#ifdef FEAT_MBYTE
5545 if (enc_utf8)
5546 ScreenLinesUC[off] = 0;
5547#endif
5548 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005549 if (draw_color_col)
5550 draw_color_col = advance_color_col(VCOL_HLC,
5551 &color_cols);
5552
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005553 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005554 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005555 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005556 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005557 else
5558 ScreenAttrs[off++] = 0;
5559
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005560 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005561 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005562
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005563 ++vcol;
5564 }
5565 }
5566#endif
5567
Bram Moolenaar53f81742017-09-22 14:35:51 +02005568 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005569 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 row++;
5571
5572 /*
5573 * Update w_cline_height and w_cline_folded if the cursor line was
5574 * updated (saves a call to plines() later).
5575 */
5576 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5577 {
5578 curwin->w_cline_row = startrow;
5579 curwin->w_cline_height = row - startrow;
5580#ifdef FEAT_FOLDING
5581 curwin->w_cline_folded = FALSE;
5582#endif
5583 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5584 }
5585
5586 break;
5587 }
5588
5589 /* line continues beyond line end */
5590 if (lcs_ext
5591 && !wp->w_p_wrap
5592#ifdef FEAT_DIFF
5593 && filler_todo <= 0
5594#endif
5595 && (
5596#ifdef FEAT_RIGHTLEFT
5597 wp->w_p_rl ? col == 0 :
5598#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005599 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005601 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5603 {
5604 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005605 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606#ifdef FEAT_MBYTE
5607 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005608 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005611 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005612 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 else
5615 mb_utf8 = FALSE;
5616#endif
5617 }
5618
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005619#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005620 /* advance to the next 'colorcolumn' */
5621 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005622 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005623
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625 * highlight the cursor position itself.
5626 * Also highlight the 'colorcolumn' if it is different than
5627 * 'cursorcolumn' */
5628 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005629 if (draw_state == WL_LINE && !lnum_in_visual_area
5630 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005632 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633 && lnum != wp->w_cursor.lnum)
5634 {
5635 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005636 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005638 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639 {
5640 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005641 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005643 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005644#endif
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /*
5647 * Store character to be displayed.
5648 * Skip characters that are left of the screen for 'nowrap'.
5649 */
5650 vcol_prev = vcol;
5651 if (draw_state < WL_LINE || n_skip <= 0)
5652 {
5653 /*
5654 * Store the character.
5655 */
5656#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5657 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5658 {
5659 /* A double-wide character is: put first halve in left cell. */
5660 --off;
5661 --col;
5662 }
5663#endif
5664 ScreenLines[off] = c;
5665#ifdef FEAT_MBYTE
5666 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005667 {
5668 if ((mb_c & 0xff00) == 0x8e00)
5669 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 else if (enc_utf8)
5673 {
5674 if (mb_utf8)
5675 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005676 int i;
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005679 if ((c & 0xff) == 0)
5680 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005681 for (i = 0; i < Screen_mco; ++i)
5682 {
5683 ScreenLinesC[i][off] = u8cc[i];
5684 if (u8cc[i] == 0)
5685 break;
5686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else
5689 ScreenLinesUC[off] = 0;
5690 }
5691 if (multi_attr)
5692 {
5693 ScreenAttrs[off] = multi_attr;
5694 multi_attr = 0;
5695 }
5696 else
5697#endif
5698 ScreenAttrs[off] = char_attr;
5699
5700#ifdef FEAT_MBYTE
5701 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5702 {
5703 /* Need to fill two screen columns. */
5704 ++off;
5705 ++col;
5706 if (enc_utf8)
5707 /* UTF-8: Put a 0 in the second screen char. */
5708 ScreenLines[off] = 0;
5709 else
5710 /* DBCS: Put second byte in the second screen char. */
5711 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005712 if (draw_state > WL_NR
5713#ifdef FEAT_DIFF
5714 && filler_todo <= 0
5715#endif
5716 )
5717 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 /* When "tocol" is halfway a character, set it to the end of
5719 * the character, otherwise highlighting won't stop. */
5720 if (tocol == vcol)
5721 ++tocol;
5722#ifdef FEAT_RIGHTLEFT
5723 if (wp->w_p_rl)
5724 {
5725 /* now it's time to backup one cell */
5726 --off;
5727 --col;
5728 }
5729#endif
5730 }
5731#endif
5732#ifdef FEAT_RIGHTLEFT
5733 if (wp->w_p_rl)
5734 {
5735 --off;
5736 --col;
5737 }
5738 else
5739#endif
5740 {
5741 ++off;
5742 ++col;
5743 }
5744 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005745#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005746 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005747 {
5748 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005749 ++vcol_off;
5750 if (n_extra > 0)
5751 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005752 if (wp->w_p_wrap)
5753 {
5754 /*
5755 * Special voodoo required if 'wrap' is on.
5756 *
5757 * Advance the column indicator to force the line
5758 * drawing to wrap early. This will make the line
5759 * take up the same screen space when parts are concealed,
5760 * so that cursor line computations aren't messed up.
5761 *
5762 * To avoid the fictitious advance of 'col' causing
5763 * trailing junk to be written out of the screen line
5764 * we are building, 'boguscols' keeps track of the number
5765 * of bad columns we have advanced.
5766 */
5767 if (n_extra > 0)
5768 {
5769 vcol += n_extra;
5770# ifdef FEAT_RIGHTLEFT
5771 if (wp->w_p_rl)
5772 {
5773 col -= n_extra;
5774 boguscols -= n_extra;
5775 }
5776 else
5777# endif
5778 {
5779 col += n_extra;
5780 boguscols += n_extra;
5781 }
5782 n_extra = 0;
5783 n_attr = 0;
5784 }
5785
5786
5787# ifdef FEAT_MBYTE
5788 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5789 {
5790 /* Need to fill two screen columns. */
5791# ifdef FEAT_RIGHTLEFT
5792 if (wp->w_p_rl)
5793 {
5794 --boguscols;
5795 --col;
5796 }
5797 else
5798# endif
5799 {
5800 ++boguscols;
5801 ++col;
5802 }
5803 }
5804# endif
5805
5806# ifdef FEAT_RIGHTLEFT
5807 if (wp->w_p_rl)
5808 {
5809 --boguscols;
5810 --col;
5811 }
5812 else
5813# endif
5814 {
5815 ++boguscols;
5816 ++col;
5817 }
5818 }
5819 else
5820 {
5821 if (n_extra > 0)
5822 {
5823 vcol += n_extra;
5824 n_extra = 0;
5825 n_attr = 0;
5826 }
5827 }
5828
5829 }
5830#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 else
5832 --n_skip;
5833
Bram Moolenaar64486672010-05-16 15:46:46 +02005834 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5835 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005836 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837#ifdef FEAT_DIFF
5838 && filler_todo <= 0
5839#endif
5840 )
5841 ++vcol;
5842
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005843#ifdef FEAT_SYN_HL
5844 if (vcol_save_attr >= 0)
5845 char_attr = vcol_save_attr;
5846#endif
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 /* restore attributes after "predeces" in 'listchars' */
5849 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5850 char_attr = saved_attr3;
5851
5852 /* restore attributes after last 'listchars' or 'number' char */
5853 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5854 char_attr = saved_attr2;
5855
5856 /*
5857 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005858 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 */
5860 if ((
5861#ifdef FEAT_RIGHTLEFT
5862 wp->w_p_rl ? (col < 0) :
5863#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005864 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 && (*ptr != NUL
5866#ifdef FEAT_DIFF
5867 || filler_todo > 0
5868#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005869 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5871 )
5872 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005873#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005874 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005875 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876 boguscols = 0;
5877#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005878 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005879 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 ++row;
5882 ++screen_row;
5883
5884 /* When not wrapping and finished diff lines, or when displayed
5885 * '$' and highlighting until last column, break here. */
5886 if ((!wp->w_p_wrap
5887#ifdef FEAT_DIFF
5888 && filler_todo <= 0
5889#endif
5890 ) || lcs_eol_one == -1)
5891 break;
5892
5893 /* When the window is too narrow draw all "@" lines. */
5894 if (draw_state != WL_LINE
5895#ifdef FEAT_DIFF
5896 && filler_todo <= 0
5897#endif
5898 )
5899 {
5900 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 row = endrow;
5903 }
5904
5905 /* When line got too long for screen break here. */
5906 if (row == endrow)
5907 {
5908 ++row;
5909 break;
5910 }
5911
5912 if (screen_cur_row == screen_row - 1
5913#ifdef FEAT_DIFF
5914 && filler_todo <= 0
5915#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005916 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
5918 /* Remember that the line wraps, used for modeless copy. */
5919 LineWraps[screen_row - 1] = TRUE;
5920
5921 /*
5922 * Special trick to make copy/paste of wrapped lines work with
5923 * xterm/screen: write an extra character beyond the end of
5924 * the line. This will work with all terminal types
5925 * (regardless of the xn,am settings).
5926 * Only do this on a fast tty.
5927 * Only do this if the cursor is on the current line
5928 * (something has been written in it).
5929 * Don't do this for the GUI.
5930 * Don't do this for double-width characters.
5931 * Don't do this for a window not at the right screen border.
5932 */
5933 if (p_tf
5934#ifdef FEAT_GUI
5935 && !gui.in_use
5936#endif
5937#ifdef FEAT_MBYTE
5938 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005939 && ((*mb_off2cells)(LineOffset[screen_row],
5940 LineOffset[screen_row] + screen_Columns)
5941 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005943 + (int)Columns - 2,
5944 LineOffset[screen_row] + screen_Columns)
5945 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#endif
5947 )
5948 {
5949 /* First make sure we are at the end of the screen line,
5950 * then output the same character again to let the
5951 * terminal know about the wrap. If the terminal doesn't
5952 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005953 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 screen_char(LineOffset[screen_row - 1]
5955 + (unsigned)Columns - 1,
5956 screen_row - 1, (int)(Columns - 1));
5957
5958#ifdef FEAT_MBYTE
5959 /* When there is a multi-byte character, just output a
5960 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005961 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5962 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 out_char(' ');
5964 else
5965#endif
5966 out_char(ScreenLines[LineOffset[screen_row - 1]
5967 + (Columns - 1)]);
5968 /* force a redraw of the first char on the next line */
5969 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5970 screen_start(); /* don't know where cursor is now */
5971 }
5972 }
5973
5974 col = 0;
5975 off = (unsigned)(current_ScreenLine - ScreenLines);
5976#ifdef FEAT_RIGHTLEFT
5977 if (wp->w_p_rl)
5978 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005979 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 off += col;
5981 }
5982#endif
5983
5984 /* reset the drawing state for the start of a wrapped line */
5985 draw_state = WL_START;
5986 saved_n_extra = n_extra;
5987 saved_p_extra = p_extra;
5988 saved_c_extra = c_extra;
5989 saved_char_attr = char_attr;
5990 n_extra = 0;
5991 lcs_prec_todo = lcs_prec;
5992#ifdef FEAT_LINEBREAK
5993# ifdef FEAT_DIFF
5994 if (filler_todo <= 0)
5995# endif
5996 need_showbreak = TRUE;
5997#endif
5998#ifdef FEAT_DIFF
5999 --filler_todo;
6000 /* When the filler lines are actually below the last line of the
6001 * file, don't draw the line itself, break here. */
6002 if (filler_todo == 0 && wp->w_botfill)
6003 break;
6004#endif
6005 }
6006
6007 } /* for every character in the line */
6008
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006009#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006010 /* After an empty line check first word for capital. */
6011 if (*skipwhite(line) == NUL)
6012 {
6013 capcol_lnum = lnum + 1;
6014 cap_col = 0;
6015 }
6016#endif
6017
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006018 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 return row;
6020}
6021
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006022#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006023static int comp_char_differs(int, int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006024
6025/*
6026 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006027 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006028 */
6029 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006030comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031{
6032 int i;
6033
6034 for (i = 0; i < Screen_mco; ++i)
6035 {
6036 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6037 return TRUE;
6038 if (ScreenLinesC[i][off_from] == 0)
6039 break;
6040 }
6041 return FALSE;
6042}
6043#endif
6044
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045/*
6046 * Check whether the given character needs redrawing:
6047 * - the (first byte of the) character is different
6048 * - the attributes are different
6049 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006050 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
6052 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006053char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 if (cols > 0
6056 && ((ScreenLines[off_from] != ScreenLines[off_to]
6057 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6058
6059#ifdef FEAT_MBYTE
6060 || (enc_dbcs != 0
6061 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6062 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6063 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6064 : (cols > 1 && ScreenLines[off_from + 1]
6065 != ScreenLines[off_to + 1])))
6066 || (enc_utf8
6067 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6068 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006069 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006070 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6071 && ScreenLines[off_from + 1]
6072 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073#endif
6074 ))
6075 return TRUE;
6076 return FALSE;
6077}
6078
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006079#if defined(FEAT_TERMINAL) || defined(PROTO)
6080/*
6081 * Return the index in ScreenLines[] for the current screen line.
6082 */
6083 int
6084screen_get_current_line_off()
6085{
6086 return (int)(current_ScreenLine - ScreenLines);
6087}
6088#endif
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090/*
6091 * Move one "cooked" screen line to the screen, but only the characters that
6092 * have actually changed. Handle insert/delete character.
6093 * "coloff" gives the first column on the screen for this line.
6094 * "endcol" gives the columns where valid characters are.
6095 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6096 * needs to be cleared, negative otherwise.
6097 * "rlflag" is TRUE in a rightleft window:
6098 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6099 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6100 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006101 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006102screen_line(
6103 int row,
6104 int coloff,
6105 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006106 int clear_width,
6107 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 unsigned off_from;
6110 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006111#ifdef FEAT_MBYTE
6112 unsigned max_off_from;
6113 unsigned max_off_to;
6114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 int force = FALSE; /* force update rest of the line */
6118 int redraw_this /* bool: does character need redraw? */
6119#ifdef FEAT_GUI
6120 = TRUE /* For GUI when while-loop empty */
6121#endif
6122 ;
6123 int redraw_next; /* redraw_this for next character */
6124#ifdef FEAT_MBYTE
6125 int clear_next = FALSE;
6126 int char_cells; /* 1: normal char */
6127 /* 2: occupies two display cells */
6128# define CHAR_CELLS char_cells
6129#else
6130# define CHAR_CELLS 1
6131#endif
6132
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006133 /* Check for illegal row and col, just in case. */
6134 if (row >= Rows)
6135 row = Rows - 1;
6136 if (endcol > Columns)
6137 endcol = Columns;
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139# ifdef FEAT_CLIPBOARD
6140 clip_may_clear_selection(row, row);
6141# endif
6142
6143 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6144 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006145#ifdef FEAT_MBYTE
6146 max_off_from = off_from + screen_Columns;
6147 max_off_to = LineOffset[row] + screen_Columns;
6148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150#ifdef FEAT_RIGHTLEFT
6151 if (rlflag)
6152 {
6153 /* Clear rest first, because it's left of the text. */
6154 if (clear_width > 0)
6155 {
6156 while (col <= endcol && ScreenLines[off_to] == ' '
6157 && ScreenAttrs[off_to] == 0
6158# ifdef FEAT_MBYTE
6159 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6160# endif
6161 )
6162 {
6163 ++off_to;
6164 ++col;
6165 }
6166 if (col <= endcol)
6167 screen_fill(row, row + 1, col + coloff,
6168 endcol + coloff + 1, ' ', ' ', 0);
6169 }
6170 col = endcol + 1;
6171 off_to = LineOffset[row] + col + coloff;
6172 off_from += col;
6173 endcol = (clear_width > 0 ? clear_width : -clear_width);
6174 }
6175#endif /* FEAT_RIGHTLEFT */
6176
6177 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6178
6179 while (col < endcol)
6180 {
6181#ifdef FEAT_MBYTE
6182 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006183 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 else
6185 char_cells = 1;
6186#endif
6187
6188 redraw_this = redraw_next;
6189 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6190 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6191
6192#ifdef FEAT_GUI
6193 /* If the next character was bold, then redraw the current character to
6194 * remove any pixels that might have spilt over into us. This only
6195 * happens in the GUI.
6196 */
6197 if (redraw_next && gui.in_use)
6198 {
6199 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006200 if (hl > HL_ALL)
6201 hl = syn_attr2attr(hl);
6202 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 redraw_this = TRUE;
6204 }
6205#endif
6206
6207 if (redraw_this)
6208 {
6209 /*
6210 * Special handling when 'xs' termcap flag set (hpterm):
6211 * Attributes for characters are stored at the position where the
6212 * cursor is when writing the highlighting code. The
6213 * start-highlighting code must be written with the cursor on the
6214 * first highlighted character. The stop-highlighting code must
6215 * be written with the cursor just after the last highlighted
6216 * character.
6217 * Overwriting a character doesn't remove it's highlighting. Need
6218 * to clear the rest of the line, and force redrawing it
6219 * completely.
6220 */
6221 if ( p_wiv
6222 && !force
6223#ifdef FEAT_GUI
6224 && !gui.in_use
6225#endif
6226 && ScreenAttrs[off_to] != 0
6227 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6228 {
6229 /*
6230 * Need to remove highlighting attributes here.
6231 */
6232 windgoto(row, col + coloff);
6233 out_str(T_CE); /* clear rest of this screen line */
6234 screen_start(); /* don't know where cursor is now */
6235 force = TRUE; /* force redraw of rest of the line */
6236 redraw_next = TRUE; /* or else next char would miss out */
6237
6238 /*
6239 * If the previous character was highlighted, need to stop
6240 * highlighting at this character.
6241 */
6242 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6243 {
6244 screen_attr = ScreenAttrs[off_to - 1];
6245 term_windgoto(row, col + coloff);
6246 screen_stop_highlight();
6247 }
6248 else
6249 screen_attr = 0; /* highlighting has stopped */
6250 }
6251#ifdef FEAT_MBYTE
6252 if (enc_dbcs != 0)
6253 {
6254 /* Check if overwriting a double-byte with a single-byte or
6255 * the other way around requires another character to be
6256 * redrawn. For UTF-8 this isn't needed, because comparing
6257 * ScreenLinesUC[] is sufficient. */
6258 if (char_cells == 1
6259 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006260 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
6262 /* Writing a single-cell character over a double-cell
6263 * character: need to redraw the next cell. */
6264 ScreenLines[off_to + 1] = 0;
6265 redraw_next = TRUE;
6266 }
6267 else if (char_cells == 2
6268 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006269 && (*mb_off2cells)(off_to, max_off_to) == 1
6270 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272 /* Writing the second half of a double-cell character over
6273 * a double-cell character: need to redraw the second
6274 * cell. */
6275 ScreenLines[off_to + 2] = 0;
6276 redraw_next = TRUE;
6277 }
6278
6279 if (enc_dbcs == DBCS_JPNU)
6280 ScreenLines2[off_to] = ScreenLines2[off_from];
6281 }
6282 /* When writing a single-width character over a double-width
6283 * character and at the end of the redrawn text, need to clear out
6284 * the right halve of the old character.
6285 * Also required when writing the right halve of a double-width
6286 * char over the left halve of an existing one. */
6287 if (has_mbyte && col + char_cells == endcol
6288 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006289 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006291 && (*mb_off2cells)(off_to, max_off_to) == 1
6292 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 clear_next = TRUE;
6294#endif
6295
6296 ScreenLines[off_to] = ScreenLines[off_from];
6297#ifdef FEAT_MBYTE
6298 if (enc_utf8)
6299 {
6300 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6301 if (ScreenLinesUC[off_from] != 0)
6302 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006303 int i;
6304
6305 for (i = 0; i < Screen_mco; ++i)
6306 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
6308 }
6309 if (char_cells == 2)
6310 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6311#endif
6312
6313#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006314 /* The bold trick makes a single column of pixels appear in the
6315 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 * character should be redrawn too. This happens for our own GUI
6317 * and for some xterms. */
6318 if (
6319# ifdef FEAT_GUI
6320 gui.in_use
6321# endif
6322# if defined(FEAT_GUI) && defined(UNIX)
6323 ||
6324# endif
6325# ifdef UNIX
6326 term_is_xterm
6327# endif
6328 )
6329 {
6330 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006331 if (hl > HL_ALL)
6332 hl = syn_attr2attr(hl);
6333 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 redraw_next = TRUE;
6335 }
6336#endif
6337 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6338#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006339 /* For simplicity set the attributes of second half of a
6340 * double-wide character equal to the first half. */
6341 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006343
6344 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 else
6347#endif
6348 screen_char(off_to, row, col + coloff);
6349 }
6350 else if ( p_wiv
6351#ifdef FEAT_GUI
6352 && !gui.in_use
6353#endif
6354 && col + coloff > 0)
6355 {
6356 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6357 {
6358 /*
6359 * Don't output stop-highlight when moving the cursor, it will
6360 * stop the highlighting when it should continue.
6361 */
6362 screen_attr = 0;
6363 }
6364 else if (screen_attr != 0)
6365 screen_stop_highlight();
6366 }
6367
6368 off_to += CHAR_CELLS;
6369 off_from += CHAR_CELLS;
6370 col += CHAR_CELLS;
6371 }
6372
6373#ifdef FEAT_MBYTE
6374 if (clear_next)
6375 {
6376 /* Clear the second half of a double-wide character of which the left
6377 * half was overwritten with a single-wide character. */
6378 ScreenLines[off_to] = ' ';
6379 if (enc_utf8)
6380 ScreenLinesUC[off_to] = 0;
6381 screen_char(off_to, row, col + coloff);
6382 }
6383#endif
6384
6385 if (clear_width > 0
6386#ifdef FEAT_RIGHTLEFT
6387 && !rlflag
6388#endif
6389 )
6390 {
6391#ifdef FEAT_GUI
6392 int startCol = col;
6393#endif
6394
6395 /* blank out the rest of the line */
6396 while (col < clear_width && ScreenLines[off_to] == ' '
6397 && ScreenAttrs[off_to] == 0
6398#ifdef FEAT_MBYTE
6399 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6400#endif
6401 )
6402 {
6403 ++off_to;
6404 ++col;
6405 }
6406 if (col < clear_width)
6407 {
6408#ifdef FEAT_GUI
6409 /*
6410 * In the GUI, clearing the rest of the line may leave pixels
6411 * behind if the first character cleared was bold. Some bold
6412 * fonts spill over the left. In this case we redraw the previous
6413 * character too. If we didn't skip any blanks above, then we
6414 * only redraw if the character wasn't already redrawn anyway.
6415 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006416 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 {
6418 hl = ScreenAttrs[off_to];
6419 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006420 {
6421 int prev_cells = 1;
6422# ifdef FEAT_MBYTE
6423 if (enc_utf8)
6424 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6425 * that its width is 2. */
6426 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6427 else if (enc_dbcs != 0)
6428 {
6429 /* find previous character by counting from first
6430 * column and get its width. */
6431 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006432 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006433
6434 while (off < off_to)
6435 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006436 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006437 off += prev_cells;
6438 }
6439 }
6440
6441 if (enc_dbcs != 0 && prev_cells > 1)
6442 screen_char_2(off_to - prev_cells, row,
6443 col + coloff - prev_cells);
6444 else
6445# endif
6446 screen_char(off_to - prev_cells, row,
6447 col + coloff - prev_cells);
6448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 }
6450#endif
6451 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6452 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 off_to += clear_width - col;
6454 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
6456 }
6457
6458 if (clear_width > 0)
6459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 /* For a window that's left of another, draw the separator char. */
6461 if (col + coloff < Columns)
6462 {
6463 int c;
6464
6465 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006466 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006467#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006468 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6469 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 || ScreenAttrs[off_to] != hl)
6472 {
6473 ScreenLines[off_to] = c;
6474 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006475#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (enc_utf8)
6477 {
6478 if (c >= 0x80)
6479 {
6480 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006481 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
6483 else
6484 ScreenLinesUC[off_to] = 0;
6485 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 screen_char(off_to, row, col + coloff);
6488 }
6489 }
6490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 LineWraps[row] = FALSE;
6492 }
6493}
6494
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006495#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006497 * Mirror text "str" for right-left displaying.
6498 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006500 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006501rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 char_u *p1, *p2;
6504 int t;
6505
6506 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6507 {
6508 t = *p1;
6509 *p1 = *p2;
6510 *p2 = t;
6511 }
6512}
6513#endif
6514
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515/*
6516 * mark all status lines for redraw; used after first :cd
6517 */
6518 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006519status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 win_T *wp;
6522
Bram Moolenaar29323592016-07-24 22:04:11 +02006523 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (wp->w_status_height)
6525 {
6526 wp->w_redr_status = TRUE;
6527 redraw_later(VALID);
6528 }
6529}
6530
6531/*
6532 * mark all status lines of the current buffer for redraw
6533 */
6534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006535status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 win_T *wp;
6538
Bram Moolenaar29323592016-07-24 22:04:11 +02006539 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6541 {
6542 wp->w_redr_status = TRUE;
6543 redraw_later(VALID);
6544 }
6545}
6546
6547/*
6548 * Redraw all status lines that need to be redrawn.
6549 */
6550 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006551redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
6553 win_T *wp;
6554
Bram Moolenaar29323592016-07-24 22:04:11 +02006555 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006557 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006558 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006559 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
Bram Moolenaar4033c552017-09-16 20:54:51 +02006562#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563/*
6564 * Redraw all status lines at the bottom of frame "frp".
6565 */
6566 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006567win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 if (frp->fr_layout == FR_LEAF)
6570 frp->fr_win->w_redr_status = TRUE;
6571 else if (frp->fr_layout == FR_ROW)
6572 {
6573 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
6574 win_redraw_last_status(frp);
6575 }
6576 else /* frp->fr_layout == FR_COL */
6577 {
6578 frp = frp->fr_child;
6579 while (frp->fr_next != NULL)
6580 frp = frp->fr_next;
6581 win_redraw_last_status(frp);
6582 }
6583}
6584#endif
6585
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586/*
6587 * Draw the verticap separator right of window "wp" starting with line "row".
6588 */
6589 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006590draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 int hl;
6593 int c;
6594
6595 if (wp->w_vsep_width)
6596 {
6597 /* draw the vertical separator right of this window */
6598 c = fillchar_vsep(&hl);
6599 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6600 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6601 c, ' ', hl);
6602 }
6603}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
6605#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006606static int status_match_len(expand_T *xp, char_u *s);
6607static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608
6609/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006610 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 */
6612 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006613status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 int len = 0;
6616
6617#ifdef FEAT_MENU
6618 int emenu = (xp->xp_context == EXPAND_MENUS
6619 || xp->xp_context == EXPAND_MENUNAMES);
6620
6621 /* Check for menu separators - replace with '|'. */
6622 if (emenu && menu_is_separator(s))
6623 return 1;
6624#endif
6625
6626 while (*s != NUL)
6627 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006628 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006629 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006630 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 }
6632
6633 return len;
6634}
6635
6636/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006637 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006638 * These are backslashes used for escaping. Do show backslashes in help tags.
6639 */
6640 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006641skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006642{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006643 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006644#ifdef FEAT_MENU
6645 || ((xp->xp_context == EXPAND_MENUS
6646 || xp->xp_context == EXPAND_MENUNAMES)
6647 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6648#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006649 )
6650 {
6651#ifndef BACKSLASH_IN_FILENAME
6652 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6653 return 2;
6654#endif
6655 return 1;
6656 }
6657 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006658}
6659
6660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * Show wildchar matches in the status line.
6662 * Show at least the "match" item.
6663 * We start at item 'first_match' in the list and show all matches that fit.
6664 *
6665 * If inversion is possible we use it. Else '=' characters are used.
6666 */
6667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006668win_redr_status_matches(
6669 expand_T *xp,
6670 int num_matches,
6671 char_u **matches, /* list of matches */
6672 int match,
6673 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
6675#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6676 int row;
6677 char_u *buf;
6678 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006679 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 int fillchar;
6681 int attr;
6682 int i;
6683 int highlight = TRUE;
6684 char_u *selstart = NULL;
6685 int selstart_col = 0;
6686 char_u *selend = NULL;
6687 static int first_match = 0;
6688 int add_left = FALSE;
6689 char_u *s;
6690#ifdef FEAT_MENU
6691 int emenu;
6692#endif
6693#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6694 int l;
6695#endif
6696
6697 if (matches == NULL) /* interrupted completion? */
6698 return;
6699
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006700#ifdef FEAT_MBYTE
6701 if (has_mbyte)
6702 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6703 else
6704#endif
6705 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 if (buf == NULL)
6707 return;
6708
6709 if (match == -1) /* don't show match but original text */
6710 {
6711 match = 0;
6712 highlight = FALSE;
6713 }
6714 /* count 1 for the ending ">" */
6715 clen = status_match_len(xp, L_MATCH(match)) + 3;
6716 if (match == 0)
6717 first_match = 0;
6718 else if (match < first_match)
6719 {
6720 /* jumping left, as far as we can go */
6721 first_match = match;
6722 add_left = TRUE;
6723 }
6724 else
6725 {
6726 /* check if match fits on the screen */
6727 for (i = first_match; i < match; ++i)
6728 clen += status_match_len(xp, L_MATCH(i)) + 2;
6729 if (first_match > 0)
6730 clen += 2;
6731 /* jumping right, put match at the left */
6732 if ((long)clen > Columns)
6733 {
6734 first_match = match;
6735 /* if showing the last match, we can add some on the left */
6736 clen = 2;
6737 for (i = match; i < num_matches; ++i)
6738 {
6739 clen += status_match_len(xp, L_MATCH(i)) + 2;
6740 if ((long)clen >= Columns)
6741 break;
6742 }
6743 if (i == num_matches)
6744 add_left = TRUE;
6745 }
6746 }
6747 if (add_left)
6748 while (first_match > 0)
6749 {
6750 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6751 if ((long)clen >= Columns)
6752 break;
6753 --first_match;
6754 }
6755
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006756 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
6758 if (first_match == 0)
6759 {
6760 *buf = NUL;
6761 len = 0;
6762 }
6763 else
6764 {
6765 STRCPY(buf, "< ");
6766 len = 2;
6767 }
6768 clen = len;
6769
6770 i = first_match;
6771 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6772 {
6773 if (i == match)
6774 {
6775 selstart = buf + len;
6776 selstart_col = clen;
6777 }
6778
6779 s = L_MATCH(i);
6780 /* Check for menu separators - replace with '|' */
6781#ifdef FEAT_MENU
6782 emenu = (xp->xp_context == EXPAND_MENUS
6783 || xp->xp_context == EXPAND_MENUNAMES);
6784 if (emenu && menu_is_separator(s))
6785 {
6786 STRCPY(buf + len, transchar('|'));
6787 l = (int)STRLEN(buf + len);
6788 len += l;
6789 clen += l;
6790 }
6791 else
6792#endif
6793 for ( ; *s != NUL; ++s)
6794 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006795 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 clen += ptr2cells(s);
6797#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006798 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
6800 STRNCPY(buf + len, s, l);
6801 s += l - 1;
6802 len += l;
6803 }
6804 else
6805#endif
6806 {
6807 STRCPY(buf + len, transchar_byte(*s));
6808 len += (int)STRLEN(buf + len);
6809 }
6810 }
6811 if (i == match)
6812 selend = buf + len;
6813
6814 *(buf + len++) = ' ';
6815 *(buf + len++) = ' ';
6816 clen += 2;
6817 if (++i == num_matches)
6818 break;
6819 }
6820
6821 if (i != num_matches)
6822 {
6823 *(buf + len++) = '>';
6824 ++clen;
6825 }
6826
6827 buf[len] = NUL;
6828
6829 row = cmdline_row - 1;
6830 if (row >= 0)
6831 {
6832 if (wild_menu_showing == 0)
6833 {
6834 if (msg_scrolled > 0)
6835 {
6836 /* Put the wildmenu just above the command line. If there is
6837 * no room, scroll the screen one line up. */
6838 if (cmdline_row == Rows - 1)
6839 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006840 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 ++msg_scrolled;
6842 }
6843 else
6844 {
6845 ++cmdline_row;
6846 ++row;
6847 }
6848 wild_menu_showing = WM_SCROLLED;
6849 }
6850 else
6851 {
6852 /* Create status line if needed by setting 'laststatus' to 2.
6853 * Set 'winminheight' to zero to avoid that the window is
6854 * resized. */
6855 if (lastwin->w_status_height == 0)
6856 {
6857 save_p_ls = p_ls;
6858 save_p_wmh = p_wmh;
6859 p_ls = 2;
6860 p_wmh = 0;
6861 last_status(FALSE);
6862 }
6863 wild_menu_showing = WM_SHOWN;
6864 }
6865 }
6866
6867 screen_puts(buf, row, 0, attr);
6868 if (selstart != NULL && highlight)
6869 {
6870 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006871 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 }
6873
6874 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6875 }
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 vim_free(buf);
6879}
6880#endif
6881
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882/*
6883 * Redraw the status line of window wp.
6884 *
6885 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006886 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6887 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006889 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006890win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 int row;
6893 char_u *p;
6894 int len;
6895 int fillchar;
6896 int attr;
6897 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006898 static int busy = FALSE;
6899
6900 /* It's possible to get here recursively when 'statusline' (indirectly)
6901 * invokes ":redrawstatus". Simply ignore the call then. */
6902 if (busy)
6903 return;
6904 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 wp->w_redr_status = FALSE;
6907 if (wp->w_status_height == 0)
6908 {
6909 /* no status line, can only be last window */
6910 redraw_cmdline = TRUE;
6911 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006912 else if (!redrawing()
6913#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006914 // don't update status line when popup menu is visible and may be
6915 // drawn over it, unless it will be redrawn later
6916 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006917#endif
6918 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
6920 /* Don't redraw right now, do it later. */
6921 wp->w_redr_status = TRUE;
6922 }
6923#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006924 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 {
6926 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006927 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 }
6929#endif
6930 else
6931 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006932 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006934 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 p = NameBuff;
6936 len = (int)STRLEN(p);
6937
Bram Moolenaard85f2712017-07-28 21:51:57 +02006938 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939#ifdef FEAT_QUICKFIX
6940 || wp->w_p_pvw
6941#endif
6942 || bufIsChanged(wp->w_buffer)
6943 || wp->w_buffer->b_p_ro)
6944 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006945 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006947 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 len += (int)STRLEN(p + len);
6949 }
6950#ifdef FEAT_QUICKFIX
6951 if (wp->w_p_pvw)
6952 {
6953 STRCPY(p + len, _("[Preview]"));
6954 len += (int)STRLEN(p + len);
6955 }
6956#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006957 if (bufIsChanged(wp->w_buffer)
6958#ifdef FEAT_TERMINAL
6959 && !bt_terminal(wp->w_buffer)
6960#endif
6961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
6963 STRCPY(p + len, "[+]");
6964 len += 3;
6965 }
6966 if (wp->w_buffer->b_p_ro)
6967 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006968 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006969 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971
Bram Moolenaar02631462017-09-22 15:20:32 +02006972 this_ru_col = ru_col - (Columns - wp->w_width);
6973 if (this_ru_col < (wp->w_width + 1) / 2)
6974 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (this_ru_col <= 1)
6976 {
6977 p = (char_u *)"<"; /* No room for file name! */
6978 len = 1;
6979 }
6980 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981#ifdef FEAT_MBYTE
6982 if (has_mbyte)
6983 {
6984 int clen = 0, i;
6985
6986 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006987 clen = mb_string2cells(p, -1);
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 /* Find first character that will fit.
6990 * Going from start to end is much faster for DBCS. */
6991 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006992 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 clen -= (*mb_ptr2cells)(p + i);
6994 len = clen;
6995 if (i > 0)
6996 {
6997 p = p + i - 1;
6998 *p = '<';
6999 ++len;
7000 }
7001
7002 }
7003 else
7004#endif
7005 if (len > this_ru_col - 1)
7006 {
7007 p += len - (this_ru_col - 1);
7008 *p = '<';
7009 len = this_ru_col - 1;
7010 }
7011
7012 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007013 screen_puts(p, row, wp->w_wincol, attr);
7014 screen_fill(row, row + 1, len + wp->w_wincol,
7015 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007017 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7019 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007020 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007023 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024#endif
7025 }
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /*
7028 * May need to draw the character below the vertical separator.
7029 */
7030 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7031 {
7032 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007033 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 else
7035 fillchar = fillchar_vsep(&attr);
7036 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7037 attr);
7038 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007039 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040}
7041
Bram Moolenaar238a5642006-02-21 22:12:05 +00007042#ifdef FEAT_STL_OPT
7043/*
7044 * Redraw the status line according to 'statusline' and take care of any
7045 * errors encountered.
7046 */
7047 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007048redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007049{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007050 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007051 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007052
7053 /* When called recursively return. This can happen when the statusline
7054 * contains an expression that triggers a redraw. */
7055 if (entered)
7056 return;
7057 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007058
Bram Moolenaara742e082016-04-05 21:10:38 +02007059 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007060 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007061 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007062 {
7063 /* When there is an error disable the statusline, otherwise the
7064 * display is messed up with errors and a redraw triggers the problem
7065 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007066 set_string_option_direct((char_u *)"statusline", -1,
7067 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007068 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007069 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007070 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007071 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007072}
7073#endif
7074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075/*
7076 * Return TRUE if the status line of window "wp" is connected to the status
7077 * line of the window right of it. If not, then it's a vertical separator.
7078 * Only call if (wp->w_vsep_width != 0).
7079 */
7080 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007081stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
7083 frame_T *fr;
7084
7085 fr = wp->w_frame;
7086 while (fr->fr_parent != NULL)
7087 {
7088 if (fr->fr_parent->fr_layout == FR_COL)
7089 {
7090 if (fr->fr_next != NULL)
7091 break;
7092 }
7093 else
7094 {
7095 if (fr->fr_next != NULL)
7096 return TRUE;
7097 }
7098 fr = fr->fr_parent;
7099 }
7100 return FALSE;
7101}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104/*
7105 * Get the value to show for the language mappings, active 'keymap'.
7106 */
7107 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007108get_keymap_str(
7109 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007110 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007111 char_u *buf, /* buffer for the result */
7112 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 char_u *p;
7115
7116 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7117 return FALSE;
7118
7119 {
7120#ifdef FEAT_EVAL
7121 buf_T *old_curbuf = curbuf;
7122 win_T *old_curwin = curwin;
7123 char_u *s;
7124
7125 curbuf = wp->w_buffer;
7126 curwin = wp;
7127 STRCPY(buf, "b:keymap_name"); /* must be writable */
7128 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007129 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 --emsg_skip;
7131 curbuf = old_curbuf;
7132 curwin = old_curwin;
7133 if (p == NULL || *p == NUL)
7134#endif
7135 {
7136#ifdef FEAT_KEYMAP
7137 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7138 p = wp->w_buffer->b_p_keymap;
7139 else
7140#endif
7141 p = (char_u *)"lang";
7142 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007143 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 buf[0] = NUL;
7145#ifdef FEAT_EVAL
7146 vim_free(s);
7147#endif
7148 }
7149 return buf[0] != NUL;
7150}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152#if defined(FEAT_STL_OPT) || defined(PROTO)
7153/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007154 * Redraw the status line or ruler of window "wp".
7155 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 */
7157 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007158win_redr_custom(
7159 win_T *wp,
7160 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007162 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 int attr;
7164 int curattr;
7165 int row;
7166 int col = 0;
7167 int maxwidth;
7168 int width;
7169 int n;
7170 int len;
7171 int fillchar;
7172 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007173 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007175 struct stl_hlrec hltab[STL_MAX_ITEM];
7176 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007177 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007178 win_T *ewp;
7179 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar1d633412013-12-11 15:52:01 +01007181 /* There is a tiny chance that this gets called recursively: When
7182 * redrawing a status line triggers redrawing the ruler or tabline.
7183 * Avoid trouble by not allowing recursion. */
7184 if (entered)
7185 return;
7186 entered = TRUE;
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007189 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007191 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007192 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007193 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007194 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007195 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007196 maxwidth = Columns;
7197# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007198 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007201 else
7202 {
7203 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007204 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007205 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007206
7207 if (draw_ruler)
7208 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007209 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007210 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007211 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007212 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007213 if (*++stl == '-')
7214 stl++;
7215 if (atoi((char *)stl))
7216 while (VIM_ISDIGIT(*stl))
7217 stl++;
7218 if (*stl++ != '(')
7219 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007220 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007221 col = ru_col - (Columns - wp->w_width);
7222 if (col < (wp->w_width + 1) / 2)
7223 col = (wp->w_width + 1) / 2;
7224 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007225 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007226 {
7227 row = Rows - 1;
7228 --maxwidth; /* writing in last column may cause scrolling */
7229 fillchar = ' ';
7230 attr = 0;
7231 }
7232
7233# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007234 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007235# endif
7236 }
7237 else
7238 {
7239 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007240 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007241 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007242 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007243# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007244 use_sandbox = was_set_insecurely((char_u *)"statusline",
7245 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007246# endif
7247 }
7248
Bram Moolenaar53f81742017-09-22 14:35:51 +02007249 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007250 }
7251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007253 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
Bram Moolenaar61452852011-02-01 18:01:11 +01007255 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7256 * the cursor away and back. */
7257 ewp = wp == NULL ? curwin : wp;
7258 p_crb_save = ewp->w_p_crb;
7259 ewp->w_p_crb = FALSE;
7260
Bram Moolenaar362f3562009-11-03 16:20:34 +00007261 /* Make a copy, because the statusline may include a function call that
7262 * might change the option value and free the memory. */
7263 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007264 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007265 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007267 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007268 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007270 /* Make all characters printable. */
7271 p = transstr(buf);
7272 if (p != NULL)
7273 {
7274 vim_strncpy(buf, p, sizeof(buf) - 1);
7275 vim_free(p);
7276 }
7277
7278 /* fill up with "fillchar" */
7279 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007280 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
7282#ifdef FEAT_MBYTE
7283 len += (*mb_char2bytes)(fillchar, buf + len);
7284#else
7285 buf[len++] = fillchar;
7286#endif
7287 ++width;
7288 }
7289 buf[len] = NUL;
7290
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007291 /*
7292 * Draw each snippet with the specified highlighting.
7293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 curattr = attr;
7295 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007296 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007298 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 screen_puts_len(p, len, row, col, curattr);
7300 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007301 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007303 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007305 else if (hltab[n].userhl < 0)
7306 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007307#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007308 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7309 && wp->w_status_height != 0)
7310 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007311 else if (wp != NULL && bt_terminal(wp->w_buffer)
7312 && wp->w_status_height != 0)
7313 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007314#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007315 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007316 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007318 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007321
7322 if (wp == NULL)
7323 {
7324 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7325 col = 0;
7326 len = 0;
7327 p = buf;
7328 fillchar = 0;
7329 for (n = 0; tabtab[n].start != NULL; n++)
7330 {
7331 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7332 while (col < len)
7333 TabPageIdxs[col++] = fillchar;
7334 p = tabtab[n].start;
7335 fillchar = tabtab[n].userhl;
7336 }
7337 while (col < Columns)
7338 TabPageIdxs[col++] = fillchar;
7339 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007340
7341theend:
7342 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343}
7344
7345#endif /* FEAT_STL_OPT */
7346
7347/*
7348 * Output a single character directly to the screen and update ScreenLines.
7349 */
7350 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007351screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 char_u buf[MB_MAXBYTES + 1];
7354
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007355#ifdef FEAT_MBYTE
7356 if (has_mbyte)
7357 buf[(*mb_char2bytes)(c, buf)] = NUL;
7358 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007360 {
7361 buf[0] = c;
7362 buf[1] = NUL;
7363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 screen_puts(buf, row, col, attr);
7365}
7366
7367/*
7368 * Get a single character directly from ScreenLines into "bytes[]".
7369 * Also return its attribute in *attrp;
7370 */
7371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007372screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 unsigned off;
7375
7376 /* safety check */
7377 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7378 {
7379 off = LineOffset[row] + col;
7380 *attrp = ScreenAttrs[off];
7381 bytes[0] = ScreenLines[off];
7382 bytes[1] = NUL;
7383
7384#ifdef FEAT_MBYTE
7385 if (enc_utf8 && ScreenLinesUC[off] != 0)
7386 bytes[utfc_char2bytes(off, bytes)] = NUL;
7387 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7388 {
7389 bytes[0] = ScreenLines[off];
7390 bytes[1] = ScreenLines2[off];
7391 bytes[2] = NUL;
7392 }
7393 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7394 {
7395 bytes[1] = ScreenLines[off + 1];
7396 bytes[2] = NUL;
7397 }
7398#endif
7399 }
7400}
7401
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007402#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007403static int screen_comp_differs(int, int*);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007404
7405/*
7406 * Return TRUE if composing characters for screen posn "off" differs from
7407 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007408 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007409 */
7410 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007411screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007412{
7413 int i;
7414
7415 for (i = 0; i < Screen_mco; ++i)
7416 {
7417 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7418 return TRUE;
7419 if (u8cc[i] == 0)
7420 break;
7421 }
7422 return FALSE;
7423}
7424#endif
7425
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426/*
7427 * Put string '*text' on the screen at position 'row' and 'col', with
7428 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7429 * Note: only outputs within one row, message is truncated at screen boundary!
7430 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7431 */
7432 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007433screen_puts(
7434 char_u *text,
7435 int row,
7436 int col,
7437 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 screen_puts_len(text, -1, row, col, attr);
7440}
7441
7442/*
7443 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7444 * a NUL.
7445 */
7446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007447screen_puts_len(
7448 char_u *text,
7449 int textlen,
7450 int row,
7451 int col,
7452 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 unsigned off;
7455 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007456 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 int c;
7458#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007459 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int mbyte_blen = 1;
7461 int mbyte_cells = 1;
7462 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007463 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 int clear_next_cell = FALSE;
7465# ifdef FEAT_ARABIC
7466 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007467 int pc, nc, nc1;
7468 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469# endif
7470#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007471#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7472 int force_redraw_this;
7473 int force_redraw_next = FALSE;
7474#endif
7475 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
7477 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7478 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007479 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
Bram Moolenaarc236c162008-07-13 17:41:49 +00007481#ifdef FEAT_MBYTE
7482 /* When drawing over the right halve of a double-wide char clear out the
7483 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007484 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007485# ifdef FEAT_GUI
7486 && !gui.in_use
7487# endif
7488 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007489 {
7490 ScreenLines[off - 1] = ' ';
7491 ScreenAttrs[off - 1] = 0;
7492 if (enc_utf8)
7493 {
7494 ScreenLinesUC[off - 1] = 0;
7495 ScreenLinesC[0][off - 1] = 0;
7496 }
7497 /* redraw the previous cell, make it empty */
7498 screen_char(off - 1, row, col - 1);
7499 /* force the cell at "col" to be redrawn */
7500 force_redraw_next = TRUE;
7501 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007502#endif
7503
Bram Moolenaar367329b2007-08-30 11:53:22 +00007504#ifdef FEAT_MBYTE
7505 max_off = LineOffset[row] + screen_Columns;
7506#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007507 while (col < screen_Columns
7508 && (len < 0 || (int)(ptr - text) < len)
7509 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 {
7511 c = *ptr;
7512#ifdef FEAT_MBYTE
7513 /* check if this is the first byte of a multibyte */
7514 if (has_mbyte)
7515 {
7516 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007517 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007519 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7521 mbyte_cells = 1;
7522 else if (enc_dbcs != 0)
7523 mbyte_cells = mbyte_blen;
7524 else /* enc_utf8 */
7525 {
7526 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007527 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 (int)((text + len) - ptr));
7529 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007530 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007532# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 /* Non-BMP character: display as ? or fullwidth ?. */
7534 if (u8c >= 0x10000)
7535 {
7536 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7537 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007538 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541# ifdef FEAT_ARABIC
7542 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7543 {
7544 /* Do Arabic shaping. */
7545 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7546 {
7547 /* Past end of string to be displayed. */
7548 nc = NUL;
7549 nc1 = NUL;
7550 }
7551 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007552 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007553 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7554 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555 nc1 = pcc[0];
7556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 pc = prev_c;
7558 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007559 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 }
7561 else
7562 prev_c = u8c;
7563# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007564 if (col + mbyte_cells > screen_Columns)
7565 {
7566 /* Only 1 cell left, but character requires 2 cells:
7567 * display a '>' in the last column to avoid wrapping. */
7568 c = '>';
7569 mbyte_cells = 1;
7570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 }
7572 }
7573#endif
7574
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007575#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7576 force_redraw_this = force_redraw_next;
7577 force_redraw_next = FALSE;
7578#endif
7579
7580 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581#ifdef FEAT_MBYTE
7582 || (mbyte_cells == 2
7583 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7584 || (enc_dbcs == DBCS_JPNU
7585 && c == 0x8e
7586 && ScreenLines2[off] != ptr[1])
7587 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007588 && (ScreenLinesUC[off] !=
7589 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7590 || (ScreenLinesUC[off] != 0
7591 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592#endif
7593 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007594 || exmode_active;
7595
7596 if (need_redraw
7597#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7598 || force_redraw_this
7599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 )
7601 {
7602#if defined(FEAT_GUI) || defined(UNIX)
7603 /* The bold trick makes a single row of pixels appear in the next
7604 * character. When a bold character is removed, the next
7605 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007606 * and for some xterms. */
7607 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608# ifdef FEAT_GUI
7609 gui.in_use
7610# endif
7611# if defined(FEAT_GUI) && defined(UNIX)
7612 ||
7613# endif
7614# ifdef UNIX
7615 term_is_xterm
7616# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007617 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007619 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007621 if (n > HL_ALL)
7622 n = syn_attr2attr(n);
7623 if (n & HL_BOLD)
7624 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626#endif
7627#ifdef FEAT_MBYTE
7628 /* When at the end of the text and overwriting a two-cell
7629 * character with a one-cell character, need to clear the next
7630 * cell. Also when overwriting the left halve of a two-cell char
7631 * with the right halve of a two-cell char. Do this only once
7632 * (mb_off2cells() may return 2 on the right halve). */
7633 if (clear_next_cell)
7634 clear_next_cell = FALSE;
7635 else if (has_mbyte
7636 && (len < 0 ? ptr[mbyte_blen] == NUL
7637 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007638 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007640 && (*mb_off2cells)(off, max_off) == 1
7641 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 clear_next_cell = TRUE;
7643
7644 /* Make sure we never leave a second byte of a double-byte behind,
7645 * it confuses mb_off2cells(). */
7646 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007647 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007649 && (*mb_off2cells)(off, max_off) == 1
7650 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 ScreenLines[off + mbyte_blen] = 0;
7652#endif
7653 ScreenLines[off] = c;
7654 ScreenAttrs[off] = attr;
7655#ifdef FEAT_MBYTE
7656 if (enc_utf8)
7657 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007658 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 ScreenLinesUC[off] = 0;
7660 else
7661 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007662 int i;
7663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007665 for (i = 0; i < Screen_mco; ++i)
7666 {
7667 ScreenLinesC[i][off] = u8cc[i];
7668 if (u8cc[i] == 0)
7669 break;
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672 if (mbyte_cells == 2)
7673 {
7674 ScreenLines[off + 1] = 0;
7675 ScreenAttrs[off + 1] = attr;
7676 }
7677 screen_char(off, row, col);
7678 }
7679 else if (mbyte_cells == 2)
7680 {
7681 ScreenLines[off + 1] = ptr[1];
7682 ScreenAttrs[off + 1] = attr;
7683 screen_char_2(off, row, col);
7684 }
7685 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7686 {
7687 ScreenLines2[off] = ptr[1];
7688 screen_char(off, row, col);
7689 }
7690 else
7691#endif
7692 screen_char(off, row, col);
7693 }
7694#ifdef FEAT_MBYTE
7695 if (has_mbyte)
7696 {
7697 off += mbyte_cells;
7698 col += mbyte_cells;
7699 ptr += mbyte_blen;
7700 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007701 {
7702 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007704 len = -1;
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707 else
7708#endif
7709 {
7710 ++off;
7711 ++col;
7712 ++ptr;
7713 }
7714 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007715
7716#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7717 /* If we detected the next character needs to be redrawn, but the text
7718 * doesn't extend up to there, update the character here. */
7719 if (force_redraw_next && col < screen_Columns)
7720 {
7721# ifdef FEAT_MBYTE
7722 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7723 screen_char_2(off, row, col);
7724 else
7725# endif
7726 screen_char(off, row, col);
7727 }
7728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729}
7730
7731#ifdef FEAT_SEARCH_EXTRA
7732/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007733 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 */
7735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007736start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737{
7738 if (p_hls && !no_hlsearch)
7739 {
7740 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007741 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007742# ifdef FEAT_RELTIME
7743 /* Set the time limit to 'redrawtime'. */
7744 profile_setlimit(p_rdt, &search_hl.tm);
7745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747}
7748
7749/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007750 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 */
7752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007753end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
7755 if (search_hl.rm.regprog != NULL)
7756 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007757 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 search_hl.rm.regprog = NULL;
7759 }
7760}
7761
7762/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007763 * Init for calling prepare_search_hl().
7764 */
7765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007766init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007767{
7768 matchitem_T *cur;
7769
7770 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7771 * match */
7772 cur = wp->w_match_head;
7773 while (cur != NULL)
7774 {
7775 cur->hl.rm = cur->match;
7776 if (cur->hlg_id == 0)
7777 cur->hl.attr = 0;
7778 else
7779 cur->hl.attr = syn_id2attr(cur->hlg_id);
7780 cur->hl.buf = wp->w_buffer;
7781 cur->hl.lnum = 0;
7782 cur->hl.first_lnum = 0;
7783# ifdef FEAT_RELTIME
7784 /* Set the time limit to 'redrawtime'. */
7785 profile_setlimit(p_rdt, &(cur->hl.tm));
7786# endif
7787 cur = cur->next;
7788 }
7789 search_hl.buf = wp->w_buffer;
7790 search_hl.lnum = 0;
7791 search_hl.first_lnum = 0;
7792 /* time limit is set at the toplevel, for all windows */
7793}
7794
7795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 * Advance to the match in window "wp" line "lnum" or past it.
7797 */
7798 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007799prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007801 matchitem_T *cur; /* points to the match list */
7802 match_T *shl; /* points to search_hl or a match */
7803 int shl_flag; /* flag to indicate whether search_hl
7804 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007805 int pos_inprogress; /* marks that position match search is
7806 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 int n;
7808
7809 /*
7810 * When using a multi-line pattern, start searching at the top
7811 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007812 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007814 cur = wp->w_match_head;
7815 shl_flag = FALSE;
7816 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007818 if (shl_flag == FALSE)
7819 {
7820 shl = &search_hl;
7821 shl_flag = TRUE;
7822 }
7823 else
7824 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 if (shl->rm.regprog != NULL
7826 && shl->lnum == 0
7827 && re_multiline(shl->rm.regprog))
7828 {
7829 if (shl->first_lnum == 0)
7830 {
7831# ifdef FEAT_FOLDING
7832 for (shl->first_lnum = lnum;
7833 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7834 if (hasFoldingWin(wp, shl->first_lnum - 1,
7835 NULL, NULL, TRUE, NULL))
7836 break;
7837# else
7838 shl->first_lnum = wp->w_topline;
7839# endif
7840 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007841 if (cur != NULL)
7842 cur->pos.cur = 0;
7843 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007845 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7846 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007848 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7849 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007850 pos_inprogress = cur == NULL || cur->pos.cur == 0
7851 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (shl->lnum != 0)
7853 {
7854 shl->first_lnum = shl->lnum
7855 + shl->rm.endpos[0].lnum
7856 - shl->rm.startpos[0].lnum;
7857 n = shl->rm.endpos[0].col;
7858 }
7859 else
7860 {
7861 ++shl->first_lnum;
7862 n = 0;
7863 }
7864 }
7865 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007866 if (shl != &search_hl && cur != NULL)
7867 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 }
7869}
7870
7871/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007872 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 * Uses shl->buf.
7874 * Sets shl->lnum and shl->rm contents.
7875 * Note: Assumes a previous match is always before "lnum", unless
7876 * shl->lnum is zero.
7877 * Careful: Any pointers for buffer lines will become invalid.
7878 */
7879 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007880next_search_hl(
7881 win_T *win,
7882 match_T *shl, /* points to search_hl or a match */
7883 linenr_T lnum,
7884 colnr_T mincol, /* minimal column for a match */
7885 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 linenr_T l;
7888 colnr_T matchcol;
7889 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007890 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007892 // for :{range}s/pat only highlight inside the range
7893 if (lnum < search_first_line || lnum > search_last_line)
7894 {
7895 shl->lnum = 0;
7896 return;
7897 }
7898
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (shl->lnum != 0)
7900 {
7901 /* Check for three situations:
7902 * 1. If the "lnum" is below a previous match, start a new search.
7903 * 2. If the previous match includes "mincol", use it.
7904 * 3. Continue after the previous match.
7905 */
7906 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7907 if (lnum > l)
7908 shl->lnum = 0;
7909 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7910 return;
7911 }
7912
7913 /*
7914 * Repeat searching for a match until one is found that includes "mincol"
7915 * or none is found in this line.
7916 */
7917 called_emsg = FALSE;
7918 for (;;)
7919 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007920#ifdef FEAT_RELTIME
7921 /* Stop searching after passing the time limit. */
7922 if (profile_passed_limit(&(shl->tm)))
7923 {
7924 shl->lnum = 0; /* no match found in time */
7925 break;
7926 }
7927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 /* Three situations:
7929 * 1. No useful previous match: search from start of line.
7930 * 2. Not Vi compatible or empty match: continue at next character.
7931 * Break the loop if this is beyond the end of the line.
7932 * 3. Vi compatible searching: continue at end of previous match.
7933 */
7934 if (shl->lnum == 0)
7935 matchcol = 0;
7936 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7937 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007938 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007940 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007941
7942 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007943 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007944 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007946 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 shl->lnum = 0;
7948 break;
7949 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007950#ifdef FEAT_MBYTE
7951 if (has_mbyte)
7952 matchcol += mb_ptr2len(ml);
7953 else
7954#endif
7955 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 }
7957 else
7958 matchcol = shl->rm.endpos[0].col;
7959
7960 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007963 /* Remember whether shl->rm is using a copy of the regprog in
7964 * cur->match. */
7965 int regprog_is_copy = (shl != &search_hl && cur != NULL
7966 && shl == &cur->hl
7967 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007968 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007969
Bram Moolenaarb3414592014-06-17 17:48:32 +02007970 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7971 matchcol,
7972#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007973 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02007974#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007975 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02007976#endif
7977 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007978 /* Copy the regprog, in case it got freed and recompiled. */
7979 if (regprog_is_copy)
7980 cur->match.regprog = cur->hl.rm.regprog;
7981
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007982 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007983 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 /* Error while handling regexp: stop using this regexp. */
7985 if (shl == &search_hl)
7986 {
7987 /* don't free regprog in the match list, it's a copy */
7988 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02007989 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 }
7991 shl->rm.regprog = NULL;
7992 shl->lnum = 0;
7993 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
7994 message */
7995 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00007996 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 }
7998 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02007999 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008000 else
8001 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 if (nmatched == 0)
8003 {
8004 shl->lnum = 0; /* no match found */
8005 break;
8006 }
8007 if (shl->rm.startpos[0].lnum > 0
8008 || shl->rm.startpos[0].col >= mincol
8009 || nmatched > 1
8010 || shl->rm.endpos[0].col > mincol)
8011 {
8012 shl->lnum += shl->rm.startpos[0].lnum;
8013 break; /* useful match found */
8014 }
8015 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008016
8017 // Restore called_emsg for assert_fails().
8018 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
Bram Moolenaar85077472016-10-16 14:35:48 +02008021/*
8022 * If there is a match fill "shl" and return one.
8023 * Return zero otherwise.
8024 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008026next_search_hl_pos(
8027 match_T *shl, /* points to a match */
8028 linenr_T lnum,
8029 posmatch_T *posmatch, /* match positions */
8030 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008031{
8032 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008033 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008034
Bram Moolenaarb3414592014-06-17 17:48:32 +02008035 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8036 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008037 llpos_T *pos = &posmatch->pos[i];
8038
8039 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008040 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008041 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008042 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008043 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008044 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008045 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008046 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008047 /* if this match comes before the one at "found" then swap
8048 * them */
8049 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008050 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008051 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008052
Bram Moolenaar85077472016-10-16 14:35:48 +02008053 *pos = posmatch->pos[found];
8054 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008055 }
8056 }
8057 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008058 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008059 }
8060 }
8061 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008062 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008063 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008064 colnr_T start = posmatch->pos[found].col == 0
8065 ? 0 : posmatch->pos[found].col - 1;
8066 colnr_T end = posmatch->pos[found].col == 0
8067 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008068
Bram Moolenaar85077472016-10-16 14:35:48 +02008069 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008070 shl->rm.startpos[0].lnum = 0;
8071 shl->rm.startpos[0].col = start;
8072 shl->rm.endpos[0].lnum = 0;
8073 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008074 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 posmatch->cur = found + 1;
8076 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008078 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008079}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008080#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008083screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 attrentry_T *aep = NULL;
8086
8087 screen_attr = attr;
8088 if (full_screen
8089#ifdef WIN3264
8090 && termcap_active
8091#endif
8092 )
8093 {
8094#ifdef FEAT_GUI
8095 if (gui.in_use)
8096 {
8097 char buf[20];
8098
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008099 /* The GUI handles this internally. */
8100 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 OUT_STR(buf);
8102 }
8103 else
8104#endif
8105 {
8106 if (attr > HL_ALL) /* special HL attr. */
8107 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008108 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 aep = syn_cterm_attr2entry(attr);
8110 else
8111 aep = syn_term_attr2entry(attr);
8112 if (aep == NULL) /* did ":syntax clear" */
8113 attr = 0;
8114 else
8115 attr = aep->ae_attr;
8116 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008117 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008119 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008120#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008121 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8122 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8123 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008124#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008125 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008126 /* If the Normal FG color has BOLD attribute and the new HL
8127 * has a FG color defined, clear BOLD. */
8128 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008129 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008131 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008132 out_str(T_UCS);
8133 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008134 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8135 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008137 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008139 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008141 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008142 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
8144 /*
8145 * Output the color or start string after bold etc., in case the
8146 * bold etc. override the color setting.
8147 */
8148 if (aep != NULL)
8149 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008150#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008151 /* When 'termguicolors' is set but fg or bg is unset,
8152 * fall back to the cterm colors. This helps for SpellBad,
8153 * where the GUI uses a red undercurl. */
8154 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008156 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008157 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008158 }
8159 else
8160#endif
8161 if (t_colors > 1)
8162 {
8163 if (aep->ae_u.cterm.fg_color)
8164 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8165 }
8166#ifdef FEAT_TERMGUICOLORS
8167 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8168 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008169 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008170 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 }
8172 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008173#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008174 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008176 if (aep->ae_u.cterm.bg_color)
8177 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8178 }
8179
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008180 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008181 {
8182 if (aep->ae_u.term.start != NULL)
8183 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185 }
8186 }
8187 }
8188}
8189
8190 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008191screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192{
8193 int do_ME = FALSE; /* output T_ME code */
8194
8195 if (screen_attr != 0
8196#ifdef WIN3264
8197 && termcap_active
8198#endif
8199 )
8200 {
8201#ifdef FEAT_GUI
8202 if (gui.in_use)
8203 {
8204 char buf[20];
8205
8206 /* use internal GUI code */
8207 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8208 OUT_STR(buf);
8209 }
8210 else
8211#endif
8212 {
8213 if (screen_attr > HL_ALL) /* special HL attr. */
8214 {
8215 attrentry_T *aep;
8216
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008217 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 /*
8220 * Assume that t_me restores the original colors!
8221 */
8222 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008223 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008224#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008225 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8226 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8227 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008228#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008229 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008230#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008231 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8232 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8233 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008234#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 do_ME = TRUE;
8237 }
8238 else
8239 {
8240 aep = syn_term_attr2entry(screen_attr);
8241 if (aep != NULL && aep->ae_u.term.stop != NULL)
8242 {
8243 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8244 do_ME = TRUE;
8245 else
8246 out_str(aep->ae_u.term.stop);
8247 }
8248 }
8249 if (aep == NULL) /* did ":syntax clear" */
8250 screen_attr = 0;
8251 else
8252 screen_attr = aep->ae_attr;
8253 }
8254
8255 /*
8256 * Often all ending-codes are equal to T_ME. Avoid outputting the
8257 * same sequence several times.
8258 */
8259 if (screen_attr & HL_STANDOUT)
8260 {
8261 if (STRCMP(T_SE, T_ME) == 0)
8262 do_ME = TRUE;
8263 else
8264 out_str(T_SE);
8265 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008266 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008267 {
8268 if (STRCMP(T_UCE, T_ME) == 0)
8269 do_ME = TRUE;
8270 else
8271 out_str(T_UCE);
8272 }
8273 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008274 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
8276 if (STRCMP(T_UE, T_ME) == 0)
8277 do_ME = TRUE;
8278 else
8279 out_str(T_UE);
8280 }
8281 if (screen_attr & HL_ITALIC)
8282 {
8283 if (STRCMP(T_CZR, T_ME) == 0)
8284 do_ME = TRUE;
8285 else
8286 out_str(T_CZR);
8287 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008288 if (screen_attr & HL_STRIKETHROUGH)
8289 {
8290 if (STRCMP(T_STE, T_ME) == 0)
8291 do_ME = TRUE;
8292 else
8293 out_str(T_STE);
8294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8296 out_str(T_ME);
8297
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008298#ifdef FEAT_TERMGUICOLORS
8299 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008301 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008302 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008303 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008304 term_bg_rgb_color(cterm_normal_bg_gui_color);
8305 }
8306 else
8307#endif
8308 {
8309 if (t_colors > 1)
8310 {
8311 /* set Normal cterm colors */
8312 if (cterm_normal_fg_color != 0)
8313 term_fg_color(cterm_normal_fg_color - 1);
8314 if (cterm_normal_bg_color != 0)
8315 term_bg_color(cterm_normal_bg_color - 1);
8316 if (cterm_normal_fg_bold)
8317 out_str(T_MD);
8318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
8320 }
8321 }
8322 screen_attr = 0;
8323}
8324
8325/*
8326 * Reset the colors for a cterm. Used when leaving Vim.
8327 * The machine specific code may override this again.
8328 */
8329 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008330reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008332 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008335#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008336 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8337 || cterm_normal_bg_gui_color != INVALCOLOR)
8338 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008339#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
8343 out_str(T_OP);
8344 screen_attr = -1;
8345 }
8346 if (cterm_normal_fg_bold)
8347 {
8348 out_str(T_ME);
8349 screen_attr = -1;
8350 }
8351 }
8352}
8353
8354/*
8355 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8356 * using the attributes from ScreenAttrs["off"].
8357 */
8358 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008359screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
8361 int attr;
8362
8363 /* Check for illegal values, just in case (could happen just after
8364 * resizing). */
8365 if (row >= screen_Rows || col >= screen_Columns)
8366 return;
8367
Bram Moolenaar494838a2015-02-10 19:20:37 +01008368 /* Outputting a character in the last cell on the screen may scroll the
8369 * screen up. Only do it when the "xn" termcap property is set, otherwise
8370 * mark the character invalid (update it when scrolled up). */
8371 if (*T_XN == NUL
8372 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#ifdef FEAT_RIGHTLEFT
8374 /* account for first command-line character in rightleft mode */
8375 && !cmdmsg_rl
8376#endif
8377 )
8378 {
8379 ScreenAttrs[off] = (sattr_T)-1;
8380 return;
8381 }
8382
8383 /*
8384 * Stop highlighting first, so it's easier to move the cursor.
8385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 if (screen_char_attr != 0)
8387 attr = screen_char_attr;
8388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 attr = ScreenAttrs[off];
8390 if (screen_attr != attr)
8391 screen_stop_highlight();
8392
8393 windgoto(row, col);
8394
8395 if (screen_attr != attr)
8396 screen_start_highlight(attr);
8397
8398#ifdef FEAT_MBYTE
8399 if (enc_utf8 && ScreenLinesUC[off] != 0)
8400 {
8401 char_u buf[MB_MAXBYTES + 1];
8402
Bram Moolenaarcb070082016-04-02 22:14:51 +02008403 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008404 {
8405 if (*p_ambw == 'd'
8406# ifdef FEAT_GUI
8407 && !gui.in_use
8408# endif
8409 )
8410 {
8411 /* Clear the two screen cells. If the character is actually
8412 * single width it won't change the second cell. */
8413 out_str((char_u *)" ");
8414 term_windgoto(row, col);
8415 }
8416 /* not sure where the cursor is after drawing the ambiguous width
8417 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008418 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008419 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008420 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008422
8423 /* Convert the UTF-8 character to bytes and write it. */
8424 buf[utfc_char2bytes(off, buf)] = NUL;
8425 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427 else
8428#endif
8429 {
8430#ifdef FEAT_MBYTE
8431 out_flush_check();
8432#endif
8433 out_char(ScreenLines[off]);
8434#ifdef FEAT_MBYTE
8435 /* double-byte character in single-width cell */
8436 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8437 out_char(ScreenLines2[off]);
8438#endif
8439 }
8440
8441 screen_cur_col++;
8442}
8443
8444#ifdef FEAT_MBYTE
8445
8446/*
8447 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8448 * on the screen at position 'row' and 'col'.
8449 * The attributes of the first byte is used for all. This is required to
8450 * output the two bytes of a double-byte character with nothing in between.
8451 */
8452 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008453screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 /* Check for illegal values (could be wrong when screen was resized). */
8456 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8457 return;
8458
8459 /* Outputting the last character on the screen may scrollup the screen.
8460 * Don't to it! Mark the character invalid (update it when scrolled up) */
8461 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8462 {
8463 ScreenAttrs[off] = (sattr_T)-1;
8464 return;
8465 }
8466
8467 /* Output the first byte normally (positions the cursor), then write the
8468 * second byte directly. */
8469 screen_char(off, row, col);
8470 out_char(ScreenLines[off + 1]);
8471 ++screen_cur_col;
8472}
8473#endif
8474
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475/*
8476 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8477 * This uses the contents of ScreenLines[] and doesn't change it.
8478 */
8479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008480screen_draw_rectangle(
8481 int row,
8482 int col,
8483 int height,
8484 int width,
8485 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 int r, c;
8488 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008489#ifdef FEAT_MBYTE
8490 int max_off;
8491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008493 /* Can't use ScreenLines unless initialized */
8494 if (ScreenLines == NULL)
8495 return;
8496
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (invert)
8498 screen_char_attr = HL_INVERSE;
8499 for (r = row; r < row + height; ++r)
8500 {
8501 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008502#ifdef FEAT_MBYTE
8503 max_off = off + screen_Columns;
8504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 for (c = col; c < col + width; ++c)
8506 {
8507#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008508 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {
8510 screen_char_2(off + c, r, c);
8511 ++c;
8512 }
8513 else
8514#endif
8515 {
8516 screen_char(off + c, r, c);
8517#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008518 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 ++c;
8520#endif
8521 }
8522 }
8523 }
8524 screen_char_attr = 0;
8525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527/*
8528 * Redraw the characters for a vertically split window.
8529 */
8530 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008531redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 int col;
8534 int width;
8535
8536# ifdef FEAT_CLIPBOARD
8537 clip_may_clear_selection(row, end - 1);
8538# endif
8539
8540 if (wp == NULL)
8541 {
8542 col = 0;
8543 width = Columns;
8544 }
8545 else
8546 {
8547 col = wp->w_wincol;
8548 width = wp->w_width;
8549 }
8550 screen_draw_rectangle(row, col, end - row, width, FALSE);
8551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008553 static void
8554space_to_screenline(int off, int attr)
8555{
8556 ScreenLines[off] = ' ';
8557 ScreenAttrs[off] = attr;
8558# ifdef FEAT_MBYTE
8559 if (enc_utf8)
8560 ScreenLinesUC[off] = 0;
8561# endif
8562}
8563
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564/*
8565 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8566 * with character 'c1' in first column followed by 'c2' in the other columns.
8567 * Use attributes 'attr'.
8568 */
8569 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008570screen_fill(
8571 int start_row,
8572 int end_row,
8573 int start_col,
8574 int end_col,
8575 int c1,
8576 int c2,
8577 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 int row;
8580 int col;
8581 int off;
8582 int end_off;
8583 int did_delete;
8584 int c;
8585 int norm_term;
8586#if defined(FEAT_GUI) || defined(UNIX)
8587 int force_next = FALSE;
8588#endif
8589
8590 if (end_row > screen_Rows) /* safety check */
8591 end_row = screen_Rows;
8592 if (end_col > screen_Columns) /* safety check */
8593 end_col = screen_Columns;
8594 if (ScreenLines == NULL
8595 || start_row >= end_row
8596 || start_col >= end_col) /* nothing to do */
8597 return;
8598
8599 /* it's a "normal" terminal when not in a GUI or cterm */
8600 norm_term = (
8601#ifdef FEAT_GUI
8602 !gui.in_use &&
8603#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008604 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 for (row = start_row; row < end_row; ++row)
8606 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008607#ifdef FEAT_MBYTE
8608 if (has_mbyte
8609# ifdef FEAT_GUI
8610 && !gui.in_use
8611# endif
8612 )
8613 {
8614 /* When drawing over the right halve of a double-wide char clear
8615 * out the left halve. When drawing over the left halve of a
8616 * double wide-char clear out the right halve. Only needed in a
8617 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008618 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008619 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008620 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008621 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008622 }
8623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 /*
8625 * Try to use delete-line termcap code, when no attributes or in a
8626 * "normal" terminal, where a bold/italic space is just a
8627 * space.
8628 */
8629 did_delete = FALSE;
8630 if (c2 == ' '
8631 && end_col == Columns
8632 && can_clear(T_CE)
8633 && (attr == 0
8634 || (norm_term
8635 && attr <= HL_ALL
8636 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8637 {
8638 /*
8639 * check if we really need to clear something
8640 */
8641 col = start_col;
8642 if (c1 != ' ') /* don't clear first char */
8643 ++col;
8644
8645 off = LineOffset[row] + col;
8646 end_off = LineOffset[row] + end_col;
8647
8648 /* skip blanks (used often, keep it fast!) */
8649#ifdef FEAT_MBYTE
8650 if (enc_utf8)
8651 while (off < end_off && ScreenLines[off] == ' '
8652 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8653 ++off;
8654 else
8655#endif
8656 while (off < end_off && ScreenLines[off] == ' '
8657 && ScreenAttrs[off] == 0)
8658 ++off;
8659 if (off < end_off) /* something to be cleared */
8660 {
8661 col = off - LineOffset[row];
8662 screen_stop_highlight();
8663 term_windgoto(row, col);/* clear rest of this screen line */
8664 out_str(T_CE);
8665 screen_start(); /* don't know where cursor is now */
8666 col = end_col - col;
8667 while (col--) /* clear chars in ScreenLines */
8668 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008669 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 ++off;
8671 }
8672 }
8673 did_delete = TRUE; /* the chars are cleared now */
8674 }
8675
8676 off = LineOffset[row] + start_col;
8677 c = c1;
8678 for (col = start_col; col < end_col; ++col)
8679 {
8680 if (ScreenLines[off] != c
8681#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008682 || (enc_utf8 && (int)ScreenLinesUC[off]
8683 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684#endif
8685 || ScreenAttrs[off] != attr
8686#if defined(FEAT_GUI) || defined(UNIX)
8687 || force_next
8688#endif
8689 )
8690 {
8691#if defined(FEAT_GUI) || defined(UNIX)
8692 /* The bold trick may make a single row of pixels appear in
8693 * the next character. When a bold character is removed, the
8694 * next character should be redrawn too. This happens for our
8695 * own GUI and for some xterms. */
8696 if (
8697# ifdef FEAT_GUI
8698 gui.in_use
8699# endif
8700# if defined(FEAT_GUI) && defined(UNIX)
8701 ||
8702# endif
8703# ifdef UNIX
8704 term_is_xterm
8705# endif
8706 )
8707 {
8708 if (ScreenLines[off] != ' '
8709 && (ScreenAttrs[off] > HL_ALL
8710 || ScreenAttrs[off] & HL_BOLD))
8711 force_next = TRUE;
8712 else
8713 force_next = FALSE;
8714 }
8715#endif
8716 ScreenLines[off] = c;
8717#ifdef FEAT_MBYTE
8718 if (enc_utf8)
8719 {
8720 if (c >= 0x80)
8721 {
8722 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008723 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 }
8725 else
8726 ScreenLinesUC[off] = 0;
8727 }
8728#endif
8729 ScreenAttrs[off] = attr;
8730 if (!did_delete || c != ' ')
8731 screen_char(off, row, col);
8732 }
8733 ++off;
8734 if (col == start_col)
8735 {
8736 if (did_delete)
8737 break;
8738 c = c2;
8739 }
8740 }
8741 if (end_col == Columns)
8742 LineWraps[row] = FALSE;
8743 if (row == Rows - 1) /* overwritten the command line */
8744 {
8745 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008746 if (start_col == 0 && end_col == Columns
8747 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008749 if (start_col == 0)
8750 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 }
8752 }
8753}
8754
8755/*
8756 * Check if there should be a delay. Used before clearing or redrawing the
8757 * screen or the command line.
8758 */
8759 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008760check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8763 && !did_wait_return
8764 && emsg_silent == 0)
8765 {
8766 out_flush();
8767 ui_delay(1000L, TRUE);
8768 emsg_on_display = FALSE;
8769 if (check_msg_scroll)
8770 msg_scroll = FALSE;
8771 }
8772}
8773
8774/*
8775 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008776 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 * Returns TRUE if there is a valid screen to write to.
8778 * Returns FALSE when starting up and screen not initialized yet.
8779 */
8780 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008781screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008783 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 return (ScreenLines != NULL);
8785}
8786
8787/*
8788 * Resize the shell to Rows and Columns.
8789 * Allocate ScreenLines[] and associated items.
8790 *
8791 * There may be some time between setting Rows and Columns and (re)allocating
8792 * ScreenLines[]. This happens when starting up and when (manually) changing
8793 * the shell size. Always use screen_Rows and screen_Columns to access items
8794 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8795 * final size of the shell is needed.
8796 */
8797 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008798screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
8800 int new_row, old_row;
8801#ifdef FEAT_GUI
8802 int old_Rows;
8803#endif
8804 win_T *wp;
8805 int outofmem = FALSE;
8806 int len;
8807 schar_T *new_ScreenLines;
8808#ifdef FEAT_MBYTE
8809 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008810 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008812 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813#endif
8814 sattr_T *new_ScreenAttrs;
8815 unsigned *new_LineOffset;
8816 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008817 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008818 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008820 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008821 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008823retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 /*
8825 * Allocation of the screen buffers is done only when the size changes and
8826 * when Rows and Columns have been set and we have started doing full
8827 * screen stuff.
8828 */
8829 if ((ScreenLines != NULL
8830 && Rows == screen_Rows
8831 && Columns == screen_Columns
8832#ifdef FEAT_MBYTE
8833 && enc_utf8 == (ScreenLinesUC != NULL)
8834 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008835 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836#endif
8837 )
8838 || Rows == 0
8839 || Columns == 0
8840 || (!full_screen && ScreenLines == NULL))
8841 return;
8842
8843 /*
8844 * It's possible that we produce an out-of-memory message below, which
8845 * will cause this function to be called again. To break the loop, just
8846 * return here.
8847 */
8848 if (entered)
8849 return;
8850 entered = TRUE;
8851
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008852 /*
8853 * Note that the window sizes are updated before reallocating the arrays,
8854 * thus we must not redraw here!
8855 */
8856 ++RedrawingDisabled;
8857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 win_new_shellsize(); /* fit the windows in the new sized shell */
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 comp_col(); /* recompute columns for shown command and ruler */
8861
8862 /*
8863 * We're changing the size of the screen.
8864 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8865 * - Move lines from the old arrays into the new arrays, clear extra
8866 * lines (unless the screen is going to be cleared).
8867 * - Free the old arrays.
8868 *
8869 * If anything fails, make ScreenLines NULL, so we don't do anything!
8870 * Continuing with the old ScreenLines may result in a crash, because the
8871 * size is wrong.
8872 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008873 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008875 if (aucmd_win != NULL)
8876 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
8878 new_ScreenLines = (schar_T *)lalloc((long_u)(
8879 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8880#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008881 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 if (enc_utf8)
8883 {
8884 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8885 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008886 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008887 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8889 }
8890 if (enc_dbcs == DBCS_JPNU)
8891 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8892 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8893#endif
8894 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8895 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8896 new_LineOffset = (unsigned *)lalloc((long_u)(
8897 Rows * sizeof(unsigned)), FALSE);
8898 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008899 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
8903 if (win_alloc_lines(wp) == FAIL)
8904 {
8905 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008906 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
8908 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008909 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8910 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008911 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008912give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008914#ifdef FEAT_MBYTE
8915 for (i = 0; i < p_mco; ++i)
8916 if (new_ScreenLinesC[i] == NULL)
8917 break;
8918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 if (new_ScreenLines == NULL
8920#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008921 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
8923#endif
8924 || new_ScreenAttrs == NULL
8925 || new_LineOffset == NULL
8926 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008927 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 || outofmem)
8929 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008930 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008931 {
8932 /* guess the size */
8933 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8934
8935 /* Remember we did this to avoid getting outofmem messages over
8936 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008937 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008938 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008939 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01008941 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008942 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008943 VIM_CLEAR(new_ScreenLinesC[i]);
8944 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(new_ScreenAttrs);
8947 VIM_CLEAR(new_LineOffset);
8948 VIM_CLEAR(new_LineWraps);
8949 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 else
8952 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008953 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008954
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 for (new_row = 0; new_row < Rows; ++new_row)
8956 {
8957 new_LineOffset[new_row] = new_row * Columns;
8958 new_LineWraps[new_row] = FALSE;
8959
8960 /*
8961 * If the screen is not going to be cleared, copy as much as
8962 * possible from the old screen to the new one and clear the rest
8963 * (used when resizing the window at the "--more--" prompt or when
8964 * executing an external command, for the GUI).
8965 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008966 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 {
8968 (void)vim_memset(new_ScreenLines + new_row * Columns,
8969 ' ', (size_t)Columns * sizeof(schar_T));
8970#ifdef FEAT_MBYTE
8971 if (enc_utf8)
8972 {
8973 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8974 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008975 for (i = 0; i < p_mco; ++i)
8976 (void)vim_memset(new_ScreenLinesC[i]
8977 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 0, (size_t)Columns * sizeof(u8char_T));
8979 }
8980 if (enc_dbcs == DBCS_JPNU)
8981 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8982 0, (size_t)Columns * sizeof(schar_T));
8983#endif
8984 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8985 0, (size_t)Columns * sizeof(sattr_T));
8986 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008987 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
8989 if (screen_Columns < Columns)
8990 len = screen_Columns;
8991 else
8992 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008993#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008994 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008995 * may be invalid now. Also when p_mco changes. */
8996 if (!(enc_utf8 && ScreenLinesUC == NULL)
8997 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008998#endif
8999 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9000 ScreenLines + LineOffset[old_row],
9001 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009003 if (enc_utf8 && ScreenLinesUC != NULL
9004 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 {
9006 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9007 ScreenLinesUC + LineOffset[old_row],
9008 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 for (i = 0; i < p_mco; ++i)
9010 mch_memmove(new_ScreenLinesC[i]
9011 + new_LineOffset[new_row],
9012 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 (size_t)len * sizeof(u8char_T));
9014 }
9015 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9016 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9017 ScreenLines2 + LineOffset[old_row],
9018 (size_t)len * sizeof(schar_T));
9019#endif
9020 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9021 ScreenAttrs + LineOffset[old_row],
9022 (size_t)len * sizeof(sattr_T));
9023 }
9024 }
9025 }
9026 /* Use the last line of the screen for the current line. */
9027 current_ScreenLine = new_ScreenLines + Rows * Columns;
9028 }
9029
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009030 free_screenlines();
9031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 ScreenLines = new_ScreenLines;
9033#ifdef FEAT_MBYTE
9034 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009035 for (i = 0; i < p_mco; ++i)
9036 ScreenLinesC[i] = new_ScreenLinesC[i];
9037 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 ScreenLines2 = new_ScreenLines2;
9039#endif
9040 ScreenAttrs = new_ScreenAttrs;
9041 LineOffset = new_LineOffset;
9042 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009043 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044
9045 /* It's important that screen_Rows and screen_Columns reflect the actual
9046 * size of ScreenLines[]. Set them before calling anything. */
9047#ifdef FEAT_GUI
9048 old_Rows = screen_Rows;
9049#endif
9050 screen_Rows = Rows;
9051 screen_Columns = Columns;
9052
9053 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009054 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 screenclear2();
9056
9057#ifdef FEAT_GUI
9058 else if (gui.in_use
9059 && !gui.starting
9060 && ScreenLines != NULL
9061 && old_Rows != Rows)
9062 {
9063 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9064 /*
9065 * Adjust the position of the cursor, for when executing an external
9066 * command.
9067 */
9068 if (msg_row >= Rows) /* Rows got smaller */
9069 msg_row = Rows - 1; /* put cursor at last row */
9070 else if (Rows > old_Rows) /* Rows got bigger */
9071 msg_row += Rows - old_Rows; /* put cursor in same place */
9072 if (msg_col >= Columns) /* Columns got smaller */
9073 msg_col = Columns - 1; /* put cursor at last column */
9074 }
9075#endif
9076
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009078 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009079
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009080 /*
9081 * Do not apply autocommands more than 3 times to avoid an endless loop
9082 * in case applying autocommands always changes Rows or Columns.
9083 */
9084 if (starting == 0 && ++retry_count <= 3)
9085 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009086 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009087 /* In rare cases, autocommands may have altered Rows or Columns,
9088 * jump back to check if we need to allocate the screen again. */
9089 goto retry;
9090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009094free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009095{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009096#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009097 int i;
9098
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009099 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009100 for (i = 0; i < Screen_mco; ++i)
9101 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009102 vim_free(ScreenLines2);
9103#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009104 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009105 vim_free(ScreenAttrs);
9106 vim_free(LineOffset);
9107 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009108 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009109}
9110
9111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009112screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 check_for_delay(FALSE);
9115 screenalloc(FALSE); /* allocate screen buffers if size changed */
9116 screenclear2(); /* clear the screen */
9117}
9118
9119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009120screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 int i;
9123
9124 if (starting == NO_SCREEN || ScreenLines == NULL
9125#ifdef FEAT_GUI
9126 || (gui.in_use && gui.starting)
9127#endif
9128 )
9129 return;
9130
9131#ifdef FEAT_GUI
9132 if (!gui.in_use)
9133#endif
9134 screen_attr = -1; /* force setting the Normal colors */
9135 screen_stop_highlight(); /* don't want highlighting here */
9136
9137#ifdef FEAT_CLIPBOARD
9138 /* disable selection without redrawing it */
9139 clip_scroll_selection(9999);
9140#endif
9141
9142 /* blank out ScreenLines */
9143 for (i = 0; i < Rows; ++i)
9144 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009145 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 LineWraps[i] = FALSE;
9147 }
9148
9149 if (can_clear(T_CL))
9150 {
9151 out_str(T_CL); /* clear the display */
9152 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009153 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 }
9155 else
9156 {
9157 /* can't clear the screen, mark all chars with invalid attributes */
9158 for (i = 0; i < Rows; ++i)
9159 lineinvalid(LineOffset[i], (int)Columns);
9160 clear_cmdline = TRUE;
9161 }
9162
9163 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9164
9165 win_rest_invalid(firstwin);
9166 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009167 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (must_redraw == CLEAR) /* no need to clear again */
9169 must_redraw = NOT_VALID;
9170 compute_cmdrow();
9171 msg_row = cmdline_row; /* put cursor on last line for messages */
9172 msg_col = 0;
9173 screen_start(); /* don't know where cursor is now */
9174 msg_scrolled = 0; /* can't scroll back */
9175 msg_didany = FALSE;
9176 msg_didout = FALSE;
9177}
9178
9179/*
9180 * Clear one line in ScreenLines.
9181 */
9182 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009183lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9186#ifdef FEAT_MBYTE
9187 if (enc_utf8)
9188 (void)vim_memset(ScreenLinesUC + off, 0,
9189 (size_t)width * sizeof(u8char_T));
9190#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009191 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192}
9193
9194/*
9195 * Mark one line in ScreenLines invalid by setting the attributes to an
9196 * invalid value.
9197 */
9198 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009199lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
9201 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9202}
9203
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204/*
9205 * Copy part of a Screenline for vertically split window "wp".
9206 */
9207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009208linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 unsigned off_to = LineOffset[to] + wp->w_wincol;
9211 unsigned off_from = LineOffset[from] + wp->w_wincol;
9212
9213 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9214 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009215#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 if (enc_utf8)
9217 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009218 int i;
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9221 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009222 for (i = 0; i < p_mco; ++i)
9223 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9224 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 }
9226 if (enc_dbcs == DBCS_JPNU)
9227 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9228 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9231 wp->w_width * sizeof(sattr_T));
9232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234/*
9235 * Return TRUE if clearing with term string "p" would work.
9236 * It can't work when the string is empty or it won't set the right background.
9237 */
9238 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009239can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 return (*p != NUL && (t_colors <= 1
9242#ifdef FEAT_GUI
9243 || gui.in_use
9244#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009245#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009246 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009247 || (!p_tgc && cterm_normal_bg_color == 0)
9248#else
9249 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009250#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009251 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252}
9253
9254/*
9255 * Reset cursor position. Use whenever cursor was moved because of outputting
9256 * something directly to the screen (shell commands) or a terminal control
9257 * code.
9258 */
9259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009260screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 screen_cur_row = screen_cur_col = 9999;
9263}
9264
9265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 * Move the cursor to position "row","col" in the screen.
9267 * This tries to find the most efficient way to move, minimizing the number of
9268 * characters sent to the terminal.
9269 */
9270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009271windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009273 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 int i;
9275 int plan;
9276 int cost;
9277 int wouldbe_col;
9278 int noinvcurs;
9279 char_u *bs;
9280 int goto_cost;
9281 int attr;
9282
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009283#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9285
9286#define PLAN_LE 1
9287#define PLAN_CR 2
9288#define PLAN_NL 3
9289#define PLAN_WRITE 4
9290 /* Can't use ScreenLines unless initialized */
9291 if (ScreenLines == NULL)
9292 return;
9293
9294 if (col != screen_cur_col || row != screen_cur_row)
9295 {
9296 /* Check for valid position. */
9297 if (row < 0) /* window without text lines? */
9298 row = 0;
9299 if (row >= screen_Rows)
9300 row = screen_Rows - 1;
9301 if (col >= screen_Columns)
9302 col = screen_Columns - 1;
9303
9304 /* check if no cursor movement is allowed in highlight mode */
9305 if (screen_attr && *T_MS == NUL)
9306 noinvcurs = HIGHL_COST;
9307 else
9308 noinvcurs = 0;
9309 goto_cost = GOTO_COST + noinvcurs;
9310
9311 /*
9312 * Plan how to do the positioning:
9313 * 1. Use CR to move it to column 0, same row.
9314 * 2. Use T_LE to move it a few columns to the left.
9315 * 3. Use NL to move a few lines down, column 0.
9316 * 4. Move a few columns to the right with T_ND or by writing chars.
9317 *
9318 * Don't do this if the cursor went beyond the last column, the cursor
9319 * position is unknown then (some terminals wrap, some don't )
9320 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009321 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 * characters to move the cursor to the right.
9323 */
9324 if (row >= screen_cur_row && screen_cur_col < Columns)
9325 {
9326 /*
9327 * If the cursor is in the same row, bigger col, we can use CR
9328 * or T_LE.
9329 */
9330 bs = NULL; /* init for GCC */
9331 attr = screen_attr;
9332 if (row == screen_cur_row && col < screen_cur_col)
9333 {
9334 /* "le" is preferred over "bc", because "bc" is obsolete */
9335 if (*T_LE)
9336 bs = T_LE; /* "cursor left" */
9337 else
9338 bs = T_BC; /* "backspace character (old) */
9339 if (*bs)
9340 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9341 else
9342 cost = 999;
9343 if (col + 1 < cost) /* using CR is less characters */
9344 {
9345 plan = PLAN_CR;
9346 wouldbe_col = 0;
9347 cost = 1; /* CR is just one character */
9348 }
9349 else
9350 {
9351 plan = PLAN_LE;
9352 wouldbe_col = col;
9353 }
9354 if (noinvcurs) /* will stop highlighting */
9355 {
9356 cost += noinvcurs;
9357 attr = 0;
9358 }
9359 }
9360
9361 /*
9362 * If the cursor is above where we want to be, we can use CR LF.
9363 */
9364 else if (row > screen_cur_row)
9365 {
9366 plan = PLAN_NL;
9367 wouldbe_col = 0;
9368 cost = (row - screen_cur_row) * 2; /* CR LF */
9369 if (noinvcurs) /* will stop highlighting */
9370 {
9371 cost += noinvcurs;
9372 attr = 0;
9373 }
9374 }
9375
9376 /*
9377 * If the cursor is in the same row, smaller col, just use write.
9378 */
9379 else
9380 {
9381 plan = PLAN_WRITE;
9382 wouldbe_col = screen_cur_col;
9383 cost = 0;
9384 }
9385
9386 /*
9387 * Check if any characters that need to be written have the
9388 * correct attributes. Also avoid UTF-8 characters.
9389 */
9390 i = col - wouldbe_col;
9391 if (i > 0)
9392 cost += i;
9393 if (cost < goto_cost && i > 0)
9394 {
9395 /*
9396 * Check if the attributes are correct without additionally
9397 * stopping highlighting.
9398 */
9399 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9400 while (i && *p++ == attr)
9401 --i;
9402 if (i != 0)
9403 {
9404 /*
9405 * Try if it works when highlighting is stopped here.
9406 */
9407 if (*--p == 0)
9408 {
9409 cost += noinvcurs;
9410 while (i && *p++ == 0)
9411 --i;
9412 }
9413 if (i != 0)
9414 cost = 999; /* different attributes, don't do it */
9415 }
9416#ifdef FEAT_MBYTE
9417 if (enc_utf8)
9418 {
9419 /* Don't use an UTF-8 char for positioning, it's slow. */
9420 for (i = wouldbe_col; i < col; ++i)
9421 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9422 {
9423 cost = 999;
9424 break;
9425 }
9426 }
9427#endif
9428 }
9429
9430 /*
9431 * We can do it without term_windgoto()!
9432 */
9433 if (cost < goto_cost)
9434 {
9435 if (plan == PLAN_LE)
9436 {
9437 if (noinvcurs)
9438 screen_stop_highlight();
9439 while (screen_cur_col > col)
9440 {
9441 out_str(bs);
9442 --screen_cur_col;
9443 }
9444 }
9445 else if (plan == PLAN_CR)
9446 {
9447 if (noinvcurs)
9448 screen_stop_highlight();
9449 out_char('\r');
9450 screen_cur_col = 0;
9451 }
9452 else if (plan == PLAN_NL)
9453 {
9454 if (noinvcurs)
9455 screen_stop_highlight();
9456 while (screen_cur_row < row)
9457 {
9458 out_char('\n');
9459 ++screen_cur_row;
9460 }
9461 screen_cur_col = 0;
9462 }
9463
9464 i = col - screen_cur_col;
9465 if (i > 0)
9466 {
9467 /*
9468 * Use cursor-right if it's one character only. Avoids
9469 * removing a line of pixels from the last bold char, when
9470 * using the bold trick in the GUI.
9471 */
9472 if (T_ND[0] != NUL && T_ND[1] == NUL)
9473 {
9474 while (i-- > 0)
9475 out_char(*T_ND);
9476 }
9477 else
9478 {
9479 int off;
9480
9481 off = LineOffset[row] + screen_cur_col;
9482 while (i-- > 0)
9483 {
9484 if (ScreenAttrs[off] != screen_attr)
9485 screen_stop_highlight();
9486#ifdef FEAT_MBYTE
9487 out_flush_check();
9488#endif
9489 out_char(ScreenLines[off]);
9490#ifdef FEAT_MBYTE
9491 if (enc_dbcs == DBCS_JPNU
9492 && ScreenLines[off] == 0x8e)
9493 out_char(ScreenLines2[off]);
9494#endif
9495 ++off;
9496 }
9497 }
9498 }
9499 }
9500 }
9501 else
9502 cost = 999;
9503
9504 if (cost >= goto_cost)
9505 {
9506 if (noinvcurs)
9507 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009508 if (row == screen_cur_row && (col > screen_cur_col)
9509 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 term_cursor_right(col - screen_cur_col);
9511 else
9512 term_windgoto(row, col);
9513 }
9514 screen_cur_row = row;
9515 screen_cur_col = col;
9516 }
9517}
9518
9519/*
9520 * Set cursor to its position in the current window.
9521 */
9522 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009523setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009525 setcursor_mayforce(FALSE);
9526}
9527
9528/*
9529 * Set cursor to its position in the current window.
9530 * When "force" is TRUE also when not redrawing.
9531 */
9532 void
9533setcursor_mayforce(int force)
9534{
9535 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 {
9537 validate_cursor();
9538 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009539 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009541 /* With 'rightleft' set and the cursor on a double-wide
9542 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009543 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009545 (has_mbyte
9546 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9547 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548# endif
9549 1)) :
9550#endif
9551 curwin->w_wcol));
9552 }
9553}
9554
9555
9556/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009557 * Insert 'line_count' lines at 'row' in window 'wp'.
9558 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9559 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 * scrolling.
9561 * Returns FAIL if the lines are not inserted, OK for success.
9562 */
9563 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009564win_ins_lines(
9565 win_T *wp,
9566 int row,
9567 int line_count,
9568 int invalid,
9569 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 int did_delete;
9572 int nextrow;
9573 int lastrow;
9574 int retval;
9575
9576 if (invalid)
9577 wp->w_lines_valid = 0;
9578
9579 if (wp->w_height < 5)
9580 return FAIL;
9581
9582 if (line_count > wp->w_height - row)
9583 line_count = wp->w_height - row;
9584
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009585 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 if (retval != MAYBE)
9587 return retval;
9588
9589 /*
9590 * If there is a next window or a status line, we first try to delete the
9591 * lines at the bottom to avoid messing what is after the window.
9592 * If this fails and there are following windows, don't do anything to avoid
9593 * messing up those windows, better just redraw.
9594 */
9595 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 if (wp->w_next != NULL || wp->w_status_height)
9597 {
9598 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009599 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 did_delete = TRUE;
9601 else if (wp->w_next)
9602 return FAIL;
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 /*
9605 * if no lines deleted, blank the lines that will end up below the window
9606 */
9607 if (!did_delete)
9608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009611 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 lastrow = nextrow + line_count;
9613 if (lastrow > Rows)
9614 lastrow = Rows;
9615 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009616 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 ' ', ' ', 0);
9618 }
9619
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009620 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 == FAIL)
9622 {
9623 /* deletion will have messed up other windows */
9624 if (did_delete)
9625 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 win_rest_invalid(W_NEXT(wp));
9628 }
9629 return FAIL;
9630 }
9631
9632 return OK;
9633}
9634
9635/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009636 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9638 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9639 * scrolling
9640 * Return OK for success, FAIL if the lines are not deleted.
9641 */
9642 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009643win_del_lines(
9644 win_T *wp,
9645 int row,
9646 int line_count,
9647 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009648 int mayclear,
9649 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651 int retval;
9652
9653 if (invalid)
9654 wp->w_lines_valid = 0;
9655
9656 if (line_count > wp->w_height - row)
9657 line_count = wp->w_height - row;
9658
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009659 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 if (retval != MAYBE)
9661 return retval;
9662
9663 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009664 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 return FAIL;
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 /*
9668 * If there are windows or status lines below, try to put them at the
9669 * correct place. If we can't do that, they have to be redrawn.
9670 */
9671 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9672 {
9673 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009674 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 {
9676 wp->w_redr_status = TRUE;
9677 win_rest_invalid(wp->w_next);
9678 }
9679 }
9680 /*
9681 * If this is the last window and there is no status line, redraw the
9682 * command line later.
9683 */
9684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 redraw_cmdline = TRUE;
9686 return OK;
9687}
9688
9689/*
9690 * Common code for win_ins_lines() and win_del_lines().
9691 * Returns OK or FAIL when the work has been done.
9692 * Returns MAYBE when not finished yet.
9693 */
9694 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009695win_do_lines(
9696 win_T *wp,
9697 int row,
9698 int line_count,
9699 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009700 int del,
9701 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 int retval;
9704
9705 if (!redrawing() || line_count <= 0)
9706 return FAIL;
9707
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009708 /* When inserting lines would result in loss of command output, just redraw
9709 * the lines. */
9710 if (no_win_do_lines_ins && !del)
9711 return FAIL;
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009714 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009716 if (!no_win_do_lines_ins)
9717 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 return FAIL;
9719 }
9720
9721 /*
9722 * Delete all remaining lines
9723 */
9724 if (row + line_count >= wp->w_height)
9725 {
9726 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009727 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 ' ', ' ', 0);
9729 return OK;
9730 }
9731
9732 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009733 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009735 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009737 if (!no_win_do_lines_ins)
9738 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739
9740 /*
9741 * If the terminal can set a scroll region, use that.
9742 * Always do this in a vertically split window. This will redraw from
9743 * ScreenLines[] when t_CV isn't defined. That's faster than using
9744 * win_line().
9745 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009746 * a character in the lower right corner of the scroll region may cause a
9747 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009749 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 scroll_region_set(wp, row);
9753 if (del)
9754 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009755 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 else
9757 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009758 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 scroll_region_reset();
9761 return retval;
9762 }
9763
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766
9767 return MAYBE;
9768}
9769
9770/*
9771 * window 'wp' and everything after it is messed up, mark it for redraw
9772 */
9773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009774win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 {
9778 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 wp->w_redr_status = TRUE;
9780 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 }
9782 redraw_cmdline = TRUE;
9783}
9784
9785/*
9786 * The rest of the routines in this file perform screen manipulations. The
9787 * given operation is performed physically on the screen. The corresponding
9788 * change is also made to the internal screen image. In this way, the editor
9789 * anticipates the effect of editing changes on the appearance of the screen.
9790 * That way, when we call screenupdate a complete redraw isn't usually
9791 * necessary. Another advantage is that we can keep adding code to anticipate
9792 * screen changes, and in the meantime, everything still works.
9793 */
9794
9795/*
9796 * types for inserting or deleting lines
9797 */
9798#define USE_T_CAL 1
9799#define USE_T_CDL 2
9800#define USE_T_AL 3
9801#define USE_T_CE 4
9802#define USE_T_DL 5
9803#define USE_T_SR 6
9804#define USE_NL 7
9805#define USE_T_CD 8
9806#define USE_REDRAW 9
9807
9808/*
9809 * insert lines on the screen and update ScreenLines[]
9810 * 'end' is the line after the scrolled part. Normally it is Rows.
9811 * When scrolling region used 'off' is the offset from the top for the region.
9812 * 'row' and 'end' are relative to the start of the region.
9813 *
9814 * return FAIL for failure, OK for success.
9815 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009816 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009817screen_ins_lines(
9818 int off,
9819 int row,
9820 int line_count,
9821 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009822 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009823 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824{
9825 int i;
9826 int j;
9827 unsigned temp;
9828 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009829 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 int type;
9831 int result_empty;
9832 int can_ce = can_clear(T_CE);
9833
9834 /*
9835 * FAIL if
9836 * - there is no valid screen
9837 * - the screen has to be redrawn completely
9838 * - the line count is less than one
9839 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009840 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009842 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9843#ifdef FEAT_CLIPBOARD
9844 || (clip_star.state != SELECT_CLEARED
9845 && redrawing_for_callback > 0)
9846#endif
9847 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 return FAIL;
9849
9850 /*
9851 * There are seven ways to insert lines:
9852 * 0. When in a vertically split window and t_CV isn't set, redraw the
9853 * characters from ScreenLines[].
9854 * 1. Use T_CD (clear to end of display) if it exists and the result of
9855 * the insert is just empty lines
9856 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9857 * present or line_count > 1. It looks better if we do all the inserts
9858 * at once.
9859 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9860 * insert is just empty lines and T_CE is not present or line_count >
9861 * 1.
9862 * 4. Use T_AL (insert line) if it exists.
9863 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9864 * just empty lines.
9865 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9866 * just empty lines.
9867 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9868 * the 'da' flag is not set or we have clear line capability.
9869 * 8. redraw the characters from ScreenLines[].
9870 *
9871 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9872 * the scrollbar for the window. It does have insert line, use that if it
9873 * exists.
9874 */
9875 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9877 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009878 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 type = USE_T_CD;
9880 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9881 type = USE_T_CAL;
9882 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9883 type = USE_T_CDL;
9884 else if (*T_AL != NUL)
9885 type = USE_T_AL;
9886 else if (can_ce && result_empty)
9887 type = USE_T_CE;
9888 else if (*T_DL != NUL && result_empty)
9889 type = USE_T_DL;
9890 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9891 type = USE_T_SR;
9892 else
9893 return FAIL;
9894
9895 /*
9896 * For clearing the lines screen_del_lines() is used. This will also take
9897 * care of t_db if necessary.
9898 */
9899 if (type == USE_T_CD || type == USE_T_CDL ||
9900 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009901 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903 /*
9904 * If text is retained below the screen, first clear or delete as many
9905 * lines at the bottom of the window as are about to be inserted so that
9906 * the deleted lines won't later surface during a screen_del_lines.
9907 */
9908 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009909 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
9911#ifdef FEAT_CLIPBOARD
9912 /* Remove a modeless selection when inserting lines halfway the screen
9913 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009914 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009915 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 else
9917 clip_scroll_selection(-line_count);
9918#endif
9919
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920#ifdef FEAT_GUI
9921 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9922 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009923 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924#endif
9925
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009926 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9927 cursor_col = wp->w_wincol;
9928
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (*T_CCS != NUL) /* cursor relative to region */
9930 cursor_row = row;
9931 else
9932 cursor_row = row + off;
9933
9934 /*
9935 * Shift LineOffset[] line_count down to reflect the inserted lines.
9936 * Clear the inserted lines in ScreenLines[].
9937 */
9938 row += off;
9939 end += off;
9940 for (i = 0; i < line_count; ++i)
9941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 if (wp != NULL && wp->w_width != Columns)
9943 {
9944 /* need to copy part of a line */
9945 j = end - 1 - i;
9946 while ((j -= line_count) >= row)
9947 linecopy(j + line_count, j, wp);
9948 j += line_count;
9949 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009950 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9951 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 else
9953 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9954 LineWraps[j] = FALSE;
9955 }
9956 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 {
9958 j = end - 1 - i;
9959 temp = LineOffset[j];
9960 while ((j -= line_count) >= row)
9961 {
9962 LineOffset[j + line_count] = LineOffset[j];
9963 LineWraps[j + line_count] = LineWraps[j];
9964 }
9965 LineOffset[j + line_count] = temp;
9966 LineWraps[j + line_count] = FALSE;
9967 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009968 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 else
9970 lineinvalid(temp, (int)Columns);
9971 }
9972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973
9974 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009975 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009976 if (clear_attr != 0)
9977 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 /* redraw the characters */
9980 if (type == USE_REDRAW)
9981 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009982 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 {
9984 term_append_lines(line_count);
9985 screen_start(); /* don't know where cursor is now */
9986 }
9987 else
9988 {
9989 for (i = 0; i < line_count; i++)
9990 {
9991 if (type == USE_T_AL)
9992 {
9993 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009994 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 out_str(T_AL);
9996 }
9997 else /* type == USE_T_SR */
9998 out_str(T_SR);
9999 screen_start(); /* don't know where cursor is now */
10000 }
10001 }
10002
10003 /*
10004 * With scroll-reverse and 'da' flag set we need to clear the lines that
10005 * have been scrolled down into the region.
10006 */
10007 if (type == USE_T_SR && *T_DA)
10008 {
10009 for (i = 0; i < line_count; ++i)
10010 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010011 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 out_str(T_CE);
10013 screen_start(); /* don't know where cursor is now */
10014 }
10015 }
10016
10017#ifdef FEAT_GUI
10018 gui_can_update_cursor();
10019 if (gui.in_use)
10020 out_flush(); /* always flush after a scroll */
10021#endif
10022 return OK;
10023}
10024
10025/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010026 * Delete lines on the screen and update ScreenLines[].
10027 * "end" is the line after the scrolled part. Normally it is Rows.
10028 * When scrolling region used "off" is the offset from the top for the region.
10029 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 *
10031 * Return OK for success, FAIL if the lines are not deleted.
10032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010034screen_del_lines(
10035 int off,
10036 int row,
10037 int line_count,
10038 int end,
10039 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010040 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010041 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042{
10043 int j;
10044 int i;
10045 unsigned temp;
10046 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010047 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 int cursor_end;
10049 int result_empty; /* result is empty until end of region */
10050 int can_delete; /* deleting line codes can be used */
10051 int type;
10052
10053 /*
10054 * FAIL if
10055 * - there is no valid screen
10056 * - the screen has to be redrawn completely
10057 * - the line count is less than one
10058 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010059 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010061 if (!screen_valid(TRUE) || line_count <= 0
10062 || (!force && line_count > p_ttyscroll)
10063#ifdef FEAT_CLIPBOARD
10064 || (clip_star.state != SELECT_CLEARED
10065 && redrawing_for_callback > 0)
10066#endif
10067 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 return FAIL;
10069
10070 /*
10071 * Check if the rest of the current region will become empty.
10072 */
10073 result_empty = row + line_count >= end;
10074
10075 /*
10076 * We can delete lines only when 'db' flag not set or when 'ce' option
10077 * available.
10078 */
10079 can_delete = (*T_DB == NUL || can_clear(T_CE));
10080
10081 /*
10082 * There are six ways to delete lines:
10083 * 0. When in a vertically split window and t_CV isn't set, redraw the
10084 * characters from ScreenLines[].
10085 * 1. Use T_CD if it exists and the result is empty.
10086 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10087 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10088 * none of the other ways work.
10089 * 4. Use T_CE (erase line) if the result is empty.
10090 * 5. Use T_DL (delete line) if it exists.
10091 * 6. redraw the characters from ScreenLines[].
10092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10094 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010095 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 type = USE_T_CD;
10097#if defined(__BEOS__) && defined(BEOS_DR8)
10098 /*
10099 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10100 * its internal termcap... this works okay for tests which test *T_DB !=
10101 * NUL. It has the disadvantage that the user cannot use any :set t_*
10102 * command to get T_DB (back) to empty_option, only :set term=... will do
10103 * the trick...
10104 * Anyway, this hack will hopefully go away with the next OS release.
10105 * (Olaf Seibert)
10106 */
10107 else if (row == 0 && T_DB == empty_option
10108 && (line_count == 1 || *T_CDL == NUL))
10109#else
10110 else if (row == 0 && (
10111#ifndef AMIGA
10112 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10113 * up, so use delete-line command */
10114 line_count == 1 ||
10115#endif
10116 *T_CDL == NUL))
10117#endif
10118 type = USE_NL;
10119 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10120 type = USE_T_CDL;
10121 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010122 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 type = USE_T_CE;
10124 else if (*T_DL != NUL && can_delete)
10125 type = USE_T_DL;
10126 else if (*T_CDL != NUL && can_delete)
10127 type = USE_T_CDL;
10128 else
10129 return FAIL;
10130
10131#ifdef FEAT_CLIPBOARD
10132 /* Remove a modeless selection when deleting lines halfway the screen or
10133 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010134 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010135 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 else
10137 clip_scroll_selection(line_count);
10138#endif
10139
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140#ifdef FEAT_GUI
10141 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10142 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010143 gui_dont_update_cursor(gui.cursor_row >= row + off
10144 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145#endif
10146
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010147 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10148 cursor_col = wp->w_wincol;
10149
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 if (*T_CCS != NUL) /* cursor relative to region */
10151 {
10152 cursor_row = row;
10153 cursor_end = end;
10154 }
10155 else
10156 {
10157 cursor_row = row + off;
10158 cursor_end = end + off;
10159 }
10160
10161 /*
10162 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10163 * Clear the inserted lines in ScreenLines[].
10164 */
10165 row += off;
10166 end += off;
10167 for (i = 0; i < line_count; ++i)
10168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 if (wp != NULL && wp->w_width != Columns)
10170 {
10171 /* need to copy part of a line */
10172 j = row + i;
10173 while ((j += line_count) <= end - 1)
10174 linecopy(j - line_count, j, wp);
10175 j -= line_count;
10176 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010177 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10178 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 else
10180 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10181 LineWraps[j] = FALSE;
10182 }
10183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 {
10185 /* whole width, moving the line pointers is faster */
10186 j = row + i;
10187 temp = LineOffset[j];
10188 while ((j += line_count) <= end - 1)
10189 {
10190 LineOffset[j - line_count] = LineOffset[j];
10191 LineWraps[j - line_count] = LineWraps[j];
10192 }
10193 LineOffset[j - line_count] = temp;
10194 LineWraps[j - line_count] = FALSE;
10195 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010196 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 else
10198 lineinvalid(temp, (int)Columns);
10199 }
10200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010202 if (screen_attr != clear_attr)
10203 screen_stop_highlight();
10204 if (clear_attr != 0)
10205 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 /* redraw the characters */
10208 if (type == USE_REDRAW)
10209 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010210 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010212 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 out_str(T_CD);
10214 screen_start(); /* don't know where cursor is now */
10215 }
10216 else if (type == USE_T_CDL)
10217 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010218 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 term_delete_lines(line_count);
10220 screen_start(); /* don't know where cursor is now */
10221 }
10222 /*
10223 * Deleting lines at top of the screen or scroll region: Just scroll
10224 * the whole screen (scroll region) up by outputting newlines on the
10225 * last line.
10226 */
10227 else if (type == USE_NL)
10228 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010229 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 for (i = line_count; --i >= 0; )
10231 out_char('\n'); /* cursor will remain on same line */
10232 }
10233 else
10234 {
10235 for (i = line_count; --i >= 0; )
10236 {
10237 if (type == USE_T_DL)
10238 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010239 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 out_str(T_DL); /* delete a line */
10241 }
10242 else /* type == USE_T_CE */
10243 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010244 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 out_str(T_CE); /* erase a line */
10246 }
10247 screen_start(); /* don't know where cursor is now */
10248 }
10249 }
10250
10251 /*
10252 * If the 'db' flag is set, we need to clear the lines that have been
10253 * scrolled up at the bottom of the region.
10254 */
10255 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10256 {
10257 for (i = line_count; i > 0; --i)
10258 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010259 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 out_str(T_CE); /* erase a line */
10261 screen_start(); /* don't know where cursor is now */
10262 }
10263 }
10264
10265#ifdef FEAT_GUI
10266 gui_can_update_cursor();
10267 if (gui.in_use)
10268 out_flush(); /* always flush after a scroll */
10269#endif
10270
10271 return OK;
10272}
10273
10274/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010275 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 *
10277 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10278 * If clear_cmdline is FALSE there may be a message there that needs to be
10279 * cleared only if a mode is shown.
10280 * Return the length of the message (0 if no message).
10281 */
10282 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010283showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 int need_clear;
10286 int length = 0;
10287 int do_mode;
10288 int attr;
10289 int nwr_save;
10290#ifdef FEAT_INS_EXPAND
10291 int sub_attr;
10292#endif
10293
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010294 do_mode = ((p_smd && msg_silent == 0)
10295 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010296 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010297 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010298 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 {
10300 /*
10301 * Don't show mode right now, when not redrawing or inside a mapping.
10302 * Call char_avail() only when we are going to show something, because
10303 * it takes a bit of time.
10304 */
10305 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10306 {
10307 redraw_cmdline = TRUE; /* show mode later */
10308 return 0;
10309 }
10310
10311 nwr_save = need_wait_return;
10312
10313 /* wait a bit before overwriting an important message */
10314 check_for_delay(FALSE);
10315
10316 /* if the cmdline is more than one line high, erase top lines */
10317 need_clear = clear_cmdline;
10318 if (clear_cmdline && cmdline_row < Rows - 1)
10319 msg_clr_cmdline(); /* will reset clear_cmdline */
10320
10321 /* Position on the last line in the window, column 0 */
10322 msg_pos_mode();
10323 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010324 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 if (do_mode)
10326 {
10327 MSG_PUTS_ATTR("--", attr);
10328#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010329 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010330# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010331 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010332# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010333 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010335 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010336# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 MSG_PUTS_ATTR(" IM", attr);
10338# else
10339 MSG_PUTS_ATTR(" XIM", attr);
10340# endif
10341#endif
10342#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10343 if (gui.in_use)
10344 {
10345 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010346 {
10347 /* HANGUL */
10348 if (enc_utf8)
10349 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10350 else
10351 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354#endif
10355#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010356 /* CTRL-X in Insert mode */
10357 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 {
10359 /* These messages can get long, avoid a wrap in a narrow
10360 * window. Prefer showing edit_submode_extra. */
10361 length = (Rows - msg_row) * Columns - 3;
10362 if (edit_submode_extra != NULL)
10363 length -= vim_strsize(edit_submode_extra);
10364 if (length > 0)
10365 {
10366 if (edit_submode_pre != NULL)
10367 length -= vim_strsize(edit_submode_pre);
10368 if (length - vim_strsize(edit_submode) > 0)
10369 {
10370 if (edit_submode_pre != NULL)
10371 msg_puts_attr(edit_submode_pre, attr);
10372 msg_puts_attr(edit_submode, attr);
10373 }
10374 if (edit_submode_extra != NULL)
10375 {
10376 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10377 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010378 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 else
10380 sub_attr = attr;
10381 msg_puts_attr(edit_submode_extra, sub_attr);
10382 }
10383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 }
10385 else
10386#endif
10387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (State & VREPLACE_FLAG)
10389 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010390 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10392 else if (State & INSERT)
10393 {
10394#ifdef FEAT_RIGHTLEFT
10395 if (p_ri)
10396 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10397#endif
10398 MSG_PUTS_ATTR(_(" INSERT"), attr);
10399 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010400 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 MSG_PUTS_ATTR(_(" (insert)"), attr);
10402 else if (restart_edit == 'R')
10403 MSG_PUTS_ATTR(_(" (replace)"), attr);
10404 else if (restart_edit == 'V')
10405 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10406#ifdef FEAT_RIGHTLEFT
10407 if (p_hkmap)
10408 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10409# ifdef FEAT_FKMAP
10410 if (p_fkmap)
10411 MSG_PUTS_ATTR(farsi_text_5, attr);
10412# endif
10413#endif
10414#ifdef FEAT_KEYMAP
10415 if (State & LANGMAP)
10416 {
10417# ifdef FEAT_ARABIC
10418 if (curwin->w_p_arab)
10419 MSG_PUTS_ATTR(_(" Arabic"), attr);
10420 else
10421# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010422 if (get_keymap_str(curwin, (char_u *)" (%s)",
10423 NameBuff, MAXPATHL))
10424 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 }
10426#endif
10427 if ((State & INSERT) && p_paste)
10428 MSG_PUTS_ATTR(_(" (paste)"), attr);
10429
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (VIsual_active)
10431 {
10432 char *p;
10433
10434 /* Don't concatenate separate words to avoid translation
10435 * problems. */
10436 switch ((VIsual_select ? 4 : 0)
10437 + (VIsual_mode == Ctrl_V) * 2
10438 + (VIsual_mode == 'V'))
10439 {
10440 case 0: p = N_(" VISUAL"); break;
10441 case 1: p = N_(" VISUAL LINE"); break;
10442 case 2: p = N_(" VISUAL BLOCK"); break;
10443 case 4: p = N_(" SELECT"); break;
10444 case 5: p = N_(" SELECT LINE"); break;
10445 default: p = N_(" SELECT BLOCK"); break;
10446 }
10447 MSG_PUTS_ATTR(_(p), attr);
10448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 MSG_PUTS_ATTR(" --", attr);
10450 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010451
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 need_clear = TRUE;
10453 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010454 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455#ifdef FEAT_INS_EXPAND
10456 && edit_submode == NULL /* otherwise it gets too long */
10457#endif
10458 )
10459 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010460 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 need_clear = TRUE;
10462 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010463
10464 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (need_clear || clear_cmdline)
10466 msg_clr_eos();
10467 msg_didout = FALSE; /* overwrite this message */
10468 length = msg_col;
10469 msg_col = 0;
10470 need_wait_return = nwr_save; /* never ask for hit-return for this */
10471 }
10472 else if (clear_cmdline && msg_silent == 0)
10473 /* Clear the whole command line. Will reset "clear_cmdline". */
10474 msg_clr_cmdline();
10475
10476#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 /* In Visual mode the size of the selected area must be redrawn. */
10478 if (VIsual_active)
10479 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
10481 /* If the last window has no status line, the ruler is after the mode
10482 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010483 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010484 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485#endif
10486 redraw_cmdline = FALSE;
10487 clear_cmdline = FALSE;
10488
10489 return length;
10490}
10491
10492/*
10493 * Position for a mode message.
10494 */
10495 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010496msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 msg_col = 0;
10499 msg_row = Rows - 1;
10500}
10501
10502/*
10503 * Delete mode message. Used when ESC is typed which is expected to end
10504 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010505 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 */
10507 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010508unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509{
10510 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010511 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 */
10513 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10514 redraw_cmdline = TRUE; /* delete mode later */
10515 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010516 clearmode();
10517}
10518
10519/*
10520 * Clear the mode message.
10521 */
10522 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010523clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010524{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010525 int save_msg_row = msg_row;
10526 int save_msg_col = msg_col;
10527
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010528 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010529 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010530 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010531 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010532
10533 msg_col = save_msg_col;
10534 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010537 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010538recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010539{
10540 MSG_PUTS_ATTR(_("recording"), attr);
10541 if (!shortmess(SHM_RECORDING))
10542 {
10543 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010544 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010545 MSG_PUTS_ATTR(s, attr);
10546 }
10547}
10548
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010549/*
10550 * Draw the tab pages line at the top of the Vim window.
10551 */
10552 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010553draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010554{
10555 int tabcount = 0;
10556 tabpage_T *tp;
10557 int tabwidth;
10558 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010559 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010560 int attr;
10561 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010562 win_T *cwp;
10563 int wincount;
10564 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010565 int c;
10566 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010567 int attr_sel = HL_ATTR(HLF_TPS);
10568 int attr_nosel = HL_ATTR(HLF_TP);
10569 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010570 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010571 int room;
10572 int use_sep_chars = (t_colors < 8
10573#ifdef FEAT_GUI
10574 && !gui.in_use
10575#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010576#ifdef FEAT_TERMGUICOLORS
10577 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010578#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010579 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010580
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010581 if (ScreenLines == NULL)
10582 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010583 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010584
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010585#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010586 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010587 if (gui_use_tabline())
10588 {
10589 gui_update_tabline();
10590 return;
10591 }
10592#endif
10593
10594 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010595 return;
10596
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010597#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010598
10599 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10600 for (scol = 0; scol < Columns; ++scol)
10601 TabPageIdxs[scol] = 0;
10602
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010603 /* Use the 'tabline' option if it's set. */
10604 if (*p_tal != NUL)
10605 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010606 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607
10608 /* Check for an error. If there is one we would loop in redrawing the
10609 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010610 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010611 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010612 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010614 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010615 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010616 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010618#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010619 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010620 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010622
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10624 if (tabwidth < 6)
10625 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 attr = attr_nosel;
10628 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010629 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010630 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10631 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010632 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010634
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 if (tp->tp_topframe == topframe)
10636 attr = attr_sel;
10637 if (use_sep_chars && col > 0)
10638 screen_putchar('|', 0, col++, attr);
10639
10640 if (tp->tp_topframe != topframe)
10641 attr = attr_nosel;
10642
10643 screen_putchar(' ', 0, col++, attr);
10644
10645 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010646 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 cwp = curwin;
10648 wp = firstwin;
10649 }
10650 else
10651 {
10652 cwp = tp->tp_curwin;
10653 wp = tp->tp_firstwin;
10654 }
10655
10656 modified = FALSE;
10657 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10658 if (bufIsChanged(wp->w_buffer))
10659 modified = TRUE;
10660 if (modified || wincount > 1)
10661 {
10662 if (wincount > 1)
10663 {
10664 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010665 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010666 if (col + len >= Columns - 3)
10667 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010669#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010670 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010671#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010672 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010673#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 );
10675 col += len;
10676 }
10677 if (modified)
10678 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10679 screen_putchar(' ', 0, col++, attr);
10680 }
10681
10682 room = scol - col + tabwidth - 1;
10683 if (room > 0)
10684 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010685 /* Get buffer name in NameBuff[] */
10686 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010687 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 len = vim_strsize(NameBuff);
10689 p = NameBuff;
10690#ifdef FEAT_MBYTE
10691 if (has_mbyte)
10692 while (len > room)
10693 {
10694 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010695 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010696 }
10697 else
10698#endif
10699 if (len > room)
10700 {
10701 p += len - room;
10702 len = room;
10703 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010704 if (len > Columns - col - 1)
10705 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010707 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010708 col += len;
10709 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010710 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010711
10712 /* Store the tab page number in TabPageIdxs[], so that
10713 * jump_to_mouse() knows where each one is. */
10714 ++tabcount;
10715 while (scol < col)
10716 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010717 }
10718
Bram Moolenaar238a5642006-02-21 22:12:05 +000010719 if (use_sep_chars)
10720 c = '_';
10721 else
10722 c = ' ';
10723 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010724
10725 /* Put an "X" for closing the current tab if there are several. */
10726 if (first_tabpage->tp_next != NULL)
10727 {
10728 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10729 TabPageIdxs[Columns - 1] = -999;
10730 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010731 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010732
10733 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10734 * set. */
10735 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010737
10738/*
10739 * Get buffer name for "buf" into NameBuff[].
10740 * Takes care of special buffer names and translates special characters.
10741 */
10742 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010743get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010744{
10745 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010746 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010747 else
10748 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10749 trans_characters(NameBuff, MAXPATHL);
10750}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752/*
10753 * Get the character to use in a status line. Get its attributes in "*attr".
10754 */
10755 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759
10760#ifdef FEAT_TERMINAL
10761 if (bt_terminal(wp->w_buffer))
10762 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010763 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010764 {
10765 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010766 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010767 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010768 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010769 {
10770 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010771 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010772 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 }
10774 else
10775#endif
10776 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010778 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 fill = fill_stl;
10780 }
10781 else
10782 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010783 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 fill = fill_stlnc;
10785 }
10786 /* Use fill when there is highlighting, and highlighting of current
10787 * window differs, or the fillchars differ, or this is not the
10788 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010789 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010790 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 || (fill_stl != fill_stlnc)))
10792 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010793 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 return '^';
10795 return '=';
10796}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798/*
10799 * Get the character to use in a separator between vertically split windows.
10800 * Get its attributes in "*attr".
10801 */
10802 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010803fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010805 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (*attr == 0 && fill_vert == ' ')
10807 return '|';
10808 else
10809 return fill_vert;
10810}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
10812/*
10813 * Return TRUE if redrawing should currently be done.
10814 */
10815 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010816redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010818#ifdef FEAT_EVAL
10819 if (disable_redraw_for_testing)
10820 return 0;
10821 else
10822#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010823 return ((!RedrawingDisabled
10824#ifdef FEAT_EVAL
10825 || ignore_redraw_flag_for_testing
10826#endif
10827 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * Return TRUE if printing messages should currently be done.
10832 */
10833 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010834messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 return (!(p_lz && char_avail() && !KeyTyped));
10837}
10838
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010839#ifdef FEAT_MENU
10840/*
10841 * Draw the window toolbar.
10842 */
10843 static void
10844redraw_win_toolbar(win_T *wp)
10845{
10846 vimmenu_T *menu;
10847 int item_idx = 0;
10848 int item_count = 0;
10849 int col = 0;
10850 int next_col;
10851 int off = (int)(current_ScreenLine - ScreenLines);
10852 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10853 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10854
10855 vim_free(wp->w_winbar_items);
10856 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10857 ++item_count;
10858 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10859 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10860
10861 /* TODO: use fewer spaces if there is not enough room */
10862 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010863 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010864 {
10865 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010866 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010867 break;
10868 if (col > 1)
10869 {
10870 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010871 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010872 break;
10873 }
10874
10875 wp->w_winbar_items[item_idx].wb_startcol = col;
10876 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010877 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010878 break;
10879
10880 next_col = text_to_screenline(wp, menu->name, col);
10881 while (col < next_col)
10882 {
10883 ScreenAttrs[off + col] = button_attr;
10884 ++col;
10885 }
10886 wp->w_winbar_items[item_idx].wb_endcol = col;
10887 wp->w_winbar_items[item_idx].wb_menu = menu;
10888 ++item_idx;
10889
Bram Moolenaar02631462017-09-22 15:20:32 +020010890 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010891 break;
10892 space_to_screenline(off + col, button_attr);
10893 ++col;
10894 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010895 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010896 {
10897 space_to_screenline(off + col, fill_attr);
10898 ++col;
10899 }
10900 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10901
Bram Moolenaar02631462017-09-22 15:20:32 +020010902 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10903 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010904}
10905#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010906
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907/*
10908 * Show current status info in ruler and various other places
10909 * If always is FALSE, only show ruler if position has changed.
10910 */
10911 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010912showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913{
10914 if (!always && !redrawing())
10915 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010916#ifdef FEAT_INS_EXPAND
10917 if (pum_visible())
10918 {
10919 /* Don't redraw right now, do it later. */
10920 curwin->w_redr_status = TRUE;
10921 return;
10922 }
10923#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010924#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010925 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010926 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000010927 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 else
10930#endif
10931#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010932 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933#endif
10934
10935#ifdef FEAT_TITLE
10936 if (need_maketitle
10937# ifdef FEAT_STL_OPT
10938 || (p_icon && (stl_syntax & STL_IN_ICON))
10939 || (p_title && (stl_syntax & STL_IN_TITLE))
10940# endif
10941 )
10942 maketitle();
10943#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010944 /* Redraw the tab pages line if needed. */
10945 if (redraw_tabline)
10946 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947}
10948
10949#ifdef FEAT_CMDL_INFO
10950 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010951win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010953#define RULER_BUF_LEN 70
10954 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 int row;
10956 int fillchar;
10957 int attr;
10958 int empty_line = FALSE;
10959 colnr_T virtcol;
10960 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010961 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 int this_ru_col;
10964 int off = 0;
10965 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966
10967 /* If 'ruler' off or redrawing disabled, don't do anything */
10968 if (!p_ru)
10969 return;
10970
10971 /*
10972 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10973 * after deleting lines, before cursor.lnum is corrected.
10974 */
10975 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10976 return;
10977
10978#ifdef FEAT_INS_EXPAND
10979 /* Don't draw the ruler while doing insert-completion, it might overwrite
10980 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (edit_submode != NULL)
10983 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010984 // Don't draw the ruler when the popup menu is visible, it may overlap.
10985 // Except when the popup menu will be redrawn anyway.
10986 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#endif
10989
10990#ifdef FEAT_STL_OPT
10991 if (*p_ruf)
10992 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010993 int save_called_emsg = called_emsg;
10994
10995 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010997 if (called_emsg)
10998 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010999 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011000 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 return;
11002 }
11003#endif
11004
11005 /*
11006 * Check if not in Insert mode and the line is empty (will show "0-1").
11007 */
11008 if (!(State & INSERT)
11009 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11010 empty_line = TRUE;
11011
11012 /*
11013 * Only draw the ruler when something changed.
11014 */
11015 validate_virtcol_win(wp);
11016 if ( redraw_cmdline
11017 || always
11018 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11019 || wp->w_cursor.col != wp->w_ru_cursor.col
11020 || wp->w_virtcol != wp->w_ru_virtcol
11021#ifdef FEAT_VIRTUALEDIT
11022 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11023#endif
11024 || wp->w_topline != wp->w_ru_topline
11025 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11026#ifdef FEAT_DIFF
11027 || wp->w_topfill != wp->w_ru_topfill
11028#endif
11029 || empty_line != wp->w_ru_empty)
11030 {
11031 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 if (wp->w_status_height)
11033 {
11034 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011035 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011036 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011037 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 }
11039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 {
11041 row = Rows - 1;
11042 fillchar = ' ';
11043 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044 width = Columns;
11045 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 }
11047
11048 /* In list mode virtcol needs to be recomputed */
11049 virtcol = wp->w_virtcol;
11050 if (wp->w_p_list && lcs_tab1 == NUL)
11051 {
11052 wp->w_p_list = FALSE;
11053 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11054 wp->w_p_list = TRUE;
11055 }
11056
11057 /*
11058 * Some sprintfs return the length, some return a pointer.
11059 * To avoid portability problems we use strlen() here.
11060 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011061 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11063 ? 0L
11064 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011065 len = STRLEN(buffer);
11066 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11068 (int)virtcol + 1);
11069
11070 /*
11071 * Add a "50%" if there is room for it.
11072 * On the last line, don't print in the last column (scrolls the
11073 * screen up on some terminals).
11074 */
11075 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011076 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 this_ru_col = ru_col - (Columns - width);
11081 if (this_ru_col < 0)
11082 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 /* Never use more than half the window/screen width, leave the other
11084 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011085 if (this_ru_col < (width + 1) / 2)
11086 this_ru_col = (width + 1) / 2;
11087 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011089 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011090 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 {
11092#ifdef FEAT_MBYTE
11093 if (has_mbyte)
11094 i += (*mb_char2bytes)(fillchar, buffer + i);
11095 else
11096#endif
11097 buffer[i++] = fillchar;
11098 ++o;
11099 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011100 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 }
11102 /* Truncate at window boundary. */
11103#ifdef FEAT_MBYTE
11104 if (has_mbyte)
11105 {
11106 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011107 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 {
11109 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011110 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
11112 buffer[i] = NUL;
11113 break;
11114 }
11115 }
11116 }
11117 else
11118#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011119 if (this_ru_col + (int)STRLEN(buffer) > width)
11120 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121
Bram Moolenaar4033c552017-09-16 20:54:51 +020011122 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 i = redraw_cmdline;
11124 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011125 this_ru_col + off + (int)STRLEN(buffer),
11126 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 fillchar, fillchar, attr);
11128 /* don't redraw the cmdline because of showing the ruler */
11129 redraw_cmdline = i;
11130 wp->w_ru_cursor = wp->w_cursor;
11131 wp->w_ru_virtcol = wp->w_virtcol;
11132 wp->w_ru_empty = empty_line;
11133 wp->w_ru_topline = wp->w_topline;
11134 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11135#ifdef FEAT_DIFF
11136 wp->w_ru_topfill = wp->w_topfill;
11137#endif
11138 }
11139}
11140#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011141
11142#if defined(FEAT_LINEBREAK) || defined(PROTO)
11143/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011144 * Return the width of the 'number' and 'relativenumber' column.
11145 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011146 * Otherwise it depends on 'numberwidth' and the line count.
11147 */
11148 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011149number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011150{
11151 int n;
11152 linenr_T lnum;
11153
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011154 if (wp->w_p_rnu && !wp->w_p_nu)
11155 /* cursor line shows "0" */
11156 lnum = wp->w_height;
11157 else
11158 /* cursor line shows absolute line number */
11159 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011160
Bram Moolenaar6b314672015-03-20 15:42:10 +010011161 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011162 return wp->w_nrwidth_width;
11163 wp->w_nrwidth_line_count = lnum;
11164
11165 n = 0;
11166 do
11167 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011168 lnum /= 10;
11169 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011170 } while (lnum > 0);
11171
11172 /* 'numberwidth' gives the minimal width plus one */
11173 if (n < wp->w_p_nuw - 1)
11174 n = wp->w_p_nuw - 1;
11175
11176 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011177 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011178 return n;
11179}
11180#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011181
11182/*
11183 * Return the current cursor column. This is the actual position on the
11184 * screen. First column is 0.
11185 */
11186 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011187screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011188{
11189 return screen_cur_col;
11190}
11191
11192/*
11193 * Return the current cursor row. This is the actual position on the screen.
11194 * First row is 0.
11195 */
11196 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011197screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011198{
11199 return screen_cur_row;
11200}