blob: 04f19d7acd32a529ebd527637adbcb4ef31018fd [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
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static 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 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static 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 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200157static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200159#ifdef FEAT_MENU
160static void redraw_win_toolbar(win_T *wp);
161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200166static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/* Ugly global: overrule attribute used by screen_char() */
170static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200172#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
173/* Can limit syntax highlight time to 'redrawtime'. */
174# define SYN_TIME_LIMIT 1
175#endif
176
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200177#ifdef FEAT_RIGHTLEFT
178# define HAS_RIGHTLEFT(x) x
179#else
180# define HAS_RIGHTLEFT(x) FALSE
181#endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183/*
184 * Redraw the current window later, with update_screen(type).
185 * Set must_redraw only if not already set to a higher value.
186 * e.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
187 */
188 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100189redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
191 redraw_win_later(curwin, type);
192}
193
194 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100195redraw_win_later(
196 win_T *wp,
197 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200199 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100214redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100232redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 win_T *wp;
235
236 FOR_ALL_WINDOWS(wp)
237 {
238 redraw_win_later(wp, type);
239 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100240 // This may be needed when switching tabs.
241 if (must_redraw < type)
242 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200266 void
267redraw_buf_and_status_later(buf_T *buf, int type)
268{
269 win_T *wp;
270
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200271#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200272 if (wild_menu_showing != 0)
273 /* Don't redraw while the command line completion is displayed, it
274 * would disappear. */
275 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200276#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200277 FOR_ALL_WINDOWS(wp)
278 {
279 if (wp->w_buffer == buf)
280 {
281 redraw_win_later(wp, type);
282 wp->w_redr_status = TRUE;
283 }
284 }
285}
286
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200288 * Redraw as soon as possible. When the command line is not scrolled redraw
289 * right away and restore what was on the command line.
290 * Return a code indicating what happened.
291 */
292 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100293redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200294{
295 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200296 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200297 int r;
298 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200299 schar_T *screenline; /* copy from ScreenLines[] */
300 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200301#ifdef FEAT_MBYTE
302 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200303 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200305 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200306#endif
307
308 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200309 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310 return ret;
311
312 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200318 if (screenline == NULL || screenattr == NULL)
319 ret = 2;
320#ifdef FEAT_MBYTE
321 if (enc_utf8)
322 {
323 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200324 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenlineUC == NULL)
326 ret = 2;
327 for (i = 0; i < p_mco; ++i)
328 {
329 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200331 if (screenlineC[i] == NULL)
332 ret = 2;
333 }
334 }
335 if (enc_dbcs == DBCS_JPNU)
336 {
337 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200338 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenline2 == NULL)
340 ret = 2;
341 }
342#endif
343
344 if (ret != 2)
345 {
346 /* Save the text displayed in the command line area. */
347 for (r = 0; r < rows; ++r)
348 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 (size_t)cols * sizeof(schar_T));
352 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355#ifdef FEAT_MBYTE
356 if (enc_utf8)
357 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200359 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 mch_memmove(screenlineC[i] + r * cols,
363 ScreenLinesC[i] + LineOffset[cmdline_row + r],
364 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 }
366 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370#endif
371 }
372
373 update_screen(0);
374 ret = 3;
375
376 if (must_redraw == 0)
377 {
378 int off = (int)(current_ScreenLine - ScreenLines);
379
380 /* Restore the text displayed in the command line area. */
381 for (r = 0; r < rows; ++r)
382 {
383 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200384 screenline + r * cols,
385 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200386 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenattr + r * cols,
388 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389#ifdef FEAT_MBYTE
390 if (enc_utf8)
391 {
392 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200393 screenlineUC + r * cols,
394 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200395 for (i = 0; i < p_mco; ++i)
396 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenlineC[i] + r * cols,
398 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 }
400 if (enc_dbcs == DBCS_JPNU)
401 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenline2 + r * cols,
403 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200405 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 ret = 4;
408 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200409 }
410
411 vim_free(screenline);
412 vim_free(screenattr);
413#ifdef FEAT_MBYTE
414 if (enc_utf8)
415 {
416 vim_free(screenlineUC);
417 for (i = 0; i < p_mco; ++i)
418 vim_free(screenlineC[i]);
419 }
420 if (enc_dbcs == DBCS_JPNU)
421 vim_free(screenline2);
422#endif
423
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200424 /* Show the intro message when appropriate. */
425 maybe_intro_message();
426
427 setcursor();
428
Bram Moolenaar2951b772013-07-03 12:45:31 +0200429 return ret;
430}
431
432/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100433 * Invoked after an asynchronous callback is called.
434 * If an echo command was used the cursor needs to be put back where
435 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200436 * If "call_update_screen" is FALSE don't call update_screen() when at the
437 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 */
439 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200440redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100441{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200442 ++redrawing_for_callback;
443
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100444 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200445 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100446 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200447 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200448 // Don't redraw when in prompt_for_number().
449 if (cmdline_row > 0)
450 {
451 // Redrawing only works when the screen didn't scroll. Don't clear
452 // wildmenu entries.
453 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200454#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200456#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200457 && call_update_screen)
458 update_screen(0);
459
460 // Redraw in the same position, so that the user can continue
461 // editing the command.
462 redrawcmdline_ex(FALSE);
463 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200464 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200465 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100466 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200467 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200468 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100469 setcursor();
470 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100471 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100472#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100473 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200474 // Don't update the cursor when it is blinking and off to avoid
475 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100476 out_flush_cursor(FALSE, FALSE);
477 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100479 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200480
481 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482}
483
484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 * Changed something in the current window, at buffer line "lnum", that
486 * requires that line and possibly other lines to be redrawn.
487 * Used when entering/leaving Insert mode with the cursor on a folded line.
488 * Used to remove the "$" from a change command.
489 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
490 * may become invalid and the whole window will have to be redrawn.
491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100493redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200494 win_T *wp,
Bram Moolenaar05540972016-01-30 20:31:25 +0100495 linenr_T lnum,
496 int invalid UNUSED) /* window line height is invalid now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498#ifdef FEAT_FOLDING
499 int i;
500#endif
501
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
508#ifdef FEAT_FOLDING
509 if (invalid)
510 {
511 /* A w_lines[] entry for this lnum has become invalid. */
Bram Moolenaar90a99792018-09-12 21:52:18 +0200512 i = find_wl_entry(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (i >= 0)
Bram Moolenaar90a99792018-09-12 21:52:18 +0200514 wp->w_lines[i].wl_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 }
516#endif
517}
518
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200519 void
520reset_updating_screen(int may_resize_shell UNUSED)
521{
522 updating_screen = FALSE;
523#ifdef FEAT_GUI
524 if (may_resize_shell)
525 gui_may_resize_shell();
526#endif
527#ifdef FEAT_TERMINAL
528 term_check_channel_closed_recently();
529#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200530
531#ifdef HAVE_DROP_FILE
532 // If handle_drop() was called while updating_screen was TRUE need to
533 // handle the drop now.
534 handle_any_postponed_drop();
535#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200536}
537
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200539 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 */
541 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100542update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 redraw_curbuf_later(type);
545 update_screen(type);
546}
547
548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 * Based on the current value of curwin->w_topline, transfer a screenfull
550 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200551 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200553 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200556 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 win_T *wp;
558 static int did_intro = FALSE;
559#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
560 int did_one;
561#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200562#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200563 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200564 int gui_cursor_col;
565 int gui_cursor_row;
566#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200567 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000569 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200571 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200573 if (type == VALID_NO_UPDATE)
574 {
575 no_update = TRUE;
576 type = 0;
577 }
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (must_redraw)
580 {
581 if (type < must_redraw) /* use maximal type */
582 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000583
584 /* must_redraw is reset here, so that when we run into some weird
585 * reason to redraw while busy redrawing (e.g., asynchronous
586 * scrolling), or update_topline() in win_update() will cause a
587 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 must_redraw = 0;
589 }
590
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200591 /* May need to update w_lines[]. */
592 if (curwin->w_lines_valid == 0 && type < NOT_VALID
593#ifdef FEAT_TERMINAL
594 && !term_do_update_window(curwin)
595#endif
596 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 type = NOT_VALID;
598
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000599 /* Postpone the redrawing when it's not needed and when being called
600 * recursively. */
601 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
603 redraw_later(type); /* remember type for next time */
604 must_redraw = type;
605 if (type > INVERTED_ALL)
606 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
609
610 updating_screen = TRUE;
611#ifdef FEAT_SYN_HL
612 ++display_tick; /* let syntax code know we're in a next round of
613 * display updating */
614#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200615 if (no_update)
616 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
618 /*
619 * if the screen was scrolled up when displaying a message, scroll it down
620 */
621 if (msg_scrolled)
622 {
623 clear_cmdline = TRUE;
624 if (msg_scrolled > Rows - 5) /* clearing is faster */
625 type = CLEAR;
626 else if (type != CLEAR)
627 {
628 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200629 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
630 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 type = CLEAR;
632 FOR_ALL_WINDOWS(wp)
633 {
634 if (W_WINROW(wp) < msg_scrolled)
635 {
636 if (W_WINROW(wp) + wp->w_height > msg_scrolled
637 && wp->w_redr_type < REDRAW_TOP
638 && wp->w_lines_valid > 0
639 && wp->w_topline == wp->w_lines[0].wl_lnum)
640 {
641 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
642 wp->w_redr_type = REDRAW_TOP;
643 }
644 else
645 {
646 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200647 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
648 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 }
651 }
652 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200653 if (!no_update)
654 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000655 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 msg_scrolled = 0;
658 need_wait_return = FALSE;
659 }
660
661 /* reset cmdline_row now (may have been changed temporarily) */
662 compute_cmdrow();
663
664 /* Check for changed highlighting */
665 if (need_highlight_changed)
666 highlight_changed();
667
668 if (type == CLEAR) /* first clear screen */
669 {
670 screenclear(); /* will reset clear_cmdline */
671 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200672 /* must_redraw may be set indirectly, avoid another redraw later */
673 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
675
676 if (clear_cmdline) /* going to clear cmdline (done below) */
677 check_for_delay(FALSE);
678
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000679#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200680 /* Force redraw when width of 'number' or 'relativenumber' column
681 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000682 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200683 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
684 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000685 curwin->w_redr_type = NOT_VALID;
686#endif
687
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 /*
689 * Only start redrawing if there is really something to do.
690 */
691 if (type == INVERTED)
692 update_curswant();
693 if (curwin->w_redr_type < type
694 && !((type == VALID
695 && curwin->w_lines[0].wl_valid
696#ifdef FEAT_DIFF
697 && curwin->w_topfill == curwin->w_old_topfill
698 && curwin->w_botfill == curwin->w_old_botfill
699#endif
700 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000702 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
704 && curwin->w_old_visual_mode == VIsual_mode
705 && (curwin->w_valid & VALID_VIRTCOL)
706 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 ))
708 curwin->w_redr_type = type;
709
Bram Moolenaar5a305422006-04-28 22:38:25 +0000710 /* Redraw the tab pages line if needed. */
711 if (redraw_tabline || type >= NOT_VALID)
712 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#ifdef FEAT_SYN_HL
715 /*
716 * Correct stored syntax highlighting info for changes in each displayed
717 * buffer. Each buffer must only be done once.
718 */
719 FOR_ALL_WINDOWS(wp)
720 {
721 if (wp->w_buffer->b_mod_set)
722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 win_T *wwp;
724
725 /* Check if we already did this buffer. */
726 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
727 if (wwp->w_buffer == wp->w_buffer)
728 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200729 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 syn_stack_apply_changes(wp->w_buffer);
731 }
732 }
733#endif
734
735 /*
736 * Go from top to bottom through the windows, redrawing the ones that need
737 * it.
738 */
739#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
740 did_one = FALSE;
741#endif
742#ifdef FEAT_SEARCH_EXTRA
743 search_hl.rm.regprog = NULL;
744#endif
745 FOR_ALL_WINDOWS(wp)
746 {
747 if (wp->w_redr_type != 0)
748 {
749 cursor_off();
750#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
751 if (!did_one)
752 {
753 did_one = TRUE;
754# ifdef FEAT_SEARCH_EXTRA
755 start_search_hl();
756# endif
757# ifdef FEAT_CLIPBOARD
758 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200759 if (clip_star.available && clip_isautosel_star())
760 clip_update_selection(&clip_star);
761 if (clip_plus.available && clip_isautosel_plus())
762 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763# endif
764#ifdef FEAT_GUI
765 /* Remove the cursor before starting to do anything, because
766 * scrolling may make it difficult to redraw the text under
767 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200768 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200769 {
770 gui_cursor_col = gui.cursor_col;
771 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200773 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 }
777#endif
778 win_update(wp);
779 }
780
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 /* redraw status line after the window to minimize cursor movement */
782 if (wp->w_redr_status)
783 {
784 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200785 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788#if defined(FEAT_SEARCH_EXTRA)
789 end_search_hl();
790#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100791#ifdef FEAT_INS_EXPAND
792 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200793 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 /* Reset b_mod_set flags. Going through all windows is probably faster
797 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200798 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200801 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
803 /* Clear or redraw the command line. Done last, because scrolling may
804 * mess up the command line. */
805 if (clear_cmdline || redraw_cmdline)
806 showmode();
807
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200808 if (no_update)
809 --no_win_do_lines_ins;
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200812 if (!did_intro)
813 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 did_intro = TRUE;
815
816#ifdef FEAT_GUI
817 /* Redraw the cursor and update the scrollbars when all screen updating is
818 * done. */
819 if (gui.in_use)
820 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200821 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200822 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100823 mch_disable_flush();
824 out_flush(); /* required before updating the cursor */
825 mch_enable_flush();
826
Bram Moolenaar144445d2016-07-08 21:41:54 +0200827 /* Put the GUI position where the cursor was, gui_update_cursor()
828 * uses that. */
829 gui.col = gui_cursor_col;
830 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200831# ifdef FEAT_MBYTE
832 gui.col = mb_fix_col(gui.col, gui.row);
833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200836 screen_cur_col = gui.col;
837 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200838 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 else
840 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 gui_update_scrollbars(FALSE);
842 }
843#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200844 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100847#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
848/*
849 * Prepare for updating one or more windows.
850 * Caller must check for "updating_screen" already set to avoid recursiveness.
851 */
852 static void
853update_prepare(void)
854{
855 cursor_off();
856 updating_screen = TRUE;
857#ifdef FEAT_GUI
858 /* Remove the cursor before starting to do anything, because scrolling may
859 * make it difficult to redraw the text under it. */
860 if (gui.in_use)
861 gui_undraw_cursor();
862#endif
863#ifdef FEAT_SEARCH_EXTRA
864 start_search_hl();
865#endif
866}
867
868/*
869 * Finish updating one or more windows.
870 */
871 static void
872update_finish(void)
873{
874 if (redraw_cmdline)
875 showmode();
876
877# ifdef FEAT_SEARCH_EXTRA
878 end_search_hl();
879# endif
880
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200881 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100882
883# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884 /* Redraw the cursor and update the scrollbars when all screen updating is
885 * done. */
886 if (gui.in_use)
887 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100888 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100889 gui_update_scrollbars(FALSE);
890 }
891# endif
892}
893#endif
894
Bram Moolenaar860cae12010-06-05 23:22:07 +0200895#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200896/*
897 * Return TRUE if the cursor line in window "wp" may be concealed, according
898 * to the 'concealcursor' option.
899 */
900 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100901conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200902{
903 int c;
904
905 if (*wp->w_p_cocu == NUL)
906 return FALSE;
907 if (get_real_state() & VISUAL)
908 c = 'v';
909 else if (State & INSERT)
910 c = 'i';
911 else if (State & NORMAL)
912 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200913 else if (State & CMDLINE)
914 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200915 else
916 return FALSE;
917 return vim_strchr(wp->w_p_cocu, c) != NULL;
918}
919
920/*
921 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
922 */
923 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200924conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200925{
926 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
927 {
928 need_cursor_line_redraw = TRUE;
929 /* Need to recompute cursor column, e.g., when starting Visual mode
930 * without concealing. */
931 curs_columns(TRUE);
932 }
933}
934
Bram Moolenaar860cae12010-06-05 23:22:07 +0200935 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100936update_single_line(win_T *wp, linenr_T lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200937{
938 int row;
939 int j;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200940#ifdef SYN_TIME_LIMIT
941 proftime_T syntax_tm;
942#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200943
Bram Moolenaar908be432016-05-24 10:51:30 +0200944 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar070b33d2017-01-31 21:53:39 +0100945 if (!screen_valid(TRUE) || updating_screen)
Bram Moolenaar908be432016-05-24 10:51:30 +0200946 return;
947
Bram Moolenaar860cae12010-06-05 23:22:07 +0200948 if (lnum >= wp->w_topline && lnum < wp->w_botline
Bram Moolenaar370df582010-06-22 05:16:38 +0200949 && foldedCount(wp, lnum, &win_foldinfo) == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +0200950 {
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200951#ifdef SYN_TIME_LIMIT
952 /* Set the time limit to 'redrawtime'. */
953 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200954 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200955#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100956 update_prepare();
957
Bram Moolenaar860cae12010-06-05 23:22:07 +0200958 row = 0;
959 for (j = 0; j < wp->w_lines_valid; ++j)
960 {
961 if (lnum == wp->w_lines[j].wl_lnum)
962 {
963 screen_start(); /* not sure of screen cursor */
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +0200964# ifdef FEAT_SEARCH_EXTRA
965 init_search_hl(wp);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200966 prepare_search_hl(wp, lnum);
967# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200968 win_line(wp, lnum, row, row + wp->w_lines[j].wl_size,
969 FALSE, FALSE);
Bram Moolenaar860cae12010-06-05 23:22:07 +0200970 break;
971 }
972 row += wp->w_lines[j].wl_size;
973 }
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100974
975 update_finish();
Bram Moolenaarf3d769a2017-09-22 13:44:56 +0200976
977#ifdef SYN_TIME_LIMIT
978 syn_set_timeout(NULL);
979#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +0200980 }
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200981 need_cursor_line_redraw = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982}
983#endif
984
985#if defined(FEAT_SIGNS) || defined(PROTO)
986 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100987update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 win_T *wp;
990 int doit = FALSE;
991
992# ifdef FEAT_FOLDING
993 win_foldinfo.fi_level = 0;
994# endif
995
996 /* update/delete a specific mark */
997 FOR_ALL_WINDOWS(wp)
998 {
999 if (buf != NULL && lnum > 0)
1000 {
1001 if (wp->w_buffer == buf && lnum >= wp->w_topline
1002 && lnum < wp->w_botline)
1003 {
1004 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
1005 wp->w_redraw_top = lnum;
1006 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
1007 wp->w_redraw_bot = lnum;
1008 redraw_win_later(wp, VALID);
1009 }
1010 }
1011 else
1012 redraw_win_later(wp, VALID);
1013 if (wp->w_redr_type != 0)
1014 doit = TRUE;
1015 }
1016
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001017 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +02001018 * happening (recursive call), messages on the screen or still starting up.
1019 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001020 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +02001021 || State == ASKMORE || State == HITRETURN
1022 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +01001023#ifdef FEAT_GUI
1024 || gui.starting
1025#endif
1026 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 return;
1028
1029 /* update all windows that need updating */
1030 update_prepare();
1031
Bram Moolenaar29323592016-07-24 22:04:11 +02001032 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
1034 if (wp->w_redr_type != 0)
1035 win_update(wp);
1036 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001037 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040 update_finish();
1041}
1042#endif
1043
1044
1045#if defined(FEAT_GUI) || defined(PROTO)
1046/*
1047 * Update a single window, its status line and maybe the command line msg.
1048 * Used for the GUI scrollbar.
1049 */
1050 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001051updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001053 /* return if already busy updating */
1054 if (updating_screen)
1055 return;
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 update_prepare();
1058
1059#ifdef FEAT_CLIPBOARD
1060 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001061 if (clip_star.available && clip_isautosel_star())
1062 clip_update_selection(&clip_star);
1063 if (clip_plus.available && clip_isautosel_plus())
1064 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001068
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001069 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001070 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001071 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001072
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (wp->w_redr_status
1074# ifdef FEAT_CMDL_INFO
1075 || p_ru
1076# endif
1077# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001078 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079# endif
1080 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001081 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 update_finish();
1084}
1085#endif
1086
1087/*
1088 * Update a single window.
1089 *
1090 * This may cause the windows below it also to be redrawn (when clearing the
1091 * screen or scrolling lines).
1092 *
1093 * How the window is redrawn depends on wp->w_redr_type. Each type also
1094 * implies the one below it.
1095 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001096 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1098 * INVERTED redraw the changed part of the Visual area
1099 * INVERTED_ALL redraw the whole Visual area
1100 * VALID 1. scroll up/down to adjust for a changed w_topline
1101 * 2. update lines at the top when scrolled down
1102 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001103 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 * b_mod_top and b_mod_bot.
1105 * - if wp->w_redraw_top non-zero, redraw lines between
1106 * wp->w_redraw_top and wp->w_redr_bot.
1107 * - continue redrawing when syntax status is invalid.
1108 * 4. if scrolled up, update lines at the bottom.
1109 * This results in three areas that may need updating:
1110 * top: from first row to top_end (when scrolled down)
1111 * mid: from mid_start to mid_end (update inversion or changed text)
1112 * bot: from bot_start to last row (when scrolled up)
1113 */
1114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001115win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
1117 buf_T *buf = wp->w_buffer;
1118 int type;
1119 int top_end = 0; /* Below last row of the top area that needs
1120 updating. 0 when no top area updating. */
1121 int mid_start = 999;/* first row of the mid area that needs
1122 updating. 999 when no mid area updating. */
1123 int mid_end = 0; /* Below last row of the mid area that needs
1124 updating. 0 when no mid area updating. */
1125 int bot_start = 999;/* first row of the bot area that needs
1126 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 int scrolled_down = FALSE; /* TRUE when scrolled down when
1128 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001130 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 int top_to_mod = FALSE; /* redraw above mod_top */
1132#endif
1133
1134 int row; /* current window row to display */
1135 linenr_T lnum; /* current buffer lnum to display */
1136 int idx; /* current index in w_lines[] */
1137 int srow; /* starting row of the current line */
1138
1139 int eof = FALSE; /* if TRUE, we hit the end of the file */
1140 int didline = FALSE; /* if TRUE, we finished the last line */
1141 int i;
1142 long j;
1143 static int recursive = FALSE; /* being called recursively */
1144 int old_botline = wp->w_botline;
1145#ifdef FEAT_FOLDING
1146 long fold_count;
1147#endif
1148#ifdef FEAT_SYN_HL
1149 /* remember what happened to the previous line, to know if
1150 * check_visual_highlight() can be used */
1151#define DID_NONE 1 /* didn't update a line */
1152#define DID_LINE 2 /* updated a normal line */
1153#define DID_FOLD 3 /* updated a folded line */
1154 int did_update = DID_NONE;
1155 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1156#endif
1157 linenr_T mod_top = 0;
1158 linenr_T mod_bot = 0;
1159#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1160 int save_got_int;
1161#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001162#ifdef SYN_TIME_LIMIT
1163 proftime_T syntax_tm;
1164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166 type = wp->w_redr_type;
1167
1168 if (type == NOT_VALID)
1169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 wp->w_lines_valid = 0;
1172 }
1173
1174 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001175 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 {
1177 wp->w_redr_type = 0;
1178 return;
1179 }
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 /* Window is zero-width: Only need to draw the separator. */
1182 if (wp->w_width == 0)
1183 {
1184 /* draw the vertical separator right of this window */
1185 draw_vsep_win(wp, 0);
1186 wp->w_redr_type = 0;
1187 return;
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001190#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001191 // If this window contains a terminal, redraw works completely differently.
1192 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001193 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001194 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001195# ifdef FEAT_MENU
1196 /* Draw the window toolbar, if there is one. */
1197 if (winbar_height(wp) > 0)
1198 redraw_win_toolbar(wp);
1199# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001200 wp->w_redr_type = 0;
1201 return;
1202 }
1203#endif
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001206 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001209#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001210 /* Force redraw when width of 'number' or 'relativenumber' column
1211 * changes. */
1212 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001213 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001214 {
1215 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001216 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001217 }
1218 else
1219#endif
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1222 {
1223 /*
1224 * When there are both inserted/deleted lines and specific lines to be
1225 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1226 * everything (only happens when redrawing is off for while).
1227 */
1228 type = NOT_VALID;
1229 }
1230 else
1231 {
1232 /*
1233 * Set mod_top to the first line that needs displaying because of
1234 * changes. Set mod_bot to the first line after the changes.
1235 */
1236 mod_top = wp->w_redraw_top;
1237 if (wp->w_redraw_bot != 0)
1238 mod_bot = wp->w_redraw_bot + 1;
1239 else
1240 mod_bot = 0;
1241 wp->w_redraw_top = 0; /* reset for next time */
1242 wp->w_redraw_bot = 0;
1243 if (buf->b_mod_set)
1244 {
1245 if (mod_top == 0 || mod_top > buf->b_mod_top)
1246 {
1247 mod_top = buf->b_mod_top;
1248#ifdef FEAT_SYN_HL
1249 /* Need to redraw lines above the change that may be included
1250 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001253 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (mod_top < 1)
1255 mod_top = 1;
1256 }
1257#endif
1258 }
1259 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1260 mod_bot = buf->b_mod_bot;
1261
1262#ifdef FEAT_SEARCH_EXTRA
1263 /* When 'hlsearch' is on and using a multi-line search pattern, a
1264 * change in one line may make the Search highlighting in a
1265 * previous line invalid. Simple solution: redraw all visible
1266 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001267 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001269 if (search_hl.rm.regprog != NULL
1270 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001272 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001273 {
1274 cur = wp->w_match_head;
1275 while (cur != NULL)
1276 {
1277 if (cur->match.regprog != NULL
1278 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001279 {
1280 top_to_mod = TRUE;
1281 break;
1282 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001283 cur = cur->next;
1284 }
1285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#endif
1287 }
1288#ifdef FEAT_FOLDING
1289 if (mod_top != 0 && hasAnyFolding(wp))
1290 {
1291 linenr_T lnumt, lnumb;
1292
1293 /*
1294 * A change in a line can cause lines above it to become folded or
1295 * unfolded. Find the top most buffer line that may be affected.
1296 * If the line was previously folded and displayed, get the first
1297 * line of that fold. If the line is folded now, get the first
1298 * folded line. Use the minimum of these two.
1299 */
1300
1301 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1302 * the line below it. If there is no valid entry, use w_topline.
1303 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1304 * to this line. If there is no valid entry, use MAXLNUM. */
1305 lnumt = wp->w_topline;
1306 lnumb = MAXLNUM;
1307 for (i = 0; i < wp->w_lines_valid; ++i)
1308 if (wp->w_lines[i].wl_valid)
1309 {
1310 if (wp->w_lines[i].wl_lastlnum < mod_top)
1311 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1312 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1313 {
1314 lnumb = wp->w_lines[i].wl_lnum;
1315 /* When there is a fold column it might need updating
1316 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001317 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 ++lnumb;
1319 }
1320 }
1321
1322 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1323 if (mod_top > lnumt)
1324 mod_top = lnumt;
1325
1326 /* Now do the same for the bottom line (one above mod_bot). */
1327 --mod_bot;
1328 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1329 ++mod_bot;
1330 if (mod_bot < lnumb)
1331 mod_bot = lnumb;
1332 }
1333#endif
1334
1335 /* When a change starts above w_topline and the end is below
1336 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001337 * If the end of the change is above w_topline: do like no change was
1338 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (mod_top != 0 && mod_top < wp->w_topline)
1340 {
1341 if (mod_bot > wp->w_topline)
1342 mod_top = wp->w_topline;
1343#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001344 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 top_end = 1;
1346#endif
1347 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001348
1349 /* When line numbers are displayed need to redraw all lines below
1350 * inserted/deleted lines. */
1351 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1352 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
1355 /*
1356 * When only displaying the lines at the top, set top_end. Used when
1357 * window has scrolled down for msg_scrolled.
1358 */
1359 if (type == REDRAW_TOP)
1360 {
1361 j = 0;
1362 for (i = 0; i < wp->w_lines_valid; ++i)
1363 {
1364 j += wp->w_lines[i].wl_size;
1365 if (j >= wp->w_upd_rows)
1366 {
1367 top_end = j;
1368 break;
1369 }
1370 }
1371 if (top_end == 0)
1372 /* not found (cannot happen?): redraw everything */
1373 type = NOT_VALID;
1374 else
1375 /* top area defined, the rest is VALID */
1376 type = VALID;
1377 }
1378
Bram Moolenaar367329b2007-08-30 11:53:22 +00001379 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001380 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1381 * non-zero and thus not FALSE) will indicate that screenclear() was not
1382 * called. */
1383 if (screen_cleared)
1384 screen_cleared = MAYBE;
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 /*
1387 * If there are no changes on the screen that require a complete redraw,
1388 * handle three cases:
1389 * 1: we are off the top of the screen by a few lines: scroll down
1390 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1391 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1392 * w_lines[] that needs updating.
1393 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001394 if ((type == VALID || type == SOME_VALID
1395 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#ifdef FEAT_DIFF
1397 && !wp->w_botfill && !wp->w_old_botfill
1398#endif
1399 )
1400 {
1401 if (mod_top != 0 && wp->w_topline == mod_top)
1402 {
1403 /*
1404 * w_topline is the first changed line, the scrolling will be done
1405 * further down.
1406 */
1407 }
1408 else if (wp->w_lines[0].wl_valid
1409 && (wp->w_topline < wp->w_lines[0].wl_lnum
1410#ifdef FEAT_DIFF
1411 || (wp->w_topline == wp->w_lines[0].wl_lnum
1412 && wp->w_topfill > wp->w_old_topfill)
1413#endif
1414 ))
1415 {
1416 /*
1417 * New topline is above old topline: May scroll down.
1418 */
1419#ifdef FEAT_FOLDING
1420 if (hasAnyFolding(wp))
1421 {
1422 linenr_T ln;
1423
1424 /* count the number of lines we are off, counting a sequence
1425 * of folded lines as one */
1426 j = 0;
1427 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1428 {
1429 ++j;
1430 if (j >= wp->w_height - 2)
1431 break;
1432 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1433 }
1434 }
1435 else
1436#endif
1437 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1438 if (j < wp->w_height - 2) /* not too far off */
1439 {
1440 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1441#ifdef FEAT_DIFF
1442 /* insert extra lines for previously invisible filler lines */
1443 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1444 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1445 - wp->w_old_topfill;
1446#endif
1447 if (i < wp->w_height - 2) /* less than a screen off */
1448 {
1449 /*
1450 * Try to insert the correct number of lines.
1451 * If not the last window, delete the lines at the bottom.
1452 * win_ins_lines may fail when the terminal can't do it.
1453 */
1454 if (i > 0)
1455 check_for_delay(FALSE);
1456 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1457 {
1458 if (wp->w_lines_valid != 0)
1459 {
1460 /* Need to update rows that are new, stop at the
1461 * first one that scrolled down. */
1462 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464
1465 /* Move the entries that were scrolled, disable
1466 * the entries for the lines to be redrawn. */
1467 if ((wp->w_lines_valid += j) > wp->w_height)
1468 wp->w_lines_valid = wp->w_height;
1469 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1470 wp->w_lines[idx] = wp->w_lines[idx - j];
1471 while (idx >= 0)
1472 wp->w_lines[idx--].wl_valid = FALSE;
1473 }
1474 }
1475 else
1476 mid_start = 0; /* redraw all lines */
1477 }
1478 else
1479 mid_start = 0; /* redraw all lines */
1480 }
1481 else
1482 mid_start = 0; /* redraw all lines */
1483 }
1484 else
1485 {
1486 /*
1487 * New topline is at or below old topline: May scroll up.
1488 * When topline didn't change, find first entry in w_lines[] that
1489 * needs updating.
1490 */
1491
1492 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1493 j = -1;
1494 row = 0;
1495 for (i = 0; i < wp->w_lines_valid; i++)
1496 {
1497 if (wp->w_lines[i].wl_valid
1498 && wp->w_lines[i].wl_lnum == wp->w_topline)
1499 {
1500 j = i;
1501 break;
1502 }
1503 row += wp->w_lines[i].wl_size;
1504 }
1505 if (j == -1)
1506 {
1507 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1508 * lines */
1509 mid_start = 0;
1510 }
1511 else
1512 {
1513 /*
1514 * Try to delete the correct number of lines.
1515 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1516 */
1517#ifdef FEAT_DIFF
1518 /* If the topline didn't change, delete old filler lines,
1519 * otherwise delete filler lines of the new topline... */
1520 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1521 row += wp->w_old_topfill;
1522 else
1523 row += diff_check_fill(wp, wp->w_topline);
1524 /* ... but don't delete new filler lines. */
1525 row -= wp->w_topfill;
1526#endif
1527 if (row > 0)
1528 {
1529 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001530 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1531 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 bot_start = wp->w_height - row;
1533 else
1534 mid_start = 0; /* redraw all lines */
1535 }
1536 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1537 {
1538 /*
1539 * Skip the lines (below the deleted lines) that are still
1540 * valid and don't need redrawing. Copy their info
1541 * upwards, to compensate for the deleted lines. Set
1542 * bot_start to the first row that needs redrawing.
1543 */
1544 bot_start = 0;
1545 idx = 0;
1546 for (;;)
1547 {
1548 wp->w_lines[idx] = wp->w_lines[j];
1549 /* stop at line that didn't fit, unless it is still
1550 * valid (no lines deleted) */
1551 if (row > 0 && bot_start + row
1552 + (int)wp->w_lines[j].wl_size > wp->w_height)
1553 {
1554 wp->w_lines_valid = idx + 1;
1555 break;
1556 }
1557 bot_start += wp->w_lines[idx++].wl_size;
1558
1559 /* stop at the last valid entry in w_lines[].wl_size */
1560 if (++j >= wp->w_lines_valid)
1561 {
1562 wp->w_lines_valid = idx;
1563 break;
1564 }
1565 }
1566#ifdef FEAT_DIFF
1567 /* Correct the first entry for filler lines at the top
1568 * when it won't get updated below. */
1569 if (wp->w_p_diff && bot_start > 0)
1570 wp->w_lines[0].wl_size =
1571 plines_win_nofill(wp, wp->w_topline, TRUE)
1572 + wp->w_topfill;
1573#endif
1574 }
1575 }
1576 }
1577
1578 /* When starting redraw in the first line, redraw all lines. When
1579 * there is only one window it's probably faster to clear the screen
1580 * first. */
1581 if (mid_start == 0)
1582 {
1583 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001584 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001585 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001586 /* Clear the screen when it was not done by win_del_lines() or
1587 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1588 * then. */
1589 if (screen_cleared != TRUE)
1590 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001591 /* The screen was cleared, redraw the tab pages line. */
1592 if (redraw_tabline)
1593 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001596
1597 /* When win_del_lines() or win_ins_lines() caused the screen to be
1598 * cleared (only happens for the first window) or when screenclear()
1599 * was called directly above, "must_redraw" will have been set to
1600 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1601 if (screen_cleared == TRUE)
1602 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 else
1605 {
1606 /* Not VALID or INVERTED: redraw all lines. */
1607 mid_start = 0;
1608 mid_end = wp->w_height;
1609 }
1610
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001611 if (type == SOME_VALID)
1612 {
1613 /* SOME_VALID: redraw all lines. */
1614 mid_start = 0;
1615 mid_end = wp->w_height;
1616 type = NOT_VALID;
1617 }
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 /* check if we are updating or removing the inverted part */
1620 if ((VIsual_active && buf == curwin->w_buffer)
1621 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1622 {
1623 linenr_T from, to;
1624
1625 if (VIsual_active)
1626 {
1627 if (VIsual_active
1628 && (VIsual_mode != wp->w_old_visual_mode
1629 || type == INVERTED_ALL))
1630 {
1631 /*
1632 * If the type of Visual selection changed, redraw the whole
1633 * selection. Also when the ownership of the X selection is
1634 * gained or lost.
1635 */
1636 if (curwin->w_cursor.lnum < VIsual.lnum)
1637 {
1638 from = curwin->w_cursor.lnum;
1639 to = VIsual.lnum;
1640 }
1641 else
1642 {
1643 from = VIsual.lnum;
1644 to = curwin->w_cursor.lnum;
1645 }
1646 /* redraw more when the cursor moved as well */
1647 if (wp->w_old_cursor_lnum < from)
1648 from = wp->w_old_cursor_lnum;
1649 if (wp->w_old_cursor_lnum > to)
1650 to = wp->w_old_cursor_lnum;
1651 if (wp->w_old_visual_lnum < from)
1652 from = wp->w_old_visual_lnum;
1653 if (wp->w_old_visual_lnum > to)
1654 to = wp->w_old_visual_lnum;
1655 }
1656 else
1657 {
1658 /*
1659 * Find the line numbers that need to be updated: The lines
1660 * between the old cursor position and the current cursor
1661 * position. Also check if the Visual position changed.
1662 */
1663 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1664 {
1665 from = curwin->w_cursor.lnum;
1666 to = wp->w_old_cursor_lnum;
1667 }
1668 else
1669 {
1670 from = wp->w_old_cursor_lnum;
1671 to = curwin->w_cursor.lnum;
1672 if (from == 0) /* Visual mode just started */
1673 from = to;
1674 }
1675
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001676 if (VIsual.lnum != wp->w_old_visual_lnum
1677 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 if (wp->w_old_visual_lnum < from
1680 && wp->w_old_visual_lnum != 0)
1681 from = wp->w_old_visual_lnum;
1682 if (wp->w_old_visual_lnum > to)
1683 to = wp->w_old_visual_lnum;
1684 if (VIsual.lnum < from)
1685 from = VIsual.lnum;
1686 if (VIsual.lnum > to)
1687 to = VIsual.lnum;
1688 }
1689 }
1690
1691 /*
1692 * If in block mode and changed column or curwin->w_curswant:
1693 * update all lines.
1694 * First compute the actual start and end column.
1695 */
1696 if (VIsual_mode == Ctrl_V)
1697 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001698 colnr_T fromc, toc;
1699#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1700 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar404406a2014-10-09 13:24:43 +02001702 if (curwin->w_p_lbr)
1703 ve_flags = VE_ALL;
1704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001706#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1707 ve_flags = save_ve_flags;
1708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 ++toc;
1710 if (curwin->w_curswant == MAXCOL)
1711 toc = MAXCOL;
1712
1713 if (fromc != wp->w_old_cursor_fcol
1714 || toc != wp->w_old_cursor_lcol)
1715 {
1716 if (from > VIsual.lnum)
1717 from = VIsual.lnum;
1718 if (to < VIsual.lnum)
1719 to = VIsual.lnum;
1720 }
1721 wp->w_old_cursor_fcol = fromc;
1722 wp->w_old_cursor_lcol = toc;
1723 }
1724 }
1725 else
1726 {
1727 /* Use the line numbers of the old Visual area. */
1728 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1729 {
1730 from = wp->w_old_cursor_lnum;
1731 to = wp->w_old_visual_lnum;
1732 }
1733 else
1734 {
1735 from = wp->w_old_visual_lnum;
1736 to = wp->w_old_cursor_lnum;
1737 }
1738 }
1739
1740 /*
1741 * There is no need to update lines above the top of the window.
1742 */
1743 if (from < wp->w_topline)
1744 from = wp->w_topline;
1745
1746 /*
1747 * If we know the value of w_botline, use it to restrict the update to
1748 * the lines that are visible in the window.
1749 */
1750 if (wp->w_valid & VALID_BOTLINE)
1751 {
1752 if (from >= wp->w_botline)
1753 from = wp->w_botline - 1;
1754 if (to >= wp->w_botline)
1755 to = wp->w_botline - 1;
1756 }
1757
1758 /*
1759 * Find the minimal part to be updated.
1760 * Watch out for scrolling that made entries in w_lines[] invalid.
1761 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1762 * top_end; need to redraw from top_end to the "to" line.
1763 * A middle mouse click with a Visual selection may change the text
1764 * above the Visual area and reset wl_valid, do count these for
1765 * mid_end (in srow).
1766 */
1767 if (mid_start > 0)
1768 {
1769 lnum = wp->w_topline;
1770 idx = 0;
1771 srow = 0;
1772 if (scrolled_down)
1773 mid_start = top_end;
1774 else
1775 mid_start = 0;
1776 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1777 {
1778 if (wp->w_lines[idx].wl_valid)
1779 mid_start += wp->w_lines[idx].wl_size;
1780 else if (!scrolled_down)
1781 srow += wp->w_lines[idx].wl_size;
1782 ++idx;
1783# ifdef FEAT_FOLDING
1784 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1785 lnum = wp->w_lines[idx].wl_lnum;
1786 else
1787# endif
1788 ++lnum;
1789 }
1790 srow += mid_start;
1791 mid_end = wp->w_height;
1792 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1793 {
1794 if (wp->w_lines[idx].wl_valid
1795 && wp->w_lines[idx].wl_lnum >= to + 1)
1796 {
1797 /* Only update until first row of this line */
1798 mid_end = srow;
1799 break;
1800 }
1801 srow += wp->w_lines[idx].wl_size;
1802 }
1803 }
1804 }
1805
1806 if (VIsual_active && buf == curwin->w_buffer)
1807 {
1808 wp->w_old_visual_mode = VIsual_mode;
1809 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1810 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001811 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 wp->w_old_curswant = curwin->w_curswant;
1813 }
1814 else
1815 {
1816 wp->w_old_visual_mode = 0;
1817 wp->w_old_cursor_lnum = 0;
1818 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001819 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1823 /* reset got_int, otherwise regexp won't work */
1824 save_got_int = got_int;
1825 got_int = 0;
1826#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001827#ifdef SYN_TIME_LIMIT
1828 /* Set the time limit to 'redrawtime'. */
1829 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001830 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#ifdef FEAT_FOLDING
1833 win_foldinfo.fi_level = 0;
1834#endif
1835
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001836#ifdef FEAT_MENU
1837 /*
1838 * Draw the window toolbar, if there is one.
1839 * TODO: only when needed.
1840 */
1841 if (winbar_height(wp) > 0)
1842 redraw_win_toolbar(wp);
1843#endif
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 /*
1846 * Update all the window rows.
1847 */
1848 idx = 0; /* first entry in w_lines[].wl_size */
1849 row = 0;
1850 srow = 0;
1851 lnum = wp->w_topline; /* first line shown in window */
1852 for (;;)
1853 {
1854 /* stop updating when reached the end of the window (check for _past_
1855 * the end of the window is at the end of the loop) */
1856 if (row == wp->w_height)
1857 {
1858 didline = TRUE;
1859 break;
1860 }
1861
1862 /* stop updating when hit the end of the file */
1863 if (lnum > buf->b_ml.ml_line_count)
1864 {
1865 eof = TRUE;
1866 break;
1867 }
1868
1869 /* Remember the starting row of the line that is going to be dealt
1870 * with. It is used further down when the line doesn't fit. */
1871 srow = row;
1872
1873 /*
1874 * Update a line when it is in an area that needs updating, when it
1875 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001876 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 * win_del_lines(), check if the current line includes it.
1878 * When syntax folding is being used, the saved syntax states will
1879 * already have been updated, we can't see where the syntax state is
1880 * the same again, just update until the end of the window.
1881 */
1882 if (row < top_end
1883 || (row >= mid_start && row < mid_end)
1884#ifdef FEAT_SEARCH_EXTRA
1885 || top_to_mod
1886#endif
1887 || idx >= wp->w_lines_valid
1888 || (row + wp->w_lines[idx].wl_size > bot_start)
1889 || (mod_top != 0
1890 && (lnum == mod_top
1891 || (lnum >= mod_top
1892 && (lnum < mod_bot
1893#ifdef FEAT_SYN_HL
1894 || did_update == DID_FOLD
1895 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001896 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 && (
1898# ifdef FEAT_FOLDING
1899 (foldmethodIsSyntax(wp)
1900 && hasAnyFolding(wp)) ||
1901# endif
1902 syntax_check_changed(lnum)))
1903#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001904#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001905 /* match in fixed position might need redraw
1906 * if lines were inserted or deleted */
1907 || (wp->w_match_head != NULL
1908 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 )))))
1911 {
1912#ifdef FEAT_SEARCH_EXTRA
1913 if (lnum == mod_top)
1914 top_to_mod = FALSE;
1915#endif
1916
1917 /*
1918 * When at start of changed lines: May scroll following lines
1919 * up or down to minimize redrawing.
1920 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001921 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 */
1923 if (lnum == mod_top
1924 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001925 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 int old_rows = 0;
1928 int new_rows = 0;
1929 int xtra_rows;
1930 linenr_T l;
1931
1932 /* Count the old number of window rows, using w_lines[], which
1933 * should still contain the sizes for the lines as they are
1934 * currently displayed. */
1935 for (i = idx; i < wp->w_lines_valid; ++i)
1936 {
1937 /* Only valid lines have a meaningful wl_lnum. Invalid
1938 * lines are part of the changed area. */
1939 if (wp->w_lines[i].wl_valid
1940 && wp->w_lines[i].wl_lnum == mod_bot)
1941 break;
1942 old_rows += wp->w_lines[i].wl_size;
1943#ifdef FEAT_FOLDING
1944 if (wp->w_lines[i].wl_valid
1945 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1946 {
1947 /* Must have found the last valid entry above mod_bot.
1948 * Add following invalid entries. */
1949 ++i;
1950 while (i < wp->w_lines_valid
1951 && !wp->w_lines[i].wl_valid)
1952 old_rows += wp->w_lines[i++].wl_size;
1953 break;
1954 }
1955#endif
1956 }
1957
1958 if (i >= wp->w_lines_valid)
1959 {
1960 /* We can't find a valid line below the changed lines,
1961 * need to redraw until the end of the window.
1962 * Inserting/deleting lines has no use. */
1963 bot_start = 0;
1964 }
1965 else
1966 {
1967 /* Able to count old number of rows: Count new window
1968 * rows, and may insert/delete lines */
1969 j = idx;
1970 for (l = lnum; l < mod_bot; ++l)
1971 {
1972#ifdef FEAT_FOLDING
1973 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1974 ++new_rows;
1975 else
1976#endif
1977#ifdef FEAT_DIFF
1978 if (l == wp->w_topline)
1979 new_rows += plines_win_nofill(wp, l, TRUE)
1980 + wp->w_topfill;
1981 else
1982#endif
1983 new_rows += plines_win(wp, l, TRUE);
1984 ++j;
1985 if (new_rows > wp->w_height - row - 2)
1986 {
1987 /* it's getting too much, must redraw the rest */
1988 new_rows = 9999;
1989 break;
1990 }
1991 }
1992 xtra_rows = new_rows - old_rows;
1993 if (xtra_rows < 0)
1994 {
1995 /* May scroll text up. If there is not enough
1996 * remaining text or scrolling fails, must redraw the
1997 * rest. If scrolling works, must redraw the text
1998 * below the scrolled text. */
1999 if (row - xtra_rows >= wp->w_height - 2)
2000 mod_bot = MAXLNUM;
2001 else
2002 {
2003 check_for_delay(FALSE);
2004 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002005 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 mod_bot = MAXLNUM;
2007 else
2008 bot_start = wp->w_height + xtra_rows;
2009 }
2010 }
2011 else if (xtra_rows > 0)
2012 {
2013 /* May scroll text down. If there is not enough
2014 * remaining text of scrolling fails, must redraw the
2015 * rest. */
2016 if (row + xtra_rows >= wp->w_height - 2)
2017 mod_bot = MAXLNUM;
2018 else
2019 {
2020 check_for_delay(FALSE);
2021 if (win_ins_lines(wp, row + old_rows,
2022 xtra_rows, FALSE, FALSE) == FAIL)
2023 mod_bot = MAXLNUM;
2024 else if (top_end > row + old_rows)
2025 /* Scrolled the part at the top that requires
2026 * updating down. */
2027 top_end += xtra_rows;
2028 }
2029 }
2030
2031 /* When not updating the rest, may need to move w_lines[]
2032 * entries. */
2033 if (mod_bot != MAXLNUM && i != j)
2034 {
2035 if (j < i)
2036 {
2037 int x = row + new_rows;
2038
2039 /* move entries in w_lines[] upwards */
2040 for (;;)
2041 {
2042 /* stop at last valid entry in w_lines[] */
2043 if (i >= wp->w_lines_valid)
2044 {
2045 wp->w_lines_valid = j;
2046 break;
2047 }
2048 wp->w_lines[j] = wp->w_lines[i];
2049 /* stop at a line that won't fit */
2050 if (x + (int)wp->w_lines[j].wl_size
2051 > wp->w_height)
2052 {
2053 wp->w_lines_valid = j + 1;
2054 break;
2055 }
2056 x += wp->w_lines[j++].wl_size;
2057 ++i;
2058 }
2059 if (bot_start > x)
2060 bot_start = x;
2061 }
2062 else /* j > i */
2063 {
2064 /* move entries in w_lines[] downwards */
2065 j -= i;
2066 wp->w_lines_valid += j;
2067 if (wp->w_lines_valid > wp->w_height)
2068 wp->w_lines_valid = wp->w_height;
2069 for (i = wp->w_lines_valid; i - j >= idx; --i)
2070 wp->w_lines[i] = wp->w_lines[i - j];
2071
2072 /* The w_lines[] entries for inserted lines are
2073 * now invalid, but wl_size may be used above.
2074 * Reset to zero. */
2075 while (i >= idx)
2076 {
2077 wp->w_lines[i].wl_size = 0;
2078 wp->w_lines[i--].wl_valid = FALSE;
2079 }
2080 }
2081 }
2082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084
2085#ifdef FEAT_FOLDING
2086 /*
2087 * When lines are folded, display one line for all of them.
2088 * Otherwise, display normally (can be several display lines when
2089 * 'wrap' is on).
2090 */
2091 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2092 if (fold_count != 0)
2093 {
2094 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2095 ++row;
2096 --fold_count;
2097 wp->w_lines[idx].wl_folded = TRUE;
2098 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2099# ifdef FEAT_SYN_HL
2100 did_update = DID_FOLD;
2101# endif
2102 }
2103 else
2104#endif
2105 if (idx < wp->w_lines_valid
2106 && wp->w_lines[idx].wl_valid
2107 && wp->w_lines[idx].wl_lnum == lnum
2108 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002109 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 && srow + wp->w_lines[idx].wl_size > wp->w_height
2111#ifdef FEAT_DIFF
2112 && diff_check_fill(wp, lnum) == 0
2113#endif
2114 )
2115 {
2116 /* This line is not going to fit. Don't draw anything here,
2117 * will draw "@ " lines below. */
2118 row = wp->w_height + 1;
2119 }
2120 else
2121 {
2122#ifdef FEAT_SEARCH_EXTRA
2123 prepare_search_hl(wp, lnum);
2124#endif
2125#ifdef FEAT_SYN_HL
2126 /* Let the syntax stuff know we skipped a few lines. */
2127 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002128 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 syntax_end_parsing(syntax_last_parsed + 1);
2130#endif
2131
2132 /*
2133 * Display one line.
2134 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002135 row = win_line(wp, lnum, srow, wp->w_height,
2136 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138#ifdef FEAT_FOLDING
2139 wp->w_lines[idx].wl_folded = FALSE;
2140 wp->w_lines[idx].wl_lastlnum = lnum;
2141#endif
2142#ifdef FEAT_SYN_HL
2143 did_update = DID_LINE;
2144 syntax_last_parsed = lnum;
2145#endif
2146 }
2147
2148 wp->w_lines[idx].wl_lnum = lnum;
2149 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002150
2151 /* Past end of the window or end of the screen. Note that after
2152 * resizing wp->w_height may be end up too big. That's a problem
2153 * elsewhere, but prevent a crash here. */
2154 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002157 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2159 ++idx;
2160 break;
2161 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002162 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 wp->w_lines[idx].wl_size = row - srow;
2164 ++idx;
2165#ifdef FEAT_FOLDING
2166 lnum += fold_count + 1;
2167#else
2168 ++lnum;
2169#endif
2170 }
2171 else
2172 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002173 if (wp->w_p_rnu)
2174 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002175#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002176 // 'relativenumber' set: The text doesn't need to be drawn, but
2177 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002178 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2179 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002180 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002181 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002182#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002183 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002184 }
2185
2186 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 row += wp->w_lines[idx++].wl_size;
2188 if (row > wp->w_height) /* past end of screen */
2189 break;
2190#ifdef FEAT_FOLDING
2191 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2192#else
2193 ++lnum;
2194#endif
2195#ifdef FEAT_SYN_HL
2196 did_update = DID_NONE;
2197#endif
2198 }
2199
2200 if (lnum > buf->b_ml.ml_line_count)
2201 {
2202 eof = TRUE;
2203 break;
2204 }
2205 }
2206 /*
2207 * End of loop over all window lines.
2208 */
2209
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002210#ifdef FEAT_VTP
2211 /* Rewrite the character at the end of the screen line. */
2212 if (use_vtp())
2213 {
2214 int i;
2215
2216 for (i = 0; i < Rows; ++i)
2217# ifdef FEAT_MBYTE
2218 if (enc_utf8)
2219 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2220 LineOffset[i] + screen_Columns) > 1)
2221 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2222 else
2223 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2224 else
2225# endif
2226 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2227 }
2228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 if (idx > wp->w_lines_valid)
2231 wp->w_lines_valid = idx;
2232
2233#ifdef FEAT_SYN_HL
2234 /*
2235 * Let the syntax stuff know we stop parsing here.
2236 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002237 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 syntax_end_parsing(syntax_last_parsed + 1);
2239#endif
2240
2241 /*
2242 * If we didn't hit the end of the file, and we didn't finish the last
2243 * line we were working on, then the line didn't fit.
2244 */
2245 wp->w_empty_rows = 0;
2246#ifdef FEAT_DIFF
2247 wp->w_filler_rows = 0;
2248#endif
2249 if (!eof && !didline)
2250 {
2251 if (lnum == wp->w_topline)
2252 {
2253 /*
2254 * Single line that does not fit!
2255 * Don't overwrite it, it can be edited.
2256 */
2257 wp->w_botline = lnum + 1;
2258 }
2259#ifdef FEAT_DIFF
2260 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2261 {
2262 /* Window ends in filler lines. */
2263 wp->w_botline = lnum;
2264 wp->w_filler_rows = wp->w_height - srow;
2265 }
2266#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002267 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2268 {
2269 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2270
2271 /*
2272 * Last line isn't finished: Display "@@@" in the last screen line.
2273 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002274 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002275 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002276 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002277 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002278 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002279 set_empty_rows(wp, srow);
2280 wp->w_botline = lnum;
2281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2283 {
2284 /*
2285 * Last line isn't finished: Display "@@@" at the end.
2286 */
2287 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2288 W_WINROW(wp) + wp->w_height,
2289 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002290 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 set_empty_rows(wp, srow);
2292 wp->w_botline = lnum;
2293 }
2294 else
2295 {
2296 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2297 wp->w_botline = lnum;
2298 }
2299 }
2300 else
2301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (eof) /* we hit the end of the file */
2304 {
2305 wp->w_botline = buf->b_ml.ml_line_count + 1;
2306#ifdef FEAT_DIFF
2307 j = diff_check_fill(wp, wp->w_botline);
2308 if (j > 0 && !wp->w_botfill)
2309 {
2310 /*
2311 * Display filler lines at the end of the file
2312 */
2313 if (char2cells(fill_diff) > 1)
2314 i = '-';
2315 else
2316 i = fill_diff;
2317 if (row + j > wp->w_height)
2318 j = wp->w_height - row;
2319 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2320 row += j;
2321 }
2322#endif
2323 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002324 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 wp->w_botline = lnum;
2326
2327 /* make sure the rest of the screen is blank */
2328 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002329 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002332#ifdef SYN_TIME_LIMIT
2333 syn_set_timeout(NULL);
2334#endif
2335
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 /* Reset the type of redrawing required, the window has been updated. */
2337 wp->w_redr_type = 0;
2338#ifdef FEAT_DIFF
2339 wp->w_old_topfill = wp->w_topfill;
2340 wp->w_old_botfill = wp->w_botfill;
2341#endif
2342
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002343 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 /*
2346 * There is a trick with w_botline. If we invalidate it on each
2347 * change that might modify it, this will cause a lot of expensive
2348 * calls to plines() in update_topline() each time. Therefore the
2349 * value of w_botline is often approximated, and this value is used to
2350 * compute the value of w_topline. If the value of w_botline was
2351 * wrong, check that the value of w_topline is correct (cursor is on
2352 * the visible part of the text). If it's not, we need to redraw
2353 * again. Mostly this just means scrolling up a few lines, so it
2354 * doesn't look too bad. Only do this for the current window (where
2355 * changes are relevant).
2356 */
2357 wp->w_valid |= VALID_BOTLINE;
2358 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2359 {
2360 recursive = TRUE;
2361 curwin->w_valid &= ~VALID_TOPLINE;
2362 update_topline(); /* may invalidate w_botline again */
2363 if (must_redraw != 0)
2364 {
2365 /* Don't update for changes in buffer again. */
2366 i = curbuf->b_mod_set;
2367 curbuf->b_mod_set = FALSE;
2368 win_update(curwin);
2369 must_redraw = 0;
2370 curbuf->b_mod_set = i;
2371 }
2372 recursive = FALSE;
2373 }
2374 }
2375
2376#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2377 /* restore got_int, unless CTRL-C was hit while redrawing */
2378 if (!got_int)
2379 got_int = save_got_int;
2380#endif
2381}
2382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383/*
2384 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2385 * as the filler character.
2386 */
2387 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002388win_draw_end(
2389 win_T *wp,
2390 int c1,
2391 int c2,
2392 int row,
2393 int endrow,
2394 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2397 int n = 0;
2398# define FDC_OFF n
2399#else
2400# define FDC_OFF 0
2401#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002402#ifdef FEAT_FOLDING
2403 int fdc = compute_foldcolumn(wp, 0);
2404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406#ifdef FEAT_RIGHTLEFT
2407 if (wp->w_p_rl)
2408 {
2409 /* No check for cmdline window: should never be right-left. */
2410# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002411 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 if (n > 0)
2414 {
2415 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002416 if (n > wp->w_width)
2417 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2419 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002420 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422# endif
2423# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002424 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
2426 int nn = n + 2;
2427
2428 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002429 if (nn > wp->w_width)
2430 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2432 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002433 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 n = nn;
2435 }
2436# endif
2437 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002438 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002439 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2441 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002442 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444 else
2445#endif
2446 {
2447#ifdef FEAT_CMDWIN
2448 if (cmdwin_type != 0 && wp == curwin)
2449 {
2450 /* draw the cmdline character in the leftmost column */
2451 n = 1;
2452 if (n > wp->w_width)
2453 n = wp->w_width;
2454 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002455 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002456 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458#endif
2459#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002460 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002462 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
2464 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002465 if (nn > wp->w_width)
2466 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002468 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002469 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 n = nn;
2471 }
2472#endif
2473#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002474 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 int nn = n + 2;
2477
2478 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002479 if (nn > wp->w_width)
2480 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002482 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002483 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 n = nn;
2485 }
2486#endif
2487 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002488 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002489 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491 set_empty_rows(wp, row);
2492}
2493
Bram Moolenaar1a384422010-07-14 19:53:30 +02002494#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002495/*
2496 * Advance **color_cols and return TRUE when there are columns to draw.
2497 */
2498 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002499advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002500{
2501 while (**color_cols >= 0 && vcol > **color_cols)
2502 ++*color_cols;
2503 return (**color_cols >= 0);
2504}
2505#endif
2506
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002507#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2508/*
2509 * Copy "text" to ScreenLines using "attr".
2510 * Returns the next screen column.
2511 */
2512 static int
2513text_to_screenline(win_T *wp, char_u *text, int col)
2514{
2515 int off = (int)(current_ScreenLine - ScreenLines);
2516
2517#ifdef FEAT_MBYTE
2518 if (has_mbyte)
2519 {
2520 int cells;
2521 int u8c, u8cc[MAX_MCO];
2522 int i;
2523 int idx;
2524 int c_len;
2525 char_u *p;
2526# ifdef FEAT_ARABIC
2527 int prev_c = 0; /* previous Arabic character */
2528 int prev_c1 = 0; /* first composing char for prev_c */
2529# endif
2530
2531# ifdef FEAT_RIGHTLEFT
2532 if (wp->w_p_rl)
2533 idx = off;
2534 else
2535# endif
2536 idx = off + col;
2537
2538 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2539 for (p = text; *p != NUL; )
2540 {
2541 cells = (*mb_ptr2cells)(p);
2542 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002543 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002544# ifdef FEAT_RIGHTLEFT
2545 - (wp->w_p_rl ? col : 0)
2546# endif
2547 )
2548 break;
2549 ScreenLines[idx] = *p;
2550 if (enc_utf8)
2551 {
2552 u8c = utfc_ptr2char(p, u8cc);
2553 if (*p < 0x80 && u8cc[0] == 0)
2554 {
2555 ScreenLinesUC[idx] = 0;
2556#ifdef FEAT_ARABIC
2557 prev_c = u8c;
2558#endif
2559 }
2560 else
2561 {
2562#ifdef FEAT_ARABIC
2563 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2564 {
2565 /* Do Arabic shaping. */
2566 int pc, pc1, nc;
2567 int pcc[MAX_MCO];
2568 int firstbyte = *p;
2569
2570 /* The idea of what is the previous and next
2571 * character depends on 'rightleft'. */
2572 if (wp->w_p_rl)
2573 {
2574 pc = prev_c;
2575 pc1 = prev_c1;
2576 nc = utf_ptr2char(p + c_len);
2577 prev_c1 = u8cc[0];
2578 }
2579 else
2580 {
2581 pc = utfc_ptr2char(p + c_len, pcc);
2582 nc = prev_c;
2583 pc1 = pcc[0];
2584 }
2585 prev_c = u8c;
2586
2587 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2588 pc, pc1, nc);
2589 ScreenLines[idx] = firstbyte;
2590 }
2591 else
2592 prev_c = u8c;
2593#endif
2594 /* Non-BMP character: display as ? or fullwidth ?. */
2595#ifdef UNICODE16
2596 if (u8c >= 0x10000)
2597 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2598 else
2599#endif
2600 ScreenLinesUC[idx] = u8c;
2601 for (i = 0; i < Screen_mco; ++i)
2602 {
2603 ScreenLinesC[i][idx] = u8cc[i];
2604 if (u8cc[i] == 0)
2605 break;
2606 }
2607 }
2608 if (cells > 1)
2609 ScreenLines[idx + 1] = 0;
2610 }
2611 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2612 /* double-byte single width character */
2613 ScreenLines2[idx] = p[1];
2614 else if (cells > 1)
2615 /* double-width character */
2616 ScreenLines[idx + 1] = p[1];
2617 col += cells;
2618 idx += cells;
2619 p += c_len;
2620 }
2621 }
2622 else
2623#endif
2624 {
2625 int len = (int)STRLEN(text);
2626
Bram Moolenaar02631462017-09-22 15:20:32 +02002627 if (len > wp->w_width - col)
2628 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002629 if (len > 0)
2630 {
2631#ifdef FEAT_RIGHTLEFT
2632 if (wp->w_p_rl)
2633 STRNCPY(current_ScreenLine, text, len);
2634 else
2635#endif
2636 STRNCPY(current_ScreenLine + col, text, len);
2637 col += len;
2638 }
2639 }
2640 return col;
2641}
2642#endif
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644#ifdef FEAT_FOLDING
2645/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002646 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2647 * space is available for window "wp", minus "col".
2648 */
2649 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002650compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002651{
2652 int fdc = wp->w_p_fdc;
2653 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002654 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002655
2656 if (fdc > wwidth - (col + wmw))
2657 fdc = wwidth - (col + wmw);
2658 return fdc;
2659}
2660
2661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 * Display one folded line.
2663 */
2664 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002665fold_line(
2666 win_T *wp,
2667 long fold_count,
2668 foldinfo_T *foldinfo,
2669 linenr_T lnum,
2670 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002672 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 pos_T *top, *bot;
2674 linenr_T lnume = lnum + fold_count - 1;
2675 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002676 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 int col;
2679 int txtcol;
2680 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002681 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 /* Build the fold line:
2684 * 1. Add the cmdwin_type for the command-line window
2685 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002686 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 * 4. Compose the text
2688 * 5. Add the text
2689 * 6. set highlighting for the Visual area an other text
2690 */
2691 col = 0;
2692
2693 /*
2694 * 1. Add the cmdwin_type for the command-line window
2695 * Ignores 'rightleft', this window is never right-left.
2696 */
2697#ifdef FEAT_CMDWIN
2698 if (cmdwin_type != 0 && wp == curwin)
2699 {
2700 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002701 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702#ifdef FEAT_MBYTE
2703 if (enc_utf8)
2704 ScreenLinesUC[off] = 0;
2705#endif
2706 ++col;
2707 }
2708#endif
2709
2710 /*
2711 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002712 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002714 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (fdc > 0)
2716 {
2717 fill_foldcolumn(buf, wp, TRUE, lnum);
2718#ifdef FEAT_RIGHTLEFT
2719 if (wp->w_p_rl)
2720 {
2721 int i;
2722
Bram Moolenaar02631462017-09-22 15:20:32 +02002723 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002724 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 /* reverse the fold column */
2726 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
2729 else
2730#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002731 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 col += fdc;
2733 }
2734
2735#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002736# define RL_MEMSET(p, v, l) \
2737 do { \
2738 if (wp->w_p_rl) \
2739 for (ri = 0; ri < l; ++ri) \
2740 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2741 else \
2742 for (ri = 0; ri < l; ++ri) \
2743 ScreenAttrs[off + (p) + ri] = v; \
2744 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002746# define RL_MEMSET(p, v, l) \
2747 do { \
2748 for (ri = 0; ri < l; ++ri) \
2749 ScreenAttrs[off + (p) + ri] = v; \
2750 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752
Bram Moolenaar64486672010-05-16 15:46:46 +02002753 /* Set all attributes of the 'number' or 'relativenumber' column and the
2754 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002755 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757#ifdef FEAT_SIGNS
2758 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002759 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002761 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (len > 0)
2763 {
2764 if (len > 2)
2765 len = 2;
2766# ifdef FEAT_RIGHTLEFT
2767 if (wp->w_p_rl)
2768 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002769 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002770 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 else
2772# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002773 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 col += len;
2775 }
2776 }
2777#endif
2778
2779 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002780 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002782 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002784 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (len > 0)
2786 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002787 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002788 long num;
2789 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002790
2791 if (len > w + 1)
2792 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002793
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002794 if (wp->w_p_nu && !wp->w_p_rnu)
2795 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002796 num = (long)lnum;
2797 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002798 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002799 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002800 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002801 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002802 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002803 /* 'number' + 'relativenumber': cursor line shows absolute
2804 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002805 num = lnum;
2806 fmt = "%-*ld ";
2807 }
2808 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002809
Bram Moolenaar700e7342013-01-30 12:31:36 +01002810 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811#ifdef FEAT_RIGHTLEFT
2812 if (wp->w_p_rl)
2813 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002814 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002815 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 else
2817#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002818 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 col += len;
2820 }
2821 }
2822
2823 /*
2824 * 4. Compose the folded-line string with 'foldtext', if set.
2825 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002826 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 txtcol = col; /* remember where text starts */
2829
2830 /*
2831 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2832 * Right-left text is put in columns 0 - number-col, normal text is put
2833 * in columns number-col - window-width.
2834 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002835 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836
2837 /* Fill the rest of the line with the fold filler */
2838#ifdef FEAT_RIGHTLEFT
2839 if (wp->w_p_rl)
2840 col -= txtcol;
2841#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002842 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#ifdef FEAT_RIGHTLEFT
2844 - (wp->w_p_rl ? txtcol : 0)
2845#endif
2846 )
2847 {
2848#ifdef FEAT_MBYTE
2849 if (enc_utf8)
2850 {
2851 if (fill_fold >= 0x80)
2852 {
2853 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002854 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002855 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002860 ScreenLines[off + col] = fill_fold;
2861 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002862 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002866 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868
2869 if (text != buf)
2870 vim_free(text);
2871
2872 /*
2873 * 6. set highlighting for the Visual area an other text.
2874 * If all folded lines are in the Visual area, highlight the line.
2875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2877 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002878 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 /* Visual is after curwin->w_cursor */
2881 top = &curwin->w_cursor;
2882 bot = &VIsual;
2883 }
2884 else
2885 {
2886 /* Visual is before curwin->w_cursor */
2887 top = &VIsual;
2888 bot = &curwin->w_cursor;
2889 }
2890 if (lnum >= top->lnum
2891 && lnume <= bot->lnum
2892 && (VIsual_mode != 'v'
2893 || ((lnum > top->lnum
2894 || (lnum == top->lnum
2895 && top->col == 0))
2896 && (lnume < bot->lnum
2897 || (lnume == bot->lnum
2898 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002899 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 {
2901 if (VIsual_mode == Ctrl_V)
2902 {
2903 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002906 if (wp->w_old_cursor_lcol != MAXCOL
2907 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002908 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 len = wp->w_old_cursor_lcol;
2910 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002911 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002912 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002913 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 }
2916 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002919 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
2922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002924#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002925 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2926 if (wp->w_p_cc_cols)
2927 {
2928 int i = 0;
2929 int j = wp->w_p_cc_cols[i];
2930 int old_txtcol = txtcol;
2931
2932 while (j > -1)
2933 {
2934 txtcol += j;
2935 if (wp->w_p_wrap)
2936 txtcol -= wp->w_skipcol;
2937 else
2938 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002939 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002940 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002941 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002942 txtcol = old_txtcol;
2943 j = wp->w_p_cc_cols[++i];
2944 }
2945 }
2946
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002947 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002948 if (wp->w_p_cuc)
2949 {
2950 txtcol += wp->w_virtcol;
2951 if (wp->w_p_wrap)
2952 txtcol -= wp->w_skipcol;
2953 else
2954 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002955 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002956 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002957 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002958 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
Bram Moolenaar02631462017-09-22 15:20:32 +02002961 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2962 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 /*
2965 * Update w_cline_height and w_cline_folded if the cursor line was
2966 * updated (saves a call to plines() later).
2967 */
2968 if (wp == curwin
2969 && lnum <= curwin->w_cursor.lnum
2970 && lnume >= curwin->w_cursor.lnum)
2971 {
2972 curwin->w_cline_row = row;
2973 curwin->w_cline_height = 1;
2974 curwin->w_cline_folded = TRUE;
2975 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2976 }
2977}
2978
2979/*
2980 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2981 */
2982 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002983copy_text_attr(
2984 int off,
2985 char_u *buf,
2986 int len,
2987 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002989 int i;
2990
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 mch_memmove(ScreenLines + off, buf, (size_t)len);
2992# ifdef FEAT_MBYTE
2993 if (enc_utf8)
2994 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2995# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002996 for (i = 0; i < len; ++i)
2997 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998}
2999
3000/*
3001 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003002 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 */
3004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003005fill_foldcolumn(
3006 char_u *p,
3007 win_T *wp,
3008 int closed, /* TRUE of FALSE */
3009 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 int i = 0;
3012 int level;
3013 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003014 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003015 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
3017 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003018 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 level = win_foldinfo.fi_level;
3021 if (level > 0)
3022 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003023 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003024 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 /* If the column is too narrow, we start at the lowest level that
3027 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003028 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (first_level < 1)
3030 first_level = 1;
3031
Bram Moolenaar1c934292015-01-27 16:39:29 +01003032 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 {
3034 if (win_foldinfo.fi_lnum == lnum
3035 && first_level + i >= win_foldinfo.fi_low_level)
3036 p[i] = '-';
3037 else if (first_level == 1)
3038 p[i] = '|';
3039 else if (first_level + i <= 9)
3040 p[i] = '0' + first_level + i;
3041 else
3042 p[i] = '>';
3043 if (first_level + i == level)
3044 break;
3045 }
3046 }
3047 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003048 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049}
3050#endif /* FEAT_FOLDING */
3051
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003052#ifdef FEAT_TEXT_PROP
3053static textprop_T *current_text_props = NULL;
3054static buf_T *current_buf = NULL;
3055
3056 static int
3057#ifdef __BORLANDC__
3058_RTLENTRYF
3059#endif
3060text_prop_compare(const void *s1, const void *s2)
3061{
3062 int idx1, idx2;
3063 proptype_T *pt1, *pt2;
3064 colnr_T col1, col2;
3065
3066 idx1 = *(int *)s1;
3067 idx2 = *(int *)s2;
3068 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3069 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3070 if (pt1 == pt2)
3071 return 0;
3072 if (pt1 == NULL)
3073 return -1;
3074 if (pt2 == NULL)
3075 return 1;
3076 if (pt1->pt_priority != pt2->pt_priority)
3077 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3078 col1 = current_text_props[idx1].tp_col;
3079 col2 = current_text_props[idx2].tp_col;
3080 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3081}
3082#endif
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084/*
3085 * Display line "lnum" of window 'wp' on the screen.
3086 * Start at row "startrow", stop when "endrow" is reached.
3087 * wp->w_virtcol needs to be valid.
3088 *
3089 * Return the number of last row the line occupies.
3090 */
3091 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003092win_line(
3093 win_T *wp,
3094 linenr_T lnum,
3095 int startrow,
3096 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003097 int nochange UNUSED, // not updating for changed text
3098 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003100 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3102 int c = 0; /* init for GCC */
3103 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003104#ifdef FEAT_LINEBREAK
3105 long vcol_sbr = -1; /* virtual column after showbreak */
3106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 long vcol_prev = -1; /* "vcol" of previous character */
3108 char_u *line; /* current line */
3109 char_u *ptr; /* current position in "line" */
3110 int row; /* row in the window, excl w_winrow */
3111 int screen_row; /* row on the screen, incl w_winrow */
3112
3113 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3114 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003115 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003116 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int c_extra = NUL; /* extra chars, all the same */
3118 int extra_attr = 0; /* attributes when n_extra != 0 */
3119 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3120 displaying lcs_eol at end-of-line */
3121 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3122 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3123
3124 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3125 int saved_n_extra = 0;
3126 char_u *saved_p_extra = NULL;
3127 int saved_c_extra = 0;
3128 int saved_char_attr = 0;
3129
3130 int n_attr = 0; /* chars with special attr */
3131 int saved_attr2 = 0; /* char_attr saved for n_attr */
3132 int n_attr3 = 0; /* chars with overruling special attr */
3133 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3134
3135 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3136
3137 int fromcol, tocol; /* start/end of inverting */
3138 int fromcol_prev = -2; /* start of inverting after cursor */
3139 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003141 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 pos_T pos;
3143 long v;
3144
3145 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003146 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3148 in this line */
3149 int attr = 0; /* attributes for area highlighting */
3150 int area_attr = 0; /* attributes desired by highlighting */
3151 int search_attr = 0; /* attributes desired by 'hlsearch' */
3152#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003153 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 int syntax_attr = 0; /* attributes desired by syntax */
3155 int has_syntax = FALSE; /* this buffer has syntax highl. */
3156 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003157 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003158 int draw_color_col = FALSE; /* highlight colorcolumn */
3159 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003160#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003161#ifdef FEAT_TEXT_PROP
3162 int text_prop_count;
3163 int text_prop_next = 0; // next text property to use
3164 textprop_T *text_props = NULL;
3165 int *text_prop_idxs = NULL;
3166 int text_props_active = 0;
3167 proptype_T *text_prop_type = NULL;
3168 int text_prop_attr = 0;
3169#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003170#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003171 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003172# define SPWORDLEN 150
3173 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003174 int nextlinecol = 0; /* column where nextline[] starts */
3175 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003176 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003177 int spell_attr = 0; /* attributes desired by spelling */
3178 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003179 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3180 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003181 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003182 static int cap_col = -1; /* column to check for Cap word */
3183 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003184 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003186 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187#ifdef FEAT_MBYTE
3188 int multi_attr = 0; /* attributes desired by multibyte */
3189 int mb_l = 1; /* multi-byte byte length */
3190 int mb_c = 0; /* decoded multi-byte character */
3191 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003192 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#endif
3194#ifdef FEAT_DIFF
3195 int filler_lines; /* nr of filler lines to be drawn */
3196 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003197 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 int change_start = MAXCOL; /* first col of changed area */
3199 int change_end = -1; /* last col of changed area */
3200#endif
3201 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3202#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003203 int need_showbreak = FALSE; /* overlong line, skipping first x
3204 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003206#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003207 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003209 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#endif
3211#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003212 matchitem_T *cur; /* points to the match list */
3213 match_T *shl; /* points to search_hl or a match */
3214 int shl_flag; /* flag to indicate whether search_hl
3215 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003216 int pos_inprogress; /* marks that position match search is
3217 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003218 int prevcol_hl_flag; /* flag to indicate whether prevcol
3219 equals startcol of search_hl or one
3220 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#endif
3222#ifdef FEAT_ARABIC
3223 int prev_c = 0; /* previous Arabic character */
3224 int prev_c1 = 0; /* first composing char for prev_c */
3225#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003226#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003227 int did_line_attr = 0;
3228#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003229#ifdef FEAT_TERMINAL
3230 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003231 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234 /* draw_state: items that are drawn in sequence: */
3235#define WL_START 0 /* nothing done yet */
3236#ifdef FEAT_CMDWIN
3237# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3238#else
3239# define WL_CMDLINE WL_START
3240#endif
3241#ifdef FEAT_FOLDING
3242# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3243#else
3244# define WL_FOLD WL_CMDLINE
3245#endif
3246#ifdef FEAT_SIGNS
3247# define WL_SIGN WL_FOLD + 1 /* column for signs */
3248#else
3249# define WL_SIGN WL_FOLD /* column for signs */
3250#endif
3251#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003252#ifdef FEAT_LINEBREAK
3253# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003255# define WL_BRI WL_NR
3256#endif
3257#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3258# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3259#else
3260# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#endif
3262#define WL_LINE WL_SBR + 1 /* text in the line */
3263 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003264#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 int feedback_col = 0;
3266 int feedback_old_attr = -1;
3267#endif
3268
Bram Moolenaar860cae12010-06-05 23:22:07 +02003269#ifdef FEAT_CONCEAL
3270 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003271 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003272 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003273 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003274 int is_concealing = FALSE;
3275 int boguscols = 0; /* nonexistent columns added to force
3276 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003277 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003278 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003279 int match_conc = 0; /* cchar for match functions */
3280 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003281 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003282# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003283# define FIX_FOR_BOGUSCOLS \
3284 { \
3285 n_extra += vcol_off; \
3286 vcol -= vcol_off; \
3287 vcol_off = 0; \
3288 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003289 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003290 boguscols = 0; \
3291 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003292#else
3293# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 if (startrow > endrow) /* past the end already! */
3297 return startrow;
3298
3299 row = startrow;
3300 screen_row = row + W_WINROW(wp);
3301
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003302 if (!number_only)
3303 {
3304 /*
3305 * To speed up the loop below, set extra_check when there is linebreak,
3306 * trailing white space and/or syntax processing to be done.
3307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003309 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310#endif
3311#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003312 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003313# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003314 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003315# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003318 /* Prepare for syntax highlighting in this line. When there is an
3319 * error, stop syntax highlighting. */
3320 save_did_emsg = did_emsg;
3321 did_emsg = FALSE;
3322 syntax_start(wp, lnum);
3323 if (did_emsg)
3324 wp->w_s->b_syn_error = TRUE;
3325 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003326 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003327 did_emsg = save_did_emsg;
3328#ifdef SYN_TIME_LIMIT
3329 if (!wp->w_s->b_syn_slow)
3330#endif
3331 {
3332 has_syntax = TRUE;
3333 extra_check = TRUE;
3334 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003337
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003338 /* Check for columns to display for 'colorcolumn'. */
3339 color_cols = wp->w_p_cc_cols;
3340 if (color_cols != NULL)
3341 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003342#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003343
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003344#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003345 if (term_show_buffer(wp->w_buffer))
3346 {
3347 extra_check = TRUE;
3348 get_term_attr = TRUE;
3349 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3350 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003351#endif
3352
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003353#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003354 if (wp->w_p_spell
3355 && *wp->w_s->b_p_spl != NUL
3356 && wp->w_s->b_langp.ga_len > 0
3357 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003358 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003359 /* Prepare for spell checking. */
3360 has_spell = TRUE;
3361 extra_check = TRUE;
3362
Bram Moolenaar7701f302018-10-02 21:20:32 +02003363 /* Get the start of the next line, so that words that wrap to the
3364 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 * Trick: skip a few chars for C/shell/Vim comments */
3366 nextline[SPWORDLEN] = NUL;
3367 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3368 {
3369 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3370 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3371 }
3372
Bram Moolenaar7701f302018-10-02 21:20:32 +02003373 /* When a word wrapped from the previous line the start of the
3374 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 if (lnum == checked_lnum)
3376 cur_checked_col = checked_col;
3377 checked_lnum = 0;
3378
3379 /* When there was a sentence end in the previous line may require a
3380 * word starting with capital in this line. In line 1 always check
3381 * the first word. */
3382 if (lnum != capcol_lnum)
3383 cap_col = -1;
3384 if (lnum == 1)
3385 cap_col = 0;
3386 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#endif
3389
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003390 /*
3391 * handle visual active in this window
3392 */
3393 fromcol = -10;
3394 tocol = MAXCOL;
3395 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003397 /* Visual is after curwin->w_cursor */
3398 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003400 top = &curwin->w_cursor;
3401 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003405 top = &VIsual;
3406 bot = &curwin->w_cursor;
3407 }
3408 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3409 if (VIsual_mode == Ctrl_V) /* block mode */
3410 {
3411 if (lnum_in_visual_area)
3412 {
3413 fromcol = wp->w_old_cursor_fcol;
3414 tocol = wp->w_old_cursor_lcol;
3415 }
3416 }
3417 else /* non-block mode */
3418 {
3419 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 if (VIsual_mode == 'V') /* linewise */
3424 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 else
3426 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003427 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3428 if (gchar_pos(top) == NUL)
3429 tocol = fromcol + 1;
3430 }
3431 }
3432 if (VIsual_mode != 'V' && lnum == bot->lnum)
3433 {
3434 if (*p_sel == 'e' && bot->col == 0
3435#ifdef FEAT_VIRTUALEDIT
3436 && bot->coladd == 0
3437#endif
3438 )
3439 {
3440 fromcol = -10;
3441 tocol = MAXCOL;
3442 }
3443 else if (bot->col == MAXCOL)
3444 tocol = MAXCOL;
3445 else
3446 {
3447 pos = *bot;
3448 if (*p_sel == 'e')
3449 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3450 else
3451 {
3452 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3453 ++tocol;
3454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456 }
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 /* Check if the character under the cursor should not be inverted */
3460 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003461#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003463#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003464 )
3465 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 /* if inverting in this line set area_highlighting */
3468 if (fromcol >= 0)
3469 {
3470 area_highlighting = TRUE;
3471 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003473 if ((clip_star.available && !clip_star.owned
3474 && clip_isautosel_star())
3475 || (clip_plus.available && !clip_plus.owned
3476 && clip_isautosel_plus()))
3477 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 /*
3483 * handle 'incsearch' and ":s///c" highlighting
3484 */
3485 else if (highlight_match
3486 && wp == curwin
3487 && lnum >= curwin->w_cursor.lnum
3488 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003490 if (lnum == curwin->w_cursor.lnum)
3491 getvcol(curwin, &(curwin->w_cursor),
3492 (colnr_T *)&fromcol, NULL, NULL);
3493 else
3494 fromcol = 0;
3495 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3496 {
3497 pos.lnum = lnum;
3498 pos.col = search_match_endcol;
3499 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3500 }
3501 else
3502 tocol = MAXCOL;
3503 /* do at least one character; happens when past end of line */
3504 if (fromcol == tocol)
3505 tocol = fromcol + 1;
3506 area_highlighting = TRUE;
3507 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510
3511#ifdef FEAT_DIFF
3512 filler_lines = diff_check(wp, lnum);
3513 if (filler_lines < 0)
3514 {
3515 if (filler_lines == -1)
3516 {
3517 if (diff_find_change(wp, lnum, &change_start, &change_end))
3518 diff_hlf = HLF_ADD; /* added line */
3519 else if (change_start == 0)
3520 diff_hlf = HLF_TXD; /* changed text */
3521 else
3522 diff_hlf = HLF_CHD; /* changed line */
3523 }
3524 else
3525 diff_hlf = HLF_ADD; /* added line */
3526 filler_lines = 0;
3527 area_highlighting = TRUE;
3528 }
3529 if (lnum == wp->w_topline)
3530 filler_lines = wp->w_topfill;
3531 filler_todo = filler_lines;
3532#endif
3533
3534#ifdef LINE_ATTR
3535# ifdef FEAT_SIGNS
3536 /* If this line has a sign with line highlighting set line_attr. */
3537 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3538 if (v != 0)
3539 line_attr = sign_get_attr((int)v, TRUE);
3540# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003541# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003543 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003544 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545# endif
3546 if (line_attr != 0)
3547 area_highlighting = TRUE;
3548#endif
3549
3550 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3551 ptr = line;
3552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003553#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003554 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003555 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003556 /* For checking first word with a capital skip white space. */
3557 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003558 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003559
Bram Moolenaar30abd282005-06-22 22:35:10 +00003560 /* To be able to spell-check over line boundaries copy the end of the
3561 * current line into nextline[]. Above the start of the next line was
3562 * copied to nextline[SPWORDLEN]. */
3563 if (nextline[SPWORDLEN] == NUL)
3564 {
3565 /* No next line or it is empty. */
3566 nextlinecol = MAXCOL;
3567 nextline_idx = 0;
3568 }
3569 else
3570 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003571 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003572 if (v < SPWORDLEN)
3573 {
3574 /* Short line, use it completely and append the start of the
3575 * next line. */
3576 nextlinecol = 0;
3577 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003578 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003579 nextline_idx = v + 1;
3580 }
3581 else
3582 {
3583 /* Long line, use only the last SPWORDLEN bytes. */
3584 nextlinecol = v - SPWORDLEN;
3585 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3586 nextline_idx = SPWORDLEN + 1;
3587 }
3588 }
3589 }
3590#endif
3591
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003592 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003594 if (lcs_space || lcs_trail)
3595 extra_check = TRUE;
3596 /* find start of trailing whitespace */
3597 if (lcs_trail)
3598 {
3599 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003600 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003601 --trailcol;
3602 trailcol += (colnr_T) (ptr - line);
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605
3606 /*
3607 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3608 * first character to be displayed.
3609 */
3610 if (wp->w_p_wrap)
3611 v = wp->w_skipcol;
3612 else
3613 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003614 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616#ifdef FEAT_MBYTE
3617 char_u *prev_ptr = ptr;
3618#endif
3619 while (vcol < v && *ptr != NUL)
3620 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003621 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 vcol += c;
3623#ifdef FEAT_MBYTE
3624 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003626 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003629 /* When:
3630 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003631 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003632 * - 'virtualedit' is set, or
3633 * - the visual mode is active,
3634 * the end of the line may be before the start of the displayed part.
3635 */
3636 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003637#ifdef FEAT_SYN_HL
3638 wp->w_p_cuc || draw_color_col ||
3639#endif
3640#ifdef FEAT_VIRTUALEDIT
3641 virtual_active() ||
3642#endif
3643 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 /* Handle a character that's not completely on the screen: Put ptr at
3649 * that character but skip the first few screen characters. */
3650 if (vcol > v)
3651 {
3652 vcol -= c;
3653#ifdef FEAT_MBYTE
3654 ptr = prev_ptr;
3655#else
3656 --ptr;
3657#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003658 /* If the character fits on the screen, don't need to skip it.
3659 * Except for a TAB. */
3660 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003661#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003662 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003663#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003664 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003665 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 /*
3669 * Adjust for when the inverted text is before the screen,
3670 * and when the start of the inverted text is before the screen.
3671 */
3672 if (tocol <= vcol)
3673 fromcol = 0;
3674 else if (fromcol >= 0 && fromcol < vcol)
3675 fromcol = vcol;
3676
3677#ifdef FEAT_LINEBREAK
3678 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3679 if (wp->w_p_wrap)
3680 need_showbreak = TRUE;
3681#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003682#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003683 /* When spell checking a word we need to figure out the start of the
3684 * word and if it's badly spelled or not. */
3685 if (has_spell)
3686 {
3687 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003688 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003689 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003690
3691 pos = wp->w_cursor;
3692 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003693 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003694 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003695
3696 /* spell_move_to() may call ml_get() and make "line" invalid */
3697 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3698 ptr = line + linecol;
3699
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003700 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003701 {
3702 /* no bad word found at line start, don't check until end of a
3703 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003704 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003705 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003706 }
3707 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003708 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003709 /* bad word found, use attributes until end of word */
3710 word_end = wp->w_cursor.col + len + 1;
3711
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003712 /* Turn index into actual attributes. */
3713 if (spell_hlf != HLF_COUNT)
3714 spell_attr = highlight_attr[spell_hlf];
3715 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003716 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003717
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003718# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003719 /* Need to restart syntax highlighting for this line. */
3720 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003721 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003722# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003723 }
3724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726
3727 /*
3728 * Correct highlighting for cursor that can't be disabled.
3729 * Avoids having to check this for each character.
3730 */
3731 if (fromcol >= 0)
3732 {
3733 if (noinvcur)
3734 {
3735 if ((colnr_T)fromcol == wp->w_virtcol)
3736 {
3737 /* highlighting starts at cursor, let it start just after the
3738 * cursor */
3739 fromcol_prev = fromcol;
3740 fromcol = -1;
3741 }
3742 else if ((colnr_T)fromcol < wp->w_virtcol)
3743 /* restart highlighting after the cursor */
3744 fromcol_prev = wp->w_virtcol;
3745 }
3746 if (fromcol >= tocol)
3747 fromcol = -1;
3748 }
3749
3750#ifdef FEAT_SEARCH_EXTRA
3751 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003752 * Handle highlighting the last used search pattern and matches.
3753 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003755 cur = wp->w_match_head;
3756 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003757 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003759 if (shl_flag == FALSE)
3760 {
3761 shl = &search_hl;
3762 shl_flag = TRUE;
3763 }
3764 else
3765 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003766 shl->startcol = MAXCOL;
3767 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003769 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003770 v = (long)(ptr - line);
3771 if (cur != NULL)
3772 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003773 next_search_hl(wp, shl, lnum, (colnr_T)v,
3774 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003775
3776 /* Need to get the line again, a multi-line regexp may have made it
3777 * invalid. */
3778 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3779 ptr = line + v;
3780
3781 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003783 if (shl->lnum == lnum)
3784 shl->startcol = shl->rm.startpos[0].col;
3785 else
3786 shl->startcol = 0;
3787 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3788 - shl->rm.startpos[0].lnum)
3789 shl->endcol = shl->rm.endpos[0].col;
3790 else
3791 shl->endcol = MAXCOL;
3792 /* Highlight one character for an empty match. */
3793 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003796 if (has_mbyte && line[shl->endcol] != NUL)
3797 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003800 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003802 if ((long)shl->startcol < v) /* match at leftcol */
3803 {
3804 shl->attr_cur = shl->attr;
3805 search_attr = shl->attr;
3806 }
3807 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003809 if (shl != &search_hl && cur != NULL)
3810 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812#endif
3813
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003814#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003815 /* Cursor line highlighting for 'cursorline' in the current window. Not
3816 * when Visual mode is active, because it's not clear what is selected
3817 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003818 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003819 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003820 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003821 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003822 area_highlighting = TRUE;
3823 }
3824#endif
3825
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003826#ifdef FEAT_TEXT_PROP
3827 {
3828 char_u *prop_start;
3829
3830 text_prop_count = get_text_props(wp->w_buffer, lnum,
3831 &prop_start, FALSE);
3832 if (text_prop_count > 0)
3833 {
3834 // Make a copy of the properties, so that they are properly
3835 // aligned.
3836 text_props = (textprop_T *)alloc(
3837 text_prop_count * sizeof(textprop_T));
3838 if (text_props != NULL)
3839 mch_memmove(text_props, prop_start,
3840 text_prop_count * sizeof(textprop_T));
3841
3842 // Allocate an array for the indexes.
3843 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3844 area_highlighting = TRUE;
3845 extra_check = TRUE;
3846 }
3847 }
3848#endif
3849
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003850 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 col = 0;
3852#ifdef FEAT_RIGHTLEFT
3853 if (wp->w_p_rl)
3854 {
3855 /* Rightleft window: process the text in the normal direction, but put
3856 * it in current_ScreenLine[] from right to left. Start at the
3857 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003858 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 off += col;
3860 }
3861#endif
3862
3863 /*
3864 * Repeat for the whole displayed line.
3865 */
3866 for (;;)
3867 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003868#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003869 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 /* Skip this quickly when working on the text. */
3872 if (draw_state != WL_LINE)
3873 {
3874#ifdef FEAT_CMDWIN
3875 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3876 {
3877 draw_state = WL_CMDLINE;
3878 if (cmdwin_type != 0 && wp == curwin)
3879 {
3880 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003882 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003883 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 }
3886#endif
3887
3888#ifdef FEAT_FOLDING
3889 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3890 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003891 int fdc = compute_foldcolumn(wp, 0);
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003894 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003896 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003897 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003898 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003899 p_extra_free = alloc(12 + 1);
3900
3901 if (p_extra_free != NULL)
3902 {
3903 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3904 n_extra = fdc;
3905 p_extra_free[n_extra] = NUL;
3906 p_extra = p_extra_free;
3907 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003908 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
3911 }
3912#endif
3913
3914#ifdef FEAT_SIGNS
3915 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3916 {
3917 draw_state = WL_SIGN;
3918 /* Show the sign column when there are any signs in this
3919 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003920 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003922 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003924 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925# endif
3926
3927 /* Draw two cells with the sign value or blank. */
3928 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003929 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 n_extra = 2;
3931
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003932 if (row == startrow
3933#ifdef FEAT_DIFF
3934 + filler_lines && filler_todo <= 0
3935#endif
3936 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 {
3938 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3939 SIGN_TEXT);
3940# ifdef FEAT_SIGN_ICONS
3941 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3942 SIGN_ICON);
3943 if (gui.in_use && icon_sign != 0)
3944 {
3945 /* Use the image in this position. */
3946 c_extra = SIGN_BYTE;
3947# ifdef FEAT_NETBEANS_INTG
3948 if (buf_signcount(wp->w_buffer, lnum) > 1)
3949 c_extra = MULTISIGN_BYTE;
3950# endif
3951 char_attr = icon_sign;
3952 }
3953 else
3954# endif
3955 if (text_sign != 0)
3956 {
3957 p_extra = sign_get_text(text_sign);
3958 if (p_extra != NULL)
3959 {
3960 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003961 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
3963 char_attr = sign_get_attr(text_sign, FALSE);
3964 }
3965 }
3966 }
3967 }
3968#endif
3969
3970 if (draw_state == WL_NR - 1 && n_extra == 0)
3971 {
3972 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003973 /* Display the absolute or relative line number. After the
3974 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3975 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 && (row == startrow
3977#ifdef FEAT_DIFF
3978 + filler_lines
3979#endif
3980 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3981 {
3982 /* Draw the line number (empty space after wrapping). */
3983 if (row == startrow
3984#ifdef FEAT_DIFF
3985 + filler_lines
3986#endif
3987 )
3988 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003989 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003990 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003991
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003992 if (wp->w_p_nu && !wp->w_p_rnu)
3993 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003994 num = (long)lnum;
3995 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003996 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003997 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003998 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003999 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004000 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004001 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004002 num = lnum;
4003 fmt = "%-*ld ";
4004 }
4005 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004006
Bram Moolenaar700e7342013-01-30 12:31:36 +01004007 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004008 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (wp->w_skipcol > 0)
4010 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4011 *p_extra = '-';
4012#ifdef FEAT_RIGHTLEFT
4013 if (wp->w_p_rl) /* reverse line numbers */
4014 rl_mirror(extra);
4015#endif
4016 p_extra = extra;
4017 c_extra = NUL;
4018 }
4019 else
4020 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004021 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004022 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004023#ifdef FEAT_SYN_HL
4024 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004025 * the current line differently.
4026 * TODO: Can we use CursorLine instead of CursorLineNr
4027 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004028 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004029 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004030 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033 }
4034
Bram Moolenaar597a4222014-06-25 14:39:50 +02004035#ifdef FEAT_LINEBREAK
4036 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4037 && n_extra == 0 && *p_sbr != NUL)
4038 /* draw indent after showbreak value */
4039 draw_state = WL_BRI;
4040 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4041 /* After the showbreak, draw the breakindent */
4042 draw_state = WL_BRI - 1;
4043
4044 /* draw 'breakindent': indent wrapped text accordingly */
4045 if (draw_state == WL_BRI - 1 && n_extra == 0)
4046 {
4047 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004048 /* if need_showbreak is set, breakindent also applies */
4049 if (wp->w_p_bri && n_extra == 0
4050 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004051# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004052 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004053# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 )
4055 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004056 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004057# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004058 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004059 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004060 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004061# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004062 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4063 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004064 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004065# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004066 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004067# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004068 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004069 c_extra = ' ';
4070 n_extra = get_breakindent_win(wp,
4071 ml_get_buf(wp->w_buffer, lnum, FALSE));
4072 /* Correct end of highlighted area for 'breakindent',
4073 * required when 'linebreak' is also set. */
4074 if (tocol == vcol)
4075 tocol += n_extra;
4076 }
4077 }
4078#endif
4079
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4081 if (draw_state == WL_SBR - 1 && n_extra == 0)
4082 {
4083 draw_state = WL_SBR;
4084# ifdef FEAT_DIFF
4085 if (filler_todo > 0)
4086 {
4087 /* Draw "deleted" diff line(s). */
4088 if (char2cells(fill_diff) > 1)
4089 c_extra = '-';
4090 else
4091 c_extra = fill_diff;
4092# ifdef FEAT_RIGHTLEFT
4093 if (wp->w_p_rl)
4094 n_extra = col + 1;
4095 else
4096# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004097 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004098 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100# endif
4101# ifdef FEAT_LINEBREAK
4102 if (*p_sbr != NUL && need_showbreak)
4103 {
4104 /* Draw 'showbreak' at the start of each broken line. */
4105 p_extra = p_sbr;
4106 c_extra = NUL;
4107 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004108 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004110 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 /* Correct end of highlighted area for 'showbreak',
4112 * required when 'linebreak' is also set. */
4113 if (tocol == vcol)
4114 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004115#ifdef FEAT_SYN_HL
4116 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004117 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004118 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004119 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122# endif
4123 }
4124#endif
4125
4126 if (draw_state == WL_LINE - 1 && n_extra == 0)
4127 {
4128 draw_state = WL_LINE;
4129 if (saved_n_extra)
4130 {
4131 /* Continue item from end of wrapped line. */
4132 n_extra = saved_n_extra;
4133 c_extra = saved_c_extra;
4134 p_extra = saved_p_extra;
4135 char_attr = saved_char_attr;
4136 }
4137 else
4138 char_attr = 0;
4139 }
4140 }
4141
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004142 // When still displaying '$' of change command, stop at cursor.
4143 // When only displaying the (relative) line number and that's done,
4144 // stop here.
4145 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004146 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#ifdef FEAT_DIFF
4148 && filler_todo <= 0
4149#endif
4150 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004151 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004153 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004154 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004155 /* Pretend we have finished updating the window. Except when
4156 * 'cursorcolumn' is set. */
4157#ifdef FEAT_SYN_HL
4158 if (wp->w_p_cuc)
4159 row = wp->w_cline_row + wp->w_cline_height;
4160 else
4161#endif
4162 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 break;
4164 }
4165
Bram Moolenaar637532b2019-01-03 21:44:40 +01004166 if (draw_state == WL_LINE && (area_highlighting
4167#ifdef FEAT_SPELL
4168 || has_spell
4169#endif
4170 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
4172 /* handle Visual or match highlighting in this line */
4173 if (vcol == fromcol
4174#ifdef FEAT_MBYTE
4175 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4176 && (*mb_ptr2cells)(ptr) > 1)
4177#endif
4178 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004179 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 && vcol < tocol))
4181 area_attr = attr; /* start highlighting */
4182 else if (area_attr != 0
4183 && (vcol == tocol
4184 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
4187#ifdef FEAT_SEARCH_EXTRA
4188 if (!n_extra)
4189 {
4190 /*
4191 * Check for start/end of search pattern match.
4192 * After end, check for start/end of next match.
4193 * When another match, have to check for start again.
4194 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004195 * Do this for 'search_hl' and the match list (ordered by
4196 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004198 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004199 cur = wp->w_match_head;
4200 shl_flag = FALSE;
4201 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004203 if (shl_flag == FALSE
4204 && ((cur != NULL
4205 && cur->priority > SEARCH_HL_PRIORITY)
4206 || cur == NULL))
4207 {
4208 shl = &search_hl;
4209 shl_flag = TRUE;
4210 }
4211 else
4212 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004213 if (cur != NULL)
4214 cur->pos.cur = 0;
4215 pos_inprogress = TRUE;
4216 while (shl->rm.regprog != NULL
4217 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004219 if (shl->startcol != MAXCOL
4220 && v >= (long)shl->startcol
4221 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004223#ifdef FEAT_MBYTE
4224 int tmp_col = v + MB_PTR2LEN(ptr);
4225
4226 if (shl->endcol < tmp_col)
4227 shl->endcol = tmp_col;
4228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004230#ifdef FEAT_CONCEAL
4231 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4232 == cur->hlg_id)
4233 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004234 has_match_conc =
4235 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004236 match_conc = cur->conceal_char;
4237 }
4238 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004239 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004242 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004245 next_search_hl(wp, shl, lnum, (colnr_T)v,
4246 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004247 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004248 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250 /* Need to get the line again, a multi-line regexp
4251 * may have made it invalid. */
4252 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4253 ptr = line + v;
4254
4255 if (shl->lnum == lnum)
4256 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004257 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004259 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004261 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004263 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {
4265 /* highlight empty match, try again after
4266 * it */
4267#ifdef FEAT_MBYTE
4268 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004269 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004270 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 else
4272#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004273 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275
4276 /* Loop to check if the match starts at the
4277 * current position */
4278 continue;
4279 }
4280 }
4281 break;
4282 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004283 if (shl != &search_hl && cur != NULL)
4284 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004286
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004287 /* Use attributes from match with highest priority among
4288 * 'search_hl' and the match list. */
4289 search_attr = search_hl.attr_cur;
4290 cur = wp->w_match_head;
4291 shl_flag = FALSE;
4292 while (cur != NULL || shl_flag == FALSE)
4293 {
4294 if (shl_flag == FALSE
4295 && ((cur != NULL
4296 && cur->priority > SEARCH_HL_PRIORITY)
4297 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004298 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004299 shl = &search_hl;
4300 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004301 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004302 else
4303 shl = &cur->hl;
4304 if (shl->attr_cur != 0)
4305 search_attr = shl->attr_cur;
4306 if (shl != &search_hl && cur != NULL)
4307 cur = cur->next;
4308 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004309 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004310 if (*ptr == NUL && (did_line_attr >= 1
4311 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004312 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314#endif
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004317 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004319 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4320 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004322 if (diff_hlf == HLF_TXD && ptr - line > change_end
4323 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004325 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004326 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004327 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004330
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004331#ifdef FEAT_TEXT_PROP
4332 if (text_props != NULL)
4333 {
4334 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004335 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004336
4337 // Check if any active property ends.
4338 for (pi = 0; pi < text_props_active; ++pi)
4339 {
4340 int tpi = text_prop_idxs[pi];
4341
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004342 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004343 + text_props[tpi].tp_len)
4344 {
4345 if (pi + 1 < text_props_active)
4346 mch_memmove(text_prop_idxs + pi,
4347 text_prop_idxs + pi + 1,
4348 sizeof(int)
4349 * (text_props_active - (pi + 1)));
4350 --text_props_active;
4351 --pi;
4352 }
4353 }
4354
4355 // Add any text property that starts in this column.
4356 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004357 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004358 text_prop_idxs[text_props_active++] = text_prop_next++;
4359
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004360 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004361 if (text_props_active > 0)
4362 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004363 // Sort the properties on priority and/or starting last.
4364 // Then combine the attributes, highest priority last.
4365 current_text_props = text_props;
4366 current_buf = wp->w_buffer;
4367 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4368 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004369
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004370 for (pi = 0; pi < text_props_active; ++pi)
4371 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004372 int tpi = text_prop_idxs[pi];
4373 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004374
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004375 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004376 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004377 int pt_attr = syn_id2attr(pt->pt_hl_id);
4378
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004379 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004380 if (text_prop_attr == 0)
4381 text_prop_attr = pt_attr;
4382 else
4383 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004384 }
4385 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004386 }
4387 }
4388#endif
4389
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004390 /* Decide which of the highlight attributes to use. */
4391 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004392#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004393 if (area_attr != 0)
4394 char_attr = hl_combine_attr(line_attr, area_attr);
4395 else if (search_attr != 0)
4396 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004397 /* Use line_attr when not in the Visual or 'incsearch' area
4398 * (area_attr may be 0 when "noinvcur" is set). */
4399 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004400 || vcol < fromcol || vcol_prev < fromcol_prev
4401 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004402 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004403#else
4404 if (area_attr != 0)
4405 char_attr = area_attr;
4406 else if (search_attr != 0)
4407 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004408#endif
4409 else
4410 {
4411 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004412#ifdef FEAT_TEXT_PROP
4413 if (text_prop_type != NULL)
4414 char_attr = text_prop_attr;
4415 else
4416#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004417#ifdef FEAT_SYN_HL
4418 if (has_syntax)
4419 char_attr = syntax_attr;
4420 else
4421#endif
4422 char_attr = 0;
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425
4426 /*
4427 * Get the next character to put on the screen.
4428 */
4429 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004430 * The "p_extra" points to the extra stuff that is inserted to
4431 * represent special characters (non-printable stuff) and other
4432 * things. When all characters are the same, c_extra is used.
4433 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4434 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4436 */
4437 if (n_extra > 0)
4438 {
4439 if (c_extra != NUL)
4440 {
4441 c = c_extra;
4442#ifdef FEAT_MBYTE
4443 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004444 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 {
4446 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004447 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004448 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 else
4451 mb_utf8 = FALSE;
4452#endif
4453 }
4454 else
4455 {
4456 c = *p_extra;
4457#ifdef FEAT_MBYTE
4458 if (has_mbyte)
4459 {
4460 mb_c = c;
4461 if (enc_utf8)
4462 {
4463 /* If the UTF-8 character is more than one byte:
4464 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004465 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 mb_utf8 = FALSE;
4467 if (mb_l > n_extra)
4468 mb_l = 1;
4469 else if (mb_l > 1)
4470 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004473 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 }
4476 else
4477 {
4478 /* if this is a DBCS character, put it in "mb_c" */
4479 mb_l = MB_BYTE2LEN(c);
4480 if (mb_l >= n_extra)
4481 mb_l = 1;
4482 else if (mb_l > 1)
4483 mb_c = (c << 8) + p_extra[1];
4484 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004485 if (mb_l == 0) /* at the NUL at end-of-line */
4486 mb_l = 1;
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 /* If a double-width char doesn't fit display a '>' in the
4489 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004490 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491# ifdef FEAT_RIGHTLEFT
4492 wp->w_p_rl ? (col <= 0) :
4493# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004494 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 && (*mb_char2cells)(mb_c) == 2)
4496 {
4497 c = '>';
4498 mb_c = c;
4499 mb_l = 1;
4500 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004501 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 /* put the pointer back to output the double-width
4503 * character at the start of the next line. */
4504 ++n_extra;
4505 --p_extra;
4506 }
4507 else
4508 {
4509 n_extra -= mb_l - 1;
4510 p_extra += mb_l - 1;
4511 }
4512 }
4513#endif
4514 ++p_extra;
4515 }
4516 --n_extra;
4517 }
4518 else
4519 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004520#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004521 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004522#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004523
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004524 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004525 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 /*
4527 * Get a character from the line itself.
4528 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004529 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004530#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004531 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533#ifdef FEAT_MBYTE
4534 if (has_mbyte)
4535 {
4536 mb_c = c;
4537 if (enc_utf8)
4538 {
4539 /* If the UTF-8 character is more than one byte: Decode it
4540 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004541 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 mb_utf8 = FALSE;
4543 if (mb_l > 1)
4544 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004545 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 /* Overlong encoded ASCII or ASCII with composing char
4547 * is displayed normally, except a NUL. */
4548 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004549 {
4550 c = mb_c;
4551# ifdef FEAT_LINEBREAK
4552 c0 = mb_c;
4553# endif
4554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004556
4557 /* At start of the line we can have a composing char.
4558 * Draw it as a space with a composing char. */
4559 if (utf_iscomposing(mb_c))
4560 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004561 int i;
4562
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004563 for (i = Screen_mco - 1; i > 0; --i)
4564 u8cc[i] = u8cc[i - 1];
4565 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004566 mb_c = ' ';
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569
4570 if ((mb_l == 1 && c >= 0x80)
4571 || (mb_l >= 1 && mb_c == 0)
4572 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004573# ifdef UNICODE16
4574 || mb_c >= 0x10000
4575# endif
4576 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578 /*
4579 * Illegal UTF-8 byte: display as <xx>.
4580 * Non-BMP character : display as ? or fullwidth ?.
4581 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004582# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (wp->w_p_rl) /* reverse */
4589 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004592# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 else if (utf_char2cells(mb_c) != 2)
4594 STRCPY(extra, "?");
4595 else
4596 /* 0xff1f in UTF-8: full-width '?' */
4597 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 p_extra = extra;
4601 c = *p_extra;
4602 mb_c = mb_ptr2char_adv(&p_extra);
4603 mb_utf8 = (c >= 0x80);
4604 n_extra = (int)STRLEN(p_extra);
4605 c_extra = NUL;
4606 if (area_attr == 0 && search_attr == 0)
4607 {
4608 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004609 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 saved_attr2 = char_attr; /* save current attr */
4611 }
4612 }
4613 else if (mb_l == 0) /* at the NUL at end-of-line */
4614 mb_l = 1;
4615#ifdef FEAT_ARABIC
4616 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4617 {
4618 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004619 int pc, pc1, nc;
4620 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
4622 /* The idea of what is the previous and next
4623 * character depends on 'rightleft'. */
4624 if (wp->w_p_rl)
4625 {
4626 pc = prev_c;
4627 pc1 = prev_c1;
4628 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004629 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 else
4632 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004633 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004635 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637 prev_c = mb_c;
4638
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004639 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 else
4642 prev_c = mb_c;
4643#endif
4644 }
4645 else /* enc_dbcs */
4646 {
4647 mb_l = MB_BYTE2LEN(c);
4648 if (mb_l == 0) /* at the NUL at end-of-line */
4649 mb_l = 1;
4650 else if (mb_l > 1)
4651 {
4652 /* We assume a second byte below 32 is illegal.
4653 * Hopefully this is OK for all double-byte encodings!
4654 */
4655 if (ptr[1] >= 32)
4656 mb_c = (c << 8) + ptr[1];
4657 else
4658 {
4659 if (ptr[1] == NUL)
4660 {
4661 /* head byte at end of line */
4662 mb_l = 1;
4663 transchar_nonprint(extra, c);
4664 }
4665 else
4666 {
4667 /* illegal tail byte */
4668 mb_l = 2;
4669 STRCPY(extra, "XX");
4670 }
4671 p_extra = extra;
4672 n_extra = (int)STRLEN(extra) - 1;
4673 c_extra = NUL;
4674 c = *p_extra++;
4675 if (area_attr == 0 && search_attr == 0)
4676 {
4677 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004678 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 saved_attr2 = char_attr; /* save current attr */
4680 }
4681 mb_c = c;
4682 }
4683 }
4684 }
4685 /* If a double-width char doesn't fit display a '>' in the
4686 * last column; the character is displayed at the start of the
4687 * next line. */
4688 if ((
4689# ifdef FEAT_RIGHTLEFT
4690 wp->w_p_rl ? (col <= 0) :
4691# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004692 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 && (*mb_char2cells)(mb_c) == 2)
4694 {
4695 c = '>';
4696 mb_c = c;
4697 mb_utf8 = FALSE;
4698 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004699 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 /* Put pointer back so that the character will be
4701 * displayed at the start of the next line. */
4702 --ptr;
4703 }
4704 else if (*ptr != NUL)
4705 ptr += mb_l - 1;
4706
4707 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004708 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004709 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004710 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004713 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 c = ' ';
4715 if (area_attr == 0 && search_attr == 0)
4716 {
4717 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004718 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 saved_attr2 = char_attr; /* save current attr */
4720 }
4721 mb_c = c;
4722 mb_utf8 = FALSE;
4723 mb_l = 1;
4724 }
4725
4726 }
4727#endif
4728 ++ptr;
4729
4730 if (extra_check)
4731 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004732#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004733 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004734#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004735
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004736#ifdef FEAT_TERMINAL
4737 if (get_term_attr)
4738 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004739 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004740
4741 if (!attr_pri)
4742 char_attr = syntax_attr;
4743 else
4744 char_attr = hl_combine_attr(syntax_attr, char_attr);
4745 }
4746#endif
4747
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004748#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004749 // Get syntax attribute, unless still at the start of the line
4750 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004751 v = (long)(ptr - line);
4752 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 /* Get the syntax attribute for the character. If there
4755 * is an error, disable syntax highlighting. */
4756 save_did_emsg = did_emsg;
4757 did_emsg = FALSE;
4758
Bram Moolenaar217ad922005-03-20 22:37:15 +00004759 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004760# ifdef FEAT_SPELL
4761 has_spell ? &can_spell :
4762# endif
4763 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004766 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004767 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004768 has_syntax = FALSE;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
4771 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004772#ifdef SYN_TIME_LIMIT
4773 if (wp->w_s->b_syn_slow)
4774 has_syntax = FALSE;
4775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 /* Need to get the line again, a multi-line regexp may
4778 * have made it invalid. */
4779 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4780 ptr = line + v;
4781
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004782# ifdef FEAT_TEXT_PROP
4783 // Text properties overrule syntax highlighting.
4784 if (text_prop_attr == 0)
4785#endif
4786 {
4787 if (!attr_pri)
4788 char_attr = syntax_attr;
4789 else
4790 char_attr = hl_combine_attr(syntax_attr, char_attr);
4791 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004792# ifdef FEAT_CONCEAL
4793 /* no concealing past the end of the line, it interferes
4794 * with line highlighting */
4795 if (c == NUL)
4796 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004797 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004798 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004801#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004802
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004803#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004804 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004805 * Only do this when there is no syntax highlighting, the
4806 * @Spell cluster is not used or the current syntax item
4807 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004808 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004809 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004810 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004811 if (c != 0 && (
4812# ifdef FEAT_SYN_HL
4813 !has_syntax ||
4814# endif
4815 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004816 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004817 char_u *prev_ptr, *p;
4818 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004819 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004820# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004821 if (has_mbyte)
4822 {
4823 prev_ptr = ptr - mb_l;
4824 v -= mb_l - 1;
4825 }
4826 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004827# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004828 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004829
4830 /* Use nextline[] if possible, it has the start of the
4831 * next line concatenated. */
4832 if ((prev_ptr - line) - nextlinecol >= 0)
4833 p = nextline + (prev_ptr - line) - nextlinecol;
4834 else
4835 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004836 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004837 len = spell_check(wp, p, &spell_hlf, &cap_col,
4838 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004839 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004840
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004841 /* In Insert mode only highlight a word that
4842 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004843 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004844 && (State & INSERT) != 0
4845 && wp->w_cursor.lnum == lnum
4846 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004847 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004848 && wp->w_cursor.col < (colnr_T)word_end)
4849 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004850 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004851 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004852 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004853
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004854 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004855 && (p - nextline) + len > nextline_idx)
4856 {
4857 /* Remember that the good word continues at the
4858 * start of the next line. */
4859 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004860 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004861 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004862
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004863 /* Turn index into actual attributes. */
4864 if (spell_hlf != HLF_COUNT)
4865 spell_attr = highlight_attr[spell_hlf];
4866
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004867 if (cap_col > 0)
4868 {
4869 if (p != prev_ptr
4870 && (p - nextline) + cap_col >= nextline_idx)
4871 {
4872 /* Remember that the word in the next line
4873 * must start with a capital. */
4874 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004875 cap_col = (int)((p - nextline) + cap_col
4876 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004877 }
4878 else
4879 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004880 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004881 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004882 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004883 }
4884 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004885 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004886 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004887 char_attr = hl_combine_attr(char_attr, spell_attr);
4888 else
4889 char_attr = hl_combine_attr(spell_attr, char_attr);
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#endif
4892#ifdef FEAT_LINEBREAK
4893 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004894 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004896 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004897 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004899# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004900 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004901# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004902 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004904 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004906 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004907
Bram Moolenaar597a4222014-06-25 14:39:50 +02004908 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004909 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004910 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004911 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004912# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004913 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004914 wp->w_buffer->b_p_vts_array) - 1;
4915# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004916 n_extra = (int)wp->w_buffer->b_p_ts
4917 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004918# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004919
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004920# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004921 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004922# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004924# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004925 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004926 {
4927#ifdef FEAT_CONCEAL
4928 if (c == TAB)
4929 /* See "Tab alignment" below. */
4930 FIX_FOR_BOGUSCOLS;
4931#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004932 if (!wp->w_p_list)
4933 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936#endif
4937
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004938 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4939 */
4940 if (wp->w_p_list
4941 && (((c == 160
4942#ifdef FEAT_MBYTE
4943 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4944#endif
4945 ) && lcs_nbsp)
4946 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4947 {
4948 c = (c == ' ') ? lcs_space : lcs_nbsp;
4949 if (area_attr == 0 && search_attr == 0)
4950 {
4951 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004952 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004953 saved_attr2 = char_attr; /* save current attr */
4954 }
4955#ifdef FEAT_MBYTE
4956 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004957 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004958 {
4959 mb_utf8 = TRUE;
4960 u8cc[0] = 0;
4961 c = 0xc0;
4962 }
4963 else
4964 mb_utf8 = FALSE;
4965#endif
4966 }
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4969 {
4970 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004971 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004974 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 saved_attr2 = char_attr; /* save current attr */
4976 }
4977#ifdef FEAT_MBYTE
4978 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004979 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004982 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004983 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 else
4986 mb_utf8 = FALSE;
4987#endif
4988 }
4989 }
4990
4991 /*
4992 * Handling of non-printable characters.
4993 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004994 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
4996 /*
4997 * when getting a character from the file, we may have to
4998 * turn it into something else on the way to putting it
4999 * into "ScreenLines".
5000 */
5001 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5002 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005003 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005004 long vcol_adjusted = vcol; /* removed showbreak length */
5005#ifdef FEAT_LINEBREAK
5006 /* only adjust the tab_len, when at the first column
5007 * after the showbreak value was drawn */
5008 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5009 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005012#ifdef FEAT_VARTABS
5013 tab_len = tabstop_padding(vcol_adjusted,
5014 wp->w_buffer->b_p_ts,
5015 wp->w_buffer->b_p_vts_array) - 1;
5016#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005017 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005018 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5019#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005020
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005021#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005022 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005023#endif
5024 /* tab amount depends on current column */
5025 n_extra = tab_len;
5026#ifdef FEAT_LINEBREAK
5027 else
5028 {
5029 char_u *p;
5030 int len = n_extra;
5031 int i;
5032 int saved_nextra = n_extra;
5033
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005034#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005035 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005036 /* there are characters to conceal */
5037 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005038 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5039 */
5040 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5041 && n_extra > tab_len)
5042 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005043#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005044
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005045 /* if n_extra > 0, it gives the number of chars, to
5046 * use for a tab, else we need to calculate the width
5047 * for a tab */
5048#ifdef FEAT_MBYTE
5049 len = (tab_len * mb_char2len(lcs_tab2));
5050 if (n_extra > 0)
5051 len += n_extra - tab_len;
5052#endif
5053 c = lcs_tab1;
5054 p = alloc((unsigned)(len + 1));
5055 vim_memset(p, ' ', len);
5056 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005057 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005058 p_extra_free = p;
5059 for (i = 0; i < tab_len; i++)
5060 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005061 if (*p == NUL)
5062 {
5063 tab_len = i;
5064 break;
5065 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005066#ifdef FEAT_MBYTE
5067 mb_char2bytes(lcs_tab2, p);
5068 p += mb_char2len(lcs_tab2);
5069 n_extra += mb_char2len(lcs_tab2)
5070 - (saved_nextra > 0 ? 1 : 0);
5071#else
5072 p[i] = lcs_tab2;
5073#endif
5074 }
5075 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005076#ifdef FEAT_CONCEAL
5077 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5078 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005079 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005080 n_extra -= vcol_off;
5081#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005082 }
5083#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005084#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005085 {
5086 int vc_saved = vcol_off;
5087
5088 /* Tab alignment should be identical regardless of
5089 * 'conceallevel' value. So tab compensates of all
5090 * previous concealed characters, and thus resets
5091 * vcol_off and boguscols accumulated so far in the
5092 * line. Note that the tab can be longer than
5093 * 'tabstop' when there are concealed characters. */
5094 FIX_FOR_BOGUSCOLS;
5095
5096 /* Make sure, the highlighting for the tab char will be
5097 * correctly set further below (effectively reverts the
5098 * FIX_FOR_BOGSUCOLS macro */
5099 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005100 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005101 tab_len += vc_saved;
5102 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#ifdef FEAT_MBYTE
5105 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5106#endif
5107 if (wp->w_p_list)
5108 {
5109 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005110#ifdef FEAT_LINEBREAK
5111 if (wp->w_p_lbr)
5112 c_extra = NUL; /* using p_extra from above */
5113 else
5114#endif
5115 c_extra = lcs_tab2;
5116 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005117 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 saved_attr2 = char_attr; /* save current attr */
5119#ifdef FEAT_MBYTE
5120 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005121 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005124 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005125 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127#endif
5128 }
5129 else
5130 {
5131 c_extra = ' ';
5132 c = ' ';
5133 }
5134 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005135 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005136 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005137 || ((fromcol >= 0 || fromcol_prev >= 0)
5138 && tocol > vcol
5139 && VIsual_mode != Ctrl_V
5140 && (
5141# ifdef FEAT_RIGHTLEFT
5142 wp->w_p_rl ? (col >= 0) :
5143# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005144 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005145 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005146 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005147 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005148 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005150 /* Display a '$' after the line or highlight an extra
5151 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5153 /* For a diff line the highlighting continues after the
5154 * "$". */
5155 if (
5156# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005157 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158# ifdef LINE_ATTR
5159 &&
5160# endif
5161# endif
5162# ifdef LINE_ATTR
5163 line_attr == 0
5164# endif
5165 )
5166#endif
5167 {
5168#ifdef FEAT_VIRTUALEDIT
5169 /* In virtualedit, visual selections may extend
5170 * beyond end of line. */
5171 if (area_highlighting && virtual_active()
5172 && tocol != MAXCOL && vcol < tocol)
5173 n_extra = 0;
5174 else
5175#endif
5176 {
5177 p_extra = at_end_str;
5178 n_extra = 1;
5179 c_extra = NUL;
5180 }
5181 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005182 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005183 c = lcs_eol;
5184 else
5185 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 lcs_eol_one = -1;
5187 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005188 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005190 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 n_attr = 1;
5192 }
5193#ifdef FEAT_MBYTE
5194 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005195 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005198 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005199 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 else
5202 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5203#endif
5204 }
5205 else if (c != NUL)
5206 {
5207 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005208 if (n_extra == 0)
5209 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#ifdef FEAT_RIGHTLEFT
5211 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5212 rl_mirror(p_extra); /* reverse "<12>" */
5213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005215#ifdef FEAT_LINEBREAK
5216 if (wp->w_p_lbr)
5217 {
5218 char_u *p;
5219
5220 c = *p_extra;
5221 p = alloc((unsigned)n_extra + 1);
5222 vim_memset(p, ' ', n_extra);
5223 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5224 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005225 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005226 p_extra_free = p_extra = p;
5227 }
5228 else
5229#endif
5230 {
5231 n_extra = byte2cells(c) - 1;
5232 c = *p_extra++;
5233 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005234 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005237 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 saved_attr2 = char_attr; /* save current attr */
5239 }
5240#ifdef FEAT_MBYTE
5241 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5242#endif
5243 }
5244#ifdef FEAT_VIRTUALEDIT
5245 else if (VIsual_active
5246 && (VIsual_mode == Ctrl_V
5247 || VIsual_mode == 'v')
5248 && virtual_active()
5249 && tocol != MAXCOL
5250 && vcol < tocol
5251 && (
5252# ifdef FEAT_RIGHTLEFT
5253 wp->w_p_rl ? (col >= 0) :
5254# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005255 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
5257 c = ' ';
5258 --ptr; /* put it back at the NUL */
5259 }
5260#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005261#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 else if ((
5263# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005264 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005266# ifdef FEAT_TERMINAL
5267 term_attr != 0 ||
5268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 ) && (
5271# ifdef FEAT_RIGHTLEFT
5272 wp->w_p_rl ? (col >= 0) :
5273# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005274 (col
5275# ifdef FEAT_CONCEAL
5276 - boguscols
5277# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005278 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 /* Highlight until the right side of the window */
5281 c = ' ';
5282 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005283
5284 /* Remember we do the char for line highlighting. */
5285 ++did_line_attr;
5286
5287 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005288 if (line_attr != 0 && char_attr == search_attr
5289 && (did_line_attr > 1
5290 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005291 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292# ifdef FEAT_DIFF
5293 if (diff_hlf == HLF_TXD)
5294 {
5295 diff_hlf = HLF_CHD;
5296 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005297 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005298 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005299 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5300 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005301 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 }
5304# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005305# ifdef FEAT_TERMINAL
5306 if (term_attr != 0)
5307 {
5308 char_attr = term_attr;
5309 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5310 char_attr = hl_combine_attr(char_attr,
5311 HL_ATTR(HLF_CUL));
5312 }
5313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315#endif
5316 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005317
5318#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005319 if ( wp->w_p_cole > 0
5320 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005321 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005322 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005323 && !(lnum_in_visual_area
5324 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325 {
5326 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005327 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005328 && (syn_get_sub_char() != NUL || match_conc
5329 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005330 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005331 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005332 /* First time at this concealed item: display one
5333 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005334 if (match_conc)
5335 c = match_conc;
5336 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005337 c = syn_get_sub_char();
5338 else if (lcs_conceal != NUL)
5339 c = lcs_conceal;
5340 else
5341 c = ' ';
5342
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005343 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005344
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005345 if (n_extra > 0)
5346 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005347 vcol += n_extra;
5348 if (wp->w_p_wrap && n_extra > 0)
5349 {
5350# ifdef FEAT_RIGHTLEFT
5351 if (wp->w_p_rl)
5352 {
5353 col -= n_extra;
5354 boguscols -= n_extra;
5355 }
5356 else
5357# endif
5358 {
5359 boguscols += n_extra;
5360 col += n_extra;
5361 }
5362 }
5363 n_extra = 0;
5364 n_attr = 0;
5365 }
5366 else if (n_skip == 0)
5367 {
5368 is_concealing = TRUE;
5369 n_skip = 1;
5370 }
5371# ifdef FEAT_MBYTE
5372 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005373 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005374 {
5375 mb_utf8 = TRUE;
5376 u8cc[0] = 0;
5377 c = 0xc0;
5378 }
5379 else
5380 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5381# endif
5382 }
5383 else
5384 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005385 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005386 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005387 }
5388#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005391#ifdef FEAT_CONCEAL
5392 /* In the cursor line and we may be concealing characters: correct
5393 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005394 if (!did_wcol && draw_state == WL_LINE
5395 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005396 && conceal_cursor_line(wp)
5397 && (int)wp->w_virtcol <= vcol + n_skip)
5398 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005399# ifdef FEAT_RIGHTLEFT
5400 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005401 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005402 else
5403# endif
5404 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005405 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005406 did_wcol = TRUE;
5407 }
5408#endif
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 /* Don't override visual selection highlighting. */
5411 if (n_attr > 0
5412 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005413 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 char_attr = extra_attr;
5415
Bram Moolenaar81695252004-12-29 20:58:21 +00005416#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 /* XIM don't send preedit_start and preedit_end, but they send
5418 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5419 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005420 if (p_imst == IM_ON_THE_SPOT
5421 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005422 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 && (State & INSERT)
5424 && !p_imdisable
5425 && im_is_preediting()
5426 && draw_state == WL_LINE)
5427 {
5428 colnr_T tcol;
5429
5430 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005431 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 else
5433 tcol = preedit_end_col;
5434 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5435 {
5436 if (feedback_old_attr < 0)
5437 {
5438 feedback_col = 0;
5439 feedback_old_attr = char_attr;
5440 }
5441 char_attr = im_get_feedback_attr(feedback_col);
5442 if (char_attr < 0)
5443 char_attr = feedback_old_attr;
5444 feedback_col++;
5445 }
5446 else if (feedback_old_attr >= 0)
5447 {
5448 char_attr = feedback_old_attr;
5449 feedback_old_attr = -1;
5450 feedback_col = 0;
5451 }
5452 }
5453#endif
5454 /*
5455 * Handle the case where we are in column 0 but not on the first
5456 * character of the line and the user wants us to show us a
5457 * special character (via 'listchars' option "precedes:<char>".
5458 */
5459 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005460 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5462#ifdef FEAT_DIFF
5463 && filler_todo <= 0
5464#endif
5465 && draw_state > WL_NR
5466 && c != NUL)
5467 {
5468 c = lcs_prec;
5469 lcs_prec_todo = NUL;
5470#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005471 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5472 {
5473 /* Double-width character being overwritten by the "precedes"
5474 * character, need to fill up half the character. */
5475 c_extra = MB_FILLER_CHAR;
5476 n_extra = 1;
5477 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005478 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005481 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005484 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005485 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else
5488 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5489#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005490 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
5492 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005493 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 n_attr3 = 1;
5495 }
5496 }
5497
5498 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005501 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005502#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005503 || did_line_attr == 1
5504#endif
5505 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005507#ifdef FEAT_SEARCH_EXTRA
5508 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005509
5510 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005511 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005512 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005513#endif
5514
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005515 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * highlight match at end of line. If it's beyond the last
5517 * char on the screen, just overwrite that one (tricky!) Not
5518 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005519#ifdef FEAT_SEARCH_EXTRA
5520 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005521 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005522 prevcol_hl_flag = TRUE;
5523 else
5524 {
5525 cur = wp->w_match_head;
5526 while (cur != NULL)
5527 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005528 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005529 {
5530 prevcol_hl_flag = TRUE;
5531 break;
5532 }
5533 cur = cur->next;
5534 }
5535 }
5536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005538 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005539 && (VIsual_mode != Ctrl_V
5540 || lnum == VIsual.lnum
5541 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005542 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#ifdef FEAT_SEARCH_EXTRA
5544 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005545 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005546# ifdef FEAT_SYN_HL
5547 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5548 && !(wp == curwin && VIsual_active))
5549# endif
5550# ifdef FEAT_DIFF
5551 && diff_hlf == (hlf_T)0
5552# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005553# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005554 && did_line_attr <= 1
5555# endif
5556 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557#endif
5558 ))
5559 {
5560 int n = 0;
5561
5562#ifdef FEAT_RIGHTLEFT
5563 if (wp->w_p_rl)
5564 {
5565 if (col < 0)
5566 n = 1;
5567 }
5568 else
5569#endif
5570 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005571 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 n = -1;
5573 }
5574 if (n != 0)
5575 {
5576 /* At the window boundary, highlight the last character
5577 * instead (better than nothing). */
5578 off += n;
5579 col += n;
5580 }
5581 else
5582 {
5583 /* Add a blank character to highlight. */
5584 ScreenLines[off] = ' ';
5585#ifdef FEAT_MBYTE
5586 if (enc_utf8)
5587 ScreenLinesUC[off] = 0;
5588#endif
5589 }
5590#ifdef FEAT_SEARCH_EXTRA
5591 if (area_attr == 0)
5592 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005593 /* Use attributes from match with highest priority among
5594 * 'search_hl' and the match list. */
5595 char_attr = search_hl.attr;
5596 cur = wp->w_match_head;
5597 shl_flag = FALSE;
5598 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005599 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005600 if (shl_flag == FALSE
5601 && ((cur != NULL
5602 && cur->priority > SEARCH_HL_PRIORITY)
5603 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005604 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005605 shl = &search_hl;
5606 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005607 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005608 else
5609 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005610 if ((ptr - line) - 1 == (long)shl->startcol
5611 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005612 char_attr = shl->attr;
5613 if (shl != &search_hl && cur != NULL)
5614 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617#endif
5618 ScreenAttrs[off] = char_attr;
5619#ifdef FEAT_RIGHTLEFT
5620 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005623 --off;
5624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 else
5626#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005629 ++off;
5630 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005631 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005632#ifdef FEAT_SYN_HL
5633 eol_hl_off = 1;
5634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar91170f82006-05-05 21:15:17 +00005638 /*
5639 * At end of the text line.
5640 */
5641 if (c == NUL)
5642 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005643#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005644 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005645 if (wp->w_p_wrap)
5646 v = wp->w_skipcol;
5647 else
5648 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005649
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005650 /* check if line ends before left margin */
5651 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005652 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005653#ifdef FEAT_CONCEAL
5654 /* Get rid of the boguscols now, we want to draw until the right
5655 * edge for 'cursorcolumn'. */
5656 col -= boguscols;
5657 boguscols = 0;
5658#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005659
5660 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005661 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005662
5663 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005664 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5665 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005666 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005667 && lnum != wp->w_cursor.lnum)
5668 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005669# ifdef FEAT_RIGHTLEFT
5670 && !wp->w_p_rl
5671# endif
5672 )
5673 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005674 int rightmost_vcol = 0;
5675 int i;
5676
5677 if (wp->w_p_cuc)
5678 rightmost_vcol = wp->w_virtcol;
5679 if (draw_color_col)
5680 /* determine rightmost colorcolumn to possibly draw */
5681 for (i = 0; color_cols[i] >= 0; ++i)
5682 if (rightmost_vcol < color_cols[i])
5683 rightmost_vcol = color_cols[i];
5684
Bram Moolenaar02631462017-09-22 15:20:32 +02005685 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005686 {
5687 ScreenLines[off] = ' ';
5688#ifdef FEAT_MBYTE
5689 if (enc_utf8)
5690 ScreenLinesUC[off] = 0;
5691#endif
5692 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005693 if (draw_color_col)
5694 draw_color_col = advance_color_col(VCOL_HLC,
5695 &color_cols);
5696
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005697 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005698 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005699 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005700 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005701 else
5702 ScreenAttrs[off++] = 0;
5703
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005704 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005705 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005706
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005707 ++vcol;
5708 }
5709 }
5710#endif
5711
Bram Moolenaar53f81742017-09-22 14:35:51 +02005712 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005713 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 row++;
5715
5716 /*
5717 * Update w_cline_height and w_cline_folded if the cursor line was
5718 * updated (saves a call to plines() later).
5719 */
5720 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5721 {
5722 curwin->w_cline_row = startrow;
5723 curwin->w_cline_height = row - startrow;
5724#ifdef FEAT_FOLDING
5725 curwin->w_cline_folded = FALSE;
5726#endif
5727 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5728 }
5729
5730 break;
5731 }
5732
5733 /* line continues beyond line end */
5734 if (lcs_ext
5735 && !wp->w_p_wrap
5736#ifdef FEAT_DIFF
5737 && filler_todo <= 0
5738#endif
5739 && (
5740#ifdef FEAT_RIGHTLEFT
5741 wp->w_p_rl ? col == 0 :
5742#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005743 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005745 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5747 {
5748 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005749 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750#ifdef FEAT_MBYTE
5751 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005752 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005755 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005756 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758 else
5759 mb_utf8 = FALSE;
5760#endif
5761 }
5762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005763#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764 /* advance to the next 'colorcolumn' */
5765 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005766 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005767
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005768 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005769 * highlight the cursor position itself.
5770 * Also highlight the 'colorcolumn' if it is different than
5771 * 'cursorcolumn' */
5772 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005773 if (draw_state == WL_LINE && !lnum_in_visual_area
5774 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005775 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005776 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005777 && lnum != wp->w_cursor.lnum)
5778 {
5779 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005780 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005781 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005782 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005783 {
5784 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005785 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005786 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005787 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005788#endif
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 /*
5791 * Store character to be displayed.
5792 * Skip characters that are left of the screen for 'nowrap'.
5793 */
5794 vcol_prev = vcol;
5795 if (draw_state < WL_LINE || n_skip <= 0)
5796 {
5797 /*
5798 * Store the character.
5799 */
5800#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5801 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5802 {
5803 /* A double-wide character is: put first halve in left cell. */
5804 --off;
5805 --col;
5806 }
5807#endif
5808 ScreenLines[off] = c;
5809#ifdef FEAT_MBYTE
5810 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005811 {
5812 if ((mb_c & 0xff00) == 0x8e00)
5813 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 else if (enc_utf8)
5817 {
5818 if (mb_utf8)
5819 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005820 int i;
5821
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005823 if ((c & 0xff) == 0)
5824 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005825 for (i = 0; i < Screen_mco; ++i)
5826 {
5827 ScreenLinesC[i][off] = u8cc[i];
5828 if (u8cc[i] == 0)
5829 break;
5830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
5832 else
5833 ScreenLinesUC[off] = 0;
5834 }
5835 if (multi_attr)
5836 {
5837 ScreenAttrs[off] = multi_attr;
5838 multi_attr = 0;
5839 }
5840 else
5841#endif
5842 ScreenAttrs[off] = char_attr;
5843
5844#ifdef FEAT_MBYTE
5845 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5846 {
5847 /* Need to fill two screen columns. */
5848 ++off;
5849 ++col;
5850 if (enc_utf8)
5851 /* UTF-8: Put a 0 in the second screen char. */
5852 ScreenLines[off] = 0;
5853 else
5854 /* DBCS: Put second byte in the second screen char. */
5855 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005856 if (draw_state > WL_NR
5857#ifdef FEAT_DIFF
5858 && filler_todo <= 0
5859#endif
5860 )
5861 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 /* When "tocol" is halfway a character, set it to the end of
5863 * the character, otherwise highlighting won't stop. */
5864 if (tocol == vcol)
5865 ++tocol;
5866#ifdef FEAT_RIGHTLEFT
5867 if (wp->w_p_rl)
5868 {
5869 /* now it's time to backup one cell */
5870 --off;
5871 --col;
5872 }
5873#endif
5874 }
5875#endif
5876#ifdef FEAT_RIGHTLEFT
5877 if (wp->w_p_rl)
5878 {
5879 --off;
5880 --col;
5881 }
5882 else
5883#endif
5884 {
5885 ++off;
5886 ++col;
5887 }
5888 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005889#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005890 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005891 {
5892 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005893 ++vcol_off;
5894 if (n_extra > 0)
5895 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005896 if (wp->w_p_wrap)
5897 {
5898 /*
5899 * Special voodoo required if 'wrap' is on.
5900 *
5901 * Advance the column indicator to force the line
5902 * drawing to wrap early. This will make the line
5903 * take up the same screen space when parts are concealed,
5904 * so that cursor line computations aren't messed up.
5905 *
5906 * To avoid the fictitious advance of 'col' causing
5907 * trailing junk to be written out of the screen line
5908 * we are building, 'boguscols' keeps track of the number
5909 * of bad columns we have advanced.
5910 */
5911 if (n_extra > 0)
5912 {
5913 vcol += n_extra;
5914# ifdef FEAT_RIGHTLEFT
5915 if (wp->w_p_rl)
5916 {
5917 col -= n_extra;
5918 boguscols -= n_extra;
5919 }
5920 else
5921# endif
5922 {
5923 col += n_extra;
5924 boguscols += n_extra;
5925 }
5926 n_extra = 0;
5927 n_attr = 0;
5928 }
5929
5930
5931# ifdef FEAT_MBYTE
5932 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5933 {
5934 /* Need to fill two screen columns. */
5935# ifdef FEAT_RIGHTLEFT
5936 if (wp->w_p_rl)
5937 {
5938 --boguscols;
5939 --col;
5940 }
5941 else
5942# endif
5943 {
5944 ++boguscols;
5945 ++col;
5946 }
5947 }
5948# endif
5949
5950# ifdef FEAT_RIGHTLEFT
5951 if (wp->w_p_rl)
5952 {
5953 --boguscols;
5954 --col;
5955 }
5956 else
5957# endif
5958 {
5959 ++boguscols;
5960 ++col;
5961 }
5962 }
5963 else
5964 {
5965 if (n_extra > 0)
5966 {
5967 vcol += n_extra;
5968 n_extra = 0;
5969 n_attr = 0;
5970 }
5971 }
5972
5973 }
5974#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 else
5976 --n_skip;
5977
Bram Moolenaar64486672010-05-16 15:46:46 +02005978 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5979 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005980 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981#ifdef FEAT_DIFF
5982 && filler_todo <= 0
5983#endif
5984 )
5985 ++vcol;
5986
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005987#ifdef FEAT_SYN_HL
5988 if (vcol_save_attr >= 0)
5989 char_attr = vcol_save_attr;
5990#endif
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 /* restore attributes after "predeces" in 'listchars' */
5993 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5994 char_attr = saved_attr3;
5995
5996 /* restore attributes after last 'listchars' or 'number' char */
5997 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5998 char_attr = saved_attr2;
5999
6000 /*
6001 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006002 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 */
6004 if ((
6005#ifdef FEAT_RIGHTLEFT
6006 wp->w_p_rl ? (col < 0) :
6007#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006008 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 && (*ptr != NUL
6010#ifdef FEAT_DIFF
6011 || filler_todo > 0
6012#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006013 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6015 )
6016 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006017#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006018 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02006019 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006020 boguscols = 0;
6021#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006022 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02006023 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02006024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 ++row;
6026 ++screen_row;
6027
6028 /* When not wrapping and finished diff lines, or when displayed
6029 * '$' and highlighting until last column, break here. */
6030 if ((!wp->w_p_wrap
6031#ifdef FEAT_DIFF
6032 && filler_todo <= 0
6033#endif
6034 ) || lcs_eol_one == -1)
6035 break;
6036
6037 /* When the window is too narrow draw all "@" lines. */
6038 if (draw_state != WL_LINE
6039#ifdef FEAT_DIFF
6040 && filler_todo <= 0
6041#endif
6042 )
6043 {
6044 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 row = endrow;
6047 }
6048
6049 /* When line got too long for screen break here. */
6050 if (row == endrow)
6051 {
6052 ++row;
6053 break;
6054 }
6055
6056 if (screen_cur_row == screen_row - 1
6057#ifdef FEAT_DIFF
6058 && filler_todo <= 0
6059#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006060 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
6062 /* Remember that the line wraps, used for modeless copy. */
6063 LineWraps[screen_row - 1] = TRUE;
6064
6065 /*
6066 * Special trick to make copy/paste of wrapped lines work with
6067 * xterm/screen: write an extra character beyond the end of
6068 * the line. This will work with all terminal types
6069 * (regardless of the xn,am settings).
6070 * Only do this on a fast tty.
6071 * Only do this if the cursor is on the current line
6072 * (something has been written in it).
6073 * Don't do this for the GUI.
6074 * Don't do this for double-width characters.
6075 * Don't do this for a window not at the right screen border.
6076 */
6077 if (p_tf
6078#ifdef FEAT_GUI
6079 && !gui.in_use
6080#endif
6081#ifdef FEAT_MBYTE
6082 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006083 && ((*mb_off2cells)(LineOffset[screen_row],
6084 LineOffset[screen_row] + screen_Columns)
6085 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006087 + (int)Columns - 2,
6088 LineOffset[screen_row] + screen_Columns)
6089 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090#endif
6091 )
6092 {
6093 /* First make sure we are at the end of the screen line,
6094 * then output the same character again to let the
6095 * terminal know about the wrap. If the terminal doesn't
6096 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006097 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 screen_char(LineOffset[screen_row - 1]
6099 + (unsigned)Columns - 1,
6100 screen_row - 1, (int)(Columns - 1));
6101
6102#ifdef FEAT_MBYTE
6103 /* When there is a multi-byte character, just output a
6104 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006105 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6106 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 out_char(' ');
6108 else
6109#endif
6110 out_char(ScreenLines[LineOffset[screen_row - 1]
6111 + (Columns - 1)]);
6112 /* force a redraw of the first char on the next line */
6113 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6114 screen_start(); /* don't know where cursor is now */
6115 }
6116 }
6117
6118 col = 0;
6119 off = (unsigned)(current_ScreenLine - ScreenLines);
6120#ifdef FEAT_RIGHTLEFT
6121 if (wp->w_p_rl)
6122 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006123 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 off += col;
6125 }
6126#endif
6127
6128 /* reset the drawing state for the start of a wrapped line */
6129 draw_state = WL_START;
6130 saved_n_extra = n_extra;
6131 saved_p_extra = p_extra;
6132 saved_c_extra = c_extra;
6133 saved_char_attr = char_attr;
6134 n_extra = 0;
6135 lcs_prec_todo = lcs_prec;
6136#ifdef FEAT_LINEBREAK
6137# ifdef FEAT_DIFF
6138 if (filler_todo <= 0)
6139# endif
6140 need_showbreak = TRUE;
6141#endif
6142#ifdef FEAT_DIFF
6143 --filler_todo;
6144 /* When the filler lines are actually below the last line of the
6145 * file, don't draw the line itself, break here. */
6146 if (filler_todo == 0 && wp->w_botfill)
6147 break;
6148#endif
6149 }
6150
6151 } /* for every character in the line */
6152
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006153#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006154 /* After an empty line check first word for capital. */
6155 if (*skipwhite(line) == NUL)
6156 {
6157 capcol_lnum = lnum + 1;
6158 cap_col = 0;
6159 }
6160#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006161#ifdef FEAT_TEXT_PROP
6162 vim_free(text_props);
6163 vim_free(text_prop_idxs);
6164#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006165
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006166 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 return row;
6168}
6169
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006170#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006171/*
6172 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006173 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006174 */
6175 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006176comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006177{
6178 int i;
6179
6180 for (i = 0; i < Screen_mco; ++i)
6181 {
6182 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6183 return TRUE;
6184 if (ScreenLinesC[i][off_from] == 0)
6185 break;
6186 }
6187 return FALSE;
6188}
6189#endif
6190
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191/*
6192 * Check whether the given character needs redrawing:
6193 * - the (first byte of the) character is different
6194 * - the attributes are different
6195 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006196 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 */
6198 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006199char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 if (cols > 0
6202 && ((ScreenLines[off_from] != ScreenLines[off_to]
6203 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6204
6205#ifdef FEAT_MBYTE
6206 || (enc_dbcs != 0
6207 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6208 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6209 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6210 : (cols > 1 && ScreenLines[off_from + 1]
6211 != ScreenLines[off_to + 1])))
6212 || (enc_utf8
6213 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6214 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006215 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006216 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6217 && ScreenLines[off_from + 1]
6218 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219#endif
6220 ))
6221 return TRUE;
6222 return FALSE;
6223}
6224
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006225#if defined(FEAT_TERMINAL) || defined(PROTO)
6226/*
6227 * Return the index in ScreenLines[] for the current screen line.
6228 */
6229 int
6230screen_get_current_line_off()
6231{
6232 return (int)(current_ScreenLine - ScreenLines);
6233}
6234#endif
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236/*
6237 * Move one "cooked" screen line to the screen, but only the characters that
6238 * have actually changed. Handle insert/delete character.
6239 * "coloff" gives the first column on the screen for this line.
6240 * "endcol" gives the columns where valid characters are.
6241 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6242 * needs to be cleared, negative otherwise.
6243 * "rlflag" is TRUE in a rightleft window:
6244 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6245 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6246 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006247 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006248screen_line(
6249 int row,
6250 int coloff,
6251 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006252 int clear_width,
6253 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 unsigned off_from;
6256 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006257#ifdef FEAT_MBYTE
6258 unsigned max_off_from;
6259 unsigned max_off_to;
6260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 int force = FALSE; /* force update rest of the line */
6264 int redraw_this /* bool: does character need redraw? */
6265#ifdef FEAT_GUI
6266 = TRUE /* For GUI when while-loop empty */
6267#endif
6268 ;
6269 int redraw_next; /* redraw_this for next character */
6270#ifdef FEAT_MBYTE
6271 int clear_next = FALSE;
6272 int char_cells; /* 1: normal char */
6273 /* 2: occupies two display cells */
6274# define CHAR_CELLS char_cells
6275#else
6276# define CHAR_CELLS 1
6277#endif
6278
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006279 /* Check for illegal row and col, just in case. */
6280 if (row >= Rows)
6281 row = Rows - 1;
6282 if (endcol > Columns)
6283 endcol = Columns;
6284
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285# ifdef FEAT_CLIPBOARD
6286 clip_may_clear_selection(row, row);
6287# endif
6288
6289 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6290 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006291#ifdef FEAT_MBYTE
6292 max_off_from = off_from + screen_Columns;
6293 max_off_to = LineOffset[row] + screen_Columns;
6294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
6296#ifdef FEAT_RIGHTLEFT
6297 if (rlflag)
6298 {
6299 /* Clear rest first, because it's left of the text. */
6300 if (clear_width > 0)
6301 {
6302 while (col <= endcol && ScreenLines[off_to] == ' '
6303 && ScreenAttrs[off_to] == 0
6304# ifdef FEAT_MBYTE
6305 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6306# endif
6307 )
6308 {
6309 ++off_to;
6310 ++col;
6311 }
6312 if (col <= endcol)
6313 screen_fill(row, row + 1, col + coloff,
6314 endcol + coloff + 1, ' ', ' ', 0);
6315 }
6316 col = endcol + 1;
6317 off_to = LineOffset[row] + col + coloff;
6318 off_from += col;
6319 endcol = (clear_width > 0 ? clear_width : -clear_width);
6320 }
6321#endif /* FEAT_RIGHTLEFT */
6322
6323 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6324
6325 while (col < endcol)
6326 {
6327#ifdef FEAT_MBYTE
6328 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006329 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 else
6331 char_cells = 1;
6332#endif
6333
6334 redraw_this = redraw_next;
6335 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6336 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6337
6338#ifdef FEAT_GUI
6339 /* If the next character was bold, then redraw the current character to
6340 * remove any pixels that might have spilt over into us. This only
6341 * happens in the GUI.
6342 */
6343 if (redraw_next && gui.in_use)
6344 {
6345 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006346 if (hl > HL_ALL)
6347 hl = syn_attr2attr(hl);
6348 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 redraw_this = TRUE;
6350 }
6351#endif
6352
6353 if (redraw_this)
6354 {
6355 /*
6356 * Special handling when 'xs' termcap flag set (hpterm):
6357 * Attributes for characters are stored at the position where the
6358 * cursor is when writing the highlighting code. The
6359 * start-highlighting code must be written with the cursor on the
6360 * first highlighted character. The stop-highlighting code must
6361 * be written with the cursor just after the last highlighted
6362 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006363 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 * to clear the rest of the line, and force redrawing it
6365 * completely.
6366 */
6367 if ( p_wiv
6368 && !force
6369#ifdef FEAT_GUI
6370 && !gui.in_use
6371#endif
6372 && ScreenAttrs[off_to] != 0
6373 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6374 {
6375 /*
6376 * Need to remove highlighting attributes here.
6377 */
6378 windgoto(row, col + coloff);
6379 out_str(T_CE); /* clear rest of this screen line */
6380 screen_start(); /* don't know where cursor is now */
6381 force = TRUE; /* force redraw of rest of the line */
6382 redraw_next = TRUE; /* or else next char would miss out */
6383
6384 /*
6385 * If the previous character was highlighted, need to stop
6386 * highlighting at this character.
6387 */
6388 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6389 {
6390 screen_attr = ScreenAttrs[off_to - 1];
6391 term_windgoto(row, col + coloff);
6392 screen_stop_highlight();
6393 }
6394 else
6395 screen_attr = 0; /* highlighting has stopped */
6396 }
6397#ifdef FEAT_MBYTE
6398 if (enc_dbcs != 0)
6399 {
6400 /* Check if overwriting a double-byte with a single-byte or
6401 * the other way around requires another character to be
6402 * redrawn. For UTF-8 this isn't needed, because comparing
6403 * ScreenLinesUC[] is sufficient. */
6404 if (char_cells == 1
6405 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006406 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 /* Writing a single-cell character over a double-cell
6409 * character: need to redraw the next cell. */
6410 ScreenLines[off_to + 1] = 0;
6411 redraw_next = TRUE;
6412 }
6413 else if (char_cells == 2
6414 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006415 && (*mb_off2cells)(off_to, max_off_to) == 1
6416 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 {
6418 /* Writing the second half of a double-cell character over
6419 * a double-cell character: need to redraw the second
6420 * cell. */
6421 ScreenLines[off_to + 2] = 0;
6422 redraw_next = TRUE;
6423 }
6424
6425 if (enc_dbcs == DBCS_JPNU)
6426 ScreenLines2[off_to] = ScreenLines2[off_from];
6427 }
6428 /* When writing a single-width character over a double-width
6429 * character and at the end of the redrawn text, need to clear out
6430 * the right halve of the old character.
6431 * Also required when writing the right halve of a double-width
6432 * char over the left halve of an existing one. */
6433 if (has_mbyte && col + char_cells == endcol
6434 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006435 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006437 && (*mb_off2cells)(off_to, max_off_to) == 1
6438 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 clear_next = TRUE;
6440#endif
6441
6442 ScreenLines[off_to] = ScreenLines[off_from];
6443#ifdef FEAT_MBYTE
6444 if (enc_utf8)
6445 {
6446 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6447 if (ScreenLinesUC[off_from] != 0)
6448 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006449 int i;
6450
6451 for (i = 0; i < Screen_mco; ++i)
6452 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 }
6454 }
6455 if (char_cells == 2)
6456 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6457#endif
6458
6459#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006460 /* The bold trick makes a single column of pixels appear in the
6461 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 * character should be redrawn too. This happens for our own GUI
6463 * and for some xterms. */
6464 if (
6465# ifdef FEAT_GUI
6466 gui.in_use
6467# endif
6468# if defined(FEAT_GUI) && defined(UNIX)
6469 ||
6470# endif
6471# ifdef UNIX
6472 term_is_xterm
6473# endif
6474 )
6475 {
6476 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006477 if (hl > HL_ALL)
6478 hl = syn_attr2attr(hl);
6479 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 redraw_next = TRUE;
6481 }
6482#endif
6483 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6484#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006485 /* For simplicity set the attributes of second half of a
6486 * double-wide character equal to the first half. */
6487 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006489
6490 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 else
6493#endif
6494 screen_char(off_to, row, col + coloff);
6495 }
6496 else if ( p_wiv
6497#ifdef FEAT_GUI
6498 && !gui.in_use
6499#endif
6500 && col + coloff > 0)
6501 {
6502 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6503 {
6504 /*
6505 * Don't output stop-highlight when moving the cursor, it will
6506 * stop the highlighting when it should continue.
6507 */
6508 screen_attr = 0;
6509 }
6510 else if (screen_attr != 0)
6511 screen_stop_highlight();
6512 }
6513
6514 off_to += CHAR_CELLS;
6515 off_from += CHAR_CELLS;
6516 col += CHAR_CELLS;
6517 }
6518
6519#ifdef FEAT_MBYTE
6520 if (clear_next)
6521 {
6522 /* Clear the second half of a double-wide character of which the left
6523 * half was overwritten with a single-wide character. */
6524 ScreenLines[off_to] = ' ';
6525 if (enc_utf8)
6526 ScreenLinesUC[off_to] = 0;
6527 screen_char(off_to, row, col + coloff);
6528 }
6529#endif
6530
6531 if (clear_width > 0
6532#ifdef FEAT_RIGHTLEFT
6533 && !rlflag
6534#endif
6535 )
6536 {
6537#ifdef FEAT_GUI
6538 int startCol = col;
6539#endif
6540
6541 /* blank out the rest of the line */
6542 while (col < clear_width && ScreenLines[off_to] == ' '
6543 && ScreenAttrs[off_to] == 0
6544#ifdef FEAT_MBYTE
6545 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6546#endif
6547 )
6548 {
6549 ++off_to;
6550 ++col;
6551 }
6552 if (col < clear_width)
6553 {
6554#ifdef FEAT_GUI
6555 /*
6556 * In the GUI, clearing the rest of the line may leave pixels
6557 * behind if the first character cleared was bold. Some bold
6558 * fonts spill over the left. In this case we redraw the previous
6559 * character too. If we didn't skip any blanks above, then we
6560 * only redraw if the character wasn't already redrawn anyway.
6561 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006562 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
6564 hl = ScreenAttrs[off_to];
6565 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006566 {
6567 int prev_cells = 1;
6568# ifdef FEAT_MBYTE
6569 if (enc_utf8)
6570 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6571 * that its width is 2. */
6572 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6573 else if (enc_dbcs != 0)
6574 {
6575 /* find previous character by counting from first
6576 * column and get its width. */
6577 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006578 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006579
6580 while (off < off_to)
6581 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006582 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006583 off += prev_cells;
6584 }
6585 }
6586
6587 if (enc_dbcs != 0 && prev_cells > 1)
6588 screen_char_2(off_to - prev_cells, row,
6589 col + coloff - prev_cells);
6590 else
6591# endif
6592 screen_char(off_to - prev_cells, row,
6593 col + coloff - prev_cells);
6594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
6596#endif
6597 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6598 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 off_to += clear_width - col;
6600 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
6602 }
6603
6604 if (clear_width > 0)
6605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 /* For a window that's left of another, draw the separator char. */
6607 if (col + coloff < Columns)
6608 {
6609 int c;
6610
6611 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006612 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006613#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006614 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6615 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 || ScreenAttrs[off_to] != hl)
6618 {
6619 ScreenLines[off_to] = c;
6620 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006621#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (enc_utf8)
6623 {
6624 if (c >= 0x80)
6625 {
6626 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006627 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 }
6629 else
6630 ScreenLinesUC[off_to] = 0;
6631 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 screen_char(off_to, row, col + coloff);
6634 }
6635 }
6636 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 LineWraps[row] = FALSE;
6638 }
6639}
6640
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006641#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006643 * Mirror text "str" for right-left displaying.
6644 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006647rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 char_u *p1, *p2;
6650 int t;
6651
6652 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6653 {
6654 t = *p1;
6655 *p1 = *p2;
6656 *p2 = t;
6657 }
6658}
6659#endif
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661/*
6662 * mark all status lines for redraw; used after first :cd
6663 */
6664 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006665status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 win_T *wp;
6668
Bram Moolenaar29323592016-07-24 22:04:11 +02006669 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (wp->w_status_height)
6671 {
6672 wp->w_redr_status = TRUE;
6673 redraw_later(VALID);
6674 }
6675}
6676
6677/*
6678 * mark all status lines of the current buffer for redraw
6679 */
6680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006681status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 win_T *wp;
6684
Bram Moolenaar29323592016-07-24 22:04:11 +02006685 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6687 {
6688 wp->w_redr_status = TRUE;
6689 redraw_later(VALID);
6690 }
6691}
6692
6693/*
6694 * Redraw all status lines that need to be redrawn.
6695 */
6696 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006697redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 win_T *wp;
6700
Bram Moolenaar29323592016-07-24 22:04:11 +02006701 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006703 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006704 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006705 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
Bram Moolenaar4033c552017-09-16 20:54:51 +02006708#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709/*
6710 * Redraw all status lines at the bottom of frame "frp".
6711 */
6712 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006713win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 if (frp->fr_layout == FR_LEAF)
6716 frp->fr_win->w_redr_status = TRUE;
6717 else if (frp->fr_layout == FR_ROW)
6718 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006719 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 win_redraw_last_status(frp);
6721 }
6722 else /* frp->fr_layout == FR_COL */
6723 {
6724 frp = frp->fr_child;
6725 while (frp->fr_next != NULL)
6726 frp = frp->fr_next;
6727 win_redraw_last_status(frp);
6728 }
6729}
6730#endif
6731
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732/*
6733 * Draw the verticap separator right of window "wp" starting with line "row".
6734 */
6735 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006736draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 int hl;
6739 int c;
6740
6741 if (wp->w_vsep_width)
6742 {
6743 /* draw the vertical separator right of this window */
6744 c = fillchar_vsep(&hl);
6745 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6746 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6747 c, ' ', hl);
6748 }
6749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006752static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
6754/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006755 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 */
6757 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006758status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 int len = 0;
6761
6762#ifdef FEAT_MENU
6763 int emenu = (xp->xp_context == EXPAND_MENUS
6764 || xp->xp_context == EXPAND_MENUNAMES);
6765
6766 /* Check for menu separators - replace with '|'. */
6767 if (emenu && menu_is_separator(s))
6768 return 1;
6769#endif
6770
6771 while (*s != NUL)
6772 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006773 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006774 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006775 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777
6778 return len;
6779}
6780
6781/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006782 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006783 * These are backslashes used for escaping. Do show backslashes in help tags.
6784 */
6785 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006786skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006787{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006788 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006789#ifdef FEAT_MENU
6790 || ((xp->xp_context == EXPAND_MENUS
6791 || xp->xp_context == EXPAND_MENUNAMES)
6792 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6793#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006794 )
6795 {
6796#ifndef BACKSLASH_IN_FILENAME
6797 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6798 return 2;
6799#endif
6800 return 1;
6801 }
6802 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006803}
6804
6805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 * Show wildchar matches in the status line.
6807 * Show at least the "match" item.
6808 * We start at item 'first_match' in the list and show all matches that fit.
6809 *
6810 * If inversion is possible we use it. Else '=' characters are used.
6811 */
6812 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006813win_redr_status_matches(
6814 expand_T *xp,
6815 int num_matches,
6816 char_u **matches, /* list of matches */
6817 int match,
6818 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6821 int row;
6822 char_u *buf;
6823 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006824 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 int fillchar;
6826 int attr;
6827 int i;
6828 int highlight = TRUE;
6829 char_u *selstart = NULL;
6830 int selstart_col = 0;
6831 char_u *selend = NULL;
6832 static int first_match = 0;
6833 int add_left = FALSE;
6834 char_u *s;
6835#ifdef FEAT_MENU
6836 int emenu;
6837#endif
6838#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6839 int l;
6840#endif
6841
6842 if (matches == NULL) /* interrupted completion? */
6843 return;
6844
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006845#ifdef FEAT_MBYTE
6846 if (has_mbyte)
6847 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6848 else
6849#endif
6850 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 if (buf == NULL)
6852 return;
6853
6854 if (match == -1) /* don't show match but original text */
6855 {
6856 match = 0;
6857 highlight = FALSE;
6858 }
6859 /* count 1 for the ending ">" */
6860 clen = status_match_len(xp, L_MATCH(match)) + 3;
6861 if (match == 0)
6862 first_match = 0;
6863 else if (match < first_match)
6864 {
6865 /* jumping left, as far as we can go */
6866 first_match = match;
6867 add_left = TRUE;
6868 }
6869 else
6870 {
6871 /* check if match fits on the screen */
6872 for (i = first_match; i < match; ++i)
6873 clen += status_match_len(xp, L_MATCH(i)) + 2;
6874 if (first_match > 0)
6875 clen += 2;
6876 /* jumping right, put match at the left */
6877 if ((long)clen > Columns)
6878 {
6879 first_match = match;
6880 /* if showing the last match, we can add some on the left */
6881 clen = 2;
6882 for (i = match; i < num_matches; ++i)
6883 {
6884 clen += status_match_len(xp, L_MATCH(i)) + 2;
6885 if ((long)clen >= Columns)
6886 break;
6887 }
6888 if (i == num_matches)
6889 add_left = TRUE;
6890 }
6891 }
6892 if (add_left)
6893 while (first_match > 0)
6894 {
6895 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6896 if ((long)clen >= Columns)
6897 break;
6898 --first_match;
6899 }
6900
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006901 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903 if (first_match == 0)
6904 {
6905 *buf = NUL;
6906 len = 0;
6907 }
6908 else
6909 {
6910 STRCPY(buf, "< ");
6911 len = 2;
6912 }
6913 clen = len;
6914
6915 i = first_match;
6916 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6917 {
6918 if (i == match)
6919 {
6920 selstart = buf + len;
6921 selstart_col = clen;
6922 }
6923
6924 s = L_MATCH(i);
6925 /* Check for menu separators - replace with '|' */
6926#ifdef FEAT_MENU
6927 emenu = (xp->xp_context == EXPAND_MENUS
6928 || xp->xp_context == EXPAND_MENUNAMES);
6929 if (emenu && menu_is_separator(s))
6930 {
6931 STRCPY(buf + len, transchar('|'));
6932 l = (int)STRLEN(buf + len);
6933 len += l;
6934 clen += l;
6935 }
6936 else
6937#endif
6938 for ( ; *s != NUL; ++s)
6939 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006940 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 clen += ptr2cells(s);
6942#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006943 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {
6945 STRNCPY(buf + len, s, l);
6946 s += l - 1;
6947 len += l;
6948 }
6949 else
6950#endif
6951 {
6952 STRCPY(buf + len, transchar_byte(*s));
6953 len += (int)STRLEN(buf + len);
6954 }
6955 }
6956 if (i == match)
6957 selend = buf + len;
6958
6959 *(buf + len++) = ' ';
6960 *(buf + len++) = ' ';
6961 clen += 2;
6962 if (++i == num_matches)
6963 break;
6964 }
6965
6966 if (i != num_matches)
6967 {
6968 *(buf + len++) = '>';
6969 ++clen;
6970 }
6971
6972 buf[len] = NUL;
6973
6974 row = cmdline_row - 1;
6975 if (row >= 0)
6976 {
6977 if (wild_menu_showing == 0)
6978 {
6979 if (msg_scrolled > 0)
6980 {
6981 /* Put the wildmenu just above the command line. If there is
6982 * no room, scroll the screen one line up. */
6983 if (cmdline_row == Rows - 1)
6984 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006985 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 ++msg_scrolled;
6987 }
6988 else
6989 {
6990 ++cmdline_row;
6991 ++row;
6992 }
6993 wild_menu_showing = WM_SCROLLED;
6994 }
6995 else
6996 {
6997 /* Create status line if needed by setting 'laststatus' to 2.
6998 * Set 'winminheight' to zero to avoid that the window is
6999 * resized. */
7000 if (lastwin->w_status_height == 0)
7001 {
7002 save_p_ls = p_ls;
7003 save_p_wmh = p_wmh;
7004 p_ls = 2;
7005 p_wmh = 0;
7006 last_status(FALSE);
7007 }
7008 wild_menu_showing = WM_SHOWN;
7009 }
7010 }
7011
7012 screen_puts(buf, row, 0, attr);
7013 if (selstart != NULL && highlight)
7014 {
7015 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007016 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018
7019 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7020 }
7021
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 vim_free(buf);
7024}
7025#endif
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027/*
7028 * Redraw the status line of window wp.
7029 *
7030 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007031 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7032 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007034 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007035win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 int row;
7038 char_u *p;
7039 int len;
7040 int fillchar;
7041 int attr;
7042 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007043 static int busy = FALSE;
7044
7045 /* It's possible to get here recursively when 'statusline' (indirectly)
7046 * invokes ":redrawstatus". Simply ignore the call then. */
7047 if (busy)
7048 return;
7049 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051 wp->w_redr_status = FALSE;
7052 if (wp->w_status_height == 0)
7053 {
7054 /* no status line, can only be last window */
7055 redraw_cmdline = TRUE;
7056 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007057 else if (!redrawing()
7058#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007059 // don't update status line when popup menu is visible and may be
7060 // drawn over it, unless it will be redrawn later
7061 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007062#endif
7063 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
7065 /* Don't redraw right now, do it later. */
7066 wp->w_redr_status = TRUE;
7067 }
7068#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007069 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
7071 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007072 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 }
7074#endif
7075 else
7076 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007077 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007079 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 p = NameBuff;
7081 len = (int)STRLEN(p);
7082
Bram Moolenaard85f2712017-07-28 21:51:57 +02007083 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#ifdef FEAT_QUICKFIX
7085 || wp->w_p_pvw
7086#endif
7087 || bufIsChanged(wp->w_buffer)
7088 || wp->w_buffer->b_p_ro)
7089 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007090 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007092 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 len += (int)STRLEN(p + len);
7094 }
7095#ifdef FEAT_QUICKFIX
7096 if (wp->w_p_pvw)
7097 {
7098 STRCPY(p + len, _("[Preview]"));
7099 len += (int)STRLEN(p + len);
7100 }
7101#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007102 if (bufIsChanged(wp->w_buffer)
7103#ifdef FEAT_TERMINAL
7104 && !bt_terminal(wp->w_buffer)
7105#endif
7106 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
7108 STRCPY(p + len, "[+]");
7109 len += 3;
7110 }
7111 if (wp->w_buffer->b_p_ro)
7112 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007113 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007114 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116
Bram Moolenaar02631462017-09-22 15:20:32 +02007117 this_ru_col = ru_col - (Columns - wp->w_width);
7118 if (this_ru_col < (wp->w_width + 1) / 2)
7119 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (this_ru_col <= 1)
7121 {
7122 p = (char_u *)"<"; /* No room for file name! */
7123 len = 1;
7124 }
7125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126#ifdef FEAT_MBYTE
7127 if (has_mbyte)
7128 {
7129 int clen = 0, i;
7130
7131 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007132 clen = mb_string2cells(p, -1);
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 /* Find first character that will fit.
7135 * Going from start to end is much faster for DBCS. */
7136 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007137 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 clen -= (*mb_ptr2cells)(p + i);
7139 len = clen;
7140 if (i > 0)
7141 {
7142 p = p + i - 1;
7143 *p = '<';
7144 ++len;
7145 }
7146
7147 }
7148 else
7149#endif
7150 if (len > this_ru_col - 1)
7151 {
7152 p += len - (this_ru_col - 1);
7153 *p = '<';
7154 len = this_ru_col - 1;
7155 }
7156
7157 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007158 screen_puts(p, row, wp->w_wincol, attr);
7159 screen_fill(row, row + 1, len + wp->w_wincol,
7160 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007162 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7164 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007165 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007168 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169#endif
7170 }
7171
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 /*
7173 * May need to draw the character below the vertical separator.
7174 */
7175 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7176 {
7177 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007178 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 else
7180 fillchar = fillchar_vsep(&attr);
7181 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7182 attr);
7183 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007184 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185}
7186
Bram Moolenaar238a5642006-02-21 22:12:05 +00007187#ifdef FEAT_STL_OPT
7188/*
7189 * Redraw the status line according to 'statusline' and take care of any
7190 * errors encountered.
7191 */
7192 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007193redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007194{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007195 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007196 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007197
7198 /* When called recursively return. This can happen when the statusline
7199 * contains an expression that triggers a redraw. */
7200 if (entered)
7201 return;
7202 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007203
Bram Moolenaara742e082016-04-05 21:10:38 +02007204 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007205 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007206 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007207 {
7208 /* When there is an error disable the statusline, otherwise the
7209 * display is messed up with errors and a redraw triggers the problem
7210 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007211 set_string_option_direct((char_u *)"statusline", -1,
7212 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007213 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007214 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007215 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007216 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007217}
7218#endif
7219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220/*
7221 * Return TRUE if the status line of window "wp" is connected to the status
7222 * line of the window right of it. If not, then it's a vertical separator.
7223 * Only call if (wp->w_vsep_width != 0).
7224 */
7225 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007226stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 frame_T *fr;
7229
7230 fr = wp->w_frame;
7231 while (fr->fr_parent != NULL)
7232 {
7233 if (fr->fr_parent->fr_layout == FR_COL)
7234 {
7235 if (fr->fr_next != NULL)
7236 break;
7237 }
7238 else
7239 {
7240 if (fr->fr_next != NULL)
7241 return TRUE;
7242 }
7243 fr = fr->fr_parent;
7244 }
7245 return FALSE;
7246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249/*
7250 * Get the value to show for the language mappings, active 'keymap'.
7251 */
7252 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007253get_keymap_str(
7254 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007255 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007256 char_u *buf, /* buffer for the result */
7257 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 char_u *p;
7260
7261 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7262 return FALSE;
7263
7264 {
7265#ifdef FEAT_EVAL
7266 buf_T *old_curbuf = curbuf;
7267 win_T *old_curwin = curwin;
7268 char_u *s;
7269
7270 curbuf = wp->w_buffer;
7271 curwin = wp;
7272 STRCPY(buf, "b:keymap_name"); /* must be writable */
7273 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007274 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 --emsg_skip;
7276 curbuf = old_curbuf;
7277 curwin = old_curwin;
7278 if (p == NULL || *p == NUL)
7279#endif
7280 {
7281#ifdef FEAT_KEYMAP
7282 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7283 p = wp->w_buffer->b_p_keymap;
7284 else
7285#endif
7286 p = (char_u *)"lang";
7287 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007288 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 buf[0] = NUL;
7290#ifdef FEAT_EVAL
7291 vim_free(s);
7292#endif
7293 }
7294 return buf[0] != NUL;
7295}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
7297#if defined(FEAT_STL_OPT) || defined(PROTO)
7298/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007299 * Redraw the status line or ruler of window "wp".
7300 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 */
7302 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007303win_redr_custom(
7304 win_T *wp,
7305 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007307 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 int attr;
7309 int curattr;
7310 int row;
7311 int col = 0;
7312 int maxwidth;
7313 int width;
7314 int n;
7315 int len;
7316 int fillchar;
7317 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007318 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007320 struct stl_hlrec hltab[STL_MAX_ITEM];
7321 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007323 win_T *ewp;
7324 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325
Bram Moolenaar1d633412013-12-11 15:52:01 +01007326 /* There is a tiny chance that this gets called recursively: When
7327 * redrawing a status line triggers redrawing the ruler or tabline.
7328 * Avoid trouble by not allowing recursion. */
7329 if (entered)
7330 return;
7331 entered = TRUE;
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007334 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007337 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007338 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007339 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007340 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007341 maxwidth = Columns;
7342# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007343 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007346 else
7347 {
7348 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007349 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007350 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007351
7352 if (draw_ruler)
7353 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007354 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007355 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007356 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007357 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007358 if (*++stl == '-')
7359 stl++;
7360 if (atoi((char *)stl))
7361 while (VIM_ISDIGIT(*stl))
7362 stl++;
7363 if (*stl++ != '(')
7364 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007365 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007366 col = ru_col - (Columns - wp->w_width);
7367 if (col < (wp->w_width + 1) / 2)
7368 col = (wp->w_width + 1) / 2;
7369 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007370 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007371 {
7372 row = Rows - 1;
7373 --maxwidth; /* writing in last column may cause scrolling */
7374 fillchar = ' ';
7375 attr = 0;
7376 }
7377
7378# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007380# endif
7381 }
7382 else
7383 {
7384 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007385 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007386 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007387 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007388# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007389 use_sandbox = was_set_insecurely((char_u *)"statusline",
7390 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007391# endif
7392 }
7393
Bram Moolenaar53f81742017-09-22 14:35:51 +02007394 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007395 }
7396
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007398 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
Bram Moolenaar61452852011-02-01 18:01:11 +01007400 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7401 * the cursor away and back. */
7402 ewp = wp == NULL ? curwin : wp;
7403 p_crb_save = ewp->w_p_crb;
7404 ewp->w_p_crb = FALSE;
7405
Bram Moolenaar362f3562009-11-03 16:20:34 +00007406 /* Make a copy, because the statusline may include a function call that
7407 * might change the option value and free the memory. */
7408 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007409 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007410 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007411 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007412 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007413 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007415 /* Make all characters printable. */
7416 p = transstr(buf);
7417 if (p != NULL)
7418 {
7419 vim_strncpy(buf, p, sizeof(buf) - 1);
7420 vim_free(p);
7421 }
7422
7423 /* fill up with "fillchar" */
7424 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007425 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
7427#ifdef FEAT_MBYTE
7428 len += (*mb_char2bytes)(fillchar, buf + len);
7429#else
7430 buf[len++] = fillchar;
7431#endif
7432 ++width;
7433 }
7434 buf[len] = NUL;
7435
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007436 /*
7437 * Draw each snippet with the specified highlighting.
7438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 curattr = attr;
7440 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007441 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007443 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 screen_puts_len(p, len, row, col, curattr);
7445 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007446 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007448 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007450 else if (hltab[n].userhl < 0)
7451 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007452#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007453 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7454 && wp->w_status_height != 0)
7455 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007456 else if (wp != NULL && bt_terminal(wp->w_buffer)
7457 && wp->w_status_height != 0)
7458 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007459#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007460 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007461 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007463 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
7465 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007466
7467 if (wp == NULL)
7468 {
7469 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7470 col = 0;
7471 len = 0;
7472 p = buf;
7473 fillchar = 0;
7474 for (n = 0; tabtab[n].start != NULL; n++)
7475 {
7476 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7477 while (col < len)
7478 TabPageIdxs[col++] = fillchar;
7479 p = tabtab[n].start;
7480 fillchar = tabtab[n].userhl;
7481 }
7482 while (col < Columns)
7483 TabPageIdxs[col++] = fillchar;
7484 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007485
7486theend:
7487 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488}
7489
7490#endif /* FEAT_STL_OPT */
7491
7492/*
7493 * Output a single character directly to the screen and update ScreenLines.
7494 */
7495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007496screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 char_u buf[MB_MAXBYTES + 1];
7499
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007500#ifdef FEAT_MBYTE
7501 if (has_mbyte)
7502 buf[(*mb_char2bytes)(c, buf)] = NUL;
7503 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007505 {
7506 buf[0] = c;
7507 buf[1] = NUL;
7508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 screen_puts(buf, row, col, attr);
7510}
7511
7512/*
7513 * Get a single character directly from ScreenLines into "bytes[]".
7514 * Also return its attribute in *attrp;
7515 */
7516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007517screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518{
7519 unsigned off;
7520
7521 /* safety check */
7522 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7523 {
7524 off = LineOffset[row] + col;
7525 *attrp = ScreenAttrs[off];
7526 bytes[0] = ScreenLines[off];
7527 bytes[1] = NUL;
7528
7529#ifdef FEAT_MBYTE
7530 if (enc_utf8 && ScreenLinesUC[off] != 0)
7531 bytes[utfc_char2bytes(off, bytes)] = NUL;
7532 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7533 {
7534 bytes[0] = ScreenLines[off];
7535 bytes[1] = ScreenLines2[off];
7536 bytes[2] = NUL;
7537 }
7538 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7539 {
7540 bytes[1] = ScreenLines[off + 1];
7541 bytes[2] = NUL;
7542 }
7543#endif
7544 }
7545}
7546
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007547#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007548/*
7549 * Return TRUE if composing characters for screen posn "off" differs from
7550 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007551 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007552 */
7553 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007554screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007555{
7556 int i;
7557
7558 for (i = 0; i < Screen_mco; ++i)
7559 {
7560 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7561 return TRUE;
7562 if (u8cc[i] == 0)
7563 break;
7564 }
7565 return FALSE;
7566}
7567#endif
7568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569/*
7570 * Put string '*text' on the screen at position 'row' and 'col', with
7571 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7572 * Note: only outputs within one row, message is truncated at screen boundary!
7573 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7574 */
7575 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007576screen_puts(
7577 char_u *text,
7578 int row,
7579 int col,
7580 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581{
7582 screen_puts_len(text, -1, row, col, attr);
7583}
7584
7585/*
7586 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7587 * a NUL.
7588 */
7589 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007590screen_puts_len(
7591 char_u *text,
7592 int textlen,
7593 int row,
7594 int col,
7595 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 unsigned off;
7598 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007599 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 int c;
7601#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007602 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 int mbyte_blen = 1;
7604 int mbyte_cells = 1;
7605 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007606 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 int clear_next_cell = FALSE;
7608# ifdef FEAT_ARABIC
7609 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007610 int pc, nc, nc1;
7611 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612# endif
7613#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007614#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7615 int force_redraw_this;
7616 int force_redraw_next = FALSE;
7617#endif
7618 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619
7620 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7621 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007622 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623
Bram Moolenaarc236c162008-07-13 17:41:49 +00007624#ifdef FEAT_MBYTE
7625 /* When drawing over the right halve of a double-wide char clear out the
7626 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007627 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007628# ifdef FEAT_GUI
7629 && !gui.in_use
7630# endif
7631 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007632 {
7633 ScreenLines[off - 1] = ' ';
7634 ScreenAttrs[off - 1] = 0;
7635 if (enc_utf8)
7636 {
7637 ScreenLinesUC[off - 1] = 0;
7638 ScreenLinesC[0][off - 1] = 0;
7639 }
7640 /* redraw the previous cell, make it empty */
7641 screen_char(off - 1, row, col - 1);
7642 /* force the cell at "col" to be redrawn */
7643 force_redraw_next = TRUE;
7644 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007645#endif
7646
Bram Moolenaar367329b2007-08-30 11:53:22 +00007647#ifdef FEAT_MBYTE
7648 max_off = LineOffset[row] + screen_Columns;
7649#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007650 while (col < screen_Columns
7651 && (len < 0 || (int)(ptr - text) < len)
7652 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
7654 c = *ptr;
7655#ifdef FEAT_MBYTE
7656 /* check if this is the first byte of a multibyte */
7657 if (has_mbyte)
7658 {
7659 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007660 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007662 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7664 mbyte_cells = 1;
7665 else if (enc_dbcs != 0)
7666 mbyte_cells = mbyte_blen;
7667 else /* enc_utf8 */
7668 {
7669 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007670 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 (int)((text + len) - ptr));
7672 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007673 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007675# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 /* Non-BMP character: display as ? or fullwidth ?. */
7677 if (u8c >= 0x10000)
7678 {
7679 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7680 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007681 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684# ifdef FEAT_ARABIC
7685 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7686 {
7687 /* Do Arabic shaping. */
7688 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7689 {
7690 /* Past end of string to be displayed. */
7691 nc = NUL;
7692 nc1 = NUL;
7693 }
7694 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007695 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007696 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7697 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007698 nc1 = pcc[0];
7699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 pc = prev_c;
7701 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007702 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 else
7705 prev_c = u8c;
7706# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007707 if (col + mbyte_cells > screen_Columns)
7708 {
7709 /* Only 1 cell left, but character requires 2 cells:
7710 * display a '>' in the last column to avoid wrapping. */
7711 c = '>';
7712 mbyte_cells = 1;
7713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 }
7716#endif
7717
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007718#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7719 force_redraw_this = force_redraw_next;
7720 force_redraw_next = FALSE;
7721#endif
7722
7723 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724#ifdef FEAT_MBYTE
7725 || (mbyte_cells == 2
7726 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7727 || (enc_dbcs == DBCS_JPNU
7728 && c == 0x8e
7729 && ScreenLines2[off] != ptr[1])
7730 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007731 && (ScreenLinesUC[off] !=
7732 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7733 || (ScreenLinesUC[off] != 0
7734 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735#endif
7736 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007737 || exmode_active;
7738
7739 if (need_redraw
7740#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7741 || force_redraw_this
7742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 )
7744 {
7745#if defined(FEAT_GUI) || defined(UNIX)
7746 /* The bold trick makes a single row of pixels appear in the next
7747 * character. When a bold character is removed, the next
7748 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007749 * and for some xterms. */
7750 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751# ifdef FEAT_GUI
7752 gui.in_use
7753# endif
7754# if defined(FEAT_GUI) && defined(UNIX)
7755 ||
7756# endif
7757# ifdef UNIX
7758 term_is_xterm
7759# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007760 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007762 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007764 if (n > HL_ALL)
7765 n = syn_attr2attr(n);
7766 if (n & HL_BOLD)
7767 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769#endif
7770#ifdef FEAT_MBYTE
7771 /* When at the end of the text and overwriting a two-cell
7772 * character with a one-cell character, need to clear the next
7773 * cell. Also when overwriting the left halve of a two-cell char
7774 * with the right halve of a two-cell char. Do this only once
7775 * (mb_off2cells() may return 2 on the right halve). */
7776 if (clear_next_cell)
7777 clear_next_cell = FALSE;
7778 else if (has_mbyte
7779 && (len < 0 ? ptr[mbyte_blen] == NUL
7780 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007781 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007783 && (*mb_off2cells)(off, max_off) == 1
7784 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 clear_next_cell = TRUE;
7786
7787 /* Make sure we never leave a second byte of a double-byte behind,
7788 * it confuses mb_off2cells(). */
7789 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007790 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007792 && (*mb_off2cells)(off, max_off) == 1
7793 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 ScreenLines[off + mbyte_blen] = 0;
7795#endif
7796 ScreenLines[off] = c;
7797 ScreenAttrs[off] = attr;
7798#ifdef FEAT_MBYTE
7799 if (enc_utf8)
7800 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007801 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 ScreenLinesUC[off] = 0;
7803 else
7804 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007805 int i;
7806
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007808 for (i = 0; i < Screen_mco; ++i)
7809 {
7810 ScreenLinesC[i][off] = u8cc[i];
7811 if (u8cc[i] == 0)
7812 break;
7813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
7815 if (mbyte_cells == 2)
7816 {
7817 ScreenLines[off + 1] = 0;
7818 ScreenAttrs[off + 1] = attr;
7819 }
7820 screen_char(off, row, col);
7821 }
7822 else if (mbyte_cells == 2)
7823 {
7824 ScreenLines[off + 1] = ptr[1];
7825 ScreenAttrs[off + 1] = attr;
7826 screen_char_2(off, row, col);
7827 }
7828 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7829 {
7830 ScreenLines2[off] = ptr[1];
7831 screen_char(off, row, col);
7832 }
7833 else
7834#endif
7835 screen_char(off, row, col);
7836 }
7837#ifdef FEAT_MBYTE
7838 if (has_mbyte)
7839 {
7840 off += mbyte_cells;
7841 col += mbyte_cells;
7842 ptr += mbyte_blen;
7843 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007844 {
7845 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007847 len = -1;
7848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850 else
7851#endif
7852 {
7853 ++off;
7854 ++col;
7855 ++ptr;
7856 }
7857 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007858
7859#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7860 /* If we detected the next character needs to be redrawn, but the text
7861 * doesn't extend up to there, update the character here. */
7862 if (force_redraw_next && col < screen_Columns)
7863 {
7864# ifdef FEAT_MBYTE
7865 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7866 screen_char_2(off, row, col);
7867 else
7868# endif
7869 screen_char(off, row, col);
7870 }
7871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872}
7873
7874#ifdef FEAT_SEARCH_EXTRA
7875/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007876 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 */
7878 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007879start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 if (p_hls && !no_hlsearch)
7882 {
7883 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007884 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007885# ifdef FEAT_RELTIME
7886 /* Set the time limit to 'redrawtime'. */
7887 profile_setlimit(p_rdt, &search_hl.tm);
7888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890}
7891
7892/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007893 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 */
7895 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007896end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 if (search_hl.rm.regprog != NULL)
7899 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007900 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 search_hl.rm.regprog = NULL;
7902 }
7903}
7904
7905/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007906 * Init for calling prepare_search_hl().
7907 */
7908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007909init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007910{
7911 matchitem_T *cur;
7912
7913 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7914 * match */
7915 cur = wp->w_match_head;
7916 while (cur != NULL)
7917 {
7918 cur->hl.rm = cur->match;
7919 if (cur->hlg_id == 0)
7920 cur->hl.attr = 0;
7921 else
7922 cur->hl.attr = syn_id2attr(cur->hlg_id);
7923 cur->hl.buf = wp->w_buffer;
7924 cur->hl.lnum = 0;
7925 cur->hl.first_lnum = 0;
7926# ifdef FEAT_RELTIME
7927 /* Set the time limit to 'redrawtime'. */
7928 profile_setlimit(p_rdt, &(cur->hl.tm));
7929# endif
7930 cur = cur->next;
7931 }
7932 search_hl.buf = wp->w_buffer;
7933 search_hl.lnum = 0;
7934 search_hl.first_lnum = 0;
7935 /* time limit is set at the toplevel, for all windows */
7936}
7937
7938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 * Advance to the match in window "wp" line "lnum" or past it.
7940 */
7941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007942prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007944 matchitem_T *cur; /* points to the match list */
7945 match_T *shl; /* points to search_hl or a match */
7946 int shl_flag; /* flag to indicate whether search_hl
7947 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007948 int pos_inprogress; /* marks that position match search is
7949 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 int n;
7951
7952 /*
7953 * When using a multi-line pattern, start searching at the top
7954 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007955 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007957 cur = wp->w_match_head;
7958 shl_flag = FALSE;
7959 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007961 if (shl_flag == FALSE)
7962 {
7963 shl = &search_hl;
7964 shl_flag = TRUE;
7965 }
7966 else
7967 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 if (shl->rm.regprog != NULL
7969 && shl->lnum == 0
7970 && re_multiline(shl->rm.regprog))
7971 {
7972 if (shl->first_lnum == 0)
7973 {
7974# ifdef FEAT_FOLDING
7975 for (shl->first_lnum = lnum;
7976 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7977 if (hasFoldingWin(wp, shl->first_lnum - 1,
7978 NULL, NULL, TRUE, NULL))
7979 break;
7980# else
7981 shl->first_lnum = wp->w_topline;
7982# endif
7983 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007984 if (cur != NULL)
7985 cur->pos.cur = 0;
7986 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007988 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7989 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007991 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7992 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007993 pos_inprogress = cur == NULL || cur->pos.cur == 0
7994 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (shl->lnum != 0)
7996 {
7997 shl->first_lnum = shl->lnum
7998 + shl->rm.endpos[0].lnum
7999 - shl->rm.startpos[0].lnum;
8000 n = shl->rm.endpos[0].col;
8001 }
8002 else
8003 {
8004 ++shl->first_lnum;
8005 n = 0;
8006 }
8007 }
8008 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008009 if (shl != &search_hl && cur != NULL)
8010 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 }
8012}
8013
8014/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008015 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 * Uses shl->buf.
8017 * Sets shl->lnum and shl->rm contents.
8018 * Note: Assumes a previous match is always before "lnum", unless
8019 * shl->lnum is zero.
8020 * Careful: Any pointers for buffer lines will become invalid.
8021 */
8022 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008023next_search_hl(
8024 win_T *win,
8025 match_T *shl, /* points to search_hl or a match */
8026 linenr_T lnum,
8027 colnr_T mincol, /* minimal column for a match */
8028 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
8030 linenr_T l;
8031 colnr_T matchcol;
8032 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008033 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008035 // for :{range}s/pat only highlight inside the range
8036 if (lnum < search_first_line || lnum > search_last_line)
8037 {
8038 shl->lnum = 0;
8039 return;
8040 }
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 if (shl->lnum != 0)
8043 {
8044 /* Check for three situations:
8045 * 1. If the "lnum" is below a previous match, start a new search.
8046 * 2. If the previous match includes "mincol", use it.
8047 * 3. Continue after the previous match.
8048 */
8049 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8050 if (lnum > l)
8051 shl->lnum = 0;
8052 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8053 return;
8054 }
8055
8056 /*
8057 * Repeat searching for a match until one is found that includes "mincol"
8058 * or none is found in this line.
8059 */
8060 called_emsg = FALSE;
8061 for (;;)
8062 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008063#ifdef FEAT_RELTIME
8064 /* Stop searching after passing the time limit. */
8065 if (profile_passed_limit(&(shl->tm)))
8066 {
8067 shl->lnum = 0; /* no match found in time */
8068 break;
8069 }
8070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 /* Three situations:
8072 * 1. No useful previous match: search from start of line.
8073 * 2. Not Vi compatible or empty match: continue at next character.
8074 * Break the loop if this is beyond the end of the line.
8075 * 3. Vi compatible searching: continue at end of previous match.
8076 */
8077 if (shl->lnum == 0)
8078 matchcol = 0;
8079 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8080 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008081 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008083 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008084
8085 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008086 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008087 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008089 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 shl->lnum = 0;
8091 break;
8092 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008093#ifdef FEAT_MBYTE
8094 if (has_mbyte)
8095 matchcol += mb_ptr2len(ml);
8096 else
8097#endif
8098 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 }
8100 else
8101 matchcol = shl->rm.endpos[0].col;
8102
8103 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008104 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008106 /* Remember whether shl->rm is using a copy of the regprog in
8107 * cur->match. */
8108 int regprog_is_copy = (shl != &search_hl && cur != NULL
8109 && shl == &cur->hl
8110 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008111 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008112
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8114 matchcol,
8115#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008116 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008117#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008118 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008119#endif
8120 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008121 /* Copy the regprog, in case it got freed and recompiled. */
8122 if (regprog_is_copy)
8123 cur->match.regprog = cur->hl.rm.regprog;
8124
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008125 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008126 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008127 /* Error while handling regexp: stop using this regexp. */
8128 if (shl == &search_hl)
8129 {
8130 /* don't free regprog in the match list, it's a copy */
8131 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008132 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008133 }
8134 shl->rm.regprog = NULL;
8135 shl->lnum = 0;
8136 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8137 message */
8138 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008139 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008140 }
8141 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008142 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008143 else
8144 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (nmatched == 0)
8146 {
8147 shl->lnum = 0; /* no match found */
8148 break;
8149 }
8150 if (shl->rm.startpos[0].lnum > 0
8151 || shl->rm.startpos[0].col >= mincol
8152 || nmatched > 1
8153 || shl->rm.endpos[0].col > mincol)
8154 {
8155 shl->lnum += shl->rm.startpos[0].lnum;
8156 break; /* useful match found */
8157 }
8158 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008159
8160 // Restore called_emsg for assert_fails().
8161 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
Bram Moolenaar85077472016-10-16 14:35:48 +02008164/*
8165 * If there is a match fill "shl" and return one.
8166 * Return zero otherwise.
8167 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008168 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008169next_search_hl_pos(
8170 match_T *shl, /* points to a match */
8171 linenr_T lnum,
8172 posmatch_T *posmatch, /* match positions */
8173 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008174{
8175 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008176 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008177
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8179 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008180 llpos_T *pos = &posmatch->pos[i];
8181
8182 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008183 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008184 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008185 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008186 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008188 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008189 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008190 /* if this match comes before the one at "found" then swap
8191 * them */
8192 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008194 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195
Bram Moolenaar85077472016-10-16 14:35:48 +02008196 *pos = posmatch->pos[found];
8197 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008198 }
8199 }
8200 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008201 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008202 }
8203 }
8204 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008205 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008206 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008207 colnr_T start = posmatch->pos[found].col == 0
8208 ? 0 : posmatch->pos[found].col - 1;
8209 colnr_T end = posmatch->pos[found].col == 0
8210 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008211
Bram Moolenaar85077472016-10-16 14:35:48 +02008212 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008213 shl->rm.startpos[0].lnum = 0;
8214 shl->rm.startpos[0].col = start;
8215 shl->rm.endpos[0].lnum = 0;
8216 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008217 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008218 posmatch->cur = found + 1;
8219 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008220 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008221 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008222}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008223#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008224
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008226screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 attrentry_T *aep = NULL;
8229
8230 screen_attr = attr;
8231 if (full_screen
8232#ifdef WIN3264
8233 && termcap_active
8234#endif
8235 )
8236 {
8237#ifdef FEAT_GUI
8238 if (gui.in_use)
8239 {
8240 char buf[20];
8241
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008242 /* The GUI handles this internally. */
8243 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 OUT_STR(buf);
8245 }
8246 else
8247#endif
8248 {
8249 if (attr > HL_ALL) /* special HL attr. */
8250 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008251 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 aep = syn_cterm_attr2entry(attr);
8253 else
8254 aep = syn_term_attr2entry(attr);
8255 if (aep == NULL) /* did ":syntax clear" */
8256 attr = 0;
8257 else
8258 attr = aep->ae_attr;
8259 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008260 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008262 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008263#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008264 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8265 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8266 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008267#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008268 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008269 /* If the Normal FG color has BOLD attribute and the new HL
8270 * has a FG color defined, clear BOLD. */
8271 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008272 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008274 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008275 out_str(T_UCS);
8276 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008277 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8278 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008280 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008282 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008284 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008285 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
8287 /*
8288 * Output the color or start string after bold etc., in case the
8289 * bold etc. override the color setting.
8290 */
8291 if (aep != NULL)
8292 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008293#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008294 /* When 'termguicolors' is set but fg or bg is unset,
8295 * fall back to the cterm colors. This helps for SpellBad,
8296 * where the GUI uses a red undercurl. */
8297 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008299 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008300 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008301 }
8302 else
8303#endif
8304 if (t_colors > 1)
8305 {
8306 if (aep->ae_u.cterm.fg_color)
8307 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8308 }
8309#ifdef FEAT_TERMGUICOLORS
8310 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8311 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008312 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008313 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
8315 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008316#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008317 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008319 if (aep->ae_u.cterm.bg_color)
8320 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8321 }
8322
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008323 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008324 {
8325 if (aep->ae_u.term.start != NULL)
8326 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 }
8329 }
8330 }
8331}
8332
8333 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008334screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 int do_ME = FALSE; /* output T_ME code */
8337
8338 if (screen_attr != 0
8339#ifdef WIN3264
8340 && termcap_active
8341#endif
8342 )
8343 {
8344#ifdef FEAT_GUI
8345 if (gui.in_use)
8346 {
8347 char buf[20];
8348
8349 /* use internal GUI code */
8350 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8351 OUT_STR(buf);
8352 }
8353 else
8354#endif
8355 {
8356 if (screen_attr > HL_ALL) /* special HL attr. */
8357 {
8358 attrentry_T *aep;
8359
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008360 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {
8362 /*
8363 * Assume that t_me restores the original colors!
8364 */
8365 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008366 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008367#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008368 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8369 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8370 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008371#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008372 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008373#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008374 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8375 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8376 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008377#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008378 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 do_ME = TRUE;
8380 }
8381 else
8382 {
8383 aep = syn_term_attr2entry(screen_attr);
8384 if (aep != NULL && aep->ae_u.term.stop != NULL)
8385 {
8386 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8387 do_ME = TRUE;
8388 else
8389 out_str(aep->ae_u.term.stop);
8390 }
8391 }
8392 if (aep == NULL) /* did ":syntax clear" */
8393 screen_attr = 0;
8394 else
8395 screen_attr = aep->ae_attr;
8396 }
8397
8398 /*
8399 * Often all ending-codes are equal to T_ME. Avoid outputting the
8400 * same sequence several times.
8401 */
8402 if (screen_attr & HL_STANDOUT)
8403 {
8404 if (STRCMP(T_SE, T_ME) == 0)
8405 do_ME = TRUE;
8406 else
8407 out_str(T_SE);
8408 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008409 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008410 {
8411 if (STRCMP(T_UCE, T_ME) == 0)
8412 do_ME = TRUE;
8413 else
8414 out_str(T_UCE);
8415 }
8416 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008417 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {
8419 if (STRCMP(T_UE, T_ME) == 0)
8420 do_ME = TRUE;
8421 else
8422 out_str(T_UE);
8423 }
8424 if (screen_attr & HL_ITALIC)
8425 {
8426 if (STRCMP(T_CZR, T_ME) == 0)
8427 do_ME = TRUE;
8428 else
8429 out_str(T_CZR);
8430 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008431 if (screen_attr & HL_STRIKETHROUGH)
8432 {
8433 if (STRCMP(T_STE, T_ME) == 0)
8434 do_ME = TRUE;
8435 else
8436 out_str(T_STE);
8437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8439 out_str(T_ME);
8440
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008441#ifdef FEAT_TERMGUICOLORS
8442 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008444 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008445 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008446 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008447 term_bg_rgb_color(cterm_normal_bg_gui_color);
8448 }
8449 else
8450#endif
8451 {
8452 if (t_colors > 1)
8453 {
8454 /* set Normal cterm colors */
8455 if (cterm_normal_fg_color != 0)
8456 term_fg_color(cterm_normal_fg_color - 1);
8457 if (cterm_normal_bg_color != 0)
8458 term_bg_color(cterm_normal_bg_color - 1);
8459 if (cterm_normal_fg_bold)
8460 out_str(T_MD);
8461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 }
8463 }
8464 }
8465 screen_attr = 0;
8466}
8467
8468/*
8469 * Reset the colors for a cterm. Used when leaving Vim.
8470 * The machine specific code may override this again.
8471 */
8472 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008473reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008475 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
8477 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008478#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008479 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8480 || cterm_normal_bg_gui_color != INVALCOLOR)
8481 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {
8486 out_str(T_OP);
8487 screen_attr = -1;
8488 }
8489 if (cterm_normal_fg_bold)
8490 {
8491 out_str(T_ME);
8492 screen_attr = -1;
8493 }
8494 }
8495}
8496
8497/*
8498 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8499 * using the attributes from ScreenAttrs["off"].
8500 */
8501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008502screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
8504 int attr;
8505
8506 /* Check for illegal values, just in case (could happen just after
8507 * resizing). */
8508 if (row >= screen_Rows || col >= screen_Columns)
8509 return;
8510
Bram Moolenaar494838a2015-02-10 19:20:37 +01008511 /* Outputting a character in the last cell on the screen may scroll the
8512 * screen up. Only do it when the "xn" termcap property is set, otherwise
8513 * mark the character invalid (update it when scrolled up). */
8514 if (*T_XN == NUL
8515 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516#ifdef FEAT_RIGHTLEFT
8517 /* account for first command-line character in rightleft mode */
8518 && !cmdmsg_rl
8519#endif
8520 )
8521 {
8522 ScreenAttrs[off] = (sattr_T)-1;
8523 return;
8524 }
8525
8526 /*
8527 * Stop highlighting first, so it's easier to move the cursor.
8528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (screen_char_attr != 0)
8530 attr = screen_char_attr;
8531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 attr = ScreenAttrs[off];
8533 if (screen_attr != attr)
8534 screen_stop_highlight();
8535
8536 windgoto(row, col);
8537
8538 if (screen_attr != attr)
8539 screen_start_highlight(attr);
8540
8541#ifdef FEAT_MBYTE
8542 if (enc_utf8 && ScreenLinesUC[off] != 0)
8543 {
8544 char_u buf[MB_MAXBYTES + 1];
8545
Bram Moolenaarcb070082016-04-02 22:14:51 +02008546 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008547 {
8548 if (*p_ambw == 'd'
8549# ifdef FEAT_GUI
8550 && !gui.in_use
8551# endif
8552 )
8553 {
8554 /* Clear the two screen cells. If the character is actually
8555 * single width it won't change the second cell. */
8556 out_str((char_u *)" ");
8557 term_windgoto(row, col);
8558 }
8559 /* not sure where the cursor is after drawing the ambiguous width
8560 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008561 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008562 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008563 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008565
8566 /* Convert the UTF-8 character to bytes and write it. */
8567 buf[utfc_char2bytes(off, buf)] = NUL;
8568 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570 else
8571#endif
8572 {
8573#ifdef FEAT_MBYTE
8574 out_flush_check();
8575#endif
8576 out_char(ScreenLines[off]);
8577#ifdef FEAT_MBYTE
8578 /* double-byte character in single-width cell */
8579 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8580 out_char(ScreenLines2[off]);
8581#endif
8582 }
8583
8584 screen_cur_col++;
8585}
8586
8587#ifdef FEAT_MBYTE
8588
8589/*
8590 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8591 * on the screen at position 'row' and 'col'.
8592 * The attributes of the first byte is used for all. This is required to
8593 * output the two bytes of a double-byte character with nothing in between.
8594 */
8595 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008596screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 /* Check for illegal values (could be wrong when screen was resized). */
8599 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8600 return;
8601
8602 /* Outputting the last character on the screen may scrollup the screen.
8603 * Don't to it! Mark the character invalid (update it when scrolled up) */
8604 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8605 {
8606 ScreenAttrs[off] = (sattr_T)-1;
8607 return;
8608 }
8609
8610 /* Output the first byte normally (positions the cursor), then write the
8611 * second byte directly. */
8612 screen_char(off, row, col);
8613 out_char(ScreenLines[off + 1]);
8614 ++screen_cur_col;
8615}
8616#endif
8617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618/*
8619 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8620 * This uses the contents of ScreenLines[] and doesn't change it.
8621 */
8622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008623screen_draw_rectangle(
8624 int row,
8625 int col,
8626 int height,
8627 int width,
8628 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 int r, c;
8631 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008632#ifdef FEAT_MBYTE
8633 int max_off;
8634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008636 /* Can't use ScreenLines unless initialized */
8637 if (ScreenLines == NULL)
8638 return;
8639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 if (invert)
8641 screen_char_attr = HL_INVERSE;
8642 for (r = row; r < row + height; ++r)
8643 {
8644 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008645#ifdef FEAT_MBYTE
8646 max_off = off + screen_Columns;
8647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 for (c = col; c < col + width; ++c)
8649 {
8650#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008651 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {
8653 screen_char_2(off + c, r, c);
8654 ++c;
8655 }
8656 else
8657#endif
8658 {
8659 screen_char(off + c, r, c);
8660#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008661 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 ++c;
8663#endif
8664 }
8665 }
8666 }
8667 screen_char_attr = 0;
8668}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670/*
8671 * Redraw the characters for a vertically split window.
8672 */
8673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008674redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675{
8676 int col;
8677 int width;
8678
8679# ifdef FEAT_CLIPBOARD
8680 clip_may_clear_selection(row, end - 1);
8681# endif
8682
8683 if (wp == NULL)
8684 {
8685 col = 0;
8686 width = Columns;
8687 }
8688 else
8689 {
8690 col = wp->w_wincol;
8691 width = wp->w_width;
8692 }
8693 screen_draw_rectangle(row, col, end - row, width, FALSE);
8694}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008696 static void
8697space_to_screenline(int off, int attr)
8698{
8699 ScreenLines[off] = ' ';
8700 ScreenAttrs[off] = attr;
8701# ifdef FEAT_MBYTE
8702 if (enc_utf8)
8703 ScreenLinesUC[off] = 0;
8704# endif
8705}
8706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707/*
8708 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8709 * with character 'c1' in first column followed by 'c2' in the other columns.
8710 * Use attributes 'attr'.
8711 */
8712 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008713screen_fill(
8714 int start_row,
8715 int end_row,
8716 int start_col,
8717 int end_col,
8718 int c1,
8719 int c2,
8720 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 int row;
8723 int col;
8724 int off;
8725 int end_off;
8726 int did_delete;
8727 int c;
8728 int norm_term;
8729#if defined(FEAT_GUI) || defined(UNIX)
8730 int force_next = FALSE;
8731#endif
8732
8733 if (end_row > screen_Rows) /* safety check */
8734 end_row = screen_Rows;
8735 if (end_col > screen_Columns) /* safety check */
8736 end_col = screen_Columns;
8737 if (ScreenLines == NULL
8738 || start_row >= end_row
8739 || start_col >= end_col) /* nothing to do */
8740 return;
8741
8742 /* it's a "normal" terminal when not in a GUI or cterm */
8743 norm_term = (
8744#ifdef FEAT_GUI
8745 !gui.in_use &&
8746#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008747 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 for (row = start_row; row < end_row; ++row)
8749 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008750#ifdef FEAT_MBYTE
8751 if (has_mbyte
8752# ifdef FEAT_GUI
8753 && !gui.in_use
8754# endif
8755 )
8756 {
8757 /* When drawing over the right halve of a double-wide char clear
8758 * out the left halve. When drawing over the left halve of a
8759 * double wide-char clear out the right halve. Only needed in a
8760 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008761 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008762 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008763 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008764 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008765 }
8766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 /*
8768 * Try to use delete-line termcap code, when no attributes or in a
8769 * "normal" terminal, where a bold/italic space is just a
8770 * space.
8771 */
8772 did_delete = FALSE;
8773 if (c2 == ' '
8774 && end_col == Columns
8775 && can_clear(T_CE)
8776 && (attr == 0
8777 || (norm_term
8778 && attr <= HL_ALL
8779 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8780 {
8781 /*
8782 * check if we really need to clear something
8783 */
8784 col = start_col;
8785 if (c1 != ' ') /* don't clear first char */
8786 ++col;
8787
8788 off = LineOffset[row] + col;
8789 end_off = LineOffset[row] + end_col;
8790
8791 /* skip blanks (used often, keep it fast!) */
8792#ifdef FEAT_MBYTE
8793 if (enc_utf8)
8794 while (off < end_off && ScreenLines[off] == ' '
8795 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8796 ++off;
8797 else
8798#endif
8799 while (off < end_off && ScreenLines[off] == ' '
8800 && ScreenAttrs[off] == 0)
8801 ++off;
8802 if (off < end_off) /* something to be cleared */
8803 {
8804 col = off - LineOffset[row];
8805 screen_stop_highlight();
8806 term_windgoto(row, col);/* clear rest of this screen line */
8807 out_str(T_CE);
8808 screen_start(); /* don't know where cursor is now */
8809 col = end_col - col;
8810 while (col--) /* clear chars in ScreenLines */
8811 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008812 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 ++off;
8814 }
8815 }
8816 did_delete = TRUE; /* the chars are cleared now */
8817 }
8818
8819 off = LineOffset[row] + start_col;
8820 c = c1;
8821 for (col = start_col; col < end_col; ++col)
8822 {
8823 if (ScreenLines[off] != c
8824#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008825 || (enc_utf8 && (int)ScreenLinesUC[off]
8826 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827#endif
8828 || ScreenAttrs[off] != attr
8829#if defined(FEAT_GUI) || defined(UNIX)
8830 || force_next
8831#endif
8832 )
8833 {
8834#if defined(FEAT_GUI) || defined(UNIX)
8835 /* The bold trick may make a single row of pixels appear in
8836 * the next character. When a bold character is removed, the
8837 * next character should be redrawn too. This happens for our
8838 * own GUI and for some xterms. */
8839 if (
8840# ifdef FEAT_GUI
8841 gui.in_use
8842# endif
8843# if defined(FEAT_GUI) && defined(UNIX)
8844 ||
8845# endif
8846# ifdef UNIX
8847 term_is_xterm
8848# endif
8849 )
8850 {
8851 if (ScreenLines[off] != ' '
8852 && (ScreenAttrs[off] > HL_ALL
8853 || ScreenAttrs[off] & HL_BOLD))
8854 force_next = TRUE;
8855 else
8856 force_next = FALSE;
8857 }
8858#endif
8859 ScreenLines[off] = c;
8860#ifdef FEAT_MBYTE
8861 if (enc_utf8)
8862 {
8863 if (c >= 0x80)
8864 {
8865 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008866 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 }
8868 else
8869 ScreenLinesUC[off] = 0;
8870 }
8871#endif
8872 ScreenAttrs[off] = attr;
8873 if (!did_delete || c != ' ')
8874 screen_char(off, row, col);
8875 }
8876 ++off;
8877 if (col == start_col)
8878 {
8879 if (did_delete)
8880 break;
8881 c = c2;
8882 }
8883 }
8884 if (end_col == Columns)
8885 LineWraps[row] = FALSE;
8886 if (row == Rows - 1) /* overwritten the command line */
8887 {
8888 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008889 if (start_col == 0 && end_col == Columns
8890 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008892 if (start_col == 0)
8893 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895 }
8896}
8897
8898/*
8899 * Check if there should be a delay. Used before clearing or redrawing the
8900 * screen or the command line.
8901 */
8902 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008903check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8906 && !did_wait_return
8907 && emsg_silent == 0)
8908 {
8909 out_flush();
8910 ui_delay(1000L, TRUE);
8911 emsg_on_display = FALSE;
8912 if (check_msg_scroll)
8913 msg_scroll = FALSE;
8914 }
8915}
8916
8917/*
8918 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008919 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 * Returns TRUE if there is a valid screen to write to.
8921 * Returns FALSE when starting up and screen not initialized yet.
8922 */
8923 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008924screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008926 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 return (ScreenLines != NULL);
8928}
8929
8930/*
8931 * Resize the shell to Rows and Columns.
8932 * Allocate ScreenLines[] and associated items.
8933 *
8934 * There may be some time between setting Rows and Columns and (re)allocating
8935 * ScreenLines[]. This happens when starting up and when (manually) changing
8936 * the shell size. Always use screen_Rows and screen_Columns to access items
8937 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8938 * final size of the shell is needed.
8939 */
8940 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008941screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 int new_row, old_row;
8944#ifdef FEAT_GUI
8945 int old_Rows;
8946#endif
8947 win_T *wp;
8948 int outofmem = FALSE;
8949 int len;
8950 schar_T *new_ScreenLines;
8951#ifdef FEAT_MBYTE
8952 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008953 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008955 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956#endif
8957 sattr_T *new_ScreenAttrs;
8958 unsigned *new_LineOffset;
8959 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008960 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008961 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008963 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008964 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008966retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 /*
8968 * Allocation of the screen buffers is done only when the size changes and
8969 * when Rows and Columns have been set and we have started doing full
8970 * screen stuff.
8971 */
8972 if ((ScreenLines != NULL
8973 && Rows == screen_Rows
8974 && Columns == screen_Columns
8975#ifdef FEAT_MBYTE
8976 && enc_utf8 == (ScreenLinesUC != NULL)
8977 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008978 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979#endif
8980 )
8981 || Rows == 0
8982 || Columns == 0
8983 || (!full_screen && ScreenLines == NULL))
8984 return;
8985
8986 /*
8987 * It's possible that we produce an out-of-memory message below, which
8988 * will cause this function to be called again. To break the loop, just
8989 * return here.
8990 */
8991 if (entered)
8992 return;
8993 entered = TRUE;
8994
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008995 /*
8996 * Note that the window sizes are updated before reallocating the arrays,
8997 * thus we must not redraw here!
8998 */
8999 ++RedrawingDisabled;
9000
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 win_new_shellsize(); /* fit the windows in the new sized shell */
9002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 comp_col(); /* recompute columns for shown command and ruler */
9004
9005 /*
9006 * We're changing the size of the screen.
9007 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9008 * - Move lines from the old arrays into the new arrays, clear extra
9009 * lines (unless the screen is going to be cleared).
9010 * - Free the old arrays.
9011 *
9012 * If anything fails, make ScreenLines NULL, so we don't do anything!
9013 * Continuing with the old ScreenLines may result in a crash, because the
9014 * size is wrong.
9015 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009016 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009018 if (aucmd_win != NULL)
9019 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
9021 new_ScreenLines = (schar_T *)lalloc((long_u)(
9022 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9023#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009024 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 if (enc_utf8)
9026 {
9027 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9028 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009029 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009030 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9032 }
9033 if (enc_dbcs == DBCS_JPNU)
9034 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9035 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9036#endif
9037 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9038 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9039 new_LineOffset = (unsigned *)lalloc((long_u)(
9040 Rows * sizeof(unsigned)), FALSE);
9041 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009042 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009044 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 {
9046 if (win_alloc_lines(wp) == FAIL)
9047 {
9048 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009049 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 }
9051 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009052 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9053 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009054 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009055give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009057#ifdef FEAT_MBYTE
9058 for (i = 0; i < p_mco; ++i)
9059 if (new_ScreenLinesC[i] == NULL)
9060 break;
9061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (new_ScreenLines == NULL
9063#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009064 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9066#endif
9067 || new_ScreenAttrs == NULL
9068 || new_LineOffset == NULL
9069 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009070 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 || outofmem)
9072 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009073 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009074 {
9075 /* guess the size */
9076 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9077
9078 /* Remember we did this to avoid getting outofmem messages over
9079 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009080 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009081 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009082 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009084 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009085 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009086 VIM_CLEAR(new_ScreenLinesC[i]);
9087 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009089 VIM_CLEAR(new_ScreenAttrs);
9090 VIM_CLEAR(new_LineOffset);
9091 VIM_CLEAR(new_LineWraps);
9092 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 }
9094 else
9095 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009096 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009097
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 for (new_row = 0; new_row < Rows; ++new_row)
9099 {
9100 new_LineOffset[new_row] = new_row * Columns;
9101 new_LineWraps[new_row] = FALSE;
9102
9103 /*
9104 * If the screen is not going to be cleared, copy as much as
9105 * possible from the old screen to the new one and clear the rest
9106 * (used when resizing the window at the "--more--" prompt or when
9107 * executing an external command, for the GUI).
9108 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009109 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 {
9111 (void)vim_memset(new_ScreenLines + new_row * Columns,
9112 ' ', (size_t)Columns * sizeof(schar_T));
9113#ifdef FEAT_MBYTE
9114 if (enc_utf8)
9115 {
9116 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9117 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009118 for (i = 0; i < p_mco; ++i)
9119 (void)vim_memset(new_ScreenLinesC[i]
9120 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 0, (size_t)Columns * sizeof(u8char_T));
9122 }
9123 if (enc_dbcs == DBCS_JPNU)
9124 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9125 0, (size_t)Columns * sizeof(schar_T));
9126#endif
9127 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9128 0, (size_t)Columns * sizeof(sattr_T));
9129 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009130 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 {
9132 if (screen_Columns < Columns)
9133 len = screen_Columns;
9134 else
9135 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009136#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009137 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009138 * may be invalid now. Also when p_mco changes. */
9139 if (!(enc_utf8 && ScreenLinesUC == NULL)
9140 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009141#endif
9142 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9143 ScreenLines + LineOffset[old_row],
9144 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009146 if (enc_utf8 && ScreenLinesUC != NULL
9147 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 {
9149 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9150 ScreenLinesUC + LineOffset[old_row],
9151 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009152 for (i = 0; i < p_mco; ++i)
9153 mch_memmove(new_ScreenLinesC[i]
9154 + new_LineOffset[new_row],
9155 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 (size_t)len * sizeof(u8char_T));
9157 }
9158 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9159 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9160 ScreenLines2 + LineOffset[old_row],
9161 (size_t)len * sizeof(schar_T));
9162#endif
9163 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9164 ScreenAttrs + LineOffset[old_row],
9165 (size_t)len * sizeof(sattr_T));
9166 }
9167 }
9168 }
9169 /* Use the last line of the screen for the current line. */
9170 current_ScreenLine = new_ScreenLines + Rows * Columns;
9171 }
9172
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009173 free_screenlines();
9174
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 ScreenLines = new_ScreenLines;
9176#ifdef FEAT_MBYTE
9177 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009178 for (i = 0; i < p_mco; ++i)
9179 ScreenLinesC[i] = new_ScreenLinesC[i];
9180 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 ScreenLines2 = new_ScreenLines2;
9182#endif
9183 ScreenAttrs = new_ScreenAttrs;
9184 LineOffset = new_LineOffset;
9185 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009186 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187
9188 /* It's important that screen_Rows and screen_Columns reflect the actual
9189 * size of ScreenLines[]. Set them before calling anything. */
9190#ifdef FEAT_GUI
9191 old_Rows = screen_Rows;
9192#endif
9193 screen_Rows = Rows;
9194 screen_Columns = Columns;
9195
9196 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009197 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 screenclear2();
9199
9200#ifdef FEAT_GUI
9201 else if (gui.in_use
9202 && !gui.starting
9203 && ScreenLines != NULL
9204 && old_Rows != Rows)
9205 {
9206 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9207 /*
9208 * Adjust the position of the cursor, for when executing an external
9209 * command.
9210 */
9211 if (msg_row >= Rows) /* Rows got smaller */
9212 msg_row = Rows - 1; /* put cursor at last row */
9213 else if (Rows > old_Rows) /* Rows got bigger */
9214 msg_row += Rows - old_Rows; /* put cursor in same place */
9215 if (msg_col >= Columns) /* Columns got smaller */
9216 msg_col = Columns - 1; /* put cursor at last column */
9217 }
9218#endif
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009221 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009222
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009223 /*
9224 * Do not apply autocommands more than 3 times to avoid an endless loop
9225 * in case applying autocommands always changes Rows or Columns.
9226 */
9227 if (starting == 0 && ++retry_count <= 3)
9228 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009229 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009230 /* In rare cases, autocommands may have altered Rows or Columns,
9231 * jump back to check if we need to allocate the screen again. */
9232 goto retry;
9233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234}
9235
9236 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009237free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009238{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009239#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009240 int i;
9241
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009242 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009243 for (i = 0; i < Screen_mco; ++i)
9244 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009245 vim_free(ScreenLines2);
9246#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009247 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009248 vim_free(ScreenAttrs);
9249 vim_free(LineOffset);
9250 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009251 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009252}
9253
9254 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009255screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257 check_for_delay(FALSE);
9258 screenalloc(FALSE); /* allocate screen buffers if size changed */
9259 screenclear2(); /* clear the screen */
9260}
9261
9262 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009263screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
9265 int i;
9266
9267 if (starting == NO_SCREEN || ScreenLines == NULL
9268#ifdef FEAT_GUI
9269 || (gui.in_use && gui.starting)
9270#endif
9271 )
9272 return;
9273
9274#ifdef FEAT_GUI
9275 if (!gui.in_use)
9276#endif
9277 screen_attr = -1; /* force setting the Normal colors */
9278 screen_stop_highlight(); /* don't want highlighting here */
9279
9280#ifdef FEAT_CLIPBOARD
9281 /* disable selection without redrawing it */
9282 clip_scroll_selection(9999);
9283#endif
9284
9285 /* blank out ScreenLines */
9286 for (i = 0; i < Rows; ++i)
9287 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009288 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 LineWraps[i] = FALSE;
9290 }
9291
9292 if (can_clear(T_CL))
9293 {
9294 out_str(T_CL); /* clear the display */
9295 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009296 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 }
9298 else
9299 {
9300 /* can't clear the screen, mark all chars with invalid attributes */
9301 for (i = 0; i < Rows; ++i)
9302 lineinvalid(LineOffset[i], (int)Columns);
9303 clear_cmdline = TRUE;
9304 }
9305
9306 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9307
9308 win_rest_invalid(firstwin);
9309 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009310 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 if (must_redraw == CLEAR) /* no need to clear again */
9312 must_redraw = NOT_VALID;
9313 compute_cmdrow();
9314 msg_row = cmdline_row; /* put cursor on last line for messages */
9315 msg_col = 0;
9316 screen_start(); /* don't know where cursor is now */
9317 msg_scrolled = 0; /* can't scroll back */
9318 msg_didany = FALSE;
9319 msg_didout = FALSE;
9320}
9321
9322/*
9323 * Clear one line in ScreenLines.
9324 */
9325 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009326lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9329#ifdef FEAT_MBYTE
9330 if (enc_utf8)
9331 (void)vim_memset(ScreenLinesUC + off, 0,
9332 (size_t)width * sizeof(u8char_T));
9333#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009334 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335}
9336
9337/*
9338 * Mark one line in ScreenLines invalid by setting the attributes to an
9339 * invalid value.
9340 */
9341 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009342lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
9344 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9345}
9346
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347/*
9348 * Copy part of a Screenline for vertically split window "wp".
9349 */
9350 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009351linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353 unsigned off_to = LineOffset[to] + wp->w_wincol;
9354 unsigned off_from = LineOffset[from] + wp->w_wincol;
9355
9356 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9357 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009358#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 if (enc_utf8)
9360 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009361 int i;
9362
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9364 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009365 for (i = 0; i < p_mco; ++i)
9366 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9367 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 }
9369 if (enc_dbcs == DBCS_JPNU)
9370 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9371 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9374 wp->w_width * sizeof(sattr_T));
9375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376
9377/*
9378 * Return TRUE if clearing with term string "p" would work.
9379 * It can't work when the string is empty or it won't set the right background.
9380 */
9381 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009382can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 return (*p != NUL && (t_colors <= 1
9385#ifdef FEAT_GUI
9386 || gui.in_use
9387#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009388#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009389 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009390 || (!p_tgc && cterm_normal_bg_color == 0)
9391#else
9392 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009393#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009394 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395}
9396
9397/*
9398 * Reset cursor position. Use whenever cursor was moved because of outputting
9399 * something directly to the screen (shell commands) or a terminal control
9400 * code.
9401 */
9402 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009403screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405 screen_cur_row = screen_cur_col = 9999;
9406}
9407
9408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 * Move the cursor to position "row","col" in the screen.
9410 * This tries to find the most efficient way to move, minimizing the number of
9411 * characters sent to the terminal.
9412 */
9413 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009414windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009416 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 int i;
9418 int plan;
9419 int cost;
9420 int wouldbe_col;
9421 int noinvcurs;
9422 char_u *bs;
9423 int goto_cost;
9424 int attr;
9425
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009426#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9428
9429#define PLAN_LE 1
9430#define PLAN_CR 2
9431#define PLAN_NL 3
9432#define PLAN_WRITE 4
9433 /* Can't use ScreenLines unless initialized */
9434 if (ScreenLines == NULL)
9435 return;
9436
9437 if (col != screen_cur_col || row != screen_cur_row)
9438 {
9439 /* Check for valid position. */
9440 if (row < 0) /* window without text lines? */
9441 row = 0;
9442 if (row >= screen_Rows)
9443 row = screen_Rows - 1;
9444 if (col >= screen_Columns)
9445 col = screen_Columns - 1;
9446
9447 /* check if no cursor movement is allowed in highlight mode */
9448 if (screen_attr && *T_MS == NUL)
9449 noinvcurs = HIGHL_COST;
9450 else
9451 noinvcurs = 0;
9452 goto_cost = GOTO_COST + noinvcurs;
9453
9454 /*
9455 * Plan how to do the positioning:
9456 * 1. Use CR to move it to column 0, same row.
9457 * 2. Use T_LE to move it a few columns to the left.
9458 * 3. Use NL to move a few lines down, column 0.
9459 * 4. Move a few columns to the right with T_ND or by writing chars.
9460 *
9461 * Don't do this if the cursor went beyond the last column, the cursor
9462 * position is unknown then (some terminals wrap, some don't )
9463 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009464 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 * characters to move the cursor to the right.
9466 */
9467 if (row >= screen_cur_row && screen_cur_col < Columns)
9468 {
9469 /*
9470 * If the cursor is in the same row, bigger col, we can use CR
9471 * or T_LE.
9472 */
9473 bs = NULL; /* init for GCC */
9474 attr = screen_attr;
9475 if (row == screen_cur_row && col < screen_cur_col)
9476 {
9477 /* "le" is preferred over "bc", because "bc" is obsolete */
9478 if (*T_LE)
9479 bs = T_LE; /* "cursor left" */
9480 else
9481 bs = T_BC; /* "backspace character (old) */
9482 if (*bs)
9483 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9484 else
9485 cost = 999;
9486 if (col + 1 < cost) /* using CR is less characters */
9487 {
9488 plan = PLAN_CR;
9489 wouldbe_col = 0;
9490 cost = 1; /* CR is just one character */
9491 }
9492 else
9493 {
9494 plan = PLAN_LE;
9495 wouldbe_col = col;
9496 }
9497 if (noinvcurs) /* will stop highlighting */
9498 {
9499 cost += noinvcurs;
9500 attr = 0;
9501 }
9502 }
9503
9504 /*
9505 * If the cursor is above where we want to be, we can use CR LF.
9506 */
9507 else if (row > screen_cur_row)
9508 {
9509 plan = PLAN_NL;
9510 wouldbe_col = 0;
9511 cost = (row - screen_cur_row) * 2; /* CR LF */
9512 if (noinvcurs) /* will stop highlighting */
9513 {
9514 cost += noinvcurs;
9515 attr = 0;
9516 }
9517 }
9518
9519 /*
9520 * If the cursor is in the same row, smaller col, just use write.
9521 */
9522 else
9523 {
9524 plan = PLAN_WRITE;
9525 wouldbe_col = screen_cur_col;
9526 cost = 0;
9527 }
9528
9529 /*
9530 * Check if any characters that need to be written have the
9531 * correct attributes. Also avoid UTF-8 characters.
9532 */
9533 i = col - wouldbe_col;
9534 if (i > 0)
9535 cost += i;
9536 if (cost < goto_cost && i > 0)
9537 {
9538 /*
9539 * Check if the attributes are correct without additionally
9540 * stopping highlighting.
9541 */
9542 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9543 while (i && *p++ == attr)
9544 --i;
9545 if (i != 0)
9546 {
9547 /*
9548 * Try if it works when highlighting is stopped here.
9549 */
9550 if (*--p == 0)
9551 {
9552 cost += noinvcurs;
9553 while (i && *p++ == 0)
9554 --i;
9555 }
9556 if (i != 0)
9557 cost = 999; /* different attributes, don't do it */
9558 }
9559#ifdef FEAT_MBYTE
9560 if (enc_utf8)
9561 {
9562 /* Don't use an UTF-8 char for positioning, it's slow. */
9563 for (i = wouldbe_col; i < col; ++i)
9564 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9565 {
9566 cost = 999;
9567 break;
9568 }
9569 }
9570#endif
9571 }
9572
9573 /*
9574 * We can do it without term_windgoto()!
9575 */
9576 if (cost < goto_cost)
9577 {
9578 if (plan == PLAN_LE)
9579 {
9580 if (noinvcurs)
9581 screen_stop_highlight();
9582 while (screen_cur_col > col)
9583 {
9584 out_str(bs);
9585 --screen_cur_col;
9586 }
9587 }
9588 else if (plan == PLAN_CR)
9589 {
9590 if (noinvcurs)
9591 screen_stop_highlight();
9592 out_char('\r');
9593 screen_cur_col = 0;
9594 }
9595 else if (plan == PLAN_NL)
9596 {
9597 if (noinvcurs)
9598 screen_stop_highlight();
9599 while (screen_cur_row < row)
9600 {
9601 out_char('\n');
9602 ++screen_cur_row;
9603 }
9604 screen_cur_col = 0;
9605 }
9606
9607 i = col - screen_cur_col;
9608 if (i > 0)
9609 {
9610 /*
9611 * Use cursor-right if it's one character only. Avoids
9612 * removing a line of pixels from the last bold char, when
9613 * using the bold trick in the GUI.
9614 */
9615 if (T_ND[0] != NUL && T_ND[1] == NUL)
9616 {
9617 while (i-- > 0)
9618 out_char(*T_ND);
9619 }
9620 else
9621 {
9622 int off;
9623
9624 off = LineOffset[row] + screen_cur_col;
9625 while (i-- > 0)
9626 {
9627 if (ScreenAttrs[off] != screen_attr)
9628 screen_stop_highlight();
9629#ifdef FEAT_MBYTE
9630 out_flush_check();
9631#endif
9632 out_char(ScreenLines[off]);
9633#ifdef FEAT_MBYTE
9634 if (enc_dbcs == DBCS_JPNU
9635 && ScreenLines[off] == 0x8e)
9636 out_char(ScreenLines2[off]);
9637#endif
9638 ++off;
9639 }
9640 }
9641 }
9642 }
9643 }
9644 else
9645 cost = 999;
9646
9647 if (cost >= goto_cost)
9648 {
9649 if (noinvcurs)
9650 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009651 if (row == screen_cur_row && (col > screen_cur_col)
9652 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 term_cursor_right(col - screen_cur_col);
9654 else
9655 term_windgoto(row, col);
9656 }
9657 screen_cur_row = row;
9658 screen_cur_col = col;
9659 }
9660}
9661
9662/*
9663 * Set cursor to its position in the current window.
9664 */
9665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009666setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009668 setcursor_mayforce(FALSE);
9669}
9670
9671/*
9672 * Set cursor to its position in the current window.
9673 * When "force" is TRUE also when not redrawing.
9674 */
9675 void
9676setcursor_mayforce(int force)
9677{
9678 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 {
9680 validate_cursor();
9681 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009682 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009684 /* With 'rightleft' set and the cursor on a double-wide
9685 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009686 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009688 (has_mbyte
9689 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9690 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691# endif
9692 1)) :
9693#endif
9694 curwin->w_wcol));
9695 }
9696}
9697
9698
9699/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009700 * Insert 'line_count' lines at 'row' in window 'wp'.
9701 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9702 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * scrolling.
9704 * Returns FAIL if the lines are not inserted, OK for success.
9705 */
9706 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009707win_ins_lines(
9708 win_T *wp,
9709 int row,
9710 int line_count,
9711 int invalid,
9712 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
9714 int did_delete;
9715 int nextrow;
9716 int lastrow;
9717 int retval;
9718
9719 if (invalid)
9720 wp->w_lines_valid = 0;
9721
9722 if (wp->w_height < 5)
9723 return FAIL;
9724
9725 if (line_count > wp->w_height - row)
9726 line_count = wp->w_height - row;
9727
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009728 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 if (retval != MAYBE)
9730 return retval;
9731
9732 /*
9733 * If there is a next window or a status line, we first try to delete the
9734 * lines at the bottom to avoid messing what is after the window.
9735 * If this fails and there are following windows, don't do anything to avoid
9736 * messing up those windows, better just redraw.
9737 */
9738 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (wp->w_next != NULL || wp->w_status_height)
9740 {
9741 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009742 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 did_delete = TRUE;
9744 else if (wp->w_next)
9745 return FAIL;
9746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 /*
9748 * if no lines deleted, blank the lines that will end up below the window
9749 */
9750 if (!did_delete)
9751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009754 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 lastrow = nextrow + line_count;
9756 if (lastrow > Rows)
9757 lastrow = Rows;
9758 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009759 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 ' ', ' ', 0);
9761 }
9762
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009763 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 == FAIL)
9765 {
9766 /* deletion will have messed up other windows */
9767 if (did_delete)
9768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 win_rest_invalid(W_NEXT(wp));
9771 }
9772 return FAIL;
9773 }
9774
9775 return OK;
9776}
9777
9778/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009779 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9781 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9782 * scrolling
9783 * Return OK for success, FAIL if the lines are not deleted.
9784 */
9785 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009786win_del_lines(
9787 win_T *wp,
9788 int row,
9789 int line_count,
9790 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009791 int mayclear,
9792 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793{
9794 int retval;
9795
9796 if (invalid)
9797 wp->w_lines_valid = 0;
9798
9799 if (line_count > wp->w_height - row)
9800 line_count = wp->w_height - row;
9801
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009802 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 if (retval != MAYBE)
9804 return retval;
9805
9806 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009807 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 return FAIL;
9809
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 /*
9811 * If there are windows or status lines below, try to put them at the
9812 * correct place. If we can't do that, they have to be redrawn.
9813 */
9814 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9815 {
9816 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009817 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 {
9819 wp->w_redr_status = TRUE;
9820 win_rest_invalid(wp->w_next);
9821 }
9822 }
9823 /*
9824 * If this is the last window and there is no status line, redraw the
9825 * command line later.
9826 */
9827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 redraw_cmdline = TRUE;
9829 return OK;
9830}
9831
9832/*
9833 * Common code for win_ins_lines() and win_del_lines().
9834 * Returns OK or FAIL when the work has been done.
9835 * Returns MAYBE when not finished yet.
9836 */
9837 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009838win_do_lines(
9839 win_T *wp,
9840 int row,
9841 int line_count,
9842 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009843 int del,
9844 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845{
9846 int retval;
9847
9848 if (!redrawing() || line_count <= 0)
9849 return FAIL;
9850
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009851 /* When inserting lines would result in loss of command output, just redraw
9852 * the lines. */
9853 if (no_win_do_lines_ins && !del)
9854 return FAIL;
9855
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009857 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009859 if (!no_win_do_lines_ins)
9860 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 return FAIL;
9862 }
9863
9864 /*
9865 * Delete all remaining lines
9866 */
9867 if (row + line_count >= wp->w_height)
9868 {
9869 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009870 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 ' ', ' ', 0);
9872 return OK;
9873 }
9874
9875 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009876 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009878 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009880 if (!no_win_do_lines_ins)
9881 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
9883 /*
9884 * If the terminal can set a scroll region, use that.
9885 * Always do this in a vertically split window. This will redraw from
9886 * ScreenLines[] when t_CV isn't defined. That's faster than using
9887 * win_line().
9888 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009889 * a character in the lower right corner of the scroll region may cause a
9890 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009892 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 scroll_region_set(wp, row);
9896 if (del)
9897 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009898 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 else
9900 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009901 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 scroll_region_reset();
9904 return retval;
9905 }
9906
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9908 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909
9910 return MAYBE;
9911}
9912
9913/*
9914 * window 'wp' and everything after it is messed up, mark it for redraw
9915 */
9916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009917win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 {
9921 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 wp->w_redr_status = TRUE;
9923 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 }
9925 redraw_cmdline = TRUE;
9926}
9927
9928/*
9929 * The rest of the routines in this file perform screen manipulations. The
9930 * given operation is performed physically on the screen. The corresponding
9931 * change is also made to the internal screen image. In this way, the editor
9932 * anticipates the effect of editing changes on the appearance of the screen.
9933 * That way, when we call screenupdate a complete redraw isn't usually
9934 * necessary. Another advantage is that we can keep adding code to anticipate
9935 * screen changes, and in the meantime, everything still works.
9936 */
9937
9938/*
9939 * types for inserting or deleting lines
9940 */
9941#define USE_T_CAL 1
9942#define USE_T_CDL 2
9943#define USE_T_AL 3
9944#define USE_T_CE 4
9945#define USE_T_DL 5
9946#define USE_T_SR 6
9947#define USE_NL 7
9948#define USE_T_CD 8
9949#define USE_REDRAW 9
9950
9951/*
9952 * insert lines on the screen and update ScreenLines[]
9953 * 'end' is the line after the scrolled part. Normally it is Rows.
9954 * When scrolling region used 'off' is the offset from the top for the region.
9955 * 'row' and 'end' are relative to the start of the region.
9956 *
9957 * return FAIL for failure, OK for success.
9958 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009959 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009960screen_ins_lines(
9961 int off,
9962 int row,
9963 int line_count,
9964 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009965 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009966 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967{
9968 int i;
9969 int j;
9970 unsigned temp;
9971 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009972 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 int type;
9974 int result_empty;
9975 int can_ce = can_clear(T_CE);
9976
9977 /*
9978 * FAIL if
9979 * - there is no valid screen
9980 * - the screen has to be redrawn completely
9981 * - the line count is less than one
9982 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009983 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009985 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9986#ifdef FEAT_CLIPBOARD
9987 || (clip_star.state != SELECT_CLEARED
9988 && redrawing_for_callback > 0)
9989#endif
9990 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 return FAIL;
9992
9993 /*
9994 * There are seven ways to insert lines:
9995 * 0. When in a vertically split window and t_CV isn't set, redraw the
9996 * characters from ScreenLines[].
9997 * 1. Use T_CD (clear to end of display) if it exists and the result of
9998 * the insert is just empty lines
9999 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10000 * present or line_count > 1. It looks better if we do all the inserts
10001 * at once.
10002 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10003 * insert is just empty lines and T_CE is not present or line_count >
10004 * 1.
10005 * 4. Use T_AL (insert line) if it exists.
10006 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10007 * just empty lines.
10008 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10009 * just empty lines.
10010 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10011 * the 'da' flag is not set or we have clear line capability.
10012 * 8. redraw the characters from ScreenLines[].
10013 *
10014 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10015 * the scrollbar for the window. It does have insert line, use that if it
10016 * exists.
10017 */
10018 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10020 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010021 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 type = USE_T_CD;
10023 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10024 type = USE_T_CAL;
10025 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10026 type = USE_T_CDL;
10027 else if (*T_AL != NUL)
10028 type = USE_T_AL;
10029 else if (can_ce && result_empty)
10030 type = USE_T_CE;
10031 else if (*T_DL != NUL && result_empty)
10032 type = USE_T_DL;
10033 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10034 type = USE_T_SR;
10035 else
10036 return FAIL;
10037
10038 /*
10039 * For clearing the lines screen_del_lines() is used. This will also take
10040 * care of t_db if necessary.
10041 */
10042 if (type == USE_T_CD || type == USE_T_CDL ||
10043 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010044 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
10046 /*
10047 * If text is retained below the screen, first clear or delete as many
10048 * lines at the bottom of the window as are about to be inserted so that
10049 * the deleted lines won't later surface during a screen_del_lines.
10050 */
10051 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010052 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054#ifdef FEAT_CLIPBOARD
10055 /* Remove a modeless selection when inserting lines halfway the screen
10056 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010057 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010058 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 else
10060 clip_scroll_selection(-line_count);
10061#endif
10062
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063#ifdef FEAT_GUI
10064 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10065 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010066 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#endif
10068
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010069 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10070 cursor_col = wp->w_wincol;
10071
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (*T_CCS != NUL) /* cursor relative to region */
10073 cursor_row = row;
10074 else
10075 cursor_row = row + off;
10076
10077 /*
10078 * Shift LineOffset[] line_count down to reflect the inserted lines.
10079 * Clear the inserted lines in ScreenLines[].
10080 */
10081 row += off;
10082 end += off;
10083 for (i = 0; i < line_count; ++i)
10084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (wp != NULL && wp->w_width != Columns)
10086 {
10087 /* need to copy part of a line */
10088 j = end - 1 - i;
10089 while ((j -= line_count) >= row)
10090 linecopy(j + line_count, j, wp);
10091 j += line_count;
10092 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010093 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10094 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 else
10096 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10097 LineWraps[j] = FALSE;
10098 }
10099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 {
10101 j = end - 1 - i;
10102 temp = LineOffset[j];
10103 while ((j -= line_count) >= row)
10104 {
10105 LineOffset[j + line_count] = LineOffset[j];
10106 LineWraps[j + line_count] = LineWraps[j];
10107 }
10108 LineOffset[j + line_count] = temp;
10109 LineWraps[j + line_count] = FALSE;
10110 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010111 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 else
10113 lineinvalid(temp, (int)Columns);
10114 }
10115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
10117 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010118 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010119 if (clear_attr != 0)
10120 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 /* redraw the characters */
10123 if (type == USE_REDRAW)
10124 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010125 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
10127 term_append_lines(line_count);
10128 screen_start(); /* don't know where cursor is now */
10129 }
10130 else
10131 {
10132 for (i = 0; i < line_count; i++)
10133 {
10134 if (type == USE_T_AL)
10135 {
10136 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010137 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 out_str(T_AL);
10139 }
10140 else /* type == USE_T_SR */
10141 out_str(T_SR);
10142 screen_start(); /* don't know where cursor is now */
10143 }
10144 }
10145
10146 /*
10147 * With scroll-reverse and 'da' flag set we need to clear the lines that
10148 * have been scrolled down into the region.
10149 */
10150 if (type == USE_T_SR && *T_DA)
10151 {
10152 for (i = 0; i < line_count; ++i)
10153 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010154 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 out_str(T_CE);
10156 screen_start(); /* don't know where cursor is now */
10157 }
10158 }
10159
10160#ifdef FEAT_GUI
10161 gui_can_update_cursor();
10162 if (gui.in_use)
10163 out_flush(); /* always flush after a scroll */
10164#endif
10165 return OK;
10166}
10167
10168/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010169 * Delete lines on the screen and update ScreenLines[].
10170 * "end" is the line after the scrolled part. Normally it is Rows.
10171 * When scrolling region used "off" is the offset from the top for the region.
10172 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 *
10174 * Return OK for success, FAIL if the lines are not deleted.
10175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010177screen_del_lines(
10178 int off,
10179 int row,
10180 int line_count,
10181 int end,
10182 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010183 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010184 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 int j;
10187 int i;
10188 unsigned temp;
10189 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010190 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 int cursor_end;
10192 int result_empty; /* result is empty until end of region */
10193 int can_delete; /* deleting line codes can be used */
10194 int type;
10195
10196 /*
10197 * FAIL if
10198 * - there is no valid screen
10199 * - the screen has to be redrawn completely
10200 * - the line count is less than one
10201 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010202 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010204 if (!screen_valid(TRUE) || line_count <= 0
10205 || (!force && line_count > p_ttyscroll)
10206#ifdef FEAT_CLIPBOARD
10207 || (clip_star.state != SELECT_CLEARED
10208 && redrawing_for_callback > 0)
10209#endif
10210 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 return FAIL;
10212
10213 /*
10214 * Check if the rest of the current region will become empty.
10215 */
10216 result_empty = row + line_count >= end;
10217
10218 /*
10219 * We can delete lines only when 'db' flag not set or when 'ce' option
10220 * available.
10221 */
10222 can_delete = (*T_DB == NUL || can_clear(T_CE));
10223
10224 /*
10225 * There are six ways to delete lines:
10226 * 0. When in a vertically split window and t_CV isn't set, redraw the
10227 * characters from ScreenLines[].
10228 * 1. Use T_CD if it exists and the result is empty.
10229 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10230 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10231 * none of the other ways work.
10232 * 4. Use T_CE (erase line) if the result is empty.
10233 * 5. Use T_DL (delete line) if it exists.
10234 * 6. redraw the characters from ScreenLines[].
10235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10237 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010238 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 type = USE_T_CD;
10240#if defined(__BEOS__) && defined(BEOS_DR8)
10241 /*
10242 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10243 * its internal termcap... this works okay for tests which test *T_DB !=
10244 * NUL. It has the disadvantage that the user cannot use any :set t_*
10245 * command to get T_DB (back) to empty_option, only :set term=... will do
10246 * the trick...
10247 * Anyway, this hack will hopefully go away with the next OS release.
10248 * (Olaf Seibert)
10249 */
10250 else if (row == 0 && T_DB == empty_option
10251 && (line_count == 1 || *T_CDL == NUL))
10252#else
10253 else if (row == 0 && (
10254#ifndef AMIGA
10255 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10256 * up, so use delete-line command */
10257 line_count == 1 ||
10258#endif
10259 *T_CDL == NUL))
10260#endif
10261 type = USE_NL;
10262 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10263 type = USE_T_CDL;
10264 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010265 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 type = USE_T_CE;
10267 else if (*T_DL != NUL && can_delete)
10268 type = USE_T_DL;
10269 else if (*T_CDL != NUL && can_delete)
10270 type = USE_T_CDL;
10271 else
10272 return FAIL;
10273
10274#ifdef FEAT_CLIPBOARD
10275 /* Remove a modeless selection when deleting lines halfway the screen or
10276 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010277 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010278 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 else
10280 clip_scroll_selection(line_count);
10281#endif
10282
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283#ifdef FEAT_GUI
10284 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10285 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010286 gui_dont_update_cursor(gui.cursor_row >= row + off
10287 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288#endif
10289
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010290 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10291 cursor_col = wp->w_wincol;
10292
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 if (*T_CCS != NUL) /* cursor relative to region */
10294 {
10295 cursor_row = row;
10296 cursor_end = end;
10297 }
10298 else
10299 {
10300 cursor_row = row + off;
10301 cursor_end = end + off;
10302 }
10303
10304 /*
10305 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10306 * Clear the inserted lines in ScreenLines[].
10307 */
10308 row += off;
10309 end += off;
10310 for (i = 0; i < line_count; ++i)
10311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 if (wp != NULL && wp->w_width != Columns)
10313 {
10314 /* need to copy part of a line */
10315 j = row + i;
10316 while ((j += line_count) <= end - 1)
10317 linecopy(j - line_count, j, wp);
10318 j -= line_count;
10319 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010320 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10321 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 else
10323 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10324 LineWraps[j] = FALSE;
10325 }
10326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 {
10328 /* whole width, moving the line pointers is faster */
10329 j = row + i;
10330 temp = LineOffset[j];
10331 while ((j += line_count) <= end - 1)
10332 {
10333 LineOffset[j - line_count] = LineOffset[j];
10334 LineWraps[j - line_count] = LineWraps[j];
10335 }
10336 LineOffset[j - line_count] = temp;
10337 LineWraps[j - line_count] = FALSE;
10338 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010339 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 else
10341 lineinvalid(temp, (int)Columns);
10342 }
10343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010345 if (screen_attr != clear_attr)
10346 screen_stop_highlight();
10347 if (clear_attr != 0)
10348 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 /* redraw the characters */
10351 if (type == USE_REDRAW)
10352 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010353 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010355 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 out_str(T_CD);
10357 screen_start(); /* don't know where cursor is now */
10358 }
10359 else if (type == USE_T_CDL)
10360 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010361 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 term_delete_lines(line_count);
10363 screen_start(); /* don't know where cursor is now */
10364 }
10365 /*
10366 * Deleting lines at top of the screen or scroll region: Just scroll
10367 * the whole screen (scroll region) up by outputting newlines on the
10368 * last line.
10369 */
10370 else if (type == USE_NL)
10371 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010372 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 for (i = line_count; --i >= 0; )
10374 out_char('\n'); /* cursor will remain on same line */
10375 }
10376 else
10377 {
10378 for (i = line_count; --i >= 0; )
10379 {
10380 if (type == USE_T_DL)
10381 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010382 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 out_str(T_DL); /* delete a line */
10384 }
10385 else /* type == USE_T_CE */
10386 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010387 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 out_str(T_CE); /* erase a line */
10389 }
10390 screen_start(); /* don't know where cursor is now */
10391 }
10392 }
10393
10394 /*
10395 * If the 'db' flag is set, we need to clear the lines that have been
10396 * scrolled up at the bottom of the region.
10397 */
10398 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10399 {
10400 for (i = line_count; i > 0; --i)
10401 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010402 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 out_str(T_CE); /* erase a line */
10404 screen_start(); /* don't know where cursor is now */
10405 }
10406 }
10407
10408#ifdef FEAT_GUI
10409 gui_can_update_cursor();
10410 if (gui.in_use)
10411 out_flush(); /* always flush after a scroll */
10412#endif
10413
10414 return OK;
10415}
10416
10417/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010418 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 *
10420 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10421 * If clear_cmdline is FALSE there may be a message there that needs to be
10422 * cleared only if a mode is shown.
10423 * Return the length of the message (0 if no message).
10424 */
10425 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010426showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427{
10428 int need_clear;
10429 int length = 0;
10430 int do_mode;
10431 int attr;
10432 int nwr_save;
10433#ifdef FEAT_INS_EXPAND
10434 int sub_attr;
10435#endif
10436
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010437 do_mode = ((p_smd && msg_silent == 0)
10438 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010439 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010440 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010441 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 {
10443 /*
10444 * Don't show mode right now, when not redrawing or inside a mapping.
10445 * Call char_avail() only when we are going to show something, because
10446 * it takes a bit of time.
10447 */
10448 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10449 {
10450 redraw_cmdline = TRUE; /* show mode later */
10451 return 0;
10452 }
10453
10454 nwr_save = need_wait_return;
10455
10456 /* wait a bit before overwriting an important message */
10457 check_for_delay(FALSE);
10458
10459 /* if the cmdline is more than one line high, erase top lines */
10460 need_clear = clear_cmdline;
10461 if (clear_cmdline && cmdline_row < Rows - 1)
10462 msg_clr_cmdline(); /* will reset clear_cmdline */
10463
10464 /* Position on the last line in the window, column 0 */
10465 msg_pos_mode();
10466 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010467 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 if (do_mode)
10469 {
10470 MSG_PUTS_ATTR("--", attr);
10471#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010472 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010473# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010474 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010475# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010476 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010477# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010478 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010479# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 MSG_PUTS_ATTR(" IM", attr);
10481# else
10482 MSG_PUTS_ATTR(" XIM", attr);
10483# endif
10484#endif
10485#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10486 if (gui.in_use)
10487 {
10488 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010489 {
10490 /* HANGUL */
10491 if (enc_utf8)
10492 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10493 else
10494 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 }
10497#endif
10498#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010499 /* CTRL-X in Insert mode */
10500 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 {
10502 /* These messages can get long, avoid a wrap in a narrow
10503 * window. Prefer showing edit_submode_extra. */
10504 length = (Rows - msg_row) * Columns - 3;
10505 if (edit_submode_extra != NULL)
10506 length -= vim_strsize(edit_submode_extra);
10507 if (length > 0)
10508 {
10509 if (edit_submode_pre != NULL)
10510 length -= vim_strsize(edit_submode_pre);
10511 if (length - vim_strsize(edit_submode) > 0)
10512 {
10513 if (edit_submode_pre != NULL)
10514 msg_puts_attr(edit_submode_pre, attr);
10515 msg_puts_attr(edit_submode, attr);
10516 }
10517 if (edit_submode_extra != NULL)
10518 {
10519 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10520 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010521 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 else
10523 sub_attr = attr;
10524 msg_puts_attr(edit_submode_extra, sub_attr);
10525 }
10526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 }
10528 else
10529#endif
10530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 if (State & VREPLACE_FLAG)
10532 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010533 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10535 else if (State & INSERT)
10536 {
10537#ifdef FEAT_RIGHTLEFT
10538 if (p_ri)
10539 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10540#endif
10541 MSG_PUTS_ATTR(_(" INSERT"), attr);
10542 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010543 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 MSG_PUTS_ATTR(_(" (insert)"), attr);
10545 else if (restart_edit == 'R')
10546 MSG_PUTS_ATTR(_(" (replace)"), attr);
10547 else if (restart_edit == 'V')
10548 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10549#ifdef FEAT_RIGHTLEFT
10550 if (p_hkmap)
10551 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10552# ifdef FEAT_FKMAP
10553 if (p_fkmap)
10554 MSG_PUTS_ATTR(farsi_text_5, attr);
10555# endif
10556#endif
10557#ifdef FEAT_KEYMAP
10558 if (State & LANGMAP)
10559 {
10560# ifdef FEAT_ARABIC
10561 if (curwin->w_p_arab)
10562 MSG_PUTS_ATTR(_(" Arabic"), attr);
10563 else
10564# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010565 if (get_keymap_str(curwin, (char_u *)" (%s)",
10566 NameBuff, MAXPATHL))
10567 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569#endif
10570 if ((State & INSERT) && p_paste)
10571 MSG_PUTS_ATTR(_(" (paste)"), attr);
10572
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 if (VIsual_active)
10574 {
10575 char *p;
10576
10577 /* Don't concatenate separate words to avoid translation
10578 * problems. */
10579 switch ((VIsual_select ? 4 : 0)
10580 + (VIsual_mode == Ctrl_V) * 2
10581 + (VIsual_mode == 'V'))
10582 {
10583 case 0: p = N_(" VISUAL"); break;
10584 case 1: p = N_(" VISUAL LINE"); break;
10585 case 2: p = N_(" VISUAL BLOCK"); break;
10586 case 4: p = N_(" SELECT"); break;
10587 case 5: p = N_(" SELECT LINE"); break;
10588 default: p = N_(" SELECT BLOCK"); break;
10589 }
10590 MSG_PUTS_ATTR(_(p), attr);
10591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 MSG_PUTS_ATTR(" --", attr);
10593 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 need_clear = TRUE;
10596 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010597 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598#ifdef FEAT_INS_EXPAND
10599 && edit_submode == NULL /* otherwise it gets too long */
10600#endif
10601 )
10602 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010603 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 need_clear = TRUE;
10605 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010606
10607 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 if (need_clear || clear_cmdline)
10609 msg_clr_eos();
10610 msg_didout = FALSE; /* overwrite this message */
10611 length = msg_col;
10612 msg_col = 0;
10613 need_wait_return = nwr_save; /* never ask for hit-return for this */
10614 }
10615 else if (clear_cmdline && msg_silent == 0)
10616 /* Clear the whole command line. Will reset "clear_cmdline". */
10617 msg_clr_cmdline();
10618
10619#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 /* In Visual mode the size of the selected area must be redrawn. */
10621 if (VIsual_active)
10622 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623
10624 /* If the last window has no status line, the ruler is after the mode
10625 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010626 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010627 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628#endif
10629 redraw_cmdline = FALSE;
10630 clear_cmdline = FALSE;
10631
10632 return length;
10633}
10634
10635/*
10636 * Position for a mode message.
10637 */
10638 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010639msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640{
10641 msg_col = 0;
10642 msg_row = Rows - 1;
10643}
10644
10645/*
10646 * Delete mode message. Used when ESC is typed which is expected to end
10647 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010648 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 */
10650 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010651unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652{
10653 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010654 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 */
10656 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10657 redraw_cmdline = TRUE; /* delete mode later */
10658 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010659 clearmode();
10660}
10661
10662/*
10663 * Clear the mode message.
10664 */
10665 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010666clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010667{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010668 int save_msg_row = msg_row;
10669 int save_msg_col = msg_col;
10670
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010671 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010672 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010673 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010674 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010675
10676 msg_col = save_msg_col;
10677 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678}
10679
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010680 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010681recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010682{
10683 MSG_PUTS_ATTR(_("recording"), attr);
10684 if (!shortmess(SHM_RECORDING))
10685 {
10686 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010687 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010688 MSG_PUTS_ATTR(s, attr);
10689 }
10690}
10691
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010692/*
10693 * Draw the tab pages line at the top of the Vim window.
10694 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010695 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010696draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010697{
10698 int tabcount = 0;
10699 tabpage_T *tp;
10700 int tabwidth;
10701 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010702 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010703 int attr;
10704 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010705 win_T *cwp;
10706 int wincount;
10707 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010708 int c;
10709 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010710 int attr_sel = HL_ATTR(HLF_TPS);
10711 int attr_nosel = HL_ATTR(HLF_TP);
10712 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010713 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010714 int room;
10715 int use_sep_chars = (t_colors < 8
10716#ifdef FEAT_GUI
10717 && !gui.in_use
10718#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010719#ifdef FEAT_TERMGUICOLORS
10720 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010721#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010722 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010723
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010724 if (ScreenLines == NULL)
10725 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010726 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010727
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010728#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010729 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010730 if (gui_use_tabline())
10731 {
10732 gui_update_tabline();
10733 return;
10734 }
10735#endif
10736
10737 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010738 return;
10739
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010740#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010741
10742 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10743 for (scol = 0; scol < Columns; ++scol)
10744 TabPageIdxs[scol] = 0;
10745
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010746 /* Use the 'tabline' option if it's set. */
10747 if (*p_tal != NUL)
10748 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010749 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010750
10751 /* Check for an error. If there is one we would loop in redrawing the
10752 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010753 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010754 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010755 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010756 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010757 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010758 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010759 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010760 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010761#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010762 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010763 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010764 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010765
Bram Moolenaar238a5642006-02-21 22:12:05 +000010766 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10767 if (tabwidth < 6)
10768 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010769
Bram Moolenaar238a5642006-02-21 22:12:05 +000010770 attr = attr_nosel;
10771 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010772 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010773 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10774 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010775 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010776 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010777
Bram Moolenaar238a5642006-02-21 22:12:05 +000010778 if (tp->tp_topframe == topframe)
10779 attr = attr_sel;
10780 if (use_sep_chars && col > 0)
10781 screen_putchar('|', 0, col++, attr);
10782
10783 if (tp->tp_topframe != topframe)
10784 attr = attr_nosel;
10785
10786 screen_putchar(' ', 0, col++, attr);
10787
10788 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010789 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010790 cwp = curwin;
10791 wp = firstwin;
10792 }
10793 else
10794 {
10795 cwp = tp->tp_curwin;
10796 wp = tp->tp_firstwin;
10797 }
10798
10799 modified = FALSE;
10800 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10801 if (bufIsChanged(wp->w_buffer))
10802 modified = TRUE;
10803 if (modified || wincount > 1)
10804 {
10805 if (wincount > 1)
10806 {
10807 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010808 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010809 if (col + len >= Columns - 3)
10810 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010811 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010812#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010813 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010814#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010815 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010816#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010817 );
10818 col += len;
10819 }
10820 if (modified)
10821 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10822 screen_putchar(' ', 0, col++, attr);
10823 }
10824
10825 room = scol - col + tabwidth - 1;
10826 if (room > 0)
10827 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010828 /* Get buffer name in NameBuff[] */
10829 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010830 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010831 len = vim_strsize(NameBuff);
10832 p = NameBuff;
10833#ifdef FEAT_MBYTE
10834 if (has_mbyte)
10835 while (len > room)
10836 {
10837 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010838 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010839 }
10840 else
10841#endif
10842 if (len > room)
10843 {
10844 p += len - room;
10845 len = room;
10846 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010847 if (len > Columns - col - 1)
10848 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010849
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010850 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010851 col += len;
10852 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010853 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010854
10855 /* Store the tab page number in TabPageIdxs[], so that
10856 * jump_to_mouse() knows where each one is. */
10857 ++tabcount;
10858 while (scol < col)
10859 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010860 }
10861
Bram Moolenaar238a5642006-02-21 22:12:05 +000010862 if (use_sep_chars)
10863 c = '_';
10864 else
10865 c = ' ';
10866 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010867
10868 /* Put an "X" for closing the current tab if there are several. */
10869 if (first_tabpage->tp_next != NULL)
10870 {
10871 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10872 TabPageIdxs[Columns - 1] = -999;
10873 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010874 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010875
10876 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10877 * set. */
10878 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010879}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010880
10881/*
10882 * Get buffer name for "buf" into NameBuff[].
10883 * Takes care of special buffer names and translates special characters.
10884 */
10885 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010886get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010887{
10888 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010889 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010890 else
10891 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10892 trans_characters(NameBuff, MAXPATHL);
10893}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010894
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895/*
10896 * Get the character to use in a status line. Get its attributes in "*attr".
10897 */
10898 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010899fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010902
10903#ifdef FEAT_TERMINAL
10904 if (bt_terminal(wp->w_buffer))
10905 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010906 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010907 {
10908 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010909 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010910 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010911 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010912 {
10913 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010914 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010915 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010916 }
10917 else
10918#endif
10919 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010921 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 fill = fill_stl;
10923 }
10924 else
10925 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010926 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 fill = fill_stlnc;
10928 }
10929 /* Use fill when there is highlighting, and highlighting of current
10930 * window differs, or the fillchars differ, or this is not the
10931 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010932 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010933 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 || (fill_stl != fill_stlnc)))
10935 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010936 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 return '^';
10938 return '=';
10939}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941/*
10942 * Get the character to use in a separator between vertically split windows.
10943 * Get its attributes in "*attr".
10944 */
10945 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010946fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010948 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 if (*attr == 0 && fill_vert == ' ')
10950 return '|';
10951 else
10952 return fill_vert;
10953}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954
10955/*
10956 * Return TRUE if redrawing should currently be done.
10957 */
10958 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010959redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010961#ifdef FEAT_EVAL
10962 if (disable_redraw_for_testing)
10963 return 0;
10964 else
10965#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010966 return ((!RedrawingDisabled
10967#ifdef FEAT_EVAL
10968 || ignore_redraw_flag_for_testing
10969#endif
10970 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971}
10972
10973/*
10974 * Return TRUE if printing messages should currently be done.
10975 */
10976 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010977messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978{
10979 return (!(p_lz && char_avail() && !KeyTyped));
10980}
10981
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010982#ifdef FEAT_MENU
10983/*
10984 * Draw the window toolbar.
10985 */
10986 static void
10987redraw_win_toolbar(win_T *wp)
10988{
10989 vimmenu_T *menu;
10990 int item_idx = 0;
10991 int item_count = 0;
10992 int col = 0;
10993 int next_col;
10994 int off = (int)(current_ScreenLine - ScreenLines);
10995 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10996 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10997
10998 vim_free(wp->w_winbar_items);
10999 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11000 ++item_count;
11001 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
11002 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
11003
11004 /* TODO: use fewer spaces if there is not enough room */
11005 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011006 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011007 {
11008 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011009 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011010 break;
11011 if (col > 1)
11012 {
11013 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011014 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011015 break;
11016 }
11017
11018 wp->w_winbar_items[item_idx].wb_startcol = col;
11019 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011020 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011021 break;
11022
11023 next_col = text_to_screenline(wp, menu->name, col);
11024 while (col < next_col)
11025 {
11026 ScreenAttrs[off + col] = button_attr;
11027 ++col;
11028 }
11029 wp->w_winbar_items[item_idx].wb_endcol = col;
11030 wp->w_winbar_items[item_idx].wb_menu = menu;
11031 ++item_idx;
11032
Bram Moolenaar02631462017-09-22 15:20:32 +020011033 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011034 break;
11035 space_to_screenline(off + col, button_attr);
11036 ++col;
11037 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011038 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011039 {
11040 space_to_screenline(off + col, fill_attr);
11041 ++col;
11042 }
11043 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11044
Bram Moolenaar02631462017-09-22 15:20:32 +020011045 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11046 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011047}
11048#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011049
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050/*
11051 * Show current status info in ruler and various other places
11052 * If always is FALSE, only show ruler if position has changed.
11053 */
11054 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011055showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056{
11057 if (!always && !redrawing())
11058 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011059#ifdef FEAT_INS_EXPAND
11060 if (pum_visible())
11061 {
11062 /* Don't redraw right now, do it later. */
11063 curwin->w_redr_status = TRUE;
11064 return;
11065 }
11066#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011067#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011068 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011069 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011070 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 else
11073#endif
11074#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011075 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076#endif
11077
11078#ifdef FEAT_TITLE
11079 if (need_maketitle
11080# ifdef FEAT_STL_OPT
11081 || (p_icon && (stl_syntax & STL_IN_ICON))
11082 || (p_title && (stl_syntax & STL_IN_TITLE))
11083# endif
11084 )
11085 maketitle();
11086#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011087 /* Redraw the tab pages line if needed. */
11088 if (redraw_tabline)
11089 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090}
11091
11092#ifdef FEAT_CMDL_INFO
11093 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011094win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011096#define RULER_BUF_LEN 70
11097 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 int row;
11099 int fillchar;
11100 int attr;
11101 int empty_line = FALSE;
11102 colnr_T virtcol;
11103 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011104 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 int this_ru_col;
11107 int off = 0;
11108 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
11110 /* If 'ruler' off or redrawing disabled, don't do anything */
11111 if (!p_ru)
11112 return;
11113
11114 /*
11115 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11116 * after deleting lines, before cursor.lnum is corrected.
11117 */
11118 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11119 return;
11120
11121#ifdef FEAT_INS_EXPAND
11122 /* Don't draw the ruler while doing insert-completion, it might overwrite
11123 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 if (edit_submode != NULL)
11126 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011127 // Don't draw the ruler when the popup menu is visible, it may overlap.
11128 // Except when the popup menu will be redrawn anyway.
11129 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011130 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131#endif
11132
11133#ifdef FEAT_STL_OPT
11134 if (*p_ruf)
11135 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011136 int save_called_emsg = called_emsg;
11137
11138 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011140 if (called_emsg)
11141 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011142 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011143 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 return;
11145 }
11146#endif
11147
11148 /*
11149 * Check if not in Insert mode and the line is empty (will show "0-1").
11150 */
11151 if (!(State & INSERT)
11152 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11153 empty_line = TRUE;
11154
11155 /*
11156 * Only draw the ruler when something changed.
11157 */
11158 validate_virtcol_win(wp);
11159 if ( redraw_cmdline
11160 || always
11161 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11162 || wp->w_cursor.col != wp->w_ru_cursor.col
11163 || wp->w_virtcol != wp->w_ru_virtcol
11164#ifdef FEAT_VIRTUALEDIT
11165 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11166#endif
11167 || wp->w_topline != wp->w_ru_topline
11168 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11169#ifdef FEAT_DIFF
11170 || wp->w_topfill != wp->w_ru_topfill
11171#endif
11172 || empty_line != wp->w_ru_empty)
11173 {
11174 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 if (wp->w_status_height)
11176 {
11177 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011178 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011179 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011180 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 }
11182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 {
11184 row = Rows - 1;
11185 fillchar = ' ';
11186 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 width = Columns;
11188 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 }
11190
11191 /* In list mode virtcol needs to be recomputed */
11192 virtcol = wp->w_virtcol;
11193 if (wp->w_p_list && lcs_tab1 == NUL)
11194 {
11195 wp->w_p_list = FALSE;
11196 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11197 wp->w_p_list = TRUE;
11198 }
11199
11200 /*
11201 * Some sprintfs return the length, some return a pointer.
11202 * To avoid portability problems we use strlen() here.
11203 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011204 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11206 ? 0L
11207 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011208 len = STRLEN(buffer);
11209 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11211 (int)virtcol + 1);
11212
11213 /*
11214 * Add a "50%" if there is room for it.
11215 * On the last line, don't print in the last column (scrolls the
11216 * screen up on some terminals).
11217 */
11218 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011219 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223 this_ru_col = ru_col - (Columns - width);
11224 if (this_ru_col < 0)
11225 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 /* Never use more than half the window/screen width, leave the other
11227 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011228 if (this_ru_col < (width + 1) / 2)
11229 this_ru_col = (width + 1) / 2;
11230 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011232 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011233 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 {
11235#ifdef FEAT_MBYTE
11236 if (has_mbyte)
11237 i += (*mb_char2bytes)(fillchar, buffer + i);
11238 else
11239#endif
11240 buffer[i++] = fillchar;
11241 ++o;
11242 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011243 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 }
11245 /* Truncate at window boundary. */
11246#ifdef FEAT_MBYTE
11247 if (has_mbyte)
11248 {
11249 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011250 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 {
11252 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011253 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 {
11255 buffer[i] = NUL;
11256 break;
11257 }
11258 }
11259 }
11260 else
11261#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011262 if (this_ru_col + (int)STRLEN(buffer) > width)
11263 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264
Bram Moolenaar4033c552017-09-16 20:54:51 +020011265 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 i = redraw_cmdline;
11267 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011268 this_ru_col + off + (int)STRLEN(buffer),
11269 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 fillchar, fillchar, attr);
11271 /* don't redraw the cmdline because of showing the ruler */
11272 redraw_cmdline = i;
11273 wp->w_ru_cursor = wp->w_cursor;
11274 wp->w_ru_virtcol = wp->w_virtcol;
11275 wp->w_ru_empty = empty_line;
11276 wp->w_ru_topline = wp->w_topline;
11277 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11278#ifdef FEAT_DIFF
11279 wp->w_ru_topfill = wp->w_topfill;
11280#endif
11281 }
11282}
11283#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011284
11285#if defined(FEAT_LINEBREAK) || defined(PROTO)
11286/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011287 * Return the width of the 'number' and 'relativenumber' column.
11288 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011289 * Otherwise it depends on 'numberwidth' and the line count.
11290 */
11291 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011292number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011293{
11294 int n;
11295 linenr_T lnum;
11296
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011297 if (wp->w_p_rnu && !wp->w_p_nu)
11298 /* cursor line shows "0" */
11299 lnum = wp->w_height;
11300 else
11301 /* cursor line shows absolute line number */
11302 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011303
Bram Moolenaar6b314672015-03-20 15:42:10 +010011304 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011305 return wp->w_nrwidth_width;
11306 wp->w_nrwidth_line_count = lnum;
11307
11308 n = 0;
11309 do
11310 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011311 lnum /= 10;
11312 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011313 } while (lnum > 0);
11314
11315 /* 'numberwidth' gives the minimal width plus one */
11316 if (n < wp->w_p_nuw - 1)
11317 n = wp->w_p_nuw - 1;
11318
11319 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011320 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011321 return n;
11322}
11323#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011324
11325/*
11326 * Return the current cursor column. This is the actual position on the
11327 * screen. First column is 0.
11328 */
11329 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011330screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011331{
11332 return screen_cur_col;
11333}
11334
11335/*
11336 * Return the current cursor row. This is the actual position on the screen.
11337 * First row is 0.
11338 */
11339 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011340screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011341{
11342 return screen_cur_row;
11343}