blob: 5f3947bf9eeb2784f8d51b32bfbdf560fba4a4c1 [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 Moolenaarc799fe22019-05-28 23:08:19 +0200331 screenline = LALLOC_MULT(schar_T, rows * cols);
332 screenattr = LALLOC_MULT(sattr_T, rows * cols);
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 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200337 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200338 if (screenlineUC == NULL)
339 ret = 2;
340 for (i = 0; i < p_mco; ++i)
341 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200342 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200349 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200411 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200509/*
510 * To be called when "updating_screen" was set before and now the postponed
511 * side effects may take place.
512 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200513 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200514after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515{
516 updating_screen = FALSE;
517#ifdef FEAT_GUI
518 if (may_resize_shell)
519 gui_may_resize_shell();
520#endif
521#ifdef FEAT_TERMINAL
522 term_check_channel_closed_recently();
523#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200524
525#ifdef HAVE_DROP_FILE
526 // If handle_drop() was called while updating_screen was TRUE need to
527 // handle the drop now.
528 handle_any_postponed_drop();
529#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
535 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100536update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 redraw_curbuf_later(type);
539 update_screen(type);
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Based on the current value of curwin->w_topline, transfer a screenfull
544 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 win_T *wp;
552 static int did_intro = FALSE;
553#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
554 int did_one;
555#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200557 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200558 int gui_cursor_col = 0;
559 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000563 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200567 if (type == VALID_NO_UPDATE)
568 {
569 no_update = TRUE;
570 type = 0;
571 }
572
Bram Moolenaara3347722019-05-11 21:14:24 +0200573#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200574 {
575 buf_T *buf;
576
577 // Before updating the screen, notify any listeners of changed text.
578 FOR_ALL_BUFFERS(buf)
579 invoke_listeners(buf);
580 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (must_redraw)
584 {
585 if (type < must_redraw) /* use maximal type */
586 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000587
588 /* must_redraw is reset here, so that when we run into some weird
589 * reason to redraw while busy redrawing (e.g., asynchronous
590 * scrolling), or update_topline() in win_update() will cause a
591 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 must_redraw = 0;
593 }
594
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200595 /* May need to update w_lines[]. */
596 if (curwin->w_lines_valid == 0 && type < NOT_VALID
597#ifdef FEAT_TERMINAL
598 && !term_do_update_window(curwin)
599#endif
600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 type = NOT_VALID;
602
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000603 /* Postpone the redrawing when it's not needed and when being called
604 * recursively. */
605 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 redraw_later(type); /* remember type for next time */
608 must_redraw = type;
609 if (type > INVERTED_ALL)
610 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200613#ifdef FEAT_TEXT_PROP
614 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200615 if (popup_any_visible())
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200616 type = NOT_VALID;
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 updating_screen = TRUE;
620#ifdef FEAT_SYN_HL
621 ++display_tick; /* let syntax code know we're in a next round of
622 * display updating */
623#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200624 if (no_update)
625 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * if the screen was scrolled up when displaying a message, scroll it down
629 */
630 if (msg_scrolled)
631 {
632 clear_cmdline = TRUE;
633 if (msg_scrolled > Rows - 5) /* clearing is faster */
634 type = CLEAR;
635 else if (type != CLEAR)
636 {
637 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200638 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
639 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 type = CLEAR;
641 FOR_ALL_WINDOWS(wp)
642 {
643 if (W_WINROW(wp) < msg_scrolled)
644 {
645 if (W_WINROW(wp) + wp->w_height > msg_scrolled
646 && wp->w_redr_type < REDRAW_TOP
647 && wp->w_lines_valid > 0
648 && wp->w_topline == wp->w_lines[0].wl_lnum)
649 {
650 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
651 wp->w_redr_type = REDRAW_TOP;
652 }
653 else
654 {
655 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200656 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
657 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660 }
661 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200662 if (!no_update)
663 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000664 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 msg_scrolled = 0;
667 need_wait_return = FALSE;
668 }
669
670 /* reset cmdline_row now (may have been changed temporarily) */
671 compute_cmdrow();
672
673 /* Check for changed highlighting */
674 if (need_highlight_changed)
675 highlight_changed();
676
677 if (type == CLEAR) /* first clear screen */
678 {
679 screenclear(); /* will reset clear_cmdline */
680 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200681 /* must_redraw may be set indirectly, avoid another redraw later */
682 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684
685 if (clear_cmdline) /* going to clear cmdline (done below) */
686 check_for_delay(FALSE);
687
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200689 /* Force redraw when width of 'number' or 'relativenumber' column
690 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000691 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200692 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
693 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000694 curwin->w_redr_type = NOT_VALID;
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 /*
698 * Only start redrawing if there is really something to do.
699 */
700 if (type == INVERTED)
701 update_curswant();
702 if (curwin->w_redr_type < type
703 && !((type == VALID
704 && curwin->w_lines[0].wl_valid
705#ifdef FEAT_DIFF
706 && curwin->w_topfill == curwin->w_old_topfill
707 && curwin->w_botfill == curwin->w_old_botfill
708#endif
709 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000711 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
713 && curwin->w_old_visual_mode == VIsual_mode
714 && (curwin->w_valid & VALID_VIRTCOL)
715 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ))
717 curwin->w_redr_type = type;
718
Bram Moolenaar5a305422006-04-28 22:38:25 +0000719 /* Redraw the tab pages line if needed. */
720 if (redraw_tabline || type >= NOT_VALID)
721 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_SYN_HL
724 /*
725 * Correct stored syntax highlighting info for changes in each displayed
726 * buffer. Each buffer must only be done once.
727 */
728 FOR_ALL_WINDOWS(wp)
729 {
730 if (wp->w_buffer->b_mod_set)
731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 win_T *wwp;
733
734 /* Check if we already did this buffer. */
735 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
736 if (wwp->w_buffer == wp->w_buffer)
737 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200738 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 syn_stack_apply_changes(wp->w_buffer);
740 }
741 }
742#endif
743
744 /*
745 * Go from top to bottom through the windows, redrawing the ones that need
746 * it.
747 */
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 did_one = FALSE;
750#endif
751#ifdef FEAT_SEARCH_EXTRA
752 search_hl.rm.regprog = NULL;
753#endif
754 FOR_ALL_WINDOWS(wp)
755 {
756 if (wp->w_redr_type != 0)
757 {
758 cursor_off();
759#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
760 if (!did_one)
761 {
762 did_one = TRUE;
763# ifdef FEAT_SEARCH_EXTRA
764 start_search_hl();
765# endif
766# ifdef FEAT_CLIPBOARD
767 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200768 if (clip_star.available && clip_isautosel_star())
769 clip_update_selection(&clip_star);
770 if (clip_plus.available && clip_isautosel_plus())
771 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772# endif
773#ifdef FEAT_GUI
774 /* Remove the cursor before starting to do anything, because
775 * scrolling may make it difficult to redraw the text under
776 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200777 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200778 {
779 gui_cursor_col = gui.cursor_col;
780 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200782 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#endif
785 }
786#endif
787 win_update(wp);
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* redraw status line after the window to minimize cursor movement */
791 if (wp->w_redr_status)
792 {
793 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200794 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797#if defined(FEAT_SEARCH_EXTRA)
798 end_search_hl();
799#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100800#ifdef FEAT_INS_EXPAND
801 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200802 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /* Reset b_mod_set flags. Going through all windows is probably faster
806 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200807 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200810 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 /* Clear or redraw the command line. Done last, because scrolling may
813 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200814 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 showmode();
816
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200817 if (no_update)
818 --no_win_do_lines_ins;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200821 if (!did_intro)
822 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 did_intro = TRUE;
824
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200825#ifdef FEAT_TEXT_PROP
826 // Display popup windows on top of the others.
827 update_popups();
828#endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_GUI
831 /* Redraw the cursor and update the scrollbars when all screen updating is
832 * done. */
833 if (gui.in_use)
834 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200835 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 mch_disable_flush();
838 out_flush(); /* required before updating the cursor */
839 mch_enable_flush();
840
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 /* Put the GUI position where the cursor was, gui_update_cursor()
842 * uses that. */
843 gui.col = gui_cursor_col;
844 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200845 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100847 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200848 screen_cur_col = gui.col;
849 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200850 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100851 else
852 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 gui_update_scrollbars(FALSE);
854 }
855#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200856 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100859#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100860/*
861 * Prepare for updating one or more windows.
862 * Caller must check for "updating_screen" already set to avoid recursiveness.
863 */
864 static void
865update_prepare(void)
866{
867 cursor_off();
868 updating_screen = TRUE;
869#ifdef FEAT_GUI
870 /* Remove the cursor before starting to do anything, because scrolling may
871 * make it difficult to redraw the text under it. */
872 if (gui.in_use)
873 gui_undraw_cursor();
874#endif
875#ifdef FEAT_SEARCH_EXTRA
876 start_search_hl();
877#endif
878}
879
880/*
881 * Finish updating one or more windows.
882 */
883 static void
884update_finish(void)
885{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200886 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 showmode();
888
889# ifdef FEAT_SEARCH_EXTRA
890 end_search_hl();
891# endif
892
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200893 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894
895# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100896 /* Redraw the cursor and update the scrollbars when all screen updating is
897 * done. */
898 if (gui.in_use)
899 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100900 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100901 gui_update_scrollbars(FALSE);
902 }
903# endif
904}
905#endif
906
Bram Moolenaar860cae12010-06-05 23:22:07 +0200907#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908/*
909 * Return TRUE if the cursor line in window "wp" may be concealed, according
910 * to the 'concealcursor' option.
911 */
912 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100913conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200914{
915 int c;
916
917 if (*wp->w_p_cocu == NUL)
918 return FALSE;
919 if (get_real_state() & VISUAL)
920 c = 'v';
921 else if (State & INSERT)
922 c = 'i';
923 else if (State & NORMAL)
924 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200925 else if (State & CMDLINE)
926 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200927 else
928 return FALSE;
929 return vim_strchr(wp->w_p_cocu, c) != NULL;
930}
931
932/*
933 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
934 */
935 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200936conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200937{
938 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
939 {
940 need_cursor_line_redraw = TRUE;
941 /* Need to recompute cursor column, e.g., when starting Visual mode
942 * without concealing. */
943 curs_columns(TRUE);
944 }
945}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#endif
947
Bram Moolenaar113e1072019-01-20 15:30:40 +0100948#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100950update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 win_T *wp;
953 int doit = FALSE;
954
955# ifdef FEAT_FOLDING
956 win_foldinfo.fi_level = 0;
957# endif
958
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100959 // update/delete a specific sign
960 redraw_buf_line_later(buf, lnum);
961
962 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (wp->w_redr_type != 0)
965 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100967 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200968 * happening (recursive call), messages on the screen or still starting up.
969 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100970 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200971 || State == ASKMORE || State == HITRETURN
972 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100973#ifdef FEAT_GUI
974 || gui.starting
975#endif
976 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 return;
978
979 /* update all windows that need updating */
980 update_prepare();
981
Bram Moolenaar29323592016-07-24 22:04:11 +0200982 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
984 if (wp->w_redr_type != 0)
985 win_update(wp);
986 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200987 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 update_finish();
991}
992#endif
993
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200994#ifdef FEAT_TEXT_PROP
995 static void
996update_popups(void)
997{
998 win_T *wp;
999 win_T *lowest_wp;
1000 int lowest_zindex;
1001
1002 // Reset all the VALID_POPUP flags.
1003 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001004 wp->w_popup_flags &= ~POPF_REDRAWN;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001005 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001006 wp->w_popup_flags &= ~POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001007
1008 // TODO: don't redraw every popup every time.
1009 for (;;)
1010 {
1011 // Find the window with the lowest zindex that hasn't been updated yet,
1012 // so that the window with a higher zindex is drawn later, thus goes on
1013 // top.
1014 lowest_zindex = INT_MAX;
1015 lowest_wp = NULL;
1016 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001017 if ((wp->w_popup_flags & (POPF_REDRAWN|POPF_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001018 && wp->w_zindex < lowest_zindex)
1019 {
1020 lowest_zindex = wp->w_zindex;
1021 lowest_wp = wp;
1022 }
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001023 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001024 if ((wp->w_popup_flags & (POPF_REDRAWN|POPF_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001025 && wp->w_zindex < lowest_zindex)
1026 {
1027 lowest_zindex = wp->w_zindex;
1028 lowest_wp = wp;
1029 }
1030
1031 if (lowest_wp == NULL)
1032 break;
Bram Moolenaar17146962019-05-30 00:12:11 +02001033
1034 // Recompute the position if the text changed.
1035 if (lowest_wp->w_popup_last_changedtick
1036 != CHANGEDTICK(lowest_wp->w_buffer))
1037 popup_adjust_position(lowest_wp);
1038
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001039 win_update(lowest_wp);
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001040 lowest_wp->w_popup_flags |= POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001041 }
1042}
1043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001045/*
1046 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1047 * window then get the "Pmenu" highlight attribute.
1048 */
1049 static int
1050get_wcr_attr(win_T *wp)
1051{
1052 int wcr_attr = 0;
1053
1054 if (*wp->w_p_wcr != NUL)
1055 wcr_attr = syn_name2attr(wp->w_p_wcr);
1056#ifdef FEAT_TEXT_PROP
1057 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1058 wcr_attr = HL_ATTR(HLF_PNI);
1059#endif
1060 return wcr_attr;
1061}
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#if defined(FEAT_GUI) || defined(PROTO)
1064/*
1065 * Update a single window, its status line and maybe the command line msg.
1066 * Used for the GUI scrollbar.
1067 */
1068 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001069updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001071 /* return if already busy updating */
1072 if (updating_screen)
1073 return;
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 update_prepare();
1076
1077#ifdef FEAT_CLIPBOARD
1078 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001079 if (clip_star.available && clip_isautosel_star())
1080 clip_update_selection(&clip_star);
1081 if (clip_plus.available && clip_isautosel_plus())
1082 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001086
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001087 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001088 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001089 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (wp->w_redr_status
1092# ifdef FEAT_CMDL_INFO
1093 || p_ru
1094# endif
1095# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001096 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097# endif
1098 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001099 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100
1101 update_finish();
1102}
1103#endif
1104
1105/*
1106 * Update a single window.
1107 *
1108 * This may cause the windows below it also to be redrawn (when clearing the
1109 * screen or scrolling lines).
1110 *
1111 * How the window is redrawn depends on wp->w_redr_type. Each type also
1112 * implies the one below it.
1113 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001114 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1116 * INVERTED redraw the changed part of the Visual area
1117 * INVERTED_ALL redraw the whole Visual area
1118 * VALID 1. scroll up/down to adjust for a changed w_topline
1119 * 2. update lines at the top when scrolled down
1120 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001121 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 * b_mod_top and b_mod_bot.
1123 * - if wp->w_redraw_top non-zero, redraw lines between
1124 * wp->w_redraw_top and wp->w_redr_bot.
1125 * - continue redrawing when syntax status is invalid.
1126 * 4. if scrolled up, update lines at the bottom.
1127 * This results in three areas that may need updating:
1128 * top: from first row to top_end (when scrolled down)
1129 * mid: from mid_start to mid_end (update inversion or changed text)
1130 * bot: from bot_start to last row (when scrolled up)
1131 */
1132 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001133win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134{
1135 buf_T *buf = wp->w_buffer;
1136 int type;
1137 int top_end = 0; /* Below last row of the top area that needs
1138 updating. 0 when no top area updating. */
1139 int mid_start = 999;/* first row of the mid area that needs
1140 updating. 999 when no mid area updating. */
1141 int mid_end = 0; /* Below last row of the mid area that needs
1142 updating. 0 when no mid area updating. */
1143 int bot_start = 999;/* first row of the bot area that needs
1144 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 int scrolled_down = FALSE; /* TRUE when scrolled down when
1146 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001148 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 int top_to_mod = FALSE; /* redraw above mod_top */
1150#endif
1151
1152 int row; /* current window row to display */
1153 linenr_T lnum; /* current buffer lnum to display */
1154 int idx; /* current index in w_lines[] */
1155 int srow; /* starting row of the current line */
1156
1157 int eof = FALSE; /* if TRUE, we hit the end of the file */
1158 int didline = FALSE; /* if TRUE, we finished the last line */
1159 int i;
1160 long j;
1161 static int recursive = FALSE; /* being called recursively */
1162 int old_botline = wp->w_botline;
1163#ifdef FEAT_FOLDING
1164 long fold_count;
1165#endif
1166#ifdef FEAT_SYN_HL
1167 /* remember what happened to the previous line, to know if
1168 * check_visual_highlight() can be used */
1169#define DID_NONE 1 /* didn't update a line */
1170#define DID_LINE 2 /* updated a normal line */
1171#define DID_FOLD 3 /* updated a folded line */
1172 int did_update = DID_NONE;
1173 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1174#endif
1175 linenr_T mod_top = 0;
1176 linenr_T mod_bot = 0;
1177#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1178 int save_got_int;
1179#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001180#ifdef SYN_TIME_LIMIT
1181 proftime_T syntax_tm;
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
1184 type = wp->w_redr_type;
1185
1186 if (type == NOT_VALID)
1187 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 wp->w_lines_valid = 0;
1190 }
1191
1192 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001193 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 wp->w_redr_type = 0;
1196 return;
1197 }
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 /* Window is zero-width: Only need to draw the separator. */
1200 if (wp->w_width == 0)
1201 {
1202 /* draw the vertical separator right of this window */
1203 draw_vsep_win(wp, 0);
1204 wp->w_redr_type = 0;
1205 return;
1206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001208#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001209 // If this window contains a terminal, redraw works completely differently.
1210 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001211 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001212 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001213# ifdef FEAT_MENU
1214 /* Draw the window toolbar, if there is one. */
1215 if (winbar_height(wp) > 0)
1216 redraw_win_toolbar(wp);
1217# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001218 wp->w_redr_type = 0;
1219 return;
1220 }
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001224 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225#endif
1226
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001227#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001228 /* Force redraw when width of 'number' or 'relativenumber' column
1229 * changes. */
1230 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001231 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001232 {
1233 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001234 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001235 }
1236 else
1237#endif
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1240 {
1241 /*
1242 * When there are both inserted/deleted lines and specific lines to be
1243 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1244 * everything (only happens when redrawing is off for while).
1245 */
1246 type = NOT_VALID;
1247 }
1248 else
1249 {
1250 /*
1251 * Set mod_top to the first line that needs displaying because of
1252 * changes. Set mod_bot to the first line after the changes.
1253 */
1254 mod_top = wp->w_redraw_top;
1255 if (wp->w_redraw_bot != 0)
1256 mod_bot = wp->w_redraw_bot + 1;
1257 else
1258 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (buf->b_mod_set)
1260 {
1261 if (mod_top == 0 || mod_top > buf->b_mod_top)
1262 {
1263 mod_top = buf->b_mod_top;
1264#ifdef FEAT_SYN_HL
1265 /* Need to redraw lines above the change that may be included
1266 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001267 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001269 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (mod_top < 1)
1271 mod_top = 1;
1272 }
1273#endif
1274 }
1275 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1276 mod_bot = buf->b_mod_bot;
1277
1278#ifdef FEAT_SEARCH_EXTRA
1279 /* When 'hlsearch' is on and using a multi-line search pattern, a
1280 * change in one line may make the Search highlighting in a
1281 * previous line invalid. Simple solution: redraw all visible
1282 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001283 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001285 if (search_hl.rm.regprog != NULL
1286 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001288 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001289 {
1290 cur = wp->w_match_head;
1291 while (cur != NULL)
1292 {
1293 if (cur->match.regprog != NULL
1294 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001295 {
1296 top_to_mod = TRUE;
1297 break;
1298 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001299 cur = cur->next;
1300 }
1301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#endif
1303 }
1304#ifdef FEAT_FOLDING
1305 if (mod_top != 0 && hasAnyFolding(wp))
1306 {
1307 linenr_T lnumt, lnumb;
1308
1309 /*
1310 * A change in a line can cause lines above it to become folded or
1311 * unfolded. Find the top most buffer line that may be affected.
1312 * If the line was previously folded and displayed, get the first
1313 * line of that fold. If the line is folded now, get the first
1314 * folded line. Use the minimum of these two.
1315 */
1316
1317 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1318 * the line below it. If there is no valid entry, use w_topline.
1319 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1320 * to this line. If there is no valid entry, use MAXLNUM. */
1321 lnumt = wp->w_topline;
1322 lnumb = MAXLNUM;
1323 for (i = 0; i < wp->w_lines_valid; ++i)
1324 if (wp->w_lines[i].wl_valid)
1325 {
1326 if (wp->w_lines[i].wl_lastlnum < mod_top)
1327 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1328 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1329 {
1330 lnumb = wp->w_lines[i].wl_lnum;
1331 /* When there is a fold column it might need updating
1332 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001333 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 ++lnumb;
1335 }
1336 }
1337
1338 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1339 if (mod_top > lnumt)
1340 mod_top = lnumt;
1341
1342 /* Now do the same for the bottom line (one above mod_bot). */
1343 --mod_bot;
1344 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1345 ++mod_bot;
1346 if (mod_bot < lnumb)
1347 mod_bot = lnumb;
1348 }
1349#endif
1350
1351 /* When a change starts above w_topline and the end is below
1352 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001353 * If the end of the change is above w_topline: do like no change was
1354 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (mod_top != 0 && mod_top < wp->w_topline)
1356 {
1357 if (mod_bot > wp->w_topline)
1358 mod_top = wp->w_topline;
1359#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001360 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 top_end = 1;
1362#endif
1363 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001364
1365 /* When line numbers are displayed need to redraw all lines below
1366 * inserted/deleted lines. */
1367 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1368 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001370 wp->w_redraw_top = 0; // reset for next time
1371 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 /*
1374 * When only displaying the lines at the top, set top_end. Used when
1375 * window has scrolled down for msg_scrolled.
1376 */
1377 if (type == REDRAW_TOP)
1378 {
1379 j = 0;
1380 for (i = 0; i < wp->w_lines_valid; ++i)
1381 {
1382 j += wp->w_lines[i].wl_size;
1383 if (j >= wp->w_upd_rows)
1384 {
1385 top_end = j;
1386 break;
1387 }
1388 }
1389 if (top_end == 0)
1390 /* not found (cannot happen?): redraw everything */
1391 type = NOT_VALID;
1392 else
1393 /* top area defined, the rest is VALID */
1394 type = VALID;
1395 }
1396
Bram Moolenaar367329b2007-08-30 11:53:22 +00001397 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001398 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1399 * non-zero and thus not FALSE) will indicate that screenclear() was not
1400 * called. */
1401 if (screen_cleared)
1402 screen_cleared = MAYBE;
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /*
1405 * If there are no changes on the screen that require a complete redraw,
1406 * handle three cases:
1407 * 1: we are off the top of the screen by a few lines: scroll down
1408 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1409 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1410 * w_lines[] that needs updating.
1411 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001412 if ((type == VALID || type == SOME_VALID
1413 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414#ifdef FEAT_DIFF
1415 && !wp->w_botfill && !wp->w_old_botfill
1416#endif
1417 )
1418 {
1419 if (mod_top != 0 && wp->w_topline == mod_top)
1420 {
1421 /*
1422 * w_topline is the first changed line, the scrolling will be done
1423 * further down.
1424 */
1425 }
1426 else if (wp->w_lines[0].wl_valid
1427 && (wp->w_topline < wp->w_lines[0].wl_lnum
1428#ifdef FEAT_DIFF
1429 || (wp->w_topline == wp->w_lines[0].wl_lnum
1430 && wp->w_topfill > wp->w_old_topfill)
1431#endif
1432 ))
1433 {
1434 /*
1435 * New topline is above old topline: May scroll down.
1436 */
1437#ifdef FEAT_FOLDING
1438 if (hasAnyFolding(wp))
1439 {
1440 linenr_T ln;
1441
1442 /* count the number of lines we are off, counting a sequence
1443 * of folded lines as one */
1444 j = 0;
1445 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1446 {
1447 ++j;
1448 if (j >= wp->w_height - 2)
1449 break;
1450 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1451 }
1452 }
1453 else
1454#endif
1455 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1456 if (j < wp->w_height - 2) /* not too far off */
1457 {
1458 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1459#ifdef FEAT_DIFF
1460 /* insert extra lines for previously invisible filler lines */
1461 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1462 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1463 - wp->w_old_topfill;
1464#endif
1465 if (i < wp->w_height - 2) /* less than a screen off */
1466 {
1467 /*
1468 * Try to insert the correct number of lines.
1469 * If not the last window, delete the lines at the bottom.
1470 * win_ins_lines may fail when the terminal can't do it.
1471 */
1472 if (i > 0)
1473 check_for_delay(FALSE);
1474 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1475 {
1476 if (wp->w_lines_valid != 0)
1477 {
1478 /* Need to update rows that are new, stop at the
1479 * first one that scrolled down. */
1480 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 /* Move the entries that were scrolled, disable
1484 * the entries for the lines to be redrawn. */
1485 if ((wp->w_lines_valid += j) > wp->w_height)
1486 wp->w_lines_valid = wp->w_height;
1487 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1488 wp->w_lines[idx] = wp->w_lines[idx - j];
1489 while (idx >= 0)
1490 wp->w_lines[idx--].wl_valid = FALSE;
1491 }
1492 }
1493 else
1494 mid_start = 0; /* redraw all lines */
1495 }
1496 else
1497 mid_start = 0; /* redraw all lines */
1498 }
1499 else
1500 mid_start = 0; /* redraw all lines */
1501 }
1502 else
1503 {
1504 /*
1505 * New topline is at or below old topline: May scroll up.
1506 * When topline didn't change, find first entry in w_lines[] that
1507 * needs updating.
1508 */
1509
1510 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1511 j = -1;
1512 row = 0;
1513 for (i = 0; i < wp->w_lines_valid; i++)
1514 {
1515 if (wp->w_lines[i].wl_valid
1516 && wp->w_lines[i].wl_lnum == wp->w_topline)
1517 {
1518 j = i;
1519 break;
1520 }
1521 row += wp->w_lines[i].wl_size;
1522 }
1523 if (j == -1)
1524 {
1525 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1526 * lines */
1527 mid_start = 0;
1528 }
1529 else
1530 {
1531 /*
1532 * Try to delete the correct number of lines.
1533 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1534 */
1535#ifdef FEAT_DIFF
1536 /* If the topline didn't change, delete old filler lines,
1537 * otherwise delete filler lines of the new topline... */
1538 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1539 row += wp->w_old_topfill;
1540 else
1541 row += diff_check_fill(wp, wp->w_topline);
1542 /* ... but don't delete new filler lines. */
1543 row -= wp->w_topfill;
1544#endif
1545 if (row > 0)
1546 {
1547 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001548 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1549 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 bot_start = wp->w_height - row;
1551 else
1552 mid_start = 0; /* redraw all lines */
1553 }
1554 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1555 {
1556 /*
1557 * Skip the lines (below the deleted lines) that are still
1558 * valid and don't need redrawing. Copy their info
1559 * upwards, to compensate for the deleted lines. Set
1560 * bot_start to the first row that needs redrawing.
1561 */
1562 bot_start = 0;
1563 idx = 0;
1564 for (;;)
1565 {
1566 wp->w_lines[idx] = wp->w_lines[j];
1567 /* stop at line that didn't fit, unless it is still
1568 * valid (no lines deleted) */
1569 if (row > 0 && bot_start + row
1570 + (int)wp->w_lines[j].wl_size > wp->w_height)
1571 {
1572 wp->w_lines_valid = idx + 1;
1573 break;
1574 }
1575 bot_start += wp->w_lines[idx++].wl_size;
1576
1577 /* stop at the last valid entry in w_lines[].wl_size */
1578 if (++j >= wp->w_lines_valid)
1579 {
1580 wp->w_lines_valid = idx;
1581 break;
1582 }
1583 }
1584#ifdef FEAT_DIFF
1585 /* Correct the first entry for filler lines at the top
1586 * when it won't get updated below. */
1587 if (wp->w_p_diff && bot_start > 0)
1588 wp->w_lines[0].wl_size =
1589 plines_win_nofill(wp, wp->w_topline, TRUE)
1590 + wp->w_topfill;
1591#endif
1592 }
1593 }
1594 }
1595
1596 /* When starting redraw in the first line, redraw all lines. When
1597 * there is only one window it's probably faster to clear the screen
1598 * first. */
1599 if (mid_start == 0)
1600 {
1601 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001602 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001603 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001604 /* Clear the screen when it was not done by win_del_lines() or
1605 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1606 * then. */
1607 if (screen_cleared != TRUE)
1608 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001609 /* The screen was cleared, redraw the tab pages line. */
1610 if (redraw_tabline)
1611 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001614
1615 /* When win_del_lines() or win_ins_lines() caused the screen to be
1616 * cleared (only happens for the first window) or when screenclear()
1617 * was called directly above, "must_redraw" will have been set to
1618 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1619 if (screen_cleared == TRUE)
1620 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 }
1622 else
1623 {
1624 /* Not VALID or INVERTED: redraw all lines. */
1625 mid_start = 0;
1626 mid_end = wp->w_height;
1627 }
1628
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001629 if (type == SOME_VALID)
1630 {
1631 /* SOME_VALID: redraw all lines. */
1632 mid_start = 0;
1633 mid_end = wp->w_height;
1634 type = NOT_VALID;
1635 }
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 /* check if we are updating or removing the inverted part */
1638 if ((VIsual_active && buf == curwin->w_buffer)
1639 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1640 {
1641 linenr_T from, to;
1642
1643 if (VIsual_active)
1644 {
1645 if (VIsual_active
1646 && (VIsual_mode != wp->w_old_visual_mode
1647 || type == INVERTED_ALL))
1648 {
1649 /*
1650 * If the type of Visual selection changed, redraw the whole
1651 * selection. Also when the ownership of the X selection is
1652 * gained or lost.
1653 */
1654 if (curwin->w_cursor.lnum < VIsual.lnum)
1655 {
1656 from = curwin->w_cursor.lnum;
1657 to = VIsual.lnum;
1658 }
1659 else
1660 {
1661 from = VIsual.lnum;
1662 to = curwin->w_cursor.lnum;
1663 }
1664 /* redraw more when the cursor moved as well */
1665 if (wp->w_old_cursor_lnum < from)
1666 from = wp->w_old_cursor_lnum;
1667 if (wp->w_old_cursor_lnum > to)
1668 to = wp->w_old_cursor_lnum;
1669 if (wp->w_old_visual_lnum < from)
1670 from = wp->w_old_visual_lnum;
1671 if (wp->w_old_visual_lnum > to)
1672 to = wp->w_old_visual_lnum;
1673 }
1674 else
1675 {
1676 /*
1677 * Find the line numbers that need to be updated: The lines
1678 * between the old cursor position and the current cursor
1679 * position. Also check if the Visual position changed.
1680 */
1681 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1682 {
1683 from = curwin->w_cursor.lnum;
1684 to = wp->w_old_cursor_lnum;
1685 }
1686 else
1687 {
1688 from = wp->w_old_cursor_lnum;
1689 to = curwin->w_cursor.lnum;
1690 if (from == 0) /* Visual mode just started */
1691 from = to;
1692 }
1693
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001694 if (VIsual.lnum != wp->w_old_visual_lnum
1695 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 if (wp->w_old_visual_lnum < from
1698 && wp->w_old_visual_lnum != 0)
1699 from = wp->w_old_visual_lnum;
1700 if (wp->w_old_visual_lnum > to)
1701 to = wp->w_old_visual_lnum;
1702 if (VIsual.lnum < from)
1703 from = VIsual.lnum;
1704 if (VIsual.lnum > to)
1705 to = VIsual.lnum;
1706 }
1707 }
1708
1709 /*
1710 * If in block mode and changed column or curwin->w_curswant:
1711 * update all lines.
1712 * First compute the actual start and end column.
1713 */
1714 if (VIsual_mode == Ctrl_V)
1715 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001716 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001717#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001718 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
Bram Moolenaar404406a2014-10-09 13:24:43 +02001720 if (curwin->w_p_lbr)
1721 ve_flags = VE_ALL;
1722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001724#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001725 ve_flags = save_ve_flags;
1726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 ++toc;
1728 if (curwin->w_curswant == MAXCOL)
1729 toc = MAXCOL;
1730
1731 if (fromc != wp->w_old_cursor_fcol
1732 || toc != wp->w_old_cursor_lcol)
1733 {
1734 if (from > VIsual.lnum)
1735 from = VIsual.lnum;
1736 if (to < VIsual.lnum)
1737 to = VIsual.lnum;
1738 }
1739 wp->w_old_cursor_fcol = fromc;
1740 wp->w_old_cursor_lcol = toc;
1741 }
1742 }
1743 else
1744 {
1745 /* Use the line numbers of the old Visual area. */
1746 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1747 {
1748 from = wp->w_old_cursor_lnum;
1749 to = wp->w_old_visual_lnum;
1750 }
1751 else
1752 {
1753 from = wp->w_old_visual_lnum;
1754 to = wp->w_old_cursor_lnum;
1755 }
1756 }
1757
1758 /*
1759 * There is no need to update lines above the top of the window.
1760 */
1761 if (from < wp->w_topline)
1762 from = wp->w_topline;
1763
1764 /*
1765 * If we know the value of w_botline, use it to restrict the update to
1766 * the lines that are visible in the window.
1767 */
1768 if (wp->w_valid & VALID_BOTLINE)
1769 {
1770 if (from >= wp->w_botline)
1771 from = wp->w_botline - 1;
1772 if (to >= wp->w_botline)
1773 to = wp->w_botline - 1;
1774 }
1775
1776 /*
1777 * Find the minimal part to be updated.
1778 * Watch out for scrolling that made entries in w_lines[] invalid.
1779 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1780 * top_end; need to redraw from top_end to the "to" line.
1781 * A middle mouse click with a Visual selection may change the text
1782 * above the Visual area and reset wl_valid, do count these for
1783 * mid_end (in srow).
1784 */
1785 if (mid_start > 0)
1786 {
1787 lnum = wp->w_topline;
1788 idx = 0;
1789 srow = 0;
1790 if (scrolled_down)
1791 mid_start = top_end;
1792 else
1793 mid_start = 0;
1794 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1795 {
1796 if (wp->w_lines[idx].wl_valid)
1797 mid_start += wp->w_lines[idx].wl_size;
1798 else if (!scrolled_down)
1799 srow += wp->w_lines[idx].wl_size;
1800 ++idx;
1801# ifdef FEAT_FOLDING
1802 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1803 lnum = wp->w_lines[idx].wl_lnum;
1804 else
1805# endif
1806 ++lnum;
1807 }
1808 srow += mid_start;
1809 mid_end = wp->w_height;
1810 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1811 {
1812 if (wp->w_lines[idx].wl_valid
1813 && wp->w_lines[idx].wl_lnum >= to + 1)
1814 {
1815 /* Only update until first row of this line */
1816 mid_end = srow;
1817 break;
1818 }
1819 srow += wp->w_lines[idx].wl_size;
1820 }
1821 }
1822 }
1823
1824 if (VIsual_active && buf == curwin->w_buffer)
1825 {
1826 wp->w_old_visual_mode = VIsual_mode;
1827 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1828 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001829 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 wp->w_old_curswant = curwin->w_curswant;
1831 }
1832 else
1833 {
1834 wp->w_old_visual_mode = 0;
1835 wp->w_old_cursor_lnum = 0;
1836 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001837 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
1840#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1841 /* reset got_int, otherwise regexp won't work */
1842 save_got_int = got_int;
1843 got_int = 0;
1844#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001845#ifdef SYN_TIME_LIMIT
1846 /* Set the time limit to 'redrawtime'. */
1847 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001848 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#ifdef FEAT_FOLDING
1851 win_foldinfo.fi_level = 0;
1852#endif
1853
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001854#ifdef FEAT_MENU
1855 /*
1856 * Draw the window toolbar, if there is one.
1857 * TODO: only when needed.
1858 */
1859 if (winbar_height(wp) > 0)
1860 redraw_win_toolbar(wp);
1861#endif
1862
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 /*
1864 * Update all the window rows.
1865 */
1866 idx = 0; /* first entry in w_lines[].wl_size */
1867 row = 0;
1868 srow = 0;
1869 lnum = wp->w_topline; /* first line shown in window */
1870 for (;;)
1871 {
1872 /* stop updating when reached the end of the window (check for _past_
1873 * the end of the window is at the end of the loop) */
1874 if (row == wp->w_height)
1875 {
1876 didline = TRUE;
1877 break;
1878 }
1879
1880 /* stop updating when hit the end of the file */
1881 if (lnum > buf->b_ml.ml_line_count)
1882 {
1883 eof = TRUE;
1884 break;
1885 }
1886
1887 /* Remember the starting row of the line that is going to be dealt
1888 * with. It is used further down when the line doesn't fit. */
1889 srow = row;
1890
1891 /*
1892 * Update a line when it is in an area that needs updating, when it
1893 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001894 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 * win_del_lines(), check if the current line includes it.
1896 * When syntax folding is being used, the saved syntax states will
1897 * already have been updated, we can't see where the syntax state is
1898 * the same again, just update until the end of the window.
1899 */
1900 if (row < top_end
1901 || (row >= mid_start && row < mid_end)
1902#ifdef FEAT_SEARCH_EXTRA
1903 || top_to_mod
1904#endif
1905 || idx >= wp->w_lines_valid
1906 || (row + wp->w_lines[idx].wl_size > bot_start)
1907 || (mod_top != 0
1908 && (lnum == mod_top
1909 || (lnum >= mod_top
1910 && (lnum < mod_bot
1911#ifdef FEAT_SYN_HL
1912 || did_update == DID_FOLD
1913 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001914 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 && (
1916# ifdef FEAT_FOLDING
1917 (foldmethodIsSyntax(wp)
1918 && hasAnyFolding(wp)) ||
1919# endif
1920 syntax_check_changed(lnum)))
1921#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001922#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001923 /* match in fixed position might need redraw
1924 * if lines were inserted or deleted */
1925 || (wp->w_match_head != NULL
1926 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 )))))
1929 {
1930#ifdef FEAT_SEARCH_EXTRA
1931 if (lnum == mod_top)
1932 top_to_mod = FALSE;
1933#endif
1934
1935 /*
1936 * When at start of changed lines: May scroll following lines
1937 * up or down to minimize redrawing.
1938 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001939 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 */
1941 if (lnum == mod_top
1942 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001943 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 int old_rows = 0;
1946 int new_rows = 0;
1947 int xtra_rows;
1948 linenr_T l;
1949
1950 /* Count the old number of window rows, using w_lines[], which
1951 * should still contain the sizes for the lines as they are
1952 * currently displayed. */
1953 for (i = idx; i < wp->w_lines_valid; ++i)
1954 {
1955 /* Only valid lines have a meaningful wl_lnum. Invalid
1956 * lines are part of the changed area. */
1957 if (wp->w_lines[i].wl_valid
1958 && wp->w_lines[i].wl_lnum == mod_bot)
1959 break;
1960 old_rows += wp->w_lines[i].wl_size;
1961#ifdef FEAT_FOLDING
1962 if (wp->w_lines[i].wl_valid
1963 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1964 {
1965 /* Must have found the last valid entry above mod_bot.
1966 * Add following invalid entries. */
1967 ++i;
1968 while (i < wp->w_lines_valid
1969 && !wp->w_lines[i].wl_valid)
1970 old_rows += wp->w_lines[i++].wl_size;
1971 break;
1972 }
1973#endif
1974 }
1975
1976 if (i >= wp->w_lines_valid)
1977 {
1978 /* We can't find a valid line below the changed lines,
1979 * need to redraw until the end of the window.
1980 * Inserting/deleting lines has no use. */
1981 bot_start = 0;
1982 }
1983 else
1984 {
1985 /* Able to count old number of rows: Count new window
1986 * rows, and may insert/delete lines */
1987 j = idx;
1988 for (l = lnum; l < mod_bot; ++l)
1989 {
1990#ifdef FEAT_FOLDING
1991 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1992 ++new_rows;
1993 else
1994#endif
1995#ifdef FEAT_DIFF
1996 if (l == wp->w_topline)
1997 new_rows += plines_win_nofill(wp, l, TRUE)
1998 + wp->w_topfill;
1999 else
2000#endif
2001 new_rows += plines_win(wp, l, TRUE);
2002 ++j;
2003 if (new_rows > wp->w_height - row - 2)
2004 {
2005 /* it's getting too much, must redraw the rest */
2006 new_rows = 9999;
2007 break;
2008 }
2009 }
2010 xtra_rows = new_rows - old_rows;
2011 if (xtra_rows < 0)
2012 {
2013 /* May scroll text up. If there is not enough
2014 * remaining text or scrolling fails, must redraw the
2015 * rest. If scrolling works, must redraw the text
2016 * below the scrolled text. */
2017 if (row - xtra_rows >= wp->w_height - 2)
2018 mod_bot = MAXLNUM;
2019 else
2020 {
2021 check_for_delay(FALSE);
2022 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002023 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 mod_bot = MAXLNUM;
2025 else
2026 bot_start = wp->w_height + xtra_rows;
2027 }
2028 }
2029 else if (xtra_rows > 0)
2030 {
2031 /* May scroll text down. If there is not enough
2032 * remaining text of scrolling fails, must redraw the
2033 * rest. */
2034 if (row + xtra_rows >= wp->w_height - 2)
2035 mod_bot = MAXLNUM;
2036 else
2037 {
2038 check_for_delay(FALSE);
2039 if (win_ins_lines(wp, row + old_rows,
2040 xtra_rows, FALSE, FALSE) == FAIL)
2041 mod_bot = MAXLNUM;
2042 else if (top_end > row + old_rows)
2043 /* Scrolled the part at the top that requires
2044 * updating down. */
2045 top_end += xtra_rows;
2046 }
2047 }
2048
2049 /* When not updating the rest, may need to move w_lines[]
2050 * entries. */
2051 if (mod_bot != MAXLNUM && i != j)
2052 {
2053 if (j < i)
2054 {
2055 int x = row + new_rows;
2056
2057 /* move entries in w_lines[] upwards */
2058 for (;;)
2059 {
2060 /* stop at last valid entry in w_lines[] */
2061 if (i >= wp->w_lines_valid)
2062 {
2063 wp->w_lines_valid = j;
2064 break;
2065 }
2066 wp->w_lines[j] = wp->w_lines[i];
2067 /* stop at a line that won't fit */
2068 if (x + (int)wp->w_lines[j].wl_size
2069 > wp->w_height)
2070 {
2071 wp->w_lines_valid = j + 1;
2072 break;
2073 }
2074 x += wp->w_lines[j++].wl_size;
2075 ++i;
2076 }
2077 if (bot_start > x)
2078 bot_start = x;
2079 }
2080 else /* j > i */
2081 {
2082 /* move entries in w_lines[] downwards */
2083 j -= i;
2084 wp->w_lines_valid += j;
2085 if (wp->w_lines_valid > wp->w_height)
2086 wp->w_lines_valid = wp->w_height;
2087 for (i = wp->w_lines_valid; i - j >= idx; --i)
2088 wp->w_lines[i] = wp->w_lines[i - j];
2089
2090 /* The w_lines[] entries for inserted lines are
2091 * now invalid, but wl_size may be used above.
2092 * Reset to zero. */
2093 while (i >= idx)
2094 {
2095 wp->w_lines[i].wl_size = 0;
2096 wp->w_lines[i--].wl_valid = FALSE;
2097 }
2098 }
2099 }
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 }
2102
2103#ifdef FEAT_FOLDING
2104 /*
2105 * When lines are folded, display one line for all of them.
2106 * Otherwise, display normally (can be several display lines when
2107 * 'wrap' is on).
2108 */
2109 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2110 if (fold_count != 0)
2111 {
2112 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2113 ++row;
2114 --fold_count;
2115 wp->w_lines[idx].wl_folded = TRUE;
2116 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2117# ifdef FEAT_SYN_HL
2118 did_update = DID_FOLD;
2119# endif
2120 }
2121 else
2122#endif
2123 if (idx < wp->w_lines_valid
2124 && wp->w_lines[idx].wl_valid
2125 && wp->w_lines[idx].wl_lnum == lnum
2126 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002127 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002128#ifdef FEAT_TEXT_PROP
2129 && !bt_popup(wp->w_buffer)
2130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 && srow + wp->w_lines[idx].wl_size > wp->w_height
2132#ifdef FEAT_DIFF
2133 && diff_check_fill(wp, lnum) == 0
2134#endif
2135 )
2136 {
2137 /* This line is not going to fit. Don't draw anything here,
2138 * will draw "@ " lines below. */
2139 row = wp->w_height + 1;
2140 }
2141 else
2142 {
2143#ifdef FEAT_SEARCH_EXTRA
2144 prepare_search_hl(wp, lnum);
2145#endif
2146#ifdef FEAT_SYN_HL
2147 /* Let the syntax stuff know we skipped a few lines. */
2148 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002149 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 syntax_end_parsing(syntax_last_parsed + 1);
2151#endif
2152
2153 /*
2154 * Display one line.
2155 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002156 row = win_line(wp, lnum, srow, wp->w_height,
2157 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158
2159#ifdef FEAT_FOLDING
2160 wp->w_lines[idx].wl_folded = FALSE;
2161 wp->w_lines[idx].wl_lastlnum = lnum;
2162#endif
2163#ifdef FEAT_SYN_HL
2164 did_update = DID_LINE;
2165 syntax_last_parsed = lnum;
2166#endif
2167 }
2168
2169 wp->w_lines[idx].wl_lnum = lnum;
2170 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002171
2172 /* Past end of the window or end of the screen. Note that after
2173 * resizing wp->w_height may be end up too big. That's a problem
2174 * elsewhere, but prevent a crash here. */
2175 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002178 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2180 ++idx;
2181 break;
2182 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002183 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 wp->w_lines[idx].wl_size = row - srow;
2185 ++idx;
2186#ifdef FEAT_FOLDING
2187 lnum += fold_count + 1;
2188#else
2189 ++lnum;
2190#endif
2191 }
2192 else
2193 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002194 if (wp->w_p_rnu)
2195 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002196#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002197 // 'relativenumber' set: The text doesn't need to be drawn, but
2198 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002199 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2200 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002201 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002202 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002203#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002204 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002205 }
2206
2207 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 row += wp->w_lines[idx++].wl_size;
2209 if (row > wp->w_height) /* past end of screen */
2210 break;
2211#ifdef FEAT_FOLDING
2212 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2213#else
2214 ++lnum;
2215#endif
2216#ifdef FEAT_SYN_HL
2217 did_update = DID_NONE;
2218#endif
2219 }
2220
2221 if (lnum > buf->b_ml.ml_line_count)
2222 {
2223 eof = TRUE;
2224 break;
2225 }
2226 }
2227 /*
2228 * End of loop over all window lines.
2229 */
2230
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002231#ifdef FEAT_VTP
2232 /* Rewrite the character at the end of the screen line. */
2233 if (use_vtp())
2234 {
2235 int i;
2236
2237 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002238 if (enc_utf8)
2239 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2240 LineOffset[i] + screen_Columns) > 1)
2241 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2242 else
2243 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2244 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002245 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2246 }
2247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249 if (idx > wp->w_lines_valid)
2250 wp->w_lines_valid = idx;
2251
2252#ifdef FEAT_SYN_HL
2253 /*
2254 * Let the syntax stuff know we stop parsing here.
2255 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002256 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 syntax_end_parsing(syntax_last_parsed + 1);
2258#endif
2259
2260 /*
2261 * If we didn't hit the end of the file, and we didn't finish the last
2262 * line we were working on, then the line didn't fit.
2263 */
2264 wp->w_empty_rows = 0;
2265#ifdef FEAT_DIFF
2266 wp->w_filler_rows = 0;
2267#endif
2268 if (!eof && !didline)
2269 {
2270 if (lnum == wp->w_topline)
2271 {
2272 /*
2273 * Single line that does not fit!
2274 * Don't overwrite it, it can be edited.
2275 */
2276 wp->w_botline = lnum + 1;
2277 }
2278#ifdef FEAT_DIFF
2279 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2280 {
2281 /* Window ends in filler lines. */
2282 wp->w_botline = lnum;
2283 wp->w_filler_rows = wp->w_height - srow;
2284 }
2285#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002286#ifdef FEAT_TEXT_PROP
2287 else if (bt_popup(wp->w_buffer))
2288 {
2289 // popup line that doesn't fit is left as-is
2290 wp->w_botline = lnum;
2291 }
2292#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002293 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2294 {
2295 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2296
2297 /*
2298 * Last line isn't finished: Display "@@@" in the last screen line.
2299 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002300 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002301 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002302 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002303 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002304 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002305 set_empty_rows(wp, srow);
2306 wp->w_botline = lnum;
2307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2309 {
2310 /*
2311 * Last line isn't finished: Display "@@@" at the end.
2312 */
2313 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2314 W_WINROW(wp) + wp->w_height,
2315 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002316 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 set_empty_rows(wp, srow);
2318 wp->w_botline = lnum;
2319 }
2320 else
2321 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002322 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 wp->w_botline = lnum;
2324 }
2325 }
2326 else
2327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (eof) /* we hit the end of the file */
2330 {
2331 wp->w_botline = buf->b_ml.ml_line_count + 1;
2332#ifdef FEAT_DIFF
2333 j = diff_check_fill(wp, wp->w_botline);
2334 if (j > 0 && !wp->w_botfill)
2335 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002336 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 if (char2cells(fill_diff) > 1)
2338 i = '-';
2339 else
2340 i = fill_diff;
2341 if (row + j > wp->w_height)
2342 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002343 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 row += j;
2345 }
2346#endif
2347 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002348 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 wp->w_botline = lnum;
2350
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002351 // Make sure the rest of the screen is blank
2352 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002353 win_draw_end(wp,
2354#ifdef FEAT_TEXT_PROP
2355 bt_popup(wp->w_buffer) ? ' ' :
2356#endif
2357 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002360#ifdef SYN_TIME_LIMIT
2361 syn_set_timeout(NULL);
2362#endif
2363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 /* Reset the type of redrawing required, the window has been updated. */
2365 wp->w_redr_type = 0;
2366#ifdef FEAT_DIFF
2367 wp->w_old_topfill = wp->w_topfill;
2368 wp->w_old_botfill = wp->w_botfill;
2369#endif
2370
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002371 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 /*
2374 * There is a trick with w_botline. If we invalidate it on each
2375 * change that might modify it, this will cause a lot of expensive
2376 * calls to plines() in update_topline() each time. Therefore the
2377 * value of w_botline is often approximated, and this value is used to
2378 * compute the value of w_topline. If the value of w_botline was
2379 * wrong, check that the value of w_topline is correct (cursor is on
2380 * the visible part of the text). If it's not, we need to redraw
2381 * again. Mostly this just means scrolling up a few lines, so it
2382 * doesn't look too bad. Only do this for the current window (where
2383 * changes are relevant).
2384 */
2385 wp->w_valid |= VALID_BOTLINE;
2386 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2387 {
2388 recursive = TRUE;
2389 curwin->w_valid &= ~VALID_TOPLINE;
2390 update_topline(); /* may invalidate w_botline again */
2391 if (must_redraw != 0)
2392 {
2393 /* Don't update for changes in buffer again. */
2394 i = curbuf->b_mod_set;
2395 curbuf->b_mod_set = FALSE;
2396 win_update(curwin);
2397 must_redraw = 0;
2398 curbuf->b_mod_set = i;
2399 }
2400 recursive = FALSE;
2401 }
2402 }
2403
2404#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2405 /* restore got_int, unless CTRL-C was hit while redrawing */
2406 if (!got_int)
2407 got_int = save_got_int;
2408#endif
2409}
2410
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002412 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2413 * Return the new offset.
2414 */
2415 static int
2416screen_fill_end(
2417 win_T *wp,
2418 int c1,
2419 int c2,
2420 int off,
2421 int width,
2422 int row,
2423 int endrow,
2424 int attr)
2425{
2426 int nn = off + width;
2427
2428 if (nn > wp->w_width)
2429 nn = wp->w_width;
2430#ifdef FEAT_RIGHTLEFT
2431 if (wp->w_p_rl)
2432 {
2433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2434 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2435 c1, c2, attr);
2436 }
2437 else
2438#endif
2439 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2440 wp->w_wincol + off, (int)wp->w_wincol + nn,
2441 c1, c2, attr);
2442 return nn;
2443}
2444
2445/*
2446 * Clear lines near the end the window and mark the unused lines with "c1".
2447 * use "c2" as the filler character.
2448 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 */
2450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002451win_draw_end(
2452 win_T *wp,
2453 int c1,
2454 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002455 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002456 int row,
2457 int endrow,
2458 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002461 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002462 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002463
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002464 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002465
2466 if (draw_margin)
2467 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002468#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002469 int fdc = compute_foldcolumn(wp, 0);
2470
2471 if (fdc > 0)
2472 // draw the fold column
2473 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002474 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002475#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002476#ifdef FEAT_SIGNS
2477 if (signcolumn_on(wp))
2478 // draw the sign column
2479 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002480 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002481#endif
2482 if ((wp->w_p_nu || wp->w_p_rnu)
2483 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2484 // draw the number column
2485 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002486 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489#ifdef FEAT_RIGHTLEFT
2490 if (wp->w_p_rl)
2491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002493 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002494 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002496 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002497 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 }
2499 else
2500#endif
2501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002503 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002504 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 set_empty_rows(wp, row);
2508}
2509
Bram Moolenaar1a384422010-07-14 19:53:30 +02002510#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002511/*
2512 * Advance **color_cols and return TRUE when there are columns to draw.
2513 */
2514 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002515advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002516{
2517 while (**color_cols >= 0 && vcol > **color_cols)
2518 ++*color_cols;
2519 return (**color_cols >= 0);
2520}
2521#endif
2522
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002523#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2524/*
2525 * Copy "text" to ScreenLines using "attr".
2526 * Returns the next screen column.
2527 */
2528 static int
2529text_to_screenline(win_T *wp, char_u *text, int col)
2530{
2531 int off = (int)(current_ScreenLine - ScreenLines);
2532
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002533 if (has_mbyte)
2534 {
2535 int cells;
2536 int u8c, u8cc[MAX_MCO];
2537 int i;
2538 int idx;
2539 int c_len;
2540 char_u *p;
2541# ifdef FEAT_ARABIC
2542 int prev_c = 0; /* previous Arabic character */
2543 int prev_c1 = 0; /* first composing char for prev_c */
2544# endif
2545
2546# ifdef FEAT_RIGHTLEFT
2547 if (wp->w_p_rl)
2548 idx = off;
2549 else
2550# endif
2551 idx = off + col;
2552
2553 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2554 for (p = text; *p != NUL; )
2555 {
2556 cells = (*mb_ptr2cells)(p);
2557 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002558 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002559# ifdef FEAT_RIGHTLEFT
2560 - (wp->w_p_rl ? col : 0)
2561# endif
2562 )
2563 break;
2564 ScreenLines[idx] = *p;
2565 if (enc_utf8)
2566 {
2567 u8c = utfc_ptr2char(p, u8cc);
2568 if (*p < 0x80 && u8cc[0] == 0)
2569 {
2570 ScreenLinesUC[idx] = 0;
2571#ifdef FEAT_ARABIC
2572 prev_c = u8c;
2573#endif
2574 }
2575 else
2576 {
2577#ifdef FEAT_ARABIC
2578 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2579 {
2580 /* Do Arabic shaping. */
2581 int pc, pc1, nc;
2582 int pcc[MAX_MCO];
2583 int firstbyte = *p;
2584
2585 /* The idea of what is the previous and next
2586 * character depends on 'rightleft'. */
2587 if (wp->w_p_rl)
2588 {
2589 pc = prev_c;
2590 pc1 = prev_c1;
2591 nc = utf_ptr2char(p + c_len);
2592 prev_c1 = u8cc[0];
2593 }
2594 else
2595 {
2596 pc = utfc_ptr2char(p + c_len, pcc);
2597 nc = prev_c;
2598 pc1 = pcc[0];
2599 }
2600 prev_c = u8c;
2601
2602 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2603 pc, pc1, nc);
2604 ScreenLines[idx] = firstbyte;
2605 }
2606 else
2607 prev_c = u8c;
2608#endif
2609 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002610 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002611 for (i = 0; i < Screen_mco; ++i)
2612 {
2613 ScreenLinesC[i][idx] = u8cc[i];
2614 if (u8cc[i] == 0)
2615 break;
2616 }
2617 }
2618 if (cells > 1)
2619 ScreenLines[idx + 1] = 0;
2620 }
2621 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2622 /* double-byte single width character */
2623 ScreenLines2[idx] = p[1];
2624 else if (cells > 1)
2625 /* double-width character */
2626 ScreenLines[idx + 1] = p[1];
2627 col += cells;
2628 idx += cells;
2629 p += c_len;
2630 }
2631 }
2632 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002633 {
2634 int len = (int)STRLEN(text);
2635
Bram Moolenaar02631462017-09-22 15:20:32 +02002636 if (len > wp->w_width - col)
2637 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002638 if (len > 0)
2639 {
2640#ifdef FEAT_RIGHTLEFT
2641 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002642 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002643 else
2644#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002645 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002646 col += len;
2647 }
2648 }
2649 return col;
2650}
2651#endif
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653#ifdef FEAT_FOLDING
2654/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002655 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2656 * space is available for window "wp", minus "col".
2657 */
2658 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002659compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002660{
2661 int fdc = wp->w_p_fdc;
2662 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002663 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002664
2665 if (fdc > wwidth - (col + wmw))
2666 fdc = wwidth - (col + wmw);
2667 return fdc;
2668}
2669
2670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 * Display one folded line.
2672 */
2673 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002674fold_line(
2675 win_T *wp,
2676 long fold_count,
2677 foldinfo_T *foldinfo,
2678 linenr_T lnum,
2679 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002681 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 pos_T *top, *bot;
2683 linenr_T lnume = lnum + fold_count - 1;
2684 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002685 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 int col;
2688 int txtcol;
2689 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002690 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 /* Build the fold line:
2693 * 1. Add the cmdwin_type for the command-line window
2694 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002695 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 * 4. Compose the text
2697 * 5. Add the text
2698 * 6. set highlighting for the Visual area an other text
2699 */
2700 col = 0;
2701
2702 /*
2703 * 1. Add the cmdwin_type for the command-line window
2704 * Ignores 'rightleft', this window is never right-left.
2705 */
2706#ifdef FEAT_CMDWIN
2707 if (cmdwin_type != 0 && wp == curwin)
2708 {
2709 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002710 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (enc_utf8)
2712 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 ++col;
2714 }
2715#endif
2716
2717 /*
2718 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002719 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002721 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 if (fdc > 0)
2723 {
2724 fill_foldcolumn(buf, wp, TRUE, lnum);
2725#ifdef FEAT_RIGHTLEFT
2726 if (wp->w_p_rl)
2727 {
2728 int i;
2729
Bram Moolenaar02631462017-09-22 15:20:32 +02002730 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002731 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 /* reverse the fold column */
2733 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002734 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 else
2737#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002738 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 col += fdc;
2740 }
2741
2742#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002743# define RL_MEMSET(p, v, l) \
2744 do { \
2745 if (wp->w_p_rl) \
2746 for (ri = 0; ri < l; ++ri) \
2747 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2748 else \
2749 for (ri = 0; ri < l; ++ri) \
2750 ScreenAttrs[off + (p) + ri] = v; \
2751 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002753# define RL_MEMSET(p, v, l) \
2754 do { \
2755 for (ri = 0; ri < l; ++ri) \
2756 ScreenAttrs[off + (p) + ri] = v; \
2757 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759
Bram Moolenaar64486672010-05-16 15:46:46 +02002760 /* Set all attributes of the 'number' or 'relativenumber' column and the
2761 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
2764#ifdef FEAT_SIGNS
2765 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002766 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002768 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 if (len > 0)
2770 {
2771 if (len > 2)
2772 len = 2;
2773# ifdef FEAT_RIGHTLEFT
2774 if (wp->w_p_rl)
2775 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002776 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002777 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 else
2779# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002780 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 col += len;
2782 }
2783 }
2784#endif
2785
2786 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002787 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002789 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002791 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (len > 0)
2793 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002794 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002795 long num;
2796 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002797
2798 if (len > w + 1)
2799 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002800
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002801 if (wp->w_p_nu && !wp->w_p_rnu)
2802 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002803 num = (long)lnum;
2804 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002805 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002806 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002807 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002808 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002809 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002810 /* 'number' + 'relativenumber': cursor line shows absolute
2811 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002812 num = lnum;
2813 fmt = "%-*ld ";
2814 }
2815 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002816
Bram Moolenaar700e7342013-01-30 12:31:36 +01002817 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818#ifdef FEAT_RIGHTLEFT
2819 if (wp->w_p_rl)
2820 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002821 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002822 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 else
2824#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002825 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 col += len;
2827 }
2828 }
2829
2830 /*
2831 * 4. Compose the folded-line string with 'foldtext', if set.
2832 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002833 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
2835 txtcol = col; /* remember where text starts */
2836
2837 /*
2838 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2839 * Right-left text is put in columns 0 - number-col, normal text is put
2840 * in columns number-col - window-width.
2841 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002842 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
2844 /* Fill the rest of the line with the fold filler */
2845#ifdef FEAT_RIGHTLEFT
2846 if (wp->w_p_rl)
2847 col -= txtcol;
2848#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002849 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#ifdef FEAT_RIGHTLEFT
2851 - (wp->w_p_rl ? txtcol : 0)
2852#endif
2853 )
2854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 if (enc_utf8)
2856 {
2857 if (fill_fold >= 0x80)
2858 {
2859 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002860 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002861 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002866 ScreenLines[off + col] = fill_fold;
2867 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002868 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002870 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002871 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873
2874 if (text != buf)
2875 vim_free(text);
2876
2877 /*
2878 * 6. set highlighting for the Visual area an other text.
2879 * If all folded lines are in the Visual area, highlight the line.
2880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2882 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002883 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 /* Visual is after curwin->w_cursor */
2886 top = &curwin->w_cursor;
2887 bot = &VIsual;
2888 }
2889 else
2890 {
2891 /* Visual is before curwin->w_cursor */
2892 top = &VIsual;
2893 bot = &curwin->w_cursor;
2894 }
2895 if (lnum >= top->lnum
2896 && lnume <= bot->lnum
2897 && (VIsual_mode != 'v'
2898 || ((lnum > top->lnum
2899 || (lnum == top->lnum
2900 && top->col == 0))
2901 && (lnume < bot->lnum
2902 || (lnume == bot->lnum
2903 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002904 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
2906 if (VIsual_mode == Ctrl_V)
2907 {
2908 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002911 if (wp->w_old_cursor_lcol != MAXCOL
2912 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002913 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 len = wp->w_old_cursor_lcol;
2915 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002916 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002917 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002918 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920 }
2921 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002924 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002929#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002930 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2931 if (wp->w_p_cc_cols)
2932 {
2933 int i = 0;
2934 int j = wp->w_p_cc_cols[i];
2935 int old_txtcol = txtcol;
2936
2937 while (j > -1)
2938 {
2939 txtcol += j;
2940 if (wp->w_p_wrap)
2941 txtcol -= wp->w_skipcol;
2942 else
2943 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002944 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002945 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002946 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002947 txtcol = old_txtcol;
2948 j = wp->w_p_cc_cols[++i];
2949 }
2950 }
2951
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002952 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002953 if (wp->w_p_cuc)
2954 {
2955 txtcol += wp->w_virtcol;
2956 if (wp->w_p_wrap)
2957 txtcol -= wp->w_skipcol;
2958 else
2959 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002960 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002961 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002962 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002963 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaar02631462017-09-22 15:20:32 +02002966 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002967 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969 /*
2970 * Update w_cline_height and w_cline_folded if the cursor line was
2971 * updated (saves a call to plines() later).
2972 */
2973 if (wp == curwin
2974 && lnum <= curwin->w_cursor.lnum
2975 && lnume >= curwin->w_cursor.lnum)
2976 {
2977 curwin->w_cline_row = row;
2978 curwin->w_cline_height = 1;
2979 curwin->w_cline_folded = TRUE;
2980 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2981 }
2982}
2983
2984/*
2985 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2986 */
2987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002988copy_text_attr(
2989 int off,
2990 char_u *buf,
2991 int len,
2992 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002994 int i;
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 if (enc_utf8)
2998 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002999 for (i = 0; i < len; ++i)
3000 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001}
3002
3003/*
3004 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003005 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 */
3007 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003008fill_foldcolumn(
3009 char_u *p,
3010 win_T *wp,
3011 int closed, /* TRUE of FALSE */
3012 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 int i = 0;
3015 int level;
3016 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003017 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003018 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003021 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 level = win_foldinfo.fi_level;
3024 if (level > 0)
3025 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003026 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003027 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003028
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 /* If the column is too narrow, we start at the lowest level that
3030 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003031 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (first_level < 1)
3033 first_level = 1;
3034
Bram Moolenaar1c934292015-01-27 16:39:29 +01003035 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 {
3037 if (win_foldinfo.fi_lnum == lnum
3038 && first_level + i >= win_foldinfo.fi_low_level)
3039 p[i] = '-';
3040 else if (first_level == 1)
3041 p[i] = '|';
3042 else if (first_level + i <= 9)
3043 p[i] = '0' + first_level + i;
3044 else
3045 p[i] = '>';
3046 if (first_level + i == level)
3047 break;
3048 }
3049 }
3050 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003051 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052}
3053#endif /* FEAT_FOLDING */
3054
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003055#ifdef FEAT_TEXT_PROP
3056static textprop_T *current_text_props = NULL;
3057static buf_T *current_buf = NULL;
3058
3059 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003060text_prop_compare(const void *s1, const void *s2)
3061{
3062 int idx1, idx2;
3063 proptype_T *pt1, *pt2;
3064 colnr_T col1, col2;
3065
3066 idx1 = *(int *)s1;
3067 idx2 = *(int *)s2;
3068 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3069 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3070 if (pt1 == pt2)
3071 return 0;
3072 if (pt1 == NULL)
3073 return -1;
3074 if (pt2 == NULL)
3075 return 1;
3076 if (pt1->pt_priority != pt2->pt_priority)
3077 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3078 col1 = current_text_props[idx1].tp_col;
3079 col2 = current_text_props[idx2].tp_col;
3080 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3081}
3082#endif
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084/*
3085 * Display line "lnum" of window 'wp' on the screen.
3086 * Start at row "startrow", stop when "endrow" is reached.
3087 * wp->w_virtcol needs to be valid.
3088 *
3089 * Return the number of last row the line occupies.
3090 */
3091 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003092win_line(
3093 win_T *wp,
3094 linenr_T lnum,
3095 int startrow,
3096 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003097 int nochange UNUSED, // not updating for changed text
3098 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003100 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3102 int c = 0; /* init for GCC */
3103 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003104#ifdef FEAT_LINEBREAK
3105 long vcol_sbr = -1; /* virtual column after showbreak */
3106#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 long vcol_prev = -1; /* "vcol" of previous character */
3108 char_u *line; /* current line */
3109 char_u *ptr; /* current position in "line" */
3110 int row; /* row in the window, excl w_winrow */
3111 int screen_row; /* row on the screen, incl w_winrow */
3112
3113 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3114 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003115 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003116 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003118 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 int extra_attr = 0; /* attributes when n_extra != 0 */
3120 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3121 displaying lcs_eol at end-of-line */
3122 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3123 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3124
3125 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3126 int saved_n_extra = 0;
3127 char_u *saved_p_extra = NULL;
3128 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003129 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 int saved_char_attr = 0;
3131
3132 int n_attr = 0; /* chars with special attr */
3133 int saved_attr2 = 0; /* char_attr saved for n_attr */
3134 int n_attr3 = 0; /* chars with overruling special attr */
3135 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3136
3137 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3138
3139 int fromcol, tocol; /* start/end of inverting */
3140 int fromcol_prev = -2; /* start of inverting after cursor */
3141 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003143 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 pos_T pos;
3145 long v;
3146
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003147 int char_attr = 0; // attributes for next character
3148 int attr_pri = FALSE; // char_attr has priority
3149 int area_highlighting = FALSE; // Visual or incsearch highlighting
3150 // in this line
3151 int vi_attr = 0; // attributes for Visual and incsearch
3152 // highlighting
3153 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003154 int win_attr = 0; // background for whole window, except
3155 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003156 int area_attr = 0; // attributes desired by highlighting
3157 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003159 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 int syntax_attr = 0; /* attributes desired by syntax */
3161 int has_syntax = FALSE; /* this buffer has syntax highl. */
3162 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003163 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003164 int draw_color_col = FALSE; /* highlight colorcolumn */
3165 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003166#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003167#ifdef FEAT_TEXT_PROP
3168 int text_prop_count;
3169 int text_prop_next = 0; // next text property to use
3170 textprop_T *text_props = NULL;
3171 int *text_prop_idxs = NULL;
3172 int text_props_active = 0;
3173 proptype_T *text_prop_type = NULL;
3174 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003175 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003176#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003177#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003178 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003179# define SPWORDLEN 150
3180 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003181 int nextlinecol = 0; /* column where nextline[] starts */
3182 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003183 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003184 int spell_attr = 0; /* attributes desired by spelling */
3185 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003186 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3187 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003188 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003189 static int cap_col = -1; /* column to check for Cap word */
3190 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003191 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003193 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 int multi_attr = 0; /* attributes desired by multibyte */
3195 int mb_l = 1; /* multi-byte byte length */
3196 int mb_c = 0; /* decoded multi-byte character */
3197 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003198 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#ifdef FEAT_DIFF
3200 int filler_lines; /* nr of filler lines to be drawn */
3201 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003202 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 int change_start = MAXCOL; /* first col of changed area */
3204 int change_end = -1; /* last col of changed area */
3205#endif
3206 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3207#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003208 int need_showbreak = FALSE; /* overlong line, skipping first x
3209 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003211#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003212 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003214 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#endif
3216#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003217 matchitem_T *cur; /* points to the match list */
3218 match_T *shl; /* points to search_hl or a match */
3219 int shl_flag; /* flag to indicate whether search_hl
3220 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003221 int pos_inprogress; /* marks that position match search is
3222 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003223 int prevcol_hl_flag; /* flag to indicate whether prevcol
3224 equals startcol of search_hl or one
3225 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226#endif
3227#ifdef FEAT_ARABIC
3228 int prev_c = 0; /* previous Arabic character */
3229 int prev_c1 = 0; /* first composing char for prev_c */
3230#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003231#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003232 int did_line_attr = 0;
3233#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003234#ifdef FEAT_TERMINAL
3235 int get_term_attr = FALSE;
3236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 /* draw_state: items that are drawn in sequence: */
3239#define WL_START 0 /* nothing done yet */
3240#ifdef FEAT_CMDWIN
3241# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3242#else
3243# define WL_CMDLINE WL_START
3244#endif
3245#ifdef FEAT_FOLDING
3246# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3247#else
3248# define WL_FOLD WL_CMDLINE
3249#endif
3250#ifdef FEAT_SIGNS
3251# define WL_SIGN WL_FOLD + 1 /* column for signs */
3252#else
3253# define WL_SIGN WL_FOLD /* column for signs */
3254#endif
3255#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003256#ifdef FEAT_LINEBREAK
3257# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003259# define WL_BRI WL_NR
3260#endif
3261#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3262# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3263#else
3264# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#endif
3266#define WL_LINE WL_SBR + 1 /* text in the line */
3267 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003268#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 int feedback_col = 0;
3270 int feedback_old_attr = -1;
3271#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003272 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar860cae12010-06-05 23:22:07 +02003274#ifdef FEAT_CONCEAL
3275 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003276 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003277 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003278 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003279 int is_concealing = FALSE;
3280 int boguscols = 0; /* nonexistent columns added to force
3281 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003282 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003283 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003284 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003285 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003286# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003287# define FIX_FOR_BOGUSCOLS \
3288 { \
3289 n_extra += vcol_off; \
3290 vcol -= vcol_off; \
3291 vcol_off = 0; \
3292 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003293 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003294 boguscols = 0; \
3295 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003296#else
3297# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299
3300 if (startrow > endrow) /* past the end already! */
3301 return startrow;
3302
3303 row = startrow;
3304 screen_row = row + W_WINROW(wp);
3305
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003306 if (!number_only)
3307 {
3308 /*
3309 * To speed up the loop below, set extra_check when there is linebreak,
3310 * trailing white space and/or syntax processing to be done.
3311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003313 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#endif
3315#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003316 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003317# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003318 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003319# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003322 /* Prepare for syntax highlighting in this line. When there is an
3323 * error, stop syntax highlighting. */
3324 save_did_emsg = did_emsg;
3325 did_emsg = FALSE;
3326 syntax_start(wp, lnum);
3327 if (did_emsg)
3328 wp->w_s->b_syn_error = TRUE;
3329 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003330 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003331 did_emsg = save_did_emsg;
3332#ifdef SYN_TIME_LIMIT
3333 if (!wp->w_s->b_syn_slow)
3334#endif
3335 {
3336 has_syntax = TRUE;
3337 extra_check = TRUE;
3338 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003341
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003342 /* Check for columns to display for 'colorcolumn'. */
3343 color_cols = wp->w_p_cc_cols;
3344 if (color_cols != NULL)
3345 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003346#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003347
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003348#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003349 if (term_show_buffer(wp->w_buffer))
3350 {
3351 extra_check = TRUE;
3352 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003353 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003354 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003355#endif
3356
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003357#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 if (wp->w_p_spell
3359 && *wp->w_s->b_p_spl != NUL
3360 && wp->w_s->b_langp.ga_len > 0
3361 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003362 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 /* Prepare for spell checking. */
3364 has_spell = TRUE;
3365 extra_check = TRUE;
3366
Bram Moolenaar7701f302018-10-02 21:20:32 +02003367 /* Get the start of the next line, so that words that wrap to the
3368 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003369 * Trick: skip a few chars for C/shell/Vim comments */
3370 nextline[SPWORDLEN] = NUL;
3371 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3372 {
3373 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3374 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3375 }
3376
Bram Moolenaar7701f302018-10-02 21:20:32 +02003377 /* When a word wrapped from the previous line the start of the
3378 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003379 if (lnum == checked_lnum)
3380 cur_checked_col = checked_col;
3381 checked_lnum = 0;
3382
3383 /* When there was a sentence end in the previous line may require a
3384 * word starting with capital in this line. In line 1 always check
3385 * the first word. */
3386 if (lnum != capcol_lnum)
3387 cap_col = -1;
3388 if (lnum == 1)
3389 cap_col = 0;
3390 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392#endif
3393
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003394 /*
3395 * handle visual active in this window
3396 */
3397 fromcol = -10;
3398 tocol = MAXCOL;
3399 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 /* Visual is after curwin->w_cursor */
3402 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003404 top = &curwin->w_cursor;
3405 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003409 top = &VIsual;
3410 bot = &curwin->w_cursor;
3411 }
3412 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3413 if (VIsual_mode == Ctrl_V) /* block mode */
3414 {
3415 if (lnum_in_visual_area)
3416 {
3417 fromcol = wp->w_old_cursor_fcol;
3418 tocol = wp->w_old_cursor_lcol;
3419 }
3420 }
3421 else /* non-block mode */
3422 {
3423 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003425 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003427 if (VIsual_mode == 'V') /* linewise */
3428 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 else
3430 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003431 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3432 if (gchar_pos(top) == NUL)
3433 tocol = fromcol + 1;
3434 }
3435 }
3436 if (VIsual_mode != 'V' && lnum == bot->lnum)
3437 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003438 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 {
3440 fromcol = -10;
3441 tocol = MAXCOL;
3442 }
3443 else if (bot->col == MAXCOL)
3444 tocol = MAXCOL;
3445 else
3446 {
3447 pos = *bot;
3448 if (*p_sel == 'e')
3449 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3450 else
3451 {
3452 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3453 ++tocol;
3454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456 }
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 /* Check if the character under the cursor should not be inverted */
3460 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003461#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003463#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003464 )
3465 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 /* if inverting in this line set area_highlighting */
3468 if (fromcol >= 0)
3469 {
3470 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003471 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003473 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003474 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003475 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003476 && clip_isautosel_plus()))
3477 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 /*
3483 * handle 'incsearch' and ":s///c" highlighting
3484 */
3485 else if (highlight_match
3486 && wp == curwin
3487 && lnum >= curwin->w_cursor.lnum
3488 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003490 if (lnum == curwin->w_cursor.lnum)
3491 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003492 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003493 else
3494 fromcol = 0;
3495 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3496 {
3497 pos.lnum = lnum;
3498 pos.col = search_match_endcol;
3499 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3500 }
3501 else
3502 tocol = MAXCOL;
3503 /* do at least one character; happens when past end of line */
3504 if (fromcol == tocol)
3505 tocol = fromcol + 1;
3506 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003507 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510
3511#ifdef FEAT_DIFF
3512 filler_lines = diff_check(wp, lnum);
3513 if (filler_lines < 0)
3514 {
3515 if (filler_lines == -1)
3516 {
3517 if (diff_find_change(wp, lnum, &change_start, &change_end))
3518 diff_hlf = HLF_ADD; /* added line */
3519 else if (change_start == 0)
3520 diff_hlf = HLF_TXD; /* changed text */
3521 else
3522 diff_hlf = HLF_CHD; /* changed line */
3523 }
3524 else
3525 diff_hlf = HLF_ADD; /* added line */
3526 filler_lines = 0;
3527 area_highlighting = TRUE;
3528 }
3529 if (lnum == wp->w_topline)
3530 filler_lines = wp->w_topfill;
3531 filler_todo = filler_lines;
3532#endif
3533
3534#ifdef LINE_ATTR
3535# ifdef FEAT_SIGNS
3536 /* If this line has a sign with line highlighting set line_attr. */
3537 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3538 if (v != 0)
3539 line_attr = sign_get_attr((int)v, TRUE);
3540# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003541# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003543 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003544 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545# endif
3546 if (line_attr != 0)
3547 area_highlighting = TRUE;
3548#endif
3549
3550 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3551 ptr = line;
3552
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003553#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003554 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003555 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003556 /* For checking first word with a capital skip white space. */
3557 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003558 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003559
Bram Moolenaar30abd282005-06-22 22:35:10 +00003560 /* To be able to spell-check over line boundaries copy the end of the
3561 * current line into nextline[]. Above the start of the next line was
3562 * copied to nextline[SPWORDLEN]. */
3563 if (nextline[SPWORDLEN] == NUL)
3564 {
3565 /* No next line or it is empty. */
3566 nextlinecol = MAXCOL;
3567 nextline_idx = 0;
3568 }
3569 else
3570 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003571 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003572 if (v < SPWORDLEN)
3573 {
3574 /* Short line, use it completely and append the start of the
3575 * next line. */
3576 nextlinecol = 0;
3577 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003578 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003579 nextline_idx = v + 1;
3580 }
3581 else
3582 {
3583 /* Long line, use only the last SPWORDLEN bytes. */
3584 nextlinecol = v - SPWORDLEN;
3585 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3586 nextline_idx = SPWORDLEN + 1;
3587 }
3588 }
3589 }
3590#endif
3591
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003592 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003594 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003595 extra_check = TRUE;
3596 /* find start of trailing whitespace */
3597 if (lcs_trail)
3598 {
3599 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003600 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003601 --trailcol;
3602 trailcol += (colnr_T) (ptr - line);
3603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 }
3605
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003606 wcr_attr = get_wcr_attr(wp);
3607 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003608 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003609 win_attr = wcr_attr;
3610 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003611 }
3612#ifdef FEAT_TEXT_PROP
3613 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003614 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003615#endif
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 /*
3618 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3619 * first character to be displayed.
3620 */
3621 if (wp->w_p_wrap)
3622 v = wp->w_skipcol;
3623 else
3624 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003625 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 while (vcol < v && *ptr != NUL)
3630 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003631 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003634 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003637 /* When:
3638 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003639 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003640 * - 'virtualedit' is set, or
3641 * - the visual mode is active,
3642 * the end of the line may be before the start of the displayed part.
3643 */
3644 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003645#ifdef FEAT_SYN_HL
3646 wp->w_p_cuc || draw_color_col ||
3647#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003648 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003649 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 /* Handle a character that's not completely on the screen: Put ptr at
3653 * that character but skip the first few screen characters. */
3654 if (vcol > v)
3655 {
3656 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003658 /* If the character fits on the screen, don't need to skip it.
3659 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003660 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003661 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663
3664 /*
3665 * Adjust for when the inverted text is before the screen,
3666 * and when the start of the inverted text is before the screen.
3667 */
3668 if (tocol <= vcol)
3669 fromcol = 0;
3670 else if (fromcol >= 0 && fromcol < vcol)
3671 fromcol = vcol;
3672
3673#ifdef FEAT_LINEBREAK
3674 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3675 if (wp->w_p_wrap)
3676 need_showbreak = TRUE;
3677#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003678#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003679 /* When spell checking a word we need to figure out the start of the
3680 * word and if it's badly spelled or not. */
3681 if (has_spell)
3682 {
3683 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003684 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003685 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003686
3687 pos = wp->w_cursor;
3688 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003689 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003690 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003691
3692 /* spell_move_to() may call ml_get() and make "line" invalid */
3693 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3694 ptr = line + linecol;
3695
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003696 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003697 {
3698 /* no bad word found at line start, don't check until end of a
3699 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003700 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003701 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003702 }
3703 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003704 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003705 /* bad word found, use attributes until end of word */
3706 word_end = wp->w_cursor.col + len + 1;
3707
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003708 /* Turn index into actual attributes. */
3709 if (spell_hlf != HLF_COUNT)
3710 spell_attr = highlight_attr[spell_hlf];
3711 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003712 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003713
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003714# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003715 /* Need to restart syntax highlighting for this line. */
3716 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003717 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003718# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003719 }
3720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722
3723 /*
3724 * Correct highlighting for cursor that can't be disabled.
3725 * Avoids having to check this for each character.
3726 */
3727 if (fromcol >= 0)
3728 {
3729 if (noinvcur)
3730 {
3731 if ((colnr_T)fromcol == wp->w_virtcol)
3732 {
3733 /* highlighting starts at cursor, let it start just after the
3734 * cursor */
3735 fromcol_prev = fromcol;
3736 fromcol = -1;
3737 }
3738 else if ((colnr_T)fromcol < wp->w_virtcol)
3739 /* restart highlighting after the cursor */
3740 fromcol_prev = wp->w_virtcol;
3741 }
3742 if (fromcol >= tocol)
3743 fromcol = -1;
3744 }
3745
3746#ifdef FEAT_SEARCH_EXTRA
3747 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003748 * Handle highlighting the last used search pattern and matches.
3749 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003750 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003752 cur = wp->w_match_head;
3753 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003754 while ((cur != NULL || shl_flag == FALSE) && !number_only
3755 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003757 if (shl_flag == FALSE)
3758 {
3759 shl = &search_hl;
3760 shl_flag = TRUE;
3761 }
3762 else
3763 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003764 shl->startcol = MAXCOL;
3765 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003767 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003768 v = (long)(ptr - line);
3769 if (cur != NULL)
3770 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003771 next_search_hl(wp, shl, lnum, (colnr_T)v,
3772 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003773
3774 /* Need to get the line again, a multi-line regexp may have made it
3775 * invalid. */
3776 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3777 ptr = line + v;
3778
3779 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003781 if (shl->lnum == lnum)
3782 shl->startcol = shl->rm.startpos[0].col;
3783 else
3784 shl->startcol = 0;
3785 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3786 - shl->rm.startpos[0].lnum)
3787 shl->endcol = shl->rm.endpos[0].col;
3788 else
3789 shl->endcol = MAXCOL;
3790 /* Highlight one character for an empty match. */
3791 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003793 if (has_mbyte && line[shl->endcol] != NUL)
3794 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3795 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003796 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003798 if ((long)shl->startcol < v) /* match at leftcol */
3799 {
3800 shl->attr_cur = shl->attr;
3801 search_attr = shl->attr;
3802 }
3803 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003805 if (shl != &search_hl && cur != NULL)
3806 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808#endif
3809
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003810#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003811 // Cursor line highlighting for 'cursorline' in the current window.
3812 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003813 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003814 // Do not show the cursor line when Visual mode is active, because it's
3815 // not clear what is selected then. Do update w_last_cursorline.
3816 if (!(wp == curwin && VIsual_active))
3817 {
3818 line_attr = HL_ATTR(HLF_CUL);
3819 area_highlighting = TRUE;
3820 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003821 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003822 }
3823#endif
3824
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003825#ifdef FEAT_TEXT_PROP
3826 {
3827 char_u *prop_start;
3828
3829 text_prop_count = get_text_props(wp->w_buffer, lnum,
3830 &prop_start, FALSE);
3831 if (text_prop_count > 0)
3832 {
3833 // Make a copy of the properties, so that they are properly
3834 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003835 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003836 if (text_props != NULL)
3837 mch_memmove(text_props, prop_start,
3838 text_prop_count * sizeof(textprop_T));
3839
3840 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003841 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003842 area_highlighting = TRUE;
3843 extra_check = TRUE;
3844 }
3845 }
3846#endif
3847
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003848 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851#ifdef FEAT_RIGHTLEFT
3852 if (wp->w_p_rl)
3853 {
3854 /* Rightleft window: process the text in the normal direction, but put
3855 * it in current_ScreenLine[] from right to left. Start at the
3856 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003857 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003859 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861#endif
3862
3863 /*
3864 * Repeat for the whole displayed line.
3865 */
3866 for (;;)
3867 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003868#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003869 int has_match_conc = 0; // match wants to conceal
3870 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 /* Skip this quickly when working on the text. */
3873 if (draw_state != WL_LINE)
3874 {
3875#ifdef FEAT_CMDWIN
3876 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3877 {
3878 draw_state = WL_CMDLINE;
3879 if (cmdwin_type != 0 && wp == curwin)
3880 {
3881 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003883 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003884 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003885 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 }
3887 }
3888#endif
3889
3890#ifdef FEAT_FOLDING
3891 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3892 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003893 int fdc = compute_foldcolumn(wp, 0);
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003896 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003898 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003899 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003900 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003901 p_extra_free = alloc(12 + 1);
3902
3903 if (p_extra_free != NULL)
3904 {
3905 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3906 n_extra = fdc;
3907 p_extra_free[n_extra] = NUL;
3908 p_extra = p_extra_free;
3909 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003910 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003911 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 }
3915#endif
3916
3917#ifdef FEAT_SIGNS
3918 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3919 {
3920 draw_state = WL_SIGN;
3921 /* Show the sign column when there are any signs in this
3922 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003923 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003925 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003927 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928# endif
3929
3930 /* Draw two cells with the sign value or blank. */
3931 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003932 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003933 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 n_extra = 2;
3935
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003936 if (row == startrow
3937#ifdef FEAT_DIFF
3938 + filler_lines && filler_todo <= 0
3939#endif
3940 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
3942 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3943 SIGN_TEXT);
3944# ifdef FEAT_SIGN_ICONS
3945 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3946 SIGN_ICON);
3947 if (gui.in_use && icon_sign != 0)
3948 {
3949 /* Use the image in this position. */
3950 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003951 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952# ifdef FEAT_NETBEANS_INTG
3953 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003956 c_final = NUL;
3957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958# endif
3959 char_attr = icon_sign;
3960 }
3961 else
3962# endif
3963 if (text_sign != 0)
3964 {
3965 p_extra = sign_get_text(text_sign);
3966 if (p_extra != NULL)
3967 {
3968 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003969 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003970 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 char_attr = sign_get_attr(text_sign, FALSE);
3973 }
3974 }
3975 }
3976 }
3977#endif
3978
3979 if (draw_state == WL_NR - 1 && n_extra == 0)
3980 {
3981 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003982 /* Display the absolute or relative line number. After the
3983 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3984 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 && (row == startrow
3986#ifdef FEAT_DIFF
3987 + filler_lines
3988#endif
3989 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3990 {
3991 /* Draw the line number (empty space after wrapping). */
3992 if (row == startrow
3993#ifdef FEAT_DIFF
3994 + filler_lines
3995#endif
3996 )
3997 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003998 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003999 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004000
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004001 if (wp->w_p_nu && !wp->w_p_rnu)
4002 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004003 num = (long)lnum;
4004 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004005 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004006 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004007 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004008 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004009 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004010 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004011 num = lnum;
4012 fmt = "%-*ld ";
4013 }
4014 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004015
Bram Moolenaar700e7342013-01-30 12:31:36 +01004016 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004017 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (wp->w_skipcol > 0)
4019 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4020 *p_extra = '-';
4021#ifdef FEAT_RIGHTLEFT
4022 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004023 {
4024 char_u *p1, *p2;
4025 int t;
4026
4027 // like rl_mirror(), but keep the space at the end
4028 p2 = skiptowhite(extra) - 1;
4029 for (p1 = extra; p1 < p2; ++p1, --p2)
4030 {
4031 t = *p1;
4032 *p1 = *p2;
4033 *p2 = t;
4034 }
4035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036#endif
4037 p_extra = extra;
4038 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004039 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004044 c_final = NUL;
4045 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004046 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004047 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004048#ifdef FEAT_SYN_HL
4049 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004050 * the current line differently.
4051 * TODO: Can we use CursorLine instead of CursorLineNr
4052 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004053 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004054 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004055 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 }
4059
Bram Moolenaar597a4222014-06-25 14:39:50 +02004060#ifdef FEAT_LINEBREAK
4061 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4062 && n_extra == 0 && *p_sbr != NUL)
4063 /* draw indent after showbreak value */
4064 draw_state = WL_BRI;
4065 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4066 /* After the showbreak, draw the breakindent */
4067 draw_state = WL_BRI - 1;
4068
4069 /* draw 'breakindent': indent wrapped text accordingly */
4070 if (draw_state == WL_BRI - 1 && n_extra == 0)
4071 {
4072 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004073 /* if need_showbreak is set, breakindent also applies */
4074 if (wp->w_p_bri && n_extra == 0
4075 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004076# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004077 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004078# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004079 )
4080 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004081 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004082# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004083 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004084 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004085 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004086# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004087 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4088 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004089 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004090# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004091 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004092# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004093 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004094 c_extra = ' ';
4095 n_extra = get_breakindent_win(wp,
4096 ml_get_buf(wp->w_buffer, lnum, FALSE));
4097 /* Correct end of highlighted area for 'breakindent',
4098 * required when 'linebreak' is also set. */
4099 if (tocol == vcol)
4100 tocol += n_extra;
4101 }
4102 }
4103#endif
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4106 if (draw_state == WL_SBR - 1 && n_extra == 0)
4107 {
4108 draw_state = WL_SBR;
4109# ifdef FEAT_DIFF
4110 if (filler_todo > 0)
4111 {
4112 /* Draw "deleted" diff line(s). */
4113 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004116 c_final = NUL;
4117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004121 c_final = NUL;
4122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123# ifdef FEAT_RIGHTLEFT
4124 if (wp->w_p_rl)
4125 n_extra = col + 1;
4126 else
4127# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004128 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004129 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131# endif
4132# ifdef FEAT_LINEBREAK
4133 if (*p_sbr != NUL && need_showbreak)
4134 {
4135 /* Draw 'showbreak' at the start of each broken line. */
4136 p_extra = p_sbr;
4137 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004138 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004140 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004142 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 /* Correct end of highlighted area for 'showbreak',
4144 * required when 'linebreak' is also set. */
4145 if (tocol == vcol)
4146 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004147#ifdef FEAT_SYN_HL
4148 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004149 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004150 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004151 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154# endif
4155 }
4156#endif
4157
4158 if (draw_state == WL_LINE - 1 && n_extra == 0)
4159 {
4160 draw_state = WL_LINE;
4161 if (saved_n_extra)
4162 {
4163 /* Continue item from end of wrapped line. */
4164 n_extra = saved_n_extra;
4165 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004166 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 p_extra = saved_p_extra;
4168 char_attr = saved_char_attr;
4169 }
4170 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004171 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 }
4174
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004175 // When still displaying '$' of change command, stop at cursor.
4176 // When only displaying the (relative) line number and that's done,
4177 // stop here.
4178 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004179 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180#ifdef FEAT_DIFF
4181 && filler_todo <= 0
4182#endif
4183 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004184 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004186 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004187 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004188 /* Pretend we have finished updating the window. Except when
4189 * 'cursorcolumn' is set. */
4190#ifdef FEAT_SYN_HL
4191 if (wp->w_p_cuc)
4192 row = wp->w_cline_row + wp->w_cline_height;
4193 else
4194#endif
4195 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 break;
4197 }
4198
Bram Moolenaar637532b2019-01-03 21:44:40 +01004199 if (draw_state == WL_LINE && (area_highlighting
4200#ifdef FEAT_SPELL
4201 || has_spell
4202#endif
4203 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 /* handle Visual or match highlighting in this line */
4206 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4208 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004210 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004212 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 else if (area_attr != 0
4214 && (vcol == tocol
4215 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
4218#ifdef FEAT_SEARCH_EXTRA
4219 if (!n_extra)
4220 {
4221 /*
4222 * Check for start/end of search pattern match.
4223 * After end, check for start/end of next match.
4224 * When another match, have to check for start again.
4225 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004226 * Do this for 'search_hl' and the match list (ordered by
4227 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004229 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004230 cur = wp->w_match_head;
4231 shl_flag = FALSE;
4232 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004234 if (shl_flag == FALSE
4235 && ((cur != NULL
4236 && cur->priority > SEARCH_HL_PRIORITY)
4237 || cur == NULL))
4238 {
4239 shl = &search_hl;
4240 shl_flag = TRUE;
4241 }
4242 else
4243 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004244 if (cur != NULL)
4245 cur->pos.cur = 0;
4246 pos_inprogress = TRUE;
4247 while (shl->rm.regprog != NULL
4248 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004250 if (shl->startcol != MAXCOL
4251 && v >= (long)shl->startcol
4252 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004254 int tmp_col = v + MB_PTR2LEN(ptr);
4255
4256 if (shl->endcol < tmp_col)
4257 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004259#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004260 // Match with the "Conceal" group results in hiding
4261 // the match.
4262 if (cur != NULL
4263 && shl != &search_hl
4264 && syn_name2id((char_u *)"Conceal")
4265 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004266 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004267 has_match_conc =
4268 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004269 match_conc = cur->conceal_char;
4270 }
4271 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004272 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004275 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
4277 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004278 next_search_hl(wp, shl, lnum, (colnr_T)v,
4279 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004280 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004281 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 /* Need to get the line again, a multi-line regexp
4284 * may have made it invalid. */
4285 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4286 ptr = line + v;
4287
4288 if (shl->lnum == lnum)
4289 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004290 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004292 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004294 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004296 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 /* highlight empty match, try again after
4299 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004301 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004302 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004304 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306
4307 /* Loop to check if the match starts at the
4308 * current position */
4309 continue;
4310 }
4311 }
4312 break;
4313 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004314 if (shl != &search_hl && cur != NULL)
4315 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004317
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004318 /* Use attributes from match with highest priority among
4319 * 'search_hl' and the match list. */
4320 search_attr = search_hl.attr_cur;
4321 cur = wp->w_match_head;
4322 shl_flag = FALSE;
4323 while (cur != NULL || shl_flag == FALSE)
4324 {
4325 if (shl_flag == FALSE
4326 && ((cur != NULL
4327 && cur->priority > SEARCH_HL_PRIORITY)
4328 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004329 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004330 shl = &search_hl;
4331 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004332 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004333 else
4334 shl = &cur->hl;
4335 if (shl->attr_cur != 0)
4336 search_attr = shl->attr_cur;
4337 if (shl != &search_hl && cur != NULL)
4338 cur = cur->next;
4339 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004340 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004341 if (*ptr == NUL && (did_line_attr >= 1
4342 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004343 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345#endif
4346
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004348 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004350 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4351 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004353 if (diff_hlf == HLF_TXD && ptr - line > change_end
4354 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004356 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004357 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004358 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004361
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004362#ifdef FEAT_TEXT_PROP
4363 if (text_props != NULL)
4364 {
4365 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004366 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004367
4368 // Check if any active property ends.
4369 for (pi = 0; pi < text_props_active; ++pi)
4370 {
4371 int tpi = text_prop_idxs[pi];
4372
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004373 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004374 + text_props[tpi].tp_len)
4375 {
4376 if (pi + 1 < text_props_active)
4377 mch_memmove(text_prop_idxs + pi,
4378 text_prop_idxs + pi + 1,
4379 sizeof(int)
4380 * (text_props_active - (pi + 1)));
4381 --text_props_active;
4382 --pi;
4383 }
4384 }
4385
4386 // Add any text property that starts in this column.
4387 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004388 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004389 text_prop_idxs[text_props_active++] = text_prop_next++;
4390
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004391 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004392 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004393 if (text_props_active > 0)
4394 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004395 // Sort the properties on priority and/or starting last.
4396 // Then combine the attributes, highest priority last.
4397 current_text_props = text_props;
4398 current_buf = wp->w_buffer;
4399 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4400 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004401
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004402 for (pi = 0; pi < text_props_active; ++pi)
4403 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004404 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004405 proptype_T *pt = text_prop_type_by_id(
4406 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004407
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004408 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004409 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004410 int pt_attr = syn_id2attr(pt->pt_hl_id);
4411
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004412 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004413 text_prop_attr =
4414 hl_combine_attr(text_prop_attr, pt_attr);
4415 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004416 }
4417 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004418 }
4419 }
4420#endif
4421
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004422 /* Decide which of the highlight attributes to use. */
4423 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004424#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004425 if (area_attr != 0)
4426 char_attr = hl_combine_attr(line_attr, area_attr);
4427 else if (search_attr != 0)
4428 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004429# ifdef FEAT_TEXT_PROP
4430 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004431 {
4432 char_attr = hl_combine_attr(
4433 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4434 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004435# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004436 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004437 || vcol < fromcol || vcol_prev < fromcol_prev
4438 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004439 // Use line_attr when not in the Visual or 'incsearch' area
4440 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004441 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004442#else
4443 if (area_attr != 0)
4444 char_attr = area_attr;
4445 else if (search_attr != 0)
4446 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004447#endif
4448 else
4449 {
4450 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004451#ifdef FEAT_TEXT_PROP
4452 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004453 {
4454 if (text_prop_combine)
4455 char_attr = hl_combine_attr(
4456 syntax_attr, text_prop_attr);
4457 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004458 char_attr = hl_combine_attr(
4459 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004460 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004461 else
4462#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004463#ifdef FEAT_SYN_HL
4464 if (has_syntax)
4465 char_attr = syntax_attr;
4466 else
4467#endif
4468 char_attr = 0;
4469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004471 if (char_attr == 0)
4472 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /*
4475 * Get the next character to put on the screen.
4476 */
4477 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004478 * The "p_extra" points to the extra stuff that is inserted to
4479 * represent special characters (non-printable stuff) and other
4480 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004481 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004482 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4483 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4485 */
4486 if (n_extra > 0)
4487 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004488 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004490 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004492 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
4494 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004495 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004496 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 else
4499 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 else
4502 {
4503 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (has_mbyte)
4505 {
4506 mb_c = c;
4507 if (enc_utf8)
4508 {
4509 /* If the UTF-8 character is more than one byte:
4510 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004511 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 mb_utf8 = FALSE;
4513 if (mb_l > n_extra)
4514 mb_l = 1;
4515 else if (mb_l > 1)
4516 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004517 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004519 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 }
4522 else
4523 {
4524 /* if this is a DBCS character, put it in "mb_c" */
4525 mb_l = MB_BYTE2LEN(c);
4526 if (mb_l >= n_extra)
4527 mb_l = 1;
4528 else if (mb_l > 1)
4529 mb_c = (c << 8) + p_extra[1];
4530 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004531 if (mb_l == 0) /* at the NUL at end-of-line */
4532 mb_l = 1;
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 /* If a double-width char doesn't fit display a '>' in the
4535 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004536 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537# ifdef FEAT_RIGHTLEFT
4538 wp->w_p_rl ? (col <= 0) :
4539# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004540 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 && (*mb_char2cells)(mb_c) == 2)
4542 {
4543 c = '>';
4544 mb_c = c;
4545 mb_l = 1;
4546 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004547 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 /* put the pointer back to output the double-width
4549 * character at the start of the next line. */
4550 ++n_extra;
4551 --p_extra;
4552 }
4553 else
4554 {
4555 n_extra -= mb_l - 1;
4556 p_extra += mb_l - 1;
4557 }
4558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 ++p_extra;
4560 }
4561 --n_extra;
4562 }
4563 else
4564 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004565#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004566 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004567#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004568
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004569 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004570 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 /*
4572 * Get a character from the line itself.
4573 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004574 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004575#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004576 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (has_mbyte)
4579 {
4580 mb_c = c;
4581 if (enc_utf8)
4582 {
4583 /* If the UTF-8 character is more than one byte: Decode it
4584 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004585 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 mb_utf8 = FALSE;
4587 if (mb_l > 1)
4588 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004589 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 /* Overlong encoded ASCII or ASCII with composing char
4591 * is displayed normally, except a NUL. */
4592 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004593 {
4594 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004595#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004596 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004597#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004600
4601 /* At start of the line we can have a composing char.
4602 * Draw it as a space with a composing char. */
4603 if (utf_iscomposing(mb_c))
4604 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004605 int i;
4606
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004607 for (i = Screen_mco - 1; i > 0; --i)
4608 u8cc[i] = u8cc[i - 1];
4609 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004610 mb_c = ' ';
4611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613
4614 if ((mb_l == 1 && c >= 0x80)
4615 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004616 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
4618 /*
4619 * Illegal UTF-8 byte: display as <xx>.
4620 * Non-BMP character : display as ? or fullwidth ?.
4621 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004622 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004623# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004624 if (wp->w_p_rl) /* reverse */
4625 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 p_extra = extra;
4628 c = *p_extra;
4629 mb_c = mb_ptr2char_adv(&p_extra);
4630 mb_utf8 = (c >= 0x80);
4631 n_extra = (int)STRLEN(p_extra);
4632 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004633 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (area_attr == 0 && search_attr == 0)
4635 {
4636 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004637 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 saved_attr2 = char_attr; /* save current attr */
4639 }
4640 }
4641 else if (mb_l == 0) /* at the NUL at end-of-line */
4642 mb_l = 1;
4643#ifdef FEAT_ARABIC
4644 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4645 {
4646 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004647 int pc, pc1, nc;
4648 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 /* The idea of what is the previous and next
4651 * character depends on 'rightleft'. */
4652 if (wp->w_p_rl)
4653 {
4654 pc = prev_c;
4655 pc1 = prev_c1;
4656 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004657 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659 else
4660 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004661 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004663 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665 prev_c = mb_c;
4666
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004667 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 else
4670 prev_c = mb_c;
4671#endif
4672 }
4673 else /* enc_dbcs */
4674 {
4675 mb_l = MB_BYTE2LEN(c);
4676 if (mb_l == 0) /* at the NUL at end-of-line */
4677 mb_l = 1;
4678 else if (mb_l > 1)
4679 {
4680 /* We assume a second byte below 32 is illegal.
4681 * Hopefully this is OK for all double-byte encodings!
4682 */
4683 if (ptr[1] >= 32)
4684 mb_c = (c << 8) + ptr[1];
4685 else
4686 {
4687 if (ptr[1] == NUL)
4688 {
4689 /* head byte at end of line */
4690 mb_l = 1;
4691 transchar_nonprint(extra, c);
4692 }
4693 else
4694 {
4695 /* illegal tail byte */
4696 mb_l = 2;
4697 STRCPY(extra, "XX");
4698 }
4699 p_extra = extra;
4700 n_extra = (int)STRLEN(extra) - 1;
4701 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004702 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 c = *p_extra++;
4704 if (area_attr == 0 && search_attr == 0)
4705 {
4706 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004707 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 saved_attr2 = char_attr; /* save current attr */
4709 }
4710 mb_c = c;
4711 }
4712 }
4713 }
4714 /* If a double-width char doesn't fit display a '>' in the
4715 * last column; the character is displayed at the start of the
4716 * next line. */
4717 if ((
4718# ifdef FEAT_RIGHTLEFT
4719 wp->w_p_rl ? (col <= 0) :
4720# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004721 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 && (*mb_char2cells)(mb_c) == 2)
4723 {
4724 c = '>';
4725 mb_c = c;
4726 mb_utf8 = FALSE;
4727 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004728 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004729 // Put pointer back so that the character will be
4730 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004732#ifdef FEAT_CONCEAL
4733 did_decrement_ptr = TRUE;
4734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736 else if (*ptr != NUL)
4737 ptr += mb_l - 1;
4738
4739 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004740 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004741 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004742 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004745 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004746 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 c = ' ';
4748 if (area_attr == 0 && search_attr == 0)
4749 {
4750 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004751 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 saved_attr2 = char_attr; /* save current attr */
4753 }
4754 mb_c = c;
4755 mb_utf8 = FALSE;
4756 mb_l = 1;
4757 }
4758
4759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 ++ptr;
4761
4762 if (extra_check)
4763 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004764#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004765 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004766#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004767
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004768#ifdef FEAT_TERMINAL
4769 if (get_term_attr)
4770 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004771 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004772
4773 if (!attr_pri)
4774 char_attr = syntax_attr;
4775 else
4776 char_attr = hl_combine_attr(syntax_attr, char_attr);
4777 }
4778#endif
4779
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004780#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004781 // Get syntax attribute, unless still at the start of the line
4782 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004783 v = (long)(ptr - line);
4784 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 /* Get the syntax attribute for the character. If there
4787 * is an error, disable syntax highlighting. */
4788 save_did_emsg = did_emsg;
4789 did_emsg = FALSE;
4790
Bram Moolenaar217ad922005-03-20 22:37:15 +00004791 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004792# ifdef FEAT_SPELL
4793 has_spell ? &can_spell :
4794# endif
4795 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004798 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004799 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004800 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004801 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 else
4804 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004805
4806 // combine syntax attribute with 'wincolor'
4807 if (win_attr != 0)
4808 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4809
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004810#ifdef SYN_TIME_LIMIT
4811 if (wp->w_s->b_syn_slow)
4812 has_syntax = FALSE;
4813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
4815 /* Need to get the line again, a multi-line regexp may
4816 * have made it invalid. */
4817 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4818 ptr = line + v;
4819
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004820# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004821 // Text properties overrule syntax highlighting or combine.
4822 if (text_prop_attr == 0 || text_prop_combine)
4823# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004824 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004825 int comb_attr = syntax_attr;
4826# ifdef FEAT_TEXT_PROP
4827 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4828# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004829 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004830 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004831 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004832 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004833 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004834# ifdef FEAT_CONCEAL
4835 /* no concealing past the end of the line, it interferes
4836 * with line highlighting */
4837 if (c == NUL)
4838 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004839 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004840 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004843#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004844
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004845#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004846 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004847 * Only do this when there is no syntax highlighting, the
4848 * @Spell cluster is not used or the current syntax item
4849 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004850 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004851 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004852 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004853 if (c != 0 && (
4854# ifdef FEAT_SYN_HL
4855 !has_syntax ||
4856# endif
4857 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004858 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004859 char_u *prev_ptr, *p;
4860 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004861 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004862 if (has_mbyte)
4863 {
4864 prev_ptr = ptr - mb_l;
4865 v -= mb_l - 1;
4866 }
4867 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004868 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004869
4870 /* Use nextline[] if possible, it has the start of the
4871 * next line concatenated. */
4872 if ((prev_ptr - line) - nextlinecol >= 0)
4873 p = nextline + (prev_ptr - line) - nextlinecol;
4874 else
4875 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004876 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004877 len = spell_check(wp, p, &spell_hlf, &cap_col,
4878 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004879 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004880
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004881 /* In Insert mode only highlight a word that
4882 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004883 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004884 && (State & INSERT) != 0
4885 && wp->w_cursor.lnum == lnum
4886 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004887 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004888 && wp->w_cursor.col < (colnr_T)word_end)
4889 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004890 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004891 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004892 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004893
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004894 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004895 && (p - nextline) + len > nextline_idx)
4896 {
4897 /* Remember that the good word continues at the
4898 * start of the next line. */
4899 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004900 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004901 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004902
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004903 /* Turn index into actual attributes. */
4904 if (spell_hlf != HLF_COUNT)
4905 spell_attr = highlight_attr[spell_hlf];
4906
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004907 if (cap_col > 0)
4908 {
4909 if (p != prev_ptr
4910 && (p - nextline) + cap_col >= nextline_idx)
4911 {
4912 /* Remember that the word in the next line
4913 * must start with a capital. */
4914 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004915 cap_col = (int)((p - nextline) + cap_col
4916 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004917 }
4918 else
4919 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004920 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004921 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004922 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004923 }
4924 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004925 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004926 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004927 char_attr = hl_combine_attr(char_attr, spell_attr);
4928 else
4929 char_attr = hl_combine_attr(spell_attr, char_attr);
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931#endif
4932#ifdef FEAT_LINEBREAK
4933 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004934 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004936 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004937 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004939 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004940 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004941
Bram Moolenaar597a4222014-06-25 14:39:50 +02004942 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004944 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004945 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004946# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004947 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004948 wp->w_buffer->b_p_vts_array) - 1;
4949# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004950 n_extra = (int)wp->w_buffer->b_p_ts
4951 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004952# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004953
Bram Moolenaar4df70292015-03-21 14:20:16 +01004954 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004955 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004956 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004957 {
4958#ifdef FEAT_CONCEAL
4959 if (c == TAB)
4960 /* See "Tab alignment" below. */
4961 FIX_FOR_BOGUSCOLS;
4962#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004963 if (!wp->w_p_list)
4964 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967#endif
4968
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004969 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4970 // But not when the character is followed by a composing
4971 // character (use mb_l to check that).
4972 if (wp->w_p_list
4973 && ((((c == 160 && mb_l == 1)
4974 || (mb_utf8
4975 && ((mb_c == 160 && mb_l == 2)
4976 || (mb_c == 0x202f && mb_l == 3))))
4977 && lcs_nbsp)
4978 || (c == ' '
4979 && mb_l == 1
4980 && lcs_space
4981 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004982 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004983 c = (c == ' ') ? lcs_space : lcs_nbsp;
4984 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004985 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004986 n_attr = 1;
4987 extra_attr = HL_ATTR(HLF_8);
4988 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004989 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004990 mb_c = c;
4991 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004992 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004993 mb_utf8 = TRUE;
4994 u8cc[0] = 0;
4995 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004996 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004997 else
4998 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004999 }
5000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5002 {
5003 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005004 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005007 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 saved_attr2 = char_attr; /* save current attr */
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005011 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005014 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005015 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 else
5018 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 }
5021
5022 /*
5023 * Handling of non-printable characters.
5024 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005025 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
5027 /*
5028 * when getting a character from the file, we may have to
5029 * turn it into something else on the way to putting it
5030 * into "ScreenLines".
5031 */
5032 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5033 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005034 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005035 long vcol_adjusted = vcol; /* removed showbreak length */
5036#ifdef FEAT_LINEBREAK
5037 /* only adjust the tab_len, when at the first column
5038 * after the showbreak value was drawn */
5039 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5040 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005043#ifdef FEAT_VARTABS
5044 tab_len = tabstop_padding(vcol_adjusted,
5045 wp->w_buffer->b_p_ts,
5046 wp->w_buffer->b_p_vts_array) - 1;
5047#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005048 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005049 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5050#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005051
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005052#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005053 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005054#endif
5055 /* tab amount depends on current column */
5056 n_extra = tab_len;
5057#ifdef FEAT_LINEBREAK
5058 else
5059 {
5060 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005061 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005062 int i;
5063 int saved_nextra = n_extra;
5064
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005065#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005066 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005067 /* there are characters to conceal */
5068 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005069 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5070 */
5071 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5072 && n_extra > tab_len)
5073 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005074#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005075
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076 /* if n_extra > 0, it gives the number of chars, to
5077 * use for a tab, else we need to calculate the width
5078 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005079 len = (tab_len * mb_char2len(lcs_tab2));
5080 if (n_extra > 0)
5081 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005082 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005083 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005084 vim_memset(p, ' ', len);
5085 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005086 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005087 p_extra_free = p;
5088 for (i = 0; i < tab_len; i++)
5089 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005090 if (*p == NUL)
5091 {
5092 tab_len = i;
5093 break;
5094 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005095 mb_char2bytes(lcs_tab2, p);
5096 p += mb_char2len(lcs_tab2);
5097 n_extra += mb_char2len(lcs_tab2)
5098 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005099 }
5100 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005101#ifdef FEAT_CONCEAL
5102 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5103 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005104 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005105 n_extra -= vcol_off;
5106#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005107 }
5108#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005109#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005110 {
5111 int vc_saved = vcol_off;
5112
5113 /* Tab alignment should be identical regardless of
5114 * 'conceallevel' value. So tab compensates of all
5115 * previous concealed characters, and thus resets
5116 * vcol_off and boguscols accumulated so far in the
5117 * line. Note that the tab can be longer than
5118 * 'tabstop' when there are concealed characters. */
5119 FIX_FOR_BOGUSCOLS;
5120
5121 /* Make sure, the highlighting for the tab char will be
5122 * correctly set further below (effectively reverts the
5123 * FIX_FOR_BOGSUCOLS macro */
5124 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005125 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005126 tab_len += vc_saved;
5127 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (wp->w_p_list)
5131 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005132 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005133#ifdef FEAT_LINEBREAK
5134 if (wp->w_p_lbr)
5135 c_extra = NUL; /* using p_extra from above */
5136 else
5137#endif
5138 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005139 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005140 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005141 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005144 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005147 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005148 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 else
5152 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005153 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 c_extra = ' ';
5155 c = ' ';
5156 }
5157 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005158 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005159 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005160 || ((fromcol >= 0 || fromcol_prev >= 0)
5161 && tocol > vcol
5162 && VIsual_mode != Ctrl_V
5163 && (
5164# ifdef FEAT_RIGHTLEFT
5165 wp->w_p_rl ? (col >= 0) :
5166# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005167 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005168 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005169 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005170 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005171 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005173 /* Display a '$' after the line or highlight an extra
5174 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5176 /* For a diff line the highlighting continues after the
5177 * "$". */
5178 if (
5179# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005180 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181# ifdef LINE_ATTR
5182 &&
5183# endif
5184# endif
5185# ifdef LINE_ATTR
5186 line_attr == 0
5187# endif
5188 )
5189#endif
5190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 /* In virtualedit, visual selections may extend
5192 * beyond end of line. */
5193 if (area_highlighting && virtual_active()
5194 && tocol != MAXCOL && vcol < tocol)
5195 n_extra = 0;
5196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
5198 p_extra = at_end_str;
5199 n_extra = 1;
5200 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005201 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 }
5203 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005204 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005205 c = lcs_eol;
5206 else
5207 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 lcs_eol_one = -1;
5209 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005210 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005212 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 n_attr = 1;
5214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005216 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005219 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005220 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222 else
5223 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225 else if (c != NUL)
5226 {
5227 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005228 if (n_extra == 0)
5229 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#ifdef FEAT_RIGHTLEFT
5231 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5232 rl_mirror(p_extra); /* reverse "<12>" */
5233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005235 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005236#ifdef FEAT_LINEBREAK
5237 if (wp->w_p_lbr)
5238 {
5239 char_u *p;
5240
5241 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005242 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005243 vim_memset(p, ' ', n_extra);
5244 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5245 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005246 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005247 p_extra_free = p_extra = p;
5248 }
5249 else
5250#endif
5251 {
5252 n_extra = byte2cells(c) - 1;
5253 c = *p_extra++;
5254 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005255 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
5257 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005258 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 saved_attr2 = char_attr; /* save current attr */
5260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 else if (VIsual_active
5264 && (VIsual_mode == Ctrl_V
5265 || VIsual_mode == 'v')
5266 && virtual_active()
5267 && tocol != MAXCOL
5268 && vcol < tocol
5269 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005270#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005272#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005273 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 c = ' ';
5276 --ptr; /* put it back at the NUL */
5277 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005278#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 else if ((
5280# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005281 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005283# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005284 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 ) && (
5288# ifdef FEAT_RIGHTLEFT
5289 wp->w_p_rl ? (col >= 0) :
5290# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005291 (col
5292# ifdef FEAT_CONCEAL
5293 - boguscols
5294# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005295 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
5297 /* Highlight until the right side of the window */
5298 c = ' ';
5299 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005300
5301 /* Remember we do the char for line highlighting. */
5302 ++did_line_attr;
5303
5304 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005305 if (line_attr != 0 && char_attr == search_attr
5306 && (did_line_attr > 1
5307 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005308 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309# ifdef FEAT_DIFF
5310 if (diff_hlf == HLF_TXD)
5311 {
5312 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005313 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005314 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005315 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005316 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5317 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005318 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005322# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005323 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005324 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005325 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005326 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5327 char_attr = hl_combine_attr(char_attr,
5328 HL_ATTR(HLF_CUL));
5329 }
5330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332#endif
5333 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334
5335#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005336 if ( wp->w_p_cole > 0
5337 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005338 conceal_cursor_line(wp))
5339 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005340 && !(lnum_in_visual_area
5341 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005342 {
5343 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005344 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005345 && (syn_get_sub_char() != NUL || match_conc
5346 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005347 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005348 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005349 /* First time at this concealed item: display one
5350 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005351 if (match_conc)
5352 c = match_conc;
5353 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005354 c = syn_get_sub_char();
5355 else if (lcs_conceal != NUL)
5356 c = lcs_conceal;
5357 else
5358 c = ' ';
5359
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005360 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005361
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005362 if (n_extra > 0)
5363 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005364 vcol += n_extra;
5365 if (wp->w_p_wrap && n_extra > 0)
5366 {
5367# ifdef FEAT_RIGHTLEFT
5368 if (wp->w_p_rl)
5369 {
5370 col -= n_extra;
5371 boguscols -= n_extra;
5372 }
5373 else
5374# endif
5375 {
5376 boguscols += n_extra;
5377 col += n_extra;
5378 }
5379 }
5380 n_extra = 0;
5381 n_attr = 0;
5382 }
5383 else if (n_skip == 0)
5384 {
5385 is_concealing = TRUE;
5386 n_skip = 1;
5387 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005388 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005389 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005390 {
5391 mb_utf8 = TRUE;
5392 u8cc[0] = 0;
5393 c = 0xc0;
5394 }
5395 else
5396 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005397 }
5398 else
5399 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005400 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005401 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005402 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005403
5404 if (n_skip > 0 && did_decrement_ptr)
5405 // not showing the '>', put pointer back to avoid getting stuck
5406 ++ptr;
5407
5408#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005411#ifdef FEAT_CONCEAL
5412 /* In the cursor line and we may be concealing characters: correct
5413 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005414 if (!did_wcol && draw_state == WL_LINE
5415 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005416 && conceal_cursor_line(wp)
5417 && (int)wp->w_virtcol <= vcol + n_skip)
5418 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005419# ifdef FEAT_RIGHTLEFT
5420 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005421 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005422 else
5423# endif
5424 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005425 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005426 did_wcol = TRUE;
5427 }
5428#endif
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 /* Don't override visual selection highlighting. */
5431 if (n_attr > 0
5432 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005433 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 char_attr = extra_attr;
5435
Bram Moolenaar81695252004-12-29 20:58:21 +00005436#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 /* XIM don't send preedit_start and preedit_end, but they send
5438 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5439 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005440 if (p_imst == IM_ON_THE_SPOT
5441 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005442 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 && (State & INSERT)
5444 && !p_imdisable
5445 && im_is_preediting()
5446 && draw_state == WL_LINE)
5447 {
5448 colnr_T tcol;
5449
5450 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005451 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 else
5453 tcol = preedit_end_col;
5454 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5455 {
5456 if (feedback_old_attr < 0)
5457 {
5458 feedback_col = 0;
5459 feedback_old_attr = char_attr;
5460 }
5461 char_attr = im_get_feedback_attr(feedback_col);
5462 if (char_attr < 0)
5463 char_attr = feedback_old_attr;
5464 feedback_col++;
5465 }
5466 else if (feedback_old_attr >= 0)
5467 {
5468 char_attr = feedback_old_attr;
5469 feedback_old_attr = -1;
5470 feedback_col = 0;
5471 }
5472 }
5473#endif
5474 /*
5475 * Handle the case where we are in column 0 but not on the first
5476 * character of the line and the user wants us to show us a
5477 * special character (via 'listchars' option "precedes:<char>".
5478 */
5479 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005480 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5482#ifdef FEAT_DIFF
5483 && filler_todo <= 0
5484#endif
5485 && draw_state > WL_NR
5486 && c != NUL)
5487 {
5488 c = lcs_prec;
5489 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005490 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5491 {
5492 /* Double-width character being overwritten by the "precedes"
5493 * character, need to fill up half the character. */
5494 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005495 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005496 n_extra = 1;
5497 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005498 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005501 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005504 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005505 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
5507 else
5508 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005509 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005512 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 n_attr3 = 1;
5514 }
5515 }
5516
5517 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005518 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005520 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005521#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005522 || did_line_attr == 1
5523#endif
5524 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005526#ifdef FEAT_SEARCH_EXTRA
5527 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005528
5529 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005530 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005531 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005532#endif
5533
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005534 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 * highlight match at end of line. If it's beyond the last
5536 * char on the screen, just overwrite that one (tricky!) Not
5537 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005538#ifdef FEAT_SEARCH_EXTRA
5539 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005540 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005541 prevcol_hl_flag = TRUE;
5542 else
5543 {
5544 cur = wp->w_match_head;
5545 while (cur != NULL)
5546 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005547 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005548 {
5549 prevcol_hl_flag = TRUE;
5550 break;
5551 }
5552 cur = cur->next;
5553 }
5554 }
5555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005557 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005558 && (VIsual_mode != Ctrl_V
5559 || lnum == VIsual.lnum
5560 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005561 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562#ifdef FEAT_SEARCH_EXTRA
5563 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005564 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005565# ifdef FEAT_SYN_HL
5566 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5567 && !(wp == curwin && VIsual_active))
5568# endif
5569# ifdef FEAT_DIFF
5570 && diff_hlf == (hlf_T)0
5571# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005572# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005573 && did_line_attr <= 1
5574# endif
5575 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576#endif
5577 ))
5578 {
5579 int n = 0;
5580
5581#ifdef FEAT_RIGHTLEFT
5582 if (wp->w_p_rl)
5583 {
5584 if (col < 0)
5585 n = 1;
5586 }
5587 else
5588#endif
5589 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005590 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 n = -1;
5592 }
5593 if (n != 0)
5594 {
5595 /* At the window boundary, highlight the last character
5596 * instead (better than nothing). */
5597 off += n;
5598 col += n;
5599 }
5600 else
5601 {
5602 /* Add a blank character to highlight. */
5603 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (enc_utf8)
5605 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
5607#ifdef FEAT_SEARCH_EXTRA
5608 if (area_attr == 0)
5609 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005610 /* Use attributes from match with highest priority among
5611 * 'search_hl' and the match list. */
5612 char_attr = search_hl.attr;
5613 cur = wp->w_match_head;
5614 shl_flag = FALSE;
5615 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005616 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005617 if (shl_flag == FALSE
5618 && ((cur != NULL
5619 && cur->priority > SEARCH_HL_PRIORITY)
5620 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005621 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005622 shl = &search_hl;
5623 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005624 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005625 else
5626 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005627 if ((ptr - line) - 1 == (long)shl->startcol
5628 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005629 char_attr = shl->attr;
5630 if (shl != &search_hl && cur != NULL)
5631 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
5634#endif
5635 ScreenAttrs[off] = char_attr;
5636#ifdef FEAT_RIGHTLEFT
5637 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005640 --off;
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 else
5643#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005646 ++off;
5647 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005648 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005649#ifdef FEAT_SYN_HL
5650 eol_hl_off = 1;
5651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaar91170f82006-05-05 21:15:17 +00005655 /*
5656 * At end of the text line.
5657 */
5658 if (c == NUL)
5659 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005660#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005661 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005662 if (wp->w_p_wrap)
5663 v = wp->w_skipcol;
5664 else
5665 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005666
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005667 /* check if line ends before left margin */
5668 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005669 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005670#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005671 // Get rid of the boguscols now, we want to draw until the right
5672 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005673 col -= boguscols;
5674 boguscols = 0;
5675#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005676
5677 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005678 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005679
5680 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005681 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5682 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005683 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005684 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005685 || draw_color_col
5686 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005687# ifdef FEAT_RIGHTLEFT
5688 && !wp->w_p_rl
5689# endif
5690 )
5691 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005692 int rightmost_vcol = 0;
5693 int i;
5694
5695 if (wp->w_p_cuc)
5696 rightmost_vcol = wp->w_virtcol;
5697 if (draw_color_col)
5698 /* determine rightmost colorcolumn to possibly draw */
5699 for (i = 0; color_cols[i] >= 0; ++i)
5700 if (rightmost_vcol < color_cols[i])
5701 rightmost_vcol = color_cols[i];
5702
Bram Moolenaar02631462017-09-22 15:20:32 +02005703 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005704 {
5705 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005706 if (enc_utf8)
5707 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005708 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005709 if (draw_color_col)
5710 draw_color_col = advance_color_col(VCOL_HLC,
5711 &color_cols);
5712
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005713 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005714 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005715 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005716 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005717 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005718 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005719
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005720 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005721 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005722
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005723 ++vcol;
5724 }
5725 }
5726#endif
5727
Bram Moolenaar53f81742017-09-22 14:35:51 +02005728 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005729 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 row++;
5731
5732 /*
5733 * Update w_cline_height and w_cline_folded if the cursor line was
5734 * updated (saves a call to plines() later).
5735 */
5736 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5737 {
5738 curwin->w_cline_row = startrow;
5739 curwin->w_cline_height = row - startrow;
5740#ifdef FEAT_FOLDING
5741 curwin->w_cline_folded = FALSE;
5742#endif
5743 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5744 }
5745
5746 break;
5747 }
5748
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005749 // Show "extends" character from 'listchars' if beyond the line end and
5750 // 'list' is set.
5751 if (lcs_ext != NUL
5752 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 && !wp->w_p_wrap
5754#ifdef FEAT_DIFF
5755 && filler_todo <= 0
5756#endif
5757 && (
5758#ifdef FEAT_RIGHTLEFT
5759 wp->w_p_rl ? col == 0 :
5760#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005761 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005763 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5765 {
5766 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005767 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005769 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005772 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005773 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 }
5775 else
5776 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005779#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005780 /* advance to the next 'colorcolumn' */
5781 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005782 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005783
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005784 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005785 * highlight the cursor position itself.
5786 * Also highlight the 'colorcolumn' if it is different than
5787 * 'cursorcolumn' */
5788 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005789 if (draw_state == WL_LINE && !lnum_in_visual_area
5790 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005791 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005792 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005793 && lnum != wp->w_cursor.lnum)
5794 {
5795 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005796 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005797 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005798 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005799 {
5800 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005801 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005802 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005803 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005804#endif
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 /*
5807 * Store character to be displayed.
5808 * Skip characters that are left of the screen for 'nowrap'.
5809 */
5810 vcol_prev = vcol;
5811 if (draw_state < WL_LINE || n_skip <= 0)
5812 {
5813 /*
5814 * Store the character.
5815 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005816#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5818 {
5819 /* A double-wide character is: put first halve in left cell. */
5820 --off;
5821 --col;
5822 }
5823#endif
5824 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005826 {
5827 if ((mb_c & 0xff00) == 0x8e00)
5828 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 else if (enc_utf8)
5832 {
5833 if (mb_utf8)
5834 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005835 int i;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005838 if ((c & 0xff) == 0)
5839 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005840 for (i = 0; i < Screen_mco; ++i)
5841 {
5842 ScreenLinesC[i][off] = u8cc[i];
5843 if (u8cc[i] == 0)
5844 break;
5845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 }
5847 else
5848 ScreenLinesUC[off] = 0;
5849 }
5850 if (multi_attr)
5851 {
5852 ScreenAttrs[off] = multi_attr;
5853 multi_attr = 0;
5854 }
5855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 ScreenAttrs[off] = char_attr;
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5859 {
5860 /* Need to fill two screen columns. */
5861 ++off;
5862 ++col;
5863 if (enc_utf8)
5864 /* UTF-8: Put a 0 in the second screen char. */
5865 ScreenLines[off] = 0;
5866 else
5867 /* DBCS: Put second byte in the second screen char. */
5868 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005869 if (draw_state > WL_NR
5870#ifdef FEAT_DIFF
5871 && filler_todo <= 0
5872#endif
5873 )
5874 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 /* When "tocol" is halfway a character, set it to the end of
5876 * the character, otherwise highlighting won't stop. */
5877 if (tocol == vcol)
5878 ++tocol;
5879#ifdef FEAT_RIGHTLEFT
5880 if (wp->w_p_rl)
5881 {
5882 /* now it's time to backup one cell */
5883 --off;
5884 --col;
5885 }
5886#endif
5887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#ifdef FEAT_RIGHTLEFT
5889 if (wp->w_p_rl)
5890 {
5891 --off;
5892 --col;
5893 }
5894 else
5895#endif
5896 {
5897 ++off;
5898 ++col;
5899 }
5900 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005901#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005902 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005903 {
5904 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005905 ++vcol_off;
5906 if (n_extra > 0)
5907 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005908 if (wp->w_p_wrap)
5909 {
5910 /*
5911 * Special voodoo required if 'wrap' is on.
5912 *
5913 * Advance the column indicator to force the line
5914 * drawing to wrap early. This will make the line
5915 * take up the same screen space when parts are concealed,
5916 * so that cursor line computations aren't messed up.
5917 *
5918 * To avoid the fictitious advance of 'col' causing
5919 * trailing junk to be written out of the screen line
5920 * we are building, 'boguscols' keeps track of the number
5921 * of bad columns we have advanced.
5922 */
5923 if (n_extra > 0)
5924 {
5925 vcol += n_extra;
5926# ifdef FEAT_RIGHTLEFT
5927 if (wp->w_p_rl)
5928 {
5929 col -= n_extra;
5930 boguscols -= n_extra;
5931 }
5932 else
5933# endif
5934 {
5935 col += n_extra;
5936 boguscols += n_extra;
5937 }
5938 n_extra = 0;
5939 n_attr = 0;
5940 }
5941
5942
Bram Moolenaar860cae12010-06-05 23:22:07 +02005943 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5944 {
5945 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005946# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005947 if (wp->w_p_rl)
5948 {
5949 --boguscols;
5950 --col;
5951 }
5952 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005953# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005954 {
5955 ++boguscols;
5956 ++col;
5957 }
5958 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005959
5960# ifdef FEAT_RIGHTLEFT
5961 if (wp->w_p_rl)
5962 {
5963 --boguscols;
5964 --col;
5965 }
5966 else
5967# endif
5968 {
5969 ++boguscols;
5970 ++col;
5971 }
5972 }
5973 else
5974 {
5975 if (n_extra > 0)
5976 {
5977 vcol += n_extra;
5978 n_extra = 0;
5979 n_attr = 0;
5980 }
5981 }
5982
5983 }
5984#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 else
5986 --n_skip;
5987
Bram Moolenaar64486672010-05-16 15:46:46 +02005988 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5989 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005990 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#ifdef FEAT_DIFF
5992 && filler_todo <= 0
5993#endif
5994 )
5995 ++vcol;
5996
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005997#ifdef FEAT_SYN_HL
5998 if (vcol_save_attr >= 0)
5999 char_attr = vcol_save_attr;
6000#endif
6001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 /* restore attributes after "predeces" in 'listchars' */
6003 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6004 char_attr = saved_attr3;
6005
6006 /* restore attributes after last 'listchars' or 'number' char */
6007 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6008 char_attr = saved_attr2;
6009
6010 /*
6011 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006012 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 */
6014 if ((
6015#ifdef FEAT_RIGHTLEFT
6016 wp->w_p_rl ? (col < 0) :
6017#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006018 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 && (*ptr != NUL
6020#ifdef FEAT_DIFF
6021 || filler_todo > 0
6022#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006023 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6025 )
6026 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006027#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006028 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006029 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006030 boguscols = 0;
6031#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006032 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006033 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 ++row;
6036 ++screen_row;
6037
6038 /* When not wrapping and finished diff lines, or when displayed
6039 * '$' and highlighting until last column, break here. */
6040 if ((!wp->w_p_wrap
6041#ifdef FEAT_DIFF
6042 && filler_todo <= 0
6043#endif
6044 ) || lcs_eol_one == -1)
6045 break;
6046
6047 /* When the window is too narrow draw all "@" lines. */
6048 if (draw_state != WL_LINE
6049#ifdef FEAT_DIFF
6050 && filler_todo <= 0
6051#endif
6052 )
6053 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006054 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 row = endrow;
6057 }
6058
6059 /* When line got too long for screen break here. */
6060 if (row == endrow)
6061 {
6062 ++row;
6063 break;
6064 }
6065
6066 if (screen_cur_row == screen_row - 1
6067#ifdef FEAT_DIFF
6068 && filler_todo <= 0
6069#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006070 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
6072 /* Remember that the line wraps, used for modeless copy. */
6073 LineWraps[screen_row - 1] = TRUE;
6074
6075 /*
6076 * Special trick to make copy/paste of wrapped lines work with
6077 * xterm/screen: write an extra character beyond the end of
6078 * the line. This will work with all terminal types
6079 * (regardless of the xn,am settings).
6080 * Only do this on a fast tty.
6081 * Only do this if the cursor is on the current line
6082 * (something has been written in it).
6083 * Don't do this for the GUI.
6084 * Don't do this for double-width characters.
6085 * Don't do this for a window not at the right screen border.
6086 */
6087 if (p_tf
6088#ifdef FEAT_GUI
6089 && !gui.in_use
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006092 && ((*mb_off2cells)(LineOffset[screen_row],
6093 LineOffset[screen_row] + screen_Columns)
6094 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006096 + (int)Columns - 2,
6097 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006098 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {
6100 /* First make sure we are at the end of the screen line,
6101 * then output the same character again to let the
6102 * terminal know about the wrap. If the terminal doesn't
6103 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006104 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 screen_char(LineOffset[screen_row - 1]
6106 + (unsigned)Columns - 1,
6107 screen_row - 1, (int)(Columns - 1));
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 /* When there is a multi-byte character, just output a
6110 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006111 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6112 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 out_char(' ');
6114 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 out_char(ScreenLines[LineOffset[screen_row - 1]
6116 + (Columns - 1)]);
6117 /* force a redraw of the first char on the next line */
6118 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6119 screen_start(); /* don't know where cursor is now */
6120 }
6121 }
6122
6123 col = 0;
6124 off = (unsigned)(current_ScreenLine - ScreenLines);
6125#ifdef FEAT_RIGHTLEFT
6126 if (wp->w_p_rl)
6127 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006128 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 off += col;
6130 }
6131#endif
6132
6133 /* reset the drawing state for the start of a wrapped line */
6134 draw_state = WL_START;
6135 saved_n_extra = n_extra;
6136 saved_p_extra = p_extra;
6137 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006138 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 saved_char_attr = char_attr;
6140 n_extra = 0;
6141 lcs_prec_todo = lcs_prec;
6142#ifdef FEAT_LINEBREAK
6143# ifdef FEAT_DIFF
6144 if (filler_todo <= 0)
6145# endif
6146 need_showbreak = TRUE;
6147#endif
6148#ifdef FEAT_DIFF
6149 --filler_todo;
6150 /* When the filler lines are actually below the last line of the
6151 * file, don't draw the line itself, break here. */
6152 if (filler_todo == 0 && wp->w_botfill)
6153 break;
6154#endif
6155 }
6156
6157 } /* for every character in the line */
6158
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006159#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006160 /* After an empty line check first word for capital. */
6161 if (*skipwhite(line) == NUL)
6162 {
6163 capcol_lnum = lnum + 1;
6164 cap_col = 0;
6165 }
6166#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006167#ifdef FEAT_TEXT_PROP
6168 vim_free(text_props);
6169 vim_free(text_prop_idxs);
6170#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006171
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006172 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 return row;
6174}
6175
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006176/*
6177 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006178 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006179 */
6180 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006181comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006182{
6183 int i;
6184
6185 for (i = 0; i < Screen_mco; ++i)
6186 {
6187 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6188 return TRUE;
6189 if (ScreenLinesC[i][off_from] == 0)
6190 break;
6191 }
6192 return FALSE;
6193}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195/*
6196 * Check whether the given character needs redrawing:
6197 * - the (first byte of the) character is different
6198 * - the attributes are different
6199 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006200 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 */
6202 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006203char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
6205 if (cols > 0
6206 && ((ScreenLines[off_from] != ScreenLines[off_to]
6207 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 || (enc_dbcs != 0
6209 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6210 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6211 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6212 : (cols > 1 && ScreenLines[off_from + 1]
6213 != ScreenLines[off_to + 1])))
6214 || (enc_utf8
6215 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6216 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006217 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006218 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6219 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006220 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 return TRUE;
6222 return FALSE;
6223}
6224
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006225#if defined(FEAT_TERMINAL) || defined(PROTO)
6226/*
6227 * Return the index in ScreenLines[] for the current screen line.
6228 */
6229 int
6230screen_get_current_line_off()
6231{
6232 return (int)(current_ScreenLine - ScreenLines);
6233}
6234#endif
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236/*
6237 * Move one "cooked" screen line to the screen, but only the characters that
6238 * have actually changed. Handle insert/delete character.
6239 * "coloff" gives the first column on the screen for this line.
6240 * "endcol" gives the columns where valid characters are.
6241 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6242 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006243 * "flags" can have bits:
6244 * SLF_POPUP popup window
6245 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6247 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6248 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006249 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006250screen_line(
6251 int row,
6252 int coloff,
6253 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006254 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006255 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 unsigned off_from;
6258 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006259 unsigned max_off_from;
6260 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 int force = FALSE; /* force update rest of the line */
6264 int redraw_this /* bool: does character need redraw? */
6265#ifdef FEAT_GUI
6266 = TRUE /* For GUI when while-loop empty */
6267#endif
6268 ;
6269 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 int clear_next = FALSE;
6271 int char_cells; /* 1: normal char */
6272 /* 2: occupies two display cells */
6273# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006275 /* Check for illegal row and col, just in case. */
6276 if (row >= Rows)
6277 row = Rows - 1;
6278 if (endcol > Columns)
6279 endcol = Columns;
6280
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281# ifdef FEAT_CLIPBOARD
6282 clip_may_clear_selection(row, row);
6283# endif
6284
6285 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6286 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006287 max_off_from = off_from + screen_Columns;
6288 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006291 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 {
6293 /* Clear rest first, because it's left of the text. */
6294 if (clear_width > 0)
6295 {
6296 while (col <= endcol && ScreenLines[off_to] == ' '
6297 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006298 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 ++off_to;
6301 ++col;
6302 }
6303 if (col <= endcol)
6304 screen_fill(row, row + 1, col + coloff,
6305 endcol + coloff + 1, ' ', ' ', 0);
6306 }
6307 col = endcol + 1;
6308 off_to = LineOffset[row] + col + coloff;
6309 off_from += col;
6310 endcol = (clear_width > 0 ? clear_width : -clear_width);
6311 }
6312#endif /* FEAT_RIGHTLEFT */
6313
6314 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6315
6316 while (col < endcol)
6317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006319 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 else
6321 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
6323 redraw_this = redraw_next;
6324 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6325 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6326
6327#ifdef FEAT_GUI
6328 /* If the next character was bold, then redraw the current character to
6329 * remove any pixels that might have spilt over into us. This only
6330 * happens in the GUI.
6331 */
6332 if (redraw_next && gui.in_use)
6333 {
6334 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006335 if (hl > HL_ALL)
6336 hl = syn_attr2attr(hl);
6337 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 redraw_this = TRUE;
6339 }
6340#endif
6341
6342 if (redraw_this)
6343 {
6344 /*
6345 * Special handling when 'xs' termcap flag set (hpterm):
6346 * Attributes for characters are stored at the position where the
6347 * cursor is when writing the highlighting code. The
6348 * start-highlighting code must be written with the cursor on the
6349 * first highlighted character. The stop-highlighting code must
6350 * be written with the cursor just after the last highlighted
6351 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006352 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 * to clear the rest of the line, and force redrawing it
6354 * completely.
6355 */
6356 if ( p_wiv
6357 && !force
6358#ifdef FEAT_GUI
6359 && !gui.in_use
6360#endif
6361 && ScreenAttrs[off_to] != 0
6362 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6363 {
6364 /*
6365 * Need to remove highlighting attributes here.
6366 */
6367 windgoto(row, col + coloff);
6368 out_str(T_CE); /* clear rest of this screen line */
6369 screen_start(); /* don't know where cursor is now */
6370 force = TRUE; /* force redraw of rest of the line */
6371 redraw_next = TRUE; /* or else next char would miss out */
6372
6373 /*
6374 * If the previous character was highlighted, need to stop
6375 * highlighting at this character.
6376 */
6377 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6378 {
6379 screen_attr = ScreenAttrs[off_to - 1];
6380 term_windgoto(row, col + coloff);
6381 screen_stop_highlight();
6382 }
6383 else
6384 screen_attr = 0; /* highlighting has stopped */
6385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 if (enc_dbcs != 0)
6387 {
6388 /* Check if overwriting a double-byte with a single-byte or
6389 * the other way around requires another character to be
6390 * redrawn. For UTF-8 this isn't needed, because comparing
6391 * ScreenLinesUC[] is sufficient. */
6392 if (char_cells == 1
6393 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006394 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 {
6396 /* Writing a single-cell character over a double-cell
6397 * character: need to redraw the next cell. */
6398 ScreenLines[off_to + 1] = 0;
6399 redraw_next = TRUE;
6400 }
6401 else if (char_cells == 2
6402 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006403 && (*mb_off2cells)(off_to, max_off_to) == 1
6404 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 /* Writing the second half of a double-cell character over
6407 * a double-cell character: need to redraw the second
6408 * cell. */
6409 ScreenLines[off_to + 2] = 0;
6410 redraw_next = TRUE;
6411 }
6412
6413 if (enc_dbcs == DBCS_JPNU)
6414 ScreenLines2[off_to] = ScreenLines2[off_from];
6415 }
6416 /* When writing a single-width character over a double-width
6417 * character and at the end of the redrawn text, need to clear out
6418 * the right halve of the old character.
6419 * Also required when writing the right halve of a double-width
6420 * char over the left halve of an existing one. */
6421 if (has_mbyte && col + char_cells == endcol
6422 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006423 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006425 && (*mb_off2cells)(off_to, max_off_to) == 1
6426 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (enc_utf8)
6431 {
6432 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6433 if (ScreenLinesUC[off_from] != 0)
6434 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006435 int i;
6436
6437 for (i = 0; i < Screen_mco; ++i)
6438 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 }
6440 }
6441 if (char_cells == 2)
6442 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443
6444#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006445 /* The bold trick makes a single column of pixels appear in the
6446 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 * character should be redrawn too. This happens for our own GUI
6448 * and for some xterms. */
6449 if (
6450# ifdef FEAT_GUI
6451 gui.in_use
6452# endif
6453# if defined(FEAT_GUI) && defined(UNIX)
6454 ||
6455# endif
6456# ifdef UNIX
6457 term_is_xterm
6458# endif
6459 )
6460 {
6461 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006462 if (hl > HL_ALL)
6463 hl = syn_attr2attr(hl);
6464 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 redraw_next = TRUE;
6466 }
6467#endif
6468 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006469
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006470 /* For simplicity set the attributes of second half of a
6471 * double-wide character equal to the first half. */
6472 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006474
6475 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 screen_char(off_to, row, col + coloff);
6479 }
6480 else if ( p_wiv
6481#ifdef FEAT_GUI
6482 && !gui.in_use
6483#endif
6484 && col + coloff > 0)
6485 {
6486 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6487 {
6488 /*
6489 * Don't output stop-highlight when moving the cursor, it will
6490 * stop the highlighting when it should continue.
6491 */
6492 screen_attr = 0;
6493 }
6494 else if (screen_attr != 0)
6495 screen_stop_highlight();
6496 }
6497
6498 off_to += CHAR_CELLS;
6499 off_from += CHAR_CELLS;
6500 col += CHAR_CELLS;
6501 }
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 if (clear_next)
6504 {
6505 /* Clear the second half of a double-wide character of which the left
6506 * half was overwritten with a single-wide character. */
6507 ScreenLines[off_to] = ' ';
6508 if (enc_utf8)
6509 ScreenLinesUC[off_to] = 0;
6510 screen_char(off_to, row, col + coloff);
6511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512
6513 if (clear_width > 0
6514#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006515 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516#endif
6517 )
6518 {
6519#ifdef FEAT_GUI
6520 int startCol = col;
6521#endif
6522
6523 /* blank out the rest of the line */
6524 while (col < clear_width && ScreenLines[off_to] == ' '
6525 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006526 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
6528 ++off_to;
6529 ++col;
6530 }
6531 if (col < clear_width)
6532 {
6533#ifdef FEAT_GUI
6534 /*
6535 * In the GUI, clearing the rest of the line may leave pixels
6536 * behind if the first character cleared was bold. Some bold
6537 * fonts spill over the left. In this case we redraw the previous
6538 * character too. If we didn't skip any blanks above, then we
6539 * only redraw if the character wasn't already redrawn anyway.
6540 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006541 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
6543 hl = ScreenAttrs[off_to];
6544 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006545 {
6546 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006547
Bram Moolenaar9c697322006-10-09 20:11:17 +00006548 if (enc_utf8)
6549 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6550 * that its width is 2. */
6551 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6552 else if (enc_dbcs != 0)
6553 {
6554 /* find previous character by counting from first
6555 * column and get its width. */
6556 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006557 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006558
6559 while (off < off_to)
6560 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006561 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006562 off += prev_cells;
6563 }
6564 }
6565
6566 if (enc_dbcs != 0 && prev_cells > 1)
6567 screen_char_2(off_to - prev_cells, row,
6568 col + coloff - prev_cells);
6569 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006570 screen_char(off_to - prev_cells, row,
6571 col + coloff - prev_cells);
6572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 }
6574#endif
6575 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6576 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 off_to += clear_width - col;
6578 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580 }
6581
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006582 if (clear_width > 0
6583#ifdef FEAT_TEXT_PROP
6584 && !(flags & SLF_POPUP) // no separator for popup window
6585#endif
6586 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006588 // For a window that has a right neighbor, draw the separator char
6589 // right of the window contents.
6590 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {
6592 int c;
6593
6594 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006595 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006596 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6597 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 || ScreenAttrs[off_to] != hl)
6599 {
6600 ScreenLines[off_to] = c;
6601 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 if (enc_utf8)
6603 {
6604 if (c >= 0x80)
6605 {
6606 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006607 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 else
6610 ScreenLinesUC[off_to] = 0;
6611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 screen_char(off_to, row, col + coloff);
6613 }
6614 }
6615 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 LineWraps[row] = FALSE;
6617 }
6618}
6619
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006620#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006622 * Mirror text "str" for right-left displaying.
6623 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006625 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006626rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 char_u *p1, *p2;
6629 int t;
6630
6631 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6632 {
6633 t = *p1;
6634 *p1 = *p2;
6635 *p2 = t;
6636 }
6637}
6638#endif
6639
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640/*
6641 * mark all status lines for redraw; used after first :cd
6642 */
6643 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006644status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 win_T *wp;
6647
Bram Moolenaar29323592016-07-24 22:04:11 +02006648 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 if (wp->w_status_height)
6650 {
6651 wp->w_redr_status = TRUE;
6652 redraw_later(VALID);
6653 }
6654}
6655
6656/*
6657 * mark all status lines of the current buffer for redraw
6658 */
6659 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006660status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 win_T *wp;
6663
Bram Moolenaar29323592016-07-24 22:04:11 +02006664 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6666 {
6667 wp->w_redr_status = TRUE;
6668 redraw_later(VALID);
6669 }
6670}
6671
6672/*
6673 * Redraw all status lines that need to be redrawn.
6674 */
6675 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006676redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 win_T *wp;
6679
Bram Moolenaar29323592016-07-24 22:04:11 +02006680 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006682 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006683 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006684 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
Bram Moolenaar4033c552017-09-16 20:54:51 +02006687#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688/*
6689 * Redraw all status lines at the bottom of frame "frp".
6690 */
6691 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006692win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693{
6694 if (frp->fr_layout == FR_LEAF)
6695 frp->fr_win->w_redr_status = TRUE;
6696 else if (frp->fr_layout == FR_ROW)
6697 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006698 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 win_redraw_last_status(frp);
6700 }
6701 else /* frp->fr_layout == FR_COL */
6702 {
6703 frp = frp->fr_child;
6704 while (frp->fr_next != NULL)
6705 frp = frp->fr_next;
6706 win_redraw_last_status(frp);
6707 }
6708}
6709#endif
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
6712 * Draw the verticap separator right of window "wp" starting with line "row".
6713 */
6714 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006715draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 int hl;
6718 int c;
6719
6720 if (wp->w_vsep_width)
6721 {
6722 /* draw the vertical separator right of this window */
6723 c = fillchar_vsep(&hl);
6724 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6725 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6726 c, ' ', hl);
6727 }
6728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006731static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732
6733/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006734 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 */
6736 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006737status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int len = 0;
6740
6741#ifdef FEAT_MENU
6742 int emenu = (xp->xp_context == EXPAND_MENUS
6743 || xp->xp_context == EXPAND_MENUNAMES);
6744
6745 /* Check for menu separators - replace with '|'. */
6746 if (emenu && menu_is_separator(s))
6747 return 1;
6748#endif
6749
6750 while (*s != NUL)
6751 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006752 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006753 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006754 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
6756
6757 return len;
6758}
6759
6760/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006761 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006762 * These are backslashes used for escaping. Do show backslashes in help tags.
6763 */
6764 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006765skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006766{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006767 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006768#ifdef FEAT_MENU
6769 || ((xp->xp_context == EXPAND_MENUS
6770 || xp->xp_context == EXPAND_MENUNAMES)
6771 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6772#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006773 )
6774 {
6775#ifndef BACKSLASH_IN_FILENAME
6776 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6777 return 2;
6778#endif
6779 return 1;
6780 }
6781 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006782}
6783
6784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 * Show wildchar matches in the status line.
6786 * Show at least the "match" item.
6787 * We start at item 'first_match' in the list and show all matches that fit.
6788 *
6789 * If inversion is possible we use it. Else '=' characters are used.
6790 */
6791 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006792win_redr_status_matches(
6793 expand_T *xp,
6794 int num_matches,
6795 char_u **matches, /* list of matches */
6796 int match,
6797 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6800 int row;
6801 char_u *buf;
6802 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006803 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 int fillchar;
6805 int attr;
6806 int i;
6807 int highlight = TRUE;
6808 char_u *selstart = NULL;
6809 int selstart_col = 0;
6810 char_u *selend = NULL;
6811 static int first_match = 0;
6812 int add_left = FALSE;
6813 char_u *s;
6814#ifdef FEAT_MENU
6815 int emenu;
6816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
6819 if (matches == NULL) /* interrupted completion? */
6820 return;
6821
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006822 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006823 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006824 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006825 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (buf == NULL)
6827 return;
6828
6829 if (match == -1) /* don't show match but original text */
6830 {
6831 match = 0;
6832 highlight = FALSE;
6833 }
6834 /* count 1 for the ending ">" */
6835 clen = status_match_len(xp, L_MATCH(match)) + 3;
6836 if (match == 0)
6837 first_match = 0;
6838 else if (match < first_match)
6839 {
6840 /* jumping left, as far as we can go */
6841 first_match = match;
6842 add_left = TRUE;
6843 }
6844 else
6845 {
6846 /* check if match fits on the screen */
6847 for (i = first_match; i < match; ++i)
6848 clen += status_match_len(xp, L_MATCH(i)) + 2;
6849 if (first_match > 0)
6850 clen += 2;
6851 /* jumping right, put match at the left */
6852 if ((long)clen > Columns)
6853 {
6854 first_match = match;
6855 /* if showing the last match, we can add some on the left */
6856 clen = 2;
6857 for (i = match; i < num_matches; ++i)
6858 {
6859 clen += status_match_len(xp, L_MATCH(i)) + 2;
6860 if ((long)clen >= Columns)
6861 break;
6862 }
6863 if (i == num_matches)
6864 add_left = TRUE;
6865 }
6866 }
6867 if (add_left)
6868 while (first_match > 0)
6869 {
6870 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6871 if ((long)clen >= Columns)
6872 break;
6873 --first_match;
6874 }
6875
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006876 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878 if (first_match == 0)
6879 {
6880 *buf = NUL;
6881 len = 0;
6882 }
6883 else
6884 {
6885 STRCPY(buf, "< ");
6886 len = 2;
6887 }
6888 clen = len;
6889
6890 i = first_match;
6891 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6892 {
6893 if (i == match)
6894 {
6895 selstart = buf + len;
6896 selstart_col = clen;
6897 }
6898
6899 s = L_MATCH(i);
6900 /* Check for menu separators - replace with '|' */
6901#ifdef FEAT_MENU
6902 emenu = (xp->xp_context == EXPAND_MENUS
6903 || xp->xp_context == EXPAND_MENUNAMES);
6904 if (emenu && menu_is_separator(s))
6905 {
6906 STRCPY(buf + len, transchar('|'));
6907 l = (int)STRLEN(buf + len);
6908 len += l;
6909 clen += l;
6910 }
6911 else
6912#endif
6913 for ( ; *s != NUL; ++s)
6914 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006915 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006917 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 STRNCPY(buf + len, s, l);
6920 s += l - 1;
6921 len += l;
6922 }
6923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
6925 STRCPY(buf + len, transchar_byte(*s));
6926 len += (int)STRLEN(buf + len);
6927 }
6928 }
6929 if (i == match)
6930 selend = buf + len;
6931
6932 *(buf + len++) = ' ';
6933 *(buf + len++) = ' ';
6934 clen += 2;
6935 if (++i == num_matches)
6936 break;
6937 }
6938
6939 if (i != num_matches)
6940 {
6941 *(buf + len++) = '>';
6942 ++clen;
6943 }
6944
6945 buf[len] = NUL;
6946
6947 row = cmdline_row - 1;
6948 if (row >= 0)
6949 {
6950 if (wild_menu_showing == 0)
6951 {
6952 if (msg_scrolled > 0)
6953 {
6954 /* Put the wildmenu just above the command line. If there is
6955 * no room, scroll the screen one line up. */
6956 if (cmdline_row == Rows - 1)
6957 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006958 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 ++msg_scrolled;
6960 }
6961 else
6962 {
6963 ++cmdline_row;
6964 ++row;
6965 }
6966 wild_menu_showing = WM_SCROLLED;
6967 }
6968 else
6969 {
6970 /* Create status line if needed by setting 'laststatus' to 2.
6971 * Set 'winminheight' to zero to avoid that the window is
6972 * resized. */
6973 if (lastwin->w_status_height == 0)
6974 {
6975 save_p_ls = p_ls;
6976 save_p_wmh = p_wmh;
6977 p_ls = 2;
6978 p_wmh = 0;
6979 last_status(FALSE);
6980 }
6981 wild_menu_showing = WM_SHOWN;
6982 }
6983 }
6984
6985 screen_puts(buf, row, 0, attr);
6986 if (selstart != NULL && highlight)
6987 {
6988 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006989 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
6991
6992 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6993 }
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 vim_free(buf);
6997}
6998#endif
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000/*
7001 * Redraw the status line of window wp.
7002 *
7003 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007004 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7005 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007007 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007008win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 int row;
7011 char_u *p;
7012 int len;
7013 int fillchar;
7014 int attr;
7015 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007016 static int busy = FALSE;
7017
7018 /* It's possible to get here recursively when 'statusline' (indirectly)
7019 * invokes ":redrawstatus". Simply ignore the call then. */
7020 if (busy)
7021 return;
7022 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 wp->w_redr_status = FALSE;
7025 if (wp->w_status_height == 0)
7026 {
7027 /* no status line, can only be last window */
7028 redraw_cmdline = TRUE;
7029 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007030 else if (!redrawing()
7031#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007032 // don't update status line when popup menu is visible and may be
7033 // drawn over it, unless it will be redrawn later
7034 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007035#endif
7036 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 /* Don't redraw right now, do it later. */
7039 wp->w_redr_status = TRUE;
7040 }
7041#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007042 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007045 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047#endif
7048 else
7049 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007050 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007052 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 p = NameBuff;
7054 len = (int)STRLEN(p);
7055
Bram Moolenaard85f2712017-07-28 21:51:57 +02007056 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#ifdef FEAT_QUICKFIX
7058 || wp->w_p_pvw
7059#endif
7060 || bufIsChanged(wp->w_buffer)
7061 || wp->w_buffer->b_p_ro)
7062 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007063 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007065 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 len += (int)STRLEN(p + len);
7067 }
7068#ifdef FEAT_QUICKFIX
7069 if (wp->w_p_pvw)
7070 {
7071 STRCPY(p + len, _("[Preview]"));
7072 len += (int)STRLEN(p + len);
7073 }
7074#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007075 if (bufIsChanged(wp->w_buffer)
7076#ifdef FEAT_TERMINAL
7077 && !bt_terminal(wp->w_buffer)
7078#endif
7079 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
7081 STRCPY(p + len, "[+]");
7082 len += 3;
7083 }
7084 if (wp->w_buffer->b_p_ro)
7085 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007086 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007087 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089
Bram Moolenaar02631462017-09-22 15:20:32 +02007090 this_ru_col = ru_col - (Columns - wp->w_width);
7091 if (this_ru_col < (wp->w_width + 1) / 2)
7092 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (this_ru_col <= 1)
7094 {
7095 p = (char_u *)"<"; /* No room for file name! */
7096 len = 1;
7097 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007098 else if (has_mbyte)
7099 {
7100 int clen = 0, i;
7101
7102 /* Count total number of display cells. */
7103 clen = mb_string2cells(p, -1);
7104
7105 /* Find first character that will fit.
7106 * Going from start to end is much faster for DBCS. */
7107 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7108 i += (*mb_ptr2len)(p + i))
7109 clen -= (*mb_ptr2cells)(p + i);
7110 len = clen;
7111 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007113 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007115 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 }
7117
Bram Moolenaara12a1612019-01-24 16:39:02 +01007118 }
7119 else if (len > this_ru_col - 1)
7120 {
7121 p += len - (this_ru_col - 1);
7122 *p = '<';
7123 len = this_ru_col - 1;
7124 }
7125
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007127 screen_puts(p, row, wp->w_wincol, attr);
7128 screen_fill(row, row + 1, len + wp->w_wincol,
7129 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007131 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7133 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007134 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007137 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138#endif
7139 }
7140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 /*
7142 * May need to draw the character below the vertical separator.
7143 */
7144 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7145 {
7146 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007147 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 else
7149 fillchar = fillchar_vsep(&attr);
7150 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7151 attr);
7152 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007153 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154}
7155
Bram Moolenaar238a5642006-02-21 22:12:05 +00007156#ifdef FEAT_STL_OPT
7157/*
7158 * Redraw the status line according to 'statusline' and take care of any
7159 * errors encountered.
7160 */
7161 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007162redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007163{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007164 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007165 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007166
7167 /* When called recursively return. This can happen when the statusline
7168 * contains an expression that triggers a redraw. */
7169 if (entered)
7170 return;
7171 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007172
Bram Moolenaara742e082016-04-05 21:10:38 +02007173 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007174 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007175 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007176 {
7177 /* When there is an error disable the statusline, otherwise the
7178 * display is messed up with errors and a redraw triggers the problem
7179 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007180 set_string_option_direct((char_u *)"statusline", -1,
7181 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007182 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007183 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007184 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007185 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007186}
7187#endif
7188
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189/*
7190 * Return TRUE if the status line of window "wp" is connected to the status
7191 * line of the window right of it. If not, then it's a vertical separator.
7192 * Only call if (wp->w_vsep_width != 0).
7193 */
7194 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007195stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
7197 frame_T *fr;
7198
7199 fr = wp->w_frame;
7200 while (fr->fr_parent != NULL)
7201 {
7202 if (fr->fr_parent->fr_layout == FR_COL)
7203 {
7204 if (fr->fr_next != NULL)
7205 break;
7206 }
7207 else
7208 {
7209 if (fr->fr_next != NULL)
7210 return TRUE;
7211 }
7212 fr = fr->fr_parent;
7213 }
7214 return FALSE;
7215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218/*
7219 * Get the value to show for the language mappings, active 'keymap'.
7220 */
7221 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007222get_keymap_str(
7223 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007224 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007225 char_u *buf, /* buffer for the result */
7226 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 char_u *p;
7229
7230 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7231 return FALSE;
7232
7233 {
7234#ifdef FEAT_EVAL
7235 buf_T *old_curbuf = curbuf;
7236 win_T *old_curwin = curwin;
7237 char_u *s;
7238
7239 curbuf = wp->w_buffer;
7240 curwin = wp;
7241 STRCPY(buf, "b:keymap_name"); /* must be writable */
7242 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007243 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 --emsg_skip;
7245 curbuf = old_curbuf;
7246 curwin = old_curwin;
7247 if (p == NULL || *p == NUL)
7248#endif
7249 {
7250#ifdef FEAT_KEYMAP
7251 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7252 p = wp->w_buffer->b_p_keymap;
7253 else
7254#endif
7255 p = (char_u *)"lang";
7256 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007257 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 buf[0] = NUL;
7259#ifdef FEAT_EVAL
7260 vim_free(s);
7261#endif
7262 }
7263 return buf[0] != NUL;
7264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
7266#if defined(FEAT_STL_OPT) || defined(PROTO)
7267/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007268 * Redraw the status line or ruler of window "wp".
7269 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 */
7271 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007272win_redr_custom(
7273 win_T *wp,
7274 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007276 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 int attr;
7278 int curattr;
7279 int row;
7280 int col = 0;
7281 int maxwidth;
7282 int width;
7283 int n;
7284 int len;
7285 int fillchar;
7286 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007287 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007289 struct stl_hlrec hltab[STL_MAX_ITEM];
7290 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007292 win_T *ewp;
7293 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaar1d633412013-12-11 15:52:01 +01007295 /* There is a tiny chance that this gets called recursively: When
7296 * redrawing a status line triggers redrawing the ruler or tabline.
7297 * Avoid trouble by not allowing recursion. */
7298 if (entered)
7299 return;
7300 entered = TRUE;
7301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007306 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007307 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007308 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007309 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007310 maxwidth = Columns;
7311# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007312 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007315 else
7316 {
7317 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007318 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007319 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320
7321 if (draw_ruler)
7322 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007323 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007324 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007325 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007326 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 if (*++stl == '-')
7328 stl++;
7329 if (atoi((char *)stl))
7330 while (VIM_ISDIGIT(*stl))
7331 stl++;
7332 if (*stl++ != '(')
7333 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007334 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007335 col = ru_col - (Columns - wp->w_width);
7336 if (col < (wp->w_width + 1) / 2)
7337 col = (wp->w_width + 1) / 2;
7338 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007339 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007340 {
7341 row = Rows - 1;
7342 --maxwidth; /* writing in last column may cause scrolling */
7343 fillchar = ' ';
7344 attr = 0;
7345 }
7346
7347# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007348 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007349# endif
7350 }
7351 else
7352 {
7353 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007354 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007355 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007356 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007357# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007358 use_sandbox = was_set_insecurely((char_u *)"statusline",
7359 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007360# endif
7361 }
7362
Bram Moolenaar53f81742017-09-22 14:35:51 +02007363 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007364 }
7365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007367 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
Bram Moolenaar61452852011-02-01 18:01:11 +01007369 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7370 * the cursor away and back. */
7371 ewp = wp == NULL ? curwin : wp;
7372 p_crb_save = ewp->w_p_crb;
7373 ewp->w_p_crb = FALSE;
7374
Bram Moolenaar362f3562009-11-03 16:20:34 +00007375 /* Make a copy, because the statusline may include a function call that
7376 * might change the option value and free the memory. */
7377 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007378 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007379 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007380 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007381 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007382 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007384 /* Make all characters printable. */
7385 p = transstr(buf);
7386 if (p != NULL)
7387 {
7388 vim_strncpy(buf, p, sizeof(buf) - 1);
7389 vim_free(p);
7390 }
7391
7392 /* fill up with "fillchar" */
7393 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007394 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 ++width;
7398 }
7399 buf[len] = NUL;
7400
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007401 /*
7402 * Draw each snippet with the specified highlighting.
7403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 curattr = attr;
7405 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007406 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007408 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 screen_puts_len(p, len, row, col, curattr);
7410 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007411 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007413 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007415 else if (hltab[n].userhl < 0)
7416 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007417#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007418 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7419 && wp->w_status_height != 0)
7420 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007421 else if (wp != NULL && bt_terminal(wp->w_buffer)
7422 && wp->w_status_height != 0)
7423 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007424#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007425 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007426 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007428 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007431
7432 if (wp == NULL)
7433 {
7434 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7435 col = 0;
7436 len = 0;
7437 p = buf;
7438 fillchar = 0;
7439 for (n = 0; tabtab[n].start != NULL; n++)
7440 {
7441 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7442 while (col < len)
7443 TabPageIdxs[col++] = fillchar;
7444 p = tabtab[n].start;
7445 fillchar = tabtab[n].userhl;
7446 }
7447 while (col < Columns)
7448 TabPageIdxs[col++] = fillchar;
7449 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007450
7451theend:
7452 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453}
7454
7455#endif /* FEAT_STL_OPT */
7456
7457/*
7458 * Output a single character directly to the screen and update ScreenLines.
7459 */
7460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007461screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 char_u buf[MB_MAXBYTES + 1];
7464
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007465 if (has_mbyte)
7466 buf[(*mb_char2bytes)(c, buf)] = NUL;
7467 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007468 {
7469 buf[0] = c;
7470 buf[1] = NUL;
7471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 screen_puts(buf, row, col, attr);
7473}
7474
7475/*
7476 * Get a single character directly from ScreenLines into "bytes[]".
7477 * Also return its attribute in *attrp;
7478 */
7479 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007480screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 unsigned off;
7483
7484 /* safety check */
7485 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7486 {
7487 off = LineOffset[row] + col;
7488 *attrp = ScreenAttrs[off];
7489 bytes[0] = ScreenLines[off];
7490 bytes[1] = NUL;
7491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (enc_utf8 && ScreenLinesUC[off] != 0)
7493 bytes[utfc_char2bytes(off, bytes)] = NUL;
7494 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7495 {
7496 bytes[0] = ScreenLines[off];
7497 bytes[1] = ScreenLines2[off];
7498 bytes[2] = NUL;
7499 }
7500 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7501 {
7502 bytes[1] = ScreenLines[off + 1];
7503 bytes[2] = NUL;
7504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506}
7507
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007508/*
7509 * Return TRUE if composing characters for screen posn "off" differs from
7510 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007511 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007512 */
7513 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007514screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007515{
7516 int i;
7517
7518 for (i = 0; i < Screen_mco; ++i)
7519 {
7520 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7521 return TRUE;
7522 if (u8cc[i] == 0)
7523 break;
7524 }
7525 return FALSE;
7526}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528/*
7529 * Put string '*text' on the screen at position 'row' and 'col', with
7530 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7531 * Note: only outputs within one row, message is truncated at screen boundary!
7532 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7533 */
7534 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007535screen_puts(
7536 char_u *text,
7537 int row,
7538 int col,
7539 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
7541 screen_puts_len(text, -1, row, col, attr);
7542}
7543
7544/*
7545 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7546 * a NUL.
7547 */
7548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007549screen_puts_len(
7550 char_u *text,
7551 int textlen,
7552 int row,
7553 int col,
7554 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555{
7556 unsigned off;
7557 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007558 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007560 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 int mbyte_blen = 1;
7562 int mbyte_cells = 1;
7563 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007564 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007566#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007568 int pc, nc, nc1;
7569 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007571 int force_redraw_this;
7572 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007573 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574
7575 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7576 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007577 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaarc236c162008-07-13 17:41:49 +00007579 /* When drawing over the right halve of a double-wide char clear out the
7580 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007581 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007582#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007583 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007584#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007585 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007586 {
7587 ScreenLines[off - 1] = ' ';
7588 ScreenAttrs[off - 1] = 0;
7589 if (enc_utf8)
7590 {
7591 ScreenLinesUC[off - 1] = 0;
7592 ScreenLinesC[0][off - 1] = 0;
7593 }
7594 /* redraw the previous cell, make it empty */
7595 screen_char(off - 1, row, col - 1);
7596 /* force the cell at "col" to be redrawn */
7597 force_redraw_next = TRUE;
7598 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007599
Bram Moolenaar367329b2007-08-30 11:53:22 +00007600 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007601 while (col < screen_Columns
7602 && (len < 0 || (int)(ptr - text) < len)
7603 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 {
7605 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 /* check if this is the first byte of a multibyte */
7607 if (has_mbyte)
7608 {
7609 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007610 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007612 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7614 mbyte_cells = 1;
7615 else if (enc_dbcs != 0)
7616 mbyte_cells = mbyte_blen;
7617 else /* enc_utf8 */
7618 {
7619 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007620 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 (int)((text + len) - ptr));
7622 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007623 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007625#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7627 {
7628 /* Do Arabic shaping. */
7629 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7630 {
7631 /* Past end of string to be displayed. */
7632 nc = NUL;
7633 nc1 = NUL;
7634 }
7635 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007636 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007637 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7638 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007639 nc1 = pcc[0];
7640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 pc = prev_c;
7642 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007643 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 }
7645 else
7646 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007647#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007648 if (col + mbyte_cells > screen_Columns)
7649 {
7650 /* Only 1 cell left, but character requires 2 cells:
7651 * display a '>' in the last column to avoid wrapping. */
7652 c = '>';
7653 mbyte_cells = 1;
7654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
7656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007658 force_redraw_this = force_redraw_next;
7659 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007660
7661 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 || (mbyte_cells == 2
7663 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7664 || (enc_dbcs == DBCS_JPNU
7665 && c == 0x8e
7666 && ScreenLines2[off] != ptr[1])
7667 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007668 && (ScreenLinesUC[off] !=
7669 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7670 || (ScreenLinesUC[off] != 0
7671 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007673 || exmode_active;
7674
Bram Moolenaara12a1612019-01-24 16:39:02 +01007675 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
7677#if defined(FEAT_GUI) || defined(UNIX)
7678 /* The bold trick makes a single row of pixels appear in the next
7679 * character. When a bold character is removed, the next
7680 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007681 * and for some xterms. */
7682 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683# ifdef FEAT_GUI
7684 gui.in_use
7685# endif
7686# if defined(FEAT_GUI) && defined(UNIX)
7687 ||
7688# endif
7689# ifdef UNIX
7690 term_is_xterm
7691# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007692 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007694 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007696 if (n > HL_ALL)
7697 n = syn_attr2attr(n);
7698 if (n & HL_BOLD)
7699 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 /* When at the end of the text and overwriting a two-cell
7703 * character with a one-cell character, need to clear the next
7704 * cell. Also when overwriting the left halve of a two-cell char
7705 * with the right halve of a two-cell char. Do this only once
7706 * (mb_off2cells() may return 2 on the right halve). */
7707 if (clear_next_cell)
7708 clear_next_cell = FALSE;
7709 else if (has_mbyte
7710 && (len < 0 ? ptr[mbyte_blen] == NUL
7711 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007712 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007714 && (*mb_off2cells)(off, max_off) == 1
7715 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 clear_next_cell = TRUE;
7717
7718 /* Make sure we never leave a second byte of a double-byte behind,
7719 * it confuses mb_off2cells(). */
7720 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007721 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007723 && (*mb_off2cells)(off, max_off) == 1
7724 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 ScreenLines[off] = c;
7727 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (enc_utf8)
7729 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007730 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 ScreenLinesUC[off] = 0;
7732 else
7733 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007734 int i;
7735
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007737 for (i = 0; i < Screen_mco; ++i)
7738 {
7739 ScreenLinesC[i][off] = u8cc[i];
7740 if (u8cc[i] == 0)
7741 break;
7742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
7744 if (mbyte_cells == 2)
7745 {
7746 ScreenLines[off + 1] = 0;
7747 ScreenAttrs[off + 1] = attr;
7748 }
7749 screen_char(off, row, col);
7750 }
7751 else if (mbyte_cells == 2)
7752 {
7753 ScreenLines[off + 1] = ptr[1];
7754 ScreenAttrs[off + 1] = attr;
7755 screen_char_2(off, row, col);
7756 }
7757 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7758 {
7759 ScreenLines2[off] = ptr[1];
7760 screen_char(off, row, col);
7761 }
7762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 screen_char(off, row, col);
7764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 if (has_mbyte)
7766 {
7767 off += mbyte_cells;
7768 col += mbyte_cells;
7769 ptr += mbyte_blen;
7770 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007771 {
7772 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007774 len = -1;
7775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
7779 ++off;
7780 ++col;
7781 ++ptr;
7782 }
7783 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007784
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007785 /* If we detected the next character needs to be redrawn, but the text
7786 * doesn't extend up to there, update the character here. */
7787 if (force_redraw_next && col < screen_Columns)
7788 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007789 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7790 screen_char_2(off, row, col);
7791 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007792 screen_char(off, row, col);
7793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794}
7795
7796#ifdef FEAT_SEARCH_EXTRA
7797/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007798 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 */
7800 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007801start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802{
7803 if (p_hls && !no_hlsearch)
7804 {
7805 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007806 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007807# ifdef FEAT_RELTIME
7808 /* Set the time limit to 'redrawtime'. */
7809 profile_setlimit(p_rdt, &search_hl.tm);
7810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 }
7812}
7813
7814/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007815 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 */
7817 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007818end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 if (search_hl.rm.regprog != NULL)
7821 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007822 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 search_hl.rm.regprog = NULL;
7824 }
7825}
7826
7827/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007828 * Init for calling prepare_search_hl().
7829 */
7830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007831init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007832{
7833 matchitem_T *cur;
7834
7835 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7836 * match */
7837 cur = wp->w_match_head;
7838 while (cur != NULL)
7839 {
7840 cur->hl.rm = cur->match;
7841 if (cur->hlg_id == 0)
7842 cur->hl.attr = 0;
7843 else
7844 cur->hl.attr = syn_id2attr(cur->hlg_id);
7845 cur->hl.buf = wp->w_buffer;
7846 cur->hl.lnum = 0;
7847 cur->hl.first_lnum = 0;
7848# ifdef FEAT_RELTIME
7849 /* Set the time limit to 'redrawtime'. */
7850 profile_setlimit(p_rdt, &(cur->hl.tm));
7851# endif
7852 cur = cur->next;
7853 }
7854 search_hl.buf = wp->w_buffer;
7855 search_hl.lnum = 0;
7856 search_hl.first_lnum = 0;
7857 /* time limit is set at the toplevel, for all windows */
7858}
7859
7860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 * Advance to the match in window "wp" line "lnum" or past it.
7862 */
7863 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007864prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007866 matchitem_T *cur; /* points to the match list */
7867 match_T *shl; /* points to search_hl or a match */
7868 int shl_flag; /* flag to indicate whether search_hl
7869 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007870 int pos_inprogress; /* marks that position match search is
7871 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 int n;
7873
7874 /*
7875 * When using a multi-line pattern, start searching at the top
7876 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007877 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007879 cur = wp->w_match_head;
7880 shl_flag = FALSE;
7881 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007883 if (shl_flag == FALSE)
7884 {
7885 shl = &search_hl;
7886 shl_flag = TRUE;
7887 }
7888 else
7889 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 if (shl->rm.regprog != NULL
7891 && shl->lnum == 0
7892 && re_multiline(shl->rm.regprog))
7893 {
7894 if (shl->first_lnum == 0)
7895 {
7896# ifdef FEAT_FOLDING
7897 for (shl->first_lnum = lnum;
7898 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7899 if (hasFoldingWin(wp, shl->first_lnum - 1,
7900 NULL, NULL, TRUE, NULL))
7901 break;
7902# else
7903 shl->first_lnum = wp->w_topline;
7904# endif
7905 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007906 if (cur != NULL)
7907 cur->pos.cur = 0;
7908 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007910 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7911 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007913 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7914 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007915 pos_inprogress = cur == NULL || cur->pos.cur == 0
7916 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 if (shl->lnum != 0)
7918 {
7919 shl->first_lnum = shl->lnum
7920 + shl->rm.endpos[0].lnum
7921 - shl->rm.startpos[0].lnum;
7922 n = shl->rm.endpos[0].col;
7923 }
7924 else
7925 {
7926 ++shl->first_lnum;
7927 n = 0;
7928 }
7929 }
7930 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007931 if (shl != &search_hl && cur != NULL)
7932 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 }
7934}
7935
7936/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007937 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 * Uses shl->buf.
7939 * Sets shl->lnum and shl->rm contents.
7940 * Note: Assumes a previous match is always before "lnum", unless
7941 * shl->lnum is zero.
7942 * Careful: Any pointers for buffer lines will become invalid.
7943 */
7944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007945next_search_hl(
7946 win_T *win,
7947 match_T *shl, /* points to search_hl or a match */
7948 linenr_T lnum,
7949 colnr_T mincol, /* minimal column for a match */
7950 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 linenr_T l;
7953 colnr_T matchcol;
7954 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007955 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007957 // for :{range}s/pat only highlight inside the range
7958 if (lnum < search_first_line || lnum > search_last_line)
7959 {
7960 shl->lnum = 0;
7961 return;
7962 }
7963
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 if (shl->lnum != 0)
7965 {
7966 /* Check for three situations:
7967 * 1. If the "lnum" is below a previous match, start a new search.
7968 * 2. If the previous match includes "mincol", use it.
7969 * 3. Continue after the previous match.
7970 */
7971 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7972 if (lnum > l)
7973 shl->lnum = 0;
7974 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7975 return;
7976 }
7977
7978 /*
7979 * Repeat searching for a match until one is found that includes "mincol"
7980 * or none is found in this line.
7981 */
7982 called_emsg = FALSE;
7983 for (;;)
7984 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007985#ifdef FEAT_RELTIME
7986 /* Stop searching after passing the time limit. */
7987 if (profile_passed_limit(&(shl->tm)))
7988 {
7989 shl->lnum = 0; /* no match found in time */
7990 break;
7991 }
7992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 /* Three situations:
7994 * 1. No useful previous match: search from start of line.
7995 * 2. Not Vi compatible or empty match: continue at next character.
7996 * Break the loop if this is beyond the end of the line.
7997 * 3. Vi compatible searching: continue at end of previous match.
7998 */
7999 if (shl->lnum == 0)
8000 matchcol = 0;
8001 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8002 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008003 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008005 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008006
8007 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008008 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008009 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008011 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 shl->lnum = 0;
8013 break;
8014 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008015 if (has_mbyte)
8016 matchcol += mb_ptr2len(ml);
8017 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008018 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020 else
8021 matchcol = shl->rm.endpos[0].col;
8022
8023 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008026 /* Remember whether shl->rm is using a copy of the regprog in
8027 * cur->match. */
8028 int regprog_is_copy = (shl != &search_hl && cur != NULL
8029 && shl == &cur->hl
8030 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008031 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008032
Bram Moolenaarb3414592014-06-17 17:48:32 +02008033 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8034 matchcol,
8035#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008036 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008037#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008038 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039#endif
8040 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008041 /* Copy the regprog, in case it got freed and recompiled. */
8042 if (regprog_is_copy)
8043 cur->match.regprog = cur->hl.rm.regprog;
8044
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008045 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008046 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047 /* Error while handling regexp: stop using this regexp. */
8048 if (shl == &search_hl)
8049 {
8050 /* don't free regprog in the match list, it's a copy */
8051 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008052 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008053 }
8054 shl->rm.regprog = NULL;
8055 shl->lnum = 0;
8056 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8057 message */
8058 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008059 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008060 }
8061 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008063 else
8064 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (nmatched == 0)
8066 {
8067 shl->lnum = 0; /* no match found */
8068 break;
8069 }
8070 if (shl->rm.startpos[0].lnum > 0
8071 || shl->rm.startpos[0].col >= mincol
8072 || nmatched > 1
8073 || shl->rm.endpos[0].col > mincol)
8074 {
8075 shl->lnum += shl->rm.startpos[0].lnum;
8076 break; /* useful match found */
8077 }
8078 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008079
8080 // Restore called_emsg for assert_fails().
8081 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083
Bram Moolenaar85077472016-10-16 14:35:48 +02008084/*
8085 * If there is a match fill "shl" and return one.
8086 * Return zero otherwise.
8087 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008089next_search_hl_pos(
8090 match_T *shl, /* points to a match */
8091 linenr_T lnum,
8092 posmatch_T *posmatch, /* match positions */
8093 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008094{
8095 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008096 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8099 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008100 llpos_T *pos = &posmatch->pos[i];
8101
8102 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008103 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008104 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008105 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008106 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008107 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008108 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008109 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008110 /* if this match comes before the one at "found" then swap
8111 * them */
8112 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008114 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008115
Bram Moolenaar85077472016-10-16 14:35:48 +02008116 *pos = posmatch->pos[found];
8117 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008118 }
8119 }
8120 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008121 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008122 }
8123 }
8124 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008125 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008126 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008127 colnr_T start = posmatch->pos[found].col == 0
8128 ? 0 : posmatch->pos[found].col - 1;
8129 colnr_T end = posmatch->pos[found].col == 0
8130 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008131
Bram Moolenaar85077472016-10-16 14:35:48 +02008132 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008133 shl->rm.startpos[0].lnum = 0;
8134 shl->rm.startpos[0].col = start;
8135 shl->rm.endpos[0].lnum = 0;
8136 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008137 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008138 posmatch->cur = found + 1;
8139 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008140 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008141 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008142}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008143#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008144
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008146screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147{
8148 attrentry_T *aep = NULL;
8149
8150 screen_attr = attr;
8151 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008152#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 && termcap_active
8154#endif
8155 )
8156 {
8157#ifdef FEAT_GUI
8158 if (gui.in_use)
8159 {
8160 char buf[20];
8161
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008162 /* The GUI handles this internally. */
8163 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 OUT_STR(buf);
8165 }
8166 else
8167#endif
8168 {
8169 if (attr > HL_ALL) /* special HL attr. */
8170 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008171 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 aep = syn_cterm_attr2entry(attr);
8173 else
8174 aep = syn_term_attr2entry(attr);
8175 if (aep == NULL) /* did ":syntax clear" */
8176 attr = 0;
8177 else
8178 attr = aep->ae_attr;
8179 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008180 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008182 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008183#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008184 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8185 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8186 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008187#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008188 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008189 /* If the Normal FG color has BOLD attribute and the new HL
8190 * has a FG color defined, clear BOLD. */
8191 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008192 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008194 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008195 out_str(T_UCS);
8196 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008197 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8198 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008200 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008202 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008204 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008205 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
8207 /*
8208 * Output the color or start string after bold etc., in case the
8209 * bold etc. override the color setting.
8210 */
8211 if (aep != NULL)
8212 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008213#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008214 /* When 'termguicolors' is set but fg or bg is unset,
8215 * fall back to the cterm colors. This helps for SpellBad,
8216 * where the GUI uses a red undercurl. */
8217 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008219 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008220 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008221 }
8222 else
8223#endif
8224 if (t_colors > 1)
8225 {
8226 if (aep->ae_u.cterm.fg_color)
8227 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8228 }
8229#ifdef FEAT_TERMGUICOLORS
8230 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8231 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008232 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008233 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008236#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008237 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008239 if (aep->ae_u.cterm.bg_color)
8240 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8241 }
8242
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008243 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008244 {
8245 if (aep->ae_u.term.start != NULL)
8246 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248 }
8249 }
8250 }
8251}
8252
8253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008254screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
8256 int do_ME = FALSE; /* output T_ME code */
8257
8258 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008259#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 && termcap_active
8261#endif
8262 )
8263 {
8264#ifdef FEAT_GUI
8265 if (gui.in_use)
8266 {
8267 char buf[20];
8268
8269 /* use internal GUI code */
8270 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8271 OUT_STR(buf);
8272 }
8273 else
8274#endif
8275 {
8276 if (screen_attr > HL_ALL) /* special HL attr. */
8277 {
8278 attrentry_T *aep;
8279
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008280 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
8282 /*
8283 * Assume that t_me restores the original colors!
8284 */
8285 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008286 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008287#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008288 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8289 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8290 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008291#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008292 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008293#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008294 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8295 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8296 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008297#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008298 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 do_ME = TRUE;
8300 }
8301 else
8302 {
8303 aep = syn_term_attr2entry(screen_attr);
8304 if (aep != NULL && aep->ae_u.term.stop != NULL)
8305 {
8306 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8307 do_ME = TRUE;
8308 else
8309 out_str(aep->ae_u.term.stop);
8310 }
8311 }
8312 if (aep == NULL) /* did ":syntax clear" */
8313 screen_attr = 0;
8314 else
8315 screen_attr = aep->ae_attr;
8316 }
8317
8318 /*
8319 * Often all ending-codes are equal to T_ME. Avoid outputting the
8320 * same sequence several times.
8321 */
8322 if (screen_attr & HL_STANDOUT)
8323 {
8324 if (STRCMP(T_SE, T_ME) == 0)
8325 do_ME = TRUE;
8326 else
8327 out_str(T_SE);
8328 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008329 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008330 {
8331 if (STRCMP(T_UCE, T_ME) == 0)
8332 do_ME = TRUE;
8333 else
8334 out_str(T_UCE);
8335 }
8336 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008337 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {
8339 if (STRCMP(T_UE, T_ME) == 0)
8340 do_ME = TRUE;
8341 else
8342 out_str(T_UE);
8343 }
8344 if (screen_attr & HL_ITALIC)
8345 {
8346 if (STRCMP(T_CZR, T_ME) == 0)
8347 do_ME = TRUE;
8348 else
8349 out_str(T_CZR);
8350 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008351 if (screen_attr & HL_STRIKETHROUGH)
8352 {
8353 if (STRCMP(T_STE, T_ME) == 0)
8354 do_ME = TRUE;
8355 else
8356 out_str(T_STE);
8357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8359 out_str(T_ME);
8360
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008361#ifdef FEAT_TERMGUICOLORS
8362 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008364 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008365 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008366 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008367 term_bg_rgb_color(cterm_normal_bg_gui_color);
8368 }
8369 else
8370#endif
8371 {
8372 if (t_colors > 1)
8373 {
8374 /* set Normal cterm colors */
8375 if (cterm_normal_fg_color != 0)
8376 term_fg_color(cterm_normal_fg_color - 1);
8377 if (cterm_normal_bg_color != 0)
8378 term_bg_color(cterm_normal_bg_color - 1);
8379 if (cterm_normal_fg_bold)
8380 out_str(T_MD);
8381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 }
8384 }
8385 screen_attr = 0;
8386}
8387
8388/*
8389 * Reset the colors for a cterm. Used when leaving Vim.
8390 * The machine specific code may override this again.
8391 */
8392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008393reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008395 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {
8397 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008398#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008399 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8400 || cterm_normal_bg_gui_color != INVALCOLOR)
8401 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
8406 out_str(T_OP);
8407 screen_attr = -1;
8408 }
8409 if (cterm_normal_fg_bold)
8410 {
8411 out_str(T_ME);
8412 screen_attr = -1;
8413 }
8414 }
8415}
8416
8417/*
8418 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8419 * using the attributes from ScreenAttrs["off"].
8420 */
8421 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008422screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 int attr;
8425
8426 /* Check for illegal values, just in case (could happen just after
8427 * resizing). */
8428 if (row >= screen_Rows || col >= screen_Columns)
8429 return;
8430
Bram Moolenaarae654382019-01-17 21:09:05 +01008431#ifdef FEAT_INS_EXPAND
8432 if (pum_under_menu(row, col))
8433 return;
8434#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008435 /* Outputting a character in the last cell on the screen may scroll the
8436 * screen up. Only do it when the "xn" termcap property is set, otherwise
8437 * mark the character invalid (update it when scrolled up). */
8438 if (*T_XN == NUL
8439 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#ifdef FEAT_RIGHTLEFT
8441 /* account for first command-line character in rightleft mode */
8442 && !cmdmsg_rl
8443#endif
8444 )
8445 {
8446 ScreenAttrs[off] = (sattr_T)-1;
8447 return;
8448 }
8449
8450 /*
8451 * Stop highlighting first, so it's easier to move the cursor.
8452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (screen_char_attr != 0)
8454 attr = screen_char_attr;
8455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 attr = ScreenAttrs[off];
8457 if (screen_attr != attr)
8458 screen_stop_highlight();
8459
8460 windgoto(row, col);
8461
8462 if (screen_attr != attr)
8463 screen_start_highlight(attr);
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 if (enc_utf8 && ScreenLinesUC[off] != 0)
8466 {
8467 char_u buf[MB_MAXBYTES + 1];
8468
Bram Moolenaarcb070082016-04-02 22:14:51 +02008469 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008470 {
8471 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008472#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008473 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008474#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008475 )
8476 {
8477 /* Clear the two screen cells. If the character is actually
8478 * single width it won't change the second cell. */
8479 out_str((char_u *)" ");
8480 term_windgoto(row, col);
8481 }
8482 /* not sure where the cursor is after drawing the ambiguous width
8483 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008484 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008485 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008486 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008488
8489 /* Convert the UTF-8 character to bytes and write it. */
8490 buf[utfc_char2bytes(off, buf)] = NUL;
8491 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 }
8493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 /* double-byte character in single-width cell */
8498 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8499 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 }
8501
8502 screen_cur_col++;
8503}
8504
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505/*
8506 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8507 * on the screen at position 'row' and 'col'.
8508 * The attributes of the first byte is used for all. This is required to
8509 * output the two bytes of a double-byte character with nothing in between.
8510 */
8511 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008512screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
8514 /* Check for illegal values (could be wrong when screen was resized). */
8515 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8516 return;
8517
8518 /* Outputting the last character on the screen may scrollup the screen.
8519 * Don't to it! Mark the character invalid (update it when scrolled up) */
8520 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8521 {
8522 ScreenAttrs[off] = (sattr_T)-1;
8523 return;
8524 }
8525
8526 /* Output the first byte normally (positions the cursor), then write the
8527 * second byte directly. */
8528 screen_char(off, row, col);
8529 out_char(ScreenLines[off + 1]);
8530 ++screen_cur_col;
8531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533/*
8534 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8535 * This uses the contents of ScreenLines[] and doesn't change it.
8536 */
8537 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008538screen_draw_rectangle(
8539 int row,
8540 int col,
8541 int height,
8542 int width,
8543 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 int r, c;
8546 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008547 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008549 /* Can't use ScreenLines unless initialized */
8550 if (ScreenLines == NULL)
8551 return;
8552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 if (invert)
8554 screen_char_attr = HL_INVERSE;
8555 for (r = row; r < row + height; ++r)
8556 {
8557 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008558 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 for (c = col; c < col + width; ++c)
8560 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008561 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
8563 screen_char_2(off + c, r, c);
8564 ++c;
8565 }
8566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
8568 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008569 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 }
8572 }
8573 }
8574 screen_char_attr = 0;
8575}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577/*
8578 * Redraw the characters for a vertically split window.
8579 */
8580 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008581redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 int col;
8584 int width;
8585
8586# ifdef FEAT_CLIPBOARD
8587 clip_may_clear_selection(row, end - 1);
8588# endif
8589
8590 if (wp == NULL)
8591 {
8592 col = 0;
8593 width = Columns;
8594 }
8595 else
8596 {
8597 col = wp->w_wincol;
8598 width = wp->w_width;
8599 }
8600 screen_draw_rectangle(row, col, end - row, width, FALSE);
8601}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008603 static void
8604space_to_screenline(int off, int attr)
8605{
8606 ScreenLines[off] = ' ';
8607 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008608 if (enc_utf8)
8609 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008610}
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612/*
8613 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8614 * with character 'c1' in first column followed by 'c2' in the other columns.
8615 * Use attributes 'attr'.
8616 */
8617 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008618screen_fill(
8619 int start_row,
8620 int end_row,
8621 int start_col,
8622 int end_col,
8623 int c1,
8624 int c2,
8625 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 int row;
8628 int col;
8629 int off;
8630 int end_off;
8631 int did_delete;
8632 int c;
8633 int norm_term;
8634#if defined(FEAT_GUI) || defined(UNIX)
8635 int force_next = FALSE;
8636#endif
8637
8638 if (end_row > screen_Rows) /* safety check */
8639 end_row = screen_Rows;
8640 if (end_col > screen_Columns) /* safety check */
8641 end_col = screen_Columns;
8642 if (ScreenLines == NULL
8643 || start_row >= end_row
8644 || start_col >= end_col) /* nothing to do */
8645 return;
8646
8647 /* it's a "normal" terminal when not in a GUI or cterm */
8648 norm_term = (
8649#ifdef FEAT_GUI
8650 !gui.in_use &&
8651#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008652 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 for (row = start_row; row < end_row; ++row)
8654 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008655 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008656#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008657 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008658#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008659 )
8660 {
8661 /* When drawing over the right halve of a double-wide char clear
8662 * out the left halve. When drawing over the left halve of a
8663 * double wide-char clear out the right halve. Only needed in a
8664 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008665 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008666 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008667 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008668 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 /*
8671 * Try to use delete-line termcap code, when no attributes or in a
8672 * "normal" terminal, where a bold/italic space is just a
8673 * space.
8674 */
8675 did_delete = FALSE;
8676 if (c2 == ' '
8677 && end_col == Columns
8678 && can_clear(T_CE)
8679 && (attr == 0
8680 || (norm_term
8681 && attr <= HL_ALL
8682 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8683 {
8684 /*
8685 * check if we really need to clear something
8686 */
8687 col = start_col;
8688 if (c1 != ' ') /* don't clear first char */
8689 ++col;
8690
8691 off = LineOffset[row] + col;
8692 end_off = LineOffset[row] + end_col;
8693
8694 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 if (enc_utf8)
8696 while (off < end_off && ScreenLines[off] == ' '
8697 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8698 ++off;
8699 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 while (off < end_off && ScreenLines[off] == ' '
8701 && ScreenAttrs[off] == 0)
8702 ++off;
8703 if (off < end_off) /* something to be cleared */
8704 {
8705 col = off - LineOffset[row];
8706 screen_stop_highlight();
8707 term_windgoto(row, col);/* clear rest of this screen line */
8708 out_str(T_CE);
8709 screen_start(); /* don't know where cursor is now */
8710 col = end_col - col;
8711 while (col--) /* clear chars in ScreenLines */
8712 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008713 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 ++off;
8715 }
8716 }
8717 did_delete = TRUE; /* the chars are cleared now */
8718 }
8719
8720 off = LineOffset[row] + start_col;
8721 c = c1;
8722 for (col = start_col; col < end_col; ++col)
8723 {
8724 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008725 || (enc_utf8 && (int)ScreenLinesUC[off]
8726 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 || ScreenAttrs[off] != attr
8728#if defined(FEAT_GUI) || defined(UNIX)
8729 || force_next
8730#endif
8731 )
8732 {
8733#if defined(FEAT_GUI) || defined(UNIX)
8734 /* The bold trick may make a single row of pixels appear in
8735 * the next character. When a bold character is removed, the
8736 * next character should be redrawn too. This happens for our
8737 * own GUI and for some xterms. */
8738 if (
8739# ifdef FEAT_GUI
8740 gui.in_use
8741# endif
8742# if defined(FEAT_GUI) && defined(UNIX)
8743 ||
8744# endif
8745# ifdef UNIX
8746 term_is_xterm
8747# endif
8748 )
8749 {
8750 if (ScreenLines[off] != ' '
8751 && (ScreenAttrs[off] > HL_ALL
8752 || ScreenAttrs[off] & HL_BOLD))
8753 force_next = TRUE;
8754 else
8755 force_next = FALSE;
8756 }
8757#endif
8758 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 if (enc_utf8)
8760 {
8761 if (c >= 0x80)
8762 {
8763 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008764 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 }
8766 else
8767 ScreenLinesUC[off] = 0;
8768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 ScreenAttrs[off] = attr;
8770 if (!did_delete || c != ' ')
8771 screen_char(off, row, col);
8772 }
8773 ++off;
8774 if (col == start_col)
8775 {
8776 if (did_delete)
8777 break;
8778 c = c2;
8779 }
8780 }
8781 if (end_col == Columns)
8782 LineWraps[row] = FALSE;
8783 if (row == Rows - 1) /* overwritten the command line */
8784 {
8785 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008786 if (start_col == 0 && end_col == Columns
8787 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008789 if (start_col == 0)
8790 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 }
8792 }
8793}
8794
8795/*
8796 * Check if there should be a delay. Used before clearing or redrawing the
8797 * screen or the command line.
8798 */
8799 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008800check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
8802 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8803 && !did_wait_return
8804 && emsg_silent == 0)
8805 {
8806 out_flush();
8807 ui_delay(1000L, TRUE);
8808 emsg_on_display = FALSE;
8809 if (check_msg_scroll)
8810 msg_scroll = FALSE;
8811 }
8812}
8813
8814/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008815 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8816 */
8817 static void
8818clear_TabPageIdxs(void)
8819{
8820 int scol;
8821
8822 for (scol = 0; scol < Columns; ++scol)
8823 TabPageIdxs[scol] = 0;
8824}
8825
8826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008828 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 * Returns TRUE if there is a valid screen to write to.
8830 * Returns FALSE when starting up and screen not initialized yet.
8831 */
8832 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008833screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008835 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 return (ScreenLines != NULL);
8837}
8838
8839/*
8840 * Resize the shell to Rows and Columns.
8841 * Allocate ScreenLines[] and associated items.
8842 *
8843 * There may be some time between setting Rows and Columns and (re)allocating
8844 * ScreenLines[]. This happens when starting up and when (manually) changing
8845 * the shell size. Always use screen_Rows and screen_Columns to access items
8846 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8847 * final size of the shell is needed.
8848 */
8849 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008850screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 int new_row, old_row;
8853#ifdef FEAT_GUI
8854 int old_Rows;
8855#endif
8856 win_T *wp;
8857 int outofmem = FALSE;
8858 int len;
8859 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008861 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008863 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 sattr_T *new_ScreenAttrs;
8865 unsigned *new_LineOffset;
8866 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008867 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008868 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008870 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008871 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008873retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 /*
8875 * Allocation of the screen buffers is done only when the size changes and
8876 * when Rows and Columns have been set and we have started doing full
8877 * screen stuff.
8878 */
8879 if ((ScreenLines != NULL
8880 && Rows == screen_Rows
8881 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 && enc_utf8 == (ScreenLinesUC != NULL)
8883 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008884 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 || Rows == 0
8886 || Columns == 0
8887 || (!full_screen && ScreenLines == NULL))
8888 return;
8889
8890 /*
8891 * It's possible that we produce an out-of-memory message below, which
8892 * will cause this function to be called again. To break the loop, just
8893 * return here.
8894 */
8895 if (entered)
8896 return;
8897 entered = TRUE;
8898
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008899 /*
8900 * Note that the window sizes are updated before reallocating the arrays,
8901 * thus we must not redraw here!
8902 */
8903 ++RedrawingDisabled;
8904
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 win_new_shellsize(); /* fit the windows in the new sized shell */
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 comp_col(); /* recompute columns for shown command and ruler */
8908
8909 /*
8910 * We're changing the size of the screen.
8911 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8912 * - Move lines from the old arrays into the new arrays, clear extra
8913 * lines (unless the screen is going to be cleared).
8914 * - Free the old arrays.
8915 *
8916 * If anything fails, make ScreenLines NULL, so we don't do anything!
8917 * Continuing with the old ScreenLines may result in a crash, because the
8918 * size is wrong.
8919 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008920 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008922 if (aucmd_win != NULL)
8923 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008925 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008926 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 if (enc_utf8)
8928 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008929 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008931 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8932 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
8934 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008935 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8936 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8937 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8938 new_LineWraps = LALLOC_MULT(char_u, Rows);
8939 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008941 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 {
8943 if (win_alloc_lines(wp) == FAIL)
8944 {
8945 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008946 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 }
8948 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008949 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8950 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008951 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008952give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008954 for (i = 0; i < p_mco; ++i)
8955 if (new_ScreenLinesC[i] == NULL)
8956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008958 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 || new_ScreenAttrs == NULL
8961 || new_LineOffset == NULL
8962 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008963 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 || outofmem)
8965 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008966 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008967 {
8968 /* guess the size */
8969 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8970
8971 /* Remember we did this to avoid getting outofmem messages over
8972 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008973 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008974 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008975 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008976 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008977 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008978 VIM_CLEAR(new_ScreenLinesC[i]);
8979 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008980 VIM_CLEAR(new_ScreenAttrs);
8981 VIM_CLEAR(new_LineOffset);
8982 VIM_CLEAR(new_LineWraps);
8983 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
8985 else
8986 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008987 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008988
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 for (new_row = 0; new_row < Rows; ++new_row)
8990 {
8991 new_LineOffset[new_row] = new_row * Columns;
8992 new_LineWraps[new_row] = FALSE;
8993
8994 /*
8995 * If the screen is not going to be cleared, copy as much as
8996 * possible from the old screen to the new one and clear the rest
8997 * (used when resizing the window at the "--more--" prompt or when
8998 * executing an external command, for the GUI).
8999 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009000 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
9002 (void)vim_memset(new_ScreenLines + new_row * Columns,
9003 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 if (enc_utf8)
9005 {
9006 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9007 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009008 for (i = 0; i < p_mco; ++i)
9009 (void)vim_memset(new_ScreenLinesC[i]
9010 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 0, (size_t)Columns * sizeof(u8char_T));
9012 }
9013 if (enc_dbcs == DBCS_JPNU)
9014 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9015 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9017 0, (size_t)Columns * sizeof(sattr_T));
9018 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009019 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 {
9021 if (screen_Columns < Columns)
9022 len = screen_Columns;
9023 else
9024 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009025 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009026 * may be invalid now. Also when p_mco changes. */
9027 if (!(enc_utf8 && ScreenLinesUC == NULL)
9028 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009029 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9030 ScreenLines + LineOffset[old_row],
9031 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009032 if (enc_utf8 && ScreenLinesUC != NULL
9033 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 {
9035 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9036 ScreenLinesUC + LineOffset[old_row],
9037 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009038 for (i = 0; i < p_mco; ++i)
9039 mch_memmove(new_ScreenLinesC[i]
9040 + new_LineOffset[new_row],
9041 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 (size_t)len * sizeof(u8char_T));
9043 }
9044 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9045 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9046 ScreenLines2 + LineOffset[old_row],
9047 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9049 ScreenAttrs + LineOffset[old_row],
9050 (size_t)len * sizeof(sattr_T));
9051 }
9052 }
9053 }
9054 /* Use the last line of the screen for the current line. */
9055 current_ScreenLine = new_ScreenLines + Rows * Columns;
9056 }
9057
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009058 free_screenlines();
9059
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009062 for (i = 0; i < p_mco; ++i)
9063 ScreenLinesC[i] = new_ScreenLinesC[i];
9064 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 ScreenAttrs = new_ScreenAttrs;
9067 LineOffset = new_LineOffset;
9068 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009069 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070
9071 /* It's important that screen_Rows and screen_Columns reflect the actual
9072 * size of ScreenLines[]. Set them before calling anything. */
9073#ifdef FEAT_GUI
9074 old_Rows = screen_Rows;
9075#endif
9076 screen_Rows = Rows;
9077 screen_Columns = Columns;
9078
9079 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009080 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082#ifdef FEAT_GUI
9083 else if (gui.in_use
9084 && !gui.starting
9085 && ScreenLines != NULL
9086 && old_Rows != Rows)
9087 {
9088 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9089 /*
9090 * Adjust the position of the cursor, for when executing an external
9091 * command.
9092 */
9093 if (msg_row >= Rows) /* Rows got smaller */
9094 msg_row = Rows - 1; /* put cursor at last row */
9095 else if (Rows > old_Rows) /* Rows got bigger */
9096 msg_row += Rows - old_Rows; /* put cursor in same place */
9097 if (msg_col >= Columns) /* Columns got smaller */
9098 msg_col = Columns - 1; /* put cursor at last column */
9099 }
9100#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009101 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009104 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009105
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009106 /*
9107 * Do not apply autocommands more than 3 times to avoid an endless loop
9108 * in case applying autocommands always changes Rows or Columns.
9109 */
9110 if (starting == 0 && ++retry_count <= 3)
9111 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009112 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009113 /* In rare cases, autocommands may have altered Rows or Columns,
9114 * jump back to check if we need to allocate the screen again. */
9115 goto retry;
9116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117}
9118
9119 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009120free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009121{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009122 int i;
9123
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009124 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009125 for (i = 0; i < Screen_mco; ++i)
9126 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009127 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009128 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009129 vim_free(ScreenAttrs);
9130 vim_free(LineOffset);
9131 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009132 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009133}
9134
9135 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009136screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 check_for_delay(FALSE);
9139 screenalloc(FALSE); /* allocate screen buffers if size changed */
9140 screenclear2(); /* clear the screen */
9141}
9142
9143 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009144screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 int i;
9147
9148 if (starting == NO_SCREEN || ScreenLines == NULL
9149#ifdef FEAT_GUI
9150 || (gui.in_use && gui.starting)
9151#endif
9152 )
9153 return;
9154
9155#ifdef FEAT_GUI
9156 if (!gui.in_use)
9157#endif
9158 screen_attr = -1; /* force setting the Normal colors */
9159 screen_stop_highlight(); /* don't want highlighting here */
9160
9161#ifdef FEAT_CLIPBOARD
9162 /* disable selection without redrawing it */
9163 clip_scroll_selection(9999);
9164#endif
9165
9166 /* blank out ScreenLines */
9167 for (i = 0; i < Rows; ++i)
9168 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009169 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 LineWraps[i] = FALSE;
9171 }
9172
9173 if (can_clear(T_CL))
9174 {
9175 out_str(T_CL); /* clear the display */
9176 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009177 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 }
9179 else
9180 {
9181 /* can't clear the screen, mark all chars with invalid attributes */
9182 for (i = 0; i < Rows; ++i)
9183 lineinvalid(LineOffset[i], (int)Columns);
9184 clear_cmdline = TRUE;
9185 }
9186
9187 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9188
9189 win_rest_invalid(firstwin);
9190 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009191 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 if (must_redraw == CLEAR) /* no need to clear again */
9193 must_redraw = NOT_VALID;
9194 compute_cmdrow();
9195 msg_row = cmdline_row; /* put cursor on last line for messages */
9196 msg_col = 0;
9197 screen_start(); /* don't know where cursor is now */
9198 msg_scrolled = 0; /* can't scroll back */
9199 msg_didany = FALSE;
9200 msg_didout = FALSE;
9201}
9202
9203/*
9204 * Clear one line in ScreenLines.
9205 */
9206 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009207lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 if (enc_utf8)
9211 (void)vim_memset(ScreenLinesUC + off, 0,
9212 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009213 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214}
9215
9216/*
9217 * Mark one line in ScreenLines invalid by setting the attributes to an
9218 * invalid value.
9219 */
9220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009221lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
9223 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9224}
9225
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226/*
9227 * Copy part of a Screenline for vertically split window "wp".
9228 */
9229 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009230linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 unsigned off_to = LineOffset[to] + wp->w_wincol;
9233 unsigned off_from = LineOffset[from] + wp->w_wincol;
9234
9235 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9236 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 if (enc_utf8)
9238 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009239 int i;
9240
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9242 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009243 for (i = 0; i < p_mco; ++i)
9244 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9245 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 }
9247 if (enc_dbcs == DBCS_JPNU)
9248 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9249 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9251 wp->w_width * sizeof(sattr_T));
9252}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253
9254/*
9255 * Return TRUE if clearing with term string "p" would work.
9256 * It can't work when the string is empty or it won't set the right background.
9257 */
9258 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009259can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
9261 return (*p != NUL && (t_colors <= 1
9262#ifdef FEAT_GUI
9263 || gui.in_use
9264#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009265#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009266 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009267 || (!p_tgc && cterm_normal_bg_color == 0)
9268#else
9269 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009270#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009271 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272}
9273
9274/*
9275 * Reset cursor position. Use whenever cursor was moved because of outputting
9276 * something directly to the screen (shell commands) or a terminal control
9277 * code.
9278 */
9279 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009280screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
9282 screen_cur_row = screen_cur_col = 9999;
9283}
9284
9285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 * Move the cursor to position "row","col" in the screen.
9287 * This tries to find the most efficient way to move, minimizing the number of
9288 * characters sent to the terminal.
9289 */
9290 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009291windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009293 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 int i;
9295 int plan;
9296 int cost;
9297 int wouldbe_col;
9298 int noinvcurs;
9299 char_u *bs;
9300 int goto_cost;
9301 int attr;
9302
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009303#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9305
9306#define PLAN_LE 1
9307#define PLAN_CR 2
9308#define PLAN_NL 3
9309#define PLAN_WRITE 4
9310 /* Can't use ScreenLines unless initialized */
9311 if (ScreenLines == NULL)
9312 return;
9313
9314 if (col != screen_cur_col || row != screen_cur_row)
9315 {
9316 /* Check for valid position. */
9317 if (row < 0) /* window without text lines? */
9318 row = 0;
9319 if (row >= screen_Rows)
9320 row = screen_Rows - 1;
9321 if (col >= screen_Columns)
9322 col = screen_Columns - 1;
9323
9324 /* check if no cursor movement is allowed in highlight mode */
9325 if (screen_attr && *T_MS == NUL)
9326 noinvcurs = HIGHL_COST;
9327 else
9328 noinvcurs = 0;
9329 goto_cost = GOTO_COST + noinvcurs;
9330
9331 /*
9332 * Plan how to do the positioning:
9333 * 1. Use CR to move it to column 0, same row.
9334 * 2. Use T_LE to move it a few columns to the left.
9335 * 3. Use NL to move a few lines down, column 0.
9336 * 4. Move a few columns to the right with T_ND or by writing chars.
9337 *
9338 * Don't do this if the cursor went beyond the last column, the cursor
9339 * position is unknown then (some terminals wrap, some don't )
9340 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009341 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 * characters to move the cursor to the right.
9343 */
9344 if (row >= screen_cur_row && screen_cur_col < Columns)
9345 {
9346 /*
9347 * If the cursor is in the same row, bigger col, we can use CR
9348 * or T_LE.
9349 */
9350 bs = NULL; /* init for GCC */
9351 attr = screen_attr;
9352 if (row == screen_cur_row && col < screen_cur_col)
9353 {
9354 /* "le" is preferred over "bc", because "bc" is obsolete */
9355 if (*T_LE)
9356 bs = T_LE; /* "cursor left" */
9357 else
9358 bs = T_BC; /* "backspace character (old) */
9359 if (*bs)
9360 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9361 else
9362 cost = 999;
9363 if (col + 1 < cost) /* using CR is less characters */
9364 {
9365 plan = PLAN_CR;
9366 wouldbe_col = 0;
9367 cost = 1; /* CR is just one character */
9368 }
9369 else
9370 {
9371 plan = PLAN_LE;
9372 wouldbe_col = col;
9373 }
9374 if (noinvcurs) /* will stop highlighting */
9375 {
9376 cost += noinvcurs;
9377 attr = 0;
9378 }
9379 }
9380
9381 /*
9382 * If the cursor is above where we want to be, we can use CR LF.
9383 */
9384 else if (row > screen_cur_row)
9385 {
9386 plan = PLAN_NL;
9387 wouldbe_col = 0;
9388 cost = (row - screen_cur_row) * 2; /* CR LF */
9389 if (noinvcurs) /* will stop highlighting */
9390 {
9391 cost += noinvcurs;
9392 attr = 0;
9393 }
9394 }
9395
9396 /*
9397 * If the cursor is in the same row, smaller col, just use write.
9398 */
9399 else
9400 {
9401 plan = PLAN_WRITE;
9402 wouldbe_col = screen_cur_col;
9403 cost = 0;
9404 }
9405
9406 /*
9407 * Check if any characters that need to be written have the
9408 * correct attributes. Also avoid UTF-8 characters.
9409 */
9410 i = col - wouldbe_col;
9411 if (i > 0)
9412 cost += i;
9413 if (cost < goto_cost && i > 0)
9414 {
9415 /*
9416 * Check if the attributes are correct without additionally
9417 * stopping highlighting.
9418 */
9419 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9420 while (i && *p++ == attr)
9421 --i;
9422 if (i != 0)
9423 {
9424 /*
9425 * Try if it works when highlighting is stopped here.
9426 */
9427 if (*--p == 0)
9428 {
9429 cost += noinvcurs;
9430 while (i && *p++ == 0)
9431 --i;
9432 }
9433 if (i != 0)
9434 cost = 999; /* different attributes, don't do it */
9435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 if (enc_utf8)
9437 {
9438 /* Don't use an UTF-8 char for positioning, it's slow. */
9439 for (i = wouldbe_col; i < col; ++i)
9440 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9441 {
9442 cost = 999;
9443 break;
9444 }
9445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 }
9447
9448 /*
9449 * We can do it without term_windgoto()!
9450 */
9451 if (cost < goto_cost)
9452 {
9453 if (plan == PLAN_LE)
9454 {
9455 if (noinvcurs)
9456 screen_stop_highlight();
9457 while (screen_cur_col > col)
9458 {
9459 out_str(bs);
9460 --screen_cur_col;
9461 }
9462 }
9463 else if (plan == PLAN_CR)
9464 {
9465 if (noinvcurs)
9466 screen_stop_highlight();
9467 out_char('\r');
9468 screen_cur_col = 0;
9469 }
9470 else if (plan == PLAN_NL)
9471 {
9472 if (noinvcurs)
9473 screen_stop_highlight();
9474 while (screen_cur_row < row)
9475 {
9476 out_char('\n');
9477 ++screen_cur_row;
9478 }
9479 screen_cur_col = 0;
9480 }
9481
9482 i = col - screen_cur_col;
9483 if (i > 0)
9484 {
9485 /*
9486 * Use cursor-right if it's one character only. Avoids
9487 * removing a line of pixels from the last bold char, when
9488 * using the bold trick in the GUI.
9489 */
9490 if (T_ND[0] != NUL && T_ND[1] == NUL)
9491 {
9492 while (i-- > 0)
9493 out_char(*T_ND);
9494 }
9495 else
9496 {
9497 int off;
9498
9499 off = LineOffset[row] + screen_cur_col;
9500 while (i-- > 0)
9501 {
9502 if (ScreenAttrs[off] != screen_attr)
9503 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (enc_dbcs == DBCS_JPNU
9507 && ScreenLines[off] == 0x8e)
9508 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 ++off;
9510 }
9511 }
9512 }
9513 }
9514 }
9515 else
9516 cost = 999;
9517
9518 if (cost >= goto_cost)
9519 {
9520 if (noinvcurs)
9521 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009522 if (row == screen_cur_row && (col > screen_cur_col)
9523 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 term_cursor_right(col - screen_cur_col);
9525 else
9526 term_windgoto(row, col);
9527 }
9528 screen_cur_row = row;
9529 screen_cur_col = col;
9530 }
9531}
9532
9533/*
9534 * Set cursor to its position in the current window.
9535 */
9536 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009537setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009539 setcursor_mayforce(FALSE);
9540}
9541
9542/*
9543 * Set cursor to its position in the current window.
9544 * When "force" is TRUE also when not redrawing.
9545 */
9546 void
9547setcursor_mayforce(int force)
9548{
9549 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 {
9551 validate_cursor();
9552 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009553 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009555 /* With 'rightleft' set and the cursor on a double-wide
9556 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009557 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9558 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009559 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009560 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#endif
9562 curwin->w_wcol));
9563 }
9564}
9565
9566
9567/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009568 * Insert 'line_count' lines at 'row' in window 'wp'.
9569 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9570 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 * scrolling.
9572 * Returns FAIL if the lines are not inserted, OK for success.
9573 */
9574 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009575win_ins_lines(
9576 win_T *wp,
9577 int row,
9578 int line_count,
9579 int invalid,
9580 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
9582 int did_delete;
9583 int nextrow;
9584 int lastrow;
9585 int retval;
9586
9587 if (invalid)
9588 wp->w_lines_valid = 0;
9589
9590 if (wp->w_height < 5)
9591 return FAIL;
9592
9593 if (line_count > wp->w_height - row)
9594 line_count = wp->w_height - row;
9595
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009596 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 if (retval != MAYBE)
9598 return retval;
9599
9600 /*
9601 * If there is a next window or a status line, we first try to delete the
9602 * lines at the bottom to avoid messing what is after the window.
9603 * If this fails and there are following windows, don't do anything to avoid
9604 * messing up those windows, better just redraw.
9605 */
9606 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 if (wp->w_next != NULL || wp->w_status_height)
9608 {
9609 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009610 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 did_delete = TRUE;
9612 else if (wp->w_next)
9613 return FAIL;
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 /*
9616 * if no lines deleted, blank the lines that will end up below the window
9617 */
9618 if (!did_delete)
9619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009622 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 lastrow = nextrow + line_count;
9624 if (lastrow > Rows)
9625 lastrow = Rows;
9626 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009627 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 ' ', ' ', 0);
9629 }
9630
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009631 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 == FAIL)
9633 {
9634 /* deletion will have messed up other windows */
9635 if (did_delete)
9636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 win_rest_invalid(W_NEXT(wp));
9639 }
9640 return FAIL;
9641 }
9642
9643 return OK;
9644}
9645
9646/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009647 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9649 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9650 * scrolling
9651 * Return OK for success, FAIL if the lines are not deleted.
9652 */
9653 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009654win_del_lines(
9655 win_T *wp,
9656 int row,
9657 int line_count,
9658 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009659 int mayclear,
9660 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661{
9662 int retval;
9663
9664 if (invalid)
9665 wp->w_lines_valid = 0;
9666
9667 if (line_count > wp->w_height - row)
9668 line_count = wp->w_height - row;
9669
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009670 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 if (retval != MAYBE)
9672 return retval;
9673
9674 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009675 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 return FAIL;
9677
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 /*
9679 * If there are windows or status lines below, try to put them at the
9680 * correct place. If we can't do that, they have to be redrawn.
9681 */
9682 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9683 {
9684 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009685 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 {
9687 wp->w_redr_status = TRUE;
9688 win_rest_invalid(wp->w_next);
9689 }
9690 }
9691 /*
9692 * If this is the last window and there is no status line, redraw the
9693 * command line later.
9694 */
9695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 redraw_cmdline = TRUE;
9697 return OK;
9698}
9699
9700/*
9701 * Common code for win_ins_lines() and win_del_lines().
9702 * Returns OK or FAIL when the work has been done.
9703 * Returns MAYBE when not finished yet.
9704 */
9705 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009706win_do_lines(
9707 win_T *wp,
9708 int row,
9709 int line_count,
9710 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009711 int del,
9712 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
9714 int retval;
9715
9716 if (!redrawing() || line_count <= 0)
9717 return FAIL;
9718
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009719 /* When inserting lines would result in loss of command output, just redraw
9720 * the lines. */
9721 if (no_win_do_lines_ins && !del)
9722 return FAIL;
9723
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009725 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009727 if (!no_win_do_lines_ins)
9728 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 return FAIL;
9730 }
9731
9732 /*
9733 * Delete all remaining lines
9734 */
9735 if (row + line_count >= wp->w_height)
9736 {
9737 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009738 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 ' ', ' ', 0);
9740 return OK;
9741 }
9742
9743 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009744 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009746 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009748 if (!no_win_do_lines_ins)
9749 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750
9751 /*
9752 * If the terminal can set a scroll region, use that.
9753 * Always do this in a vertically split window. This will redraw from
9754 * ScreenLines[] when t_CV isn't defined. That's faster than using
9755 * win_line().
9756 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009757 * a character in the lower right corner of the scroll region may cause a
9758 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009760 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 scroll_region_set(wp, row);
9764 if (del)
9765 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009766 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 else
9768 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009769 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 scroll_region_reset();
9772 return retval;
9773 }
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9776 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777
9778 return MAYBE;
9779}
9780
9781/*
9782 * window 'wp' and everything after it is messed up, mark it for redraw
9783 */
9784 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009785win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
9789 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 wp->w_redr_status = TRUE;
9791 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 }
9793 redraw_cmdline = TRUE;
9794}
9795
9796/*
9797 * The rest of the routines in this file perform screen manipulations. The
9798 * given operation is performed physically on the screen. The corresponding
9799 * change is also made to the internal screen image. In this way, the editor
9800 * anticipates the effect of editing changes on the appearance of the screen.
9801 * That way, when we call screenupdate a complete redraw isn't usually
9802 * necessary. Another advantage is that we can keep adding code to anticipate
9803 * screen changes, and in the meantime, everything still works.
9804 */
9805
9806/*
9807 * types for inserting or deleting lines
9808 */
9809#define USE_T_CAL 1
9810#define USE_T_CDL 2
9811#define USE_T_AL 3
9812#define USE_T_CE 4
9813#define USE_T_DL 5
9814#define USE_T_SR 6
9815#define USE_NL 7
9816#define USE_T_CD 8
9817#define USE_REDRAW 9
9818
9819/*
9820 * insert lines on the screen and update ScreenLines[]
9821 * 'end' is the line after the scrolled part. Normally it is Rows.
9822 * When scrolling region used 'off' is the offset from the top for the region.
9823 * 'row' and 'end' are relative to the start of the region.
9824 *
9825 * return FAIL for failure, OK for success.
9826 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009827 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009828screen_ins_lines(
9829 int off,
9830 int row,
9831 int line_count,
9832 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009833 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009834 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835{
9836 int i;
9837 int j;
9838 unsigned temp;
9839 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009840 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 int type;
9842 int result_empty;
9843 int can_ce = can_clear(T_CE);
9844
9845 /*
9846 * FAIL if
9847 * - there is no valid screen
9848 * - the screen has to be redrawn completely
9849 * - the line count is less than one
9850 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009851 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009853 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9854#ifdef FEAT_CLIPBOARD
9855 || (clip_star.state != SELECT_CLEARED
9856 && redrawing_for_callback > 0)
9857#endif
9858 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 return FAIL;
9860
9861 /*
9862 * There are seven ways to insert lines:
9863 * 0. When in a vertically split window and t_CV isn't set, redraw the
9864 * characters from ScreenLines[].
9865 * 1. Use T_CD (clear to end of display) if it exists and the result of
9866 * the insert is just empty lines
9867 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9868 * present or line_count > 1. It looks better if we do all the inserts
9869 * at once.
9870 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9871 * insert is just empty lines and T_CE is not present or line_count >
9872 * 1.
9873 * 4. Use T_AL (insert line) if it exists.
9874 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9875 * just empty lines.
9876 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9877 * just empty lines.
9878 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9879 * the 'da' flag is not set or we have clear line capability.
9880 * 8. redraw the characters from ScreenLines[].
9881 *
9882 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9883 * the scrollbar for the window. It does have insert line, use that if it
9884 * exists.
9885 */
9886 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9888 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009889 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 type = USE_T_CD;
9891 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9892 type = USE_T_CAL;
9893 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9894 type = USE_T_CDL;
9895 else if (*T_AL != NUL)
9896 type = USE_T_AL;
9897 else if (can_ce && result_empty)
9898 type = USE_T_CE;
9899 else if (*T_DL != NUL && result_empty)
9900 type = USE_T_DL;
9901 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9902 type = USE_T_SR;
9903 else
9904 return FAIL;
9905
9906 /*
9907 * For clearing the lines screen_del_lines() is used. This will also take
9908 * care of t_db if necessary.
9909 */
9910 if (type == USE_T_CD || type == USE_T_CDL ||
9911 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009912 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 /*
9915 * If text is retained below the screen, first clear or delete as many
9916 * lines at the bottom of the window as are about to be inserted so that
9917 * the deleted lines won't later surface during a screen_del_lines.
9918 */
9919 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009920 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921
9922#ifdef FEAT_CLIPBOARD
9923 /* Remove a modeless selection when inserting lines halfway the screen
9924 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009925 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009926 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 else
9928 clip_scroll_selection(-line_count);
9929#endif
9930
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#ifdef FEAT_GUI
9932 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9933 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009934 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#endif
9936
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009937 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9938 cursor_col = wp->w_wincol;
9939
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 if (*T_CCS != NUL) /* cursor relative to region */
9941 cursor_row = row;
9942 else
9943 cursor_row = row + off;
9944
9945 /*
9946 * Shift LineOffset[] line_count down to reflect the inserted lines.
9947 * Clear the inserted lines in ScreenLines[].
9948 */
9949 row += off;
9950 end += off;
9951 for (i = 0; i < line_count; ++i)
9952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 if (wp != NULL && wp->w_width != Columns)
9954 {
9955 /* need to copy part of a line */
9956 j = end - 1 - i;
9957 while ((j -= line_count) >= row)
9958 linecopy(j + line_count, j, wp);
9959 j += line_count;
9960 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009961 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9962 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 else
9964 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9965 LineWraps[j] = FALSE;
9966 }
9967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 {
9969 j = end - 1 - i;
9970 temp = LineOffset[j];
9971 while ((j -= line_count) >= row)
9972 {
9973 LineOffset[j + line_count] = LineOffset[j];
9974 LineWraps[j + line_count] = LineWraps[j];
9975 }
9976 LineOffset[j + line_count] = temp;
9977 LineWraps[j + line_count] = FALSE;
9978 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009979 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 else
9981 lineinvalid(temp, (int)Columns);
9982 }
9983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984
9985 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009986 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009987 if (clear_attr != 0)
9988 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 /* redraw the characters */
9991 if (type == USE_REDRAW)
9992 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009993 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 {
9995 term_append_lines(line_count);
9996 screen_start(); /* don't know where cursor is now */
9997 }
9998 else
9999 {
10000 for (i = 0; i < line_count; i++)
10001 {
10002 if (type == USE_T_AL)
10003 {
10004 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010005 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 out_str(T_AL);
10007 }
10008 else /* type == USE_T_SR */
10009 out_str(T_SR);
10010 screen_start(); /* don't know where cursor is now */
10011 }
10012 }
10013
10014 /*
10015 * With scroll-reverse and 'da' flag set we need to clear the lines that
10016 * have been scrolled down into the region.
10017 */
10018 if (type == USE_T_SR && *T_DA)
10019 {
10020 for (i = 0; i < line_count; ++i)
10021 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010022 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 out_str(T_CE);
10024 screen_start(); /* don't know where cursor is now */
10025 }
10026 }
10027
10028#ifdef FEAT_GUI
10029 gui_can_update_cursor();
10030 if (gui.in_use)
10031 out_flush(); /* always flush after a scroll */
10032#endif
10033 return OK;
10034}
10035
10036/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010037 * Delete lines on the screen and update ScreenLines[].
10038 * "end" is the line after the scrolled part. Normally it is Rows.
10039 * When scrolling region used "off" is the offset from the top for the region.
10040 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 *
10042 * Return OK for success, FAIL if the lines are not deleted.
10043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010045screen_del_lines(
10046 int off,
10047 int row,
10048 int line_count,
10049 int end,
10050 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010051 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010052 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053{
10054 int j;
10055 int i;
10056 unsigned temp;
10057 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010058 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 int cursor_end;
10060 int result_empty; /* result is empty until end of region */
10061 int can_delete; /* deleting line codes can be used */
10062 int type;
10063
10064 /*
10065 * FAIL if
10066 * - there is no valid screen
10067 * - the screen has to be redrawn completely
10068 * - the line count is less than one
10069 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010070 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010072 if (!screen_valid(TRUE) || line_count <= 0
10073 || (!force && line_count > p_ttyscroll)
10074#ifdef FEAT_CLIPBOARD
10075 || (clip_star.state != SELECT_CLEARED
10076 && redrawing_for_callback > 0)
10077#endif
10078 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 return FAIL;
10080
10081 /*
10082 * Check if the rest of the current region will become empty.
10083 */
10084 result_empty = row + line_count >= end;
10085
10086 /*
10087 * We can delete lines only when 'db' flag not set or when 'ce' option
10088 * available.
10089 */
10090 can_delete = (*T_DB == NUL || can_clear(T_CE));
10091
10092 /*
10093 * There are six ways to delete lines:
10094 * 0. When in a vertically split window and t_CV isn't set, redraw the
10095 * characters from ScreenLines[].
10096 * 1. Use T_CD if it exists and the result is empty.
10097 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10098 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10099 * none of the other ways work.
10100 * 4. Use T_CE (erase line) if the result is empty.
10101 * 5. Use T_DL (delete line) if it exists.
10102 * 6. redraw the characters from ScreenLines[].
10103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10105 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010106 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 type = USE_T_CD;
10108#if defined(__BEOS__) && defined(BEOS_DR8)
10109 /*
10110 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10111 * its internal termcap... this works okay for tests which test *T_DB !=
10112 * NUL. It has the disadvantage that the user cannot use any :set t_*
10113 * command to get T_DB (back) to empty_option, only :set term=... will do
10114 * the trick...
10115 * Anyway, this hack will hopefully go away with the next OS release.
10116 * (Olaf Seibert)
10117 */
10118 else if (row == 0 && T_DB == empty_option
10119 && (line_count == 1 || *T_CDL == NUL))
10120#else
10121 else if (row == 0 && (
10122#ifndef AMIGA
10123 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10124 * up, so use delete-line command */
10125 line_count == 1 ||
10126#endif
10127 *T_CDL == NUL))
10128#endif
10129 type = USE_NL;
10130 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10131 type = USE_T_CDL;
10132 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010133 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 type = USE_T_CE;
10135 else if (*T_DL != NUL && can_delete)
10136 type = USE_T_DL;
10137 else if (*T_CDL != NUL && can_delete)
10138 type = USE_T_CDL;
10139 else
10140 return FAIL;
10141
10142#ifdef FEAT_CLIPBOARD
10143 /* Remove a modeless selection when deleting lines halfway the screen or
10144 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010145 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010146 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 else
10148 clip_scroll_selection(line_count);
10149#endif
10150
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#ifdef FEAT_GUI
10152 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10153 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010154 gui_dont_update_cursor(gui.cursor_row >= row + off
10155 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156#endif
10157
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010158 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10159 cursor_col = wp->w_wincol;
10160
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 if (*T_CCS != NUL) /* cursor relative to region */
10162 {
10163 cursor_row = row;
10164 cursor_end = end;
10165 }
10166 else
10167 {
10168 cursor_row = row + off;
10169 cursor_end = end + off;
10170 }
10171
10172 /*
10173 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10174 * Clear the inserted lines in ScreenLines[].
10175 */
10176 row += off;
10177 end += off;
10178 for (i = 0; i < line_count; ++i)
10179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (wp != NULL && wp->w_width != Columns)
10181 {
10182 /* need to copy part of a line */
10183 j = row + i;
10184 while ((j += line_count) <= end - 1)
10185 linecopy(j - line_count, j, wp);
10186 j -= line_count;
10187 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010188 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10189 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 else
10191 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10192 LineWraps[j] = FALSE;
10193 }
10194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 {
10196 /* whole width, moving the line pointers is faster */
10197 j = row + i;
10198 temp = LineOffset[j];
10199 while ((j += line_count) <= end - 1)
10200 {
10201 LineOffset[j - line_count] = LineOffset[j];
10202 LineWraps[j - line_count] = LineWraps[j];
10203 }
10204 LineOffset[j - line_count] = temp;
10205 LineWraps[j - line_count] = FALSE;
10206 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010207 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 else
10209 lineinvalid(temp, (int)Columns);
10210 }
10211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010213 if (screen_attr != clear_attr)
10214 screen_stop_highlight();
10215 if (clear_attr != 0)
10216 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 /* redraw the characters */
10219 if (type == USE_REDRAW)
10220 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010221 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010223 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 out_str(T_CD);
10225 screen_start(); /* don't know where cursor is now */
10226 }
10227 else if (type == USE_T_CDL)
10228 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010229 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 term_delete_lines(line_count);
10231 screen_start(); /* don't know where cursor is now */
10232 }
10233 /*
10234 * Deleting lines at top of the screen or scroll region: Just scroll
10235 * the whole screen (scroll region) up by outputting newlines on the
10236 * last line.
10237 */
10238 else if (type == USE_NL)
10239 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010240 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 for (i = line_count; --i >= 0; )
10242 out_char('\n'); /* cursor will remain on same line */
10243 }
10244 else
10245 {
10246 for (i = line_count; --i >= 0; )
10247 {
10248 if (type == USE_T_DL)
10249 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010250 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 out_str(T_DL); /* delete a line */
10252 }
10253 else /* type == USE_T_CE */
10254 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010255 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 out_str(T_CE); /* erase a line */
10257 }
10258 screen_start(); /* don't know where cursor is now */
10259 }
10260 }
10261
10262 /*
10263 * If the 'db' flag is set, we need to clear the lines that have been
10264 * scrolled up at the bottom of the region.
10265 */
10266 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10267 {
10268 for (i = line_count; i > 0; --i)
10269 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010270 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271 out_str(T_CE); /* erase a line */
10272 screen_start(); /* don't know where cursor is now */
10273 }
10274 }
10275
10276#ifdef FEAT_GUI
10277 gui_can_update_cursor();
10278 if (gui.in_use)
10279 out_flush(); /* always flush after a scroll */
10280#endif
10281
10282 return OK;
10283}
10284
10285/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010286 * Return TRUE when postponing displaying the mode message: when not redrawing
10287 * or inside a mapping.
10288 */
10289 int
10290skip_showmode()
10291{
10292 // Call char_avail() only when we are going to show something, because it
10293 // takes a bit of time. redrawing() may also call char_avail_avail().
10294 if (global_busy
10295 || msg_silent != 0
10296 || !redrawing()
10297 || (char_avail() && !KeyTyped))
10298 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010299 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010300 return TRUE;
10301 }
10302 return FALSE;
10303}
10304
10305/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010306 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 *
10308 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10309 * If clear_cmdline is FALSE there may be a message there that needs to be
10310 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010311 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 * Return the length of the message (0 if no message).
10313 */
10314 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010315showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
10317 int need_clear;
10318 int length = 0;
10319 int do_mode;
10320 int attr;
10321 int nwr_save;
10322#ifdef FEAT_INS_EXPAND
10323 int sub_attr;
10324#endif
10325
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010326 do_mode = ((p_smd && msg_silent == 0)
10327 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010328 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010329 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010330 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010332 if (skip_showmode())
10333 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334
10335 nwr_save = need_wait_return;
10336
10337 /* wait a bit before overwriting an important message */
10338 check_for_delay(FALSE);
10339
10340 /* if the cmdline is more than one line high, erase top lines */
10341 need_clear = clear_cmdline;
10342 if (clear_cmdline && cmdline_row < Rows - 1)
10343 msg_clr_cmdline(); /* will reset clear_cmdline */
10344
10345 /* Position on the last line in the window, column 0 */
10346 msg_pos_mode();
10347 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010348 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 if (do_mode)
10350 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010351 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010353 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010354# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010355 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010356# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010357 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010358# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010359 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010360# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010361 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010363 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364# endif
10365#endif
10366#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10367 if (gui.in_use)
10368 {
10369 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010370 {
10371 /* HANGUL */
10372 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010373 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010374 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010375 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 }
10378#endif
10379#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010380 /* CTRL-X in Insert mode */
10381 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
10383 /* These messages can get long, avoid a wrap in a narrow
10384 * window. Prefer showing edit_submode_extra. */
10385 length = (Rows - msg_row) * Columns - 3;
10386 if (edit_submode_extra != NULL)
10387 length -= vim_strsize(edit_submode_extra);
10388 if (length > 0)
10389 {
10390 if (edit_submode_pre != NULL)
10391 length -= vim_strsize(edit_submode_pre);
10392 if (length - vim_strsize(edit_submode) > 0)
10393 {
10394 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010395 msg_puts_attr((char *)edit_submode_pre, attr);
10396 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 }
10398 if (edit_submode_extra != NULL)
10399 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010400 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010402 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 else
10404 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010405 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
10407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 }
10409 else
10410#endif
10411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010413 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010414 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010415 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 else if (State & INSERT)
10417 {
10418#ifdef FEAT_RIGHTLEFT
10419 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010420 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010422 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010424 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010425 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010427 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010429 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#ifdef FEAT_RIGHTLEFT
10431 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010432 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433#endif
10434#ifdef FEAT_KEYMAP
10435 if (State & LANGMAP)
10436 {
10437# ifdef FEAT_ARABIC
10438 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010439 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 else
10441# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010442 if (get_keymap_str(curwin, (char_u *)" (%s)",
10443 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010444 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 }
10446#endif
10447 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010448 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 if (VIsual_active)
10451 {
10452 char *p;
10453
10454 /* Don't concatenate separate words to avoid translation
10455 * problems. */
10456 switch ((VIsual_select ? 4 : 0)
10457 + (VIsual_mode == Ctrl_V) * 2
10458 + (VIsual_mode == 'V'))
10459 {
10460 case 0: p = N_(" VISUAL"); break;
10461 case 1: p = N_(" VISUAL LINE"); break;
10462 case 2: p = N_(" VISUAL BLOCK"); break;
10463 case 4: p = N_(" SELECT"); break;
10464 case 5: p = N_(" SELECT LINE"); break;
10465 default: p = N_(" SELECT BLOCK"); break;
10466 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010467 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010469 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010471
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 need_clear = TRUE;
10473 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010474 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475#ifdef FEAT_INS_EXPAND
10476 && edit_submode == NULL /* otherwise it gets too long */
10477#endif
10478 )
10479 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010480 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 need_clear = TRUE;
10482 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010483
10484 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010485 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 msg_clr_eos();
10487 msg_didout = FALSE; /* overwrite this message */
10488 length = msg_col;
10489 msg_col = 0;
10490 need_wait_return = nwr_save; /* never ask for hit-return for this */
10491 }
10492 else if (clear_cmdline && msg_silent == 0)
10493 /* Clear the whole command line. Will reset "clear_cmdline". */
10494 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010495 else if (redraw_mode)
10496 {
10497 msg_pos_mode();
10498 msg_clr_eos();
10499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500
10501#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 /* In Visual mode the size of the selected area must be redrawn. */
10503 if (VIsual_active)
10504 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505
10506 /* If the last window has no status line, the ruler is after the mode
10507 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010508 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010509 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510#endif
10511 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010512 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 clear_cmdline = FALSE;
10514
10515 return length;
10516}
10517
10518/*
10519 * Position for a mode message.
10520 */
10521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010522msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524 msg_col = 0;
10525 msg_row = Rows - 1;
10526}
10527
10528/*
10529 * Delete mode message. Used when ESC is typed which is expected to end
10530 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010531 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 */
10533 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010534unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
10536 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010537 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 */
10539 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10540 redraw_cmdline = TRUE; /* delete mode later */
10541 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010542 clearmode();
10543}
10544
10545/*
10546 * Clear the mode message.
10547 */
10548 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010549clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010550{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010551 int save_msg_row = msg_row;
10552 int save_msg_col = msg_col;
10553
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010554 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010555 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010556 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010557 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010558
10559 msg_col = save_msg_col;
10560 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561}
10562
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010563 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010564recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010565{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010566 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010567 if (!shortmess(SHM_RECORDING))
10568 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010569 char s[4];
10570
10571 sprintf(s, " @%c", reg_recording);
10572 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010573 }
10574}
10575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010576/*
10577 * Draw the tab pages line at the top of the Vim window.
10578 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010579 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010580draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010581{
10582 int tabcount = 0;
10583 tabpage_T *tp;
10584 int tabwidth;
10585 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010586 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010587 int attr;
10588 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010589 win_T *cwp;
10590 int wincount;
10591 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010592 int c;
10593 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010594 int attr_sel = HL_ATTR(HLF_TPS);
10595 int attr_nosel = HL_ATTR(HLF_TP);
10596 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010597 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010598 int room;
10599 int use_sep_chars = (t_colors < 8
10600#ifdef FEAT_GUI
10601 && !gui.in_use
10602#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010603#ifdef FEAT_TERMGUICOLORS
10604 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010605#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010606 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010607
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010608 if (ScreenLines == NULL)
10609 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010610 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010611
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010612#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010613 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010614 if (gui_use_tabline())
10615 {
10616 gui_update_tabline();
10617 return;
10618 }
10619#endif
10620
10621 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010622 return;
10623
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010624#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010625 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010626
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010627 /* Use the 'tabline' option if it's set. */
10628 if (*p_tal != NUL)
10629 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010630 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010631
10632 /* Check for an error. If there is one we would loop in redrawing the
10633 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010634 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010635 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010636 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010637 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010638 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010639 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010640 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010642#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010643 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010644 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010645 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010646
Bram Moolenaar238a5642006-02-21 22:12:05 +000010647 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10648 if (tabwidth < 6)
10649 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010650
Bram Moolenaar238a5642006-02-21 22:12:05 +000010651 attr = attr_nosel;
10652 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010653 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10654 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010655 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010656 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010657
Bram Moolenaar238a5642006-02-21 22:12:05 +000010658 if (tp->tp_topframe == topframe)
10659 attr = attr_sel;
10660 if (use_sep_chars && col > 0)
10661 screen_putchar('|', 0, col++, attr);
10662
10663 if (tp->tp_topframe != topframe)
10664 attr = attr_nosel;
10665
10666 screen_putchar(' ', 0, col++, attr);
10667
10668 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010669 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010670 cwp = curwin;
10671 wp = firstwin;
10672 }
10673 else
10674 {
10675 cwp = tp->tp_curwin;
10676 wp = tp->tp_firstwin;
10677 }
10678
10679 modified = FALSE;
10680 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10681 if (bufIsChanged(wp->w_buffer))
10682 modified = TRUE;
10683 if (modified || wincount > 1)
10684 {
10685 if (wincount > 1)
10686 {
10687 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010688 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010689 if (col + len >= Columns - 3)
10690 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010691 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010692#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010693 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010694#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010695 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010696#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010697 );
10698 col += len;
10699 }
10700 if (modified)
10701 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10702 screen_putchar(' ', 0, col++, attr);
10703 }
10704
10705 room = scol - col + tabwidth - 1;
10706 if (room > 0)
10707 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010708 /* Get buffer name in NameBuff[] */
10709 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010710 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010711 len = vim_strsize(NameBuff);
10712 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010713 if (has_mbyte)
10714 while (len > room)
10715 {
10716 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010717 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010718 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010719 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010720 {
10721 p += len - room;
10722 len = room;
10723 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010724 if (len > Columns - col - 1)
10725 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010726
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010727 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010728 col += len;
10729 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010730 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010731
10732 /* Store the tab page number in TabPageIdxs[], so that
10733 * jump_to_mouse() knows where each one is. */
10734 ++tabcount;
10735 while (scol < col)
10736 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010737 }
10738
Bram Moolenaar238a5642006-02-21 22:12:05 +000010739 if (use_sep_chars)
10740 c = '_';
10741 else
10742 c = ' ';
10743 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010744
10745 /* Put an "X" for closing the current tab if there are several. */
10746 if (first_tabpage->tp_next != NULL)
10747 {
10748 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10749 TabPageIdxs[Columns - 1] = -999;
10750 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010752
10753 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10754 * set. */
10755 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010756}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010757
10758/*
10759 * Get buffer name for "buf" into NameBuff[].
10760 * Takes care of special buffer names and translates special characters.
10761 */
10762 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010763get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010764{
10765 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010766 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010767 else
10768 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10769 trans_characters(NameBuff, MAXPATHL);
10770}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010771
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772/*
10773 * Get the character to use in a status line. Get its attributes in "*attr".
10774 */
10775 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010776fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777{
10778 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010779
10780#ifdef FEAT_TERMINAL
10781 if (bt_terminal(wp->w_buffer))
10782 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010783 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010784 {
10785 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010786 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010787 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010788 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010789 {
10790 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010791 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010792 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010793 }
10794 else
10795#endif
10796 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010798 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 fill = fill_stl;
10800 }
10801 else
10802 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010803 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 fill = fill_stlnc;
10805 }
10806 /* Use fill when there is highlighting, and highlighting of current
10807 * window differs, or the fillchars differ, or this is not the
10808 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010809 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010810 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 || (fill_stl != fill_stlnc)))
10812 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010813 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 return '^';
10815 return '=';
10816}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818/*
10819 * Get the character to use in a separator between vertically split windows.
10820 * Get its attributes in "*attr".
10821 */
10822 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010823fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010825 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 if (*attr == 0 && fill_vert == ' ')
10827 return '|';
10828 else
10829 return fill_vert;
10830}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831
10832/*
10833 * Return TRUE if redrawing should currently be done.
10834 */
10835 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010836redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010838#ifdef FEAT_EVAL
10839 if (disable_redraw_for_testing)
10840 return 0;
10841 else
10842#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010843 return ((!RedrawingDisabled
10844#ifdef FEAT_EVAL
10845 || ignore_redraw_flag_for_testing
10846#endif
10847 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848}
10849
10850/*
10851 * Return TRUE if printing messages should currently be done.
10852 */
10853 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010854messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855{
10856 return (!(p_lz && char_avail() && !KeyTyped));
10857}
10858
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010859#ifdef FEAT_MENU
10860/*
10861 * Draw the window toolbar.
10862 */
10863 static void
10864redraw_win_toolbar(win_T *wp)
10865{
10866 vimmenu_T *menu;
10867 int item_idx = 0;
10868 int item_count = 0;
10869 int col = 0;
10870 int next_col;
10871 int off = (int)(current_ScreenLine - ScreenLines);
10872 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10873 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10874
10875 vim_free(wp->w_winbar_items);
10876 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10877 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010878 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010879
10880 /* TODO: use fewer spaces if there is not enough room */
10881 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010882 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010883 {
10884 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010885 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010886 break;
10887 if (col > 1)
10888 {
10889 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010890 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010891 break;
10892 }
10893
10894 wp->w_winbar_items[item_idx].wb_startcol = col;
10895 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010896 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010897 break;
10898
10899 next_col = text_to_screenline(wp, menu->name, col);
10900 while (col < next_col)
10901 {
10902 ScreenAttrs[off + col] = button_attr;
10903 ++col;
10904 }
10905 wp->w_winbar_items[item_idx].wb_endcol = col;
10906 wp->w_winbar_items[item_idx].wb_menu = menu;
10907 ++item_idx;
10908
Bram Moolenaar02631462017-09-22 15:20:32 +020010909 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010910 break;
10911 space_to_screenline(off + col, button_attr);
10912 ++col;
10913 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010914 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010915 {
10916 space_to_screenline(off + col, fill_attr);
10917 ++col;
10918 }
10919 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10920
Bram Moolenaar02631462017-09-22 15:20:32 +020010921 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010922 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010923}
10924#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010925
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926/*
10927 * Show current status info in ruler and various other places
10928 * If always is FALSE, only show ruler if position has changed.
10929 */
10930 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010931showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932{
10933 if (!always && !redrawing())
10934 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010935#ifdef FEAT_INS_EXPAND
10936 if (pum_visible())
10937 {
10938 /* Don't redraw right now, do it later. */
10939 curwin->w_redr_status = TRUE;
10940 return;
10941 }
10942#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010943#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010944 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010945 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 else
10947#endif
10948#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010949 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950#endif
10951
10952#ifdef FEAT_TITLE
10953 if (need_maketitle
10954# ifdef FEAT_STL_OPT
10955 || (p_icon && (stl_syntax & STL_IN_ICON))
10956 || (p_title && (stl_syntax & STL_IN_TITLE))
10957# endif
10958 )
10959 maketitle();
10960#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010961 /* Redraw the tab pages line if needed. */
10962 if (redraw_tabline)
10963 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964}
10965
10966#ifdef FEAT_CMDL_INFO
10967 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010968win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010970#define RULER_BUF_LEN 70
10971 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 int row;
10973 int fillchar;
10974 int attr;
10975 int empty_line = FALSE;
10976 colnr_T virtcol;
10977 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010978 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 int this_ru_col;
10981 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010982 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983
10984 /* If 'ruler' off or redrawing disabled, don't do anything */
10985 if (!p_ru)
10986 return;
10987
10988 /*
10989 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10990 * after deleting lines, before cursor.lnum is corrected.
10991 */
10992 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10993 return;
10994
10995#ifdef FEAT_INS_EXPAND
10996 /* Don't draw the ruler while doing insert-completion, it might overwrite
10997 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (edit_submode != NULL)
11000 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011001 // Don't draw the ruler when the popup menu is visible, it may overlap.
11002 // Except when the popup menu will be redrawn anyway.
11003 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011004 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005#endif
11006
11007#ifdef FEAT_STL_OPT
11008 if (*p_ruf)
11009 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011010 int save_called_emsg = called_emsg;
11011
11012 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011014 if (called_emsg)
11015 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011016 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011017 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 return;
11019 }
11020#endif
11021
11022 /*
11023 * Check if not in Insert mode and the line is empty (will show "0-1").
11024 */
11025 if (!(State & INSERT)
11026 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11027 empty_line = TRUE;
11028
11029 /*
11030 * Only draw the ruler when something changed.
11031 */
11032 validate_virtcol_win(wp);
11033 if ( redraw_cmdline
11034 || always
11035 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11036 || wp->w_cursor.col != wp->w_ru_cursor.col
11037 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 || wp->w_topline != wp->w_ru_topline
11040 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11041#ifdef FEAT_DIFF
11042 || wp->w_topfill != wp->w_ru_topfill
11043#endif
11044 || empty_line != wp->w_ru_empty)
11045 {
11046 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 if (wp->w_status_height)
11048 {
11049 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011050 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011051 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011052 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 }
11054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 {
11056 row = Rows - 1;
11057 fillchar = ' ';
11058 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 width = Columns;
11060 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 }
11062
11063 /* In list mode virtcol needs to be recomputed */
11064 virtcol = wp->w_virtcol;
11065 if (wp->w_p_list && lcs_tab1 == NUL)
11066 {
11067 wp->w_p_list = FALSE;
11068 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11069 wp->w_p_list = TRUE;
11070 }
11071
11072 /*
11073 * Some sprintfs return the length, some return a pointer.
11074 * To avoid portability problems we use strlen() here.
11075 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011076 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11078 ? 0L
11079 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011080 len = STRLEN(buffer);
11081 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11083 (int)virtcol + 1);
11084
11085 /*
11086 * Add a "50%" if there is room for it.
11087 * On the last line, don't print in the last column (scrolls the
11088 * screen up on some terminals).
11089 */
11090 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011091 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 this_ru_col = ru_col - (Columns - width);
11096 if (this_ru_col < 0)
11097 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 /* Never use more than half the window/screen width, leave the other
11099 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011100 if (this_ru_col < (width + 1) / 2)
11101 this_ru_col = (width + 1) / 2;
11102 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011104 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011105 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 if (has_mbyte)
11108 i += (*mb_char2bytes)(fillchar, buffer + i);
11109 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 buffer[i++] = fillchar;
11111 ++o;
11112 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011113 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 }
11115 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 if (has_mbyte)
11117 {
11118 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011119 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 {
11121 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011122 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 {
11124 buffer[i] = NUL;
11125 break;
11126 }
11127 }
11128 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011129 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011130 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131
Bram Moolenaar4033c552017-09-16 20:54:51 +020011132 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 i = redraw_cmdline;
11134 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011135 this_ru_col + off + (int)STRLEN(buffer),
11136 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 fillchar, fillchar, attr);
11138 /* don't redraw the cmdline because of showing the ruler */
11139 redraw_cmdline = i;
11140 wp->w_ru_cursor = wp->w_cursor;
11141 wp->w_ru_virtcol = wp->w_virtcol;
11142 wp->w_ru_empty = empty_line;
11143 wp->w_ru_topline = wp->w_topline;
11144 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11145#ifdef FEAT_DIFF
11146 wp->w_ru_topfill = wp->w_topfill;
11147#endif
11148 }
11149}
11150#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011151
11152#if defined(FEAT_LINEBREAK) || defined(PROTO)
11153/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011154 * Return the width of the 'number' and 'relativenumber' column.
11155 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011156 * Otherwise it depends on 'numberwidth' and the line count.
11157 */
11158 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011159number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011160{
11161 int n;
11162 linenr_T lnum;
11163
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011164 if (wp->w_p_rnu && !wp->w_p_nu)
11165 /* cursor line shows "0" */
11166 lnum = wp->w_height;
11167 else
11168 /* cursor line shows absolute line number */
11169 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011170
Bram Moolenaar6b314672015-03-20 15:42:10 +010011171 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011172 return wp->w_nrwidth_width;
11173 wp->w_nrwidth_line_count = lnum;
11174
11175 n = 0;
11176 do
11177 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011178 lnum /= 10;
11179 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011180 } while (lnum > 0);
11181
11182 /* 'numberwidth' gives the minimal width plus one */
11183 if (n < wp->w_p_nuw - 1)
11184 n = wp->w_p_nuw - 1;
11185
11186 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011187 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011188 return n;
11189}
11190#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011191
Bram Moolenaar113e1072019-01-20 15:30:40 +010011192#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011193/*
11194 * Return the current cursor column. This is the actual position on the
11195 * screen. First column is 0.
11196 */
11197 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011198screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011199{
11200 return screen_cur_col;
11201}
11202
11203/*
11204 * Return the current cursor row. This is the actual position on the screen.
11205 * First row is 0.
11206 */
11207 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011208screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011209{
11210 return screen_cur_row;
11211}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011212#endif