blob: de0833a679538b3c4c392017ff9ce623acedb088 [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;
1033 win_update(lowest_wp);
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001034 lowest_wp->w_popup_flags |= POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001035 }
1036}
1037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001039/*
1040 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1041 * window then get the "Pmenu" highlight attribute.
1042 */
1043 static int
1044get_wcr_attr(win_T *wp)
1045{
1046 int wcr_attr = 0;
1047
1048 if (*wp->w_p_wcr != NUL)
1049 wcr_attr = syn_name2attr(wp->w_p_wcr);
1050#ifdef FEAT_TEXT_PROP
1051 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1052 wcr_attr = HL_ATTR(HLF_PNI);
1053#endif
1054 return wcr_attr;
1055}
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#if defined(FEAT_GUI) || defined(PROTO)
1058/*
1059 * Update a single window, its status line and maybe the command line msg.
1060 * Used for the GUI scrollbar.
1061 */
1062 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001063updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001065 /* return if already busy updating */
1066 if (updating_screen)
1067 return;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 update_prepare();
1070
1071#ifdef FEAT_CLIPBOARD
1072 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001073 if (clip_star.available && clip_isautosel_star())
1074 clip_update_selection(&clip_star);
1075 if (clip_plus.available && clip_isautosel_plus())
1076 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001081 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001082 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001083 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (wp->w_redr_status
1086# ifdef FEAT_CMDL_INFO
1087 || p_ru
1088# endif
1089# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001090 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091# endif
1092 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001093 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 update_finish();
1096}
1097#endif
1098
1099/*
1100 * Update a single window.
1101 *
1102 * This may cause the windows below it also to be redrawn (when clearing the
1103 * screen or scrolling lines).
1104 *
1105 * How the window is redrawn depends on wp->w_redr_type. Each type also
1106 * implies the one below it.
1107 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001108 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1110 * INVERTED redraw the changed part of the Visual area
1111 * INVERTED_ALL redraw the whole Visual area
1112 * VALID 1. scroll up/down to adjust for a changed w_topline
1113 * 2. update lines at the top when scrolled down
1114 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001115 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 * b_mod_top and b_mod_bot.
1117 * - if wp->w_redraw_top non-zero, redraw lines between
1118 * wp->w_redraw_top and wp->w_redr_bot.
1119 * - continue redrawing when syntax status is invalid.
1120 * 4. if scrolled up, update lines at the bottom.
1121 * This results in three areas that may need updating:
1122 * top: from first row to top_end (when scrolled down)
1123 * mid: from mid_start to mid_end (update inversion or changed text)
1124 * bot: from bot_start to last row (when scrolled up)
1125 */
1126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001127win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
1129 buf_T *buf = wp->w_buffer;
1130 int type;
1131 int top_end = 0; /* Below last row of the top area that needs
1132 updating. 0 when no top area updating. */
1133 int mid_start = 999;/* first row of the mid area that needs
1134 updating. 999 when no mid area updating. */
1135 int mid_end = 0; /* Below last row of the mid area that needs
1136 updating. 0 when no mid area updating. */
1137 int bot_start = 999;/* first row of the bot area that needs
1138 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 int scrolled_down = FALSE; /* TRUE when scrolled down when
1140 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001142 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 int top_to_mod = FALSE; /* redraw above mod_top */
1144#endif
1145
1146 int row; /* current window row to display */
1147 linenr_T lnum; /* current buffer lnum to display */
1148 int idx; /* current index in w_lines[] */
1149 int srow; /* starting row of the current line */
1150
1151 int eof = FALSE; /* if TRUE, we hit the end of the file */
1152 int didline = FALSE; /* if TRUE, we finished the last line */
1153 int i;
1154 long j;
1155 static int recursive = FALSE; /* being called recursively */
1156 int old_botline = wp->w_botline;
1157#ifdef FEAT_FOLDING
1158 long fold_count;
1159#endif
1160#ifdef FEAT_SYN_HL
1161 /* remember what happened to the previous line, to know if
1162 * check_visual_highlight() can be used */
1163#define DID_NONE 1 /* didn't update a line */
1164#define DID_LINE 2 /* updated a normal line */
1165#define DID_FOLD 3 /* updated a folded line */
1166 int did_update = DID_NONE;
1167 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1168#endif
1169 linenr_T mod_top = 0;
1170 linenr_T mod_bot = 0;
1171#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1172 int save_got_int;
1173#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001174#ifdef SYN_TIME_LIMIT
1175 proftime_T syntax_tm;
1176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 type = wp->w_redr_type;
1179
1180 if (type == NOT_VALID)
1181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 wp->w_lines_valid = 0;
1184 }
1185
1186 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001187 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
1189 wp->w_redr_type = 0;
1190 return;
1191 }
1192
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 /* Window is zero-width: Only need to draw the separator. */
1194 if (wp->w_width == 0)
1195 {
1196 /* draw the vertical separator right of this window */
1197 draw_vsep_win(wp, 0);
1198 wp->w_redr_type = 0;
1199 return;
1200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001202#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001203 // If this window contains a terminal, redraw works completely differently.
1204 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001205 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001206 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001207# ifdef FEAT_MENU
1208 /* Draw the window toolbar, if there is one. */
1209 if (winbar_height(wp) > 0)
1210 redraw_win_toolbar(wp);
1211# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001212 wp->w_redr_type = 0;
1213 return;
1214 }
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001218 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219#endif
1220
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001221#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001222 /* Force redraw when width of 'number' or 'relativenumber' column
1223 * changes. */
1224 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001225 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001226 {
1227 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001228 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001229 }
1230 else
1231#endif
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1234 {
1235 /*
1236 * When there are both inserted/deleted lines and specific lines to be
1237 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1238 * everything (only happens when redrawing is off for while).
1239 */
1240 type = NOT_VALID;
1241 }
1242 else
1243 {
1244 /*
1245 * Set mod_top to the first line that needs displaying because of
1246 * changes. Set mod_bot to the first line after the changes.
1247 */
1248 mod_top = wp->w_redraw_top;
1249 if (wp->w_redraw_bot != 0)
1250 mod_bot = wp->w_redraw_bot + 1;
1251 else
1252 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (buf->b_mod_set)
1254 {
1255 if (mod_top == 0 || mod_top > buf->b_mod_top)
1256 {
1257 mod_top = buf->b_mod_top;
1258#ifdef FEAT_SYN_HL
1259 /* Need to redraw lines above the change that may be included
1260 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001263 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 if (mod_top < 1)
1265 mod_top = 1;
1266 }
1267#endif
1268 }
1269 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1270 mod_bot = buf->b_mod_bot;
1271
1272#ifdef FEAT_SEARCH_EXTRA
1273 /* When 'hlsearch' is on and using a multi-line search pattern, a
1274 * change in one line may make the Search highlighting in a
1275 * previous line invalid. Simple solution: redraw all visible
1276 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001277 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001279 if (search_hl.rm.regprog != NULL
1280 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001282 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001283 {
1284 cur = wp->w_match_head;
1285 while (cur != NULL)
1286 {
1287 if (cur->match.regprog != NULL
1288 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001289 {
1290 top_to_mod = TRUE;
1291 break;
1292 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001293 cur = cur->next;
1294 }
1295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296#endif
1297 }
1298#ifdef FEAT_FOLDING
1299 if (mod_top != 0 && hasAnyFolding(wp))
1300 {
1301 linenr_T lnumt, lnumb;
1302
1303 /*
1304 * A change in a line can cause lines above it to become folded or
1305 * unfolded. Find the top most buffer line that may be affected.
1306 * If the line was previously folded and displayed, get the first
1307 * line of that fold. If the line is folded now, get the first
1308 * folded line. Use the minimum of these two.
1309 */
1310
1311 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1312 * the line below it. If there is no valid entry, use w_topline.
1313 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1314 * to this line. If there is no valid entry, use MAXLNUM. */
1315 lnumt = wp->w_topline;
1316 lnumb = MAXLNUM;
1317 for (i = 0; i < wp->w_lines_valid; ++i)
1318 if (wp->w_lines[i].wl_valid)
1319 {
1320 if (wp->w_lines[i].wl_lastlnum < mod_top)
1321 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1322 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1323 {
1324 lnumb = wp->w_lines[i].wl_lnum;
1325 /* When there is a fold column it might need updating
1326 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001327 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 ++lnumb;
1329 }
1330 }
1331
1332 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1333 if (mod_top > lnumt)
1334 mod_top = lnumt;
1335
1336 /* Now do the same for the bottom line (one above mod_bot). */
1337 --mod_bot;
1338 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1339 ++mod_bot;
1340 if (mod_bot < lnumb)
1341 mod_bot = lnumb;
1342 }
1343#endif
1344
1345 /* When a change starts above w_topline and the end is below
1346 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001347 * If the end of the change is above w_topline: do like no change was
1348 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (mod_top != 0 && mod_top < wp->w_topline)
1350 {
1351 if (mod_bot > wp->w_topline)
1352 mod_top = wp->w_topline;
1353#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001354 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 top_end = 1;
1356#endif
1357 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001358
1359 /* When line numbers are displayed need to redraw all lines below
1360 * inserted/deleted lines. */
1361 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1362 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001364 wp->w_redraw_top = 0; // reset for next time
1365 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 /*
1368 * When only displaying the lines at the top, set top_end. Used when
1369 * window has scrolled down for msg_scrolled.
1370 */
1371 if (type == REDRAW_TOP)
1372 {
1373 j = 0;
1374 for (i = 0; i < wp->w_lines_valid; ++i)
1375 {
1376 j += wp->w_lines[i].wl_size;
1377 if (j >= wp->w_upd_rows)
1378 {
1379 top_end = j;
1380 break;
1381 }
1382 }
1383 if (top_end == 0)
1384 /* not found (cannot happen?): redraw everything */
1385 type = NOT_VALID;
1386 else
1387 /* top area defined, the rest is VALID */
1388 type = VALID;
1389 }
1390
Bram Moolenaar367329b2007-08-30 11:53:22 +00001391 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001392 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1393 * non-zero and thus not FALSE) will indicate that screenclear() was not
1394 * called. */
1395 if (screen_cleared)
1396 screen_cleared = MAYBE;
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /*
1399 * If there are no changes on the screen that require a complete redraw,
1400 * handle three cases:
1401 * 1: we are off the top of the screen by a few lines: scroll down
1402 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1403 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1404 * w_lines[] that needs updating.
1405 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001406 if ((type == VALID || type == SOME_VALID
1407 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#ifdef FEAT_DIFF
1409 && !wp->w_botfill && !wp->w_old_botfill
1410#endif
1411 )
1412 {
1413 if (mod_top != 0 && wp->w_topline == mod_top)
1414 {
1415 /*
1416 * w_topline is the first changed line, the scrolling will be done
1417 * further down.
1418 */
1419 }
1420 else if (wp->w_lines[0].wl_valid
1421 && (wp->w_topline < wp->w_lines[0].wl_lnum
1422#ifdef FEAT_DIFF
1423 || (wp->w_topline == wp->w_lines[0].wl_lnum
1424 && wp->w_topfill > wp->w_old_topfill)
1425#endif
1426 ))
1427 {
1428 /*
1429 * New topline is above old topline: May scroll down.
1430 */
1431#ifdef FEAT_FOLDING
1432 if (hasAnyFolding(wp))
1433 {
1434 linenr_T ln;
1435
1436 /* count the number of lines we are off, counting a sequence
1437 * of folded lines as one */
1438 j = 0;
1439 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1440 {
1441 ++j;
1442 if (j >= wp->w_height - 2)
1443 break;
1444 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1445 }
1446 }
1447 else
1448#endif
1449 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1450 if (j < wp->w_height - 2) /* not too far off */
1451 {
1452 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1453#ifdef FEAT_DIFF
1454 /* insert extra lines for previously invisible filler lines */
1455 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1456 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1457 - wp->w_old_topfill;
1458#endif
1459 if (i < wp->w_height - 2) /* less than a screen off */
1460 {
1461 /*
1462 * Try to insert the correct number of lines.
1463 * If not the last window, delete the lines at the bottom.
1464 * win_ins_lines may fail when the terminal can't do it.
1465 */
1466 if (i > 0)
1467 check_for_delay(FALSE);
1468 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1469 {
1470 if (wp->w_lines_valid != 0)
1471 {
1472 /* Need to update rows that are new, stop at the
1473 * first one that scrolled down. */
1474 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477 /* Move the entries that were scrolled, disable
1478 * the entries for the lines to be redrawn. */
1479 if ((wp->w_lines_valid += j) > wp->w_height)
1480 wp->w_lines_valid = wp->w_height;
1481 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1482 wp->w_lines[idx] = wp->w_lines[idx - j];
1483 while (idx >= 0)
1484 wp->w_lines[idx--].wl_valid = FALSE;
1485 }
1486 }
1487 else
1488 mid_start = 0; /* redraw all lines */
1489 }
1490 else
1491 mid_start = 0; /* redraw all lines */
1492 }
1493 else
1494 mid_start = 0; /* redraw all lines */
1495 }
1496 else
1497 {
1498 /*
1499 * New topline is at or below old topline: May scroll up.
1500 * When topline didn't change, find first entry in w_lines[] that
1501 * needs updating.
1502 */
1503
1504 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1505 j = -1;
1506 row = 0;
1507 for (i = 0; i < wp->w_lines_valid; i++)
1508 {
1509 if (wp->w_lines[i].wl_valid
1510 && wp->w_lines[i].wl_lnum == wp->w_topline)
1511 {
1512 j = i;
1513 break;
1514 }
1515 row += wp->w_lines[i].wl_size;
1516 }
1517 if (j == -1)
1518 {
1519 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1520 * lines */
1521 mid_start = 0;
1522 }
1523 else
1524 {
1525 /*
1526 * Try to delete the correct number of lines.
1527 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1528 */
1529#ifdef FEAT_DIFF
1530 /* If the topline didn't change, delete old filler lines,
1531 * otherwise delete filler lines of the new topline... */
1532 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1533 row += wp->w_old_topfill;
1534 else
1535 row += diff_check_fill(wp, wp->w_topline);
1536 /* ... but don't delete new filler lines. */
1537 row -= wp->w_topfill;
1538#endif
1539 if (row > 0)
1540 {
1541 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001542 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1543 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 bot_start = wp->w_height - row;
1545 else
1546 mid_start = 0; /* redraw all lines */
1547 }
1548 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1549 {
1550 /*
1551 * Skip the lines (below the deleted lines) that are still
1552 * valid and don't need redrawing. Copy their info
1553 * upwards, to compensate for the deleted lines. Set
1554 * bot_start to the first row that needs redrawing.
1555 */
1556 bot_start = 0;
1557 idx = 0;
1558 for (;;)
1559 {
1560 wp->w_lines[idx] = wp->w_lines[j];
1561 /* stop at line that didn't fit, unless it is still
1562 * valid (no lines deleted) */
1563 if (row > 0 && bot_start + row
1564 + (int)wp->w_lines[j].wl_size > wp->w_height)
1565 {
1566 wp->w_lines_valid = idx + 1;
1567 break;
1568 }
1569 bot_start += wp->w_lines[idx++].wl_size;
1570
1571 /* stop at the last valid entry in w_lines[].wl_size */
1572 if (++j >= wp->w_lines_valid)
1573 {
1574 wp->w_lines_valid = idx;
1575 break;
1576 }
1577 }
1578#ifdef FEAT_DIFF
1579 /* Correct the first entry for filler lines at the top
1580 * when it won't get updated below. */
1581 if (wp->w_p_diff && bot_start > 0)
1582 wp->w_lines[0].wl_size =
1583 plines_win_nofill(wp, wp->w_topline, TRUE)
1584 + wp->w_topfill;
1585#endif
1586 }
1587 }
1588 }
1589
1590 /* When starting redraw in the first line, redraw all lines. When
1591 * there is only one window it's probably faster to clear the screen
1592 * first. */
1593 if (mid_start == 0)
1594 {
1595 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001596 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001597 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001598 /* Clear the screen when it was not done by win_del_lines() or
1599 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1600 * then. */
1601 if (screen_cleared != TRUE)
1602 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001603 /* The screen was cleared, redraw the tab pages line. */
1604 if (redraw_tabline)
1605 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001608
1609 /* When win_del_lines() or win_ins_lines() caused the screen to be
1610 * cleared (only happens for the first window) or when screenclear()
1611 * was called directly above, "must_redraw" will have been set to
1612 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1613 if (screen_cleared == TRUE)
1614 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 else
1617 {
1618 /* Not VALID or INVERTED: redraw all lines. */
1619 mid_start = 0;
1620 mid_end = wp->w_height;
1621 }
1622
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001623 if (type == SOME_VALID)
1624 {
1625 /* SOME_VALID: redraw all lines. */
1626 mid_start = 0;
1627 mid_end = wp->w_height;
1628 type = NOT_VALID;
1629 }
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 /* check if we are updating or removing the inverted part */
1632 if ((VIsual_active && buf == curwin->w_buffer)
1633 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1634 {
1635 linenr_T from, to;
1636
1637 if (VIsual_active)
1638 {
1639 if (VIsual_active
1640 && (VIsual_mode != wp->w_old_visual_mode
1641 || type == INVERTED_ALL))
1642 {
1643 /*
1644 * If the type of Visual selection changed, redraw the whole
1645 * selection. Also when the ownership of the X selection is
1646 * gained or lost.
1647 */
1648 if (curwin->w_cursor.lnum < VIsual.lnum)
1649 {
1650 from = curwin->w_cursor.lnum;
1651 to = VIsual.lnum;
1652 }
1653 else
1654 {
1655 from = VIsual.lnum;
1656 to = curwin->w_cursor.lnum;
1657 }
1658 /* redraw more when the cursor moved as well */
1659 if (wp->w_old_cursor_lnum < from)
1660 from = wp->w_old_cursor_lnum;
1661 if (wp->w_old_cursor_lnum > to)
1662 to = wp->w_old_cursor_lnum;
1663 if (wp->w_old_visual_lnum < from)
1664 from = wp->w_old_visual_lnum;
1665 if (wp->w_old_visual_lnum > to)
1666 to = wp->w_old_visual_lnum;
1667 }
1668 else
1669 {
1670 /*
1671 * Find the line numbers that need to be updated: The lines
1672 * between the old cursor position and the current cursor
1673 * position. Also check if the Visual position changed.
1674 */
1675 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1676 {
1677 from = curwin->w_cursor.lnum;
1678 to = wp->w_old_cursor_lnum;
1679 }
1680 else
1681 {
1682 from = wp->w_old_cursor_lnum;
1683 to = curwin->w_cursor.lnum;
1684 if (from == 0) /* Visual mode just started */
1685 from = to;
1686 }
1687
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001688 if (VIsual.lnum != wp->w_old_visual_lnum
1689 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 if (wp->w_old_visual_lnum < from
1692 && wp->w_old_visual_lnum != 0)
1693 from = wp->w_old_visual_lnum;
1694 if (wp->w_old_visual_lnum > to)
1695 to = wp->w_old_visual_lnum;
1696 if (VIsual.lnum < from)
1697 from = VIsual.lnum;
1698 if (VIsual.lnum > to)
1699 to = VIsual.lnum;
1700 }
1701 }
1702
1703 /*
1704 * If in block mode and changed column or curwin->w_curswant:
1705 * update all lines.
1706 * First compute the actual start and end column.
1707 */
1708 if (VIsual_mode == Ctrl_V)
1709 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001710 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001711#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001712 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar404406a2014-10-09 13:24:43 +02001714 if (curwin->w_p_lbr)
1715 ve_flags = VE_ALL;
1716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001718#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001719 ve_flags = save_ve_flags;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 ++toc;
1722 if (curwin->w_curswant == MAXCOL)
1723 toc = MAXCOL;
1724
1725 if (fromc != wp->w_old_cursor_fcol
1726 || toc != wp->w_old_cursor_lcol)
1727 {
1728 if (from > VIsual.lnum)
1729 from = VIsual.lnum;
1730 if (to < VIsual.lnum)
1731 to = VIsual.lnum;
1732 }
1733 wp->w_old_cursor_fcol = fromc;
1734 wp->w_old_cursor_lcol = toc;
1735 }
1736 }
1737 else
1738 {
1739 /* Use the line numbers of the old Visual area. */
1740 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1741 {
1742 from = wp->w_old_cursor_lnum;
1743 to = wp->w_old_visual_lnum;
1744 }
1745 else
1746 {
1747 from = wp->w_old_visual_lnum;
1748 to = wp->w_old_cursor_lnum;
1749 }
1750 }
1751
1752 /*
1753 * There is no need to update lines above the top of the window.
1754 */
1755 if (from < wp->w_topline)
1756 from = wp->w_topline;
1757
1758 /*
1759 * If we know the value of w_botline, use it to restrict the update to
1760 * the lines that are visible in the window.
1761 */
1762 if (wp->w_valid & VALID_BOTLINE)
1763 {
1764 if (from >= wp->w_botline)
1765 from = wp->w_botline - 1;
1766 if (to >= wp->w_botline)
1767 to = wp->w_botline - 1;
1768 }
1769
1770 /*
1771 * Find the minimal part to be updated.
1772 * Watch out for scrolling that made entries in w_lines[] invalid.
1773 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1774 * top_end; need to redraw from top_end to the "to" line.
1775 * A middle mouse click with a Visual selection may change the text
1776 * above the Visual area and reset wl_valid, do count these for
1777 * mid_end (in srow).
1778 */
1779 if (mid_start > 0)
1780 {
1781 lnum = wp->w_topline;
1782 idx = 0;
1783 srow = 0;
1784 if (scrolled_down)
1785 mid_start = top_end;
1786 else
1787 mid_start = 0;
1788 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1789 {
1790 if (wp->w_lines[idx].wl_valid)
1791 mid_start += wp->w_lines[idx].wl_size;
1792 else if (!scrolled_down)
1793 srow += wp->w_lines[idx].wl_size;
1794 ++idx;
1795# ifdef FEAT_FOLDING
1796 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1797 lnum = wp->w_lines[idx].wl_lnum;
1798 else
1799# endif
1800 ++lnum;
1801 }
1802 srow += mid_start;
1803 mid_end = wp->w_height;
1804 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1805 {
1806 if (wp->w_lines[idx].wl_valid
1807 && wp->w_lines[idx].wl_lnum >= to + 1)
1808 {
1809 /* Only update until first row of this line */
1810 mid_end = srow;
1811 break;
1812 }
1813 srow += wp->w_lines[idx].wl_size;
1814 }
1815 }
1816 }
1817
1818 if (VIsual_active && buf == curwin->w_buffer)
1819 {
1820 wp->w_old_visual_mode = VIsual_mode;
1821 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1822 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001823 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 wp->w_old_curswant = curwin->w_curswant;
1825 }
1826 else
1827 {
1828 wp->w_old_visual_mode = 0;
1829 wp->w_old_cursor_lnum = 0;
1830 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001831 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1835 /* reset got_int, otherwise regexp won't work */
1836 save_got_int = got_int;
1837 got_int = 0;
1838#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001839#ifdef SYN_TIME_LIMIT
1840 /* Set the time limit to 'redrawtime'. */
1841 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001842 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#ifdef FEAT_FOLDING
1845 win_foldinfo.fi_level = 0;
1846#endif
1847
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001848#ifdef FEAT_MENU
1849 /*
1850 * Draw the window toolbar, if there is one.
1851 * TODO: only when needed.
1852 */
1853 if (winbar_height(wp) > 0)
1854 redraw_win_toolbar(wp);
1855#endif
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 /*
1858 * Update all the window rows.
1859 */
1860 idx = 0; /* first entry in w_lines[].wl_size */
1861 row = 0;
1862 srow = 0;
1863 lnum = wp->w_topline; /* first line shown in window */
1864 for (;;)
1865 {
1866 /* stop updating when reached the end of the window (check for _past_
1867 * the end of the window is at the end of the loop) */
1868 if (row == wp->w_height)
1869 {
1870 didline = TRUE;
1871 break;
1872 }
1873
1874 /* stop updating when hit the end of the file */
1875 if (lnum > buf->b_ml.ml_line_count)
1876 {
1877 eof = TRUE;
1878 break;
1879 }
1880
1881 /* Remember the starting row of the line that is going to be dealt
1882 * with. It is used further down when the line doesn't fit. */
1883 srow = row;
1884
1885 /*
1886 * Update a line when it is in an area that needs updating, when it
1887 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001888 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 * win_del_lines(), check if the current line includes it.
1890 * When syntax folding is being used, the saved syntax states will
1891 * already have been updated, we can't see where the syntax state is
1892 * the same again, just update until the end of the window.
1893 */
1894 if (row < top_end
1895 || (row >= mid_start && row < mid_end)
1896#ifdef FEAT_SEARCH_EXTRA
1897 || top_to_mod
1898#endif
1899 || idx >= wp->w_lines_valid
1900 || (row + wp->w_lines[idx].wl_size > bot_start)
1901 || (mod_top != 0
1902 && (lnum == mod_top
1903 || (lnum >= mod_top
1904 && (lnum < mod_bot
1905#ifdef FEAT_SYN_HL
1906 || did_update == DID_FOLD
1907 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001908 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 && (
1910# ifdef FEAT_FOLDING
1911 (foldmethodIsSyntax(wp)
1912 && hasAnyFolding(wp)) ||
1913# endif
1914 syntax_check_changed(lnum)))
1915#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001916#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001917 /* match in fixed position might need redraw
1918 * if lines were inserted or deleted */
1919 || (wp->w_match_head != NULL
1920 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 )))))
1923 {
1924#ifdef FEAT_SEARCH_EXTRA
1925 if (lnum == mod_top)
1926 top_to_mod = FALSE;
1927#endif
1928
1929 /*
1930 * When at start of changed lines: May scroll following lines
1931 * up or down to minimize redrawing.
1932 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001933 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 */
1935 if (lnum == mod_top
1936 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001937 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 int old_rows = 0;
1940 int new_rows = 0;
1941 int xtra_rows;
1942 linenr_T l;
1943
1944 /* Count the old number of window rows, using w_lines[], which
1945 * should still contain the sizes for the lines as they are
1946 * currently displayed. */
1947 for (i = idx; i < wp->w_lines_valid; ++i)
1948 {
1949 /* Only valid lines have a meaningful wl_lnum. Invalid
1950 * lines are part of the changed area. */
1951 if (wp->w_lines[i].wl_valid
1952 && wp->w_lines[i].wl_lnum == mod_bot)
1953 break;
1954 old_rows += wp->w_lines[i].wl_size;
1955#ifdef FEAT_FOLDING
1956 if (wp->w_lines[i].wl_valid
1957 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1958 {
1959 /* Must have found the last valid entry above mod_bot.
1960 * Add following invalid entries. */
1961 ++i;
1962 while (i < wp->w_lines_valid
1963 && !wp->w_lines[i].wl_valid)
1964 old_rows += wp->w_lines[i++].wl_size;
1965 break;
1966 }
1967#endif
1968 }
1969
1970 if (i >= wp->w_lines_valid)
1971 {
1972 /* We can't find a valid line below the changed lines,
1973 * need to redraw until the end of the window.
1974 * Inserting/deleting lines has no use. */
1975 bot_start = 0;
1976 }
1977 else
1978 {
1979 /* Able to count old number of rows: Count new window
1980 * rows, and may insert/delete lines */
1981 j = idx;
1982 for (l = lnum; l < mod_bot; ++l)
1983 {
1984#ifdef FEAT_FOLDING
1985 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1986 ++new_rows;
1987 else
1988#endif
1989#ifdef FEAT_DIFF
1990 if (l == wp->w_topline)
1991 new_rows += plines_win_nofill(wp, l, TRUE)
1992 + wp->w_topfill;
1993 else
1994#endif
1995 new_rows += plines_win(wp, l, TRUE);
1996 ++j;
1997 if (new_rows > wp->w_height - row - 2)
1998 {
1999 /* it's getting too much, must redraw the rest */
2000 new_rows = 9999;
2001 break;
2002 }
2003 }
2004 xtra_rows = new_rows - old_rows;
2005 if (xtra_rows < 0)
2006 {
2007 /* May scroll text up. If there is not enough
2008 * remaining text or scrolling fails, must redraw the
2009 * rest. If scrolling works, must redraw the text
2010 * below the scrolled text. */
2011 if (row - xtra_rows >= wp->w_height - 2)
2012 mod_bot = MAXLNUM;
2013 else
2014 {
2015 check_for_delay(FALSE);
2016 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002017 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 mod_bot = MAXLNUM;
2019 else
2020 bot_start = wp->w_height + xtra_rows;
2021 }
2022 }
2023 else if (xtra_rows > 0)
2024 {
2025 /* May scroll text down. If there is not enough
2026 * remaining text of scrolling fails, must redraw the
2027 * rest. */
2028 if (row + xtra_rows >= wp->w_height - 2)
2029 mod_bot = MAXLNUM;
2030 else
2031 {
2032 check_for_delay(FALSE);
2033 if (win_ins_lines(wp, row + old_rows,
2034 xtra_rows, FALSE, FALSE) == FAIL)
2035 mod_bot = MAXLNUM;
2036 else if (top_end > row + old_rows)
2037 /* Scrolled the part at the top that requires
2038 * updating down. */
2039 top_end += xtra_rows;
2040 }
2041 }
2042
2043 /* When not updating the rest, may need to move w_lines[]
2044 * entries. */
2045 if (mod_bot != MAXLNUM && i != j)
2046 {
2047 if (j < i)
2048 {
2049 int x = row + new_rows;
2050
2051 /* move entries in w_lines[] upwards */
2052 for (;;)
2053 {
2054 /* stop at last valid entry in w_lines[] */
2055 if (i >= wp->w_lines_valid)
2056 {
2057 wp->w_lines_valid = j;
2058 break;
2059 }
2060 wp->w_lines[j] = wp->w_lines[i];
2061 /* stop at a line that won't fit */
2062 if (x + (int)wp->w_lines[j].wl_size
2063 > wp->w_height)
2064 {
2065 wp->w_lines_valid = j + 1;
2066 break;
2067 }
2068 x += wp->w_lines[j++].wl_size;
2069 ++i;
2070 }
2071 if (bot_start > x)
2072 bot_start = x;
2073 }
2074 else /* j > i */
2075 {
2076 /* move entries in w_lines[] downwards */
2077 j -= i;
2078 wp->w_lines_valid += j;
2079 if (wp->w_lines_valid > wp->w_height)
2080 wp->w_lines_valid = wp->w_height;
2081 for (i = wp->w_lines_valid; i - j >= idx; --i)
2082 wp->w_lines[i] = wp->w_lines[i - j];
2083
2084 /* The w_lines[] entries for inserted lines are
2085 * now invalid, but wl_size may be used above.
2086 * Reset to zero. */
2087 while (i >= idx)
2088 {
2089 wp->w_lines[i].wl_size = 0;
2090 wp->w_lines[i--].wl_valid = FALSE;
2091 }
2092 }
2093 }
2094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096
2097#ifdef FEAT_FOLDING
2098 /*
2099 * When lines are folded, display one line for all of them.
2100 * Otherwise, display normally (can be several display lines when
2101 * 'wrap' is on).
2102 */
2103 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2104 if (fold_count != 0)
2105 {
2106 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2107 ++row;
2108 --fold_count;
2109 wp->w_lines[idx].wl_folded = TRUE;
2110 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2111# ifdef FEAT_SYN_HL
2112 did_update = DID_FOLD;
2113# endif
2114 }
2115 else
2116#endif
2117 if (idx < wp->w_lines_valid
2118 && wp->w_lines[idx].wl_valid
2119 && wp->w_lines[idx].wl_lnum == lnum
2120 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002121 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 && srow + wp->w_lines[idx].wl_size > wp->w_height
2123#ifdef FEAT_DIFF
2124 && diff_check_fill(wp, lnum) == 0
2125#endif
2126 )
2127 {
2128 /* This line is not going to fit. Don't draw anything here,
2129 * will draw "@ " lines below. */
2130 row = wp->w_height + 1;
2131 }
2132 else
2133 {
2134#ifdef FEAT_SEARCH_EXTRA
2135 prepare_search_hl(wp, lnum);
2136#endif
2137#ifdef FEAT_SYN_HL
2138 /* Let the syntax stuff know we skipped a few lines. */
2139 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002140 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 syntax_end_parsing(syntax_last_parsed + 1);
2142#endif
2143
2144 /*
2145 * Display one line.
2146 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002147 row = win_line(wp, lnum, srow, wp->w_height,
2148 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150#ifdef FEAT_FOLDING
2151 wp->w_lines[idx].wl_folded = FALSE;
2152 wp->w_lines[idx].wl_lastlnum = lnum;
2153#endif
2154#ifdef FEAT_SYN_HL
2155 did_update = DID_LINE;
2156 syntax_last_parsed = lnum;
2157#endif
2158 }
2159
2160 wp->w_lines[idx].wl_lnum = lnum;
2161 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002162
2163 /* Past end of the window or end of the screen. Note that after
2164 * resizing wp->w_height may be end up too big. That's a problem
2165 * elsewhere, but prevent a crash here. */
2166 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002169 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2171 ++idx;
2172 break;
2173 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002174 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 wp->w_lines[idx].wl_size = row - srow;
2176 ++idx;
2177#ifdef FEAT_FOLDING
2178 lnum += fold_count + 1;
2179#else
2180 ++lnum;
2181#endif
2182 }
2183 else
2184 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002185 if (wp->w_p_rnu)
2186 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002187#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002188 // 'relativenumber' set: The text doesn't need to be drawn, but
2189 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002190 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2191 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002192 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002193 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002194#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002195 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002196 }
2197
2198 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 row += wp->w_lines[idx++].wl_size;
2200 if (row > wp->w_height) /* past end of screen */
2201 break;
2202#ifdef FEAT_FOLDING
2203 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2204#else
2205 ++lnum;
2206#endif
2207#ifdef FEAT_SYN_HL
2208 did_update = DID_NONE;
2209#endif
2210 }
2211
2212 if (lnum > buf->b_ml.ml_line_count)
2213 {
2214 eof = TRUE;
2215 break;
2216 }
2217 }
2218 /*
2219 * End of loop over all window lines.
2220 */
2221
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002222#ifdef FEAT_VTP
2223 /* Rewrite the character at the end of the screen line. */
2224 if (use_vtp())
2225 {
2226 int i;
2227
2228 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002229 if (enc_utf8)
2230 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2231 LineOffset[i] + screen_Columns) > 1)
2232 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2233 else
2234 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2235 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002236 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2237 }
2238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 if (idx > wp->w_lines_valid)
2241 wp->w_lines_valid = idx;
2242
2243#ifdef FEAT_SYN_HL
2244 /*
2245 * Let the syntax stuff know we stop parsing here.
2246 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002247 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 syntax_end_parsing(syntax_last_parsed + 1);
2249#endif
2250
2251 /*
2252 * If we didn't hit the end of the file, and we didn't finish the last
2253 * line we were working on, then the line didn't fit.
2254 */
2255 wp->w_empty_rows = 0;
2256#ifdef FEAT_DIFF
2257 wp->w_filler_rows = 0;
2258#endif
2259 if (!eof && !didline)
2260 {
2261 if (lnum == wp->w_topline)
2262 {
2263 /*
2264 * Single line that does not fit!
2265 * Don't overwrite it, it can be edited.
2266 */
2267 wp->w_botline = lnum + 1;
2268 }
2269#ifdef FEAT_DIFF
2270 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2271 {
2272 /* Window ends in filler lines. */
2273 wp->w_botline = lnum;
2274 wp->w_filler_rows = wp->w_height - srow;
2275 }
2276#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002277 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2278 {
2279 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2280
2281 /*
2282 * Last line isn't finished: Display "@@@" in the last screen line.
2283 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002284 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002285 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002286 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002287 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002288 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002289 set_empty_rows(wp, srow);
2290 wp->w_botline = lnum;
2291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2293 {
2294 /*
2295 * Last line isn't finished: Display "@@@" at the end.
2296 */
2297 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2298 W_WINROW(wp) + wp->w_height,
2299 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002300 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 set_empty_rows(wp, srow);
2302 wp->w_botline = lnum;
2303 }
2304 else
2305 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002306 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 wp->w_botline = lnum;
2308 }
2309 }
2310 else
2311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (eof) /* we hit the end of the file */
2314 {
2315 wp->w_botline = buf->b_ml.ml_line_count + 1;
2316#ifdef FEAT_DIFF
2317 j = diff_check_fill(wp, wp->w_botline);
2318 if (j > 0 && !wp->w_botfill)
2319 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002320 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 if (char2cells(fill_diff) > 1)
2322 i = '-';
2323 else
2324 i = fill_diff;
2325 if (row + j > wp->w_height)
2326 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002327 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 row += j;
2329 }
2330#endif
2331 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002332 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 wp->w_botline = lnum;
2334
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002335 // Make sure the rest of the screen is blank
2336 // put '~'s on rows that aren't part of the file.
2337 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002340#ifdef SYN_TIME_LIMIT
2341 syn_set_timeout(NULL);
2342#endif
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 /* Reset the type of redrawing required, the window has been updated. */
2345 wp->w_redr_type = 0;
2346#ifdef FEAT_DIFF
2347 wp->w_old_topfill = wp->w_topfill;
2348 wp->w_old_botfill = wp->w_botfill;
2349#endif
2350
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002351 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
2353 /*
2354 * There is a trick with w_botline. If we invalidate it on each
2355 * change that might modify it, this will cause a lot of expensive
2356 * calls to plines() in update_topline() each time. Therefore the
2357 * value of w_botline is often approximated, and this value is used to
2358 * compute the value of w_topline. If the value of w_botline was
2359 * wrong, check that the value of w_topline is correct (cursor is on
2360 * the visible part of the text). If it's not, we need to redraw
2361 * again. Mostly this just means scrolling up a few lines, so it
2362 * doesn't look too bad. Only do this for the current window (where
2363 * changes are relevant).
2364 */
2365 wp->w_valid |= VALID_BOTLINE;
2366 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2367 {
2368 recursive = TRUE;
2369 curwin->w_valid &= ~VALID_TOPLINE;
2370 update_topline(); /* may invalidate w_botline again */
2371 if (must_redraw != 0)
2372 {
2373 /* Don't update for changes in buffer again. */
2374 i = curbuf->b_mod_set;
2375 curbuf->b_mod_set = FALSE;
2376 win_update(curwin);
2377 must_redraw = 0;
2378 curbuf->b_mod_set = i;
2379 }
2380 recursive = FALSE;
2381 }
2382 }
2383
2384#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2385 /* restore got_int, unless CTRL-C was hit while redrawing */
2386 if (!got_int)
2387 got_int = save_got_int;
2388#endif
2389}
2390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002392 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2393 * Return the new offset.
2394 */
2395 static int
2396screen_fill_end(
2397 win_T *wp,
2398 int c1,
2399 int c2,
2400 int off,
2401 int width,
2402 int row,
2403 int endrow,
2404 int attr)
2405{
2406 int nn = off + width;
2407
2408 if (nn > wp->w_width)
2409 nn = wp->w_width;
2410#ifdef FEAT_RIGHTLEFT
2411 if (wp->w_p_rl)
2412 {
2413 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2414 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2415 c1, c2, attr);
2416 }
2417 else
2418#endif
2419 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2420 wp->w_wincol + off, (int)wp->w_wincol + nn,
2421 c1, c2, attr);
2422 return nn;
2423}
2424
2425/*
2426 * Clear lines near the end the window and mark the unused lines with "c1".
2427 * use "c2" as the filler character.
2428 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 */
2430 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002431win_draw_end(
2432 win_T *wp,
2433 int c1,
2434 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002435 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002436 int row,
2437 int endrow,
2438 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002441 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002442 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002443
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002444 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002445
2446 if (draw_margin)
2447 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002448#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002449 int fdc = compute_foldcolumn(wp, 0);
2450
2451 if (fdc > 0)
2452 // draw the fold column
2453 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002454 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002455#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002456#ifdef FEAT_SIGNS
2457 if (signcolumn_on(wp))
2458 // draw the sign column
2459 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002460 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002461#endif
2462 if ((wp->w_p_nu || wp->w_p_rnu)
2463 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2464 // draw the number column
2465 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002466 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469#ifdef FEAT_RIGHTLEFT
2470 if (wp->w_p_rl)
2471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002473 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002474 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002476 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002477 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479 else
2480#endif
2481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002483 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002484 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 set_empty_rows(wp, row);
2488}
2489
Bram Moolenaar1a384422010-07-14 19:53:30 +02002490#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002491/*
2492 * Advance **color_cols and return TRUE when there are columns to draw.
2493 */
2494 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002495advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002496{
2497 while (**color_cols >= 0 && vcol > **color_cols)
2498 ++*color_cols;
2499 return (**color_cols >= 0);
2500}
2501#endif
2502
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002503#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2504/*
2505 * Copy "text" to ScreenLines using "attr".
2506 * Returns the next screen column.
2507 */
2508 static int
2509text_to_screenline(win_T *wp, char_u *text, int col)
2510{
2511 int off = (int)(current_ScreenLine - ScreenLines);
2512
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002513 if (has_mbyte)
2514 {
2515 int cells;
2516 int u8c, u8cc[MAX_MCO];
2517 int i;
2518 int idx;
2519 int c_len;
2520 char_u *p;
2521# ifdef FEAT_ARABIC
2522 int prev_c = 0; /* previous Arabic character */
2523 int prev_c1 = 0; /* first composing char for prev_c */
2524# endif
2525
2526# ifdef FEAT_RIGHTLEFT
2527 if (wp->w_p_rl)
2528 idx = off;
2529 else
2530# endif
2531 idx = off + col;
2532
2533 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2534 for (p = text; *p != NUL; )
2535 {
2536 cells = (*mb_ptr2cells)(p);
2537 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002538 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002539# ifdef FEAT_RIGHTLEFT
2540 - (wp->w_p_rl ? col : 0)
2541# endif
2542 )
2543 break;
2544 ScreenLines[idx] = *p;
2545 if (enc_utf8)
2546 {
2547 u8c = utfc_ptr2char(p, u8cc);
2548 if (*p < 0x80 && u8cc[0] == 0)
2549 {
2550 ScreenLinesUC[idx] = 0;
2551#ifdef FEAT_ARABIC
2552 prev_c = u8c;
2553#endif
2554 }
2555 else
2556 {
2557#ifdef FEAT_ARABIC
2558 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2559 {
2560 /* Do Arabic shaping. */
2561 int pc, pc1, nc;
2562 int pcc[MAX_MCO];
2563 int firstbyte = *p;
2564
2565 /* The idea of what is the previous and next
2566 * character depends on 'rightleft'. */
2567 if (wp->w_p_rl)
2568 {
2569 pc = prev_c;
2570 pc1 = prev_c1;
2571 nc = utf_ptr2char(p + c_len);
2572 prev_c1 = u8cc[0];
2573 }
2574 else
2575 {
2576 pc = utfc_ptr2char(p + c_len, pcc);
2577 nc = prev_c;
2578 pc1 = pcc[0];
2579 }
2580 prev_c = u8c;
2581
2582 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2583 pc, pc1, nc);
2584 ScreenLines[idx] = firstbyte;
2585 }
2586 else
2587 prev_c = u8c;
2588#endif
2589 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002590 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002591 for (i = 0; i < Screen_mco; ++i)
2592 {
2593 ScreenLinesC[i][idx] = u8cc[i];
2594 if (u8cc[i] == 0)
2595 break;
2596 }
2597 }
2598 if (cells > 1)
2599 ScreenLines[idx + 1] = 0;
2600 }
2601 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2602 /* double-byte single width character */
2603 ScreenLines2[idx] = p[1];
2604 else if (cells > 1)
2605 /* double-width character */
2606 ScreenLines[idx + 1] = p[1];
2607 col += cells;
2608 idx += cells;
2609 p += c_len;
2610 }
2611 }
2612 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002613 {
2614 int len = (int)STRLEN(text);
2615
Bram Moolenaar02631462017-09-22 15:20:32 +02002616 if (len > wp->w_width - col)
2617 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002618 if (len > 0)
2619 {
2620#ifdef FEAT_RIGHTLEFT
2621 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002622 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002623 else
2624#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002625 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002626 col += len;
2627 }
2628 }
2629 return col;
2630}
2631#endif
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#ifdef FEAT_FOLDING
2634/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002635 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2636 * space is available for window "wp", minus "col".
2637 */
2638 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002639compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002640{
2641 int fdc = wp->w_p_fdc;
2642 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002643 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002644
2645 if (fdc > wwidth - (col + wmw))
2646 fdc = wwidth - (col + wmw);
2647 return fdc;
2648}
2649
2650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 * Display one folded line.
2652 */
2653 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002654fold_line(
2655 win_T *wp,
2656 long fold_count,
2657 foldinfo_T *foldinfo,
2658 linenr_T lnum,
2659 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002661 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 pos_T *top, *bot;
2663 linenr_T lnume = lnum + fold_count - 1;
2664 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002665 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 int col;
2668 int txtcol;
2669 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002670 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /* Build the fold line:
2673 * 1. Add the cmdwin_type for the command-line window
2674 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002675 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 * 4. Compose the text
2677 * 5. Add the text
2678 * 6. set highlighting for the Visual area an other text
2679 */
2680 col = 0;
2681
2682 /*
2683 * 1. Add the cmdwin_type for the command-line window
2684 * Ignores 'rightleft', this window is never right-left.
2685 */
2686#ifdef FEAT_CMDWIN
2687 if (cmdwin_type != 0 && wp == curwin)
2688 {
2689 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002690 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (enc_utf8)
2692 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 ++col;
2694 }
2695#endif
2696
2697 /*
2698 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002699 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002701 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (fdc > 0)
2703 {
2704 fill_foldcolumn(buf, wp, TRUE, lnum);
2705#ifdef FEAT_RIGHTLEFT
2706 if (wp->w_p_rl)
2707 {
2708 int i;
2709
Bram Moolenaar02631462017-09-22 15:20:32 +02002710 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002711 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 /* reverse the fold column */
2713 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002714 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716 else
2717#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002718 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 col += fdc;
2720 }
2721
2722#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002723# define RL_MEMSET(p, v, l) \
2724 do { \
2725 if (wp->w_p_rl) \
2726 for (ri = 0; ri < l; ++ri) \
2727 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2728 else \
2729 for (ri = 0; ri < l; ++ri) \
2730 ScreenAttrs[off + (p) + ri] = v; \
2731 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002733# define RL_MEMSET(p, v, l) \
2734 do { \
2735 for (ri = 0; ri < l; ++ri) \
2736 ScreenAttrs[off + (p) + ri] = v; \
2737 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738#endif
2739
Bram Moolenaar64486672010-05-16 15:46:46 +02002740 /* Set all attributes of the 'number' or 'relativenumber' column and the
2741 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002742 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744#ifdef FEAT_SIGNS
2745 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002746 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002748 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 if (len > 0)
2750 {
2751 if (len > 2)
2752 len = 2;
2753# ifdef FEAT_RIGHTLEFT
2754 if (wp->w_p_rl)
2755 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002756 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002757 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
2759# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002760 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 col += len;
2762 }
2763 }
2764#endif
2765
2766 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002767 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002769 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002771 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (len > 0)
2773 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002774 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002775 long num;
2776 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002777
2778 if (len > w + 1)
2779 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002780
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002781 if (wp->w_p_nu && !wp->w_p_rnu)
2782 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002783 num = (long)lnum;
2784 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002785 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002786 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002787 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002788 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002789 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002790 /* 'number' + 'relativenumber': cursor line shows absolute
2791 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002792 num = lnum;
2793 fmt = "%-*ld ";
2794 }
2795 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002796
Bram Moolenaar700e7342013-01-30 12:31:36 +01002797 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798#ifdef FEAT_RIGHTLEFT
2799 if (wp->w_p_rl)
2800 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002801 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002802 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else
2804#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002805 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 col += len;
2807 }
2808 }
2809
2810 /*
2811 * 4. Compose the folded-line string with 'foldtext', if set.
2812 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002813 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815 txtcol = col; /* remember where text starts */
2816
2817 /*
2818 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2819 * Right-left text is put in columns 0 - number-col, normal text is put
2820 * in columns number-col - window-width.
2821 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002822 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 /* Fill the rest of the line with the fold filler */
2825#ifdef FEAT_RIGHTLEFT
2826 if (wp->w_p_rl)
2827 col -= txtcol;
2828#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002829 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#ifdef FEAT_RIGHTLEFT
2831 - (wp->w_p_rl ? txtcol : 0)
2832#endif
2833 )
2834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (enc_utf8)
2836 {
2837 if (fill_fold >= 0x80)
2838 {
2839 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002840 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002841 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002846 ScreenLines[off + col] = fill_fold;
2847 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002848 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002850 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002851 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853
2854 if (text != buf)
2855 vim_free(text);
2856
2857 /*
2858 * 6. set highlighting for the Visual area an other text.
2859 * If all folded lines are in the Visual area, highlight the line.
2860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2862 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002863 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
2865 /* Visual is after curwin->w_cursor */
2866 top = &curwin->w_cursor;
2867 bot = &VIsual;
2868 }
2869 else
2870 {
2871 /* Visual is before curwin->w_cursor */
2872 top = &VIsual;
2873 bot = &curwin->w_cursor;
2874 }
2875 if (lnum >= top->lnum
2876 && lnume <= bot->lnum
2877 && (VIsual_mode != 'v'
2878 || ((lnum > top->lnum
2879 || (lnum == top->lnum
2880 && top->col == 0))
2881 && (lnume < bot->lnum
2882 || (lnume == bot->lnum
2883 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002884 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
2886 if (VIsual_mode == Ctrl_V)
2887 {
2888 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002891 if (wp->w_old_cursor_lcol != MAXCOL
2892 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002893 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 len = wp->w_old_cursor_lcol;
2895 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002897 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002898 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900 }
2901 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002909#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002910 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2911 if (wp->w_p_cc_cols)
2912 {
2913 int i = 0;
2914 int j = wp->w_p_cc_cols[i];
2915 int old_txtcol = txtcol;
2916
2917 while (j > -1)
2918 {
2919 txtcol += j;
2920 if (wp->w_p_wrap)
2921 txtcol -= wp->w_skipcol;
2922 else
2923 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002924 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002925 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002926 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002927 txtcol = old_txtcol;
2928 j = wp->w_p_cc_cols[++i];
2929 }
2930 }
2931
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002932 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002933 if (wp->w_p_cuc)
2934 {
2935 txtcol += wp->w_virtcol;
2936 if (wp->w_p_wrap)
2937 txtcol -= wp->w_skipcol;
2938 else
2939 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002940 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002941 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002942 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002943 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar02631462017-09-22 15:20:32 +02002946 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002947 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /*
2950 * Update w_cline_height and w_cline_folded if the cursor line was
2951 * updated (saves a call to plines() later).
2952 */
2953 if (wp == curwin
2954 && lnum <= curwin->w_cursor.lnum
2955 && lnume >= curwin->w_cursor.lnum)
2956 {
2957 curwin->w_cline_row = row;
2958 curwin->w_cline_height = 1;
2959 curwin->w_cline_folded = TRUE;
2960 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2961 }
2962}
2963
2964/*
2965 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2966 */
2967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002968copy_text_attr(
2969 int off,
2970 char_u *buf,
2971 int len,
2972 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002974 int i;
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 if (enc_utf8)
2978 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002979 for (i = 0; i < len; ++i)
2980 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981}
2982
2983/*
2984 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002985 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 */
2987 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002988fill_foldcolumn(
2989 char_u *p,
2990 win_T *wp,
2991 int closed, /* TRUE of FALSE */
2992 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 int i = 0;
2995 int level;
2996 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002997 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002998 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003001 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 level = win_foldinfo.fi_level;
3004 if (level > 0)
3005 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003006 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003007 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 /* If the column is too narrow, we start at the lowest level that
3010 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003011 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (first_level < 1)
3013 first_level = 1;
3014
Bram Moolenaar1c934292015-01-27 16:39:29 +01003015 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
3017 if (win_foldinfo.fi_lnum == lnum
3018 && first_level + i >= win_foldinfo.fi_low_level)
3019 p[i] = '-';
3020 else if (first_level == 1)
3021 p[i] = '|';
3022 else if (first_level + i <= 9)
3023 p[i] = '0' + first_level + i;
3024 else
3025 p[i] = '>';
3026 if (first_level + i == level)
3027 break;
3028 }
3029 }
3030 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003031 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032}
3033#endif /* FEAT_FOLDING */
3034
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003035#ifdef FEAT_TEXT_PROP
3036static textprop_T *current_text_props = NULL;
3037static buf_T *current_buf = NULL;
3038
3039 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003040text_prop_compare(const void *s1, const void *s2)
3041{
3042 int idx1, idx2;
3043 proptype_T *pt1, *pt2;
3044 colnr_T col1, col2;
3045
3046 idx1 = *(int *)s1;
3047 idx2 = *(int *)s2;
3048 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3049 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3050 if (pt1 == pt2)
3051 return 0;
3052 if (pt1 == NULL)
3053 return -1;
3054 if (pt2 == NULL)
3055 return 1;
3056 if (pt1->pt_priority != pt2->pt_priority)
3057 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3058 col1 = current_text_props[idx1].tp_col;
3059 col2 = current_text_props[idx2].tp_col;
3060 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3061}
3062#endif
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064/*
3065 * Display line "lnum" of window 'wp' on the screen.
3066 * Start at row "startrow", stop when "endrow" is reached.
3067 * wp->w_virtcol needs to be valid.
3068 *
3069 * Return the number of last row the line occupies.
3070 */
3071 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003072win_line(
3073 win_T *wp,
3074 linenr_T lnum,
3075 int startrow,
3076 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003077 int nochange UNUSED, // not updating for changed text
3078 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003080 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3082 int c = 0; /* init for GCC */
3083 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003084#ifdef FEAT_LINEBREAK
3085 long vcol_sbr = -1; /* virtual column after showbreak */
3086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 long vcol_prev = -1; /* "vcol" of previous character */
3088 char_u *line; /* current line */
3089 char_u *ptr; /* current position in "line" */
3090 int row; /* row in the window, excl w_winrow */
3091 int screen_row; /* row on the screen, incl w_winrow */
3092
3093 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3094 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003095 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003096 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003098 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 int extra_attr = 0; /* attributes when n_extra != 0 */
3100 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3101 displaying lcs_eol at end-of-line */
3102 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3103 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3104
3105 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3106 int saved_n_extra = 0;
3107 char_u *saved_p_extra = NULL;
3108 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003109 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 int saved_char_attr = 0;
3111
3112 int n_attr = 0; /* chars with special attr */
3113 int saved_attr2 = 0; /* char_attr saved for n_attr */
3114 int n_attr3 = 0; /* chars with overruling special attr */
3115 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3116
3117 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3118
3119 int fromcol, tocol; /* start/end of inverting */
3120 int fromcol_prev = -2; /* start of inverting after cursor */
3121 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003123 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 pos_T pos;
3125 long v;
3126
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003127 int char_attr = 0; // attributes for next character
3128 int attr_pri = FALSE; // char_attr has priority
3129 int area_highlighting = FALSE; // Visual or incsearch highlighting
3130 // in this line
3131 int vi_attr = 0; // attributes for Visual and incsearch
3132 // highlighting
3133 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003134 int win_attr = 0; // background for whole window, except
3135 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003136 int area_attr = 0; // attributes desired by highlighting
3137 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003139 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int syntax_attr = 0; /* attributes desired by syntax */
3141 int has_syntax = FALSE; /* this buffer has syntax highl. */
3142 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003143 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003144 int draw_color_col = FALSE; /* highlight colorcolumn */
3145 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003146#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003147#ifdef FEAT_TEXT_PROP
3148 int text_prop_count;
3149 int text_prop_next = 0; // next text property to use
3150 textprop_T *text_props = NULL;
3151 int *text_prop_idxs = NULL;
3152 int text_props_active = 0;
3153 proptype_T *text_prop_type = NULL;
3154 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003155 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003156#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003157#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003158 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003159# define SPWORDLEN 150
3160 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003161 int nextlinecol = 0; /* column where nextline[] starts */
3162 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003163 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003164 int spell_attr = 0; /* attributes desired by spelling */
3165 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003166 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3167 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003168 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003169 static int cap_col = -1; /* column to check for Cap word */
3170 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003171 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003173 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int multi_attr = 0; /* attributes desired by multibyte */
3175 int mb_l = 1; /* multi-byte byte length */
3176 int mb_c = 0; /* decoded multi-byte character */
3177 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003178 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#ifdef FEAT_DIFF
3180 int filler_lines; /* nr of filler lines to be drawn */
3181 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003182 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int change_start = MAXCOL; /* first col of changed area */
3184 int change_end = -1; /* last col of changed area */
3185#endif
3186 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3187#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003188 int need_showbreak = FALSE; /* overlong line, skipping first x
3189 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003191#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003192 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003194 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003197 matchitem_T *cur; /* points to the match list */
3198 match_T *shl; /* points to search_hl or a match */
3199 int shl_flag; /* flag to indicate whether search_hl
3200 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003201 int pos_inprogress; /* marks that position match search is
3202 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003203 int prevcol_hl_flag; /* flag to indicate whether prevcol
3204 equals startcol of search_hl or one
3205 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#endif
3207#ifdef FEAT_ARABIC
3208 int prev_c = 0; /* previous Arabic character */
3209 int prev_c1 = 0; /* first composing char for prev_c */
3210#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003211#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003212 int did_line_attr = 0;
3213#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003214#ifdef FEAT_TERMINAL
3215 int get_term_attr = FALSE;
3216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
3218 /* draw_state: items that are drawn in sequence: */
3219#define WL_START 0 /* nothing done yet */
3220#ifdef FEAT_CMDWIN
3221# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3222#else
3223# define WL_CMDLINE WL_START
3224#endif
3225#ifdef FEAT_FOLDING
3226# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3227#else
3228# define WL_FOLD WL_CMDLINE
3229#endif
3230#ifdef FEAT_SIGNS
3231# define WL_SIGN WL_FOLD + 1 /* column for signs */
3232#else
3233# define WL_SIGN WL_FOLD /* column for signs */
3234#endif
3235#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003236#ifdef FEAT_LINEBREAK
3237# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003239# define WL_BRI WL_NR
3240#endif
3241#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3242# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3243#else
3244# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#endif
3246#define WL_LINE WL_SBR + 1 /* text in the line */
3247 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003248#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int feedback_col = 0;
3250 int feedback_old_attr = -1;
3251#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003252 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaar860cae12010-06-05 23:22:07 +02003254#ifdef FEAT_CONCEAL
3255 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003256 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003257 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003258 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003259 int is_concealing = FALSE;
3260 int boguscols = 0; /* nonexistent columns added to force
3261 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003262 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003263 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003264 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003265 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003266# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003267# define FIX_FOR_BOGUSCOLS \
3268 { \
3269 n_extra += vcol_off; \
3270 vcol -= vcol_off; \
3271 vcol_off = 0; \
3272 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003273 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003274 boguscols = 0; \
3275 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003276#else
3277# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
3280 if (startrow > endrow) /* past the end already! */
3281 return startrow;
3282
3283 row = startrow;
3284 screen_row = row + W_WINROW(wp);
3285
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003286 if (!number_only)
3287 {
3288 /*
3289 * To speed up the loop below, set extra_check when there is linebreak,
3290 * trailing white space and/or syntax processing to be done.
3291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003293 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#endif
3295#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003296 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003297# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003298 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003299# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003300 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003302 /* Prepare for syntax highlighting in this line. When there is an
3303 * error, stop syntax highlighting. */
3304 save_did_emsg = did_emsg;
3305 did_emsg = FALSE;
3306 syntax_start(wp, lnum);
3307 if (did_emsg)
3308 wp->w_s->b_syn_error = TRUE;
3309 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003310 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003311 did_emsg = save_did_emsg;
3312#ifdef SYN_TIME_LIMIT
3313 if (!wp->w_s->b_syn_slow)
3314#endif
3315 {
3316 has_syntax = TRUE;
3317 extra_check = TRUE;
3318 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003321
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003322 /* Check for columns to display for 'colorcolumn'. */
3323 color_cols = wp->w_p_cc_cols;
3324 if (color_cols != NULL)
3325 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003326#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003327
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003328#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003329 if (term_show_buffer(wp->w_buffer))
3330 {
3331 extra_check = TRUE;
3332 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003333 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003334 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003335#endif
3336
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003337#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003338 if (wp->w_p_spell
3339 && *wp->w_s->b_p_spl != NUL
3340 && wp->w_s->b_langp.ga_len > 0
3341 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003342 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003343 /* Prepare for spell checking. */
3344 has_spell = TRUE;
3345 extra_check = TRUE;
3346
Bram Moolenaar7701f302018-10-02 21:20:32 +02003347 /* Get the start of the next line, so that words that wrap to the
3348 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003349 * Trick: skip a few chars for C/shell/Vim comments */
3350 nextline[SPWORDLEN] = NUL;
3351 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3352 {
3353 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3354 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3355 }
3356
Bram Moolenaar7701f302018-10-02 21:20:32 +02003357 /* When a word wrapped from the previous line the start of the
3358 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003359 if (lnum == checked_lnum)
3360 cur_checked_col = checked_col;
3361 checked_lnum = 0;
3362
3363 /* When there was a sentence end in the previous line may require a
3364 * word starting with capital in this line. In line 1 always check
3365 * the first word. */
3366 if (lnum != capcol_lnum)
3367 cap_col = -1;
3368 if (lnum == 1)
3369 cap_col = 0;
3370 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#endif
3373
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003374 /*
3375 * handle visual active in this window
3376 */
3377 fromcol = -10;
3378 tocol = MAXCOL;
3379 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003381 /* Visual is after curwin->w_cursor */
3382 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003384 top = &curwin->w_cursor;
3385 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003389 top = &VIsual;
3390 bot = &curwin->w_cursor;
3391 }
3392 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3393 if (VIsual_mode == Ctrl_V) /* block mode */
3394 {
3395 if (lnum_in_visual_area)
3396 {
3397 fromcol = wp->w_old_cursor_fcol;
3398 tocol = wp->w_old_cursor_lcol;
3399 }
3400 }
3401 else /* non-block mode */
3402 {
3403 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003405 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 if (VIsual_mode == 'V') /* linewise */
3408 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 else
3410 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003411 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3412 if (gchar_pos(top) == NUL)
3413 tocol = fromcol + 1;
3414 }
3415 }
3416 if (VIsual_mode != 'V' && lnum == bot->lnum)
3417 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003418 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 {
3420 fromcol = -10;
3421 tocol = MAXCOL;
3422 }
3423 else if (bot->col == MAXCOL)
3424 tocol = MAXCOL;
3425 else
3426 {
3427 pos = *bot;
3428 if (*p_sel == 'e')
3429 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3430 else
3431 {
3432 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3433 ++tocol;
3434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 }
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 /* Check if the character under the cursor should not be inverted */
3440 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003441#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003442 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003443#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 )
3445 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003447 /* if inverting in this line set area_highlighting */
3448 if (fromcol >= 0)
3449 {
3450 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003451 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003454 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003455 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003456 && clip_isautosel_plus()))
3457 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 /*
3463 * handle 'incsearch' and ":s///c" highlighting
3464 */
3465 else if (highlight_match
3466 && wp == curwin
3467 && lnum >= curwin->w_cursor.lnum
3468 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003470 if (lnum == curwin->w_cursor.lnum)
3471 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003472 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003473 else
3474 fromcol = 0;
3475 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3476 {
3477 pos.lnum = lnum;
3478 pos.col = search_match_endcol;
3479 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3480 }
3481 else
3482 tocol = MAXCOL;
3483 /* do at least one character; happens when past end of line */
3484 if (fromcol == tocol)
3485 tocol = fromcol + 1;
3486 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003487 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490
3491#ifdef FEAT_DIFF
3492 filler_lines = diff_check(wp, lnum);
3493 if (filler_lines < 0)
3494 {
3495 if (filler_lines == -1)
3496 {
3497 if (diff_find_change(wp, lnum, &change_start, &change_end))
3498 diff_hlf = HLF_ADD; /* added line */
3499 else if (change_start == 0)
3500 diff_hlf = HLF_TXD; /* changed text */
3501 else
3502 diff_hlf = HLF_CHD; /* changed line */
3503 }
3504 else
3505 diff_hlf = HLF_ADD; /* added line */
3506 filler_lines = 0;
3507 area_highlighting = TRUE;
3508 }
3509 if (lnum == wp->w_topline)
3510 filler_lines = wp->w_topfill;
3511 filler_todo = filler_lines;
3512#endif
3513
3514#ifdef LINE_ATTR
3515# ifdef FEAT_SIGNS
3516 /* If this line has a sign with line highlighting set line_attr. */
3517 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3518 if (v != 0)
3519 line_attr = sign_get_attr((int)v, TRUE);
3520# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003521# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003523 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003524 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525# endif
3526 if (line_attr != 0)
3527 area_highlighting = TRUE;
3528#endif
3529
3530 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3531 ptr = line;
3532
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003533#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003535 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003536 /* For checking first word with a capital skip white space. */
3537 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003538 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003539
Bram Moolenaar30abd282005-06-22 22:35:10 +00003540 /* To be able to spell-check over line boundaries copy the end of the
3541 * current line into nextline[]. Above the start of the next line was
3542 * copied to nextline[SPWORDLEN]. */
3543 if (nextline[SPWORDLEN] == NUL)
3544 {
3545 /* No next line or it is empty. */
3546 nextlinecol = MAXCOL;
3547 nextline_idx = 0;
3548 }
3549 else
3550 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003552 if (v < SPWORDLEN)
3553 {
3554 /* Short line, use it completely and append the start of the
3555 * next line. */
3556 nextlinecol = 0;
3557 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003558 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003559 nextline_idx = v + 1;
3560 }
3561 else
3562 {
3563 /* Long line, use only the last SPWORDLEN bytes. */
3564 nextlinecol = v - SPWORDLEN;
3565 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3566 nextline_idx = SPWORDLEN + 1;
3567 }
3568 }
3569 }
3570#endif
3571
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003572 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003574 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003575 extra_check = TRUE;
3576 /* find start of trailing whitespace */
3577 if (lcs_trail)
3578 {
3579 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003580 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003581 --trailcol;
3582 trailcol += (colnr_T) (ptr - line);
3583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003586 wcr_attr = get_wcr_attr(wp);
3587 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003588 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003589 win_attr = wcr_attr;
3590 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003591 }
3592#ifdef FEAT_TEXT_PROP
3593 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003594 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003595#endif
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 /*
3598 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3599 * first character to be displayed.
3600 */
3601 if (wp->w_p_wrap)
3602 v = wp->w_skipcol;
3603 else
3604 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003605 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003608
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 while (vcol < v && *ptr != NUL)
3610 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003611 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003614 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003617 /* When:
3618 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003619 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003620 * - 'virtualedit' is set, or
3621 * - the visual mode is active,
3622 * the end of the line may be before the start of the displayed part.
3623 */
3624 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003625#ifdef FEAT_SYN_HL
3626 wp->w_p_cuc || draw_color_col ||
3627#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003628 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003629 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
3632 /* Handle a character that's not completely on the screen: Put ptr at
3633 * that character but skip the first few screen characters. */
3634 if (vcol > v)
3635 {
3636 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003638 /* If the character fits on the screen, don't need to skip it.
3639 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003640 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003641 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643
3644 /*
3645 * Adjust for when the inverted text is before the screen,
3646 * and when the start of the inverted text is before the screen.
3647 */
3648 if (tocol <= vcol)
3649 fromcol = 0;
3650 else if (fromcol >= 0 && fromcol < vcol)
3651 fromcol = vcol;
3652
3653#ifdef FEAT_LINEBREAK
3654 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3655 if (wp->w_p_wrap)
3656 need_showbreak = TRUE;
3657#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003658#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003659 /* When spell checking a word we need to figure out the start of the
3660 * word and if it's badly spelled or not. */
3661 if (has_spell)
3662 {
3663 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003664 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003665 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003666
3667 pos = wp->w_cursor;
3668 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003669 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003670 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003671
3672 /* spell_move_to() may call ml_get() and make "line" invalid */
3673 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3674 ptr = line + linecol;
3675
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003676 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003677 {
3678 /* no bad word found at line start, don't check until end of a
3679 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003680 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003681 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003682 }
3683 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003684 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003685 /* bad word found, use attributes until end of word */
3686 word_end = wp->w_cursor.col + len + 1;
3687
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003688 /* Turn index into actual attributes. */
3689 if (spell_hlf != HLF_COUNT)
3690 spell_attr = highlight_attr[spell_hlf];
3691 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003692 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003693
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003694# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003695 /* Need to restart syntax highlighting for this line. */
3696 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003697 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003698# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003699 }
3700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702
3703 /*
3704 * Correct highlighting for cursor that can't be disabled.
3705 * Avoids having to check this for each character.
3706 */
3707 if (fromcol >= 0)
3708 {
3709 if (noinvcur)
3710 {
3711 if ((colnr_T)fromcol == wp->w_virtcol)
3712 {
3713 /* highlighting starts at cursor, let it start just after the
3714 * cursor */
3715 fromcol_prev = fromcol;
3716 fromcol = -1;
3717 }
3718 else if ((colnr_T)fromcol < wp->w_virtcol)
3719 /* restart highlighting after the cursor */
3720 fromcol_prev = wp->w_virtcol;
3721 }
3722 if (fromcol >= tocol)
3723 fromcol = -1;
3724 }
3725
3726#ifdef FEAT_SEARCH_EXTRA
3727 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003728 * Handle highlighting the last used search pattern and matches.
3729 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003730 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003732 cur = wp->w_match_head;
3733 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003734 while ((cur != NULL || shl_flag == FALSE) && !number_only
3735 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003737 if (shl_flag == FALSE)
3738 {
3739 shl = &search_hl;
3740 shl_flag = TRUE;
3741 }
3742 else
3743 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003744 shl->startcol = MAXCOL;
3745 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003747 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003748 v = (long)(ptr - line);
3749 if (cur != NULL)
3750 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003751 next_search_hl(wp, shl, lnum, (colnr_T)v,
3752 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003753
3754 /* Need to get the line again, a multi-line regexp may have made it
3755 * invalid. */
3756 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3757 ptr = line + v;
3758
3759 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003761 if (shl->lnum == lnum)
3762 shl->startcol = shl->rm.startpos[0].col;
3763 else
3764 shl->startcol = 0;
3765 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3766 - shl->rm.startpos[0].lnum)
3767 shl->endcol = shl->rm.endpos[0].col;
3768 else
3769 shl->endcol = MAXCOL;
3770 /* Highlight one character for an empty match. */
3771 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003773 if (has_mbyte && line[shl->endcol] != NUL)
3774 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3775 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003776 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003778 if ((long)shl->startcol < v) /* match at leftcol */
3779 {
3780 shl->attr_cur = shl->attr;
3781 search_attr = shl->attr;
3782 }
3783 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003785 if (shl != &search_hl && cur != NULL)
3786 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
3788#endif
3789
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003790#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003791 // Cursor line highlighting for 'cursorline' in the current window.
3792 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003793 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003794 // Do not show the cursor line when Visual mode is active, because it's
3795 // not clear what is selected then. Do update w_last_cursorline.
3796 if (!(wp == curwin && VIsual_active))
3797 {
3798 line_attr = HL_ATTR(HLF_CUL);
3799 area_highlighting = TRUE;
3800 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003801 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003802 }
3803#endif
3804
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003805#ifdef FEAT_TEXT_PROP
3806 {
3807 char_u *prop_start;
3808
3809 text_prop_count = get_text_props(wp->w_buffer, lnum,
3810 &prop_start, FALSE);
3811 if (text_prop_count > 0)
3812 {
3813 // Make a copy of the properties, so that they are properly
3814 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003815 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003816 if (text_props != NULL)
3817 mch_memmove(text_props, prop_start,
3818 text_prop_count * sizeof(textprop_T));
3819
3820 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003821 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003822 area_highlighting = TRUE;
3823 extra_check = TRUE;
3824 }
3825 }
3826#endif
3827
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003828 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#ifdef FEAT_RIGHTLEFT
3832 if (wp->w_p_rl)
3833 {
3834 /* Rightleft window: process the text in the normal direction, but put
3835 * it in current_ScreenLine[] from right to left. Start at the
3836 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003837 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003839 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841#endif
3842
3843 /*
3844 * Repeat for the whole displayed line.
3845 */
3846 for (;;)
3847 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003848#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003849 int has_match_conc = 0; // match wants to conceal
3850 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 /* Skip this quickly when working on the text. */
3853 if (draw_state != WL_LINE)
3854 {
3855#ifdef FEAT_CMDWIN
3856 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3857 {
3858 draw_state = WL_CMDLINE;
3859 if (cmdwin_type != 0 && wp == curwin)
3860 {
3861 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003863 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003864 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003865 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
3867 }
3868#endif
3869
3870#ifdef FEAT_FOLDING
3871 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3872 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003873 int fdc = compute_foldcolumn(wp, 0);
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003876 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003878 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003879 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003880 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003881 p_extra_free = alloc(12 + 1);
3882
3883 if (p_extra_free != NULL)
3884 {
3885 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3886 n_extra = fdc;
3887 p_extra_free[n_extra] = NUL;
3888 p_extra = p_extra_free;
3889 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003890 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003891 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 }
3894 }
3895#endif
3896
3897#ifdef FEAT_SIGNS
3898 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3899 {
3900 draw_state = WL_SIGN;
3901 /* Show the sign column when there are any signs in this
3902 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003903 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003905 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003907 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908# endif
3909
3910 /* Draw two cells with the sign value or blank. */
3911 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003912 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003913 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 n_extra = 2;
3915
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003916 if (row == startrow
3917#ifdef FEAT_DIFF
3918 + filler_lines && filler_todo <= 0
3919#endif
3920 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
3922 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3923 SIGN_TEXT);
3924# ifdef FEAT_SIGN_ICONS
3925 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3926 SIGN_ICON);
3927 if (gui.in_use && icon_sign != 0)
3928 {
3929 /* Use the image in this position. */
3930 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003931 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932# ifdef FEAT_NETBEANS_INTG
3933 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003936 c_final = NUL;
3937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938# endif
3939 char_attr = icon_sign;
3940 }
3941 else
3942# endif
3943 if (text_sign != 0)
3944 {
3945 p_extra = sign_get_text(text_sign);
3946 if (p_extra != NULL)
3947 {
3948 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003949 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003950 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952 char_attr = sign_get_attr(text_sign, FALSE);
3953 }
3954 }
3955 }
3956 }
3957#endif
3958
3959 if (draw_state == WL_NR - 1 && n_extra == 0)
3960 {
3961 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003962 /* Display the absolute or relative line number. After the
3963 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3964 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 && (row == startrow
3966#ifdef FEAT_DIFF
3967 + filler_lines
3968#endif
3969 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3970 {
3971 /* Draw the line number (empty space after wrapping). */
3972 if (row == startrow
3973#ifdef FEAT_DIFF
3974 + filler_lines
3975#endif
3976 )
3977 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003978 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003979 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003980
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003981 if (wp->w_p_nu && !wp->w_p_rnu)
3982 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003983 num = (long)lnum;
3984 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003985 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003986 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003987 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003988 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003989 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003990 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003991 num = lnum;
3992 fmt = "%-*ld ";
3993 }
3994 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003995
Bram Moolenaar700e7342013-01-30 12:31:36 +01003996 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003997 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (wp->w_skipcol > 0)
3999 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4000 *p_extra = '-';
4001#ifdef FEAT_RIGHTLEFT
4002 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004003 {
4004 char_u *p1, *p2;
4005 int t;
4006
4007 // like rl_mirror(), but keep the space at the end
4008 p2 = skiptowhite(extra) - 1;
4009 for (p1 = extra; p1 < p2; ++p1, --p2)
4010 {
4011 t = *p1;
4012 *p1 = *p2;
4013 *p2 = t;
4014 }
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#endif
4017 p_extra = extra;
4018 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004019 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004024 c_final = NUL;
4025 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004026 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004027 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004028#ifdef FEAT_SYN_HL
4029 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004030 * the current line differently.
4031 * TODO: Can we use CursorLine instead of CursorLineNr
4032 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004033 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004034 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004035 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 }
4039
Bram Moolenaar597a4222014-06-25 14:39:50 +02004040#ifdef FEAT_LINEBREAK
4041 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4042 && n_extra == 0 && *p_sbr != NUL)
4043 /* draw indent after showbreak value */
4044 draw_state = WL_BRI;
4045 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4046 /* After the showbreak, draw the breakindent */
4047 draw_state = WL_BRI - 1;
4048
4049 /* draw 'breakindent': indent wrapped text accordingly */
4050 if (draw_state == WL_BRI - 1 && n_extra == 0)
4051 {
4052 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004053 /* if need_showbreak is set, breakindent also applies */
4054 if (wp->w_p_bri && n_extra == 0
4055 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004056# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004057 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004058# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004059 )
4060 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004061 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004062# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004063 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004064 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004065 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004066# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004067 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4068 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004069 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004070# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004071 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004072# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004073 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004074 c_extra = ' ';
4075 n_extra = get_breakindent_win(wp,
4076 ml_get_buf(wp->w_buffer, lnum, FALSE));
4077 /* Correct end of highlighted area for 'breakindent',
4078 * required when 'linebreak' is also set. */
4079 if (tocol == vcol)
4080 tocol += n_extra;
4081 }
4082 }
4083#endif
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4086 if (draw_state == WL_SBR - 1 && n_extra == 0)
4087 {
4088 draw_state = WL_SBR;
4089# ifdef FEAT_DIFF
4090 if (filler_todo > 0)
4091 {
4092 /* Draw "deleted" diff line(s). */
4093 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004096 c_final = NUL;
4097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004101 c_final = NUL;
4102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103# ifdef FEAT_RIGHTLEFT
4104 if (wp->w_p_rl)
4105 n_extra = col + 1;
4106 else
4107# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004108 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004109 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111# endif
4112# ifdef FEAT_LINEBREAK
4113 if (*p_sbr != NUL && need_showbreak)
4114 {
4115 /* Draw 'showbreak' at the start of each broken line. */
4116 p_extra = p_sbr;
4117 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004118 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004120 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004122 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 /* Correct end of highlighted area for 'showbreak',
4124 * required when 'linebreak' is also set. */
4125 if (tocol == vcol)
4126 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004127#ifdef FEAT_SYN_HL
4128 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004129 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004130 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004131 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134# endif
4135 }
4136#endif
4137
4138 if (draw_state == WL_LINE - 1 && n_extra == 0)
4139 {
4140 draw_state = WL_LINE;
4141 if (saved_n_extra)
4142 {
4143 /* Continue item from end of wrapped line. */
4144 n_extra = saved_n_extra;
4145 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004146 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 p_extra = saved_p_extra;
4148 char_attr = saved_char_attr;
4149 }
4150 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004151 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 }
4154
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004155 // When still displaying '$' of change command, stop at cursor.
4156 // When only displaying the (relative) line number and that's done,
4157 // stop here.
4158 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004159 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160#ifdef FEAT_DIFF
4161 && filler_todo <= 0
4162#endif
4163 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004164 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004166 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004167 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004168 /* Pretend we have finished updating the window. Except when
4169 * 'cursorcolumn' is set. */
4170#ifdef FEAT_SYN_HL
4171 if (wp->w_p_cuc)
4172 row = wp->w_cline_row + wp->w_cline_height;
4173 else
4174#endif
4175 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 break;
4177 }
4178
Bram Moolenaar637532b2019-01-03 21:44:40 +01004179 if (draw_state == WL_LINE && (area_highlighting
4180#ifdef FEAT_SPELL
4181 || has_spell
4182#endif
4183 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 /* handle Visual or match highlighting in this line */
4186 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4188 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004190 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004192 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else if (area_attr != 0
4194 && (vcol == tocol
4195 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198#ifdef FEAT_SEARCH_EXTRA
4199 if (!n_extra)
4200 {
4201 /*
4202 * Check for start/end of search pattern match.
4203 * After end, check for start/end of next match.
4204 * When another match, have to check for start again.
4205 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004206 * Do this for 'search_hl' and the match list (ordered by
4207 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004209 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004210 cur = wp->w_match_head;
4211 shl_flag = FALSE;
4212 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004214 if (shl_flag == FALSE
4215 && ((cur != NULL
4216 && cur->priority > SEARCH_HL_PRIORITY)
4217 || cur == NULL))
4218 {
4219 shl = &search_hl;
4220 shl_flag = TRUE;
4221 }
4222 else
4223 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004224 if (cur != NULL)
4225 cur->pos.cur = 0;
4226 pos_inprogress = TRUE;
4227 while (shl->rm.regprog != NULL
4228 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004230 if (shl->startcol != MAXCOL
4231 && v >= (long)shl->startcol
4232 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004234 int tmp_col = v + MB_PTR2LEN(ptr);
4235
4236 if (shl->endcol < tmp_col)
4237 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004239#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004240 // Match with the "Conceal" group results in hiding
4241 // the match.
4242 if (cur != NULL
4243 && shl != &search_hl
4244 && syn_name2id((char_u *)"Conceal")
4245 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004246 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004247 has_match_conc =
4248 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004249 match_conc = cur->conceal_char;
4250 }
4251 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004252 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004255 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
4257 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004258 next_search_hl(wp, shl, lnum, (colnr_T)v,
4259 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004260 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004261 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /* Need to get the line again, a multi-line regexp
4264 * may have made it invalid. */
4265 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4266 ptr = line + v;
4267
4268 if (shl->lnum == lnum)
4269 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004270 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004272 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004274 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004276 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
4278 /* highlight empty match, try again after
4279 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004281 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004282 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004284 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286
4287 /* Loop to check if the match starts at the
4288 * current position */
4289 continue;
4290 }
4291 }
4292 break;
4293 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004294 if (shl != &search_hl && cur != NULL)
4295 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004297
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004298 /* Use attributes from match with highest priority among
4299 * 'search_hl' and the match list. */
4300 search_attr = search_hl.attr_cur;
4301 cur = wp->w_match_head;
4302 shl_flag = FALSE;
4303 while (cur != NULL || shl_flag == FALSE)
4304 {
4305 if (shl_flag == FALSE
4306 && ((cur != NULL
4307 && cur->priority > SEARCH_HL_PRIORITY)
4308 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004309 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004310 shl = &search_hl;
4311 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004312 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004313 else
4314 shl = &cur->hl;
4315 if (shl->attr_cur != 0)
4316 search_attr = shl->attr_cur;
4317 if (shl != &search_hl && cur != NULL)
4318 cur = cur->next;
4319 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004320 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004321 if (*ptr == NUL && (did_line_attr >= 1
4322 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004323 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325#endif
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004328 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004330 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4331 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004333 if (diff_hlf == HLF_TXD && ptr - line > change_end
4334 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004336 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004337 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004338 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004341
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004342#ifdef FEAT_TEXT_PROP
4343 if (text_props != NULL)
4344 {
4345 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004346 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004347
4348 // Check if any active property ends.
4349 for (pi = 0; pi < text_props_active; ++pi)
4350 {
4351 int tpi = text_prop_idxs[pi];
4352
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004353 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004354 + text_props[tpi].tp_len)
4355 {
4356 if (pi + 1 < text_props_active)
4357 mch_memmove(text_prop_idxs + pi,
4358 text_prop_idxs + pi + 1,
4359 sizeof(int)
4360 * (text_props_active - (pi + 1)));
4361 --text_props_active;
4362 --pi;
4363 }
4364 }
4365
4366 // Add any text property that starts in this column.
4367 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004368 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004369 text_prop_idxs[text_props_active++] = text_prop_next++;
4370
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004371 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004372 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004373 if (text_props_active > 0)
4374 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004375 // Sort the properties on priority and/or starting last.
4376 // Then combine the attributes, highest priority last.
4377 current_text_props = text_props;
4378 current_buf = wp->w_buffer;
4379 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4380 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004381
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004382 for (pi = 0; pi < text_props_active; ++pi)
4383 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004384 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004385 proptype_T *pt = text_prop_type_by_id(
4386 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004387
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004388 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004389 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004390 int pt_attr = syn_id2attr(pt->pt_hl_id);
4391
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004392 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004393 text_prop_attr =
4394 hl_combine_attr(text_prop_attr, pt_attr);
4395 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004396 }
4397 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004398 }
4399 }
4400#endif
4401
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004402 /* Decide which of the highlight attributes to use. */
4403 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004404#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004405 if (area_attr != 0)
4406 char_attr = hl_combine_attr(line_attr, area_attr);
4407 else if (search_attr != 0)
4408 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004409# ifdef FEAT_TEXT_PROP
4410 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004411 {
4412 char_attr = hl_combine_attr(
4413 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4414 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004415# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004416 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004417 || vcol < fromcol || vcol_prev < fromcol_prev
4418 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004419 // Use line_attr when not in the Visual or 'incsearch' area
4420 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004421 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004422#else
4423 if (area_attr != 0)
4424 char_attr = area_attr;
4425 else if (search_attr != 0)
4426 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004427#endif
4428 else
4429 {
4430 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004431#ifdef FEAT_TEXT_PROP
4432 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004433 {
4434 if (text_prop_combine)
4435 char_attr = hl_combine_attr(
4436 syntax_attr, text_prop_attr);
4437 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004438 char_attr = hl_combine_attr(
4439 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004440 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004441 else
4442#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004443#ifdef FEAT_SYN_HL
4444 if (has_syntax)
4445 char_attr = syntax_attr;
4446 else
4447#endif
4448 char_attr = 0;
4449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004451 if (char_attr == 0)
4452 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
4455 * Get the next character to put on the screen.
4456 */
4457 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004458 * The "p_extra" points to the extra stuff that is inserted to
4459 * represent special characters (non-printable stuff) and other
4460 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004461 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004462 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4463 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4465 */
4466 if (n_extra > 0)
4467 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004468 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004470 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004472 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
4474 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004475 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004476 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 else
4479 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481 else
4482 {
4483 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (has_mbyte)
4485 {
4486 mb_c = c;
4487 if (enc_utf8)
4488 {
4489 /* If the UTF-8 character is more than one byte:
4490 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004491 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 mb_utf8 = FALSE;
4493 if (mb_l > n_extra)
4494 mb_l = 1;
4495 else if (mb_l > 1)
4496 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004499 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 }
4502 else
4503 {
4504 /* if this is a DBCS character, put it in "mb_c" */
4505 mb_l = MB_BYTE2LEN(c);
4506 if (mb_l >= n_extra)
4507 mb_l = 1;
4508 else if (mb_l > 1)
4509 mb_c = (c << 8) + p_extra[1];
4510 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004511 if (mb_l == 0) /* at the NUL at end-of-line */
4512 mb_l = 1;
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 /* If a double-width char doesn't fit display a '>' in the
4515 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004516 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517# ifdef FEAT_RIGHTLEFT
4518 wp->w_p_rl ? (col <= 0) :
4519# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004520 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 && (*mb_char2cells)(mb_c) == 2)
4522 {
4523 c = '>';
4524 mb_c = c;
4525 mb_l = 1;
4526 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004527 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 /* put the pointer back to output the double-width
4529 * character at the start of the next line. */
4530 ++n_extra;
4531 --p_extra;
4532 }
4533 else
4534 {
4535 n_extra -= mb_l - 1;
4536 p_extra += mb_l - 1;
4537 }
4538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 ++p_extra;
4540 }
4541 --n_extra;
4542 }
4543 else
4544 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004545#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004546 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004547#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004548
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004549 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004550 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 /*
4552 * Get a character from the line itself.
4553 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004554 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004555#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004556 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (has_mbyte)
4559 {
4560 mb_c = c;
4561 if (enc_utf8)
4562 {
4563 /* If the UTF-8 character is more than one byte: Decode it
4564 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004565 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 mb_utf8 = FALSE;
4567 if (mb_l > 1)
4568 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004569 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 /* Overlong encoded ASCII or ASCII with composing char
4571 * is displayed normally, except a NUL. */
4572 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004573 {
4574 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004575#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004576 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004577#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004580
4581 /* At start of the line we can have a composing char.
4582 * Draw it as a space with a composing char. */
4583 if (utf_iscomposing(mb_c))
4584 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004585 int i;
4586
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587 for (i = Screen_mco - 1; i > 0; --i)
4588 u8cc[i] = u8cc[i - 1];
4589 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004590 mb_c = ' ';
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593
4594 if ((mb_l == 1 && c >= 0x80)
4595 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004596 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
4598 /*
4599 * Illegal UTF-8 byte: display as <xx>.
4600 * Non-BMP character : display as ? or fullwidth ?.
4601 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004602 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004604 if (wp->w_p_rl) /* reverse */
4605 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 p_extra = extra;
4608 c = *p_extra;
4609 mb_c = mb_ptr2char_adv(&p_extra);
4610 mb_utf8 = (c >= 0x80);
4611 n_extra = (int)STRLEN(p_extra);
4612 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004613 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (area_attr == 0 && search_attr == 0)
4615 {
4616 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004617 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 saved_attr2 = char_attr; /* save current attr */
4619 }
4620 }
4621 else if (mb_l == 0) /* at the NUL at end-of-line */
4622 mb_l = 1;
4623#ifdef FEAT_ARABIC
4624 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4625 {
4626 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004627 int pc, pc1, nc;
4628 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
4630 /* The idea of what is the previous and next
4631 * character depends on 'rightleft'. */
4632 if (wp->w_p_rl)
4633 {
4634 pc = prev_c;
4635 pc1 = prev_c1;
4636 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004637 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 else
4640 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004641 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004643 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 prev_c = mb_c;
4646
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004647 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 else
4650 prev_c = mb_c;
4651#endif
4652 }
4653 else /* enc_dbcs */
4654 {
4655 mb_l = MB_BYTE2LEN(c);
4656 if (mb_l == 0) /* at the NUL at end-of-line */
4657 mb_l = 1;
4658 else if (mb_l > 1)
4659 {
4660 /* We assume a second byte below 32 is illegal.
4661 * Hopefully this is OK for all double-byte encodings!
4662 */
4663 if (ptr[1] >= 32)
4664 mb_c = (c << 8) + ptr[1];
4665 else
4666 {
4667 if (ptr[1] == NUL)
4668 {
4669 /* head byte at end of line */
4670 mb_l = 1;
4671 transchar_nonprint(extra, c);
4672 }
4673 else
4674 {
4675 /* illegal tail byte */
4676 mb_l = 2;
4677 STRCPY(extra, "XX");
4678 }
4679 p_extra = extra;
4680 n_extra = (int)STRLEN(extra) - 1;
4681 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004682 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 c = *p_extra++;
4684 if (area_attr == 0 && search_attr == 0)
4685 {
4686 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004687 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 saved_attr2 = char_attr; /* save current attr */
4689 }
4690 mb_c = c;
4691 }
4692 }
4693 }
4694 /* If a double-width char doesn't fit display a '>' in the
4695 * last column; the character is displayed at the start of the
4696 * next line. */
4697 if ((
4698# ifdef FEAT_RIGHTLEFT
4699 wp->w_p_rl ? (col <= 0) :
4700# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004701 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 && (*mb_char2cells)(mb_c) == 2)
4703 {
4704 c = '>';
4705 mb_c = c;
4706 mb_utf8 = FALSE;
4707 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004708 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004709 // Put pointer back so that the character will be
4710 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004712#ifdef FEAT_CONCEAL
4713 did_decrement_ptr = TRUE;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716 else if (*ptr != NUL)
4717 ptr += mb_l - 1;
4718
4719 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004720 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004721 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004722 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004725 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004726 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 c = ' ';
4728 if (area_attr == 0 && search_attr == 0)
4729 {
4730 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004731 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 saved_attr2 = char_attr; /* save current attr */
4733 }
4734 mb_c = c;
4735 mb_utf8 = FALSE;
4736 mb_l = 1;
4737 }
4738
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 ++ptr;
4741
4742 if (extra_check)
4743 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004744#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004745 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004746#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004747
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004748#ifdef FEAT_TERMINAL
4749 if (get_term_attr)
4750 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004751 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004752
4753 if (!attr_pri)
4754 char_attr = syntax_attr;
4755 else
4756 char_attr = hl_combine_attr(syntax_attr, char_attr);
4757 }
4758#endif
4759
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004760#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004761 // Get syntax attribute, unless still at the start of the line
4762 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004763 v = (long)(ptr - line);
4764 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 /* Get the syntax attribute for the character. If there
4767 * is an error, disable syntax highlighting. */
4768 save_did_emsg = did_emsg;
4769 did_emsg = FALSE;
4770
Bram Moolenaar217ad922005-03-20 22:37:15 +00004771 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004772# ifdef FEAT_SPELL
4773 has_spell ? &can_spell :
4774# endif
4775 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004778 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004779 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004780 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004781 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
4784 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004785
4786 // combine syntax attribute with 'wincolor'
4787 if (win_attr != 0)
4788 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4789
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004790#ifdef SYN_TIME_LIMIT
4791 if (wp->w_s->b_syn_slow)
4792 has_syntax = FALSE;
4793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795 /* Need to get the line again, a multi-line regexp may
4796 * have made it invalid. */
4797 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4798 ptr = line + v;
4799
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004800# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004801 // Text properties overrule syntax highlighting or combine.
4802 if (text_prop_attr == 0 || text_prop_combine)
4803# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004804 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004805 int comb_attr = syntax_attr;
4806# ifdef FEAT_TEXT_PROP
4807 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4808# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004809 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004810 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004811 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004812 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004813 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004814# ifdef FEAT_CONCEAL
4815 /* no concealing past the end of the line, it interferes
4816 * with line highlighting */
4817 if (c == NUL)
4818 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004819 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004820 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004823#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004824
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004825#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004826 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004827 * Only do this when there is no syntax highlighting, the
4828 * @Spell cluster is not used or the current syntax item
4829 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004830 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004831 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004832 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004833 if (c != 0 && (
4834# ifdef FEAT_SYN_HL
4835 !has_syntax ||
4836# endif
4837 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004838 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004839 char_u *prev_ptr, *p;
4840 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004841 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004842 if (has_mbyte)
4843 {
4844 prev_ptr = ptr - mb_l;
4845 v -= mb_l - 1;
4846 }
4847 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004848 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004849
4850 /* Use nextline[] if possible, it has the start of the
4851 * next line concatenated. */
4852 if ((prev_ptr - line) - nextlinecol >= 0)
4853 p = nextline + (prev_ptr - line) - nextlinecol;
4854 else
4855 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004856 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004857 len = spell_check(wp, p, &spell_hlf, &cap_col,
4858 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004859 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004860
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004861 /* In Insert mode only highlight a word that
4862 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004863 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004864 && (State & INSERT) != 0
4865 && wp->w_cursor.lnum == lnum
4866 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004867 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004868 && wp->w_cursor.col < (colnr_T)word_end)
4869 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004870 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004871 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004872 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004873
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004874 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004875 && (p - nextline) + len > nextline_idx)
4876 {
4877 /* Remember that the good word continues at the
4878 * start of the next line. */
4879 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004880 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004881 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004882
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004883 /* Turn index into actual attributes. */
4884 if (spell_hlf != HLF_COUNT)
4885 spell_attr = highlight_attr[spell_hlf];
4886
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004887 if (cap_col > 0)
4888 {
4889 if (p != prev_ptr
4890 && (p - nextline) + cap_col >= nextline_idx)
4891 {
4892 /* Remember that the word in the next line
4893 * must start with a capital. */
4894 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004895 cap_col = (int)((p - nextline) + cap_col
4896 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004897 }
4898 else
4899 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004900 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004901 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004902 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004903 }
4904 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004905 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004906 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004907 char_attr = hl_combine_attr(char_attr, spell_attr);
4908 else
4909 char_attr = hl_combine_attr(spell_attr, char_attr);
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#endif
4912#ifdef FEAT_LINEBREAK
4913 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004914 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004916 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004917 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004919 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004920 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004921
Bram Moolenaar597a4222014-06-25 14:39:50 +02004922 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004923 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004924 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004925 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004926# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004927 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004928 wp->w_buffer->b_p_vts_array) - 1;
4929# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004930 n_extra = (int)wp->w_buffer->b_p_ts
4931 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004932# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004933
Bram Moolenaar4df70292015-03-21 14:20:16 +01004934 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004935 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004936 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004937 {
4938#ifdef FEAT_CONCEAL
4939 if (c == TAB)
4940 /* See "Tab alignment" below. */
4941 FIX_FOR_BOGUSCOLS;
4942#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004943 if (!wp->w_p_list)
4944 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947#endif
4948
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004949 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4950 // But not when the character is followed by a composing
4951 // character (use mb_l to check that).
4952 if (wp->w_p_list
4953 && ((((c == 160 && mb_l == 1)
4954 || (mb_utf8
4955 && ((mb_c == 160 && mb_l == 2)
4956 || (mb_c == 0x202f && mb_l == 3))))
4957 && lcs_nbsp)
4958 || (c == ' '
4959 && mb_l == 1
4960 && lcs_space
4961 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004962 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004963 c = (c == ' ') ? lcs_space : lcs_nbsp;
4964 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004965 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004966 n_attr = 1;
4967 extra_attr = HL_ATTR(HLF_8);
4968 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004969 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004970 mb_c = c;
4971 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004972 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004973 mb_utf8 = TRUE;
4974 u8cc[0] = 0;
4975 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004976 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004977 else
4978 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004979 }
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4982 {
4983 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004984 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004987 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 saved_attr2 = char_attr; /* save current attr */
4989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004991 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004994 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004995 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 else
4998 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
5000 }
5001
5002 /*
5003 * Handling of non-printable characters.
5004 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005005 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 /*
5008 * when getting a character from the file, we may have to
5009 * turn it into something else on the way to putting it
5010 * into "ScreenLines".
5011 */
5012 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5013 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005014 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005015 long vcol_adjusted = vcol; /* removed showbreak length */
5016#ifdef FEAT_LINEBREAK
5017 /* only adjust the tab_len, when at the first column
5018 * after the showbreak value was drawn */
5019 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5020 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005023#ifdef FEAT_VARTABS
5024 tab_len = tabstop_padding(vcol_adjusted,
5025 wp->w_buffer->b_p_ts,
5026 wp->w_buffer->b_p_vts_array) - 1;
5027#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005028 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005029 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5030#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005031
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005032#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005033 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005034#endif
5035 /* tab amount depends on current column */
5036 n_extra = tab_len;
5037#ifdef FEAT_LINEBREAK
5038 else
5039 {
5040 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005041 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005042 int i;
5043 int saved_nextra = n_extra;
5044
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005045#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005046 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005047 /* there are characters to conceal */
5048 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005049 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5050 */
5051 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5052 && n_extra > tab_len)
5053 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005054#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005055
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005056 /* if n_extra > 0, it gives the number of chars, to
5057 * use for a tab, else we need to calculate the width
5058 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005059 len = (tab_len * mb_char2len(lcs_tab2));
5060 if (n_extra > 0)
5061 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005062 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005063 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005064 vim_memset(p, ' ', len);
5065 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005066 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005067 p_extra_free = p;
5068 for (i = 0; i < tab_len; i++)
5069 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005070 if (*p == NUL)
5071 {
5072 tab_len = i;
5073 break;
5074 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005075 mb_char2bytes(lcs_tab2, p);
5076 p += mb_char2len(lcs_tab2);
5077 n_extra += mb_char2len(lcs_tab2)
5078 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005079 }
5080 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005081#ifdef FEAT_CONCEAL
5082 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5083 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005084 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005085 n_extra -= vcol_off;
5086#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005087 }
5088#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005089#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005090 {
5091 int vc_saved = vcol_off;
5092
5093 /* Tab alignment should be identical regardless of
5094 * 'conceallevel' value. So tab compensates of all
5095 * previous concealed characters, and thus resets
5096 * vcol_off and boguscols accumulated so far in the
5097 * line. Note that the tab can be longer than
5098 * 'tabstop' when there are concealed characters. */
5099 FIX_FOR_BOGUSCOLS;
5100
5101 /* Make sure, the highlighting for the tab char will be
5102 * correctly set further below (effectively reverts the
5103 * FIX_FOR_BOGSUCOLS macro */
5104 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005105 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005106 tab_len += vc_saved;
5107 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if (wp->w_p_list)
5111 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005112 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005113#ifdef FEAT_LINEBREAK
5114 if (wp->w_p_lbr)
5115 c_extra = NUL; /* using p_extra from above */
5116 else
5117#endif
5118 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005119 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005120 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005121 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005124 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005127 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005128 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 else
5132 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005133 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 c_extra = ' ';
5135 c = ' ';
5136 }
5137 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005138 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005139 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005140 || ((fromcol >= 0 || fromcol_prev >= 0)
5141 && tocol > vcol
5142 && VIsual_mode != Ctrl_V
5143 && (
5144# ifdef FEAT_RIGHTLEFT
5145 wp->w_p_rl ? (col >= 0) :
5146# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005147 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005148 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005149 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005150 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005151 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005153 /* Display a '$' after the line or highlight an extra
5154 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5156 /* For a diff line the highlighting continues after the
5157 * "$". */
5158 if (
5159# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005160 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161# ifdef LINE_ATTR
5162 &&
5163# endif
5164# endif
5165# ifdef LINE_ATTR
5166 line_attr == 0
5167# endif
5168 )
5169#endif
5170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 /* In virtualedit, visual selections may extend
5172 * beyond end of line. */
5173 if (area_highlighting && virtual_active()
5174 && tocol != MAXCOL && vcol < tocol)
5175 n_extra = 0;
5176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178 p_extra = at_end_str;
5179 n_extra = 1;
5180 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005181 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005184 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005185 c = lcs_eol;
5186 else
5187 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 lcs_eol_one = -1;
5189 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005190 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005192 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 n_attr = 1;
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005196 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
5198 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005199 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005200 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 else
5203 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205 else if (c != NUL)
5206 {
5207 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005208 if (n_extra == 0)
5209 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#ifdef FEAT_RIGHTLEFT
5211 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5212 rl_mirror(p_extra); /* reverse "<12>" */
5213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005215 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005216#ifdef FEAT_LINEBREAK
5217 if (wp->w_p_lbr)
5218 {
5219 char_u *p;
5220
5221 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005222 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005223 vim_memset(p, ' ', n_extra);
5224 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5225 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005226 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005227 p_extra_free = p_extra = p;
5228 }
5229 else
5230#endif
5231 {
5232 n_extra = byte2cells(c) - 1;
5233 c = *p_extra++;
5234 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005235 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
5237 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005238 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 saved_attr2 = char_attr; /* save current attr */
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else if (VIsual_active
5244 && (VIsual_mode == Ctrl_V
5245 || VIsual_mode == 'v')
5246 && virtual_active()
5247 && tocol != MAXCOL
5248 && vcol < tocol
5249 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005250#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005252#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005253 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
5255 c = ' ';
5256 --ptr; /* put it back at the NUL */
5257 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005258#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else if ((
5260# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005261 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005263# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005264 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 ) && (
5268# ifdef FEAT_RIGHTLEFT
5269 wp->w_p_rl ? (col >= 0) :
5270# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005271 (col
5272# ifdef FEAT_CONCEAL
5273 - boguscols
5274# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005275 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
5277 /* Highlight until the right side of the window */
5278 c = ' ';
5279 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005280
5281 /* Remember we do the char for line highlighting. */
5282 ++did_line_attr;
5283
5284 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005285 if (line_attr != 0 && char_attr == search_attr
5286 && (did_line_attr > 1
5287 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005288 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289# ifdef FEAT_DIFF
5290 if (diff_hlf == HLF_TXD)
5291 {
5292 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005293 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005294 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005295 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005296 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5297 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005298 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005302# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005303 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005304 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005305 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005306 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5307 char_attr = hl_combine_attr(char_attr,
5308 HL_ATTR(HLF_CUL));
5309 }
5310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 }
5312#endif
5313 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005314
5315#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005316 if ( wp->w_p_cole > 0
5317 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005318 conceal_cursor_line(wp))
5319 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005320 && !(lnum_in_visual_area
5321 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005322 {
5323 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005324 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005325 && (syn_get_sub_char() != NUL || match_conc
5326 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005327 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005328 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005329 /* First time at this concealed item: display one
5330 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005331 if (match_conc)
5332 c = match_conc;
5333 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005334 c = syn_get_sub_char();
5335 else if (lcs_conceal != NUL)
5336 c = lcs_conceal;
5337 else
5338 c = ' ';
5339
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005340 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005341
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005342 if (n_extra > 0)
5343 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005344 vcol += n_extra;
5345 if (wp->w_p_wrap && n_extra > 0)
5346 {
5347# ifdef FEAT_RIGHTLEFT
5348 if (wp->w_p_rl)
5349 {
5350 col -= n_extra;
5351 boguscols -= n_extra;
5352 }
5353 else
5354# endif
5355 {
5356 boguscols += n_extra;
5357 col += n_extra;
5358 }
5359 }
5360 n_extra = 0;
5361 n_attr = 0;
5362 }
5363 else if (n_skip == 0)
5364 {
5365 is_concealing = TRUE;
5366 n_skip = 1;
5367 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005368 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005369 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005370 {
5371 mb_utf8 = TRUE;
5372 u8cc[0] = 0;
5373 c = 0xc0;
5374 }
5375 else
5376 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005377 }
5378 else
5379 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005380 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005381 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005382 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005383
5384 if (n_skip > 0 && did_decrement_ptr)
5385 // not showing the '>', put pointer back to avoid getting stuck
5386 ++ptr;
5387
5388#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005391#ifdef FEAT_CONCEAL
5392 /* In the cursor line and we may be concealing characters: correct
5393 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005394 if (!did_wcol && draw_state == WL_LINE
5395 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005396 && conceal_cursor_line(wp)
5397 && (int)wp->w_virtcol <= vcol + n_skip)
5398 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005399# ifdef FEAT_RIGHTLEFT
5400 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005401 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005402 else
5403# endif
5404 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005405 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005406 did_wcol = TRUE;
5407 }
5408#endif
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 /* Don't override visual selection highlighting. */
5411 if (n_attr > 0
5412 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005413 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 char_attr = extra_attr;
5415
Bram Moolenaar81695252004-12-29 20:58:21 +00005416#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 /* XIM don't send preedit_start and preedit_end, but they send
5418 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5419 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005420 if (p_imst == IM_ON_THE_SPOT
5421 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005422 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 && (State & INSERT)
5424 && !p_imdisable
5425 && im_is_preediting()
5426 && draw_state == WL_LINE)
5427 {
5428 colnr_T tcol;
5429
5430 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005431 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 else
5433 tcol = preedit_end_col;
5434 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5435 {
5436 if (feedback_old_attr < 0)
5437 {
5438 feedback_col = 0;
5439 feedback_old_attr = char_attr;
5440 }
5441 char_attr = im_get_feedback_attr(feedback_col);
5442 if (char_attr < 0)
5443 char_attr = feedback_old_attr;
5444 feedback_col++;
5445 }
5446 else if (feedback_old_attr >= 0)
5447 {
5448 char_attr = feedback_old_attr;
5449 feedback_old_attr = -1;
5450 feedback_col = 0;
5451 }
5452 }
5453#endif
5454 /*
5455 * Handle the case where we are in column 0 but not on the first
5456 * character of the line and the user wants us to show us a
5457 * special character (via 'listchars' option "precedes:<char>".
5458 */
5459 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005460 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5462#ifdef FEAT_DIFF
5463 && filler_todo <= 0
5464#endif
5465 && draw_state > WL_NR
5466 && c != NUL)
5467 {
5468 c = lcs_prec;
5469 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005470 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5471 {
5472 /* Double-width character being overwritten by the "precedes"
5473 * character, need to fill up half the character. */
5474 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005475 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005476 n_extra = 1;
5477 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005478 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005481 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005484 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005485 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else
5488 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005489 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005492 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 n_attr3 = 1;
5494 }
5495 }
5496
5497 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005498 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005500 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005501#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005502 || did_line_attr == 1
5503#endif
5504 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005506#ifdef FEAT_SEARCH_EXTRA
5507 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005508
5509 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005510 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005511 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005512#endif
5513
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005514 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * highlight match at end of line. If it's beyond the last
5516 * char on the screen, just overwrite that one (tricky!) Not
5517 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005518#ifdef FEAT_SEARCH_EXTRA
5519 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005520 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005521 prevcol_hl_flag = TRUE;
5522 else
5523 {
5524 cur = wp->w_match_head;
5525 while (cur != NULL)
5526 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005527 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005528 {
5529 prevcol_hl_flag = TRUE;
5530 break;
5531 }
5532 cur = cur->next;
5533 }
5534 }
5535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005537 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005538 && (VIsual_mode != Ctrl_V
5539 || lnum == VIsual.lnum
5540 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005541 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542#ifdef FEAT_SEARCH_EXTRA
5543 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005544 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005545# ifdef FEAT_SYN_HL
5546 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5547 && !(wp == curwin && VIsual_active))
5548# endif
5549# ifdef FEAT_DIFF
5550 && diff_hlf == (hlf_T)0
5551# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005552# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005553 && did_line_attr <= 1
5554# endif
5555 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556#endif
5557 ))
5558 {
5559 int n = 0;
5560
5561#ifdef FEAT_RIGHTLEFT
5562 if (wp->w_p_rl)
5563 {
5564 if (col < 0)
5565 n = 1;
5566 }
5567 else
5568#endif
5569 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005570 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 n = -1;
5572 }
5573 if (n != 0)
5574 {
5575 /* At the window boundary, highlight the last character
5576 * instead (better than nothing). */
5577 off += n;
5578 col += n;
5579 }
5580 else
5581 {
5582 /* Add a blank character to highlight. */
5583 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (enc_utf8)
5585 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587#ifdef FEAT_SEARCH_EXTRA
5588 if (area_attr == 0)
5589 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005590 /* Use attributes from match with highest priority among
5591 * 'search_hl' and the match list. */
5592 char_attr = search_hl.attr;
5593 cur = wp->w_match_head;
5594 shl_flag = FALSE;
5595 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005596 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005597 if (shl_flag == FALSE
5598 && ((cur != NULL
5599 && cur->priority > SEARCH_HL_PRIORITY)
5600 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005601 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005602 shl = &search_hl;
5603 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005604 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005605 else
5606 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005607 if ((ptr - line) - 1 == (long)shl->startcol
5608 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005609 char_attr = shl->attr;
5610 if (shl != &search_hl && cur != NULL)
5611 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614#endif
5615 ScreenAttrs[off] = char_attr;
5616#ifdef FEAT_RIGHTLEFT
5617 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005620 --off;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 else
5623#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005626 ++off;
5627 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005628 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005629#ifdef FEAT_SYN_HL
5630 eol_hl_off = 1;
5631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
Bram Moolenaar91170f82006-05-05 21:15:17 +00005635 /*
5636 * At end of the text line.
5637 */
5638 if (c == NUL)
5639 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005640#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005641 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005642 if (wp->w_p_wrap)
5643 v = wp->w_skipcol;
5644 else
5645 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005646
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005647 /* check if line ends before left margin */
5648 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005649 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005650#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005651 // Get rid of the boguscols now, we want to draw until the right
5652 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005653 col -= boguscols;
5654 boguscols = 0;
5655#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656
5657 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005658 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005659
5660 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005661 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5662 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005663 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005664 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005665 || draw_color_col
5666 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005667# ifdef FEAT_RIGHTLEFT
5668 && !wp->w_p_rl
5669# endif
5670 )
5671 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005672 int rightmost_vcol = 0;
5673 int i;
5674
5675 if (wp->w_p_cuc)
5676 rightmost_vcol = wp->w_virtcol;
5677 if (draw_color_col)
5678 /* determine rightmost colorcolumn to possibly draw */
5679 for (i = 0; color_cols[i] >= 0; ++i)
5680 if (rightmost_vcol < color_cols[i])
5681 rightmost_vcol = color_cols[i];
5682
Bram Moolenaar02631462017-09-22 15:20:32 +02005683 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005684 {
5685 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005686 if (enc_utf8)
5687 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005688 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005689 if (draw_color_col)
5690 draw_color_col = advance_color_col(VCOL_HLC,
5691 &color_cols);
5692
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005693 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005694 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005695 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005696 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005697 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005698 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005699
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005700 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005701 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005702
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005703 ++vcol;
5704 }
5705 }
5706#endif
5707
Bram Moolenaar53f81742017-09-22 14:35:51 +02005708 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005709 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 row++;
5711
5712 /*
5713 * Update w_cline_height and w_cline_folded if the cursor line was
5714 * updated (saves a call to plines() later).
5715 */
5716 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5717 {
5718 curwin->w_cline_row = startrow;
5719 curwin->w_cline_height = row - startrow;
5720#ifdef FEAT_FOLDING
5721 curwin->w_cline_folded = FALSE;
5722#endif
5723 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5724 }
5725
5726 break;
5727 }
5728
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005729 // Show "extends" character from 'listchars' if beyond the line end and
5730 // 'list' is set.
5731 if (lcs_ext != NUL
5732 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 && !wp->w_p_wrap
5734#ifdef FEAT_DIFF
5735 && filler_todo <= 0
5736#endif
5737 && (
5738#ifdef FEAT_RIGHTLEFT
5739 wp->w_p_rl ? col == 0 :
5740#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005741 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005743 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5745 {
5746 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005747 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005749 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005752 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005753 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755 else
5756 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005759#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760 /* advance to the next 'colorcolumn' */
5761 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005762 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005763
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005764 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005765 * highlight the cursor position itself.
5766 * Also highlight the 'colorcolumn' if it is different than
5767 * 'cursorcolumn' */
5768 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005769 if (draw_state == WL_LINE && !lnum_in_visual_area
5770 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005771 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005772 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005773 && lnum != wp->w_cursor.lnum)
5774 {
5775 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005776 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005777 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005778 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005779 {
5780 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005781 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005782 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005784#endif
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 /*
5787 * Store character to be displayed.
5788 * Skip characters that are left of the screen for 'nowrap'.
5789 */
5790 vcol_prev = vcol;
5791 if (draw_state < WL_LINE || n_skip <= 0)
5792 {
5793 /*
5794 * Store the character.
5795 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005796#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5798 {
5799 /* A double-wide character is: put first halve in left cell. */
5800 --off;
5801 --col;
5802 }
5803#endif
5804 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005806 {
5807 if ((mb_c & 0xff00) == 0x8e00)
5808 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 else if (enc_utf8)
5812 {
5813 if (mb_utf8)
5814 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005815 int i;
5816
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005818 if ((c & 0xff) == 0)
5819 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005820 for (i = 0; i < Screen_mco; ++i)
5821 {
5822 ScreenLinesC[i][off] = u8cc[i];
5823 if (u8cc[i] == 0)
5824 break;
5825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
5827 else
5828 ScreenLinesUC[off] = 0;
5829 }
5830 if (multi_attr)
5831 {
5832 ScreenAttrs[off] = multi_attr;
5833 multi_attr = 0;
5834 }
5835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 ScreenAttrs[off] = char_attr;
5837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5839 {
5840 /* Need to fill two screen columns. */
5841 ++off;
5842 ++col;
5843 if (enc_utf8)
5844 /* UTF-8: Put a 0 in the second screen char. */
5845 ScreenLines[off] = 0;
5846 else
5847 /* DBCS: Put second byte in the second screen char. */
5848 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005849 if (draw_state > WL_NR
5850#ifdef FEAT_DIFF
5851 && filler_todo <= 0
5852#endif
5853 )
5854 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* When "tocol" is halfway a character, set it to the end of
5856 * the character, otherwise highlighting won't stop. */
5857 if (tocol == vcol)
5858 ++tocol;
5859#ifdef FEAT_RIGHTLEFT
5860 if (wp->w_p_rl)
5861 {
5862 /* now it's time to backup one cell */
5863 --off;
5864 --col;
5865 }
5866#endif
5867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868#ifdef FEAT_RIGHTLEFT
5869 if (wp->w_p_rl)
5870 {
5871 --off;
5872 --col;
5873 }
5874 else
5875#endif
5876 {
5877 ++off;
5878 ++col;
5879 }
5880 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005881#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005882 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005883 {
5884 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005885 ++vcol_off;
5886 if (n_extra > 0)
5887 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005888 if (wp->w_p_wrap)
5889 {
5890 /*
5891 * Special voodoo required if 'wrap' is on.
5892 *
5893 * Advance the column indicator to force the line
5894 * drawing to wrap early. This will make the line
5895 * take up the same screen space when parts are concealed,
5896 * so that cursor line computations aren't messed up.
5897 *
5898 * To avoid the fictitious advance of 'col' causing
5899 * trailing junk to be written out of the screen line
5900 * we are building, 'boguscols' keeps track of the number
5901 * of bad columns we have advanced.
5902 */
5903 if (n_extra > 0)
5904 {
5905 vcol += n_extra;
5906# ifdef FEAT_RIGHTLEFT
5907 if (wp->w_p_rl)
5908 {
5909 col -= n_extra;
5910 boguscols -= n_extra;
5911 }
5912 else
5913# endif
5914 {
5915 col += n_extra;
5916 boguscols += n_extra;
5917 }
5918 n_extra = 0;
5919 n_attr = 0;
5920 }
5921
5922
Bram Moolenaar860cae12010-06-05 23:22:07 +02005923 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5924 {
5925 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005926# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005927 if (wp->w_p_rl)
5928 {
5929 --boguscols;
5930 --col;
5931 }
5932 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005933# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005934 {
5935 ++boguscols;
5936 ++col;
5937 }
5938 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005939
5940# ifdef FEAT_RIGHTLEFT
5941 if (wp->w_p_rl)
5942 {
5943 --boguscols;
5944 --col;
5945 }
5946 else
5947# endif
5948 {
5949 ++boguscols;
5950 ++col;
5951 }
5952 }
5953 else
5954 {
5955 if (n_extra > 0)
5956 {
5957 vcol += n_extra;
5958 n_extra = 0;
5959 n_attr = 0;
5960 }
5961 }
5962
5963 }
5964#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 else
5966 --n_skip;
5967
Bram Moolenaar64486672010-05-16 15:46:46 +02005968 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5969 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005970 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971#ifdef FEAT_DIFF
5972 && filler_todo <= 0
5973#endif
5974 )
5975 ++vcol;
5976
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005977#ifdef FEAT_SYN_HL
5978 if (vcol_save_attr >= 0)
5979 char_attr = vcol_save_attr;
5980#endif
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 /* restore attributes after "predeces" in 'listchars' */
5983 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5984 char_attr = saved_attr3;
5985
5986 /* restore attributes after last 'listchars' or 'number' char */
5987 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5988 char_attr = saved_attr2;
5989
5990 /*
5991 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005992 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 */
5994 if ((
5995#ifdef FEAT_RIGHTLEFT
5996 wp->w_p_rl ? (col < 0) :
5997#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005998 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 && (*ptr != NUL
6000#ifdef FEAT_DIFF
6001 || filler_todo > 0
6002#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006003 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6005 )
6006 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006007#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006008 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006009 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006010 boguscols = 0;
6011#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006012 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006013 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 ++row;
6016 ++screen_row;
6017
6018 /* When not wrapping and finished diff lines, or when displayed
6019 * '$' and highlighting until last column, break here. */
6020 if ((!wp->w_p_wrap
6021#ifdef FEAT_DIFF
6022 && filler_todo <= 0
6023#endif
6024 ) || lcs_eol_one == -1)
6025 break;
6026
6027 /* When the window is too narrow draw all "@" lines. */
6028 if (draw_state != WL_LINE
6029#ifdef FEAT_DIFF
6030 && filler_todo <= 0
6031#endif
6032 )
6033 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006034 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 row = endrow;
6037 }
6038
6039 /* When line got too long for screen break here. */
6040 if (row == endrow)
6041 {
6042 ++row;
6043 break;
6044 }
6045
6046 if (screen_cur_row == screen_row - 1
6047#ifdef FEAT_DIFF
6048 && filler_todo <= 0
6049#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006050 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 {
6052 /* Remember that the line wraps, used for modeless copy. */
6053 LineWraps[screen_row - 1] = TRUE;
6054
6055 /*
6056 * Special trick to make copy/paste of wrapped lines work with
6057 * xterm/screen: write an extra character beyond the end of
6058 * the line. This will work with all terminal types
6059 * (regardless of the xn,am settings).
6060 * Only do this on a fast tty.
6061 * Only do this if the cursor is on the current line
6062 * (something has been written in it).
6063 * Don't do this for the GUI.
6064 * Don't do this for double-width characters.
6065 * Don't do this for a window not at the right screen border.
6066 */
6067 if (p_tf
6068#ifdef FEAT_GUI
6069 && !gui.in_use
6070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006072 && ((*mb_off2cells)(LineOffset[screen_row],
6073 LineOffset[screen_row] + screen_Columns)
6074 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006076 + (int)Columns - 2,
6077 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006078 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
6080 /* First make sure we are at the end of the screen line,
6081 * then output the same character again to let the
6082 * terminal know about the wrap. If the terminal doesn't
6083 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006084 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 screen_char(LineOffset[screen_row - 1]
6086 + (unsigned)Columns - 1,
6087 screen_row - 1, (int)(Columns - 1));
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 /* When there is a multi-byte character, just output a
6090 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006091 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6092 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 out_char(' ');
6094 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 out_char(ScreenLines[LineOffset[screen_row - 1]
6096 + (Columns - 1)]);
6097 /* force a redraw of the first char on the next line */
6098 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6099 screen_start(); /* don't know where cursor is now */
6100 }
6101 }
6102
6103 col = 0;
6104 off = (unsigned)(current_ScreenLine - ScreenLines);
6105#ifdef FEAT_RIGHTLEFT
6106 if (wp->w_p_rl)
6107 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006108 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 off += col;
6110 }
6111#endif
6112
6113 /* reset the drawing state for the start of a wrapped line */
6114 draw_state = WL_START;
6115 saved_n_extra = n_extra;
6116 saved_p_extra = p_extra;
6117 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006118 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 saved_char_attr = char_attr;
6120 n_extra = 0;
6121 lcs_prec_todo = lcs_prec;
6122#ifdef FEAT_LINEBREAK
6123# ifdef FEAT_DIFF
6124 if (filler_todo <= 0)
6125# endif
6126 need_showbreak = TRUE;
6127#endif
6128#ifdef FEAT_DIFF
6129 --filler_todo;
6130 /* When the filler lines are actually below the last line of the
6131 * file, don't draw the line itself, break here. */
6132 if (filler_todo == 0 && wp->w_botfill)
6133 break;
6134#endif
6135 }
6136
6137 } /* for every character in the line */
6138
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006139#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006140 /* After an empty line check first word for capital. */
6141 if (*skipwhite(line) == NUL)
6142 {
6143 capcol_lnum = lnum + 1;
6144 cap_col = 0;
6145 }
6146#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006147#ifdef FEAT_TEXT_PROP
6148 vim_free(text_props);
6149 vim_free(text_prop_idxs);
6150#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006151
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006152 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 return row;
6154}
6155
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006156/*
6157 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006158 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006159 */
6160 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006161comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006162{
6163 int i;
6164
6165 for (i = 0; i < Screen_mco; ++i)
6166 {
6167 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6168 return TRUE;
6169 if (ScreenLinesC[i][off_from] == 0)
6170 break;
6171 }
6172 return FALSE;
6173}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175/*
6176 * Check whether the given character needs redrawing:
6177 * - the (first byte of the) character is different
6178 * - the attributes are different
6179 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006180 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 */
6182 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006183char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 if (cols > 0
6186 && ((ScreenLines[off_from] != ScreenLines[off_to]
6187 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 || (enc_dbcs != 0
6189 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6190 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6191 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6192 : (cols > 1 && ScreenLines[off_from + 1]
6193 != ScreenLines[off_to + 1])))
6194 || (enc_utf8
6195 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6196 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006197 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006198 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6199 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006200 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 return TRUE;
6202 return FALSE;
6203}
6204
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006205#if defined(FEAT_TERMINAL) || defined(PROTO)
6206/*
6207 * Return the index in ScreenLines[] for the current screen line.
6208 */
6209 int
6210screen_get_current_line_off()
6211{
6212 return (int)(current_ScreenLine - ScreenLines);
6213}
6214#endif
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216/*
6217 * Move one "cooked" screen line to the screen, but only the characters that
6218 * have actually changed. Handle insert/delete character.
6219 * "coloff" gives the first column on the screen for this line.
6220 * "endcol" gives the columns where valid characters are.
6221 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6222 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006223 * "flags" can have bits:
6224 * SLF_POPUP popup window
6225 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6227 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6228 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006230screen_line(
6231 int row,
6232 int coloff,
6233 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006234 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006235 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 unsigned off_from;
6238 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006239 unsigned max_off_from;
6240 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 int force = FALSE; /* force update rest of the line */
6244 int redraw_this /* bool: does character need redraw? */
6245#ifdef FEAT_GUI
6246 = TRUE /* For GUI when while-loop empty */
6247#endif
6248 ;
6249 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 int clear_next = FALSE;
6251 int char_cells; /* 1: normal char */
6252 /* 2: occupies two display cells */
6253# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006255 /* Check for illegal row and col, just in case. */
6256 if (row >= Rows)
6257 row = Rows - 1;
6258 if (endcol > Columns)
6259 endcol = Columns;
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261# ifdef FEAT_CLIPBOARD
6262 clip_may_clear_selection(row, row);
6263# endif
6264
6265 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6266 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006267 max_off_from = off_from + screen_Columns;
6268 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006271 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
6273 /* Clear rest first, because it's left of the text. */
6274 if (clear_width > 0)
6275 {
6276 while (col <= endcol && ScreenLines[off_to] == ' '
6277 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006278 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 {
6280 ++off_to;
6281 ++col;
6282 }
6283 if (col <= endcol)
6284 screen_fill(row, row + 1, col + coloff,
6285 endcol + coloff + 1, ' ', ' ', 0);
6286 }
6287 col = endcol + 1;
6288 off_to = LineOffset[row] + col + coloff;
6289 off_from += col;
6290 endcol = (clear_width > 0 ? clear_width : -clear_width);
6291 }
6292#endif /* FEAT_RIGHTLEFT */
6293
6294 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6295
6296 while (col < endcol)
6297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006299 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 else
6301 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 redraw_this = redraw_next;
6304 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6305 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6306
6307#ifdef FEAT_GUI
6308 /* If the next character was bold, then redraw the current character to
6309 * remove any pixels that might have spilt over into us. This only
6310 * happens in the GUI.
6311 */
6312 if (redraw_next && gui.in_use)
6313 {
6314 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006315 if (hl > HL_ALL)
6316 hl = syn_attr2attr(hl);
6317 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 redraw_this = TRUE;
6319 }
6320#endif
6321
6322 if (redraw_this)
6323 {
6324 /*
6325 * Special handling when 'xs' termcap flag set (hpterm):
6326 * Attributes for characters are stored at the position where the
6327 * cursor is when writing the highlighting code. The
6328 * start-highlighting code must be written with the cursor on the
6329 * first highlighted character. The stop-highlighting code must
6330 * be written with the cursor just after the last highlighted
6331 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006332 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 * to clear the rest of the line, and force redrawing it
6334 * completely.
6335 */
6336 if ( p_wiv
6337 && !force
6338#ifdef FEAT_GUI
6339 && !gui.in_use
6340#endif
6341 && ScreenAttrs[off_to] != 0
6342 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6343 {
6344 /*
6345 * Need to remove highlighting attributes here.
6346 */
6347 windgoto(row, col + coloff);
6348 out_str(T_CE); /* clear rest of this screen line */
6349 screen_start(); /* don't know where cursor is now */
6350 force = TRUE; /* force redraw of rest of the line */
6351 redraw_next = TRUE; /* or else next char would miss out */
6352
6353 /*
6354 * If the previous character was highlighted, need to stop
6355 * highlighting at this character.
6356 */
6357 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6358 {
6359 screen_attr = ScreenAttrs[off_to - 1];
6360 term_windgoto(row, col + coloff);
6361 screen_stop_highlight();
6362 }
6363 else
6364 screen_attr = 0; /* highlighting has stopped */
6365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 if (enc_dbcs != 0)
6367 {
6368 /* Check if overwriting a double-byte with a single-byte or
6369 * the other way around requires another character to be
6370 * redrawn. For UTF-8 this isn't needed, because comparing
6371 * ScreenLinesUC[] is sufficient. */
6372 if (char_cells == 1
6373 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006374 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
6376 /* Writing a single-cell character over a double-cell
6377 * character: need to redraw the next cell. */
6378 ScreenLines[off_to + 1] = 0;
6379 redraw_next = TRUE;
6380 }
6381 else if (char_cells == 2
6382 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006383 && (*mb_off2cells)(off_to, max_off_to) == 1
6384 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 {
6386 /* Writing the second half of a double-cell character over
6387 * a double-cell character: need to redraw the second
6388 * cell. */
6389 ScreenLines[off_to + 2] = 0;
6390 redraw_next = TRUE;
6391 }
6392
6393 if (enc_dbcs == DBCS_JPNU)
6394 ScreenLines2[off_to] = ScreenLines2[off_from];
6395 }
6396 /* When writing a single-width character over a double-width
6397 * character and at the end of the redrawn text, need to clear out
6398 * the right halve of the old character.
6399 * Also required when writing the right halve of a double-width
6400 * char over the left halve of an existing one. */
6401 if (has_mbyte && col + char_cells == endcol
6402 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006403 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006405 && (*mb_off2cells)(off_to, max_off_to) == 1
6406 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
6409 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (enc_utf8)
6411 {
6412 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6413 if (ScreenLinesUC[off_from] != 0)
6414 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006415 int i;
6416
6417 for (i = 0; i < Screen_mco; ++i)
6418 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 }
6420 }
6421 if (char_cells == 2)
6422 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
6424#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006425 /* The bold trick makes a single column of pixels appear in the
6426 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 * character should be redrawn too. This happens for our own GUI
6428 * and for some xterms. */
6429 if (
6430# ifdef FEAT_GUI
6431 gui.in_use
6432# endif
6433# if defined(FEAT_GUI) && defined(UNIX)
6434 ||
6435# endif
6436# ifdef UNIX
6437 term_is_xterm
6438# endif
6439 )
6440 {
6441 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006442 if (hl > HL_ALL)
6443 hl = syn_attr2attr(hl);
6444 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 redraw_next = TRUE;
6446 }
6447#endif
6448 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006449
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006450 /* For simplicity set the attributes of second half of a
6451 * double-wide character equal to the first half. */
6452 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006454
6455 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 screen_char(off_to, row, col + coloff);
6459 }
6460 else if ( p_wiv
6461#ifdef FEAT_GUI
6462 && !gui.in_use
6463#endif
6464 && col + coloff > 0)
6465 {
6466 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6467 {
6468 /*
6469 * Don't output stop-highlight when moving the cursor, it will
6470 * stop the highlighting when it should continue.
6471 */
6472 screen_attr = 0;
6473 }
6474 else if (screen_attr != 0)
6475 screen_stop_highlight();
6476 }
6477
6478 off_to += CHAR_CELLS;
6479 off_from += CHAR_CELLS;
6480 col += CHAR_CELLS;
6481 }
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (clear_next)
6484 {
6485 /* Clear the second half of a double-wide character of which the left
6486 * half was overwritten with a single-wide character. */
6487 ScreenLines[off_to] = ' ';
6488 if (enc_utf8)
6489 ScreenLinesUC[off_to] = 0;
6490 screen_char(off_to, row, col + coloff);
6491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
6493 if (clear_width > 0
6494#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006495 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496#endif
6497 )
6498 {
6499#ifdef FEAT_GUI
6500 int startCol = col;
6501#endif
6502
6503 /* blank out the rest of the line */
6504 while (col < clear_width && ScreenLines[off_to] == ' '
6505 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006506 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
6508 ++off_to;
6509 ++col;
6510 }
6511 if (col < clear_width)
6512 {
6513#ifdef FEAT_GUI
6514 /*
6515 * In the GUI, clearing the rest of the line may leave pixels
6516 * behind if the first character cleared was bold. Some bold
6517 * fonts spill over the left. In this case we redraw the previous
6518 * character too. If we didn't skip any blanks above, then we
6519 * only redraw if the character wasn't already redrawn anyway.
6520 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006521 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 {
6523 hl = ScreenAttrs[off_to];
6524 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006525 {
6526 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006527
Bram Moolenaar9c697322006-10-09 20:11:17 +00006528 if (enc_utf8)
6529 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6530 * that its width is 2. */
6531 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6532 else if (enc_dbcs != 0)
6533 {
6534 /* find previous character by counting from first
6535 * column and get its width. */
6536 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006537 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006538
6539 while (off < off_to)
6540 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006541 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006542 off += prev_cells;
6543 }
6544 }
6545
6546 if (enc_dbcs != 0 && prev_cells > 1)
6547 screen_char_2(off_to - prev_cells, row,
6548 col + coloff - prev_cells);
6549 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006550 screen_char(off_to - prev_cells, row,
6551 col + coloff - prev_cells);
6552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
6554#endif
6555 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6556 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 off_to += clear_width - col;
6558 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 }
6560 }
6561
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006562 if (clear_width > 0
6563#ifdef FEAT_TEXT_PROP
6564 && !(flags & SLF_POPUP) // no separator for popup window
6565#endif
6566 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006568 // For a window that has a right neighbor, draw the separator char
6569 // right of the window contents.
6570 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
6572 int c;
6573
6574 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006575 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006576 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6577 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 || ScreenAttrs[off_to] != hl)
6579 {
6580 ScreenLines[off_to] = c;
6581 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 if (enc_utf8)
6583 {
6584 if (c >= 0x80)
6585 {
6586 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006587 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 }
6589 else
6590 ScreenLinesUC[off_to] = 0;
6591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 screen_char(off_to, row, col + coloff);
6593 }
6594 }
6595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 LineWraps[row] = FALSE;
6597 }
6598}
6599
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006600#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006602 * Mirror text "str" for right-left displaying.
6603 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006605 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006606rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 char_u *p1, *p2;
6609 int t;
6610
6611 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6612 {
6613 t = *p1;
6614 *p1 = *p2;
6615 *p2 = t;
6616 }
6617}
6618#endif
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
6621 * mark all status lines for redraw; used after first :cd
6622 */
6623 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006624status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 win_T *wp;
6627
Bram Moolenaar29323592016-07-24 22:04:11 +02006628 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 if (wp->w_status_height)
6630 {
6631 wp->w_redr_status = TRUE;
6632 redraw_later(VALID);
6633 }
6634}
6635
6636/*
6637 * mark all status lines of the current buffer for redraw
6638 */
6639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006640status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 win_T *wp;
6643
Bram Moolenaar29323592016-07-24 22:04:11 +02006644 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6646 {
6647 wp->w_redr_status = TRUE;
6648 redraw_later(VALID);
6649 }
6650}
6651
6652/*
6653 * Redraw all status lines that need to be redrawn.
6654 */
6655 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006656redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 win_T *wp;
6659
Bram Moolenaar29323592016-07-24 22:04:11 +02006660 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006662 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006663 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006664 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
Bram Moolenaar4033c552017-09-16 20:54:51 +02006667#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668/*
6669 * Redraw all status lines at the bottom of frame "frp".
6670 */
6671 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006672win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 if (frp->fr_layout == FR_LEAF)
6675 frp->fr_win->w_redr_status = TRUE;
6676 else if (frp->fr_layout == FR_ROW)
6677 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006678 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 win_redraw_last_status(frp);
6680 }
6681 else /* frp->fr_layout == FR_COL */
6682 {
6683 frp = frp->fr_child;
6684 while (frp->fr_next != NULL)
6685 frp = frp->fr_next;
6686 win_redraw_last_status(frp);
6687 }
6688}
6689#endif
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691/*
6692 * Draw the verticap separator right of window "wp" starting with line "row".
6693 */
6694 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006695draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 int hl;
6698 int c;
6699
6700 if (wp->w_vsep_width)
6701 {
6702 /* draw the vertical separator right of this window */
6703 c = fillchar_vsep(&hl);
6704 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6705 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6706 c, ' ', hl);
6707 }
6708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006711static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006714 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 */
6716 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006717status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 int len = 0;
6720
6721#ifdef FEAT_MENU
6722 int emenu = (xp->xp_context == EXPAND_MENUS
6723 || xp->xp_context == EXPAND_MENUNAMES);
6724
6725 /* Check for menu separators - replace with '|'. */
6726 if (emenu && menu_is_separator(s))
6727 return 1;
6728#endif
6729
6730 while (*s != NUL)
6731 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006732 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006733 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006734 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 }
6736
6737 return len;
6738}
6739
6740/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006741 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006742 * These are backslashes used for escaping. Do show backslashes in help tags.
6743 */
6744 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006745skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006746{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006747 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006748#ifdef FEAT_MENU
6749 || ((xp->xp_context == EXPAND_MENUS
6750 || xp->xp_context == EXPAND_MENUNAMES)
6751 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6752#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006753 )
6754 {
6755#ifndef BACKSLASH_IN_FILENAME
6756 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6757 return 2;
6758#endif
6759 return 1;
6760 }
6761 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006762}
6763
6764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 * Show wildchar matches in the status line.
6766 * Show at least the "match" item.
6767 * We start at item 'first_match' in the list and show all matches that fit.
6768 *
6769 * If inversion is possible we use it. Else '=' characters are used.
6770 */
6771 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006772win_redr_status_matches(
6773 expand_T *xp,
6774 int num_matches,
6775 char_u **matches, /* list of matches */
6776 int match,
6777 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6780 int row;
6781 char_u *buf;
6782 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006783 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 int fillchar;
6785 int attr;
6786 int i;
6787 int highlight = TRUE;
6788 char_u *selstart = NULL;
6789 int selstart_col = 0;
6790 char_u *selend = NULL;
6791 static int first_match = 0;
6792 int add_left = FALSE;
6793 char_u *s;
6794#ifdef FEAT_MENU
6795 int emenu;
6796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 if (matches == NULL) /* interrupted completion? */
6800 return;
6801
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006802 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006803 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006804 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006805 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (buf == NULL)
6807 return;
6808
6809 if (match == -1) /* don't show match but original text */
6810 {
6811 match = 0;
6812 highlight = FALSE;
6813 }
6814 /* count 1 for the ending ">" */
6815 clen = status_match_len(xp, L_MATCH(match)) + 3;
6816 if (match == 0)
6817 first_match = 0;
6818 else if (match < first_match)
6819 {
6820 /* jumping left, as far as we can go */
6821 first_match = match;
6822 add_left = TRUE;
6823 }
6824 else
6825 {
6826 /* check if match fits on the screen */
6827 for (i = first_match; i < match; ++i)
6828 clen += status_match_len(xp, L_MATCH(i)) + 2;
6829 if (first_match > 0)
6830 clen += 2;
6831 /* jumping right, put match at the left */
6832 if ((long)clen > Columns)
6833 {
6834 first_match = match;
6835 /* if showing the last match, we can add some on the left */
6836 clen = 2;
6837 for (i = match; i < num_matches; ++i)
6838 {
6839 clen += status_match_len(xp, L_MATCH(i)) + 2;
6840 if ((long)clen >= Columns)
6841 break;
6842 }
6843 if (i == num_matches)
6844 add_left = TRUE;
6845 }
6846 }
6847 if (add_left)
6848 while (first_match > 0)
6849 {
6850 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6851 if ((long)clen >= Columns)
6852 break;
6853 --first_match;
6854 }
6855
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006856 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
6858 if (first_match == 0)
6859 {
6860 *buf = NUL;
6861 len = 0;
6862 }
6863 else
6864 {
6865 STRCPY(buf, "< ");
6866 len = 2;
6867 }
6868 clen = len;
6869
6870 i = first_match;
6871 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6872 {
6873 if (i == match)
6874 {
6875 selstart = buf + len;
6876 selstart_col = clen;
6877 }
6878
6879 s = L_MATCH(i);
6880 /* Check for menu separators - replace with '|' */
6881#ifdef FEAT_MENU
6882 emenu = (xp->xp_context == EXPAND_MENUS
6883 || xp->xp_context == EXPAND_MENUNAMES);
6884 if (emenu && menu_is_separator(s))
6885 {
6886 STRCPY(buf + len, transchar('|'));
6887 l = (int)STRLEN(buf + len);
6888 len += l;
6889 clen += l;
6890 }
6891 else
6892#endif
6893 for ( ; *s != NUL; ++s)
6894 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006895 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006897 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 {
6899 STRNCPY(buf + len, s, l);
6900 s += l - 1;
6901 len += l;
6902 }
6903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
6905 STRCPY(buf + len, transchar_byte(*s));
6906 len += (int)STRLEN(buf + len);
6907 }
6908 }
6909 if (i == match)
6910 selend = buf + len;
6911
6912 *(buf + len++) = ' ';
6913 *(buf + len++) = ' ';
6914 clen += 2;
6915 if (++i == num_matches)
6916 break;
6917 }
6918
6919 if (i != num_matches)
6920 {
6921 *(buf + len++) = '>';
6922 ++clen;
6923 }
6924
6925 buf[len] = NUL;
6926
6927 row = cmdline_row - 1;
6928 if (row >= 0)
6929 {
6930 if (wild_menu_showing == 0)
6931 {
6932 if (msg_scrolled > 0)
6933 {
6934 /* Put the wildmenu just above the command line. If there is
6935 * no room, scroll the screen one line up. */
6936 if (cmdline_row == Rows - 1)
6937 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006938 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 ++msg_scrolled;
6940 }
6941 else
6942 {
6943 ++cmdline_row;
6944 ++row;
6945 }
6946 wild_menu_showing = WM_SCROLLED;
6947 }
6948 else
6949 {
6950 /* Create status line if needed by setting 'laststatus' to 2.
6951 * Set 'winminheight' to zero to avoid that the window is
6952 * resized. */
6953 if (lastwin->w_status_height == 0)
6954 {
6955 save_p_ls = p_ls;
6956 save_p_wmh = p_wmh;
6957 p_ls = 2;
6958 p_wmh = 0;
6959 last_status(FALSE);
6960 }
6961 wild_menu_showing = WM_SHOWN;
6962 }
6963 }
6964
6965 screen_puts(buf, row, 0, attr);
6966 if (selstart != NULL && highlight)
6967 {
6968 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006969 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971
6972 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6973 }
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 vim_free(buf);
6977}
6978#endif
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980/*
6981 * Redraw the status line of window wp.
6982 *
6983 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006984 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6985 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006987 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006988win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
6990 int row;
6991 char_u *p;
6992 int len;
6993 int fillchar;
6994 int attr;
6995 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006996 static int busy = FALSE;
6997
6998 /* It's possible to get here recursively when 'statusline' (indirectly)
6999 * invokes ":redrawstatus". Simply ignore the call then. */
7000 if (busy)
7001 return;
7002 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 wp->w_redr_status = FALSE;
7005 if (wp->w_status_height == 0)
7006 {
7007 /* no status line, can only be last window */
7008 redraw_cmdline = TRUE;
7009 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007010 else if (!redrawing()
7011#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007012 // don't update status line when popup menu is visible and may be
7013 // drawn over it, unless it will be redrawn later
7014 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007015#endif
7016 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {
7018 /* Don't redraw right now, do it later. */
7019 wp->w_redr_status = TRUE;
7020 }
7021#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007022 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {
7024 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007025 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027#endif
7028 else
7029 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007030 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007032 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 p = NameBuff;
7034 len = (int)STRLEN(p);
7035
Bram Moolenaard85f2712017-07-28 21:51:57 +02007036 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037#ifdef FEAT_QUICKFIX
7038 || wp->w_p_pvw
7039#endif
7040 || bufIsChanged(wp->w_buffer)
7041 || wp->w_buffer->b_p_ro)
7042 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007043 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007045 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 len += (int)STRLEN(p + len);
7047 }
7048#ifdef FEAT_QUICKFIX
7049 if (wp->w_p_pvw)
7050 {
7051 STRCPY(p + len, _("[Preview]"));
7052 len += (int)STRLEN(p + len);
7053 }
7054#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007055 if (bufIsChanged(wp->w_buffer)
7056#ifdef FEAT_TERMINAL
7057 && !bt_terminal(wp->w_buffer)
7058#endif
7059 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
7061 STRCPY(p + len, "[+]");
7062 len += 3;
7063 }
7064 if (wp->w_buffer->b_p_ro)
7065 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007066 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007067 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
7069
Bram Moolenaar02631462017-09-22 15:20:32 +02007070 this_ru_col = ru_col - (Columns - wp->w_width);
7071 if (this_ru_col < (wp->w_width + 1) / 2)
7072 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (this_ru_col <= 1)
7074 {
7075 p = (char_u *)"<"; /* No room for file name! */
7076 len = 1;
7077 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007078 else if (has_mbyte)
7079 {
7080 int clen = 0, i;
7081
7082 /* Count total number of display cells. */
7083 clen = mb_string2cells(p, -1);
7084
7085 /* Find first character that will fit.
7086 * Going from start to end is much faster for DBCS. */
7087 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7088 i += (*mb_ptr2len)(p + i))
7089 clen -= (*mb_ptr2cells)(p + i);
7090 len = clen;
7091 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007093 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007095 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 }
7097
Bram Moolenaara12a1612019-01-24 16:39:02 +01007098 }
7099 else if (len > this_ru_col - 1)
7100 {
7101 p += len - (this_ru_col - 1);
7102 *p = '<';
7103 len = this_ru_col - 1;
7104 }
7105
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007107 screen_puts(p, row, wp->w_wincol, attr);
7108 screen_fill(row, row + 1, len + wp->w_wincol,
7109 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007111 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7113 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007114 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007117 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#endif
7119 }
7120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 /*
7122 * May need to draw the character below the vertical separator.
7123 */
7124 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7125 {
7126 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007127 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 else
7129 fillchar = fillchar_vsep(&attr);
7130 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7131 attr);
7132 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007133 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134}
7135
Bram Moolenaar238a5642006-02-21 22:12:05 +00007136#ifdef FEAT_STL_OPT
7137/*
7138 * Redraw the status line according to 'statusline' and take care of any
7139 * errors encountered.
7140 */
7141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007142redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007143{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007144 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007145 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007146
7147 /* When called recursively return. This can happen when the statusline
7148 * contains an expression that triggers a redraw. */
7149 if (entered)
7150 return;
7151 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007152
Bram Moolenaara742e082016-04-05 21:10:38 +02007153 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007154 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007155 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007156 {
7157 /* When there is an error disable the statusline, otherwise the
7158 * display is messed up with errors and a redraw triggers the problem
7159 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007160 set_string_option_direct((char_u *)"statusline", -1,
7161 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007162 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007163 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007164 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007165 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007166}
7167#endif
7168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169/*
7170 * Return TRUE if the status line of window "wp" is connected to the status
7171 * line of the window right of it. If not, then it's a vertical separator.
7172 * Only call if (wp->w_vsep_width != 0).
7173 */
7174 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007175stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 frame_T *fr;
7178
7179 fr = wp->w_frame;
7180 while (fr->fr_parent != NULL)
7181 {
7182 if (fr->fr_parent->fr_layout == FR_COL)
7183 {
7184 if (fr->fr_next != NULL)
7185 break;
7186 }
7187 else
7188 {
7189 if (fr->fr_next != NULL)
7190 return TRUE;
7191 }
7192 fr = fr->fr_parent;
7193 }
7194 return FALSE;
7195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198/*
7199 * Get the value to show for the language mappings, active 'keymap'.
7200 */
7201 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007202get_keymap_str(
7203 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007204 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007205 char_u *buf, /* buffer for the result */
7206 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 char_u *p;
7209
7210 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7211 return FALSE;
7212
7213 {
7214#ifdef FEAT_EVAL
7215 buf_T *old_curbuf = curbuf;
7216 win_T *old_curwin = curwin;
7217 char_u *s;
7218
7219 curbuf = wp->w_buffer;
7220 curwin = wp;
7221 STRCPY(buf, "b:keymap_name"); /* must be writable */
7222 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007223 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 --emsg_skip;
7225 curbuf = old_curbuf;
7226 curwin = old_curwin;
7227 if (p == NULL || *p == NUL)
7228#endif
7229 {
7230#ifdef FEAT_KEYMAP
7231 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7232 p = wp->w_buffer->b_p_keymap;
7233 else
7234#endif
7235 p = (char_u *)"lang";
7236 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007237 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 buf[0] = NUL;
7239#ifdef FEAT_EVAL
7240 vim_free(s);
7241#endif
7242 }
7243 return buf[0] != NUL;
7244}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
7246#if defined(FEAT_STL_OPT) || defined(PROTO)
7247/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007248 * Redraw the status line or ruler of window "wp".
7249 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 */
7251 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007252win_redr_custom(
7253 win_T *wp,
7254 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007256 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 int attr;
7258 int curattr;
7259 int row;
7260 int col = 0;
7261 int maxwidth;
7262 int width;
7263 int n;
7264 int len;
7265 int fillchar;
7266 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007267 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007269 struct stl_hlrec hltab[STL_MAX_ITEM];
7270 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007271 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007272 win_T *ewp;
7273 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar1d633412013-12-11 15:52:01 +01007275 /* There is a tiny chance that this gets called recursively: When
7276 * redrawing a status line triggers redrawing the ruler or tabline.
7277 * Avoid trouble by not allowing recursion. */
7278 if (entered)
7279 return;
7280 entered = TRUE;
7281
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007283 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007285 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007286 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007287 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007288 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007289 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007290 maxwidth = Columns;
7291# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007292 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007295 else
7296 {
7297 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007298 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007299 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007300
7301 if (draw_ruler)
7302 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007303 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007304 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007305 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007306 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007307 if (*++stl == '-')
7308 stl++;
7309 if (atoi((char *)stl))
7310 while (VIM_ISDIGIT(*stl))
7311 stl++;
7312 if (*stl++ != '(')
7313 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007314 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007315 col = ru_col - (Columns - wp->w_width);
7316 if (col < (wp->w_width + 1) / 2)
7317 col = (wp->w_width + 1) / 2;
7318 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007320 {
7321 row = Rows - 1;
7322 --maxwidth; /* writing in last column may cause scrolling */
7323 fillchar = ' ';
7324 attr = 0;
7325 }
7326
7327# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007328 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007329# endif
7330 }
7331 else
7332 {
7333 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007334 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007335 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007336 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007337# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007338 use_sandbox = was_set_insecurely((char_u *)"statusline",
7339 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007340# endif
7341 }
7342
Bram Moolenaar53f81742017-09-22 14:35:51 +02007343 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007344 }
7345
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007347 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
Bram Moolenaar61452852011-02-01 18:01:11 +01007349 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7350 * the cursor away and back. */
7351 ewp = wp == NULL ? curwin : wp;
7352 p_crb_save = ewp->w_p_crb;
7353 ewp->w_p_crb = FALSE;
7354
Bram Moolenaar362f3562009-11-03 16:20:34 +00007355 /* Make a copy, because the statusline may include a function call that
7356 * might change the option value and free the memory. */
7357 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007358 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007359 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007360 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007361 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007362 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007364 /* Make all characters printable. */
7365 p = transstr(buf);
7366 if (p != NULL)
7367 {
7368 vim_strncpy(buf, p, sizeof(buf) - 1);
7369 vim_free(p);
7370 }
7371
7372 /* fill up with "fillchar" */
7373 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007374 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 ++width;
7378 }
7379 buf[len] = NUL;
7380
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007381 /*
7382 * Draw each snippet with the specified highlighting.
7383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 curattr = attr;
7385 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007386 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007388 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 screen_puts_len(p, len, row, col, curattr);
7390 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007391 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007393 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007395 else if (hltab[n].userhl < 0)
7396 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007397#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007398 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7399 && wp->w_status_height != 0)
7400 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007401 else if (wp != NULL && bt_terminal(wp->w_buffer)
7402 && wp->w_status_height != 0)
7403 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007404#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007405 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007406 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007408 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007411
7412 if (wp == NULL)
7413 {
7414 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7415 col = 0;
7416 len = 0;
7417 p = buf;
7418 fillchar = 0;
7419 for (n = 0; tabtab[n].start != NULL; n++)
7420 {
7421 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7422 while (col < len)
7423 TabPageIdxs[col++] = fillchar;
7424 p = tabtab[n].start;
7425 fillchar = tabtab[n].userhl;
7426 }
7427 while (col < Columns)
7428 TabPageIdxs[col++] = fillchar;
7429 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007430
7431theend:
7432 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433}
7434
7435#endif /* FEAT_STL_OPT */
7436
7437/*
7438 * Output a single character directly to the screen and update ScreenLines.
7439 */
7440 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007441screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 char_u buf[MB_MAXBYTES + 1];
7444
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007445 if (has_mbyte)
7446 buf[(*mb_char2bytes)(c, buf)] = NUL;
7447 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007448 {
7449 buf[0] = c;
7450 buf[1] = NUL;
7451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 screen_puts(buf, row, col, attr);
7453}
7454
7455/*
7456 * Get a single character directly from ScreenLines into "bytes[]".
7457 * Also return its attribute in *attrp;
7458 */
7459 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007460screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461{
7462 unsigned off;
7463
7464 /* safety check */
7465 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7466 {
7467 off = LineOffset[row] + col;
7468 *attrp = ScreenAttrs[off];
7469 bytes[0] = ScreenLines[off];
7470 bytes[1] = NUL;
7471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 if (enc_utf8 && ScreenLinesUC[off] != 0)
7473 bytes[utfc_char2bytes(off, bytes)] = NUL;
7474 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7475 {
7476 bytes[0] = ScreenLines[off];
7477 bytes[1] = ScreenLines2[off];
7478 bytes[2] = NUL;
7479 }
7480 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7481 {
7482 bytes[1] = ScreenLines[off + 1];
7483 bytes[2] = NUL;
7484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486}
7487
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007488/*
7489 * Return TRUE if composing characters for screen posn "off" differs from
7490 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007491 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007492 */
7493 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007494screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007495{
7496 int i;
7497
7498 for (i = 0; i < Screen_mco; ++i)
7499 {
7500 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7501 return TRUE;
7502 if (u8cc[i] == 0)
7503 break;
7504 }
7505 return FALSE;
7506}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007507
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508/*
7509 * Put string '*text' on the screen at position 'row' and 'col', with
7510 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7511 * Note: only outputs within one row, message is truncated at screen boundary!
7512 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7513 */
7514 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007515screen_puts(
7516 char_u *text,
7517 int row,
7518 int col,
7519 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 screen_puts_len(text, -1, row, col, attr);
7522}
7523
7524/*
7525 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7526 * a NUL.
7527 */
7528 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007529screen_puts_len(
7530 char_u *text,
7531 int textlen,
7532 int row,
7533 int col,
7534 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535{
7536 unsigned off;
7537 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007538 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007540 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 int mbyte_blen = 1;
7542 int mbyte_cells = 1;
7543 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007546#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007548 int pc, nc, nc1;
7549 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007551 int force_redraw_this;
7552 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007553 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
7555 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7556 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007557 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
Bram Moolenaarc236c162008-07-13 17:41:49 +00007559 /* When drawing over the right halve of a double-wide char clear out the
7560 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007561 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007562#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007563 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007564#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007565 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007566 {
7567 ScreenLines[off - 1] = ' ';
7568 ScreenAttrs[off - 1] = 0;
7569 if (enc_utf8)
7570 {
7571 ScreenLinesUC[off - 1] = 0;
7572 ScreenLinesC[0][off - 1] = 0;
7573 }
7574 /* redraw the previous cell, make it empty */
7575 screen_char(off - 1, row, col - 1);
7576 /* force the cell at "col" to be redrawn */
7577 force_redraw_next = TRUE;
7578 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007579
Bram Moolenaar367329b2007-08-30 11:53:22 +00007580 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007581 while (col < screen_Columns
7582 && (len < 0 || (int)(ptr - text) < len)
7583 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
7585 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 /* check if this is the first byte of a multibyte */
7587 if (has_mbyte)
7588 {
7589 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007590 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007592 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7594 mbyte_cells = 1;
7595 else if (enc_dbcs != 0)
7596 mbyte_cells = mbyte_blen;
7597 else /* enc_utf8 */
7598 {
7599 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 (int)((text + len) - ptr));
7602 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007605#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7607 {
7608 /* Do Arabic shaping. */
7609 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7610 {
7611 /* Past end of string to be displayed. */
7612 nc = NUL;
7613 nc1 = NUL;
7614 }
7615 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007616 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007617 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7618 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007619 nc1 = pcc[0];
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 pc = prev_c;
7622 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007623 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625 else
7626 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007627#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007628 if (col + mbyte_cells > screen_Columns)
7629 {
7630 /* Only 1 cell left, but character requires 2 cells:
7631 * display a '>' in the last column to avoid wrapping. */
7632 c = '>';
7633 mbyte_cells = 1;
7634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007638 force_redraw_this = force_redraw_next;
7639 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007640
7641 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 || (mbyte_cells == 2
7643 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7644 || (enc_dbcs == DBCS_JPNU
7645 && c == 0x8e
7646 && ScreenLines2[off] != ptr[1])
7647 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007648 && (ScreenLinesUC[off] !=
7649 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7650 || (ScreenLinesUC[off] != 0
7651 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007653 || exmode_active;
7654
Bram Moolenaara12a1612019-01-24 16:39:02 +01007655 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 {
7657#if defined(FEAT_GUI) || defined(UNIX)
7658 /* The bold trick makes a single row of pixels appear in the next
7659 * character. When a bold character is removed, the next
7660 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007661 * and for some xterms. */
7662 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663# ifdef FEAT_GUI
7664 gui.in_use
7665# endif
7666# if defined(FEAT_GUI) && defined(UNIX)
7667 ||
7668# endif
7669# ifdef UNIX
7670 term_is_xterm
7671# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007672 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007674 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007676 if (n > HL_ALL)
7677 n = syn_attr2attr(n);
7678 if (n & HL_BOLD)
7679 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
7681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 /* When at the end of the text and overwriting a two-cell
7683 * character with a one-cell character, need to clear the next
7684 * cell. Also when overwriting the left halve of a two-cell char
7685 * with the right halve of a two-cell char. Do this only once
7686 * (mb_off2cells() may return 2 on the right halve). */
7687 if (clear_next_cell)
7688 clear_next_cell = FALSE;
7689 else if (has_mbyte
7690 && (len < 0 ? ptr[mbyte_blen] == NUL
7691 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007692 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007694 && (*mb_off2cells)(off, max_off) == 1
7695 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 clear_next_cell = TRUE;
7697
7698 /* Make sure we never leave a second byte of a double-byte behind,
7699 * it confuses mb_off2cells(). */
7700 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007701 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007703 && (*mb_off2cells)(off, max_off) == 1
7704 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 ScreenLines[off] = c;
7707 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 if (enc_utf8)
7709 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007710 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 ScreenLinesUC[off] = 0;
7712 else
7713 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007714 int i;
7715
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007717 for (i = 0; i < Screen_mco; ++i)
7718 {
7719 ScreenLinesC[i][off] = u8cc[i];
7720 if (u8cc[i] == 0)
7721 break;
7722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 if (mbyte_cells == 2)
7725 {
7726 ScreenLines[off + 1] = 0;
7727 ScreenAttrs[off + 1] = attr;
7728 }
7729 screen_char(off, row, col);
7730 }
7731 else if (mbyte_cells == 2)
7732 {
7733 ScreenLines[off + 1] = ptr[1];
7734 ScreenAttrs[off + 1] = attr;
7735 screen_char_2(off, row, col);
7736 }
7737 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7738 {
7739 ScreenLines2[off] = ptr[1];
7740 screen_char(off, row, col);
7741 }
7742 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 screen_char(off, row, col);
7744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 if (has_mbyte)
7746 {
7747 off += mbyte_cells;
7748 col += mbyte_cells;
7749 ptr += mbyte_blen;
7750 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007751 {
7752 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007754 len = -1;
7755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
7759 ++off;
7760 ++col;
7761 ++ptr;
7762 }
7763 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007764
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007765 /* If we detected the next character needs to be redrawn, but the text
7766 * doesn't extend up to there, update the character here. */
7767 if (force_redraw_next && col < screen_Columns)
7768 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007769 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7770 screen_char_2(off, row, col);
7771 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007772 screen_char(off, row, col);
7773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774}
7775
7776#ifdef FEAT_SEARCH_EXTRA
7777/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007778 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 */
7780 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007781start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 if (p_hls && !no_hlsearch)
7784 {
7785 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007786 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007787# ifdef FEAT_RELTIME
7788 /* Set the time limit to 'redrawtime'. */
7789 profile_setlimit(p_rdt, &search_hl.tm);
7790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
7792}
7793
7794/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007795 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 */
7797 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007798end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 if (search_hl.rm.regprog != NULL)
7801 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007802 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 search_hl.rm.regprog = NULL;
7804 }
7805}
7806
7807/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007808 * Init for calling prepare_search_hl().
7809 */
7810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007811init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007812{
7813 matchitem_T *cur;
7814
7815 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7816 * match */
7817 cur = wp->w_match_head;
7818 while (cur != NULL)
7819 {
7820 cur->hl.rm = cur->match;
7821 if (cur->hlg_id == 0)
7822 cur->hl.attr = 0;
7823 else
7824 cur->hl.attr = syn_id2attr(cur->hlg_id);
7825 cur->hl.buf = wp->w_buffer;
7826 cur->hl.lnum = 0;
7827 cur->hl.first_lnum = 0;
7828# ifdef FEAT_RELTIME
7829 /* Set the time limit to 'redrawtime'. */
7830 profile_setlimit(p_rdt, &(cur->hl.tm));
7831# endif
7832 cur = cur->next;
7833 }
7834 search_hl.buf = wp->w_buffer;
7835 search_hl.lnum = 0;
7836 search_hl.first_lnum = 0;
7837 /* time limit is set at the toplevel, for all windows */
7838}
7839
7840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 * Advance to the match in window "wp" line "lnum" or past it.
7842 */
7843 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007844prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007846 matchitem_T *cur; /* points to the match list */
7847 match_T *shl; /* points to search_hl or a match */
7848 int shl_flag; /* flag to indicate whether search_hl
7849 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007850 int pos_inprogress; /* marks that position match search is
7851 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 int n;
7853
7854 /*
7855 * When using a multi-line pattern, start searching at the top
7856 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007857 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 cur = wp->w_match_head;
7860 shl_flag = FALSE;
7861 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007863 if (shl_flag == FALSE)
7864 {
7865 shl = &search_hl;
7866 shl_flag = TRUE;
7867 }
7868 else
7869 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (shl->rm.regprog != NULL
7871 && shl->lnum == 0
7872 && re_multiline(shl->rm.regprog))
7873 {
7874 if (shl->first_lnum == 0)
7875 {
7876# ifdef FEAT_FOLDING
7877 for (shl->first_lnum = lnum;
7878 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7879 if (hasFoldingWin(wp, shl->first_lnum - 1,
7880 NULL, NULL, TRUE, NULL))
7881 break;
7882# else
7883 shl->first_lnum = wp->w_topline;
7884# endif
7885 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007886 if (cur != NULL)
7887 cur->pos.cur = 0;
7888 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007890 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7891 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007893 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7894 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007895 pos_inprogress = cur == NULL || cur->pos.cur == 0
7896 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 if (shl->lnum != 0)
7898 {
7899 shl->first_lnum = shl->lnum
7900 + shl->rm.endpos[0].lnum
7901 - shl->rm.startpos[0].lnum;
7902 n = shl->rm.endpos[0].col;
7903 }
7904 else
7905 {
7906 ++shl->first_lnum;
7907 n = 0;
7908 }
7909 }
7910 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007911 if (shl != &search_hl && cur != NULL)
7912 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 }
7914}
7915
7916/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007917 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 * Uses shl->buf.
7919 * Sets shl->lnum and shl->rm contents.
7920 * Note: Assumes a previous match is always before "lnum", unless
7921 * shl->lnum is zero.
7922 * Careful: Any pointers for buffer lines will become invalid.
7923 */
7924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007925next_search_hl(
7926 win_T *win,
7927 match_T *shl, /* points to search_hl or a match */
7928 linenr_T lnum,
7929 colnr_T mincol, /* minimal column for a match */
7930 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
7932 linenr_T l;
7933 colnr_T matchcol;
7934 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007935 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007937 // for :{range}s/pat only highlight inside the range
7938 if (lnum < search_first_line || lnum > search_last_line)
7939 {
7940 shl->lnum = 0;
7941 return;
7942 }
7943
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (shl->lnum != 0)
7945 {
7946 /* Check for three situations:
7947 * 1. If the "lnum" is below a previous match, start a new search.
7948 * 2. If the previous match includes "mincol", use it.
7949 * 3. Continue after the previous match.
7950 */
7951 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7952 if (lnum > l)
7953 shl->lnum = 0;
7954 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7955 return;
7956 }
7957
7958 /*
7959 * Repeat searching for a match until one is found that includes "mincol"
7960 * or none is found in this line.
7961 */
7962 called_emsg = FALSE;
7963 for (;;)
7964 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007965#ifdef FEAT_RELTIME
7966 /* Stop searching after passing the time limit. */
7967 if (profile_passed_limit(&(shl->tm)))
7968 {
7969 shl->lnum = 0; /* no match found in time */
7970 break;
7971 }
7972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 /* Three situations:
7974 * 1. No useful previous match: search from start of line.
7975 * 2. Not Vi compatible or empty match: continue at next character.
7976 * Break the loop if this is beyond the end of the line.
7977 * 3. Vi compatible searching: continue at end of previous match.
7978 */
7979 if (shl->lnum == 0)
7980 matchcol = 0;
7981 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7982 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007983 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007985 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007986
7987 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007988 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007989 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007991 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 shl->lnum = 0;
7993 break;
7994 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007995 if (has_mbyte)
7996 matchcol += mb_ptr2len(ml);
7997 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007998 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 }
8000 else
8001 matchcol = shl->rm.endpos[0].col;
8002
8003 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008006 /* Remember whether shl->rm is using a copy of the regprog in
8007 * cur->match. */
8008 int regprog_is_copy = (shl != &search_hl && cur != NULL
8009 && shl == &cur->hl
8010 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008011 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008012
Bram Moolenaarb3414592014-06-17 17:48:32 +02008013 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8014 matchcol,
8015#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008016 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008017#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008018 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008019#endif
8020 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008021 /* Copy the regprog, in case it got freed and recompiled. */
8022 if (regprog_is_copy)
8023 cur->match.regprog = cur->hl.rm.regprog;
8024
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008025 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008026 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027 /* Error while handling regexp: stop using this regexp. */
8028 if (shl == &search_hl)
8029 {
8030 /* don't free regprog in the match list, it's a copy */
8031 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008032 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008033 }
8034 shl->rm.regprog = NULL;
8035 shl->lnum = 0;
8036 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8037 message */
8038 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008039 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008040 }
8041 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008042 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008043 else
8044 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 if (nmatched == 0)
8046 {
8047 shl->lnum = 0; /* no match found */
8048 break;
8049 }
8050 if (shl->rm.startpos[0].lnum > 0
8051 || shl->rm.startpos[0].col >= mincol
8052 || nmatched > 1
8053 || shl->rm.endpos[0].col > mincol)
8054 {
8055 shl->lnum += shl->rm.startpos[0].lnum;
8056 break; /* useful match found */
8057 }
8058 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008059
8060 // Restore called_emsg for assert_fails().
8061 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
Bram Moolenaar85077472016-10-16 14:35:48 +02008064/*
8065 * If there is a match fill "shl" and return one.
8066 * Return zero otherwise.
8067 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008068 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008069next_search_hl_pos(
8070 match_T *shl, /* points to a match */
8071 linenr_T lnum,
8072 posmatch_T *posmatch, /* match positions */
8073 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074{
8075 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008076 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077
Bram Moolenaarb3414592014-06-17 17:48:32 +02008078 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8079 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008080 llpos_T *pos = &posmatch->pos[i];
8081
8082 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008083 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008084 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008085 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008086 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008087 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008088 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008089 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008090 /* if this match comes before the one at "found" then swap
8091 * them */
8092 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008093 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008094 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008095
Bram Moolenaar85077472016-10-16 14:35:48 +02008096 *pos = posmatch->pos[found];
8097 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 }
8099 }
8100 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008101 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102 }
8103 }
8104 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008105 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008106 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008107 colnr_T start = posmatch->pos[found].col == 0
8108 ? 0 : posmatch->pos[found].col - 1;
8109 colnr_T end = posmatch->pos[found].col == 0
8110 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111
Bram Moolenaar85077472016-10-16 14:35:48 +02008112 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113 shl->rm.startpos[0].lnum = 0;
8114 shl->rm.startpos[0].col = start;
8115 shl->rm.endpos[0].lnum = 0;
8116 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008117 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008118 posmatch->cur = found + 1;
8119 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008120 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008121 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008122}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008123#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008124
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008126screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 attrentry_T *aep = NULL;
8129
8130 screen_attr = attr;
8131 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008132#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 && termcap_active
8134#endif
8135 )
8136 {
8137#ifdef FEAT_GUI
8138 if (gui.in_use)
8139 {
8140 char buf[20];
8141
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008142 /* The GUI handles this internally. */
8143 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 OUT_STR(buf);
8145 }
8146 else
8147#endif
8148 {
8149 if (attr > HL_ALL) /* special HL attr. */
8150 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008151 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 aep = syn_cterm_attr2entry(attr);
8153 else
8154 aep = syn_term_attr2entry(attr);
8155 if (aep == NULL) /* did ":syntax clear" */
8156 attr = 0;
8157 else
8158 attr = aep->ae_attr;
8159 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008160 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008162 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008163#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008164 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8165 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8166 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008167#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008168 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008169 /* If the Normal FG color has BOLD attribute and the new HL
8170 * has a FG color defined, clear BOLD. */
8171 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008172 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008174 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008175 out_str(T_UCS);
8176 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008177 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8178 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008180 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008182 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008184 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008185 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186
8187 /*
8188 * Output the color or start string after bold etc., in case the
8189 * bold etc. override the color setting.
8190 */
8191 if (aep != NULL)
8192 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008193#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008194 /* When 'termguicolors' is set but fg or bg is unset,
8195 * fall back to the cterm colors. This helps for SpellBad,
8196 * where the GUI uses a red undercurl. */
8197 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008199 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008200 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008201 }
8202 else
8203#endif
8204 if (t_colors > 1)
8205 {
8206 if (aep->ae_u.cterm.fg_color)
8207 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8208 }
8209#ifdef FEAT_TERMGUICOLORS
8210 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8211 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008212 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008213 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 }
8215 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008216#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008217 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008219 if (aep->ae_u.cterm.bg_color)
8220 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8221 }
8222
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008223 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008224 {
8225 if (aep->ae_u.term.start != NULL)
8226 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
8228 }
8229 }
8230 }
8231}
8232
8233 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008234screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 int do_ME = FALSE; /* output T_ME code */
8237
8238 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008239#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 && termcap_active
8241#endif
8242 )
8243 {
8244#ifdef FEAT_GUI
8245 if (gui.in_use)
8246 {
8247 char buf[20];
8248
8249 /* use internal GUI code */
8250 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8251 OUT_STR(buf);
8252 }
8253 else
8254#endif
8255 {
8256 if (screen_attr > HL_ALL) /* special HL attr. */
8257 {
8258 attrentry_T *aep;
8259
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008260 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 /*
8263 * Assume that t_me restores the original colors!
8264 */
8265 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008266 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008267#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008268 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8269 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8270 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008271#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008272 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008273#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008274 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8275 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8276 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008277#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008278 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 do_ME = TRUE;
8280 }
8281 else
8282 {
8283 aep = syn_term_attr2entry(screen_attr);
8284 if (aep != NULL && aep->ae_u.term.stop != NULL)
8285 {
8286 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8287 do_ME = TRUE;
8288 else
8289 out_str(aep->ae_u.term.stop);
8290 }
8291 }
8292 if (aep == NULL) /* did ":syntax clear" */
8293 screen_attr = 0;
8294 else
8295 screen_attr = aep->ae_attr;
8296 }
8297
8298 /*
8299 * Often all ending-codes are equal to T_ME. Avoid outputting the
8300 * same sequence several times.
8301 */
8302 if (screen_attr & HL_STANDOUT)
8303 {
8304 if (STRCMP(T_SE, T_ME) == 0)
8305 do_ME = TRUE;
8306 else
8307 out_str(T_SE);
8308 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008309 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008310 {
8311 if (STRCMP(T_UCE, T_ME) == 0)
8312 do_ME = TRUE;
8313 else
8314 out_str(T_UCE);
8315 }
8316 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008317 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
8319 if (STRCMP(T_UE, T_ME) == 0)
8320 do_ME = TRUE;
8321 else
8322 out_str(T_UE);
8323 }
8324 if (screen_attr & HL_ITALIC)
8325 {
8326 if (STRCMP(T_CZR, T_ME) == 0)
8327 do_ME = TRUE;
8328 else
8329 out_str(T_CZR);
8330 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008331 if (screen_attr & HL_STRIKETHROUGH)
8332 {
8333 if (STRCMP(T_STE, T_ME) == 0)
8334 do_ME = TRUE;
8335 else
8336 out_str(T_STE);
8337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8339 out_str(T_ME);
8340
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008341#ifdef FEAT_TERMGUICOLORS
8342 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008344 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008345 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008346 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008347 term_bg_rgb_color(cterm_normal_bg_gui_color);
8348 }
8349 else
8350#endif
8351 {
8352 if (t_colors > 1)
8353 {
8354 /* set Normal cterm colors */
8355 if (cterm_normal_fg_color != 0)
8356 term_fg_color(cterm_normal_fg_color - 1);
8357 if (cterm_normal_bg_color != 0)
8358 term_bg_color(cterm_normal_bg_color - 1);
8359 if (cterm_normal_fg_bold)
8360 out_str(T_MD);
8361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 }
8363 }
8364 }
8365 screen_attr = 0;
8366}
8367
8368/*
8369 * Reset the colors for a cterm. Used when leaving Vim.
8370 * The machine specific code may override this again.
8371 */
8372 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008373reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008375 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
8377 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008378#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008379 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8380 || cterm_normal_bg_gui_color != INVALCOLOR)
8381 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008382#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386 out_str(T_OP);
8387 screen_attr = -1;
8388 }
8389 if (cterm_normal_fg_bold)
8390 {
8391 out_str(T_ME);
8392 screen_attr = -1;
8393 }
8394 }
8395}
8396
8397/*
8398 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8399 * using the attributes from ScreenAttrs["off"].
8400 */
8401 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008402screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int attr;
8405
8406 /* Check for illegal values, just in case (could happen just after
8407 * resizing). */
8408 if (row >= screen_Rows || col >= screen_Columns)
8409 return;
8410
Bram Moolenaarae654382019-01-17 21:09:05 +01008411#ifdef FEAT_INS_EXPAND
8412 if (pum_under_menu(row, col))
8413 return;
8414#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008415 /* Outputting a character in the last cell on the screen may scroll the
8416 * screen up. Only do it when the "xn" termcap property is set, otherwise
8417 * mark the character invalid (update it when scrolled up). */
8418 if (*T_XN == NUL
8419 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420#ifdef FEAT_RIGHTLEFT
8421 /* account for first command-line character in rightleft mode */
8422 && !cmdmsg_rl
8423#endif
8424 )
8425 {
8426 ScreenAttrs[off] = (sattr_T)-1;
8427 return;
8428 }
8429
8430 /*
8431 * Stop highlighting first, so it's easier to move the cursor.
8432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 if (screen_char_attr != 0)
8434 attr = screen_char_attr;
8435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 attr = ScreenAttrs[off];
8437 if (screen_attr != attr)
8438 screen_stop_highlight();
8439
8440 windgoto(row, col);
8441
8442 if (screen_attr != attr)
8443 screen_start_highlight(attr);
8444
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 if (enc_utf8 && ScreenLinesUC[off] != 0)
8446 {
8447 char_u buf[MB_MAXBYTES + 1];
8448
Bram Moolenaarcb070082016-04-02 22:14:51 +02008449 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008450 {
8451 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008452#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008453 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008454#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008455 )
8456 {
8457 /* Clear the two screen cells. If the character is actually
8458 * single width it won't change the second cell. */
8459 out_str((char_u *)" ");
8460 term_windgoto(row, col);
8461 }
8462 /* not sure where the cursor is after drawing the ambiguous width
8463 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008464 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008465 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008466 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008468
8469 /* Convert the UTF-8 character to bytes and write it. */
8470 buf[utfc_char2bytes(off, buf)] = NUL;
8471 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 }
8473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 /* double-byte character in single-width cell */
8478 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8479 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 }
8481
8482 screen_cur_col++;
8483}
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485/*
8486 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8487 * on the screen at position 'row' and 'col'.
8488 * The attributes of the first byte is used for all. This is required to
8489 * output the two bytes of a double-byte character with nothing in between.
8490 */
8491 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008492screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
8494 /* Check for illegal values (could be wrong when screen was resized). */
8495 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8496 return;
8497
8498 /* Outputting the last character on the screen may scrollup the screen.
8499 * Don't to it! Mark the character invalid (update it when scrolled up) */
8500 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8501 {
8502 ScreenAttrs[off] = (sattr_T)-1;
8503 return;
8504 }
8505
8506 /* Output the first byte normally (positions the cursor), then write the
8507 * second byte directly. */
8508 screen_char(off, row, col);
8509 out_char(ScreenLines[off + 1]);
8510 ++screen_cur_col;
8511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513/*
8514 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8515 * This uses the contents of ScreenLines[] and doesn't change it.
8516 */
8517 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008518screen_draw_rectangle(
8519 int row,
8520 int col,
8521 int height,
8522 int width,
8523 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 int r, c;
8526 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008527 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008529 /* Can't use ScreenLines unless initialized */
8530 if (ScreenLines == NULL)
8531 return;
8532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (invert)
8534 screen_char_attr = HL_INVERSE;
8535 for (r = row; r < row + height; ++r)
8536 {
8537 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008538 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 for (c = col; c < col + width; ++c)
8540 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008541 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {
8543 screen_char_2(off + c, r, c);
8544 ++c;
8545 }
8546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
8548 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008549 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 }
8552 }
8553 }
8554 screen_char_attr = 0;
8555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557/*
8558 * Redraw the characters for a vertically split window.
8559 */
8560 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008561redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 int col;
8564 int width;
8565
8566# ifdef FEAT_CLIPBOARD
8567 clip_may_clear_selection(row, end - 1);
8568# endif
8569
8570 if (wp == NULL)
8571 {
8572 col = 0;
8573 width = Columns;
8574 }
8575 else
8576 {
8577 col = wp->w_wincol;
8578 width = wp->w_width;
8579 }
8580 screen_draw_rectangle(row, col, end - row, width, FALSE);
8581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008583 static void
8584space_to_screenline(int off, int attr)
8585{
8586 ScreenLines[off] = ' ';
8587 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008588 if (enc_utf8)
8589 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008590}
8591
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592/*
8593 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8594 * with character 'c1' in first column followed by 'c2' in the other columns.
8595 * Use attributes 'attr'.
8596 */
8597 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008598screen_fill(
8599 int start_row,
8600 int end_row,
8601 int start_col,
8602 int end_col,
8603 int c1,
8604 int c2,
8605 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 int row;
8608 int col;
8609 int off;
8610 int end_off;
8611 int did_delete;
8612 int c;
8613 int norm_term;
8614#if defined(FEAT_GUI) || defined(UNIX)
8615 int force_next = FALSE;
8616#endif
8617
8618 if (end_row > screen_Rows) /* safety check */
8619 end_row = screen_Rows;
8620 if (end_col > screen_Columns) /* safety check */
8621 end_col = screen_Columns;
8622 if (ScreenLines == NULL
8623 || start_row >= end_row
8624 || start_col >= end_col) /* nothing to do */
8625 return;
8626
8627 /* it's a "normal" terminal when not in a GUI or cterm */
8628 norm_term = (
8629#ifdef FEAT_GUI
8630 !gui.in_use &&
8631#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008632 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 for (row = start_row; row < end_row; ++row)
8634 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008635 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008636#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008637 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008638#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008639 )
8640 {
8641 /* When drawing over the right halve of a double-wide char clear
8642 * out the left halve. When drawing over the left halve of a
8643 * double wide-char clear out the right halve. Only needed in a
8644 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008645 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008646 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008647 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008648 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 /*
8651 * Try to use delete-line termcap code, when no attributes or in a
8652 * "normal" terminal, where a bold/italic space is just a
8653 * space.
8654 */
8655 did_delete = FALSE;
8656 if (c2 == ' '
8657 && end_col == Columns
8658 && can_clear(T_CE)
8659 && (attr == 0
8660 || (norm_term
8661 && attr <= HL_ALL
8662 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8663 {
8664 /*
8665 * check if we really need to clear something
8666 */
8667 col = start_col;
8668 if (c1 != ' ') /* don't clear first char */
8669 ++col;
8670
8671 off = LineOffset[row] + col;
8672 end_off = LineOffset[row] + end_col;
8673
8674 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 if (enc_utf8)
8676 while (off < end_off && ScreenLines[off] == ' '
8677 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8678 ++off;
8679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 while (off < end_off && ScreenLines[off] == ' '
8681 && ScreenAttrs[off] == 0)
8682 ++off;
8683 if (off < end_off) /* something to be cleared */
8684 {
8685 col = off - LineOffset[row];
8686 screen_stop_highlight();
8687 term_windgoto(row, col);/* clear rest of this screen line */
8688 out_str(T_CE);
8689 screen_start(); /* don't know where cursor is now */
8690 col = end_col - col;
8691 while (col--) /* clear chars in ScreenLines */
8692 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008693 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 ++off;
8695 }
8696 }
8697 did_delete = TRUE; /* the chars are cleared now */
8698 }
8699
8700 off = LineOffset[row] + start_col;
8701 c = c1;
8702 for (col = start_col; col < end_col; ++col)
8703 {
8704 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008705 || (enc_utf8 && (int)ScreenLinesUC[off]
8706 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 || ScreenAttrs[off] != attr
8708#if defined(FEAT_GUI) || defined(UNIX)
8709 || force_next
8710#endif
8711 )
8712 {
8713#if defined(FEAT_GUI) || defined(UNIX)
8714 /* The bold trick may make a single row of pixels appear in
8715 * the next character. When a bold character is removed, the
8716 * next character should be redrawn too. This happens for our
8717 * own GUI and for some xterms. */
8718 if (
8719# ifdef FEAT_GUI
8720 gui.in_use
8721# endif
8722# if defined(FEAT_GUI) && defined(UNIX)
8723 ||
8724# endif
8725# ifdef UNIX
8726 term_is_xterm
8727# endif
8728 )
8729 {
8730 if (ScreenLines[off] != ' '
8731 && (ScreenAttrs[off] > HL_ALL
8732 || ScreenAttrs[off] & HL_BOLD))
8733 force_next = TRUE;
8734 else
8735 force_next = FALSE;
8736 }
8737#endif
8738 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 if (enc_utf8)
8740 {
8741 if (c >= 0x80)
8742 {
8743 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008744 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 }
8746 else
8747 ScreenLinesUC[off] = 0;
8748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 ScreenAttrs[off] = attr;
8750 if (!did_delete || c != ' ')
8751 screen_char(off, row, col);
8752 }
8753 ++off;
8754 if (col == start_col)
8755 {
8756 if (did_delete)
8757 break;
8758 c = c2;
8759 }
8760 }
8761 if (end_col == Columns)
8762 LineWraps[row] = FALSE;
8763 if (row == Rows - 1) /* overwritten the command line */
8764 {
8765 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008766 if (start_col == 0 && end_col == Columns
8767 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008769 if (start_col == 0)
8770 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 }
8772 }
8773}
8774
8775/*
8776 * Check if there should be a delay. Used before clearing or redrawing the
8777 * screen or the command line.
8778 */
8779 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008780check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8783 && !did_wait_return
8784 && emsg_silent == 0)
8785 {
8786 out_flush();
8787 ui_delay(1000L, TRUE);
8788 emsg_on_display = FALSE;
8789 if (check_msg_scroll)
8790 msg_scroll = FALSE;
8791 }
8792}
8793
8794/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008795 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8796 */
8797 static void
8798clear_TabPageIdxs(void)
8799{
8800 int scol;
8801
8802 for (scol = 0; scol < Columns; ++scol)
8803 TabPageIdxs[scol] = 0;
8804}
8805
8806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008808 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 * Returns TRUE if there is a valid screen to write to.
8810 * Returns FALSE when starting up and screen not initialized yet.
8811 */
8812 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008813screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008815 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 return (ScreenLines != NULL);
8817}
8818
8819/*
8820 * Resize the shell to Rows and Columns.
8821 * Allocate ScreenLines[] and associated items.
8822 *
8823 * There may be some time between setting Rows and Columns and (re)allocating
8824 * ScreenLines[]. This happens when starting up and when (manually) changing
8825 * the shell size. Always use screen_Rows and screen_Columns to access items
8826 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8827 * final size of the shell is needed.
8828 */
8829 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008830screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831{
8832 int new_row, old_row;
8833#ifdef FEAT_GUI
8834 int old_Rows;
8835#endif
8836 win_T *wp;
8837 int outofmem = FALSE;
8838 int len;
8839 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008843 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 sattr_T *new_ScreenAttrs;
8845 unsigned *new_LineOffset;
8846 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008847 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008848 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008850 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008851 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008853retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 /*
8855 * Allocation of the screen buffers is done only when the size changes and
8856 * when Rows and Columns have been set and we have started doing full
8857 * screen stuff.
8858 */
8859 if ((ScreenLines != NULL
8860 && Rows == screen_Rows
8861 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 && enc_utf8 == (ScreenLinesUC != NULL)
8863 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008864 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 || Rows == 0
8866 || Columns == 0
8867 || (!full_screen && ScreenLines == NULL))
8868 return;
8869
8870 /*
8871 * It's possible that we produce an out-of-memory message below, which
8872 * will cause this function to be called again. To break the loop, just
8873 * return here.
8874 */
8875 if (entered)
8876 return;
8877 entered = TRUE;
8878
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008879 /*
8880 * Note that the window sizes are updated before reallocating the arrays,
8881 * thus we must not redraw here!
8882 */
8883 ++RedrawingDisabled;
8884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 win_new_shellsize(); /* fit the windows in the new sized shell */
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 comp_col(); /* recompute columns for shown command and ruler */
8888
8889 /*
8890 * We're changing the size of the screen.
8891 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8892 * - Move lines from the old arrays into the new arrays, clear extra
8893 * lines (unless the screen is going to be cleared).
8894 * - Free the old arrays.
8895 *
8896 * If anything fails, make ScreenLines NULL, so we don't do anything!
8897 * Continuing with the old ScreenLines may result in a crash, because the
8898 * size is wrong.
8899 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008900 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008902 if (aucmd_win != NULL)
8903 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008905 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008906 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (enc_utf8)
8908 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008909 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008910 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008911 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8912 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008915 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8916 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8917 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8918 new_LineWraps = LALLOC_MULT(char_u, Rows);
8919 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008921 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 {
8923 if (win_alloc_lines(wp) == FAIL)
8924 {
8925 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008926 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008929 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8930 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008931 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008932give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008934 for (i = 0; i < p_mco; ++i)
8935 if (new_ScreenLinesC[i] == NULL)
8936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 || new_ScreenAttrs == NULL
8941 || new_LineOffset == NULL
8942 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008943 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 || outofmem)
8945 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008946 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008947 {
8948 /* guess the size */
8949 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8950
8951 /* Remember we did this to avoid getting outofmem messages over
8952 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008953 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008954 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008955 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008956 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008957 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008958 VIM_CLEAR(new_ScreenLinesC[i]);
8959 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008960 VIM_CLEAR(new_ScreenAttrs);
8961 VIM_CLEAR(new_LineOffset);
8962 VIM_CLEAR(new_LineWraps);
8963 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 else
8966 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008967 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 for (new_row = 0; new_row < Rows; ++new_row)
8970 {
8971 new_LineOffset[new_row] = new_row * Columns;
8972 new_LineWraps[new_row] = FALSE;
8973
8974 /*
8975 * If the screen is not going to be cleared, copy as much as
8976 * possible from the old screen to the new one and clear the rest
8977 * (used when resizing the window at the "--more--" prompt or when
8978 * executing an external command, for the GUI).
8979 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008980 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 {
8982 (void)vim_memset(new_ScreenLines + new_row * Columns,
8983 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 if (enc_utf8)
8985 {
8986 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8987 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008988 for (i = 0; i < p_mco; ++i)
8989 (void)vim_memset(new_ScreenLinesC[i]
8990 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 0, (size_t)Columns * sizeof(u8char_T));
8992 }
8993 if (enc_dbcs == DBCS_JPNU)
8994 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8995 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8997 0, (size_t)Columns * sizeof(sattr_T));
8998 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008999 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
9001 if (screen_Columns < Columns)
9002 len = screen_Columns;
9003 else
9004 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009005 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009006 * may be invalid now. Also when p_mco changes. */
9007 if (!(enc_utf8 && ScreenLinesUC == NULL)
9008 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009009 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9010 ScreenLines + LineOffset[old_row],
9011 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009012 if (enc_utf8 && ScreenLinesUC != NULL
9013 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 {
9015 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9016 ScreenLinesUC + LineOffset[old_row],
9017 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009018 for (i = 0; i < p_mco; ++i)
9019 mch_memmove(new_ScreenLinesC[i]
9020 + new_LineOffset[new_row],
9021 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 (size_t)len * sizeof(u8char_T));
9023 }
9024 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9025 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9026 ScreenLines2 + LineOffset[old_row],
9027 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9029 ScreenAttrs + LineOffset[old_row],
9030 (size_t)len * sizeof(sattr_T));
9031 }
9032 }
9033 }
9034 /* Use the last line of the screen for the current line. */
9035 current_ScreenLine = new_ScreenLines + Rows * Columns;
9036 }
9037
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009038 free_screenlines();
9039
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009042 for (i = 0; i < p_mco; ++i)
9043 ScreenLinesC[i] = new_ScreenLinesC[i];
9044 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 ScreenAttrs = new_ScreenAttrs;
9047 LineOffset = new_LineOffset;
9048 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009049 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 /* It's important that screen_Rows and screen_Columns reflect the actual
9052 * size of ScreenLines[]. Set them before calling anything. */
9053#ifdef FEAT_GUI
9054 old_Rows = screen_Rows;
9055#endif
9056 screen_Rows = Rows;
9057 screen_Columns = Columns;
9058
9059 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009060 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062#ifdef FEAT_GUI
9063 else if (gui.in_use
9064 && !gui.starting
9065 && ScreenLines != NULL
9066 && old_Rows != Rows)
9067 {
9068 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9069 /*
9070 * Adjust the position of the cursor, for when executing an external
9071 * command.
9072 */
9073 if (msg_row >= Rows) /* Rows got smaller */
9074 msg_row = Rows - 1; /* put cursor at last row */
9075 else if (Rows > old_Rows) /* Rows got bigger */
9076 msg_row += Rows - old_Rows; /* put cursor in same place */
9077 if (msg_col >= Columns) /* Columns got smaller */
9078 msg_col = Columns - 1; /* put cursor at last column */
9079 }
9080#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009081 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009084 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009085
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009086 /*
9087 * Do not apply autocommands more than 3 times to avoid an endless loop
9088 * in case applying autocommands always changes Rows or Columns.
9089 */
9090 if (starting == 0 && ++retry_count <= 3)
9091 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009092 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009093 /* In rare cases, autocommands may have altered Rows or Columns,
9094 * jump back to check if we need to allocate the screen again. */
9095 goto retry;
9096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097}
9098
9099 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009100free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009101{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009102 int i;
9103
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009104 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009105 for (i = 0; i < Screen_mco; ++i)
9106 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009107 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009108 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009109 vim_free(ScreenAttrs);
9110 vim_free(LineOffset);
9111 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009112 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009113}
9114
9115 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009116screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
9118 check_for_delay(FALSE);
9119 screenalloc(FALSE); /* allocate screen buffers if size changed */
9120 screenclear2(); /* clear the screen */
9121}
9122
9123 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009124screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126 int i;
9127
9128 if (starting == NO_SCREEN || ScreenLines == NULL
9129#ifdef FEAT_GUI
9130 || (gui.in_use && gui.starting)
9131#endif
9132 )
9133 return;
9134
9135#ifdef FEAT_GUI
9136 if (!gui.in_use)
9137#endif
9138 screen_attr = -1; /* force setting the Normal colors */
9139 screen_stop_highlight(); /* don't want highlighting here */
9140
9141#ifdef FEAT_CLIPBOARD
9142 /* disable selection without redrawing it */
9143 clip_scroll_selection(9999);
9144#endif
9145
9146 /* blank out ScreenLines */
9147 for (i = 0; i < Rows; ++i)
9148 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009149 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 LineWraps[i] = FALSE;
9151 }
9152
9153 if (can_clear(T_CL))
9154 {
9155 out_str(T_CL); /* clear the display */
9156 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009157 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 }
9159 else
9160 {
9161 /* can't clear the screen, mark all chars with invalid attributes */
9162 for (i = 0; i < Rows; ++i)
9163 lineinvalid(LineOffset[i], (int)Columns);
9164 clear_cmdline = TRUE;
9165 }
9166
9167 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9168
9169 win_rest_invalid(firstwin);
9170 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009171 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 if (must_redraw == CLEAR) /* no need to clear again */
9173 must_redraw = NOT_VALID;
9174 compute_cmdrow();
9175 msg_row = cmdline_row; /* put cursor on last line for messages */
9176 msg_col = 0;
9177 screen_start(); /* don't know where cursor is now */
9178 msg_scrolled = 0; /* can't scroll back */
9179 msg_didany = FALSE;
9180 msg_didout = FALSE;
9181}
9182
9183/*
9184 * Clear one line in ScreenLines.
9185 */
9186 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009187lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 if (enc_utf8)
9191 (void)vim_memset(ScreenLinesUC + off, 0,
9192 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009193 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
9197 * Mark one line in ScreenLines invalid by setting the attributes to an
9198 * invalid value.
9199 */
9200 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009201lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9204}
9205
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206/*
9207 * Copy part of a Screenline for vertically split window "wp".
9208 */
9209 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009210linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 unsigned off_to = LineOffset[to] + wp->w_wincol;
9213 unsigned off_from = LineOffset[from] + wp->w_wincol;
9214
9215 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9216 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 if (enc_utf8)
9218 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009219 int i;
9220
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9222 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009223 for (i = 0; i < p_mco; ++i)
9224 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9225 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 }
9227 if (enc_dbcs == DBCS_JPNU)
9228 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9229 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9231 wp->w_width * sizeof(sattr_T));
9232}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234/*
9235 * Return TRUE if clearing with term string "p" would work.
9236 * It can't work when the string is empty or it won't set the right background.
9237 */
9238 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009239can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 return (*p != NUL && (t_colors <= 1
9242#ifdef FEAT_GUI
9243 || gui.in_use
9244#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009245#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009246 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009247 || (!p_tgc && cterm_normal_bg_color == 0)
9248#else
9249 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009250#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009251 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252}
9253
9254/*
9255 * Reset cursor position. Use whenever cursor was moved because of outputting
9256 * something directly to the screen (shell commands) or a terminal control
9257 * code.
9258 */
9259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009260screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 screen_cur_row = screen_cur_col = 9999;
9263}
9264
9265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 * Move the cursor to position "row","col" in the screen.
9267 * This tries to find the most efficient way to move, minimizing the number of
9268 * characters sent to the terminal.
9269 */
9270 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009271windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009273 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 int i;
9275 int plan;
9276 int cost;
9277 int wouldbe_col;
9278 int noinvcurs;
9279 char_u *bs;
9280 int goto_cost;
9281 int attr;
9282
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009283#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9285
9286#define PLAN_LE 1
9287#define PLAN_CR 2
9288#define PLAN_NL 3
9289#define PLAN_WRITE 4
9290 /* Can't use ScreenLines unless initialized */
9291 if (ScreenLines == NULL)
9292 return;
9293
9294 if (col != screen_cur_col || row != screen_cur_row)
9295 {
9296 /* Check for valid position. */
9297 if (row < 0) /* window without text lines? */
9298 row = 0;
9299 if (row >= screen_Rows)
9300 row = screen_Rows - 1;
9301 if (col >= screen_Columns)
9302 col = screen_Columns - 1;
9303
9304 /* check if no cursor movement is allowed in highlight mode */
9305 if (screen_attr && *T_MS == NUL)
9306 noinvcurs = HIGHL_COST;
9307 else
9308 noinvcurs = 0;
9309 goto_cost = GOTO_COST + noinvcurs;
9310
9311 /*
9312 * Plan how to do the positioning:
9313 * 1. Use CR to move it to column 0, same row.
9314 * 2. Use T_LE to move it a few columns to the left.
9315 * 3. Use NL to move a few lines down, column 0.
9316 * 4. Move a few columns to the right with T_ND or by writing chars.
9317 *
9318 * Don't do this if the cursor went beyond the last column, the cursor
9319 * position is unknown then (some terminals wrap, some don't )
9320 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009321 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 * characters to move the cursor to the right.
9323 */
9324 if (row >= screen_cur_row && screen_cur_col < Columns)
9325 {
9326 /*
9327 * If the cursor is in the same row, bigger col, we can use CR
9328 * or T_LE.
9329 */
9330 bs = NULL; /* init for GCC */
9331 attr = screen_attr;
9332 if (row == screen_cur_row && col < screen_cur_col)
9333 {
9334 /* "le" is preferred over "bc", because "bc" is obsolete */
9335 if (*T_LE)
9336 bs = T_LE; /* "cursor left" */
9337 else
9338 bs = T_BC; /* "backspace character (old) */
9339 if (*bs)
9340 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9341 else
9342 cost = 999;
9343 if (col + 1 < cost) /* using CR is less characters */
9344 {
9345 plan = PLAN_CR;
9346 wouldbe_col = 0;
9347 cost = 1; /* CR is just one character */
9348 }
9349 else
9350 {
9351 plan = PLAN_LE;
9352 wouldbe_col = col;
9353 }
9354 if (noinvcurs) /* will stop highlighting */
9355 {
9356 cost += noinvcurs;
9357 attr = 0;
9358 }
9359 }
9360
9361 /*
9362 * If the cursor is above where we want to be, we can use CR LF.
9363 */
9364 else if (row > screen_cur_row)
9365 {
9366 plan = PLAN_NL;
9367 wouldbe_col = 0;
9368 cost = (row - screen_cur_row) * 2; /* CR LF */
9369 if (noinvcurs) /* will stop highlighting */
9370 {
9371 cost += noinvcurs;
9372 attr = 0;
9373 }
9374 }
9375
9376 /*
9377 * If the cursor is in the same row, smaller col, just use write.
9378 */
9379 else
9380 {
9381 plan = PLAN_WRITE;
9382 wouldbe_col = screen_cur_col;
9383 cost = 0;
9384 }
9385
9386 /*
9387 * Check if any characters that need to be written have the
9388 * correct attributes. Also avoid UTF-8 characters.
9389 */
9390 i = col - wouldbe_col;
9391 if (i > 0)
9392 cost += i;
9393 if (cost < goto_cost && i > 0)
9394 {
9395 /*
9396 * Check if the attributes are correct without additionally
9397 * stopping highlighting.
9398 */
9399 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9400 while (i && *p++ == attr)
9401 --i;
9402 if (i != 0)
9403 {
9404 /*
9405 * Try if it works when highlighting is stopped here.
9406 */
9407 if (*--p == 0)
9408 {
9409 cost += noinvcurs;
9410 while (i && *p++ == 0)
9411 --i;
9412 }
9413 if (i != 0)
9414 cost = 999; /* different attributes, don't do it */
9415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 if (enc_utf8)
9417 {
9418 /* Don't use an UTF-8 char for positioning, it's slow. */
9419 for (i = wouldbe_col; i < col; ++i)
9420 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9421 {
9422 cost = 999;
9423 break;
9424 }
9425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427
9428 /*
9429 * We can do it without term_windgoto()!
9430 */
9431 if (cost < goto_cost)
9432 {
9433 if (plan == PLAN_LE)
9434 {
9435 if (noinvcurs)
9436 screen_stop_highlight();
9437 while (screen_cur_col > col)
9438 {
9439 out_str(bs);
9440 --screen_cur_col;
9441 }
9442 }
9443 else if (plan == PLAN_CR)
9444 {
9445 if (noinvcurs)
9446 screen_stop_highlight();
9447 out_char('\r');
9448 screen_cur_col = 0;
9449 }
9450 else if (plan == PLAN_NL)
9451 {
9452 if (noinvcurs)
9453 screen_stop_highlight();
9454 while (screen_cur_row < row)
9455 {
9456 out_char('\n');
9457 ++screen_cur_row;
9458 }
9459 screen_cur_col = 0;
9460 }
9461
9462 i = col - screen_cur_col;
9463 if (i > 0)
9464 {
9465 /*
9466 * Use cursor-right if it's one character only. Avoids
9467 * removing a line of pixels from the last bold char, when
9468 * using the bold trick in the GUI.
9469 */
9470 if (T_ND[0] != NUL && T_ND[1] == NUL)
9471 {
9472 while (i-- > 0)
9473 out_char(*T_ND);
9474 }
9475 else
9476 {
9477 int off;
9478
9479 off = LineOffset[row] + screen_cur_col;
9480 while (i-- > 0)
9481 {
9482 if (ScreenAttrs[off] != screen_attr)
9483 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 if (enc_dbcs == DBCS_JPNU
9487 && ScreenLines[off] == 0x8e)
9488 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 ++off;
9490 }
9491 }
9492 }
9493 }
9494 }
9495 else
9496 cost = 999;
9497
9498 if (cost >= goto_cost)
9499 {
9500 if (noinvcurs)
9501 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009502 if (row == screen_cur_row && (col > screen_cur_col)
9503 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 term_cursor_right(col - screen_cur_col);
9505 else
9506 term_windgoto(row, col);
9507 }
9508 screen_cur_row = row;
9509 screen_cur_col = col;
9510 }
9511}
9512
9513/*
9514 * Set cursor to its position in the current window.
9515 */
9516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009517setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009519 setcursor_mayforce(FALSE);
9520}
9521
9522/*
9523 * Set cursor to its position in the current window.
9524 * When "force" is TRUE also when not redrawing.
9525 */
9526 void
9527setcursor_mayforce(int force)
9528{
9529 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 {
9531 validate_cursor();
9532 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009533 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009535 /* With 'rightleft' set and the cursor on a double-wide
9536 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009537 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9538 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009539 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009540 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541#endif
9542 curwin->w_wcol));
9543 }
9544}
9545
9546
9547/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009548 * Insert 'line_count' lines at 'row' in window 'wp'.
9549 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9550 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 * scrolling.
9552 * Returns FAIL if the lines are not inserted, OK for success.
9553 */
9554 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009555win_ins_lines(
9556 win_T *wp,
9557 int row,
9558 int line_count,
9559 int invalid,
9560 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 int did_delete;
9563 int nextrow;
9564 int lastrow;
9565 int retval;
9566
9567 if (invalid)
9568 wp->w_lines_valid = 0;
9569
9570 if (wp->w_height < 5)
9571 return FAIL;
9572
9573 if (line_count > wp->w_height - row)
9574 line_count = wp->w_height - row;
9575
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009576 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 if (retval != MAYBE)
9578 return retval;
9579
9580 /*
9581 * If there is a next window or a status line, we first try to delete the
9582 * lines at the bottom to avoid messing what is after the window.
9583 * If this fails and there are following windows, don't do anything to avoid
9584 * messing up those windows, better just redraw.
9585 */
9586 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 if (wp->w_next != NULL || wp->w_status_height)
9588 {
9589 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009590 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 did_delete = TRUE;
9592 else if (wp->w_next)
9593 return FAIL;
9594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 /*
9596 * if no lines deleted, blank the lines that will end up below the window
9597 */
9598 if (!did_delete)
9599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009602 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 lastrow = nextrow + line_count;
9604 if (lastrow > Rows)
9605 lastrow = Rows;
9606 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009607 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 ' ', ' ', 0);
9609 }
9610
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009611 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 == FAIL)
9613 {
9614 /* deletion will have messed up other windows */
9615 if (did_delete)
9616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 win_rest_invalid(W_NEXT(wp));
9619 }
9620 return FAIL;
9621 }
9622
9623 return OK;
9624}
9625
9626/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009627 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9629 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9630 * scrolling
9631 * Return OK for success, FAIL if the lines are not deleted.
9632 */
9633 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009634win_del_lines(
9635 win_T *wp,
9636 int row,
9637 int line_count,
9638 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009639 int mayclear,
9640 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 int retval;
9643
9644 if (invalid)
9645 wp->w_lines_valid = 0;
9646
9647 if (line_count > wp->w_height - row)
9648 line_count = wp->w_height - row;
9649
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009650 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (retval != MAYBE)
9652 return retval;
9653
9654 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009655 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 return FAIL;
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 /*
9659 * If there are windows or status lines below, try to put them at the
9660 * correct place. If we can't do that, they have to be redrawn.
9661 */
9662 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9663 {
9664 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009665 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 {
9667 wp->w_redr_status = TRUE;
9668 win_rest_invalid(wp->w_next);
9669 }
9670 }
9671 /*
9672 * If this is the last window and there is no status line, redraw the
9673 * command line later.
9674 */
9675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 redraw_cmdline = TRUE;
9677 return OK;
9678}
9679
9680/*
9681 * Common code for win_ins_lines() and win_del_lines().
9682 * Returns OK or FAIL when the work has been done.
9683 * Returns MAYBE when not finished yet.
9684 */
9685 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009686win_do_lines(
9687 win_T *wp,
9688 int row,
9689 int line_count,
9690 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009691 int del,
9692 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 int retval;
9695
9696 if (!redrawing() || line_count <= 0)
9697 return FAIL;
9698
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009699 /* When inserting lines would result in loss of command output, just redraw
9700 * the lines. */
9701 if (no_win_do_lines_ins && !del)
9702 return FAIL;
9703
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009705 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009707 if (!no_win_do_lines_ins)
9708 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 return FAIL;
9710 }
9711
9712 /*
9713 * Delete all remaining lines
9714 */
9715 if (row + line_count >= wp->w_height)
9716 {
9717 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009718 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 ' ', ' ', 0);
9720 return OK;
9721 }
9722
9723 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009724 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009726 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009728 if (!no_win_do_lines_ins)
9729 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
9731 /*
9732 * If the terminal can set a scroll region, use that.
9733 * Always do this in a vertically split window. This will redraw from
9734 * ScreenLines[] when t_CV isn't defined. That's faster than using
9735 * win_line().
9736 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009737 * a character in the lower right corner of the scroll region may cause a
9738 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009740 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 scroll_region_set(wp, row);
9744 if (del)
9745 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009746 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 else
9748 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009749 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 scroll_region_reset();
9752 return retval;
9753 }
9754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9756 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757
9758 return MAYBE;
9759}
9760
9761/*
9762 * window 'wp' and everything after it is messed up, mark it for redraw
9763 */
9764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009765win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 {
9769 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 wp->w_redr_status = TRUE;
9771 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 }
9773 redraw_cmdline = TRUE;
9774}
9775
9776/*
9777 * The rest of the routines in this file perform screen manipulations. The
9778 * given operation is performed physically on the screen. The corresponding
9779 * change is also made to the internal screen image. In this way, the editor
9780 * anticipates the effect of editing changes on the appearance of the screen.
9781 * That way, when we call screenupdate a complete redraw isn't usually
9782 * necessary. Another advantage is that we can keep adding code to anticipate
9783 * screen changes, and in the meantime, everything still works.
9784 */
9785
9786/*
9787 * types for inserting or deleting lines
9788 */
9789#define USE_T_CAL 1
9790#define USE_T_CDL 2
9791#define USE_T_AL 3
9792#define USE_T_CE 4
9793#define USE_T_DL 5
9794#define USE_T_SR 6
9795#define USE_NL 7
9796#define USE_T_CD 8
9797#define USE_REDRAW 9
9798
9799/*
9800 * insert lines on the screen and update ScreenLines[]
9801 * 'end' is the line after the scrolled part. Normally it is Rows.
9802 * When scrolling region used 'off' is the offset from the top for the region.
9803 * 'row' and 'end' are relative to the start of the region.
9804 *
9805 * return FAIL for failure, OK for success.
9806 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009807 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009808screen_ins_lines(
9809 int off,
9810 int row,
9811 int line_count,
9812 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009813 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009814 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 int i;
9817 int j;
9818 unsigned temp;
9819 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009820 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 int type;
9822 int result_empty;
9823 int can_ce = can_clear(T_CE);
9824
9825 /*
9826 * FAIL if
9827 * - there is no valid screen
9828 * - the screen has to be redrawn completely
9829 * - the line count is less than one
9830 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009831 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009833 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9834#ifdef FEAT_CLIPBOARD
9835 || (clip_star.state != SELECT_CLEARED
9836 && redrawing_for_callback > 0)
9837#endif
9838 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 return FAIL;
9840
9841 /*
9842 * There are seven ways to insert lines:
9843 * 0. When in a vertically split window and t_CV isn't set, redraw the
9844 * characters from ScreenLines[].
9845 * 1. Use T_CD (clear to end of display) if it exists and the result of
9846 * the insert is just empty lines
9847 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9848 * present or line_count > 1. It looks better if we do all the inserts
9849 * at once.
9850 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9851 * insert is just empty lines and T_CE is not present or line_count >
9852 * 1.
9853 * 4. Use T_AL (insert line) if it exists.
9854 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9855 * just empty lines.
9856 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9857 * just empty lines.
9858 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9859 * the 'da' flag is not set or we have clear line capability.
9860 * 8. redraw the characters from ScreenLines[].
9861 *
9862 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9863 * the scrollbar for the window. It does have insert line, use that if it
9864 * exists.
9865 */
9866 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9868 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009869 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 type = USE_T_CD;
9871 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9872 type = USE_T_CAL;
9873 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9874 type = USE_T_CDL;
9875 else if (*T_AL != NUL)
9876 type = USE_T_AL;
9877 else if (can_ce && result_empty)
9878 type = USE_T_CE;
9879 else if (*T_DL != NUL && result_empty)
9880 type = USE_T_DL;
9881 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9882 type = USE_T_SR;
9883 else
9884 return FAIL;
9885
9886 /*
9887 * For clearing the lines screen_del_lines() is used. This will also take
9888 * care of t_db if necessary.
9889 */
9890 if (type == USE_T_CD || type == USE_T_CDL ||
9891 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009892 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
9894 /*
9895 * If text is retained below the screen, first clear or delete as many
9896 * lines at the bottom of the window as are about to be inserted so that
9897 * the deleted lines won't later surface during a screen_del_lines.
9898 */
9899 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009900 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
9902#ifdef FEAT_CLIPBOARD
9903 /* Remove a modeless selection when inserting lines halfway the screen
9904 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009905 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009906 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 else
9908 clip_scroll_selection(-line_count);
9909#endif
9910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911#ifdef FEAT_GUI
9912 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9913 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009914 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915#endif
9916
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009917 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9918 cursor_col = wp->w_wincol;
9919
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 if (*T_CCS != NUL) /* cursor relative to region */
9921 cursor_row = row;
9922 else
9923 cursor_row = row + off;
9924
9925 /*
9926 * Shift LineOffset[] line_count down to reflect the inserted lines.
9927 * Clear the inserted lines in ScreenLines[].
9928 */
9929 row += off;
9930 end += off;
9931 for (i = 0; i < line_count; ++i)
9932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 if (wp != NULL && wp->w_width != Columns)
9934 {
9935 /* need to copy part of a line */
9936 j = end - 1 - i;
9937 while ((j -= line_count) >= row)
9938 linecopy(j + line_count, j, wp);
9939 j += line_count;
9940 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009941 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9942 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 else
9944 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9945 LineWraps[j] = FALSE;
9946 }
9947 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 {
9949 j = end - 1 - i;
9950 temp = LineOffset[j];
9951 while ((j -= line_count) >= row)
9952 {
9953 LineOffset[j + line_count] = LineOffset[j];
9954 LineWraps[j + line_count] = LineWraps[j];
9955 }
9956 LineOffset[j + line_count] = temp;
9957 LineWraps[j + line_count] = FALSE;
9958 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009959 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 else
9961 lineinvalid(temp, (int)Columns);
9962 }
9963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
9965 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009966 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009967 if (clear_attr != 0)
9968 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 /* redraw the characters */
9971 if (type == USE_REDRAW)
9972 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009973 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 {
9975 term_append_lines(line_count);
9976 screen_start(); /* don't know where cursor is now */
9977 }
9978 else
9979 {
9980 for (i = 0; i < line_count; i++)
9981 {
9982 if (type == USE_T_AL)
9983 {
9984 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009985 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 out_str(T_AL);
9987 }
9988 else /* type == USE_T_SR */
9989 out_str(T_SR);
9990 screen_start(); /* don't know where cursor is now */
9991 }
9992 }
9993
9994 /*
9995 * With scroll-reverse and 'da' flag set we need to clear the lines that
9996 * have been scrolled down into the region.
9997 */
9998 if (type == USE_T_SR && *T_DA)
9999 {
10000 for (i = 0; i < line_count; ++i)
10001 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010002 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003 out_str(T_CE);
10004 screen_start(); /* don't know where cursor is now */
10005 }
10006 }
10007
10008#ifdef FEAT_GUI
10009 gui_can_update_cursor();
10010 if (gui.in_use)
10011 out_flush(); /* always flush after a scroll */
10012#endif
10013 return OK;
10014}
10015
10016/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010017 * Delete lines on the screen and update ScreenLines[].
10018 * "end" is the line after the scrolled part. Normally it is Rows.
10019 * When scrolling region used "off" is the offset from the top for the region.
10020 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 *
10022 * Return OK for success, FAIL if the lines are not deleted.
10023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010025screen_del_lines(
10026 int off,
10027 int row,
10028 int line_count,
10029 int end,
10030 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010031 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010032 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033{
10034 int j;
10035 int i;
10036 unsigned temp;
10037 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010038 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 int cursor_end;
10040 int result_empty; /* result is empty until end of region */
10041 int can_delete; /* deleting line codes can be used */
10042 int type;
10043
10044 /*
10045 * FAIL if
10046 * - there is no valid screen
10047 * - the screen has to be redrawn completely
10048 * - the line count is less than one
10049 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010050 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010052 if (!screen_valid(TRUE) || line_count <= 0
10053 || (!force && line_count > p_ttyscroll)
10054#ifdef FEAT_CLIPBOARD
10055 || (clip_star.state != SELECT_CLEARED
10056 && redrawing_for_callback > 0)
10057#endif
10058 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 return FAIL;
10060
10061 /*
10062 * Check if the rest of the current region will become empty.
10063 */
10064 result_empty = row + line_count >= end;
10065
10066 /*
10067 * We can delete lines only when 'db' flag not set or when 'ce' option
10068 * available.
10069 */
10070 can_delete = (*T_DB == NUL || can_clear(T_CE));
10071
10072 /*
10073 * There are six ways to delete lines:
10074 * 0. When in a vertically split window and t_CV isn't set, redraw the
10075 * characters from ScreenLines[].
10076 * 1. Use T_CD if it exists and the result is empty.
10077 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10078 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10079 * none of the other ways work.
10080 * 4. Use T_CE (erase line) if the result is empty.
10081 * 5. Use T_DL (delete line) if it exists.
10082 * 6. redraw the characters from ScreenLines[].
10083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10085 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010086 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 type = USE_T_CD;
10088#if defined(__BEOS__) && defined(BEOS_DR8)
10089 /*
10090 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10091 * its internal termcap... this works okay for tests which test *T_DB !=
10092 * NUL. It has the disadvantage that the user cannot use any :set t_*
10093 * command to get T_DB (back) to empty_option, only :set term=... will do
10094 * the trick...
10095 * Anyway, this hack will hopefully go away with the next OS release.
10096 * (Olaf Seibert)
10097 */
10098 else if (row == 0 && T_DB == empty_option
10099 && (line_count == 1 || *T_CDL == NUL))
10100#else
10101 else if (row == 0 && (
10102#ifndef AMIGA
10103 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10104 * up, so use delete-line command */
10105 line_count == 1 ||
10106#endif
10107 *T_CDL == NUL))
10108#endif
10109 type = USE_NL;
10110 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10111 type = USE_T_CDL;
10112 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010113 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 type = USE_T_CE;
10115 else if (*T_DL != NUL && can_delete)
10116 type = USE_T_DL;
10117 else if (*T_CDL != NUL && can_delete)
10118 type = USE_T_CDL;
10119 else
10120 return FAIL;
10121
10122#ifdef FEAT_CLIPBOARD
10123 /* Remove a modeless selection when deleting lines halfway the screen or
10124 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010125 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010126 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 else
10128 clip_scroll_selection(line_count);
10129#endif
10130
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131#ifdef FEAT_GUI
10132 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10133 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010134 gui_dont_update_cursor(gui.cursor_row >= row + off
10135 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136#endif
10137
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010138 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10139 cursor_col = wp->w_wincol;
10140
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 if (*T_CCS != NUL) /* cursor relative to region */
10142 {
10143 cursor_row = row;
10144 cursor_end = end;
10145 }
10146 else
10147 {
10148 cursor_row = row + off;
10149 cursor_end = end + off;
10150 }
10151
10152 /*
10153 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10154 * Clear the inserted lines in ScreenLines[].
10155 */
10156 row += off;
10157 end += off;
10158 for (i = 0; i < line_count; ++i)
10159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 if (wp != NULL && wp->w_width != Columns)
10161 {
10162 /* need to copy part of a line */
10163 j = row + i;
10164 while ((j += line_count) <= end - 1)
10165 linecopy(j - line_count, j, wp);
10166 j -= line_count;
10167 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010168 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10169 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 else
10171 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10172 LineWraps[j] = FALSE;
10173 }
10174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 {
10176 /* whole width, moving the line pointers is faster */
10177 j = row + i;
10178 temp = LineOffset[j];
10179 while ((j += line_count) <= end - 1)
10180 {
10181 LineOffset[j - line_count] = LineOffset[j];
10182 LineWraps[j - line_count] = LineWraps[j];
10183 }
10184 LineOffset[j - line_count] = temp;
10185 LineWraps[j - line_count] = FALSE;
10186 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010187 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
10189 lineinvalid(temp, (int)Columns);
10190 }
10191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010193 if (screen_attr != clear_attr)
10194 screen_stop_highlight();
10195 if (clear_attr != 0)
10196 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 /* redraw the characters */
10199 if (type == USE_REDRAW)
10200 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010201 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010203 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 out_str(T_CD);
10205 screen_start(); /* don't know where cursor is now */
10206 }
10207 else if (type == USE_T_CDL)
10208 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010209 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 term_delete_lines(line_count);
10211 screen_start(); /* don't know where cursor is now */
10212 }
10213 /*
10214 * Deleting lines at top of the screen or scroll region: Just scroll
10215 * the whole screen (scroll region) up by outputting newlines on the
10216 * last line.
10217 */
10218 else if (type == USE_NL)
10219 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010220 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 for (i = line_count; --i >= 0; )
10222 out_char('\n'); /* cursor will remain on same line */
10223 }
10224 else
10225 {
10226 for (i = line_count; --i >= 0; )
10227 {
10228 if (type == USE_T_DL)
10229 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010230 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 out_str(T_DL); /* delete a line */
10232 }
10233 else /* type == USE_T_CE */
10234 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010235 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 out_str(T_CE); /* erase a line */
10237 }
10238 screen_start(); /* don't know where cursor is now */
10239 }
10240 }
10241
10242 /*
10243 * If the 'db' flag is set, we need to clear the lines that have been
10244 * scrolled up at the bottom of the region.
10245 */
10246 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10247 {
10248 for (i = line_count; i > 0; --i)
10249 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010250 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 out_str(T_CE); /* erase a line */
10252 screen_start(); /* don't know where cursor is now */
10253 }
10254 }
10255
10256#ifdef FEAT_GUI
10257 gui_can_update_cursor();
10258 if (gui.in_use)
10259 out_flush(); /* always flush after a scroll */
10260#endif
10261
10262 return OK;
10263}
10264
10265/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010266 * Return TRUE when postponing displaying the mode message: when not redrawing
10267 * or inside a mapping.
10268 */
10269 int
10270skip_showmode()
10271{
10272 // Call char_avail() only when we are going to show something, because it
10273 // takes a bit of time. redrawing() may also call char_avail_avail().
10274 if (global_busy
10275 || msg_silent != 0
10276 || !redrawing()
10277 || (char_avail() && !KeyTyped))
10278 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010279 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010280 return TRUE;
10281 }
10282 return FALSE;
10283}
10284
10285/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010286 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 *
10288 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10289 * If clear_cmdline is FALSE there may be a message there that needs to be
10290 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010291 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 * Return the length of the message (0 if no message).
10293 */
10294 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010295showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296{
10297 int need_clear;
10298 int length = 0;
10299 int do_mode;
10300 int attr;
10301 int nwr_save;
10302#ifdef FEAT_INS_EXPAND
10303 int sub_attr;
10304#endif
10305
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010306 do_mode = ((p_smd && msg_silent == 0)
10307 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010308 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010309 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010310 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010312 if (skip_showmode())
10313 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314
10315 nwr_save = need_wait_return;
10316
10317 /* wait a bit before overwriting an important message */
10318 check_for_delay(FALSE);
10319
10320 /* if the cmdline is more than one line high, erase top lines */
10321 need_clear = clear_cmdline;
10322 if (clear_cmdline && cmdline_row < Rows - 1)
10323 msg_clr_cmdline(); /* will reset clear_cmdline */
10324
10325 /* Position on the last line in the window, column 0 */
10326 msg_pos_mode();
10327 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010328 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (do_mode)
10330 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010331 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010333 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010334# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010335 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010336# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010337 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010338# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010339 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010340# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010341 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010343 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344# endif
10345#endif
10346#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10347 if (gui.in_use)
10348 {
10349 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010350 {
10351 /* HANGUL */
10352 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010353 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010354 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010355 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 }
10358#endif
10359#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010360 /* CTRL-X in Insert mode */
10361 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 {
10363 /* These messages can get long, avoid a wrap in a narrow
10364 * window. Prefer showing edit_submode_extra. */
10365 length = (Rows - msg_row) * Columns - 3;
10366 if (edit_submode_extra != NULL)
10367 length -= vim_strsize(edit_submode_extra);
10368 if (length > 0)
10369 {
10370 if (edit_submode_pre != NULL)
10371 length -= vim_strsize(edit_submode_pre);
10372 if (length - vim_strsize(edit_submode) > 0)
10373 {
10374 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010375 msg_puts_attr((char *)edit_submode_pre, attr);
10376 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 }
10378 if (edit_submode_extra != NULL)
10379 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010380 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010382 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 else
10384 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010385 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 }
10387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 }
10389 else
10390#endif
10391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010393 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010394 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010395 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 else if (State & INSERT)
10397 {
10398#ifdef FEAT_RIGHTLEFT
10399 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010400 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010402 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010404 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010405 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010407 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010409 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410#ifdef FEAT_RIGHTLEFT
10411 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010412 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413#endif
10414#ifdef FEAT_KEYMAP
10415 if (State & LANGMAP)
10416 {
10417# ifdef FEAT_ARABIC
10418 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010419 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 else
10421# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010422 if (get_keymap_str(curwin, (char_u *)" (%s)",
10423 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010424 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 }
10426#endif
10427 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010428 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 if (VIsual_active)
10431 {
10432 char *p;
10433
10434 /* Don't concatenate separate words to avoid translation
10435 * problems. */
10436 switch ((VIsual_select ? 4 : 0)
10437 + (VIsual_mode == Ctrl_V) * 2
10438 + (VIsual_mode == 'V'))
10439 {
10440 case 0: p = N_(" VISUAL"); break;
10441 case 1: p = N_(" VISUAL LINE"); break;
10442 case 2: p = N_(" VISUAL BLOCK"); break;
10443 case 4: p = N_(" SELECT"); break;
10444 case 5: p = N_(" SELECT LINE"); break;
10445 default: p = N_(" SELECT BLOCK"); break;
10446 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010447 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010449 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010451
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 need_clear = TRUE;
10453 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010454 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455#ifdef FEAT_INS_EXPAND
10456 && edit_submode == NULL /* otherwise it gets too long */
10457#endif
10458 )
10459 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010460 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 need_clear = TRUE;
10462 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010463
10464 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010465 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 msg_clr_eos();
10467 msg_didout = FALSE; /* overwrite this message */
10468 length = msg_col;
10469 msg_col = 0;
10470 need_wait_return = nwr_save; /* never ask for hit-return for this */
10471 }
10472 else if (clear_cmdline && msg_silent == 0)
10473 /* Clear the whole command line. Will reset "clear_cmdline". */
10474 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010475 else if (redraw_mode)
10476 {
10477 msg_pos_mode();
10478 msg_clr_eos();
10479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
10481#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 /* In Visual mode the size of the selected area must be redrawn. */
10483 if (VIsual_active)
10484 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485
10486 /* If the last window has no status line, the ruler is after the mode
10487 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010488 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010489 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490#endif
10491 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010492 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 clear_cmdline = FALSE;
10494
10495 return length;
10496}
10497
10498/*
10499 * Position for a mode message.
10500 */
10501 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010502msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 msg_col = 0;
10505 msg_row = Rows - 1;
10506}
10507
10508/*
10509 * Delete mode message. Used when ESC is typed which is expected to end
10510 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010511 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 */
10513 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010514unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515{
10516 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010517 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 */
10519 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10520 redraw_cmdline = TRUE; /* delete mode later */
10521 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010522 clearmode();
10523}
10524
10525/*
10526 * Clear the mode message.
10527 */
10528 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010529clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010530{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010531 int save_msg_row = msg_row;
10532 int save_msg_col = msg_col;
10533
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010534 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010535 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010536 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010537 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010538
10539 msg_col = save_msg_col;
10540 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541}
10542
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010543 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010544recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010545{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010546 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010547 if (!shortmess(SHM_RECORDING))
10548 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010549 char s[4];
10550
10551 sprintf(s, " @%c", reg_recording);
10552 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010553 }
10554}
10555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010556/*
10557 * Draw the tab pages line at the top of the Vim window.
10558 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010559 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010560draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010561{
10562 int tabcount = 0;
10563 tabpage_T *tp;
10564 int tabwidth;
10565 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010566 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010567 int attr;
10568 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010569 win_T *cwp;
10570 int wincount;
10571 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010572 int c;
10573 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010574 int attr_sel = HL_ATTR(HLF_TPS);
10575 int attr_nosel = HL_ATTR(HLF_TP);
10576 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010577 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010578 int room;
10579 int use_sep_chars = (t_colors < 8
10580#ifdef FEAT_GUI
10581 && !gui.in_use
10582#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010583#ifdef FEAT_TERMGUICOLORS
10584 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010585#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010586 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010587
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010588 if (ScreenLines == NULL)
10589 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010590 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010591
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010592#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010593 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010594 if (gui_use_tabline())
10595 {
10596 gui_update_tabline();
10597 return;
10598 }
10599#endif
10600
10601 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010602 return;
10603
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010604#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010605 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010606
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010607 /* Use the 'tabline' option if it's set. */
10608 if (*p_tal != NUL)
10609 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010610 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010611
10612 /* Check for an error. If there is one we would loop in redrawing the
10613 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010614 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010615 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010616 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010618 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010619 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010620 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010622#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010623 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010624 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010625 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010626
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10628 if (tabwidth < 6)
10629 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010630
Bram Moolenaar238a5642006-02-21 22:12:05 +000010631 attr = attr_nosel;
10632 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010633 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10634 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010635 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010636 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010637
Bram Moolenaar238a5642006-02-21 22:12:05 +000010638 if (tp->tp_topframe == topframe)
10639 attr = attr_sel;
10640 if (use_sep_chars && col > 0)
10641 screen_putchar('|', 0, col++, attr);
10642
10643 if (tp->tp_topframe != topframe)
10644 attr = attr_nosel;
10645
10646 screen_putchar(' ', 0, col++, attr);
10647
10648 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010649 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010650 cwp = curwin;
10651 wp = firstwin;
10652 }
10653 else
10654 {
10655 cwp = tp->tp_curwin;
10656 wp = tp->tp_firstwin;
10657 }
10658
10659 modified = FALSE;
10660 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10661 if (bufIsChanged(wp->w_buffer))
10662 modified = TRUE;
10663 if (modified || wincount > 1)
10664 {
10665 if (wincount > 1)
10666 {
10667 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010668 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010669 if (col + len >= Columns - 3)
10670 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010671 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010672#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010673 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010674#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010675 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010676#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010677 );
10678 col += len;
10679 }
10680 if (modified)
10681 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10682 screen_putchar(' ', 0, col++, attr);
10683 }
10684
10685 room = scol - col + tabwidth - 1;
10686 if (room > 0)
10687 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010688 /* Get buffer name in NameBuff[] */
10689 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010690 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010691 len = vim_strsize(NameBuff);
10692 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010693 if (has_mbyte)
10694 while (len > room)
10695 {
10696 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010697 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010698 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010699 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010700 {
10701 p += len - room;
10702 len = room;
10703 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010704 if (len > Columns - col - 1)
10705 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010706
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010707 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010708 col += len;
10709 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010710 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010711
10712 /* Store the tab page number in TabPageIdxs[], so that
10713 * jump_to_mouse() knows where each one is. */
10714 ++tabcount;
10715 while (scol < col)
10716 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010717 }
10718
Bram Moolenaar238a5642006-02-21 22:12:05 +000010719 if (use_sep_chars)
10720 c = '_';
10721 else
10722 c = ' ';
10723 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010724
10725 /* Put an "X" for closing the current tab if there are several. */
10726 if (first_tabpage->tp_next != NULL)
10727 {
10728 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10729 TabPageIdxs[Columns - 1] = -999;
10730 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010731 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010732
10733 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10734 * set. */
10735 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010736}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010737
10738/*
10739 * Get buffer name for "buf" into NameBuff[].
10740 * Takes care of special buffer names and translates special characters.
10741 */
10742 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010743get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010744{
10745 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010746 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010747 else
10748 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10749 trans_characters(NameBuff, MAXPATHL);
10750}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752/*
10753 * Get the character to use in a status line. Get its attributes in "*attr".
10754 */
10755 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759
10760#ifdef FEAT_TERMINAL
10761 if (bt_terminal(wp->w_buffer))
10762 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010763 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010764 {
10765 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010766 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010767 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010768 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010769 {
10770 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010771 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010772 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010773 }
10774 else
10775#endif
10776 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010778 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 fill = fill_stl;
10780 }
10781 else
10782 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010783 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 fill = fill_stlnc;
10785 }
10786 /* Use fill when there is highlighting, and highlighting of current
10787 * window differs, or the fillchars differ, or this is not the
10788 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010789 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010790 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 || (fill_stl != fill_stlnc)))
10792 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010793 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 return '^';
10795 return '=';
10796}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798/*
10799 * Get the character to use in a separator between vertically split windows.
10800 * Get its attributes in "*attr".
10801 */
10802 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010803fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010805 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (*attr == 0 && fill_vert == ' ')
10807 return '|';
10808 else
10809 return fill_vert;
10810}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
10812/*
10813 * Return TRUE if redrawing should currently be done.
10814 */
10815 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010816redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010818#ifdef FEAT_EVAL
10819 if (disable_redraw_for_testing)
10820 return 0;
10821 else
10822#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010823 return ((!RedrawingDisabled
10824#ifdef FEAT_EVAL
10825 || ignore_redraw_flag_for_testing
10826#endif
10827 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828}
10829
10830/*
10831 * Return TRUE if printing messages should currently be done.
10832 */
10833 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010834messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 return (!(p_lz && char_avail() && !KeyTyped));
10837}
10838
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010839#ifdef FEAT_MENU
10840/*
10841 * Draw the window toolbar.
10842 */
10843 static void
10844redraw_win_toolbar(win_T *wp)
10845{
10846 vimmenu_T *menu;
10847 int item_idx = 0;
10848 int item_count = 0;
10849 int col = 0;
10850 int next_col;
10851 int off = (int)(current_ScreenLine - ScreenLines);
10852 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10853 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10854
10855 vim_free(wp->w_winbar_items);
10856 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10857 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010858 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010859
10860 /* TODO: use fewer spaces if there is not enough room */
10861 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010862 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010863 {
10864 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010865 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010866 break;
10867 if (col > 1)
10868 {
10869 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010870 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010871 break;
10872 }
10873
10874 wp->w_winbar_items[item_idx].wb_startcol = col;
10875 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010876 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010877 break;
10878
10879 next_col = text_to_screenline(wp, menu->name, col);
10880 while (col < next_col)
10881 {
10882 ScreenAttrs[off + col] = button_attr;
10883 ++col;
10884 }
10885 wp->w_winbar_items[item_idx].wb_endcol = col;
10886 wp->w_winbar_items[item_idx].wb_menu = menu;
10887 ++item_idx;
10888
Bram Moolenaar02631462017-09-22 15:20:32 +020010889 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010890 break;
10891 space_to_screenline(off + col, button_attr);
10892 ++col;
10893 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010894 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010895 {
10896 space_to_screenline(off + col, fill_attr);
10897 ++col;
10898 }
10899 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10900
Bram Moolenaar02631462017-09-22 15:20:32 +020010901 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010902 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010903}
10904#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010905
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906/*
10907 * Show current status info in ruler and various other places
10908 * If always is FALSE, only show ruler if position has changed.
10909 */
10910 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010911showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913 if (!always && !redrawing())
10914 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010915#ifdef FEAT_INS_EXPAND
10916 if (pum_visible())
10917 {
10918 /* Don't redraw right now, do it later. */
10919 curwin->w_redr_status = TRUE;
10920 return;
10921 }
10922#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010923#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010924 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010925 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 else
10927#endif
10928#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010929 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930#endif
10931
10932#ifdef FEAT_TITLE
10933 if (need_maketitle
10934# ifdef FEAT_STL_OPT
10935 || (p_icon && (stl_syntax & STL_IN_ICON))
10936 || (p_title && (stl_syntax & STL_IN_TITLE))
10937# endif
10938 )
10939 maketitle();
10940#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010941 /* Redraw the tab pages line if needed. */
10942 if (redraw_tabline)
10943 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944}
10945
10946#ifdef FEAT_CMDL_INFO
10947 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010948win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010950#define RULER_BUF_LEN 70
10951 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 int row;
10953 int fillchar;
10954 int attr;
10955 int empty_line = FALSE;
10956 colnr_T virtcol;
10957 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010958 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 int this_ru_col;
10961 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010962 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963
10964 /* If 'ruler' off or redrawing disabled, don't do anything */
10965 if (!p_ru)
10966 return;
10967
10968 /*
10969 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10970 * after deleting lines, before cursor.lnum is corrected.
10971 */
10972 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10973 return;
10974
10975#ifdef FEAT_INS_EXPAND
10976 /* Don't draw the ruler while doing insert-completion, it might overwrite
10977 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 if (edit_submode != NULL)
10980 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010981 // Don't draw the ruler when the popup menu is visible, it may overlap.
10982 // Except when the popup menu will be redrawn anyway.
10983 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010984 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985#endif
10986
10987#ifdef FEAT_STL_OPT
10988 if (*p_ruf)
10989 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010990 int save_called_emsg = called_emsg;
10991
10992 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010994 if (called_emsg)
10995 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010996 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010997 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 return;
10999 }
11000#endif
11001
11002 /*
11003 * Check if not in Insert mode and the line is empty (will show "0-1").
11004 */
11005 if (!(State & INSERT)
11006 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11007 empty_line = TRUE;
11008
11009 /*
11010 * Only draw the ruler when something changed.
11011 */
11012 validate_virtcol_win(wp);
11013 if ( redraw_cmdline
11014 || always
11015 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11016 || wp->w_cursor.col != wp->w_ru_cursor.col
11017 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 || wp->w_topline != wp->w_ru_topline
11020 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11021#ifdef FEAT_DIFF
11022 || wp->w_topfill != wp->w_ru_topfill
11023#endif
11024 || empty_line != wp->w_ru_empty)
11025 {
11026 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 if (wp->w_status_height)
11028 {
11029 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011030 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011031 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011032 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 }
11034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 {
11036 row = Rows - 1;
11037 fillchar = ' ';
11038 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 width = Columns;
11040 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 }
11042
11043 /* In list mode virtcol needs to be recomputed */
11044 virtcol = wp->w_virtcol;
11045 if (wp->w_p_list && lcs_tab1 == NUL)
11046 {
11047 wp->w_p_list = FALSE;
11048 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11049 wp->w_p_list = TRUE;
11050 }
11051
11052 /*
11053 * Some sprintfs return the length, some return a pointer.
11054 * To avoid portability problems we use strlen() here.
11055 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011056 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11058 ? 0L
11059 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011060 len = STRLEN(buffer);
11061 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11063 (int)virtcol + 1);
11064
11065 /*
11066 * Add a "50%" if there is room for it.
11067 * On the last line, don't print in the last column (scrolls the
11068 * screen up on some terminals).
11069 */
11070 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011071 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 this_ru_col = ru_col - (Columns - width);
11076 if (this_ru_col < 0)
11077 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 /* Never use more than half the window/screen width, leave the other
11079 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011080 if (this_ru_col < (width + 1) / 2)
11081 this_ru_col = (width + 1) / 2;
11082 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011084 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011085 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 if (has_mbyte)
11088 i += (*mb_char2bytes)(fillchar, buffer + i);
11089 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 buffer[i++] = fillchar;
11091 ++o;
11092 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011093 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 }
11095 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 if (has_mbyte)
11097 {
11098 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011099 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 {
11101 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011102 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 {
11104 buffer[i] = NUL;
11105 break;
11106 }
11107 }
11108 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011109 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011110 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
Bram Moolenaar4033c552017-09-16 20:54:51 +020011112 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 i = redraw_cmdline;
11114 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011115 this_ru_col + off + (int)STRLEN(buffer),
11116 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 fillchar, fillchar, attr);
11118 /* don't redraw the cmdline because of showing the ruler */
11119 redraw_cmdline = i;
11120 wp->w_ru_cursor = wp->w_cursor;
11121 wp->w_ru_virtcol = wp->w_virtcol;
11122 wp->w_ru_empty = empty_line;
11123 wp->w_ru_topline = wp->w_topline;
11124 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11125#ifdef FEAT_DIFF
11126 wp->w_ru_topfill = wp->w_topfill;
11127#endif
11128 }
11129}
11130#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011131
11132#if defined(FEAT_LINEBREAK) || defined(PROTO)
11133/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011134 * Return the width of the 'number' and 'relativenumber' column.
11135 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011136 * Otherwise it depends on 'numberwidth' and the line count.
11137 */
11138 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011139number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011140{
11141 int n;
11142 linenr_T lnum;
11143
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011144 if (wp->w_p_rnu && !wp->w_p_nu)
11145 /* cursor line shows "0" */
11146 lnum = wp->w_height;
11147 else
11148 /* cursor line shows absolute line number */
11149 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011150
Bram Moolenaar6b314672015-03-20 15:42:10 +010011151 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011152 return wp->w_nrwidth_width;
11153 wp->w_nrwidth_line_count = lnum;
11154
11155 n = 0;
11156 do
11157 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011158 lnum /= 10;
11159 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011160 } while (lnum > 0);
11161
11162 /* 'numberwidth' gives the minimal width plus one */
11163 if (n < wp->w_p_nuw - 1)
11164 n = wp->w_p_nuw - 1;
11165
11166 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011167 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011168 return n;
11169}
11170#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011171
Bram Moolenaar113e1072019-01-20 15:30:40 +010011172#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011173/*
11174 * Return the current cursor column. This is the actual position on the
11175 * screen. First column is 0.
11176 */
11177 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011178screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011179{
11180 return screen_cur_col;
11181}
11182
11183/*
11184 * Return the current cursor row. This is the actual position on the screen.
11185 * First row is 0.
11186 */
11187 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011188screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011189{
11190 return screen_cur_row;
11191}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011192#endif