blob: 30e1bb21ab3ae1626ccd4e61b13fbcc97ec8f99c [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 Moolenaar9c27b1c2019-05-26 18:48:13 +0200613 if (first_popupwin != NULL || curtab->tp_first_popupwin != NULL)
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)
1002 wp->w_valid &= ~VALID_POPUP;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001003 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001004 wp->w_valid &= ~VALID_POPUP;
1005
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)
1015 if ((wp->w_valid & VALID_POPUP) == 0
1016 && 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 Moolenaar4d784b22019-05-25 19:51:39 +02001022 if ((wp->w_valid & VALID_POPUP) == 0
1023 && 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);
1032 lowest_wp->w_valid |= VALID_POPUP;
1033 }
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)
4408 char_attr = hl_combine_attr(line_attr, text_prop_attr);
4409# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004410 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004411 || vcol < fromcol || vcol_prev < fromcol_prev
4412 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004413 // Use line_attr when not in the Visual or 'incsearch' area
4414 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004415 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004416#else
4417 if (area_attr != 0)
4418 char_attr = area_attr;
4419 else if (search_attr != 0)
4420 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004421#endif
4422 else
4423 {
4424 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004425#ifdef FEAT_TEXT_PROP
4426 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004427 {
4428 if (text_prop_combine)
4429 char_attr = hl_combine_attr(
4430 syntax_attr, text_prop_attr);
4431 else
4432 char_attr = text_prop_attr;
4433 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004434 else
4435#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004436#ifdef FEAT_SYN_HL
4437 if (has_syntax)
4438 char_attr = syntax_attr;
4439 else
4440#endif
4441 char_attr = 0;
4442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004444 if (char_attr == 0)
4445 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 /*
4448 * Get the next character to put on the screen.
4449 */
4450 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004451 * The "p_extra" points to the extra stuff that is inserted to
4452 * represent special characters (non-printable stuff) and other
4453 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004454 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004455 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4456 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4458 */
4459 if (n_extra > 0)
4460 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004461 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004463 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004465 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
4467 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004468 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004469 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else
4472 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else
4475 {
4476 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (has_mbyte)
4478 {
4479 mb_c = c;
4480 if (enc_utf8)
4481 {
4482 /* If the UTF-8 character is more than one byte:
4483 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004484 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 mb_utf8 = FALSE;
4486 if (mb_l > n_extra)
4487 mb_l = 1;
4488 else if (mb_l > 1)
4489 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004490 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004492 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 }
4495 else
4496 {
4497 /* if this is a DBCS character, put it in "mb_c" */
4498 mb_l = MB_BYTE2LEN(c);
4499 if (mb_l >= n_extra)
4500 mb_l = 1;
4501 else if (mb_l > 1)
4502 mb_c = (c << 8) + p_extra[1];
4503 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004504 if (mb_l == 0) /* at the NUL at end-of-line */
4505 mb_l = 1;
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* If a double-width char doesn't fit display a '>' in the
4508 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004509 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510# ifdef FEAT_RIGHTLEFT
4511 wp->w_p_rl ? (col <= 0) :
4512# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004513 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 && (*mb_char2cells)(mb_c) == 2)
4515 {
4516 c = '>';
4517 mb_c = c;
4518 mb_l = 1;
4519 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004520 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 /* put the pointer back to output the double-width
4522 * character at the start of the next line. */
4523 ++n_extra;
4524 --p_extra;
4525 }
4526 else
4527 {
4528 n_extra -= mb_l - 1;
4529 p_extra += mb_l - 1;
4530 }
4531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 ++p_extra;
4533 }
4534 --n_extra;
4535 }
4536 else
4537 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004538#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004539 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004540#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004541
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004542 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004543 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 /*
4545 * Get a character from the line itself.
4546 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004547 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004548#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004549 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (has_mbyte)
4552 {
4553 mb_c = c;
4554 if (enc_utf8)
4555 {
4556 /* If the UTF-8 character is more than one byte: Decode it
4557 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004558 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 mb_utf8 = FALSE;
4560 if (mb_l > 1)
4561 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004562 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 /* Overlong encoded ASCII or ASCII with composing char
4564 * is displayed normally, except a NUL. */
4565 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004566 {
4567 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004568#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004569 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004570#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004573
4574 /* At start of the line we can have a composing char.
4575 * Draw it as a space with a composing char. */
4576 if (utf_iscomposing(mb_c))
4577 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004578 int i;
4579
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004580 for (i = Screen_mco - 1; i > 0; --i)
4581 u8cc[i] = u8cc[i - 1];
4582 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004583 mb_c = ' ';
4584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586
4587 if ((mb_l == 1 && c >= 0x80)
4588 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004589 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
4591 /*
4592 * Illegal UTF-8 byte: display as <xx>.
4593 * Non-BMP character : display as ? or fullwidth ?.
4594 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004595 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004596# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004597 if (wp->w_p_rl) /* reverse */
4598 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004599# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 p_extra = extra;
4601 c = *p_extra;
4602 mb_c = mb_ptr2char_adv(&p_extra);
4603 mb_utf8 = (c >= 0x80);
4604 n_extra = (int)STRLEN(p_extra);
4605 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004606 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (area_attr == 0 && search_attr == 0)
4608 {
4609 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004610 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 saved_attr2 = char_attr; /* save current attr */
4612 }
4613 }
4614 else if (mb_l == 0) /* at the NUL at end-of-line */
4615 mb_l = 1;
4616#ifdef FEAT_ARABIC
4617 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4618 {
4619 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004620 int pc, pc1, nc;
4621 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 /* The idea of what is the previous and next
4624 * character depends on 'rightleft'. */
4625 if (wp->w_p_rl)
4626 {
4627 pc = prev_c;
4628 pc1 = prev_c1;
4629 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 else
4633 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004636 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 prev_c = mb_c;
4639
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 else
4643 prev_c = mb_c;
4644#endif
4645 }
4646 else /* enc_dbcs */
4647 {
4648 mb_l = MB_BYTE2LEN(c);
4649 if (mb_l == 0) /* at the NUL at end-of-line */
4650 mb_l = 1;
4651 else if (mb_l > 1)
4652 {
4653 /* We assume a second byte below 32 is illegal.
4654 * Hopefully this is OK for all double-byte encodings!
4655 */
4656 if (ptr[1] >= 32)
4657 mb_c = (c << 8) + ptr[1];
4658 else
4659 {
4660 if (ptr[1] == NUL)
4661 {
4662 /* head byte at end of line */
4663 mb_l = 1;
4664 transchar_nonprint(extra, c);
4665 }
4666 else
4667 {
4668 /* illegal tail byte */
4669 mb_l = 2;
4670 STRCPY(extra, "XX");
4671 }
4672 p_extra = extra;
4673 n_extra = (int)STRLEN(extra) - 1;
4674 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004675 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 c = *p_extra++;
4677 if (area_attr == 0 && search_attr == 0)
4678 {
4679 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004680 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 saved_attr2 = char_attr; /* save current attr */
4682 }
4683 mb_c = c;
4684 }
4685 }
4686 }
4687 /* If a double-width char doesn't fit display a '>' in the
4688 * last column; the character is displayed at the start of the
4689 * next line. */
4690 if ((
4691# ifdef FEAT_RIGHTLEFT
4692 wp->w_p_rl ? (col <= 0) :
4693# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004694 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 && (*mb_char2cells)(mb_c) == 2)
4696 {
4697 c = '>';
4698 mb_c = c;
4699 mb_utf8 = FALSE;
4700 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004701 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004702 // Put pointer back so that the character will be
4703 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004705#ifdef FEAT_CONCEAL
4706 did_decrement_ptr = TRUE;
4707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 else if (*ptr != NUL)
4710 ptr += mb_l - 1;
4711
4712 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004713 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004714 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004715 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004718 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004719 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 c = ' ';
4721 if (area_attr == 0 && search_attr == 0)
4722 {
4723 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004724 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 saved_attr2 = char_attr; /* save current attr */
4726 }
4727 mb_c = c;
4728 mb_utf8 = FALSE;
4729 mb_l = 1;
4730 }
4731
4732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 ++ptr;
4734
4735 if (extra_check)
4736 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004737#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004738 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004739#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004740
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004741#ifdef FEAT_TERMINAL
4742 if (get_term_attr)
4743 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004744 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004745
4746 if (!attr_pri)
4747 char_attr = syntax_attr;
4748 else
4749 char_attr = hl_combine_attr(syntax_attr, char_attr);
4750 }
4751#endif
4752
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004753#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004754 // Get syntax attribute, unless still at the start of the line
4755 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004756 v = (long)(ptr - line);
4757 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
4759 /* Get the syntax attribute for the character. If there
4760 * is an error, disable syntax highlighting. */
4761 save_did_emsg = did_emsg;
4762 did_emsg = FALSE;
4763
Bram Moolenaar217ad922005-03-20 22:37:15 +00004764 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004765# ifdef FEAT_SPELL
4766 has_spell ? &can_spell :
4767# endif
4768 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004771 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004772 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004773 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004774 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 else
4777 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004778
4779 // combine syntax attribute with 'wincolor'
4780 if (win_attr != 0)
4781 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4782
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004783#ifdef SYN_TIME_LIMIT
4784 if (wp->w_s->b_syn_slow)
4785 has_syntax = FALSE;
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 /* Need to get the line again, a multi-line regexp may
4789 * have made it invalid. */
4790 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4791 ptr = line + v;
4792
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004793# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004794 // Text properties overrule syntax highlighting or combine.
4795 if (text_prop_attr == 0 || text_prop_combine)
4796# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004797 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004798 int comb_attr = syntax_attr;
4799# ifdef FEAT_TEXT_PROP
4800 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4801# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004802 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004803 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004804 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004805 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004806 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004807# ifdef FEAT_CONCEAL
4808 /* no concealing past the end of the line, it interferes
4809 * with line highlighting */
4810 if (c == NUL)
4811 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004812 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004813 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004816#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004817
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004818#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004819 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004820 * Only do this when there is no syntax highlighting, the
4821 * @Spell cluster is not used or the current syntax item
4822 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004823 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004824 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004825 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004826 if (c != 0 && (
4827# ifdef FEAT_SYN_HL
4828 !has_syntax ||
4829# endif
4830 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004831 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004832 char_u *prev_ptr, *p;
4833 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004834 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004835 if (has_mbyte)
4836 {
4837 prev_ptr = ptr - mb_l;
4838 v -= mb_l - 1;
4839 }
4840 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004841 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004842
4843 /* Use nextline[] if possible, it has the start of the
4844 * next line concatenated. */
4845 if ((prev_ptr - line) - nextlinecol >= 0)
4846 p = nextline + (prev_ptr - line) - nextlinecol;
4847 else
4848 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004849 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004850 len = spell_check(wp, p, &spell_hlf, &cap_col,
4851 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004852 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004853
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004854 /* In Insert mode only highlight a word that
4855 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004856 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004857 && (State & INSERT) != 0
4858 && wp->w_cursor.lnum == lnum
4859 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004860 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004861 && wp->w_cursor.col < (colnr_T)word_end)
4862 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004863 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004864 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004865 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004866
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004867 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004868 && (p - nextline) + len > nextline_idx)
4869 {
4870 /* Remember that the good word continues at the
4871 * start of the next line. */
4872 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004873 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004874 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004875
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004876 /* Turn index into actual attributes. */
4877 if (spell_hlf != HLF_COUNT)
4878 spell_attr = highlight_attr[spell_hlf];
4879
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004880 if (cap_col > 0)
4881 {
4882 if (p != prev_ptr
4883 && (p - nextline) + cap_col >= nextline_idx)
4884 {
4885 /* Remember that the word in the next line
4886 * must start with a capital. */
4887 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004888 cap_col = (int)((p - nextline) + cap_col
4889 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004890 }
4891 else
4892 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004893 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004894 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004895 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004896 }
4897 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004898 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004899 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004900 char_attr = hl_combine_attr(char_attr, spell_attr);
4901 else
4902 char_attr = hl_combine_attr(spell_attr, char_attr);
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#endif
4905#ifdef FEAT_LINEBREAK
4906 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004907 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004909 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004910 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004912 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004913 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004914
Bram Moolenaar597a4222014-06-25 14:39:50 +02004915 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004916 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004917 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004918 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004919# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004920 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004921 wp->w_buffer->b_p_vts_array) - 1;
4922# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004923 n_extra = (int)wp->w_buffer->b_p_ts
4924 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004925# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004926
Bram Moolenaar4df70292015-03-21 14:20:16 +01004927 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004928 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004929 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004930 {
4931#ifdef FEAT_CONCEAL
4932 if (c == TAB)
4933 /* See "Tab alignment" below. */
4934 FIX_FOR_BOGUSCOLS;
4935#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004936 if (!wp->w_p_list)
4937 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940#endif
4941
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004942 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4943 // But not when the character is followed by a composing
4944 // character (use mb_l to check that).
4945 if (wp->w_p_list
4946 && ((((c == 160 && mb_l == 1)
4947 || (mb_utf8
4948 && ((mb_c == 160 && mb_l == 2)
4949 || (mb_c == 0x202f && mb_l == 3))))
4950 && lcs_nbsp)
4951 || (c == ' '
4952 && mb_l == 1
4953 && lcs_space
4954 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004955 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004956 c = (c == ' ') ? lcs_space : lcs_nbsp;
4957 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004958 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004959 n_attr = 1;
4960 extra_attr = HL_ATTR(HLF_8);
4961 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004962 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004963 mb_c = c;
4964 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004965 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004966 mb_utf8 = TRUE;
4967 u8cc[0] = 0;
4968 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004969 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004970 else
4971 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004972 }
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4975 {
4976 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004977 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004980 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 saved_attr2 = char_attr; /* save current attr */
4982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004984 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004987 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004988 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 else
4991 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
4994
4995 /*
4996 * Handling of non-printable characters.
4997 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004998 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 /*
5001 * when getting a character from the file, we may have to
5002 * turn it into something else on the way to putting it
5003 * into "ScreenLines".
5004 */
5005 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5006 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005007 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005008 long vcol_adjusted = vcol; /* removed showbreak length */
5009#ifdef FEAT_LINEBREAK
5010 /* only adjust the tab_len, when at the first column
5011 * after the showbreak value was drawn */
5012 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5013 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005016#ifdef FEAT_VARTABS
5017 tab_len = tabstop_padding(vcol_adjusted,
5018 wp->w_buffer->b_p_ts,
5019 wp->w_buffer->b_p_vts_array) - 1;
5020#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005021 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005022 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5023#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005024
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005025#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005026 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005027#endif
5028 /* tab amount depends on current column */
5029 n_extra = tab_len;
5030#ifdef FEAT_LINEBREAK
5031 else
5032 {
5033 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005034 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005035 int i;
5036 int saved_nextra = n_extra;
5037
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005038#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005039 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005040 /* there are characters to conceal */
5041 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005042 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5043 */
5044 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5045 && n_extra > tab_len)
5046 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005047#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005048
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005049 /* if n_extra > 0, it gives the number of chars, to
5050 * use for a tab, else we need to calculate the width
5051 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005052 len = (tab_len * mb_char2len(lcs_tab2));
5053 if (n_extra > 0)
5054 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005055 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005056 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005057 vim_memset(p, ' ', len);
5058 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005059 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005060 p_extra_free = p;
5061 for (i = 0; i < tab_len; i++)
5062 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005063 if (*p == NUL)
5064 {
5065 tab_len = i;
5066 break;
5067 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005068 mb_char2bytes(lcs_tab2, p);
5069 p += mb_char2len(lcs_tab2);
5070 n_extra += mb_char2len(lcs_tab2)
5071 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005072 }
5073 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005074#ifdef FEAT_CONCEAL
5075 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5076 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005077 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005078 n_extra -= vcol_off;
5079#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005080 }
5081#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005082#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005083 {
5084 int vc_saved = vcol_off;
5085
5086 /* Tab alignment should be identical regardless of
5087 * 'conceallevel' value. So tab compensates of all
5088 * previous concealed characters, and thus resets
5089 * vcol_off and boguscols accumulated so far in the
5090 * line. Note that the tab can be longer than
5091 * 'tabstop' when there are concealed characters. */
5092 FIX_FOR_BOGUSCOLS;
5093
5094 /* Make sure, the highlighting for the tab char will be
5095 * correctly set further below (effectively reverts the
5096 * FIX_FOR_BOGSUCOLS macro */
5097 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005098 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005099 tab_len += vc_saved;
5100 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (wp->w_p_list)
5104 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005105 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005106#ifdef FEAT_LINEBREAK
5107 if (wp->w_p_lbr)
5108 c_extra = NUL; /* using p_extra from above */
5109 else
5110#endif
5111 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005112 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005113 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005114 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005117 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005120 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005121 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 else
5125 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005126 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 c_extra = ' ';
5128 c = ' ';
5129 }
5130 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005131 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005132 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005133 || ((fromcol >= 0 || fromcol_prev >= 0)
5134 && tocol > vcol
5135 && VIsual_mode != Ctrl_V
5136 && (
5137# ifdef FEAT_RIGHTLEFT
5138 wp->w_p_rl ? (col >= 0) :
5139# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005140 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005141 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005142 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005143 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005144 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005146 /* Display a '$' after the line or highlight an extra
5147 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5149 /* For a diff line the highlighting continues after the
5150 * "$". */
5151 if (
5152# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005153 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154# ifdef LINE_ATTR
5155 &&
5156# endif
5157# endif
5158# ifdef LINE_ATTR
5159 line_attr == 0
5160# endif
5161 )
5162#endif
5163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 /* In virtualedit, visual selections may extend
5165 * beyond end of line. */
5166 if (area_highlighting && virtual_active()
5167 && tocol != MAXCOL && vcol < tocol)
5168 n_extra = 0;
5169 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 p_extra = at_end_str;
5172 n_extra = 1;
5173 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005174 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005177 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005178 c = lcs_eol;
5179 else
5180 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 lcs_eol_one = -1;
5182 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005183 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005185 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 n_attr = 1;
5187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005189 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005192 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005193 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 else
5196 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 else if (c != NUL)
5199 {
5200 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005201 if (n_extra == 0)
5202 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203#ifdef FEAT_RIGHTLEFT
5204 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5205 rl_mirror(p_extra); /* reverse "<12>" */
5206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005208 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005209#ifdef FEAT_LINEBREAK
5210 if (wp->w_p_lbr)
5211 {
5212 char_u *p;
5213
5214 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005215 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005216 vim_memset(p, ' ', n_extra);
5217 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5218 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005219 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005220 p_extra_free = p_extra = p;
5221 }
5222 else
5223#endif
5224 {
5225 n_extra = byte2cells(c) - 1;
5226 c = *p_extra++;
5227 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005228 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 {
5230 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005231 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 saved_attr2 = char_attr; /* save current attr */
5233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 else if (VIsual_active
5237 && (VIsual_mode == Ctrl_V
5238 || VIsual_mode == 'v')
5239 && virtual_active()
5240 && tocol != MAXCOL
5241 && vcol < tocol
5242 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005243#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005245#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005246 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
5248 c = ' ';
5249 --ptr; /* put it back at the NUL */
5250 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005251#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 else if ((
5253# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005254 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005256# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005257 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 ) && (
5261# ifdef FEAT_RIGHTLEFT
5262 wp->w_p_rl ? (col >= 0) :
5263# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005264 (col
5265# ifdef FEAT_CONCEAL
5266 - boguscols
5267# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005268 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 /* Highlight until the right side of the window */
5271 c = ' ';
5272 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005273
5274 /* Remember we do the char for line highlighting. */
5275 ++did_line_attr;
5276
5277 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005278 if (line_attr != 0 && char_attr == search_attr
5279 && (did_line_attr > 1
5280 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005281 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282# ifdef FEAT_DIFF
5283 if (diff_hlf == HLF_TXD)
5284 {
5285 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005286 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005287 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005288 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005289 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5290 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005291 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005295# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005296 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005297 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005298 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005299 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5300 char_attr = hl_combine_attr(char_attr,
5301 HL_ATTR(HLF_CUL));
5302 }
5303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305#endif
5306 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005307
5308#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005309 if ( wp->w_p_cole > 0
5310 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005311 conceal_cursor_line(wp))
5312 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005313 && !(lnum_in_visual_area
5314 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005315 {
5316 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005317 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005318 && (syn_get_sub_char() != NUL || match_conc
5319 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005320 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005322 /* First time at this concealed item: display one
5323 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005324 if (match_conc)
5325 c = match_conc;
5326 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005327 c = syn_get_sub_char();
5328 else if (lcs_conceal != NUL)
5329 c = lcs_conceal;
5330 else
5331 c = ' ';
5332
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005333 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005335 if (n_extra > 0)
5336 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005337 vcol += n_extra;
5338 if (wp->w_p_wrap && n_extra > 0)
5339 {
5340# ifdef FEAT_RIGHTLEFT
5341 if (wp->w_p_rl)
5342 {
5343 col -= n_extra;
5344 boguscols -= n_extra;
5345 }
5346 else
5347# endif
5348 {
5349 boguscols += n_extra;
5350 col += n_extra;
5351 }
5352 }
5353 n_extra = 0;
5354 n_attr = 0;
5355 }
5356 else if (n_skip == 0)
5357 {
5358 is_concealing = TRUE;
5359 n_skip = 1;
5360 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005361 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005362 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005363 {
5364 mb_utf8 = TRUE;
5365 u8cc[0] = 0;
5366 c = 0xc0;
5367 }
5368 else
5369 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005370 }
5371 else
5372 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005373 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005374 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005375 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005376
5377 if (n_skip > 0 && did_decrement_ptr)
5378 // not showing the '>', put pointer back to avoid getting stuck
5379 ++ptr;
5380
5381#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 }
5383
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005384#ifdef FEAT_CONCEAL
5385 /* In the cursor line and we may be concealing characters: correct
5386 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005387 if (!did_wcol && draw_state == WL_LINE
5388 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005389 && conceal_cursor_line(wp)
5390 && (int)wp->w_virtcol <= vcol + n_skip)
5391 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005392# ifdef FEAT_RIGHTLEFT
5393 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005394 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005395 else
5396# endif
5397 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005398 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005399 did_wcol = TRUE;
5400 }
5401#endif
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 /* Don't override visual selection highlighting. */
5404 if (n_attr > 0
5405 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005406 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 char_attr = extra_attr;
5408
Bram Moolenaar81695252004-12-29 20:58:21 +00005409#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 /* XIM don't send preedit_start and preedit_end, but they send
5411 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5412 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005413 if (p_imst == IM_ON_THE_SPOT
5414 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005415 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 && (State & INSERT)
5417 && !p_imdisable
5418 && im_is_preediting()
5419 && draw_state == WL_LINE)
5420 {
5421 colnr_T tcol;
5422
5423 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005424 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 else
5426 tcol = preedit_end_col;
5427 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5428 {
5429 if (feedback_old_attr < 0)
5430 {
5431 feedback_col = 0;
5432 feedback_old_attr = char_attr;
5433 }
5434 char_attr = im_get_feedback_attr(feedback_col);
5435 if (char_attr < 0)
5436 char_attr = feedback_old_attr;
5437 feedback_col++;
5438 }
5439 else if (feedback_old_attr >= 0)
5440 {
5441 char_attr = feedback_old_attr;
5442 feedback_old_attr = -1;
5443 feedback_col = 0;
5444 }
5445 }
5446#endif
5447 /*
5448 * Handle the case where we are in column 0 but not on the first
5449 * character of the line and the user wants us to show us a
5450 * special character (via 'listchars' option "precedes:<char>".
5451 */
5452 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005453 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5455#ifdef FEAT_DIFF
5456 && filler_todo <= 0
5457#endif
5458 && draw_state > WL_NR
5459 && c != NUL)
5460 {
5461 c = lcs_prec;
5462 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005463 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5464 {
5465 /* Double-width character being overwritten by the "precedes"
5466 * character, need to fill up half the character. */
5467 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005468 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005469 n_extra = 1;
5470 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005471 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005474 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005477 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005478 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480 else
5481 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005482 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
5484 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005485 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 n_attr3 = 1;
5487 }
5488 }
5489
5490 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005491 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005493 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005494#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005495 || did_line_attr == 1
5496#endif
5497 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499#ifdef FEAT_SEARCH_EXTRA
5500 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005501
5502 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005503 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005504 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005505#endif
5506
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005507 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 * highlight match at end of line. If it's beyond the last
5509 * char on the screen, just overwrite that one (tricky!) Not
5510 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005511#ifdef FEAT_SEARCH_EXTRA
5512 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005513 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005514 prevcol_hl_flag = TRUE;
5515 else
5516 {
5517 cur = wp->w_match_head;
5518 while (cur != NULL)
5519 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005520 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005521 {
5522 prevcol_hl_flag = TRUE;
5523 break;
5524 }
5525 cur = cur->next;
5526 }
5527 }
5528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005530 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005531 && (VIsual_mode != Ctrl_V
5532 || lnum == VIsual.lnum
5533 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005534 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#ifdef FEAT_SEARCH_EXTRA
5536 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005537 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005538# ifdef FEAT_SYN_HL
5539 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5540 && !(wp == curwin && VIsual_active))
5541# endif
5542# ifdef FEAT_DIFF
5543 && diff_hlf == (hlf_T)0
5544# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005545# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005546 && did_line_attr <= 1
5547# endif
5548 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549#endif
5550 ))
5551 {
5552 int n = 0;
5553
5554#ifdef FEAT_RIGHTLEFT
5555 if (wp->w_p_rl)
5556 {
5557 if (col < 0)
5558 n = 1;
5559 }
5560 else
5561#endif
5562 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005563 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 n = -1;
5565 }
5566 if (n != 0)
5567 {
5568 /* At the window boundary, highlight the last character
5569 * instead (better than nothing). */
5570 off += n;
5571 col += n;
5572 }
5573 else
5574 {
5575 /* Add a blank character to highlight. */
5576 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 if (enc_utf8)
5578 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580#ifdef FEAT_SEARCH_EXTRA
5581 if (area_attr == 0)
5582 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005583 /* Use attributes from match with highest priority among
5584 * 'search_hl' and the match list. */
5585 char_attr = search_hl.attr;
5586 cur = wp->w_match_head;
5587 shl_flag = FALSE;
5588 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005589 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005590 if (shl_flag == FALSE
5591 && ((cur != NULL
5592 && cur->priority > SEARCH_HL_PRIORITY)
5593 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005594 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005595 shl = &search_hl;
5596 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005597 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005598 else
5599 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005600 if ((ptr - line) - 1 == (long)shl->startcol
5601 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 char_attr = shl->attr;
5603 if (shl != &search_hl && cur != NULL)
5604 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607#endif
5608 ScreenAttrs[off] = char_attr;
5609#ifdef FEAT_RIGHTLEFT
5610 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005613 --off;
5614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 else
5616#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005619 ++off;
5620 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005621 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005622#ifdef FEAT_SYN_HL
5623 eol_hl_off = 1;
5624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Bram Moolenaar91170f82006-05-05 21:15:17 +00005628 /*
5629 * At end of the text line.
5630 */
5631 if (c == NUL)
5632 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005633#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005635 if (wp->w_p_wrap)
5636 v = wp->w_skipcol;
5637 else
5638 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005640 /* check if line ends before left margin */
5641 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005642 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005643#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005644 // Get rid of the boguscols now, we want to draw until the right
5645 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005646 col -= boguscols;
5647 boguscols = 0;
5648#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005649
5650 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005651 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652
5653 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005654 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5655 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005656 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005657 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005658 || draw_color_col
5659 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005660# ifdef FEAT_RIGHTLEFT
5661 && !wp->w_p_rl
5662# endif
5663 )
5664 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005665 int rightmost_vcol = 0;
5666 int i;
5667
5668 if (wp->w_p_cuc)
5669 rightmost_vcol = wp->w_virtcol;
5670 if (draw_color_col)
5671 /* determine rightmost colorcolumn to possibly draw */
5672 for (i = 0; color_cols[i] >= 0; ++i)
5673 if (rightmost_vcol < color_cols[i])
5674 rightmost_vcol = color_cols[i];
5675
Bram Moolenaar02631462017-09-22 15:20:32 +02005676 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005677 {
5678 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005679 if (enc_utf8)
5680 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005681 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005682 if (draw_color_col)
5683 draw_color_col = advance_color_col(VCOL_HLC,
5684 &color_cols);
5685
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005686 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005687 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005688 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005689 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005690 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005691 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005692
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005693 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005694 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005695
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005696 ++vcol;
5697 }
5698 }
5699#endif
5700
Bram Moolenaar53f81742017-09-22 14:35:51 +02005701 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005702 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 row++;
5704
5705 /*
5706 * Update w_cline_height and w_cline_folded if the cursor line was
5707 * updated (saves a call to plines() later).
5708 */
5709 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5710 {
5711 curwin->w_cline_row = startrow;
5712 curwin->w_cline_height = row - startrow;
5713#ifdef FEAT_FOLDING
5714 curwin->w_cline_folded = FALSE;
5715#endif
5716 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5717 }
5718
5719 break;
5720 }
5721
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005722 // Show "extends" character from 'listchars' if beyond the line end and
5723 // 'list' is set.
5724 if (lcs_ext != NUL
5725 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 && !wp->w_p_wrap
5727#ifdef FEAT_DIFF
5728 && filler_todo <= 0
5729#endif
5730 && (
5731#ifdef FEAT_RIGHTLEFT
5732 wp->w_p_rl ? col == 0 :
5733#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005734 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005736 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5738 {
5739 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005740 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005742 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005745 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005746 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 else
5749 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005752#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005753 /* advance to the next 'colorcolumn' */
5754 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005755 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005756
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005757 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005758 * highlight the cursor position itself.
5759 * Also highlight the 'colorcolumn' if it is different than
5760 * 'cursorcolumn' */
5761 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005762 if (draw_state == WL_LINE && !lnum_in_visual_area
5763 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005764 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005765 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 && lnum != wp->w_cursor.lnum)
5767 {
5768 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005769 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005770 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005771 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005772 {
5773 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005774 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005775 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005776 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005777#endif
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 /*
5780 * Store character to be displayed.
5781 * Skip characters that are left of the screen for 'nowrap'.
5782 */
5783 vcol_prev = vcol;
5784 if (draw_state < WL_LINE || n_skip <= 0)
5785 {
5786 /*
5787 * Store the character.
5788 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005789#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5791 {
5792 /* A double-wide character is: put first halve in left cell. */
5793 --off;
5794 --col;
5795 }
5796#endif
5797 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005799 {
5800 if ((mb_c & 0xff00) == 0x8e00)
5801 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else if (enc_utf8)
5805 {
5806 if (mb_utf8)
5807 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005808 int i;
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005811 if ((c & 0xff) == 0)
5812 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005813 for (i = 0; i < Screen_mco; ++i)
5814 {
5815 ScreenLinesC[i][off] = u8cc[i];
5816 if (u8cc[i] == 0)
5817 break;
5818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
5820 else
5821 ScreenLinesUC[off] = 0;
5822 }
5823 if (multi_attr)
5824 {
5825 ScreenAttrs[off] = multi_attr;
5826 multi_attr = 0;
5827 }
5828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 ScreenAttrs[off] = char_attr;
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5832 {
5833 /* Need to fill two screen columns. */
5834 ++off;
5835 ++col;
5836 if (enc_utf8)
5837 /* UTF-8: Put a 0 in the second screen char. */
5838 ScreenLines[off] = 0;
5839 else
5840 /* DBCS: Put second byte in the second screen char. */
5841 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005842 if (draw_state > WL_NR
5843#ifdef FEAT_DIFF
5844 && filler_todo <= 0
5845#endif
5846 )
5847 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 /* When "tocol" is halfway a character, set it to the end of
5849 * the character, otherwise highlighting won't stop. */
5850 if (tocol == vcol)
5851 ++tocol;
5852#ifdef FEAT_RIGHTLEFT
5853 if (wp->w_p_rl)
5854 {
5855 /* now it's time to backup one cell */
5856 --off;
5857 --col;
5858 }
5859#endif
5860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861#ifdef FEAT_RIGHTLEFT
5862 if (wp->w_p_rl)
5863 {
5864 --off;
5865 --col;
5866 }
5867 else
5868#endif
5869 {
5870 ++off;
5871 ++col;
5872 }
5873 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005874#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005875 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005876 {
5877 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005878 ++vcol_off;
5879 if (n_extra > 0)
5880 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005881 if (wp->w_p_wrap)
5882 {
5883 /*
5884 * Special voodoo required if 'wrap' is on.
5885 *
5886 * Advance the column indicator to force the line
5887 * drawing to wrap early. This will make the line
5888 * take up the same screen space when parts are concealed,
5889 * so that cursor line computations aren't messed up.
5890 *
5891 * To avoid the fictitious advance of 'col' causing
5892 * trailing junk to be written out of the screen line
5893 * we are building, 'boguscols' keeps track of the number
5894 * of bad columns we have advanced.
5895 */
5896 if (n_extra > 0)
5897 {
5898 vcol += n_extra;
5899# ifdef FEAT_RIGHTLEFT
5900 if (wp->w_p_rl)
5901 {
5902 col -= n_extra;
5903 boguscols -= n_extra;
5904 }
5905 else
5906# endif
5907 {
5908 col += n_extra;
5909 boguscols += n_extra;
5910 }
5911 n_extra = 0;
5912 n_attr = 0;
5913 }
5914
5915
Bram Moolenaar860cae12010-06-05 23:22:07 +02005916 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5917 {
5918 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005919# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005920 if (wp->w_p_rl)
5921 {
5922 --boguscols;
5923 --col;
5924 }
5925 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005926# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005927 {
5928 ++boguscols;
5929 ++col;
5930 }
5931 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005932
5933# ifdef FEAT_RIGHTLEFT
5934 if (wp->w_p_rl)
5935 {
5936 --boguscols;
5937 --col;
5938 }
5939 else
5940# endif
5941 {
5942 ++boguscols;
5943 ++col;
5944 }
5945 }
5946 else
5947 {
5948 if (n_extra > 0)
5949 {
5950 vcol += n_extra;
5951 n_extra = 0;
5952 n_attr = 0;
5953 }
5954 }
5955
5956 }
5957#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 else
5959 --n_skip;
5960
Bram Moolenaar64486672010-05-16 15:46:46 +02005961 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5962 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005963 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964#ifdef FEAT_DIFF
5965 && filler_todo <= 0
5966#endif
5967 )
5968 ++vcol;
5969
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005970#ifdef FEAT_SYN_HL
5971 if (vcol_save_attr >= 0)
5972 char_attr = vcol_save_attr;
5973#endif
5974
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 /* restore attributes after "predeces" in 'listchars' */
5976 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5977 char_attr = saved_attr3;
5978
5979 /* restore attributes after last 'listchars' or 'number' char */
5980 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5981 char_attr = saved_attr2;
5982
5983 /*
5984 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005985 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 */
5987 if ((
5988#ifdef FEAT_RIGHTLEFT
5989 wp->w_p_rl ? (col < 0) :
5990#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005991 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 && (*ptr != NUL
5993#ifdef FEAT_DIFF
5994 || filler_todo > 0
5995#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005996 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5998 )
5999 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006000#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006001 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006002 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006003 boguscols = 0;
6004#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006005 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006006 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 ++row;
6009 ++screen_row;
6010
6011 /* When not wrapping and finished diff lines, or when displayed
6012 * '$' and highlighting until last column, break here. */
6013 if ((!wp->w_p_wrap
6014#ifdef FEAT_DIFF
6015 && filler_todo <= 0
6016#endif
6017 ) || lcs_eol_one == -1)
6018 break;
6019
6020 /* When the window is too narrow draw all "@" lines. */
6021 if (draw_state != WL_LINE
6022#ifdef FEAT_DIFF
6023 && filler_todo <= 0
6024#endif
6025 )
6026 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006027 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 row = endrow;
6030 }
6031
6032 /* When line got too long for screen break here. */
6033 if (row == endrow)
6034 {
6035 ++row;
6036 break;
6037 }
6038
6039 if (screen_cur_row == screen_row - 1
6040#ifdef FEAT_DIFF
6041 && filler_todo <= 0
6042#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006043 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
6045 /* Remember that the line wraps, used for modeless copy. */
6046 LineWraps[screen_row - 1] = TRUE;
6047
6048 /*
6049 * Special trick to make copy/paste of wrapped lines work with
6050 * xterm/screen: write an extra character beyond the end of
6051 * the line. This will work with all terminal types
6052 * (regardless of the xn,am settings).
6053 * Only do this on a fast tty.
6054 * Only do this if the cursor is on the current line
6055 * (something has been written in it).
6056 * Don't do this for the GUI.
6057 * Don't do this for double-width characters.
6058 * Don't do this for a window not at the right screen border.
6059 */
6060 if (p_tf
6061#ifdef FEAT_GUI
6062 && !gui.in_use
6063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006065 && ((*mb_off2cells)(LineOffset[screen_row],
6066 LineOffset[screen_row] + screen_Columns)
6067 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006069 + (int)Columns - 2,
6070 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006071 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
6073 /* First make sure we are at the end of the screen line,
6074 * then output the same character again to let the
6075 * terminal know about the wrap. If the terminal doesn't
6076 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006077 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 screen_char(LineOffset[screen_row - 1]
6079 + (unsigned)Columns - 1,
6080 screen_row - 1, (int)(Columns - 1));
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 /* When there is a multi-byte character, just output a
6083 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006084 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6085 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 out_char(' ');
6087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 out_char(ScreenLines[LineOffset[screen_row - 1]
6089 + (Columns - 1)]);
6090 /* force a redraw of the first char on the next line */
6091 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6092 screen_start(); /* don't know where cursor is now */
6093 }
6094 }
6095
6096 col = 0;
6097 off = (unsigned)(current_ScreenLine - ScreenLines);
6098#ifdef FEAT_RIGHTLEFT
6099 if (wp->w_p_rl)
6100 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006101 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 off += col;
6103 }
6104#endif
6105
6106 /* reset the drawing state for the start of a wrapped line */
6107 draw_state = WL_START;
6108 saved_n_extra = n_extra;
6109 saved_p_extra = p_extra;
6110 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006111 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 saved_char_attr = char_attr;
6113 n_extra = 0;
6114 lcs_prec_todo = lcs_prec;
6115#ifdef FEAT_LINEBREAK
6116# ifdef FEAT_DIFF
6117 if (filler_todo <= 0)
6118# endif
6119 need_showbreak = TRUE;
6120#endif
6121#ifdef FEAT_DIFF
6122 --filler_todo;
6123 /* When the filler lines are actually below the last line of the
6124 * file, don't draw the line itself, break here. */
6125 if (filler_todo == 0 && wp->w_botfill)
6126 break;
6127#endif
6128 }
6129
6130 } /* for every character in the line */
6131
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006132#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006133 /* After an empty line check first word for capital. */
6134 if (*skipwhite(line) == NUL)
6135 {
6136 capcol_lnum = lnum + 1;
6137 cap_col = 0;
6138 }
6139#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006140#ifdef FEAT_TEXT_PROP
6141 vim_free(text_props);
6142 vim_free(text_prop_idxs);
6143#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006144
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006145 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 return row;
6147}
6148
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006149/*
6150 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006151 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006152 */
6153 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006154comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006155{
6156 int i;
6157
6158 for (i = 0; i < Screen_mco; ++i)
6159 {
6160 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6161 return TRUE;
6162 if (ScreenLinesC[i][off_from] == 0)
6163 break;
6164 }
6165 return FALSE;
6166}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006167
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168/*
6169 * Check whether the given character needs redrawing:
6170 * - the (first byte of the) character is different
6171 * - the attributes are different
6172 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006173 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 */
6175 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006176char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 if (cols > 0
6179 && ((ScreenLines[off_from] != ScreenLines[off_to]
6180 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 || (enc_dbcs != 0
6182 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6183 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6184 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6185 : (cols > 1 && ScreenLines[off_from + 1]
6186 != ScreenLines[off_to + 1])))
6187 || (enc_utf8
6188 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6189 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006190 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006191 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6192 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006193 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 return TRUE;
6195 return FALSE;
6196}
6197
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006198#if defined(FEAT_TERMINAL) || defined(PROTO)
6199/*
6200 * Return the index in ScreenLines[] for the current screen line.
6201 */
6202 int
6203screen_get_current_line_off()
6204{
6205 return (int)(current_ScreenLine - ScreenLines);
6206}
6207#endif
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209/*
6210 * Move one "cooked" screen line to the screen, but only the characters that
6211 * have actually changed. Handle insert/delete character.
6212 * "coloff" gives the first column on the screen for this line.
6213 * "endcol" gives the columns where valid characters are.
6214 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6215 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006216 * "flags" can have bits:
6217 * SLF_POPUP popup window
6218 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6220 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6221 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006222 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006223screen_line(
6224 int row,
6225 int coloff,
6226 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006227 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006228 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 unsigned off_from;
6231 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006232 unsigned max_off_from;
6233 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 int force = FALSE; /* force update rest of the line */
6237 int redraw_this /* bool: does character need redraw? */
6238#ifdef FEAT_GUI
6239 = TRUE /* For GUI when while-loop empty */
6240#endif
6241 ;
6242 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 int clear_next = FALSE;
6244 int char_cells; /* 1: normal char */
6245 /* 2: occupies two display cells */
6246# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006248 /* Check for illegal row and col, just in case. */
6249 if (row >= Rows)
6250 row = Rows - 1;
6251 if (endcol > Columns)
6252 endcol = Columns;
6253
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254# ifdef FEAT_CLIPBOARD
6255 clip_may_clear_selection(row, row);
6256# endif
6257
6258 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6259 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006260 max_off_from = off_from + screen_Columns;
6261 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006264 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
6266 /* Clear rest first, because it's left of the text. */
6267 if (clear_width > 0)
6268 {
6269 while (col <= endcol && ScreenLines[off_to] == ' '
6270 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006271 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
6273 ++off_to;
6274 ++col;
6275 }
6276 if (col <= endcol)
6277 screen_fill(row, row + 1, col + coloff,
6278 endcol + coloff + 1, ' ', ' ', 0);
6279 }
6280 col = endcol + 1;
6281 off_to = LineOffset[row] + col + coloff;
6282 off_from += col;
6283 endcol = (clear_width > 0 ? clear_width : -clear_width);
6284 }
6285#endif /* FEAT_RIGHTLEFT */
6286
6287 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6288
6289 while (col < endcol)
6290 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006292 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 else
6294 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
6296 redraw_this = redraw_next;
6297 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6298 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6299
6300#ifdef FEAT_GUI
6301 /* If the next character was bold, then redraw the current character to
6302 * remove any pixels that might have spilt over into us. This only
6303 * happens in the GUI.
6304 */
6305 if (redraw_next && gui.in_use)
6306 {
6307 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006308 if (hl > HL_ALL)
6309 hl = syn_attr2attr(hl);
6310 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 redraw_this = TRUE;
6312 }
6313#endif
6314
6315 if (redraw_this)
6316 {
6317 /*
6318 * Special handling when 'xs' termcap flag set (hpterm):
6319 * Attributes for characters are stored at the position where the
6320 * cursor is when writing the highlighting code. The
6321 * start-highlighting code must be written with the cursor on the
6322 * first highlighted character. The stop-highlighting code must
6323 * be written with the cursor just after the last highlighted
6324 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006325 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 * to clear the rest of the line, and force redrawing it
6327 * completely.
6328 */
6329 if ( p_wiv
6330 && !force
6331#ifdef FEAT_GUI
6332 && !gui.in_use
6333#endif
6334 && ScreenAttrs[off_to] != 0
6335 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6336 {
6337 /*
6338 * Need to remove highlighting attributes here.
6339 */
6340 windgoto(row, col + coloff);
6341 out_str(T_CE); /* clear rest of this screen line */
6342 screen_start(); /* don't know where cursor is now */
6343 force = TRUE; /* force redraw of rest of the line */
6344 redraw_next = TRUE; /* or else next char would miss out */
6345
6346 /*
6347 * If the previous character was highlighted, need to stop
6348 * highlighting at this character.
6349 */
6350 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6351 {
6352 screen_attr = ScreenAttrs[off_to - 1];
6353 term_windgoto(row, col + coloff);
6354 screen_stop_highlight();
6355 }
6356 else
6357 screen_attr = 0; /* highlighting has stopped */
6358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (enc_dbcs != 0)
6360 {
6361 /* Check if overwriting a double-byte with a single-byte or
6362 * the other way around requires another character to be
6363 * redrawn. For UTF-8 this isn't needed, because comparing
6364 * ScreenLinesUC[] is sufficient. */
6365 if (char_cells == 1
6366 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006367 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 {
6369 /* Writing a single-cell character over a double-cell
6370 * character: need to redraw the next cell. */
6371 ScreenLines[off_to + 1] = 0;
6372 redraw_next = TRUE;
6373 }
6374 else if (char_cells == 2
6375 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006376 && (*mb_off2cells)(off_to, max_off_to) == 1
6377 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 {
6379 /* Writing the second half of a double-cell character over
6380 * a double-cell character: need to redraw the second
6381 * cell. */
6382 ScreenLines[off_to + 2] = 0;
6383 redraw_next = TRUE;
6384 }
6385
6386 if (enc_dbcs == DBCS_JPNU)
6387 ScreenLines2[off_to] = ScreenLines2[off_from];
6388 }
6389 /* When writing a single-width character over a double-width
6390 * character and at the end of the redrawn text, need to clear out
6391 * the right halve of the old character.
6392 * Also required when writing the right halve of a double-width
6393 * char over the left halve of an existing one. */
6394 if (has_mbyte && col + char_cells == endcol
6395 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006396 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006398 && (*mb_off2cells)(off_to, max_off_to) == 1
6399 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (enc_utf8)
6404 {
6405 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6406 if (ScreenLinesUC[off_from] != 0)
6407 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006408 int i;
6409
6410 for (i = 0; i < Screen_mco; ++i)
6411 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 }
6413 }
6414 if (char_cells == 2)
6415 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006418 /* The bold trick makes a single column of pixels appear in the
6419 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 * character should be redrawn too. This happens for our own GUI
6421 * and for some xterms. */
6422 if (
6423# ifdef FEAT_GUI
6424 gui.in_use
6425# endif
6426# if defined(FEAT_GUI) && defined(UNIX)
6427 ||
6428# endif
6429# ifdef UNIX
6430 term_is_xterm
6431# endif
6432 )
6433 {
6434 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006435 if (hl > HL_ALL)
6436 hl = syn_attr2attr(hl);
6437 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 redraw_next = TRUE;
6439 }
6440#endif
6441 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006442
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006443 /* For simplicity set the attributes of second half of a
6444 * double-wide character equal to the first half. */
6445 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006447
6448 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 screen_char(off_to, row, col + coloff);
6452 }
6453 else if ( p_wiv
6454#ifdef FEAT_GUI
6455 && !gui.in_use
6456#endif
6457 && col + coloff > 0)
6458 {
6459 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6460 {
6461 /*
6462 * Don't output stop-highlight when moving the cursor, it will
6463 * stop the highlighting when it should continue.
6464 */
6465 screen_attr = 0;
6466 }
6467 else if (screen_attr != 0)
6468 screen_stop_highlight();
6469 }
6470
6471 off_to += CHAR_CELLS;
6472 off_from += CHAR_CELLS;
6473 col += CHAR_CELLS;
6474 }
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (clear_next)
6477 {
6478 /* Clear the second half of a double-wide character of which the left
6479 * half was overwritten with a single-wide character. */
6480 ScreenLines[off_to] = ' ';
6481 if (enc_utf8)
6482 ScreenLinesUC[off_to] = 0;
6483 screen_char(off_to, row, col + coloff);
6484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486 if (clear_width > 0
6487#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006488 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489#endif
6490 )
6491 {
6492#ifdef FEAT_GUI
6493 int startCol = col;
6494#endif
6495
6496 /* blank out the rest of the line */
6497 while (col < clear_width && ScreenLines[off_to] == ' '
6498 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006499 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
6501 ++off_to;
6502 ++col;
6503 }
6504 if (col < clear_width)
6505 {
6506#ifdef FEAT_GUI
6507 /*
6508 * In the GUI, clearing the rest of the line may leave pixels
6509 * behind if the first character cleared was bold. Some bold
6510 * fonts spill over the left. In this case we redraw the previous
6511 * character too. If we didn't skip any blanks above, then we
6512 * only redraw if the character wasn't already redrawn anyway.
6513 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006514 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 hl = ScreenAttrs[off_to];
6517 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006518 {
6519 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006520
Bram Moolenaar9c697322006-10-09 20:11:17 +00006521 if (enc_utf8)
6522 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6523 * that its width is 2. */
6524 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6525 else if (enc_dbcs != 0)
6526 {
6527 /* find previous character by counting from first
6528 * column and get its width. */
6529 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006530 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006531
6532 while (off < off_to)
6533 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006534 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006535 off += prev_cells;
6536 }
6537 }
6538
6539 if (enc_dbcs != 0 && prev_cells > 1)
6540 screen_char_2(off_to - prev_cells, row,
6541 col + coloff - prev_cells);
6542 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006543 screen_char(off_to - prev_cells, row,
6544 col + coloff - prev_cells);
6545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547#endif
6548 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6549 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 off_to += clear_width - col;
6551 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
6553 }
6554
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006555 if (clear_width > 0
6556#ifdef FEAT_TEXT_PROP
6557 && !(flags & SLF_POPUP) // no separator for popup window
6558#endif
6559 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006561 // For a window that has a right neighbor, draw the separator char
6562 // right of the window contents.
6563 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {
6565 int c;
6566
6567 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006568 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006569 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6570 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 || ScreenAttrs[off_to] != hl)
6572 {
6573 ScreenLines[off_to] = c;
6574 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (enc_utf8)
6576 {
6577 if (c >= 0x80)
6578 {
6579 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006580 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
6582 else
6583 ScreenLinesUC[off_to] = 0;
6584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 screen_char(off_to, row, col + coloff);
6586 }
6587 }
6588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 LineWraps[row] = FALSE;
6590 }
6591}
6592
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006593#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006595 * Mirror text "str" for right-left displaying.
6596 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006598 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006599rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 char_u *p1, *p2;
6602 int t;
6603
6604 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6605 {
6606 t = *p1;
6607 *p1 = *p2;
6608 *p2 = t;
6609 }
6610}
6611#endif
6612
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613/*
6614 * mark all status lines for redraw; used after first :cd
6615 */
6616 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006617status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 win_T *wp;
6620
Bram Moolenaar29323592016-07-24 22:04:11 +02006621 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 if (wp->w_status_height)
6623 {
6624 wp->w_redr_status = TRUE;
6625 redraw_later(VALID);
6626 }
6627}
6628
6629/*
6630 * mark all status lines of the current buffer for redraw
6631 */
6632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006633status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 win_T *wp;
6636
Bram Moolenaar29323592016-07-24 22:04:11 +02006637 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6639 {
6640 wp->w_redr_status = TRUE;
6641 redraw_later(VALID);
6642 }
6643}
6644
6645/*
6646 * Redraw all status lines that need to be redrawn.
6647 */
6648 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006649redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650{
6651 win_T *wp;
6652
Bram Moolenaar29323592016-07-24 22:04:11 +02006653 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006655 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006656 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006657 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar4033c552017-09-16 20:54:51 +02006660#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661/*
6662 * Redraw all status lines at the bottom of frame "frp".
6663 */
6664 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006665win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 if (frp->fr_layout == FR_LEAF)
6668 frp->fr_win->w_redr_status = TRUE;
6669 else if (frp->fr_layout == FR_ROW)
6670 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006671 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 win_redraw_last_status(frp);
6673 }
6674 else /* frp->fr_layout == FR_COL */
6675 {
6676 frp = frp->fr_child;
6677 while (frp->fr_next != NULL)
6678 frp = frp->fr_next;
6679 win_redraw_last_status(frp);
6680 }
6681}
6682#endif
6683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684/*
6685 * Draw the verticap separator right of window "wp" starting with line "row".
6686 */
6687 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006688draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
6690 int hl;
6691 int c;
6692
6693 if (wp->w_vsep_width)
6694 {
6695 /* draw the vertical separator right of this window */
6696 c = fillchar_vsep(&hl);
6697 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6698 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6699 c, ' ', hl);
6700 }
6701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006704static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006707 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 */
6709 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006710status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 int len = 0;
6713
6714#ifdef FEAT_MENU
6715 int emenu = (xp->xp_context == EXPAND_MENUS
6716 || xp->xp_context == EXPAND_MENUNAMES);
6717
6718 /* Check for menu separators - replace with '|'. */
6719 if (emenu && menu_is_separator(s))
6720 return 1;
6721#endif
6722
6723 while (*s != NUL)
6724 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006725 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006726 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006727 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
6729
6730 return len;
6731}
6732
6733/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006734 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006735 * These are backslashes used for escaping. Do show backslashes in help tags.
6736 */
6737 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006738skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006739{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006740 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006741#ifdef FEAT_MENU
6742 || ((xp->xp_context == EXPAND_MENUS
6743 || xp->xp_context == EXPAND_MENUNAMES)
6744 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6745#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006746 )
6747 {
6748#ifndef BACKSLASH_IN_FILENAME
6749 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6750 return 2;
6751#endif
6752 return 1;
6753 }
6754 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006755}
6756
6757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 * Show wildchar matches in the status line.
6759 * Show at least the "match" item.
6760 * We start at item 'first_match' in the list and show all matches that fit.
6761 *
6762 * If inversion is possible we use it. Else '=' characters are used.
6763 */
6764 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006765win_redr_status_matches(
6766 expand_T *xp,
6767 int num_matches,
6768 char_u **matches, /* list of matches */
6769 int match,
6770 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
6772#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6773 int row;
6774 char_u *buf;
6775 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006776 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 int fillchar;
6778 int attr;
6779 int i;
6780 int highlight = TRUE;
6781 char_u *selstart = NULL;
6782 int selstart_col = 0;
6783 char_u *selend = NULL;
6784 static int first_match = 0;
6785 int add_left = FALSE;
6786 char_u *s;
6787#ifdef FEAT_MENU
6788 int emenu;
6789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
6792 if (matches == NULL) /* interrupted completion? */
6793 return;
6794
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006795 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006796 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006797 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006798 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (buf == NULL)
6800 return;
6801
6802 if (match == -1) /* don't show match but original text */
6803 {
6804 match = 0;
6805 highlight = FALSE;
6806 }
6807 /* count 1 for the ending ">" */
6808 clen = status_match_len(xp, L_MATCH(match)) + 3;
6809 if (match == 0)
6810 first_match = 0;
6811 else if (match < first_match)
6812 {
6813 /* jumping left, as far as we can go */
6814 first_match = match;
6815 add_left = TRUE;
6816 }
6817 else
6818 {
6819 /* check if match fits on the screen */
6820 for (i = first_match; i < match; ++i)
6821 clen += status_match_len(xp, L_MATCH(i)) + 2;
6822 if (first_match > 0)
6823 clen += 2;
6824 /* jumping right, put match at the left */
6825 if ((long)clen > Columns)
6826 {
6827 first_match = match;
6828 /* if showing the last match, we can add some on the left */
6829 clen = 2;
6830 for (i = match; i < num_matches; ++i)
6831 {
6832 clen += status_match_len(xp, L_MATCH(i)) + 2;
6833 if ((long)clen >= Columns)
6834 break;
6835 }
6836 if (i == num_matches)
6837 add_left = TRUE;
6838 }
6839 }
6840 if (add_left)
6841 while (first_match > 0)
6842 {
6843 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6844 if ((long)clen >= Columns)
6845 break;
6846 --first_match;
6847 }
6848
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006849 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
6851 if (first_match == 0)
6852 {
6853 *buf = NUL;
6854 len = 0;
6855 }
6856 else
6857 {
6858 STRCPY(buf, "< ");
6859 len = 2;
6860 }
6861 clen = len;
6862
6863 i = first_match;
6864 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6865 {
6866 if (i == match)
6867 {
6868 selstart = buf + len;
6869 selstart_col = clen;
6870 }
6871
6872 s = L_MATCH(i);
6873 /* Check for menu separators - replace with '|' */
6874#ifdef FEAT_MENU
6875 emenu = (xp->xp_context == EXPAND_MENUS
6876 || xp->xp_context == EXPAND_MENUNAMES);
6877 if (emenu && menu_is_separator(s))
6878 {
6879 STRCPY(buf + len, transchar('|'));
6880 l = (int)STRLEN(buf + len);
6881 len += l;
6882 clen += l;
6883 }
6884 else
6885#endif
6886 for ( ; *s != NUL; ++s)
6887 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006888 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006890 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
6892 STRNCPY(buf + len, s, l);
6893 s += l - 1;
6894 len += l;
6895 }
6896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
6898 STRCPY(buf + len, transchar_byte(*s));
6899 len += (int)STRLEN(buf + len);
6900 }
6901 }
6902 if (i == match)
6903 selend = buf + len;
6904
6905 *(buf + len++) = ' ';
6906 *(buf + len++) = ' ';
6907 clen += 2;
6908 if (++i == num_matches)
6909 break;
6910 }
6911
6912 if (i != num_matches)
6913 {
6914 *(buf + len++) = '>';
6915 ++clen;
6916 }
6917
6918 buf[len] = NUL;
6919
6920 row = cmdline_row - 1;
6921 if (row >= 0)
6922 {
6923 if (wild_menu_showing == 0)
6924 {
6925 if (msg_scrolled > 0)
6926 {
6927 /* Put the wildmenu just above the command line. If there is
6928 * no room, scroll the screen one line up. */
6929 if (cmdline_row == Rows - 1)
6930 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006931 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 ++msg_scrolled;
6933 }
6934 else
6935 {
6936 ++cmdline_row;
6937 ++row;
6938 }
6939 wild_menu_showing = WM_SCROLLED;
6940 }
6941 else
6942 {
6943 /* Create status line if needed by setting 'laststatus' to 2.
6944 * Set 'winminheight' to zero to avoid that the window is
6945 * resized. */
6946 if (lastwin->w_status_height == 0)
6947 {
6948 save_p_ls = p_ls;
6949 save_p_wmh = p_wmh;
6950 p_ls = 2;
6951 p_wmh = 0;
6952 last_status(FALSE);
6953 }
6954 wild_menu_showing = WM_SHOWN;
6955 }
6956 }
6957
6958 screen_puts(buf, row, 0, attr);
6959 if (selstart != NULL && highlight)
6960 {
6961 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006962 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 }
6964
6965 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6966 }
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 vim_free(buf);
6970}
6971#endif
6972
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973/*
6974 * Redraw the status line of window wp.
6975 *
6976 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006977 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6978 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006980 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006981win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 int row;
6984 char_u *p;
6985 int len;
6986 int fillchar;
6987 int attr;
6988 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006989 static int busy = FALSE;
6990
6991 /* It's possible to get here recursively when 'statusline' (indirectly)
6992 * invokes ":redrawstatus". Simply ignore the call then. */
6993 if (busy)
6994 return;
6995 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
6997 wp->w_redr_status = FALSE;
6998 if (wp->w_status_height == 0)
6999 {
7000 /* no status line, can only be last window */
7001 redraw_cmdline = TRUE;
7002 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007003 else if (!redrawing()
7004#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007005 // don't update status line when popup menu is visible and may be
7006 // drawn over it, unless it will be redrawn later
7007 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007008#endif
7009 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 /* Don't redraw right now, do it later. */
7012 wp->w_redr_status = TRUE;
7013 }
7014#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007015 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
7017 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007018 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
7020#endif
7021 else
7022 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007023 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007025 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 p = NameBuff;
7027 len = (int)STRLEN(p);
7028
Bram Moolenaard85f2712017-07-28 21:51:57 +02007029 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030#ifdef FEAT_QUICKFIX
7031 || wp->w_p_pvw
7032#endif
7033 || bufIsChanged(wp->w_buffer)
7034 || wp->w_buffer->b_p_ro)
7035 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007036 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007038 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 len += (int)STRLEN(p + len);
7040 }
7041#ifdef FEAT_QUICKFIX
7042 if (wp->w_p_pvw)
7043 {
7044 STRCPY(p + len, _("[Preview]"));
7045 len += (int)STRLEN(p + len);
7046 }
7047#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007048 if (bufIsChanged(wp->w_buffer)
7049#ifdef FEAT_TERMINAL
7050 && !bt_terminal(wp->w_buffer)
7051#endif
7052 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {
7054 STRCPY(p + len, "[+]");
7055 len += 3;
7056 }
7057 if (wp->w_buffer->b_p_ro)
7058 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007059 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007060 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 }
7062
Bram Moolenaar02631462017-09-22 15:20:32 +02007063 this_ru_col = ru_col - (Columns - wp->w_width);
7064 if (this_ru_col < (wp->w_width + 1) / 2)
7065 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (this_ru_col <= 1)
7067 {
7068 p = (char_u *)"<"; /* No room for file name! */
7069 len = 1;
7070 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007071 else if (has_mbyte)
7072 {
7073 int clen = 0, i;
7074
7075 /* Count total number of display cells. */
7076 clen = mb_string2cells(p, -1);
7077
7078 /* Find first character that will fit.
7079 * Going from start to end is much faster for DBCS. */
7080 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7081 i += (*mb_ptr2len)(p + i))
7082 clen -= (*mb_ptr2cells)(p + i);
7083 len = clen;
7084 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007086 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007088 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 }
7090
Bram Moolenaara12a1612019-01-24 16:39:02 +01007091 }
7092 else if (len > this_ru_col - 1)
7093 {
7094 p += len - (this_ru_col - 1);
7095 *p = '<';
7096 len = this_ru_col - 1;
7097 }
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007100 screen_puts(p, row, wp->w_wincol, attr);
7101 screen_fill(row, row + 1, len + wp->w_wincol,
7102 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007104 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7106 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007107 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007110 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111#endif
7112 }
7113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 /*
7115 * May need to draw the character below the vertical separator.
7116 */
7117 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7118 {
7119 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007120 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 else
7122 fillchar = fillchar_vsep(&attr);
7123 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7124 attr);
7125 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007126 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127}
7128
Bram Moolenaar238a5642006-02-21 22:12:05 +00007129#ifdef FEAT_STL_OPT
7130/*
7131 * Redraw the status line according to 'statusline' and take care of any
7132 * errors encountered.
7133 */
7134 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007135redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007136{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007137 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007138 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007139
7140 /* When called recursively return. This can happen when the statusline
7141 * contains an expression that triggers a redraw. */
7142 if (entered)
7143 return;
7144 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007145
Bram Moolenaara742e082016-04-05 21:10:38 +02007146 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007147 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007148 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007149 {
7150 /* When there is an error disable the statusline, otherwise the
7151 * display is messed up with errors and a redraw triggers the problem
7152 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007153 set_string_option_direct((char_u *)"statusline", -1,
7154 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007155 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007157 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007159}
7160#endif
7161
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162/*
7163 * Return TRUE if the status line of window "wp" is connected to the status
7164 * line of the window right of it. If not, then it's a vertical separator.
7165 * Only call if (wp->w_vsep_width != 0).
7166 */
7167 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007168stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 frame_T *fr;
7171
7172 fr = wp->w_frame;
7173 while (fr->fr_parent != NULL)
7174 {
7175 if (fr->fr_parent->fr_layout == FR_COL)
7176 {
7177 if (fr->fr_next != NULL)
7178 break;
7179 }
7180 else
7181 {
7182 if (fr->fr_next != NULL)
7183 return TRUE;
7184 }
7185 fr = fr->fr_parent;
7186 }
7187 return FALSE;
7188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191/*
7192 * Get the value to show for the language mappings, active 'keymap'.
7193 */
7194 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007195get_keymap_str(
7196 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007197 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007198 char_u *buf, /* buffer for the result */
7199 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 char_u *p;
7202
7203 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7204 return FALSE;
7205
7206 {
7207#ifdef FEAT_EVAL
7208 buf_T *old_curbuf = curbuf;
7209 win_T *old_curwin = curwin;
7210 char_u *s;
7211
7212 curbuf = wp->w_buffer;
7213 curwin = wp;
7214 STRCPY(buf, "b:keymap_name"); /* must be writable */
7215 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007216 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 --emsg_skip;
7218 curbuf = old_curbuf;
7219 curwin = old_curwin;
7220 if (p == NULL || *p == NUL)
7221#endif
7222 {
7223#ifdef FEAT_KEYMAP
7224 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7225 p = wp->w_buffer->b_p_keymap;
7226 else
7227#endif
7228 p = (char_u *)"lang";
7229 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007230 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 buf[0] = NUL;
7232#ifdef FEAT_EVAL
7233 vim_free(s);
7234#endif
7235 }
7236 return buf[0] != NUL;
7237}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
7239#if defined(FEAT_STL_OPT) || defined(PROTO)
7240/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007241 * Redraw the status line or ruler of window "wp".
7242 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 */
7244 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007245win_redr_custom(
7246 win_T *wp,
7247 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007249 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 int attr;
7251 int curattr;
7252 int row;
7253 int col = 0;
7254 int maxwidth;
7255 int width;
7256 int n;
7257 int len;
7258 int fillchar;
7259 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007260 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007262 struct stl_hlrec hltab[STL_MAX_ITEM];
7263 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007264 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007265 win_T *ewp;
7266 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
Bram Moolenaar1d633412013-12-11 15:52:01 +01007268 /* There is a tiny chance that this gets called recursively: When
7269 * redrawing a status line triggers redrawing the ruler or tabline.
7270 * Avoid trouble by not allowing recursion. */
7271 if (entered)
7272 return;
7273 entered = TRUE;
7274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007276 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007278 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007279 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007280 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007281 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007282 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007283 maxwidth = Columns;
7284# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007285 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007288 else
7289 {
7290 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007291 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007292 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007293
7294 if (draw_ruler)
7295 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007296 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007297 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007298 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007299 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007300 if (*++stl == '-')
7301 stl++;
7302 if (atoi((char *)stl))
7303 while (VIM_ISDIGIT(*stl))
7304 stl++;
7305 if (*stl++ != '(')
7306 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007307 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007308 col = ru_col - (Columns - wp->w_width);
7309 if (col < (wp->w_width + 1) / 2)
7310 col = (wp->w_width + 1) / 2;
7311 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007312 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007313 {
7314 row = Rows - 1;
7315 --maxwidth; /* writing in last column may cause scrolling */
7316 fillchar = ' ';
7317 attr = 0;
7318 }
7319
7320# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007321 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322# endif
7323 }
7324 else
7325 {
7326 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007329 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007330# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007331 use_sandbox = was_set_insecurely((char_u *)"statusline",
7332 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007333# endif
7334 }
7335
Bram Moolenaar53f81742017-09-22 14:35:51 +02007336 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007337 }
7338
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007340 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
Bram Moolenaar61452852011-02-01 18:01:11 +01007342 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7343 * the cursor away and back. */
7344 ewp = wp == NULL ? curwin : wp;
7345 p_crb_save = ewp->w_p_crb;
7346 ewp->w_p_crb = FALSE;
7347
Bram Moolenaar362f3562009-11-03 16:20:34 +00007348 /* Make a copy, because the statusline may include a function call that
7349 * might change the option value and free the memory. */
7350 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007351 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007352 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007353 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007354 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007355 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007357 /* Make all characters printable. */
7358 p = transstr(buf);
7359 if (p != NULL)
7360 {
7361 vim_strncpy(buf, p, sizeof(buf) - 1);
7362 vim_free(p);
7363 }
7364
7365 /* fill up with "fillchar" */
7366 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007367 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 ++width;
7371 }
7372 buf[len] = NUL;
7373
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007374 /*
7375 * Draw each snippet with the specified highlighting.
7376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 curattr = attr;
7378 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007381 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 screen_puts_len(p, len, row, col, curattr);
7383 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007384 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007386 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007388 else if (hltab[n].userhl < 0)
7389 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007390#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007391 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7392 && wp->w_status_height != 0)
7393 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007394 else if (wp != NULL && bt_terminal(wp->w_buffer)
7395 && wp->w_status_height != 0)
7396 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007397#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007398 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007399 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007401 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 }
7403 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007404
7405 if (wp == NULL)
7406 {
7407 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7408 col = 0;
7409 len = 0;
7410 p = buf;
7411 fillchar = 0;
7412 for (n = 0; tabtab[n].start != NULL; n++)
7413 {
7414 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7415 while (col < len)
7416 TabPageIdxs[col++] = fillchar;
7417 p = tabtab[n].start;
7418 fillchar = tabtab[n].userhl;
7419 }
7420 while (col < Columns)
7421 TabPageIdxs[col++] = fillchar;
7422 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007423
7424theend:
7425 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426}
7427
7428#endif /* FEAT_STL_OPT */
7429
7430/*
7431 * Output a single character directly to the screen and update ScreenLines.
7432 */
7433 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007434screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 char_u buf[MB_MAXBYTES + 1];
7437
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007438 if (has_mbyte)
7439 buf[(*mb_char2bytes)(c, buf)] = NUL;
7440 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007441 {
7442 buf[0] = c;
7443 buf[1] = NUL;
7444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 screen_puts(buf, row, col, attr);
7446}
7447
7448/*
7449 * Get a single character directly from ScreenLines into "bytes[]".
7450 * Also return its attribute in *attrp;
7451 */
7452 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007453screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
7455 unsigned off;
7456
7457 /* safety check */
7458 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7459 {
7460 off = LineOffset[row] + col;
7461 *attrp = ScreenAttrs[off];
7462 bytes[0] = ScreenLines[off];
7463 bytes[1] = NUL;
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (enc_utf8 && ScreenLinesUC[off] != 0)
7466 bytes[utfc_char2bytes(off, bytes)] = NUL;
7467 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7468 {
7469 bytes[0] = ScreenLines[off];
7470 bytes[1] = ScreenLines2[off];
7471 bytes[2] = NUL;
7472 }
7473 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7474 {
7475 bytes[1] = ScreenLines[off + 1];
7476 bytes[2] = NUL;
7477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479}
7480
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007481/*
7482 * Return TRUE if composing characters for screen posn "off" differs from
7483 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007484 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007485 */
7486 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007487screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007488{
7489 int i;
7490
7491 for (i = 0; i < Screen_mco; ++i)
7492 {
7493 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7494 return TRUE;
7495 if (u8cc[i] == 0)
7496 break;
7497 }
7498 return FALSE;
7499}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501/*
7502 * Put string '*text' on the screen at position 'row' and 'col', with
7503 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7504 * Note: only outputs within one row, message is truncated at screen boundary!
7505 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7506 */
7507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007508screen_puts(
7509 char_u *text,
7510 int row,
7511 int col,
7512 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 screen_puts_len(text, -1, row, col, attr);
7515}
7516
7517/*
7518 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7519 * a NUL.
7520 */
7521 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007522screen_puts_len(
7523 char_u *text,
7524 int textlen,
7525 int row,
7526 int col,
7527 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
7529 unsigned off;
7530 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007531 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007533 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 int mbyte_blen = 1;
7535 int mbyte_cells = 1;
7536 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007537 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007539#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007541 int pc, nc, nc1;
7542 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 int force_redraw_this;
7545 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007546 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547
7548 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7549 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
Bram Moolenaarc236c162008-07-13 17:41:49 +00007552 /* When drawing over the right halve of a double-wide char clear out the
7553 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007554 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007555#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007556 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007557#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007558 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007559 {
7560 ScreenLines[off - 1] = ' ';
7561 ScreenAttrs[off - 1] = 0;
7562 if (enc_utf8)
7563 {
7564 ScreenLinesUC[off - 1] = 0;
7565 ScreenLinesC[0][off - 1] = 0;
7566 }
7567 /* redraw the previous cell, make it empty */
7568 screen_char(off - 1, row, col - 1);
7569 /* force the cell at "col" to be redrawn */
7570 force_redraw_next = TRUE;
7571 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007572
Bram Moolenaar367329b2007-08-30 11:53:22 +00007573 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007574 while (col < screen_Columns
7575 && (len < 0 || (int)(ptr - text) < len)
7576 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {
7578 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 /* check if this is the first byte of a multibyte */
7580 if (has_mbyte)
7581 {
7582 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007583 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007585 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7587 mbyte_cells = 1;
7588 else if (enc_dbcs != 0)
7589 mbyte_cells = mbyte_blen;
7590 else /* enc_utf8 */
7591 {
7592 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007593 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 (int)((text + len) - ptr));
7595 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007596 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007598#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7600 {
7601 /* Do Arabic shaping. */
7602 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7603 {
7604 /* Past end of string to be displayed. */
7605 nc = NUL;
7606 nc1 = NUL;
7607 }
7608 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007609 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007610 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7611 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007612 nc1 = pcc[0];
7613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 pc = prev_c;
7615 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007616 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618 else
7619 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007620#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007621 if (col + mbyte_cells > screen_Columns)
7622 {
7623 /* Only 1 cell left, but character requires 2 cells:
7624 * display a '>' in the last column to avoid wrapping. */
7625 c = '>';
7626 mbyte_cells = 1;
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007631 force_redraw_this = force_redraw_next;
7632 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007633
7634 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 || (mbyte_cells == 2
7636 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7637 || (enc_dbcs == DBCS_JPNU
7638 && c == 0x8e
7639 && ScreenLines2[off] != ptr[1])
7640 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007641 && (ScreenLinesUC[off] !=
7642 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7643 || (ScreenLinesUC[off] != 0
7644 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007646 || exmode_active;
7647
Bram Moolenaara12a1612019-01-24 16:39:02 +01007648 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 {
7650#if defined(FEAT_GUI) || defined(UNIX)
7651 /* The bold trick makes a single row of pixels appear in the next
7652 * character. When a bold character is removed, the next
7653 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007654 * and for some xterms. */
7655 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656# ifdef FEAT_GUI
7657 gui.in_use
7658# endif
7659# if defined(FEAT_GUI) && defined(UNIX)
7660 ||
7661# endif
7662# ifdef UNIX
7663 term_is_xterm
7664# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007665 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007667 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007669 if (n > HL_ALL)
7670 n = syn_attr2attr(n);
7671 if (n & HL_BOLD)
7672 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 }
7674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 /* When at the end of the text and overwriting a two-cell
7676 * character with a one-cell character, need to clear the next
7677 * cell. Also when overwriting the left halve of a two-cell char
7678 * with the right halve of a two-cell char. Do this only once
7679 * (mb_off2cells() may return 2 on the right halve). */
7680 if (clear_next_cell)
7681 clear_next_cell = FALSE;
7682 else if (has_mbyte
7683 && (len < 0 ? ptr[mbyte_blen] == NUL
7684 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007685 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007687 && (*mb_off2cells)(off, max_off) == 1
7688 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 clear_next_cell = TRUE;
7690
7691 /* Make sure we never leave a second byte of a double-byte behind,
7692 * it confuses mb_off2cells(). */
7693 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007694 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007696 && (*mb_off2cells)(off, max_off) == 1
7697 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 ScreenLines[off] = c;
7700 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (enc_utf8)
7702 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007703 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 ScreenLinesUC[off] = 0;
7705 else
7706 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007707 int i;
7708
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007710 for (i = 0; i < Screen_mco; ++i)
7711 {
7712 ScreenLinesC[i][off] = u8cc[i];
7713 if (u8cc[i] == 0)
7714 break;
7715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
7717 if (mbyte_cells == 2)
7718 {
7719 ScreenLines[off + 1] = 0;
7720 ScreenAttrs[off + 1] = attr;
7721 }
7722 screen_char(off, row, col);
7723 }
7724 else if (mbyte_cells == 2)
7725 {
7726 ScreenLines[off + 1] = ptr[1];
7727 ScreenAttrs[off + 1] = attr;
7728 screen_char_2(off, row, col);
7729 }
7730 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7731 {
7732 ScreenLines2[off] = ptr[1];
7733 screen_char(off, row, col);
7734 }
7735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 screen_char(off, row, col);
7737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 if (has_mbyte)
7739 {
7740 off += mbyte_cells;
7741 col += mbyte_cells;
7742 ptr += mbyte_blen;
7743 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007744 {
7745 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007747 len = -1;
7748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 }
7750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 ++off;
7753 ++col;
7754 ++ptr;
7755 }
7756 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007757
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007758 /* If we detected the next character needs to be redrawn, but the text
7759 * doesn't extend up to there, update the character here. */
7760 if (force_redraw_next && col < screen_Columns)
7761 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007762 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7763 screen_char_2(off, row, col);
7764 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007765 screen_char(off, row, col);
7766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767}
7768
7769#ifdef FEAT_SEARCH_EXTRA
7770/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007771 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 */
7773 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007774start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
7776 if (p_hls && !no_hlsearch)
7777 {
7778 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007779 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007780# ifdef FEAT_RELTIME
7781 /* Set the time limit to 'redrawtime'. */
7782 profile_setlimit(p_rdt, &search_hl.tm);
7783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
7785}
7786
7787/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007788 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 */
7790 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007791end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 if (search_hl.rm.regprog != NULL)
7794 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007795 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 search_hl.rm.regprog = NULL;
7797 }
7798}
7799
7800/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007801 * Init for calling prepare_search_hl().
7802 */
7803 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007804init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007805{
7806 matchitem_T *cur;
7807
7808 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7809 * match */
7810 cur = wp->w_match_head;
7811 while (cur != NULL)
7812 {
7813 cur->hl.rm = cur->match;
7814 if (cur->hlg_id == 0)
7815 cur->hl.attr = 0;
7816 else
7817 cur->hl.attr = syn_id2attr(cur->hlg_id);
7818 cur->hl.buf = wp->w_buffer;
7819 cur->hl.lnum = 0;
7820 cur->hl.first_lnum = 0;
7821# ifdef FEAT_RELTIME
7822 /* Set the time limit to 'redrawtime'. */
7823 profile_setlimit(p_rdt, &(cur->hl.tm));
7824# endif
7825 cur = cur->next;
7826 }
7827 search_hl.buf = wp->w_buffer;
7828 search_hl.lnum = 0;
7829 search_hl.first_lnum = 0;
7830 /* time limit is set at the toplevel, for all windows */
7831}
7832
7833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 * Advance to the match in window "wp" line "lnum" or past it.
7835 */
7836 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007837prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007839 matchitem_T *cur; /* points to the match list */
7840 match_T *shl; /* points to search_hl or a match */
7841 int shl_flag; /* flag to indicate whether search_hl
7842 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007843 int pos_inprogress; /* marks that position match search is
7844 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 int n;
7846
7847 /*
7848 * When using a multi-line pattern, start searching at the top
7849 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007850 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007852 cur = wp->w_match_head;
7853 shl_flag = FALSE;
7854 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 if (shl_flag == FALSE)
7857 {
7858 shl = &search_hl;
7859 shl_flag = TRUE;
7860 }
7861 else
7862 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (shl->rm.regprog != NULL
7864 && shl->lnum == 0
7865 && re_multiline(shl->rm.regprog))
7866 {
7867 if (shl->first_lnum == 0)
7868 {
7869# ifdef FEAT_FOLDING
7870 for (shl->first_lnum = lnum;
7871 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7872 if (hasFoldingWin(wp, shl->first_lnum - 1,
7873 NULL, NULL, TRUE, NULL))
7874 break;
7875# else
7876 shl->first_lnum = wp->w_topline;
7877# endif
7878 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007879 if (cur != NULL)
7880 cur->pos.cur = 0;
7881 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007883 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7884 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007886 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7887 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007888 pos_inprogress = cur == NULL || cur->pos.cur == 0
7889 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (shl->lnum != 0)
7891 {
7892 shl->first_lnum = shl->lnum
7893 + shl->rm.endpos[0].lnum
7894 - shl->rm.startpos[0].lnum;
7895 n = shl->rm.endpos[0].col;
7896 }
7897 else
7898 {
7899 ++shl->first_lnum;
7900 n = 0;
7901 }
7902 }
7903 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007904 if (shl != &search_hl && cur != NULL)
7905 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 }
7907}
7908
7909/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007910 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 * Uses shl->buf.
7912 * Sets shl->lnum and shl->rm contents.
7913 * Note: Assumes a previous match is always before "lnum", unless
7914 * shl->lnum is zero.
7915 * Careful: Any pointers for buffer lines will become invalid.
7916 */
7917 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007918next_search_hl(
7919 win_T *win,
7920 match_T *shl, /* points to search_hl or a match */
7921 linenr_T lnum,
7922 colnr_T mincol, /* minimal column for a match */
7923 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924{
7925 linenr_T l;
7926 colnr_T matchcol;
7927 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007928 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007930 // for :{range}s/pat only highlight inside the range
7931 if (lnum < search_first_line || lnum > search_last_line)
7932 {
7933 shl->lnum = 0;
7934 return;
7935 }
7936
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 if (shl->lnum != 0)
7938 {
7939 /* Check for three situations:
7940 * 1. If the "lnum" is below a previous match, start a new search.
7941 * 2. If the previous match includes "mincol", use it.
7942 * 3. Continue after the previous match.
7943 */
7944 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7945 if (lnum > l)
7946 shl->lnum = 0;
7947 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7948 return;
7949 }
7950
7951 /*
7952 * Repeat searching for a match until one is found that includes "mincol"
7953 * or none is found in this line.
7954 */
7955 called_emsg = FALSE;
7956 for (;;)
7957 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007958#ifdef FEAT_RELTIME
7959 /* Stop searching after passing the time limit. */
7960 if (profile_passed_limit(&(shl->tm)))
7961 {
7962 shl->lnum = 0; /* no match found in time */
7963 break;
7964 }
7965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 /* Three situations:
7967 * 1. No useful previous match: search from start of line.
7968 * 2. Not Vi compatible or empty match: continue at next character.
7969 * Break the loop if this is beyond the end of the line.
7970 * 3. Vi compatible searching: continue at end of previous match.
7971 */
7972 if (shl->lnum == 0)
7973 matchcol = 0;
7974 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7975 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007976 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007978 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007979
7980 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007981 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007984 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 shl->lnum = 0;
7986 break;
7987 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007988 if (has_mbyte)
7989 matchcol += mb_ptr2len(ml);
7990 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007991 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 }
7993 else
7994 matchcol = shl->rm.endpos[0].col;
7995
7996 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007999 /* Remember whether shl->rm is using a copy of the regprog in
8000 * cur->match. */
8001 int regprog_is_copy = (shl != &search_hl && cur != NULL
8002 && shl == &cur->hl
8003 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008004 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008005
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8007 matchcol,
8008#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008009 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008010#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008011 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012#endif
8013 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008014 /* Copy the regprog, in case it got freed and recompiled. */
8015 if (regprog_is_copy)
8016 cur->match.regprog = cur->hl.rm.regprog;
8017
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008018 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008019 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008020 /* Error while handling regexp: stop using this regexp. */
8021 if (shl == &search_hl)
8022 {
8023 /* don't free regprog in the match list, it's a copy */
8024 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008025 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026 }
8027 shl->rm.regprog = NULL;
8028 shl->lnum = 0;
8029 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8030 message */
8031 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008032 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008033 }
8034 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008035 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008036 else
8037 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (nmatched == 0)
8039 {
8040 shl->lnum = 0; /* no match found */
8041 break;
8042 }
8043 if (shl->rm.startpos[0].lnum > 0
8044 || shl->rm.startpos[0].col >= mincol
8045 || nmatched > 1
8046 || shl->rm.endpos[0].col > mincol)
8047 {
8048 shl->lnum += shl->rm.startpos[0].lnum;
8049 break; /* useful match found */
8050 }
8051 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008052
8053 // Restore called_emsg for assert_fails().
8054 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
Bram Moolenaar85077472016-10-16 14:35:48 +02008057/*
8058 * If there is a match fill "shl" and return one.
8059 * Return zero otherwise.
8060 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008061 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008062next_search_hl_pos(
8063 match_T *shl, /* points to a match */
8064 linenr_T lnum,
8065 posmatch_T *posmatch, /* match positions */
8066 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008067{
8068 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008069 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008070
Bram Moolenaarb3414592014-06-17 17:48:32 +02008071 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8072 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008073 llpos_T *pos = &posmatch->pos[i];
8074
8075 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008077 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008078 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008079 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008081 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008083 /* if this match comes before the one at "found" then swap
8084 * them */
8085 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008087 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088
Bram Moolenaar85077472016-10-16 14:35:48 +02008089 *pos = posmatch->pos[found];
8090 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008091 }
8092 }
8093 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008094 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008095 }
8096 }
8097 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008098 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008099 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008100 colnr_T start = posmatch->pos[found].col == 0
8101 ? 0 : posmatch->pos[found].col - 1;
8102 colnr_T end = posmatch->pos[found].col == 0
8103 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008104
Bram Moolenaar85077472016-10-16 14:35:48 +02008105 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008106 shl->rm.startpos[0].lnum = 0;
8107 shl->rm.startpos[0].col = start;
8108 shl->rm.endpos[0].lnum = 0;
8109 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008110 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008111 posmatch->cur = found + 1;
8112 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008114 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008115}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008116#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008117
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008119screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120{
8121 attrentry_T *aep = NULL;
8122
8123 screen_attr = attr;
8124 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008125#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 && termcap_active
8127#endif
8128 )
8129 {
8130#ifdef FEAT_GUI
8131 if (gui.in_use)
8132 {
8133 char buf[20];
8134
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008135 /* The GUI handles this internally. */
8136 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 OUT_STR(buf);
8138 }
8139 else
8140#endif
8141 {
8142 if (attr > HL_ALL) /* special HL attr. */
8143 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008144 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 aep = syn_cterm_attr2entry(attr);
8146 else
8147 aep = syn_term_attr2entry(attr);
8148 if (aep == NULL) /* did ":syntax clear" */
8149 attr = 0;
8150 else
8151 attr = aep->ae_attr;
8152 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008153 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008155 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008156#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008157 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8158 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8159 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008160#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008162 /* If the Normal FG color has BOLD attribute and the new HL
8163 * has a FG color defined, clear BOLD. */
8164 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008165 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008167 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008168 out_str(T_UCS);
8169 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008170 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8171 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008173 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008175 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008177 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008178 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 /*
8181 * Output the color or start string after bold etc., in case the
8182 * bold etc. override the color setting.
8183 */
8184 if (aep != NULL)
8185 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008186#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008187 /* When 'termguicolors' is set but fg or bg is unset,
8188 * fall back to the cterm colors. This helps for SpellBad,
8189 * where the GUI uses a red undercurl. */
8190 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008192 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008193 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008194 }
8195 else
8196#endif
8197 if (t_colors > 1)
8198 {
8199 if (aep->ae_u.cterm.fg_color)
8200 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8201 }
8202#ifdef FEAT_TERMGUICOLORS
8203 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8204 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008205 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008206 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
8208 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008209#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008210 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008212 if (aep->ae_u.cterm.bg_color)
8213 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8214 }
8215
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008216 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008217 {
8218 if (aep->ae_u.term.start != NULL)
8219 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221 }
8222 }
8223 }
8224}
8225
8226 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008227screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 int do_ME = FALSE; /* output T_ME code */
8230
8231 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008232#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 && termcap_active
8234#endif
8235 )
8236 {
8237#ifdef FEAT_GUI
8238 if (gui.in_use)
8239 {
8240 char buf[20];
8241
8242 /* use internal GUI code */
8243 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8244 OUT_STR(buf);
8245 }
8246 else
8247#endif
8248 {
8249 if (screen_attr > HL_ALL) /* special HL attr. */
8250 {
8251 attrentry_T *aep;
8252
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008253 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {
8255 /*
8256 * Assume that t_me restores the original colors!
8257 */
8258 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008259 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008260#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008261 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8262 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8263 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008264#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008266#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008267 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8268 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8269 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008270#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008271 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 do_ME = TRUE;
8273 }
8274 else
8275 {
8276 aep = syn_term_attr2entry(screen_attr);
8277 if (aep != NULL && aep->ae_u.term.stop != NULL)
8278 {
8279 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8280 do_ME = TRUE;
8281 else
8282 out_str(aep->ae_u.term.stop);
8283 }
8284 }
8285 if (aep == NULL) /* did ":syntax clear" */
8286 screen_attr = 0;
8287 else
8288 screen_attr = aep->ae_attr;
8289 }
8290
8291 /*
8292 * Often all ending-codes are equal to T_ME. Avoid outputting the
8293 * same sequence several times.
8294 */
8295 if (screen_attr & HL_STANDOUT)
8296 {
8297 if (STRCMP(T_SE, T_ME) == 0)
8298 do_ME = TRUE;
8299 else
8300 out_str(T_SE);
8301 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008302 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008303 {
8304 if (STRCMP(T_UCE, T_ME) == 0)
8305 do_ME = TRUE;
8306 else
8307 out_str(T_UCE);
8308 }
8309 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008310 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
8312 if (STRCMP(T_UE, T_ME) == 0)
8313 do_ME = TRUE;
8314 else
8315 out_str(T_UE);
8316 }
8317 if (screen_attr & HL_ITALIC)
8318 {
8319 if (STRCMP(T_CZR, T_ME) == 0)
8320 do_ME = TRUE;
8321 else
8322 out_str(T_CZR);
8323 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008324 if (screen_attr & HL_STRIKETHROUGH)
8325 {
8326 if (STRCMP(T_STE, T_ME) == 0)
8327 do_ME = TRUE;
8328 else
8329 out_str(T_STE);
8330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8332 out_str(T_ME);
8333
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008334#ifdef FEAT_TERMGUICOLORS
8335 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008337 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008338 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008339 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008340 term_bg_rgb_color(cterm_normal_bg_gui_color);
8341 }
8342 else
8343#endif
8344 {
8345 if (t_colors > 1)
8346 {
8347 /* set Normal cterm colors */
8348 if (cterm_normal_fg_color != 0)
8349 term_fg_color(cterm_normal_fg_color - 1);
8350 if (cterm_normal_bg_color != 0)
8351 term_bg_color(cterm_normal_bg_color - 1);
8352 if (cterm_normal_fg_bold)
8353 out_str(T_MD);
8354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
8356 }
8357 }
8358 screen_attr = 0;
8359}
8360
8361/*
8362 * Reset the colors for a cterm. Used when leaving Vim.
8363 * The machine specific code may override this again.
8364 */
8365 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008366reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008368 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {
8370 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008371#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008372 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8373 || cterm_normal_bg_gui_color != INVALCOLOR)
8374 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008375#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {
8379 out_str(T_OP);
8380 screen_attr = -1;
8381 }
8382 if (cterm_normal_fg_bold)
8383 {
8384 out_str(T_ME);
8385 screen_attr = -1;
8386 }
8387 }
8388}
8389
8390/*
8391 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8392 * using the attributes from ScreenAttrs["off"].
8393 */
8394 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008395screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 int attr;
8398
8399 /* Check for illegal values, just in case (could happen just after
8400 * resizing). */
8401 if (row >= screen_Rows || col >= screen_Columns)
8402 return;
8403
Bram Moolenaarae654382019-01-17 21:09:05 +01008404#ifdef FEAT_INS_EXPAND
8405 if (pum_under_menu(row, col))
8406 return;
8407#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008408 /* Outputting a character in the last cell on the screen may scroll the
8409 * screen up. Only do it when the "xn" termcap property is set, otherwise
8410 * mark the character invalid (update it when scrolled up). */
8411 if (*T_XN == NUL
8412 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413#ifdef FEAT_RIGHTLEFT
8414 /* account for first command-line character in rightleft mode */
8415 && !cmdmsg_rl
8416#endif
8417 )
8418 {
8419 ScreenAttrs[off] = (sattr_T)-1;
8420 return;
8421 }
8422
8423 /*
8424 * Stop highlighting first, so it's easier to move the cursor.
8425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 if (screen_char_attr != 0)
8427 attr = screen_char_attr;
8428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 attr = ScreenAttrs[off];
8430 if (screen_attr != attr)
8431 screen_stop_highlight();
8432
8433 windgoto(row, col);
8434
8435 if (screen_attr != attr)
8436 screen_start_highlight(attr);
8437
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 if (enc_utf8 && ScreenLinesUC[off] != 0)
8439 {
8440 char_u buf[MB_MAXBYTES + 1];
8441
Bram Moolenaarcb070082016-04-02 22:14:51 +02008442 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008443 {
8444 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008445#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008446 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008447#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008448 )
8449 {
8450 /* Clear the two screen cells. If the character is actually
8451 * single width it won't change the second cell. */
8452 out_str((char_u *)" ");
8453 term_windgoto(row, col);
8454 }
8455 /* not sure where the cursor is after drawing the ambiguous width
8456 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008457 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008458 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008459 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008461
8462 /* Convert the UTF-8 character to bytes and write it. */
8463 buf[utfc_char2bytes(off, buf)] = NUL;
8464 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 }
8466 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 /* double-byte character in single-width cell */
8471 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8472 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 }
8474
8475 screen_cur_col++;
8476}
8477
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478/*
8479 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8480 * on the screen at position 'row' and 'col'.
8481 * The attributes of the first byte is used for all. This is required to
8482 * output the two bytes of a double-byte character with nothing in between.
8483 */
8484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008485screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
8487 /* Check for illegal values (could be wrong when screen was resized). */
8488 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8489 return;
8490
8491 /* Outputting the last character on the screen may scrollup the screen.
8492 * Don't to it! Mark the character invalid (update it when scrolled up) */
8493 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8494 {
8495 ScreenAttrs[off] = (sattr_T)-1;
8496 return;
8497 }
8498
8499 /* Output the first byte normally (positions the cursor), then write the
8500 * second byte directly. */
8501 screen_char(off, row, col);
8502 out_char(ScreenLines[off + 1]);
8503 ++screen_cur_col;
8504}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506/*
8507 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8508 * This uses the contents of ScreenLines[] and doesn't change it.
8509 */
8510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008511screen_draw_rectangle(
8512 int row,
8513 int col,
8514 int height,
8515 int width,
8516 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 int r, c;
8519 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008520 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008522 /* Can't use ScreenLines unless initialized */
8523 if (ScreenLines == NULL)
8524 return;
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (invert)
8527 screen_char_attr = HL_INVERSE;
8528 for (r = row; r < row + height; ++r)
8529 {
8530 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008531 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 for (c = col; c < col + width; ++c)
8533 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008534 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {
8536 screen_char_2(off + c, r, c);
8537 ++c;
8538 }
8539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {
8541 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008542 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
8545 }
8546 }
8547 screen_char_attr = 0;
8548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550/*
8551 * Redraw the characters for a vertically split window.
8552 */
8553 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008554redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 int col;
8557 int width;
8558
8559# ifdef FEAT_CLIPBOARD
8560 clip_may_clear_selection(row, end - 1);
8561# endif
8562
8563 if (wp == NULL)
8564 {
8565 col = 0;
8566 width = Columns;
8567 }
8568 else
8569 {
8570 col = wp->w_wincol;
8571 width = wp->w_width;
8572 }
8573 screen_draw_rectangle(row, col, end - row, width, FALSE);
8574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008576 static void
8577space_to_screenline(int off, int attr)
8578{
8579 ScreenLines[off] = ' ';
8580 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008581 if (enc_utf8)
8582 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008583}
8584
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585/*
8586 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8587 * with character 'c1' in first column followed by 'c2' in the other columns.
8588 * Use attributes 'attr'.
8589 */
8590 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008591screen_fill(
8592 int start_row,
8593 int end_row,
8594 int start_col,
8595 int end_col,
8596 int c1,
8597 int c2,
8598 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 int row;
8601 int col;
8602 int off;
8603 int end_off;
8604 int did_delete;
8605 int c;
8606 int norm_term;
8607#if defined(FEAT_GUI) || defined(UNIX)
8608 int force_next = FALSE;
8609#endif
8610
8611 if (end_row > screen_Rows) /* safety check */
8612 end_row = screen_Rows;
8613 if (end_col > screen_Columns) /* safety check */
8614 end_col = screen_Columns;
8615 if (ScreenLines == NULL
8616 || start_row >= end_row
8617 || start_col >= end_col) /* nothing to do */
8618 return;
8619
8620 /* it's a "normal" terminal when not in a GUI or cterm */
8621 norm_term = (
8622#ifdef FEAT_GUI
8623 !gui.in_use &&
8624#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008625 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 for (row = start_row; row < end_row; ++row)
8627 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008628 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008629#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008630 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008631#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008632 )
8633 {
8634 /* When drawing over the right halve of a double-wide char clear
8635 * out the left halve. When drawing over the left halve of a
8636 * double wide-char clear out the right halve. Only needed in a
8637 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008638 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008639 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008640 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008641 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 /*
8644 * Try to use delete-line termcap code, when no attributes or in a
8645 * "normal" terminal, where a bold/italic space is just a
8646 * space.
8647 */
8648 did_delete = FALSE;
8649 if (c2 == ' '
8650 && end_col == Columns
8651 && can_clear(T_CE)
8652 && (attr == 0
8653 || (norm_term
8654 && attr <= HL_ALL
8655 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8656 {
8657 /*
8658 * check if we really need to clear something
8659 */
8660 col = start_col;
8661 if (c1 != ' ') /* don't clear first char */
8662 ++col;
8663
8664 off = LineOffset[row] + col;
8665 end_off = LineOffset[row] + end_col;
8666
8667 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 if (enc_utf8)
8669 while (off < end_off && ScreenLines[off] == ' '
8670 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8671 ++off;
8672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 while (off < end_off && ScreenLines[off] == ' '
8674 && ScreenAttrs[off] == 0)
8675 ++off;
8676 if (off < end_off) /* something to be cleared */
8677 {
8678 col = off - LineOffset[row];
8679 screen_stop_highlight();
8680 term_windgoto(row, col);/* clear rest of this screen line */
8681 out_str(T_CE);
8682 screen_start(); /* don't know where cursor is now */
8683 col = end_col - col;
8684 while (col--) /* clear chars in ScreenLines */
8685 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008686 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 ++off;
8688 }
8689 }
8690 did_delete = TRUE; /* the chars are cleared now */
8691 }
8692
8693 off = LineOffset[row] + start_col;
8694 c = c1;
8695 for (col = start_col; col < end_col; ++col)
8696 {
8697 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008698 || (enc_utf8 && (int)ScreenLinesUC[off]
8699 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 || ScreenAttrs[off] != attr
8701#if defined(FEAT_GUI) || defined(UNIX)
8702 || force_next
8703#endif
8704 )
8705 {
8706#if defined(FEAT_GUI) || defined(UNIX)
8707 /* The bold trick may make a single row of pixels appear in
8708 * the next character. When a bold character is removed, the
8709 * next character should be redrawn too. This happens for our
8710 * own GUI and for some xterms. */
8711 if (
8712# ifdef FEAT_GUI
8713 gui.in_use
8714# endif
8715# if defined(FEAT_GUI) && defined(UNIX)
8716 ||
8717# endif
8718# ifdef UNIX
8719 term_is_xterm
8720# endif
8721 )
8722 {
8723 if (ScreenLines[off] != ' '
8724 && (ScreenAttrs[off] > HL_ALL
8725 || ScreenAttrs[off] & HL_BOLD))
8726 force_next = TRUE;
8727 else
8728 force_next = FALSE;
8729 }
8730#endif
8731 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (enc_utf8)
8733 {
8734 if (c >= 0x80)
8735 {
8736 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008737 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 }
8739 else
8740 ScreenLinesUC[off] = 0;
8741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 ScreenAttrs[off] = attr;
8743 if (!did_delete || c != ' ')
8744 screen_char(off, row, col);
8745 }
8746 ++off;
8747 if (col == start_col)
8748 {
8749 if (did_delete)
8750 break;
8751 c = c2;
8752 }
8753 }
8754 if (end_col == Columns)
8755 LineWraps[row] = FALSE;
8756 if (row == Rows - 1) /* overwritten the command line */
8757 {
8758 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008759 if (start_col == 0 && end_col == Columns
8760 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008762 if (start_col == 0)
8763 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
8765 }
8766}
8767
8768/*
8769 * Check if there should be a delay. Used before clearing or redrawing the
8770 * screen or the command line.
8771 */
8772 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008773check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8776 && !did_wait_return
8777 && emsg_silent == 0)
8778 {
8779 out_flush();
8780 ui_delay(1000L, TRUE);
8781 emsg_on_display = FALSE;
8782 if (check_msg_scroll)
8783 msg_scroll = FALSE;
8784 }
8785}
8786
8787/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008788 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8789 */
8790 static void
8791clear_TabPageIdxs(void)
8792{
8793 int scol;
8794
8795 for (scol = 0; scol < Columns; ++scol)
8796 TabPageIdxs[scol] = 0;
8797}
8798
8799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008801 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 * Returns TRUE if there is a valid screen to write to.
8803 * Returns FALSE when starting up and screen not initialized yet.
8804 */
8805 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008806screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008808 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 return (ScreenLines != NULL);
8810}
8811
8812/*
8813 * Resize the shell to Rows and Columns.
8814 * Allocate ScreenLines[] and associated items.
8815 *
8816 * There may be some time between setting Rows and Columns and (re)allocating
8817 * ScreenLines[]. This happens when starting up and when (manually) changing
8818 * the shell size. Always use screen_Rows and screen_Columns to access items
8819 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8820 * final size of the shell is needed.
8821 */
8822 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008823screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 int new_row, old_row;
8826#ifdef FEAT_GUI
8827 int old_Rows;
8828#endif
8829 win_T *wp;
8830 int outofmem = FALSE;
8831 int len;
8832 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008834 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008836 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 sattr_T *new_ScreenAttrs;
8838 unsigned *new_LineOffset;
8839 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008840 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008841 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008843 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008844 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008846retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 /*
8848 * Allocation of the screen buffers is done only when the size changes and
8849 * when Rows and Columns have been set and we have started doing full
8850 * screen stuff.
8851 */
8852 if ((ScreenLines != NULL
8853 && Rows == screen_Rows
8854 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 && enc_utf8 == (ScreenLinesUC != NULL)
8856 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008857 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 || Rows == 0
8859 || Columns == 0
8860 || (!full_screen && ScreenLines == NULL))
8861 return;
8862
8863 /*
8864 * It's possible that we produce an out-of-memory message below, which
8865 * will cause this function to be called again. To break the loop, just
8866 * return here.
8867 */
8868 if (entered)
8869 return;
8870 entered = TRUE;
8871
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008872 /*
8873 * Note that the window sizes are updated before reallocating the arrays,
8874 * thus we must not redraw here!
8875 */
8876 ++RedrawingDisabled;
8877
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 win_new_shellsize(); /* fit the windows in the new sized shell */
8879
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 comp_col(); /* recompute columns for shown command and ruler */
8881
8882 /*
8883 * We're changing the size of the screen.
8884 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8885 * - Move lines from the old arrays into the new arrays, clear extra
8886 * lines (unless the screen is going to be cleared).
8887 * - Free the old arrays.
8888 *
8889 * If anything fails, make ScreenLines NULL, so we don't do anything!
8890 * Continuing with the old ScreenLines may result in a crash, because the
8891 * size is wrong.
8892 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008895 if (aucmd_win != NULL)
8896 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008898 new_ScreenLines = (schar_T *)lalloc(
8899 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008900 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 if (enc_utf8)
8902 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008903 new_ScreenLinesUC = (u8char_T *)lalloc(
8904 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008905 for (i = 0; i < p_mco; ++i)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008906 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear(
8907 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 }
8909 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008910 new_ScreenLines2 = (schar_T *)lalloc(
8911 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
8912 new_ScreenAttrs = (sattr_T *)lalloc(
8913 (Rows + 1) * Columns * sizeof(sattr_T), FALSE);
8914 new_LineOffset = (unsigned *)lalloc(Rows * sizeof(unsigned), FALSE);
8915 new_LineWraps = (char_u *)lalloc(Rows * sizeof(char_u), FALSE);
8916 new_TabPageIdxs = (short *)lalloc(Columns * sizeof(short), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008918 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
8920 if (win_alloc_lines(wp) == FAIL)
8921 {
8922 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008923 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 }
8925 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008926 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8927 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008928 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008929give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008931 for (i = 0; i < p_mco; ++i)
8932 if (new_ScreenLinesC[i] == NULL)
8933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008935 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 || new_ScreenAttrs == NULL
8938 || new_LineOffset == NULL
8939 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008940 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 || outofmem)
8942 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008943 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008944 {
8945 /* guess the size */
8946 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8947
8948 /* Remember we did this to avoid getting outofmem messages over
8949 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008950 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008951 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008952 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008953 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008954 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008955 VIM_CLEAR(new_ScreenLinesC[i]);
8956 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008957 VIM_CLEAR(new_ScreenAttrs);
8958 VIM_CLEAR(new_LineOffset);
8959 VIM_CLEAR(new_LineWraps);
8960 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962 else
8963 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008964 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 for (new_row = 0; new_row < Rows; ++new_row)
8967 {
8968 new_LineOffset[new_row] = new_row * Columns;
8969 new_LineWraps[new_row] = FALSE;
8970
8971 /*
8972 * If the screen is not going to be cleared, copy as much as
8973 * possible from the old screen to the new one and clear the rest
8974 * (used when resizing the window at the "--more--" prompt or when
8975 * executing an external command, for the GUI).
8976 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008977 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 {
8979 (void)vim_memset(new_ScreenLines + new_row * Columns,
8980 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 if (enc_utf8)
8982 {
8983 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8984 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008985 for (i = 0; i < p_mco; ++i)
8986 (void)vim_memset(new_ScreenLinesC[i]
8987 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 0, (size_t)Columns * sizeof(u8char_T));
8989 }
8990 if (enc_dbcs == DBCS_JPNU)
8991 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8992 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8994 0, (size_t)Columns * sizeof(sattr_T));
8995 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008996 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 {
8998 if (screen_Columns < Columns)
8999 len = screen_Columns;
9000 else
9001 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009002 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009003 * may be invalid now. Also when p_mco changes. */
9004 if (!(enc_utf8 && ScreenLinesUC == NULL)
9005 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009006 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9007 ScreenLines + LineOffset[old_row],
9008 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 if (enc_utf8 && ScreenLinesUC != NULL
9010 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 {
9012 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9013 ScreenLinesUC + LineOffset[old_row],
9014 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009015 for (i = 0; i < p_mco; ++i)
9016 mch_memmove(new_ScreenLinesC[i]
9017 + new_LineOffset[new_row],
9018 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 (size_t)len * sizeof(u8char_T));
9020 }
9021 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9022 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9023 ScreenLines2 + LineOffset[old_row],
9024 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9026 ScreenAttrs + LineOffset[old_row],
9027 (size_t)len * sizeof(sattr_T));
9028 }
9029 }
9030 }
9031 /* Use the last line of the screen for the current line. */
9032 current_ScreenLine = new_ScreenLines + Rows * Columns;
9033 }
9034
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009035 free_screenlines();
9036
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009039 for (i = 0; i < p_mco; ++i)
9040 ScreenLinesC[i] = new_ScreenLinesC[i];
9041 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 ScreenAttrs = new_ScreenAttrs;
9044 LineOffset = new_LineOffset;
9045 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009046 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048 /* It's important that screen_Rows and screen_Columns reflect the actual
9049 * size of ScreenLines[]. Set them before calling anything. */
9050#ifdef FEAT_GUI
9051 old_Rows = screen_Rows;
9052#endif
9053 screen_Rows = Rows;
9054 screen_Columns = Columns;
9055
9056 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009057 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059#ifdef FEAT_GUI
9060 else if (gui.in_use
9061 && !gui.starting
9062 && ScreenLines != NULL
9063 && old_Rows != Rows)
9064 {
9065 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9066 /*
9067 * Adjust the position of the cursor, for when executing an external
9068 * command.
9069 */
9070 if (msg_row >= Rows) /* Rows got smaller */
9071 msg_row = Rows - 1; /* put cursor at last row */
9072 else if (Rows > old_Rows) /* Rows got bigger */
9073 msg_row += Rows - old_Rows; /* put cursor in same place */
9074 if (msg_col >= Columns) /* Columns got smaller */
9075 msg_col = Columns - 1; /* put cursor at last column */
9076 }
9077#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009078 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009081 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009082
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009083 /*
9084 * Do not apply autocommands more than 3 times to avoid an endless loop
9085 * in case applying autocommands always changes Rows or Columns.
9086 */
9087 if (starting == 0 && ++retry_count <= 3)
9088 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009089 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009090 /* In rare cases, autocommands may have altered Rows or Columns,
9091 * jump back to check if we need to allocate the screen again. */
9092 goto retry;
9093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094}
9095
9096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009097free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009098{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009099 int i;
9100
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009101 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009102 for (i = 0; i < Screen_mco; ++i)
9103 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009104 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009105 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009106 vim_free(ScreenAttrs);
9107 vim_free(LineOffset);
9108 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009109 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009110}
9111
9112 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009113screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
9115 check_for_delay(FALSE);
9116 screenalloc(FALSE); /* allocate screen buffers if size changed */
9117 screenclear2(); /* clear the screen */
9118}
9119
9120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009121screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 int i;
9124
9125 if (starting == NO_SCREEN || ScreenLines == NULL
9126#ifdef FEAT_GUI
9127 || (gui.in_use && gui.starting)
9128#endif
9129 )
9130 return;
9131
9132#ifdef FEAT_GUI
9133 if (!gui.in_use)
9134#endif
9135 screen_attr = -1; /* force setting the Normal colors */
9136 screen_stop_highlight(); /* don't want highlighting here */
9137
9138#ifdef FEAT_CLIPBOARD
9139 /* disable selection without redrawing it */
9140 clip_scroll_selection(9999);
9141#endif
9142
9143 /* blank out ScreenLines */
9144 for (i = 0; i < Rows; ++i)
9145 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009146 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 LineWraps[i] = FALSE;
9148 }
9149
9150 if (can_clear(T_CL))
9151 {
9152 out_str(T_CL); /* clear the display */
9153 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009154 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 }
9156 else
9157 {
9158 /* can't clear the screen, mark all chars with invalid attributes */
9159 for (i = 0; i < Rows; ++i)
9160 lineinvalid(LineOffset[i], (int)Columns);
9161 clear_cmdline = TRUE;
9162 }
9163
9164 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9165
9166 win_rest_invalid(firstwin);
9167 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009168 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 if (must_redraw == CLEAR) /* no need to clear again */
9170 must_redraw = NOT_VALID;
9171 compute_cmdrow();
9172 msg_row = cmdline_row; /* put cursor on last line for messages */
9173 msg_col = 0;
9174 screen_start(); /* don't know where cursor is now */
9175 msg_scrolled = 0; /* can't scroll back */
9176 msg_didany = FALSE;
9177 msg_didout = FALSE;
9178}
9179
9180/*
9181 * Clear one line in ScreenLines.
9182 */
9183 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009184lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 if (enc_utf8)
9188 (void)vim_memset(ScreenLinesUC + off, 0,
9189 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009190 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192
9193/*
9194 * Mark one line in ScreenLines invalid by setting the attributes to an
9195 * invalid value.
9196 */
9197 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009198lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9201}
9202
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203/*
9204 * Copy part of a Screenline for vertically split window "wp".
9205 */
9206 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009207linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 unsigned off_to = LineOffset[to] + wp->w_wincol;
9210 unsigned off_from = LineOffset[from] + wp->w_wincol;
9211
9212 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9213 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 if (enc_utf8)
9215 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009216 int i;
9217
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9219 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009220 for (i = 0; i < p_mco; ++i)
9221 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9222 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 }
9224 if (enc_dbcs == DBCS_JPNU)
9225 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9226 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9228 wp->w_width * sizeof(sattr_T));
9229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
9231/*
9232 * Return TRUE if clearing with term string "p" would work.
9233 * It can't work when the string is empty or it won't set the right background.
9234 */
9235 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009236can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 return (*p != NUL && (t_colors <= 1
9239#ifdef FEAT_GUI
9240 || gui.in_use
9241#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009242#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009243 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009244 || (!p_tgc && cterm_normal_bg_color == 0)
9245#else
9246 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009247#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009248 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
9251/*
9252 * Reset cursor position. Use whenever cursor was moved because of outputting
9253 * something directly to the screen (shell commands) or a terminal control
9254 * code.
9255 */
9256 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009257screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 screen_cur_row = screen_cur_col = 9999;
9260}
9261
9262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 * Move the cursor to position "row","col" in the screen.
9264 * This tries to find the most efficient way to move, minimizing the number of
9265 * characters sent to the terminal.
9266 */
9267 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009268windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009270 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 int i;
9272 int plan;
9273 int cost;
9274 int wouldbe_col;
9275 int noinvcurs;
9276 char_u *bs;
9277 int goto_cost;
9278 int attr;
9279
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009280#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9282
9283#define PLAN_LE 1
9284#define PLAN_CR 2
9285#define PLAN_NL 3
9286#define PLAN_WRITE 4
9287 /* Can't use ScreenLines unless initialized */
9288 if (ScreenLines == NULL)
9289 return;
9290
9291 if (col != screen_cur_col || row != screen_cur_row)
9292 {
9293 /* Check for valid position. */
9294 if (row < 0) /* window without text lines? */
9295 row = 0;
9296 if (row >= screen_Rows)
9297 row = screen_Rows - 1;
9298 if (col >= screen_Columns)
9299 col = screen_Columns - 1;
9300
9301 /* check if no cursor movement is allowed in highlight mode */
9302 if (screen_attr && *T_MS == NUL)
9303 noinvcurs = HIGHL_COST;
9304 else
9305 noinvcurs = 0;
9306 goto_cost = GOTO_COST + noinvcurs;
9307
9308 /*
9309 * Plan how to do the positioning:
9310 * 1. Use CR to move it to column 0, same row.
9311 * 2. Use T_LE to move it a few columns to the left.
9312 * 3. Use NL to move a few lines down, column 0.
9313 * 4. Move a few columns to the right with T_ND or by writing chars.
9314 *
9315 * Don't do this if the cursor went beyond the last column, the cursor
9316 * position is unknown then (some terminals wrap, some don't )
9317 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009318 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 * characters to move the cursor to the right.
9320 */
9321 if (row >= screen_cur_row && screen_cur_col < Columns)
9322 {
9323 /*
9324 * If the cursor is in the same row, bigger col, we can use CR
9325 * or T_LE.
9326 */
9327 bs = NULL; /* init for GCC */
9328 attr = screen_attr;
9329 if (row == screen_cur_row && col < screen_cur_col)
9330 {
9331 /* "le" is preferred over "bc", because "bc" is obsolete */
9332 if (*T_LE)
9333 bs = T_LE; /* "cursor left" */
9334 else
9335 bs = T_BC; /* "backspace character (old) */
9336 if (*bs)
9337 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9338 else
9339 cost = 999;
9340 if (col + 1 < cost) /* using CR is less characters */
9341 {
9342 plan = PLAN_CR;
9343 wouldbe_col = 0;
9344 cost = 1; /* CR is just one character */
9345 }
9346 else
9347 {
9348 plan = PLAN_LE;
9349 wouldbe_col = col;
9350 }
9351 if (noinvcurs) /* will stop highlighting */
9352 {
9353 cost += noinvcurs;
9354 attr = 0;
9355 }
9356 }
9357
9358 /*
9359 * If the cursor is above where we want to be, we can use CR LF.
9360 */
9361 else if (row > screen_cur_row)
9362 {
9363 plan = PLAN_NL;
9364 wouldbe_col = 0;
9365 cost = (row - screen_cur_row) * 2; /* CR LF */
9366 if (noinvcurs) /* will stop highlighting */
9367 {
9368 cost += noinvcurs;
9369 attr = 0;
9370 }
9371 }
9372
9373 /*
9374 * If the cursor is in the same row, smaller col, just use write.
9375 */
9376 else
9377 {
9378 plan = PLAN_WRITE;
9379 wouldbe_col = screen_cur_col;
9380 cost = 0;
9381 }
9382
9383 /*
9384 * Check if any characters that need to be written have the
9385 * correct attributes. Also avoid UTF-8 characters.
9386 */
9387 i = col - wouldbe_col;
9388 if (i > 0)
9389 cost += i;
9390 if (cost < goto_cost && i > 0)
9391 {
9392 /*
9393 * Check if the attributes are correct without additionally
9394 * stopping highlighting.
9395 */
9396 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9397 while (i && *p++ == attr)
9398 --i;
9399 if (i != 0)
9400 {
9401 /*
9402 * Try if it works when highlighting is stopped here.
9403 */
9404 if (*--p == 0)
9405 {
9406 cost += noinvcurs;
9407 while (i && *p++ == 0)
9408 --i;
9409 }
9410 if (i != 0)
9411 cost = 999; /* different attributes, don't do it */
9412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 if (enc_utf8)
9414 {
9415 /* Don't use an UTF-8 char for positioning, it's slow. */
9416 for (i = wouldbe_col; i < col; ++i)
9417 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9418 {
9419 cost = 999;
9420 break;
9421 }
9422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 }
9424
9425 /*
9426 * We can do it without term_windgoto()!
9427 */
9428 if (cost < goto_cost)
9429 {
9430 if (plan == PLAN_LE)
9431 {
9432 if (noinvcurs)
9433 screen_stop_highlight();
9434 while (screen_cur_col > col)
9435 {
9436 out_str(bs);
9437 --screen_cur_col;
9438 }
9439 }
9440 else if (plan == PLAN_CR)
9441 {
9442 if (noinvcurs)
9443 screen_stop_highlight();
9444 out_char('\r');
9445 screen_cur_col = 0;
9446 }
9447 else if (plan == PLAN_NL)
9448 {
9449 if (noinvcurs)
9450 screen_stop_highlight();
9451 while (screen_cur_row < row)
9452 {
9453 out_char('\n');
9454 ++screen_cur_row;
9455 }
9456 screen_cur_col = 0;
9457 }
9458
9459 i = col - screen_cur_col;
9460 if (i > 0)
9461 {
9462 /*
9463 * Use cursor-right if it's one character only. Avoids
9464 * removing a line of pixels from the last bold char, when
9465 * using the bold trick in the GUI.
9466 */
9467 if (T_ND[0] != NUL && T_ND[1] == NUL)
9468 {
9469 while (i-- > 0)
9470 out_char(*T_ND);
9471 }
9472 else
9473 {
9474 int off;
9475
9476 off = LineOffset[row] + screen_cur_col;
9477 while (i-- > 0)
9478 {
9479 if (ScreenAttrs[off] != screen_attr)
9480 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (enc_dbcs == DBCS_JPNU
9484 && ScreenLines[off] == 0x8e)
9485 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 ++off;
9487 }
9488 }
9489 }
9490 }
9491 }
9492 else
9493 cost = 999;
9494
9495 if (cost >= goto_cost)
9496 {
9497 if (noinvcurs)
9498 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009499 if (row == screen_cur_row && (col > screen_cur_col)
9500 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 term_cursor_right(col - screen_cur_col);
9502 else
9503 term_windgoto(row, col);
9504 }
9505 screen_cur_row = row;
9506 screen_cur_col = col;
9507 }
9508}
9509
9510/*
9511 * Set cursor to its position in the current window.
9512 */
9513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009514setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009516 setcursor_mayforce(FALSE);
9517}
9518
9519/*
9520 * Set cursor to its position in the current window.
9521 * When "force" is TRUE also when not redrawing.
9522 */
9523 void
9524setcursor_mayforce(int force)
9525{
9526 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 {
9528 validate_cursor();
9529 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009530 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009532 /* With 'rightleft' set and the cursor on a double-wide
9533 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009534 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9535 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009536 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009537 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538#endif
9539 curwin->w_wcol));
9540 }
9541}
9542
9543
9544/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009545 * Insert 'line_count' lines at 'row' in window 'wp'.
9546 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9547 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 * scrolling.
9549 * Returns FAIL if the lines are not inserted, OK for success.
9550 */
9551 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009552win_ins_lines(
9553 win_T *wp,
9554 int row,
9555 int line_count,
9556 int invalid,
9557 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558{
9559 int did_delete;
9560 int nextrow;
9561 int lastrow;
9562 int retval;
9563
9564 if (invalid)
9565 wp->w_lines_valid = 0;
9566
9567 if (wp->w_height < 5)
9568 return FAIL;
9569
9570 if (line_count > wp->w_height - row)
9571 line_count = wp->w_height - row;
9572
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009573 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 if (retval != MAYBE)
9575 return retval;
9576
9577 /*
9578 * If there is a next window or a status line, we first try to delete the
9579 * lines at the bottom to avoid messing what is after the window.
9580 * If this fails and there are following windows, don't do anything to avoid
9581 * messing up those windows, better just redraw.
9582 */
9583 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 if (wp->w_next != NULL || wp->w_status_height)
9585 {
9586 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009587 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 did_delete = TRUE;
9589 else if (wp->w_next)
9590 return FAIL;
9591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 /*
9593 * if no lines deleted, blank the lines that will end up below the window
9594 */
9595 if (!did_delete)
9596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009599 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 lastrow = nextrow + line_count;
9601 if (lastrow > Rows)
9602 lastrow = Rows;
9603 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009604 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 ' ', ' ', 0);
9606 }
9607
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009608 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 == FAIL)
9610 {
9611 /* deletion will have messed up other windows */
9612 if (did_delete)
9613 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 win_rest_invalid(W_NEXT(wp));
9616 }
9617 return FAIL;
9618 }
9619
9620 return OK;
9621}
9622
9623/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009624 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9626 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9627 * scrolling
9628 * Return OK for success, FAIL if the lines are not deleted.
9629 */
9630 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009631win_del_lines(
9632 win_T *wp,
9633 int row,
9634 int line_count,
9635 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009636 int mayclear,
9637 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 int retval;
9640
9641 if (invalid)
9642 wp->w_lines_valid = 0;
9643
9644 if (line_count > wp->w_height - row)
9645 line_count = wp->w_height - row;
9646
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009647 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (retval != MAYBE)
9649 return retval;
9650
9651 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009652 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 return FAIL;
9654
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 /*
9656 * If there are windows or status lines below, try to put them at the
9657 * correct place. If we can't do that, they have to be redrawn.
9658 */
9659 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9660 {
9661 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009662 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 {
9664 wp->w_redr_status = TRUE;
9665 win_rest_invalid(wp->w_next);
9666 }
9667 }
9668 /*
9669 * If this is the last window and there is no status line, redraw the
9670 * command line later.
9671 */
9672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 redraw_cmdline = TRUE;
9674 return OK;
9675}
9676
9677/*
9678 * Common code for win_ins_lines() and win_del_lines().
9679 * Returns OK or FAIL when the work has been done.
9680 * Returns MAYBE when not finished yet.
9681 */
9682 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009683win_do_lines(
9684 win_T *wp,
9685 int row,
9686 int line_count,
9687 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009688 int del,
9689 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
9691 int retval;
9692
9693 if (!redrawing() || line_count <= 0)
9694 return FAIL;
9695
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009696 /* When inserting lines would result in loss of command output, just redraw
9697 * the lines. */
9698 if (no_win_do_lines_ins && !del)
9699 return FAIL;
9700
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009702 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009704 if (!no_win_do_lines_ins)
9705 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 return FAIL;
9707 }
9708
9709 /*
9710 * Delete all remaining lines
9711 */
9712 if (row + line_count >= wp->w_height)
9713 {
9714 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009715 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 ' ', ' ', 0);
9717 return OK;
9718 }
9719
9720 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009721 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009723 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009725 if (!no_win_do_lines_ins)
9726 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727
9728 /*
9729 * If the terminal can set a scroll region, use that.
9730 * Always do this in a vertically split window. This will redraw from
9731 * ScreenLines[] when t_CV isn't defined. That's faster than using
9732 * win_line().
9733 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009734 * a character in the lower right corner of the scroll region may cause a
9735 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009737 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 scroll_region_set(wp, row);
9741 if (del)
9742 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009743 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 else
9745 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009746 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 scroll_region_reset();
9749 return retval;
9750 }
9751
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9753 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754
9755 return MAYBE;
9756}
9757
9758/*
9759 * window 'wp' and everything after it is messed up, mark it for redraw
9760 */
9761 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009762win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 {
9766 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 wp->w_redr_status = TRUE;
9768 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 }
9770 redraw_cmdline = TRUE;
9771}
9772
9773/*
9774 * The rest of the routines in this file perform screen manipulations. The
9775 * given operation is performed physically on the screen. The corresponding
9776 * change is also made to the internal screen image. In this way, the editor
9777 * anticipates the effect of editing changes on the appearance of the screen.
9778 * That way, when we call screenupdate a complete redraw isn't usually
9779 * necessary. Another advantage is that we can keep adding code to anticipate
9780 * screen changes, and in the meantime, everything still works.
9781 */
9782
9783/*
9784 * types for inserting or deleting lines
9785 */
9786#define USE_T_CAL 1
9787#define USE_T_CDL 2
9788#define USE_T_AL 3
9789#define USE_T_CE 4
9790#define USE_T_DL 5
9791#define USE_T_SR 6
9792#define USE_NL 7
9793#define USE_T_CD 8
9794#define USE_REDRAW 9
9795
9796/*
9797 * insert lines on the screen and update ScreenLines[]
9798 * 'end' is the line after the scrolled part. Normally it is Rows.
9799 * When scrolling region used 'off' is the offset from the top for the region.
9800 * 'row' and 'end' are relative to the start of the region.
9801 *
9802 * return FAIL for failure, OK for success.
9803 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009804 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009805screen_ins_lines(
9806 int off,
9807 int row,
9808 int line_count,
9809 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009810 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009811 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813 int i;
9814 int j;
9815 unsigned temp;
9816 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009817 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 int type;
9819 int result_empty;
9820 int can_ce = can_clear(T_CE);
9821
9822 /*
9823 * FAIL if
9824 * - there is no valid screen
9825 * - the screen has to be redrawn completely
9826 * - the line count is less than one
9827 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009828 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009830 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9831#ifdef FEAT_CLIPBOARD
9832 || (clip_star.state != SELECT_CLEARED
9833 && redrawing_for_callback > 0)
9834#endif
9835 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 return FAIL;
9837
9838 /*
9839 * There are seven ways to insert lines:
9840 * 0. When in a vertically split window and t_CV isn't set, redraw the
9841 * characters from ScreenLines[].
9842 * 1. Use T_CD (clear to end of display) if it exists and the result of
9843 * the insert is just empty lines
9844 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9845 * present or line_count > 1. It looks better if we do all the inserts
9846 * at once.
9847 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9848 * insert is just empty lines and T_CE is not present or line_count >
9849 * 1.
9850 * 4. Use T_AL (insert line) if it exists.
9851 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9852 * just empty lines.
9853 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9854 * just empty lines.
9855 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9856 * the 'da' flag is not set or we have clear line capability.
9857 * 8. redraw the characters from ScreenLines[].
9858 *
9859 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9860 * the scrollbar for the window. It does have insert line, use that if it
9861 * exists.
9862 */
9863 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9865 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009866 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 type = USE_T_CD;
9868 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9869 type = USE_T_CAL;
9870 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9871 type = USE_T_CDL;
9872 else if (*T_AL != NUL)
9873 type = USE_T_AL;
9874 else if (can_ce && result_empty)
9875 type = USE_T_CE;
9876 else if (*T_DL != NUL && result_empty)
9877 type = USE_T_DL;
9878 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9879 type = USE_T_SR;
9880 else
9881 return FAIL;
9882
9883 /*
9884 * For clearing the lines screen_del_lines() is used. This will also take
9885 * care of t_db if necessary.
9886 */
9887 if (type == USE_T_CD || type == USE_T_CDL ||
9888 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009889 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 /*
9892 * If text is retained below the screen, first clear or delete as many
9893 * lines at the bottom of the window as are about to be inserted so that
9894 * the deleted lines won't later surface during a screen_del_lines.
9895 */
9896 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009897 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898
9899#ifdef FEAT_CLIPBOARD
9900 /* Remove a modeless selection when inserting lines halfway the screen
9901 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009902 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009903 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 else
9905 clip_scroll_selection(-line_count);
9906#endif
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908#ifdef FEAT_GUI
9909 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9910 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009911 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#endif
9913
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009914 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9915 cursor_col = wp->w_wincol;
9916
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (*T_CCS != NUL) /* cursor relative to region */
9918 cursor_row = row;
9919 else
9920 cursor_row = row + off;
9921
9922 /*
9923 * Shift LineOffset[] line_count down to reflect the inserted lines.
9924 * Clear the inserted lines in ScreenLines[].
9925 */
9926 row += off;
9927 end += off;
9928 for (i = 0; i < line_count; ++i)
9929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (wp != NULL && wp->w_width != Columns)
9931 {
9932 /* need to copy part of a line */
9933 j = end - 1 - i;
9934 while ((j -= line_count) >= row)
9935 linecopy(j + line_count, j, wp);
9936 j += line_count;
9937 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009938 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9939 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 else
9941 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9942 LineWraps[j] = FALSE;
9943 }
9944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 {
9946 j = end - 1 - i;
9947 temp = LineOffset[j];
9948 while ((j -= line_count) >= row)
9949 {
9950 LineOffset[j + line_count] = LineOffset[j];
9951 LineWraps[j + line_count] = LineWraps[j];
9952 }
9953 LineOffset[j + line_count] = temp;
9954 LineWraps[j + line_count] = FALSE;
9955 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009956 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 else
9958 lineinvalid(temp, (int)Columns);
9959 }
9960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961
9962 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009963 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009964 if (clear_attr != 0)
9965 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 /* redraw the characters */
9968 if (type == USE_REDRAW)
9969 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009970 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 {
9972 term_append_lines(line_count);
9973 screen_start(); /* don't know where cursor is now */
9974 }
9975 else
9976 {
9977 for (i = 0; i < line_count; i++)
9978 {
9979 if (type == USE_T_AL)
9980 {
9981 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009982 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 out_str(T_AL);
9984 }
9985 else /* type == USE_T_SR */
9986 out_str(T_SR);
9987 screen_start(); /* don't know where cursor is now */
9988 }
9989 }
9990
9991 /*
9992 * With scroll-reverse and 'da' flag set we need to clear the lines that
9993 * have been scrolled down into the region.
9994 */
9995 if (type == USE_T_SR && *T_DA)
9996 {
9997 for (i = 0; i < line_count; ++i)
9998 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009999 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 out_str(T_CE);
10001 screen_start(); /* don't know where cursor is now */
10002 }
10003 }
10004
10005#ifdef FEAT_GUI
10006 gui_can_update_cursor();
10007 if (gui.in_use)
10008 out_flush(); /* always flush after a scroll */
10009#endif
10010 return OK;
10011}
10012
10013/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010014 * Delete lines on the screen and update ScreenLines[].
10015 * "end" is the line after the scrolled part. Normally it is Rows.
10016 * When scrolling region used "off" is the offset from the top for the region.
10017 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 *
10019 * Return OK for success, FAIL if the lines are not deleted.
10020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010022screen_del_lines(
10023 int off,
10024 int row,
10025 int line_count,
10026 int end,
10027 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010028 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010029 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
10031 int j;
10032 int i;
10033 unsigned temp;
10034 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010035 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 int cursor_end;
10037 int result_empty; /* result is empty until end of region */
10038 int can_delete; /* deleting line codes can be used */
10039 int type;
10040
10041 /*
10042 * FAIL if
10043 * - there is no valid screen
10044 * - the screen has to be redrawn completely
10045 * - the line count is less than one
10046 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010047 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010049 if (!screen_valid(TRUE) || line_count <= 0
10050 || (!force && line_count > p_ttyscroll)
10051#ifdef FEAT_CLIPBOARD
10052 || (clip_star.state != SELECT_CLEARED
10053 && redrawing_for_callback > 0)
10054#endif
10055 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 return FAIL;
10057
10058 /*
10059 * Check if the rest of the current region will become empty.
10060 */
10061 result_empty = row + line_count >= end;
10062
10063 /*
10064 * We can delete lines only when 'db' flag not set or when 'ce' option
10065 * available.
10066 */
10067 can_delete = (*T_DB == NUL || can_clear(T_CE));
10068
10069 /*
10070 * There are six ways to delete lines:
10071 * 0. When in a vertically split window and t_CV isn't set, redraw the
10072 * characters from ScreenLines[].
10073 * 1. Use T_CD if it exists and the result is empty.
10074 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10075 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10076 * none of the other ways work.
10077 * 4. Use T_CE (erase line) if the result is empty.
10078 * 5. Use T_DL (delete line) if it exists.
10079 * 6. redraw the characters from ScreenLines[].
10080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10082 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010083 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 type = USE_T_CD;
10085#if defined(__BEOS__) && defined(BEOS_DR8)
10086 /*
10087 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10088 * its internal termcap... this works okay for tests which test *T_DB !=
10089 * NUL. It has the disadvantage that the user cannot use any :set t_*
10090 * command to get T_DB (back) to empty_option, only :set term=... will do
10091 * the trick...
10092 * Anyway, this hack will hopefully go away with the next OS release.
10093 * (Olaf Seibert)
10094 */
10095 else if (row == 0 && T_DB == empty_option
10096 && (line_count == 1 || *T_CDL == NUL))
10097#else
10098 else if (row == 0 && (
10099#ifndef AMIGA
10100 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10101 * up, so use delete-line command */
10102 line_count == 1 ||
10103#endif
10104 *T_CDL == NUL))
10105#endif
10106 type = USE_NL;
10107 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10108 type = USE_T_CDL;
10109 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010110 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 type = USE_T_CE;
10112 else if (*T_DL != NUL && can_delete)
10113 type = USE_T_DL;
10114 else if (*T_CDL != NUL && can_delete)
10115 type = USE_T_CDL;
10116 else
10117 return FAIL;
10118
10119#ifdef FEAT_CLIPBOARD
10120 /* Remove a modeless selection when deleting lines halfway the screen or
10121 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010122 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010123 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 else
10125 clip_scroll_selection(line_count);
10126#endif
10127
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128#ifdef FEAT_GUI
10129 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10130 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010131 gui_dont_update_cursor(gui.cursor_row >= row + off
10132 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133#endif
10134
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010135 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10136 cursor_col = wp->w_wincol;
10137
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 if (*T_CCS != NUL) /* cursor relative to region */
10139 {
10140 cursor_row = row;
10141 cursor_end = end;
10142 }
10143 else
10144 {
10145 cursor_row = row + off;
10146 cursor_end = end + off;
10147 }
10148
10149 /*
10150 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10151 * Clear the inserted lines in ScreenLines[].
10152 */
10153 row += off;
10154 end += off;
10155 for (i = 0; i < line_count; ++i)
10156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 if (wp != NULL && wp->w_width != Columns)
10158 {
10159 /* need to copy part of a line */
10160 j = row + i;
10161 while ((j += line_count) <= end - 1)
10162 linecopy(j - line_count, j, wp);
10163 j -= line_count;
10164 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010165 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10166 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 else
10168 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10169 LineWraps[j] = FALSE;
10170 }
10171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 {
10173 /* whole width, moving the line pointers is faster */
10174 j = row + i;
10175 temp = LineOffset[j];
10176 while ((j += line_count) <= end - 1)
10177 {
10178 LineOffset[j - line_count] = LineOffset[j];
10179 LineWraps[j - line_count] = LineWraps[j];
10180 }
10181 LineOffset[j - line_count] = temp;
10182 LineWraps[j - line_count] = FALSE;
10183 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010184 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 else
10186 lineinvalid(temp, (int)Columns);
10187 }
10188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010190 if (screen_attr != clear_attr)
10191 screen_stop_highlight();
10192 if (clear_attr != 0)
10193 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 /* redraw the characters */
10196 if (type == USE_REDRAW)
10197 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010198 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010200 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 out_str(T_CD);
10202 screen_start(); /* don't know where cursor is now */
10203 }
10204 else if (type == USE_T_CDL)
10205 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010206 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 term_delete_lines(line_count);
10208 screen_start(); /* don't know where cursor is now */
10209 }
10210 /*
10211 * Deleting lines at top of the screen or scroll region: Just scroll
10212 * the whole screen (scroll region) up by outputting newlines on the
10213 * last line.
10214 */
10215 else if (type == USE_NL)
10216 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010217 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 for (i = line_count; --i >= 0; )
10219 out_char('\n'); /* cursor will remain on same line */
10220 }
10221 else
10222 {
10223 for (i = line_count; --i >= 0; )
10224 {
10225 if (type == USE_T_DL)
10226 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010227 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 out_str(T_DL); /* delete a line */
10229 }
10230 else /* type == USE_T_CE */
10231 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010232 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 out_str(T_CE); /* erase a line */
10234 }
10235 screen_start(); /* don't know where cursor is now */
10236 }
10237 }
10238
10239 /*
10240 * If the 'db' flag is set, we need to clear the lines that have been
10241 * scrolled up at the bottom of the region.
10242 */
10243 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10244 {
10245 for (i = line_count; i > 0; --i)
10246 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010247 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 out_str(T_CE); /* erase a line */
10249 screen_start(); /* don't know where cursor is now */
10250 }
10251 }
10252
10253#ifdef FEAT_GUI
10254 gui_can_update_cursor();
10255 if (gui.in_use)
10256 out_flush(); /* always flush after a scroll */
10257#endif
10258
10259 return OK;
10260}
10261
10262/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010263 * Return TRUE when postponing displaying the mode message: when not redrawing
10264 * or inside a mapping.
10265 */
10266 int
10267skip_showmode()
10268{
10269 // Call char_avail() only when we are going to show something, because it
10270 // takes a bit of time. redrawing() may also call char_avail_avail().
10271 if (global_busy
10272 || msg_silent != 0
10273 || !redrawing()
10274 || (char_avail() && !KeyTyped))
10275 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010276 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010277 return TRUE;
10278 }
10279 return FALSE;
10280}
10281
10282/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010283 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 *
10285 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10286 * If clear_cmdline is FALSE there may be a message there that needs to be
10287 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010288 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 * Return the length of the message (0 if no message).
10290 */
10291 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010292showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293{
10294 int need_clear;
10295 int length = 0;
10296 int do_mode;
10297 int attr;
10298 int nwr_save;
10299#ifdef FEAT_INS_EXPAND
10300 int sub_attr;
10301#endif
10302
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010303 do_mode = ((p_smd && msg_silent == 0)
10304 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010305 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010306 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010307 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010309 if (skip_showmode())
10310 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311
10312 nwr_save = need_wait_return;
10313
10314 /* wait a bit before overwriting an important message */
10315 check_for_delay(FALSE);
10316
10317 /* if the cmdline is more than one line high, erase top lines */
10318 need_clear = clear_cmdline;
10319 if (clear_cmdline && cmdline_row < Rows - 1)
10320 msg_clr_cmdline(); /* will reset clear_cmdline */
10321
10322 /* Position on the last line in the window, column 0 */
10323 msg_pos_mode();
10324 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010325 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 if (do_mode)
10327 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010328 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010330 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010331# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010332 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010333# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010335# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010336 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010337# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010338 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010340 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341# endif
10342#endif
10343#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10344 if (gui.in_use)
10345 {
10346 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010347 {
10348 /* HANGUL */
10349 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010350 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010351 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010352 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 }
10355#endif
10356#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010357 /* CTRL-X in Insert mode */
10358 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 {
10360 /* These messages can get long, avoid a wrap in a narrow
10361 * window. Prefer showing edit_submode_extra. */
10362 length = (Rows - msg_row) * Columns - 3;
10363 if (edit_submode_extra != NULL)
10364 length -= vim_strsize(edit_submode_extra);
10365 if (length > 0)
10366 {
10367 if (edit_submode_pre != NULL)
10368 length -= vim_strsize(edit_submode_pre);
10369 if (length - vim_strsize(edit_submode) > 0)
10370 {
10371 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010372 msg_puts_attr((char *)edit_submode_pre, attr);
10373 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 }
10375 if (edit_submode_extra != NULL)
10376 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010377 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010379 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 else
10381 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010382 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 }
10384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386 else
10387#endif
10388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010390 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010391 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010392 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 else if (State & INSERT)
10394 {
10395#ifdef FEAT_RIGHTLEFT
10396 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010397 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010399 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010401 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010402 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010404 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010406 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407#ifdef FEAT_RIGHTLEFT
10408 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010409 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#endif
10411#ifdef FEAT_KEYMAP
10412 if (State & LANGMAP)
10413 {
10414# ifdef FEAT_ARABIC
10415 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010416 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 else
10418# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010419 if (get_keymap_str(curwin, (char_u *)" (%s)",
10420 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010421 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 }
10423#endif
10424 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010425 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 if (VIsual_active)
10428 {
10429 char *p;
10430
10431 /* Don't concatenate separate words to avoid translation
10432 * problems. */
10433 switch ((VIsual_select ? 4 : 0)
10434 + (VIsual_mode == Ctrl_V) * 2
10435 + (VIsual_mode == 'V'))
10436 {
10437 case 0: p = N_(" VISUAL"); break;
10438 case 1: p = N_(" VISUAL LINE"); break;
10439 case 2: p = N_(" VISUAL BLOCK"); break;
10440 case 4: p = N_(" SELECT"); break;
10441 case 5: p = N_(" SELECT LINE"); break;
10442 default: p = N_(" SELECT BLOCK"); break;
10443 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010444 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010446 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010448
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 need_clear = TRUE;
10450 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010451 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452#ifdef FEAT_INS_EXPAND
10453 && edit_submode == NULL /* otherwise it gets too long */
10454#endif
10455 )
10456 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010457 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 need_clear = TRUE;
10459 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010460
10461 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010462 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 msg_clr_eos();
10464 msg_didout = FALSE; /* overwrite this message */
10465 length = msg_col;
10466 msg_col = 0;
10467 need_wait_return = nwr_save; /* never ask for hit-return for this */
10468 }
10469 else if (clear_cmdline && msg_silent == 0)
10470 /* Clear the whole command line. Will reset "clear_cmdline". */
10471 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010472 else if (redraw_mode)
10473 {
10474 msg_pos_mode();
10475 msg_clr_eos();
10476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477
10478#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 /* In Visual mode the size of the selected area must be redrawn. */
10480 if (VIsual_active)
10481 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
10483 /* If the last window has no status line, the ruler is after the mode
10484 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010485 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010486 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487#endif
10488 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010489 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 clear_cmdline = FALSE;
10491
10492 return length;
10493}
10494
10495/*
10496 * Position for a mode message.
10497 */
10498 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010499msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 msg_col = 0;
10502 msg_row = Rows - 1;
10503}
10504
10505/*
10506 * Delete mode message. Used when ESC is typed which is expected to end
10507 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010508 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 */
10510 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010511unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512{
10513 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010514 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 */
10516 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10517 redraw_cmdline = TRUE; /* delete mode later */
10518 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010519 clearmode();
10520}
10521
10522/*
10523 * Clear the mode message.
10524 */
10525 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010526clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010527{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010528 int save_msg_row = msg_row;
10529 int save_msg_col = msg_col;
10530
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010531 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010532 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010533 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010534 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010535
10536 msg_col = save_msg_col;
10537 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538}
10539
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010540 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010541recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010542{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010543 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010544 if (!shortmess(SHM_RECORDING))
10545 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010546 char s[4];
10547
10548 sprintf(s, " @%c", reg_recording);
10549 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010550 }
10551}
10552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010553/*
10554 * Draw the tab pages line at the top of the Vim window.
10555 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010556 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010557draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010558{
10559 int tabcount = 0;
10560 tabpage_T *tp;
10561 int tabwidth;
10562 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010563 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010564 int attr;
10565 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010566 win_T *cwp;
10567 int wincount;
10568 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010569 int c;
10570 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010571 int attr_sel = HL_ATTR(HLF_TPS);
10572 int attr_nosel = HL_ATTR(HLF_TP);
10573 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010574 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010575 int room;
10576 int use_sep_chars = (t_colors < 8
10577#ifdef FEAT_GUI
10578 && !gui.in_use
10579#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010580#ifdef FEAT_TERMGUICOLORS
10581 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010582#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010583 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010584
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010585 if (ScreenLines == NULL)
10586 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010587 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010588
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010589#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010590 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010591 if (gui_use_tabline())
10592 {
10593 gui_update_tabline();
10594 return;
10595 }
10596#endif
10597
10598 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010599 return;
10600
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010601#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010602 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010603
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010604 /* Use the 'tabline' option if it's set. */
10605 if (*p_tal != NUL)
10606 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010607 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010608
10609 /* Check for an error. If there is one we would loop in redrawing the
10610 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010611 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010612 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010613 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010614 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010615 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010616 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010617 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010619#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010620 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010621 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010622 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010623
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10625 if (tabwidth < 6)
10626 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010627
Bram Moolenaar238a5642006-02-21 22:12:05 +000010628 attr = attr_nosel;
10629 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010630 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10631 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010632 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010634
Bram Moolenaar238a5642006-02-21 22:12:05 +000010635 if (tp->tp_topframe == topframe)
10636 attr = attr_sel;
10637 if (use_sep_chars && col > 0)
10638 screen_putchar('|', 0, col++, attr);
10639
10640 if (tp->tp_topframe != topframe)
10641 attr = attr_nosel;
10642
10643 screen_putchar(' ', 0, col++, attr);
10644
10645 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010646 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 cwp = curwin;
10648 wp = firstwin;
10649 }
10650 else
10651 {
10652 cwp = tp->tp_curwin;
10653 wp = tp->tp_firstwin;
10654 }
10655
10656 modified = FALSE;
10657 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10658 if (bufIsChanged(wp->w_buffer))
10659 modified = TRUE;
10660 if (modified || wincount > 1)
10661 {
10662 if (wincount > 1)
10663 {
10664 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010665 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010666 if (col + len >= Columns - 3)
10667 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010669#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010670 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010671#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010672 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010673#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 );
10675 col += len;
10676 }
10677 if (modified)
10678 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10679 screen_putchar(' ', 0, col++, attr);
10680 }
10681
10682 room = scol - col + tabwidth - 1;
10683 if (room > 0)
10684 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010685 /* Get buffer name in NameBuff[] */
10686 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010687 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 len = vim_strsize(NameBuff);
10689 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010690 if (has_mbyte)
10691 while (len > room)
10692 {
10693 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010694 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010695 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010696 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010697 {
10698 p += len - room;
10699 len = room;
10700 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010701 if (len > Columns - col - 1)
10702 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010703
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010704 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010705 col += len;
10706 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010707 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010708
10709 /* Store the tab page number in TabPageIdxs[], so that
10710 * jump_to_mouse() knows where each one is. */
10711 ++tabcount;
10712 while (scol < col)
10713 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010714 }
10715
Bram Moolenaar238a5642006-02-21 22:12:05 +000010716 if (use_sep_chars)
10717 c = '_';
10718 else
10719 c = ' ';
10720 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010721
10722 /* Put an "X" for closing the current tab if there are several. */
10723 if (first_tabpage->tp_next != NULL)
10724 {
10725 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10726 TabPageIdxs[Columns - 1] = -999;
10727 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010728 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010729
10730 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10731 * set. */
10732 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010733}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010734
10735/*
10736 * Get buffer name for "buf" into NameBuff[].
10737 * Takes care of special buffer names and translates special characters.
10738 */
10739 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010740get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010741{
10742 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010743 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010744 else
10745 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10746 trans_characters(NameBuff, MAXPATHL);
10747}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010748
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749/*
10750 * Get the character to use in a status line. Get its attributes in "*attr".
10751 */
10752 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010753fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754{
10755 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756
10757#ifdef FEAT_TERMINAL
10758 if (bt_terminal(wp->w_buffer))
10759 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010760 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010761 {
10762 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010763 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010764 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010765 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010766 {
10767 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010768 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010769 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010770 }
10771 else
10772#endif
10773 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010775 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 fill = fill_stl;
10777 }
10778 else
10779 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010780 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 fill = fill_stlnc;
10782 }
10783 /* Use fill when there is highlighting, and highlighting of current
10784 * window differs, or the fillchars differ, or this is not the
10785 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010786 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010787 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 || (fill_stl != fill_stlnc)))
10789 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010790 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 return '^';
10792 return '=';
10793}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795/*
10796 * Get the character to use in a separator between vertically split windows.
10797 * Get its attributes in "*attr".
10798 */
10799 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010800fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010802 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 if (*attr == 0 && fill_vert == ' ')
10804 return '|';
10805 else
10806 return fill_vert;
10807}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808
10809/*
10810 * Return TRUE if redrawing should currently be done.
10811 */
10812 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010813redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010815#ifdef FEAT_EVAL
10816 if (disable_redraw_for_testing)
10817 return 0;
10818 else
10819#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010820 return ((!RedrawingDisabled
10821#ifdef FEAT_EVAL
10822 || ignore_redraw_flag_for_testing
10823#endif
10824 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825}
10826
10827/*
10828 * Return TRUE if printing messages should currently be done.
10829 */
10830 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010831messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
10833 return (!(p_lz && char_avail() && !KeyTyped));
10834}
10835
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010836#ifdef FEAT_MENU
10837/*
10838 * Draw the window toolbar.
10839 */
10840 static void
10841redraw_win_toolbar(win_T *wp)
10842{
10843 vimmenu_T *menu;
10844 int item_idx = 0;
10845 int item_count = 0;
10846 int col = 0;
10847 int next_col;
10848 int off = (int)(current_ScreenLine - ScreenLines);
10849 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10850 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10851
10852 vim_free(wp->w_winbar_items);
10853 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10854 ++item_count;
10855 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
Bram Moolenaar18a4ba22019-05-24 19:39:03 +020010856 sizeof(winbar_item_T) * (item_count + 1));
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010857
10858 /* TODO: use fewer spaces if there is not enough room */
10859 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010860 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010861 {
10862 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010863 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010864 break;
10865 if (col > 1)
10866 {
10867 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010868 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010869 break;
10870 }
10871
10872 wp->w_winbar_items[item_idx].wb_startcol = col;
10873 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010874 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010875 break;
10876
10877 next_col = text_to_screenline(wp, menu->name, col);
10878 while (col < next_col)
10879 {
10880 ScreenAttrs[off + col] = button_attr;
10881 ++col;
10882 }
10883 wp->w_winbar_items[item_idx].wb_endcol = col;
10884 wp->w_winbar_items[item_idx].wb_menu = menu;
10885 ++item_idx;
10886
Bram Moolenaar02631462017-09-22 15:20:32 +020010887 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010888 break;
10889 space_to_screenline(off + col, button_attr);
10890 ++col;
10891 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010892 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010893 {
10894 space_to_screenline(off + col, fill_attr);
10895 ++col;
10896 }
10897 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10898
Bram Moolenaar02631462017-09-22 15:20:32 +020010899 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010900 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010901}
10902#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010903
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904/*
10905 * Show current status info in ruler and various other places
10906 * If always is FALSE, only show ruler if position has changed.
10907 */
10908 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010909showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910{
10911 if (!always && !redrawing())
10912 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010913#ifdef FEAT_INS_EXPAND
10914 if (pum_visible())
10915 {
10916 /* Don't redraw right now, do it later. */
10917 curwin->w_redr_status = TRUE;
10918 return;
10919 }
10920#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010921#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010922 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010923 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 else
10925#endif
10926#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010927 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928#endif
10929
10930#ifdef FEAT_TITLE
10931 if (need_maketitle
10932# ifdef FEAT_STL_OPT
10933 || (p_icon && (stl_syntax & STL_IN_ICON))
10934 || (p_title && (stl_syntax & STL_IN_TITLE))
10935# endif
10936 )
10937 maketitle();
10938#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010939 /* Redraw the tab pages line if needed. */
10940 if (redraw_tabline)
10941 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942}
10943
10944#ifdef FEAT_CMDL_INFO
10945 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010946win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010948#define RULER_BUF_LEN 70
10949 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 int row;
10951 int fillchar;
10952 int attr;
10953 int empty_line = FALSE;
10954 colnr_T virtcol;
10955 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010956 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 int this_ru_col;
10959 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010960 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961
10962 /* If 'ruler' off or redrawing disabled, don't do anything */
10963 if (!p_ru)
10964 return;
10965
10966 /*
10967 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10968 * after deleting lines, before cursor.lnum is corrected.
10969 */
10970 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10971 return;
10972
10973#ifdef FEAT_INS_EXPAND
10974 /* Don't draw the ruler while doing insert-completion, it might overwrite
10975 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 if (edit_submode != NULL)
10978 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010979 // Don't draw the ruler when the popup menu is visible, it may overlap.
10980 // Except when the popup menu will be redrawn anyway.
10981 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010982 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983#endif
10984
10985#ifdef FEAT_STL_OPT
10986 if (*p_ruf)
10987 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010988 int save_called_emsg = called_emsg;
10989
10990 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010992 if (called_emsg)
10993 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010994 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010995 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 return;
10997 }
10998#endif
10999
11000 /*
11001 * Check if not in Insert mode and the line is empty (will show "0-1").
11002 */
11003 if (!(State & INSERT)
11004 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11005 empty_line = TRUE;
11006
11007 /*
11008 * Only draw the ruler when something changed.
11009 */
11010 validate_virtcol_win(wp);
11011 if ( redraw_cmdline
11012 || always
11013 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11014 || wp->w_cursor.col != wp->w_ru_cursor.col
11015 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 || wp->w_topline != wp->w_ru_topline
11018 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11019#ifdef FEAT_DIFF
11020 || wp->w_topfill != wp->w_ru_topfill
11021#endif
11022 || empty_line != wp->w_ru_empty)
11023 {
11024 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 if (wp->w_status_height)
11026 {
11027 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011028 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011029 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011030 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 }
11032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 {
11034 row = Rows - 1;
11035 fillchar = ' ';
11036 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 width = Columns;
11038 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 }
11040
11041 /* In list mode virtcol needs to be recomputed */
11042 virtcol = wp->w_virtcol;
11043 if (wp->w_p_list && lcs_tab1 == NUL)
11044 {
11045 wp->w_p_list = FALSE;
11046 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11047 wp->w_p_list = TRUE;
11048 }
11049
11050 /*
11051 * Some sprintfs return the length, some return a pointer.
11052 * To avoid portability problems we use strlen() here.
11053 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011054 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11056 ? 0L
11057 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011058 len = STRLEN(buffer);
11059 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11061 (int)virtcol + 1);
11062
11063 /*
11064 * Add a "50%" if there is room for it.
11065 * On the last line, don't print in the last column (scrolls the
11066 * screen up on some terminals).
11067 */
11068 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011069 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 this_ru_col = ru_col - (Columns - width);
11074 if (this_ru_col < 0)
11075 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 /* Never use more than half the window/screen width, leave the other
11077 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011078 if (this_ru_col < (width + 1) / 2)
11079 this_ru_col = (width + 1) / 2;
11080 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011082 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011083 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 if (has_mbyte)
11086 i += (*mb_char2bytes)(fillchar, buffer + i);
11087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 buffer[i++] = fillchar;
11089 ++o;
11090 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011091 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 }
11093 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 if (has_mbyte)
11095 {
11096 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011097 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 {
11099 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011100 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 {
11102 buffer[i] = NUL;
11103 break;
11104 }
11105 }
11106 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011107 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011108 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
Bram Moolenaar4033c552017-09-16 20:54:51 +020011110 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 i = redraw_cmdline;
11112 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011113 this_ru_col + off + (int)STRLEN(buffer),
11114 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 fillchar, fillchar, attr);
11116 /* don't redraw the cmdline because of showing the ruler */
11117 redraw_cmdline = i;
11118 wp->w_ru_cursor = wp->w_cursor;
11119 wp->w_ru_virtcol = wp->w_virtcol;
11120 wp->w_ru_empty = empty_line;
11121 wp->w_ru_topline = wp->w_topline;
11122 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11123#ifdef FEAT_DIFF
11124 wp->w_ru_topfill = wp->w_topfill;
11125#endif
11126 }
11127}
11128#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011129
11130#if defined(FEAT_LINEBREAK) || defined(PROTO)
11131/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011132 * Return the width of the 'number' and 'relativenumber' column.
11133 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011134 * Otherwise it depends on 'numberwidth' and the line count.
11135 */
11136 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011137number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011138{
11139 int n;
11140 linenr_T lnum;
11141
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011142 if (wp->w_p_rnu && !wp->w_p_nu)
11143 /* cursor line shows "0" */
11144 lnum = wp->w_height;
11145 else
11146 /* cursor line shows absolute line number */
11147 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011148
Bram Moolenaar6b314672015-03-20 15:42:10 +010011149 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011150 return wp->w_nrwidth_width;
11151 wp->w_nrwidth_line_count = lnum;
11152
11153 n = 0;
11154 do
11155 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011156 lnum /= 10;
11157 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011158 } while (lnum > 0);
11159
11160 /* 'numberwidth' gives the minimal width plus one */
11161 if (n < wp->w_p_nuw - 1)
11162 n = wp->w_p_nuw - 1;
11163
11164 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011165 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011166 return n;
11167}
11168#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011169
Bram Moolenaar113e1072019-01-20 15:30:40 +010011170#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011171/*
11172 * Return the current cursor column. This is the actual position on the
11173 * screen. First column is 0.
11174 */
11175 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011176screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011177{
11178 return screen_cur_col;
11179}
11180
11181/*
11182 * Return the current cursor row. This is the actual position on the screen.
11183 * First row is 0.
11184 */
11185 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011186screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011187{
11188 return screen_cur_row;
11189}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011190#endif