blob: 9b976ffaf1d8e2c8d9549904312d1b66bfb2d6b6 [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
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100267redraw_buf_line_later(buf_T *buf, linenr_T lnum)
268{
269 win_T *wp;
270
271 FOR_ALL_WINDOWS(wp)
272 if (wp->w_buffer == buf && lnum >= wp->w_topline
273 && lnum < wp->w_botline)
274 redrawWinline(wp, lnum);
275}
276
277 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200278redraw_buf_and_status_later(buf_T *buf, int type)
279{
280 win_T *wp;
281
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200282#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200283 if (wild_menu_showing != 0)
284 /* Don't redraw while the command line completion is displayed, it
285 * would disappear. */
286 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200287#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200288 FOR_ALL_WINDOWS(wp)
289 {
290 if (wp->w_buffer == buf)
291 {
292 redraw_win_later(wp, type);
293 wp->w_redr_status = TRUE;
294 }
295 }
296}
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200299 * Redraw as soon as possible. When the command line is not scrolled redraw
300 * right away and restore what was on the command line.
301 * Return a code indicating what happened.
302 */
303 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100304redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200305{
306 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200307 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 int r;
309 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline; /* copy from ScreenLines[] */
311 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200312#ifdef FEAT_MBYTE
313 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200314 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200315 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200316 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317#endif
318
319 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200320 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 return ret;
322
323 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200324 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200328 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200329 if (screenline == NULL || screenattr == NULL)
330 ret = 2;
331#ifdef FEAT_MBYTE
332 if (enc_utf8)
333 {
334 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200335 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (screenlineUC == NULL)
337 ret = 2;
338 for (i = 0; i < p_mco; ++i)
339 {
340 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200341 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200342 if (screenlineC[i] == NULL)
343 ret = 2;
344 }
345 }
346 if (enc_dbcs == DBCS_JPNU)
347 {
348 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200349 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
353#endif
354
355 if (ret != 2)
356 {
357 /* Save the text displayed in the command line area. */
358 for (r = 0; r < rows; ++r)
359 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 (size_t)cols * sizeof(schar_T));
363 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366#ifdef FEAT_MBYTE
367 if (enc_utf8)
368 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 mch_memmove(screenlineC[i] + r * cols,
374 ScreenLinesC[i] + LineOffset[cmdline_row + r],
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 }
377 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381#endif
382 }
383
384 update_screen(0);
385 ret = 3;
386
387 if (must_redraw == 0)
388 {
389 int off = (int)(current_ScreenLine - ScreenLines);
390
391 /* Restore the text displayed in the command line area. */
392 for (r = 0; r < rows; ++r)
393 {
394 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenline + r * cols,
396 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200398 screenattr + r * cols,
399 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200400#ifdef FEAT_MBYTE
401 if (enc_utf8)
402 {
403 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineUC + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 for (i = 0; i < p_mco; ++i)
407 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200408 screenlineC[i] + r * cols,
409 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200410 }
411 if (enc_dbcs == DBCS_JPNU)
412 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200413 screenline2 + r * cols,
414 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200416 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418 ret = 4;
419 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 }
421
422 vim_free(screenline);
423 vim_free(screenattr);
424#ifdef FEAT_MBYTE
425 if (enc_utf8)
426 {
427 vim_free(screenlineUC);
428 for (i = 0; i < p_mco; ++i)
429 vim_free(screenlineC[i]);
430 }
431 if (enc_dbcs == DBCS_JPNU)
432 vim_free(screenline2);
433#endif
434
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200435 /* Show the intro message when appropriate. */
436 maybe_intro_message();
437
438 setcursor();
439
Bram Moolenaar2951b772013-07-03 12:45:31 +0200440 return ret;
441}
442
443/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100444 * Invoked after an asynchronous callback is called.
445 * If an echo command was used the cursor needs to be put back where
446 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200447 * If "call_update_screen" is FALSE don't call update_screen() when at the
448 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100449 */
450 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200451redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100452{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200453 ++redrawing_for_callback;
454
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100455 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200456 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100457 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200458 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 // Don't redraw when in prompt_for_number().
460 if (cmdline_row > 0)
461 {
462 // Redrawing only works when the screen didn't scroll. Don't clear
463 // wildmenu entries.
464 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200465#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200466 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200467#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200468 && call_update_screen)
469 update_screen(0);
470
471 // Redraw in the same position, so that the user can continue
472 // editing the command.
473 redrawcmdline_ex(FALSE);
474 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200475 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200476 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100477 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200478 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200479 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100480 setcursor();
481 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100484 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200485 // Don't update the cursor when it is blinking and off to avoid
486 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100487 out_flush_cursor(FALSE, FALSE);
488 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100489#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100490 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200491
492 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100493}
494
495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 * Changed something in the current window, at buffer line "lnum", that
497 * requires that line and possibly other lines to be redrawn.
498 * Used when entering/leaving Insert mode with the cursor on a folded line.
499 * Used to remove the "$" from a change command.
500 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
501 * may become invalid and the whole window will have to be redrawn.
502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100504redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200505 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100506 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200508 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
509 wp->w_redraw_top = lnum;
510 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
511 wp->w_redraw_bot = lnum;
512 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513}
514
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515 void
516reset_updating_screen(int may_resize_shell UNUSED)
517{
518 updating_screen = FALSE;
519#ifdef FEAT_GUI
520 if (may_resize_shell)
521 gui_may_resize_shell();
522#endif
523#ifdef FEAT_TERMINAL
524 term_check_channel_closed_recently();
525#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200526
527#ifdef HAVE_DROP_FILE
528 // If handle_drop() was called while updating_screen was TRUE need to
529 // handle the drop now.
530 handle_any_postponed_drop();
531#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200532}
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 */
537 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100538update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 redraw_curbuf_later(type);
541 update_screen(type);
542}
543
544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 * Based on the current value of curwin->w_topline, transfer a screenfull
546 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200549 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200552 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 win_T *wp;
554 static int did_intro = FALSE;
555#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
556 int did_one;
557#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200558#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200559 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560 int gui_cursor_col;
561 int gui_cursor_row;
562#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200563 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000565 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200569 if (type == VALID_NO_UPDATE)
570 {
571 no_update = TRUE;
572 type = 0;
573 }
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (must_redraw)
576 {
577 if (type < must_redraw) /* use maximal type */
578 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000579
580 /* must_redraw is reset here, so that when we run into some weird
581 * reason to redraw while busy redrawing (e.g., asynchronous
582 * scrolling), or update_topline() in win_update() will cause a
583 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 must_redraw = 0;
585 }
586
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200587 /* May need to update w_lines[]. */
588 if (curwin->w_lines_valid == 0 && type < NOT_VALID
589#ifdef FEAT_TERMINAL
590 && !term_do_update_window(curwin)
591#endif
592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 type = NOT_VALID;
594
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000595 /* Postpone the redrawing when it's not needed and when being called
596 * recursively. */
597 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
599 redraw_later(type); /* remember type for next time */
600 must_redraw = type;
601 if (type > INVERTED_ALL)
602 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200603 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605
606 updating_screen = TRUE;
607#ifdef FEAT_SYN_HL
608 ++display_tick; /* let syntax code know we're in a next round of
609 * display updating */
610#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200611 if (no_update)
612 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614 /*
615 * if the screen was scrolled up when displaying a message, scroll it down
616 */
617 if (msg_scrolled)
618 {
619 clear_cmdline = TRUE;
620 if (msg_scrolled > Rows - 5) /* clearing is faster */
621 type = CLEAR;
622 else if (type != CLEAR)
623 {
624 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200625 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
626 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 type = CLEAR;
628 FOR_ALL_WINDOWS(wp)
629 {
630 if (W_WINROW(wp) < msg_scrolled)
631 {
632 if (W_WINROW(wp) + wp->w_height > msg_scrolled
633 && wp->w_redr_type < REDRAW_TOP
634 && wp->w_lines_valid > 0
635 && wp->w_topline == wp->w_lines[0].wl_lnum)
636 {
637 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
638 wp->w_redr_type = REDRAW_TOP;
639 }
640 else
641 {
642 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200643 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
644 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
647 }
648 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200649 if (!no_update)
650 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000651 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 msg_scrolled = 0;
654 need_wait_return = FALSE;
655 }
656
657 /* reset cmdline_row now (may have been changed temporarily) */
658 compute_cmdrow();
659
660 /* Check for changed highlighting */
661 if (need_highlight_changed)
662 highlight_changed();
663
664 if (type == CLEAR) /* first clear screen */
665 {
666 screenclear(); /* will reset clear_cmdline */
667 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200668 /* must_redraw may be set indirectly, avoid another redraw later */
669 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671
672 if (clear_cmdline) /* going to clear cmdline (done below) */
673 check_for_delay(FALSE);
674
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000675#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200676 /* Force redraw when width of 'number' or 'relativenumber' column
677 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000678 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200679 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
680 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000681 curwin->w_redr_type = NOT_VALID;
682#endif
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 /*
685 * Only start redrawing if there is really something to do.
686 */
687 if (type == INVERTED)
688 update_curswant();
689 if (curwin->w_redr_type < type
690 && !((type == VALID
691 && curwin->w_lines[0].wl_valid
692#ifdef FEAT_DIFF
693 && curwin->w_topfill == curwin->w_old_topfill
694 && curwin->w_botfill == curwin->w_old_botfill
695#endif
696 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000698 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
700 && curwin->w_old_visual_mode == VIsual_mode
701 && (curwin->w_valid & VALID_VIRTCOL)
702 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ))
704 curwin->w_redr_type = type;
705
Bram Moolenaar5a305422006-04-28 22:38:25 +0000706 /* Redraw the tab pages line if needed. */
707 if (redraw_tabline || type >= NOT_VALID)
708 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_SYN_HL
711 /*
712 * Correct stored syntax highlighting info for changes in each displayed
713 * buffer. Each buffer must only be done once.
714 */
715 FOR_ALL_WINDOWS(wp)
716 {
717 if (wp->w_buffer->b_mod_set)
718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 win_T *wwp;
720
721 /* Check if we already did this buffer. */
722 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
723 if (wwp->w_buffer == wp->w_buffer)
724 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200725 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 syn_stack_apply_changes(wp->w_buffer);
727 }
728 }
729#endif
730
731 /*
732 * Go from top to bottom through the windows, redrawing the ones that need
733 * it.
734 */
735#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
736 did_one = FALSE;
737#endif
738#ifdef FEAT_SEARCH_EXTRA
739 search_hl.rm.regprog = NULL;
740#endif
741 FOR_ALL_WINDOWS(wp)
742 {
743 if (wp->w_redr_type != 0)
744 {
745 cursor_off();
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 if (!did_one)
748 {
749 did_one = TRUE;
750# ifdef FEAT_SEARCH_EXTRA
751 start_search_hl();
752# endif
753# ifdef FEAT_CLIPBOARD
754 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200755 if (clip_star.available && clip_isautosel_star())
756 clip_update_selection(&clip_star);
757 if (clip_plus.available && clip_isautosel_plus())
758 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759# endif
760#ifdef FEAT_GUI
761 /* Remove the cursor before starting to do anything, because
762 * scrolling may make it difficult to redraw the text under
763 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200764 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200765 {
766 gui_cursor_col = gui.cursor_col;
767 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200769 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772 }
773#endif
774 win_update(wp);
775 }
776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 /* redraw status line after the window to minimize cursor movement */
778 if (wp->w_redr_status)
779 {
780 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200781 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784#if defined(FEAT_SEARCH_EXTRA)
785 end_search_hl();
786#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100787#ifdef FEAT_INS_EXPAND
788 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200789 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 /* Reset b_mod_set flags. Going through all windows is probably faster
793 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200794 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200797 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799 /* Clear or redraw the command line. Done last, because scrolling may
800 * mess up the command line. */
801 if (clear_cmdline || redraw_cmdline)
802 showmode();
803
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200804 if (no_update)
805 --no_win_do_lines_ins;
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200808 if (!did_intro)
809 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 did_intro = TRUE;
811
812#ifdef FEAT_GUI
813 /* Redraw the cursor and update the scrollbars when all screen updating is
814 * done. */
815 if (gui.in_use)
816 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200817 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200818 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100819 mch_disable_flush();
820 out_flush(); /* required before updating the cursor */
821 mch_enable_flush();
822
Bram Moolenaar144445d2016-07-08 21:41:54 +0200823 /* Put the GUI position where the cursor was, gui_update_cursor()
824 * uses that. */
825 gui.col = gui_cursor_col;
826 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200827# ifdef FEAT_MBYTE
828 gui.col = mb_fix_col(gui.col, gui.row);
829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100831 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200832 screen_cur_col = gui.col;
833 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 else
836 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 gui_update_scrollbars(FALSE);
838 }
839#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200840 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100843#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
844/*
845 * Prepare for updating one or more windows.
846 * Caller must check for "updating_screen" already set to avoid recursiveness.
847 */
848 static void
849update_prepare(void)
850{
851 cursor_off();
852 updating_screen = TRUE;
853#ifdef FEAT_GUI
854 /* Remove the cursor before starting to do anything, because scrolling may
855 * make it difficult to redraw the text under it. */
856 if (gui.in_use)
857 gui_undraw_cursor();
858#endif
859#ifdef FEAT_SEARCH_EXTRA
860 start_search_hl();
861#endif
862}
863
864/*
865 * Finish updating one or more windows.
866 */
867 static void
868update_finish(void)
869{
870 if (redraw_cmdline)
871 showmode();
872
873# ifdef FEAT_SEARCH_EXTRA
874 end_search_hl();
875# endif
876
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200877 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100878
879# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100880 /* Redraw the cursor and update the scrollbars when all screen updating is
881 * done. */
882 if (gui.in_use)
883 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100884 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 gui_update_scrollbars(FALSE);
886 }
887# endif
888}
889#endif
890
Bram Moolenaar860cae12010-06-05 23:22:07 +0200891#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200892/*
893 * Return TRUE if the cursor line in window "wp" may be concealed, according
894 * to the 'concealcursor' option.
895 */
896 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100897conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200898{
899 int c;
900
901 if (*wp->w_p_cocu == NUL)
902 return FALSE;
903 if (get_real_state() & VISUAL)
904 c = 'v';
905 else if (State & INSERT)
906 c = 'i';
907 else if (State & NORMAL)
908 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200909 else if (State & CMDLINE)
910 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200911 else
912 return FALSE;
913 return vim_strchr(wp->w_p_cocu, c) != NULL;
914}
915
916/*
917 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
918 */
919 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200920conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200921{
922 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
923 {
924 need_cursor_line_redraw = TRUE;
925 /* Need to recompute cursor column, e.g., when starting Visual mode
926 * without concealing. */
927 curs_columns(TRUE);
928 }
929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931
932#if defined(FEAT_SIGNS) || defined(PROTO)
933 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100934update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
936 win_T *wp;
937 int doit = FALSE;
938
939# ifdef FEAT_FOLDING
940 win_foldinfo.fi_level = 0;
941# endif
942
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100943 // update/delete a specific sign
944 redraw_buf_line_later(buf, lnum);
945
946 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (wp->w_redr_type != 0)
949 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100951 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200952 * happening (recursive call), messages on the screen or still starting up.
953 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100954 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200955 || State == ASKMORE || State == HITRETURN
956 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957#ifdef FEAT_GUI
958 || gui.starting
959#endif
960 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 return;
962
963 /* update all windows that need updating */
964 update_prepare();
965
Bram Moolenaar29323592016-07-24 22:04:11 +0200966 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
968 if (wp->w_redr_type != 0)
969 win_update(wp);
970 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200971 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 update_finish();
975}
976#endif
977
978
979#if defined(FEAT_GUI) || defined(PROTO)
980/*
981 * Update a single window, its status line and maybe the command line msg.
982 * Used for the GUI scrollbar.
983 */
984 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100985updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000987 /* return if already busy updating */
988 if (updating_screen)
989 return;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 update_prepare();
992
993#ifdef FEAT_CLIPBOARD
994 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200995 if (clip_star.available && clip_isautosel_star())
996 clip_update_selection(&clip_star);
997 if (clip_plus.available && clip_isautosel_plus())
998 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001002
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001003 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001004 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001005 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 if (wp->w_redr_status
1008# ifdef FEAT_CMDL_INFO
1009 || p_ru
1010# endif
1011# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001012 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013# endif
1014 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001015 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016
1017 update_finish();
1018}
1019#endif
1020
1021/*
1022 * Update a single window.
1023 *
1024 * This may cause the windows below it also to be redrawn (when clearing the
1025 * screen or scrolling lines).
1026 *
1027 * How the window is redrawn depends on wp->w_redr_type. Each type also
1028 * implies the one below it.
1029 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001030 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1032 * INVERTED redraw the changed part of the Visual area
1033 * INVERTED_ALL redraw the whole Visual area
1034 * VALID 1. scroll up/down to adjust for a changed w_topline
1035 * 2. update lines at the top when scrolled down
1036 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001037 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 * b_mod_top and b_mod_bot.
1039 * - if wp->w_redraw_top non-zero, redraw lines between
1040 * wp->w_redraw_top and wp->w_redr_bot.
1041 * - continue redrawing when syntax status is invalid.
1042 * 4. if scrolled up, update lines at the bottom.
1043 * This results in three areas that may need updating:
1044 * top: from first row to top_end (when scrolled down)
1045 * mid: from mid_start to mid_end (update inversion or changed text)
1046 * bot: from bot_start to last row (when scrolled up)
1047 */
1048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001049win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
1051 buf_T *buf = wp->w_buffer;
1052 int type;
1053 int top_end = 0; /* Below last row of the top area that needs
1054 updating. 0 when no top area updating. */
1055 int mid_start = 999;/* first row of the mid area that needs
1056 updating. 999 when no mid area updating. */
1057 int mid_end = 0; /* Below last row of the mid area that needs
1058 updating. 0 when no mid area updating. */
1059 int bot_start = 999;/* first row of the bot area that needs
1060 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 int scrolled_down = FALSE; /* TRUE when scrolled down when
1062 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001064 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 int top_to_mod = FALSE; /* redraw above mod_top */
1066#endif
1067
1068 int row; /* current window row to display */
1069 linenr_T lnum; /* current buffer lnum to display */
1070 int idx; /* current index in w_lines[] */
1071 int srow; /* starting row of the current line */
1072
1073 int eof = FALSE; /* if TRUE, we hit the end of the file */
1074 int didline = FALSE; /* if TRUE, we finished the last line */
1075 int i;
1076 long j;
1077 static int recursive = FALSE; /* being called recursively */
1078 int old_botline = wp->w_botline;
1079#ifdef FEAT_FOLDING
1080 long fold_count;
1081#endif
1082#ifdef FEAT_SYN_HL
1083 /* remember what happened to the previous line, to know if
1084 * check_visual_highlight() can be used */
1085#define DID_NONE 1 /* didn't update a line */
1086#define DID_LINE 2 /* updated a normal line */
1087#define DID_FOLD 3 /* updated a folded line */
1088 int did_update = DID_NONE;
1089 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1090#endif
1091 linenr_T mod_top = 0;
1092 linenr_T mod_bot = 0;
1093#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1094 int save_got_int;
1095#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001096#ifdef SYN_TIME_LIMIT
1097 proftime_T syntax_tm;
1098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
1100 type = wp->w_redr_type;
1101
1102 if (type == NOT_VALID)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 wp->w_lines_valid = 0;
1106 }
1107
1108 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001109 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 wp->w_redr_type = 0;
1112 return;
1113 }
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 /* Window is zero-width: Only need to draw the separator. */
1116 if (wp->w_width == 0)
1117 {
1118 /* draw the vertical separator right of this window */
1119 draw_vsep_win(wp, 0);
1120 wp->w_redr_type = 0;
1121 return;
1122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001124#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001125 // If this window contains a terminal, redraw works completely differently.
1126 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001127 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001128 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001129# ifdef FEAT_MENU
1130 /* Draw the window toolbar, if there is one. */
1131 if (winbar_height(wp) > 0)
1132 redraw_win_toolbar(wp);
1133# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001134 wp->w_redr_type = 0;
1135 return;
1136 }
1137#endif
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001140 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#endif
1142
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001143#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001144 /* Force redraw when width of 'number' or 'relativenumber' column
1145 * changes. */
1146 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001147 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001148 {
1149 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001150 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001151 }
1152 else
1153#endif
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1156 {
1157 /*
1158 * When there are both inserted/deleted lines and specific lines to be
1159 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1160 * everything (only happens when redrawing is off for while).
1161 */
1162 type = NOT_VALID;
1163 }
1164 else
1165 {
1166 /*
1167 * Set mod_top to the first line that needs displaying because of
1168 * changes. Set mod_bot to the first line after the changes.
1169 */
1170 mod_top = wp->w_redraw_top;
1171 if (wp->w_redraw_bot != 0)
1172 mod_bot = wp->w_redraw_bot + 1;
1173 else
1174 mod_bot = 0;
1175 wp->w_redraw_top = 0; /* reset for next time */
1176 wp->w_redraw_bot = 0;
1177 if (buf->b_mod_set)
1178 {
1179 if (mod_top == 0 || mod_top > buf->b_mod_top)
1180 {
1181 mod_top = buf->b_mod_top;
1182#ifdef FEAT_SYN_HL
1183 /* Need to redraw lines above the change that may be included
1184 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001185 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001187 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (mod_top < 1)
1189 mod_top = 1;
1190 }
1191#endif
1192 }
1193 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1194 mod_bot = buf->b_mod_bot;
1195
1196#ifdef FEAT_SEARCH_EXTRA
1197 /* When 'hlsearch' is on and using a multi-line search pattern, a
1198 * change in one line may make the Search highlighting in a
1199 * previous line invalid. Simple solution: redraw all visible
1200 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001201 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001203 if (search_hl.rm.regprog != NULL
1204 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001206 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001207 {
1208 cur = wp->w_match_head;
1209 while (cur != NULL)
1210 {
1211 if (cur->match.regprog != NULL
1212 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001213 {
1214 top_to_mod = TRUE;
1215 break;
1216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001217 cur = cur->next;
1218 }
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#endif
1221 }
1222#ifdef FEAT_FOLDING
1223 if (mod_top != 0 && hasAnyFolding(wp))
1224 {
1225 linenr_T lnumt, lnumb;
1226
1227 /*
1228 * A change in a line can cause lines above it to become folded or
1229 * unfolded. Find the top most buffer line that may be affected.
1230 * If the line was previously folded and displayed, get the first
1231 * line of that fold. If the line is folded now, get the first
1232 * folded line. Use the minimum of these two.
1233 */
1234
1235 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1236 * the line below it. If there is no valid entry, use w_topline.
1237 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1238 * to this line. If there is no valid entry, use MAXLNUM. */
1239 lnumt = wp->w_topline;
1240 lnumb = MAXLNUM;
1241 for (i = 0; i < wp->w_lines_valid; ++i)
1242 if (wp->w_lines[i].wl_valid)
1243 {
1244 if (wp->w_lines[i].wl_lastlnum < mod_top)
1245 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1246 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1247 {
1248 lnumb = wp->w_lines[i].wl_lnum;
1249 /* When there is a fold column it might need updating
1250 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001251 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 ++lnumb;
1253 }
1254 }
1255
1256 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1257 if (mod_top > lnumt)
1258 mod_top = lnumt;
1259
1260 /* Now do the same for the bottom line (one above mod_bot). */
1261 --mod_bot;
1262 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1263 ++mod_bot;
1264 if (mod_bot < lnumb)
1265 mod_bot = lnumb;
1266 }
1267#endif
1268
1269 /* When a change starts above w_topline and the end is below
1270 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001271 * If the end of the change is above w_topline: do like no change was
1272 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (mod_top != 0 && mod_top < wp->w_topline)
1274 {
1275 if (mod_bot > wp->w_topline)
1276 mod_top = wp->w_topline;
1277#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001278 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 top_end = 1;
1280#endif
1281 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001282
1283 /* When line numbers are displayed need to redraw all lines below
1284 * inserted/deleted lines. */
1285 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1286 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288
1289 /*
1290 * When only displaying the lines at the top, set top_end. Used when
1291 * window has scrolled down for msg_scrolled.
1292 */
1293 if (type == REDRAW_TOP)
1294 {
1295 j = 0;
1296 for (i = 0; i < wp->w_lines_valid; ++i)
1297 {
1298 j += wp->w_lines[i].wl_size;
1299 if (j >= wp->w_upd_rows)
1300 {
1301 top_end = j;
1302 break;
1303 }
1304 }
1305 if (top_end == 0)
1306 /* not found (cannot happen?): redraw everything */
1307 type = NOT_VALID;
1308 else
1309 /* top area defined, the rest is VALID */
1310 type = VALID;
1311 }
1312
Bram Moolenaar367329b2007-08-30 11:53:22 +00001313 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001314 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1315 * non-zero and thus not FALSE) will indicate that screenclear() was not
1316 * called. */
1317 if (screen_cleared)
1318 screen_cleared = MAYBE;
1319
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 /*
1321 * If there are no changes on the screen that require a complete redraw,
1322 * handle three cases:
1323 * 1: we are off the top of the screen by a few lines: scroll down
1324 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1325 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1326 * w_lines[] that needs updating.
1327 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001328 if ((type == VALID || type == SOME_VALID
1329 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef FEAT_DIFF
1331 && !wp->w_botfill && !wp->w_old_botfill
1332#endif
1333 )
1334 {
1335 if (mod_top != 0 && wp->w_topline == mod_top)
1336 {
1337 /*
1338 * w_topline is the first changed line, the scrolling will be done
1339 * further down.
1340 */
1341 }
1342 else if (wp->w_lines[0].wl_valid
1343 && (wp->w_topline < wp->w_lines[0].wl_lnum
1344#ifdef FEAT_DIFF
1345 || (wp->w_topline == wp->w_lines[0].wl_lnum
1346 && wp->w_topfill > wp->w_old_topfill)
1347#endif
1348 ))
1349 {
1350 /*
1351 * New topline is above old topline: May scroll down.
1352 */
1353#ifdef FEAT_FOLDING
1354 if (hasAnyFolding(wp))
1355 {
1356 linenr_T ln;
1357
1358 /* count the number of lines we are off, counting a sequence
1359 * of folded lines as one */
1360 j = 0;
1361 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1362 {
1363 ++j;
1364 if (j >= wp->w_height - 2)
1365 break;
1366 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1367 }
1368 }
1369 else
1370#endif
1371 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1372 if (j < wp->w_height - 2) /* not too far off */
1373 {
1374 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1375#ifdef FEAT_DIFF
1376 /* insert extra lines for previously invisible filler lines */
1377 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1378 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1379 - wp->w_old_topfill;
1380#endif
1381 if (i < wp->w_height - 2) /* less than a screen off */
1382 {
1383 /*
1384 * Try to insert the correct number of lines.
1385 * If not the last window, delete the lines at the bottom.
1386 * win_ins_lines may fail when the terminal can't do it.
1387 */
1388 if (i > 0)
1389 check_for_delay(FALSE);
1390 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1391 {
1392 if (wp->w_lines_valid != 0)
1393 {
1394 /* Need to update rows that are new, stop at the
1395 * first one that scrolled down. */
1396 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
1399 /* Move the entries that were scrolled, disable
1400 * the entries for the lines to be redrawn. */
1401 if ((wp->w_lines_valid += j) > wp->w_height)
1402 wp->w_lines_valid = wp->w_height;
1403 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1404 wp->w_lines[idx] = wp->w_lines[idx - j];
1405 while (idx >= 0)
1406 wp->w_lines[idx--].wl_valid = FALSE;
1407 }
1408 }
1409 else
1410 mid_start = 0; /* redraw all lines */
1411 }
1412 else
1413 mid_start = 0; /* redraw all lines */
1414 }
1415 else
1416 mid_start = 0; /* redraw all lines */
1417 }
1418 else
1419 {
1420 /*
1421 * New topline is at or below old topline: May scroll up.
1422 * When topline didn't change, find first entry in w_lines[] that
1423 * needs updating.
1424 */
1425
1426 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1427 j = -1;
1428 row = 0;
1429 for (i = 0; i < wp->w_lines_valid; i++)
1430 {
1431 if (wp->w_lines[i].wl_valid
1432 && wp->w_lines[i].wl_lnum == wp->w_topline)
1433 {
1434 j = i;
1435 break;
1436 }
1437 row += wp->w_lines[i].wl_size;
1438 }
1439 if (j == -1)
1440 {
1441 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1442 * lines */
1443 mid_start = 0;
1444 }
1445 else
1446 {
1447 /*
1448 * Try to delete the correct number of lines.
1449 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1450 */
1451#ifdef FEAT_DIFF
1452 /* If the topline didn't change, delete old filler lines,
1453 * otherwise delete filler lines of the new topline... */
1454 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1455 row += wp->w_old_topfill;
1456 else
1457 row += diff_check_fill(wp, wp->w_topline);
1458 /* ... but don't delete new filler lines. */
1459 row -= wp->w_topfill;
1460#endif
1461 if (row > 0)
1462 {
1463 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001464 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1465 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 bot_start = wp->w_height - row;
1467 else
1468 mid_start = 0; /* redraw all lines */
1469 }
1470 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1471 {
1472 /*
1473 * Skip the lines (below the deleted lines) that are still
1474 * valid and don't need redrawing. Copy their info
1475 * upwards, to compensate for the deleted lines. Set
1476 * bot_start to the first row that needs redrawing.
1477 */
1478 bot_start = 0;
1479 idx = 0;
1480 for (;;)
1481 {
1482 wp->w_lines[idx] = wp->w_lines[j];
1483 /* stop at line that didn't fit, unless it is still
1484 * valid (no lines deleted) */
1485 if (row > 0 && bot_start + row
1486 + (int)wp->w_lines[j].wl_size > wp->w_height)
1487 {
1488 wp->w_lines_valid = idx + 1;
1489 break;
1490 }
1491 bot_start += wp->w_lines[idx++].wl_size;
1492
1493 /* stop at the last valid entry in w_lines[].wl_size */
1494 if (++j >= wp->w_lines_valid)
1495 {
1496 wp->w_lines_valid = idx;
1497 break;
1498 }
1499 }
1500#ifdef FEAT_DIFF
1501 /* Correct the first entry for filler lines at the top
1502 * when it won't get updated below. */
1503 if (wp->w_p_diff && bot_start > 0)
1504 wp->w_lines[0].wl_size =
1505 plines_win_nofill(wp, wp->w_topline, TRUE)
1506 + wp->w_topfill;
1507#endif
1508 }
1509 }
1510 }
1511
1512 /* When starting redraw in the first line, redraw all lines. When
1513 * there is only one window it's probably faster to clear the screen
1514 * first. */
1515 if (mid_start == 0)
1516 {
1517 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001518 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001519 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001520 /* Clear the screen when it was not done by win_del_lines() or
1521 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1522 * then. */
1523 if (screen_cleared != TRUE)
1524 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001525 /* The screen was cleared, redraw the tab pages line. */
1526 if (redraw_tabline)
1527 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001530
1531 /* When win_del_lines() or win_ins_lines() caused the screen to be
1532 * cleared (only happens for the first window) or when screenclear()
1533 * was called directly above, "must_redraw" will have been set to
1534 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1535 if (screen_cleared == TRUE)
1536 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 else
1539 {
1540 /* Not VALID or INVERTED: redraw all lines. */
1541 mid_start = 0;
1542 mid_end = wp->w_height;
1543 }
1544
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001545 if (type == SOME_VALID)
1546 {
1547 /* SOME_VALID: redraw all lines. */
1548 mid_start = 0;
1549 mid_end = wp->w_height;
1550 type = NOT_VALID;
1551 }
1552
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 /* check if we are updating or removing the inverted part */
1554 if ((VIsual_active && buf == curwin->w_buffer)
1555 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1556 {
1557 linenr_T from, to;
1558
1559 if (VIsual_active)
1560 {
1561 if (VIsual_active
1562 && (VIsual_mode != wp->w_old_visual_mode
1563 || type == INVERTED_ALL))
1564 {
1565 /*
1566 * If the type of Visual selection changed, redraw the whole
1567 * selection. Also when the ownership of the X selection is
1568 * gained or lost.
1569 */
1570 if (curwin->w_cursor.lnum < VIsual.lnum)
1571 {
1572 from = curwin->w_cursor.lnum;
1573 to = VIsual.lnum;
1574 }
1575 else
1576 {
1577 from = VIsual.lnum;
1578 to = curwin->w_cursor.lnum;
1579 }
1580 /* redraw more when the cursor moved as well */
1581 if (wp->w_old_cursor_lnum < from)
1582 from = wp->w_old_cursor_lnum;
1583 if (wp->w_old_cursor_lnum > to)
1584 to = wp->w_old_cursor_lnum;
1585 if (wp->w_old_visual_lnum < from)
1586 from = wp->w_old_visual_lnum;
1587 if (wp->w_old_visual_lnum > to)
1588 to = wp->w_old_visual_lnum;
1589 }
1590 else
1591 {
1592 /*
1593 * Find the line numbers that need to be updated: The lines
1594 * between the old cursor position and the current cursor
1595 * position. Also check if the Visual position changed.
1596 */
1597 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1598 {
1599 from = curwin->w_cursor.lnum;
1600 to = wp->w_old_cursor_lnum;
1601 }
1602 else
1603 {
1604 from = wp->w_old_cursor_lnum;
1605 to = curwin->w_cursor.lnum;
1606 if (from == 0) /* Visual mode just started */
1607 from = to;
1608 }
1609
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001610 if (VIsual.lnum != wp->w_old_visual_lnum
1611 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 {
1613 if (wp->w_old_visual_lnum < from
1614 && wp->w_old_visual_lnum != 0)
1615 from = wp->w_old_visual_lnum;
1616 if (wp->w_old_visual_lnum > to)
1617 to = wp->w_old_visual_lnum;
1618 if (VIsual.lnum < from)
1619 from = VIsual.lnum;
1620 if (VIsual.lnum > to)
1621 to = VIsual.lnum;
1622 }
1623 }
1624
1625 /*
1626 * If in block mode and changed column or curwin->w_curswant:
1627 * update all lines.
1628 * First compute the actual start and end column.
1629 */
1630 if (VIsual_mode == Ctrl_V)
1631 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001632 colnr_T fromc, toc;
1633#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1634 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar404406a2014-10-09 13:24:43 +02001636 if (curwin->w_p_lbr)
1637 ve_flags = VE_ALL;
1638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001640#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1641 ve_flags = save_ve_flags;
1642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 ++toc;
1644 if (curwin->w_curswant == MAXCOL)
1645 toc = MAXCOL;
1646
1647 if (fromc != wp->w_old_cursor_fcol
1648 || toc != wp->w_old_cursor_lcol)
1649 {
1650 if (from > VIsual.lnum)
1651 from = VIsual.lnum;
1652 if (to < VIsual.lnum)
1653 to = VIsual.lnum;
1654 }
1655 wp->w_old_cursor_fcol = fromc;
1656 wp->w_old_cursor_lcol = toc;
1657 }
1658 }
1659 else
1660 {
1661 /* Use the line numbers of the old Visual area. */
1662 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1663 {
1664 from = wp->w_old_cursor_lnum;
1665 to = wp->w_old_visual_lnum;
1666 }
1667 else
1668 {
1669 from = wp->w_old_visual_lnum;
1670 to = wp->w_old_cursor_lnum;
1671 }
1672 }
1673
1674 /*
1675 * There is no need to update lines above the top of the window.
1676 */
1677 if (from < wp->w_topline)
1678 from = wp->w_topline;
1679
1680 /*
1681 * If we know the value of w_botline, use it to restrict the update to
1682 * the lines that are visible in the window.
1683 */
1684 if (wp->w_valid & VALID_BOTLINE)
1685 {
1686 if (from >= wp->w_botline)
1687 from = wp->w_botline - 1;
1688 if (to >= wp->w_botline)
1689 to = wp->w_botline - 1;
1690 }
1691
1692 /*
1693 * Find the minimal part to be updated.
1694 * Watch out for scrolling that made entries in w_lines[] invalid.
1695 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1696 * top_end; need to redraw from top_end to the "to" line.
1697 * A middle mouse click with a Visual selection may change the text
1698 * above the Visual area and reset wl_valid, do count these for
1699 * mid_end (in srow).
1700 */
1701 if (mid_start > 0)
1702 {
1703 lnum = wp->w_topline;
1704 idx = 0;
1705 srow = 0;
1706 if (scrolled_down)
1707 mid_start = top_end;
1708 else
1709 mid_start = 0;
1710 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1711 {
1712 if (wp->w_lines[idx].wl_valid)
1713 mid_start += wp->w_lines[idx].wl_size;
1714 else if (!scrolled_down)
1715 srow += wp->w_lines[idx].wl_size;
1716 ++idx;
1717# ifdef FEAT_FOLDING
1718 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1719 lnum = wp->w_lines[idx].wl_lnum;
1720 else
1721# endif
1722 ++lnum;
1723 }
1724 srow += mid_start;
1725 mid_end = wp->w_height;
1726 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1727 {
1728 if (wp->w_lines[idx].wl_valid
1729 && wp->w_lines[idx].wl_lnum >= to + 1)
1730 {
1731 /* Only update until first row of this line */
1732 mid_end = srow;
1733 break;
1734 }
1735 srow += wp->w_lines[idx].wl_size;
1736 }
1737 }
1738 }
1739
1740 if (VIsual_active && buf == curwin->w_buffer)
1741 {
1742 wp->w_old_visual_mode = VIsual_mode;
1743 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1744 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001745 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 wp->w_old_curswant = curwin->w_curswant;
1747 }
1748 else
1749 {
1750 wp->w_old_visual_mode = 0;
1751 wp->w_old_cursor_lnum = 0;
1752 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001753 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755
1756#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1757 /* reset got_int, otherwise regexp won't work */
1758 save_got_int = got_int;
1759 got_int = 0;
1760#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001761#ifdef SYN_TIME_LIMIT
1762 /* Set the time limit to 'redrawtime'. */
1763 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001764 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_FOLDING
1767 win_foldinfo.fi_level = 0;
1768#endif
1769
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001770#ifdef FEAT_MENU
1771 /*
1772 * Draw the window toolbar, if there is one.
1773 * TODO: only when needed.
1774 */
1775 if (winbar_height(wp) > 0)
1776 redraw_win_toolbar(wp);
1777#endif
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /*
1780 * Update all the window rows.
1781 */
1782 idx = 0; /* first entry in w_lines[].wl_size */
1783 row = 0;
1784 srow = 0;
1785 lnum = wp->w_topline; /* first line shown in window */
1786 for (;;)
1787 {
1788 /* stop updating when reached the end of the window (check for _past_
1789 * the end of the window is at the end of the loop) */
1790 if (row == wp->w_height)
1791 {
1792 didline = TRUE;
1793 break;
1794 }
1795
1796 /* stop updating when hit the end of the file */
1797 if (lnum > buf->b_ml.ml_line_count)
1798 {
1799 eof = TRUE;
1800 break;
1801 }
1802
1803 /* Remember the starting row of the line that is going to be dealt
1804 * with. It is used further down when the line doesn't fit. */
1805 srow = row;
1806
1807 /*
1808 * Update a line when it is in an area that needs updating, when it
1809 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001810 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 * win_del_lines(), check if the current line includes it.
1812 * When syntax folding is being used, the saved syntax states will
1813 * already have been updated, we can't see where the syntax state is
1814 * the same again, just update until the end of the window.
1815 */
1816 if (row < top_end
1817 || (row >= mid_start && row < mid_end)
1818#ifdef FEAT_SEARCH_EXTRA
1819 || top_to_mod
1820#endif
1821 || idx >= wp->w_lines_valid
1822 || (row + wp->w_lines[idx].wl_size > bot_start)
1823 || (mod_top != 0
1824 && (lnum == mod_top
1825 || (lnum >= mod_top
1826 && (lnum < mod_bot
1827#ifdef FEAT_SYN_HL
1828 || did_update == DID_FOLD
1829 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001830 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 && (
1832# ifdef FEAT_FOLDING
1833 (foldmethodIsSyntax(wp)
1834 && hasAnyFolding(wp)) ||
1835# endif
1836 syntax_check_changed(lnum)))
1837#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001838#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001839 /* match in fixed position might need redraw
1840 * if lines were inserted or deleted */
1841 || (wp->w_match_head != NULL
1842 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 )))))
1845 {
1846#ifdef FEAT_SEARCH_EXTRA
1847 if (lnum == mod_top)
1848 top_to_mod = FALSE;
1849#endif
1850
1851 /*
1852 * When at start of changed lines: May scroll following lines
1853 * up or down to minimize redrawing.
1854 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001855 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 */
1857 if (lnum == mod_top
1858 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001859 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
1861 int old_rows = 0;
1862 int new_rows = 0;
1863 int xtra_rows;
1864 linenr_T l;
1865
1866 /* Count the old number of window rows, using w_lines[], which
1867 * should still contain the sizes for the lines as they are
1868 * currently displayed. */
1869 for (i = idx; i < wp->w_lines_valid; ++i)
1870 {
1871 /* Only valid lines have a meaningful wl_lnum. Invalid
1872 * lines are part of the changed area. */
1873 if (wp->w_lines[i].wl_valid
1874 && wp->w_lines[i].wl_lnum == mod_bot)
1875 break;
1876 old_rows += wp->w_lines[i].wl_size;
1877#ifdef FEAT_FOLDING
1878 if (wp->w_lines[i].wl_valid
1879 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1880 {
1881 /* Must have found the last valid entry above mod_bot.
1882 * Add following invalid entries. */
1883 ++i;
1884 while (i < wp->w_lines_valid
1885 && !wp->w_lines[i].wl_valid)
1886 old_rows += wp->w_lines[i++].wl_size;
1887 break;
1888 }
1889#endif
1890 }
1891
1892 if (i >= wp->w_lines_valid)
1893 {
1894 /* We can't find a valid line below the changed lines,
1895 * need to redraw until the end of the window.
1896 * Inserting/deleting lines has no use. */
1897 bot_start = 0;
1898 }
1899 else
1900 {
1901 /* Able to count old number of rows: Count new window
1902 * rows, and may insert/delete lines */
1903 j = idx;
1904 for (l = lnum; l < mod_bot; ++l)
1905 {
1906#ifdef FEAT_FOLDING
1907 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1908 ++new_rows;
1909 else
1910#endif
1911#ifdef FEAT_DIFF
1912 if (l == wp->w_topline)
1913 new_rows += plines_win_nofill(wp, l, TRUE)
1914 + wp->w_topfill;
1915 else
1916#endif
1917 new_rows += plines_win(wp, l, TRUE);
1918 ++j;
1919 if (new_rows > wp->w_height - row - 2)
1920 {
1921 /* it's getting too much, must redraw the rest */
1922 new_rows = 9999;
1923 break;
1924 }
1925 }
1926 xtra_rows = new_rows - old_rows;
1927 if (xtra_rows < 0)
1928 {
1929 /* May scroll text up. If there is not enough
1930 * remaining text or scrolling fails, must redraw the
1931 * rest. If scrolling works, must redraw the text
1932 * below the scrolled text. */
1933 if (row - xtra_rows >= wp->w_height - 2)
1934 mod_bot = MAXLNUM;
1935 else
1936 {
1937 check_for_delay(FALSE);
1938 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001939 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 mod_bot = MAXLNUM;
1941 else
1942 bot_start = wp->w_height + xtra_rows;
1943 }
1944 }
1945 else if (xtra_rows > 0)
1946 {
1947 /* May scroll text down. If there is not enough
1948 * remaining text of scrolling fails, must redraw the
1949 * rest. */
1950 if (row + xtra_rows >= wp->w_height - 2)
1951 mod_bot = MAXLNUM;
1952 else
1953 {
1954 check_for_delay(FALSE);
1955 if (win_ins_lines(wp, row + old_rows,
1956 xtra_rows, FALSE, FALSE) == FAIL)
1957 mod_bot = MAXLNUM;
1958 else if (top_end > row + old_rows)
1959 /* Scrolled the part at the top that requires
1960 * updating down. */
1961 top_end += xtra_rows;
1962 }
1963 }
1964
1965 /* When not updating the rest, may need to move w_lines[]
1966 * entries. */
1967 if (mod_bot != MAXLNUM && i != j)
1968 {
1969 if (j < i)
1970 {
1971 int x = row + new_rows;
1972
1973 /* move entries in w_lines[] upwards */
1974 for (;;)
1975 {
1976 /* stop at last valid entry in w_lines[] */
1977 if (i >= wp->w_lines_valid)
1978 {
1979 wp->w_lines_valid = j;
1980 break;
1981 }
1982 wp->w_lines[j] = wp->w_lines[i];
1983 /* stop at a line that won't fit */
1984 if (x + (int)wp->w_lines[j].wl_size
1985 > wp->w_height)
1986 {
1987 wp->w_lines_valid = j + 1;
1988 break;
1989 }
1990 x += wp->w_lines[j++].wl_size;
1991 ++i;
1992 }
1993 if (bot_start > x)
1994 bot_start = x;
1995 }
1996 else /* j > i */
1997 {
1998 /* move entries in w_lines[] downwards */
1999 j -= i;
2000 wp->w_lines_valid += j;
2001 if (wp->w_lines_valid > wp->w_height)
2002 wp->w_lines_valid = wp->w_height;
2003 for (i = wp->w_lines_valid; i - j >= idx; --i)
2004 wp->w_lines[i] = wp->w_lines[i - j];
2005
2006 /* The w_lines[] entries for inserted lines are
2007 * now invalid, but wl_size may be used above.
2008 * Reset to zero. */
2009 while (i >= idx)
2010 {
2011 wp->w_lines[i].wl_size = 0;
2012 wp->w_lines[i--].wl_valid = FALSE;
2013 }
2014 }
2015 }
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018
2019#ifdef FEAT_FOLDING
2020 /*
2021 * When lines are folded, display one line for all of them.
2022 * Otherwise, display normally (can be several display lines when
2023 * 'wrap' is on).
2024 */
2025 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2026 if (fold_count != 0)
2027 {
2028 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2029 ++row;
2030 --fold_count;
2031 wp->w_lines[idx].wl_folded = TRUE;
2032 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2033# ifdef FEAT_SYN_HL
2034 did_update = DID_FOLD;
2035# endif
2036 }
2037 else
2038#endif
2039 if (idx < wp->w_lines_valid
2040 && wp->w_lines[idx].wl_valid
2041 && wp->w_lines[idx].wl_lnum == lnum
2042 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002043 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 && srow + wp->w_lines[idx].wl_size > wp->w_height
2045#ifdef FEAT_DIFF
2046 && diff_check_fill(wp, lnum) == 0
2047#endif
2048 )
2049 {
2050 /* This line is not going to fit. Don't draw anything here,
2051 * will draw "@ " lines below. */
2052 row = wp->w_height + 1;
2053 }
2054 else
2055 {
2056#ifdef FEAT_SEARCH_EXTRA
2057 prepare_search_hl(wp, lnum);
2058#endif
2059#ifdef FEAT_SYN_HL
2060 /* Let the syntax stuff know we skipped a few lines. */
2061 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002062 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 syntax_end_parsing(syntax_last_parsed + 1);
2064#endif
2065
2066 /*
2067 * Display one line.
2068 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002069 row = win_line(wp, lnum, srow, wp->w_height,
2070 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072#ifdef FEAT_FOLDING
2073 wp->w_lines[idx].wl_folded = FALSE;
2074 wp->w_lines[idx].wl_lastlnum = lnum;
2075#endif
2076#ifdef FEAT_SYN_HL
2077 did_update = DID_LINE;
2078 syntax_last_parsed = lnum;
2079#endif
2080 }
2081
2082 wp->w_lines[idx].wl_lnum = lnum;
2083 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002084
2085 /* Past end of the window or end of the screen. Note that after
2086 * resizing wp->w_height may be end up too big. That's a problem
2087 * elsewhere, but prevent a crash here. */
2088 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002091 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2093 ++idx;
2094 break;
2095 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002096 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 wp->w_lines[idx].wl_size = row - srow;
2098 ++idx;
2099#ifdef FEAT_FOLDING
2100 lnum += fold_count + 1;
2101#else
2102 ++lnum;
2103#endif
2104 }
2105 else
2106 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002107 if (wp->w_p_rnu)
2108 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002109#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002110 // 'relativenumber' set: The text doesn't need to be drawn, but
2111 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002112 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2113 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002114 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002115 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002116#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002117 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002118 }
2119
2120 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 row += wp->w_lines[idx++].wl_size;
2122 if (row > wp->w_height) /* past end of screen */
2123 break;
2124#ifdef FEAT_FOLDING
2125 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2126#else
2127 ++lnum;
2128#endif
2129#ifdef FEAT_SYN_HL
2130 did_update = DID_NONE;
2131#endif
2132 }
2133
2134 if (lnum > buf->b_ml.ml_line_count)
2135 {
2136 eof = TRUE;
2137 break;
2138 }
2139 }
2140 /*
2141 * End of loop over all window lines.
2142 */
2143
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002144#ifdef FEAT_VTP
2145 /* Rewrite the character at the end of the screen line. */
2146 if (use_vtp())
2147 {
2148 int i;
2149
2150 for (i = 0; i < Rows; ++i)
2151# ifdef FEAT_MBYTE
2152 if (enc_utf8)
2153 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2154 LineOffset[i] + screen_Columns) > 1)
2155 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2156 else
2157 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2158 else
2159# endif
2160 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2161 }
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164 if (idx > wp->w_lines_valid)
2165 wp->w_lines_valid = idx;
2166
2167#ifdef FEAT_SYN_HL
2168 /*
2169 * Let the syntax stuff know we stop parsing here.
2170 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002171 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 syntax_end_parsing(syntax_last_parsed + 1);
2173#endif
2174
2175 /*
2176 * If we didn't hit the end of the file, and we didn't finish the last
2177 * line we were working on, then the line didn't fit.
2178 */
2179 wp->w_empty_rows = 0;
2180#ifdef FEAT_DIFF
2181 wp->w_filler_rows = 0;
2182#endif
2183 if (!eof && !didline)
2184 {
2185 if (lnum == wp->w_topline)
2186 {
2187 /*
2188 * Single line that does not fit!
2189 * Don't overwrite it, it can be edited.
2190 */
2191 wp->w_botline = lnum + 1;
2192 }
2193#ifdef FEAT_DIFF
2194 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2195 {
2196 /* Window ends in filler lines. */
2197 wp->w_botline = lnum;
2198 wp->w_filler_rows = wp->w_height - srow;
2199 }
2200#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002201 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2202 {
2203 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2204
2205 /*
2206 * Last line isn't finished: Display "@@@" in the last screen line.
2207 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002208 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002209 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002210 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002211 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002212 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002213 set_empty_rows(wp, srow);
2214 wp->w_botline = lnum;
2215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2217 {
2218 /*
2219 * Last line isn't finished: Display "@@@" at the end.
2220 */
2221 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2222 W_WINROW(wp) + wp->w_height,
2223 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002224 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 set_empty_rows(wp, srow);
2226 wp->w_botline = lnum;
2227 }
2228 else
2229 {
2230 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2231 wp->w_botline = lnum;
2232 }
2233 }
2234 else
2235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (eof) /* we hit the end of the file */
2238 {
2239 wp->w_botline = buf->b_ml.ml_line_count + 1;
2240#ifdef FEAT_DIFF
2241 j = diff_check_fill(wp, wp->w_botline);
2242 if (j > 0 && !wp->w_botfill)
2243 {
2244 /*
2245 * Display filler lines at the end of the file
2246 */
2247 if (char2cells(fill_diff) > 1)
2248 i = '-';
2249 else
2250 i = fill_diff;
2251 if (row + j > wp->w_height)
2252 j = wp->w_height - row;
2253 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2254 row += j;
2255 }
2256#endif
2257 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002258 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 wp->w_botline = lnum;
2260
2261 /* make sure the rest of the screen is blank */
2262 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002263 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002266#ifdef SYN_TIME_LIMIT
2267 syn_set_timeout(NULL);
2268#endif
2269
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 /* Reset the type of redrawing required, the window has been updated. */
2271 wp->w_redr_type = 0;
2272#ifdef FEAT_DIFF
2273 wp->w_old_topfill = wp->w_topfill;
2274 wp->w_old_botfill = wp->w_botfill;
2275#endif
2276
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002277 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
2279 /*
2280 * There is a trick with w_botline. If we invalidate it on each
2281 * change that might modify it, this will cause a lot of expensive
2282 * calls to plines() in update_topline() each time. Therefore the
2283 * value of w_botline is often approximated, and this value is used to
2284 * compute the value of w_topline. If the value of w_botline was
2285 * wrong, check that the value of w_topline is correct (cursor is on
2286 * the visible part of the text). If it's not, we need to redraw
2287 * again. Mostly this just means scrolling up a few lines, so it
2288 * doesn't look too bad. Only do this for the current window (where
2289 * changes are relevant).
2290 */
2291 wp->w_valid |= VALID_BOTLINE;
2292 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2293 {
2294 recursive = TRUE;
2295 curwin->w_valid &= ~VALID_TOPLINE;
2296 update_topline(); /* may invalidate w_botline again */
2297 if (must_redraw != 0)
2298 {
2299 /* Don't update for changes in buffer again. */
2300 i = curbuf->b_mod_set;
2301 curbuf->b_mod_set = FALSE;
2302 win_update(curwin);
2303 must_redraw = 0;
2304 curbuf->b_mod_set = i;
2305 }
2306 recursive = FALSE;
2307 }
2308 }
2309
2310#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2311 /* restore got_int, unless CTRL-C was hit while redrawing */
2312 if (!got_int)
2313 got_int = save_got_int;
2314#endif
2315}
2316
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317/*
2318 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2319 * as the filler character.
2320 */
2321 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002322win_draw_end(
2323 win_T *wp,
2324 int c1,
2325 int c2,
2326 int row,
2327 int endrow,
2328 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2331 int n = 0;
2332# define FDC_OFF n
2333#else
2334# define FDC_OFF 0
2335#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002336#ifdef FEAT_FOLDING
2337 int fdc = compute_foldcolumn(wp, 0);
2338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340#ifdef FEAT_RIGHTLEFT
2341 if (wp->w_p_rl)
2342 {
2343 /* No check for cmdline window: should never be right-left. */
2344# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002345 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 if (n > 0)
2348 {
2349 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002350 if (n > wp->w_width)
2351 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2353 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002354 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356# endif
2357# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002358 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
2360 int nn = n + 2;
2361
2362 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002363 if (nn > wp->w_width)
2364 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2366 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002367 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 n = nn;
2369 }
2370# endif
2371 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002372 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002373 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2375 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002376 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378 else
2379#endif
2380 {
2381#ifdef FEAT_CMDWIN
2382 if (cmdwin_type != 0 && wp == curwin)
2383 {
2384 /* draw the cmdline character in the leftmost column */
2385 n = 1;
2386 if (n > wp->w_width)
2387 n = wp->w_width;
2388 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002389 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002390 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 }
2392#endif
2393#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002394 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002396 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002399 if (nn > wp->w_width)
2400 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002402 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002403 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 n = nn;
2405 }
2406#endif
2407#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002408 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 int nn = n + 2;
2411
2412 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002413 if (nn > wp->w_width)
2414 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002416 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002417 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 n = nn;
2419 }
2420#endif
2421 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002422 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002423 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 set_empty_rows(wp, row);
2426}
2427
Bram Moolenaar1a384422010-07-14 19:53:30 +02002428#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002429/*
2430 * Advance **color_cols and return TRUE when there are columns to draw.
2431 */
2432 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002433advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002434{
2435 while (**color_cols >= 0 && vcol > **color_cols)
2436 ++*color_cols;
2437 return (**color_cols >= 0);
2438}
2439#endif
2440
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002441#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2442/*
2443 * Copy "text" to ScreenLines using "attr".
2444 * Returns the next screen column.
2445 */
2446 static int
2447text_to_screenline(win_T *wp, char_u *text, int col)
2448{
2449 int off = (int)(current_ScreenLine - ScreenLines);
2450
2451#ifdef FEAT_MBYTE
2452 if (has_mbyte)
2453 {
2454 int cells;
2455 int u8c, u8cc[MAX_MCO];
2456 int i;
2457 int idx;
2458 int c_len;
2459 char_u *p;
2460# ifdef FEAT_ARABIC
2461 int prev_c = 0; /* previous Arabic character */
2462 int prev_c1 = 0; /* first composing char for prev_c */
2463# endif
2464
2465# ifdef FEAT_RIGHTLEFT
2466 if (wp->w_p_rl)
2467 idx = off;
2468 else
2469# endif
2470 idx = off + col;
2471
2472 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2473 for (p = text; *p != NUL; )
2474 {
2475 cells = (*mb_ptr2cells)(p);
2476 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002477 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002478# ifdef FEAT_RIGHTLEFT
2479 - (wp->w_p_rl ? col : 0)
2480# endif
2481 )
2482 break;
2483 ScreenLines[idx] = *p;
2484 if (enc_utf8)
2485 {
2486 u8c = utfc_ptr2char(p, u8cc);
2487 if (*p < 0x80 && u8cc[0] == 0)
2488 {
2489 ScreenLinesUC[idx] = 0;
2490#ifdef FEAT_ARABIC
2491 prev_c = u8c;
2492#endif
2493 }
2494 else
2495 {
2496#ifdef FEAT_ARABIC
2497 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2498 {
2499 /* Do Arabic shaping. */
2500 int pc, pc1, nc;
2501 int pcc[MAX_MCO];
2502 int firstbyte = *p;
2503
2504 /* The idea of what is the previous and next
2505 * character depends on 'rightleft'. */
2506 if (wp->w_p_rl)
2507 {
2508 pc = prev_c;
2509 pc1 = prev_c1;
2510 nc = utf_ptr2char(p + c_len);
2511 prev_c1 = u8cc[0];
2512 }
2513 else
2514 {
2515 pc = utfc_ptr2char(p + c_len, pcc);
2516 nc = prev_c;
2517 pc1 = pcc[0];
2518 }
2519 prev_c = u8c;
2520
2521 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2522 pc, pc1, nc);
2523 ScreenLines[idx] = firstbyte;
2524 }
2525 else
2526 prev_c = u8c;
2527#endif
2528 /* Non-BMP character: display as ? or fullwidth ?. */
2529#ifdef UNICODE16
2530 if (u8c >= 0x10000)
2531 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2532 else
2533#endif
2534 ScreenLinesUC[idx] = u8c;
2535 for (i = 0; i < Screen_mco; ++i)
2536 {
2537 ScreenLinesC[i][idx] = u8cc[i];
2538 if (u8cc[i] == 0)
2539 break;
2540 }
2541 }
2542 if (cells > 1)
2543 ScreenLines[idx + 1] = 0;
2544 }
2545 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2546 /* double-byte single width character */
2547 ScreenLines2[idx] = p[1];
2548 else if (cells > 1)
2549 /* double-width character */
2550 ScreenLines[idx + 1] = p[1];
2551 col += cells;
2552 idx += cells;
2553 p += c_len;
2554 }
2555 }
2556 else
2557#endif
2558 {
2559 int len = (int)STRLEN(text);
2560
Bram Moolenaar02631462017-09-22 15:20:32 +02002561 if (len > wp->w_width - col)
2562 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002563 if (len > 0)
2564 {
2565#ifdef FEAT_RIGHTLEFT
2566 if (wp->w_p_rl)
2567 STRNCPY(current_ScreenLine, text, len);
2568 else
2569#endif
2570 STRNCPY(current_ScreenLine + col, text, len);
2571 col += len;
2572 }
2573 }
2574 return col;
2575}
2576#endif
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#ifdef FEAT_FOLDING
2579/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002580 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2581 * space is available for window "wp", minus "col".
2582 */
2583 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002584compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002585{
2586 int fdc = wp->w_p_fdc;
2587 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002588 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002589
2590 if (fdc > wwidth - (col + wmw))
2591 fdc = wwidth - (col + wmw);
2592 return fdc;
2593}
2594
2595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * Display one folded line.
2597 */
2598 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002599fold_line(
2600 win_T *wp,
2601 long fold_count,
2602 foldinfo_T *foldinfo,
2603 linenr_T lnum,
2604 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002606 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 pos_T *top, *bot;
2608 linenr_T lnume = lnum + fold_count - 1;
2609 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002610 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 int col;
2613 int txtcol;
2614 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002615 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 /* Build the fold line:
2618 * 1. Add the cmdwin_type for the command-line window
2619 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002620 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 * 4. Compose the text
2622 * 5. Add the text
2623 * 6. set highlighting for the Visual area an other text
2624 */
2625 col = 0;
2626
2627 /*
2628 * 1. Add the cmdwin_type for the command-line window
2629 * Ignores 'rightleft', this window is never right-left.
2630 */
2631#ifdef FEAT_CMDWIN
2632 if (cmdwin_type != 0 && wp == curwin)
2633 {
2634 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002635 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_MBYTE
2637 if (enc_utf8)
2638 ScreenLinesUC[off] = 0;
2639#endif
2640 ++col;
2641 }
2642#endif
2643
2644 /*
2645 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002646 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002648 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if (fdc > 0)
2650 {
2651 fill_foldcolumn(buf, wp, TRUE, lnum);
2652#ifdef FEAT_RIGHTLEFT
2653 if (wp->w_p_rl)
2654 {
2655 int i;
2656
Bram Moolenaar02631462017-09-22 15:20:32 +02002657 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002658 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 /* reverse the fold column */
2660 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002661 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
2663 else
2664#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002665 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 col += fdc;
2667 }
2668
2669#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002670# define RL_MEMSET(p, v, l) \
2671 do { \
2672 if (wp->w_p_rl) \
2673 for (ri = 0; ri < l; ++ri) \
2674 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2675 else \
2676 for (ri = 0; ri < l; ++ri) \
2677 ScreenAttrs[off + (p) + ri] = v; \
2678 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002680# define RL_MEMSET(p, v, l) \
2681 do { \
2682 for (ri = 0; ri < l; ++ri) \
2683 ScreenAttrs[off + (p) + ri] = v; \
2684 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#endif
2686
Bram Moolenaar64486672010-05-16 15:46:46 +02002687 /* Set all attributes of the 'number' or 'relativenumber' column and the
2688 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002689 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691#ifdef FEAT_SIGNS
2692 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002693 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002695 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (len > 0)
2697 {
2698 if (len > 2)
2699 len = 2;
2700# ifdef FEAT_RIGHTLEFT
2701 if (wp->w_p_rl)
2702 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002703 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002704 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 else
2706# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002707 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 col += len;
2709 }
2710 }
2711#endif
2712
2713 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002714 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002716 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002718 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (len > 0)
2720 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002721 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002722 long num;
2723 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002724
2725 if (len > w + 1)
2726 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002727
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002728 if (wp->w_p_nu && !wp->w_p_rnu)
2729 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002730 num = (long)lnum;
2731 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002732 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002733 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002734 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002735 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002736 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002737 /* 'number' + 'relativenumber': cursor line shows absolute
2738 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002739 num = lnum;
2740 fmt = "%-*ld ";
2741 }
2742 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002743
Bram Moolenaar700e7342013-01-30 12:31:36 +01002744 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#ifdef FEAT_RIGHTLEFT
2746 if (wp->w_p_rl)
2747 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002748 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002749 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 else
2751#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002752 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 col += len;
2754 }
2755 }
2756
2757 /*
2758 * 4. Compose the folded-line string with 'foldtext', if set.
2759 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002760 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 txtcol = col; /* remember where text starts */
2763
2764 /*
2765 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2766 * Right-left text is put in columns 0 - number-col, normal text is put
2767 * in columns number-col - window-width.
2768 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002769 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 /* Fill the rest of the line with the fold filler */
2772#ifdef FEAT_RIGHTLEFT
2773 if (wp->w_p_rl)
2774 col -= txtcol;
2775#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002776 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#ifdef FEAT_RIGHTLEFT
2778 - (wp->w_p_rl ? txtcol : 0)
2779#endif
2780 )
2781 {
2782#ifdef FEAT_MBYTE
2783 if (enc_utf8)
2784 {
2785 if (fill_fold >= 0x80)
2786 {
2787 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002788 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002789 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002794 ScreenLines[off + col] = fill_fold;
2795 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002796 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002800 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802
2803 if (text != buf)
2804 vim_free(text);
2805
2806 /*
2807 * 6. set highlighting for the Visual area an other text.
2808 * If all folded lines are in the Visual area, highlight the line.
2809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2811 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002812 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 /* Visual is after curwin->w_cursor */
2815 top = &curwin->w_cursor;
2816 bot = &VIsual;
2817 }
2818 else
2819 {
2820 /* Visual is before curwin->w_cursor */
2821 top = &VIsual;
2822 bot = &curwin->w_cursor;
2823 }
2824 if (lnum >= top->lnum
2825 && lnume <= bot->lnum
2826 && (VIsual_mode != 'v'
2827 || ((lnum > top->lnum
2828 || (lnum == top->lnum
2829 && top->col == 0))
2830 && (lnume < bot->lnum
2831 || (lnume == bot->lnum
2832 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002833 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
2835 if (VIsual_mode == Ctrl_V)
2836 {
2837 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002838 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002840 if (wp->w_old_cursor_lcol != MAXCOL
2841 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002842 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 len = wp->w_old_cursor_lcol;
2844 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002845 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002846 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002847 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849 }
2850 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002853 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
2856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002858#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002859 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2860 if (wp->w_p_cc_cols)
2861 {
2862 int i = 0;
2863 int j = wp->w_p_cc_cols[i];
2864 int old_txtcol = txtcol;
2865
2866 while (j > -1)
2867 {
2868 txtcol += j;
2869 if (wp->w_p_wrap)
2870 txtcol -= wp->w_skipcol;
2871 else
2872 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002874 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002875 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002876 txtcol = old_txtcol;
2877 j = wp->w_p_cc_cols[++i];
2878 }
2879 }
2880
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002881 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002882 if (wp->w_p_cuc)
2883 {
2884 txtcol += wp->w_virtcol;
2885 if (wp->w_p_wrap)
2886 txtcol -= wp->w_skipcol;
2887 else
2888 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002890 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002891 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002892 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2896 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 /*
2899 * Update w_cline_height and w_cline_folded if the cursor line was
2900 * updated (saves a call to plines() later).
2901 */
2902 if (wp == curwin
2903 && lnum <= curwin->w_cursor.lnum
2904 && lnume >= curwin->w_cursor.lnum)
2905 {
2906 curwin->w_cline_row = row;
2907 curwin->w_cline_height = 1;
2908 curwin->w_cline_folded = TRUE;
2909 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2910 }
2911}
2912
2913/*
2914 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2915 */
2916 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002917copy_text_attr(
2918 int off,
2919 char_u *buf,
2920 int len,
2921 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002923 int i;
2924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 mch_memmove(ScreenLines + off, buf, (size_t)len);
2926# ifdef FEAT_MBYTE
2927 if (enc_utf8)
2928 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2929# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002930 for (i = 0; i < len; ++i)
2931 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932}
2933
2934/*
2935 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002936 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002939fill_foldcolumn(
2940 char_u *p,
2941 win_T *wp,
2942 int closed, /* TRUE of FALSE */
2943 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
2945 int i = 0;
2946 int level;
2947 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002948 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002949 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002952 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
2954 level = win_foldinfo.fi_level;
2955 if (level > 0)
2956 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002957 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002958 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 /* If the column is too narrow, we start at the lowest level that
2961 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002962 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 if (first_level < 1)
2964 first_level = 1;
2965
Bram Moolenaar1c934292015-01-27 16:39:29 +01002966 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 {
2968 if (win_foldinfo.fi_lnum == lnum
2969 && first_level + i >= win_foldinfo.fi_low_level)
2970 p[i] = '-';
2971 else if (first_level == 1)
2972 p[i] = '|';
2973 else if (first_level + i <= 9)
2974 p[i] = '0' + first_level + i;
2975 else
2976 p[i] = '>';
2977 if (first_level + i == level)
2978 break;
2979 }
2980 }
2981 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002982 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983}
2984#endif /* FEAT_FOLDING */
2985
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002986#ifdef FEAT_TEXT_PROP
2987static textprop_T *current_text_props = NULL;
2988static buf_T *current_buf = NULL;
2989
2990 static int
2991#ifdef __BORLANDC__
2992_RTLENTRYF
2993#endif
2994text_prop_compare(const void *s1, const void *s2)
2995{
2996 int idx1, idx2;
2997 proptype_T *pt1, *pt2;
2998 colnr_T col1, col2;
2999
3000 idx1 = *(int *)s1;
3001 idx2 = *(int *)s2;
3002 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3003 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3004 if (pt1 == pt2)
3005 return 0;
3006 if (pt1 == NULL)
3007 return -1;
3008 if (pt2 == NULL)
3009 return 1;
3010 if (pt1->pt_priority != pt2->pt_priority)
3011 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3012 col1 = current_text_props[idx1].tp_col;
3013 col2 = current_text_props[idx2].tp_col;
3014 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3015}
3016#endif
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
3019 * Display line "lnum" of window 'wp' on the screen.
3020 * Start at row "startrow", stop when "endrow" is reached.
3021 * wp->w_virtcol needs to be valid.
3022 *
3023 * Return the number of last row the line occupies.
3024 */
3025 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003026win_line(
3027 win_T *wp,
3028 linenr_T lnum,
3029 int startrow,
3030 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003031 int nochange UNUSED, // not updating for changed text
3032 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003034 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3036 int c = 0; /* init for GCC */
3037 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003038#ifdef FEAT_LINEBREAK
3039 long vcol_sbr = -1; /* virtual column after showbreak */
3040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 long vcol_prev = -1; /* "vcol" of previous character */
3042 char_u *line; /* current line */
3043 char_u *ptr; /* current position in "line" */
3044 int row; /* row in the window, excl w_winrow */
3045 int screen_row; /* row on the screen, incl w_winrow */
3046
3047 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3048 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003049 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003050 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 int c_extra = NUL; /* extra chars, all the same */
3052 int extra_attr = 0; /* attributes when n_extra != 0 */
3053 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3054 displaying lcs_eol at end-of-line */
3055 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3056 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3057
3058 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3059 int saved_n_extra = 0;
3060 char_u *saved_p_extra = NULL;
3061 int saved_c_extra = 0;
3062 int saved_char_attr = 0;
3063
3064 int n_attr = 0; /* chars with special attr */
3065 int saved_attr2 = 0; /* char_attr saved for n_attr */
3066 int n_attr3 = 0; /* chars with overruling special attr */
3067 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3068
3069 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3070
3071 int fromcol, tocol; /* start/end of inverting */
3072 int fromcol_prev = -2; /* start of inverting after cursor */
3073 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003075 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 pos_T pos;
3077 long v;
3078
3079 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003080 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3082 in this line */
3083 int attr = 0; /* attributes for area highlighting */
3084 int area_attr = 0; /* attributes desired by highlighting */
3085 int search_attr = 0; /* attributes desired by 'hlsearch' */
3086#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003087 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 int syntax_attr = 0; /* attributes desired by syntax */
3089 int has_syntax = FALSE; /* this buffer has syntax highl. */
3090 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003091 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003092 int draw_color_col = FALSE; /* highlight colorcolumn */
3093 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003094#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003095#ifdef FEAT_TEXT_PROP
3096 int text_prop_count;
3097 int text_prop_next = 0; // next text property to use
3098 textprop_T *text_props = NULL;
3099 int *text_prop_idxs = NULL;
3100 int text_props_active = 0;
3101 proptype_T *text_prop_type = NULL;
3102 int text_prop_attr = 0;
3103#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003104#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003105 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003106# define SPWORDLEN 150
3107 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003108 int nextlinecol = 0; /* column where nextline[] starts */
3109 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003110 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003111 int spell_attr = 0; /* attributes desired by spelling */
3112 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003113 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3114 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003115 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003116 static int cap_col = -1; /* column to check for Cap word */
3117 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003118 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003120 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#ifdef FEAT_MBYTE
3122 int multi_attr = 0; /* attributes desired by multibyte */
3123 int mb_l = 1; /* multi-byte byte length */
3124 int mb_c = 0; /* decoded multi-byte character */
3125 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003126 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128#ifdef FEAT_DIFF
3129 int filler_lines; /* nr of filler lines to be drawn */
3130 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003131 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int change_start = MAXCOL; /* first col of changed area */
3133 int change_end = -1; /* last col of changed area */
3134#endif
3135 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3136#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003137 int need_showbreak = FALSE; /* overlong line, skipping first x
3138 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003140#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003141 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003143 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#endif
3145#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003146 matchitem_T *cur; /* points to the match list */
3147 match_T *shl; /* points to search_hl or a match */
3148 int shl_flag; /* flag to indicate whether search_hl
3149 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003150 int pos_inprogress; /* marks that position match search is
3151 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003152 int prevcol_hl_flag; /* flag to indicate whether prevcol
3153 equals startcol of search_hl or one
3154 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156#ifdef FEAT_ARABIC
3157 int prev_c = 0; /* previous Arabic character */
3158 int prev_c1 = 0; /* first composing char for prev_c */
3159#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003160#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003161 int did_line_attr = 0;
3162#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003163#ifdef FEAT_TERMINAL
3164 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003165 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 /* draw_state: items that are drawn in sequence: */
3169#define WL_START 0 /* nothing done yet */
3170#ifdef FEAT_CMDWIN
3171# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3172#else
3173# define WL_CMDLINE WL_START
3174#endif
3175#ifdef FEAT_FOLDING
3176# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3177#else
3178# define WL_FOLD WL_CMDLINE
3179#endif
3180#ifdef FEAT_SIGNS
3181# define WL_SIGN WL_FOLD + 1 /* column for signs */
3182#else
3183# define WL_SIGN WL_FOLD /* column for signs */
3184#endif
3185#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003186#ifdef FEAT_LINEBREAK
3187# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003189# define WL_BRI WL_NR
3190#endif
3191#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3192# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3193#else
3194# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#define WL_LINE WL_SBR + 1 /* text in the line */
3197 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003198#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 int feedback_col = 0;
3200 int feedback_old_attr = -1;
3201#endif
3202
Bram Moolenaar860cae12010-06-05 23:22:07 +02003203#ifdef FEAT_CONCEAL
3204 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003205 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003206 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003207 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003208 int is_concealing = FALSE;
3209 int boguscols = 0; /* nonexistent columns added to force
3210 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003211 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003212 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003213 int match_conc = 0; /* cchar for match functions */
3214 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003215 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003216# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003217# define FIX_FOR_BOGUSCOLS \
3218 { \
3219 n_extra += vcol_off; \
3220 vcol -= vcol_off; \
3221 vcol_off = 0; \
3222 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003223 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003224 boguscols = 0; \
3225 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003226#else
3227# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
3230 if (startrow > endrow) /* past the end already! */
3231 return startrow;
3232
3233 row = startrow;
3234 screen_row = row + W_WINROW(wp);
3235
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003236 if (!number_only)
3237 {
3238 /*
3239 * To speed up the loop below, set extra_check when there is linebreak,
3240 * trailing white space and/or syntax processing to be done.
3241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003243 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244#endif
3245#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003246 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003247# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003248 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003249# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003250 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003252 /* Prepare for syntax highlighting in this line. When there is an
3253 * error, stop syntax highlighting. */
3254 save_did_emsg = did_emsg;
3255 did_emsg = FALSE;
3256 syntax_start(wp, lnum);
3257 if (did_emsg)
3258 wp->w_s->b_syn_error = TRUE;
3259 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003260 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003261 did_emsg = save_did_emsg;
3262#ifdef SYN_TIME_LIMIT
3263 if (!wp->w_s->b_syn_slow)
3264#endif
3265 {
3266 has_syntax = TRUE;
3267 extra_check = TRUE;
3268 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003271
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003272 /* Check for columns to display for 'colorcolumn'. */
3273 color_cols = wp->w_p_cc_cols;
3274 if (color_cols != NULL)
3275 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003276#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003277
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003278#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003279 if (term_show_buffer(wp->w_buffer))
3280 {
3281 extra_check = TRUE;
3282 get_term_attr = TRUE;
3283 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3284 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003285#endif
3286
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003287#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003288 if (wp->w_p_spell
3289 && *wp->w_s->b_p_spl != NUL
3290 && wp->w_s->b_langp.ga_len > 0
3291 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003292 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003293 /* Prepare for spell checking. */
3294 has_spell = TRUE;
3295 extra_check = TRUE;
3296
Bram Moolenaar7701f302018-10-02 21:20:32 +02003297 /* Get the start of the next line, so that words that wrap to the
3298 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003299 * Trick: skip a few chars for C/shell/Vim comments */
3300 nextline[SPWORDLEN] = NUL;
3301 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3302 {
3303 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3304 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3305 }
3306
Bram Moolenaar7701f302018-10-02 21:20:32 +02003307 /* When a word wrapped from the previous line the start of the
3308 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003309 if (lnum == checked_lnum)
3310 cur_checked_col = checked_col;
3311 checked_lnum = 0;
3312
3313 /* When there was a sentence end in the previous line may require a
3314 * word starting with capital in this line. In line 1 always check
3315 * the first word. */
3316 if (lnum != capcol_lnum)
3317 cap_col = -1;
3318 if (lnum == 1)
3319 cap_col = 0;
3320 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#endif
3323
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003324 /*
3325 * handle visual active in this window
3326 */
3327 fromcol = -10;
3328 tocol = MAXCOL;
3329 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003331 /* Visual is after curwin->w_cursor */
3332 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003334 top = &curwin->w_cursor;
3335 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003337 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 top = &VIsual;
3340 bot = &curwin->w_cursor;
3341 }
3342 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3343 if (VIsual_mode == Ctrl_V) /* block mode */
3344 {
3345 if (lnum_in_visual_area)
3346 {
3347 fromcol = wp->w_old_cursor_fcol;
3348 tocol = wp->w_old_cursor_lcol;
3349 }
3350 }
3351 else /* non-block mode */
3352 {
3353 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003355 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 if (VIsual_mode == 'V') /* linewise */
3358 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
3360 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3362 if (gchar_pos(top) == NUL)
3363 tocol = fromcol + 1;
3364 }
3365 }
3366 if (VIsual_mode != 'V' && lnum == bot->lnum)
3367 {
3368 if (*p_sel == 'e' && bot->col == 0
3369#ifdef FEAT_VIRTUALEDIT
3370 && bot->coladd == 0
3371#endif
3372 )
3373 {
3374 fromcol = -10;
3375 tocol = MAXCOL;
3376 }
3377 else if (bot->col == MAXCOL)
3378 tocol = MAXCOL;
3379 else
3380 {
3381 pos = *bot;
3382 if (*p_sel == 'e')
3383 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3384 else
3385 {
3386 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3387 ++tocol;
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 }
3391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 /* Check if the character under the cursor should not be inverted */
3394 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003395#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003396 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003397#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 )
3399 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 /* if inverting in this line set area_highlighting */
3402 if (fromcol >= 0)
3403 {
3404 area_highlighting = TRUE;
3405 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 if ((clip_star.available && !clip_star.owned
3408 && clip_isautosel_star())
3409 || (clip_plus.available && !clip_plus.owned
3410 && clip_isautosel_plus()))
3411 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003416 /*
3417 * handle 'incsearch' and ":s///c" highlighting
3418 */
3419 else if (highlight_match
3420 && wp == curwin
3421 && lnum >= curwin->w_cursor.lnum
3422 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 if (lnum == curwin->w_cursor.lnum)
3425 getvcol(curwin, &(curwin->w_cursor),
3426 (colnr_T *)&fromcol, NULL, NULL);
3427 else
3428 fromcol = 0;
3429 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3430 {
3431 pos.lnum = lnum;
3432 pos.col = search_match_endcol;
3433 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3434 }
3435 else
3436 tocol = MAXCOL;
3437 /* do at least one character; happens when past end of line */
3438 if (fromcol == tocol)
3439 tocol = fromcol + 1;
3440 area_highlighting = TRUE;
3441 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444
3445#ifdef FEAT_DIFF
3446 filler_lines = diff_check(wp, lnum);
3447 if (filler_lines < 0)
3448 {
3449 if (filler_lines == -1)
3450 {
3451 if (diff_find_change(wp, lnum, &change_start, &change_end))
3452 diff_hlf = HLF_ADD; /* added line */
3453 else if (change_start == 0)
3454 diff_hlf = HLF_TXD; /* changed text */
3455 else
3456 diff_hlf = HLF_CHD; /* changed line */
3457 }
3458 else
3459 diff_hlf = HLF_ADD; /* added line */
3460 filler_lines = 0;
3461 area_highlighting = TRUE;
3462 }
3463 if (lnum == wp->w_topline)
3464 filler_lines = wp->w_topfill;
3465 filler_todo = filler_lines;
3466#endif
3467
3468#ifdef LINE_ATTR
3469# ifdef FEAT_SIGNS
3470 /* If this line has a sign with line highlighting set line_attr. */
3471 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3472 if (v != 0)
3473 line_attr = sign_get_attr((int)v, TRUE);
3474# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003475# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003477 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003478 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479# endif
3480 if (line_attr != 0)
3481 area_highlighting = TRUE;
3482#endif
3483
3484 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3485 ptr = line;
3486
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003487#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003488 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003489 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003490 /* For checking first word with a capital skip white space. */
3491 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003492 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003493
Bram Moolenaar30abd282005-06-22 22:35:10 +00003494 /* To be able to spell-check over line boundaries copy the end of the
3495 * current line into nextline[]. Above the start of the next line was
3496 * copied to nextline[SPWORDLEN]. */
3497 if (nextline[SPWORDLEN] == NUL)
3498 {
3499 /* No next line or it is empty. */
3500 nextlinecol = MAXCOL;
3501 nextline_idx = 0;
3502 }
3503 else
3504 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003505 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003506 if (v < SPWORDLEN)
3507 {
3508 /* Short line, use it completely and append the start of the
3509 * next line. */
3510 nextlinecol = 0;
3511 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003512 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003513 nextline_idx = v + 1;
3514 }
3515 else
3516 {
3517 /* Long line, use only the last SPWORDLEN bytes. */
3518 nextlinecol = v - SPWORDLEN;
3519 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3520 nextline_idx = SPWORDLEN + 1;
3521 }
3522 }
3523 }
3524#endif
3525
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003526 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003528 if (lcs_space || lcs_trail)
3529 extra_check = TRUE;
3530 /* find start of trailing whitespace */
3531 if (lcs_trail)
3532 {
3533 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003534 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003535 --trailcol;
3536 trailcol += (colnr_T) (ptr - line);
3537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539
3540 /*
3541 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3542 * first character to be displayed.
3543 */
3544 if (wp->w_p_wrap)
3545 v = wp->w_skipcol;
3546 else
3547 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550#ifdef FEAT_MBYTE
3551 char_u *prev_ptr = ptr;
3552#endif
3553 while (vcol < v && *ptr != NUL)
3554 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003555 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 vcol += c;
3557#ifdef FEAT_MBYTE
3558 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003560 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003563 /* When:
3564 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003565 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003566 * - 'virtualedit' is set, or
3567 * - the visual mode is active,
3568 * the end of the line may be before the start of the displayed part.
3569 */
3570 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003571#ifdef FEAT_SYN_HL
3572 wp->w_p_cuc || draw_color_col ||
3573#endif
3574#ifdef FEAT_VIRTUALEDIT
3575 virtual_active() ||
3576#endif
3577 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 /* Handle a character that's not completely on the screen: Put ptr at
3583 * that character but skip the first few screen characters. */
3584 if (vcol > v)
3585 {
3586 vcol -= c;
3587#ifdef FEAT_MBYTE
3588 ptr = prev_ptr;
3589#else
3590 --ptr;
3591#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003592 /* If the character fits on the screen, don't need to skip it.
3593 * Except for a TAB. */
3594 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003595#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003596 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003597#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003598 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003599 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
3601
3602 /*
3603 * Adjust for when the inverted text is before the screen,
3604 * and when the start of the inverted text is before the screen.
3605 */
3606 if (tocol <= vcol)
3607 fromcol = 0;
3608 else if (fromcol >= 0 && fromcol < vcol)
3609 fromcol = vcol;
3610
3611#ifdef FEAT_LINEBREAK
3612 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3613 if (wp->w_p_wrap)
3614 need_showbreak = TRUE;
3615#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003616#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003617 /* When spell checking a word we need to figure out the start of the
3618 * word and if it's badly spelled or not. */
3619 if (has_spell)
3620 {
3621 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003622 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003623 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003624
3625 pos = wp->w_cursor;
3626 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003627 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003628 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003629
3630 /* spell_move_to() may call ml_get() and make "line" invalid */
3631 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3632 ptr = line + linecol;
3633
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003634 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003635 {
3636 /* no bad word found at line start, don't check until end of a
3637 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003638 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003639 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003640 }
3641 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003642 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003643 /* bad word found, use attributes until end of word */
3644 word_end = wp->w_cursor.col + len + 1;
3645
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003646 /* Turn index into actual attributes. */
3647 if (spell_hlf != HLF_COUNT)
3648 spell_attr = highlight_attr[spell_hlf];
3649 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003650 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003651
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003652# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003653 /* Need to restart syntax highlighting for this line. */
3654 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003655 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003656# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003657 }
3658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660
3661 /*
3662 * Correct highlighting for cursor that can't be disabled.
3663 * Avoids having to check this for each character.
3664 */
3665 if (fromcol >= 0)
3666 {
3667 if (noinvcur)
3668 {
3669 if ((colnr_T)fromcol == wp->w_virtcol)
3670 {
3671 /* highlighting starts at cursor, let it start just after the
3672 * cursor */
3673 fromcol_prev = fromcol;
3674 fromcol = -1;
3675 }
3676 else if ((colnr_T)fromcol < wp->w_virtcol)
3677 /* restart highlighting after the cursor */
3678 fromcol_prev = wp->w_virtcol;
3679 }
3680 if (fromcol >= tocol)
3681 fromcol = -1;
3682 }
3683
3684#ifdef FEAT_SEARCH_EXTRA
3685 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003686 * Handle highlighting the last used search pattern and matches.
3687 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003689 cur = wp->w_match_head;
3690 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003691 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003693 if (shl_flag == FALSE)
3694 {
3695 shl = &search_hl;
3696 shl_flag = TRUE;
3697 }
3698 else
3699 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003700 shl->startcol = MAXCOL;
3701 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003703 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003704 v = (long)(ptr - line);
3705 if (cur != NULL)
3706 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003707 next_search_hl(wp, shl, lnum, (colnr_T)v,
3708 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003709
3710 /* Need to get the line again, a multi-line regexp may have made it
3711 * invalid. */
3712 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3713 ptr = line + v;
3714
3715 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003717 if (shl->lnum == lnum)
3718 shl->startcol = shl->rm.startpos[0].col;
3719 else
3720 shl->startcol = 0;
3721 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3722 - shl->rm.startpos[0].lnum)
3723 shl->endcol = shl->rm.endpos[0].col;
3724 else
3725 shl->endcol = MAXCOL;
3726 /* Highlight one character for an empty match. */
3727 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003730 if (has_mbyte && line[shl->endcol] != NUL)
3731 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003734 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003736 if ((long)shl->startcol < v) /* match at leftcol */
3737 {
3738 shl->attr_cur = shl->attr;
3739 search_attr = shl->attr;
3740 }
3741 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003743 if (shl != &search_hl && cur != NULL)
3744 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746#endif
3747
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003748#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003749 /* Cursor line highlighting for 'cursorline' in the current window. Not
3750 * when Visual mode is active, because it's not clear what is selected
3751 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003752 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003753 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003754 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003755 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003756 area_highlighting = TRUE;
3757 }
3758#endif
3759
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003760#ifdef FEAT_TEXT_PROP
3761 {
3762 char_u *prop_start;
3763
3764 text_prop_count = get_text_props(wp->w_buffer, lnum,
3765 &prop_start, FALSE);
3766 if (text_prop_count > 0)
3767 {
3768 // Make a copy of the properties, so that they are properly
3769 // aligned.
3770 text_props = (textprop_T *)alloc(
3771 text_prop_count * sizeof(textprop_T));
3772 if (text_props != NULL)
3773 mch_memmove(text_props, prop_start,
3774 text_prop_count * sizeof(textprop_T));
3775
3776 // Allocate an array for the indexes.
3777 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3778 area_highlighting = TRUE;
3779 extra_check = TRUE;
3780 }
3781 }
3782#endif
3783
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003784 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 col = 0;
3786#ifdef FEAT_RIGHTLEFT
3787 if (wp->w_p_rl)
3788 {
3789 /* Rightleft window: process the text in the normal direction, but put
3790 * it in current_ScreenLine[] from right to left. Start at the
3791 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003792 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 off += col;
3794 }
3795#endif
3796
3797 /*
3798 * Repeat for the whole displayed line.
3799 */
3800 for (;;)
3801 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003802#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003803 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 /* Skip this quickly when working on the text. */
3806 if (draw_state != WL_LINE)
3807 {
3808#ifdef FEAT_CMDWIN
3809 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3810 {
3811 draw_state = WL_CMDLINE;
3812 if (cmdwin_type != 0 && wp == curwin)
3813 {
3814 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003816 c_extra = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003817 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 }
3820#endif
3821
3822#ifdef FEAT_FOLDING
3823 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3824 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003825 int fdc = compute_foldcolumn(wp, 0);
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003828 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003830 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003831 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003832 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003833 p_extra_free = alloc(12 + 1);
3834
3835 if (p_extra_free != NULL)
3836 {
3837 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3838 n_extra = fdc;
3839 p_extra_free[n_extra] = NUL;
3840 p_extra = p_extra_free;
3841 c_extra = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003842 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 }
3846#endif
3847
3848#ifdef FEAT_SIGNS
3849 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3850 {
3851 draw_state = WL_SIGN;
3852 /* Show the sign column when there are any signs in this
3853 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003854 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003856 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003858 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859# endif
3860
3861 /* Draw two cells with the sign value or blank. */
3862 c_extra = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01003863 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 n_extra = 2;
3865
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003866 if (row == startrow
3867#ifdef FEAT_DIFF
3868 + filler_lines && filler_todo <= 0
3869#endif
3870 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
3872 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3873 SIGN_TEXT);
3874# ifdef FEAT_SIGN_ICONS
3875 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3876 SIGN_ICON);
3877 if (gui.in_use && icon_sign != 0)
3878 {
3879 /* Use the image in this position. */
3880 c_extra = SIGN_BYTE;
3881# ifdef FEAT_NETBEANS_INTG
3882 if (buf_signcount(wp->w_buffer, lnum) > 1)
3883 c_extra = MULTISIGN_BYTE;
3884# endif
3885 char_attr = icon_sign;
3886 }
3887 else
3888# endif
3889 if (text_sign != 0)
3890 {
3891 p_extra = sign_get_text(text_sign);
3892 if (p_extra != NULL)
3893 {
3894 c_extra = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003895 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 char_attr = sign_get_attr(text_sign, FALSE);
3898 }
3899 }
3900 }
3901 }
3902#endif
3903
3904 if (draw_state == WL_NR - 1 && n_extra == 0)
3905 {
3906 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003907 /* Display the absolute or relative line number. After the
3908 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3909 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 && (row == startrow
3911#ifdef FEAT_DIFF
3912 + filler_lines
3913#endif
3914 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3915 {
3916 /* Draw the line number (empty space after wrapping). */
3917 if (row == startrow
3918#ifdef FEAT_DIFF
3919 + filler_lines
3920#endif
3921 )
3922 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003923 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003924 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003925
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003926 if (wp->w_p_nu && !wp->w_p_rnu)
3927 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003928 num = (long)lnum;
3929 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003930 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003931 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003932 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003933 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003934 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003935 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003936 num = lnum;
3937 fmt = "%-*ld ";
3938 }
3939 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003940
Bram Moolenaar700e7342013-01-30 12:31:36 +01003941 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003942 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (wp->w_skipcol > 0)
3944 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3945 *p_extra = '-';
3946#ifdef FEAT_RIGHTLEFT
3947 if (wp->w_p_rl) /* reverse line numbers */
3948 rl_mirror(extra);
3949#endif
3950 p_extra = extra;
3951 c_extra = NUL;
3952 }
3953 else
3954 c_extra = ' ';
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003955 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003956 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003957#ifdef FEAT_SYN_HL
3958 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003959 * the current line differently.
3960 * TODO: Can we use CursorLine instead of CursorLineNr
3961 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003962 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003963 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003964 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 }
3968
Bram Moolenaar597a4222014-06-25 14:39:50 +02003969#ifdef FEAT_LINEBREAK
3970 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3971 && n_extra == 0 && *p_sbr != NUL)
3972 /* draw indent after showbreak value */
3973 draw_state = WL_BRI;
3974 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3975 /* After the showbreak, draw the breakindent */
3976 draw_state = WL_BRI - 1;
3977
3978 /* draw 'breakindent': indent wrapped text accordingly */
3979 if (draw_state == WL_BRI - 1 && n_extra == 0)
3980 {
3981 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01003982 /* if need_showbreak is set, breakindent also applies */
3983 if (wp->w_p_bri && n_extra == 0
3984 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003985# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003986 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003987# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02003988 )
3989 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01003990 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003991# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02003992 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02003993 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003994 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003995# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02003996 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
3997 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003998 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02003999# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004000 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004001# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004002 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004003 c_extra = ' ';
4004 n_extra = get_breakindent_win(wp,
4005 ml_get_buf(wp->w_buffer, lnum, FALSE));
4006 /* Correct end of highlighted area for 'breakindent',
4007 * required when 'linebreak' is also set. */
4008 if (tocol == vcol)
4009 tocol += n_extra;
4010 }
4011 }
4012#endif
4013
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4015 if (draw_state == WL_SBR - 1 && n_extra == 0)
4016 {
4017 draw_state = WL_SBR;
4018# ifdef FEAT_DIFF
4019 if (filler_todo > 0)
4020 {
4021 /* Draw "deleted" diff line(s). */
4022 if (char2cells(fill_diff) > 1)
4023 c_extra = '-';
4024 else
4025 c_extra = fill_diff;
4026# ifdef FEAT_RIGHTLEFT
4027 if (wp->w_p_rl)
4028 n_extra = col + 1;
4029 else
4030# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004031 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004032 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034# endif
4035# ifdef FEAT_LINEBREAK
4036 if (*p_sbr != NUL && need_showbreak)
4037 {
4038 /* Draw 'showbreak' at the start of each broken line. */
4039 p_extra = p_sbr;
4040 c_extra = NUL;
4041 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004042 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004044 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 /* Correct end of highlighted area for 'showbreak',
4046 * required when 'linebreak' is also set. */
4047 if (tocol == vcol)
4048 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004049#ifdef FEAT_SYN_HL
4050 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004051 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004052 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004053 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
4056# endif
4057 }
4058#endif
4059
4060 if (draw_state == WL_LINE - 1 && n_extra == 0)
4061 {
4062 draw_state = WL_LINE;
4063 if (saved_n_extra)
4064 {
4065 /* Continue item from end of wrapped line. */
4066 n_extra = saved_n_extra;
4067 c_extra = saved_c_extra;
4068 p_extra = saved_p_extra;
4069 char_attr = saved_char_attr;
4070 }
4071 else
4072 char_attr = 0;
4073 }
4074 }
4075
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004076 // When still displaying '$' of change command, stop at cursor.
4077 // When only displaying the (relative) line number and that's done,
4078 // stop here.
4079 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#ifdef FEAT_DIFF
4082 && filler_todo <= 0
4083#endif
4084 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004085 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004087 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004088 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004089 /* Pretend we have finished updating the window. Except when
4090 * 'cursorcolumn' is set. */
4091#ifdef FEAT_SYN_HL
4092 if (wp->w_p_cuc)
4093 row = wp->w_cline_row + wp->w_cline_height;
4094 else
4095#endif
4096 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 break;
4098 }
4099
Bram Moolenaar637532b2019-01-03 21:44:40 +01004100 if (draw_state == WL_LINE && (area_highlighting
4101#ifdef FEAT_SPELL
4102 || has_spell
4103#endif
4104 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
4106 /* handle Visual or match highlighting in this line */
4107 if (vcol == fromcol
4108#ifdef FEAT_MBYTE
4109 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4110 && (*mb_ptr2cells)(ptr) > 1)
4111#endif
4112 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004113 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 && vcol < tocol))
4115 area_attr = attr; /* start highlighting */
4116 else if (area_attr != 0
4117 && (vcol == tocol
4118 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121#ifdef FEAT_SEARCH_EXTRA
4122 if (!n_extra)
4123 {
4124 /*
4125 * Check for start/end of search pattern match.
4126 * After end, check for start/end of next match.
4127 * When another match, have to check for start again.
4128 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004129 * Do this for 'search_hl' and the match list (ordered by
4130 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004132 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004133 cur = wp->w_match_head;
4134 shl_flag = FALSE;
4135 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004137 if (shl_flag == FALSE
4138 && ((cur != NULL
4139 && cur->priority > SEARCH_HL_PRIORITY)
4140 || cur == NULL))
4141 {
4142 shl = &search_hl;
4143 shl_flag = TRUE;
4144 }
4145 else
4146 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004147 if (cur != NULL)
4148 cur->pos.cur = 0;
4149 pos_inprogress = TRUE;
4150 while (shl->rm.regprog != NULL
4151 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004153 if (shl->startcol != MAXCOL
4154 && v >= (long)shl->startcol
4155 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004157#ifdef FEAT_MBYTE
4158 int tmp_col = v + MB_PTR2LEN(ptr);
4159
4160 if (shl->endcol < tmp_col)
4161 shl->endcol = tmp_col;
4162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004164#ifdef FEAT_CONCEAL
4165 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4166 == cur->hlg_id)
4167 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004168 has_match_conc =
4169 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004170 match_conc = cur->conceal_char;
4171 }
4172 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004173 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004176 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {
4178 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004179 next_search_hl(wp, shl, lnum, (colnr_T)v,
4180 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004181 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004182 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 /* Need to get the line again, a multi-line regexp
4185 * may have made it invalid. */
4186 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4187 ptr = line + v;
4188
4189 if (shl->lnum == lnum)
4190 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004191 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004193 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004195 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
4199 /* highlight empty match, try again after
4200 * it */
4201#ifdef FEAT_MBYTE
4202 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004203 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004204 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 else
4206#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004207 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
4209
4210 /* Loop to check if the match starts at the
4211 * current position */
4212 continue;
4213 }
4214 }
4215 break;
4216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004217 if (shl != &search_hl && cur != NULL)
4218 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004220
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004221 /* Use attributes from match with highest priority among
4222 * 'search_hl' and the match list. */
4223 search_attr = search_hl.attr_cur;
4224 cur = wp->w_match_head;
4225 shl_flag = FALSE;
4226 while (cur != NULL || shl_flag == FALSE)
4227 {
4228 if (shl_flag == FALSE
4229 && ((cur != NULL
4230 && cur->priority > SEARCH_HL_PRIORITY)
4231 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004232 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004233 shl = &search_hl;
4234 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004235 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004236 else
4237 shl = &cur->hl;
4238 if (shl->attr_cur != 0)
4239 search_attr = shl->attr_cur;
4240 if (shl != &search_hl && cur != NULL)
4241 cur = cur->next;
4242 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004243 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004244 if (*ptr == NUL && (did_line_attr >= 1
4245 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004246 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248#endif
4249
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004251 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004253 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4254 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004256 if (diff_hlf == HLF_TXD && ptr - line > change_end
4257 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004259 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004260 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004261 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004264
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004265#ifdef FEAT_TEXT_PROP
4266 if (text_props != NULL)
4267 {
4268 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004269 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004270
4271 // Check if any active property ends.
4272 for (pi = 0; pi < text_props_active; ++pi)
4273 {
4274 int tpi = text_prop_idxs[pi];
4275
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004276 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004277 + text_props[tpi].tp_len)
4278 {
4279 if (pi + 1 < text_props_active)
4280 mch_memmove(text_prop_idxs + pi,
4281 text_prop_idxs + pi + 1,
4282 sizeof(int)
4283 * (text_props_active - (pi + 1)));
4284 --text_props_active;
4285 --pi;
4286 }
4287 }
4288
4289 // Add any text property that starts in this column.
4290 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004291 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004292 text_prop_idxs[text_props_active++] = text_prop_next++;
4293
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004294 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004295 if (text_props_active > 0)
4296 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004297 // Sort the properties on priority and/or starting last.
4298 // Then combine the attributes, highest priority last.
4299 current_text_props = text_props;
4300 current_buf = wp->w_buffer;
4301 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4302 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004303
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004304 for (pi = 0; pi < text_props_active; ++pi)
4305 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004306 int tpi = text_prop_idxs[pi];
4307 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004308
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004309 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004310 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004311 int pt_attr = syn_id2attr(pt->pt_hl_id);
4312
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004313 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004314 if (text_prop_attr == 0)
4315 text_prop_attr = pt_attr;
4316 else
4317 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004318 }
4319 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004320 }
4321 }
4322#endif
4323
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324 /* Decide which of the highlight attributes to use. */
4325 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004326#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004327 if (area_attr != 0)
4328 char_attr = hl_combine_attr(line_attr, area_attr);
4329 else if (search_attr != 0)
4330 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004331 /* Use line_attr when not in the Visual or 'incsearch' area
4332 * (area_attr may be 0 when "noinvcur" is set). */
4333 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004334 || vcol < fromcol || vcol_prev < fromcol_prev
4335 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004336 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004337#else
4338 if (area_attr != 0)
4339 char_attr = area_attr;
4340 else if (search_attr != 0)
4341 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004342#endif
4343 else
4344 {
4345 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004346#ifdef FEAT_TEXT_PROP
4347 if (text_prop_type != NULL)
4348 char_attr = text_prop_attr;
4349 else
4350#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004351#ifdef FEAT_SYN_HL
4352 if (has_syntax)
4353 char_attr = syntax_attr;
4354 else
4355#endif
4356 char_attr = 0;
4357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359
4360 /*
4361 * Get the next character to put on the screen.
4362 */
4363 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004364 * The "p_extra" points to the extra stuff that is inserted to
4365 * represent special characters (non-printable stuff) and other
4366 * things. When all characters are the same, c_extra is used.
4367 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4368 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4370 */
4371 if (n_extra > 0)
4372 {
4373 if (c_extra != NUL)
4374 {
4375 c = c_extra;
4376#ifdef FEAT_MBYTE
4377 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004378 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 {
4380 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004381 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004382 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 }
4384 else
4385 mb_utf8 = FALSE;
4386#endif
4387 }
4388 else
4389 {
4390 c = *p_extra;
4391#ifdef FEAT_MBYTE
4392 if (has_mbyte)
4393 {
4394 mb_c = c;
4395 if (enc_utf8)
4396 {
4397 /* If the UTF-8 character is more than one byte:
4398 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004399 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 mb_utf8 = FALSE;
4401 if (mb_l > n_extra)
4402 mb_l = 1;
4403 else if (mb_l > 1)
4404 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004405 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004407 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 }
4410 else
4411 {
4412 /* if this is a DBCS character, put it in "mb_c" */
4413 mb_l = MB_BYTE2LEN(c);
4414 if (mb_l >= n_extra)
4415 mb_l = 1;
4416 else if (mb_l > 1)
4417 mb_c = (c << 8) + p_extra[1];
4418 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004419 if (mb_l == 0) /* at the NUL at end-of-line */
4420 mb_l = 1;
4421
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 /* If a double-width char doesn't fit display a '>' in the
4423 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004424 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425# ifdef FEAT_RIGHTLEFT
4426 wp->w_p_rl ? (col <= 0) :
4427# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004428 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 && (*mb_char2cells)(mb_c) == 2)
4430 {
4431 c = '>';
4432 mb_c = c;
4433 mb_l = 1;
4434 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004435 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 /* put the pointer back to output the double-width
4437 * character at the start of the next line. */
4438 ++n_extra;
4439 --p_extra;
4440 }
4441 else
4442 {
4443 n_extra -= mb_l - 1;
4444 p_extra += mb_l - 1;
4445 }
4446 }
4447#endif
4448 ++p_extra;
4449 }
4450 --n_extra;
4451 }
4452 else
4453 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004454#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004455 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004456#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004457
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004458 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004459 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 /*
4461 * Get a character from the line itself.
4462 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004463 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004464#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004465 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467#ifdef FEAT_MBYTE
4468 if (has_mbyte)
4469 {
4470 mb_c = c;
4471 if (enc_utf8)
4472 {
4473 /* If the UTF-8 character is more than one byte: Decode it
4474 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004475 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 mb_utf8 = FALSE;
4477 if (mb_l > 1)
4478 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004479 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /* Overlong encoded ASCII or ASCII with composing char
4481 * is displayed normally, except a NUL. */
4482 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004483 {
4484 c = mb_c;
4485# ifdef FEAT_LINEBREAK
4486 c0 = mb_c;
4487# endif
4488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004490
4491 /* At start of the line we can have a composing char.
4492 * Draw it as a space with a composing char. */
4493 if (utf_iscomposing(mb_c))
4494 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004495 int i;
4496
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 for (i = Screen_mco - 1; i > 0; --i)
4498 u8cc[i] = u8cc[i - 1];
4499 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004500 mb_c = ' ';
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503
4504 if ((mb_l == 1 && c >= 0x80)
4505 || (mb_l >= 1 && mb_c == 0)
4506 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004507# ifdef UNICODE16
4508 || mb_c >= 0x10000
4509# endif
4510 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 /*
4513 * Illegal UTF-8 byte: display as <xx>.
4514 * Non-BMP character : display as ? or fullwidth ?.
4515 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004516# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004518# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
4520 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004521# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 if (wp->w_p_rl) /* reverse */
4523 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004526# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 else if (utf_char2cells(mb_c) != 2)
4528 STRCPY(extra, "?");
4529 else
4530 /* 0xff1f in UTF-8: full-width '?' */
4531 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 p_extra = extra;
4535 c = *p_extra;
4536 mb_c = mb_ptr2char_adv(&p_extra);
4537 mb_utf8 = (c >= 0x80);
4538 n_extra = (int)STRLEN(p_extra);
4539 c_extra = NUL;
4540 if (area_attr == 0 && search_attr == 0)
4541 {
4542 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004543 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 saved_attr2 = char_attr; /* save current attr */
4545 }
4546 }
4547 else if (mb_l == 0) /* at the NUL at end-of-line */
4548 mb_l = 1;
4549#ifdef FEAT_ARABIC
4550 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4551 {
4552 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004553 int pc, pc1, nc;
4554 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556 /* The idea of what is the previous and next
4557 * character depends on 'rightleft'. */
4558 if (wp->w_p_rl)
4559 {
4560 pc = prev_c;
4561 pc1 = prev_c1;
4562 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004563 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 else
4566 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004567 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004569 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571 prev_c = mb_c;
4572
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004573 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575 else
4576 prev_c = mb_c;
4577#endif
4578 }
4579 else /* enc_dbcs */
4580 {
4581 mb_l = MB_BYTE2LEN(c);
4582 if (mb_l == 0) /* at the NUL at end-of-line */
4583 mb_l = 1;
4584 else if (mb_l > 1)
4585 {
4586 /* We assume a second byte below 32 is illegal.
4587 * Hopefully this is OK for all double-byte encodings!
4588 */
4589 if (ptr[1] >= 32)
4590 mb_c = (c << 8) + ptr[1];
4591 else
4592 {
4593 if (ptr[1] == NUL)
4594 {
4595 /* head byte at end of line */
4596 mb_l = 1;
4597 transchar_nonprint(extra, c);
4598 }
4599 else
4600 {
4601 /* illegal tail byte */
4602 mb_l = 2;
4603 STRCPY(extra, "XX");
4604 }
4605 p_extra = extra;
4606 n_extra = (int)STRLEN(extra) - 1;
4607 c_extra = NUL;
4608 c = *p_extra++;
4609 if (area_attr == 0 && search_attr == 0)
4610 {
4611 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004612 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 saved_attr2 = char_attr; /* save current attr */
4614 }
4615 mb_c = c;
4616 }
4617 }
4618 }
4619 /* If a double-width char doesn't fit display a '>' in the
4620 * last column; the character is displayed at the start of the
4621 * next line. */
4622 if ((
4623# ifdef FEAT_RIGHTLEFT
4624 wp->w_p_rl ? (col <= 0) :
4625# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004626 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 && (*mb_char2cells)(mb_c) == 2)
4628 {
4629 c = '>';
4630 mb_c = c;
4631 mb_utf8 = FALSE;
4632 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004633 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 /* Put pointer back so that the character will be
4635 * displayed at the start of the next line. */
4636 --ptr;
4637 }
4638 else if (*ptr != NUL)
4639 ptr += mb_l - 1;
4640
4641 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004642 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004643 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004644 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004647 c_extra = MB_FILLER_CHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 c = ' ';
4649 if (area_attr == 0 && search_attr == 0)
4650 {
4651 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004652 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 saved_attr2 = char_attr; /* save current attr */
4654 }
4655 mb_c = c;
4656 mb_utf8 = FALSE;
4657 mb_l = 1;
4658 }
4659
4660 }
4661#endif
4662 ++ptr;
4663
4664 if (extra_check)
4665 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004666#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004667 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004668#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004669
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004670#ifdef FEAT_TERMINAL
4671 if (get_term_attr)
4672 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004673 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004674
4675 if (!attr_pri)
4676 char_attr = syntax_attr;
4677 else
4678 char_attr = hl_combine_attr(syntax_attr, char_attr);
4679 }
4680#endif
4681
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004682#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004683 // Get syntax attribute, unless still at the start of the line
4684 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004685 v = (long)(ptr - line);
4686 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
4688 /* Get the syntax attribute for the character. If there
4689 * is an error, disable syntax highlighting. */
4690 save_did_emsg = did_emsg;
4691 did_emsg = FALSE;
4692
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004694# ifdef FEAT_SPELL
4695 has_spell ? &can_spell :
4696# endif
4697 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
4699 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004700 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004701 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004702 has_syntax = FALSE;
4703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 else
4705 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004706#ifdef SYN_TIME_LIMIT
4707 if (wp->w_s->b_syn_slow)
4708 has_syntax = FALSE;
4709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710
4711 /* Need to get the line again, a multi-line regexp may
4712 * have made it invalid. */
4713 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4714 ptr = line + v;
4715
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004716# ifdef FEAT_TEXT_PROP
4717 // Text properties overrule syntax highlighting.
4718 if (text_prop_attr == 0)
4719#endif
4720 {
4721 if (!attr_pri)
4722 char_attr = syntax_attr;
4723 else
4724 char_attr = hl_combine_attr(syntax_attr, char_attr);
4725 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004726# ifdef FEAT_CONCEAL
4727 /* no concealing past the end of the line, it interferes
4728 * with line highlighting */
4729 if (c == NUL)
4730 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004731 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004732 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004735#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004736
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004737#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004738 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004739 * Only do this when there is no syntax highlighting, the
4740 * @Spell cluster is not used or the current syntax item
4741 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004742 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004743 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004744 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004745 if (c != 0 && (
4746# ifdef FEAT_SYN_HL
4747 !has_syntax ||
4748# endif
4749 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004750 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004751 char_u *prev_ptr, *p;
4752 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004753 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004754# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004755 if (has_mbyte)
4756 {
4757 prev_ptr = ptr - mb_l;
4758 v -= mb_l - 1;
4759 }
4760 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004761# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004762 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004763
4764 /* Use nextline[] if possible, it has the start of the
4765 * next line concatenated. */
4766 if ((prev_ptr - line) - nextlinecol >= 0)
4767 p = nextline + (prev_ptr - line) - nextlinecol;
4768 else
4769 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004770 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004771 len = spell_check(wp, p, &spell_hlf, &cap_col,
4772 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004773 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004774
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004775 /* In Insert mode only highlight a word that
4776 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004777 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004778 && (State & INSERT) != 0
4779 && wp->w_cursor.lnum == lnum
4780 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004781 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004782 && wp->w_cursor.col < (colnr_T)word_end)
4783 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004784 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004785 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004786 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004787
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004788 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004789 && (p - nextline) + len > nextline_idx)
4790 {
4791 /* Remember that the good word continues at the
4792 * start of the next line. */
4793 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004794 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004795 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004796
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004797 /* Turn index into actual attributes. */
4798 if (spell_hlf != HLF_COUNT)
4799 spell_attr = highlight_attr[spell_hlf];
4800
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004801 if (cap_col > 0)
4802 {
4803 if (p != prev_ptr
4804 && (p - nextline) + cap_col >= nextline_idx)
4805 {
4806 /* Remember that the word in the next line
4807 * must start with a capital. */
4808 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004809 cap_col = (int)((p - nextline) + cap_col
4810 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004811 }
4812 else
4813 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004814 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004815 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004816 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817 }
4818 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004819 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004820 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004821 char_attr = hl_combine_attr(char_attr, spell_attr);
4822 else
4823 char_attr = hl_combine_attr(spell_attr, char_attr);
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#endif
4826#ifdef FEAT_LINEBREAK
4827 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004828 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004830 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004831 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004833# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004834 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004835# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004836 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004838 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004840 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004841
Bram Moolenaar597a4222014-06-25 14:39:50 +02004842 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004843 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004844 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004845 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004846# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004847 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004848 wp->w_buffer->b_p_vts_array) - 1;
4849# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004850 n_extra = (int)wp->w_buffer->b_p_ts
4851 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004852# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004853
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004854# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004855 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004856# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004858# endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01004859 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004860 {
4861#ifdef FEAT_CONCEAL
4862 if (c == TAB)
4863 /* See "Tab alignment" below. */
4864 FIX_FOR_BOGUSCOLS;
4865#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004866 if (!wp->w_p_list)
4867 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870#endif
4871
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004872 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4873 */
4874 if (wp->w_p_list
4875 && (((c == 160
4876#ifdef FEAT_MBYTE
4877 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4878#endif
4879 ) && lcs_nbsp)
4880 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4881 {
4882 c = (c == ' ') ? lcs_space : lcs_nbsp;
4883 if (area_attr == 0 && search_attr == 0)
4884 {
4885 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004886 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004887 saved_attr2 = char_attr; /* save current attr */
4888 }
4889#ifdef FEAT_MBYTE
4890 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004891 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004892 {
4893 mb_utf8 = TRUE;
4894 u8cc[0] = 0;
4895 c = 0xc0;
4896 }
4897 else
4898 mb_utf8 = FALSE;
4899#endif
4900 }
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4903 {
4904 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004905 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004908 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 saved_attr2 = char_attr; /* save current attr */
4910 }
4911#ifdef FEAT_MBYTE
4912 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004913 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
4915 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004916 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004917 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
4920 mb_utf8 = FALSE;
4921#endif
4922 }
4923 }
4924
4925 /*
4926 * Handling of non-printable characters.
4927 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004928 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 {
4930 /*
4931 * when getting a character from the file, we may have to
4932 * turn it into something else on the way to putting it
4933 * into "ScreenLines".
4934 */
4935 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4936 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004937 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004938 long vcol_adjusted = vcol; /* removed showbreak length */
4939#ifdef FEAT_LINEBREAK
4940 /* only adjust the tab_len, when at the first column
4941 * after the showbreak value was drawn */
4942 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4943 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004946#ifdef FEAT_VARTABS
4947 tab_len = tabstop_padding(vcol_adjusted,
4948 wp->w_buffer->b_p_ts,
4949 wp->w_buffer->b_p_vts_array) - 1;
4950#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004951 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004952 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4953#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004954
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004955#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004956 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004957#endif
4958 /* tab amount depends on current column */
4959 n_extra = tab_len;
4960#ifdef FEAT_LINEBREAK
4961 else
4962 {
4963 char_u *p;
4964 int len = n_extra;
4965 int i;
4966 int saved_nextra = n_extra;
4967
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004968#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004969 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004970 /* there are characters to conceal */
4971 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004972 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4973 */
4974 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4975 && n_extra > tab_len)
4976 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004977#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004978
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004979 /* if n_extra > 0, it gives the number of chars, to
4980 * use for a tab, else we need to calculate the width
4981 * for a tab */
4982#ifdef FEAT_MBYTE
4983 len = (tab_len * mb_char2len(lcs_tab2));
4984 if (n_extra > 0)
4985 len += n_extra - tab_len;
4986#endif
4987 c = lcs_tab1;
4988 p = alloc((unsigned)(len + 1));
4989 vim_memset(p, ' ', len);
4990 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004991 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004992 p_extra_free = p;
4993 for (i = 0; i < tab_len; i++)
4994 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004995 if (*p == NUL)
4996 {
4997 tab_len = i;
4998 break;
4999 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005000#ifdef FEAT_MBYTE
5001 mb_char2bytes(lcs_tab2, p);
5002 p += mb_char2len(lcs_tab2);
5003 n_extra += mb_char2len(lcs_tab2)
5004 - (saved_nextra > 0 ? 1 : 0);
5005#else
5006 p[i] = lcs_tab2;
5007#endif
5008 }
5009 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005010#ifdef FEAT_CONCEAL
5011 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5012 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005013 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005014 n_extra -= vcol_off;
5015#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005016 }
5017#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005018#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005019 {
5020 int vc_saved = vcol_off;
5021
5022 /* Tab alignment should be identical regardless of
5023 * 'conceallevel' value. So tab compensates of all
5024 * previous concealed characters, and thus resets
5025 * vcol_off and boguscols accumulated so far in the
5026 * line. Note that the tab can be longer than
5027 * 'tabstop' when there are concealed characters. */
5028 FIX_FOR_BOGUSCOLS;
5029
5030 /* Make sure, the highlighting for the tab char will be
5031 * correctly set further below (effectively reverts the
5032 * FIX_FOR_BOGSUCOLS macro */
5033 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005034 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005035 tab_len += vc_saved;
5036 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#ifdef FEAT_MBYTE
5039 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5040#endif
5041 if (wp->w_p_list)
5042 {
5043 c = lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005044#ifdef FEAT_LINEBREAK
5045 if (wp->w_p_lbr)
5046 c_extra = NUL; /* using p_extra from above */
5047 else
5048#endif
5049 c_extra = lcs_tab2;
5050 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005051 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 saved_attr2 = char_attr; /* save current attr */
5053#ifdef FEAT_MBYTE
5054 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005055 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005058 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061#endif
5062 }
5063 else
5064 {
5065 c_extra = ' ';
5066 c = ' ';
5067 }
5068 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005069 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005070 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005071 || ((fromcol >= 0 || fromcol_prev >= 0)
5072 && tocol > vcol
5073 && VIsual_mode != Ctrl_V
5074 && (
5075# ifdef FEAT_RIGHTLEFT
5076 wp->w_p_rl ? (col >= 0) :
5077# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005078 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005079 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005080 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005081 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005082 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005084 /* Display a '$' after the line or highlight an extra
5085 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5087 /* For a diff line the highlighting continues after the
5088 * "$". */
5089 if (
5090# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005091 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092# ifdef LINE_ATTR
5093 &&
5094# endif
5095# endif
5096# ifdef LINE_ATTR
5097 line_attr == 0
5098# endif
5099 )
5100#endif
5101 {
5102#ifdef FEAT_VIRTUALEDIT
5103 /* In virtualedit, visual selections may extend
5104 * beyond end of line. */
5105 if (area_highlighting && virtual_active()
5106 && tocol != MAXCOL && vcol < tocol)
5107 n_extra = 0;
5108 else
5109#endif
5110 {
5111 p_extra = at_end_str;
5112 n_extra = 1;
5113 c_extra = NUL;
5114 }
5115 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005116 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005117 c = lcs_eol;
5118 else
5119 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 lcs_eol_one = -1;
5121 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005122 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005124 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 n_attr = 1;
5126 }
5127#ifdef FEAT_MBYTE
5128 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005129 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
5131 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005132 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005133 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 else
5136 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5137#endif
5138 }
5139 else if (c != NUL)
5140 {
5141 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005142 if (n_extra == 0)
5143 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144#ifdef FEAT_RIGHTLEFT
5145 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5146 rl_mirror(p_extra); /* reverse "<12>" */
5147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 c_extra = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005149#ifdef FEAT_LINEBREAK
5150 if (wp->w_p_lbr)
5151 {
5152 char_u *p;
5153
5154 c = *p_extra;
5155 p = alloc((unsigned)n_extra + 1);
5156 vim_memset(p, ' ', n_extra);
5157 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5158 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005159 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005160 p_extra_free = p_extra = p;
5161 }
5162 else
5163#endif
5164 {
5165 n_extra = byte2cells(c) - 1;
5166 c = *p_extra++;
5167 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005168 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
5170 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005171 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 saved_attr2 = char_attr; /* save current attr */
5173 }
5174#ifdef FEAT_MBYTE
5175 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5176#endif
5177 }
5178#ifdef FEAT_VIRTUALEDIT
5179 else if (VIsual_active
5180 && (VIsual_mode == Ctrl_V
5181 || VIsual_mode == 'v')
5182 && virtual_active()
5183 && tocol != MAXCOL
5184 && vcol < tocol
5185 && (
5186# ifdef FEAT_RIGHTLEFT
5187 wp->w_p_rl ? (col >= 0) :
5188# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005189 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 c = ' ';
5192 --ptr; /* put it back at the NUL */
5193 }
5194#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005195#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 else if ((
5197# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005198 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005200# ifdef FEAT_TERMINAL
5201 term_attr != 0 ||
5202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ) && (
5205# ifdef FEAT_RIGHTLEFT
5206 wp->w_p_rl ? (col >= 0) :
5207# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005208 (col
5209# ifdef FEAT_CONCEAL
5210 - boguscols
5211# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005212 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 /* Highlight until the right side of the window */
5215 c = ' ';
5216 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005217
5218 /* Remember we do the char for line highlighting. */
5219 ++did_line_attr;
5220
5221 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005222 if (line_attr != 0 && char_attr == search_attr
5223 && (did_line_attr > 1
5224 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005225 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226# ifdef FEAT_DIFF
5227 if (diff_hlf == HLF_TXD)
5228 {
5229 diff_hlf = HLF_CHD;
5230 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005231 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005232 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005233 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5234 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005235 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005239# ifdef FEAT_TERMINAL
5240 if (term_attr != 0)
5241 {
5242 char_attr = term_attr;
5243 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5244 char_attr = hl_combine_attr(char_attr,
5245 HL_ATTR(HLF_CUL));
5246 }
5247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249#endif
5250 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005251
5252#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005253 if ( wp->w_p_cole > 0
5254 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005255 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005256 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005257 && !(lnum_in_visual_area
5258 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005259 {
5260 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005261 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005262 && (syn_get_sub_char() != NUL || match_conc
5263 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005264 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005265 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005266 /* First time at this concealed item: display one
5267 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005268 if (match_conc)
5269 c = match_conc;
5270 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005271 c = syn_get_sub_char();
5272 else if (lcs_conceal != NUL)
5273 c = lcs_conceal;
5274 else
5275 c = ' ';
5276
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005277 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005278
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005279 if (n_extra > 0)
5280 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005281 vcol += n_extra;
5282 if (wp->w_p_wrap && n_extra > 0)
5283 {
5284# ifdef FEAT_RIGHTLEFT
5285 if (wp->w_p_rl)
5286 {
5287 col -= n_extra;
5288 boguscols -= n_extra;
5289 }
5290 else
5291# endif
5292 {
5293 boguscols += n_extra;
5294 col += n_extra;
5295 }
5296 }
5297 n_extra = 0;
5298 n_attr = 0;
5299 }
5300 else if (n_skip == 0)
5301 {
5302 is_concealing = TRUE;
5303 n_skip = 1;
5304 }
5305# ifdef FEAT_MBYTE
5306 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005307 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005308 {
5309 mb_utf8 = TRUE;
5310 u8cc[0] = 0;
5311 c = 0xc0;
5312 }
5313 else
5314 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5315# endif
5316 }
5317 else
5318 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005319 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005320 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 }
5322#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005325#ifdef FEAT_CONCEAL
5326 /* In the cursor line and we may be concealing characters: correct
5327 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005328 if (!did_wcol && draw_state == WL_LINE
5329 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005330 && conceal_cursor_line(wp)
5331 && (int)wp->w_virtcol <= vcol + n_skip)
5332 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005333# ifdef FEAT_RIGHTLEFT
5334 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005335 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005336 else
5337# endif
5338 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005339 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005340 did_wcol = TRUE;
5341 }
5342#endif
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 /* Don't override visual selection highlighting. */
5345 if (n_attr > 0
5346 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005347 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 char_attr = extra_attr;
5349
Bram Moolenaar81695252004-12-29 20:58:21 +00005350#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 /* XIM don't send preedit_start and preedit_end, but they send
5352 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5353 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005354 if (p_imst == IM_ON_THE_SPOT
5355 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005356 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 && (State & INSERT)
5358 && !p_imdisable
5359 && im_is_preediting()
5360 && draw_state == WL_LINE)
5361 {
5362 colnr_T tcol;
5363
5364 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005365 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 else
5367 tcol = preedit_end_col;
5368 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5369 {
5370 if (feedback_old_attr < 0)
5371 {
5372 feedback_col = 0;
5373 feedback_old_attr = char_attr;
5374 }
5375 char_attr = im_get_feedback_attr(feedback_col);
5376 if (char_attr < 0)
5377 char_attr = feedback_old_attr;
5378 feedback_col++;
5379 }
5380 else if (feedback_old_attr >= 0)
5381 {
5382 char_attr = feedback_old_attr;
5383 feedback_old_attr = -1;
5384 feedback_col = 0;
5385 }
5386 }
5387#endif
5388 /*
5389 * Handle the case where we are in column 0 but not on the first
5390 * character of the line and the user wants us to show us a
5391 * special character (via 'listchars' option "precedes:<char>".
5392 */
5393 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005394 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5396#ifdef FEAT_DIFF
5397 && filler_todo <= 0
5398#endif
5399 && draw_state > WL_NR
5400 && c != NUL)
5401 {
5402 c = lcs_prec;
5403 lcs_prec_todo = NUL;
5404#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005405 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5406 {
5407 /* Double-width character being overwritten by the "precedes"
5408 * character, need to fill up half the character. */
5409 c_extra = MB_FILLER_CHAR;
5410 n_extra = 1;
5411 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005412 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005415 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005418 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005419 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421 else
5422 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5423#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005424 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005427 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 n_attr3 = 1;
5429 }
5430 }
5431
5432 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005433 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005435 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005436#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005437 || did_line_attr == 1
5438#endif
5439 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005441#ifdef FEAT_SEARCH_EXTRA
5442 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005443
5444 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005445 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005446 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005447#endif
5448
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005449 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * highlight match at end of line. If it's beyond the last
5451 * char on the screen, just overwrite that one (tricky!) Not
5452 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005453#ifdef FEAT_SEARCH_EXTRA
5454 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005455 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005456 prevcol_hl_flag = TRUE;
5457 else
5458 {
5459 cur = wp->w_match_head;
5460 while (cur != NULL)
5461 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005462 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005463 {
5464 prevcol_hl_flag = TRUE;
5465 break;
5466 }
5467 cur = cur->next;
5468 }
5469 }
5470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005472 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005473 && (VIsual_mode != Ctrl_V
5474 || lnum == VIsual.lnum
5475 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005476 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#ifdef FEAT_SEARCH_EXTRA
5478 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005479 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005480# ifdef FEAT_SYN_HL
5481 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5482 && !(wp == curwin && VIsual_active))
5483# endif
5484# ifdef FEAT_DIFF
5485 && diff_hlf == (hlf_T)0
5486# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005487# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005488 && did_line_attr <= 1
5489# endif
5490 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#endif
5492 ))
5493 {
5494 int n = 0;
5495
5496#ifdef FEAT_RIGHTLEFT
5497 if (wp->w_p_rl)
5498 {
5499 if (col < 0)
5500 n = 1;
5501 }
5502 else
5503#endif
5504 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005505 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 n = -1;
5507 }
5508 if (n != 0)
5509 {
5510 /* At the window boundary, highlight the last character
5511 * instead (better than nothing). */
5512 off += n;
5513 col += n;
5514 }
5515 else
5516 {
5517 /* Add a blank character to highlight. */
5518 ScreenLines[off] = ' ';
5519#ifdef FEAT_MBYTE
5520 if (enc_utf8)
5521 ScreenLinesUC[off] = 0;
5522#endif
5523 }
5524#ifdef FEAT_SEARCH_EXTRA
5525 if (area_attr == 0)
5526 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005527 /* Use attributes from match with highest priority among
5528 * 'search_hl' and the match list. */
5529 char_attr = search_hl.attr;
5530 cur = wp->w_match_head;
5531 shl_flag = FALSE;
5532 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005533 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005534 if (shl_flag == FALSE
5535 && ((cur != NULL
5536 && cur->priority > SEARCH_HL_PRIORITY)
5537 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005538 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005539 shl = &search_hl;
5540 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005541 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005542 else
5543 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005544 if ((ptr - line) - 1 == (long)shl->startcol
5545 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005546 char_attr = shl->attr;
5547 if (shl != &search_hl && cur != NULL)
5548 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551#endif
5552 ScreenAttrs[off] = char_attr;
5553#ifdef FEAT_RIGHTLEFT
5554 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005557 --off;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else
5560#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005563 ++off;
5564 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005565 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005566#ifdef FEAT_SYN_HL
5567 eol_hl_off = 1;
5568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar91170f82006-05-05 21:15:17 +00005572 /*
5573 * At end of the text line.
5574 */
5575 if (c == NUL)
5576 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005577#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005578 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005579 if (wp->w_p_wrap)
5580 v = wp->w_skipcol;
5581 else
5582 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005583
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005584 /* check if line ends before left margin */
5585 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005586 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005587#ifdef FEAT_CONCEAL
5588 /* Get rid of the boguscols now, we want to draw until the right
5589 * edge for 'cursorcolumn'. */
5590 col -= boguscols;
5591 boguscols = 0;
5592#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005593
5594 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005595 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005596
5597 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005598 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5599 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005600 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005601 && lnum != wp->w_cursor.lnum)
5602 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005603# ifdef FEAT_RIGHTLEFT
5604 && !wp->w_p_rl
5605# endif
5606 )
5607 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005608 int rightmost_vcol = 0;
5609 int i;
5610
5611 if (wp->w_p_cuc)
5612 rightmost_vcol = wp->w_virtcol;
5613 if (draw_color_col)
5614 /* determine rightmost colorcolumn to possibly draw */
5615 for (i = 0; color_cols[i] >= 0; ++i)
5616 if (rightmost_vcol < color_cols[i])
5617 rightmost_vcol = color_cols[i];
5618
Bram Moolenaar02631462017-09-22 15:20:32 +02005619 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005620 {
5621 ScreenLines[off] = ' ';
5622#ifdef FEAT_MBYTE
5623 if (enc_utf8)
5624 ScreenLinesUC[off] = 0;
5625#endif
5626 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005627 if (draw_color_col)
5628 draw_color_col = advance_color_col(VCOL_HLC,
5629 &color_cols);
5630
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005631 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005632 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005633 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005634 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005635 else
5636 ScreenAttrs[off++] = 0;
5637
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005638 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005639 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005640
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005641 ++vcol;
5642 }
5643 }
5644#endif
5645
Bram Moolenaar53f81742017-09-22 14:35:51 +02005646 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005647 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 row++;
5649
5650 /*
5651 * Update w_cline_height and w_cline_folded if the cursor line was
5652 * updated (saves a call to plines() later).
5653 */
5654 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5655 {
5656 curwin->w_cline_row = startrow;
5657 curwin->w_cline_height = row - startrow;
5658#ifdef FEAT_FOLDING
5659 curwin->w_cline_folded = FALSE;
5660#endif
5661 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5662 }
5663
5664 break;
5665 }
5666
5667 /* line continues beyond line end */
5668 if (lcs_ext
5669 && !wp->w_p_wrap
5670#ifdef FEAT_DIFF
5671 && filler_todo <= 0
5672#endif
5673 && (
5674#ifdef FEAT_RIGHTLEFT
5675 wp->w_p_rl ? col == 0 :
5676#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005677 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005679 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5681 {
5682 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005683 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684#ifdef FEAT_MBYTE
5685 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005686 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 {
5688 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005689 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005690 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else
5693 mb_utf8 = FALSE;
5694#endif
5695 }
5696
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005697#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005698 /* advance to the next 'colorcolumn' */
5699 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005700 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005701
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005702 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005703 * highlight the cursor position itself.
5704 * Also highlight the 'colorcolumn' if it is different than
5705 * 'cursorcolumn' */
5706 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005707 if (draw_state == WL_LINE && !lnum_in_visual_area
5708 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005709 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005710 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005711 && lnum != wp->w_cursor.lnum)
5712 {
5713 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005714 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005715 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005716 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005717 {
5718 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005719 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005720 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005721 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005722#endif
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 /*
5725 * Store character to be displayed.
5726 * Skip characters that are left of the screen for 'nowrap'.
5727 */
5728 vcol_prev = vcol;
5729 if (draw_state < WL_LINE || n_skip <= 0)
5730 {
5731 /*
5732 * Store the character.
5733 */
5734#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5735 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5736 {
5737 /* A double-wide character is: put first halve in left cell. */
5738 --off;
5739 --col;
5740 }
5741#endif
5742 ScreenLines[off] = c;
5743#ifdef FEAT_MBYTE
5744 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005745 {
5746 if ((mb_c & 0xff00) == 0x8e00)
5747 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 else if (enc_utf8)
5751 {
5752 if (mb_utf8)
5753 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005754 int i;
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005757 if ((c & 0xff) == 0)
5758 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005759 for (i = 0; i < Screen_mco; ++i)
5760 {
5761 ScreenLinesC[i][off] = u8cc[i];
5762 if (u8cc[i] == 0)
5763 break;
5764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766 else
5767 ScreenLinesUC[off] = 0;
5768 }
5769 if (multi_attr)
5770 {
5771 ScreenAttrs[off] = multi_attr;
5772 multi_attr = 0;
5773 }
5774 else
5775#endif
5776 ScreenAttrs[off] = char_attr;
5777
5778#ifdef FEAT_MBYTE
5779 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5780 {
5781 /* Need to fill two screen columns. */
5782 ++off;
5783 ++col;
5784 if (enc_utf8)
5785 /* UTF-8: Put a 0 in the second screen char. */
5786 ScreenLines[off] = 0;
5787 else
5788 /* DBCS: Put second byte in the second screen char. */
5789 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005790 if (draw_state > WL_NR
5791#ifdef FEAT_DIFF
5792 && filler_todo <= 0
5793#endif
5794 )
5795 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 /* When "tocol" is halfway a character, set it to the end of
5797 * the character, otherwise highlighting won't stop. */
5798 if (tocol == vcol)
5799 ++tocol;
5800#ifdef FEAT_RIGHTLEFT
5801 if (wp->w_p_rl)
5802 {
5803 /* now it's time to backup one cell */
5804 --off;
5805 --col;
5806 }
5807#endif
5808 }
5809#endif
5810#ifdef FEAT_RIGHTLEFT
5811 if (wp->w_p_rl)
5812 {
5813 --off;
5814 --col;
5815 }
5816 else
5817#endif
5818 {
5819 ++off;
5820 ++col;
5821 }
5822 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005823#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005824 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005825 {
5826 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005827 ++vcol_off;
5828 if (n_extra > 0)
5829 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005830 if (wp->w_p_wrap)
5831 {
5832 /*
5833 * Special voodoo required if 'wrap' is on.
5834 *
5835 * Advance the column indicator to force the line
5836 * drawing to wrap early. This will make the line
5837 * take up the same screen space when parts are concealed,
5838 * so that cursor line computations aren't messed up.
5839 *
5840 * To avoid the fictitious advance of 'col' causing
5841 * trailing junk to be written out of the screen line
5842 * we are building, 'boguscols' keeps track of the number
5843 * of bad columns we have advanced.
5844 */
5845 if (n_extra > 0)
5846 {
5847 vcol += n_extra;
5848# ifdef FEAT_RIGHTLEFT
5849 if (wp->w_p_rl)
5850 {
5851 col -= n_extra;
5852 boguscols -= n_extra;
5853 }
5854 else
5855# endif
5856 {
5857 col += n_extra;
5858 boguscols += n_extra;
5859 }
5860 n_extra = 0;
5861 n_attr = 0;
5862 }
5863
5864
5865# ifdef FEAT_MBYTE
5866 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5867 {
5868 /* Need to fill two screen columns. */
5869# ifdef FEAT_RIGHTLEFT
5870 if (wp->w_p_rl)
5871 {
5872 --boguscols;
5873 --col;
5874 }
5875 else
5876# endif
5877 {
5878 ++boguscols;
5879 ++col;
5880 }
5881 }
5882# endif
5883
5884# ifdef FEAT_RIGHTLEFT
5885 if (wp->w_p_rl)
5886 {
5887 --boguscols;
5888 --col;
5889 }
5890 else
5891# endif
5892 {
5893 ++boguscols;
5894 ++col;
5895 }
5896 }
5897 else
5898 {
5899 if (n_extra > 0)
5900 {
5901 vcol += n_extra;
5902 n_extra = 0;
5903 n_attr = 0;
5904 }
5905 }
5906
5907 }
5908#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 else
5910 --n_skip;
5911
Bram Moolenaar64486672010-05-16 15:46:46 +02005912 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5913 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005914 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#ifdef FEAT_DIFF
5916 && filler_todo <= 0
5917#endif
5918 )
5919 ++vcol;
5920
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005921#ifdef FEAT_SYN_HL
5922 if (vcol_save_attr >= 0)
5923 char_attr = vcol_save_attr;
5924#endif
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 /* restore attributes after "predeces" in 'listchars' */
5927 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5928 char_attr = saved_attr3;
5929
5930 /* restore attributes after last 'listchars' or 'number' char */
5931 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5932 char_attr = saved_attr2;
5933
5934 /*
5935 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005936 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 */
5938 if ((
5939#ifdef FEAT_RIGHTLEFT
5940 wp->w_p_rl ? (col < 0) :
5941#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005942 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 && (*ptr != NUL
5944#ifdef FEAT_DIFF
5945 || filler_todo > 0
5946#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005947 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5949 )
5950 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005951#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005952 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005953 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954 boguscols = 0;
5955#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005956 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005957 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 ++row;
5960 ++screen_row;
5961
5962 /* When not wrapping and finished diff lines, or when displayed
5963 * '$' and highlighting until last column, break here. */
5964 if ((!wp->w_p_wrap
5965#ifdef FEAT_DIFF
5966 && filler_todo <= 0
5967#endif
5968 ) || lcs_eol_one == -1)
5969 break;
5970
5971 /* When the window is too narrow draw all "@" lines. */
5972 if (draw_state != WL_LINE
5973#ifdef FEAT_DIFF
5974 && filler_todo <= 0
5975#endif
5976 )
5977 {
5978 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 row = endrow;
5981 }
5982
5983 /* When line got too long for screen break here. */
5984 if (row == endrow)
5985 {
5986 ++row;
5987 break;
5988 }
5989
5990 if (screen_cur_row == screen_row - 1
5991#ifdef FEAT_DIFF
5992 && filler_todo <= 0
5993#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005994 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 /* Remember that the line wraps, used for modeless copy. */
5997 LineWraps[screen_row - 1] = TRUE;
5998
5999 /*
6000 * Special trick to make copy/paste of wrapped lines work with
6001 * xterm/screen: write an extra character beyond the end of
6002 * the line. This will work with all terminal types
6003 * (regardless of the xn,am settings).
6004 * Only do this on a fast tty.
6005 * Only do this if the cursor is on the current line
6006 * (something has been written in it).
6007 * Don't do this for the GUI.
6008 * Don't do this for double-width characters.
6009 * Don't do this for a window not at the right screen border.
6010 */
6011 if (p_tf
6012#ifdef FEAT_GUI
6013 && !gui.in_use
6014#endif
6015#ifdef FEAT_MBYTE
6016 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006017 && ((*mb_off2cells)(LineOffset[screen_row],
6018 LineOffset[screen_row] + screen_Columns)
6019 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006021 + (int)Columns - 2,
6022 LineOffset[screen_row] + screen_Columns)
6023 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024#endif
6025 )
6026 {
6027 /* First make sure we are at the end of the screen line,
6028 * then output the same character again to let the
6029 * terminal know about the wrap. If the terminal doesn't
6030 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006031 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 screen_char(LineOffset[screen_row - 1]
6033 + (unsigned)Columns - 1,
6034 screen_row - 1, (int)(Columns - 1));
6035
6036#ifdef FEAT_MBYTE
6037 /* When there is a multi-byte character, just output a
6038 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006039 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6040 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 out_char(' ');
6042 else
6043#endif
6044 out_char(ScreenLines[LineOffset[screen_row - 1]
6045 + (Columns - 1)]);
6046 /* force a redraw of the first char on the next line */
6047 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6048 screen_start(); /* don't know where cursor is now */
6049 }
6050 }
6051
6052 col = 0;
6053 off = (unsigned)(current_ScreenLine - ScreenLines);
6054#ifdef FEAT_RIGHTLEFT
6055 if (wp->w_p_rl)
6056 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006057 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 off += col;
6059 }
6060#endif
6061
6062 /* reset the drawing state for the start of a wrapped line */
6063 draw_state = WL_START;
6064 saved_n_extra = n_extra;
6065 saved_p_extra = p_extra;
6066 saved_c_extra = c_extra;
6067 saved_char_attr = char_attr;
6068 n_extra = 0;
6069 lcs_prec_todo = lcs_prec;
6070#ifdef FEAT_LINEBREAK
6071# ifdef FEAT_DIFF
6072 if (filler_todo <= 0)
6073# endif
6074 need_showbreak = TRUE;
6075#endif
6076#ifdef FEAT_DIFF
6077 --filler_todo;
6078 /* When the filler lines are actually below the last line of the
6079 * file, don't draw the line itself, break here. */
6080 if (filler_todo == 0 && wp->w_botfill)
6081 break;
6082#endif
6083 }
6084
6085 } /* for every character in the line */
6086
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006087#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006088 /* After an empty line check first word for capital. */
6089 if (*skipwhite(line) == NUL)
6090 {
6091 capcol_lnum = lnum + 1;
6092 cap_col = 0;
6093 }
6094#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006095#ifdef FEAT_TEXT_PROP
6096 vim_free(text_props);
6097 vim_free(text_prop_idxs);
6098#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006099
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006100 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 return row;
6102}
6103
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006104#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006105/*
6106 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006107 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006108 */
6109 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006110comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006111{
6112 int i;
6113
6114 for (i = 0; i < Screen_mco; ++i)
6115 {
6116 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6117 return TRUE;
6118 if (ScreenLinesC[i][off_from] == 0)
6119 break;
6120 }
6121 return FALSE;
6122}
6123#endif
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125/*
6126 * Check whether the given character needs redrawing:
6127 * - the (first byte of the) character is different
6128 * - the attributes are different
6129 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006130 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 */
6132 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006133char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
6135 if (cols > 0
6136 && ((ScreenLines[off_from] != ScreenLines[off_to]
6137 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6138
6139#ifdef FEAT_MBYTE
6140 || (enc_dbcs != 0
6141 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6142 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6143 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6144 : (cols > 1 && ScreenLines[off_from + 1]
6145 != ScreenLines[off_to + 1])))
6146 || (enc_utf8
6147 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6148 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006149 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006150 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6151 && ScreenLines[off_from + 1]
6152 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153#endif
6154 ))
6155 return TRUE;
6156 return FALSE;
6157}
6158
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006159#if defined(FEAT_TERMINAL) || defined(PROTO)
6160/*
6161 * Return the index in ScreenLines[] for the current screen line.
6162 */
6163 int
6164screen_get_current_line_off()
6165{
6166 return (int)(current_ScreenLine - ScreenLines);
6167}
6168#endif
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170/*
6171 * Move one "cooked" screen line to the screen, but only the characters that
6172 * have actually changed. Handle insert/delete character.
6173 * "coloff" gives the first column on the screen for this line.
6174 * "endcol" gives the columns where valid characters are.
6175 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6176 * needs to be cleared, negative otherwise.
6177 * "rlflag" is TRUE in a rightleft window:
6178 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6179 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6180 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006181 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006182screen_line(
6183 int row,
6184 int coloff,
6185 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006186 int clear_width,
6187 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 unsigned off_from;
6190 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006191#ifdef FEAT_MBYTE
6192 unsigned max_off_from;
6193 unsigned max_off_to;
6194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 int force = FALSE; /* force update rest of the line */
6198 int redraw_this /* bool: does character need redraw? */
6199#ifdef FEAT_GUI
6200 = TRUE /* For GUI when while-loop empty */
6201#endif
6202 ;
6203 int redraw_next; /* redraw_this for next character */
6204#ifdef FEAT_MBYTE
6205 int clear_next = FALSE;
6206 int char_cells; /* 1: normal char */
6207 /* 2: occupies two display cells */
6208# define CHAR_CELLS char_cells
6209#else
6210# define CHAR_CELLS 1
6211#endif
6212
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006213 /* Check for illegal row and col, just in case. */
6214 if (row >= Rows)
6215 row = Rows - 1;
6216 if (endcol > Columns)
6217 endcol = Columns;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219# ifdef FEAT_CLIPBOARD
6220 clip_may_clear_selection(row, row);
6221# endif
6222
6223 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6224 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006225#ifdef FEAT_MBYTE
6226 max_off_from = off_from + screen_Columns;
6227 max_off_to = LineOffset[row] + screen_Columns;
6228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230#ifdef FEAT_RIGHTLEFT
6231 if (rlflag)
6232 {
6233 /* Clear rest first, because it's left of the text. */
6234 if (clear_width > 0)
6235 {
6236 while (col <= endcol && ScreenLines[off_to] == ' '
6237 && ScreenAttrs[off_to] == 0
6238# ifdef FEAT_MBYTE
6239 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6240# endif
6241 )
6242 {
6243 ++off_to;
6244 ++col;
6245 }
6246 if (col <= endcol)
6247 screen_fill(row, row + 1, col + coloff,
6248 endcol + coloff + 1, ' ', ' ', 0);
6249 }
6250 col = endcol + 1;
6251 off_to = LineOffset[row] + col + coloff;
6252 off_from += col;
6253 endcol = (clear_width > 0 ? clear_width : -clear_width);
6254 }
6255#endif /* FEAT_RIGHTLEFT */
6256
6257 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6258
6259 while (col < endcol)
6260 {
6261#ifdef FEAT_MBYTE
6262 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006263 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 else
6265 char_cells = 1;
6266#endif
6267
6268 redraw_this = redraw_next;
6269 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6270 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6271
6272#ifdef FEAT_GUI
6273 /* If the next character was bold, then redraw the current character to
6274 * remove any pixels that might have spilt over into us. This only
6275 * happens in the GUI.
6276 */
6277 if (redraw_next && gui.in_use)
6278 {
6279 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006280 if (hl > HL_ALL)
6281 hl = syn_attr2attr(hl);
6282 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 redraw_this = TRUE;
6284 }
6285#endif
6286
6287 if (redraw_this)
6288 {
6289 /*
6290 * Special handling when 'xs' termcap flag set (hpterm):
6291 * Attributes for characters are stored at the position where the
6292 * cursor is when writing the highlighting code. The
6293 * start-highlighting code must be written with the cursor on the
6294 * first highlighted character. The stop-highlighting code must
6295 * be written with the cursor just after the last highlighted
6296 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006297 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 * to clear the rest of the line, and force redrawing it
6299 * completely.
6300 */
6301 if ( p_wiv
6302 && !force
6303#ifdef FEAT_GUI
6304 && !gui.in_use
6305#endif
6306 && ScreenAttrs[off_to] != 0
6307 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6308 {
6309 /*
6310 * Need to remove highlighting attributes here.
6311 */
6312 windgoto(row, col + coloff);
6313 out_str(T_CE); /* clear rest of this screen line */
6314 screen_start(); /* don't know where cursor is now */
6315 force = TRUE; /* force redraw of rest of the line */
6316 redraw_next = TRUE; /* or else next char would miss out */
6317
6318 /*
6319 * If the previous character was highlighted, need to stop
6320 * highlighting at this character.
6321 */
6322 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6323 {
6324 screen_attr = ScreenAttrs[off_to - 1];
6325 term_windgoto(row, col + coloff);
6326 screen_stop_highlight();
6327 }
6328 else
6329 screen_attr = 0; /* highlighting has stopped */
6330 }
6331#ifdef FEAT_MBYTE
6332 if (enc_dbcs != 0)
6333 {
6334 /* Check if overwriting a double-byte with a single-byte or
6335 * the other way around requires another character to be
6336 * redrawn. For UTF-8 this isn't needed, because comparing
6337 * ScreenLinesUC[] is sufficient. */
6338 if (char_cells == 1
6339 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006340 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 /* Writing a single-cell character over a double-cell
6343 * character: need to redraw the next cell. */
6344 ScreenLines[off_to + 1] = 0;
6345 redraw_next = TRUE;
6346 }
6347 else if (char_cells == 2
6348 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006349 && (*mb_off2cells)(off_to, max_off_to) == 1
6350 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 /* Writing the second half of a double-cell character over
6353 * a double-cell character: need to redraw the second
6354 * cell. */
6355 ScreenLines[off_to + 2] = 0;
6356 redraw_next = TRUE;
6357 }
6358
6359 if (enc_dbcs == DBCS_JPNU)
6360 ScreenLines2[off_to] = ScreenLines2[off_from];
6361 }
6362 /* When writing a single-width character over a double-width
6363 * character and at the end of the redrawn text, need to clear out
6364 * the right halve of the old character.
6365 * Also required when writing the right halve of a double-width
6366 * char over the left halve of an existing one. */
6367 if (has_mbyte && col + char_cells == endcol
6368 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006369 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 && (*mb_off2cells)(off_to, max_off_to) == 1
6372 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 clear_next = TRUE;
6374#endif
6375
6376 ScreenLines[off_to] = ScreenLines[off_from];
6377#ifdef FEAT_MBYTE
6378 if (enc_utf8)
6379 {
6380 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6381 if (ScreenLinesUC[off_from] != 0)
6382 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006383 int i;
6384
6385 for (i = 0; i < Screen_mco; ++i)
6386 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 }
6388 }
6389 if (char_cells == 2)
6390 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6391#endif
6392
6393#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006394 /* The bold trick makes a single column of pixels appear in the
6395 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 * character should be redrawn too. This happens for our own GUI
6397 * and for some xterms. */
6398 if (
6399# ifdef FEAT_GUI
6400 gui.in_use
6401# endif
6402# if defined(FEAT_GUI) && defined(UNIX)
6403 ||
6404# endif
6405# ifdef UNIX
6406 term_is_xterm
6407# endif
6408 )
6409 {
6410 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006411 if (hl > HL_ALL)
6412 hl = syn_attr2attr(hl);
6413 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 redraw_next = TRUE;
6415 }
6416#endif
6417 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6418#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006419 /* For simplicity set the attributes of second half of a
6420 * double-wide character equal to the first half. */
6421 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006423
6424 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 else
6427#endif
6428 screen_char(off_to, row, col + coloff);
6429 }
6430 else if ( p_wiv
6431#ifdef FEAT_GUI
6432 && !gui.in_use
6433#endif
6434 && col + coloff > 0)
6435 {
6436 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6437 {
6438 /*
6439 * Don't output stop-highlight when moving the cursor, it will
6440 * stop the highlighting when it should continue.
6441 */
6442 screen_attr = 0;
6443 }
6444 else if (screen_attr != 0)
6445 screen_stop_highlight();
6446 }
6447
6448 off_to += CHAR_CELLS;
6449 off_from += CHAR_CELLS;
6450 col += CHAR_CELLS;
6451 }
6452
6453#ifdef FEAT_MBYTE
6454 if (clear_next)
6455 {
6456 /* Clear the second half of a double-wide character of which the left
6457 * half was overwritten with a single-wide character. */
6458 ScreenLines[off_to] = ' ';
6459 if (enc_utf8)
6460 ScreenLinesUC[off_to] = 0;
6461 screen_char(off_to, row, col + coloff);
6462 }
6463#endif
6464
6465 if (clear_width > 0
6466#ifdef FEAT_RIGHTLEFT
6467 && !rlflag
6468#endif
6469 )
6470 {
6471#ifdef FEAT_GUI
6472 int startCol = col;
6473#endif
6474
6475 /* blank out the rest of the line */
6476 while (col < clear_width && ScreenLines[off_to] == ' '
6477 && ScreenAttrs[off_to] == 0
6478#ifdef FEAT_MBYTE
6479 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6480#endif
6481 )
6482 {
6483 ++off_to;
6484 ++col;
6485 }
6486 if (col < clear_width)
6487 {
6488#ifdef FEAT_GUI
6489 /*
6490 * In the GUI, clearing the rest of the line may leave pixels
6491 * behind if the first character cleared was bold. Some bold
6492 * fonts spill over the left. In this case we redraw the previous
6493 * character too. If we didn't skip any blanks above, then we
6494 * only redraw if the character wasn't already redrawn anyway.
6495 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006496 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 {
6498 hl = ScreenAttrs[off_to];
6499 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006500 {
6501 int prev_cells = 1;
6502# ifdef FEAT_MBYTE
6503 if (enc_utf8)
6504 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6505 * that its width is 2. */
6506 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6507 else if (enc_dbcs != 0)
6508 {
6509 /* find previous character by counting from first
6510 * column and get its width. */
6511 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006512 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006513
6514 while (off < off_to)
6515 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006516 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006517 off += prev_cells;
6518 }
6519 }
6520
6521 if (enc_dbcs != 0 && prev_cells > 1)
6522 screen_char_2(off_to - prev_cells, row,
6523 col + coloff - prev_cells);
6524 else
6525# endif
6526 screen_char(off_to - prev_cells, row,
6527 col + coloff - prev_cells);
6528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 }
6530#endif
6531 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6532 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 off_to += clear_width - col;
6534 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536 }
6537
6538 if (clear_width > 0)
6539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 /* For a window that's left of another, draw the separator char. */
6541 if (col + coloff < Columns)
6542 {
6543 int c;
6544
6545 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006546 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006547#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006548 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6549 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 || ScreenAttrs[off_to] != hl)
6552 {
6553 ScreenLines[off_to] = c;
6554 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006555#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (enc_utf8)
6557 {
6558 if (c >= 0x80)
6559 {
6560 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006561 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563 else
6564 ScreenLinesUC[off_to] = 0;
6565 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 screen_char(off_to, row, col + coloff);
6568 }
6569 }
6570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 LineWraps[row] = FALSE;
6572 }
6573}
6574
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006575#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006577 * Mirror text "str" for right-left displaying.
6578 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006580 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006581rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 char_u *p1, *p2;
6584 int t;
6585
6586 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6587 {
6588 t = *p1;
6589 *p1 = *p2;
6590 *p2 = t;
6591 }
6592}
6593#endif
6594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595/*
6596 * mark all status lines for redraw; used after first :cd
6597 */
6598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006599status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 win_T *wp;
6602
Bram Moolenaar29323592016-07-24 22:04:11 +02006603 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 if (wp->w_status_height)
6605 {
6606 wp->w_redr_status = TRUE;
6607 redraw_later(VALID);
6608 }
6609}
6610
6611/*
6612 * mark all status lines of the current buffer for redraw
6613 */
6614 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006615status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 win_T *wp;
6618
Bram Moolenaar29323592016-07-24 22:04:11 +02006619 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6621 {
6622 wp->w_redr_status = TRUE;
6623 redraw_later(VALID);
6624 }
6625}
6626
6627/*
6628 * Redraw all status lines that need to be redrawn.
6629 */
6630 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006631redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 win_T *wp;
6634
Bram Moolenaar29323592016-07-24 22:04:11 +02006635 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006637 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006638 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006639 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641
Bram Moolenaar4033c552017-09-16 20:54:51 +02006642#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643/*
6644 * Redraw all status lines at the bottom of frame "frp".
6645 */
6646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006647win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 if (frp->fr_layout == FR_LEAF)
6650 frp->fr_win->w_redr_status = TRUE;
6651 else if (frp->fr_layout == FR_ROW)
6652 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006653 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 win_redraw_last_status(frp);
6655 }
6656 else /* frp->fr_layout == FR_COL */
6657 {
6658 frp = frp->fr_child;
6659 while (frp->fr_next != NULL)
6660 frp = frp->fr_next;
6661 win_redraw_last_status(frp);
6662 }
6663}
6664#endif
6665
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666/*
6667 * Draw the verticap separator right of window "wp" starting with line "row".
6668 */
6669 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006670draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 int hl;
6673 int c;
6674
6675 if (wp->w_vsep_width)
6676 {
6677 /* draw the vertical separator right of this window */
6678 c = fillchar_vsep(&hl);
6679 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6680 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6681 c, ' ', hl);
6682 }
6683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
6685#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006686static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687
6688/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006689 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 */
6691 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006692status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 int len = 0;
6695
6696#ifdef FEAT_MENU
6697 int emenu = (xp->xp_context == EXPAND_MENUS
6698 || xp->xp_context == EXPAND_MENUNAMES);
6699
6700 /* Check for menu separators - replace with '|'. */
6701 if (emenu && menu_is_separator(s))
6702 return 1;
6703#endif
6704
6705 while (*s != NUL)
6706 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006707 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006708 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006709 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 }
6711
6712 return len;
6713}
6714
6715/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006716 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006717 * These are backslashes used for escaping. Do show backslashes in help tags.
6718 */
6719 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006720skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006721{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006722 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006723#ifdef FEAT_MENU
6724 || ((xp->xp_context == EXPAND_MENUS
6725 || xp->xp_context == EXPAND_MENUNAMES)
6726 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6727#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006728 )
6729 {
6730#ifndef BACKSLASH_IN_FILENAME
6731 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6732 return 2;
6733#endif
6734 return 1;
6735 }
6736 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006737}
6738
6739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 * Show wildchar matches in the status line.
6741 * Show at least the "match" item.
6742 * We start at item 'first_match' in the list and show all matches that fit.
6743 *
6744 * If inversion is possible we use it. Else '=' characters are used.
6745 */
6746 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006747win_redr_status_matches(
6748 expand_T *xp,
6749 int num_matches,
6750 char_u **matches, /* list of matches */
6751 int match,
6752 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6755 int row;
6756 char_u *buf;
6757 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006758 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 int fillchar;
6760 int attr;
6761 int i;
6762 int highlight = TRUE;
6763 char_u *selstart = NULL;
6764 int selstart_col = 0;
6765 char_u *selend = NULL;
6766 static int first_match = 0;
6767 int add_left = FALSE;
6768 char_u *s;
6769#ifdef FEAT_MENU
6770 int emenu;
6771#endif
6772#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6773 int l;
6774#endif
6775
6776 if (matches == NULL) /* interrupted completion? */
6777 return;
6778
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006779#ifdef FEAT_MBYTE
6780 if (has_mbyte)
6781 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6782 else
6783#endif
6784 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (buf == NULL)
6786 return;
6787
6788 if (match == -1) /* don't show match but original text */
6789 {
6790 match = 0;
6791 highlight = FALSE;
6792 }
6793 /* count 1 for the ending ">" */
6794 clen = status_match_len(xp, L_MATCH(match)) + 3;
6795 if (match == 0)
6796 first_match = 0;
6797 else if (match < first_match)
6798 {
6799 /* jumping left, as far as we can go */
6800 first_match = match;
6801 add_left = TRUE;
6802 }
6803 else
6804 {
6805 /* check if match fits on the screen */
6806 for (i = first_match; i < match; ++i)
6807 clen += status_match_len(xp, L_MATCH(i)) + 2;
6808 if (first_match > 0)
6809 clen += 2;
6810 /* jumping right, put match at the left */
6811 if ((long)clen > Columns)
6812 {
6813 first_match = match;
6814 /* if showing the last match, we can add some on the left */
6815 clen = 2;
6816 for (i = match; i < num_matches; ++i)
6817 {
6818 clen += status_match_len(xp, L_MATCH(i)) + 2;
6819 if ((long)clen >= Columns)
6820 break;
6821 }
6822 if (i == num_matches)
6823 add_left = TRUE;
6824 }
6825 }
6826 if (add_left)
6827 while (first_match > 0)
6828 {
6829 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6830 if ((long)clen >= Columns)
6831 break;
6832 --first_match;
6833 }
6834
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006835 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
6837 if (first_match == 0)
6838 {
6839 *buf = NUL;
6840 len = 0;
6841 }
6842 else
6843 {
6844 STRCPY(buf, "< ");
6845 len = 2;
6846 }
6847 clen = len;
6848
6849 i = first_match;
6850 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6851 {
6852 if (i == match)
6853 {
6854 selstart = buf + len;
6855 selstart_col = clen;
6856 }
6857
6858 s = L_MATCH(i);
6859 /* Check for menu separators - replace with '|' */
6860#ifdef FEAT_MENU
6861 emenu = (xp->xp_context == EXPAND_MENUS
6862 || xp->xp_context == EXPAND_MENUNAMES);
6863 if (emenu && menu_is_separator(s))
6864 {
6865 STRCPY(buf + len, transchar('|'));
6866 l = (int)STRLEN(buf + len);
6867 len += l;
6868 clen += l;
6869 }
6870 else
6871#endif
6872 for ( ; *s != NUL; ++s)
6873 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006874 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 clen += ptr2cells(s);
6876#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006877 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879 STRNCPY(buf + len, s, l);
6880 s += l - 1;
6881 len += l;
6882 }
6883 else
6884#endif
6885 {
6886 STRCPY(buf + len, transchar_byte(*s));
6887 len += (int)STRLEN(buf + len);
6888 }
6889 }
6890 if (i == match)
6891 selend = buf + len;
6892
6893 *(buf + len++) = ' ';
6894 *(buf + len++) = ' ';
6895 clen += 2;
6896 if (++i == num_matches)
6897 break;
6898 }
6899
6900 if (i != num_matches)
6901 {
6902 *(buf + len++) = '>';
6903 ++clen;
6904 }
6905
6906 buf[len] = NUL;
6907
6908 row = cmdline_row - 1;
6909 if (row >= 0)
6910 {
6911 if (wild_menu_showing == 0)
6912 {
6913 if (msg_scrolled > 0)
6914 {
6915 /* Put the wildmenu just above the command line. If there is
6916 * no room, scroll the screen one line up. */
6917 if (cmdline_row == Rows - 1)
6918 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006919 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 ++msg_scrolled;
6921 }
6922 else
6923 {
6924 ++cmdline_row;
6925 ++row;
6926 }
6927 wild_menu_showing = WM_SCROLLED;
6928 }
6929 else
6930 {
6931 /* Create status line if needed by setting 'laststatus' to 2.
6932 * Set 'winminheight' to zero to avoid that the window is
6933 * resized. */
6934 if (lastwin->w_status_height == 0)
6935 {
6936 save_p_ls = p_ls;
6937 save_p_wmh = p_wmh;
6938 p_ls = 2;
6939 p_wmh = 0;
6940 last_status(FALSE);
6941 }
6942 wild_menu_showing = WM_SHOWN;
6943 }
6944 }
6945
6946 screen_puts(buf, row, 0, attr);
6947 if (selstart != NULL && highlight)
6948 {
6949 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006950 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952
6953 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6954 }
6955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 vim_free(buf);
6958}
6959#endif
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961/*
6962 * Redraw the status line of window wp.
6963 *
6964 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006965 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6966 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006968 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006969win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
6971 int row;
6972 char_u *p;
6973 int len;
6974 int fillchar;
6975 int attr;
6976 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006977 static int busy = FALSE;
6978
6979 /* It's possible to get here recursively when 'statusline' (indirectly)
6980 * invokes ":redrawstatus". Simply ignore the call then. */
6981 if (busy)
6982 return;
6983 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
6985 wp->w_redr_status = FALSE;
6986 if (wp->w_status_height == 0)
6987 {
6988 /* no status line, can only be last window */
6989 redraw_cmdline = TRUE;
6990 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006991 else if (!redrawing()
6992#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006993 // don't update status line when popup menu is visible and may be
6994 // drawn over it, unless it will be redrawn later
6995 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006996#endif
6997 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
6999 /* Don't redraw right now, do it later. */
7000 wp->w_redr_status = TRUE;
7001 }
7002#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007003 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
7005 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007006 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
7008#endif
7009 else
7010 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007011 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007013 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 p = NameBuff;
7015 len = (int)STRLEN(p);
7016
Bram Moolenaard85f2712017-07-28 21:51:57 +02007017 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018#ifdef FEAT_QUICKFIX
7019 || wp->w_p_pvw
7020#endif
7021 || bufIsChanged(wp->w_buffer)
7022 || wp->w_buffer->b_p_ro)
7023 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007024 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007026 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 len += (int)STRLEN(p + len);
7028 }
7029#ifdef FEAT_QUICKFIX
7030 if (wp->w_p_pvw)
7031 {
7032 STRCPY(p + len, _("[Preview]"));
7033 len += (int)STRLEN(p + len);
7034 }
7035#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007036 if (bufIsChanged(wp->w_buffer)
7037#ifdef FEAT_TERMINAL
7038 && !bt_terminal(wp->w_buffer)
7039#endif
7040 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
7042 STRCPY(p + len, "[+]");
7043 len += 3;
7044 }
7045 if (wp->w_buffer->b_p_ro)
7046 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007047 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007048 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 }
7050
Bram Moolenaar02631462017-09-22 15:20:32 +02007051 this_ru_col = ru_col - (Columns - wp->w_width);
7052 if (this_ru_col < (wp->w_width + 1) / 2)
7053 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 if (this_ru_col <= 1)
7055 {
7056 p = (char_u *)"<"; /* No room for file name! */
7057 len = 1;
7058 }
7059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060#ifdef FEAT_MBYTE
7061 if (has_mbyte)
7062 {
7063 int clen = 0, i;
7064
7065 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007066 clen = mb_string2cells(p, -1);
7067
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 /* Find first character that will fit.
7069 * Going from start to end is much faster for DBCS. */
7070 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007071 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 clen -= (*mb_ptr2cells)(p + i);
7073 len = clen;
7074 if (i > 0)
7075 {
7076 p = p + i - 1;
7077 *p = '<';
7078 ++len;
7079 }
7080
7081 }
7082 else
7083#endif
7084 if (len > this_ru_col - 1)
7085 {
7086 p += len - (this_ru_col - 1);
7087 *p = '<';
7088 len = this_ru_col - 1;
7089 }
7090
7091 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007092 screen_puts(p, row, wp->w_wincol, attr);
7093 screen_fill(row, row + 1, len + wp->w_wincol,
7094 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007096 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7098 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007099 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
7101#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007102 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103#endif
7104 }
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 /*
7107 * May need to draw the character below the vertical separator.
7108 */
7109 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7110 {
7111 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007112 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 else
7114 fillchar = fillchar_vsep(&attr);
7115 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7116 attr);
7117 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007118 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119}
7120
Bram Moolenaar238a5642006-02-21 22:12:05 +00007121#ifdef FEAT_STL_OPT
7122/*
7123 * Redraw the status line according to 'statusline' and take care of any
7124 * errors encountered.
7125 */
7126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007127redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007128{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007129 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007130 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131
7132 /* When called recursively return. This can happen when the statusline
7133 * contains an expression that triggers a redraw. */
7134 if (entered)
7135 return;
7136 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007137
Bram Moolenaara742e082016-04-05 21:10:38 +02007138 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007139 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007140 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007141 {
7142 /* When there is an error disable the statusline, otherwise the
7143 * display is messed up with errors and a redraw triggers the problem
7144 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007145 set_string_option_direct((char_u *)"statusline", -1,
7146 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007147 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007148 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007149 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007150 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007151}
7152#endif
7153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154/*
7155 * Return TRUE if the status line of window "wp" is connected to the status
7156 * line of the window right of it. If not, then it's a vertical separator.
7157 * Only call if (wp->w_vsep_width != 0).
7158 */
7159 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007160stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161{
7162 frame_T *fr;
7163
7164 fr = wp->w_frame;
7165 while (fr->fr_parent != NULL)
7166 {
7167 if (fr->fr_parent->fr_layout == FR_COL)
7168 {
7169 if (fr->fr_next != NULL)
7170 break;
7171 }
7172 else
7173 {
7174 if (fr->fr_next != NULL)
7175 return TRUE;
7176 }
7177 fr = fr->fr_parent;
7178 }
7179 return FALSE;
7180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183/*
7184 * Get the value to show for the language mappings, active 'keymap'.
7185 */
7186 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007187get_keymap_str(
7188 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007189 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007190 char_u *buf, /* buffer for the result */
7191 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 char_u *p;
7194
7195 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7196 return FALSE;
7197
7198 {
7199#ifdef FEAT_EVAL
7200 buf_T *old_curbuf = curbuf;
7201 win_T *old_curwin = curwin;
7202 char_u *s;
7203
7204 curbuf = wp->w_buffer;
7205 curwin = wp;
7206 STRCPY(buf, "b:keymap_name"); /* must be writable */
7207 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007208 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 --emsg_skip;
7210 curbuf = old_curbuf;
7211 curwin = old_curwin;
7212 if (p == NULL || *p == NUL)
7213#endif
7214 {
7215#ifdef FEAT_KEYMAP
7216 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7217 p = wp->w_buffer->b_p_keymap;
7218 else
7219#endif
7220 p = (char_u *)"lang";
7221 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007222 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 buf[0] = NUL;
7224#ifdef FEAT_EVAL
7225 vim_free(s);
7226#endif
7227 }
7228 return buf[0] != NUL;
7229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
7231#if defined(FEAT_STL_OPT) || defined(PROTO)
7232/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007233 * Redraw the status line or ruler of window "wp".
7234 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 */
7236 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007237win_redr_custom(
7238 win_T *wp,
7239 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007241 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 int attr;
7243 int curattr;
7244 int row;
7245 int col = 0;
7246 int maxwidth;
7247 int width;
7248 int n;
7249 int len;
7250 int fillchar;
7251 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007252 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007254 struct stl_hlrec hltab[STL_MAX_ITEM];
7255 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007256 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007257 win_T *ewp;
7258 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
Bram Moolenaar1d633412013-12-11 15:52:01 +01007260 /* There is a tiny chance that this gets called recursively: When
7261 * redrawing a status line triggers redrawing the ruler or tabline.
7262 * Avoid trouble by not allowing recursion. */
7263 if (entered)
7264 return;
7265 entered = TRUE;
7266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007268 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007270 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007271 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007272 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007273 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007274 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007275 maxwidth = Columns;
7276# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007277 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007280 else
7281 {
7282 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007283 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007284 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007285
7286 if (draw_ruler)
7287 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007288 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007289 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007290 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007292 if (*++stl == '-')
7293 stl++;
7294 if (atoi((char *)stl))
7295 while (VIM_ISDIGIT(*stl))
7296 stl++;
7297 if (*stl++ != '(')
7298 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007299 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007300 col = ru_col - (Columns - wp->w_width);
7301 if (col < (wp->w_width + 1) / 2)
7302 col = (wp->w_width + 1) / 2;
7303 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007304 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 {
7306 row = Rows - 1;
7307 --maxwidth; /* writing in last column may cause scrolling */
7308 fillchar = ' ';
7309 attr = 0;
7310 }
7311
7312# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007313 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007314# endif
7315 }
7316 else
7317 {
7318 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007319 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007321 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007323 use_sandbox = was_set_insecurely((char_u *)"statusline",
7324 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007325# endif
7326 }
7327
Bram Moolenaar53f81742017-09-22 14:35:51 +02007328 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007329 }
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007332 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
Bram Moolenaar61452852011-02-01 18:01:11 +01007334 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7335 * the cursor away and back. */
7336 ewp = wp == NULL ? curwin : wp;
7337 p_crb_save = ewp->w_p_crb;
7338 ewp->w_p_crb = FALSE;
7339
Bram Moolenaar362f3562009-11-03 16:20:34 +00007340 /* Make a copy, because the statusline may include a function call that
7341 * might change the option value and free the memory. */
7342 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007343 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007344 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007345 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007346 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007347 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007349 /* Make all characters printable. */
7350 p = transstr(buf);
7351 if (p != NULL)
7352 {
7353 vim_strncpy(buf, p, sizeof(buf) - 1);
7354 vim_free(p);
7355 }
7356
7357 /* fill up with "fillchar" */
7358 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007359 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
7361#ifdef FEAT_MBYTE
7362 len += (*mb_char2bytes)(fillchar, buf + len);
7363#else
7364 buf[len++] = fillchar;
7365#endif
7366 ++width;
7367 }
7368 buf[len] = NUL;
7369
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007370 /*
7371 * Draw each snippet with the specified highlighting.
7372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 curattr = attr;
7374 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007375 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 screen_puts_len(p, len, row, col, curattr);
7379 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007380 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007384 else if (hltab[n].userhl < 0)
7385 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007386#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007387 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7388 && wp->w_status_height != 0)
7389 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007390 else if (wp != NULL && bt_terminal(wp->w_buffer)
7391 && wp->w_status_height != 0)
7392 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007393#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007394 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007395 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007397 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
7399 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007400
7401 if (wp == NULL)
7402 {
7403 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7404 col = 0;
7405 len = 0;
7406 p = buf;
7407 fillchar = 0;
7408 for (n = 0; tabtab[n].start != NULL; n++)
7409 {
7410 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7411 while (col < len)
7412 TabPageIdxs[col++] = fillchar;
7413 p = tabtab[n].start;
7414 fillchar = tabtab[n].userhl;
7415 }
7416 while (col < Columns)
7417 TabPageIdxs[col++] = fillchar;
7418 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007419
7420theend:
7421 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422}
7423
7424#endif /* FEAT_STL_OPT */
7425
7426/*
7427 * Output a single character directly to the screen and update ScreenLines.
7428 */
7429 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007430screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 char_u buf[MB_MAXBYTES + 1];
7433
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007434#ifdef FEAT_MBYTE
7435 if (has_mbyte)
7436 buf[(*mb_char2bytes)(c, buf)] = NUL;
7437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007439 {
7440 buf[0] = c;
7441 buf[1] = NUL;
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 screen_puts(buf, row, col, attr);
7444}
7445
7446/*
7447 * Get a single character directly from ScreenLines into "bytes[]".
7448 * Also return its attribute in *attrp;
7449 */
7450 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007451screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 unsigned off;
7454
7455 /* safety check */
7456 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7457 {
7458 off = LineOffset[row] + col;
7459 *attrp = ScreenAttrs[off];
7460 bytes[0] = ScreenLines[off];
7461 bytes[1] = NUL;
7462
7463#ifdef FEAT_MBYTE
7464 if (enc_utf8 && ScreenLinesUC[off] != 0)
7465 bytes[utfc_char2bytes(off, bytes)] = NUL;
7466 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7467 {
7468 bytes[0] = ScreenLines[off];
7469 bytes[1] = ScreenLines2[off];
7470 bytes[2] = NUL;
7471 }
7472 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7473 {
7474 bytes[1] = ScreenLines[off + 1];
7475 bytes[2] = NUL;
7476 }
7477#endif
7478 }
7479}
7480
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007481#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482/*
7483 * Return TRUE if composing characters for screen posn "off" differs from
7484 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007485 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007486 */
7487 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007488screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007489{
7490 int i;
7491
7492 for (i = 0; i < Screen_mco; ++i)
7493 {
7494 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7495 return TRUE;
7496 if (u8cc[i] == 0)
7497 break;
7498 }
7499 return FALSE;
7500}
7501#endif
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503/*
7504 * Put string '*text' on the screen at position 'row' and 'col', with
7505 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7506 * Note: only outputs within one row, message is truncated at screen boundary!
7507 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7508 */
7509 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007510screen_puts(
7511 char_u *text,
7512 int row,
7513 int col,
7514 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
7516 screen_puts_len(text, -1, row, col, attr);
7517}
7518
7519/*
7520 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7521 * a NUL.
7522 */
7523 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007524screen_puts_len(
7525 char_u *text,
7526 int textlen,
7527 int row,
7528 int col,
7529 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
7531 unsigned off;
7532 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007533 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 int c;
7535#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007536 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 int mbyte_blen = 1;
7538 int mbyte_cells = 1;
7539 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007540 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 int clear_next_cell = FALSE;
7542# ifdef FEAT_ARABIC
7543 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544 int pc, nc, nc1;
7545 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546# endif
7547#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007548#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7549 int force_redraw_this;
7550 int force_redraw_next = FALSE;
7551#endif
7552 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7555 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007556 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaarc236c162008-07-13 17:41:49 +00007558#ifdef FEAT_MBYTE
7559 /* When drawing over the right halve of a double-wide char clear out the
7560 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007561 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007562# ifdef FEAT_GUI
7563 && !gui.in_use
7564# endif
7565 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007566 {
7567 ScreenLines[off - 1] = ' ';
7568 ScreenAttrs[off - 1] = 0;
7569 if (enc_utf8)
7570 {
7571 ScreenLinesUC[off - 1] = 0;
7572 ScreenLinesC[0][off - 1] = 0;
7573 }
7574 /* redraw the previous cell, make it empty */
7575 screen_char(off - 1, row, col - 1);
7576 /* force the cell at "col" to be redrawn */
7577 force_redraw_next = TRUE;
7578 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007579#endif
7580
Bram Moolenaar367329b2007-08-30 11:53:22 +00007581#ifdef FEAT_MBYTE
7582 max_off = LineOffset[row] + screen_Columns;
7583#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007584 while (col < screen_Columns
7585 && (len < 0 || (int)(ptr - text) < len)
7586 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {
7588 c = *ptr;
7589#ifdef FEAT_MBYTE
7590 /* check if this is the first byte of a multibyte */
7591 if (has_mbyte)
7592 {
7593 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007594 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007596 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7598 mbyte_cells = 1;
7599 else if (enc_dbcs != 0)
7600 mbyte_cells = mbyte_blen;
7601 else /* enc_utf8 */
7602 {
7603 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007604 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 (int)((text + len) - ptr));
7606 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007609# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 /* Non-BMP character: display as ? or fullwidth ?. */
7611 if (u8c >= 0x10000)
7612 {
7613 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7614 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007615 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007617# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618# ifdef FEAT_ARABIC
7619 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7620 {
7621 /* Do Arabic shaping. */
7622 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7623 {
7624 /* Past end of string to be displayed. */
7625 nc = NUL;
7626 nc1 = NUL;
7627 }
7628 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007629 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007630 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7631 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007632 nc1 = pcc[0];
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 pc = prev_c;
7635 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007636 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 }
7638 else
7639 prev_c = u8c;
7640# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007641 if (col + mbyte_cells > screen_Columns)
7642 {
7643 /* Only 1 cell left, but character requires 2 cells:
7644 * display a '>' in the last column to avoid wrapping. */
7645 c = '>';
7646 mbyte_cells = 1;
7647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649 }
7650#endif
7651
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7653 force_redraw_this = force_redraw_next;
7654 force_redraw_next = FALSE;
7655#endif
7656
7657 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658#ifdef FEAT_MBYTE
7659 || (mbyte_cells == 2
7660 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7661 || (enc_dbcs == DBCS_JPNU
7662 && c == 0x8e
7663 && ScreenLines2[off] != ptr[1])
7664 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007665 && (ScreenLinesUC[off] !=
7666 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7667 || (ScreenLinesUC[off] != 0
7668 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#endif
7670 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007671 || exmode_active;
7672
7673 if (need_redraw
7674#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7675 || force_redraw_this
7676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 )
7678 {
7679#if defined(FEAT_GUI) || defined(UNIX)
7680 /* The bold trick makes a single row of pixels appear in the next
7681 * character. When a bold character is removed, the next
7682 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007683 * and for some xterms. */
7684 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685# ifdef FEAT_GUI
7686 gui.in_use
7687# endif
7688# if defined(FEAT_GUI) && defined(UNIX)
7689 ||
7690# endif
7691# ifdef UNIX
7692 term_is_xterm
7693# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007694 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007696 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007698 if (n > HL_ALL)
7699 n = syn_attr2attr(n);
7700 if (n & HL_BOLD)
7701 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703#endif
7704#ifdef FEAT_MBYTE
7705 /* When at the end of the text and overwriting a two-cell
7706 * character with a one-cell character, need to clear the next
7707 * cell. Also when overwriting the left halve of a two-cell char
7708 * with the right halve of a two-cell char. Do this only once
7709 * (mb_off2cells() may return 2 on the right halve). */
7710 if (clear_next_cell)
7711 clear_next_cell = FALSE;
7712 else if (has_mbyte
7713 && (len < 0 ? ptr[mbyte_blen] == NUL
7714 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007715 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007717 && (*mb_off2cells)(off, max_off) == 1
7718 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 clear_next_cell = TRUE;
7720
7721 /* Make sure we never leave a second byte of a double-byte behind,
7722 * it confuses mb_off2cells(). */
7723 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007724 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007726 && (*mb_off2cells)(off, max_off) == 1
7727 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 ScreenLines[off + mbyte_blen] = 0;
7729#endif
7730 ScreenLines[off] = c;
7731 ScreenAttrs[off] = attr;
7732#ifdef FEAT_MBYTE
7733 if (enc_utf8)
7734 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007735 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 ScreenLinesUC[off] = 0;
7737 else
7738 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007739 int i;
7740
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007742 for (i = 0; i < Screen_mco; ++i)
7743 {
7744 ScreenLinesC[i][off] = u8cc[i];
7745 if (u8cc[i] == 0)
7746 break;
7747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
7749 if (mbyte_cells == 2)
7750 {
7751 ScreenLines[off + 1] = 0;
7752 ScreenAttrs[off + 1] = attr;
7753 }
7754 screen_char(off, row, col);
7755 }
7756 else if (mbyte_cells == 2)
7757 {
7758 ScreenLines[off + 1] = ptr[1];
7759 ScreenAttrs[off + 1] = attr;
7760 screen_char_2(off, row, col);
7761 }
7762 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7763 {
7764 ScreenLines2[off] = ptr[1];
7765 screen_char(off, row, col);
7766 }
7767 else
7768#endif
7769 screen_char(off, row, col);
7770 }
7771#ifdef FEAT_MBYTE
7772 if (has_mbyte)
7773 {
7774 off += mbyte_cells;
7775 col += mbyte_cells;
7776 ptr += mbyte_blen;
7777 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007778 {
7779 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007781 len = -1;
7782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784 else
7785#endif
7786 {
7787 ++off;
7788 ++col;
7789 ++ptr;
7790 }
7791 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007792
7793#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7794 /* If we detected the next character needs to be redrawn, but the text
7795 * doesn't extend up to there, update the character here. */
7796 if (force_redraw_next && col < screen_Columns)
7797 {
7798# ifdef FEAT_MBYTE
7799 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7800 screen_char_2(off, row, col);
7801 else
7802# endif
7803 screen_char(off, row, col);
7804 }
7805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806}
7807
7808#ifdef FEAT_SEARCH_EXTRA
7809/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007810 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 */
7812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007813start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815 if (p_hls && !no_hlsearch)
7816 {
7817 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007818 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007819# ifdef FEAT_RELTIME
7820 /* Set the time limit to 'redrawtime'. */
7821 profile_setlimit(p_rdt, &search_hl.tm);
7822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 }
7824}
7825
7826/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007827 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 */
7829 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007830end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832 if (search_hl.rm.regprog != NULL)
7833 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007834 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 search_hl.rm.regprog = NULL;
7836 }
7837}
7838
7839/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007840 * Init for calling prepare_search_hl().
7841 */
7842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007843init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007844{
7845 matchitem_T *cur;
7846
7847 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7848 * match */
7849 cur = wp->w_match_head;
7850 while (cur != NULL)
7851 {
7852 cur->hl.rm = cur->match;
7853 if (cur->hlg_id == 0)
7854 cur->hl.attr = 0;
7855 else
7856 cur->hl.attr = syn_id2attr(cur->hlg_id);
7857 cur->hl.buf = wp->w_buffer;
7858 cur->hl.lnum = 0;
7859 cur->hl.first_lnum = 0;
7860# ifdef FEAT_RELTIME
7861 /* Set the time limit to 'redrawtime'. */
7862 profile_setlimit(p_rdt, &(cur->hl.tm));
7863# endif
7864 cur = cur->next;
7865 }
7866 search_hl.buf = wp->w_buffer;
7867 search_hl.lnum = 0;
7868 search_hl.first_lnum = 0;
7869 /* time limit is set at the toplevel, for all windows */
7870}
7871
7872/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 * Advance to the match in window "wp" line "lnum" or past it.
7874 */
7875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007876prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007878 matchitem_T *cur; /* points to the match list */
7879 match_T *shl; /* points to search_hl or a match */
7880 int shl_flag; /* flag to indicate whether search_hl
7881 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882 int pos_inprogress; /* marks that position match search is
7883 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 int n;
7885
7886 /*
7887 * When using a multi-line pattern, start searching at the top
7888 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007889 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007891 cur = wp->w_match_head;
7892 shl_flag = FALSE;
7893 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007895 if (shl_flag == FALSE)
7896 {
7897 shl = &search_hl;
7898 shl_flag = TRUE;
7899 }
7900 else
7901 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 if (shl->rm.regprog != NULL
7903 && shl->lnum == 0
7904 && re_multiline(shl->rm.regprog))
7905 {
7906 if (shl->first_lnum == 0)
7907 {
7908# ifdef FEAT_FOLDING
7909 for (shl->first_lnum = lnum;
7910 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7911 if (hasFoldingWin(wp, shl->first_lnum - 1,
7912 NULL, NULL, TRUE, NULL))
7913 break;
7914# else
7915 shl->first_lnum = wp->w_topline;
7916# endif
7917 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007918 if (cur != NULL)
7919 cur->pos.cur = 0;
7920 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007922 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7923 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007925 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7926 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007927 pos_inprogress = cur == NULL || cur->pos.cur == 0
7928 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if (shl->lnum != 0)
7930 {
7931 shl->first_lnum = shl->lnum
7932 + shl->rm.endpos[0].lnum
7933 - shl->rm.startpos[0].lnum;
7934 n = shl->rm.endpos[0].col;
7935 }
7936 else
7937 {
7938 ++shl->first_lnum;
7939 n = 0;
7940 }
7941 }
7942 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007943 if (shl != &search_hl && cur != NULL)
7944 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 }
7946}
7947
7948/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007949 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 * Uses shl->buf.
7951 * Sets shl->lnum and shl->rm contents.
7952 * Note: Assumes a previous match is always before "lnum", unless
7953 * shl->lnum is zero.
7954 * Careful: Any pointers for buffer lines will become invalid.
7955 */
7956 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007957next_search_hl(
7958 win_T *win,
7959 match_T *shl, /* points to search_hl or a match */
7960 linenr_T lnum,
7961 colnr_T mincol, /* minimal column for a match */
7962 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 linenr_T l;
7965 colnr_T matchcol;
7966 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007967 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007969 // for :{range}s/pat only highlight inside the range
7970 if (lnum < search_first_line || lnum > search_last_line)
7971 {
7972 shl->lnum = 0;
7973 return;
7974 }
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (shl->lnum != 0)
7977 {
7978 /* Check for three situations:
7979 * 1. If the "lnum" is below a previous match, start a new search.
7980 * 2. If the previous match includes "mincol", use it.
7981 * 3. Continue after the previous match.
7982 */
7983 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7984 if (lnum > l)
7985 shl->lnum = 0;
7986 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7987 return;
7988 }
7989
7990 /*
7991 * Repeat searching for a match until one is found that includes "mincol"
7992 * or none is found in this line.
7993 */
7994 called_emsg = FALSE;
7995 for (;;)
7996 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007997#ifdef FEAT_RELTIME
7998 /* Stop searching after passing the time limit. */
7999 if (profile_passed_limit(&(shl->tm)))
8000 {
8001 shl->lnum = 0; /* no match found in time */
8002 break;
8003 }
8004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 /* Three situations:
8006 * 1. No useful previous match: search from start of line.
8007 * 2. Not Vi compatible or empty match: continue at next character.
8008 * Break the loop if this is beyond the end of the line.
8009 * 3. Vi compatible searching: continue at end of previous match.
8010 */
8011 if (shl->lnum == 0)
8012 matchcol = 0;
8013 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8014 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008015 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008017 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008018
8019 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008020 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008021 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008023 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 shl->lnum = 0;
8025 break;
8026 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008027#ifdef FEAT_MBYTE
8028 if (has_mbyte)
8029 matchcol += mb_ptr2len(ml);
8030 else
8031#endif
8032 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
8034 else
8035 matchcol = shl->rm.endpos[0].col;
8036
8037 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008038 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008040 /* Remember whether shl->rm is using a copy of the regprog in
8041 * cur->match. */
8042 int regprog_is_copy = (shl != &search_hl && cur != NULL
8043 && shl == &cur->hl
8044 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008045 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008046
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8048 matchcol,
8049#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008050 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008051#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008052 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008053#endif
8054 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008055 /* Copy the regprog, in case it got freed and recompiled. */
8056 if (regprog_is_copy)
8057 cur->match.regprog = cur->hl.rm.regprog;
8058
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008059 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008060 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008061 /* Error while handling regexp: stop using this regexp. */
8062 if (shl == &search_hl)
8063 {
8064 /* don't free regprog in the match list, it's a copy */
8065 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008066 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008067 }
8068 shl->rm.regprog = NULL;
8069 shl->lnum = 0;
8070 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8071 message */
8072 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008073 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074 }
8075 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008077 else
8078 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 if (nmatched == 0)
8080 {
8081 shl->lnum = 0; /* no match found */
8082 break;
8083 }
8084 if (shl->rm.startpos[0].lnum > 0
8085 || shl->rm.startpos[0].col >= mincol
8086 || nmatched > 1
8087 || shl->rm.endpos[0].col > mincol)
8088 {
8089 shl->lnum += shl->rm.startpos[0].lnum;
8090 break; /* useful match found */
8091 }
8092 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008093
8094 // Restore called_emsg for assert_fails().
8095 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
Bram Moolenaar85077472016-10-16 14:35:48 +02008098/*
8099 * If there is a match fill "shl" and return one.
8100 * Return zero otherwise.
8101 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008103next_search_hl_pos(
8104 match_T *shl, /* points to a match */
8105 linenr_T lnum,
8106 posmatch_T *posmatch, /* match positions */
8107 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008108{
8109 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008110 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111
Bram Moolenaarb3414592014-06-17 17:48:32 +02008112 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8113 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008114 llpos_T *pos = &posmatch->pos[i];
8115
8116 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008117 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008118 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008119 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008120 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008121 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008122 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008123 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008124 /* if this match comes before the one at "found" then swap
8125 * them */
8126 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008127 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008128 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008129
Bram Moolenaar85077472016-10-16 14:35:48 +02008130 *pos = posmatch->pos[found];
8131 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008132 }
8133 }
8134 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008135 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008136 }
8137 }
8138 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008139 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008140 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008141 colnr_T start = posmatch->pos[found].col == 0
8142 ? 0 : posmatch->pos[found].col - 1;
8143 colnr_T end = posmatch->pos[found].col == 0
8144 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008145
Bram Moolenaar85077472016-10-16 14:35:48 +02008146 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008147 shl->rm.startpos[0].lnum = 0;
8148 shl->rm.startpos[0].col = start;
8149 shl->rm.endpos[0].lnum = 0;
8150 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008151 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008152 posmatch->cur = found + 1;
8153 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008154 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008155 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008156}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008157#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008158
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008160screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
8162 attrentry_T *aep = NULL;
8163
8164 screen_attr = attr;
8165 if (full_screen
8166#ifdef WIN3264
8167 && termcap_active
8168#endif
8169 )
8170 {
8171#ifdef FEAT_GUI
8172 if (gui.in_use)
8173 {
8174 char buf[20];
8175
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008176 /* The GUI handles this internally. */
8177 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 OUT_STR(buf);
8179 }
8180 else
8181#endif
8182 {
8183 if (attr > HL_ALL) /* special HL attr. */
8184 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008185 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 aep = syn_cterm_attr2entry(attr);
8187 else
8188 aep = syn_term_attr2entry(attr);
8189 if (aep == NULL) /* did ":syntax clear" */
8190 attr = 0;
8191 else
8192 attr = aep->ae_attr;
8193 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008194 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008196 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008197#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008198 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8199 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8200 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008201#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008202 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008203 /* If the Normal FG color has BOLD attribute and the new HL
8204 * has a FG color defined, clear BOLD. */
8205 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008206 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008208 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008209 out_str(T_UCS);
8210 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008211 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8212 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008214 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008216 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008218 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008219 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220
8221 /*
8222 * Output the color or start string after bold etc., in case the
8223 * bold etc. override the color setting.
8224 */
8225 if (aep != NULL)
8226 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008227#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008228 /* When 'termguicolors' is set but fg or bg is unset,
8229 * fall back to the cterm colors. This helps for SpellBad,
8230 * where the GUI uses a red undercurl. */
8231 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008233 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008234 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 }
8236 else
8237#endif
8238 if (t_colors > 1)
8239 {
8240 if (aep->ae_u.cterm.fg_color)
8241 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8242 }
8243#ifdef FEAT_TERMGUICOLORS
8244 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8245 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008246 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008247 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
8249 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008250#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008251 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008253 if (aep->ae_u.cterm.bg_color)
8254 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8255 }
8256
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008257 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008258 {
8259 if (aep->ae_u.term.start != NULL)
8260 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 }
8262 }
8263 }
8264 }
8265}
8266
8267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008268screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
8270 int do_ME = FALSE; /* output T_ME code */
8271
8272 if (screen_attr != 0
8273#ifdef WIN3264
8274 && termcap_active
8275#endif
8276 )
8277 {
8278#ifdef FEAT_GUI
8279 if (gui.in_use)
8280 {
8281 char buf[20];
8282
8283 /* use internal GUI code */
8284 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8285 OUT_STR(buf);
8286 }
8287 else
8288#endif
8289 {
8290 if (screen_attr > HL_ALL) /* special HL attr. */
8291 {
8292 attrentry_T *aep;
8293
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008294 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
8296 /*
8297 * Assume that t_me restores the original colors!
8298 */
8299 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008300 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008301#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008302 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8303 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8304 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008305#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008306 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008307#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008308 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8309 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8310 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008311#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008312 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 do_ME = TRUE;
8314 }
8315 else
8316 {
8317 aep = syn_term_attr2entry(screen_attr);
8318 if (aep != NULL && aep->ae_u.term.stop != NULL)
8319 {
8320 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8321 do_ME = TRUE;
8322 else
8323 out_str(aep->ae_u.term.stop);
8324 }
8325 }
8326 if (aep == NULL) /* did ":syntax clear" */
8327 screen_attr = 0;
8328 else
8329 screen_attr = aep->ae_attr;
8330 }
8331
8332 /*
8333 * Often all ending-codes are equal to T_ME. Avoid outputting the
8334 * same sequence several times.
8335 */
8336 if (screen_attr & HL_STANDOUT)
8337 {
8338 if (STRCMP(T_SE, T_ME) == 0)
8339 do_ME = TRUE;
8340 else
8341 out_str(T_SE);
8342 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008343 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008344 {
8345 if (STRCMP(T_UCE, T_ME) == 0)
8346 do_ME = TRUE;
8347 else
8348 out_str(T_UCE);
8349 }
8350 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008351 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
8353 if (STRCMP(T_UE, T_ME) == 0)
8354 do_ME = TRUE;
8355 else
8356 out_str(T_UE);
8357 }
8358 if (screen_attr & HL_ITALIC)
8359 {
8360 if (STRCMP(T_CZR, T_ME) == 0)
8361 do_ME = TRUE;
8362 else
8363 out_str(T_CZR);
8364 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008365 if (screen_attr & HL_STRIKETHROUGH)
8366 {
8367 if (STRCMP(T_STE, T_ME) == 0)
8368 do_ME = TRUE;
8369 else
8370 out_str(T_STE);
8371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8373 out_str(T_ME);
8374
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008375#ifdef FEAT_TERMGUICOLORS
8376 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008378 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008379 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008380 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008381 term_bg_rgb_color(cterm_normal_bg_gui_color);
8382 }
8383 else
8384#endif
8385 {
8386 if (t_colors > 1)
8387 {
8388 /* set Normal cterm colors */
8389 if (cterm_normal_fg_color != 0)
8390 term_fg_color(cterm_normal_fg_color - 1);
8391 if (cterm_normal_bg_color != 0)
8392 term_bg_color(cterm_normal_bg_color - 1);
8393 if (cterm_normal_fg_bold)
8394 out_str(T_MD);
8395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 }
8397 }
8398 }
8399 screen_attr = 0;
8400}
8401
8402/*
8403 * Reset the colors for a cterm. Used when leaving Vim.
8404 * The machine specific code may override this again.
8405 */
8406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008407reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008409 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
8411 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008412#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008413 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8414 || cterm_normal_bg_gui_color != INVALCOLOR)
8415 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008416#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {
8420 out_str(T_OP);
8421 screen_attr = -1;
8422 }
8423 if (cterm_normal_fg_bold)
8424 {
8425 out_str(T_ME);
8426 screen_attr = -1;
8427 }
8428 }
8429}
8430
8431/*
8432 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8433 * using the attributes from ScreenAttrs["off"].
8434 */
8435 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008436screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437{
8438 int attr;
8439
8440 /* Check for illegal values, just in case (could happen just after
8441 * resizing). */
8442 if (row >= screen_Rows || col >= screen_Columns)
8443 return;
8444
Bram Moolenaar494838a2015-02-10 19:20:37 +01008445 /* Outputting a character in the last cell on the screen may scroll the
8446 * screen up. Only do it when the "xn" termcap property is set, otherwise
8447 * mark the character invalid (update it when scrolled up). */
8448 if (*T_XN == NUL
8449 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#ifdef FEAT_RIGHTLEFT
8451 /* account for first command-line character in rightleft mode */
8452 && !cmdmsg_rl
8453#endif
8454 )
8455 {
8456 ScreenAttrs[off] = (sattr_T)-1;
8457 return;
8458 }
8459
8460 /*
8461 * Stop highlighting first, so it's easier to move the cursor.
8462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 if (screen_char_attr != 0)
8464 attr = screen_char_attr;
8465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 attr = ScreenAttrs[off];
8467 if (screen_attr != attr)
8468 screen_stop_highlight();
8469
8470 windgoto(row, col);
8471
8472 if (screen_attr != attr)
8473 screen_start_highlight(attr);
8474
8475#ifdef FEAT_MBYTE
8476 if (enc_utf8 && ScreenLinesUC[off] != 0)
8477 {
8478 char_u buf[MB_MAXBYTES + 1];
8479
Bram Moolenaarcb070082016-04-02 22:14:51 +02008480 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008481 {
8482 if (*p_ambw == 'd'
8483# ifdef FEAT_GUI
8484 && !gui.in_use
8485# endif
8486 )
8487 {
8488 /* Clear the two screen cells. If the character is actually
8489 * single width it won't change the second cell. */
8490 out_str((char_u *)" ");
8491 term_windgoto(row, col);
8492 }
8493 /* not sure where the cursor is after drawing the ambiguous width
8494 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008495 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008496 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008497 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008499
8500 /* Convert the UTF-8 character to bytes and write it. */
8501 buf[utfc_char2bytes(off, buf)] = NUL;
8502 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 }
8504 else
8505#endif
8506 {
8507#ifdef FEAT_MBYTE
8508 out_flush_check();
8509#endif
8510 out_char(ScreenLines[off]);
8511#ifdef FEAT_MBYTE
8512 /* double-byte character in single-width cell */
8513 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8514 out_char(ScreenLines2[off]);
8515#endif
8516 }
8517
8518 screen_cur_col++;
8519}
8520
8521#ifdef FEAT_MBYTE
8522
8523/*
8524 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8525 * on the screen at position 'row' and 'col'.
8526 * The attributes of the first byte is used for all. This is required to
8527 * output the two bytes of a double-byte character with nothing in between.
8528 */
8529 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008530screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 /* Check for illegal values (could be wrong when screen was resized). */
8533 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8534 return;
8535
8536 /* Outputting the last character on the screen may scrollup the screen.
8537 * Don't to it! Mark the character invalid (update it when scrolled up) */
8538 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8539 {
8540 ScreenAttrs[off] = (sattr_T)-1;
8541 return;
8542 }
8543
8544 /* Output the first byte normally (positions the cursor), then write the
8545 * second byte directly. */
8546 screen_char(off, row, col);
8547 out_char(ScreenLines[off + 1]);
8548 ++screen_cur_col;
8549}
8550#endif
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552/*
8553 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8554 * This uses the contents of ScreenLines[] and doesn't change it.
8555 */
8556 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008557screen_draw_rectangle(
8558 int row,
8559 int col,
8560 int height,
8561 int width,
8562 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 int r, c;
8565 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008566#ifdef FEAT_MBYTE
8567 int max_off;
8568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008570 /* Can't use ScreenLines unless initialized */
8571 if (ScreenLines == NULL)
8572 return;
8573
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 if (invert)
8575 screen_char_attr = HL_INVERSE;
8576 for (r = row; r < row + height; ++r)
8577 {
8578 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008579#ifdef FEAT_MBYTE
8580 max_off = off + screen_Columns;
8581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 for (c = col; c < col + width; ++c)
8583 {
8584#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008585 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {
8587 screen_char_2(off + c, r, c);
8588 ++c;
8589 }
8590 else
8591#endif
8592 {
8593 screen_char(off + c, r, c);
8594#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008595 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 ++c;
8597#endif
8598 }
8599 }
8600 }
8601 screen_char_attr = 0;
8602}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604/*
8605 * Redraw the characters for a vertically split window.
8606 */
8607 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008608redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610 int col;
8611 int width;
8612
8613# ifdef FEAT_CLIPBOARD
8614 clip_may_clear_selection(row, end - 1);
8615# endif
8616
8617 if (wp == NULL)
8618 {
8619 col = 0;
8620 width = Columns;
8621 }
8622 else
8623 {
8624 col = wp->w_wincol;
8625 width = wp->w_width;
8626 }
8627 screen_draw_rectangle(row, col, end - row, width, FALSE);
8628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008630 static void
8631space_to_screenline(int off, int attr)
8632{
8633 ScreenLines[off] = ' ';
8634 ScreenAttrs[off] = attr;
8635# ifdef FEAT_MBYTE
8636 if (enc_utf8)
8637 ScreenLinesUC[off] = 0;
8638# endif
8639}
8640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641/*
8642 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8643 * with character 'c1' in first column followed by 'c2' in the other columns.
8644 * Use attributes 'attr'.
8645 */
8646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008647screen_fill(
8648 int start_row,
8649 int end_row,
8650 int start_col,
8651 int end_col,
8652 int c1,
8653 int c2,
8654 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 int row;
8657 int col;
8658 int off;
8659 int end_off;
8660 int did_delete;
8661 int c;
8662 int norm_term;
8663#if defined(FEAT_GUI) || defined(UNIX)
8664 int force_next = FALSE;
8665#endif
8666
8667 if (end_row > screen_Rows) /* safety check */
8668 end_row = screen_Rows;
8669 if (end_col > screen_Columns) /* safety check */
8670 end_col = screen_Columns;
8671 if (ScreenLines == NULL
8672 || start_row >= end_row
8673 || start_col >= end_col) /* nothing to do */
8674 return;
8675
8676 /* it's a "normal" terminal when not in a GUI or cterm */
8677 norm_term = (
8678#ifdef FEAT_GUI
8679 !gui.in_use &&
8680#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008681 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 for (row = start_row; row < end_row; ++row)
8683 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008684#ifdef FEAT_MBYTE
8685 if (has_mbyte
8686# ifdef FEAT_GUI
8687 && !gui.in_use
8688# endif
8689 )
8690 {
8691 /* When drawing over the right halve of a double-wide char clear
8692 * out the left halve. When drawing over the left halve of a
8693 * double wide-char clear out the right halve. Only needed in a
8694 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008695 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008696 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008697 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008698 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008699 }
8700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 /*
8702 * Try to use delete-line termcap code, when no attributes or in a
8703 * "normal" terminal, where a bold/italic space is just a
8704 * space.
8705 */
8706 did_delete = FALSE;
8707 if (c2 == ' '
8708 && end_col == Columns
8709 && can_clear(T_CE)
8710 && (attr == 0
8711 || (norm_term
8712 && attr <= HL_ALL
8713 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8714 {
8715 /*
8716 * check if we really need to clear something
8717 */
8718 col = start_col;
8719 if (c1 != ' ') /* don't clear first char */
8720 ++col;
8721
8722 off = LineOffset[row] + col;
8723 end_off = LineOffset[row] + end_col;
8724
8725 /* skip blanks (used often, keep it fast!) */
8726#ifdef FEAT_MBYTE
8727 if (enc_utf8)
8728 while (off < end_off && ScreenLines[off] == ' '
8729 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8730 ++off;
8731 else
8732#endif
8733 while (off < end_off && ScreenLines[off] == ' '
8734 && ScreenAttrs[off] == 0)
8735 ++off;
8736 if (off < end_off) /* something to be cleared */
8737 {
8738 col = off - LineOffset[row];
8739 screen_stop_highlight();
8740 term_windgoto(row, col);/* clear rest of this screen line */
8741 out_str(T_CE);
8742 screen_start(); /* don't know where cursor is now */
8743 col = end_col - col;
8744 while (col--) /* clear chars in ScreenLines */
8745 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008746 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 ++off;
8748 }
8749 }
8750 did_delete = TRUE; /* the chars are cleared now */
8751 }
8752
8753 off = LineOffset[row] + start_col;
8754 c = c1;
8755 for (col = start_col; col < end_col; ++col)
8756 {
8757 if (ScreenLines[off] != c
8758#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008759 || (enc_utf8 && (int)ScreenLinesUC[off]
8760 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#endif
8762 || ScreenAttrs[off] != attr
8763#if defined(FEAT_GUI) || defined(UNIX)
8764 || force_next
8765#endif
8766 )
8767 {
8768#if defined(FEAT_GUI) || defined(UNIX)
8769 /* The bold trick may make a single row of pixels appear in
8770 * the next character. When a bold character is removed, the
8771 * next character should be redrawn too. This happens for our
8772 * own GUI and for some xterms. */
8773 if (
8774# ifdef FEAT_GUI
8775 gui.in_use
8776# endif
8777# if defined(FEAT_GUI) && defined(UNIX)
8778 ||
8779# endif
8780# ifdef UNIX
8781 term_is_xterm
8782# endif
8783 )
8784 {
8785 if (ScreenLines[off] != ' '
8786 && (ScreenAttrs[off] > HL_ALL
8787 || ScreenAttrs[off] & HL_BOLD))
8788 force_next = TRUE;
8789 else
8790 force_next = FALSE;
8791 }
8792#endif
8793 ScreenLines[off] = c;
8794#ifdef FEAT_MBYTE
8795 if (enc_utf8)
8796 {
8797 if (c >= 0x80)
8798 {
8799 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008800 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 }
8802 else
8803 ScreenLinesUC[off] = 0;
8804 }
8805#endif
8806 ScreenAttrs[off] = attr;
8807 if (!did_delete || c != ' ')
8808 screen_char(off, row, col);
8809 }
8810 ++off;
8811 if (col == start_col)
8812 {
8813 if (did_delete)
8814 break;
8815 c = c2;
8816 }
8817 }
8818 if (end_col == Columns)
8819 LineWraps[row] = FALSE;
8820 if (row == Rows - 1) /* overwritten the command line */
8821 {
8822 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008823 if (start_col == 0 && end_col == Columns
8824 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008826 if (start_col == 0)
8827 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 }
8829 }
8830}
8831
8832/*
8833 * Check if there should be a delay. Used before clearing or redrawing the
8834 * screen or the command line.
8835 */
8836 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008837check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8840 && !did_wait_return
8841 && emsg_silent == 0)
8842 {
8843 out_flush();
8844 ui_delay(1000L, TRUE);
8845 emsg_on_display = FALSE;
8846 if (check_msg_scroll)
8847 msg_scroll = FALSE;
8848 }
8849}
8850
8851/*
8852 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008853 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * Returns TRUE if there is a valid screen to write to.
8855 * Returns FALSE when starting up and screen not initialized yet.
8856 */
8857 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008858screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008860 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 return (ScreenLines != NULL);
8862}
8863
8864/*
8865 * Resize the shell to Rows and Columns.
8866 * Allocate ScreenLines[] and associated items.
8867 *
8868 * There may be some time between setting Rows and Columns and (re)allocating
8869 * ScreenLines[]. This happens when starting up and when (manually) changing
8870 * the shell size. Always use screen_Rows and screen_Columns to access items
8871 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8872 * final size of the shell is needed.
8873 */
8874 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008875screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 int new_row, old_row;
8878#ifdef FEAT_GUI
8879 int old_Rows;
8880#endif
8881 win_T *wp;
8882 int outofmem = FALSE;
8883 int len;
8884 schar_T *new_ScreenLines;
8885#ifdef FEAT_MBYTE
8886 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008887 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008889 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#endif
8891 sattr_T *new_ScreenAttrs;
8892 unsigned *new_LineOffset;
8893 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008894 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008895 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008897 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008898 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008900retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 /*
8902 * Allocation of the screen buffers is done only when the size changes and
8903 * when Rows and Columns have been set and we have started doing full
8904 * screen stuff.
8905 */
8906 if ((ScreenLines != NULL
8907 && Rows == screen_Rows
8908 && Columns == screen_Columns
8909#ifdef FEAT_MBYTE
8910 && enc_utf8 == (ScreenLinesUC != NULL)
8911 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008912 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913#endif
8914 )
8915 || Rows == 0
8916 || Columns == 0
8917 || (!full_screen && ScreenLines == NULL))
8918 return;
8919
8920 /*
8921 * It's possible that we produce an out-of-memory message below, which
8922 * will cause this function to be called again. To break the loop, just
8923 * return here.
8924 */
8925 if (entered)
8926 return;
8927 entered = TRUE;
8928
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008929 /*
8930 * Note that the window sizes are updated before reallocating the arrays,
8931 * thus we must not redraw here!
8932 */
8933 ++RedrawingDisabled;
8934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 win_new_shellsize(); /* fit the windows in the new sized shell */
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 comp_col(); /* recompute columns for shown command and ruler */
8938
8939 /*
8940 * We're changing the size of the screen.
8941 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8942 * - Move lines from the old arrays into the new arrays, clear extra
8943 * lines (unless the screen is going to be cleared).
8944 * - Free the old arrays.
8945 *
8946 * If anything fails, make ScreenLines NULL, so we don't do anything!
8947 * Continuing with the old ScreenLines may result in a crash, because the
8948 * size is wrong.
8949 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008950 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008952 if (aucmd_win != NULL)
8953 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
8955 new_ScreenLines = (schar_T *)lalloc((long_u)(
8956 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8957#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01008958 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 if (enc_utf8)
8960 {
8961 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
8962 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008963 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01008964 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
8966 }
8967 if (enc_dbcs == DBCS_JPNU)
8968 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
8969 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
8970#endif
8971 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
8972 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
8973 new_LineOffset = (unsigned *)lalloc((long_u)(
8974 Rows * sizeof(unsigned)), FALSE);
8975 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008976 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008978 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 {
8980 if (win_alloc_lines(wp) == FAIL)
8981 {
8982 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008983 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
8985 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008986 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8987 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008988 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008989give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008991#ifdef FEAT_MBYTE
8992 for (i = 0; i < p_mco; ++i)
8993 if (new_ScreenLinesC[i] == NULL)
8994 break;
8995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 if (new_ScreenLines == NULL
8997#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008998 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9000#endif
9001 || new_ScreenAttrs == NULL
9002 || new_LineOffset == NULL
9003 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009004 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 || outofmem)
9006 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009007 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009008 {
9009 /* guess the size */
9010 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9011
9012 /* Remember we did this to avoid getting outofmem messages over
9013 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009014 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009015 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009016 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009018 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009019 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009020 VIM_CLEAR(new_ScreenLinesC[i]);
9021 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009023 VIM_CLEAR(new_ScreenAttrs);
9024 VIM_CLEAR(new_LineOffset);
9025 VIM_CLEAR(new_LineWraps);
9026 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
9028 else
9029 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009030 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 for (new_row = 0; new_row < Rows; ++new_row)
9033 {
9034 new_LineOffset[new_row] = new_row * Columns;
9035 new_LineWraps[new_row] = FALSE;
9036
9037 /*
9038 * If the screen is not going to be cleared, copy as much as
9039 * possible from the old screen to the new one and clear the rest
9040 * (used when resizing the window at the "--more--" prompt or when
9041 * executing an external command, for the GUI).
9042 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009043 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 {
9045 (void)vim_memset(new_ScreenLines + new_row * Columns,
9046 ' ', (size_t)Columns * sizeof(schar_T));
9047#ifdef FEAT_MBYTE
9048 if (enc_utf8)
9049 {
9050 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9051 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009052 for (i = 0; i < p_mco; ++i)
9053 (void)vim_memset(new_ScreenLinesC[i]
9054 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 0, (size_t)Columns * sizeof(u8char_T));
9056 }
9057 if (enc_dbcs == DBCS_JPNU)
9058 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9059 0, (size_t)Columns * sizeof(schar_T));
9060#endif
9061 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9062 0, (size_t)Columns * sizeof(sattr_T));
9063 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009064 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 {
9066 if (screen_Columns < Columns)
9067 len = screen_Columns;
9068 else
9069 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009070#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009071 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009072 * may be invalid now. Also when p_mco changes. */
9073 if (!(enc_utf8 && ScreenLinesUC == NULL)
9074 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009075#endif
9076 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9077 ScreenLines + LineOffset[old_row],
9078 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009080 if (enc_utf8 && ScreenLinesUC != NULL
9081 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 {
9083 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9084 ScreenLinesUC + LineOffset[old_row],
9085 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009086 for (i = 0; i < p_mco; ++i)
9087 mch_memmove(new_ScreenLinesC[i]
9088 + new_LineOffset[new_row],
9089 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 (size_t)len * sizeof(u8char_T));
9091 }
9092 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9093 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9094 ScreenLines2 + LineOffset[old_row],
9095 (size_t)len * sizeof(schar_T));
9096#endif
9097 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9098 ScreenAttrs + LineOffset[old_row],
9099 (size_t)len * sizeof(sattr_T));
9100 }
9101 }
9102 }
9103 /* Use the last line of the screen for the current line. */
9104 current_ScreenLine = new_ScreenLines + Rows * Columns;
9105 }
9106
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009107 free_screenlines();
9108
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 ScreenLines = new_ScreenLines;
9110#ifdef FEAT_MBYTE
9111 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009112 for (i = 0; i < p_mco; ++i)
9113 ScreenLinesC[i] = new_ScreenLinesC[i];
9114 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 ScreenLines2 = new_ScreenLines2;
9116#endif
9117 ScreenAttrs = new_ScreenAttrs;
9118 LineOffset = new_LineOffset;
9119 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009120 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
9122 /* It's important that screen_Rows and screen_Columns reflect the actual
9123 * size of ScreenLines[]. Set them before calling anything. */
9124#ifdef FEAT_GUI
9125 old_Rows = screen_Rows;
9126#endif
9127 screen_Rows = Rows;
9128 screen_Columns = Columns;
9129
9130 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009131 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 screenclear2();
9133
9134#ifdef FEAT_GUI
9135 else if (gui.in_use
9136 && !gui.starting
9137 && ScreenLines != NULL
9138 && old_Rows != Rows)
9139 {
9140 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9141 /*
9142 * Adjust the position of the cursor, for when executing an external
9143 * command.
9144 */
9145 if (msg_row >= Rows) /* Rows got smaller */
9146 msg_row = Rows - 1; /* put cursor at last row */
9147 else if (Rows > old_Rows) /* Rows got bigger */
9148 msg_row += Rows - old_Rows; /* put cursor in same place */
9149 if (msg_col >= Columns) /* Columns got smaller */
9150 msg_col = Columns - 1; /* put cursor at last column */
9151 }
9152#endif
9153
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009155 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009156
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009157 /*
9158 * Do not apply autocommands more than 3 times to avoid an endless loop
9159 * in case applying autocommands always changes Rows or Columns.
9160 */
9161 if (starting == 0 && ++retry_count <= 3)
9162 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009163 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009164 /* In rare cases, autocommands may have altered Rows or Columns,
9165 * jump back to check if we need to allocate the screen again. */
9166 goto retry;
9167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168}
9169
9170 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009171free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009172{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009173#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009174 int i;
9175
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009176 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009177 for (i = 0; i < Screen_mco; ++i)
9178 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009179 vim_free(ScreenLines2);
9180#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009181 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009182 vim_free(ScreenAttrs);
9183 vim_free(LineOffset);
9184 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009185 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009186}
9187
9188 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009189screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191 check_for_delay(FALSE);
9192 screenalloc(FALSE); /* allocate screen buffers if size changed */
9193 screenclear2(); /* clear the screen */
9194}
9195
9196 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009197screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 int i;
9200
9201 if (starting == NO_SCREEN || ScreenLines == NULL
9202#ifdef FEAT_GUI
9203 || (gui.in_use && gui.starting)
9204#endif
9205 )
9206 return;
9207
9208#ifdef FEAT_GUI
9209 if (!gui.in_use)
9210#endif
9211 screen_attr = -1; /* force setting the Normal colors */
9212 screen_stop_highlight(); /* don't want highlighting here */
9213
9214#ifdef FEAT_CLIPBOARD
9215 /* disable selection without redrawing it */
9216 clip_scroll_selection(9999);
9217#endif
9218
9219 /* blank out ScreenLines */
9220 for (i = 0; i < Rows; ++i)
9221 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009222 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 LineWraps[i] = FALSE;
9224 }
9225
9226 if (can_clear(T_CL))
9227 {
9228 out_str(T_CL); /* clear the display */
9229 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009230 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 }
9232 else
9233 {
9234 /* can't clear the screen, mark all chars with invalid attributes */
9235 for (i = 0; i < Rows; ++i)
9236 lineinvalid(LineOffset[i], (int)Columns);
9237 clear_cmdline = TRUE;
9238 }
9239
9240 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9241
9242 win_rest_invalid(firstwin);
9243 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009244 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 if (must_redraw == CLEAR) /* no need to clear again */
9246 must_redraw = NOT_VALID;
9247 compute_cmdrow();
9248 msg_row = cmdline_row; /* put cursor on last line for messages */
9249 msg_col = 0;
9250 screen_start(); /* don't know where cursor is now */
9251 msg_scrolled = 0; /* can't scroll back */
9252 msg_didany = FALSE;
9253 msg_didout = FALSE;
9254}
9255
9256/*
9257 * Clear one line in ScreenLines.
9258 */
9259 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009260lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9263#ifdef FEAT_MBYTE
9264 if (enc_utf8)
9265 (void)vim_memset(ScreenLinesUC + off, 0,
9266 (size_t)width * sizeof(u8char_T));
9267#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009268 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269}
9270
9271/*
9272 * Mark one line in ScreenLines invalid by setting the attributes to an
9273 * invalid value.
9274 */
9275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009276lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9279}
9280
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281/*
9282 * Copy part of a Screenline for vertically split window "wp".
9283 */
9284 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009285linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286{
9287 unsigned off_to = LineOffset[to] + wp->w_wincol;
9288 unsigned off_from = LineOffset[from] + wp->w_wincol;
9289
9290 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9291 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009292#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (enc_utf8)
9294 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009295 int i;
9296
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9298 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009299 for (i = 0; i < p_mco; ++i)
9300 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9301 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303 if (enc_dbcs == DBCS_JPNU)
9304 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9305 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9308 wp->w_width * sizeof(sattr_T));
9309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310
9311/*
9312 * Return TRUE if clearing with term string "p" would work.
9313 * It can't work when the string is empty or it won't set the right background.
9314 */
9315 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009316can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318 return (*p != NUL && (t_colors <= 1
9319#ifdef FEAT_GUI
9320 || gui.in_use
9321#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009322#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009323 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009324 || (!p_tgc && cterm_normal_bg_color == 0)
9325#else
9326 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009327#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009328 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329}
9330
9331/*
9332 * Reset cursor position. Use whenever cursor was moved because of outputting
9333 * something directly to the screen (shell commands) or a terminal control
9334 * code.
9335 */
9336 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009337screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338{
9339 screen_cur_row = screen_cur_col = 9999;
9340}
9341
9342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 * Move the cursor to position "row","col" in the screen.
9344 * This tries to find the most efficient way to move, minimizing the number of
9345 * characters sent to the terminal.
9346 */
9347 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009348windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009350 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 int i;
9352 int plan;
9353 int cost;
9354 int wouldbe_col;
9355 int noinvcurs;
9356 char_u *bs;
9357 int goto_cost;
9358 int attr;
9359
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009360#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9362
9363#define PLAN_LE 1
9364#define PLAN_CR 2
9365#define PLAN_NL 3
9366#define PLAN_WRITE 4
9367 /* Can't use ScreenLines unless initialized */
9368 if (ScreenLines == NULL)
9369 return;
9370
9371 if (col != screen_cur_col || row != screen_cur_row)
9372 {
9373 /* Check for valid position. */
9374 if (row < 0) /* window without text lines? */
9375 row = 0;
9376 if (row >= screen_Rows)
9377 row = screen_Rows - 1;
9378 if (col >= screen_Columns)
9379 col = screen_Columns - 1;
9380
9381 /* check if no cursor movement is allowed in highlight mode */
9382 if (screen_attr && *T_MS == NUL)
9383 noinvcurs = HIGHL_COST;
9384 else
9385 noinvcurs = 0;
9386 goto_cost = GOTO_COST + noinvcurs;
9387
9388 /*
9389 * Plan how to do the positioning:
9390 * 1. Use CR to move it to column 0, same row.
9391 * 2. Use T_LE to move it a few columns to the left.
9392 * 3. Use NL to move a few lines down, column 0.
9393 * 4. Move a few columns to the right with T_ND or by writing chars.
9394 *
9395 * Don't do this if the cursor went beyond the last column, the cursor
9396 * position is unknown then (some terminals wrap, some don't )
9397 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009398 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 * characters to move the cursor to the right.
9400 */
9401 if (row >= screen_cur_row && screen_cur_col < Columns)
9402 {
9403 /*
9404 * If the cursor is in the same row, bigger col, we can use CR
9405 * or T_LE.
9406 */
9407 bs = NULL; /* init for GCC */
9408 attr = screen_attr;
9409 if (row == screen_cur_row && col < screen_cur_col)
9410 {
9411 /* "le" is preferred over "bc", because "bc" is obsolete */
9412 if (*T_LE)
9413 bs = T_LE; /* "cursor left" */
9414 else
9415 bs = T_BC; /* "backspace character (old) */
9416 if (*bs)
9417 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9418 else
9419 cost = 999;
9420 if (col + 1 < cost) /* using CR is less characters */
9421 {
9422 plan = PLAN_CR;
9423 wouldbe_col = 0;
9424 cost = 1; /* CR is just one character */
9425 }
9426 else
9427 {
9428 plan = PLAN_LE;
9429 wouldbe_col = col;
9430 }
9431 if (noinvcurs) /* will stop highlighting */
9432 {
9433 cost += noinvcurs;
9434 attr = 0;
9435 }
9436 }
9437
9438 /*
9439 * If the cursor is above where we want to be, we can use CR LF.
9440 */
9441 else if (row > screen_cur_row)
9442 {
9443 plan = PLAN_NL;
9444 wouldbe_col = 0;
9445 cost = (row - screen_cur_row) * 2; /* CR LF */
9446 if (noinvcurs) /* will stop highlighting */
9447 {
9448 cost += noinvcurs;
9449 attr = 0;
9450 }
9451 }
9452
9453 /*
9454 * If the cursor is in the same row, smaller col, just use write.
9455 */
9456 else
9457 {
9458 plan = PLAN_WRITE;
9459 wouldbe_col = screen_cur_col;
9460 cost = 0;
9461 }
9462
9463 /*
9464 * Check if any characters that need to be written have the
9465 * correct attributes. Also avoid UTF-8 characters.
9466 */
9467 i = col - wouldbe_col;
9468 if (i > 0)
9469 cost += i;
9470 if (cost < goto_cost && i > 0)
9471 {
9472 /*
9473 * Check if the attributes are correct without additionally
9474 * stopping highlighting.
9475 */
9476 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9477 while (i && *p++ == attr)
9478 --i;
9479 if (i != 0)
9480 {
9481 /*
9482 * Try if it works when highlighting is stopped here.
9483 */
9484 if (*--p == 0)
9485 {
9486 cost += noinvcurs;
9487 while (i && *p++ == 0)
9488 --i;
9489 }
9490 if (i != 0)
9491 cost = 999; /* different attributes, don't do it */
9492 }
9493#ifdef FEAT_MBYTE
9494 if (enc_utf8)
9495 {
9496 /* Don't use an UTF-8 char for positioning, it's slow. */
9497 for (i = wouldbe_col; i < col; ++i)
9498 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9499 {
9500 cost = 999;
9501 break;
9502 }
9503 }
9504#endif
9505 }
9506
9507 /*
9508 * We can do it without term_windgoto()!
9509 */
9510 if (cost < goto_cost)
9511 {
9512 if (plan == PLAN_LE)
9513 {
9514 if (noinvcurs)
9515 screen_stop_highlight();
9516 while (screen_cur_col > col)
9517 {
9518 out_str(bs);
9519 --screen_cur_col;
9520 }
9521 }
9522 else if (plan == PLAN_CR)
9523 {
9524 if (noinvcurs)
9525 screen_stop_highlight();
9526 out_char('\r');
9527 screen_cur_col = 0;
9528 }
9529 else if (plan == PLAN_NL)
9530 {
9531 if (noinvcurs)
9532 screen_stop_highlight();
9533 while (screen_cur_row < row)
9534 {
9535 out_char('\n');
9536 ++screen_cur_row;
9537 }
9538 screen_cur_col = 0;
9539 }
9540
9541 i = col - screen_cur_col;
9542 if (i > 0)
9543 {
9544 /*
9545 * Use cursor-right if it's one character only. Avoids
9546 * removing a line of pixels from the last bold char, when
9547 * using the bold trick in the GUI.
9548 */
9549 if (T_ND[0] != NUL && T_ND[1] == NUL)
9550 {
9551 while (i-- > 0)
9552 out_char(*T_ND);
9553 }
9554 else
9555 {
9556 int off;
9557
9558 off = LineOffset[row] + screen_cur_col;
9559 while (i-- > 0)
9560 {
9561 if (ScreenAttrs[off] != screen_attr)
9562 screen_stop_highlight();
9563#ifdef FEAT_MBYTE
9564 out_flush_check();
9565#endif
9566 out_char(ScreenLines[off]);
9567#ifdef FEAT_MBYTE
9568 if (enc_dbcs == DBCS_JPNU
9569 && ScreenLines[off] == 0x8e)
9570 out_char(ScreenLines2[off]);
9571#endif
9572 ++off;
9573 }
9574 }
9575 }
9576 }
9577 }
9578 else
9579 cost = 999;
9580
9581 if (cost >= goto_cost)
9582 {
9583 if (noinvcurs)
9584 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009585 if (row == screen_cur_row && (col > screen_cur_col)
9586 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 term_cursor_right(col - screen_cur_col);
9588 else
9589 term_windgoto(row, col);
9590 }
9591 screen_cur_row = row;
9592 screen_cur_col = col;
9593 }
9594}
9595
9596/*
9597 * Set cursor to its position in the current window.
9598 */
9599 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009600setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009602 setcursor_mayforce(FALSE);
9603}
9604
9605/*
9606 * Set cursor to its position in the current window.
9607 * When "force" is TRUE also when not redrawing.
9608 */
9609 void
9610setcursor_mayforce(int force)
9611{
9612 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 {
9614 validate_cursor();
9615 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009616 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009618 /* With 'rightleft' set and the cursor on a double-wide
9619 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009620 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009622 (has_mbyte
9623 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9624 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625# endif
9626 1)) :
9627#endif
9628 curwin->w_wcol));
9629 }
9630}
9631
9632
9633/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009634 * Insert 'line_count' lines at 'row' in window 'wp'.
9635 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9636 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * scrolling.
9638 * Returns FAIL if the lines are not inserted, OK for success.
9639 */
9640 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009641win_ins_lines(
9642 win_T *wp,
9643 int row,
9644 int line_count,
9645 int invalid,
9646 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
9648 int did_delete;
9649 int nextrow;
9650 int lastrow;
9651 int retval;
9652
9653 if (invalid)
9654 wp->w_lines_valid = 0;
9655
9656 if (wp->w_height < 5)
9657 return FAIL;
9658
9659 if (line_count > wp->w_height - row)
9660 line_count = wp->w_height - row;
9661
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009662 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 if (retval != MAYBE)
9664 return retval;
9665
9666 /*
9667 * If there is a next window or a status line, we first try to delete the
9668 * lines at the bottom to avoid messing what is after the window.
9669 * If this fails and there are following windows, don't do anything to avoid
9670 * messing up those windows, better just redraw.
9671 */
9672 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (wp->w_next != NULL || wp->w_status_height)
9674 {
9675 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009676 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 did_delete = TRUE;
9678 else if (wp->w_next)
9679 return FAIL;
9680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 /*
9682 * if no lines deleted, blank the lines that will end up below the window
9683 */
9684 if (!did_delete)
9685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009688 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 lastrow = nextrow + line_count;
9690 if (lastrow > Rows)
9691 lastrow = Rows;
9692 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009693 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 ' ', ' ', 0);
9695 }
9696
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009697 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 == FAIL)
9699 {
9700 /* deletion will have messed up other windows */
9701 if (did_delete)
9702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 win_rest_invalid(W_NEXT(wp));
9705 }
9706 return FAIL;
9707 }
9708
9709 return OK;
9710}
9711
9712/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009713 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9715 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9716 * scrolling
9717 * Return OK for success, FAIL if the lines are not deleted.
9718 */
9719 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009720win_del_lines(
9721 win_T *wp,
9722 int row,
9723 int line_count,
9724 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009725 int mayclear,
9726 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727{
9728 int retval;
9729
9730 if (invalid)
9731 wp->w_lines_valid = 0;
9732
9733 if (line_count > wp->w_height - row)
9734 line_count = wp->w_height - row;
9735
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009736 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (retval != MAYBE)
9738 return retval;
9739
9740 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009741 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 return FAIL;
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 /*
9745 * If there are windows or status lines below, try to put them at the
9746 * correct place. If we can't do that, they have to be redrawn.
9747 */
9748 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9749 {
9750 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009751 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 {
9753 wp->w_redr_status = TRUE;
9754 win_rest_invalid(wp->w_next);
9755 }
9756 }
9757 /*
9758 * If this is the last window and there is no status line, redraw the
9759 * command line later.
9760 */
9761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 redraw_cmdline = TRUE;
9763 return OK;
9764}
9765
9766/*
9767 * Common code for win_ins_lines() and win_del_lines().
9768 * Returns OK or FAIL when the work has been done.
9769 * Returns MAYBE when not finished yet.
9770 */
9771 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009772win_do_lines(
9773 win_T *wp,
9774 int row,
9775 int line_count,
9776 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009777 int del,
9778 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
9780 int retval;
9781
9782 if (!redrawing() || line_count <= 0)
9783 return FAIL;
9784
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009785 /* When inserting lines would result in loss of command output, just redraw
9786 * the lines. */
9787 if (no_win_do_lines_ins && !del)
9788 return FAIL;
9789
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009791 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009793 if (!no_win_do_lines_ins)
9794 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 return FAIL;
9796 }
9797
9798 /*
9799 * Delete all remaining lines
9800 */
9801 if (row + line_count >= wp->w_height)
9802 {
9803 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009804 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 ' ', ' ', 0);
9806 return OK;
9807 }
9808
9809 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009810 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009812 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009814 if (!no_win_do_lines_ins)
9815 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816
9817 /*
9818 * If the terminal can set a scroll region, use that.
9819 * Always do this in a vertically split window. This will redraw from
9820 * ScreenLines[] when t_CV isn't defined. That's faster than using
9821 * win_line().
9822 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009823 * a character in the lower right corner of the scroll region may cause a
9824 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009826 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 scroll_region_set(wp, row);
9830 if (del)
9831 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009832 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 else
9834 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009835 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 scroll_region_reset();
9838 return retval;
9839 }
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843
9844 return MAYBE;
9845}
9846
9847/*
9848 * window 'wp' and everything after it is messed up, mark it for redraw
9849 */
9850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009851win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 {
9855 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 wp->w_redr_status = TRUE;
9857 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 }
9859 redraw_cmdline = TRUE;
9860}
9861
9862/*
9863 * The rest of the routines in this file perform screen manipulations. The
9864 * given operation is performed physically on the screen. The corresponding
9865 * change is also made to the internal screen image. In this way, the editor
9866 * anticipates the effect of editing changes on the appearance of the screen.
9867 * That way, when we call screenupdate a complete redraw isn't usually
9868 * necessary. Another advantage is that we can keep adding code to anticipate
9869 * screen changes, and in the meantime, everything still works.
9870 */
9871
9872/*
9873 * types for inserting or deleting lines
9874 */
9875#define USE_T_CAL 1
9876#define USE_T_CDL 2
9877#define USE_T_AL 3
9878#define USE_T_CE 4
9879#define USE_T_DL 5
9880#define USE_T_SR 6
9881#define USE_NL 7
9882#define USE_T_CD 8
9883#define USE_REDRAW 9
9884
9885/*
9886 * insert lines on the screen and update ScreenLines[]
9887 * 'end' is the line after the scrolled part. Normally it is Rows.
9888 * When scrolling region used 'off' is the offset from the top for the region.
9889 * 'row' and 'end' are relative to the start of the region.
9890 *
9891 * return FAIL for failure, OK for success.
9892 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009893 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009894screen_ins_lines(
9895 int off,
9896 int row,
9897 int line_count,
9898 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009899 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009900 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901{
9902 int i;
9903 int j;
9904 unsigned temp;
9905 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009906 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 int type;
9908 int result_empty;
9909 int can_ce = can_clear(T_CE);
9910
9911 /*
9912 * FAIL if
9913 * - there is no valid screen
9914 * - the screen has to be redrawn completely
9915 * - the line count is less than one
9916 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009917 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009919 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9920#ifdef FEAT_CLIPBOARD
9921 || (clip_star.state != SELECT_CLEARED
9922 && redrawing_for_callback > 0)
9923#endif
9924 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 return FAIL;
9926
9927 /*
9928 * There are seven ways to insert lines:
9929 * 0. When in a vertically split window and t_CV isn't set, redraw the
9930 * characters from ScreenLines[].
9931 * 1. Use T_CD (clear to end of display) if it exists and the result of
9932 * the insert is just empty lines
9933 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9934 * present or line_count > 1. It looks better if we do all the inserts
9935 * at once.
9936 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9937 * insert is just empty lines and T_CE is not present or line_count >
9938 * 1.
9939 * 4. Use T_AL (insert line) if it exists.
9940 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9941 * just empty lines.
9942 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9943 * just empty lines.
9944 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9945 * the 'da' flag is not set or we have clear line capability.
9946 * 8. redraw the characters from ScreenLines[].
9947 *
9948 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9949 * the scrollbar for the window. It does have insert line, use that if it
9950 * exists.
9951 */
9952 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9954 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009955 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 type = USE_T_CD;
9957 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9958 type = USE_T_CAL;
9959 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9960 type = USE_T_CDL;
9961 else if (*T_AL != NUL)
9962 type = USE_T_AL;
9963 else if (can_ce && result_empty)
9964 type = USE_T_CE;
9965 else if (*T_DL != NUL && result_empty)
9966 type = USE_T_DL;
9967 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9968 type = USE_T_SR;
9969 else
9970 return FAIL;
9971
9972 /*
9973 * For clearing the lines screen_del_lines() is used. This will also take
9974 * care of t_db if necessary.
9975 */
9976 if (type == USE_T_CD || type == USE_T_CDL ||
9977 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009978 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979
9980 /*
9981 * If text is retained below the screen, first clear or delete as many
9982 * lines at the bottom of the window as are about to be inserted so that
9983 * the deleted lines won't later surface during a screen_del_lines.
9984 */
9985 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009986 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987
9988#ifdef FEAT_CLIPBOARD
9989 /* Remove a modeless selection when inserting lines halfway the screen
9990 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009991 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009992 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 else
9994 clip_scroll_selection(-line_count);
9995#endif
9996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997#ifdef FEAT_GUI
9998 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9999 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010000 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001#endif
10002
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010003 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10004 cursor_col = wp->w_wincol;
10005
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (*T_CCS != NUL) /* cursor relative to region */
10007 cursor_row = row;
10008 else
10009 cursor_row = row + off;
10010
10011 /*
10012 * Shift LineOffset[] line_count down to reflect the inserted lines.
10013 * Clear the inserted lines in ScreenLines[].
10014 */
10015 row += off;
10016 end += off;
10017 for (i = 0; i < line_count; ++i)
10018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 if (wp != NULL && wp->w_width != Columns)
10020 {
10021 /* need to copy part of a line */
10022 j = end - 1 - i;
10023 while ((j -= line_count) >= row)
10024 linecopy(j + line_count, j, wp);
10025 j += line_count;
10026 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010027 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10028 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 else
10030 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10031 LineWraps[j] = FALSE;
10032 }
10033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 {
10035 j = end - 1 - i;
10036 temp = LineOffset[j];
10037 while ((j -= line_count) >= row)
10038 {
10039 LineOffset[j + line_count] = LineOffset[j];
10040 LineWraps[j + line_count] = LineWraps[j];
10041 }
10042 LineOffset[j + line_count] = temp;
10043 LineWraps[j + line_count] = FALSE;
10044 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010045 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 else
10047 lineinvalid(temp, (int)Columns);
10048 }
10049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
10051 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010052 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010053 if (clear_attr != 0)
10054 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 /* redraw the characters */
10057 if (type == USE_REDRAW)
10058 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010059 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 {
10061 term_append_lines(line_count);
10062 screen_start(); /* don't know where cursor is now */
10063 }
10064 else
10065 {
10066 for (i = 0; i < line_count; i++)
10067 {
10068 if (type == USE_T_AL)
10069 {
10070 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010071 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 out_str(T_AL);
10073 }
10074 else /* type == USE_T_SR */
10075 out_str(T_SR);
10076 screen_start(); /* don't know where cursor is now */
10077 }
10078 }
10079
10080 /*
10081 * With scroll-reverse and 'da' flag set we need to clear the lines that
10082 * have been scrolled down into the region.
10083 */
10084 if (type == USE_T_SR && *T_DA)
10085 {
10086 for (i = 0; i < line_count; ++i)
10087 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010088 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 out_str(T_CE);
10090 screen_start(); /* don't know where cursor is now */
10091 }
10092 }
10093
10094#ifdef FEAT_GUI
10095 gui_can_update_cursor();
10096 if (gui.in_use)
10097 out_flush(); /* always flush after a scroll */
10098#endif
10099 return OK;
10100}
10101
10102/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010103 * Delete lines on the screen and update ScreenLines[].
10104 * "end" is the line after the scrolled part. Normally it is Rows.
10105 * When scrolling region used "off" is the offset from the top for the region.
10106 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 *
10108 * Return OK for success, FAIL if the lines are not deleted.
10109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010111screen_del_lines(
10112 int off,
10113 int row,
10114 int line_count,
10115 int end,
10116 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010117 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010118 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120 int j;
10121 int i;
10122 unsigned temp;
10123 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010124 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 int cursor_end;
10126 int result_empty; /* result is empty until end of region */
10127 int can_delete; /* deleting line codes can be used */
10128 int type;
10129
10130 /*
10131 * FAIL if
10132 * - there is no valid screen
10133 * - the screen has to be redrawn completely
10134 * - the line count is less than one
10135 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010136 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010138 if (!screen_valid(TRUE) || line_count <= 0
10139 || (!force && line_count > p_ttyscroll)
10140#ifdef FEAT_CLIPBOARD
10141 || (clip_star.state != SELECT_CLEARED
10142 && redrawing_for_callback > 0)
10143#endif
10144 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 return FAIL;
10146
10147 /*
10148 * Check if the rest of the current region will become empty.
10149 */
10150 result_empty = row + line_count >= end;
10151
10152 /*
10153 * We can delete lines only when 'db' flag not set or when 'ce' option
10154 * available.
10155 */
10156 can_delete = (*T_DB == NUL || can_clear(T_CE));
10157
10158 /*
10159 * There are six ways to delete lines:
10160 * 0. When in a vertically split window and t_CV isn't set, redraw the
10161 * characters from ScreenLines[].
10162 * 1. Use T_CD if it exists and the result is empty.
10163 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10164 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10165 * none of the other ways work.
10166 * 4. Use T_CE (erase line) if the result is empty.
10167 * 5. Use T_DL (delete line) if it exists.
10168 * 6. redraw the characters from ScreenLines[].
10169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10171 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010172 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 type = USE_T_CD;
10174#if defined(__BEOS__) && defined(BEOS_DR8)
10175 /*
10176 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10177 * its internal termcap... this works okay for tests which test *T_DB !=
10178 * NUL. It has the disadvantage that the user cannot use any :set t_*
10179 * command to get T_DB (back) to empty_option, only :set term=... will do
10180 * the trick...
10181 * Anyway, this hack will hopefully go away with the next OS release.
10182 * (Olaf Seibert)
10183 */
10184 else if (row == 0 && T_DB == empty_option
10185 && (line_count == 1 || *T_CDL == NUL))
10186#else
10187 else if (row == 0 && (
10188#ifndef AMIGA
10189 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10190 * up, so use delete-line command */
10191 line_count == 1 ||
10192#endif
10193 *T_CDL == NUL))
10194#endif
10195 type = USE_NL;
10196 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10197 type = USE_T_CDL;
10198 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010199 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 type = USE_T_CE;
10201 else if (*T_DL != NUL && can_delete)
10202 type = USE_T_DL;
10203 else if (*T_CDL != NUL && can_delete)
10204 type = USE_T_CDL;
10205 else
10206 return FAIL;
10207
10208#ifdef FEAT_CLIPBOARD
10209 /* Remove a modeless selection when deleting lines halfway the screen or
10210 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010211 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010212 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 else
10214 clip_scroll_selection(line_count);
10215#endif
10216
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217#ifdef FEAT_GUI
10218 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10219 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010220 gui_dont_update_cursor(gui.cursor_row >= row + off
10221 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222#endif
10223
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010224 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10225 cursor_col = wp->w_wincol;
10226
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 if (*T_CCS != NUL) /* cursor relative to region */
10228 {
10229 cursor_row = row;
10230 cursor_end = end;
10231 }
10232 else
10233 {
10234 cursor_row = row + off;
10235 cursor_end = end + off;
10236 }
10237
10238 /*
10239 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10240 * Clear the inserted lines in ScreenLines[].
10241 */
10242 row += off;
10243 end += off;
10244 for (i = 0; i < line_count; ++i)
10245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if (wp != NULL && wp->w_width != Columns)
10247 {
10248 /* need to copy part of a line */
10249 j = row + i;
10250 while ((j += line_count) <= end - 1)
10251 linecopy(j - line_count, j, wp);
10252 j -= line_count;
10253 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010254 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10255 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 else
10257 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10258 LineWraps[j] = FALSE;
10259 }
10260 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 {
10262 /* whole width, moving the line pointers is faster */
10263 j = row + i;
10264 temp = LineOffset[j];
10265 while ((j += line_count) <= end - 1)
10266 {
10267 LineOffset[j - line_count] = LineOffset[j];
10268 LineWraps[j - line_count] = LineWraps[j];
10269 }
10270 LineOffset[j - line_count] = temp;
10271 LineWraps[j - line_count] = FALSE;
10272 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010273 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 else
10275 lineinvalid(temp, (int)Columns);
10276 }
10277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010279 if (screen_attr != clear_attr)
10280 screen_stop_highlight();
10281 if (clear_attr != 0)
10282 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 /* redraw the characters */
10285 if (type == USE_REDRAW)
10286 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010287 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010289 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 out_str(T_CD);
10291 screen_start(); /* don't know where cursor is now */
10292 }
10293 else if (type == USE_T_CDL)
10294 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010295 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 term_delete_lines(line_count);
10297 screen_start(); /* don't know where cursor is now */
10298 }
10299 /*
10300 * Deleting lines at top of the screen or scroll region: Just scroll
10301 * the whole screen (scroll region) up by outputting newlines on the
10302 * last line.
10303 */
10304 else if (type == USE_NL)
10305 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010306 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 for (i = line_count; --i >= 0; )
10308 out_char('\n'); /* cursor will remain on same line */
10309 }
10310 else
10311 {
10312 for (i = line_count; --i >= 0; )
10313 {
10314 if (type == USE_T_DL)
10315 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010316 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 out_str(T_DL); /* delete a line */
10318 }
10319 else /* type == USE_T_CE */
10320 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010321 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 out_str(T_CE); /* erase a line */
10323 }
10324 screen_start(); /* don't know where cursor is now */
10325 }
10326 }
10327
10328 /*
10329 * If the 'db' flag is set, we need to clear the lines that have been
10330 * scrolled up at the bottom of the region.
10331 */
10332 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10333 {
10334 for (i = line_count; i > 0; --i)
10335 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010336 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 out_str(T_CE); /* erase a line */
10338 screen_start(); /* don't know where cursor is now */
10339 }
10340 }
10341
10342#ifdef FEAT_GUI
10343 gui_can_update_cursor();
10344 if (gui.in_use)
10345 out_flush(); /* always flush after a scroll */
10346#endif
10347
10348 return OK;
10349}
10350
10351/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010352 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 *
10354 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10355 * If clear_cmdline is FALSE there may be a message there that needs to be
10356 * cleared only if a mode is shown.
10357 * Return the length of the message (0 if no message).
10358 */
10359 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010360showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361{
10362 int need_clear;
10363 int length = 0;
10364 int do_mode;
10365 int attr;
10366 int nwr_save;
10367#ifdef FEAT_INS_EXPAND
10368 int sub_attr;
10369#endif
10370
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010371 do_mode = ((p_smd && msg_silent == 0)
10372 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010373 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010374 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010375 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 {
10377 /*
10378 * Don't show mode right now, when not redrawing or inside a mapping.
10379 * Call char_avail() only when we are going to show something, because
10380 * it takes a bit of time.
10381 */
10382 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10383 {
10384 redraw_cmdline = TRUE; /* show mode later */
10385 return 0;
10386 }
10387
10388 nwr_save = need_wait_return;
10389
10390 /* wait a bit before overwriting an important message */
10391 check_for_delay(FALSE);
10392
10393 /* if the cmdline is more than one line high, erase top lines */
10394 need_clear = clear_cmdline;
10395 if (clear_cmdline && cmdline_row < Rows - 1)
10396 msg_clr_cmdline(); /* will reset clear_cmdline */
10397
10398 /* Position on the last line in the window, column 0 */
10399 msg_pos_mode();
10400 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010401 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 if (do_mode)
10403 {
10404 MSG_PUTS_ATTR("--", attr);
10405#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010406 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010407# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010408 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010409# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010410 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010411# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010412 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010413# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 MSG_PUTS_ATTR(" IM", attr);
10415# else
10416 MSG_PUTS_ATTR(" XIM", attr);
10417# endif
10418#endif
10419#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10420 if (gui.in_use)
10421 {
10422 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010423 {
10424 /* HANGUL */
10425 if (enc_utf8)
10426 MSG_PUTS_ATTR(" \355\225\234\352\270\200", attr);
10427 else
10428 MSG_PUTS_ATTR(" \307\321\261\333", attr);
10429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 }
10431#endif
10432#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010433 /* CTRL-X in Insert mode */
10434 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 {
10436 /* These messages can get long, avoid a wrap in a narrow
10437 * window. Prefer showing edit_submode_extra. */
10438 length = (Rows - msg_row) * Columns - 3;
10439 if (edit_submode_extra != NULL)
10440 length -= vim_strsize(edit_submode_extra);
10441 if (length > 0)
10442 {
10443 if (edit_submode_pre != NULL)
10444 length -= vim_strsize(edit_submode_pre);
10445 if (length - vim_strsize(edit_submode) > 0)
10446 {
10447 if (edit_submode_pre != NULL)
10448 msg_puts_attr(edit_submode_pre, attr);
10449 msg_puts_attr(edit_submode, attr);
10450 }
10451 if (edit_submode_extra != NULL)
10452 {
10453 MSG_PUTS_ATTR(" ", attr); /* add a space in between */
10454 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010455 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 else
10457 sub_attr = attr;
10458 msg_puts_attr(edit_submode_extra, sub_attr);
10459 }
10460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 else
10463#endif
10464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 if (State & VREPLACE_FLAG)
10466 MSG_PUTS_ATTR(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010467 else if (State & REPLACE_FLAG)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 MSG_PUTS_ATTR(_(" REPLACE"), attr);
10469 else if (State & INSERT)
10470 {
10471#ifdef FEAT_RIGHTLEFT
10472 if (p_ri)
10473 MSG_PUTS_ATTR(_(" REVERSE"), attr);
10474#endif
10475 MSG_PUTS_ATTR(_(" INSERT"), attr);
10476 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010477 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 MSG_PUTS_ATTR(_(" (insert)"), attr);
10479 else if (restart_edit == 'R')
10480 MSG_PUTS_ATTR(_(" (replace)"), attr);
10481 else if (restart_edit == 'V')
10482 MSG_PUTS_ATTR(_(" (vreplace)"), attr);
10483#ifdef FEAT_RIGHTLEFT
10484 if (p_hkmap)
10485 MSG_PUTS_ATTR(_(" Hebrew"), attr);
10486# ifdef FEAT_FKMAP
10487 if (p_fkmap)
10488 MSG_PUTS_ATTR(farsi_text_5, attr);
10489# endif
10490#endif
10491#ifdef FEAT_KEYMAP
10492 if (State & LANGMAP)
10493 {
10494# ifdef FEAT_ARABIC
10495 if (curwin->w_p_arab)
10496 MSG_PUTS_ATTR(_(" Arabic"), attr);
10497 else
10498# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010499 if (get_keymap_str(curwin, (char_u *)" (%s)",
10500 NameBuff, MAXPATHL))
10501 MSG_PUTS_ATTR(NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 }
10503#endif
10504 if ((State & INSERT) && p_paste)
10505 MSG_PUTS_ATTR(_(" (paste)"), attr);
10506
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 if (VIsual_active)
10508 {
10509 char *p;
10510
10511 /* Don't concatenate separate words to avoid translation
10512 * problems. */
10513 switch ((VIsual_select ? 4 : 0)
10514 + (VIsual_mode == Ctrl_V) * 2
10515 + (VIsual_mode == 'V'))
10516 {
10517 case 0: p = N_(" VISUAL"); break;
10518 case 1: p = N_(" VISUAL LINE"); break;
10519 case 2: p = N_(" VISUAL BLOCK"); break;
10520 case 4: p = N_(" SELECT"); break;
10521 case 5: p = N_(" SELECT LINE"); break;
10522 default: p = N_(" SELECT BLOCK"); break;
10523 }
10524 MSG_PUTS_ATTR(_(p), attr);
10525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 MSG_PUTS_ATTR(" --", attr);
10527 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010528
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 need_clear = TRUE;
10530 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010531 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#ifdef FEAT_INS_EXPAND
10533 && edit_submode == NULL /* otherwise it gets too long */
10534#endif
10535 )
10536 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010537 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 need_clear = TRUE;
10539 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010540
10541 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 if (need_clear || clear_cmdline)
10543 msg_clr_eos();
10544 msg_didout = FALSE; /* overwrite this message */
10545 length = msg_col;
10546 msg_col = 0;
10547 need_wait_return = nwr_save; /* never ask for hit-return for this */
10548 }
10549 else if (clear_cmdline && msg_silent == 0)
10550 /* Clear the whole command line. Will reset "clear_cmdline". */
10551 msg_clr_cmdline();
10552
10553#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 /* In Visual mode the size of the selected area must be redrawn. */
10555 if (VIsual_active)
10556 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557
10558 /* If the last window has no status line, the ruler is after the mode
10559 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010560 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010561 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562#endif
10563 redraw_cmdline = FALSE;
10564 clear_cmdline = FALSE;
10565
10566 return length;
10567}
10568
10569/*
10570 * Position for a mode message.
10571 */
10572 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010573msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574{
10575 msg_col = 0;
10576 msg_row = Rows - 1;
10577}
10578
10579/*
10580 * Delete mode message. Used when ESC is typed which is expected to end
10581 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010582 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 */
10584 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010585unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586{
10587 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010588 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 */
10590 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10591 redraw_cmdline = TRUE; /* delete mode later */
10592 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010593 clearmode();
10594}
10595
10596/*
10597 * Clear the mode message.
10598 */
10599 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010600clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010601{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010602 int save_msg_row = msg_row;
10603 int save_msg_col = msg_col;
10604
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010605 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010606 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010607 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010608 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010609
10610 msg_col = save_msg_col;
10611 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612}
10613
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010614 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010615recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010616{
10617 MSG_PUTS_ATTR(_("recording"), attr);
10618 if (!shortmess(SHM_RECORDING))
10619 {
10620 char_u s[4];
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010621 sprintf((char *)s, " @%c", reg_recording);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010622 MSG_PUTS_ATTR(s, attr);
10623 }
10624}
10625
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626/*
10627 * Draw the tab pages line at the top of the Vim window.
10628 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010629 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010630draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010631{
10632 int tabcount = 0;
10633 tabpage_T *tp;
10634 int tabwidth;
10635 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010636 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010637 int attr;
10638 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010639 win_T *cwp;
10640 int wincount;
10641 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010642 int c;
10643 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010644 int attr_sel = HL_ATTR(HLF_TPS);
10645 int attr_nosel = HL_ATTR(HLF_TP);
10646 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010647 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010648 int room;
10649 int use_sep_chars = (t_colors < 8
10650#ifdef FEAT_GUI
10651 && !gui.in_use
10652#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010653#ifdef FEAT_TERMGUICOLORS
10654 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010655#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010656 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010657
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010658 if (ScreenLines == NULL)
10659 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010660 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010661
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010662#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010663 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010664 if (gui_use_tabline())
10665 {
10666 gui_update_tabline();
10667 return;
10668 }
10669#endif
10670
10671 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010672 return;
10673
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010674#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010675
10676 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10677 for (scol = 0; scol < Columns; ++scol)
10678 TabPageIdxs[scol] = 0;
10679
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010680 /* Use the 'tabline' option if it's set. */
10681 if (*p_tal != NUL)
10682 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010683 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010684
10685 /* Check for an error. If there is one we would loop in redrawing the
10686 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010687 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010688 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010689 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010690 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010691 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010692 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010693 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010695#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010696 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010697 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010698 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010699
Bram Moolenaar238a5642006-02-21 22:12:05 +000010700 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10701 if (tabwidth < 6)
10702 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010703
Bram Moolenaar238a5642006-02-21 22:12:05 +000010704 attr = attr_nosel;
10705 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010706 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010707 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10708 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010709 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010711
Bram Moolenaar238a5642006-02-21 22:12:05 +000010712 if (tp->tp_topframe == topframe)
10713 attr = attr_sel;
10714 if (use_sep_chars && col > 0)
10715 screen_putchar('|', 0, col++, attr);
10716
10717 if (tp->tp_topframe != topframe)
10718 attr = attr_nosel;
10719
10720 screen_putchar(' ', 0, col++, attr);
10721
10722 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010723 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010724 cwp = curwin;
10725 wp = firstwin;
10726 }
10727 else
10728 {
10729 cwp = tp->tp_curwin;
10730 wp = tp->tp_firstwin;
10731 }
10732
10733 modified = FALSE;
10734 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10735 if (bufIsChanged(wp->w_buffer))
10736 modified = TRUE;
10737 if (modified || wincount > 1)
10738 {
10739 if (wincount > 1)
10740 {
10741 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010742 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010743 if (col + len >= Columns - 3)
10744 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010745 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010746#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010747 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010748#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010749 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010750#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010751 );
10752 col += len;
10753 }
10754 if (modified)
10755 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10756 screen_putchar(' ', 0, col++, attr);
10757 }
10758
10759 room = scol - col + tabwidth - 1;
10760 if (room > 0)
10761 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010762 /* Get buffer name in NameBuff[] */
10763 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010764 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010765 len = vim_strsize(NameBuff);
10766 p = NameBuff;
10767#ifdef FEAT_MBYTE
10768 if (has_mbyte)
10769 while (len > room)
10770 {
10771 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010772 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010773 }
10774 else
10775#endif
10776 if (len > room)
10777 {
10778 p += len - room;
10779 len = room;
10780 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010781 if (len > Columns - col - 1)
10782 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010784 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010785 col += len;
10786 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010787 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010788
10789 /* Store the tab page number in TabPageIdxs[], so that
10790 * jump_to_mouse() knows where each one is. */
10791 ++tabcount;
10792 while (scol < col)
10793 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010794 }
10795
Bram Moolenaar238a5642006-02-21 22:12:05 +000010796 if (use_sep_chars)
10797 c = '_';
10798 else
10799 c = ' ';
10800 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010801
10802 /* Put an "X" for closing the current tab if there are several. */
10803 if (first_tabpage->tp_next != NULL)
10804 {
10805 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10806 TabPageIdxs[Columns - 1] = -999;
10807 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010808 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010809
10810 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10811 * set. */
10812 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010813}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010814
10815/*
10816 * Get buffer name for "buf" into NameBuff[].
10817 * Takes care of special buffer names and translates special characters.
10818 */
10819 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010820get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010821{
10822 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010823 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010824 else
10825 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10826 trans_characters(NameBuff, MAXPATHL);
10827}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010828
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829/*
10830 * Get the character to use in a status line. Get its attributes in "*attr".
10831 */
10832 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010833fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834{
10835 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010836
10837#ifdef FEAT_TERMINAL
10838 if (bt_terminal(wp->w_buffer))
10839 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010840 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010841 {
10842 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010843 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010844 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010845 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010846 {
10847 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010848 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010849 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010850 }
10851 else
10852#endif
10853 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010855 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 fill = fill_stl;
10857 }
10858 else
10859 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010860 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 fill = fill_stlnc;
10862 }
10863 /* Use fill when there is highlighting, and highlighting of current
10864 * window differs, or the fillchars differ, or this is not the
10865 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010866 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010867 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 || (fill_stl != fill_stlnc)))
10869 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010870 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 return '^';
10872 return '=';
10873}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875/*
10876 * Get the character to use in a separator between vertically split windows.
10877 * Get its attributes in "*attr".
10878 */
10879 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010880fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010882 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 if (*attr == 0 && fill_vert == ' ')
10884 return '|';
10885 else
10886 return fill_vert;
10887}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888
10889/*
10890 * Return TRUE if redrawing should currently be done.
10891 */
10892 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010893redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010895#ifdef FEAT_EVAL
10896 if (disable_redraw_for_testing)
10897 return 0;
10898 else
10899#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010900 return ((!RedrawingDisabled
10901#ifdef FEAT_EVAL
10902 || ignore_redraw_flag_for_testing
10903#endif
10904 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905}
10906
10907/*
10908 * Return TRUE if printing messages should currently be done.
10909 */
10910 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010911messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913 return (!(p_lz && char_avail() && !KeyTyped));
10914}
10915
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010916#ifdef FEAT_MENU
10917/*
10918 * Draw the window toolbar.
10919 */
10920 static void
10921redraw_win_toolbar(win_T *wp)
10922{
10923 vimmenu_T *menu;
10924 int item_idx = 0;
10925 int item_count = 0;
10926 int col = 0;
10927 int next_col;
10928 int off = (int)(current_ScreenLine - ScreenLines);
10929 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10930 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10931
10932 vim_free(wp->w_winbar_items);
10933 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10934 ++item_count;
10935 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10936 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10937
10938 /* TODO: use fewer spaces if there is not enough room */
10939 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010940 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010941 {
10942 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010943 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010944 break;
10945 if (col > 1)
10946 {
10947 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010948 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010949 break;
10950 }
10951
10952 wp->w_winbar_items[item_idx].wb_startcol = col;
10953 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010954 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010955 break;
10956
10957 next_col = text_to_screenline(wp, menu->name, col);
10958 while (col < next_col)
10959 {
10960 ScreenAttrs[off + col] = button_attr;
10961 ++col;
10962 }
10963 wp->w_winbar_items[item_idx].wb_endcol = col;
10964 wp->w_winbar_items[item_idx].wb_menu = menu;
10965 ++item_idx;
10966
Bram Moolenaar02631462017-09-22 15:20:32 +020010967 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010968 break;
10969 space_to_screenline(off + col, button_attr);
10970 ++col;
10971 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010972 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010973 {
10974 space_to_screenline(off + col, fill_attr);
10975 ++col;
10976 }
10977 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10978
Bram Moolenaar02631462017-09-22 15:20:32 +020010979 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
10980 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010981}
10982#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010983
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984/*
10985 * Show current status info in ruler and various other places
10986 * If always is FALSE, only show ruler if position has changed.
10987 */
10988 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010989showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
10991 if (!always && !redrawing())
10992 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010993#ifdef FEAT_INS_EXPAND
10994 if (pum_visible())
10995 {
10996 /* Don't redraw right now, do it later. */
10997 curwin->w_redr_status = TRUE;
10998 return;
10999 }
11000#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011001#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011002 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011003 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011004 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 else
11007#endif
11008#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011009 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010#endif
11011
11012#ifdef FEAT_TITLE
11013 if (need_maketitle
11014# ifdef FEAT_STL_OPT
11015 || (p_icon && (stl_syntax & STL_IN_ICON))
11016 || (p_title && (stl_syntax & STL_IN_TITLE))
11017# endif
11018 )
11019 maketitle();
11020#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011021 /* Redraw the tab pages line if needed. */
11022 if (redraw_tabline)
11023 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
11026#ifdef FEAT_CMDL_INFO
11027 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011028win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011030#define RULER_BUF_LEN 70
11031 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 int row;
11033 int fillchar;
11034 int attr;
11035 int empty_line = FALSE;
11036 colnr_T virtcol;
11037 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011038 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 int this_ru_col;
11041 int off = 0;
11042 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043
11044 /* If 'ruler' off or redrawing disabled, don't do anything */
11045 if (!p_ru)
11046 return;
11047
11048 /*
11049 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11050 * after deleting lines, before cursor.lnum is corrected.
11051 */
11052 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11053 return;
11054
11055#ifdef FEAT_INS_EXPAND
11056 /* Don't draw the ruler while doing insert-completion, it might overwrite
11057 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 if (edit_submode != NULL)
11060 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011061 // Don't draw the ruler when the popup menu is visible, it may overlap.
11062 // Except when the popup menu will be redrawn anyway.
11063 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011064 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065#endif
11066
11067#ifdef FEAT_STL_OPT
11068 if (*p_ruf)
11069 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011070 int save_called_emsg = called_emsg;
11071
11072 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011074 if (called_emsg)
11075 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011076 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011077 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 return;
11079 }
11080#endif
11081
11082 /*
11083 * Check if not in Insert mode and the line is empty (will show "0-1").
11084 */
11085 if (!(State & INSERT)
11086 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11087 empty_line = TRUE;
11088
11089 /*
11090 * Only draw the ruler when something changed.
11091 */
11092 validate_virtcol_win(wp);
11093 if ( redraw_cmdline
11094 || always
11095 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11096 || wp->w_cursor.col != wp->w_ru_cursor.col
11097 || wp->w_virtcol != wp->w_ru_virtcol
11098#ifdef FEAT_VIRTUALEDIT
11099 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11100#endif
11101 || wp->w_topline != wp->w_ru_topline
11102 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11103#ifdef FEAT_DIFF
11104 || wp->w_topfill != wp->w_ru_topfill
11105#endif
11106 || empty_line != wp->w_ru_empty)
11107 {
11108 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 if (wp->w_status_height)
11110 {
11111 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011112 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011113 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011114 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 }
11116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 {
11118 row = Rows - 1;
11119 fillchar = ' ';
11120 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 width = Columns;
11122 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 }
11124
11125 /* In list mode virtcol needs to be recomputed */
11126 virtcol = wp->w_virtcol;
11127 if (wp->w_p_list && lcs_tab1 == NUL)
11128 {
11129 wp->w_p_list = FALSE;
11130 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11131 wp->w_p_list = TRUE;
11132 }
11133
11134 /*
11135 * Some sprintfs return the length, some return a pointer.
11136 * To avoid portability problems we use strlen() here.
11137 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011138 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11140 ? 0L
11141 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011142 len = STRLEN(buffer);
11143 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11145 (int)virtcol + 1);
11146
11147 /*
11148 * Add a "50%" if there is room for it.
11149 * On the last line, don't print in the last column (scrolls the
11150 * screen up on some terminals).
11151 */
11152 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011153 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 this_ru_col = ru_col - (Columns - width);
11158 if (this_ru_col < 0)
11159 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 /* Never use more than half the window/screen width, leave the other
11161 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011162 if (this_ru_col < (width + 1) / 2)
11163 this_ru_col = (width + 1) / 2;
11164 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011166 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011167 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 {
11169#ifdef FEAT_MBYTE
11170 if (has_mbyte)
11171 i += (*mb_char2bytes)(fillchar, buffer + i);
11172 else
11173#endif
11174 buffer[i++] = fillchar;
11175 ++o;
11176 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011177 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 }
11179 /* Truncate at window boundary. */
11180#ifdef FEAT_MBYTE
11181 if (has_mbyte)
11182 {
11183 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011184 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185 {
11186 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011187 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 {
11189 buffer[i] = NUL;
11190 break;
11191 }
11192 }
11193 }
11194 else
11195#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011196 if (this_ru_col + (int)STRLEN(buffer) > width)
11197 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198
Bram Moolenaar4033c552017-09-16 20:54:51 +020011199 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 i = redraw_cmdline;
11201 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011202 this_ru_col + off + (int)STRLEN(buffer),
11203 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 fillchar, fillchar, attr);
11205 /* don't redraw the cmdline because of showing the ruler */
11206 redraw_cmdline = i;
11207 wp->w_ru_cursor = wp->w_cursor;
11208 wp->w_ru_virtcol = wp->w_virtcol;
11209 wp->w_ru_empty = empty_line;
11210 wp->w_ru_topline = wp->w_topline;
11211 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11212#ifdef FEAT_DIFF
11213 wp->w_ru_topfill = wp->w_topfill;
11214#endif
11215 }
11216}
11217#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011218
11219#if defined(FEAT_LINEBREAK) || defined(PROTO)
11220/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011221 * Return the width of the 'number' and 'relativenumber' column.
11222 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011223 * Otherwise it depends on 'numberwidth' and the line count.
11224 */
11225 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011226number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011227{
11228 int n;
11229 linenr_T lnum;
11230
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011231 if (wp->w_p_rnu && !wp->w_p_nu)
11232 /* cursor line shows "0" */
11233 lnum = wp->w_height;
11234 else
11235 /* cursor line shows absolute line number */
11236 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011237
Bram Moolenaar6b314672015-03-20 15:42:10 +010011238 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011239 return wp->w_nrwidth_width;
11240 wp->w_nrwidth_line_count = lnum;
11241
11242 n = 0;
11243 do
11244 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011245 lnum /= 10;
11246 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011247 } while (lnum > 0);
11248
11249 /* 'numberwidth' gives the minimal width plus one */
11250 if (n < wp->w_p_nuw - 1)
11251 n = wp->w_p_nuw - 1;
11252
11253 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011254 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011255 return n;
11256}
11257#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011258
11259/*
11260 * Return the current cursor column. This is the actual position on the
11261 * screen. First column is 0.
11262 */
11263 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011264screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011265{
11266 return screen_cur_col;
11267}
11268
11269/*
11270 * Return the current cursor row. This is the actual position on the screen.
11271 * First row is 0.
11272 */
11273 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011274screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011275{
11276 return screen_cur_row;
11277}