blob: 7354220cbb40986de7ffc560353d4ff89c0ab2a2 [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 Moolenaar4d784b22019-05-25 19:51:39 +0200124#ifdef FEAT_TEXT_PROP
125static void update_popups(void);
126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100129static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static 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 +0100155static void win_rest_invalid(win_T *wp);
156static void msg_pos_mode(void);
157static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200184// flags for screen_line()
185#define SLF_RIGHTLEFT 1
186#define SLF_POPUP 2
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100191 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200204 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100243 // This may be needed when switching tabs.
244 if (must_redraw < type)
245 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100252redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271redraw_buf_line_later(buf_T *buf, linenr_T lnum)
272{
273 win_T *wp;
274
275 FOR_ALL_WINDOWS(wp)
276 if (wp->w_buffer == buf && lnum >= wp->w_topline
277 && lnum < wp->w_botline)
278 redrawWinline(wp, lnum);
279}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100280#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100281
Bram Moolenaar113e1072019-01-20 15:30:40 +0100282#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100283 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284redraw_buf_and_status_later(buf_T *buf, int type)
285{
286 win_T *wp;
287
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200289 if (wild_menu_showing != 0)
290 /* Don't redraw while the command line completion is displayed, it
291 * would disappear. */
292 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294 FOR_ALL_WINDOWS(wp)
295 {
296 if (wp->w_buffer == buf)
297 {
298 redraw_win_later(wp, type);
299 wp->w_redr_status = TRUE;
300 }
301 }
302}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100303#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200304
Bram Moolenaar113e1072019-01-20 15:30:40 +0100305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 * Redraw as soon as possible. When the command line is not scrolled redraw
308 * right away and restore what was on the command line.
309 * Return a code indicating what happened.
310 */
311 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100312redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313{
314 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 int r;
317 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline; /* copy from ScreenLines[] */
319 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200323 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324
325 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 return ret;
328
329 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 rows = screen_Rows - cmdline_row;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200331 screenline = (schar_T *)lalloc(rows * cols * sizeof(schar_T), FALSE);
332 screenattr = (sattr_T *)lalloc(rows * cols * sizeof(sattr_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenline == NULL || screenattr == NULL)
334 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (enc_utf8)
336 {
337 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar51e14382019-05-25 20:21:28 +0200338 rows * cols * sizeof(u8char_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenlineUC == NULL)
340 ret = 2;
341 for (i = 0; i < p_mco; ++i)
342 {
343 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar51e14382019-05-25 20:21:28 +0200344 rows * cols * sizeof(u8char_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200345 if (screenlineC[i] == NULL)
346 ret = 2;
347 }
348 }
349 if (enc_dbcs == DBCS_JPNU)
350 {
Bram Moolenaar51e14382019-05-25 20:21:28 +0200351 screenline2 = (schar_T *)lalloc(rows * cols * sizeof(schar_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 if (screenline2 == NULL)
353 ret = 2;
354 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 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 }
382
383 update_screen(0);
384 ret = 3;
385
386 if (must_redraw == 0)
387 {
388 int off = (int)(current_ScreenLine - ScreenLines);
389
390 /* Restore the text displayed in the command line area. */
391 for (r = 0; r < rows; ++r)
392 {
393 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200394 screenline + r * cols,
395 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenattr + r * cols,
398 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 if (enc_utf8)
400 {
401 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineUC + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 for (i = 0; i < p_mco; ++i)
405 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenlineC[i] + r * cols,
407 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408 }
409 if (enc_dbcs == DBCS_JPNU)
410 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200411 screenline2 + r * cols,
412 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200413 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 }
415 ret = 4;
416 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418
419 vim_free(screenline);
420 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 if (enc_utf8)
422 {
423 vim_free(screenlineUC);
424 for (i = 0; i < p_mco; ++i)
425 vim_free(screenlineC[i]);
426 }
427 if (enc_dbcs == DBCS_JPNU)
428 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200429
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200430 /* Show the intro message when appropriate. */
431 maybe_intro_message();
432
433 setcursor();
434
Bram Moolenaar2951b772013-07-03 12:45:31 +0200435 return ret;
436}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100437#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200438
439/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100440 * Invoked after an asynchronous callback is called.
441 * If an echo command was used the cursor needs to be put back where
442 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200443 * If "call_update_screen" is FALSE don't call update_screen() when at the
444 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 */
446 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200447redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100448{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200449 ++redrawing_for_callback;
450
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200452 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100453 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200454 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 // Don't redraw when in prompt_for_number().
456 if (cmdline_row > 0)
457 {
458 // Redrawing only works when the screen didn't scroll. Don't clear
459 // wildmenu entries.
460 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200463#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200464 && call_update_screen)
465 update_screen(0);
466
467 // Redraw in the same position, so that the user can continue
468 // editing the command.
469 redrawcmdline_ex(FALSE);
470 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200471 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200472 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100473 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200474 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200475 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100476 setcursor();
477 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100480 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200481 // Don't update the cursor when it is blinking and off to avoid
482 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100483 out_flush_cursor(FALSE, FALSE);
484 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100485#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100486 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200487
488 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100489}
490
491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 * Changed something in the current window, at buffer line "lnum", that
493 * requires that line and possibly other lines to be redrawn.
494 * Used when entering/leaving Insert mode with the cursor on a folded line.
495 * Used to remove the "$" from a change command.
496 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
497 * may become invalid and the whole window will have to be redrawn.
498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100500redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200501 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100502 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200504 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
505 wp->w_redraw_top = lnum;
506 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
507 wp->w_redraw_bot = lnum;
508 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509}
510
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200511 void
512reset_updating_screen(int may_resize_shell UNUSED)
513{
514 updating_screen = FALSE;
515#ifdef FEAT_GUI
516 if (may_resize_shell)
517 gui_may_resize_shell();
518#endif
519#ifdef FEAT_TERMINAL
520 term_check_channel_closed_recently();
521#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200522
523#ifdef HAVE_DROP_FILE
524 // If handle_drop() was called while updating_screen was TRUE need to
525 // handle the drop now.
526 handle_any_postponed_drop();
527#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200528}
529
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200531 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
533 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100534update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
536 redraw_curbuf_later(type);
537 update_screen(type);
538}
539
540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * Based on the current value of curwin->w_topline, transfer a screenfull
542 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 win_T *wp;
550 static int did_intro = FALSE;
551#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
552 int did_one;
553#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200555 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200556 int gui_cursor_col = 0;
557 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200558#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000561 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 if (type == VALID_NO_UPDATE)
566 {
567 no_update = TRUE;
568 type = 0;
569 }
570
Bram Moolenaara3347722019-05-11 21:14:24 +0200571#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200572 {
573 buf_T *buf;
574
575 // Before updating the screen, notify any listeners of changed text.
576 FOR_ALL_BUFFERS(buf)
577 invoke_listeners(buf);
578 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (must_redraw)
582 {
583 if (type < must_redraw) /* use maximal type */
584 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000585
586 /* must_redraw is reset here, so that when we run into some weird
587 * reason to redraw while busy redrawing (e.g., asynchronous
588 * scrolling), or update_topline() in win_update() will cause a
589 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 must_redraw = 0;
591 }
592
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200593 /* May need to update w_lines[]. */
594 if (curwin->w_lines_valid == 0 && type < NOT_VALID
595#ifdef FEAT_TERMINAL
596 && !term_do_update_window(curwin)
597#endif
598 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 type = NOT_VALID;
600
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000601 /* Postpone the redrawing when it's not needed and when being called
602 * recursively. */
603 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 redraw_later(type); /* remember type for next time */
606 must_redraw = type;
607 if (type > INVERTED_ALL)
608 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200611#ifdef FEAT_TEXT_PROP
612 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200613 if (popup_any_visible())
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200614 type = NOT_VALID;
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 updating_screen = TRUE;
618#ifdef FEAT_SYN_HL
619 ++display_tick; /* let syntax code know we're in a next round of
620 * display updating */
621#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200622 if (no_update)
623 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * if the screen was scrolled up when displaying a message, scroll it down
627 */
628 if (msg_scrolled)
629 {
630 clear_cmdline = TRUE;
631 if (msg_scrolled > Rows - 5) /* clearing is faster */
632 type = CLEAR;
633 else if (type != CLEAR)
634 {
635 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200636 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
637 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 type = CLEAR;
639 FOR_ALL_WINDOWS(wp)
640 {
641 if (W_WINROW(wp) < msg_scrolled)
642 {
643 if (W_WINROW(wp) + wp->w_height > msg_scrolled
644 && wp->w_redr_type < REDRAW_TOP
645 && wp->w_lines_valid > 0
646 && wp->w_topline == wp->w_lines[0].wl_lnum)
647 {
648 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
649 wp->w_redr_type = REDRAW_TOP;
650 }
651 else
652 {
653 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200654 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
655 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 }
659 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200660 if (!no_update)
661 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000662 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 msg_scrolled = 0;
665 need_wait_return = FALSE;
666 }
667
668 /* reset cmdline_row now (may have been changed temporarily) */
669 compute_cmdrow();
670
671 /* Check for changed highlighting */
672 if (need_highlight_changed)
673 highlight_changed();
674
675 if (type == CLEAR) /* first clear screen */
676 {
677 screenclear(); /* will reset clear_cmdline */
678 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200679 /* must_redraw may be set indirectly, avoid another redraw later */
680 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682
683 if (clear_cmdline) /* going to clear cmdline (done below) */
684 check_for_delay(FALSE);
685
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200687 /* Force redraw when width of 'number' or 'relativenumber' column
688 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200690 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
691 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000692 curwin->w_redr_type = NOT_VALID;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 /*
696 * Only start redrawing if there is really something to do.
697 */
698 if (type == INVERTED)
699 update_curswant();
700 if (curwin->w_redr_type < type
701 && !((type == VALID
702 && curwin->w_lines[0].wl_valid
703#ifdef FEAT_DIFF
704 && curwin->w_topfill == curwin->w_old_topfill
705 && curwin->w_botfill == curwin->w_old_botfill
706#endif
707 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000709 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
711 && curwin->w_old_visual_mode == VIsual_mode
712 && (curwin->w_valid & VALID_VIRTCOL)
713 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ))
715 curwin->w_redr_type = type;
716
Bram Moolenaar5a305422006-04-28 22:38:25 +0000717 /* Redraw the tab pages line if needed. */
718 if (redraw_tabline || type >= NOT_VALID)
719 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_SYN_HL
722 /*
723 * Correct stored syntax highlighting info for changes in each displayed
724 * buffer. Each buffer must only be done once.
725 */
726 FOR_ALL_WINDOWS(wp)
727 {
728 if (wp->w_buffer->b_mod_set)
729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 win_T *wwp;
731
732 /* Check if we already did this buffer. */
733 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
734 if (wwp->w_buffer == wp->w_buffer)
735 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200736 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 syn_stack_apply_changes(wp->w_buffer);
738 }
739 }
740#endif
741
742 /*
743 * Go from top to bottom through the windows, redrawing the ones that need
744 * it.
745 */
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 did_one = FALSE;
748#endif
749#ifdef FEAT_SEARCH_EXTRA
750 search_hl.rm.regprog = NULL;
751#endif
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (wp->w_redr_type != 0)
755 {
756 cursor_off();
757#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
758 if (!did_one)
759 {
760 did_one = TRUE;
761# ifdef FEAT_SEARCH_EXTRA
762 start_search_hl();
763# endif
764# ifdef FEAT_CLIPBOARD
765 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200766 if (clip_star.available && clip_isautosel_star())
767 clip_update_selection(&clip_star);
768 if (clip_plus.available && clip_isautosel_plus())
769 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770# endif
771#ifdef FEAT_GUI
772 /* Remove the cursor before starting to do anything, because
773 * scrolling may make it difficult to redraw the text under
774 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 {
777 gui_cursor_col = gui.cursor_col;
778 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200780 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 }
784#endif
785 win_update(wp);
786 }
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /* redraw status line after the window to minimize cursor movement */
789 if (wp->w_redr_status)
790 {
791 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200792 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795#if defined(FEAT_SEARCH_EXTRA)
796 end_search_hl();
797#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100798#ifdef FEAT_INS_EXPAND
799 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200800 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* Reset b_mod_set flags. Going through all windows is probably faster
804 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200808 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200812 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#ifdef FEAT_TEXT_PROP
824 // Display popup windows on top of the others.
825 update_popups();
826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_GUI
829 /* Redraw the cursor and update the scrollbars when all screen updating is
830 * done. */
831 if (gui.in_use)
832 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200833 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 mch_disable_flush();
836 out_flush(); /* required before updating the cursor */
837 mch_enable_flush();
838
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 /* Put the GUI position where the cursor was, gui_update_cursor()
840 * uses that. */
841 gui.col = gui_cursor_col;
842 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200843 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100845 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200846 screen_cur_col = gui.col;
847 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200848 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 else
850 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_scrollbars(FALSE);
852 }
853#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200854 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855}
856
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100857#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100858/*
859 * Prepare for updating one or more windows.
860 * Caller must check for "updating_screen" already set to avoid recursiveness.
861 */
862 static void
863update_prepare(void)
864{
865 cursor_off();
866 updating_screen = TRUE;
867#ifdef FEAT_GUI
868 /* Remove the cursor before starting to do anything, because scrolling may
869 * make it difficult to redraw the text under it. */
870 if (gui.in_use)
871 gui_undraw_cursor();
872#endif
873#ifdef FEAT_SEARCH_EXTRA
874 start_search_hl();
875#endif
876}
877
878/*
879 * Finish updating one or more windows.
880 */
881 static void
882update_finish(void)
883{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200884 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 showmode();
886
887# ifdef FEAT_SEARCH_EXTRA
888 end_search_hl();
889# endif
890
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200891 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892
893# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894 /* Redraw the cursor and update the scrollbars when all screen updating is
895 * done. */
896 if (gui.in_use)
897 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100898 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100899 gui_update_scrollbars(FALSE);
900 }
901# endif
902}
903#endif
904
Bram Moolenaar860cae12010-06-05 23:22:07 +0200905#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906/*
907 * Return TRUE if the cursor line in window "wp" may be concealed, according
908 * to the 'concealcursor' option.
909 */
910 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100911conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200912{
913 int c;
914
915 if (*wp->w_p_cocu == NUL)
916 return FALSE;
917 if (get_real_state() & VISUAL)
918 c = 'v';
919 else if (State & INSERT)
920 c = 'i';
921 else if (State & NORMAL)
922 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200923 else if (State & CMDLINE)
924 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200925 else
926 return FALSE;
927 return vim_strchr(wp->w_p_cocu, c) != NULL;
928}
929
930/*
931 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
932 */
933 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200934conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200935{
936 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
937 {
938 need_cursor_line_redraw = TRUE;
939 /* Need to recompute cursor column, e.g., when starting Visual mode
940 * without concealing. */
941 curs_columns(TRUE);
942 }
943}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#endif
945
Bram Moolenaar113e1072019-01-20 15:30:40 +0100946#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100948update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 win_T *wp;
951 int doit = FALSE;
952
953# ifdef FEAT_FOLDING
954 win_foldinfo.fi_level = 0;
955# endif
956
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100957 // update/delete a specific sign
958 redraw_buf_line_later(buf, lnum);
959
960 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (wp->w_redr_type != 0)
963 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100965 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200966 * happening (recursive call), messages on the screen or still starting up.
967 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100968 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200969 || State == ASKMORE || State == HITRETURN
970 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100971#ifdef FEAT_GUI
972 || gui.starting
973#endif
974 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 return;
976
977 /* update all windows that need updating */
978 update_prepare();
979
Bram Moolenaar29323592016-07-24 22:04:11 +0200980 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982 if (wp->w_redr_type != 0)
983 win_update(wp);
984 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200985 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 update_finish();
989}
990#endif
991
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200992#ifdef FEAT_TEXT_PROP
993 static void
994update_popups(void)
995{
996 win_T *wp;
997 win_T *lowest_wp;
998 int lowest_zindex;
999
1000 // Reset all the VALID_POPUP flags.
1001 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001002 wp->w_popup_flags &= ~PFL_REDRAWN;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001003 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001004 wp->w_popup_flags &= ~PFL_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001005
1006 // TODO: don't redraw every popup every time.
1007 for (;;)
1008 {
1009 // Find the window with the lowest zindex that hasn't been updated yet,
1010 // so that the window with a higher zindex is drawn later, thus goes on
1011 // top.
1012 lowest_zindex = INT_MAX;
1013 lowest_wp = NULL;
1014 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001015 if ((wp->w_popup_flags & (PFL_REDRAWN|PFL_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001016 && wp->w_zindex < lowest_zindex)
1017 {
1018 lowest_zindex = wp->w_zindex;
1019 lowest_wp = wp;
1020 }
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001021 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001022 if ((wp->w_popup_flags & (PFL_REDRAWN|PFL_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023 && wp->w_zindex < lowest_zindex)
1024 {
1025 lowest_zindex = wp->w_zindex;
1026 lowest_wp = wp;
1027 }
1028
1029 if (lowest_wp == NULL)
1030 break;
1031 win_update(lowest_wp);
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001032 lowest_wp->w_popup_flags |= PFL_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001033 }
1034}
1035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037#if defined(FEAT_GUI) || defined(PROTO)
1038/*
1039 * Update a single window, its status line and maybe the command line msg.
1040 * Used for the GUI scrollbar.
1041 */
1042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001043updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001045 /* return if already busy updating */
1046 if (updating_screen)
1047 return;
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 update_prepare();
1050
1051#ifdef FEAT_CLIPBOARD
1052 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001053 if (clip_star.available && clip_isautosel_star())
1054 clip_update_selection(&clip_star);
1055 if (clip_plus.available && clip_isautosel_plus())
1056 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001060
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001061 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001062 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001063 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (wp->w_redr_status
1066# ifdef FEAT_CMDL_INFO
1067 || p_ru
1068# endif
1069# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001070 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071# endif
1072 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001073 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 update_finish();
1076}
1077#endif
1078
1079/*
1080 * Update a single window.
1081 *
1082 * This may cause the windows below it also to be redrawn (when clearing the
1083 * screen or scrolling lines).
1084 *
1085 * How the window is redrawn depends on wp->w_redr_type. Each type also
1086 * implies the one below it.
1087 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001088 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1090 * INVERTED redraw the changed part of the Visual area
1091 * INVERTED_ALL redraw the whole Visual area
1092 * VALID 1. scroll up/down to adjust for a changed w_topline
1093 * 2. update lines at the top when scrolled down
1094 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001095 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * b_mod_top and b_mod_bot.
1097 * - if wp->w_redraw_top non-zero, redraw lines between
1098 * wp->w_redraw_top and wp->w_redr_bot.
1099 * - continue redrawing when syntax status is invalid.
1100 * 4. if scrolled up, update lines at the bottom.
1101 * This results in three areas that may need updating:
1102 * top: from first row to top_end (when scrolled down)
1103 * mid: from mid_start to mid_end (update inversion or changed text)
1104 * bot: from bot_start to last row (when scrolled up)
1105 */
1106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001107win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 buf_T *buf = wp->w_buffer;
1110 int type;
1111 int top_end = 0; /* Below last row of the top area that needs
1112 updating. 0 when no top area updating. */
1113 int mid_start = 999;/* first row of the mid area that needs
1114 updating. 999 when no mid area updating. */
1115 int mid_end = 0; /* Below last row of the mid area that needs
1116 updating. 0 when no mid area updating. */
1117 int bot_start = 999;/* first row of the bot area that needs
1118 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int scrolled_down = FALSE; /* TRUE when scrolled down when
1120 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001122 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int top_to_mod = FALSE; /* redraw above mod_top */
1124#endif
1125
1126 int row; /* current window row to display */
1127 linenr_T lnum; /* current buffer lnum to display */
1128 int idx; /* current index in w_lines[] */
1129 int srow; /* starting row of the current line */
1130
1131 int eof = FALSE; /* if TRUE, we hit the end of the file */
1132 int didline = FALSE; /* if TRUE, we finished the last line */
1133 int i;
1134 long j;
1135 static int recursive = FALSE; /* being called recursively */
1136 int old_botline = wp->w_botline;
1137#ifdef FEAT_FOLDING
1138 long fold_count;
1139#endif
1140#ifdef FEAT_SYN_HL
1141 /* remember what happened to the previous line, to know if
1142 * check_visual_highlight() can be used */
1143#define DID_NONE 1 /* didn't update a line */
1144#define DID_LINE 2 /* updated a normal line */
1145#define DID_FOLD 3 /* updated a folded line */
1146 int did_update = DID_NONE;
1147 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1148#endif
1149 linenr_T mod_top = 0;
1150 linenr_T mod_bot = 0;
1151#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1152 int save_got_int;
1153#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001154#ifdef SYN_TIME_LIMIT
1155 proftime_T syntax_tm;
1156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 type = wp->w_redr_type;
1159
1160 if (type == NOT_VALID)
1161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 wp->w_lines_valid = 0;
1164 }
1165
1166 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001167 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
1169 wp->w_redr_type = 0;
1170 return;
1171 }
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 /* Window is zero-width: Only need to draw the separator. */
1174 if (wp->w_width == 0)
1175 {
1176 /* draw the vertical separator right of this window */
1177 draw_vsep_win(wp, 0);
1178 wp->w_redr_type = 0;
1179 return;
1180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001182#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001183 // If this window contains a terminal, redraw works completely differently.
1184 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001185 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001186 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001187# ifdef FEAT_MENU
1188 /* Draw the window toolbar, if there is one. */
1189 if (winbar_height(wp) > 0)
1190 redraw_win_toolbar(wp);
1191# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192 wp->w_redr_type = 0;
1193 return;
1194 }
1195#endif
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001198 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
1200
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001201#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001202 /* Force redraw when width of 'number' or 'relativenumber' column
1203 * changes. */
1204 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001205 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001206 {
1207 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001208 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001209 }
1210 else
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1214 {
1215 /*
1216 * When there are both inserted/deleted lines and specific lines to be
1217 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1218 * everything (only happens when redrawing is off for while).
1219 */
1220 type = NOT_VALID;
1221 }
1222 else
1223 {
1224 /*
1225 * Set mod_top to the first line that needs displaying because of
1226 * changes. Set mod_bot to the first line after the changes.
1227 */
1228 mod_top = wp->w_redraw_top;
1229 if (wp->w_redraw_bot != 0)
1230 mod_bot = wp->w_redraw_bot + 1;
1231 else
1232 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (buf->b_mod_set)
1234 {
1235 if (mod_top == 0 || mod_top > buf->b_mod_top)
1236 {
1237 mod_top = buf->b_mod_top;
1238#ifdef FEAT_SYN_HL
1239 /* Need to redraw lines above the change that may be included
1240 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001243 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (mod_top < 1)
1245 mod_top = 1;
1246 }
1247#endif
1248 }
1249 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1250 mod_bot = buf->b_mod_bot;
1251
1252#ifdef FEAT_SEARCH_EXTRA
1253 /* When 'hlsearch' is on and using a multi-line search pattern, a
1254 * change in one line may make the Search highlighting in a
1255 * previous line invalid. Simple solution: redraw all visible
1256 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001257 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001259 if (search_hl.rm.regprog != NULL
1260 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001262 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001263 {
1264 cur = wp->w_match_head;
1265 while (cur != NULL)
1266 {
1267 if (cur->match.regprog != NULL
1268 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001269 {
1270 top_to_mod = TRUE;
1271 break;
1272 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001273 cur = cur->next;
1274 }
1275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277 }
1278#ifdef FEAT_FOLDING
1279 if (mod_top != 0 && hasAnyFolding(wp))
1280 {
1281 linenr_T lnumt, lnumb;
1282
1283 /*
1284 * A change in a line can cause lines above it to become folded or
1285 * unfolded. Find the top most buffer line that may be affected.
1286 * If the line was previously folded and displayed, get the first
1287 * line of that fold. If the line is folded now, get the first
1288 * folded line. Use the minimum of these two.
1289 */
1290
1291 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1292 * the line below it. If there is no valid entry, use w_topline.
1293 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1294 * to this line. If there is no valid entry, use MAXLNUM. */
1295 lnumt = wp->w_topline;
1296 lnumb = MAXLNUM;
1297 for (i = 0; i < wp->w_lines_valid; ++i)
1298 if (wp->w_lines[i].wl_valid)
1299 {
1300 if (wp->w_lines[i].wl_lastlnum < mod_top)
1301 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1302 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1303 {
1304 lnumb = wp->w_lines[i].wl_lnum;
1305 /* When there is a fold column it might need updating
1306 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001307 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ++lnumb;
1309 }
1310 }
1311
1312 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1313 if (mod_top > lnumt)
1314 mod_top = lnumt;
1315
1316 /* Now do the same for the bottom line (one above mod_bot). */
1317 --mod_bot;
1318 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1319 ++mod_bot;
1320 if (mod_bot < lnumb)
1321 mod_bot = lnumb;
1322 }
1323#endif
1324
1325 /* When a change starts above w_topline and the end is below
1326 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001327 * If the end of the change is above w_topline: do like no change was
1328 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (mod_top != 0 && mod_top < wp->w_topline)
1330 {
1331 if (mod_bot > wp->w_topline)
1332 mod_top = wp->w_topline;
1333#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001334 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 top_end = 1;
1336#endif
1337 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001338
1339 /* When line numbers are displayed need to redraw all lines below
1340 * inserted/deleted lines. */
1341 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1342 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001344 wp->w_redraw_top = 0; // reset for next time
1345 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 /*
1348 * When only displaying the lines at the top, set top_end. Used when
1349 * window has scrolled down for msg_scrolled.
1350 */
1351 if (type == REDRAW_TOP)
1352 {
1353 j = 0;
1354 for (i = 0; i < wp->w_lines_valid; ++i)
1355 {
1356 j += wp->w_lines[i].wl_size;
1357 if (j >= wp->w_upd_rows)
1358 {
1359 top_end = j;
1360 break;
1361 }
1362 }
1363 if (top_end == 0)
1364 /* not found (cannot happen?): redraw everything */
1365 type = NOT_VALID;
1366 else
1367 /* top area defined, the rest is VALID */
1368 type = VALID;
1369 }
1370
Bram Moolenaar367329b2007-08-30 11:53:22 +00001371 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001372 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1373 * non-zero and thus not FALSE) will indicate that screenclear() was not
1374 * called. */
1375 if (screen_cleared)
1376 screen_cleared = MAYBE;
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /*
1379 * If there are no changes on the screen that require a complete redraw,
1380 * handle three cases:
1381 * 1: we are off the top of the screen by a few lines: scroll down
1382 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1383 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1384 * w_lines[] that needs updating.
1385 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001386 if ((type == VALID || type == SOME_VALID
1387 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#ifdef FEAT_DIFF
1389 && !wp->w_botfill && !wp->w_old_botfill
1390#endif
1391 )
1392 {
1393 if (mod_top != 0 && wp->w_topline == mod_top)
1394 {
1395 /*
1396 * w_topline is the first changed line, the scrolling will be done
1397 * further down.
1398 */
1399 }
1400 else if (wp->w_lines[0].wl_valid
1401 && (wp->w_topline < wp->w_lines[0].wl_lnum
1402#ifdef FEAT_DIFF
1403 || (wp->w_topline == wp->w_lines[0].wl_lnum
1404 && wp->w_topfill > wp->w_old_topfill)
1405#endif
1406 ))
1407 {
1408 /*
1409 * New topline is above old topline: May scroll down.
1410 */
1411#ifdef FEAT_FOLDING
1412 if (hasAnyFolding(wp))
1413 {
1414 linenr_T ln;
1415
1416 /* count the number of lines we are off, counting a sequence
1417 * of folded lines as one */
1418 j = 0;
1419 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1420 {
1421 ++j;
1422 if (j >= wp->w_height - 2)
1423 break;
1424 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1425 }
1426 }
1427 else
1428#endif
1429 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1430 if (j < wp->w_height - 2) /* not too far off */
1431 {
1432 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1433#ifdef FEAT_DIFF
1434 /* insert extra lines for previously invisible filler lines */
1435 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1436 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1437 - wp->w_old_topfill;
1438#endif
1439 if (i < wp->w_height - 2) /* less than a screen off */
1440 {
1441 /*
1442 * Try to insert the correct number of lines.
1443 * If not the last window, delete the lines at the bottom.
1444 * win_ins_lines may fail when the terminal can't do it.
1445 */
1446 if (i > 0)
1447 check_for_delay(FALSE);
1448 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1449 {
1450 if (wp->w_lines_valid != 0)
1451 {
1452 /* Need to update rows that are new, stop at the
1453 * first one that scrolled down. */
1454 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 /* Move the entries that were scrolled, disable
1458 * the entries for the lines to be redrawn. */
1459 if ((wp->w_lines_valid += j) > wp->w_height)
1460 wp->w_lines_valid = wp->w_height;
1461 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1462 wp->w_lines[idx] = wp->w_lines[idx - j];
1463 while (idx >= 0)
1464 wp->w_lines[idx--].wl_valid = FALSE;
1465 }
1466 }
1467 else
1468 mid_start = 0; /* redraw all lines */
1469 }
1470 else
1471 mid_start = 0; /* redraw all lines */
1472 }
1473 else
1474 mid_start = 0; /* redraw all lines */
1475 }
1476 else
1477 {
1478 /*
1479 * New topline is at or below old topline: May scroll up.
1480 * When topline didn't change, find first entry in w_lines[] that
1481 * needs updating.
1482 */
1483
1484 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1485 j = -1;
1486 row = 0;
1487 for (i = 0; i < wp->w_lines_valid; i++)
1488 {
1489 if (wp->w_lines[i].wl_valid
1490 && wp->w_lines[i].wl_lnum == wp->w_topline)
1491 {
1492 j = i;
1493 break;
1494 }
1495 row += wp->w_lines[i].wl_size;
1496 }
1497 if (j == -1)
1498 {
1499 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1500 * lines */
1501 mid_start = 0;
1502 }
1503 else
1504 {
1505 /*
1506 * Try to delete the correct number of lines.
1507 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1508 */
1509#ifdef FEAT_DIFF
1510 /* If the topline didn't change, delete old filler lines,
1511 * otherwise delete filler lines of the new topline... */
1512 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1513 row += wp->w_old_topfill;
1514 else
1515 row += diff_check_fill(wp, wp->w_topline);
1516 /* ... but don't delete new filler lines. */
1517 row -= wp->w_topfill;
1518#endif
1519 if (row > 0)
1520 {
1521 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001522 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1523 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 bot_start = wp->w_height - row;
1525 else
1526 mid_start = 0; /* redraw all lines */
1527 }
1528 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1529 {
1530 /*
1531 * Skip the lines (below the deleted lines) that are still
1532 * valid and don't need redrawing. Copy their info
1533 * upwards, to compensate for the deleted lines. Set
1534 * bot_start to the first row that needs redrawing.
1535 */
1536 bot_start = 0;
1537 idx = 0;
1538 for (;;)
1539 {
1540 wp->w_lines[idx] = wp->w_lines[j];
1541 /* stop at line that didn't fit, unless it is still
1542 * valid (no lines deleted) */
1543 if (row > 0 && bot_start + row
1544 + (int)wp->w_lines[j].wl_size > wp->w_height)
1545 {
1546 wp->w_lines_valid = idx + 1;
1547 break;
1548 }
1549 bot_start += wp->w_lines[idx++].wl_size;
1550
1551 /* stop at the last valid entry in w_lines[].wl_size */
1552 if (++j >= wp->w_lines_valid)
1553 {
1554 wp->w_lines_valid = idx;
1555 break;
1556 }
1557 }
1558#ifdef FEAT_DIFF
1559 /* Correct the first entry for filler lines at the top
1560 * when it won't get updated below. */
1561 if (wp->w_p_diff && bot_start > 0)
1562 wp->w_lines[0].wl_size =
1563 plines_win_nofill(wp, wp->w_topline, TRUE)
1564 + wp->w_topfill;
1565#endif
1566 }
1567 }
1568 }
1569
1570 /* When starting redraw in the first line, redraw all lines. When
1571 * there is only one window it's probably faster to clear the screen
1572 * first. */
1573 if (mid_start == 0)
1574 {
1575 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001576 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001577 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001578 /* Clear the screen when it was not done by win_del_lines() or
1579 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1580 * then. */
1581 if (screen_cleared != TRUE)
1582 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001583 /* The screen was cleared, redraw the tab pages line. */
1584 if (redraw_tabline)
1585 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588
1589 /* When win_del_lines() or win_ins_lines() caused the screen to be
1590 * cleared (only happens for the first window) or when screenclear()
1591 * was called directly above, "must_redraw" will have been set to
1592 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1593 if (screen_cleared == TRUE)
1594 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else
1597 {
1598 /* Not VALID or INVERTED: redraw all lines. */
1599 mid_start = 0;
1600 mid_end = wp->w_height;
1601 }
1602
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001603 if (type == SOME_VALID)
1604 {
1605 /* SOME_VALID: redraw all lines. */
1606 mid_start = 0;
1607 mid_end = wp->w_height;
1608 type = NOT_VALID;
1609 }
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 /* check if we are updating or removing the inverted part */
1612 if ((VIsual_active && buf == curwin->w_buffer)
1613 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1614 {
1615 linenr_T from, to;
1616
1617 if (VIsual_active)
1618 {
1619 if (VIsual_active
1620 && (VIsual_mode != wp->w_old_visual_mode
1621 || type == INVERTED_ALL))
1622 {
1623 /*
1624 * If the type of Visual selection changed, redraw the whole
1625 * selection. Also when the ownership of the X selection is
1626 * gained or lost.
1627 */
1628 if (curwin->w_cursor.lnum < VIsual.lnum)
1629 {
1630 from = curwin->w_cursor.lnum;
1631 to = VIsual.lnum;
1632 }
1633 else
1634 {
1635 from = VIsual.lnum;
1636 to = curwin->w_cursor.lnum;
1637 }
1638 /* redraw more when the cursor moved as well */
1639 if (wp->w_old_cursor_lnum < from)
1640 from = wp->w_old_cursor_lnum;
1641 if (wp->w_old_cursor_lnum > to)
1642 to = wp->w_old_cursor_lnum;
1643 if (wp->w_old_visual_lnum < from)
1644 from = wp->w_old_visual_lnum;
1645 if (wp->w_old_visual_lnum > to)
1646 to = wp->w_old_visual_lnum;
1647 }
1648 else
1649 {
1650 /*
1651 * Find the line numbers that need to be updated: The lines
1652 * between the old cursor position and the current cursor
1653 * position. Also check if the Visual position changed.
1654 */
1655 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1656 {
1657 from = curwin->w_cursor.lnum;
1658 to = wp->w_old_cursor_lnum;
1659 }
1660 else
1661 {
1662 from = wp->w_old_cursor_lnum;
1663 to = curwin->w_cursor.lnum;
1664 if (from == 0) /* Visual mode just started */
1665 from = to;
1666 }
1667
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001668 if (VIsual.lnum != wp->w_old_visual_lnum
1669 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
1671 if (wp->w_old_visual_lnum < from
1672 && wp->w_old_visual_lnum != 0)
1673 from = wp->w_old_visual_lnum;
1674 if (wp->w_old_visual_lnum > to)
1675 to = wp->w_old_visual_lnum;
1676 if (VIsual.lnum < from)
1677 from = VIsual.lnum;
1678 if (VIsual.lnum > to)
1679 to = VIsual.lnum;
1680 }
1681 }
1682
1683 /*
1684 * If in block mode and changed column or curwin->w_curswant:
1685 * update all lines.
1686 * First compute the actual start and end column.
1687 */
1688 if (VIsual_mode == Ctrl_V)
1689 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001690 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001691#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001692 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaar404406a2014-10-09 13:24:43 +02001694 if (curwin->w_p_lbr)
1695 ve_flags = VE_ALL;
1696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001698#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001699 ve_flags = save_ve_flags;
1700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 ++toc;
1702 if (curwin->w_curswant == MAXCOL)
1703 toc = MAXCOL;
1704
1705 if (fromc != wp->w_old_cursor_fcol
1706 || toc != wp->w_old_cursor_lcol)
1707 {
1708 if (from > VIsual.lnum)
1709 from = VIsual.lnum;
1710 if (to < VIsual.lnum)
1711 to = VIsual.lnum;
1712 }
1713 wp->w_old_cursor_fcol = fromc;
1714 wp->w_old_cursor_lcol = toc;
1715 }
1716 }
1717 else
1718 {
1719 /* Use the line numbers of the old Visual area. */
1720 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1721 {
1722 from = wp->w_old_cursor_lnum;
1723 to = wp->w_old_visual_lnum;
1724 }
1725 else
1726 {
1727 from = wp->w_old_visual_lnum;
1728 to = wp->w_old_cursor_lnum;
1729 }
1730 }
1731
1732 /*
1733 * There is no need to update lines above the top of the window.
1734 */
1735 if (from < wp->w_topline)
1736 from = wp->w_topline;
1737
1738 /*
1739 * If we know the value of w_botline, use it to restrict the update to
1740 * the lines that are visible in the window.
1741 */
1742 if (wp->w_valid & VALID_BOTLINE)
1743 {
1744 if (from >= wp->w_botline)
1745 from = wp->w_botline - 1;
1746 if (to >= wp->w_botline)
1747 to = wp->w_botline - 1;
1748 }
1749
1750 /*
1751 * Find the minimal part to be updated.
1752 * Watch out for scrolling that made entries in w_lines[] invalid.
1753 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1754 * top_end; need to redraw from top_end to the "to" line.
1755 * A middle mouse click with a Visual selection may change the text
1756 * above the Visual area and reset wl_valid, do count these for
1757 * mid_end (in srow).
1758 */
1759 if (mid_start > 0)
1760 {
1761 lnum = wp->w_topline;
1762 idx = 0;
1763 srow = 0;
1764 if (scrolled_down)
1765 mid_start = top_end;
1766 else
1767 mid_start = 0;
1768 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1769 {
1770 if (wp->w_lines[idx].wl_valid)
1771 mid_start += wp->w_lines[idx].wl_size;
1772 else if (!scrolled_down)
1773 srow += wp->w_lines[idx].wl_size;
1774 ++idx;
1775# ifdef FEAT_FOLDING
1776 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1777 lnum = wp->w_lines[idx].wl_lnum;
1778 else
1779# endif
1780 ++lnum;
1781 }
1782 srow += mid_start;
1783 mid_end = wp->w_height;
1784 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1785 {
1786 if (wp->w_lines[idx].wl_valid
1787 && wp->w_lines[idx].wl_lnum >= to + 1)
1788 {
1789 /* Only update until first row of this line */
1790 mid_end = srow;
1791 break;
1792 }
1793 srow += wp->w_lines[idx].wl_size;
1794 }
1795 }
1796 }
1797
1798 if (VIsual_active && buf == curwin->w_buffer)
1799 {
1800 wp->w_old_visual_mode = VIsual_mode;
1801 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1802 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001803 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 wp->w_old_curswant = curwin->w_curswant;
1805 }
1806 else
1807 {
1808 wp->w_old_visual_mode = 0;
1809 wp->w_old_cursor_lnum = 0;
1810 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001811 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1815 /* reset got_int, otherwise regexp won't work */
1816 save_got_int = got_int;
1817 got_int = 0;
1818#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001819#ifdef SYN_TIME_LIMIT
1820 /* Set the time limit to 'redrawtime'. */
1821 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001822 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#ifdef FEAT_FOLDING
1825 win_foldinfo.fi_level = 0;
1826#endif
1827
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001828#ifdef FEAT_MENU
1829 /*
1830 * Draw the window toolbar, if there is one.
1831 * TODO: only when needed.
1832 */
1833 if (winbar_height(wp) > 0)
1834 redraw_win_toolbar(wp);
1835#endif
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * Update all the window rows.
1839 */
1840 idx = 0; /* first entry in w_lines[].wl_size */
1841 row = 0;
1842 srow = 0;
1843 lnum = wp->w_topline; /* first line shown in window */
1844 for (;;)
1845 {
1846 /* stop updating when reached the end of the window (check for _past_
1847 * the end of the window is at the end of the loop) */
1848 if (row == wp->w_height)
1849 {
1850 didline = TRUE;
1851 break;
1852 }
1853
1854 /* stop updating when hit the end of the file */
1855 if (lnum > buf->b_ml.ml_line_count)
1856 {
1857 eof = TRUE;
1858 break;
1859 }
1860
1861 /* Remember the starting row of the line that is going to be dealt
1862 * with. It is used further down when the line doesn't fit. */
1863 srow = row;
1864
1865 /*
1866 * Update a line when it is in an area that needs updating, when it
1867 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001868 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 * win_del_lines(), check if the current line includes it.
1870 * When syntax folding is being used, the saved syntax states will
1871 * already have been updated, we can't see where the syntax state is
1872 * the same again, just update until the end of the window.
1873 */
1874 if (row < top_end
1875 || (row >= mid_start && row < mid_end)
1876#ifdef FEAT_SEARCH_EXTRA
1877 || top_to_mod
1878#endif
1879 || idx >= wp->w_lines_valid
1880 || (row + wp->w_lines[idx].wl_size > bot_start)
1881 || (mod_top != 0
1882 && (lnum == mod_top
1883 || (lnum >= mod_top
1884 && (lnum < mod_bot
1885#ifdef FEAT_SYN_HL
1886 || did_update == DID_FOLD
1887 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001888 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 && (
1890# ifdef FEAT_FOLDING
1891 (foldmethodIsSyntax(wp)
1892 && hasAnyFolding(wp)) ||
1893# endif
1894 syntax_check_changed(lnum)))
1895#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001896#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001897 /* match in fixed position might need redraw
1898 * if lines were inserted or deleted */
1899 || (wp->w_match_head != NULL
1900 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 )))))
1903 {
1904#ifdef FEAT_SEARCH_EXTRA
1905 if (lnum == mod_top)
1906 top_to_mod = FALSE;
1907#endif
1908
1909 /*
1910 * When at start of changed lines: May scroll following lines
1911 * up or down to minimize redrawing.
1912 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001913 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 */
1915 if (lnum == mod_top
1916 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001917 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 int old_rows = 0;
1920 int new_rows = 0;
1921 int xtra_rows;
1922 linenr_T l;
1923
1924 /* Count the old number of window rows, using w_lines[], which
1925 * should still contain the sizes for the lines as they are
1926 * currently displayed. */
1927 for (i = idx; i < wp->w_lines_valid; ++i)
1928 {
1929 /* Only valid lines have a meaningful wl_lnum. Invalid
1930 * lines are part of the changed area. */
1931 if (wp->w_lines[i].wl_valid
1932 && wp->w_lines[i].wl_lnum == mod_bot)
1933 break;
1934 old_rows += wp->w_lines[i].wl_size;
1935#ifdef FEAT_FOLDING
1936 if (wp->w_lines[i].wl_valid
1937 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1938 {
1939 /* Must have found the last valid entry above mod_bot.
1940 * Add following invalid entries. */
1941 ++i;
1942 while (i < wp->w_lines_valid
1943 && !wp->w_lines[i].wl_valid)
1944 old_rows += wp->w_lines[i++].wl_size;
1945 break;
1946 }
1947#endif
1948 }
1949
1950 if (i >= wp->w_lines_valid)
1951 {
1952 /* We can't find a valid line below the changed lines,
1953 * need to redraw until the end of the window.
1954 * Inserting/deleting lines has no use. */
1955 bot_start = 0;
1956 }
1957 else
1958 {
1959 /* Able to count old number of rows: Count new window
1960 * rows, and may insert/delete lines */
1961 j = idx;
1962 for (l = lnum; l < mod_bot; ++l)
1963 {
1964#ifdef FEAT_FOLDING
1965 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1966 ++new_rows;
1967 else
1968#endif
1969#ifdef FEAT_DIFF
1970 if (l == wp->w_topline)
1971 new_rows += plines_win_nofill(wp, l, TRUE)
1972 + wp->w_topfill;
1973 else
1974#endif
1975 new_rows += plines_win(wp, l, TRUE);
1976 ++j;
1977 if (new_rows > wp->w_height - row - 2)
1978 {
1979 /* it's getting too much, must redraw the rest */
1980 new_rows = 9999;
1981 break;
1982 }
1983 }
1984 xtra_rows = new_rows - old_rows;
1985 if (xtra_rows < 0)
1986 {
1987 /* May scroll text up. If there is not enough
1988 * remaining text or scrolling fails, must redraw the
1989 * rest. If scrolling works, must redraw the text
1990 * below the scrolled text. */
1991 if (row - xtra_rows >= wp->w_height - 2)
1992 mod_bot = MAXLNUM;
1993 else
1994 {
1995 check_for_delay(FALSE);
1996 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001997 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 mod_bot = MAXLNUM;
1999 else
2000 bot_start = wp->w_height + xtra_rows;
2001 }
2002 }
2003 else if (xtra_rows > 0)
2004 {
2005 /* May scroll text down. If there is not enough
2006 * remaining text of scrolling fails, must redraw the
2007 * rest. */
2008 if (row + xtra_rows >= wp->w_height - 2)
2009 mod_bot = MAXLNUM;
2010 else
2011 {
2012 check_for_delay(FALSE);
2013 if (win_ins_lines(wp, row + old_rows,
2014 xtra_rows, FALSE, FALSE) == FAIL)
2015 mod_bot = MAXLNUM;
2016 else if (top_end > row + old_rows)
2017 /* Scrolled the part at the top that requires
2018 * updating down. */
2019 top_end += xtra_rows;
2020 }
2021 }
2022
2023 /* When not updating the rest, may need to move w_lines[]
2024 * entries. */
2025 if (mod_bot != MAXLNUM && i != j)
2026 {
2027 if (j < i)
2028 {
2029 int x = row + new_rows;
2030
2031 /* move entries in w_lines[] upwards */
2032 for (;;)
2033 {
2034 /* stop at last valid entry in w_lines[] */
2035 if (i >= wp->w_lines_valid)
2036 {
2037 wp->w_lines_valid = j;
2038 break;
2039 }
2040 wp->w_lines[j] = wp->w_lines[i];
2041 /* stop at a line that won't fit */
2042 if (x + (int)wp->w_lines[j].wl_size
2043 > wp->w_height)
2044 {
2045 wp->w_lines_valid = j + 1;
2046 break;
2047 }
2048 x += wp->w_lines[j++].wl_size;
2049 ++i;
2050 }
2051 if (bot_start > x)
2052 bot_start = x;
2053 }
2054 else /* j > i */
2055 {
2056 /* move entries in w_lines[] downwards */
2057 j -= i;
2058 wp->w_lines_valid += j;
2059 if (wp->w_lines_valid > wp->w_height)
2060 wp->w_lines_valid = wp->w_height;
2061 for (i = wp->w_lines_valid; i - j >= idx; --i)
2062 wp->w_lines[i] = wp->w_lines[i - j];
2063
2064 /* The w_lines[] entries for inserted lines are
2065 * now invalid, but wl_size may be used above.
2066 * Reset to zero. */
2067 while (i >= idx)
2068 {
2069 wp->w_lines[i].wl_size = 0;
2070 wp->w_lines[i--].wl_valid = FALSE;
2071 }
2072 }
2073 }
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076
2077#ifdef FEAT_FOLDING
2078 /*
2079 * When lines are folded, display one line for all of them.
2080 * Otherwise, display normally (can be several display lines when
2081 * 'wrap' is on).
2082 */
2083 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2084 if (fold_count != 0)
2085 {
2086 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2087 ++row;
2088 --fold_count;
2089 wp->w_lines[idx].wl_folded = TRUE;
2090 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2091# ifdef FEAT_SYN_HL
2092 did_update = DID_FOLD;
2093# endif
2094 }
2095 else
2096#endif
2097 if (idx < wp->w_lines_valid
2098 && wp->w_lines[idx].wl_valid
2099 && wp->w_lines[idx].wl_lnum == lnum
2100 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002101 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 && srow + wp->w_lines[idx].wl_size > wp->w_height
2103#ifdef FEAT_DIFF
2104 && diff_check_fill(wp, lnum) == 0
2105#endif
2106 )
2107 {
2108 /* This line is not going to fit. Don't draw anything here,
2109 * will draw "@ " lines below. */
2110 row = wp->w_height + 1;
2111 }
2112 else
2113 {
2114#ifdef FEAT_SEARCH_EXTRA
2115 prepare_search_hl(wp, lnum);
2116#endif
2117#ifdef FEAT_SYN_HL
2118 /* Let the syntax stuff know we skipped a few lines. */
2119 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002120 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 syntax_end_parsing(syntax_last_parsed + 1);
2122#endif
2123
2124 /*
2125 * Display one line.
2126 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002127 row = win_line(wp, lnum, srow, wp->w_height,
2128 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130#ifdef FEAT_FOLDING
2131 wp->w_lines[idx].wl_folded = FALSE;
2132 wp->w_lines[idx].wl_lastlnum = lnum;
2133#endif
2134#ifdef FEAT_SYN_HL
2135 did_update = DID_LINE;
2136 syntax_last_parsed = lnum;
2137#endif
2138 }
2139
2140 wp->w_lines[idx].wl_lnum = lnum;
2141 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002142
2143 /* Past end of the window or end of the screen. Note that after
2144 * resizing wp->w_height may be end up too big. That's a problem
2145 * elsewhere, but prevent a crash here. */
2146 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
2148 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2151 ++idx;
2152 break;
2153 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002154 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 wp->w_lines[idx].wl_size = row - srow;
2156 ++idx;
2157#ifdef FEAT_FOLDING
2158 lnum += fold_count + 1;
2159#else
2160 ++lnum;
2161#endif
2162 }
2163 else
2164 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002165 if (wp->w_p_rnu)
2166 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002167#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002168 // 'relativenumber' set: The text doesn't need to be drawn, but
2169 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002170 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2171 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002172 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002173 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002174#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002175 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002176 }
2177
2178 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 row += wp->w_lines[idx++].wl_size;
2180 if (row > wp->w_height) /* past end of screen */
2181 break;
2182#ifdef FEAT_FOLDING
2183 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2184#else
2185 ++lnum;
2186#endif
2187#ifdef FEAT_SYN_HL
2188 did_update = DID_NONE;
2189#endif
2190 }
2191
2192 if (lnum > buf->b_ml.ml_line_count)
2193 {
2194 eof = TRUE;
2195 break;
2196 }
2197 }
2198 /*
2199 * End of loop over all window lines.
2200 */
2201
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002202#ifdef FEAT_VTP
2203 /* Rewrite the character at the end of the screen line. */
2204 if (use_vtp())
2205 {
2206 int i;
2207
2208 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002209 if (enc_utf8)
2210 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2211 LineOffset[i] + screen_Columns) > 1)
2212 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2213 else
2214 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2215 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002216 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2217 }
2218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 if (idx > wp->w_lines_valid)
2221 wp->w_lines_valid = idx;
2222
2223#ifdef FEAT_SYN_HL
2224 /*
2225 * Let the syntax stuff know we stop parsing here.
2226 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002227 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 syntax_end_parsing(syntax_last_parsed + 1);
2229#endif
2230
2231 /*
2232 * If we didn't hit the end of the file, and we didn't finish the last
2233 * line we were working on, then the line didn't fit.
2234 */
2235 wp->w_empty_rows = 0;
2236#ifdef FEAT_DIFF
2237 wp->w_filler_rows = 0;
2238#endif
2239 if (!eof && !didline)
2240 {
2241 if (lnum == wp->w_topline)
2242 {
2243 /*
2244 * Single line that does not fit!
2245 * Don't overwrite it, it can be edited.
2246 */
2247 wp->w_botline = lnum + 1;
2248 }
2249#ifdef FEAT_DIFF
2250 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2251 {
2252 /* Window ends in filler lines. */
2253 wp->w_botline = lnum;
2254 wp->w_filler_rows = wp->w_height - srow;
2255 }
2256#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002257 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2258 {
2259 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2260
2261 /*
2262 * Last line isn't finished: Display "@@@" in the last screen line.
2263 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002264 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002265 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002266 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002267 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002268 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 set_empty_rows(wp, srow);
2270 wp->w_botline = lnum;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2273 {
2274 /*
2275 * Last line isn't finished: Display "@@@" at the end.
2276 */
2277 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2278 W_WINROW(wp) + wp->w_height,
2279 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002280 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 set_empty_rows(wp, srow);
2282 wp->w_botline = lnum;
2283 }
2284 else
2285 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002286 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 wp->w_botline = lnum;
2288 }
2289 }
2290 else
2291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (eof) /* we hit the end of the file */
2294 {
2295 wp->w_botline = buf->b_ml.ml_line_count + 1;
2296#ifdef FEAT_DIFF
2297 j = diff_check_fill(wp, wp->w_botline);
2298 if (j > 0 && !wp->w_botfill)
2299 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002300 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (char2cells(fill_diff) > 1)
2302 i = '-';
2303 else
2304 i = fill_diff;
2305 if (row + j > wp->w_height)
2306 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002307 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 row += j;
2309 }
2310#endif
2311 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002312 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 wp->w_botline = lnum;
2314
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002315 // Make sure the rest of the screen is blank
2316 // put '~'s on rows that aren't part of the file.
2317 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002320#ifdef SYN_TIME_LIMIT
2321 syn_set_timeout(NULL);
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /* Reset the type of redrawing required, the window has been updated. */
2325 wp->w_redr_type = 0;
2326#ifdef FEAT_DIFF
2327 wp->w_old_topfill = wp->w_topfill;
2328 wp->w_old_botfill = wp->w_botfill;
2329#endif
2330
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002331 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 /*
2334 * There is a trick with w_botline. If we invalidate it on each
2335 * change that might modify it, this will cause a lot of expensive
2336 * calls to plines() in update_topline() each time. Therefore the
2337 * value of w_botline is often approximated, and this value is used to
2338 * compute the value of w_topline. If the value of w_botline was
2339 * wrong, check that the value of w_topline is correct (cursor is on
2340 * the visible part of the text). If it's not, we need to redraw
2341 * again. Mostly this just means scrolling up a few lines, so it
2342 * doesn't look too bad. Only do this for the current window (where
2343 * changes are relevant).
2344 */
2345 wp->w_valid |= VALID_BOTLINE;
2346 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2347 {
2348 recursive = TRUE;
2349 curwin->w_valid &= ~VALID_TOPLINE;
2350 update_topline(); /* may invalidate w_botline again */
2351 if (must_redraw != 0)
2352 {
2353 /* Don't update for changes in buffer again. */
2354 i = curbuf->b_mod_set;
2355 curbuf->b_mod_set = FALSE;
2356 win_update(curwin);
2357 must_redraw = 0;
2358 curbuf->b_mod_set = i;
2359 }
2360 recursive = FALSE;
2361 }
2362 }
2363
2364#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2365 /* restore got_int, unless CTRL-C was hit while redrawing */
2366 if (!got_int)
2367 got_int = save_got_int;
2368#endif
2369}
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002372 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2373 * Return the new offset.
2374 */
2375 static int
2376screen_fill_end(
2377 win_T *wp,
2378 int c1,
2379 int c2,
2380 int off,
2381 int width,
2382 int row,
2383 int endrow,
2384 int attr)
2385{
2386 int nn = off + width;
2387
2388 if (nn > wp->w_width)
2389 nn = wp->w_width;
2390#ifdef FEAT_RIGHTLEFT
2391 if (wp->w_p_rl)
2392 {
2393 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2394 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2395 c1, c2, attr);
2396 }
2397 else
2398#endif
2399 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2400 wp->w_wincol + off, (int)wp->w_wincol + nn,
2401 c1, c2, attr);
2402 return nn;
2403}
2404
2405/*
2406 * Clear lines near the end the window and mark the unused lines with "c1".
2407 * use "c2" as the filler character.
2408 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 */
2410 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002411win_draw_end(
2412 win_T *wp,
2413 int c1,
2414 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002415 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002416 int row,
2417 int endrow,
2418 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002421 int attr = HL_ATTR(hl);
2422 int wcr_attr = 0;
2423
2424 if (*wp->w_p_wcr != NUL)
2425 {
2426 wcr_attr = syn_name2attr(wp->w_p_wcr);
2427 attr = hl_combine_attr(wcr_attr, attr);
2428 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002429
2430 if (draw_margin)
2431 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002432#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002433 int fdc = compute_foldcolumn(wp, 0);
2434
2435 if (fdc > 0)
2436 // draw the fold column
2437 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002438 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002439#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002440#ifdef FEAT_SIGNS
2441 if (signcolumn_on(wp))
2442 // draw the sign column
2443 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002444 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002445#endif
2446 if ((wp->w_p_nu || wp->w_p_rnu)
2447 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2448 // draw the number column
2449 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002450 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453#ifdef FEAT_RIGHTLEFT
2454 if (wp->w_p_rl)
2455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002457 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002458 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002460 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002461 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 else
2464#endif
2465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002467 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002468 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 set_empty_rows(wp, row);
2472}
2473
Bram Moolenaar1a384422010-07-14 19:53:30 +02002474#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002475/*
2476 * Advance **color_cols and return TRUE when there are columns to draw.
2477 */
2478 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002479advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002480{
2481 while (**color_cols >= 0 && vcol > **color_cols)
2482 ++*color_cols;
2483 return (**color_cols >= 0);
2484}
2485#endif
2486
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002487#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2488/*
2489 * Copy "text" to ScreenLines using "attr".
2490 * Returns the next screen column.
2491 */
2492 static int
2493text_to_screenline(win_T *wp, char_u *text, int col)
2494{
2495 int off = (int)(current_ScreenLine - ScreenLines);
2496
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002497 if (has_mbyte)
2498 {
2499 int cells;
2500 int u8c, u8cc[MAX_MCO];
2501 int i;
2502 int idx;
2503 int c_len;
2504 char_u *p;
2505# ifdef FEAT_ARABIC
2506 int prev_c = 0; /* previous Arabic character */
2507 int prev_c1 = 0; /* first composing char for prev_c */
2508# endif
2509
2510# ifdef FEAT_RIGHTLEFT
2511 if (wp->w_p_rl)
2512 idx = off;
2513 else
2514# endif
2515 idx = off + col;
2516
2517 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2518 for (p = text; *p != NUL; )
2519 {
2520 cells = (*mb_ptr2cells)(p);
2521 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002522 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002523# ifdef FEAT_RIGHTLEFT
2524 - (wp->w_p_rl ? col : 0)
2525# endif
2526 )
2527 break;
2528 ScreenLines[idx] = *p;
2529 if (enc_utf8)
2530 {
2531 u8c = utfc_ptr2char(p, u8cc);
2532 if (*p < 0x80 && u8cc[0] == 0)
2533 {
2534 ScreenLinesUC[idx] = 0;
2535#ifdef FEAT_ARABIC
2536 prev_c = u8c;
2537#endif
2538 }
2539 else
2540 {
2541#ifdef FEAT_ARABIC
2542 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2543 {
2544 /* Do Arabic shaping. */
2545 int pc, pc1, nc;
2546 int pcc[MAX_MCO];
2547 int firstbyte = *p;
2548
2549 /* The idea of what is the previous and next
2550 * character depends on 'rightleft'. */
2551 if (wp->w_p_rl)
2552 {
2553 pc = prev_c;
2554 pc1 = prev_c1;
2555 nc = utf_ptr2char(p + c_len);
2556 prev_c1 = u8cc[0];
2557 }
2558 else
2559 {
2560 pc = utfc_ptr2char(p + c_len, pcc);
2561 nc = prev_c;
2562 pc1 = pcc[0];
2563 }
2564 prev_c = u8c;
2565
2566 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2567 pc, pc1, nc);
2568 ScreenLines[idx] = firstbyte;
2569 }
2570 else
2571 prev_c = u8c;
2572#endif
2573 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002574 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002575 for (i = 0; i < Screen_mco; ++i)
2576 {
2577 ScreenLinesC[i][idx] = u8cc[i];
2578 if (u8cc[i] == 0)
2579 break;
2580 }
2581 }
2582 if (cells > 1)
2583 ScreenLines[idx + 1] = 0;
2584 }
2585 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2586 /* double-byte single width character */
2587 ScreenLines2[idx] = p[1];
2588 else if (cells > 1)
2589 /* double-width character */
2590 ScreenLines[idx + 1] = p[1];
2591 col += cells;
2592 idx += cells;
2593 p += c_len;
2594 }
2595 }
2596 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002597 {
2598 int len = (int)STRLEN(text);
2599
Bram Moolenaar02631462017-09-22 15:20:32 +02002600 if (len > wp->w_width - col)
2601 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002602 if (len > 0)
2603 {
2604#ifdef FEAT_RIGHTLEFT
2605 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002606 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002607 else
2608#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002609 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002610 col += len;
2611 }
2612 }
2613 return col;
2614}
2615#endif
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#ifdef FEAT_FOLDING
2618/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002619 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2620 * space is available for window "wp", minus "col".
2621 */
2622 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002623compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002624{
2625 int fdc = wp->w_p_fdc;
2626 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002627 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002628
2629 if (fdc > wwidth - (col + wmw))
2630 fdc = wwidth - (col + wmw);
2631 return fdc;
2632}
2633
2634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 * Display one folded line.
2636 */
2637 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002638fold_line(
2639 win_T *wp,
2640 long fold_count,
2641 foldinfo_T *foldinfo,
2642 linenr_T lnum,
2643 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002645 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 pos_T *top, *bot;
2647 linenr_T lnume = lnum + fold_count - 1;
2648 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002649 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 int col;
2652 int txtcol;
2653 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002654 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 /* Build the fold line:
2657 * 1. Add the cmdwin_type for the command-line window
2658 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002659 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 * 4. Compose the text
2661 * 5. Add the text
2662 * 6. set highlighting for the Visual area an other text
2663 */
2664 col = 0;
2665
2666 /*
2667 * 1. Add the cmdwin_type for the command-line window
2668 * Ignores 'rightleft', this window is never right-left.
2669 */
2670#ifdef FEAT_CMDWIN
2671 if (cmdwin_type != 0 && wp == curwin)
2672 {
2673 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002674 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (enc_utf8)
2676 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 ++col;
2678 }
2679#endif
2680
2681 /*
2682 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002683 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002685 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (fdc > 0)
2687 {
2688 fill_foldcolumn(buf, wp, TRUE, lnum);
2689#ifdef FEAT_RIGHTLEFT
2690 if (wp->w_p_rl)
2691 {
2692 int i;
2693
Bram Moolenaar02631462017-09-22 15:20:32 +02002694 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002695 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 /* reverse the fold column */
2697 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002698 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 else
2701#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002702 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 col += fdc;
2704 }
2705
2706#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002707# define RL_MEMSET(p, v, l) \
2708 do { \
2709 if (wp->w_p_rl) \
2710 for (ri = 0; ri < l; ++ri) \
2711 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2712 else \
2713 for (ri = 0; ri < l; ++ri) \
2714 ScreenAttrs[off + (p) + ri] = v; \
2715 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002717# define RL_MEMSET(p, v, l) \
2718 do { \
2719 for (ri = 0; ri < l; ++ri) \
2720 ScreenAttrs[off + (p) + ri] = v; \
2721 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#endif
2723
Bram Moolenaar64486672010-05-16 15:46:46 +02002724 /* Set all attributes of the 'number' or 'relativenumber' column and the
2725 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002726 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728#ifdef FEAT_SIGNS
2729 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002730 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002732 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (len > 0)
2734 {
2735 if (len > 2)
2736 len = 2;
2737# ifdef FEAT_RIGHTLEFT
2738 if (wp->w_p_rl)
2739 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002740 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002741 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 else
2743# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002744 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 col += len;
2746 }
2747 }
2748#endif
2749
2750 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002751 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002753 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002755 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 if (len > 0)
2757 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002758 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002759 long num;
2760 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002761
2762 if (len > w + 1)
2763 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002764
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002765 if (wp->w_p_nu && !wp->w_p_rnu)
2766 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002767 num = (long)lnum;
2768 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002769 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002770 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002771 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002772 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002773 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002774 /* 'number' + 'relativenumber': cursor line shows absolute
2775 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002776 num = lnum;
2777 fmt = "%-*ld ";
2778 }
2779 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002780
Bram Moolenaar700e7342013-01-30 12:31:36 +01002781 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782#ifdef FEAT_RIGHTLEFT
2783 if (wp->w_p_rl)
2784 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002785 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002786 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 else
2788#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002789 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 col += len;
2791 }
2792 }
2793
2794 /*
2795 * 4. Compose the folded-line string with 'foldtext', if set.
2796 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002797 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 txtcol = col; /* remember where text starts */
2800
2801 /*
2802 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2803 * Right-left text is put in columns 0 - number-col, normal text is put
2804 * in columns number-col - window-width.
2805 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002806 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 /* Fill the rest of the line with the fold filler */
2809#ifdef FEAT_RIGHTLEFT
2810 if (wp->w_p_rl)
2811 col -= txtcol;
2812#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002813 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#ifdef FEAT_RIGHTLEFT
2815 - (wp->w_p_rl ? txtcol : 0)
2816#endif
2817 )
2818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (enc_utf8)
2820 {
2821 if (fill_fold >= 0x80)
2822 {
2823 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002824 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002825 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
2827 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002830 ScreenLines[off + col] = fill_fold;
2831 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002832 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002834 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002835 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837
2838 if (text != buf)
2839 vim_free(text);
2840
2841 /*
2842 * 6. set highlighting for the Visual area an other text.
2843 * If all folded lines are in the Visual area, highlight the line.
2844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2846 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002847 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 /* Visual is after curwin->w_cursor */
2850 top = &curwin->w_cursor;
2851 bot = &VIsual;
2852 }
2853 else
2854 {
2855 /* Visual is before curwin->w_cursor */
2856 top = &VIsual;
2857 bot = &curwin->w_cursor;
2858 }
2859 if (lnum >= top->lnum
2860 && lnume <= bot->lnum
2861 && (VIsual_mode != 'v'
2862 || ((lnum > top->lnum
2863 || (lnum == top->lnum
2864 && top->col == 0))
2865 && (lnume < bot->lnum
2866 || (lnume == bot->lnum
2867 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002868 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
2870 if (VIsual_mode == Ctrl_V)
2871 {
2872 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002875 if (wp->w_old_cursor_lcol != MAXCOL
2876 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002877 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 len = wp->w_old_cursor_lcol;
2879 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002880 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002881 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002882 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 }
2885 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002888 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002893#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002894 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2895 if (wp->w_p_cc_cols)
2896 {
2897 int i = 0;
2898 int j = wp->w_p_cc_cols[i];
2899 int old_txtcol = txtcol;
2900
2901 while (j > -1)
2902 {
2903 txtcol += j;
2904 if (wp->w_p_wrap)
2905 txtcol -= wp->w_skipcol;
2906 else
2907 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002908 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002909 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002910 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002911 txtcol = old_txtcol;
2912 j = wp->w_p_cc_cols[++i];
2913 }
2914 }
2915
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002916 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002917 if (wp->w_p_cuc)
2918 {
2919 txtcol += wp->w_virtcol;
2920 if (wp->w_p_wrap)
2921 txtcol -= wp->w_skipcol;
2922 else
2923 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002924 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002925 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002926 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002927 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
Bram Moolenaar02631462017-09-22 15:20:32 +02002930 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002931 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 /*
2934 * Update w_cline_height and w_cline_folded if the cursor line was
2935 * updated (saves a call to plines() later).
2936 */
2937 if (wp == curwin
2938 && lnum <= curwin->w_cursor.lnum
2939 && lnume >= curwin->w_cursor.lnum)
2940 {
2941 curwin->w_cline_row = row;
2942 curwin->w_cline_height = 1;
2943 curwin->w_cline_folded = TRUE;
2944 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2945 }
2946}
2947
2948/*
2949 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2950 */
2951 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002952copy_text_attr(
2953 int off,
2954 char_u *buf,
2955 int len,
2956 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002958 int i;
2959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if (enc_utf8)
2962 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002963 for (i = 0; i < len; ++i)
2964 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
2967/*
2968 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002969 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 */
2971 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002972fill_foldcolumn(
2973 char_u *p,
2974 win_T *wp,
2975 int closed, /* TRUE of FALSE */
2976 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 int i = 0;
2979 int level;
2980 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002981 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002982 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002985 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 level = win_foldinfo.fi_level;
2988 if (level > 0)
2989 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002990 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002991 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002992
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 /* If the column is too narrow, we start at the lowest level that
2994 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002995 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (first_level < 1)
2997 first_level = 1;
2998
Bram Moolenaar1c934292015-01-27 16:39:29 +01002999 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 {
3001 if (win_foldinfo.fi_lnum == lnum
3002 && first_level + i >= win_foldinfo.fi_low_level)
3003 p[i] = '-';
3004 else if (first_level == 1)
3005 p[i] = '|';
3006 else if (first_level + i <= 9)
3007 p[i] = '0' + first_level + i;
3008 else
3009 p[i] = '>';
3010 if (first_level + i == level)
3011 break;
3012 }
3013 }
3014 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003015 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016}
3017#endif /* FEAT_FOLDING */
3018
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003019#ifdef FEAT_TEXT_PROP
3020static textprop_T *current_text_props = NULL;
3021static buf_T *current_buf = NULL;
3022
3023 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003024text_prop_compare(const void *s1, const void *s2)
3025{
3026 int idx1, idx2;
3027 proptype_T *pt1, *pt2;
3028 colnr_T col1, col2;
3029
3030 idx1 = *(int *)s1;
3031 idx2 = *(int *)s2;
3032 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3033 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3034 if (pt1 == pt2)
3035 return 0;
3036 if (pt1 == NULL)
3037 return -1;
3038 if (pt2 == NULL)
3039 return 1;
3040 if (pt1->pt_priority != pt2->pt_priority)
3041 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3042 col1 = current_text_props[idx1].tp_col;
3043 col2 = current_text_props[idx2].tp_col;
3044 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3045}
3046#endif
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048/*
3049 * Display line "lnum" of window 'wp' on the screen.
3050 * Start at row "startrow", stop when "endrow" is reached.
3051 * wp->w_virtcol needs to be valid.
3052 *
3053 * Return the number of last row the line occupies.
3054 */
3055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003056win_line(
3057 win_T *wp,
3058 linenr_T lnum,
3059 int startrow,
3060 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003061 int nochange UNUSED, // not updating for changed text
3062 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003064 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3066 int c = 0; /* init for GCC */
3067 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003068#ifdef FEAT_LINEBREAK
3069 long vcol_sbr = -1; /* virtual column after showbreak */
3070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 long vcol_prev = -1; /* "vcol" of previous character */
3072 char_u *line; /* current line */
3073 char_u *ptr; /* current position in "line" */
3074 int row; /* row in the window, excl w_winrow */
3075 int screen_row; /* row on the screen, incl w_winrow */
3076
3077 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3078 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003079 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003080 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003082 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 int extra_attr = 0; /* attributes when n_extra != 0 */
3084 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3085 displaying lcs_eol at end-of-line */
3086 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3087 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3088
3089 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3090 int saved_n_extra = 0;
3091 char_u *saved_p_extra = NULL;
3092 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003093 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 int saved_char_attr = 0;
3095
3096 int n_attr = 0; /* chars with special attr */
3097 int saved_attr2 = 0; /* char_attr saved for n_attr */
3098 int n_attr3 = 0; /* chars with overruling special attr */
3099 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3100
3101 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3102
3103 int fromcol, tocol; /* start/end of inverting */
3104 int fromcol_prev = -2; /* start of inverting after cursor */
3105 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003107 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 pos_T pos;
3109 long v;
3110
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003111 int char_attr = 0; // attributes for next character
3112 int attr_pri = FALSE; // char_attr has priority
3113 int area_highlighting = FALSE; // Visual or incsearch highlighting
3114 // in this line
3115 int vi_attr = 0; // attributes for Visual and incsearch
3116 // highlighting
3117 int wcr_attr = 0; // attributes from 'wincolor'
3118 int area_attr = 0; // attributes desired by highlighting
3119 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003121 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int syntax_attr = 0; /* attributes desired by syntax */
3123 int has_syntax = FALSE; /* this buffer has syntax highl. */
3124 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003125 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003126 int draw_color_col = FALSE; /* highlight colorcolumn */
3127 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003128#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003129#ifdef FEAT_TEXT_PROP
3130 int text_prop_count;
3131 int text_prop_next = 0; // next text property to use
3132 textprop_T *text_props = NULL;
3133 int *text_prop_idxs = NULL;
3134 int text_props_active = 0;
3135 proptype_T *text_prop_type = NULL;
3136 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003137 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003138#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003139#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003140 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003141# define SPWORDLEN 150
3142 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003143 int nextlinecol = 0; /* column where nextline[] starts */
3144 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003145 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003146 int spell_attr = 0; /* attributes desired by spelling */
3147 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003148 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3149 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003150 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003151 static int cap_col = -1; /* column to check for Cap word */
3152 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003153 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003155 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 int multi_attr = 0; /* attributes desired by multibyte */
3157 int mb_l = 1; /* multi-byte byte length */
3158 int mb_c = 0; /* decoded multi-byte character */
3159 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003160 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#ifdef FEAT_DIFF
3162 int filler_lines; /* nr of filler lines to be drawn */
3163 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003164 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 int change_start = MAXCOL; /* first col of changed area */
3166 int change_end = -1; /* last col of changed area */
3167#endif
3168 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3169#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003170 int need_showbreak = FALSE; /* overlong line, skipping first x
3171 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003173#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003174 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003176 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#endif
3178#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003179 matchitem_T *cur; /* points to the match list */
3180 match_T *shl; /* points to search_hl or a match */
3181 int shl_flag; /* flag to indicate whether search_hl
3182 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003183 int pos_inprogress; /* marks that position match search is
3184 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003185 int prevcol_hl_flag; /* flag to indicate whether prevcol
3186 equals startcol of search_hl or one
3187 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188#endif
3189#ifdef FEAT_ARABIC
3190 int prev_c = 0; /* previous Arabic character */
3191 int prev_c1 = 0; /* first composing char for prev_c */
3192#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003193#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003194 int did_line_attr = 0;
3195#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003196#ifdef FEAT_TERMINAL
3197 int get_term_attr = FALSE;
3198#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003199 int win_attr = 0; // background for whole window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 /* draw_state: items that are drawn in sequence: */
3202#define WL_START 0 /* nothing done yet */
3203#ifdef FEAT_CMDWIN
3204# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3205#else
3206# define WL_CMDLINE WL_START
3207#endif
3208#ifdef FEAT_FOLDING
3209# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3210#else
3211# define WL_FOLD WL_CMDLINE
3212#endif
3213#ifdef FEAT_SIGNS
3214# define WL_SIGN WL_FOLD + 1 /* column for signs */
3215#else
3216# define WL_SIGN WL_FOLD /* column for signs */
3217#endif
3218#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003219#ifdef FEAT_LINEBREAK
3220# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003222# define WL_BRI WL_NR
3223#endif
3224#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3225# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3226#else
3227# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#endif
3229#define WL_LINE WL_SBR + 1 /* text in the line */
3230 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003231#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 int feedback_col = 0;
3233 int feedback_old_attr = -1;
3234#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003235 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaar860cae12010-06-05 23:22:07 +02003237#ifdef FEAT_CONCEAL
3238 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003239 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003240 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003241 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003242 int is_concealing = FALSE;
3243 int boguscols = 0; /* nonexistent columns added to force
3244 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003245 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003246 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003247 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003248 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003249# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003250# define FIX_FOR_BOGUSCOLS \
3251 { \
3252 n_extra += vcol_off; \
3253 vcol -= vcol_off; \
3254 vcol_off = 0; \
3255 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003256 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003257 boguscols = 0; \
3258 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003259#else
3260# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
3263 if (startrow > endrow) /* past the end already! */
3264 return startrow;
3265
3266 row = startrow;
3267 screen_row = row + W_WINROW(wp);
3268
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003269 if (!number_only)
3270 {
3271 /*
3272 * To speed up the loop below, set extra_check when there is linebreak,
3273 * trailing white space and/or syntax processing to be done.
3274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003276 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#endif
3278#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003279 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003280# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003281 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003282# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003285 /* Prepare for syntax highlighting in this line. When there is an
3286 * error, stop syntax highlighting. */
3287 save_did_emsg = did_emsg;
3288 did_emsg = FALSE;
3289 syntax_start(wp, lnum);
3290 if (did_emsg)
3291 wp->w_s->b_syn_error = TRUE;
3292 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003293 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003294 did_emsg = save_did_emsg;
3295#ifdef SYN_TIME_LIMIT
3296 if (!wp->w_s->b_syn_slow)
3297#endif
3298 {
3299 has_syntax = TRUE;
3300 extra_check = TRUE;
3301 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003304
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003305 /* Check for columns to display for 'colorcolumn'. */
3306 color_cols = wp->w_p_cc_cols;
3307 if (color_cols != NULL)
3308 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003309#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003310
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003311#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003312 if (term_show_buffer(wp->w_buffer))
3313 {
3314 extra_check = TRUE;
3315 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003316 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003317 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003318#endif
3319
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003320#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003321 if (wp->w_p_spell
3322 && *wp->w_s->b_p_spl != NUL
3323 && wp->w_s->b_langp.ga_len > 0
3324 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003325 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003326 /* Prepare for spell checking. */
3327 has_spell = TRUE;
3328 extra_check = TRUE;
3329
Bram Moolenaar7701f302018-10-02 21:20:32 +02003330 /* Get the start of the next line, so that words that wrap to the
3331 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003332 * Trick: skip a few chars for C/shell/Vim comments */
3333 nextline[SPWORDLEN] = NUL;
3334 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3335 {
3336 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3337 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3338 }
3339
Bram Moolenaar7701f302018-10-02 21:20:32 +02003340 /* When a word wrapped from the previous line the start of the
3341 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003342 if (lnum == checked_lnum)
3343 cur_checked_col = checked_col;
3344 checked_lnum = 0;
3345
3346 /* When there was a sentence end in the previous line may require a
3347 * word starting with capital in this line. In line 1 always check
3348 * the first word. */
3349 if (lnum != capcol_lnum)
3350 cap_col = -1;
3351 if (lnum == 1)
3352 cap_col = 0;
3353 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#endif
3356
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 /*
3358 * handle visual active in this window
3359 */
3360 fromcol = -10;
3361 tocol = MAXCOL;
3362 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003364 /* Visual is after curwin->w_cursor */
3365 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003367 top = &curwin->w_cursor;
3368 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003372 top = &VIsual;
3373 bot = &curwin->w_cursor;
3374 }
3375 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3376 if (VIsual_mode == Ctrl_V) /* block mode */
3377 {
3378 if (lnum_in_visual_area)
3379 {
3380 fromcol = wp->w_old_cursor_fcol;
3381 tocol = wp->w_old_cursor_lcol;
3382 }
3383 }
3384 else /* non-block mode */
3385 {
3386 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003390 if (VIsual_mode == 'V') /* linewise */
3391 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 else
3393 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003394 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3395 if (gchar_pos(top) == NUL)
3396 tocol = fromcol + 1;
3397 }
3398 }
3399 if (VIsual_mode != 'V' && lnum == bot->lnum)
3400 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003401 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003402 {
3403 fromcol = -10;
3404 tocol = MAXCOL;
3405 }
3406 else if (bot->col == MAXCOL)
3407 tocol = MAXCOL;
3408 else
3409 {
3410 pos = *bot;
3411 if (*p_sel == 'e')
3412 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3413 else
3414 {
3415 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3416 ++tocol;
3417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419 }
3420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003422 /* Check if the character under the cursor should not be inverted */
3423 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003424#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003426#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003427 )
3428 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003430 /* if inverting in this line set area_highlighting */
3431 if (fromcol >= 0)
3432 {
3433 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003434 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003436 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003437 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003439 && clip_isautosel_plus()))
3440 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 /*
3446 * handle 'incsearch' and ":s///c" highlighting
3447 */
3448 else if (highlight_match
3449 && wp == curwin
3450 && lnum >= curwin->w_cursor.lnum
3451 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 if (lnum == curwin->w_cursor.lnum)
3454 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003455 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003456 else
3457 fromcol = 0;
3458 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3459 {
3460 pos.lnum = lnum;
3461 pos.col = search_match_endcol;
3462 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3463 }
3464 else
3465 tocol = MAXCOL;
3466 /* do at least one character; happens when past end of line */
3467 if (fromcol == tocol)
3468 tocol = fromcol + 1;
3469 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003470 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473
3474#ifdef FEAT_DIFF
3475 filler_lines = diff_check(wp, lnum);
3476 if (filler_lines < 0)
3477 {
3478 if (filler_lines == -1)
3479 {
3480 if (diff_find_change(wp, lnum, &change_start, &change_end))
3481 diff_hlf = HLF_ADD; /* added line */
3482 else if (change_start == 0)
3483 diff_hlf = HLF_TXD; /* changed text */
3484 else
3485 diff_hlf = HLF_CHD; /* changed line */
3486 }
3487 else
3488 diff_hlf = HLF_ADD; /* added line */
3489 filler_lines = 0;
3490 area_highlighting = TRUE;
3491 }
3492 if (lnum == wp->w_topline)
3493 filler_lines = wp->w_topfill;
3494 filler_todo = filler_lines;
3495#endif
3496
3497#ifdef LINE_ATTR
3498# ifdef FEAT_SIGNS
3499 /* If this line has a sign with line highlighting set line_attr. */
3500 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3501 if (v != 0)
3502 line_attr = sign_get_attr((int)v, TRUE);
3503# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003504# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003506 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003507 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508# endif
3509 if (line_attr != 0)
3510 area_highlighting = TRUE;
3511#endif
3512
3513 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3514 ptr = line;
3515
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003516#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003517 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003518 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003519 /* For checking first word with a capital skip white space. */
3520 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003521 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003522
Bram Moolenaar30abd282005-06-22 22:35:10 +00003523 /* To be able to spell-check over line boundaries copy the end of the
3524 * current line into nextline[]. Above the start of the next line was
3525 * copied to nextline[SPWORDLEN]. */
3526 if (nextline[SPWORDLEN] == NUL)
3527 {
3528 /* No next line or it is empty. */
3529 nextlinecol = MAXCOL;
3530 nextline_idx = 0;
3531 }
3532 else
3533 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003534 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003535 if (v < SPWORDLEN)
3536 {
3537 /* Short line, use it completely and append the start of the
3538 * next line. */
3539 nextlinecol = 0;
3540 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003541 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003542 nextline_idx = v + 1;
3543 }
3544 else
3545 {
3546 /* Long line, use only the last SPWORDLEN bytes. */
3547 nextlinecol = v - SPWORDLEN;
3548 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3549 nextline_idx = SPWORDLEN + 1;
3550 }
3551 }
3552 }
3553#endif
3554
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003555 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003557 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003558 extra_check = TRUE;
3559 /* find start of trailing whitespace */
3560 if (lcs_trail)
3561 {
3562 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003563 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003564 --trailcol;
3565 trailcol += (colnr_T) (ptr - line);
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003569 if (*wp->w_p_wcr != NUL)
3570 {
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003571 wcr_attr = syn_name2attr(wp->w_p_wcr);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003572
3573 // 'wincolor' highlighting for the whole window
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003574 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003575 {
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003576 win_attr = wcr_attr;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003577 area_highlighting = TRUE;
3578 }
3579 }
3580#ifdef FEAT_TEXT_PROP
3581 if (bt_popup(wp->w_buffer))
3582 {
3583 screen_line_flags |= SLF_POPUP;
3584
3585 if (win_attr == 0)
3586 {
3587 win_attr = HL_ATTR(HLF_PNI);
3588 area_highlighting = TRUE;
3589 }
3590 }
3591#endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 /*
3594 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3595 * first character to be displayed.
3596 */
3597 if (wp->w_p_wrap)
3598 v = wp->w_skipcol;
3599 else
3600 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003601 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 while (vcol < v && *ptr != NUL)
3606 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003607 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003610 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003613 /* When:
3614 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003615 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003616 * - 'virtualedit' is set, or
3617 * - the visual mode is active,
3618 * the end of the line may be before the start of the displayed part.
3619 */
3620 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003621#ifdef FEAT_SYN_HL
3622 wp->w_p_cuc || draw_color_col ||
3623#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003624 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003625 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 /* Handle a character that's not completely on the screen: Put ptr at
3629 * that character but skip the first few screen characters. */
3630 if (vcol > v)
3631 {
3632 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003634 /* If the character fits on the screen, don't need to skip it.
3635 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003636 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003637 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639
3640 /*
3641 * Adjust for when the inverted text is before the screen,
3642 * and when the start of the inverted text is before the screen.
3643 */
3644 if (tocol <= vcol)
3645 fromcol = 0;
3646 else if (fromcol >= 0 && fromcol < vcol)
3647 fromcol = vcol;
3648
3649#ifdef FEAT_LINEBREAK
3650 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3651 if (wp->w_p_wrap)
3652 need_showbreak = TRUE;
3653#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003654#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003655 /* When spell checking a word we need to figure out the start of the
3656 * word and if it's badly spelled or not. */
3657 if (has_spell)
3658 {
3659 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003660 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003661 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003662
3663 pos = wp->w_cursor;
3664 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003665 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003666 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003667
3668 /* spell_move_to() may call ml_get() and make "line" invalid */
3669 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3670 ptr = line + linecol;
3671
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003672 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003673 {
3674 /* no bad word found at line start, don't check until end of a
3675 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003676 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003677 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003678 }
3679 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003680 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003681 /* bad word found, use attributes until end of word */
3682 word_end = wp->w_cursor.col + len + 1;
3683
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003684 /* Turn index into actual attributes. */
3685 if (spell_hlf != HLF_COUNT)
3686 spell_attr = highlight_attr[spell_hlf];
3687 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003688 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003689
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003690# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003691 /* Need to restart syntax highlighting for this line. */
3692 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003693 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003694# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003695 }
3696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698
3699 /*
3700 * Correct highlighting for cursor that can't be disabled.
3701 * Avoids having to check this for each character.
3702 */
3703 if (fromcol >= 0)
3704 {
3705 if (noinvcur)
3706 {
3707 if ((colnr_T)fromcol == wp->w_virtcol)
3708 {
3709 /* highlighting starts at cursor, let it start just after the
3710 * cursor */
3711 fromcol_prev = fromcol;
3712 fromcol = -1;
3713 }
3714 else if ((colnr_T)fromcol < wp->w_virtcol)
3715 /* restart highlighting after the cursor */
3716 fromcol_prev = wp->w_virtcol;
3717 }
3718 if (fromcol >= tocol)
3719 fromcol = -1;
3720 }
3721
3722#ifdef FEAT_SEARCH_EXTRA
3723 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003724 * Handle highlighting the last used search pattern and matches.
3725 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003726 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003728 cur = wp->w_match_head;
3729 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003730 while ((cur != NULL || shl_flag == FALSE) && !number_only
3731 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003733 if (shl_flag == FALSE)
3734 {
3735 shl = &search_hl;
3736 shl_flag = TRUE;
3737 }
3738 else
3739 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003740 shl->startcol = MAXCOL;
3741 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003743 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003744 v = (long)(ptr - line);
3745 if (cur != NULL)
3746 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003747 next_search_hl(wp, shl, lnum, (colnr_T)v,
3748 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003749
3750 /* Need to get the line again, a multi-line regexp may have made it
3751 * invalid. */
3752 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3753 ptr = line + v;
3754
3755 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003757 if (shl->lnum == lnum)
3758 shl->startcol = shl->rm.startpos[0].col;
3759 else
3760 shl->startcol = 0;
3761 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3762 - shl->rm.startpos[0].lnum)
3763 shl->endcol = shl->rm.endpos[0].col;
3764 else
3765 shl->endcol = MAXCOL;
3766 /* Highlight one character for an empty match. */
3767 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003769 if (has_mbyte && line[shl->endcol] != NUL)
3770 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3771 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003772 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003774 if ((long)shl->startcol < v) /* match at leftcol */
3775 {
3776 shl->attr_cur = shl->attr;
3777 search_attr = shl->attr;
3778 }
3779 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003781 if (shl != &search_hl && cur != NULL)
3782 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784#endif
3785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003786#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003787 // Cursor line highlighting for 'cursorline' in the current window.
3788 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003789 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003790 // Do not show the cursor line when Visual mode is active, because it's
3791 // not clear what is selected then. Do update w_last_cursorline.
3792 if (!(wp == curwin && VIsual_active))
3793 {
3794 line_attr = HL_ATTR(HLF_CUL);
3795 area_highlighting = TRUE;
3796 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003797 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003798 }
3799#endif
3800
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003801#ifdef FEAT_TEXT_PROP
3802 {
3803 char_u *prop_start;
3804
3805 text_prop_count = get_text_props(wp->w_buffer, lnum,
3806 &prop_start, FALSE);
3807 if (text_prop_count > 0)
3808 {
3809 // Make a copy of the properties, so that they are properly
3810 // aligned.
3811 text_props = (textprop_T *)alloc(
3812 text_prop_count * sizeof(textprop_T));
3813 if (text_props != NULL)
3814 mch_memmove(text_props, prop_start,
3815 text_prop_count * sizeof(textprop_T));
3816
3817 // Allocate an array for the indexes.
3818 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3819 area_highlighting = TRUE;
3820 extra_check = TRUE;
3821 }
3822 }
3823#endif
3824
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003825 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#ifdef FEAT_RIGHTLEFT
3829 if (wp->w_p_rl)
3830 {
3831 /* Rightleft window: process the text in the normal direction, but put
3832 * it in current_ScreenLine[] from right to left. Start at the
3833 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003834 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003836 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838#endif
3839
3840 /*
3841 * Repeat for the whole displayed line.
3842 */
3843 for (;;)
3844 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003845#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003846 int has_match_conc = 0; // match wants to conceal
3847 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 /* Skip this quickly when working on the text. */
3850 if (draw_state != WL_LINE)
3851 {
3852#ifdef FEAT_CMDWIN
3853 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3854 {
3855 draw_state = WL_CMDLINE;
3856 if (cmdwin_type != 0 && wp == curwin)
3857 {
3858 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003860 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003861 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003862 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 }
3865#endif
3866
3867#ifdef FEAT_FOLDING
3868 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3869 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003870 int fdc = compute_foldcolumn(wp, 0);
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003873 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003875 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003876 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003877 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003878 p_extra_free = alloc(12 + 1);
3879
3880 if (p_extra_free != NULL)
3881 {
3882 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3883 n_extra = fdc;
3884 p_extra_free[n_extra] = NUL;
3885 p_extra = p_extra_free;
3886 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003887 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003888 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891 }
3892#endif
3893
3894#ifdef FEAT_SIGNS
3895 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3896 {
3897 draw_state = WL_SIGN;
3898 /* Show the sign column when there are any signs in this
3899 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003900 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003902 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003904 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905# endif
3906
3907 /* Draw two cells with the sign value or blank. */
3908 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003909 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003910 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 n_extra = 2;
3912
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003913 if (row == startrow
3914#ifdef FEAT_DIFF
3915 + filler_lines && filler_todo <= 0
3916#endif
3917 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3920 SIGN_TEXT);
3921# ifdef FEAT_SIGN_ICONS
3922 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3923 SIGN_ICON);
3924 if (gui.in_use && icon_sign != 0)
3925 {
3926 /* Use the image in this position. */
3927 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003928 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929# ifdef FEAT_NETBEANS_INTG
3930 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003933 c_final = NUL;
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935# endif
3936 char_attr = icon_sign;
3937 }
3938 else
3939# endif
3940 if (text_sign != 0)
3941 {
3942 p_extra = sign_get_text(text_sign);
3943 if (p_extra != NULL)
3944 {
3945 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003946 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003947 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 char_attr = sign_get_attr(text_sign, FALSE);
3950 }
3951 }
3952 }
3953 }
3954#endif
3955
3956 if (draw_state == WL_NR - 1 && n_extra == 0)
3957 {
3958 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003959 /* Display the absolute or relative line number. After the
3960 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3961 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 && (row == startrow
3963#ifdef FEAT_DIFF
3964 + filler_lines
3965#endif
3966 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3967 {
3968 /* Draw the line number (empty space after wrapping). */
3969 if (row == startrow
3970#ifdef FEAT_DIFF
3971 + filler_lines
3972#endif
3973 )
3974 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003975 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003976 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003977
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003978 if (wp->w_p_nu && !wp->w_p_rnu)
3979 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003980 num = (long)lnum;
3981 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003982 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003983 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003984 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003985 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003986 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003987 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003988 num = lnum;
3989 fmt = "%-*ld ";
3990 }
3991 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003992
Bram Moolenaar700e7342013-01-30 12:31:36 +01003993 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003994 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (wp->w_skipcol > 0)
3996 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3997 *p_extra = '-';
3998#ifdef FEAT_RIGHTLEFT
3999 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004000 {
4001 char_u *p1, *p2;
4002 int t;
4003
4004 // like rl_mirror(), but keep the space at the end
4005 p2 = skiptowhite(extra) - 1;
4006 for (p1 = extra; p1 < p2; ++p1, --p2)
4007 {
4008 t = *p1;
4009 *p1 = *p2;
4010 *p2 = t;
4011 }
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013#endif
4014 p_extra = extra;
4015 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004016 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004021 c_final = NUL;
4022 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004023 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004024 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004025#ifdef FEAT_SYN_HL
4026 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004027 * the current line differently.
4028 * TODO: Can we use CursorLine instead of CursorLineNr
4029 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004030 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004031 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004032 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 }
4036
Bram Moolenaar597a4222014-06-25 14:39:50 +02004037#ifdef FEAT_LINEBREAK
4038 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4039 && n_extra == 0 && *p_sbr != NUL)
4040 /* draw indent after showbreak value */
4041 draw_state = WL_BRI;
4042 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4043 /* After the showbreak, draw the breakindent */
4044 draw_state = WL_BRI - 1;
4045
4046 /* draw 'breakindent': indent wrapped text accordingly */
4047 if (draw_state == WL_BRI - 1 && n_extra == 0)
4048 {
4049 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004050 /* if need_showbreak is set, breakindent also applies */
4051 if (wp->w_p_bri && n_extra == 0
4052 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004053# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004055# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004056 )
4057 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004058 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004059# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004060 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004061 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004062 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4065 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004066 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004067# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004068 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004069# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004070 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004071 c_extra = ' ';
4072 n_extra = get_breakindent_win(wp,
4073 ml_get_buf(wp->w_buffer, lnum, FALSE));
4074 /* Correct end of highlighted area for 'breakindent',
4075 * required when 'linebreak' is also set. */
4076 if (tocol == vcol)
4077 tocol += n_extra;
4078 }
4079 }
4080#endif
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4083 if (draw_state == WL_SBR - 1 && n_extra == 0)
4084 {
4085 draw_state = WL_SBR;
4086# ifdef FEAT_DIFF
4087 if (filler_todo > 0)
4088 {
4089 /* Draw "deleted" diff line(s). */
4090 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004093 c_final = NUL;
4094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004098 c_final = NUL;
4099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100# ifdef FEAT_RIGHTLEFT
4101 if (wp->w_p_rl)
4102 n_extra = col + 1;
4103 else
4104# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004105 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004106 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108# endif
4109# ifdef FEAT_LINEBREAK
4110 if (*p_sbr != NUL && need_showbreak)
4111 {
4112 /* Draw 'showbreak' at the start of each broken line. */
4113 p_extra = p_sbr;
4114 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004115 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004117 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004119 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 /* Correct end of highlighted area for 'showbreak',
4121 * required when 'linebreak' is also set. */
4122 if (tocol == vcol)
4123 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004124#ifdef FEAT_SYN_HL
4125 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004126 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004127 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004128 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131# endif
4132 }
4133#endif
4134
4135 if (draw_state == WL_LINE - 1 && n_extra == 0)
4136 {
4137 draw_state = WL_LINE;
4138 if (saved_n_extra)
4139 {
4140 /* Continue item from end of wrapped line. */
4141 n_extra = saved_n_extra;
4142 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004143 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 p_extra = saved_p_extra;
4145 char_attr = saved_char_attr;
4146 }
4147 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004148 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150 }
4151
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004152 // When still displaying '$' of change command, stop at cursor.
4153 // When only displaying the (relative) line number and that's done,
4154 // stop here.
4155 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004156 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157#ifdef FEAT_DIFF
4158 && filler_todo <= 0
4159#endif
4160 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004161 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004163 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004164 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004165 /* Pretend we have finished updating the window. Except when
4166 * 'cursorcolumn' is set. */
4167#ifdef FEAT_SYN_HL
4168 if (wp->w_p_cuc)
4169 row = wp->w_cline_row + wp->w_cline_height;
4170 else
4171#endif
4172 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 break;
4174 }
4175
Bram Moolenaar637532b2019-01-03 21:44:40 +01004176 if (draw_state == WL_LINE && (area_highlighting
4177#ifdef FEAT_SPELL
4178 || has_spell
4179#endif
4180 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
4182 /* handle Visual or match highlighting in this line */
4183 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4185 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004187 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004189 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 else if (area_attr != 0
4191 && (vcol == tocol
4192 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195#ifdef FEAT_SEARCH_EXTRA
4196 if (!n_extra)
4197 {
4198 /*
4199 * Check for start/end of search pattern match.
4200 * After end, check for start/end of next match.
4201 * When another match, have to check for start again.
4202 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004203 * Do this for 'search_hl' and the match list (ordered by
4204 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004206 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004207 cur = wp->w_match_head;
4208 shl_flag = FALSE;
4209 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004211 if (shl_flag == FALSE
4212 && ((cur != NULL
4213 && cur->priority > SEARCH_HL_PRIORITY)
4214 || cur == NULL))
4215 {
4216 shl = &search_hl;
4217 shl_flag = TRUE;
4218 }
4219 else
4220 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004221 if (cur != NULL)
4222 cur->pos.cur = 0;
4223 pos_inprogress = TRUE;
4224 while (shl->rm.regprog != NULL
4225 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004227 if (shl->startcol != MAXCOL
4228 && v >= (long)shl->startcol
4229 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004231 int tmp_col = v + MB_PTR2LEN(ptr);
4232
4233 if (shl->endcol < tmp_col)
4234 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004236#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004237 // Match with the "Conceal" group results in hiding
4238 // the match.
4239 if (cur != NULL
4240 && shl != &search_hl
4241 && syn_name2id((char_u *)"Conceal")
4242 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004243 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004244 has_match_conc =
4245 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004246 match_conc = cur->conceal_char;
4247 }
4248 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004249 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004252 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004255 next_search_hl(wp, shl, lnum, (colnr_T)v,
4256 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004257 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004258 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 /* Need to get the line again, a multi-line regexp
4261 * may have made it invalid. */
4262 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4263 ptr = line + v;
4264
4265 if (shl->lnum == lnum)
4266 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004267 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004269 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004273 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 {
4275 /* highlight empty match, try again after
4276 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004278 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004279 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004281 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283
4284 /* Loop to check if the match starts at the
4285 * current position */
4286 continue;
4287 }
4288 }
4289 break;
4290 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004291 if (shl != &search_hl && cur != NULL)
4292 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004294
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004295 /* Use attributes from match with highest priority among
4296 * 'search_hl' and the match list. */
4297 search_attr = search_hl.attr_cur;
4298 cur = wp->w_match_head;
4299 shl_flag = FALSE;
4300 while (cur != NULL || shl_flag == FALSE)
4301 {
4302 if (shl_flag == FALSE
4303 && ((cur != NULL
4304 && cur->priority > SEARCH_HL_PRIORITY)
4305 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004306 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004307 shl = &search_hl;
4308 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004309 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004310 else
4311 shl = &cur->hl;
4312 if (shl->attr_cur != 0)
4313 search_attr = shl->attr_cur;
4314 if (shl != &search_hl && cur != NULL)
4315 cur = cur->next;
4316 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004317 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004318 if (*ptr == NUL && (did_line_attr >= 1
4319 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004320 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322#endif
4323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004325 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004327 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4328 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004330 if (diff_hlf == HLF_TXD && ptr - line > change_end
4331 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004333 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004334 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004335 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004338
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004339#ifdef FEAT_TEXT_PROP
4340 if (text_props != NULL)
4341 {
4342 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004343 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004344
4345 // Check if any active property ends.
4346 for (pi = 0; pi < text_props_active; ++pi)
4347 {
4348 int tpi = text_prop_idxs[pi];
4349
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004350 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004351 + text_props[tpi].tp_len)
4352 {
4353 if (pi + 1 < text_props_active)
4354 mch_memmove(text_prop_idxs + pi,
4355 text_prop_idxs + pi + 1,
4356 sizeof(int)
4357 * (text_props_active - (pi + 1)));
4358 --text_props_active;
4359 --pi;
4360 }
4361 }
4362
4363 // Add any text property that starts in this column.
4364 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004365 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004366 text_prop_idxs[text_props_active++] = text_prop_next++;
4367
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004368 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004369 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004370 if (text_props_active > 0)
4371 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004372 // Sort the properties on priority and/or starting last.
4373 // Then combine the attributes, highest priority last.
4374 current_text_props = text_props;
4375 current_buf = wp->w_buffer;
4376 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4377 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004378
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004379 for (pi = 0; pi < text_props_active; ++pi)
4380 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004381 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004382 proptype_T *pt = text_prop_type_by_id(
4383 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004384
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004385 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004386 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004387 int pt_attr = syn_id2attr(pt->pt_hl_id);
4388
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004389 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004390 text_prop_attr =
4391 hl_combine_attr(text_prop_attr, pt_attr);
4392 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004393 }
4394 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004395 }
4396 }
4397#endif
4398
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004399 /* Decide which of the highlight attributes to use. */
4400 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004401#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004402 if (area_attr != 0)
4403 char_attr = hl_combine_attr(line_attr, area_attr);
4404 else if (search_attr != 0)
4405 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004406# ifdef FEAT_TEXT_PROP
4407 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004408 {
4409 char_attr = hl_combine_attr(
4410 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4411 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004412# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004413 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004414 || vcol < fromcol || vcol_prev < fromcol_prev
4415 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004416 // Use line_attr when not in the Visual or 'incsearch' area
4417 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004418 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004419#else
4420 if (area_attr != 0)
4421 char_attr = area_attr;
4422 else if (search_attr != 0)
4423 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004424#endif
4425 else
4426 {
4427 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004428#ifdef FEAT_TEXT_PROP
4429 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004430 {
4431 if (text_prop_combine)
4432 char_attr = hl_combine_attr(
4433 syntax_attr, text_prop_attr);
4434 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004435 char_attr = hl_combine_attr(
4436 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004437 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004438 else
4439#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004440#ifdef FEAT_SYN_HL
4441 if (has_syntax)
4442 char_attr = syntax_attr;
4443 else
4444#endif
4445 char_attr = 0;
4446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004448 if (char_attr == 0)
4449 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /*
4452 * Get the next character to put on the screen.
4453 */
4454 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004455 * The "p_extra" points to the extra stuff that is inserted to
4456 * represent special characters (non-printable stuff) and other
4457 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004458 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004459 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4460 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4462 */
4463 if (n_extra > 0)
4464 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004465 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004467 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004469 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
4471 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004472 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004473 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 else
4476 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else
4479 {
4480 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (has_mbyte)
4482 {
4483 mb_c = c;
4484 if (enc_utf8)
4485 {
4486 /* If the UTF-8 character is more than one byte:
4487 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004488 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 mb_utf8 = FALSE;
4490 if (mb_l > n_extra)
4491 mb_l = 1;
4492 else if (mb_l > 1)
4493 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004494 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004496 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 }
4499 else
4500 {
4501 /* if this is a DBCS character, put it in "mb_c" */
4502 mb_l = MB_BYTE2LEN(c);
4503 if (mb_l >= n_extra)
4504 mb_l = 1;
4505 else if (mb_l > 1)
4506 mb_c = (c << 8) + p_extra[1];
4507 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004508 if (mb_l == 0) /* at the NUL at end-of-line */
4509 mb_l = 1;
4510
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 /* If a double-width char doesn't fit display a '>' in the
4512 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004513 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514# ifdef FEAT_RIGHTLEFT
4515 wp->w_p_rl ? (col <= 0) :
4516# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004517 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 && (*mb_char2cells)(mb_c) == 2)
4519 {
4520 c = '>';
4521 mb_c = c;
4522 mb_l = 1;
4523 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004524 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 /* put the pointer back to output the double-width
4526 * character at the start of the next line. */
4527 ++n_extra;
4528 --p_extra;
4529 }
4530 else
4531 {
4532 n_extra -= mb_l - 1;
4533 p_extra += mb_l - 1;
4534 }
4535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 ++p_extra;
4537 }
4538 --n_extra;
4539 }
4540 else
4541 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004542#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004543 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004544#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004545
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004546 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004547 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 /*
4549 * Get a character from the line itself.
4550 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004551 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004552#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004553 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (has_mbyte)
4556 {
4557 mb_c = c;
4558 if (enc_utf8)
4559 {
4560 /* If the UTF-8 character is more than one byte: Decode it
4561 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004562 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 mb_utf8 = FALSE;
4564 if (mb_l > 1)
4565 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004566 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /* Overlong encoded ASCII or ASCII with composing char
4568 * is displayed normally, except a NUL. */
4569 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004570 {
4571 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004572#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004573 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004574#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004577
4578 /* At start of the line we can have a composing char.
4579 * Draw it as a space with a composing char. */
4580 if (utf_iscomposing(mb_c))
4581 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004582 int i;
4583
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004584 for (i = Screen_mco - 1; i > 0; --i)
4585 u8cc[i] = u8cc[i - 1];
4586 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004587 mb_c = ' ';
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590
4591 if ((mb_l == 1 && c >= 0x80)
4592 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004593 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
4595 /*
4596 * Illegal UTF-8 byte: display as <xx>.
4597 * Non-BMP character : display as ? or fullwidth ?.
4598 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004599 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004600# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004601 if (wp->w_p_rl) /* reverse */
4602 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 p_extra = extra;
4605 c = *p_extra;
4606 mb_c = mb_ptr2char_adv(&p_extra);
4607 mb_utf8 = (c >= 0x80);
4608 n_extra = (int)STRLEN(p_extra);
4609 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004610 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 if (area_attr == 0 && search_attr == 0)
4612 {
4613 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004614 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 saved_attr2 = char_attr; /* save current attr */
4616 }
4617 }
4618 else if (mb_l == 0) /* at the NUL at end-of-line */
4619 mb_l = 1;
4620#ifdef FEAT_ARABIC
4621 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4622 {
4623 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004624 int pc, pc1, nc;
4625 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 /* The idea of what is the previous and next
4628 * character depends on 'rightleft'. */
4629 if (wp->w_p_rl)
4630 {
4631 pc = prev_c;
4632 pc1 = prev_c1;
4633 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 else
4637 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004638 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 prev_c = mb_c;
4643
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004644 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 else
4647 prev_c = mb_c;
4648#endif
4649 }
4650 else /* enc_dbcs */
4651 {
4652 mb_l = MB_BYTE2LEN(c);
4653 if (mb_l == 0) /* at the NUL at end-of-line */
4654 mb_l = 1;
4655 else if (mb_l > 1)
4656 {
4657 /* We assume a second byte below 32 is illegal.
4658 * Hopefully this is OK for all double-byte encodings!
4659 */
4660 if (ptr[1] >= 32)
4661 mb_c = (c << 8) + ptr[1];
4662 else
4663 {
4664 if (ptr[1] == NUL)
4665 {
4666 /* head byte at end of line */
4667 mb_l = 1;
4668 transchar_nonprint(extra, c);
4669 }
4670 else
4671 {
4672 /* illegal tail byte */
4673 mb_l = 2;
4674 STRCPY(extra, "XX");
4675 }
4676 p_extra = extra;
4677 n_extra = (int)STRLEN(extra) - 1;
4678 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004679 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 c = *p_extra++;
4681 if (area_attr == 0 && search_attr == 0)
4682 {
4683 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004684 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 saved_attr2 = char_attr; /* save current attr */
4686 }
4687 mb_c = c;
4688 }
4689 }
4690 }
4691 /* If a double-width char doesn't fit display a '>' in the
4692 * last column; the character is displayed at the start of the
4693 * next line. */
4694 if ((
4695# ifdef FEAT_RIGHTLEFT
4696 wp->w_p_rl ? (col <= 0) :
4697# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004698 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 && (*mb_char2cells)(mb_c) == 2)
4700 {
4701 c = '>';
4702 mb_c = c;
4703 mb_utf8 = FALSE;
4704 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004705 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004706 // Put pointer back so that the character will be
4707 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004709#ifdef FEAT_CONCEAL
4710 did_decrement_ptr = TRUE;
4711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 }
4713 else if (*ptr != NUL)
4714 ptr += mb_l - 1;
4715
4716 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004717 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004718 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004719 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004722 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004723 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 c = ' ';
4725 if (area_attr == 0 && search_attr == 0)
4726 {
4727 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004728 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 saved_attr2 = char_attr; /* save current attr */
4730 }
4731 mb_c = c;
4732 mb_utf8 = FALSE;
4733 mb_l = 1;
4734 }
4735
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 ++ptr;
4738
4739 if (extra_check)
4740 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004741#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004742 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004743#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004745#ifdef FEAT_TERMINAL
4746 if (get_term_attr)
4747 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004748 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004749
4750 if (!attr_pri)
4751 char_attr = syntax_attr;
4752 else
4753 char_attr = hl_combine_attr(syntax_attr, char_attr);
4754 }
4755#endif
4756
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004757#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004758 // Get syntax attribute, unless still at the start of the line
4759 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004760 v = (long)(ptr - line);
4761 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 /* Get the syntax attribute for the character. If there
4764 * is an error, disable syntax highlighting. */
4765 save_did_emsg = did_emsg;
4766 did_emsg = FALSE;
4767
Bram Moolenaar217ad922005-03-20 22:37:15 +00004768 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004769# ifdef FEAT_SPELL
4770 has_spell ? &can_spell :
4771# endif
4772 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004775 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004776 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004777 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004778 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 else
4781 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004782
4783 // combine syntax attribute with 'wincolor'
4784 if (win_attr != 0)
4785 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4786
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004787#ifdef SYN_TIME_LIMIT
4788 if (wp->w_s->b_syn_slow)
4789 has_syntax = FALSE;
4790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 /* Need to get the line again, a multi-line regexp may
4793 * have made it invalid. */
4794 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4795 ptr = line + v;
4796
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004797# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004798 // Text properties overrule syntax highlighting or combine.
4799 if (text_prop_attr == 0 || text_prop_combine)
4800# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004801 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004802 int comb_attr = syntax_attr;
4803# ifdef FEAT_TEXT_PROP
4804 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4805# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004806 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004807 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004808 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004809 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004810 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004811# ifdef FEAT_CONCEAL
4812 /* no concealing past the end of the line, it interferes
4813 * with line highlighting */
4814 if (c == NUL)
4815 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004816 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004817 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004820#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004821
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004822#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004823 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004824 * Only do this when there is no syntax highlighting, the
4825 * @Spell cluster is not used or the current syntax item
4826 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004827 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004828 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004829 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004830 if (c != 0 && (
4831# ifdef FEAT_SYN_HL
4832 !has_syntax ||
4833# endif
4834 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004835 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004836 char_u *prev_ptr, *p;
4837 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004838 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004839 if (has_mbyte)
4840 {
4841 prev_ptr = ptr - mb_l;
4842 v -= mb_l - 1;
4843 }
4844 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004845 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004846
4847 /* Use nextline[] if possible, it has the start of the
4848 * next line concatenated. */
4849 if ((prev_ptr - line) - nextlinecol >= 0)
4850 p = nextline + (prev_ptr - line) - nextlinecol;
4851 else
4852 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004853 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004854 len = spell_check(wp, p, &spell_hlf, &cap_col,
4855 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004856 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004857
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004858 /* In Insert mode only highlight a word that
4859 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004860 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004861 && (State & INSERT) != 0
4862 && wp->w_cursor.lnum == lnum
4863 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004864 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004865 && wp->w_cursor.col < (colnr_T)word_end)
4866 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004867 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004868 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004869 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004870
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004871 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004872 && (p - nextline) + len > nextline_idx)
4873 {
4874 /* Remember that the good word continues at the
4875 * start of the next line. */
4876 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004877 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004878 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004879
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004880 /* Turn index into actual attributes. */
4881 if (spell_hlf != HLF_COUNT)
4882 spell_attr = highlight_attr[spell_hlf];
4883
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004884 if (cap_col > 0)
4885 {
4886 if (p != prev_ptr
4887 && (p - nextline) + cap_col >= nextline_idx)
4888 {
4889 /* Remember that the word in the next line
4890 * must start with a capital. */
4891 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004892 cap_col = (int)((p - nextline) + cap_col
4893 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004894 }
4895 else
4896 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004897 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004898 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004899 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004900 }
4901 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004902 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004903 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004904 char_attr = hl_combine_attr(char_attr, spell_attr);
4905 else
4906 char_attr = hl_combine_attr(spell_attr, char_attr);
4907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#endif
4909#ifdef FEAT_LINEBREAK
4910 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004911 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004913 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004914 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004916 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004917 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004918
Bram Moolenaar597a4222014-06-25 14:39:50 +02004919 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004920 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004921 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004922 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004923# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004924 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004925 wp->w_buffer->b_p_vts_array) - 1;
4926# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004927 n_extra = (int)wp->w_buffer->b_p_ts
4928 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004929# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004930
Bram Moolenaar4df70292015-03-21 14:20:16 +01004931 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004932 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004933 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004934 {
4935#ifdef FEAT_CONCEAL
4936 if (c == TAB)
4937 /* See "Tab alignment" below. */
4938 FIX_FOR_BOGUSCOLS;
4939#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004940 if (!wp->w_p_list)
4941 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944#endif
4945
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004946 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4947 // But not when the character is followed by a composing
4948 // character (use mb_l to check that).
4949 if (wp->w_p_list
4950 && ((((c == 160 && mb_l == 1)
4951 || (mb_utf8
4952 && ((mb_c == 160 && mb_l == 2)
4953 || (mb_c == 0x202f && mb_l == 3))))
4954 && lcs_nbsp)
4955 || (c == ' '
4956 && mb_l == 1
4957 && lcs_space
4958 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004959 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004960 c = (c == ' ') ? lcs_space : lcs_nbsp;
4961 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004962 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004963 n_attr = 1;
4964 extra_attr = HL_ATTR(HLF_8);
4965 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004966 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004967 mb_c = c;
4968 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004969 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004970 mb_utf8 = TRUE;
4971 u8cc[0] = 0;
4972 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004973 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004974 else
4975 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004976 }
4977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4979 {
4980 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004981 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004984 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 saved_attr2 = char_attr; /* save current attr */
4986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004988 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
4990 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004991 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004992 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 else
4995 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 }
4998
4999 /*
5000 * Handling of non-printable characters.
5001 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005002 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 /*
5005 * when getting a character from the file, we may have to
5006 * turn it into something else on the way to putting it
5007 * into "ScreenLines".
5008 */
5009 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5010 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005011 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005012 long vcol_adjusted = vcol; /* removed showbreak length */
5013#ifdef FEAT_LINEBREAK
5014 /* only adjust the tab_len, when at the first column
5015 * after the showbreak value was drawn */
5016 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5017 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005020#ifdef FEAT_VARTABS
5021 tab_len = tabstop_padding(vcol_adjusted,
5022 wp->w_buffer->b_p_ts,
5023 wp->w_buffer->b_p_vts_array) - 1;
5024#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005025 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005026 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5027#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005028
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005029#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005030 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005031#endif
5032 /* tab amount depends on current column */
5033 n_extra = tab_len;
5034#ifdef FEAT_LINEBREAK
5035 else
5036 {
5037 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005038 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005039 int i;
5040 int saved_nextra = n_extra;
5041
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005042#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005043 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005044 /* there are characters to conceal */
5045 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005046 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5047 */
5048 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5049 && n_extra > tab_len)
5050 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005051#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005052
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005053 /* if n_extra > 0, it gives the number of chars, to
5054 * use for a tab, else we need to calculate the width
5055 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005056 len = (tab_len * mb_char2len(lcs_tab2));
5057 if (n_extra > 0)
5058 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005059 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005060 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005061 vim_memset(p, ' ', len);
5062 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005063 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005064 p_extra_free = p;
5065 for (i = 0; i < tab_len; i++)
5066 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005067 if (*p == NUL)
5068 {
5069 tab_len = i;
5070 break;
5071 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005072 mb_char2bytes(lcs_tab2, p);
5073 p += mb_char2len(lcs_tab2);
5074 n_extra += mb_char2len(lcs_tab2)
5075 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076 }
5077 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005078#ifdef FEAT_CONCEAL
5079 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5080 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005081 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005082 n_extra -= vcol_off;
5083#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005084 }
5085#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005086#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005087 {
5088 int vc_saved = vcol_off;
5089
5090 /* Tab alignment should be identical regardless of
5091 * 'conceallevel' value. So tab compensates of all
5092 * previous concealed characters, and thus resets
5093 * vcol_off and boguscols accumulated so far in the
5094 * line. Note that the tab can be longer than
5095 * 'tabstop' when there are concealed characters. */
5096 FIX_FOR_BOGUSCOLS;
5097
5098 /* Make sure, the highlighting for the tab char will be
5099 * correctly set further below (effectively reverts the
5100 * FIX_FOR_BOGSUCOLS macro */
5101 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005102 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005103 tab_len += vc_saved;
5104 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (wp->w_p_list)
5108 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005109 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005110#ifdef FEAT_LINEBREAK
5111 if (wp->w_p_lbr)
5112 c_extra = NUL; /* using p_extra from above */
5113 else
5114#endif
5115 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005116 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005117 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005118 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005121 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005124 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005125 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 else
5129 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005130 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 c_extra = ' ';
5132 c = ' ';
5133 }
5134 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005135 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005136 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005137 || ((fromcol >= 0 || fromcol_prev >= 0)
5138 && tocol > vcol
5139 && VIsual_mode != Ctrl_V
5140 && (
5141# ifdef FEAT_RIGHTLEFT
5142 wp->w_p_rl ? (col >= 0) :
5143# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005144 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005145 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005146 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005147 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005148 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005150 /* Display a '$' after the line or highlight an extra
5151 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5153 /* For a diff line the highlighting continues after the
5154 * "$". */
5155 if (
5156# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005157 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158# ifdef LINE_ATTR
5159 &&
5160# endif
5161# endif
5162# ifdef LINE_ATTR
5163 line_attr == 0
5164# endif
5165 )
5166#endif
5167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /* In virtualedit, visual selections may extend
5169 * beyond end of line. */
5170 if (area_highlighting && virtual_active()
5171 && tocol != MAXCOL && vcol < tocol)
5172 n_extra = 0;
5173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 p_extra = at_end_str;
5176 n_extra = 1;
5177 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005178 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005181 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005182 c = lcs_eol;
5183 else
5184 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 lcs_eol_one = -1;
5186 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005187 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005189 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 n_attr = 1;
5191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005193 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005196 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005197 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 else
5200 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 else if (c != NUL)
5203 {
5204 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005205 if (n_extra == 0)
5206 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#ifdef FEAT_RIGHTLEFT
5208 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5209 rl_mirror(p_extra); /* reverse "<12>" */
5210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005212 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005213#ifdef FEAT_LINEBREAK
5214 if (wp->w_p_lbr)
5215 {
5216 char_u *p;
5217
5218 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005219 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005220 vim_memset(p, ' ', n_extra);
5221 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5222 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005223 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005224 p_extra_free = p_extra = p;
5225 }
5226 else
5227#endif
5228 {
5229 n_extra = byte2cells(c) - 1;
5230 c = *p_extra++;
5231 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005232 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005235 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 saved_attr2 = char_attr; /* save current attr */
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 else if (VIsual_active
5241 && (VIsual_mode == Ctrl_V
5242 || VIsual_mode == 'v')
5243 && virtual_active()
5244 && tocol != MAXCOL
5245 && vcol < tocol
5246 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005247#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005249#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005250 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 c = ' ';
5253 --ptr; /* put it back at the NUL */
5254 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005255#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else if ((
5257# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005258 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005260# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005261 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 ) && (
5265# ifdef FEAT_RIGHTLEFT
5266 wp->w_p_rl ? (col >= 0) :
5267# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005268 (col
5269# ifdef FEAT_CONCEAL
5270 - boguscols
5271# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005272 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 /* Highlight until the right side of the window */
5275 c = ' ';
5276 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005277
5278 /* Remember we do the char for line highlighting. */
5279 ++did_line_attr;
5280
5281 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005282 if (line_attr != 0 && char_attr == search_attr
5283 && (did_line_attr > 1
5284 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005285 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286# ifdef FEAT_DIFF
5287 if (diff_hlf == HLF_TXD)
5288 {
5289 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005290 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005291 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005292 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005293 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5294 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005295 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005299# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005300 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005301 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005302 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005303 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5304 char_attr = hl_combine_attr(char_attr,
5305 HL_ATTR(HLF_CUL));
5306 }
5307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309#endif
5310 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005311
5312#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005313 if ( wp->w_p_cole > 0
5314 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005315 conceal_cursor_line(wp))
5316 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005317 && !(lnum_in_visual_area
5318 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005319 {
5320 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005321 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005322 && (syn_get_sub_char() != NUL || match_conc
5323 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005324 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005326 /* First time at this concealed item: display one
5327 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005328 if (match_conc)
5329 c = match_conc;
5330 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005331 c = syn_get_sub_char();
5332 else if (lcs_conceal != NUL)
5333 c = lcs_conceal;
5334 else
5335 c = ' ';
5336
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005337 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005338
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005339 if (n_extra > 0)
5340 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005341 vcol += n_extra;
5342 if (wp->w_p_wrap && n_extra > 0)
5343 {
5344# ifdef FEAT_RIGHTLEFT
5345 if (wp->w_p_rl)
5346 {
5347 col -= n_extra;
5348 boguscols -= n_extra;
5349 }
5350 else
5351# endif
5352 {
5353 boguscols += n_extra;
5354 col += n_extra;
5355 }
5356 }
5357 n_extra = 0;
5358 n_attr = 0;
5359 }
5360 else if (n_skip == 0)
5361 {
5362 is_concealing = TRUE;
5363 n_skip = 1;
5364 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005365 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005366 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005367 {
5368 mb_utf8 = TRUE;
5369 u8cc[0] = 0;
5370 c = 0xc0;
5371 }
5372 else
5373 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005374 }
5375 else
5376 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005377 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005378 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005379 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005380
5381 if (n_skip > 0 && did_decrement_ptr)
5382 // not showing the '>', put pointer back to avoid getting stuck
5383 ++ptr;
5384
5385#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005388#ifdef FEAT_CONCEAL
5389 /* In the cursor line and we may be concealing characters: correct
5390 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005391 if (!did_wcol && draw_state == WL_LINE
5392 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005393 && conceal_cursor_line(wp)
5394 && (int)wp->w_virtcol <= vcol + n_skip)
5395 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005396# ifdef FEAT_RIGHTLEFT
5397 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005398 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005399 else
5400# endif
5401 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005402 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005403 did_wcol = TRUE;
5404 }
5405#endif
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 /* Don't override visual selection highlighting. */
5408 if (n_attr > 0
5409 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005410 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 char_attr = extra_attr;
5412
Bram Moolenaar81695252004-12-29 20:58:21 +00005413#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 /* XIM don't send preedit_start and preedit_end, but they send
5415 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5416 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005417 if (p_imst == IM_ON_THE_SPOT
5418 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005419 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 && (State & INSERT)
5421 && !p_imdisable
5422 && im_is_preediting()
5423 && draw_state == WL_LINE)
5424 {
5425 colnr_T tcol;
5426
5427 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005428 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 else
5430 tcol = preedit_end_col;
5431 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5432 {
5433 if (feedback_old_attr < 0)
5434 {
5435 feedback_col = 0;
5436 feedback_old_attr = char_attr;
5437 }
5438 char_attr = im_get_feedback_attr(feedback_col);
5439 if (char_attr < 0)
5440 char_attr = feedback_old_attr;
5441 feedback_col++;
5442 }
5443 else if (feedback_old_attr >= 0)
5444 {
5445 char_attr = feedback_old_attr;
5446 feedback_old_attr = -1;
5447 feedback_col = 0;
5448 }
5449 }
5450#endif
5451 /*
5452 * Handle the case where we are in column 0 but not on the first
5453 * character of the line and the user wants us to show us a
5454 * special character (via 'listchars' option "precedes:<char>".
5455 */
5456 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005457 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5459#ifdef FEAT_DIFF
5460 && filler_todo <= 0
5461#endif
5462 && draw_state > WL_NR
5463 && c != NUL)
5464 {
5465 c = lcs_prec;
5466 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005467 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5468 {
5469 /* Double-width character being overwritten by the "precedes"
5470 * character, need to fill up half the character. */
5471 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005472 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005473 n_extra = 1;
5474 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005475 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005478 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
5480 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005481 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005482 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else
5485 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005486 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 {
5488 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005489 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 n_attr3 = 1;
5491 }
5492 }
5493
5494 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005495 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005497 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005498#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499 || did_line_attr == 1
5500#endif
5501 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005503#ifdef FEAT_SEARCH_EXTRA
5504 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005505
5506 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005507 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005508 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005509#endif
5510
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005511 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 * highlight match at end of line. If it's beyond the last
5513 * char on the screen, just overwrite that one (tricky!) Not
5514 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005515#ifdef FEAT_SEARCH_EXTRA
5516 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005517 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005518 prevcol_hl_flag = TRUE;
5519 else
5520 {
5521 cur = wp->w_match_head;
5522 while (cur != NULL)
5523 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005524 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005525 {
5526 prevcol_hl_flag = TRUE;
5527 break;
5528 }
5529 cur = cur->next;
5530 }
5531 }
5532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005534 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005535 && (VIsual_mode != Ctrl_V
5536 || lnum == VIsual.lnum
5537 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005538 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539#ifdef FEAT_SEARCH_EXTRA
5540 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005541 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005542# ifdef FEAT_SYN_HL
5543 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5544 && !(wp == curwin && VIsual_active))
5545# endif
5546# ifdef FEAT_DIFF
5547 && diff_hlf == (hlf_T)0
5548# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005549# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005550 && did_line_attr <= 1
5551# endif
5552 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553#endif
5554 ))
5555 {
5556 int n = 0;
5557
5558#ifdef FEAT_RIGHTLEFT
5559 if (wp->w_p_rl)
5560 {
5561 if (col < 0)
5562 n = 1;
5563 }
5564 else
5565#endif
5566 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005567 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 n = -1;
5569 }
5570 if (n != 0)
5571 {
5572 /* At the window boundary, highlight the last character
5573 * instead (better than nothing). */
5574 off += n;
5575 col += n;
5576 }
5577 else
5578 {
5579 /* Add a blank character to highlight. */
5580 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (enc_utf8)
5582 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584#ifdef FEAT_SEARCH_EXTRA
5585 if (area_attr == 0)
5586 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005587 /* Use attributes from match with highest priority among
5588 * 'search_hl' and the match list. */
5589 char_attr = search_hl.attr;
5590 cur = wp->w_match_head;
5591 shl_flag = FALSE;
5592 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005593 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005594 if (shl_flag == FALSE
5595 && ((cur != NULL
5596 && cur->priority > SEARCH_HL_PRIORITY)
5597 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005598 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005599 shl = &search_hl;
5600 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005601 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 else
5603 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005604 if ((ptr - line) - 1 == (long)shl->startcol
5605 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005606 char_attr = shl->attr;
5607 if (shl != &search_hl && cur != NULL)
5608 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611#endif
5612 ScreenAttrs[off] = char_attr;
5613#ifdef FEAT_RIGHTLEFT
5614 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005617 --off;
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 else
5620#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005623 ++off;
5624 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005625 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005626#ifdef FEAT_SYN_HL
5627 eol_hl_off = 1;
5628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar91170f82006-05-05 21:15:17 +00005632 /*
5633 * At end of the text line.
5634 */
5635 if (c == NUL)
5636 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005637#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005638 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005639 if (wp->w_p_wrap)
5640 v = wp->w_skipcol;
5641 else
5642 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005644 /* check if line ends before left margin */
5645 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005646 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005647#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005648 // Get rid of the boguscols now, we want to draw until the right
5649 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005650 col -= boguscols;
5651 boguscols = 0;
5652#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005653
5654 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005655 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656
5657 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005658 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5659 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005660 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005661 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005662 || draw_color_col
5663 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005664# ifdef FEAT_RIGHTLEFT
5665 && !wp->w_p_rl
5666# endif
5667 )
5668 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005669 int rightmost_vcol = 0;
5670 int i;
5671
5672 if (wp->w_p_cuc)
5673 rightmost_vcol = wp->w_virtcol;
5674 if (draw_color_col)
5675 /* determine rightmost colorcolumn to possibly draw */
5676 for (i = 0; color_cols[i] >= 0; ++i)
5677 if (rightmost_vcol < color_cols[i])
5678 rightmost_vcol = color_cols[i];
5679
Bram Moolenaar02631462017-09-22 15:20:32 +02005680 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005681 {
5682 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005683 if (enc_utf8)
5684 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005685 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005686 if (draw_color_col)
5687 draw_color_col = advance_color_col(VCOL_HLC,
5688 &color_cols);
5689
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005690 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005691 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005692 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005693 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005694 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005695 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005696
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005697 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005698 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005699
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005700 ++vcol;
5701 }
5702 }
5703#endif
5704
Bram Moolenaar53f81742017-09-22 14:35:51 +02005705 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005706 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 row++;
5708
5709 /*
5710 * Update w_cline_height and w_cline_folded if the cursor line was
5711 * updated (saves a call to plines() later).
5712 */
5713 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5714 {
5715 curwin->w_cline_row = startrow;
5716 curwin->w_cline_height = row - startrow;
5717#ifdef FEAT_FOLDING
5718 curwin->w_cline_folded = FALSE;
5719#endif
5720 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5721 }
5722
5723 break;
5724 }
5725
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005726 // Show "extends" character from 'listchars' if beyond the line end and
5727 // 'list' is set.
5728 if (lcs_ext != NUL
5729 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 && !wp->w_p_wrap
5731#ifdef FEAT_DIFF
5732 && filler_todo <= 0
5733#endif
5734 && (
5735#ifdef FEAT_RIGHTLEFT
5736 wp->w_p_rl ? col == 0 :
5737#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005738 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005740 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5742 {
5743 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005744 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005746 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005749 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005750 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
5752 else
5753 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005756#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005757 /* advance to the next 'colorcolumn' */
5758 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005759 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005761 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762 * highlight the cursor position itself.
5763 * Also highlight the 'colorcolumn' if it is different than
5764 * 'cursorcolumn' */
5765 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005766 if (draw_state == WL_LINE && !lnum_in_visual_area
5767 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005768 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005769 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005770 && lnum != wp->w_cursor.lnum)
5771 {
5772 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005773 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005774 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005775 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005776 {
5777 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005778 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005779 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005780 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005781#endif
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /*
5784 * Store character to be displayed.
5785 * Skip characters that are left of the screen for 'nowrap'.
5786 */
5787 vcol_prev = vcol;
5788 if (draw_state < WL_LINE || n_skip <= 0)
5789 {
5790 /*
5791 * Store the character.
5792 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005793#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5795 {
5796 /* A double-wide character is: put first halve in left cell. */
5797 --off;
5798 --col;
5799 }
5800#endif
5801 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005803 {
5804 if ((mb_c & 0xff00) == 0x8e00)
5805 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 else if (enc_utf8)
5809 {
5810 if (mb_utf8)
5811 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005812 int i;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005815 if ((c & 0xff) == 0)
5816 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005817 for (i = 0; i < Screen_mco; ++i)
5818 {
5819 ScreenLinesC[i][off] = u8cc[i];
5820 if (u8cc[i] == 0)
5821 break;
5822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
5824 else
5825 ScreenLinesUC[off] = 0;
5826 }
5827 if (multi_attr)
5828 {
5829 ScreenAttrs[off] = multi_attr;
5830 multi_attr = 0;
5831 }
5832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 ScreenAttrs[off] = char_attr;
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5836 {
5837 /* Need to fill two screen columns. */
5838 ++off;
5839 ++col;
5840 if (enc_utf8)
5841 /* UTF-8: Put a 0 in the second screen char. */
5842 ScreenLines[off] = 0;
5843 else
5844 /* DBCS: Put second byte in the second screen char. */
5845 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005846 if (draw_state > WL_NR
5847#ifdef FEAT_DIFF
5848 && filler_todo <= 0
5849#endif
5850 )
5851 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 /* When "tocol" is halfway a character, set it to the end of
5853 * the character, otherwise highlighting won't stop. */
5854 if (tocol == vcol)
5855 ++tocol;
5856#ifdef FEAT_RIGHTLEFT
5857 if (wp->w_p_rl)
5858 {
5859 /* now it's time to backup one cell */
5860 --off;
5861 --col;
5862 }
5863#endif
5864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#ifdef FEAT_RIGHTLEFT
5866 if (wp->w_p_rl)
5867 {
5868 --off;
5869 --col;
5870 }
5871 else
5872#endif
5873 {
5874 ++off;
5875 ++col;
5876 }
5877 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005878#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005879 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005880 {
5881 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005882 ++vcol_off;
5883 if (n_extra > 0)
5884 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005885 if (wp->w_p_wrap)
5886 {
5887 /*
5888 * Special voodoo required if 'wrap' is on.
5889 *
5890 * Advance the column indicator to force the line
5891 * drawing to wrap early. This will make the line
5892 * take up the same screen space when parts are concealed,
5893 * so that cursor line computations aren't messed up.
5894 *
5895 * To avoid the fictitious advance of 'col' causing
5896 * trailing junk to be written out of the screen line
5897 * we are building, 'boguscols' keeps track of the number
5898 * of bad columns we have advanced.
5899 */
5900 if (n_extra > 0)
5901 {
5902 vcol += n_extra;
5903# ifdef FEAT_RIGHTLEFT
5904 if (wp->w_p_rl)
5905 {
5906 col -= n_extra;
5907 boguscols -= n_extra;
5908 }
5909 else
5910# endif
5911 {
5912 col += n_extra;
5913 boguscols += n_extra;
5914 }
5915 n_extra = 0;
5916 n_attr = 0;
5917 }
5918
5919
Bram Moolenaar860cae12010-06-05 23:22:07 +02005920 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5921 {
5922 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005923# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005924 if (wp->w_p_rl)
5925 {
5926 --boguscols;
5927 --col;
5928 }
5929 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005930# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005931 {
5932 ++boguscols;
5933 ++col;
5934 }
5935 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005936
5937# ifdef FEAT_RIGHTLEFT
5938 if (wp->w_p_rl)
5939 {
5940 --boguscols;
5941 --col;
5942 }
5943 else
5944# endif
5945 {
5946 ++boguscols;
5947 ++col;
5948 }
5949 }
5950 else
5951 {
5952 if (n_extra > 0)
5953 {
5954 vcol += n_extra;
5955 n_extra = 0;
5956 n_attr = 0;
5957 }
5958 }
5959
5960 }
5961#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 else
5963 --n_skip;
5964
Bram Moolenaar64486672010-05-16 15:46:46 +02005965 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5966 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005967 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968#ifdef FEAT_DIFF
5969 && filler_todo <= 0
5970#endif
5971 )
5972 ++vcol;
5973
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005974#ifdef FEAT_SYN_HL
5975 if (vcol_save_attr >= 0)
5976 char_attr = vcol_save_attr;
5977#endif
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 /* restore attributes after "predeces" in 'listchars' */
5980 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5981 char_attr = saved_attr3;
5982
5983 /* restore attributes after last 'listchars' or 'number' char */
5984 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5985 char_attr = saved_attr2;
5986
5987 /*
5988 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005989 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 if ((
5992#ifdef FEAT_RIGHTLEFT
5993 wp->w_p_rl ? (col < 0) :
5994#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005995 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 && (*ptr != NUL
5997#ifdef FEAT_DIFF
5998 || filler_todo > 0
5999#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006000 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6002 )
6003 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006004#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006005 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006006 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006007 boguscols = 0;
6008#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006009 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006010 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 ++row;
6013 ++screen_row;
6014
6015 /* When not wrapping and finished diff lines, or when displayed
6016 * '$' and highlighting until last column, break here. */
6017 if ((!wp->w_p_wrap
6018#ifdef FEAT_DIFF
6019 && filler_todo <= 0
6020#endif
6021 ) || lcs_eol_one == -1)
6022 break;
6023
6024 /* When the window is too narrow draw all "@" lines. */
6025 if (draw_state != WL_LINE
6026#ifdef FEAT_DIFF
6027 && filler_todo <= 0
6028#endif
6029 )
6030 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006031 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 row = endrow;
6034 }
6035
6036 /* When line got too long for screen break here. */
6037 if (row == endrow)
6038 {
6039 ++row;
6040 break;
6041 }
6042
6043 if (screen_cur_row == screen_row - 1
6044#ifdef FEAT_DIFF
6045 && filler_todo <= 0
6046#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006047 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
6049 /* Remember that the line wraps, used for modeless copy. */
6050 LineWraps[screen_row - 1] = TRUE;
6051
6052 /*
6053 * Special trick to make copy/paste of wrapped lines work with
6054 * xterm/screen: write an extra character beyond the end of
6055 * the line. This will work with all terminal types
6056 * (regardless of the xn,am settings).
6057 * Only do this on a fast tty.
6058 * Only do this if the cursor is on the current line
6059 * (something has been written in it).
6060 * Don't do this for the GUI.
6061 * Don't do this for double-width characters.
6062 * Don't do this for a window not at the right screen border.
6063 */
6064 if (p_tf
6065#ifdef FEAT_GUI
6066 && !gui.in_use
6067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006069 && ((*mb_off2cells)(LineOffset[screen_row],
6070 LineOffset[screen_row] + screen_Columns)
6071 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006073 + (int)Columns - 2,
6074 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006075 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 {
6077 /* First make sure we are at the end of the screen line,
6078 * then output the same character again to let the
6079 * terminal know about the wrap. If the terminal doesn't
6080 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006081 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 screen_char(LineOffset[screen_row - 1]
6083 + (unsigned)Columns - 1,
6084 screen_row - 1, (int)(Columns - 1));
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 /* When there is a multi-byte character, just output a
6087 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006088 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6089 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 out_char(' ');
6091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 out_char(ScreenLines[LineOffset[screen_row - 1]
6093 + (Columns - 1)]);
6094 /* force a redraw of the first char on the next line */
6095 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6096 screen_start(); /* don't know where cursor is now */
6097 }
6098 }
6099
6100 col = 0;
6101 off = (unsigned)(current_ScreenLine - ScreenLines);
6102#ifdef FEAT_RIGHTLEFT
6103 if (wp->w_p_rl)
6104 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006105 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 off += col;
6107 }
6108#endif
6109
6110 /* reset the drawing state for the start of a wrapped line */
6111 draw_state = WL_START;
6112 saved_n_extra = n_extra;
6113 saved_p_extra = p_extra;
6114 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006115 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 saved_char_attr = char_attr;
6117 n_extra = 0;
6118 lcs_prec_todo = lcs_prec;
6119#ifdef FEAT_LINEBREAK
6120# ifdef FEAT_DIFF
6121 if (filler_todo <= 0)
6122# endif
6123 need_showbreak = TRUE;
6124#endif
6125#ifdef FEAT_DIFF
6126 --filler_todo;
6127 /* When the filler lines are actually below the last line of the
6128 * file, don't draw the line itself, break here. */
6129 if (filler_todo == 0 && wp->w_botfill)
6130 break;
6131#endif
6132 }
6133
6134 } /* for every character in the line */
6135
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006136#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006137 /* After an empty line check first word for capital. */
6138 if (*skipwhite(line) == NUL)
6139 {
6140 capcol_lnum = lnum + 1;
6141 cap_col = 0;
6142 }
6143#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006144#ifdef FEAT_TEXT_PROP
6145 vim_free(text_props);
6146 vim_free(text_prop_idxs);
6147#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006148
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006149 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 return row;
6151}
6152
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006153/*
6154 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006155 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006156 */
6157 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006158comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006159{
6160 int i;
6161
6162 for (i = 0; i < Screen_mco; ++i)
6163 {
6164 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6165 return TRUE;
6166 if (ScreenLinesC[i][off_from] == 0)
6167 break;
6168 }
6169 return FALSE;
6170}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172/*
6173 * Check whether the given character needs redrawing:
6174 * - the (first byte of the) character is different
6175 * - the attributes are different
6176 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006177 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 */
6179 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006180char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 if (cols > 0
6183 && ((ScreenLines[off_from] != ScreenLines[off_to]
6184 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 || (enc_dbcs != 0
6186 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6187 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6188 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6189 : (cols > 1 && ScreenLines[off_from + 1]
6190 != ScreenLines[off_to + 1])))
6191 || (enc_utf8
6192 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6193 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006194 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006195 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6196 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006197 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 return TRUE;
6199 return FALSE;
6200}
6201
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006202#if defined(FEAT_TERMINAL) || defined(PROTO)
6203/*
6204 * Return the index in ScreenLines[] for the current screen line.
6205 */
6206 int
6207screen_get_current_line_off()
6208{
6209 return (int)(current_ScreenLine - ScreenLines);
6210}
6211#endif
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213/*
6214 * Move one "cooked" screen line to the screen, but only the characters that
6215 * have actually changed. Handle insert/delete character.
6216 * "coloff" gives the first column on the screen for this line.
6217 * "endcol" gives the columns where valid characters are.
6218 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6219 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006220 * "flags" can have bits:
6221 * SLF_POPUP popup window
6222 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6224 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6225 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006227screen_line(
6228 int row,
6229 int coloff,
6230 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006231 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006232 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 unsigned off_from;
6235 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006236 unsigned max_off_from;
6237 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 int force = FALSE; /* force update rest of the line */
6241 int redraw_this /* bool: does character need redraw? */
6242#ifdef FEAT_GUI
6243 = TRUE /* For GUI when while-loop empty */
6244#endif
6245 ;
6246 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 int clear_next = FALSE;
6248 int char_cells; /* 1: normal char */
6249 /* 2: occupies two display cells */
6250# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006252 /* Check for illegal row and col, just in case. */
6253 if (row >= Rows)
6254 row = Rows - 1;
6255 if (endcol > Columns)
6256 endcol = Columns;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258# ifdef FEAT_CLIPBOARD
6259 clip_may_clear_selection(row, row);
6260# endif
6261
6262 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6263 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006264 max_off_from = off_from + screen_Columns;
6265 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006268 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
6270 /* Clear rest first, because it's left of the text. */
6271 if (clear_width > 0)
6272 {
6273 while (col <= endcol && ScreenLines[off_to] == ' '
6274 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006275 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
6277 ++off_to;
6278 ++col;
6279 }
6280 if (col <= endcol)
6281 screen_fill(row, row + 1, col + coloff,
6282 endcol + coloff + 1, ' ', ' ', 0);
6283 }
6284 col = endcol + 1;
6285 off_to = LineOffset[row] + col + coloff;
6286 off_from += col;
6287 endcol = (clear_width > 0 ? clear_width : -clear_width);
6288 }
6289#endif /* FEAT_RIGHTLEFT */
6290
6291 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6292
6293 while (col < endcol)
6294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006296 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 else
6298 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299
6300 redraw_this = redraw_next;
6301 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6302 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6303
6304#ifdef FEAT_GUI
6305 /* If the next character was bold, then redraw the current character to
6306 * remove any pixels that might have spilt over into us. This only
6307 * happens in the GUI.
6308 */
6309 if (redraw_next && gui.in_use)
6310 {
6311 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006312 if (hl > HL_ALL)
6313 hl = syn_attr2attr(hl);
6314 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 redraw_this = TRUE;
6316 }
6317#endif
6318
6319 if (redraw_this)
6320 {
6321 /*
6322 * Special handling when 'xs' termcap flag set (hpterm):
6323 * Attributes for characters are stored at the position where the
6324 * cursor is when writing the highlighting code. The
6325 * start-highlighting code must be written with the cursor on the
6326 * first highlighted character. The stop-highlighting code must
6327 * be written with the cursor just after the last highlighted
6328 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006329 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 * to clear the rest of the line, and force redrawing it
6331 * completely.
6332 */
6333 if ( p_wiv
6334 && !force
6335#ifdef FEAT_GUI
6336 && !gui.in_use
6337#endif
6338 && ScreenAttrs[off_to] != 0
6339 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6340 {
6341 /*
6342 * Need to remove highlighting attributes here.
6343 */
6344 windgoto(row, col + coloff);
6345 out_str(T_CE); /* clear rest of this screen line */
6346 screen_start(); /* don't know where cursor is now */
6347 force = TRUE; /* force redraw of rest of the line */
6348 redraw_next = TRUE; /* or else next char would miss out */
6349
6350 /*
6351 * If the previous character was highlighted, need to stop
6352 * highlighting at this character.
6353 */
6354 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6355 {
6356 screen_attr = ScreenAttrs[off_to - 1];
6357 term_windgoto(row, col + coloff);
6358 screen_stop_highlight();
6359 }
6360 else
6361 screen_attr = 0; /* highlighting has stopped */
6362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (enc_dbcs != 0)
6364 {
6365 /* Check if overwriting a double-byte with a single-byte or
6366 * the other way around requires another character to be
6367 * redrawn. For UTF-8 this isn't needed, because comparing
6368 * ScreenLinesUC[] is sufficient. */
6369 if (char_cells == 1
6370 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006371 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
6373 /* Writing a single-cell character over a double-cell
6374 * character: need to redraw the next cell. */
6375 ScreenLines[off_to + 1] = 0;
6376 redraw_next = TRUE;
6377 }
6378 else if (char_cells == 2
6379 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006380 && (*mb_off2cells)(off_to, max_off_to) == 1
6381 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
6383 /* Writing the second half of a double-cell character over
6384 * a double-cell character: need to redraw the second
6385 * cell. */
6386 ScreenLines[off_to + 2] = 0;
6387 redraw_next = TRUE;
6388 }
6389
6390 if (enc_dbcs == DBCS_JPNU)
6391 ScreenLines2[off_to] = ScreenLines2[off_from];
6392 }
6393 /* When writing a single-width character over a double-width
6394 * character and at the end of the redrawn text, need to clear out
6395 * the right halve of the old character.
6396 * Also required when writing the right halve of a double-width
6397 * char over the left halve of an existing one. */
6398 if (has_mbyte && col + char_cells == endcol
6399 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006402 && (*mb_off2cells)(off_to, max_off_to) == 1
6403 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (enc_utf8)
6408 {
6409 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6410 if (ScreenLinesUC[off_from] != 0)
6411 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006412 int i;
6413
6414 for (i = 0; i < Screen_mco; ++i)
6415 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417 }
6418 if (char_cells == 2)
6419 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006422 /* The bold trick makes a single column of pixels appear in the
6423 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 * character should be redrawn too. This happens for our own GUI
6425 * and for some xterms. */
6426 if (
6427# ifdef FEAT_GUI
6428 gui.in_use
6429# endif
6430# if defined(FEAT_GUI) && defined(UNIX)
6431 ||
6432# endif
6433# ifdef UNIX
6434 term_is_xterm
6435# endif
6436 )
6437 {
6438 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006439 if (hl > HL_ALL)
6440 hl = syn_attr2attr(hl);
6441 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 redraw_next = TRUE;
6443 }
6444#endif
6445 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006446
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006447 /* For simplicity set the attributes of second half of a
6448 * double-wide character equal to the first half. */
6449 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006451
6452 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 screen_char(off_to, row, col + coloff);
6456 }
6457 else if ( p_wiv
6458#ifdef FEAT_GUI
6459 && !gui.in_use
6460#endif
6461 && col + coloff > 0)
6462 {
6463 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6464 {
6465 /*
6466 * Don't output stop-highlight when moving the cursor, it will
6467 * stop the highlighting when it should continue.
6468 */
6469 screen_attr = 0;
6470 }
6471 else if (screen_attr != 0)
6472 screen_stop_highlight();
6473 }
6474
6475 off_to += CHAR_CELLS;
6476 off_from += CHAR_CELLS;
6477 col += CHAR_CELLS;
6478 }
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (clear_next)
6481 {
6482 /* Clear the second half of a double-wide character of which the left
6483 * half was overwritten with a single-wide character. */
6484 ScreenLines[off_to] = ' ';
6485 if (enc_utf8)
6486 ScreenLinesUC[off_to] = 0;
6487 screen_char(off_to, row, col + coloff);
6488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 if (clear_width > 0
6491#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006492 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493#endif
6494 )
6495 {
6496#ifdef FEAT_GUI
6497 int startCol = col;
6498#endif
6499
6500 /* blank out the rest of the line */
6501 while (col < clear_width && ScreenLines[off_to] == ' '
6502 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006503 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
6505 ++off_to;
6506 ++col;
6507 }
6508 if (col < clear_width)
6509 {
6510#ifdef FEAT_GUI
6511 /*
6512 * In the GUI, clearing the rest of the line may leave pixels
6513 * behind if the first character cleared was bold. Some bold
6514 * fonts spill over the left. In this case we redraw the previous
6515 * character too. If we didn't skip any blanks above, then we
6516 * only redraw if the character wasn't already redrawn anyway.
6517 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006518 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
6520 hl = ScreenAttrs[off_to];
6521 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006522 {
6523 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006524
Bram Moolenaar9c697322006-10-09 20:11:17 +00006525 if (enc_utf8)
6526 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6527 * that its width is 2. */
6528 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6529 else if (enc_dbcs != 0)
6530 {
6531 /* find previous character by counting from first
6532 * column and get its width. */
6533 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006534 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006535
6536 while (off < off_to)
6537 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006538 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006539 off += prev_cells;
6540 }
6541 }
6542
6543 if (enc_dbcs != 0 && prev_cells > 1)
6544 screen_char_2(off_to - prev_cells, row,
6545 col + coloff - prev_cells);
6546 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006547 screen_char(off_to - prev_cells, row,
6548 col + coloff - prev_cells);
6549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551#endif
6552 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6553 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 off_to += clear_width - col;
6555 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557 }
6558
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006559 if (clear_width > 0
6560#ifdef FEAT_TEXT_PROP
6561 && !(flags & SLF_POPUP) // no separator for popup window
6562#endif
6563 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006565 // For a window that has a right neighbor, draw the separator char
6566 // right of the window contents.
6567 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
6569 int c;
6570
6571 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006572 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006573 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6574 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 || ScreenAttrs[off_to] != hl)
6576 {
6577 ScreenLines[off_to] = c;
6578 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (enc_utf8)
6580 {
6581 if (c >= 0x80)
6582 {
6583 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006584 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 }
6586 else
6587 ScreenLinesUC[off_to] = 0;
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 screen_char(off_to, row, col + coloff);
6590 }
6591 }
6592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 LineWraps[row] = FALSE;
6594 }
6595}
6596
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006597#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006599 * Mirror text "str" for right-left displaying.
6600 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006602 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006603rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
6605 char_u *p1, *p2;
6606 int t;
6607
6608 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6609 {
6610 t = *p1;
6611 *p1 = *p2;
6612 *p2 = t;
6613 }
6614}
6615#endif
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617/*
6618 * mark all status lines for redraw; used after first :cd
6619 */
6620 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006621status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 win_T *wp;
6624
Bram Moolenaar29323592016-07-24 22:04:11 +02006625 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 if (wp->w_status_height)
6627 {
6628 wp->w_redr_status = TRUE;
6629 redraw_later(VALID);
6630 }
6631}
6632
6633/*
6634 * mark all status lines of the current buffer for redraw
6635 */
6636 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006637status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
6639 win_T *wp;
6640
Bram Moolenaar29323592016-07-24 22:04:11 +02006641 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6643 {
6644 wp->w_redr_status = TRUE;
6645 redraw_later(VALID);
6646 }
6647}
6648
6649/*
6650 * Redraw all status lines that need to be redrawn.
6651 */
6652 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006653redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655 win_T *wp;
6656
Bram Moolenaar29323592016-07-24 22:04:11 +02006657 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006659 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006660 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006661 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663
Bram Moolenaar4033c552017-09-16 20:54:51 +02006664#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665/*
6666 * Redraw all status lines at the bottom of frame "frp".
6667 */
6668 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006669win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 if (frp->fr_layout == FR_LEAF)
6672 frp->fr_win->w_redr_status = TRUE;
6673 else if (frp->fr_layout == FR_ROW)
6674 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006675 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 win_redraw_last_status(frp);
6677 }
6678 else /* frp->fr_layout == FR_COL */
6679 {
6680 frp = frp->fr_child;
6681 while (frp->fr_next != NULL)
6682 frp = frp->fr_next;
6683 win_redraw_last_status(frp);
6684 }
6685}
6686#endif
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688/*
6689 * Draw the verticap separator right of window "wp" starting with line "row".
6690 */
6691 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006692draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 int hl;
6695 int c;
6696
6697 if (wp->w_vsep_width)
6698 {
6699 /* draw the vertical separator right of this window */
6700 c = fillchar_vsep(&hl);
6701 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6702 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6703 c, ' ', hl);
6704 }
6705}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706
6707#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006708static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006711 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 */
6713 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006714status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 int len = 0;
6717
6718#ifdef FEAT_MENU
6719 int emenu = (xp->xp_context == EXPAND_MENUS
6720 || xp->xp_context == EXPAND_MENUNAMES);
6721
6722 /* Check for menu separators - replace with '|'. */
6723 if (emenu && menu_is_separator(s))
6724 return 1;
6725#endif
6726
6727 while (*s != NUL)
6728 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006729 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006730 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006731 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 }
6733
6734 return len;
6735}
6736
6737/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006738 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006739 * These are backslashes used for escaping. Do show backslashes in help tags.
6740 */
6741 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006742skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006743{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006744 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006745#ifdef FEAT_MENU
6746 || ((xp->xp_context == EXPAND_MENUS
6747 || xp->xp_context == EXPAND_MENUNAMES)
6748 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6749#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006750 )
6751 {
6752#ifndef BACKSLASH_IN_FILENAME
6753 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6754 return 2;
6755#endif
6756 return 1;
6757 }
6758 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006759}
6760
6761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 * Show wildchar matches in the status line.
6763 * Show at least the "match" item.
6764 * We start at item 'first_match' in the list and show all matches that fit.
6765 *
6766 * If inversion is possible we use it. Else '=' characters are used.
6767 */
6768 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006769win_redr_status_matches(
6770 expand_T *xp,
6771 int num_matches,
6772 char_u **matches, /* list of matches */
6773 int match,
6774 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6777 int row;
6778 char_u *buf;
6779 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006780 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 int fillchar;
6782 int attr;
6783 int i;
6784 int highlight = TRUE;
6785 char_u *selstart = NULL;
6786 int selstart_col = 0;
6787 char_u *selend = NULL;
6788 static int first_match = 0;
6789 int add_left = FALSE;
6790 char_u *s;
6791#ifdef FEAT_MENU
6792 int emenu;
6793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 if (matches == NULL) /* interrupted completion? */
6797 return;
6798
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006799 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006800 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006801 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006802 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (buf == NULL)
6804 return;
6805
6806 if (match == -1) /* don't show match but original text */
6807 {
6808 match = 0;
6809 highlight = FALSE;
6810 }
6811 /* count 1 for the ending ">" */
6812 clen = status_match_len(xp, L_MATCH(match)) + 3;
6813 if (match == 0)
6814 first_match = 0;
6815 else if (match < first_match)
6816 {
6817 /* jumping left, as far as we can go */
6818 first_match = match;
6819 add_left = TRUE;
6820 }
6821 else
6822 {
6823 /* check if match fits on the screen */
6824 for (i = first_match; i < match; ++i)
6825 clen += status_match_len(xp, L_MATCH(i)) + 2;
6826 if (first_match > 0)
6827 clen += 2;
6828 /* jumping right, put match at the left */
6829 if ((long)clen > Columns)
6830 {
6831 first_match = match;
6832 /* if showing the last match, we can add some on the left */
6833 clen = 2;
6834 for (i = match; i < num_matches; ++i)
6835 {
6836 clen += status_match_len(xp, L_MATCH(i)) + 2;
6837 if ((long)clen >= Columns)
6838 break;
6839 }
6840 if (i == num_matches)
6841 add_left = TRUE;
6842 }
6843 }
6844 if (add_left)
6845 while (first_match > 0)
6846 {
6847 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6848 if ((long)clen >= Columns)
6849 break;
6850 --first_match;
6851 }
6852
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006853 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 if (first_match == 0)
6856 {
6857 *buf = NUL;
6858 len = 0;
6859 }
6860 else
6861 {
6862 STRCPY(buf, "< ");
6863 len = 2;
6864 }
6865 clen = len;
6866
6867 i = first_match;
6868 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6869 {
6870 if (i == match)
6871 {
6872 selstart = buf + len;
6873 selstart_col = clen;
6874 }
6875
6876 s = L_MATCH(i);
6877 /* Check for menu separators - replace with '|' */
6878#ifdef FEAT_MENU
6879 emenu = (xp->xp_context == EXPAND_MENUS
6880 || xp->xp_context == EXPAND_MENUNAMES);
6881 if (emenu && menu_is_separator(s))
6882 {
6883 STRCPY(buf + len, transchar('|'));
6884 l = (int)STRLEN(buf + len);
6885 len += l;
6886 clen += l;
6887 }
6888 else
6889#endif
6890 for ( ; *s != NUL; ++s)
6891 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006892 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006894 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 {
6896 STRNCPY(buf + len, s, l);
6897 s += l - 1;
6898 len += l;
6899 }
6900 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 {
6902 STRCPY(buf + len, transchar_byte(*s));
6903 len += (int)STRLEN(buf + len);
6904 }
6905 }
6906 if (i == match)
6907 selend = buf + len;
6908
6909 *(buf + len++) = ' ';
6910 *(buf + len++) = ' ';
6911 clen += 2;
6912 if (++i == num_matches)
6913 break;
6914 }
6915
6916 if (i != num_matches)
6917 {
6918 *(buf + len++) = '>';
6919 ++clen;
6920 }
6921
6922 buf[len] = NUL;
6923
6924 row = cmdline_row - 1;
6925 if (row >= 0)
6926 {
6927 if (wild_menu_showing == 0)
6928 {
6929 if (msg_scrolled > 0)
6930 {
6931 /* Put the wildmenu just above the command line. If there is
6932 * no room, scroll the screen one line up. */
6933 if (cmdline_row == Rows - 1)
6934 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006935 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 ++msg_scrolled;
6937 }
6938 else
6939 {
6940 ++cmdline_row;
6941 ++row;
6942 }
6943 wild_menu_showing = WM_SCROLLED;
6944 }
6945 else
6946 {
6947 /* Create status line if needed by setting 'laststatus' to 2.
6948 * Set 'winminheight' to zero to avoid that the window is
6949 * resized. */
6950 if (lastwin->w_status_height == 0)
6951 {
6952 save_p_ls = p_ls;
6953 save_p_wmh = p_wmh;
6954 p_ls = 2;
6955 p_wmh = 0;
6956 last_status(FALSE);
6957 }
6958 wild_menu_showing = WM_SHOWN;
6959 }
6960 }
6961
6962 screen_puts(buf, row, 0, attr);
6963 if (selstart != NULL && highlight)
6964 {
6965 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006966 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968
6969 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6970 }
6971
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 vim_free(buf);
6974}
6975#endif
6976
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977/*
6978 * Redraw the status line of window wp.
6979 *
6980 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006981 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6982 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006984 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006985win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 int row;
6988 char_u *p;
6989 int len;
6990 int fillchar;
6991 int attr;
6992 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006993 static int busy = FALSE;
6994
6995 /* It's possible to get here recursively when 'statusline' (indirectly)
6996 * invokes ":redrawstatus". Simply ignore the call then. */
6997 if (busy)
6998 return;
6999 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 wp->w_redr_status = FALSE;
7002 if (wp->w_status_height == 0)
7003 {
7004 /* no status line, can only be last window */
7005 redraw_cmdline = TRUE;
7006 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007007 else if (!redrawing()
7008#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007009 // don't update status line when popup menu is visible and may be
7010 // drawn over it, unless it will be redrawn later
7011 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007012#endif
7013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
7015 /* Don't redraw right now, do it later. */
7016 wp->w_redr_status = TRUE;
7017 }
7018#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007019 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 {
7021 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007022 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024#endif
7025 else
7026 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007027 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007029 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 p = NameBuff;
7031 len = (int)STRLEN(p);
7032
Bram Moolenaard85f2712017-07-28 21:51:57 +02007033 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034#ifdef FEAT_QUICKFIX
7035 || wp->w_p_pvw
7036#endif
7037 || bufIsChanged(wp->w_buffer)
7038 || wp->w_buffer->b_p_ro)
7039 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007040 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007042 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 len += (int)STRLEN(p + len);
7044 }
7045#ifdef FEAT_QUICKFIX
7046 if (wp->w_p_pvw)
7047 {
7048 STRCPY(p + len, _("[Preview]"));
7049 len += (int)STRLEN(p + len);
7050 }
7051#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007052 if (bufIsChanged(wp->w_buffer)
7053#ifdef FEAT_TERMINAL
7054 && !bt_terminal(wp->w_buffer)
7055#endif
7056 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 STRCPY(p + len, "[+]");
7059 len += 3;
7060 }
7061 if (wp->w_buffer->b_p_ro)
7062 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007063 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007064 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066
Bram Moolenaar02631462017-09-22 15:20:32 +02007067 this_ru_col = ru_col - (Columns - wp->w_width);
7068 if (this_ru_col < (wp->w_width + 1) / 2)
7069 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (this_ru_col <= 1)
7071 {
7072 p = (char_u *)"<"; /* No room for file name! */
7073 len = 1;
7074 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007075 else if (has_mbyte)
7076 {
7077 int clen = 0, i;
7078
7079 /* Count total number of display cells. */
7080 clen = mb_string2cells(p, -1);
7081
7082 /* Find first character that will fit.
7083 * Going from start to end is much faster for DBCS. */
7084 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7085 i += (*mb_ptr2len)(p + i))
7086 clen -= (*mb_ptr2cells)(p + i);
7087 len = clen;
7088 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007090 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007092 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 }
7094
Bram Moolenaara12a1612019-01-24 16:39:02 +01007095 }
7096 else if (len > this_ru_col - 1)
7097 {
7098 p += len - (this_ru_col - 1);
7099 *p = '<';
7100 len = this_ru_col - 1;
7101 }
7102
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007104 screen_puts(p, row, wp->w_wincol, attr);
7105 screen_fill(row, row + 1, len + wp->w_wincol,
7106 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007108 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7110 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007111 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112
7113#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007114 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115#endif
7116 }
7117
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 /*
7119 * May need to draw the character below the vertical separator.
7120 */
7121 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7122 {
7123 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007124 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 else
7126 fillchar = fillchar_vsep(&attr);
7127 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7128 attr);
7129 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007130 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131}
7132
Bram Moolenaar238a5642006-02-21 22:12:05 +00007133#ifdef FEAT_STL_OPT
7134/*
7135 * Redraw the status line according to 'statusline' and take care of any
7136 * errors encountered.
7137 */
7138 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007139redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007140{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007141 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007142 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007143
7144 /* When called recursively return. This can happen when the statusline
7145 * contains an expression that triggers a redraw. */
7146 if (entered)
7147 return;
7148 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007149
Bram Moolenaara742e082016-04-05 21:10:38 +02007150 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007151 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007152 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007153 {
7154 /* When there is an error disable the statusline, otherwise the
7155 * display is messed up with errors and a redraw triggers the problem
7156 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007157 set_string_option_direct((char_u *)"statusline", -1,
7158 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007159 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007161 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007163}
7164#endif
7165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166/*
7167 * Return TRUE if the status line of window "wp" is connected to the status
7168 * line of the window right of it. If not, then it's a vertical separator.
7169 * Only call if (wp->w_vsep_width != 0).
7170 */
7171 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007172stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 frame_T *fr;
7175
7176 fr = wp->w_frame;
7177 while (fr->fr_parent != NULL)
7178 {
7179 if (fr->fr_parent->fr_layout == FR_COL)
7180 {
7181 if (fr->fr_next != NULL)
7182 break;
7183 }
7184 else
7185 {
7186 if (fr->fr_next != NULL)
7187 return TRUE;
7188 }
7189 fr = fr->fr_parent;
7190 }
7191 return FALSE;
7192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195/*
7196 * Get the value to show for the language mappings, active 'keymap'.
7197 */
7198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007199get_keymap_str(
7200 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007201 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007202 char_u *buf, /* buffer for the result */
7203 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204{
7205 char_u *p;
7206
7207 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7208 return FALSE;
7209
7210 {
7211#ifdef FEAT_EVAL
7212 buf_T *old_curbuf = curbuf;
7213 win_T *old_curwin = curwin;
7214 char_u *s;
7215
7216 curbuf = wp->w_buffer;
7217 curwin = wp;
7218 STRCPY(buf, "b:keymap_name"); /* must be writable */
7219 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007220 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 --emsg_skip;
7222 curbuf = old_curbuf;
7223 curwin = old_curwin;
7224 if (p == NULL || *p == NUL)
7225#endif
7226 {
7227#ifdef FEAT_KEYMAP
7228 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7229 p = wp->w_buffer->b_p_keymap;
7230 else
7231#endif
7232 p = (char_u *)"lang";
7233 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007234 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 buf[0] = NUL;
7236#ifdef FEAT_EVAL
7237 vim_free(s);
7238#endif
7239 }
7240 return buf[0] != NUL;
7241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243#if defined(FEAT_STL_OPT) || defined(PROTO)
7244/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007245 * Redraw the status line or ruler of window "wp".
7246 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 */
7248 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007249win_redr_custom(
7250 win_T *wp,
7251 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007253 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 int attr;
7255 int curattr;
7256 int row;
7257 int col = 0;
7258 int maxwidth;
7259 int width;
7260 int n;
7261 int len;
7262 int fillchar;
7263 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007264 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266 struct stl_hlrec hltab[STL_MAX_ITEM];
7267 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007268 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007269 win_T *ewp;
7270 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar1d633412013-12-11 15:52:01 +01007272 /* There is a tiny chance that this gets called recursively: When
7273 * redrawing a status line triggers redrawing the ruler or tabline.
7274 * Avoid trouble by not allowing recursion. */
7275 if (entered)
7276 return;
7277 entered = TRUE;
7278
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007280 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007282 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007283 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007285 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007286 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007287 maxwidth = Columns;
7288# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007289 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007292 else
7293 {
7294 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007295 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007296 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007297
7298 if (draw_ruler)
7299 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007300 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007301 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007302 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007304 if (*++stl == '-')
7305 stl++;
7306 if (atoi((char *)stl))
7307 while (VIM_ISDIGIT(*stl))
7308 stl++;
7309 if (*stl++ != '(')
7310 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007312 col = ru_col - (Columns - wp->w_width);
7313 if (col < (wp->w_width + 1) / 2)
7314 col = (wp->w_width + 1) / 2;
7315 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007316 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007317 {
7318 row = Rows - 1;
7319 --maxwidth; /* writing in last column may cause scrolling */
7320 fillchar = ' ';
7321 attr = 0;
7322 }
7323
7324# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007325 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007326# endif
7327 }
7328 else
7329 {
7330 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007331 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007332 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007333 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007334# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007335 use_sandbox = was_set_insecurely((char_u *)"statusline",
7336 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007337# endif
7338 }
7339
Bram Moolenaar53f81742017-09-22 14:35:51 +02007340 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007341 }
7342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007344 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
Bram Moolenaar61452852011-02-01 18:01:11 +01007346 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7347 * the cursor away and back. */
7348 ewp = wp == NULL ? curwin : wp;
7349 p_crb_save = ewp->w_p_crb;
7350 ewp->w_p_crb = FALSE;
7351
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 /* Make a copy, because the statusline may include a function call that
7353 * might change the option value and free the memory. */
7354 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007355 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007356 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007357 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007358 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007359 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007361 /* Make all characters printable. */
7362 p = transstr(buf);
7363 if (p != NULL)
7364 {
7365 vim_strncpy(buf, p, sizeof(buf) - 1);
7366 vim_free(p);
7367 }
7368
7369 /* fill up with "fillchar" */
7370 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007371 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 ++width;
7375 }
7376 buf[len] = NUL;
7377
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007378 /*
7379 * Draw each snippet with the specified highlighting.
7380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 curattr = attr;
7382 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007383 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007385 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 screen_puts_len(p, len, row, col, curattr);
7387 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007388 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007390 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007392 else if (hltab[n].userhl < 0)
7393 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007394#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007395 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7396 && wp->w_status_height != 0)
7397 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007398 else if (wp != NULL && bt_terminal(wp->w_buffer)
7399 && wp->w_status_height != 0)
7400 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007401#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007402 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007403 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007405 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
7407 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007408
7409 if (wp == NULL)
7410 {
7411 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7412 col = 0;
7413 len = 0;
7414 p = buf;
7415 fillchar = 0;
7416 for (n = 0; tabtab[n].start != NULL; n++)
7417 {
7418 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7419 while (col < len)
7420 TabPageIdxs[col++] = fillchar;
7421 p = tabtab[n].start;
7422 fillchar = tabtab[n].userhl;
7423 }
7424 while (col < Columns)
7425 TabPageIdxs[col++] = fillchar;
7426 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007427
7428theend:
7429 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431
7432#endif /* FEAT_STL_OPT */
7433
7434/*
7435 * Output a single character directly to the screen and update ScreenLines.
7436 */
7437 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007438screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 char_u buf[MB_MAXBYTES + 1];
7441
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007442 if (has_mbyte)
7443 buf[(*mb_char2bytes)(c, buf)] = NUL;
7444 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007445 {
7446 buf[0] = c;
7447 buf[1] = NUL;
7448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 screen_puts(buf, row, col, attr);
7450}
7451
7452/*
7453 * Get a single character directly from ScreenLines into "bytes[]".
7454 * Also return its attribute in *attrp;
7455 */
7456 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007457screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 unsigned off;
7460
7461 /* safety check */
7462 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7463 {
7464 off = LineOffset[row] + col;
7465 *attrp = ScreenAttrs[off];
7466 bytes[0] = ScreenLines[off];
7467 bytes[1] = NUL;
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (enc_utf8 && ScreenLinesUC[off] != 0)
7470 bytes[utfc_char2bytes(off, bytes)] = NUL;
7471 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7472 {
7473 bytes[0] = ScreenLines[off];
7474 bytes[1] = ScreenLines2[off];
7475 bytes[2] = NUL;
7476 }
7477 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7478 {
7479 bytes[1] = ScreenLines[off + 1];
7480 bytes[2] = NUL;
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
7483}
7484
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007485/*
7486 * Return TRUE if composing characters for screen posn "off" differs from
7487 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007488 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007489 */
7490 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007491screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007492{
7493 int i;
7494
7495 for (i = 0; i < Screen_mco; ++i)
7496 {
7497 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7498 return TRUE;
7499 if (u8cc[i] == 0)
7500 break;
7501 }
7502 return FALSE;
7503}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505/*
7506 * Put string '*text' on the screen at position 'row' and 'col', with
7507 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7508 * Note: only outputs within one row, message is truncated at screen boundary!
7509 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7510 */
7511 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007512screen_puts(
7513 char_u *text,
7514 int row,
7515 int col,
7516 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517{
7518 screen_puts_len(text, -1, row, col, attr);
7519}
7520
7521/*
7522 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7523 * a NUL.
7524 */
7525 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007526screen_puts_len(
7527 char_u *text,
7528 int textlen,
7529 int row,
7530 int col,
7531 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 unsigned off;
7534 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007535 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007537 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 int mbyte_blen = 1;
7539 int mbyte_cells = 1;
7540 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007541 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007543#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007545 int pc, nc, nc1;
7546 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007548 int force_redraw_this;
7549 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
7552 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7553 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007554 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
Bram Moolenaarc236c162008-07-13 17:41:49 +00007556 /* When drawing over the right halve of a double-wide char clear out the
7557 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007558 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007559#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007560 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007561#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007562 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007563 {
7564 ScreenLines[off - 1] = ' ';
7565 ScreenAttrs[off - 1] = 0;
7566 if (enc_utf8)
7567 {
7568 ScreenLinesUC[off - 1] = 0;
7569 ScreenLinesC[0][off - 1] = 0;
7570 }
7571 /* redraw the previous cell, make it empty */
7572 screen_char(off - 1, row, col - 1);
7573 /* force the cell at "col" to be redrawn */
7574 force_redraw_next = TRUE;
7575 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007576
Bram Moolenaar367329b2007-08-30 11:53:22 +00007577 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007578 while (col < screen_Columns
7579 && (len < 0 || (int)(ptr - text) < len)
7580 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
7582 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 /* check if this is the first byte of a multibyte */
7584 if (has_mbyte)
7585 {
7586 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007587 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007589 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7591 mbyte_cells = 1;
7592 else if (enc_dbcs != 0)
7593 mbyte_cells = mbyte_blen;
7594 else /* enc_utf8 */
7595 {
7596 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007597 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 (int)((text + len) - ptr));
7599 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007602#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7604 {
7605 /* Do Arabic shaping. */
7606 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7607 {
7608 /* Past end of string to be displayed. */
7609 nc = NUL;
7610 nc1 = NUL;
7611 }
7612 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007613 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007614 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7615 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007616 nc1 = pcc[0];
7617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 pc = prev_c;
7619 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007620 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
7622 else
7623 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007624#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007625 if (col + mbyte_cells > screen_Columns)
7626 {
7627 /* Only 1 cell left, but character requires 2 cells:
7628 * display a '>' in the last column to avoid wrapping. */
7629 c = '>';
7630 mbyte_cells = 1;
7631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 force_redraw_this = force_redraw_next;
7636 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637
7638 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 || (mbyte_cells == 2
7640 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7641 || (enc_dbcs == DBCS_JPNU
7642 && c == 0x8e
7643 && ScreenLines2[off] != ptr[1])
7644 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007645 && (ScreenLinesUC[off] !=
7646 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7647 || (ScreenLinesUC[off] != 0
7648 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007650 || exmode_active;
7651
Bram Moolenaara12a1612019-01-24 16:39:02 +01007652 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {
7654#if defined(FEAT_GUI) || defined(UNIX)
7655 /* The bold trick makes a single row of pixels appear in the next
7656 * character. When a bold character is removed, the next
7657 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007658 * and for some xterms. */
7659 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660# ifdef FEAT_GUI
7661 gui.in_use
7662# endif
7663# if defined(FEAT_GUI) && defined(UNIX)
7664 ||
7665# endif
7666# ifdef UNIX
7667 term_is_xterm
7668# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007669 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007671 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007673 if (n > HL_ALL)
7674 n = syn_attr2attr(n);
7675 if (n & HL_BOLD)
7676 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 /* When at the end of the text and overwriting a two-cell
7680 * character with a one-cell character, need to clear the next
7681 * cell. Also when overwriting the left halve of a two-cell char
7682 * with the right halve of a two-cell char. Do this only once
7683 * (mb_off2cells() may return 2 on the right halve). */
7684 if (clear_next_cell)
7685 clear_next_cell = FALSE;
7686 else if (has_mbyte
7687 && (len < 0 ? ptr[mbyte_blen] == NUL
7688 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007689 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007691 && (*mb_off2cells)(off, max_off) == 1
7692 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 clear_next_cell = TRUE;
7694
7695 /* Make sure we never leave a second byte of a double-byte behind,
7696 * it confuses mb_off2cells(). */
7697 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007698 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007700 && (*mb_off2cells)(off, max_off) == 1
7701 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 ScreenLines[off] = c;
7704 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (enc_utf8)
7706 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007707 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 ScreenLinesUC[off] = 0;
7709 else
7710 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007711 int i;
7712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007714 for (i = 0; i < Screen_mco; ++i)
7715 {
7716 ScreenLinesC[i][off] = u8cc[i];
7717 if (u8cc[i] == 0)
7718 break;
7719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 }
7721 if (mbyte_cells == 2)
7722 {
7723 ScreenLines[off + 1] = 0;
7724 ScreenAttrs[off + 1] = attr;
7725 }
7726 screen_char(off, row, col);
7727 }
7728 else if (mbyte_cells == 2)
7729 {
7730 ScreenLines[off + 1] = ptr[1];
7731 ScreenAttrs[off + 1] = attr;
7732 screen_char_2(off, row, col);
7733 }
7734 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7735 {
7736 ScreenLines2[off] = ptr[1];
7737 screen_char(off, row, col);
7738 }
7739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 screen_char(off, row, col);
7741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 if (has_mbyte)
7743 {
7744 off += mbyte_cells;
7745 col += mbyte_cells;
7746 ptr += mbyte_blen;
7747 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007748 {
7749 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007751 len = -1;
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 }
7754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
7756 ++off;
7757 ++col;
7758 ++ptr;
7759 }
7760 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007761
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007762 /* If we detected the next character needs to be redrawn, but the text
7763 * doesn't extend up to there, update the character here. */
7764 if (force_redraw_next && col < screen_Columns)
7765 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007766 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7767 screen_char_2(off, row, col);
7768 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007769 screen_char(off, row, col);
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771}
7772
7773#ifdef FEAT_SEARCH_EXTRA
7774/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007775 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 */
7777 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007778start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 if (p_hls && !no_hlsearch)
7781 {
7782 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007783 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007784# ifdef FEAT_RELTIME
7785 /* Set the time limit to 'redrawtime'. */
7786 profile_setlimit(p_rdt, &search_hl.tm);
7787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 }
7789}
7790
7791/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007792 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 */
7794 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007795end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 if (search_hl.rm.regprog != NULL)
7798 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007799 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 search_hl.rm.regprog = NULL;
7801 }
7802}
7803
7804/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007805 * Init for calling prepare_search_hl().
7806 */
7807 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007808init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007809{
7810 matchitem_T *cur;
7811
7812 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7813 * match */
7814 cur = wp->w_match_head;
7815 while (cur != NULL)
7816 {
7817 cur->hl.rm = cur->match;
7818 if (cur->hlg_id == 0)
7819 cur->hl.attr = 0;
7820 else
7821 cur->hl.attr = syn_id2attr(cur->hlg_id);
7822 cur->hl.buf = wp->w_buffer;
7823 cur->hl.lnum = 0;
7824 cur->hl.first_lnum = 0;
7825# ifdef FEAT_RELTIME
7826 /* Set the time limit to 'redrawtime'. */
7827 profile_setlimit(p_rdt, &(cur->hl.tm));
7828# endif
7829 cur = cur->next;
7830 }
7831 search_hl.buf = wp->w_buffer;
7832 search_hl.lnum = 0;
7833 search_hl.first_lnum = 0;
7834 /* time limit is set at the toplevel, for all windows */
7835}
7836
7837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 * Advance to the match in window "wp" line "lnum" or past it.
7839 */
7840 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007841prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007843 matchitem_T *cur; /* points to the match list */
7844 match_T *shl; /* points to search_hl or a match */
7845 int shl_flag; /* flag to indicate whether search_hl
7846 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007847 int pos_inprogress; /* marks that position match search is
7848 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 int n;
7850
7851 /*
7852 * When using a multi-line pattern, start searching at the top
7853 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007854 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 cur = wp->w_match_head;
7857 shl_flag = FALSE;
7858 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007860 if (shl_flag == FALSE)
7861 {
7862 shl = &search_hl;
7863 shl_flag = TRUE;
7864 }
7865 else
7866 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (shl->rm.regprog != NULL
7868 && shl->lnum == 0
7869 && re_multiline(shl->rm.regprog))
7870 {
7871 if (shl->first_lnum == 0)
7872 {
7873# ifdef FEAT_FOLDING
7874 for (shl->first_lnum = lnum;
7875 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7876 if (hasFoldingWin(wp, shl->first_lnum - 1,
7877 NULL, NULL, TRUE, NULL))
7878 break;
7879# else
7880 shl->first_lnum = wp->w_topline;
7881# endif
7882 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007883 if (cur != NULL)
7884 cur->pos.cur = 0;
7885 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007887 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7888 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007890 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7891 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007892 pos_inprogress = cur == NULL || cur->pos.cur == 0
7893 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 if (shl->lnum != 0)
7895 {
7896 shl->first_lnum = shl->lnum
7897 + shl->rm.endpos[0].lnum
7898 - shl->rm.startpos[0].lnum;
7899 n = shl->rm.endpos[0].col;
7900 }
7901 else
7902 {
7903 ++shl->first_lnum;
7904 n = 0;
7905 }
7906 }
7907 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007908 if (shl != &search_hl && cur != NULL)
7909 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 }
7911}
7912
7913/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007914 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * Uses shl->buf.
7916 * Sets shl->lnum and shl->rm contents.
7917 * Note: Assumes a previous match is always before "lnum", unless
7918 * shl->lnum is zero.
7919 * Careful: Any pointers for buffer lines will become invalid.
7920 */
7921 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007922next_search_hl(
7923 win_T *win,
7924 match_T *shl, /* points to search_hl or a match */
7925 linenr_T lnum,
7926 colnr_T mincol, /* minimal column for a match */
7927 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 linenr_T l;
7930 colnr_T matchcol;
7931 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007932 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007934 // for :{range}s/pat only highlight inside the range
7935 if (lnum < search_first_line || lnum > search_last_line)
7936 {
7937 shl->lnum = 0;
7938 return;
7939 }
7940
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (shl->lnum != 0)
7942 {
7943 /* Check for three situations:
7944 * 1. If the "lnum" is below a previous match, start a new search.
7945 * 2. If the previous match includes "mincol", use it.
7946 * 3. Continue after the previous match.
7947 */
7948 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7949 if (lnum > l)
7950 shl->lnum = 0;
7951 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7952 return;
7953 }
7954
7955 /*
7956 * Repeat searching for a match until one is found that includes "mincol"
7957 * or none is found in this line.
7958 */
7959 called_emsg = FALSE;
7960 for (;;)
7961 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007962#ifdef FEAT_RELTIME
7963 /* Stop searching after passing the time limit. */
7964 if (profile_passed_limit(&(shl->tm)))
7965 {
7966 shl->lnum = 0; /* no match found in time */
7967 break;
7968 }
7969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 /* Three situations:
7971 * 1. No useful previous match: search from start of line.
7972 * 2. Not Vi compatible or empty match: continue at next character.
7973 * Break the loop if this is beyond the end of the line.
7974 * 3. Vi compatible searching: continue at end of previous match.
7975 */
7976 if (shl->lnum == 0)
7977 matchcol = 0;
7978 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7979 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007980 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007982 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007983
7984 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007985 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007986 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007988 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 shl->lnum = 0;
7990 break;
7991 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007992 if (has_mbyte)
7993 matchcol += mb_ptr2len(ml);
7994 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007995 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
7997 else
7998 matchcol = shl->rm.endpos[0].col;
7999
8000 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008001 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008003 /* Remember whether shl->rm is using a copy of the regprog in
8004 * cur->match. */
8005 int regprog_is_copy = (shl != &search_hl && cur != NULL
8006 && shl == &cur->hl
8007 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008008 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008009
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8011 matchcol,
8012#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008013 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008014#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008015 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008016#endif
8017 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008018 /* Copy the regprog, in case it got freed and recompiled. */
8019 if (regprog_is_copy)
8020 cur->match.regprog = cur->hl.rm.regprog;
8021
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008022 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008023 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 /* Error while handling regexp: stop using this regexp. */
8025 if (shl == &search_hl)
8026 {
8027 /* don't free regprog in the match list, it's a copy */
8028 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008029 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008030 }
8031 shl->rm.regprog = NULL;
8032 shl->lnum = 0;
8033 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8034 message */
8035 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008036 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037 }
8038 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008040 else
8041 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 if (nmatched == 0)
8043 {
8044 shl->lnum = 0; /* no match found */
8045 break;
8046 }
8047 if (shl->rm.startpos[0].lnum > 0
8048 || shl->rm.startpos[0].col >= mincol
8049 || nmatched > 1
8050 || shl->rm.endpos[0].col > mincol)
8051 {
8052 shl->lnum += shl->rm.startpos[0].lnum;
8053 break; /* useful match found */
8054 }
8055 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008056
8057 // Restore called_emsg for assert_fails().
8058 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060
Bram Moolenaar85077472016-10-16 14:35:48 +02008061/*
8062 * If there is a match fill "shl" and return one.
8063 * Return zero otherwise.
8064 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008065 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008066next_search_hl_pos(
8067 match_T *shl, /* points to a match */
8068 linenr_T lnum,
8069 posmatch_T *posmatch, /* match positions */
8070 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008071{
8072 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008073 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074
Bram Moolenaarb3414592014-06-17 17:48:32 +02008075 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8076 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008077 llpos_T *pos = &posmatch->pos[i];
8078
8079 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008081 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008083 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008085 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008087 /* if this match comes before the one at "found" then swap
8088 * them */
8089 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008090 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008091 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008092
Bram Moolenaar85077472016-10-16 14:35:48 +02008093 *pos = posmatch->pos[found];
8094 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008095 }
8096 }
8097 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008098 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008099 }
8100 }
8101 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008102 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008103 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008104 colnr_T start = posmatch->pos[found].col == 0
8105 ? 0 : posmatch->pos[found].col - 1;
8106 colnr_T end = posmatch->pos[found].col == 0
8107 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008108
Bram Moolenaar85077472016-10-16 14:35:48 +02008109 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008110 shl->rm.startpos[0].lnum = 0;
8111 shl->rm.startpos[0].col = start;
8112 shl->rm.endpos[0].lnum = 0;
8113 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008114 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008115 posmatch->cur = found + 1;
8116 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008117 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008118 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008119}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008120#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008121
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008123screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 attrentry_T *aep = NULL;
8126
8127 screen_attr = attr;
8128 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008129#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 && termcap_active
8131#endif
8132 )
8133 {
8134#ifdef FEAT_GUI
8135 if (gui.in_use)
8136 {
8137 char buf[20];
8138
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008139 /* The GUI handles this internally. */
8140 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 OUT_STR(buf);
8142 }
8143 else
8144#endif
8145 {
8146 if (attr > HL_ALL) /* special HL attr. */
8147 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008148 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 aep = syn_cterm_attr2entry(attr);
8150 else
8151 aep = syn_term_attr2entry(attr);
8152 if (aep == NULL) /* did ":syntax clear" */
8153 attr = 0;
8154 else
8155 attr = aep->ae_attr;
8156 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008157 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008159 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008160#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8162 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8163 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008164#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008165 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008166 /* If the Normal FG color has BOLD attribute and the new HL
8167 * has a FG color defined, clear BOLD. */
8168 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008169 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008171 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008172 out_str(T_UCS);
8173 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008174 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8175 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008177 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008179 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008181 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008182 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183
8184 /*
8185 * Output the color or start string after bold etc., in case the
8186 * bold etc. override the color setting.
8187 */
8188 if (aep != NULL)
8189 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008190#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008191 /* When 'termguicolors' is set but fg or bg is unset,
8192 * fall back to the cterm colors. This helps for SpellBad,
8193 * where the GUI uses a red undercurl. */
8194 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008196 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008197 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008198 }
8199 else
8200#endif
8201 if (t_colors > 1)
8202 {
8203 if (aep->ae_u.cterm.fg_color)
8204 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8205 }
8206#ifdef FEAT_TERMGUICOLORS
8207 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8208 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008209 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008210 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 }
8212 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008213#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008214 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008216 if (aep->ae_u.cterm.bg_color)
8217 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8218 }
8219
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008220 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008221 {
8222 if (aep->ae_u.term.start != NULL)
8223 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 }
8225 }
8226 }
8227 }
8228}
8229
8230 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008231screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232{
8233 int do_ME = FALSE; /* output T_ME code */
8234
8235 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008236#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 && termcap_active
8238#endif
8239 )
8240 {
8241#ifdef FEAT_GUI
8242 if (gui.in_use)
8243 {
8244 char buf[20];
8245
8246 /* use internal GUI code */
8247 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8248 OUT_STR(buf);
8249 }
8250 else
8251#endif
8252 {
8253 if (screen_attr > HL_ALL) /* special HL attr. */
8254 {
8255 attrentry_T *aep;
8256
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008257 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {
8259 /*
8260 * Assume that t_me restores the original colors!
8261 */
8262 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008263 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008264#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8266 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8267 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008268#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008269 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008270#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008271 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8272 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8273 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008274#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008275 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 do_ME = TRUE;
8277 }
8278 else
8279 {
8280 aep = syn_term_attr2entry(screen_attr);
8281 if (aep != NULL && aep->ae_u.term.stop != NULL)
8282 {
8283 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8284 do_ME = TRUE;
8285 else
8286 out_str(aep->ae_u.term.stop);
8287 }
8288 }
8289 if (aep == NULL) /* did ":syntax clear" */
8290 screen_attr = 0;
8291 else
8292 screen_attr = aep->ae_attr;
8293 }
8294
8295 /*
8296 * Often all ending-codes are equal to T_ME. Avoid outputting the
8297 * same sequence several times.
8298 */
8299 if (screen_attr & HL_STANDOUT)
8300 {
8301 if (STRCMP(T_SE, T_ME) == 0)
8302 do_ME = TRUE;
8303 else
8304 out_str(T_SE);
8305 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008306 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008307 {
8308 if (STRCMP(T_UCE, T_ME) == 0)
8309 do_ME = TRUE;
8310 else
8311 out_str(T_UCE);
8312 }
8313 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008314 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {
8316 if (STRCMP(T_UE, T_ME) == 0)
8317 do_ME = TRUE;
8318 else
8319 out_str(T_UE);
8320 }
8321 if (screen_attr & HL_ITALIC)
8322 {
8323 if (STRCMP(T_CZR, T_ME) == 0)
8324 do_ME = TRUE;
8325 else
8326 out_str(T_CZR);
8327 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008328 if (screen_attr & HL_STRIKETHROUGH)
8329 {
8330 if (STRCMP(T_STE, T_ME) == 0)
8331 do_ME = TRUE;
8332 else
8333 out_str(T_STE);
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8336 out_str(T_ME);
8337
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008338#ifdef FEAT_TERMGUICOLORS
8339 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008341 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008342 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008343 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008344 term_bg_rgb_color(cterm_normal_bg_gui_color);
8345 }
8346 else
8347#endif
8348 {
8349 if (t_colors > 1)
8350 {
8351 /* set Normal cterm colors */
8352 if (cterm_normal_fg_color != 0)
8353 term_fg_color(cterm_normal_fg_color - 1);
8354 if (cterm_normal_bg_color != 0)
8355 term_bg_color(cterm_normal_bg_color - 1);
8356 if (cterm_normal_fg_bold)
8357 out_str(T_MD);
8358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 }
8360 }
8361 }
8362 screen_attr = 0;
8363}
8364
8365/*
8366 * Reset the colors for a cterm. Used when leaving Vim.
8367 * The machine specific code may override this again.
8368 */
8369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008370reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008372 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
8374 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008375#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008376 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8377 || cterm_normal_bg_gui_color != INVALCOLOR)
8378 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008379#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {
8383 out_str(T_OP);
8384 screen_attr = -1;
8385 }
8386 if (cterm_normal_fg_bold)
8387 {
8388 out_str(T_ME);
8389 screen_attr = -1;
8390 }
8391 }
8392}
8393
8394/*
8395 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8396 * using the attributes from ScreenAttrs["off"].
8397 */
8398 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008399screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400{
8401 int attr;
8402
8403 /* Check for illegal values, just in case (could happen just after
8404 * resizing). */
8405 if (row >= screen_Rows || col >= screen_Columns)
8406 return;
8407
Bram Moolenaarae654382019-01-17 21:09:05 +01008408#ifdef FEAT_INS_EXPAND
8409 if (pum_under_menu(row, col))
8410 return;
8411#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008412 /* Outputting a character in the last cell on the screen may scroll the
8413 * screen up. Only do it when the "xn" termcap property is set, otherwise
8414 * mark the character invalid (update it when scrolled up). */
8415 if (*T_XN == NUL
8416 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417#ifdef FEAT_RIGHTLEFT
8418 /* account for first command-line character in rightleft mode */
8419 && !cmdmsg_rl
8420#endif
8421 )
8422 {
8423 ScreenAttrs[off] = (sattr_T)-1;
8424 return;
8425 }
8426
8427 /*
8428 * Stop highlighting first, so it's easier to move the cursor.
8429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 if (screen_char_attr != 0)
8431 attr = screen_char_attr;
8432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 attr = ScreenAttrs[off];
8434 if (screen_attr != attr)
8435 screen_stop_highlight();
8436
8437 windgoto(row, col);
8438
8439 if (screen_attr != attr)
8440 screen_start_highlight(attr);
8441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 if (enc_utf8 && ScreenLinesUC[off] != 0)
8443 {
8444 char_u buf[MB_MAXBYTES + 1];
8445
Bram Moolenaarcb070082016-04-02 22:14:51 +02008446 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008447 {
8448 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008449#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008450 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008451#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008452 )
8453 {
8454 /* Clear the two screen cells. If the character is actually
8455 * single width it won't change the second cell. */
8456 out_str((char_u *)" ");
8457 term_windgoto(row, col);
8458 }
8459 /* not sure where the cursor is after drawing the ambiguous width
8460 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008461 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008462 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008463 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008465
8466 /* Convert the UTF-8 character to bytes and write it. */
8467 buf[utfc_char2bytes(off, buf)] = NUL;
8468 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 /* double-byte character in single-width cell */
8475 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8476 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 }
8478
8479 screen_cur_col++;
8480}
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482/*
8483 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8484 * on the screen at position 'row' and 'col'.
8485 * The attributes of the first byte is used for all. This is required to
8486 * output the two bytes of a double-byte character with nothing in between.
8487 */
8488 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008489screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490{
8491 /* Check for illegal values (could be wrong when screen was resized). */
8492 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8493 return;
8494
8495 /* Outputting the last character on the screen may scrollup the screen.
8496 * Don't to it! Mark the character invalid (update it when scrolled up) */
8497 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8498 {
8499 ScreenAttrs[off] = (sattr_T)-1;
8500 return;
8501 }
8502
8503 /* Output the first byte normally (positions the cursor), then write the
8504 * second byte directly. */
8505 screen_char(off, row, col);
8506 out_char(ScreenLines[off + 1]);
8507 ++screen_cur_col;
8508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510/*
8511 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8512 * This uses the contents of ScreenLines[] and doesn't change it.
8513 */
8514 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008515screen_draw_rectangle(
8516 int row,
8517 int col,
8518 int height,
8519 int width,
8520 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
8522 int r, c;
8523 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008524 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008526 /* Can't use ScreenLines unless initialized */
8527 if (ScreenLines == NULL)
8528 return;
8529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 if (invert)
8531 screen_char_attr = HL_INVERSE;
8532 for (r = row; r < row + height; ++r)
8533 {
8534 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008535 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 for (c = col; c < col + width; ++c)
8537 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008538 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 {
8540 screen_char_2(off + c, r, c);
8541 ++c;
8542 }
8543 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
8545 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008546 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 }
8549 }
8550 }
8551 screen_char_attr = 0;
8552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554/*
8555 * Redraw the characters for a vertically split window.
8556 */
8557 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008558redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
8560 int col;
8561 int width;
8562
8563# ifdef FEAT_CLIPBOARD
8564 clip_may_clear_selection(row, end - 1);
8565# endif
8566
8567 if (wp == NULL)
8568 {
8569 col = 0;
8570 width = Columns;
8571 }
8572 else
8573 {
8574 col = wp->w_wincol;
8575 width = wp->w_width;
8576 }
8577 screen_draw_rectangle(row, col, end - row, width, FALSE);
8578}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008580 static void
8581space_to_screenline(int off, int attr)
8582{
8583 ScreenLines[off] = ' ';
8584 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008585 if (enc_utf8)
8586 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008587}
8588
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589/*
8590 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8591 * with character 'c1' in first column followed by 'c2' in the other columns.
8592 * Use attributes 'attr'.
8593 */
8594 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008595screen_fill(
8596 int start_row,
8597 int end_row,
8598 int start_col,
8599 int end_col,
8600 int c1,
8601 int c2,
8602 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
8604 int row;
8605 int col;
8606 int off;
8607 int end_off;
8608 int did_delete;
8609 int c;
8610 int norm_term;
8611#if defined(FEAT_GUI) || defined(UNIX)
8612 int force_next = FALSE;
8613#endif
8614
8615 if (end_row > screen_Rows) /* safety check */
8616 end_row = screen_Rows;
8617 if (end_col > screen_Columns) /* safety check */
8618 end_col = screen_Columns;
8619 if (ScreenLines == NULL
8620 || start_row >= end_row
8621 || start_col >= end_col) /* nothing to do */
8622 return;
8623
8624 /* it's a "normal" terminal when not in a GUI or cterm */
8625 norm_term = (
8626#ifdef FEAT_GUI
8627 !gui.in_use &&
8628#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008629 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 for (row = start_row; row < end_row; ++row)
8631 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008632 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008633#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008634 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008635#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008636 )
8637 {
8638 /* When drawing over the right halve of a double-wide char clear
8639 * out the left halve. When drawing over the left halve of a
8640 * double wide-char clear out the right halve. Only needed in a
8641 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008642 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008643 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008644 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008645 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 /*
8648 * Try to use delete-line termcap code, when no attributes or in a
8649 * "normal" terminal, where a bold/italic space is just a
8650 * space.
8651 */
8652 did_delete = FALSE;
8653 if (c2 == ' '
8654 && end_col == Columns
8655 && can_clear(T_CE)
8656 && (attr == 0
8657 || (norm_term
8658 && attr <= HL_ALL
8659 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8660 {
8661 /*
8662 * check if we really need to clear something
8663 */
8664 col = start_col;
8665 if (c1 != ' ') /* don't clear first char */
8666 ++col;
8667
8668 off = LineOffset[row] + col;
8669 end_off = LineOffset[row] + end_col;
8670
8671 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 if (enc_utf8)
8673 while (off < end_off && ScreenLines[off] == ' '
8674 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8675 ++off;
8676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 while (off < end_off && ScreenLines[off] == ' '
8678 && ScreenAttrs[off] == 0)
8679 ++off;
8680 if (off < end_off) /* something to be cleared */
8681 {
8682 col = off - LineOffset[row];
8683 screen_stop_highlight();
8684 term_windgoto(row, col);/* clear rest of this screen line */
8685 out_str(T_CE);
8686 screen_start(); /* don't know where cursor is now */
8687 col = end_col - col;
8688 while (col--) /* clear chars in ScreenLines */
8689 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008690 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 ++off;
8692 }
8693 }
8694 did_delete = TRUE; /* the chars are cleared now */
8695 }
8696
8697 off = LineOffset[row] + start_col;
8698 c = c1;
8699 for (col = start_col; col < end_col; ++col)
8700 {
8701 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008702 || (enc_utf8 && (int)ScreenLinesUC[off]
8703 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 || ScreenAttrs[off] != attr
8705#if defined(FEAT_GUI) || defined(UNIX)
8706 || force_next
8707#endif
8708 )
8709 {
8710#if defined(FEAT_GUI) || defined(UNIX)
8711 /* The bold trick may make a single row of pixels appear in
8712 * the next character. When a bold character is removed, the
8713 * next character should be redrawn too. This happens for our
8714 * own GUI and for some xterms. */
8715 if (
8716# ifdef FEAT_GUI
8717 gui.in_use
8718# endif
8719# if defined(FEAT_GUI) && defined(UNIX)
8720 ||
8721# endif
8722# ifdef UNIX
8723 term_is_xterm
8724# endif
8725 )
8726 {
8727 if (ScreenLines[off] != ' '
8728 && (ScreenAttrs[off] > HL_ALL
8729 || ScreenAttrs[off] & HL_BOLD))
8730 force_next = TRUE;
8731 else
8732 force_next = FALSE;
8733 }
8734#endif
8735 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 if (enc_utf8)
8737 {
8738 if (c >= 0x80)
8739 {
8740 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008741 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743 else
8744 ScreenLinesUC[off] = 0;
8745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 ScreenAttrs[off] = attr;
8747 if (!did_delete || c != ' ')
8748 screen_char(off, row, col);
8749 }
8750 ++off;
8751 if (col == start_col)
8752 {
8753 if (did_delete)
8754 break;
8755 c = c2;
8756 }
8757 }
8758 if (end_col == Columns)
8759 LineWraps[row] = FALSE;
8760 if (row == Rows - 1) /* overwritten the command line */
8761 {
8762 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008763 if (start_col == 0 && end_col == Columns
8764 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008766 if (start_col == 0)
8767 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 }
8769 }
8770}
8771
8772/*
8773 * Check if there should be a delay. Used before clearing or redrawing the
8774 * screen or the command line.
8775 */
8776 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008777check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8780 && !did_wait_return
8781 && emsg_silent == 0)
8782 {
8783 out_flush();
8784 ui_delay(1000L, TRUE);
8785 emsg_on_display = FALSE;
8786 if (check_msg_scroll)
8787 msg_scroll = FALSE;
8788 }
8789}
8790
8791/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008792 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8793 */
8794 static void
8795clear_TabPageIdxs(void)
8796{
8797 int scol;
8798
8799 for (scol = 0; scol < Columns; ++scol)
8800 TabPageIdxs[scol] = 0;
8801}
8802
8803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008805 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 * Returns TRUE if there is a valid screen to write to.
8807 * Returns FALSE when starting up and screen not initialized yet.
8808 */
8809 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008810screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008812 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 return (ScreenLines != NULL);
8814}
8815
8816/*
8817 * Resize the shell to Rows and Columns.
8818 * Allocate ScreenLines[] and associated items.
8819 *
8820 * There may be some time between setting Rows and Columns and (re)allocating
8821 * ScreenLines[]. This happens when starting up and when (manually) changing
8822 * the shell size. Always use screen_Rows and screen_Columns to access items
8823 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8824 * final size of the shell is needed.
8825 */
8826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008827screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 int new_row, old_row;
8830#ifdef FEAT_GUI
8831 int old_Rows;
8832#endif
8833 win_T *wp;
8834 int outofmem = FALSE;
8835 int len;
8836 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008838 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008840 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 sattr_T *new_ScreenAttrs;
8842 unsigned *new_LineOffset;
8843 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008844 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008845 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008847 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008848 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008850retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 /*
8852 * Allocation of the screen buffers is done only when the size changes and
8853 * when Rows and Columns have been set and we have started doing full
8854 * screen stuff.
8855 */
8856 if ((ScreenLines != NULL
8857 && Rows == screen_Rows
8858 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 && enc_utf8 == (ScreenLinesUC != NULL)
8860 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008861 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 || Rows == 0
8863 || Columns == 0
8864 || (!full_screen && ScreenLines == NULL))
8865 return;
8866
8867 /*
8868 * It's possible that we produce an out-of-memory message below, which
8869 * will cause this function to be called again. To break the loop, just
8870 * return here.
8871 */
8872 if (entered)
8873 return;
8874 entered = TRUE;
8875
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008876 /*
8877 * Note that the window sizes are updated before reallocating the arrays,
8878 * thus we must not redraw here!
8879 */
8880 ++RedrawingDisabled;
8881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 win_new_shellsize(); /* fit the windows in the new sized shell */
8883
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 comp_col(); /* recompute columns for shown command and ruler */
8885
8886 /*
8887 * We're changing the size of the screen.
8888 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8889 * - Move lines from the old arrays into the new arrays, clear extra
8890 * lines (unless the screen is going to be cleared).
8891 * - Free the old arrays.
8892 *
8893 * If anything fails, make ScreenLines NULL, so we don't do anything!
8894 * Continuing with the old ScreenLines may result in a crash, because the
8895 * size is wrong.
8896 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008897 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008899 if (aucmd_win != NULL)
8900 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008902 new_ScreenLines = (schar_T *)lalloc(
8903 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008904 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 if (enc_utf8)
8906 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008907 new_ScreenLinesUC = (u8char_T *)lalloc(
8908 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008909 for (i = 0; i < p_mco; ++i)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008910 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear(
8911 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008914 new_ScreenLines2 = (schar_T *)lalloc(
8915 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
8916 new_ScreenAttrs = (sattr_T *)lalloc(
8917 (Rows + 1) * Columns * sizeof(sattr_T), FALSE);
8918 new_LineOffset = (unsigned *)lalloc(Rows * sizeof(unsigned), FALSE);
8919 new_LineWraps = (char_u *)lalloc(Rows * sizeof(char_u), FALSE);
8920 new_TabPageIdxs = (short *)lalloc(Columns * sizeof(short), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008922 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
8924 if (win_alloc_lines(wp) == FAIL)
8925 {
8926 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008927 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 }
8929 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008930 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8931 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008932 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008933give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008935 for (i = 0; i < p_mco; ++i)
8936 if (new_ScreenLinesC[i] == NULL)
8937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008939 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 || new_ScreenAttrs == NULL
8942 || new_LineOffset == NULL
8943 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008944 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 || outofmem)
8946 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008947 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008948 {
8949 /* guess the size */
8950 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8951
8952 /* Remember we did this to avoid getting outofmem messages over
8953 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008954 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008955 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008956 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008957 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008959 VIM_CLEAR(new_ScreenLinesC[i]);
8960 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008961 VIM_CLEAR(new_ScreenAttrs);
8962 VIM_CLEAR(new_LineOffset);
8963 VIM_CLEAR(new_LineWraps);
8964 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 }
8966 else
8967 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008968 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 for (new_row = 0; new_row < Rows; ++new_row)
8971 {
8972 new_LineOffset[new_row] = new_row * Columns;
8973 new_LineWraps[new_row] = FALSE;
8974
8975 /*
8976 * If the screen is not going to be cleared, copy as much as
8977 * possible from the old screen to the new one and clear the rest
8978 * (used when resizing the window at the "--more--" prompt or when
8979 * executing an external command, for the GUI).
8980 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008981 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 {
8983 (void)vim_memset(new_ScreenLines + new_row * Columns,
8984 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 if (enc_utf8)
8986 {
8987 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8988 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008989 for (i = 0; i < p_mco; ++i)
8990 (void)vim_memset(new_ScreenLinesC[i]
8991 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 0, (size_t)Columns * sizeof(u8char_T));
8993 }
8994 if (enc_dbcs == DBCS_JPNU)
8995 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8996 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8998 0, (size_t)Columns * sizeof(sattr_T));
8999 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009000 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
9002 if (screen_Columns < Columns)
9003 len = screen_Columns;
9004 else
9005 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009006 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009007 * may be invalid now. Also when p_mco changes. */
9008 if (!(enc_utf8 && ScreenLinesUC == NULL)
9009 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009010 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9011 ScreenLines + LineOffset[old_row],
9012 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009013 if (enc_utf8 && ScreenLinesUC != NULL
9014 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
9016 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9017 ScreenLinesUC + LineOffset[old_row],
9018 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009019 for (i = 0; i < p_mco; ++i)
9020 mch_memmove(new_ScreenLinesC[i]
9021 + new_LineOffset[new_row],
9022 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 (size_t)len * sizeof(u8char_T));
9024 }
9025 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9026 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9027 ScreenLines2 + LineOffset[old_row],
9028 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9030 ScreenAttrs + LineOffset[old_row],
9031 (size_t)len * sizeof(sattr_T));
9032 }
9033 }
9034 }
9035 /* Use the last line of the screen for the current line. */
9036 current_ScreenLine = new_ScreenLines + Rows * Columns;
9037 }
9038
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009039 free_screenlines();
9040
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009043 for (i = 0; i < p_mco; ++i)
9044 ScreenLinesC[i] = new_ScreenLinesC[i];
9045 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 ScreenAttrs = new_ScreenAttrs;
9048 LineOffset = new_LineOffset;
9049 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009050 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 /* It's important that screen_Rows and screen_Columns reflect the actual
9053 * size of ScreenLines[]. Set them before calling anything. */
9054#ifdef FEAT_GUI
9055 old_Rows = screen_Rows;
9056#endif
9057 screen_Rows = Rows;
9058 screen_Columns = Columns;
9059
9060 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009061 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063#ifdef FEAT_GUI
9064 else if (gui.in_use
9065 && !gui.starting
9066 && ScreenLines != NULL
9067 && old_Rows != Rows)
9068 {
9069 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9070 /*
9071 * Adjust the position of the cursor, for when executing an external
9072 * command.
9073 */
9074 if (msg_row >= Rows) /* Rows got smaller */
9075 msg_row = Rows - 1; /* put cursor at last row */
9076 else if (Rows > old_Rows) /* Rows got bigger */
9077 msg_row += Rows - old_Rows; /* put cursor in same place */
9078 if (msg_col >= Columns) /* Columns got smaller */
9079 msg_col = Columns - 1; /* put cursor at last column */
9080 }
9081#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009082 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009085 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009086
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009087 /*
9088 * Do not apply autocommands more than 3 times to avoid an endless loop
9089 * in case applying autocommands always changes Rows or Columns.
9090 */
9091 if (starting == 0 && ++retry_count <= 3)
9092 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009093 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009094 /* In rare cases, autocommands may have altered Rows or Columns,
9095 * jump back to check if we need to allocate the screen again. */
9096 goto retry;
9097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098}
9099
9100 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009101free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009102{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009103 int i;
9104
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009105 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009106 for (i = 0; i < Screen_mco; ++i)
9107 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009108 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009109 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009110 vim_free(ScreenAttrs);
9111 vim_free(LineOffset);
9112 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009113 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009114}
9115
9116 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009117screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118{
9119 check_for_delay(FALSE);
9120 screenalloc(FALSE); /* allocate screen buffers if size changed */
9121 screenclear2(); /* clear the screen */
9122}
9123
9124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009125screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126{
9127 int i;
9128
9129 if (starting == NO_SCREEN || ScreenLines == NULL
9130#ifdef FEAT_GUI
9131 || (gui.in_use && gui.starting)
9132#endif
9133 )
9134 return;
9135
9136#ifdef FEAT_GUI
9137 if (!gui.in_use)
9138#endif
9139 screen_attr = -1; /* force setting the Normal colors */
9140 screen_stop_highlight(); /* don't want highlighting here */
9141
9142#ifdef FEAT_CLIPBOARD
9143 /* disable selection without redrawing it */
9144 clip_scroll_selection(9999);
9145#endif
9146
9147 /* blank out ScreenLines */
9148 for (i = 0; i < Rows; ++i)
9149 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009150 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 LineWraps[i] = FALSE;
9152 }
9153
9154 if (can_clear(T_CL))
9155 {
9156 out_str(T_CL); /* clear the display */
9157 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009158 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 }
9160 else
9161 {
9162 /* can't clear the screen, mark all chars with invalid attributes */
9163 for (i = 0; i < Rows; ++i)
9164 lineinvalid(LineOffset[i], (int)Columns);
9165 clear_cmdline = TRUE;
9166 }
9167
9168 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9169
9170 win_rest_invalid(firstwin);
9171 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009172 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 if (must_redraw == CLEAR) /* no need to clear again */
9174 must_redraw = NOT_VALID;
9175 compute_cmdrow();
9176 msg_row = cmdline_row; /* put cursor on last line for messages */
9177 msg_col = 0;
9178 screen_start(); /* don't know where cursor is now */
9179 msg_scrolled = 0; /* can't scroll back */
9180 msg_didany = FALSE;
9181 msg_didout = FALSE;
9182}
9183
9184/*
9185 * Clear one line in ScreenLines.
9186 */
9187 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009188lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189{
9190 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 if (enc_utf8)
9192 (void)vim_memset(ScreenLinesUC + off, 0,
9193 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009194 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195}
9196
9197/*
9198 * Mark one line in ScreenLines invalid by setting the attributes to an
9199 * invalid value.
9200 */
9201 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009202lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
9204 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9205}
9206
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207/*
9208 * Copy part of a Screenline for vertically split window "wp".
9209 */
9210 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009211linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 unsigned off_to = LineOffset[to] + wp->w_wincol;
9214 unsigned off_from = LineOffset[from] + wp->w_wincol;
9215
9216 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9217 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 if (enc_utf8)
9219 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009220 int i;
9221
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9223 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009224 for (i = 0; i < p_mco; ++i)
9225 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9226 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 }
9228 if (enc_dbcs == DBCS_JPNU)
9229 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9230 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9232 wp->w_width * sizeof(sattr_T));
9233}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234
9235/*
9236 * Return TRUE if clearing with term string "p" would work.
9237 * It can't work when the string is empty or it won't set the right background.
9238 */
9239 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009240can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241{
9242 return (*p != NUL && (t_colors <= 1
9243#ifdef FEAT_GUI
9244 || gui.in_use
9245#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009246#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009247 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009248 || (!p_tgc && cterm_normal_bg_color == 0)
9249#else
9250 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009251#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009252 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253}
9254
9255/*
9256 * Reset cursor position. Use whenever cursor was moved because of outputting
9257 * something directly to the screen (shell commands) or a terminal control
9258 * code.
9259 */
9260 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009261screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
9263 screen_cur_row = screen_cur_col = 9999;
9264}
9265
9266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 * Move the cursor to position "row","col" in the screen.
9268 * This tries to find the most efficient way to move, minimizing the number of
9269 * characters sent to the terminal.
9270 */
9271 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009272windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009274 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 int i;
9276 int plan;
9277 int cost;
9278 int wouldbe_col;
9279 int noinvcurs;
9280 char_u *bs;
9281 int goto_cost;
9282 int attr;
9283
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009284#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9286
9287#define PLAN_LE 1
9288#define PLAN_CR 2
9289#define PLAN_NL 3
9290#define PLAN_WRITE 4
9291 /* Can't use ScreenLines unless initialized */
9292 if (ScreenLines == NULL)
9293 return;
9294
9295 if (col != screen_cur_col || row != screen_cur_row)
9296 {
9297 /* Check for valid position. */
9298 if (row < 0) /* window without text lines? */
9299 row = 0;
9300 if (row >= screen_Rows)
9301 row = screen_Rows - 1;
9302 if (col >= screen_Columns)
9303 col = screen_Columns - 1;
9304
9305 /* check if no cursor movement is allowed in highlight mode */
9306 if (screen_attr && *T_MS == NUL)
9307 noinvcurs = HIGHL_COST;
9308 else
9309 noinvcurs = 0;
9310 goto_cost = GOTO_COST + noinvcurs;
9311
9312 /*
9313 * Plan how to do the positioning:
9314 * 1. Use CR to move it to column 0, same row.
9315 * 2. Use T_LE to move it a few columns to the left.
9316 * 3. Use NL to move a few lines down, column 0.
9317 * 4. Move a few columns to the right with T_ND or by writing chars.
9318 *
9319 * Don't do this if the cursor went beyond the last column, the cursor
9320 * position is unknown then (some terminals wrap, some don't )
9321 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009322 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 * characters to move the cursor to the right.
9324 */
9325 if (row >= screen_cur_row && screen_cur_col < Columns)
9326 {
9327 /*
9328 * If the cursor is in the same row, bigger col, we can use CR
9329 * or T_LE.
9330 */
9331 bs = NULL; /* init for GCC */
9332 attr = screen_attr;
9333 if (row == screen_cur_row && col < screen_cur_col)
9334 {
9335 /* "le" is preferred over "bc", because "bc" is obsolete */
9336 if (*T_LE)
9337 bs = T_LE; /* "cursor left" */
9338 else
9339 bs = T_BC; /* "backspace character (old) */
9340 if (*bs)
9341 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9342 else
9343 cost = 999;
9344 if (col + 1 < cost) /* using CR is less characters */
9345 {
9346 plan = PLAN_CR;
9347 wouldbe_col = 0;
9348 cost = 1; /* CR is just one character */
9349 }
9350 else
9351 {
9352 plan = PLAN_LE;
9353 wouldbe_col = col;
9354 }
9355 if (noinvcurs) /* will stop highlighting */
9356 {
9357 cost += noinvcurs;
9358 attr = 0;
9359 }
9360 }
9361
9362 /*
9363 * If the cursor is above where we want to be, we can use CR LF.
9364 */
9365 else if (row > screen_cur_row)
9366 {
9367 plan = PLAN_NL;
9368 wouldbe_col = 0;
9369 cost = (row - screen_cur_row) * 2; /* CR LF */
9370 if (noinvcurs) /* will stop highlighting */
9371 {
9372 cost += noinvcurs;
9373 attr = 0;
9374 }
9375 }
9376
9377 /*
9378 * If the cursor is in the same row, smaller col, just use write.
9379 */
9380 else
9381 {
9382 plan = PLAN_WRITE;
9383 wouldbe_col = screen_cur_col;
9384 cost = 0;
9385 }
9386
9387 /*
9388 * Check if any characters that need to be written have the
9389 * correct attributes. Also avoid UTF-8 characters.
9390 */
9391 i = col - wouldbe_col;
9392 if (i > 0)
9393 cost += i;
9394 if (cost < goto_cost && i > 0)
9395 {
9396 /*
9397 * Check if the attributes are correct without additionally
9398 * stopping highlighting.
9399 */
9400 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9401 while (i && *p++ == attr)
9402 --i;
9403 if (i != 0)
9404 {
9405 /*
9406 * Try if it works when highlighting is stopped here.
9407 */
9408 if (*--p == 0)
9409 {
9410 cost += noinvcurs;
9411 while (i && *p++ == 0)
9412 --i;
9413 }
9414 if (i != 0)
9415 cost = 999; /* different attributes, don't do it */
9416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 if (enc_utf8)
9418 {
9419 /* Don't use an UTF-8 char for positioning, it's slow. */
9420 for (i = wouldbe_col; i < col; ++i)
9421 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9422 {
9423 cost = 999;
9424 break;
9425 }
9426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 }
9428
9429 /*
9430 * We can do it without term_windgoto()!
9431 */
9432 if (cost < goto_cost)
9433 {
9434 if (plan == PLAN_LE)
9435 {
9436 if (noinvcurs)
9437 screen_stop_highlight();
9438 while (screen_cur_col > col)
9439 {
9440 out_str(bs);
9441 --screen_cur_col;
9442 }
9443 }
9444 else if (plan == PLAN_CR)
9445 {
9446 if (noinvcurs)
9447 screen_stop_highlight();
9448 out_char('\r');
9449 screen_cur_col = 0;
9450 }
9451 else if (plan == PLAN_NL)
9452 {
9453 if (noinvcurs)
9454 screen_stop_highlight();
9455 while (screen_cur_row < row)
9456 {
9457 out_char('\n');
9458 ++screen_cur_row;
9459 }
9460 screen_cur_col = 0;
9461 }
9462
9463 i = col - screen_cur_col;
9464 if (i > 0)
9465 {
9466 /*
9467 * Use cursor-right if it's one character only. Avoids
9468 * removing a line of pixels from the last bold char, when
9469 * using the bold trick in the GUI.
9470 */
9471 if (T_ND[0] != NUL && T_ND[1] == NUL)
9472 {
9473 while (i-- > 0)
9474 out_char(*T_ND);
9475 }
9476 else
9477 {
9478 int off;
9479
9480 off = LineOffset[row] + screen_cur_col;
9481 while (i-- > 0)
9482 {
9483 if (ScreenAttrs[off] != screen_attr)
9484 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 if (enc_dbcs == DBCS_JPNU
9488 && ScreenLines[off] == 0x8e)
9489 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 ++off;
9491 }
9492 }
9493 }
9494 }
9495 }
9496 else
9497 cost = 999;
9498
9499 if (cost >= goto_cost)
9500 {
9501 if (noinvcurs)
9502 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009503 if (row == screen_cur_row && (col > screen_cur_col)
9504 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 term_cursor_right(col - screen_cur_col);
9506 else
9507 term_windgoto(row, col);
9508 }
9509 screen_cur_row = row;
9510 screen_cur_col = col;
9511 }
9512}
9513
9514/*
9515 * Set cursor to its position in the current window.
9516 */
9517 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009518setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009520 setcursor_mayforce(FALSE);
9521}
9522
9523/*
9524 * Set cursor to its position in the current window.
9525 * When "force" is TRUE also when not redrawing.
9526 */
9527 void
9528setcursor_mayforce(int force)
9529{
9530 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 {
9532 validate_cursor();
9533 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009534 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009536 /* With 'rightleft' set and the cursor on a double-wide
9537 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009538 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9539 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009540 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009541 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542#endif
9543 curwin->w_wcol));
9544 }
9545}
9546
9547
9548/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009549 * Insert 'line_count' lines at 'row' in window 'wp'.
9550 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9551 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 * scrolling.
9553 * Returns FAIL if the lines are not inserted, OK for success.
9554 */
9555 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009556win_ins_lines(
9557 win_T *wp,
9558 int row,
9559 int line_count,
9560 int invalid,
9561 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 int did_delete;
9564 int nextrow;
9565 int lastrow;
9566 int retval;
9567
9568 if (invalid)
9569 wp->w_lines_valid = 0;
9570
9571 if (wp->w_height < 5)
9572 return FAIL;
9573
9574 if (line_count > wp->w_height - row)
9575 line_count = wp->w_height - row;
9576
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009577 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 if (retval != MAYBE)
9579 return retval;
9580
9581 /*
9582 * If there is a next window or a status line, we first try to delete the
9583 * lines at the bottom to avoid messing what is after the window.
9584 * If this fails and there are following windows, don't do anything to avoid
9585 * messing up those windows, better just redraw.
9586 */
9587 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 if (wp->w_next != NULL || wp->w_status_height)
9589 {
9590 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009591 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 did_delete = TRUE;
9593 else if (wp->w_next)
9594 return FAIL;
9595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 /*
9597 * if no lines deleted, blank the lines that will end up below the window
9598 */
9599 if (!did_delete)
9600 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009603 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 lastrow = nextrow + line_count;
9605 if (lastrow > Rows)
9606 lastrow = Rows;
9607 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009608 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 ' ', ' ', 0);
9610 }
9611
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009612 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 == FAIL)
9614 {
9615 /* deletion will have messed up other windows */
9616 if (did_delete)
9617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 win_rest_invalid(W_NEXT(wp));
9620 }
9621 return FAIL;
9622 }
9623
9624 return OK;
9625}
9626
9627/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009628 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9630 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9631 * scrolling
9632 * Return OK for success, FAIL if the lines are not deleted.
9633 */
9634 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009635win_del_lines(
9636 win_T *wp,
9637 int row,
9638 int line_count,
9639 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009640 int mayclear,
9641 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642{
9643 int retval;
9644
9645 if (invalid)
9646 wp->w_lines_valid = 0;
9647
9648 if (line_count > wp->w_height - row)
9649 line_count = wp->w_height - row;
9650
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009651 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 if (retval != MAYBE)
9653 return retval;
9654
9655 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009656 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 return FAIL;
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 /*
9660 * If there are windows or status lines below, try to put them at the
9661 * correct place. If we can't do that, they have to be redrawn.
9662 */
9663 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9664 {
9665 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009666 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 {
9668 wp->w_redr_status = TRUE;
9669 win_rest_invalid(wp->w_next);
9670 }
9671 }
9672 /*
9673 * If this is the last window and there is no status line, redraw the
9674 * command line later.
9675 */
9676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 redraw_cmdline = TRUE;
9678 return OK;
9679}
9680
9681/*
9682 * Common code for win_ins_lines() and win_del_lines().
9683 * Returns OK or FAIL when the work has been done.
9684 * Returns MAYBE when not finished yet.
9685 */
9686 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009687win_do_lines(
9688 win_T *wp,
9689 int row,
9690 int line_count,
9691 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009692 int del,
9693 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695 int retval;
9696
9697 if (!redrawing() || line_count <= 0)
9698 return FAIL;
9699
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009700 /* When inserting lines would result in loss of command output, just redraw
9701 * the lines. */
9702 if (no_win_do_lines_ins && !del)
9703 return FAIL;
9704
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009706 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009708 if (!no_win_do_lines_ins)
9709 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 return FAIL;
9711 }
9712
9713 /*
9714 * Delete all remaining lines
9715 */
9716 if (row + line_count >= wp->w_height)
9717 {
9718 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009719 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 ' ', ' ', 0);
9721 return OK;
9722 }
9723
9724 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009725 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009727 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009729 if (!no_win_do_lines_ins)
9730 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731
9732 /*
9733 * If the terminal can set a scroll region, use that.
9734 * Always do this in a vertically split window. This will redraw from
9735 * ScreenLines[] when t_CV isn't defined. That's faster than using
9736 * win_line().
9737 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009738 * a character in the lower right corner of the scroll region may cause a
9739 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009741 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 scroll_region_set(wp, row);
9745 if (del)
9746 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009747 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 else
9749 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009750 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 scroll_region_reset();
9753 return retval;
9754 }
9755
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758
9759 return MAYBE;
9760}
9761
9762/*
9763 * window 'wp' and everything after it is messed up, mark it for redraw
9764 */
9765 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009766win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 {
9770 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 wp->w_redr_status = TRUE;
9772 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 }
9774 redraw_cmdline = TRUE;
9775}
9776
9777/*
9778 * The rest of the routines in this file perform screen manipulations. The
9779 * given operation is performed physically on the screen. The corresponding
9780 * change is also made to the internal screen image. In this way, the editor
9781 * anticipates the effect of editing changes on the appearance of the screen.
9782 * That way, when we call screenupdate a complete redraw isn't usually
9783 * necessary. Another advantage is that we can keep adding code to anticipate
9784 * screen changes, and in the meantime, everything still works.
9785 */
9786
9787/*
9788 * types for inserting or deleting lines
9789 */
9790#define USE_T_CAL 1
9791#define USE_T_CDL 2
9792#define USE_T_AL 3
9793#define USE_T_CE 4
9794#define USE_T_DL 5
9795#define USE_T_SR 6
9796#define USE_NL 7
9797#define USE_T_CD 8
9798#define USE_REDRAW 9
9799
9800/*
9801 * insert lines on the screen and update ScreenLines[]
9802 * 'end' is the line after the scrolled part. Normally it is Rows.
9803 * When scrolling region used 'off' is the offset from the top for the region.
9804 * 'row' and 'end' are relative to the start of the region.
9805 *
9806 * return FAIL for failure, OK for success.
9807 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009808 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009809screen_ins_lines(
9810 int off,
9811 int row,
9812 int line_count,
9813 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009814 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009815 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816{
9817 int i;
9818 int j;
9819 unsigned temp;
9820 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009821 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 int type;
9823 int result_empty;
9824 int can_ce = can_clear(T_CE);
9825
9826 /*
9827 * FAIL if
9828 * - there is no valid screen
9829 * - the screen has to be redrawn completely
9830 * - the line count is less than one
9831 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009832 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009834 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9835#ifdef FEAT_CLIPBOARD
9836 || (clip_star.state != SELECT_CLEARED
9837 && redrawing_for_callback > 0)
9838#endif
9839 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 return FAIL;
9841
9842 /*
9843 * There are seven ways to insert lines:
9844 * 0. When in a vertically split window and t_CV isn't set, redraw the
9845 * characters from ScreenLines[].
9846 * 1. Use T_CD (clear to end of display) if it exists and the result of
9847 * the insert is just empty lines
9848 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9849 * present or line_count > 1. It looks better if we do all the inserts
9850 * at once.
9851 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9852 * insert is just empty lines and T_CE is not present or line_count >
9853 * 1.
9854 * 4. Use T_AL (insert line) if it exists.
9855 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9856 * just empty lines.
9857 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9858 * just empty lines.
9859 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9860 * the 'da' flag is not set or we have clear line capability.
9861 * 8. redraw the characters from ScreenLines[].
9862 *
9863 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9864 * the scrollbar for the window. It does have insert line, use that if it
9865 * exists.
9866 */
9867 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9869 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009870 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 type = USE_T_CD;
9872 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9873 type = USE_T_CAL;
9874 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9875 type = USE_T_CDL;
9876 else if (*T_AL != NUL)
9877 type = USE_T_AL;
9878 else if (can_ce && result_empty)
9879 type = USE_T_CE;
9880 else if (*T_DL != NUL && result_empty)
9881 type = USE_T_DL;
9882 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9883 type = USE_T_SR;
9884 else
9885 return FAIL;
9886
9887 /*
9888 * For clearing the lines screen_del_lines() is used. This will also take
9889 * care of t_db if necessary.
9890 */
9891 if (type == USE_T_CD || type == USE_T_CDL ||
9892 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009893 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
9895 /*
9896 * If text is retained below the screen, first clear or delete as many
9897 * lines at the bottom of the window as are about to be inserted so that
9898 * the deleted lines won't later surface during a screen_del_lines.
9899 */
9900 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009901 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903#ifdef FEAT_CLIPBOARD
9904 /* Remove a modeless selection when inserting lines halfway the screen
9905 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009906 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009907 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 else
9909 clip_scroll_selection(-line_count);
9910#endif
9911
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#ifdef FEAT_GUI
9913 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9914 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009915 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916#endif
9917
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009918 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9919 cursor_col = wp->w_wincol;
9920
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (*T_CCS != NUL) /* cursor relative to region */
9922 cursor_row = row;
9923 else
9924 cursor_row = row + off;
9925
9926 /*
9927 * Shift LineOffset[] line_count down to reflect the inserted lines.
9928 * Clear the inserted lines in ScreenLines[].
9929 */
9930 row += off;
9931 end += off;
9932 for (i = 0; i < line_count; ++i)
9933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 if (wp != NULL && wp->w_width != Columns)
9935 {
9936 /* need to copy part of a line */
9937 j = end - 1 - i;
9938 while ((j -= line_count) >= row)
9939 linecopy(j + line_count, j, wp);
9940 j += line_count;
9941 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009942 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9943 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 else
9945 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9946 LineWraps[j] = FALSE;
9947 }
9948 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 {
9950 j = end - 1 - i;
9951 temp = LineOffset[j];
9952 while ((j -= line_count) >= row)
9953 {
9954 LineOffset[j + line_count] = LineOffset[j];
9955 LineWraps[j + line_count] = LineWraps[j];
9956 }
9957 LineOffset[j + line_count] = temp;
9958 LineWraps[j + line_count] = FALSE;
9959 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009960 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 else
9962 lineinvalid(temp, (int)Columns);
9963 }
9964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
9966 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009967 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009968 if (clear_attr != 0)
9969 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 /* redraw the characters */
9972 if (type == USE_REDRAW)
9973 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009974 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 {
9976 term_append_lines(line_count);
9977 screen_start(); /* don't know where cursor is now */
9978 }
9979 else
9980 {
9981 for (i = 0; i < line_count; i++)
9982 {
9983 if (type == USE_T_AL)
9984 {
9985 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009986 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987 out_str(T_AL);
9988 }
9989 else /* type == USE_T_SR */
9990 out_str(T_SR);
9991 screen_start(); /* don't know where cursor is now */
9992 }
9993 }
9994
9995 /*
9996 * With scroll-reverse and 'da' flag set we need to clear the lines that
9997 * have been scrolled down into the region.
9998 */
9999 if (type == USE_T_SR && *T_DA)
10000 {
10001 for (i = 0; i < line_count; ++i)
10002 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010003 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 out_str(T_CE);
10005 screen_start(); /* don't know where cursor is now */
10006 }
10007 }
10008
10009#ifdef FEAT_GUI
10010 gui_can_update_cursor();
10011 if (gui.in_use)
10012 out_flush(); /* always flush after a scroll */
10013#endif
10014 return OK;
10015}
10016
10017/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010018 * Delete lines on the screen and update ScreenLines[].
10019 * "end" is the line after the scrolled part. Normally it is Rows.
10020 * When scrolling region used "off" is the offset from the top for the region.
10021 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 *
10023 * Return OK for success, FAIL if the lines are not deleted.
10024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010026screen_del_lines(
10027 int off,
10028 int row,
10029 int line_count,
10030 int end,
10031 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010032 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010033 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035 int j;
10036 int i;
10037 unsigned temp;
10038 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010039 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 int cursor_end;
10041 int result_empty; /* result is empty until end of region */
10042 int can_delete; /* deleting line codes can be used */
10043 int type;
10044
10045 /*
10046 * FAIL if
10047 * - there is no valid screen
10048 * - the screen has to be redrawn completely
10049 * - the line count is less than one
10050 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010051 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010053 if (!screen_valid(TRUE) || line_count <= 0
10054 || (!force && line_count > p_ttyscroll)
10055#ifdef FEAT_CLIPBOARD
10056 || (clip_star.state != SELECT_CLEARED
10057 && redrawing_for_callback > 0)
10058#endif
10059 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 return FAIL;
10061
10062 /*
10063 * Check if the rest of the current region will become empty.
10064 */
10065 result_empty = row + line_count >= end;
10066
10067 /*
10068 * We can delete lines only when 'db' flag not set or when 'ce' option
10069 * available.
10070 */
10071 can_delete = (*T_DB == NUL || can_clear(T_CE));
10072
10073 /*
10074 * There are six ways to delete lines:
10075 * 0. When in a vertically split window and t_CV isn't set, redraw the
10076 * characters from ScreenLines[].
10077 * 1. Use T_CD if it exists and the result is empty.
10078 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10079 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10080 * none of the other ways work.
10081 * 4. Use T_CE (erase line) if the result is empty.
10082 * 5. Use T_DL (delete line) if it exists.
10083 * 6. redraw the characters from ScreenLines[].
10084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10086 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010087 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 type = USE_T_CD;
10089#if defined(__BEOS__) && defined(BEOS_DR8)
10090 /*
10091 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10092 * its internal termcap... this works okay for tests which test *T_DB !=
10093 * NUL. It has the disadvantage that the user cannot use any :set t_*
10094 * command to get T_DB (back) to empty_option, only :set term=... will do
10095 * the trick...
10096 * Anyway, this hack will hopefully go away with the next OS release.
10097 * (Olaf Seibert)
10098 */
10099 else if (row == 0 && T_DB == empty_option
10100 && (line_count == 1 || *T_CDL == NUL))
10101#else
10102 else if (row == 0 && (
10103#ifndef AMIGA
10104 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10105 * up, so use delete-line command */
10106 line_count == 1 ||
10107#endif
10108 *T_CDL == NUL))
10109#endif
10110 type = USE_NL;
10111 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10112 type = USE_T_CDL;
10113 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010114 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 type = USE_T_CE;
10116 else if (*T_DL != NUL && can_delete)
10117 type = USE_T_DL;
10118 else if (*T_CDL != NUL && can_delete)
10119 type = USE_T_CDL;
10120 else
10121 return FAIL;
10122
10123#ifdef FEAT_CLIPBOARD
10124 /* Remove a modeless selection when deleting lines halfway the screen or
10125 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010126 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010127 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 else
10129 clip_scroll_selection(line_count);
10130#endif
10131
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#ifdef FEAT_GUI
10133 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10134 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010135 gui_dont_update_cursor(gui.cursor_row >= row + off
10136 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137#endif
10138
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010139 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10140 cursor_col = wp->w_wincol;
10141
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (*T_CCS != NUL) /* cursor relative to region */
10143 {
10144 cursor_row = row;
10145 cursor_end = end;
10146 }
10147 else
10148 {
10149 cursor_row = row + off;
10150 cursor_end = end + off;
10151 }
10152
10153 /*
10154 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10155 * Clear the inserted lines in ScreenLines[].
10156 */
10157 row += off;
10158 end += off;
10159 for (i = 0; i < line_count; ++i)
10160 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 if (wp != NULL && wp->w_width != Columns)
10162 {
10163 /* need to copy part of a line */
10164 j = row + i;
10165 while ((j += line_count) <= end - 1)
10166 linecopy(j - line_count, j, wp);
10167 j -= line_count;
10168 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010169 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10170 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 else
10172 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10173 LineWraps[j] = FALSE;
10174 }
10175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 {
10177 /* whole width, moving the line pointers is faster */
10178 j = row + i;
10179 temp = LineOffset[j];
10180 while ((j += line_count) <= end - 1)
10181 {
10182 LineOffset[j - line_count] = LineOffset[j];
10183 LineWraps[j - line_count] = LineWraps[j];
10184 }
10185 LineOffset[j - line_count] = temp;
10186 LineWraps[j - line_count] = FALSE;
10187 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010188 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 else
10190 lineinvalid(temp, (int)Columns);
10191 }
10192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010194 if (screen_attr != clear_attr)
10195 screen_stop_highlight();
10196 if (clear_attr != 0)
10197 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 /* redraw the characters */
10200 if (type == USE_REDRAW)
10201 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010202 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010204 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 out_str(T_CD);
10206 screen_start(); /* don't know where cursor is now */
10207 }
10208 else if (type == USE_T_CDL)
10209 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010210 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 term_delete_lines(line_count);
10212 screen_start(); /* don't know where cursor is now */
10213 }
10214 /*
10215 * Deleting lines at top of the screen or scroll region: Just scroll
10216 * the whole screen (scroll region) up by outputting newlines on the
10217 * last line.
10218 */
10219 else if (type == USE_NL)
10220 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010221 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 for (i = line_count; --i >= 0; )
10223 out_char('\n'); /* cursor will remain on same line */
10224 }
10225 else
10226 {
10227 for (i = line_count; --i >= 0; )
10228 {
10229 if (type == USE_T_DL)
10230 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010231 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 out_str(T_DL); /* delete a line */
10233 }
10234 else /* type == USE_T_CE */
10235 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010236 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 out_str(T_CE); /* erase a line */
10238 }
10239 screen_start(); /* don't know where cursor is now */
10240 }
10241 }
10242
10243 /*
10244 * If the 'db' flag is set, we need to clear the lines that have been
10245 * scrolled up at the bottom of the region.
10246 */
10247 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10248 {
10249 for (i = line_count; i > 0; --i)
10250 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010251 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 out_str(T_CE); /* erase a line */
10253 screen_start(); /* don't know where cursor is now */
10254 }
10255 }
10256
10257#ifdef FEAT_GUI
10258 gui_can_update_cursor();
10259 if (gui.in_use)
10260 out_flush(); /* always flush after a scroll */
10261#endif
10262
10263 return OK;
10264}
10265
10266/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010267 * Return TRUE when postponing displaying the mode message: when not redrawing
10268 * or inside a mapping.
10269 */
10270 int
10271skip_showmode()
10272{
10273 // Call char_avail() only when we are going to show something, because it
10274 // takes a bit of time. redrawing() may also call char_avail_avail().
10275 if (global_busy
10276 || msg_silent != 0
10277 || !redrawing()
10278 || (char_avail() && !KeyTyped))
10279 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010280 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010281 return TRUE;
10282 }
10283 return FALSE;
10284}
10285
10286/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010287 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 *
10289 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10290 * If clear_cmdline is FALSE there may be a message there that needs to be
10291 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010292 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 * Return the length of the message (0 if no message).
10294 */
10295 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010296showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297{
10298 int need_clear;
10299 int length = 0;
10300 int do_mode;
10301 int attr;
10302 int nwr_save;
10303#ifdef FEAT_INS_EXPAND
10304 int sub_attr;
10305#endif
10306
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010307 do_mode = ((p_smd && msg_silent == 0)
10308 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010309 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010310 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010311 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010313 if (skip_showmode())
10314 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315
10316 nwr_save = need_wait_return;
10317
10318 /* wait a bit before overwriting an important message */
10319 check_for_delay(FALSE);
10320
10321 /* if the cmdline is more than one line high, erase top lines */
10322 need_clear = clear_cmdline;
10323 if (clear_cmdline && cmdline_row < Rows - 1)
10324 msg_clr_cmdline(); /* will reset clear_cmdline */
10325
10326 /* Position on the last line in the window, column 0 */
10327 msg_pos_mode();
10328 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010329 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 if (do_mode)
10331 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010332 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010335# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010336 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010337# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010338 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010339# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010340 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010341# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010342 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010344 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345# endif
10346#endif
10347#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10348 if (gui.in_use)
10349 {
10350 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010351 {
10352 /* HANGUL */
10353 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010354 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010355 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010356 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359#endif
10360#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010361 /* CTRL-X in Insert mode */
10362 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
10364 /* These messages can get long, avoid a wrap in a narrow
10365 * window. Prefer showing edit_submode_extra. */
10366 length = (Rows - msg_row) * Columns - 3;
10367 if (edit_submode_extra != NULL)
10368 length -= vim_strsize(edit_submode_extra);
10369 if (length > 0)
10370 {
10371 if (edit_submode_pre != NULL)
10372 length -= vim_strsize(edit_submode_pre);
10373 if (length - vim_strsize(edit_submode) > 0)
10374 {
10375 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010376 msg_puts_attr((char *)edit_submode_pre, attr);
10377 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 }
10379 if (edit_submode_extra != NULL)
10380 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010381 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010383 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 else
10385 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010386 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 }
10388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 }
10390 else
10391#endif
10392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010394 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010395 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010396 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 else if (State & INSERT)
10398 {
10399#ifdef FEAT_RIGHTLEFT
10400 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010401 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010403 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010405 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010406 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010408 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010410 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411#ifdef FEAT_RIGHTLEFT
10412 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010413 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414#endif
10415#ifdef FEAT_KEYMAP
10416 if (State & LANGMAP)
10417 {
10418# ifdef FEAT_ARABIC
10419 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010420 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 else
10422# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010423 if (get_keymap_str(curwin, (char_u *)" (%s)",
10424 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010425 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 }
10427#endif
10428 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010429 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 if (VIsual_active)
10432 {
10433 char *p;
10434
10435 /* Don't concatenate separate words to avoid translation
10436 * problems. */
10437 switch ((VIsual_select ? 4 : 0)
10438 + (VIsual_mode == Ctrl_V) * 2
10439 + (VIsual_mode == 'V'))
10440 {
10441 case 0: p = N_(" VISUAL"); break;
10442 case 1: p = N_(" VISUAL LINE"); break;
10443 case 2: p = N_(" VISUAL BLOCK"); break;
10444 case 4: p = N_(" SELECT"); break;
10445 case 5: p = N_(" SELECT LINE"); break;
10446 default: p = N_(" SELECT BLOCK"); break;
10447 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010448 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010450 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010452
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 need_clear = TRUE;
10454 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010455 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456#ifdef FEAT_INS_EXPAND
10457 && edit_submode == NULL /* otherwise it gets too long */
10458#endif
10459 )
10460 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010461 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 need_clear = TRUE;
10463 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010464
10465 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010466 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 msg_clr_eos();
10468 msg_didout = FALSE; /* overwrite this message */
10469 length = msg_col;
10470 msg_col = 0;
10471 need_wait_return = nwr_save; /* never ask for hit-return for this */
10472 }
10473 else if (clear_cmdline && msg_silent == 0)
10474 /* Clear the whole command line. Will reset "clear_cmdline". */
10475 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010476 else if (redraw_mode)
10477 {
10478 msg_pos_mode();
10479 msg_clr_eos();
10480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481
10482#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 /* In Visual mode the size of the selected area must be redrawn. */
10484 if (VIsual_active)
10485 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486
10487 /* If the last window has no status line, the ruler is after the mode
10488 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010489 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010490 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491#endif
10492 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010493 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 clear_cmdline = FALSE;
10495
10496 return length;
10497}
10498
10499/*
10500 * Position for a mode message.
10501 */
10502 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010503msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504{
10505 msg_col = 0;
10506 msg_row = Rows - 1;
10507}
10508
10509/*
10510 * Delete mode message. Used when ESC is typed which is expected to end
10511 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010512 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 */
10514 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010515unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010518 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 */
10520 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10521 redraw_cmdline = TRUE; /* delete mode later */
10522 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010523 clearmode();
10524}
10525
10526/*
10527 * Clear the mode message.
10528 */
10529 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010530clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010531{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010532 int save_msg_row = msg_row;
10533 int save_msg_col = msg_col;
10534
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010535 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010536 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010537 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010538 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010539
10540 msg_col = save_msg_col;
10541 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542}
10543
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010545recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010546{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010547 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010548 if (!shortmess(SHM_RECORDING))
10549 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010550 char s[4];
10551
10552 sprintf(s, " @%c", reg_recording);
10553 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010554 }
10555}
10556
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010557/*
10558 * Draw the tab pages line at the top of the Vim window.
10559 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010560 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010561draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010562{
10563 int tabcount = 0;
10564 tabpage_T *tp;
10565 int tabwidth;
10566 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010567 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010568 int attr;
10569 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010570 win_T *cwp;
10571 int wincount;
10572 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010573 int c;
10574 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010575 int attr_sel = HL_ATTR(HLF_TPS);
10576 int attr_nosel = HL_ATTR(HLF_TP);
10577 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010578 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010579 int room;
10580 int use_sep_chars = (t_colors < 8
10581#ifdef FEAT_GUI
10582 && !gui.in_use
10583#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010584#ifdef FEAT_TERMGUICOLORS
10585 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010586#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010587 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010588
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010589 if (ScreenLines == NULL)
10590 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010591 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010592
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010593#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010594 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010595 if (gui_use_tabline())
10596 {
10597 gui_update_tabline();
10598 return;
10599 }
10600#endif
10601
10602 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010603 return;
10604
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010605#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010606 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010607
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010608 /* Use the 'tabline' option if it's set. */
10609 if (*p_tal != NUL)
10610 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010611 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612
10613 /* Check for an error. If there is one we would loop in redrawing the
10614 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010615 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010616 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010617 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010619 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010620 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010621 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010622 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010623#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010624 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010625 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010627
Bram Moolenaar238a5642006-02-21 22:12:05 +000010628 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10629 if (tabwidth < 6)
10630 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010631
Bram Moolenaar238a5642006-02-21 22:12:05 +000010632 attr = attr_nosel;
10633 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010634 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10635 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010636 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 if (tp->tp_topframe == topframe)
10640 attr = attr_sel;
10641 if (use_sep_chars && col > 0)
10642 screen_putchar('|', 0, col++, attr);
10643
10644 if (tp->tp_topframe != topframe)
10645 attr = attr_nosel;
10646
10647 screen_putchar(' ', 0, col++, attr);
10648
10649 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010650 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010651 cwp = curwin;
10652 wp = firstwin;
10653 }
10654 else
10655 {
10656 cwp = tp->tp_curwin;
10657 wp = tp->tp_firstwin;
10658 }
10659
10660 modified = FALSE;
10661 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10662 if (bufIsChanged(wp->w_buffer))
10663 modified = TRUE;
10664 if (modified || wincount > 1)
10665 {
10666 if (wincount > 1)
10667 {
10668 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010669 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010670 if (col + len >= Columns - 3)
10671 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010672 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010673#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010674 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010675#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010676 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010677#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010678 );
10679 col += len;
10680 }
10681 if (modified)
10682 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10683 screen_putchar(' ', 0, col++, attr);
10684 }
10685
10686 room = scol - col + tabwidth - 1;
10687 if (room > 0)
10688 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010689 /* Get buffer name in NameBuff[] */
10690 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010691 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010692 len = vim_strsize(NameBuff);
10693 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694 if (has_mbyte)
10695 while (len > room)
10696 {
10697 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010698 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010699 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010700 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010701 {
10702 p += len - room;
10703 len = room;
10704 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010705 if (len > Columns - col - 1)
10706 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010708 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010709 col += len;
10710 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010711 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010712
10713 /* Store the tab page number in TabPageIdxs[], so that
10714 * jump_to_mouse() knows where each one is. */
10715 ++tabcount;
10716 while (scol < col)
10717 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010718 }
10719
Bram Moolenaar238a5642006-02-21 22:12:05 +000010720 if (use_sep_chars)
10721 c = '_';
10722 else
10723 c = ' ';
10724 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010725
10726 /* Put an "X" for closing the current tab if there are several. */
10727 if (first_tabpage->tp_next != NULL)
10728 {
10729 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10730 TabPageIdxs[Columns - 1] = -999;
10731 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010732 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010733
10734 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10735 * set. */
10736 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010737}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010738
10739/*
10740 * Get buffer name for "buf" into NameBuff[].
10741 * Takes care of special buffer names and translates special characters.
10742 */
10743 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010744get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010745{
10746 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010747 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010748 else
10749 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10750 trans_characters(NameBuff, MAXPATHL);
10751}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010752
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753/*
10754 * Get the character to use in a status line. Get its attributes in "*attr".
10755 */
10756 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010757fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758{
10759 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010760
10761#ifdef FEAT_TERMINAL
10762 if (bt_terminal(wp->w_buffer))
10763 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010764 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010765 {
10766 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010767 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010768 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010769 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010770 {
10771 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010772 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010773 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774 }
10775 else
10776#endif
10777 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010779 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 fill = fill_stl;
10781 }
10782 else
10783 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010784 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 fill = fill_stlnc;
10786 }
10787 /* Use fill when there is highlighting, and highlighting of current
10788 * window differs, or the fillchars differ, or this is not the
10789 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010790 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010791 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 || (fill_stl != fill_stlnc)))
10793 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010794 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 return '^';
10796 return '=';
10797}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799/*
10800 * Get the character to use in a separator between vertically split windows.
10801 * Get its attributes in "*attr".
10802 */
10803 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010804fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010806 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 if (*attr == 0 && fill_vert == ' ')
10808 return '|';
10809 else
10810 return fill_vert;
10811}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812
10813/*
10814 * Return TRUE if redrawing should currently be done.
10815 */
10816 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010817redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010819#ifdef FEAT_EVAL
10820 if (disable_redraw_for_testing)
10821 return 0;
10822 else
10823#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010824 return ((!RedrawingDisabled
10825#ifdef FEAT_EVAL
10826 || ignore_redraw_flag_for_testing
10827#endif
10828 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829}
10830
10831/*
10832 * Return TRUE if printing messages should currently be done.
10833 */
10834 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010835messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837 return (!(p_lz && char_avail() && !KeyTyped));
10838}
10839
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010840#ifdef FEAT_MENU
10841/*
10842 * Draw the window toolbar.
10843 */
10844 static void
10845redraw_win_toolbar(win_T *wp)
10846{
10847 vimmenu_T *menu;
10848 int item_idx = 0;
10849 int item_count = 0;
10850 int col = 0;
10851 int next_col;
10852 int off = (int)(current_ScreenLine - ScreenLines);
10853 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10854 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10855
10856 vim_free(wp->w_winbar_items);
10857 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10858 ++item_count;
10859 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
Bram Moolenaar18a4ba22019-05-24 19:39:03 +020010860 sizeof(winbar_item_T) * (item_count + 1));
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010861
10862 /* TODO: use fewer spaces if there is not enough room */
10863 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010864 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010865 {
10866 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010867 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010868 break;
10869 if (col > 1)
10870 {
10871 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010872 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010873 break;
10874 }
10875
10876 wp->w_winbar_items[item_idx].wb_startcol = col;
10877 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010878 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010879 break;
10880
10881 next_col = text_to_screenline(wp, menu->name, col);
10882 while (col < next_col)
10883 {
10884 ScreenAttrs[off + col] = button_attr;
10885 ++col;
10886 }
10887 wp->w_winbar_items[item_idx].wb_endcol = col;
10888 wp->w_winbar_items[item_idx].wb_menu = menu;
10889 ++item_idx;
10890
Bram Moolenaar02631462017-09-22 15:20:32 +020010891 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010892 break;
10893 space_to_screenline(off + col, button_attr);
10894 ++col;
10895 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010896 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010897 {
10898 space_to_screenline(off + col, fill_attr);
10899 ++col;
10900 }
10901 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10902
Bram Moolenaar02631462017-09-22 15:20:32 +020010903 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010904 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010905}
10906#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010907
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908/*
10909 * Show current status info in ruler and various other places
10910 * If always is FALSE, only show ruler if position has changed.
10911 */
10912 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010913showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914{
10915 if (!always && !redrawing())
10916 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010917#ifdef FEAT_INS_EXPAND
10918 if (pum_visible())
10919 {
10920 /* Don't redraw right now, do it later. */
10921 curwin->w_redr_status = TRUE;
10922 return;
10923 }
10924#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010925#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010926 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010927 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 else
10929#endif
10930#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010931 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#endif
10933
10934#ifdef FEAT_TITLE
10935 if (need_maketitle
10936# ifdef FEAT_STL_OPT
10937 || (p_icon && (stl_syntax & STL_IN_ICON))
10938 || (p_title && (stl_syntax & STL_IN_TITLE))
10939# endif
10940 )
10941 maketitle();
10942#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010943 /* Redraw the tab pages line if needed. */
10944 if (redraw_tabline)
10945 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946}
10947
10948#ifdef FEAT_CMDL_INFO
10949 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010950win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010952#define RULER_BUF_LEN 70
10953 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 int row;
10955 int fillchar;
10956 int attr;
10957 int empty_line = FALSE;
10958 colnr_T virtcol;
10959 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010960 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 int this_ru_col;
10963 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010964 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966 /* If 'ruler' off or redrawing disabled, don't do anything */
10967 if (!p_ru)
10968 return;
10969
10970 /*
10971 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10972 * after deleting lines, before cursor.lnum is corrected.
10973 */
10974 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10975 return;
10976
10977#ifdef FEAT_INS_EXPAND
10978 /* Don't draw the ruler while doing insert-completion, it might overwrite
10979 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 if (edit_submode != NULL)
10982 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010983 // Don't draw the ruler when the popup menu is visible, it may overlap.
10984 // Except when the popup menu will be redrawn anyway.
10985 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010986 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987#endif
10988
10989#ifdef FEAT_STL_OPT
10990 if (*p_ruf)
10991 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010992 int save_called_emsg = called_emsg;
10993
10994 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010996 if (called_emsg)
10997 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010998 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010999 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 return;
11001 }
11002#endif
11003
11004 /*
11005 * Check if not in Insert mode and the line is empty (will show "0-1").
11006 */
11007 if (!(State & INSERT)
11008 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11009 empty_line = TRUE;
11010
11011 /*
11012 * Only draw the ruler when something changed.
11013 */
11014 validate_virtcol_win(wp);
11015 if ( redraw_cmdline
11016 || always
11017 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11018 || wp->w_cursor.col != wp->w_ru_cursor.col
11019 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 || wp->w_topline != wp->w_ru_topline
11022 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11023#ifdef FEAT_DIFF
11024 || wp->w_topfill != wp->w_ru_topfill
11025#endif
11026 || empty_line != wp->w_ru_empty)
11027 {
11028 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 if (wp->w_status_height)
11030 {
11031 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011032 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011033 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011034 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 }
11036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 {
11038 row = Rows - 1;
11039 fillchar = ' ';
11040 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 width = Columns;
11042 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 }
11044
11045 /* In list mode virtcol needs to be recomputed */
11046 virtcol = wp->w_virtcol;
11047 if (wp->w_p_list && lcs_tab1 == NUL)
11048 {
11049 wp->w_p_list = FALSE;
11050 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11051 wp->w_p_list = TRUE;
11052 }
11053
11054 /*
11055 * Some sprintfs return the length, some return a pointer.
11056 * To avoid portability problems we use strlen() here.
11057 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011058 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11060 ? 0L
11061 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011062 len = STRLEN(buffer);
11063 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11065 (int)virtcol + 1);
11066
11067 /*
11068 * Add a "50%" if there is room for it.
11069 * On the last line, don't print in the last column (scrolls the
11070 * screen up on some terminals).
11071 */
11072 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011073 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 this_ru_col = ru_col - (Columns - width);
11078 if (this_ru_col < 0)
11079 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 /* Never use more than half the window/screen width, leave the other
11081 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011082 if (this_ru_col < (width + 1) / 2)
11083 this_ru_col = (width + 1) / 2;
11084 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011086 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011087 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 if (has_mbyte)
11090 i += (*mb_char2bytes)(fillchar, buffer + i);
11091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 buffer[i++] = fillchar;
11093 ++o;
11094 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011095 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 }
11097 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 if (has_mbyte)
11099 {
11100 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011101 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 {
11103 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011104 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 {
11106 buffer[i] = NUL;
11107 break;
11108 }
11109 }
11110 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011111 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011112 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113
Bram Moolenaar4033c552017-09-16 20:54:51 +020011114 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 i = redraw_cmdline;
11116 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011117 this_ru_col + off + (int)STRLEN(buffer),
11118 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 fillchar, fillchar, attr);
11120 /* don't redraw the cmdline because of showing the ruler */
11121 redraw_cmdline = i;
11122 wp->w_ru_cursor = wp->w_cursor;
11123 wp->w_ru_virtcol = wp->w_virtcol;
11124 wp->w_ru_empty = empty_line;
11125 wp->w_ru_topline = wp->w_topline;
11126 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11127#ifdef FEAT_DIFF
11128 wp->w_ru_topfill = wp->w_topfill;
11129#endif
11130 }
11131}
11132#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011133
11134#if defined(FEAT_LINEBREAK) || defined(PROTO)
11135/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011136 * Return the width of the 'number' and 'relativenumber' column.
11137 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011138 * Otherwise it depends on 'numberwidth' and the line count.
11139 */
11140 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011141number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011142{
11143 int n;
11144 linenr_T lnum;
11145
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011146 if (wp->w_p_rnu && !wp->w_p_nu)
11147 /* cursor line shows "0" */
11148 lnum = wp->w_height;
11149 else
11150 /* cursor line shows absolute line number */
11151 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011152
Bram Moolenaar6b314672015-03-20 15:42:10 +010011153 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011154 return wp->w_nrwidth_width;
11155 wp->w_nrwidth_line_count = lnum;
11156
11157 n = 0;
11158 do
11159 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011160 lnum /= 10;
11161 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011162 } while (lnum > 0);
11163
11164 /* 'numberwidth' gives the minimal width plus one */
11165 if (n < wp->w_p_nuw - 1)
11166 n = wp->w_p_nuw - 1;
11167
11168 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011169 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011170 return n;
11171}
11172#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011173
Bram Moolenaar113e1072019-01-20 15:30:40 +010011174#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011175/*
11176 * Return the current cursor column. This is the actual position on the
11177 * screen. First column is 0.
11178 */
11179 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011180screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011181{
11182 return screen_cur_col;
11183}
11184
11185/*
11186 * Return the current cursor row. This is the actual position on the screen.
11187 * First row is 0.
11188 */
11189 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011190screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011191{
11192 return screen_cur_row;
11193}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011194#endif