blob: bff4926ff6f2d2fa48364f4b48e64394e064dd75 [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 Moolenaar60cdb302019-05-27 21:54:10 +0200994/*
995 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
996 * window then get the "Pmenu" highlight attribute.
997 */
998 static int
999get_wcr_attr(win_T *wp)
1000{
1001 int wcr_attr = 0;
1002
1003 if (*wp->w_p_wcr != NUL)
1004 wcr_attr = syn_name2attr(wp->w_p_wcr);
1005#ifdef FEAT_TEXT_PROP
1006 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1007 wcr_attr = HL_ATTR(HLF_PNI);
1008#endif
1009 return wcr_attr;
1010}
1011
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001012#ifdef FEAT_TEXT_PROP
1013/*
1014 * Return a string of "len" spaces in IObuff.
1015 */
1016 static char_u *
1017get_spaces(int len)
1018{
1019 vim_memset(IObuff, ' ', (size_t)len);
1020 IObuff[len] = NUL;
1021 return IObuff;
1022}
1023
1024 static void
1025update_popups(void)
1026{
1027 win_T *wp;
1028 int top_off;
1029 int left_off;
1030 int total_width;
1031 int total_height;
1032 int popup_attr;
1033 int row;
1034
1035 // Find the window with the lowest zindex that hasn't been updated yet,
1036 // so that the window with a higher zindex is drawn later, thus goes on
1037 // top.
1038 // TODO: don't redraw every popup every time.
1039 popup_reset_handled();
1040 while ((wp = find_next_popup(TRUE)) != NULL)
1041 {
1042 // Recompute the position if the text changed.
1043 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1044 popup_adjust_position(wp);
1045
1046 // adjust w_winrow and w_wincol for border and padding, since
1047 // win_update() doesn't handle them.
1048 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1049 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1050 wp->w_winrow += top_off;
1051 wp->w_wincol += left_off;
1052
1053 // Draw the popup text.
1054 win_update(wp);
1055
1056 wp->w_winrow -= top_off;
1057 wp->w_wincol -= left_off;
1058
1059 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1060 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1061 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1062 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1063 popup_attr = get_wcr_attr(wp);
1064
1065 if (wp->w_popup_border[0] > 0)
1066 {
1067 // top border
1068 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1069 wp->w_wincol,
1070 wp->w_wincol + total_width,
1071 wp->w_popup_border[3] != 0 ? '+' : '-',
1072 '-', popup_attr);
1073 if (wp->w_popup_border[1] > 0)
1074 screen_puts((char_u *)"+", wp->w_winrow,
1075 wp->w_wincol + total_width - 1, popup_attr);
1076 }
1077
1078 if (wp->w_popup_padding[0] > 0)
1079 {
1080 // top padding
1081 row = wp->w_winrow + wp->w_popup_border[0];
1082 screen_fill(row, row + wp->w_popup_padding[0],
1083 wp->w_wincol + wp->w_popup_border[3],
1084 wp->w_wincol + total_width - wp->w_popup_border[1],
1085 ' ', ' ', popup_attr);
1086 }
1087
1088 for (row = wp->w_winrow + wp->w_popup_border[0];
1089 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1090 ++row)
1091 {
1092 // left border
1093 if (wp->w_popup_border[3] > 0)
1094 screen_puts((char_u *)"|", row, wp->w_wincol, popup_attr);
1095 // left padding
1096 if (wp->w_popup_padding[3] > 0)
1097 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1098 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1099 // right border
1100 if (wp->w_popup_border[1] > 0)
1101 screen_puts((char_u *)"|", row,
1102 wp->w_wincol + total_width - 1, popup_attr);
1103 // right padding
1104 if (wp->w_popup_padding[1] > 0)
1105 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1106 wp->w_wincol + wp->w_popup_border[3]
1107 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1108 }
1109
1110 if (wp->w_popup_padding[2] > 0)
1111 {
1112 // bottom padding
1113 row = wp->w_winrow + wp->w_popup_border[0]
1114 + wp->w_popup_padding[0] + wp->w_height;
1115 screen_fill(row, row + wp->w_popup_padding[2],
1116 wp->w_wincol + wp->w_popup_border[3],
1117 wp->w_wincol + total_width - wp->w_popup_border[1],
1118 ' ', ' ', popup_attr);
1119 }
1120
1121 if (wp->w_popup_border[2] > 0)
1122 {
1123 // bottom border
1124 row = wp->w_winrow + total_height - 1;
1125 screen_fill(row , row + 1,
1126 wp->w_wincol,
1127 wp->w_wincol + total_width,
1128 wp->w_popup_border[3] != 0 ? '+' : '-',
1129 '-', popup_attr);
1130 if (wp->w_popup_border[1] > 0)
1131 screen_puts((char_u *)"+", row,
1132 wp->w_wincol + total_width - 1, popup_attr);
1133 }
1134 }
1135}
1136#endif
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#if defined(FEAT_GUI) || defined(PROTO)
1139/*
1140 * Update a single window, its status line and maybe the command line msg.
1141 * Used for the GUI scrollbar.
1142 */
1143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001144updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001146 /* return if already busy updating */
1147 if (updating_screen)
1148 return;
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 update_prepare();
1151
1152#ifdef FEAT_CLIPBOARD
1153 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001154 if (clip_star.available && clip_isautosel_star())
1155 clip_update_selection(&clip_star);
1156 if (clip_plus.available && clip_isautosel_plus())
1157 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001161
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001162 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001163 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001164 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 if (wp->w_redr_status
1167# ifdef FEAT_CMDL_INFO
1168 || p_ru
1169# endif
1170# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001171 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# endif
1173 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001174 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 update_finish();
1177}
1178#endif
1179
1180/*
1181 * Update a single window.
1182 *
1183 * This may cause the windows below it also to be redrawn (when clearing the
1184 * screen or scrolling lines).
1185 *
1186 * How the window is redrawn depends on wp->w_redr_type. Each type also
1187 * implies the one below it.
1188 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001189 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1191 * INVERTED redraw the changed part of the Visual area
1192 * INVERTED_ALL redraw the whole Visual area
1193 * VALID 1. scroll up/down to adjust for a changed w_topline
1194 * 2. update lines at the top when scrolled down
1195 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001196 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 * b_mod_top and b_mod_bot.
1198 * - if wp->w_redraw_top non-zero, redraw lines between
1199 * wp->w_redraw_top and wp->w_redr_bot.
1200 * - continue redrawing when syntax status is invalid.
1201 * 4. if scrolled up, update lines at the bottom.
1202 * This results in three areas that may need updating:
1203 * top: from first row to top_end (when scrolled down)
1204 * mid: from mid_start to mid_end (update inversion or changed text)
1205 * bot: from bot_start to last row (when scrolled up)
1206 */
1207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001208win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209{
1210 buf_T *buf = wp->w_buffer;
1211 int type;
1212 int top_end = 0; /* Below last row of the top area that needs
1213 updating. 0 when no top area updating. */
1214 int mid_start = 999;/* first row of the mid area that needs
1215 updating. 999 when no mid area updating. */
1216 int mid_end = 0; /* Below last row of the mid area that needs
1217 updating. 0 when no mid area updating. */
1218 int bot_start = 999;/* first row of the bot area that needs
1219 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 int scrolled_down = FALSE; /* TRUE when scrolled down when
1221 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001223 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 int top_to_mod = FALSE; /* redraw above mod_top */
1225#endif
1226
1227 int row; /* current window row to display */
1228 linenr_T lnum; /* current buffer lnum to display */
1229 int idx; /* current index in w_lines[] */
1230 int srow; /* starting row of the current line */
1231
1232 int eof = FALSE; /* if TRUE, we hit the end of the file */
1233 int didline = FALSE; /* if TRUE, we finished the last line */
1234 int i;
1235 long j;
1236 static int recursive = FALSE; /* being called recursively */
1237 int old_botline = wp->w_botline;
1238#ifdef FEAT_FOLDING
1239 long fold_count;
1240#endif
1241#ifdef FEAT_SYN_HL
1242 /* remember what happened to the previous line, to know if
1243 * check_visual_highlight() can be used */
1244#define DID_NONE 1 /* didn't update a line */
1245#define DID_LINE 2 /* updated a normal line */
1246#define DID_FOLD 3 /* updated a folded line */
1247 int did_update = DID_NONE;
1248 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1249#endif
1250 linenr_T mod_top = 0;
1251 linenr_T mod_bot = 0;
1252#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1253 int save_got_int;
1254#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001255#ifdef SYN_TIME_LIMIT
1256 proftime_T syntax_tm;
1257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259 type = wp->w_redr_type;
1260
1261 if (type == NOT_VALID)
1262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 wp->w_lines_valid = 0;
1265 }
1266
1267 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001268 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 {
1270 wp->w_redr_type = 0;
1271 return;
1272 }
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 /* Window is zero-width: Only need to draw the separator. */
1275 if (wp->w_width == 0)
1276 {
1277 /* draw the vertical separator right of this window */
1278 draw_vsep_win(wp, 0);
1279 wp->w_redr_type = 0;
1280 return;
1281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001283#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001284 // If this window contains a terminal, redraw works completely differently.
1285 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001286 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001287 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001288# ifdef FEAT_MENU
1289 /* Draw the window toolbar, if there is one. */
1290 if (winbar_height(wp) > 0)
1291 redraw_win_toolbar(wp);
1292# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001293 wp->w_redr_type = 0;
1294 return;
1295 }
1296#endif
1297
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001299 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
1301
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001302#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001303 /* Force redraw when width of 'number' or 'relativenumber' column
1304 * changes. */
1305 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001306 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001307 {
1308 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001309 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001310 }
1311 else
1312#endif
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1315 {
1316 /*
1317 * When there are both inserted/deleted lines and specific lines to be
1318 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1319 * everything (only happens when redrawing is off for while).
1320 */
1321 type = NOT_VALID;
1322 }
1323 else
1324 {
1325 /*
1326 * Set mod_top to the first line that needs displaying because of
1327 * changes. Set mod_bot to the first line after the changes.
1328 */
1329 mod_top = wp->w_redraw_top;
1330 if (wp->w_redraw_bot != 0)
1331 mod_bot = wp->w_redraw_bot + 1;
1332 else
1333 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (buf->b_mod_set)
1335 {
1336 if (mod_top == 0 || mod_top > buf->b_mod_top)
1337 {
1338 mod_top = buf->b_mod_top;
1339#ifdef FEAT_SYN_HL
1340 /* Need to redraw lines above the change that may be included
1341 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001342 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001344 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (mod_top < 1)
1346 mod_top = 1;
1347 }
1348#endif
1349 }
1350 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1351 mod_bot = buf->b_mod_bot;
1352
1353#ifdef FEAT_SEARCH_EXTRA
1354 /* When 'hlsearch' is on and using a multi-line search pattern, a
1355 * change in one line may make the Search highlighting in a
1356 * previous line invalid. Simple solution: redraw all visible
1357 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001358 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001360 if (search_hl.rm.regprog != NULL
1361 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001363 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001364 {
1365 cur = wp->w_match_head;
1366 while (cur != NULL)
1367 {
1368 if (cur->match.regprog != NULL
1369 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001370 {
1371 top_to_mod = TRUE;
1372 break;
1373 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001374 cur = cur->next;
1375 }
1376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#endif
1378 }
1379#ifdef FEAT_FOLDING
1380 if (mod_top != 0 && hasAnyFolding(wp))
1381 {
1382 linenr_T lnumt, lnumb;
1383
1384 /*
1385 * A change in a line can cause lines above it to become folded or
1386 * unfolded. Find the top most buffer line that may be affected.
1387 * If the line was previously folded and displayed, get the first
1388 * line of that fold. If the line is folded now, get the first
1389 * folded line. Use the minimum of these two.
1390 */
1391
1392 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1393 * the line below it. If there is no valid entry, use w_topline.
1394 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1395 * to this line. If there is no valid entry, use MAXLNUM. */
1396 lnumt = wp->w_topline;
1397 lnumb = MAXLNUM;
1398 for (i = 0; i < wp->w_lines_valid; ++i)
1399 if (wp->w_lines[i].wl_valid)
1400 {
1401 if (wp->w_lines[i].wl_lastlnum < mod_top)
1402 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1403 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1404 {
1405 lnumb = wp->w_lines[i].wl_lnum;
1406 /* When there is a fold column it might need updating
1407 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001408 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 ++lnumb;
1410 }
1411 }
1412
1413 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1414 if (mod_top > lnumt)
1415 mod_top = lnumt;
1416
1417 /* Now do the same for the bottom line (one above mod_bot). */
1418 --mod_bot;
1419 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1420 ++mod_bot;
1421 if (mod_bot < lnumb)
1422 mod_bot = lnumb;
1423 }
1424#endif
1425
1426 /* When a change starts above w_topline and the end is below
1427 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001428 * If the end of the change is above w_topline: do like no change was
1429 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (mod_top != 0 && mod_top < wp->w_topline)
1431 {
1432 if (mod_bot > wp->w_topline)
1433 mod_top = wp->w_topline;
1434#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001435 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 top_end = 1;
1437#endif
1438 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001439
1440 /* When line numbers are displayed need to redraw all lines below
1441 * inserted/deleted lines. */
1442 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1443 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001445 wp->w_redraw_top = 0; // reset for next time
1446 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
1448 /*
1449 * When only displaying the lines at the top, set top_end. Used when
1450 * window has scrolled down for msg_scrolled.
1451 */
1452 if (type == REDRAW_TOP)
1453 {
1454 j = 0;
1455 for (i = 0; i < wp->w_lines_valid; ++i)
1456 {
1457 j += wp->w_lines[i].wl_size;
1458 if (j >= wp->w_upd_rows)
1459 {
1460 top_end = j;
1461 break;
1462 }
1463 }
1464 if (top_end == 0)
1465 /* not found (cannot happen?): redraw everything */
1466 type = NOT_VALID;
1467 else
1468 /* top area defined, the rest is VALID */
1469 type = VALID;
1470 }
1471
Bram Moolenaar367329b2007-08-30 11:53:22 +00001472 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001473 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1474 * non-zero and thus not FALSE) will indicate that screenclear() was not
1475 * called. */
1476 if (screen_cleared)
1477 screen_cleared = MAYBE;
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 /*
1480 * If there are no changes on the screen that require a complete redraw,
1481 * handle three cases:
1482 * 1: we are off the top of the screen by a few lines: scroll down
1483 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1484 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1485 * w_lines[] that needs updating.
1486 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001487 if ((type == VALID || type == SOME_VALID
1488 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#ifdef FEAT_DIFF
1490 && !wp->w_botfill && !wp->w_old_botfill
1491#endif
1492 )
1493 {
1494 if (mod_top != 0 && wp->w_topline == mod_top)
1495 {
1496 /*
1497 * w_topline is the first changed line, the scrolling will be done
1498 * further down.
1499 */
1500 }
1501 else if (wp->w_lines[0].wl_valid
1502 && (wp->w_topline < wp->w_lines[0].wl_lnum
1503#ifdef FEAT_DIFF
1504 || (wp->w_topline == wp->w_lines[0].wl_lnum
1505 && wp->w_topfill > wp->w_old_topfill)
1506#endif
1507 ))
1508 {
1509 /*
1510 * New topline is above old topline: May scroll down.
1511 */
1512#ifdef FEAT_FOLDING
1513 if (hasAnyFolding(wp))
1514 {
1515 linenr_T ln;
1516
1517 /* count the number of lines we are off, counting a sequence
1518 * of folded lines as one */
1519 j = 0;
1520 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1521 {
1522 ++j;
1523 if (j >= wp->w_height - 2)
1524 break;
1525 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1526 }
1527 }
1528 else
1529#endif
1530 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1531 if (j < wp->w_height - 2) /* not too far off */
1532 {
1533 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1534#ifdef FEAT_DIFF
1535 /* insert extra lines for previously invisible filler lines */
1536 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1537 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1538 - wp->w_old_topfill;
1539#endif
1540 if (i < wp->w_height - 2) /* less than a screen off */
1541 {
1542 /*
1543 * Try to insert the correct number of lines.
1544 * If not the last window, delete the lines at the bottom.
1545 * win_ins_lines may fail when the terminal can't do it.
1546 */
1547 if (i > 0)
1548 check_for_delay(FALSE);
1549 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1550 {
1551 if (wp->w_lines_valid != 0)
1552 {
1553 /* Need to update rows that are new, stop at the
1554 * first one that scrolled down. */
1555 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /* Move the entries that were scrolled, disable
1559 * the entries for the lines to be redrawn. */
1560 if ((wp->w_lines_valid += j) > wp->w_height)
1561 wp->w_lines_valid = wp->w_height;
1562 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1563 wp->w_lines[idx] = wp->w_lines[idx - j];
1564 while (idx >= 0)
1565 wp->w_lines[idx--].wl_valid = FALSE;
1566 }
1567 }
1568 else
1569 mid_start = 0; /* redraw all lines */
1570 }
1571 else
1572 mid_start = 0; /* redraw all lines */
1573 }
1574 else
1575 mid_start = 0; /* redraw all lines */
1576 }
1577 else
1578 {
1579 /*
1580 * New topline is at or below old topline: May scroll up.
1581 * When topline didn't change, find first entry in w_lines[] that
1582 * needs updating.
1583 */
1584
1585 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1586 j = -1;
1587 row = 0;
1588 for (i = 0; i < wp->w_lines_valid; i++)
1589 {
1590 if (wp->w_lines[i].wl_valid
1591 && wp->w_lines[i].wl_lnum == wp->w_topline)
1592 {
1593 j = i;
1594 break;
1595 }
1596 row += wp->w_lines[i].wl_size;
1597 }
1598 if (j == -1)
1599 {
1600 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1601 * lines */
1602 mid_start = 0;
1603 }
1604 else
1605 {
1606 /*
1607 * Try to delete the correct number of lines.
1608 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1609 */
1610#ifdef FEAT_DIFF
1611 /* If the topline didn't change, delete old filler lines,
1612 * otherwise delete filler lines of the new topline... */
1613 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1614 row += wp->w_old_topfill;
1615 else
1616 row += diff_check_fill(wp, wp->w_topline);
1617 /* ... but don't delete new filler lines. */
1618 row -= wp->w_topfill;
1619#endif
1620 if (row > 0)
1621 {
1622 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001623 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1624 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 bot_start = wp->w_height - row;
1626 else
1627 mid_start = 0; /* redraw all lines */
1628 }
1629 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1630 {
1631 /*
1632 * Skip the lines (below the deleted lines) that are still
1633 * valid and don't need redrawing. Copy their info
1634 * upwards, to compensate for the deleted lines. Set
1635 * bot_start to the first row that needs redrawing.
1636 */
1637 bot_start = 0;
1638 idx = 0;
1639 for (;;)
1640 {
1641 wp->w_lines[idx] = wp->w_lines[j];
1642 /* stop at line that didn't fit, unless it is still
1643 * valid (no lines deleted) */
1644 if (row > 0 && bot_start + row
1645 + (int)wp->w_lines[j].wl_size > wp->w_height)
1646 {
1647 wp->w_lines_valid = idx + 1;
1648 break;
1649 }
1650 bot_start += wp->w_lines[idx++].wl_size;
1651
1652 /* stop at the last valid entry in w_lines[].wl_size */
1653 if (++j >= wp->w_lines_valid)
1654 {
1655 wp->w_lines_valid = idx;
1656 break;
1657 }
1658 }
1659#ifdef FEAT_DIFF
1660 /* Correct the first entry for filler lines at the top
1661 * when it won't get updated below. */
1662 if (wp->w_p_diff && bot_start > 0)
1663 wp->w_lines[0].wl_size =
1664 plines_win_nofill(wp, wp->w_topline, TRUE)
1665 + wp->w_topfill;
1666#endif
1667 }
1668 }
1669 }
1670
1671 /* When starting redraw in the first line, redraw all lines. When
1672 * there is only one window it's probably faster to clear the screen
1673 * first. */
1674 if (mid_start == 0)
1675 {
1676 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001677 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001678 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001679 /* Clear the screen when it was not done by win_del_lines() or
1680 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1681 * then. */
1682 if (screen_cleared != TRUE)
1683 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001684 /* The screen was cleared, redraw the tab pages line. */
1685 if (redraw_tabline)
1686 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001689
1690 /* When win_del_lines() or win_ins_lines() caused the screen to be
1691 * cleared (only happens for the first window) or when screenclear()
1692 * was called directly above, "must_redraw" will have been set to
1693 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1694 if (screen_cleared == TRUE)
1695 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
1697 else
1698 {
1699 /* Not VALID or INVERTED: redraw all lines. */
1700 mid_start = 0;
1701 mid_end = wp->w_height;
1702 }
1703
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001704 if (type == SOME_VALID)
1705 {
1706 /* SOME_VALID: redraw all lines. */
1707 mid_start = 0;
1708 mid_end = wp->w_height;
1709 type = NOT_VALID;
1710 }
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 /* check if we are updating or removing the inverted part */
1713 if ((VIsual_active && buf == curwin->w_buffer)
1714 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1715 {
1716 linenr_T from, to;
1717
1718 if (VIsual_active)
1719 {
1720 if (VIsual_active
1721 && (VIsual_mode != wp->w_old_visual_mode
1722 || type == INVERTED_ALL))
1723 {
1724 /*
1725 * If the type of Visual selection changed, redraw the whole
1726 * selection. Also when the ownership of the X selection is
1727 * gained or lost.
1728 */
1729 if (curwin->w_cursor.lnum < VIsual.lnum)
1730 {
1731 from = curwin->w_cursor.lnum;
1732 to = VIsual.lnum;
1733 }
1734 else
1735 {
1736 from = VIsual.lnum;
1737 to = curwin->w_cursor.lnum;
1738 }
1739 /* redraw more when the cursor moved as well */
1740 if (wp->w_old_cursor_lnum < from)
1741 from = wp->w_old_cursor_lnum;
1742 if (wp->w_old_cursor_lnum > to)
1743 to = wp->w_old_cursor_lnum;
1744 if (wp->w_old_visual_lnum < from)
1745 from = wp->w_old_visual_lnum;
1746 if (wp->w_old_visual_lnum > to)
1747 to = wp->w_old_visual_lnum;
1748 }
1749 else
1750 {
1751 /*
1752 * Find the line numbers that need to be updated: The lines
1753 * between the old cursor position and the current cursor
1754 * position. Also check if the Visual position changed.
1755 */
1756 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1757 {
1758 from = curwin->w_cursor.lnum;
1759 to = wp->w_old_cursor_lnum;
1760 }
1761 else
1762 {
1763 from = wp->w_old_cursor_lnum;
1764 to = curwin->w_cursor.lnum;
1765 if (from == 0) /* Visual mode just started */
1766 from = to;
1767 }
1768
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001769 if (VIsual.lnum != wp->w_old_visual_lnum
1770 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
1772 if (wp->w_old_visual_lnum < from
1773 && wp->w_old_visual_lnum != 0)
1774 from = wp->w_old_visual_lnum;
1775 if (wp->w_old_visual_lnum > to)
1776 to = wp->w_old_visual_lnum;
1777 if (VIsual.lnum < from)
1778 from = VIsual.lnum;
1779 if (VIsual.lnum > to)
1780 to = VIsual.lnum;
1781 }
1782 }
1783
1784 /*
1785 * If in block mode and changed column or curwin->w_curswant:
1786 * update all lines.
1787 * First compute the actual start and end column.
1788 */
1789 if (VIsual_mode == Ctrl_V)
1790 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001791 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001792#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001793 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaar404406a2014-10-09 13:24:43 +02001795 if (curwin->w_p_lbr)
1796 ve_flags = VE_ALL;
1797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001799#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001800 ve_flags = save_ve_flags;
1801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 ++toc;
1803 if (curwin->w_curswant == MAXCOL)
1804 toc = MAXCOL;
1805
1806 if (fromc != wp->w_old_cursor_fcol
1807 || toc != wp->w_old_cursor_lcol)
1808 {
1809 if (from > VIsual.lnum)
1810 from = VIsual.lnum;
1811 if (to < VIsual.lnum)
1812 to = VIsual.lnum;
1813 }
1814 wp->w_old_cursor_fcol = fromc;
1815 wp->w_old_cursor_lcol = toc;
1816 }
1817 }
1818 else
1819 {
1820 /* Use the line numbers of the old Visual area. */
1821 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1822 {
1823 from = wp->w_old_cursor_lnum;
1824 to = wp->w_old_visual_lnum;
1825 }
1826 else
1827 {
1828 from = wp->w_old_visual_lnum;
1829 to = wp->w_old_cursor_lnum;
1830 }
1831 }
1832
1833 /*
1834 * There is no need to update lines above the top of the window.
1835 */
1836 if (from < wp->w_topline)
1837 from = wp->w_topline;
1838
1839 /*
1840 * If we know the value of w_botline, use it to restrict the update to
1841 * the lines that are visible in the window.
1842 */
1843 if (wp->w_valid & VALID_BOTLINE)
1844 {
1845 if (from >= wp->w_botline)
1846 from = wp->w_botline - 1;
1847 if (to >= wp->w_botline)
1848 to = wp->w_botline - 1;
1849 }
1850
1851 /*
1852 * Find the minimal part to be updated.
1853 * Watch out for scrolling that made entries in w_lines[] invalid.
1854 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1855 * top_end; need to redraw from top_end to the "to" line.
1856 * A middle mouse click with a Visual selection may change the text
1857 * above the Visual area and reset wl_valid, do count these for
1858 * mid_end (in srow).
1859 */
1860 if (mid_start > 0)
1861 {
1862 lnum = wp->w_topline;
1863 idx = 0;
1864 srow = 0;
1865 if (scrolled_down)
1866 mid_start = top_end;
1867 else
1868 mid_start = 0;
1869 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1870 {
1871 if (wp->w_lines[idx].wl_valid)
1872 mid_start += wp->w_lines[idx].wl_size;
1873 else if (!scrolled_down)
1874 srow += wp->w_lines[idx].wl_size;
1875 ++idx;
1876# ifdef FEAT_FOLDING
1877 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1878 lnum = wp->w_lines[idx].wl_lnum;
1879 else
1880# endif
1881 ++lnum;
1882 }
1883 srow += mid_start;
1884 mid_end = wp->w_height;
1885 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1886 {
1887 if (wp->w_lines[idx].wl_valid
1888 && wp->w_lines[idx].wl_lnum >= to + 1)
1889 {
1890 /* Only update until first row of this line */
1891 mid_end = srow;
1892 break;
1893 }
1894 srow += wp->w_lines[idx].wl_size;
1895 }
1896 }
1897 }
1898
1899 if (VIsual_active && buf == curwin->w_buffer)
1900 {
1901 wp->w_old_visual_mode = VIsual_mode;
1902 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1903 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001904 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 wp->w_old_curswant = curwin->w_curswant;
1906 }
1907 else
1908 {
1909 wp->w_old_visual_mode = 0;
1910 wp->w_old_cursor_lnum = 0;
1911 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001912 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1916 /* reset got_int, otherwise regexp won't work */
1917 save_got_int = got_int;
1918 got_int = 0;
1919#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001920#ifdef SYN_TIME_LIMIT
1921 /* Set the time limit to 'redrawtime'. */
1922 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001923 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925#ifdef FEAT_FOLDING
1926 win_foldinfo.fi_level = 0;
1927#endif
1928
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001929#ifdef FEAT_MENU
1930 /*
1931 * Draw the window toolbar, if there is one.
1932 * TODO: only when needed.
1933 */
1934 if (winbar_height(wp) > 0)
1935 redraw_win_toolbar(wp);
1936#endif
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 /*
1939 * Update all the window rows.
1940 */
1941 idx = 0; /* first entry in w_lines[].wl_size */
1942 row = 0;
1943 srow = 0;
1944 lnum = wp->w_topline; /* first line shown in window */
1945 for (;;)
1946 {
1947 /* stop updating when reached the end of the window (check for _past_
1948 * the end of the window is at the end of the loop) */
1949 if (row == wp->w_height)
1950 {
1951 didline = TRUE;
1952 break;
1953 }
1954
1955 /* stop updating when hit the end of the file */
1956 if (lnum > buf->b_ml.ml_line_count)
1957 {
1958 eof = TRUE;
1959 break;
1960 }
1961
1962 /* Remember the starting row of the line that is going to be dealt
1963 * with. It is used further down when the line doesn't fit. */
1964 srow = row;
1965
1966 /*
1967 * Update a line when it is in an area that needs updating, when it
1968 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001969 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 * win_del_lines(), check if the current line includes it.
1971 * When syntax folding is being used, the saved syntax states will
1972 * already have been updated, we can't see where the syntax state is
1973 * the same again, just update until the end of the window.
1974 */
1975 if (row < top_end
1976 || (row >= mid_start && row < mid_end)
1977#ifdef FEAT_SEARCH_EXTRA
1978 || top_to_mod
1979#endif
1980 || idx >= wp->w_lines_valid
1981 || (row + wp->w_lines[idx].wl_size > bot_start)
1982 || (mod_top != 0
1983 && (lnum == mod_top
1984 || (lnum >= mod_top
1985 && (lnum < mod_bot
1986#ifdef FEAT_SYN_HL
1987 || did_update == DID_FOLD
1988 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001989 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 && (
1991# ifdef FEAT_FOLDING
1992 (foldmethodIsSyntax(wp)
1993 && hasAnyFolding(wp)) ||
1994# endif
1995 syntax_check_changed(lnum)))
1996#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001997#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001998 /* match in fixed position might need redraw
1999 * if lines were inserted or deleted */
2000 || (wp->w_match_head != NULL
2001 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 )))))
2004 {
2005#ifdef FEAT_SEARCH_EXTRA
2006 if (lnum == mod_top)
2007 top_to_mod = FALSE;
2008#endif
2009
2010 /*
2011 * When at start of changed lines: May scroll following lines
2012 * up or down to minimize redrawing.
2013 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002014 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 */
2016 if (lnum == mod_top
2017 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002018 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 int old_rows = 0;
2021 int new_rows = 0;
2022 int xtra_rows;
2023 linenr_T l;
2024
2025 /* Count the old number of window rows, using w_lines[], which
2026 * should still contain the sizes for the lines as they are
2027 * currently displayed. */
2028 for (i = idx; i < wp->w_lines_valid; ++i)
2029 {
2030 /* Only valid lines have a meaningful wl_lnum. Invalid
2031 * lines are part of the changed area. */
2032 if (wp->w_lines[i].wl_valid
2033 && wp->w_lines[i].wl_lnum == mod_bot)
2034 break;
2035 old_rows += wp->w_lines[i].wl_size;
2036#ifdef FEAT_FOLDING
2037 if (wp->w_lines[i].wl_valid
2038 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2039 {
2040 /* Must have found the last valid entry above mod_bot.
2041 * Add following invalid entries. */
2042 ++i;
2043 while (i < wp->w_lines_valid
2044 && !wp->w_lines[i].wl_valid)
2045 old_rows += wp->w_lines[i++].wl_size;
2046 break;
2047 }
2048#endif
2049 }
2050
2051 if (i >= wp->w_lines_valid)
2052 {
2053 /* We can't find a valid line below the changed lines,
2054 * need to redraw until the end of the window.
2055 * Inserting/deleting lines has no use. */
2056 bot_start = 0;
2057 }
2058 else
2059 {
2060 /* Able to count old number of rows: Count new window
2061 * rows, and may insert/delete lines */
2062 j = idx;
2063 for (l = lnum; l < mod_bot; ++l)
2064 {
2065#ifdef FEAT_FOLDING
2066 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2067 ++new_rows;
2068 else
2069#endif
2070#ifdef FEAT_DIFF
2071 if (l == wp->w_topline)
2072 new_rows += plines_win_nofill(wp, l, TRUE)
2073 + wp->w_topfill;
2074 else
2075#endif
2076 new_rows += plines_win(wp, l, TRUE);
2077 ++j;
2078 if (new_rows > wp->w_height - row - 2)
2079 {
2080 /* it's getting too much, must redraw the rest */
2081 new_rows = 9999;
2082 break;
2083 }
2084 }
2085 xtra_rows = new_rows - old_rows;
2086 if (xtra_rows < 0)
2087 {
2088 /* May scroll text up. If there is not enough
2089 * remaining text or scrolling fails, must redraw the
2090 * rest. If scrolling works, must redraw the text
2091 * below the scrolled text. */
2092 if (row - xtra_rows >= wp->w_height - 2)
2093 mod_bot = MAXLNUM;
2094 else
2095 {
2096 check_for_delay(FALSE);
2097 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002098 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 mod_bot = MAXLNUM;
2100 else
2101 bot_start = wp->w_height + xtra_rows;
2102 }
2103 }
2104 else if (xtra_rows > 0)
2105 {
2106 /* May scroll text down. If there is not enough
2107 * remaining text of scrolling fails, must redraw the
2108 * rest. */
2109 if (row + xtra_rows >= wp->w_height - 2)
2110 mod_bot = MAXLNUM;
2111 else
2112 {
2113 check_for_delay(FALSE);
2114 if (win_ins_lines(wp, row + old_rows,
2115 xtra_rows, FALSE, FALSE) == FAIL)
2116 mod_bot = MAXLNUM;
2117 else if (top_end > row + old_rows)
2118 /* Scrolled the part at the top that requires
2119 * updating down. */
2120 top_end += xtra_rows;
2121 }
2122 }
2123
2124 /* When not updating the rest, may need to move w_lines[]
2125 * entries. */
2126 if (mod_bot != MAXLNUM && i != j)
2127 {
2128 if (j < i)
2129 {
2130 int x = row + new_rows;
2131
2132 /* move entries in w_lines[] upwards */
2133 for (;;)
2134 {
2135 /* stop at last valid entry in w_lines[] */
2136 if (i >= wp->w_lines_valid)
2137 {
2138 wp->w_lines_valid = j;
2139 break;
2140 }
2141 wp->w_lines[j] = wp->w_lines[i];
2142 /* stop at a line that won't fit */
2143 if (x + (int)wp->w_lines[j].wl_size
2144 > wp->w_height)
2145 {
2146 wp->w_lines_valid = j + 1;
2147 break;
2148 }
2149 x += wp->w_lines[j++].wl_size;
2150 ++i;
2151 }
2152 if (bot_start > x)
2153 bot_start = x;
2154 }
2155 else /* j > i */
2156 {
2157 /* move entries in w_lines[] downwards */
2158 j -= i;
2159 wp->w_lines_valid += j;
2160 if (wp->w_lines_valid > wp->w_height)
2161 wp->w_lines_valid = wp->w_height;
2162 for (i = wp->w_lines_valid; i - j >= idx; --i)
2163 wp->w_lines[i] = wp->w_lines[i - j];
2164
2165 /* The w_lines[] entries for inserted lines are
2166 * now invalid, but wl_size may be used above.
2167 * Reset to zero. */
2168 while (i >= idx)
2169 {
2170 wp->w_lines[i].wl_size = 0;
2171 wp->w_lines[i--].wl_valid = FALSE;
2172 }
2173 }
2174 }
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177
2178#ifdef FEAT_FOLDING
2179 /*
2180 * When lines are folded, display one line for all of them.
2181 * Otherwise, display normally (can be several display lines when
2182 * 'wrap' is on).
2183 */
2184 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2185 if (fold_count != 0)
2186 {
2187 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2188 ++row;
2189 --fold_count;
2190 wp->w_lines[idx].wl_folded = TRUE;
2191 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2192# ifdef FEAT_SYN_HL
2193 did_update = DID_FOLD;
2194# endif
2195 }
2196 else
2197#endif
2198 if (idx < wp->w_lines_valid
2199 && wp->w_lines[idx].wl_valid
2200 && wp->w_lines[idx].wl_lnum == lnum
2201 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002202 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002203#ifdef FEAT_TEXT_PROP
2204 && !bt_popup(wp->w_buffer)
2205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 && srow + wp->w_lines[idx].wl_size > wp->w_height
2207#ifdef FEAT_DIFF
2208 && diff_check_fill(wp, lnum) == 0
2209#endif
2210 )
2211 {
2212 /* This line is not going to fit. Don't draw anything here,
2213 * will draw "@ " lines below. */
2214 row = wp->w_height + 1;
2215 }
2216 else
2217 {
2218#ifdef FEAT_SEARCH_EXTRA
2219 prepare_search_hl(wp, lnum);
2220#endif
2221#ifdef FEAT_SYN_HL
2222 /* Let the syntax stuff know we skipped a few lines. */
2223 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002224 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 syntax_end_parsing(syntax_last_parsed + 1);
2226#endif
2227
2228 /*
2229 * Display one line.
2230 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002231 row = win_line(wp, lnum, srow, wp->w_height,
2232 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234#ifdef FEAT_FOLDING
2235 wp->w_lines[idx].wl_folded = FALSE;
2236 wp->w_lines[idx].wl_lastlnum = lnum;
2237#endif
2238#ifdef FEAT_SYN_HL
2239 did_update = DID_LINE;
2240 syntax_last_parsed = lnum;
2241#endif
2242 }
2243
2244 wp->w_lines[idx].wl_lnum = lnum;
2245 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002246
2247 /* Past end of the window or end of the screen. Note that after
2248 * resizing wp->w_height may be end up too big. That's a problem
2249 * elsewhere, but prevent a crash here. */
2250 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002253 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2255 ++idx;
2256 break;
2257 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002258 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 wp->w_lines[idx].wl_size = row - srow;
2260 ++idx;
2261#ifdef FEAT_FOLDING
2262 lnum += fold_count + 1;
2263#else
2264 ++lnum;
2265#endif
2266 }
2267 else
2268 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002269 if (wp->w_p_rnu)
2270 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002271#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002272 // 'relativenumber' set: The text doesn't need to be drawn, but
2273 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002274 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2275 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002276 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002277 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002278#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002279 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002280 }
2281
2282 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 row += wp->w_lines[idx++].wl_size;
2284 if (row > wp->w_height) /* past end of screen */
2285 break;
2286#ifdef FEAT_FOLDING
2287 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2288#else
2289 ++lnum;
2290#endif
2291#ifdef FEAT_SYN_HL
2292 did_update = DID_NONE;
2293#endif
2294 }
2295
2296 if (lnum > buf->b_ml.ml_line_count)
2297 {
2298 eof = TRUE;
2299 break;
2300 }
2301 }
2302 /*
2303 * End of loop over all window lines.
2304 */
2305
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002306#ifdef FEAT_VTP
2307 /* Rewrite the character at the end of the screen line. */
2308 if (use_vtp())
2309 {
2310 int i;
2311
2312 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002313 if (enc_utf8)
2314 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2315 LineOffset[i] + screen_Columns) > 1)
2316 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2317 else
2318 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2319 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002320 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2321 }
2322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
2324 if (idx > wp->w_lines_valid)
2325 wp->w_lines_valid = idx;
2326
2327#ifdef FEAT_SYN_HL
2328 /*
2329 * Let the syntax stuff know we stop parsing here.
2330 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002331 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 syntax_end_parsing(syntax_last_parsed + 1);
2333#endif
2334
2335 /*
2336 * If we didn't hit the end of the file, and we didn't finish the last
2337 * line we were working on, then the line didn't fit.
2338 */
2339 wp->w_empty_rows = 0;
2340#ifdef FEAT_DIFF
2341 wp->w_filler_rows = 0;
2342#endif
2343 if (!eof && !didline)
2344 {
2345 if (lnum == wp->w_topline)
2346 {
2347 /*
2348 * Single line that does not fit!
2349 * Don't overwrite it, it can be edited.
2350 */
2351 wp->w_botline = lnum + 1;
2352 }
2353#ifdef FEAT_DIFF
2354 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2355 {
2356 /* Window ends in filler lines. */
2357 wp->w_botline = lnum;
2358 wp->w_filler_rows = wp->w_height - srow;
2359 }
2360#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002361#ifdef FEAT_TEXT_PROP
2362 else if (bt_popup(wp->w_buffer))
2363 {
2364 // popup line that doesn't fit is left as-is
2365 wp->w_botline = lnum;
2366 }
2367#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002368 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2369 {
2370 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2371
2372 /*
2373 * Last line isn't finished: Display "@@@" in the last screen line.
2374 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002375 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002376 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002377 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002378 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002379 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002380 set_empty_rows(wp, srow);
2381 wp->w_botline = lnum;
2382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2384 {
2385 /*
2386 * Last line isn't finished: Display "@@@" at the end.
2387 */
2388 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2389 W_WINROW(wp) + wp->w_height,
2390 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002391 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 set_empty_rows(wp, srow);
2393 wp->w_botline = lnum;
2394 }
2395 else
2396 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002397 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 wp->w_botline = lnum;
2399 }
2400 }
2401 else
2402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (eof) /* we hit the end of the file */
2405 {
2406 wp->w_botline = buf->b_ml.ml_line_count + 1;
2407#ifdef FEAT_DIFF
2408 j = diff_check_fill(wp, wp->w_botline);
2409 if (j > 0 && !wp->w_botfill)
2410 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002411 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (char2cells(fill_diff) > 1)
2413 i = '-';
2414 else
2415 i = fill_diff;
2416 if (row + j > wp->w_height)
2417 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002418 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 row += j;
2420 }
2421#endif
2422 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002423 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 wp->w_botline = lnum;
2425
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002426 // Make sure the rest of the screen is blank
2427 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002428 win_draw_end(wp,
2429#ifdef FEAT_TEXT_PROP
2430 bt_popup(wp->w_buffer) ? ' ' :
2431#endif
2432 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002435#ifdef SYN_TIME_LIMIT
2436 syn_set_timeout(NULL);
2437#endif
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 /* Reset the type of redrawing required, the window has been updated. */
2440 wp->w_redr_type = 0;
2441#ifdef FEAT_DIFF
2442 wp->w_old_topfill = wp->w_topfill;
2443 wp->w_old_botfill = wp->w_botfill;
2444#endif
2445
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002446 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 /*
2449 * There is a trick with w_botline. If we invalidate it on each
2450 * change that might modify it, this will cause a lot of expensive
2451 * calls to plines() in update_topline() each time. Therefore the
2452 * value of w_botline is often approximated, and this value is used to
2453 * compute the value of w_topline. If the value of w_botline was
2454 * wrong, check that the value of w_topline is correct (cursor is on
2455 * the visible part of the text). If it's not, we need to redraw
2456 * again. Mostly this just means scrolling up a few lines, so it
2457 * doesn't look too bad. Only do this for the current window (where
2458 * changes are relevant).
2459 */
2460 wp->w_valid |= VALID_BOTLINE;
2461 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2462 {
2463 recursive = TRUE;
2464 curwin->w_valid &= ~VALID_TOPLINE;
2465 update_topline(); /* may invalidate w_botline again */
2466 if (must_redraw != 0)
2467 {
2468 /* Don't update for changes in buffer again. */
2469 i = curbuf->b_mod_set;
2470 curbuf->b_mod_set = FALSE;
2471 win_update(curwin);
2472 must_redraw = 0;
2473 curbuf->b_mod_set = i;
2474 }
2475 recursive = FALSE;
2476 }
2477 }
2478
2479#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2480 /* restore got_int, unless CTRL-C was hit while redrawing */
2481 if (!got_int)
2482 got_int = save_got_int;
2483#endif
2484}
2485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002487 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2488 * Return the new offset.
2489 */
2490 static int
2491screen_fill_end(
2492 win_T *wp,
2493 int c1,
2494 int c2,
2495 int off,
2496 int width,
2497 int row,
2498 int endrow,
2499 int attr)
2500{
2501 int nn = off + width;
2502
2503 if (nn > wp->w_width)
2504 nn = wp->w_width;
2505#ifdef FEAT_RIGHTLEFT
2506 if (wp->w_p_rl)
2507 {
2508 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2509 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2510 c1, c2, attr);
2511 }
2512 else
2513#endif
2514 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2515 wp->w_wincol + off, (int)wp->w_wincol + nn,
2516 c1, c2, attr);
2517 return nn;
2518}
2519
2520/*
2521 * Clear lines near the end the window and mark the unused lines with "c1".
2522 * use "c2" as the filler character.
2523 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
2525 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002526win_draw_end(
2527 win_T *wp,
2528 int c1,
2529 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002530 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002531 int row,
2532 int endrow,
2533 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002536 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002537 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002538
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002539 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002540
2541 if (draw_margin)
2542 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002543#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002544 int fdc = compute_foldcolumn(wp, 0);
2545
2546 if (fdc > 0)
2547 // draw the fold column
2548 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002549 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002550#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002551#ifdef FEAT_SIGNS
2552 if (signcolumn_on(wp))
2553 // draw the sign column
2554 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002555 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002556#endif
2557 if ((wp->w_p_nu || wp->w_p_rnu)
2558 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2559 // draw the number column
2560 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002561 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564#ifdef FEAT_RIGHTLEFT
2565 if (wp->w_p_rl)
2566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002568 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002569 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002571 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002572 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 else
2575#endif
2576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002578 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002579 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 set_empty_rows(wp, row);
2583}
2584
Bram Moolenaar1a384422010-07-14 19:53:30 +02002585#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002586/*
2587 * Advance **color_cols and return TRUE when there are columns to draw.
2588 */
2589 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002590advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002591{
2592 while (**color_cols >= 0 && vcol > **color_cols)
2593 ++*color_cols;
2594 return (**color_cols >= 0);
2595}
2596#endif
2597
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002598#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2599/*
2600 * Copy "text" to ScreenLines using "attr".
2601 * Returns the next screen column.
2602 */
2603 static int
2604text_to_screenline(win_T *wp, char_u *text, int col)
2605{
2606 int off = (int)(current_ScreenLine - ScreenLines);
2607
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002608 if (has_mbyte)
2609 {
2610 int cells;
2611 int u8c, u8cc[MAX_MCO];
2612 int i;
2613 int idx;
2614 int c_len;
2615 char_u *p;
2616# ifdef FEAT_ARABIC
2617 int prev_c = 0; /* previous Arabic character */
2618 int prev_c1 = 0; /* first composing char for prev_c */
2619# endif
2620
2621# ifdef FEAT_RIGHTLEFT
2622 if (wp->w_p_rl)
2623 idx = off;
2624 else
2625# endif
2626 idx = off + col;
2627
2628 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2629 for (p = text; *p != NUL; )
2630 {
2631 cells = (*mb_ptr2cells)(p);
2632 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002633 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002634# ifdef FEAT_RIGHTLEFT
2635 - (wp->w_p_rl ? col : 0)
2636# endif
2637 )
2638 break;
2639 ScreenLines[idx] = *p;
2640 if (enc_utf8)
2641 {
2642 u8c = utfc_ptr2char(p, u8cc);
2643 if (*p < 0x80 && u8cc[0] == 0)
2644 {
2645 ScreenLinesUC[idx] = 0;
2646#ifdef FEAT_ARABIC
2647 prev_c = u8c;
2648#endif
2649 }
2650 else
2651 {
2652#ifdef FEAT_ARABIC
2653 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2654 {
2655 /* Do Arabic shaping. */
2656 int pc, pc1, nc;
2657 int pcc[MAX_MCO];
2658 int firstbyte = *p;
2659
2660 /* The idea of what is the previous and next
2661 * character depends on 'rightleft'. */
2662 if (wp->w_p_rl)
2663 {
2664 pc = prev_c;
2665 pc1 = prev_c1;
2666 nc = utf_ptr2char(p + c_len);
2667 prev_c1 = u8cc[0];
2668 }
2669 else
2670 {
2671 pc = utfc_ptr2char(p + c_len, pcc);
2672 nc = prev_c;
2673 pc1 = pcc[0];
2674 }
2675 prev_c = u8c;
2676
2677 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2678 pc, pc1, nc);
2679 ScreenLines[idx] = firstbyte;
2680 }
2681 else
2682 prev_c = u8c;
2683#endif
2684 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002685 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002686 for (i = 0; i < Screen_mco; ++i)
2687 {
2688 ScreenLinesC[i][idx] = u8cc[i];
2689 if (u8cc[i] == 0)
2690 break;
2691 }
2692 }
2693 if (cells > 1)
2694 ScreenLines[idx + 1] = 0;
2695 }
2696 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2697 /* double-byte single width character */
2698 ScreenLines2[idx] = p[1];
2699 else if (cells > 1)
2700 /* double-width character */
2701 ScreenLines[idx + 1] = p[1];
2702 col += cells;
2703 idx += cells;
2704 p += c_len;
2705 }
2706 }
2707 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002708 {
2709 int len = (int)STRLEN(text);
2710
Bram Moolenaar02631462017-09-22 15:20:32 +02002711 if (len > wp->w_width - col)
2712 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002713 if (len > 0)
2714 {
2715#ifdef FEAT_RIGHTLEFT
2716 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002717 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002718 else
2719#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002720 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002721 col += len;
2722 }
2723 }
2724 return col;
2725}
2726#endif
2727
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#ifdef FEAT_FOLDING
2729/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002730 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2731 * space is available for window "wp", minus "col".
2732 */
2733 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002734compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002735{
2736 int fdc = wp->w_p_fdc;
2737 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002738 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002739
2740 if (fdc > wwidth - (col + wmw))
2741 fdc = wwidth - (col + wmw);
2742 return fdc;
2743}
2744
2745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 * Display one folded line.
2747 */
2748 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002749fold_line(
2750 win_T *wp,
2751 long fold_count,
2752 foldinfo_T *foldinfo,
2753 linenr_T lnum,
2754 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002756 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 pos_T *top, *bot;
2758 linenr_T lnume = lnum + fold_count - 1;
2759 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002760 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 int col;
2763 int txtcol;
2764 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002765 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
2767 /* Build the fold line:
2768 * 1. Add the cmdwin_type for the command-line window
2769 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002770 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 * 4. Compose the text
2772 * 5. Add the text
2773 * 6. set highlighting for the Visual area an other text
2774 */
2775 col = 0;
2776
2777 /*
2778 * 1. Add the cmdwin_type for the command-line window
2779 * Ignores 'rightleft', this window is never right-left.
2780 */
2781#ifdef FEAT_CMDWIN
2782 if (cmdwin_type != 0 && wp == curwin)
2783 {
2784 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002785 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (enc_utf8)
2787 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 ++col;
2789 }
2790#endif
2791
2792 /*
2793 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002794 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002796 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if (fdc > 0)
2798 {
2799 fill_foldcolumn(buf, wp, TRUE, lnum);
2800#ifdef FEAT_RIGHTLEFT
2801 if (wp->w_p_rl)
2802 {
2803 int i;
2804
Bram Moolenaar02631462017-09-22 15:20:32 +02002805 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002806 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 /* reverse the fold column */
2808 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002809 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
2811 else
2812#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002813 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 col += fdc;
2815 }
2816
2817#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002818# define RL_MEMSET(p, v, l) \
2819 do { \
2820 if (wp->w_p_rl) \
2821 for (ri = 0; ri < l; ++ri) \
2822 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2823 else \
2824 for (ri = 0; ri < l; ++ri) \
2825 ScreenAttrs[off + (p) + ri] = v; \
2826 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002828# define RL_MEMSET(p, v, l) \
2829 do { \
2830 for (ri = 0; ri < l; ++ri) \
2831 ScreenAttrs[off + (p) + ri] = v; \
2832 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833#endif
2834
Bram Moolenaar64486672010-05-16 15:46:46 +02002835 /* Set all attributes of the 'number' or 'relativenumber' column and the
2836 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002837 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
2839#ifdef FEAT_SIGNS
2840 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002841 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002843 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (len > 0)
2845 {
2846 if (len > 2)
2847 len = 2;
2848# ifdef FEAT_RIGHTLEFT
2849 if (wp->w_p_rl)
2850 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002851 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002852 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 else
2854# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002855 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 col += len;
2857 }
2858 }
2859#endif
2860
2861 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002862 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002864 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002866 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (len > 0)
2868 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002869 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002870 long num;
2871 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002872
2873 if (len > w + 1)
2874 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002875
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002876 if (wp->w_p_nu && !wp->w_p_rnu)
2877 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002878 num = (long)lnum;
2879 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002880 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002881 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002882 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002883 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002884 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002885 /* 'number' + 'relativenumber': cursor line shows absolute
2886 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002887 num = lnum;
2888 fmt = "%-*ld ";
2889 }
2890 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002891
Bram Moolenaar700e7342013-01-30 12:31:36 +01002892 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893#ifdef FEAT_RIGHTLEFT
2894 if (wp->w_p_rl)
2895 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002897 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 else
2899#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002900 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 col += len;
2902 }
2903 }
2904
2905 /*
2906 * 4. Compose the folded-line string with 'foldtext', if set.
2907 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002908 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 txtcol = col; /* remember where text starts */
2911
2912 /*
2913 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2914 * Right-left text is put in columns 0 - number-col, normal text is put
2915 * in columns number-col - window-width.
2916 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002917 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919 /* Fill the rest of the line with the fold filler */
2920#ifdef FEAT_RIGHTLEFT
2921 if (wp->w_p_rl)
2922 col -= txtcol;
2923#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002924 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#ifdef FEAT_RIGHTLEFT
2926 - (wp->w_p_rl ? txtcol : 0)
2927#endif
2928 )
2929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 if (enc_utf8)
2931 {
2932 if (fill_fold >= 0x80)
2933 {
2934 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002935 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002936 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002941 ScreenLines[off + col] = fill_fold;
2942 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002943 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002945 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002946 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948
2949 if (text != buf)
2950 vim_free(text);
2951
2952 /*
2953 * 6. set highlighting for the Visual area an other text.
2954 * If all folded lines are in the Visual area, highlight the line.
2955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2957 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002958 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 {
2960 /* Visual is after curwin->w_cursor */
2961 top = &curwin->w_cursor;
2962 bot = &VIsual;
2963 }
2964 else
2965 {
2966 /* Visual is before curwin->w_cursor */
2967 top = &VIsual;
2968 bot = &curwin->w_cursor;
2969 }
2970 if (lnum >= top->lnum
2971 && lnume <= bot->lnum
2972 && (VIsual_mode != 'v'
2973 || ((lnum > top->lnum
2974 || (lnum == top->lnum
2975 && top->col == 0))
2976 && (lnume < bot->lnum
2977 || (lnume == bot->lnum
2978 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002979 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
2981 if (VIsual_mode == Ctrl_V)
2982 {
2983 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002984 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002986 if (wp->w_old_cursor_lcol != MAXCOL
2987 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002988 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 len = wp->w_old_cursor_lcol;
2990 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002991 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002992 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002993 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 }
2996 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002999 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003004#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003005 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3006 if (wp->w_p_cc_cols)
3007 {
3008 int i = 0;
3009 int j = wp->w_p_cc_cols[i];
3010 int old_txtcol = txtcol;
3011
3012 while (j > -1)
3013 {
3014 txtcol += j;
3015 if (wp->w_p_wrap)
3016 txtcol -= wp->w_skipcol;
3017 else
3018 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003019 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003020 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003021 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003022 txtcol = old_txtcol;
3023 j = wp->w_p_cc_cols[++i];
3024 }
3025 }
3026
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003027 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003028 if (wp->w_p_cuc)
3029 {
3030 txtcol += wp->w_virtcol;
3031 if (wp->w_p_wrap)
3032 txtcol -= wp->w_skipcol;
3033 else
3034 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003035 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003036 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003037 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003038 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
Bram Moolenaar02631462017-09-22 15:20:32 +02003041 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003042 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 /*
3045 * Update w_cline_height and w_cline_folded if the cursor line was
3046 * updated (saves a call to plines() later).
3047 */
3048 if (wp == curwin
3049 && lnum <= curwin->w_cursor.lnum
3050 && lnume >= curwin->w_cursor.lnum)
3051 {
3052 curwin->w_cline_row = row;
3053 curwin->w_cline_height = 1;
3054 curwin->w_cline_folded = TRUE;
3055 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3056 }
3057}
3058
3059/*
3060 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3061 */
3062 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003063copy_text_attr(
3064 int off,
3065 char_u *buf,
3066 int len,
3067 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003069 int i;
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (enc_utf8)
3073 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003074 for (i = 0; i < len; ++i)
3075 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
3078/*
3079 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003080 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 */
3082 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003083fill_foldcolumn(
3084 char_u *p,
3085 win_T *wp,
3086 int closed, /* TRUE of FALSE */
3087 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 int i = 0;
3090 int level;
3091 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003092 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003093 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003096 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 level = win_foldinfo.fi_level;
3099 if (level > 0)
3100 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003101 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003102 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 /* If the column is too narrow, we start at the lowest level that
3105 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003106 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (first_level < 1)
3108 first_level = 1;
3109
Bram Moolenaar1c934292015-01-27 16:39:29 +01003110 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 if (win_foldinfo.fi_lnum == lnum
3113 && first_level + i >= win_foldinfo.fi_low_level)
3114 p[i] = '-';
3115 else if (first_level == 1)
3116 p[i] = '|';
3117 else if (first_level + i <= 9)
3118 p[i] = '0' + first_level + i;
3119 else
3120 p[i] = '>';
3121 if (first_level + i == level)
3122 break;
3123 }
3124 }
3125 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003126 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127}
3128#endif /* FEAT_FOLDING */
3129
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003130#ifdef FEAT_TEXT_PROP
3131static textprop_T *current_text_props = NULL;
3132static buf_T *current_buf = NULL;
3133
3134 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003135text_prop_compare(const void *s1, const void *s2)
3136{
3137 int idx1, idx2;
3138 proptype_T *pt1, *pt2;
3139 colnr_T col1, col2;
3140
3141 idx1 = *(int *)s1;
3142 idx2 = *(int *)s2;
3143 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3144 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3145 if (pt1 == pt2)
3146 return 0;
3147 if (pt1 == NULL)
3148 return -1;
3149 if (pt2 == NULL)
3150 return 1;
3151 if (pt1->pt_priority != pt2->pt_priority)
3152 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3153 col1 = current_text_props[idx1].tp_col;
3154 col2 = current_text_props[idx2].tp_col;
3155 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3156}
3157#endif
3158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159/*
3160 * Display line "lnum" of window 'wp' on the screen.
3161 * Start at row "startrow", stop when "endrow" is reached.
3162 * wp->w_virtcol needs to be valid.
3163 *
3164 * Return the number of last row the line occupies.
3165 */
3166 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003167win_line(
3168 win_T *wp,
3169 linenr_T lnum,
3170 int startrow,
3171 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003172 int nochange UNUSED, // not updating for changed text
3173 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003175 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3177 int c = 0; /* init for GCC */
3178 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003179#ifdef FEAT_LINEBREAK
3180 long vcol_sbr = -1; /* virtual column after showbreak */
3181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 long vcol_prev = -1; /* "vcol" of previous character */
3183 char_u *line; /* current line */
3184 char_u *ptr; /* current position in "line" */
3185 int row; /* row in the window, excl w_winrow */
3186 int screen_row; /* row on the screen, incl w_winrow */
3187
3188 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3189 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003190 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003191 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003193 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 int extra_attr = 0; /* attributes when n_extra != 0 */
3195 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3196 displaying lcs_eol at end-of-line */
3197 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3198 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3199
3200 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3201 int saved_n_extra = 0;
3202 char_u *saved_p_extra = NULL;
3203 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003204 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 int saved_char_attr = 0;
3206
3207 int n_attr = 0; /* chars with special attr */
3208 int saved_attr2 = 0; /* char_attr saved for n_attr */
3209 int n_attr3 = 0; /* chars with overruling special attr */
3210 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3211
3212 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3213
3214 int fromcol, tocol; /* start/end of inverting */
3215 int fromcol_prev = -2; /* start of inverting after cursor */
3216 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003218 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 pos_T pos;
3220 long v;
3221
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003222 int char_attr = 0; // attributes for next character
3223 int attr_pri = FALSE; // char_attr has priority
3224 int area_highlighting = FALSE; // Visual or incsearch highlighting
3225 // in this line
3226 int vi_attr = 0; // attributes for Visual and incsearch
3227 // highlighting
3228 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003229 int win_attr = 0; // background for whole window, except
3230 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003231 int area_attr = 0; // attributes desired by highlighting
3232 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003234 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 int syntax_attr = 0; /* attributes desired by syntax */
3236 int has_syntax = FALSE; /* this buffer has syntax highl. */
3237 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003238 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003239 int draw_color_col = FALSE; /* highlight colorcolumn */
3240 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003241#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003242#ifdef FEAT_TEXT_PROP
3243 int text_prop_count;
3244 int text_prop_next = 0; // next text property to use
3245 textprop_T *text_props = NULL;
3246 int *text_prop_idxs = NULL;
3247 int text_props_active = 0;
3248 proptype_T *text_prop_type = NULL;
3249 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003250 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003251#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003252#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003253 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003254# define SPWORDLEN 150
3255 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003256 int nextlinecol = 0; /* column where nextline[] starts */
3257 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003258 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003259 int spell_attr = 0; /* attributes desired by spelling */
3260 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003261 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3262 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003263 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003264 static int cap_col = -1; /* column to check for Cap word */
3265 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003266 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003268 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 int multi_attr = 0; /* attributes desired by multibyte */
3270 int mb_l = 1; /* multi-byte byte length */
3271 int mb_c = 0; /* decoded multi-byte character */
3272 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003273 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274#ifdef FEAT_DIFF
3275 int filler_lines; /* nr of filler lines to be drawn */
3276 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003277 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 int change_start = MAXCOL; /* first col of changed area */
3279 int change_end = -1; /* last col of changed area */
3280#endif
3281 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3282#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003283 int need_showbreak = FALSE; /* overlong line, skipping first x
3284 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003286#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003287 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003289 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#endif
3291#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003292 matchitem_T *cur; /* points to the match list */
3293 match_T *shl; /* points to search_hl or a match */
3294 int shl_flag; /* flag to indicate whether search_hl
3295 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003296 int pos_inprogress; /* marks that position match search is
3297 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003298 int prevcol_hl_flag; /* flag to indicate whether prevcol
3299 equals startcol of search_hl or one
3300 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#endif
3302#ifdef FEAT_ARABIC
3303 int prev_c = 0; /* previous Arabic character */
3304 int prev_c1 = 0; /* first composing char for prev_c */
3305#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003306#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003307 int did_line_attr = 0;
3308#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003309#ifdef FEAT_TERMINAL
3310 int get_term_attr = FALSE;
3311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
3313 /* draw_state: items that are drawn in sequence: */
3314#define WL_START 0 /* nothing done yet */
3315#ifdef FEAT_CMDWIN
3316# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3317#else
3318# define WL_CMDLINE WL_START
3319#endif
3320#ifdef FEAT_FOLDING
3321# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3322#else
3323# define WL_FOLD WL_CMDLINE
3324#endif
3325#ifdef FEAT_SIGNS
3326# define WL_SIGN WL_FOLD + 1 /* column for signs */
3327#else
3328# define WL_SIGN WL_FOLD /* column for signs */
3329#endif
3330#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003331#ifdef FEAT_LINEBREAK
3332# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003334# define WL_BRI WL_NR
3335#endif
3336#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3337# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3338#else
3339# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#endif
3341#define WL_LINE WL_SBR + 1 /* text in the line */
3342 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003343#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 int feedback_col = 0;
3345 int feedback_old_attr = -1;
3346#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003347 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaar860cae12010-06-05 23:22:07 +02003349#ifdef FEAT_CONCEAL
3350 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003351 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003352 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003353 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003354 int is_concealing = FALSE;
3355 int boguscols = 0; /* nonexistent columns added to force
3356 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003357 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003358 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003359 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003360 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003361# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003362# define FIX_FOR_BOGUSCOLS \
3363 { \
3364 n_extra += vcol_off; \
3365 vcol -= vcol_off; \
3366 vcol_off = 0; \
3367 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003368 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003369 boguscols = 0; \
3370 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003371#else
3372# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 if (startrow > endrow) /* past the end already! */
3376 return startrow;
3377
3378 row = startrow;
3379 screen_row = row + W_WINROW(wp);
3380
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003381 if (!number_only)
3382 {
3383 /*
3384 * To speed up the loop below, set extra_check when there is linebreak,
3385 * trailing white space and/or syntax processing to be done.
3386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389#endif
3390#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003391 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003392# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003394# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003395 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003397 /* Prepare for syntax highlighting in this line. When there is an
3398 * error, stop syntax highlighting. */
3399 save_did_emsg = did_emsg;
3400 did_emsg = FALSE;
3401 syntax_start(wp, lnum);
3402 if (did_emsg)
3403 wp->w_s->b_syn_error = TRUE;
3404 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003405 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003406 did_emsg = save_did_emsg;
3407#ifdef SYN_TIME_LIMIT
3408 if (!wp->w_s->b_syn_slow)
3409#endif
3410 {
3411 has_syntax = TRUE;
3412 extra_check = TRUE;
3413 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003416
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003417 /* Check for columns to display for 'colorcolumn'. */
3418 color_cols = wp->w_p_cc_cols;
3419 if (color_cols != NULL)
3420 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003421#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003422
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003423#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 if (term_show_buffer(wp->w_buffer))
3425 {
3426 extra_check = TRUE;
3427 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003428 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003430#endif
3431
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003432#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 if (wp->w_p_spell
3434 && *wp->w_s->b_p_spl != NUL
3435 && wp->w_s->b_langp.ga_len > 0
3436 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003437 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 /* Prepare for spell checking. */
3439 has_spell = TRUE;
3440 extra_check = TRUE;
3441
Bram Moolenaar7701f302018-10-02 21:20:32 +02003442 /* Get the start of the next line, so that words that wrap to the
3443 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 * Trick: skip a few chars for C/shell/Vim comments */
3445 nextline[SPWORDLEN] = NUL;
3446 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3447 {
3448 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3449 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3450 }
3451
Bram Moolenaar7701f302018-10-02 21:20:32 +02003452 /* When a word wrapped from the previous line the start of the
3453 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003454 if (lnum == checked_lnum)
3455 cur_checked_col = checked_col;
3456 checked_lnum = 0;
3457
3458 /* When there was a sentence end in the previous line may require a
3459 * word starting with capital in this line. In line 1 always check
3460 * the first word. */
3461 if (lnum != capcol_lnum)
3462 cap_col = -1;
3463 if (lnum == 1)
3464 cap_col = 0;
3465 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#endif
3468
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003469 /*
3470 * handle visual active in this window
3471 */
3472 fromcol = -10;
3473 tocol = MAXCOL;
3474 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003476 /* Visual is after curwin->w_cursor */
3477 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003479 top = &curwin->w_cursor;
3480 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003484 top = &VIsual;
3485 bot = &curwin->w_cursor;
3486 }
3487 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3488 if (VIsual_mode == Ctrl_V) /* block mode */
3489 {
3490 if (lnum_in_visual_area)
3491 {
3492 fromcol = wp->w_old_cursor_fcol;
3493 tocol = wp->w_old_cursor_lcol;
3494 }
3495 }
3496 else /* non-block mode */
3497 {
3498 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003500 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003502 if (VIsual_mode == 'V') /* linewise */
3503 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 else
3505 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003506 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3507 if (gchar_pos(top) == NUL)
3508 tocol = fromcol + 1;
3509 }
3510 }
3511 if (VIsual_mode != 'V' && lnum == bot->lnum)
3512 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003513 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003514 {
3515 fromcol = -10;
3516 tocol = MAXCOL;
3517 }
3518 else if (bot->col == MAXCOL)
3519 tocol = MAXCOL;
3520 else
3521 {
3522 pos = *bot;
3523 if (*p_sel == 'e')
3524 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3525 else
3526 {
3527 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3528 ++tocol;
3529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531 }
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003534 /* Check if the character under the cursor should not be inverted */
3535 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003536#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003537 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003538#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 )
3540 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003542 /* if inverting in this line set area_highlighting */
3543 if (fromcol >= 0)
3544 {
3545 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003546 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003549 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003550 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003551 && clip_isautosel_plus()))
3552 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003557 /*
3558 * handle 'incsearch' and ":s///c" highlighting
3559 */
3560 else if (highlight_match
3561 && wp == curwin
3562 && lnum >= curwin->w_cursor.lnum
3563 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003565 if (lnum == curwin->w_cursor.lnum)
3566 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003567 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003568 else
3569 fromcol = 0;
3570 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3571 {
3572 pos.lnum = lnum;
3573 pos.col = search_match_endcol;
3574 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3575 }
3576 else
3577 tocol = MAXCOL;
3578 /* do at least one character; happens when past end of line */
3579 if (fromcol == tocol)
3580 tocol = fromcol + 1;
3581 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003582 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585
3586#ifdef FEAT_DIFF
3587 filler_lines = diff_check(wp, lnum);
3588 if (filler_lines < 0)
3589 {
3590 if (filler_lines == -1)
3591 {
3592 if (diff_find_change(wp, lnum, &change_start, &change_end))
3593 diff_hlf = HLF_ADD; /* added line */
3594 else if (change_start == 0)
3595 diff_hlf = HLF_TXD; /* changed text */
3596 else
3597 diff_hlf = HLF_CHD; /* changed line */
3598 }
3599 else
3600 diff_hlf = HLF_ADD; /* added line */
3601 filler_lines = 0;
3602 area_highlighting = TRUE;
3603 }
3604 if (lnum == wp->w_topline)
3605 filler_lines = wp->w_topfill;
3606 filler_todo = filler_lines;
3607#endif
3608
3609#ifdef LINE_ATTR
3610# ifdef FEAT_SIGNS
3611 /* If this line has a sign with line highlighting set line_attr. */
3612 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3613 if (v != 0)
3614 line_attr = sign_get_attr((int)v, TRUE);
3615# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003616# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003618 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003619 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620# endif
3621 if (line_attr != 0)
3622 area_highlighting = TRUE;
3623#endif
3624
3625 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3626 ptr = line;
3627
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003628#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003629 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003630 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003631 /* For checking first word with a capital skip white space. */
3632 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003633 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003634
Bram Moolenaar30abd282005-06-22 22:35:10 +00003635 /* To be able to spell-check over line boundaries copy the end of the
3636 * current line into nextline[]. Above the start of the next line was
3637 * copied to nextline[SPWORDLEN]. */
3638 if (nextline[SPWORDLEN] == NUL)
3639 {
3640 /* No next line or it is empty. */
3641 nextlinecol = MAXCOL;
3642 nextline_idx = 0;
3643 }
3644 else
3645 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003646 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003647 if (v < SPWORDLEN)
3648 {
3649 /* Short line, use it completely and append the start of the
3650 * next line. */
3651 nextlinecol = 0;
3652 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003653 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003654 nextline_idx = v + 1;
3655 }
3656 else
3657 {
3658 /* Long line, use only the last SPWORDLEN bytes. */
3659 nextlinecol = v - SPWORDLEN;
3660 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3661 nextline_idx = SPWORDLEN + 1;
3662 }
3663 }
3664 }
3665#endif
3666
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003667 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003669 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003670 extra_check = TRUE;
3671 /* find start of trailing whitespace */
3672 if (lcs_trail)
3673 {
3674 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003675 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003676 --trailcol;
3677 trailcol += (colnr_T) (ptr - line);
3678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003681 wcr_attr = get_wcr_attr(wp);
3682 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003683 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003684 win_attr = wcr_attr;
3685 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003686 }
3687#ifdef FEAT_TEXT_PROP
3688 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003689 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003690#endif
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 /*
3693 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3694 * first character to be displayed.
3695 */
3696 if (wp->w_p_wrap)
3697 v = wp->w_skipcol;
3698 else
3699 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003700 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 while (vcol < v && *ptr != NUL)
3705 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003706 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003709 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003712 /* When:
3713 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003714 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003715 * - 'virtualedit' is set, or
3716 * - the visual mode is active,
3717 * the end of the line may be before the start of the displayed part.
3718 */
3719 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003720#ifdef FEAT_SYN_HL
3721 wp->w_p_cuc || draw_color_col ||
3722#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003723 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003724 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 /* Handle a character that's not completely on the screen: Put ptr at
3728 * that character but skip the first few screen characters. */
3729 if (vcol > v)
3730 {
3731 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003733 /* If the character fits on the screen, don't need to skip it.
3734 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003735 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003736 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738
3739 /*
3740 * Adjust for when the inverted text is before the screen,
3741 * and when the start of the inverted text is before the screen.
3742 */
3743 if (tocol <= vcol)
3744 fromcol = 0;
3745 else if (fromcol >= 0 && fromcol < vcol)
3746 fromcol = vcol;
3747
3748#ifdef FEAT_LINEBREAK
3749 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3750 if (wp->w_p_wrap)
3751 need_showbreak = TRUE;
3752#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003753#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003754 /* When spell checking a word we need to figure out the start of the
3755 * word and if it's badly spelled or not. */
3756 if (has_spell)
3757 {
3758 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003759 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003760 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003761
3762 pos = wp->w_cursor;
3763 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003764 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003765 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003766
3767 /* spell_move_to() may call ml_get() and make "line" invalid */
3768 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3769 ptr = line + linecol;
3770
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003771 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003772 {
3773 /* no bad word found at line start, don't check until end of a
3774 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003775 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003776 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003777 }
3778 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003779 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003780 /* bad word found, use attributes until end of word */
3781 word_end = wp->w_cursor.col + len + 1;
3782
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003783 /* Turn index into actual attributes. */
3784 if (spell_hlf != HLF_COUNT)
3785 spell_attr = highlight_attr[spell_hlf];
3786 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003787 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003788
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003789# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003790 /* Need to restart syntax highlighting for this line. */
3791 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003792 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003793# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003794 }
3795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797
3798 /*
3799 * Correct highlighting for cursor that can't be disabled.
3800 * Avoids having to check this for each character.
3801 */
3802 if (fromcol >= 0)
3803 {
3804 if (noinvcur)
3805 {
3806 if ((colnr_T)fromcol == wp->w_virtcol)
3807 {
3808 /* highlighting starts at cursor, let it start just after the
3809 * cursor */
3810 fromcol_prev = fromcol;
3811 fromcol = -1;
3812 }
3813 else if ((colnr_T)fromcol < wp->w_virtcol)
3814 /* restart highlighting after the cursor */
3815 fromcol_prev = wp->w_virtcol;
3816 }
3817 if (fromcol >= tocol)
3818 fromcol = -1;
3819 }
3820
3821#ifdef FEAT_SEARCH_EXTRA
3822 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003823 * Handle highlighting the last used search pattern and matches.
3824 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003825 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003827 cur = wp->w_match_head;
3828 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003829 while ((cur != NULL || shl_flag == FALSE) && !number_only
3830 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003832 if (shl_flag == FALSE)
3833 {
3834 shl = &search_hl;
3835 shl_flag = TRUE;
3836 }
3837 else
3838 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003839 shl->startcol = MAXCOL;
3840 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003842 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003843 v = (long)(ptr - line);
3844 if (cur != NULL)
3845 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003846 next_search_hl(wp, shl, lnum, (colnr_T)v,
3847 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003848
3849 /* Need to get the line again, a multi-line regexp may have made it
3850 * invalid. */
3851 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3852 ptr = line + v;
3853
3854 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003856 if (shl->lnum == lnum)
3857 shl->startcol = shl->rm.startpos[0].col;
3858 else
3859 shl->startcol = 0;
3860 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3861 - shl->rm.startpos[0].lnum)
3862 shl->endcol = shl->rm.endpos[0].col;
3863 else
3864 shl->endcol = MAXCOL;
3865 /* Highlight one character for an empty match. */
3866 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003868 if (has_mbyte && line[shl->endcol] != NUL)
3869 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3870 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003871 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003873 if ((long)shl->startcol < v) /* match at leftcol */
3874 {
3875 shl->attr_cur = shl->attr;
3876 search_attr = shl->attr;
3877 }
3878 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003880 if (shl != &search_hl && cur != NULL)
3881 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883#endif
3884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003885#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003886 // Cursor line highlighting for 'cursorline' in the current window.
3887 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003888 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003889 // Do not show the cursor line when Visual mode is active, because it's
3890 // not clear what is selected then. Do update w_last_cursorline.
3891 if (!(wp == curwin && VIsual_active))
3892 {
3893 line_attr = HL_ATTR(HLF_CUL);
3894 area_highlighting = TRUE;
3895 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003896 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003897 }
3898#endif
3899
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003900#ifdef FEAT_TEXT_PROP
3901 {
3902 char_u *prop_start;
3903
3904 text_prop_count = get_text_props(wp->w_buffer, lnum,
3905 &prop_start, FALSE);
3906 if (text_prop_count > 0)
3907 {
3908 // Make a copy of the properties, so that they are properly
3909 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003910 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003911 if (text_props != NULL)
3912 mch_memmove(text_props, prop_start,
3913 text_prop_count * sizeof(textprop_T));
3914
3915 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003916 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003917 area_highlighting = TRUE;
3918 extra_check = TRUE;
3919 }
3920 }
3921#endif
3922
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003923 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926#ifdef FEAT_RIGHTLEFT
3927 if (wp->w_p_rl)
3928 {
3929 /* Rightleft window: process the text in the normal direction, but put
3930 * it in current_ScreenLine[] from right to left. Start at the
3931 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003932 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003934 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936#endif
3937
3938 /*
3939 * Repeat for the whole displayed line.
3940 */
3941 for (;;)
3942 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003943#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003944 int has_match_conc = 0; // match wants to conceal
3945 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 /* Skip this quickly when working on the text. */
3948 if (draw_state != WL_LINE)
3949 {
3950#ifdef FEAT_CMDWIN
3951 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3952 {
3953 draw_state = WL_CMDLINE;
3954 if (cmdwin_type != 0 && wp == curwin)
3955 {
3956 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003958 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003959 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003960 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 }
3962 }
3963#endif
3964
3965#ifdef FEAT_FOLDING
3966 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3967 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003968 int fdc = compute_foldcolumn(wp, 0);
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003971 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003973 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003974 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003975 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003976 p_extra_free = alloc(12 + 1);
3977
3978 if (p_extra_free != NULL)
3979 {
3980 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3981 n_extra = fdc;
3982 p_extra_free[n_extra] = NUL;
3983 p_extra = p_extra_free;
3984 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003985 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003986 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 }
3990#endif
3991
3992#ifdef FEAT_SIGNS
3993 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3994 {
3995 draw_state = WL_SIGN;
3996 /* Show the sign column when there are any signs in this
3997 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003998 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004000 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004002 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003# endif
4004
4005 /* Draw two cells with the sign value or blank. */
4006 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004007 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004008 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 n_extra = 2;
4010
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004011 if (row == startrow
4012#ifdef FEAT_DIFF
4013 + filler_lines && filler_todo <= 0
4014#endif
4015 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4018 SIGN_TEXT);
4019# ifdef FEAT_SIGN_ICONS
4020 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4021 SIGN_ICON);
4022 if (gui.in_use && icon_sign != 0)
4023 {
4024 /* Use the image in this position. */
4025 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004026 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027# ifdef FEAT_NETBEANS_INTG
4028 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004031 c_final = NUL;
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033# endif
4034 char_attr = icon_sign;
4035 }
4036 else
4037# endif
4038 if (text_sign != 0)
4039 {
4040 p_extra = sign_get_text(text_sign);
4041 if (p_extra != NULL)
4042 {
4043 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004044 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004045 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047 char_attr = sign_get_attr(text_sign, FALSE);
4048 }
4049 }
4050 }
4051 }
4052#endif
4053
4054 if (draw_state == WL_NR - 1 && n_extra == 0)
4055 {
4056 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004057 /* Display the absolute or relative line number. After the
4058 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4059 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 && (row == startrow
4061#ifdef FEAT_DIFF
4062 + filler_lines
4063#endif
4064 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4065 {
4066 /* Draw the line number (empty space after wrapping). */
4067 if (row == startrow
4068#ifdef FEAT_DIFF
4069 + filler_lines
4070#endif
4071 )
4072 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004073 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004074 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004075
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004076 if (wp->w_p_nu && !wp->w_p_rnu)
4077 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004078 num = (long)lnum;
4079 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004080 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004081 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004082 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004083 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004084 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004085 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004086 num = lnum;
4087 fmt = "%-*ld ";
4088 }
4089 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004090
Bram Moolenaar700e7342013-01-30 12:31:36 +01004091 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004092 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (wp->w_skipcol > 0)
4094 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4095 *p_extra = '-';
4096#ifdef FEAT_RIGHTLEFT
4097 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004098 {
4099 char_u *p1, *p2;
4100 int t;
4101
4102 // like rl_mirror(), but keep the space at the end
4103 p2 = skiptowhite(extra) - 1;
4104 for (p1 = extra; p1 < p2; ++p1, --p2)
4105 {
4106 t = *p1;
4107 *p1 = *p2;
4108 *p2 = t;
4109 }
4110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#endif
4112 p_extra = extra;
4113 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004114 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 }
4116 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004119 c_final = NUL;
4120 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004121 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004122 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004123#ifdef FEAT_SYN_HL
4124 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004125 * the current line differently.
4126 * TODO: Can we use CursorLine instead of CursorLineNr
4127 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004128 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004129 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004130 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 }
4134
Bram Moolenaar597a4222014-06-25 14:39:50 +02004135#ifdef FEAT_LINEBREAK
4136 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4137 && n_extra == 0 && *p_sbr != NUL)
4138 /* draw indent after showbreak value */
4139 draw_state = WL_BRI;
4140 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4141 /* After the showbreak, draw the breakindent */
4142 draw_state = WL_BRI - 1;
4143
4144 /* draw 'breakindent': indent wrapped text accordingly */
4145 if (draw_state == WL_BRI - 1 && n_extra == 0)
4146 {
4147 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004148 /* if need_showbreak is set, breakindent also applies */
4149 if (wp->w_p_bri && n_extra == 0
4150 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004151# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004152 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004153# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004154 )
4155 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004156 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004157# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004158 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004159 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004160 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004161# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004162 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4163 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004164 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004165# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004166 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004167# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004168 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004169 c_extra = ' ';
4170 n_extra = get_breakindent_win(wp,
4171 ml_get_buf(wp->w_buffer, lnum, FALSE));
4172 /* Correct end of highlighted area for 'breakindent',
4173 * required when 'linebreak' is also set. */
4174 if (tocol == vcol)
4175 tocol += n_extra;
4176 }
4177 }
4178#endif
4179
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4181 if (draw_state == WL_SBR - 1 && n_extra == 0)
4182 {
4183 draw_state = WL_SBR;
4184# ifdef FEAT_DIFF
4185 if (filler_todo > 0)
4186 {
4187 /* Draw "deleted" diff line(s). */
4188 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004189 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004191 c_final = NUL;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004196 c_final = NUL;
4197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198# ifdef FEAT_RIGHTLEFT
4199 if (wp->w_p_rl)
4200 n_extra = col + 1;
4201 else
4202# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004203 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004204 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206# endif
4207# ifdef FEAT_LINEBREAK
4208 if (*p_sbr != NUL && need_showbreak)
4209 {
4210 /* Draw 'showbreak' at the start of each broken line. */
4211 p_extra = p_sbr;
4212 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004213 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004215 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004217 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 /* Correct end of highlighted area for 'showbreak',
4219 * required when 'linebreak' is also set. */
4220 if (tocol == vcol)
4221 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004222#ifdef FEAT_SYN_HL
4223 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004224 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004225 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004226 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229# endif
4230 }
4231#endif
4232
4233 if (draw_state == WL_LINE - 1 && n_extra == 0)
4234 {
4235 draw_state = WL_LINE;
4236 if (saved_n_extra)
4237 {
4238 /* Continue item from end of wrapped line. */
4239 n_extra = saved_n_extra;
4240 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004241 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 p_extra = saved_p_extra;
4243 char_attr = saved_char_attr;
4244 }
4245 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004246 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
4248 }
4249
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004250 // When still displaying '$' of change command, stop at cursor.
4251 // When only displaying the (relative) line number and that's done,
4252 // stop here.
4253 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004254 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255#ifdef FEAT_DIFF
4256 && filler_todo <= 0
4257#endif
4258 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004259 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004261 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004262 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004263 /* Pretend we have finished updating the window. Except when
4264 * 'cursorcolumn' is set. */
4265#ifdef FEAT_SYN_HL
4266 if (wp->w_p_cuc)
4267 row = wp->w_cline_row + wp->w_cline_height;
4268 else
4269#endif
4270 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272 }
4273
Bram Moolenaar637532b2019-01-03 21:44:40 +01004274 if (draw_state == WL_LINE && (area_highlighting
4275#ifdef FEAT_SPELL
4276 || has_spell
4277#endif
4278 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
4280 /* handle Visual or match highlighting in this line */
4281 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4283 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004285 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004287 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 else if (area_attr != 0
4289 && (vcol == tocol
4290 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293#ifdef FEAT_SEARCH_EXTRA
4294 if (!n_extra)
4295 {
4296 /*
4297 * Check for start/end of search pattern match.
4298 * After end, check for start/end of next match.
4299 * When another match, have to check for start again.
4300 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004301 * Do this for 'search_hl' and the match list (ordered by
4302 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004304 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004305 cur = wp->w_match_head;
4306 shl_flag = FALSE;
4307 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004309 if (shl_flag == FALSE
4310 && ((cur != NULL
4311 && cur->priority > SEARCH_HL_PRIORITY)
4312 || cur == NULL))
4313 {
4314 shl = &search_hl;
4315 shl_flag = TRUE;
4316 }
4317 else
4318 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004319 if (cur != NULL)
4320 cur->pos.cur = 0;
4321 pos_inprogress = TRUE;
4322 while (shl->rm.regprog != NULL
4323 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004325 if (shl->startcol != MAXCOL
4326 && v >= (long)shl->startcol
4327 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004329 int tmp_col = v + MB_PTR2LEN(ptr);
4330
4331 if (shl->endcol < tmp_col)
4332 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004334#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004335 // Match with the "Conceal" group results in hiding
4336 // the match.
4337 if (cur != NULL
4338 && shl != &search_hl
4339 && syn_name2id((char_u *)"Conceal")
4340 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004341 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004342 has_match_conc =
4343 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004344 match_conc = cur->conceal_char;
4345 }
4346 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004347 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004350 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
4352 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004353 next_search_hl(wp, shl, lnum, (colnr_T)v,
4354 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004355 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004356 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
4358 /* Need to get the line again, a multi-line regexp
4359 * may have made it invalid. */
4360 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4361 ptr = line + v;
4362
4363 if (shl->lnum == lnum)
4364 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004365 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004367 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004369 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004371 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
4373 /* highlight empty match, try again after
4374 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004376 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004377 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004379 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381
4382 /* Loop to check if the match starts at the
4383 * current position */
4384 continue;
4385 }
4386 }
4387 break;
4388 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004389 if (shl != &search_hl && cur != NULL)
4390 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004392
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004393 /* Use attributes from match with highest priority among
4394 * 'search_hl' and the match list. */
4395 search_attr = search_hl.attr_cur;
4396 cur = wp->w_match_head;
4397 shl_flag = FALSE;
4398 while (cur != NULL || shl_flag == FALSE)
4399 {
4400 if (shl_flag == FALSE
4401 && ((cur != NULL
4402 && cur->priority > SEARCH_HL_PRIORITY)
4403 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004404 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004405 shl = &search_hl;
4406 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004407 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004408 else
4409 shl = &cur->hl;
4410 if (shl->attr_cur != 0)
4411 search_attr = shl->attr_cur;
4412 if (shl != &search_hl && cur != NULL)
4413 cur = cur->next;
4414 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004415 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004416 if (*ptr == NUL && (did_line_attr >= 1
4417 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004418 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420#endif
4421
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004423 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004425 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4426 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004428 if (diff_hlf == HLF_TXD && ptr - line > change_end
4429 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004431 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004432 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004433 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004436
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004437#ifdef FEAT_TEXT_PROP
4438 if (text_props != NULL)
4439 {
4440 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004441 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004442
4443 // Check if any active property ends.
4444 for (pi = 0; pi < text_props_active; ++pi)
4445 {
4446 int tpi = text_prop_idxs[pi];
4447
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004448 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004449 + text_props[tpi].tp_len)
4450 {
4451 if (pi + 1 < text_props_active)
4452 mch_memmove(text_prop_idxs + pi,
4453 text_prop_idxs + pi + 1,
4454 sizeof(int)
4455 * (text_props_active - (pi + 1)));
4456 --text_props_active;
4457 --pi;
4458 }
4459 }
4460
4461 // Add any text property that starts in this column.
4462 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004463 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004464 text_prop_idxs[text_props_active++] = text_prop_next++;
4465
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004466 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004467 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004468 if (text_props_active > 0)
4469 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004470 // Sort the properties on priority and/or starting last.
4471 // Then combine the attributes, highest priority last.
4472 current_text_props = text_props;
4473 current_buf = wp->w_buffer;
4474 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4475 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004476
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004477 for (pi = 0; pi < text_props_active; ++pi)
4478 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004479 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004480 proptype_T *pt = text_prop_type_by_id(
4481 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004482
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004483 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004484 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004485 int pt_attr = syn_id2attr(pt->pt_hl_id);
4486
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004487 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004488 text_prop_attr =
4489 hl_combine_attr(text_prop_attr, pt_attr);
4490 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004491 }
4492 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004493 }
4494 }
4495#endif
4496
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004497 /* Decide which of the highlight attributes to use. */
4498 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004499#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004500 if (area_attr != 0)
4501 char_attr = hl_combine_attr(line_attr, area_attr);
4502 else if (search_attr != 0)
4503 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004504# ifdef FEAT_TEXT_PROP
4505 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004506 {
4507 char_attr = hl_combine_attr(
4508 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4509 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004510# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004511 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004512 || vcol < fromcol || vcol_prev < fromcol_prev
4513 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004514 // Use line_attr when not in the Visual or 'incsearch' area
4515 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004516 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004517#else
4518 if (area_attr != 0)
4519 char_attr = area_attr;
4520 else if (search_attr != 0)
4521 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004522#endif
4523 else
4524 {
4525 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004526#ifdef FEAT_TEXT_PROP
4527 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004528 {
4529 if (text_prop_combine)
4530 char_attr = hl_combine_attr(
4531 syntax_attr, text_prop_attr);
4532 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004533 char_attr = hl_combine_attr(
4534 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004535 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004536 else
4537#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004538#ifdef FEAT_SYN_HL
4539 if (has_syntax)
4540 char_attr = syntax_attr;
4541 else
4542#endif
4543 char_attr = 0;
4544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004546 if (char_attr == 0)
4547 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 /*
4550 * Get the next character to put on the screen.
4551 */
4552 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004553 * The "p_extra" points to the extra stuff that is inserted to
4554 * represent special characters (non-printable stuff) and other
4555 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004556 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004557 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4558 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4560 */
4561 if (n_extra > 0)
4562 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004563 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004565 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004567 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
4569 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004570 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004571 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
4573 else
4574 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576 else
4577 {
4578 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (has_mbyte)
4580 {
4581 mb_c = c;
4582 if (enc_utf8)
4583 {
4584 /* If the UTF-8 character is more than one byte:
4585 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004586 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 mb_utf8 = FALSE;
4588 if (mb_l > n_extra)
4589 mb_l = 1;
4590 else if (mb_l > 1)
4591 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004592 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004594 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597 else
4598 {
4599 /* if this is a DBCS character, put it in "mb_c" */
4600 mb_l = MB_BYTE2LEN(c);
4601 if (mb_l >= n_extra)
4602 mb_l = 1;
4603 else if (mb_l > 1)
4604 mb_c = (c << 8) + p_extra[1];
4605 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004606 if (mb_l == 0) /* at the NUL at end-of-line */
4607 mb_l = 1;
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 /* If a double-width char doesn't fit display a '>' in the
4610 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004611 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612# ifdef FEAT_RIGHTLEFT
4613 wp->w_p_rl ? (col <= 0) :
4614# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004615 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 && (*mb_char2cells)(mb_c) == 2)
4617 {
4618 c = '>';
4619 mb_c = c;
4620 mb_l = 1;
4621 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004622 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 /* put the pointer back to output the double-width
4624 * character at the start of the next line. */
4625 ++n_extra;
4626 --p_extra;
4627 }
4628 else
4629 {
4630 n_extra -= mb_l - 1;
4631 p_extra += mb_l - 1;
4632 }
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 ++p_extra;
4635 }
4636 --n_extra;
4637 }
4638 else
4639 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004640#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004641 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004642#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004643
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004644 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004645 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 /*
4647 * Get a character from the line itself.
4648 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004649 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004650#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004651 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (has_mbyte)
4654 {
4655 mb_c = c;
4656 if (enc_utf8)
4657 {
4658 /* If the UTF-8 character is more than one byte: Decode it
4659 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004660 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 mb_utf8 = FALSE;
4662 if (mb_l > 1)
4663 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004664 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /* Overlong encoded ASCII or ASCII with composing char
4666 * is displayed normally, except a NUL. */
4667 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004668 {
4669 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004670#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004671 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004672#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004675
4676 /* At start of the line we can have a composing char.
4677 * Draw it as a space with a composing char. */
4678 if (utf_iscomposing(mb_c))
4679 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004680 int i;
4681
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004682 for (i = Screen_mco - 1; i > 0; --i)
4683 u8cc[i] = u8cc[i - 1];
4684 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004685 mb_c = ' ';
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
4688
4689 if ((mb_l == 1 && c >= 0x80)
4690 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004691 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 /*
4694 * Illegal UTF-8 byte: display as <xx>.
4695 * Non-BMP character : display as ? or fullwidth ?.
4696 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004697 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004698# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004699 if (wp->w_p_rl) /* reverse */
4700 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 p_extra = extra;
4703 c = *p_extra;
4704 mb_c = mb_ptr2char_adv(&p_extra);
4705 mb_utf8 = (c >= 0x80);
4706 n_extra = (int)STRLEN(p_extra);
4707 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004708 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 if (area_attr == 0 && search_attr == 0)
4710 {
4711 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004712 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 saved_attr2 = char_attr; /* save current attr */
4714 }
4715 }
4716 else if (mb_l == 0) /* at the NUL at end-of-line */
4717 mb_l = 1;
4718#ifdef FEAT_ARABIC
4719 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4720 {
4721 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004722 int pc, pc1, nc;
4723 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 /* The idea of what is the previous and next
4726 * character depends on 'rightleft'. */
4727 if (wp->w_p_rl)
4728 {
4729 pc = prev_c;
4730 pc1 = prev_c1;
4731 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004732 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734 else
4735 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004736 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004738 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740 prev_c = mb_c;
4741
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004742 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744 else
4745 prev_c = mb_c;
4746#endif
4747 }
4748 else /* enc_dbcs */
4749 {
4750 mb_l = MB_BYTE2LEN(c);
4751 if (mb_l == 0) /* at the NUL at end-of-line */
4752 mb_l = 1;
4753 else if (mb_l > 1)
4754 {
4755 /* We assume a second byte below 32 is illegal.
4756 * Hopefully this is OK for all double-byte encodings!
4757 */
4758 if (ptr[1] >= 32)
4759 mb_c = (c << 8) + ptr[1];
4760 else
4761 {
4762 if (ptr[1] == NUL)
4763 {
4764 /* head byte at end of line */
4765 mb_l = 1;
4766 transchar_nonprint(extra, c);
4767 }
4768 else
4769 {
4770 /* illegal tail byte */
4771 mb_l = 2;
4772 STRCPY(extra, "XX");
4773 }
4774 p_extra = extra;
4775 n_extra = (int)STRLEN(extra) - 1;
4776 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004777 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 c = *p_extra++;
4779 if (area_attr == 0 && search_attr == 0)
4780 {
4781 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004782 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 saved_attr2 = char_attr; /* save current attr */
4784 }
4785 mb_c = c;
4786 }
4787 }
4788 }
4789 /* If a double-width char doesn't fit display a '>' in the
4790 * last column; the character is displayed at the start of the
4791 * next line. */
4792 if ((
4793# ifdef FEAT_RIGHTLEFT
4794 wp->w_p_rl ? (col <= 0) :
4795# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004796 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 && (*mb_char2cells)(mb_c) == 2)
4798 {
4799 c = '>';
4800 mb_c = c;
4801 mb_utf8 = FALSE;
4802 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004803 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004804 // Put pointer back so that the character will be
4805 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004807#ifdef FEAT_CONCEAL
4808 did_decrement_ptr = TRUE;
4809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 else if (*ptr != NUL)
4812 ptr += mb_l - 1;
4813
4814 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004815 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004816 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004817 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004820 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004821 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 c = ' ';
4823 if (area_attr == 0 && search_attr == 0)
4824 {
4825 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004826 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 saved_attr2 = char_attr; /* save current attr */
4828 }
4829 mb_c = c;
4830 mb_utf8 = FALSE;
4831 mb_l = 1;
4832 }
4833
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 ++ptr;
4836
4837 if (extra_check)
4838 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004839#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004840 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004841#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004842
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004843#ifdef FEAT_TERMINAL
4844 if (get_term_attr)
4845 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004846 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004847
4848 if (!attr_pri)
4849 char_attr = syntax_attr;
4850 else
4851 char_attr = hl_combine_attr(syntax_attr, char_attr);
4852 }
4853#endif
4854
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004855#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004856 // Get syntax attribute, unless still at the start of the line
4857 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004858 v = (long)(ptr - line);
4859 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
4861 /* Get the syntax attribute for the character. If there
4862 * is an error, disable syntax highlighting. */
4863 save_did_emsg = did_emsg;
4864 did_emsg = FALSE;
4865
Bram Moolenaar217ad922005-03-20 22:37:15 +00004866 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004867# ifdef FEAT_SPELL
4868 has_spell ? &can_spell :
4869# endif
4870 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004873 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004874 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004875 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004876 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 else
4879 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004880
4881 // combine syntax attribute with 'wincolor'
4882 if (win_attr != 0)
4883 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4884
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004885#ifdef SYN_TIME_LIMIT
4886 if (wp->w_s->b_syn_slow)
4887 has_syntax = FALSE;
4888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /* Need to get the line again, a multi-line regexp may
4891 * have made it invalid. */
4892 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4893 ptr = line + v;
4894
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004895# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004896 // Text properties overrule syntax highlighting or combine.
4897 if (text_prop_attr == 0 || text_prop_combine)
4898# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004899 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004900 int comb_attr = syntax_attr;
4901# ifdef FEAT_TEXT_PROP
4902 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4903# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004904 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004905 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004906 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004907 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004908 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004909# ifdef FEAT_CONCEAL
4910 /* no concealing past the end of the line, it interferes
4911 * with line highlighting */
4912 if (c == NUL)
4913 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004914 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004915 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004918#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004919
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004920#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004921 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004922 * Only do this when there is no syntax highlighting, the
4923 * @Spell cluster is not used or the current syntax item
4924 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004925 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004926 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004927 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004928 if (c != 0 && (
4929# ifdef FEAT_SYN_HL
4930 !has_syntax ||
4931# endif
4932 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004933 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004934 char_u *prev_ptr, *p;
4935 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004936 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004937 if (has_mbyte)
4938 {
4939 prev_ptr = ptr - mb_l;
4940 v -= mb_l - 1;
4941 }
4942 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004943 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004944
4945 /* Use nextline[] if possible, it has the start of the
4946 * next line concatenated. */
4947 if ((prev_ptr - line) - nextlinecol >= 0)
4948 p = nextline + (prev_ptr - line) - nextlinecol;
4949 else
4950 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004951 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004952 len = spell_check(wp, p, &spell_hlf, &cap_col,
4953 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004954 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004955
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004956 /* In Insert mode only highlight a word that
4957 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004958 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004959 && (State & INSERT) != 0
4960 && wp->w_cursor.lnum == lnum
4961 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004962 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004963 && wp->w_cursor.col < (colnr_T)word_end)
4964 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004965 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004966 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004967 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004968
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004969 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004970 && (p - nextline) + len > nextline_idx)
4971 {
4972 /* Remember that the good word continues at the
4973 * start of the next line. */
4974 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004975 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004976 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004977
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004978 /* Turn index into actual attributes. */
4979 if (spell_hlf != HLF_COUNT)
4980 spell_attr = highlight_attr[spell_hlf];
4981
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004982 if (cap_col > 0)
4983 {
4984 if (p != prev_ptr
4985 && (p - nextline) + cap_col >= nextline_idx)
4986 {
4987 /* Remember that the word in the next line
4988 * must start with a capital. */
4989 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004990 cap_col = (int)((p - nextline) + cap_col
4991 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004992 }
4993 else
4994 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004995 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004996 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004997 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004998 }
4999 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005000 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005001 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005002 char_attr = hl_combine_attr(char_attr, spell_attr);
5003 else
5004 char_attr = hl_combine_attr(spell_attr, char_attr);
5005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#endif
5007#ifdef FEAT_LINEBREAK
5008 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005009 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005011 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005012 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005014 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005015 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005016
Bram Moolenaar597a4222014-06-25 14:39:50 +02005017 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005018 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005019 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005020 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005021# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005022 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005023 wp->w_buffer->b_p_vts_array) - 1;
5024# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005025 n_extra = (int)wp->w_buffer->b_p_ts
5026 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005027# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005028
Bram Moolenaar4df70292015-03-21 14:20:16 +01005029 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005030 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005031 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005032 {
5033#ifdef FEAT_CONCEAL
5034 if (c == TAB)
5035 /* See "Tab alignment" below. */
5036 FIX_FOR_BOGUSCOLS;
5037#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005038 if (!wp->w_p_list)
5039 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042#endif
5043
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005044 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5045 // But not when the character is followed by a composing
5046 // character (use mb_l to check that).
5047 if (wp->w_p_list
5048 && ((((c == 160 && mb_l == 1)
5049 || (mb_utf8
5050 && ((mb_c == 160 && mb_l == 2)
5051 || (mb_c == 0x202f && mb_l == 3))))
5052 && lcs_nbsp)
5053 || (c == ' '
5054 && mb_l == 1
5055 && lcs_space
5056 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005057 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005058 c = (c == ' ') ? lcs_space : lcs_nbsp;
5059 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005060 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005061 n_attr = 1;
5062 extra_attr = HL_ATTR(HLF_8);
5063 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005064 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005065 mb_c = c;
5066 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005067 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005068 mb_utf8 = TRUE;
5069 u8cc[0] = 0;
5070 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005071 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005072 else
5073 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005074 }
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5077 {
5078 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005079 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005082 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 saved_attr2 = char_attr; /* save current attr */
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005086 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005089 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005090 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 else
5093 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 }
5096
5097 /*
5098 * Handling of non-printable characters.
5099 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005100 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 /*
5103 * when getting a character from the file, we may have to
5104 * turn it into something else on the way to putting it
5105 * into "ScreenLines".
5106 */
5107 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5108 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005109 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005110 long vcol_adjusted = vcol; /* removed showbreak length */
5111#ifdef FEAT_LINEBREAK
5112 /* only adjust the tab_len, when at the first column
5113 * after the showbreak value was drawn */
5114 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5115 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005118#ifdef FEAT_VARTABS
5119 tab_len = tabstop_padding(vcol_adjusted,
5120 wp->w_buffer->b_p_ts,
5121 wp->w_buffer->b_p_vts_array) - 1;
5122#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005123 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005124 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5125#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005126
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005127#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005128 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005129#endif
5130 /* tab amount depends on current column */
5131 n_extra = tab_len;
5132#ifdef FEAT_LINEBREAK
5133 else
5134 {
5135 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005136 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005137 int i;
5138 int saved_nextra = n_extra;
5139
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005140#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005141 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005142 /* there are characters to conceal */
5143 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005144 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5145 */
5146 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5147 && n_extra > tab_len)
5148 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005149#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005150
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005151 /* if n_extra > 0, it gives the number of chars, to
5152 * use for a tab, else we need to calculate the width
5153 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005154 len = (tab_len * mb_char2len(lcs_tab2));
5155 if (n_extra > 0)
5156 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005157 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005158 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005159 vim_memset(p, ' ', len);
5160 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005161 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005162 p_extra_free = p;
5163 for (i = 0; i < tab_len; i++)
5164 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005165 if (*p == NUL)
5166 {
5167 tab_len = i;
5168 break;
5169 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005170 mb_char2bytes(lcs_tab2, p);
5171 p += mb_char2len(lcs_tab2);
5172 n_extra += mb_char2len(lcs_tab2)
5173 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005174 }
5175 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005176#ifdef FEAT_CONCEAL
5177 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5178 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005179 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005180 n_extra -= vcol_off;
5181#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005182 }
5183#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005184#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005185 {
5186 int vc_saved = vcol_off;
5187
5188 /* Tab alignment should be identical regardless of
5189 * 'conceallevel' value. So tab compensates of all
5190 * previous concealed characters, and thus resets
5191 * vcol_off and boguscols accumulated so far in the
5192 * line. Note that the tab can be longer than
5193 * 'tabstop' when there are concealed characters. */
5194 FIX_FOR_BOGUSCOLS;
5195
5196 /* Make sure, the highlighting for the tab char will be
5197 * correctly set further below (effectively reverts the
5198 * FIX_FOR_BOGSUCOLS macro */
5199 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005200 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005201 tab_len += vc_saved;
5202 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (wp->w_p_list)
5206 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005207 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005208#ifdef FEAT_LINEBREAK
5209 if (wp->w_p_lbr)
5210 c_extra = NUL; /* using p_extra from above */
5211 else
5212#endif
5213 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005214 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005215 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005216 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005219 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005222 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005223 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 else
5227 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005228 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 c_extra = ' ';
5230 c = ' ';
5231 }
5232 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005233 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005234 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005235 || ((fromcol >= 0 || fromcol_prev >= 0)
5236 && tocol > vcol
5237 && VIsual_mode != Ctrl_V
5238 && (
5239# ifdef FEAT_RIGHTLEFT
5240 wp->w_p_rl ? (col >= 0) :
5241# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005242 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005243 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005244 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005245 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005246 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005248 /* Display a '$' after the line or highlight an extra
5249 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5251 /* For a diff line the highlighting continues after the
5252 * "$". */
5253 if (
5254# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005255 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256# ifdef LINE_ATTR
5257 &&
5258# endif
5259# endif
5260# ifdef LINE_ATTR
5261 line_attr == 0
5262# endif
5263 )
5264#endif
5265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /* In virtualedit, visual selections may extend
5267 * beyond end of line. */
5268 if (area_highlighting && virtual_active()
5269 && tocol != MAXCOL && vcol < tocol)
5270 n_extra = 0;
5271 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 p_extra = at_end_str;
5274 n_extra = 1;
5275 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005276 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
5278 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005279 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005280 c = lcs_eol;
5281 else
5282 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 lcs_eol_one = -1;
5284 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005285 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005287 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 n_attr = 1;
5289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005291 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005294 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005295 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297 else
5298 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300 else if (c != NUL)
5301 {
5302 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005303 if (n_extra == 0)
5304 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#ifdef FEAT_RIGHTLEFT
5306 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5307 rl_mirror(p_extra); /* reverse "<12>" */
5308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005310 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005311#ifdef FEAT_LINEBREAK
5312 if (wp->w_p_lbr)
5313 {
5314 char_u *p;
5315
5316 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005317 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005318 vim_memset(p, ' ', n_extra);
5319 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5320 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005321 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005322 p_extra_free = p_extra = p;
5323 }
5324 else
5325#endif
5326 {
5327 n_extra = byte2cells(c) - 1;
5328 c = *p_extra++;
5329 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005330 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005333 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 saved_attr2 = char_attr; /* save current attr */
5335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 else if (VIsual_active
5339 && (VIsual_mode == Ctrl_V
5340 || VIsual_mode == 'v')
5341 && virtual_active()
5342 && tocol != MAXCOL
5343 && vcol < tocol
5344 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005345#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005347#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005348 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 c = ' ';
5351 --ptr; /* put it back at the NUL */
5352 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005353#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 else if ((
5355# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005356 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005358# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005359 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ) && (
5363# ifdef FEAT_RIGHTLEFT
5364 wp->w_p_rl ? (col >= 0) :
5365# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005366 (col
5367# ifdef FEAT_CONCEAL
5368 - boguscols
5369# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005370 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
5372 /* Highlight until the right side of the window */
5373 c = ' ';
5374 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005375
5376 /* Remember we do the char for line highlighting. */
5377 ++did_line_attr;
5378
5379 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005380 if (line_attr != 0 && char_attr == search_attr
5381 && (did_line_attr > 1
5382 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005383 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384# ifdef FEAT_DIFF
5385 if (diff_hlf == HLF_TXD)
5386 {
5387 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005388 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005389 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005390 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005391 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5392 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005393 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005397# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005398 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005399 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005400 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005401 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5402 char_attr = hl_combine_attr(char_attr,
5403 HL_ATTR(HLF_CUL));
5404 }
5405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407#endif
5408 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005409
5410#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005411 if ( wp->w_p_cole > 0
5412 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005413 conceal_cursor_line(wp))
5414 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005415 && !(lnum_in_visual_area
5416 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005417 {
5418 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005419 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005420 && (syn_get_sub_char() != NUL || match_conc
5421 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005422 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005423 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005424 /* First time at this concealed item: display one
5425 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005426 if (match_conc)
5427 c = match_conc;
5428 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005429 c = syn_get_sub_char();
5430 else if (lcs_conceal != NUL)
5431 c = lcs_conceal;
5432 else
5433 c = ' ';
5434
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005435 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005436
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005437 if (n_extra > 0)
5438 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005439 vcol += n_extra;
5440 if (wp->w_p_wrap && n_extra > 0)
5441 {
5442# ifdef FEAT_RIGHTLEFT
5443 if (wp->w_p_rl)
5444 {
5445 col -= n_extra;
5446 boguscols -= n_extra;
5447 }
5448 else
5449# endif
5450 {
5451 boguscols += n_extra;
5452 col += n_extra;
5453 }
5454 }
5455 n_extra = 0;
5456 n_attr = 0;
5457 }
5458 else if (n_skip == 0)
5459 {
5460 is_concealing = TRUE;
5461 n_skip = 1;
5462 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005463 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005464 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005465 {
5466 mb_utf8 = TRUE;
5467 u8cc[0] = 0;
5468 c = 0xc0;
5469 }
5470 else
5471 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005472 }
5473 else
5474 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005475 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005476 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005477 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005478
5479 if (n_skip > 0 && did_decrement_ptr)
5480 // not showing the '>', put pointer back to avoid getting stuck
5481 ++ptr;
5482
5483#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005486#ifdef FEAT_CONCEAL
5487 /* In the cursor line and we may be concealing characters: correct
5488 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005489 if (!did_wcol && draw_state == WL_LINE
5490 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005491 && conceal_cursor_line(wp)
5492 && (int)wp->w_virtcol <= vcol + n_skip)
5493 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005494# ifdef FEAT_RIGHTLEFT
5495 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005496 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005497 else
5498# endif
5499 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005500 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005501 did_wcol = TRUE;
5502 }
5503#endif
5504
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 /* Don't override visual selection highlighting. */
5506 if (n_attr > 0
5507 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005508 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 char_attr = extra_attr;
5510
Bram Moolenaar81695252004-12-29 20:58:21 +00005511#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 /* XIM don't send preedit_start and preedit_end, but they send
5513 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5514 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005515 if (p_imst == IM_ON_THE_SPOT
5516 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005517 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 && (State & INSERT)
5519 && !p_imdisable
5520 && im_is_preediting()
5521 && draw_state == WL_LINE)
5522 {
5523 colnr_T tcol;
5524
5525 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005526 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 else
5528 tcol = preedit_end_col;
5529 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5530 {
5531 if (feedback_old_attr < 0)
5532 {
5533 feedback_col = 0;
5534 feedback_old_attr = char_attr;
5535 }
5536 char_attr = im_get_feedback_attr(feedback_col);
5537 if (char_attr < 0)
5538 char_attr = feedback_old_attr;
5539 feedback_col++;
5540 }
5541 else if (feedback_old_attr >= 0)
5542 {
5543 char_attr = feedback_old_attr;
5544 feedback_old_attr = -1;
5545 feedback_col = 0;
5546 }
5547 }
5548#endif
5549 /*
5550 * Handle the case where we are in column 0 but not on the first
5551 * character of the line and the user wants us to show us a
5552 * special character (via 'listchars' option "precedes:<char>".
5553 */
5554 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005555 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5557#ifdef FEAT_DIFF
5558 && filler_todo <= 0
5559#endif
5560 && draw_state > WL_NR
5561 && c != NUL)
5562 {
5563 c = lcs_prec;
5564 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005565 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5566 {
5567 /* Double-width character being overwritten by the "precedes"
5568 * character, need to fill up half the character. */
5569 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005570 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005571 n_extra = 1;
5572 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005573 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005576 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005579 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005580 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else
5583 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005584 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005587 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 n_attr3 = 1;
5589 }
5590 }
5591
5592 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005593 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005595 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005596#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005597 || did_line_attr == 1
5598#endif
5599 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005601#ifdef FEAT_SEARCH_EXTRA
5602 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005603
5604 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005605 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005606 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005607#endif
5608
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005609 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 * highlight match at end of line. If it's beyond the last
5611 * char on the screen, just overwrite that one (tricky!) Not
5612 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005613#ifdef FEAT_SEARCH_EXTRA
5614 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005615 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005616 prevcol_hl_flag = TRUE;
5617 else
5618 {
5619 cur = wp->w_match_head;
5620 while (cur != NULL)
5621 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005622 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005623 {
5624 prevcol_hl_flag = TRUE;
5625 break;
5626 }
5627 cur = cur->next;
5628 }
5629 }
5630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005632 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005633 && (VIsual_mode != Ctrl_V
5634 || lnum == VIsual.lnum
5635 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005636 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637#ifdef FEAT_SEARCH_EXTRA
5638 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005639 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005640# ifdef FEAT_SYN_HL
5641 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5642 && !(wp == curwin && VIsual_active))
5643# endif
5644# ifdef FEAT_DIFF
5645 && diff_hlf == (hlf_T)0
5646# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005647# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005648 && did_line_attr <= 1
5649# endif
5650 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651#endif
5652 ))
5653 {
5654 int n = 0;
5655
5656#ifdef FEAT_RIGHTLEFT
5657 if (wp->w_p_rl)
5658 {
5659 if (col < 0)
5660 n = 1;
5661 }
5662 else
5663#endif
5664 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005665 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 n = -1;
5667 }
5668 if (n != 0)
5669 {
5670 /* At the window boundary, highlight the last character
5671 * instead (better than nothing). */
5672 off += n;
5673 col += n;
5674 }
5675 else
5676 {
5677 /* Add a blank character to highlight. */
5678 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (enc_utf8)
5680 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682#ifdef FEAT_SEARCH_EXTRA
5683 if (area_attr == 0)
5684 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005685 /* Use attributes from match with highest priority among
5686 * 'search_hl' and the match list. */
5687 char_attr = search_hl.attr;
5688 cur = wp->w_match_head;
5689 shl_flag = FALSE;
5690 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005691 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005692 if (shl_flag == FALSE
5693 && ((cur != NULL
5694 && cur->priority > SEARCH_HL_PRIORITY)
5695 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005696 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005697 shl = &search_hl;
5698 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005699 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005700 else
5701 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005702 if ((ptr - line) - 1 == (long)shl->startcol
5703 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005704 char_attr = shl->attr;
5705 if (shl != &search_hl && cur != NULL)
5706 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709#endif
5710 ScreenAttrs[off] = char_attr;
5711#ifdef FEAT_RIGHTLEFT
5712 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005713 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005715 --off;
5716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 else
5718#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005721 ++off;
5722 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005723 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005724#ifdef FEAT_SYN_HL
5725 eol_hl_off = 1;
5726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar91170f82006-05-05 21:15:17 +00005730 /*
5731 * At end of the text line.
5732 */
5733 if (c == NUL)
5734 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005735#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005736 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005737 if (wp->w_p_wrap)
5738 v = wp->w_skipcol;
5739 else
5740 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005741
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005742 /* check if line ends before left margin */
5743 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005744 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005745#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005746 // Get rid of the boguscols now, we want to draw until the right
5747 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005748 col -= boguscols;
5749 boguscols = 0;
5750#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005751
5752 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005753 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005754
5755 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005756 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5757 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005758 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005759 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005760 || draw_color_col
5761 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005762# ifdef FEAT_RIGHTLEFT
5763 && !wp->w_p_rl
5764# endif
5765 )
5766 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005767 int rightmost_vcol = 0;
5768 int i;
5769
5770 if (wp->w_p_cuc)
5771 rightmost_vcol = wp->w_virtcol;
5772 if (draw_color_col)
5773 /* determine rightmost colorcolumn to possibly draw */
5774 for (i = 0; color_cols[i] >= 0; ++i)
5775 if (rightmost_vcol < color_cols[i])
5776 rightmost_vcol = color_cols[i];
5777
Bram Moolenaar02631462017-09-22 15:20:32 +02005778 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005779 {
5780 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005781 if (enc_utf8)
5782 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005784 if (draw_color_col)
5785 draw_color_col = advance_color_col(VCOL_HLC,
5786 &color_cols);
5787
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005788 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005789 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005790 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005791 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005792 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005793 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005794
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005795 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005796 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005797
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005798 ++vcol;
5799 }
5800 }
5801#endif
5802
Bram Moolenaar53f81742017-09-22 14:35:51 +02005803 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005804 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 row++;
5806
5807 /*
5808 * Update w_cline_height and w_cline_folded if the cursor line was
5809 * updated (saves a call to plines() later).
5810 */
5811 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5812 {
5813 curwin->w_cline_row = startrow;
5814 curwin->w_cline_height = row - startrow;
5815#ifdef FEAT_FOLDING
5816 curwin->w_cline_folded = FALSE;
5817#endif
5818 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5819 }
5820
5821 break;
5822 }
5823
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005824 // Show "extends" character from 'listchars' if beyond the line end and
5825 // 'list' is set.
5826 if (lcs_ext != NUL
5827 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 && !wp->w_p_wrap
5829#ifdef FEAT_DIFF
5830 && filler_todo <= 0
5831#endif
5832 && (
5833#ifdef FEAT_RIGHTLEFT
5834 wp->w_p_rl ? col == 0 :
5835#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005836 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005838 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5840 {
5841 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005842 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005844 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {
5846 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005847 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005848 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
5850 else
5851 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005854#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005855 /* advance to the next 'colorcolumn' */
5856 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005857 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005858
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005859 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005860 * highlight the cursor position itself.
5861 * Also highlight the 'colorcolumn' if it is different than
5862 * 'cursorcolumn' */
5863 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005864 if (draw_state == WL_LINE && !lnum_in_visual_area
5865 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005866 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005867 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005868 && lnum != wp->w_cursor.lnum)
5869 {
5870 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005871 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005872 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005873 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005874 {
5875 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005876 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005877 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005878 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005879#endif
5880
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 /*
5882 * Store character to be displayed.
5883 * Skip characters that are left of the screen for 'nowrap'.
5884 */
5885 vcol_prev = vcol;
5886 if (draw_state < WL_LINE || n_skip <= 0)
5887 {
5888 /*
5889 * Store the character.
5890 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005891#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5893 {
5894 /* A double-wide character is: put first halve in left cell. */
5895 --off;
5896 --col;
5897 }
5898#endif
5899 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005901 {
5902 if ((mb_c & 0xff00) == 0x8e00)
5903 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 else if (enc_utf8)
5907 {
5908 if (mb_utf8)
5909 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005910 int i;
5911
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005913 if ((c & 0xff) == 0)
5914 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005915 for (i = 0; i < Screen_mco; ++i)
5916 {
5917 ScreenLinesC[i][off] = u8cc[i];
5918 if (u8cc[i] == 0)
5919 break;
5920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 }
5922 else
5923 ScreenLinesUC[off] = 0;
5924 }
5925 if (multi_attr)
5926 {
5927 ScreenAttrs[off] = multi_attr;
5928 multi_attr = 0;
5929 }
5930 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 ScreenAttrs[off] = char_attr;
5932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5934 {
5935 /* Need to fill two screen columns. */
5936 ++off;
5937 ++col;
5938 if (enc_utf8)
5939 /* UTF-8: Put a 0 in the second screen char. */
5940 ScreenLines[off] = 0;
5941 else
5942 /* DBCS: Put second byte in the second screen char. */
5943 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005944 if (draw_state > WL_NR
5945#ifdef FEAT_DIFF
5946 && filler_todo <= 0
5947#endif
5948 )
5949 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 /* When "tocol" is halfway a character, set it to the end of
5951 * the character, otherwise highlighting won't stop. */
5952 if (tocol == vcol)
5953 ++tocol;
5954#ifdef FEAT_RIGHTLEFT
5955 if (wp->w_p_rl)
5956 {
5957 /* now it's time to backup one cell */
5958 --off;
5959 --col;
5960 }
5961#endif
5962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963#ifdef FEAT_RIGHTLEFT
5964 if (wp->w_p_rl)
5965 {
5966 --off;
5967 --col;
5968 }
5969 else
5970#endif
5971 {
5972 ++off;
5973 ++col;
5974 }
5975 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005976#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005977 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005978 {
5979 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005980 ++vcol_off;
5981 if (n_extra > 0)
5982 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005983 if (wp->w_p_wrap)
5984 {
5985 /*
5986 * Special voodoo required if 'wrap' is on.
5987 *
5988 * Advance the column indicator to force the line
5989 * drawing to wrap early. This will make the line
5990 * take up the same screen space when parts are concealed,
5991 * so that cursor line computations aren't messed up.
5992 *
5993 * To avoid the fictitious advance of 'col' causing
5994 * trailing junk to be written out of the screen line
5995 * we are building, 'boguscols' keeps track of the number
5996 * of bad columns we have advanced.
5997 */
5998 if (n_extra > 0)
5999 {
6000 vcol += n_extra;
6001# ifdef FEAT_RIGHTLEFT
6002 if (wp->w_p_rl)
6003 {
6004 col -= n_extra;
6005 boguscols -= n_extra;
6006 }
6007 else
6008# endif
6009 {
6010 col += n_extra;
6011 boguscols += n_extra;
6012 }
6013 n_extra = 0;
6014 n_attr = 0;
6015 }
6016
6017
Bram Moolenaar860cae12010-06-05 23:22:07 +02006018 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6019 {
6020 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006021# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006022 if (wp->w_p_rl)
6023 {
6024 --boguscols;
6025 --col;
6026 }
6027 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006028# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006029 {
6030 ++boguscols;
6031 ++col;
6032 }
6033 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006034
6035# ifdef FEAT_RIGHTLEFT
6036 if (wp->w_p_rl)
6037 {
6038 --boguscols;
6039 --col;
6040 }
6041 else
6042# endif
6043 {
6044 ++boguscols;
6045 ++col;
6046 }
6047 }
6048 else
6049 {
6050 if (n_extra > 0)
6051 {
6052 vcol += n_extra;
6053 n_extra = 0;
6054 n_attr = 0;
6055 }
6056 }
6057
6058 }
6059#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 else
6061 --n_skip;
6062
Bram Moolenaar64486672010-05-16 15:46:46 +02006063 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6064 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006065 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066#ifdef FEAT_DIFF
6067 && filler_todo <= 0
6068#endif
6069 )
6070 ++vcol;
6071
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006072#ifdef FEAT_SYN_HL
6073 if (vcol_save_attr >= 0)
6074 char_attr = vcol_save_attr;
6075#endif
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 /* restore attributes after "predeces" in 'listchars' */
6078 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6079 char_attr = saved_attr3;
6080
6081 /* restore attributes after last 'listchars' or 'number' char */
6082 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6083 char_attr = saved_attr2;
6084
6085 /*
6086 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006087 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 */
6089 if ((
6090#ifdef FEAT_RIGHTLEFT
6091 wp->w_p_rl ? (col < 0) :
6092#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006093 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 && (*ptr != NUL
6095#ifdef FEAT_DIFF
6096 || filler_todo > 0
6097#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006098 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6100 )
6101 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006102#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006103 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006104 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006105 boguscols = 0;
6106#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006107 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006108 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 ++row;
6111 ++screen_row;
6112
6113 /* When not wrapping and finished diff lines, or when displayed
6114 * '$' and highlighting until last column, break here. */
6115 if ((!wp->w_p_wrap
6116#ifdef FEAT_DIFF
6117 && filler_todo <= 0
6118#endif
6119 ) || lcs_eol_one == -1)
6120 break;
6121
6122 /* When the window is too narrow draw all "@" lines. */
6123 if (draw_state != WL_LINE
6124#ifdef FEAT_DIFF
6125 && filler_todo <= 0
6126#endif
6127 )
6128 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006129 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 row = endrow;
6132 }
6133
6134 /* When line got too long for screen break here. */
6135 if (row == endrow)
6136 {
6137 ++row;
6138 break;
6139 }
6140
6141 if (screen_cur_row == screen_row - 1
6142#ifdef FEAT_DIFF
6143 && filler_todo <= 0
6144#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006145 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
6147 /* Remember that the line wraps, used for modeless copy. */
6148 LineWraps[screen_row - 1] = TRUE;
6149
6150 /*
6151 * Special trick to make copy/paste of wrapped lines work with
6152 * xterm/screen: write an extra character beyond the end of
6153 * the line. This will work with all terminal types
6154 * (regardless of the xn,am settings).
6155 * Only do this on a fast tty.
6156 * Only do this if the cursor is on the current line
6157 * (something has been written in it).
6158 * Don't do this for the GUI.
6159 * Don't do this for double-width characters.
6160 * Don't do this for a window not at the right screen border.
6161 */
6162 if (p_tf
6163#ifdef FEAT_GUI
6164 && !gui.in_use
6165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006167 && ((*mb_off2cells)(LineOffset[screen_row],
6168 LineOffset[screen_row] + screen_Columns)
6169 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006171 + (int)Columns - 2,
6172 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006173 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
6175 /* First make sure we are at the end of the screen line,
6176 * then output the same character again to let the
6177 * terminal know about the wrap. If the terminal doesn't
6178 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006179 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 screen_char(LineOffset[screen_row - 1]
6181 + (unsigned)Columns - 1,
6182 screen_row - 1, (int)(Columns - 1));
6183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 /* When there is a multi-byte character, just output a
6185 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006186 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6187 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 out_char(' ');
6189 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 out_char(ScreenLines[LineOffset[screen_row - 1]
6191 + (Columns - 1)]);
6192 /* force a redraw of the first char on the next line */
6193 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6194 screen_start(); /* don't know where cursor is now */
6195 }
6196 }
6197
6198 col = 0;
6199 off = (unsigned)(current_ScreenLine - ScreenLines);
6200#ifdef FEAT_RIGHTLEFT
6201 if (wp->w_p_rl)
6202 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006203 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 off += col;
6205 }
6206#endif
6207
6208 /* reset the drawing state for the start of a wrapped line */
6209 draw_state = WL_START;
6210 saved_n_extra = n_extra;
6211 saved_p_extra = p_extra;
6212 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006213 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 saved_char_attr = char_attr;
6215 n_extra = 0;
6216 lcs_prec_todo = lcs_prec;
6217#ifdef FEAT_LINEBREAK
6218# ifdef FEAT_DIFF
6219 if (filler_todo <= 0)
6220# endif
6221 need_showbreak = TRUE;
6222#endif
6223#ifdef FEAT_DIFF
6224 --filler_todo;
6225 /* When the filler lines are actually below the last line of the
6226 * file, don't draw the line itself, break here. */
6227 if (filler_todo == 0 && wp->w_botfill)
6228 break;
6229#endif
6230 }
6231
6232 } /* for every character in the line */
6233
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006234#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006235 /* After an empty line check first word for capital. */
6236 if (*skipwhite(line) == NUL)
6237 {
6238 capcol_lnum = lnum + 1;
6239 cap_col = 0;
6240 }
6241#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006242#ifdef FEAT_TEXT_PROP
6243 vim_free(text_props);
6244 vim_free(text_prop_idxs);
6245#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006246
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006247 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 return row;
6249}
6250
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006251/*
6252 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006253 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006254 */
6255 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006256comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006257{
6258 int i;
6259
6260 for (i = 0; i < Screen_mco; ++i)
6261 {
6262 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6263 return TRUE;
6264 if (ScreenLinesC[i][off_from] == 0)
6265 break;
6266 }
6267 return FALSE;
6268}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270/*
6271 * Check whether the given character needs redrawing:
6272 * - the (first byte of the) character is different
6273 * - the attributes are different
6274 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006275 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 */
6277 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006278char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 if (cols > 0
6281 && ((ScreenLines[off_from] != ScreenLines[off_to]
6282 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 || (enc_dbcs != 0
6284 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6285 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6286 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6287 : (cols > 1 && ScreenLines[off_from + 1]
6288 != ScreenLines[off_to + 1])))
6289 || (enc_utf8
6290 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6291 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006292 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006293 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6294 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006295 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 return TRUE;
6297 return FALSE;
6298}
6299
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006300#if defined(FEAT_TERMINAL) || defined(PROTO)
6301/*
6302 * Return the index in ScreenLines[] for the current screen line.
6303 */
6304 int
6305screen_get_current_line_off()
6306{
6307 return (int)(current_ScreenLine - ScreenLines);
6308}
6309#endif
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311/*
6312 * Move one "cooked" screen line to the screen, but only the characters that
6313 * have actually changed. Handle insert/delete character.
6314 * "coloff" gives the first column on the screen for this line.
6315 * "endcol" gives the columns where valid characters are.
6316 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6317 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006318 * "flags" can have bits:
6319 * SLF_POPUP popup window
6320 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6322 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6323 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006324 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006325screen_line(
6326 int row,
6327 int coloff,
6328 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006329 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006330 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 unsigned off_from;
6333 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006334 unsigned max_off_from;
6335 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 int force = FALSE; /* force update rest of the line */
6339 int redraw_this /* bool: does character need redraw? */
6340#ifdef FEAT_GUI
6341 = TRUE /* For GUI when while-loop empty */
6342#endif
6343 ;
6344 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 int clear_next = FALSE;
6346 int char_cells; /* 1: normal char */
6347 /* 2: occupies two display cells */
6348# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006350 /* Check for illegal row and col, just in case. */
6351 if (row >= Rows)
6352 row = Rows - 1;
6353 if (endcol > Columns)
6354 endcol = Columns;
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356# ifdef FEAT_CLIPBOARD
6357 clip_may_clear_selection(row, row);
6358# endif
6359
6360 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6361 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006362 max_off_from = off_from + screen_Columns;
6363 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006366 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
6368 /* Clear rest first, because it's left of the text. */
6369 if (clear_width > 0)
6370 {
6371 while (col <= endcol && ScreenLines[off_to] == ' '
6372 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006373 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
6375 ++off_to;
6376 ++col;
6377 }
6378 if (col <= endcol)
6379 screen_fill(row, row + 1, col + coloff,
6380 endcol + coloff + 1, ' ', ' ', 0);
6381 }
6382 col = endcol + 1;
6383 off_to = LineOffset[row] + col + coloff;
6384 off_from += col;
6385 endcol = (clear_width > 0 ? clear_width : -clear_width);
6386 }
6387#endif /* FEAT_RIGHTLEFT */
6388
6389 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6390
6391 while (col < endcol)
6392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006394 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 else
6396 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
6398 redraw_this = redraw_next;
6399 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6400 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6401
6402#ifdef FEAT_GUI
6403 /* If the next character was bold, then redraw the current character to
6404 * remove any pixels that might have spilt over into us. This only
6405 * happens in the GUI.
6406 */
6407 if (redraw_next && gui.in_use)
6408 {
6409 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006410 if (hl > HL_ALL)
6411 hl = syn_attr2attr(hl);
6412 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 redraw_this = TRUE;
6414 }
6415#endif
6416
6417 if (redraw_this)
6418 {
6419 /*
6420 * Special handling when 'xs' termcap flag set (hpterm):
6421 * Attributes for characters are stored at the position where the
6422 * cursor is when writing the highlighting code. The
6423 * start-highlighting code must be written with the cursor on the
6424 * first highlighted character. The stop-highlighting code must
6425 * be written with the cursor just after the last highlighted
6426 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006427 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 * to clear the rest of the line, and force redrawing it
6429 * completely.
6430 */
6431 if ( p_wiv
6432 && !force
6433#ifdef FEAT_GUI
6434 && !gui.in_use
6435#endif
6436 && ScreenAttrs[off_to] != 0
6437 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6438 {
6439 /*
6440 * Need to remove highlighting attributes here.
6441 */
6442 windgoto(row, col + coloff);
6443 out_str(T_CE); /* clear rest of this screen line */
6444 screen_start(); /* don't know where cursor is now */
6445 force = TRUE; /* force redraw of rest of the line */
6446 redraw_next = TRUE; /* or else next char would miss out */
6447
6448 /*
6449 * If the previous character was highlighted, need to stop
6450 * highlighting at this character.
6451 */
6452 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6453 {
6454 screen_attr = ScreenAttrs[off_to - 1];
6455 term_windgoto(row, col + coloff);
6456 screen_stop_highlight();
6457 }
6458 else
6459 screen_attr = 0; /* highlighting has stopped */
6460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (enc_dbcs != 0)
6462 {
6463 /* Check if overwriting a double-byte with a single-byte or
6464 * the other way around requires another character to be
6465 * redrawn. For UTF-8 this isn't needed, because comparing
6466 * ScreenLinesUC[] is sufficient. */
6467 if (char_cells == 1
6468 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006469 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 /* Writing a single-cell character over a double-cell
6472 * character: need to redraw the next cell. */
6473 ScreenLines[off_to + 1] = 0;
6474 redraw_next = TRUE;
6475 }
6476 else if (char_cells == 2
6477 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006478 && (*mb_off2cells)(off_to, max_off_to) == 1
6479 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
6481 /* Writing the second half of a double-cell character over
6482 * a double-cell character: need to redraw the second
6483 * cell. */
6484 ScreenLines[off_to + 2] = 0;
6485 redraw_next = TRUE;
6486 }
6487
6488 if (enc_dbcs == DBCS_JPNU)
6489 ScreenLines2[off_to] = ScreenLines2[off_from];
6490 }
6491 /* When writing a single-width character over a double-width
6492 * character and at the end of the redrawn text, need to clear out
6493 * the right halve of the old character.
6494 * Also required when writing the right halve of a double-width
6495 * char over the left halve of an existing one. */
6496 if (has_mbyte && col + char_cells == endcol
6497 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006498 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006500 && (*mb_off2cells)(off_to, max_off_to) == 1
6501 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (enc_utf8)
6506 {
6507 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6508 if (ScreenLinesUC[off_from] != 0)
6509 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006510 int i;
6511
6512 for (i = 0; i < Screen_mco; ++i)
6513 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515 }
6516 if (char_cells == 2)
6517 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006520 /* The bold trick makes a single column of pixels appear in the
6521 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 * character should be redrawn too. This happens for our own GUI
6523 * and for some xterms. */
6524 if (
6525# ifdef FEAT_GUI
6526 gui.in_use
6527# endif
6528# if defined(FEAT_GUI) && defined(UNIX)
6529 ||
6530# endif
6531# ifdef UNIX
6532 term_is_xterm
6533# endif
6534 )
6535 {
6536 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006537 if (hl > HL_ALL)
6538 hl = syn_attr2attr(hl);
6539 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 redraw_next = TRUE;
6541 }
6542#endif
6543 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006544
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006545 /* For simplicity set the attributes of second half of a
6546 * double-wide character equal to the first half. */
6547 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006549
6550 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 screen_char(off_to, row, col + coloff);
6554 }
6555 else if ( p_wiv
6556#ifdef FEAT_GUI
6557 && !gui.in_use
6558#endif
6559 && col + coloff > 0)
6560 {
6561 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6562 {
6563 /*
6564 * Don't output stop-highlight when moving the cursor, it will
6565 * stop the highlighting when it should continue.
6566 */
6567 screen_attr = 0;
6568 }
6569 else if (screen_attr != 0)
6570 screen_stop_highlight();
6571 }
6572
6573 off_to += CHAR_CELLS;
6574 off_from += CHAR_CELLS;
6575 col += CHAR_CELLS;
6576 }
6577
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (clear_next)
6579 {
6580 /* Clear the second half of a double-wide character of which the left
6581 * half was overwritten with a single-wide character. */
6582 ScreenLines[off_to] = ' ';
6583 if (enc_utf8)
6584 ScreenLinesUC[off_to] = 0;
6585 screen_char(off_to, row, col + coloff);
6586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
6588 if (clear_width > 0
6589#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006590 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591#endif
6592 )
6593 {
6594#ifdef FEAT_GUI
6595 int startCol = col;
6596#endif
6597
6598 /* blank out the rest of the line */
6599 while (col < clear_width && ScreenLines[off_to] == ' '
6600 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006601 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 {
6603 ++off_to;
6604 ++col;
6605 }
6606 if (col < clear_width)
6607 {
6608#ifdef FEAT_GUI
6609 /*
6610 * In the GUI, clearing the rest of the line may leave pixels
6611 * behind if the first character cleared was bold. Some bold
6612 * fonts spill over the left. In this case we redraw the previous
6613 * character too. If we didn't skip any blanks above, then we
6614 * only redraw if the character wasn't already redrawn anyway.
6615 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006616 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
6618 hl = ScreenAttrs[off_to];
6619 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006620 {
6621 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006622
Bram Moolenaar9c697322006-10-09 20:11:17 +00006623 if (enc_utf8)
6624 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6625 * that its width is 2. */
6626 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6627 else if (enc_dbcs != 0)
6628 {
6629 /* find previous character by counting from first
6630 * column and get its width. */
6631 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006632 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006633
6634 while (off < off_to)
6635 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006636 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006637 off += prev_cells;
6638 }
6639 }
6640
6641 if (enc_dbcs != 0 && prev_cells > 1)
6642 screen_char_2(off_to - prev_cells, row,
6643 col + coloff - prev_cells);
6644 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006645 screen_char(off_to - prev_cells, row,
6646 col + coloff - prev_cells);
6647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
6649#endif
6650 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6651 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 off_to += clear_width - col;
6653 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 }
6655 }
6656
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006657 if (clear_width > 0
6658#ifdef FEAT_TEXT_PROP
6659 && !(flags & SLF_POPUP) // no separator for popup window
6660#endif
6661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006663 // For a window that has a right neighbor, draw the separator char
6664 // right of the window contents.
6665 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
6667 int c;
6668
6669 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006670 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006671 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6672 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 || ScreenAttrs[off_to] != hl)
6674 {
6675 ScreenLines[off_to] = c;
6676 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (enc_utf8)
6678 {
6679 if (c >= 0x80)
6680 {
6681 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006682 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684 else
6685 ScreenLinesUC[off_to] = 0;
6686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 screen_char(off_to, row, col + coloff);
6688 }
6689 }
6690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 LineWraps[row] = FALSE;
6692 }
6693}
6694
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006697 * Mirror text "str" for right-left displaying.
6698 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006700 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006701rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 char_u *p1, *p2;
6704 int t;
6705
6706 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6707 {
6708 t = *p1;
6709 *p1 = *p2;
6710 *p2 = t;
6711 }
6712}
6713#endif
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715/*
6716 * mark all status lines for redraw; used after first :cd
6717 */
6718 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006719status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
6721 win_T *wp;
6722
Bram Moolenaar29323592016-07-24 22:04:11 +02006723 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (wp->w_status_height)
6725 {
6726 wp->w_redr_status = TRUE;
6727 redraw_later(VALID);
6728 }
6729}
6730
6731/*
6732 * mark all status lines of the current buffer for redraw
6733 */
6734 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006735status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 win_T *wp;
6738
Bram Moolenaar29323592016-07-24 22:04:11 +02006739 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6741 {
6742 wp->w_redr_status = TRUE;
6743 redraw_later(VALID);
6744 }
6745}
6746
6747/*
6748 * Redraw all status lines that need to be redrawn.
6749 */
6750 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006751redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 win_T *wp;
6754
Bram Moolenaar29323592016-07-24 22:04:11 +02006755 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006757 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006758 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006759 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
Bram Moolenaar4033c552017-09-16 20:54:51 +02006762#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763/*
6764 * Redraw all status lines at the bottom of frame "frp".
6765 */
6766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006767win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 if (frp->fr_layout == FR_LEAF)
6770 frp->fr_win->w_redr_status = TRUE;
6771 else if (frp->fr_layout == FR_ROW)
6772 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006773 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 win_redraw_last_status(frp);
6775 }
6776 else /* frp->fr_layout == FR_COL */
6777 {
6778 frp = frp->fr_child;
6779 while (frp->fr_next != NULL)
6780 frp = frp->fr_next;
6781 win_redraw_last_status(frp);
6782 }
6783}
6784#endif
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786/*
6787 * Draw the verticap separator right of window "wp" starting with line "row".
6788 */
6789 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006790draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 int hl;
6793 int c;
6794
6795 if (wp->w_vsep_width)
6796 {
6797 /* draw the vertical separator right of this window */
6798 c = fillchar_vsep(&hl);
6799 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6800 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6801 c, ' ', hl);
6802 }
6803}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006806static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807
6808/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006809 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 */
6811 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006812status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813{
6814 int len = 0;
6815
6816#ifdef FEAT_MENU
6817 int emenu = (xp->xp_context == EXPAND_MENUS
6818 || xp->xp_context == EXPAND_MENUNAMES);
6819
6820 /* Check for menu separators - replace with '|'. */
6821 if (emenu && menu_is_separator(s))
6822 return 1;
6823#endif
6824
6825 while (*s != NUL)
6826 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006827 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006828 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006829 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831
6832 return len;
6833}
6834
6835/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006836 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006837 * These are backslashes used for escaping. Do show backslashes in help tags.
6838 */
6839 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006840skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006841{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006842 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006843#ifdef FEAT_MENU
6844 || ((xp->xp_context == EXPAND_MENUS
6845 || xp->xp_context == EXPAND_MENUNAMES)
6846 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6847#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006848 )
6849 {
6850#ifndef BACKSLASH_IN_FILENAME
6851 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6852 return 2;
6853#endif
6854 return 1;
6855 }
6856 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006857}
6858
6859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 * Show wildchar matches in the status line.
6861 * Show at least the "match" item.
6862 * We start at item 'first_match' in the list and show all matches that fit.
6863 *
6864 * If inversion is possible we use it. Else '=' characters are used.
6865 */
6866 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006867win_redr_status_matches(
6868 expand_T *xp,
6869 int num_matches,
6870 char_u **matches, /* list of matches */
6871 int match,
6872 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6875 int row;
6876 char_u *buf;
6877 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006878 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int fillchar;
6880 int attr;
6881 int i;
6882 int highlight = TRUE;
6883 char_u *selstart = NULL;
6884 int selstart_col = 0;
6885 char_u *selend = NULL;
6886 static int first_match = 0;
6887 int add_left = FALSE;
6888 char_u *s;
6889#ifdef FEAT_MENU
6890 int emenu;
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
6894 if (matches == NULL) /* interrupted completion? */
6895 return;
6896
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006897 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006898 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006899 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006900 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (buf == NULL)
6902 return;
6903
6904 if (match == -1) /* don't show match but original text */
6905 {
6906 match = 0;
6907 highlight = FALSE;
6908 }
6909 /* count 1 for the ending ">" */
6910 clen = status_match_len(xp, L_MATCH(match)) + 3;
6911 if (match == 0)
6912 first_match = 0;
6913 else if (match < first_match)
6914 {
6915 /* jumping left, as far as we can go */
6916 first_match = match;
6917 add_left = TRUE;
6918 }
6919 else
6920 {
6921 /* check if match fits on the screen */
6922 for (i = first_match; i < match; ++i)
6923 clen += status_match_len(xp, L_MATCH(i)) + 2;
6924 if (first_match > 0)
6925 clen += 2;
6926 /* jumping right, put match at the left */
6927 if ((long)clen > Columns)
6928 {
6929 first_match = match;
6930 /* if showing the last match, we can add some on the left */
6931 clen = 2;
6932 for (i = match; i < num_matches; ++i)
6933 {
6934 clen += status_match_len(xp, L_MATCH(i)) + 2;
6935 if ((long)clen >= Columns)
6936 break;
6937 }
6938 if (i == num_matches)
6939 add_left = TRUE;
6940 }
6941 }
6942 if (add_left)
6943 while (first_match > 0)
6944 {
6945 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6946 if ((long)clen >= Columns)
6947 break;
6948 --first_match;
6949 }
6950
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006951 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953 if (first_match == 0)
6954 {
6955 *buf = NUL;
6956 len = 0;
6957 }
6958 else
6959 {
6960 STRCPY(buf, "< ");
6961 len = 2;
6962 }
6963 clen = len;
6964
6965 i = first_match;
6966 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6967 {
6968 if (i == match)
6969 {
6970 selstart = buf + len;
6971 selstart_col = clen;
6972 }
6973
6974 s = L_MATCH(i);
6975 /* Check for menu separators - replace with '|' */
6976#ifdef FEAT_MENU
6977 emenu = (xp->xp_context == EXPAND_MENUS
6978 || xp->xp_context == EXPAND_MENUNAMES);
6979 if (emenu && menu_is_separator(s))
6980 {
6981 STRCPY(buf + len, transchar('|'));
6982 l = (int)STRLEN(buf + len);
6983 len += l;
6984 clen += l;
6985 }
6986 else
6987#endif
6988 for ( ; *s != NUL; ++s)
6989 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006990 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006992 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {
6994 STRNCPY(buf + len, s, l);
6995 s += l - 1;
6996 len += l;
6997 }
6998 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {
7000 STRCPY(buf + len, transchar_byte(*s));
7001 len += (int)STRLEN(buf + len);
7002 }
7003 }
7004 if (i == match)
7005 selend = buf + len;
7006
7007 *(buf + len++) = ' ';
7008 *(buf + len++) = ' ';
7009 clen += 2;
7010 if (++i == num_matches)
7011 break;
7012 }
7013
7014 if (i != num_matches)
7015 {
7016 *(buf + len++) = '>';
7017 ++clen;
7018 }
7019
7020 buf[len] = NUL;
7021
7022 row = cmdline_row - 1;
7023 if (row >= 0)
7024 {
7025 if (wild_menu_showing == 0)
7026 {
7027 if (msg_scrolled > 0)
7028 {
7029 /* Put the wildmenu just above the command line. If there is
7030 * no room, scroll the screen one line up. */
7031 if (cmdline_row == Rows - 1)
7032 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007033 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 ++msg_scrolled;
7035 }
7036 else
7037 {
7038 ++cmdline_row;
7039 ++row;
7040 }
7041 wild_menu_showing = WM_SCROLLED;
7042 }
7043 else
7044 {
7045 /* Create status line if needed by setting 'laststatus' to 2.
7046 * Set 'winminheight' to zero to avoid that the window is
7047 * resized. */
7048 if (lastwin->w_status_height == 0)
7049 {
7050 save_p_ls = p_ls;
7051 save_p_wmh = p_wmh;
7052 p_ls = 2;
7053 p_wmh = 0;
7054 last_status(FALSE);
7055 }
7056 wild_menu_showing = WM_SHOWN;
7057 }
7058 }
7059
7060 screen_puts(buf, row, 0, attr);
7061 if (selstart != NULL && highlight)
7062 {
7063 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007064 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066
7067 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7068 }
7069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 vim_free(buf);
7072}
7073#endif
7074
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075/*
7076 * Redraw the status line of window wp.
7077 *
7078 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007079 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7080 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007082 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007083win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 int row;
7086 char_u *p;
7087 int len;
7088 int fillchar;
7089 int attr;
7090 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007091 static int busy = FALSE;
7092
7093 /* It's possible to get here recursively when 'statusline' (indirectly)
7094 * invokes ":redrawstatus". Simply ignore the call then. */
7095 if (busy)
7096 return;
7097 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099 wp->w_redr_status = FALSE;
7100 if (wp->w_status_height == 0)
7101 {
7102 /* no status line, can only be last window */
7103 redraw_cmdline = TRUE;
7104 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007105 else if (!redrawing()
7106#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007107 // don't update status line when popup menu is visible and may be
7108 // drawn over it, unless it will be redrawn later
7109 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007110#endif
7111 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
7113 /* Don't redraw right now, do it later. */
7114 wp->w_redr_status = TRUE;
7115 }
7116#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007117 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
7119 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007120 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122#endif
7123 else
7124 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007125 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007127 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 p = NameBuff;
7129 len = (int)STRLEN(p);
7130
Bram Moolenaard85f2712017-07-28 21:51:57 +02007131 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132#ifdef FEAT_QUICKFIX
7133 || wp->w_p_pvw
7134#endif
7135 || bufIsChanged(wp->w_buffer)
7136 || wp->w_buffer->b_p_ro)
7137 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007138 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007140 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 len += (int)STRLEN(p + len);
7142 }
7143#ifdef FEAT_QUICKFIX
7144 if (wp->w_p_pvw)
7145 {
7146 STRCPY(p + len, _("[Preview]"));
7147 len += (int)STRLEN(p + len);
7148 }
7149#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007150 if (bufIsChanged(wp->w_buffer)
7151#ifdef FEAT_TERMINAL
7152 && !bt_terminal(wp->w_buffer)
7153#endif
7154 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
7156 STRCPY(p + len, "[+]");
7157 len += 3;
7158 }
7159 if (wp->w_buffer->b_p_ro)
7160 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007161 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007162 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
7164
Bram Moolenaar02631462017-09-22 15:20:32 +02007165 this_ru_col = ru_col - (Columns - wp->w_width);
7166 if (this_ru_col < (wp->w_width + 1) / 2)
7167 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (this_ru_col <= 1)
7169 {
7170 p = (char_u *)"<"; /* No room for file name! */
7171 len = 1;
7172 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007173 else if (has_mbyte)
7174 {
7175 int clen = 0, i;
7176
7177 /* Count total number of display cells. */
7178 clen = mb_string2cells(p, -1);
7179
7180 /* Find first character that will fit.
7181 * Going from start to end is much faster for DBCS. */
7182 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7183 i += (*mb_ptr2len)(p + i))
7184 clen -= (*mb_ptr2cells)(p + i);
7185 len = clen;
7186 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007188 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007190 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192
Bram Moolenaara12a1612019-01-24 16:39:02 +01007193 }
7194 else if (len > this_ru_col - 1)
7195 {
7196 p += len - (this_ru_col - 1);
7197 *p = '<';
7198 len = this_ru_col - 1;
7199 }
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007202 screen_puts(p, row, wp->w_wincol, attr);
7203 screen_fill(row, row + 1, len + wp->w_wincol,
7204 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007206 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7208 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007209 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
7211#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007212 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213#endif
7214 }
7215
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 /*
7217 * May need to draw the character below the vertical separator.
7218 */
7219 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7220 {
7221 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007222 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 else
7224 fillchar = fillchar_vsep(&attr);
7225 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7226 attr);
7227 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007228 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229}
7230
Bram Moolenaar238a5642006-02-21 22:12:05 +00007231#ifdef FEAT_STL_OPT
7232/*
7233 * Redraw the status line according to 'statusline' and take care of any
7234 * errors encountered.
7235 */
7236 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007237redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007238{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007239 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007240 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007241
7242 /* When called recursively return. This can happen when the statusline
7243 * contains an expression that triggers a redraw. */
7244 if (entered)
7245 return;
7246 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007247
Bram Moolenaara742e082016-04-05 21:10:38 +02007248 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007249 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007250 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007251 {
7252 /* When there is an error disable the statusline, otherwise the
7253 * display is messed up with errors and a redraw triggers the problem
7254 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007255 set_string_option_direct((char_u *)"statusline", -1,
7256 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007257 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007258 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007259 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007260 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007261}
7262#endif
7263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264/*
7265 * Return TRUE if the status line of window "wp" is connected to the status
7266 * line of the window right of it. If not, then it's a vertical separator.
7267 * Only call if (wp->w_vsep_width != 0).
7268 */
7269 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007270stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 frame_T *fr;
7273
7274 fr = wp->w_frame;
7275 while (fr->fr_parent != NULL)
7276 {
7277 if (fr->fr_parent->fr_layout == FR_COL)
7278 {
7279 if (fr->fr_next != NULL)
7280 break;
7281 }
7282 else
7283 {
7284 if (fr->fr_next != NULL)
7285 return TRUE;
7286 }
7287 fr = fr->fr_parent;
7288 }
7289 return FALSE;
7290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293/*
7294 * Get the value to show for the language mappings, active 'keymap'.
7295 */
7296 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007297get_keymap_str(
7298 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007299 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007300 char_u *buf, /* buffer for the result */
7301 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 char_u *p;
7304
7305 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7306 return FALSE;
7307
7308 {
7309#ifdef FEAT_EVAL
7310 buf_T *old_curbuf = curbuf;
7311 win_T *old_curwin = curwin;
7312 char_u *s;
7313
7314 curbuf = wp->w_buffer;
7315 curwin = wp;
7316 STRCPY(buf, "b:keymap_name"); /* must be writable */
7317 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007318 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 --emsg_skip;
7320 curbuf = old_curbuf;
7321 curwin = old_curwin;
7322 if (p == NULL || *p == NUL)
7323#endif
7324 {
7325#ifdef FEAT_KEYMAP
7326 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7327 p = wp->w_buffer->b_p_keymap;
7328 else
7329#endif
7330 p = (char_u *)"lang";
7331 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007332 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 buf[0] = NUL;
7334#ifdef FEAT_EVAL
7335 vim_free(s);
7336#endif
7337 }
7338 return buf[0] != NUL;
7339}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
7341#if defined(FEAT_STL_OPT) || defined(PROTO)
7342/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007343 * Redraw the status line or ruler of window "wp".
7344 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 */
7346 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007347win_redr_custom(
7348 win_T *wp,
7349 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007351 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 int attr;
7353 int curattr;
7354 int row;
7355 int col = 0;
7356 int maxwidth;
7357 int width;
7358 int n;
7359 int len;
7360 int fillchar;
7361 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007362 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007364 struct stl_hlrec hltab[STL_MAX_ITEM];
7365 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007366 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007367 win_T *ewp;
7368 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369
Bram Moolenaar1d633412013-12-11 15:52:01 +01007370 /* There is a tiny chance that this gets called recursively: When
7371 * redrawing a status line triggers redrawing the ruler or tabline.
7372 * Avoid trouble by not allowing recursion. */
7373 if (entered)
7374 return;
7375 entered = TRUE;
7376
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007378 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007380 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007381 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007382 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007383 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007384 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007385 maxwidth = Columns;
7386# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007387 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007390 else
7391 {
7392 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007393 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007394 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007395
7396 if (draw_ruler)
7397 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007398 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007399 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007400 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007401 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007402 if (*++stl == '-')
7403 stl++;
7404 if (atoi((char *)stl))
7405 while (VIM_ISDIGIT(*stl))
7406 stl++;
7407 if (*stl++ != '(')
7408 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007409 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007410 col = ru_col - (Columns - wp->w_width);
7411 if (col < (wp->w_width + 1) / 2)
7412 col = (wp->w_width + 1) / 2;
7413 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007414 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007415 {
7416 row = Rows - 1;
7417 --maxwidth; /* writing in last column may cause scrolling */
7418 fillchar = ' ';
7419 attr = 0;
7420 }
7421
7422# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007423 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007424# endif
7425 }
7426 else
7427 {
7428 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007429 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007430 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007431 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007432# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007433 use_sandbox = was_set_insecurely((char_u *)"statusline",
7434 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007435# endif
7436 }
7437
Bram Moolenaar53f81742017-09-22 14:35:51 +02007438 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007439 }
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007442 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
Bram Moolenaar61452852011-02-01 18:01:11 +01007444 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7445 * the cursor away and back. */
7446 ewp = wp == NULL ? curwin : wp;
7447 p_crb_save = ewp->w_p_crb;
7448 ewp->w_p_crb = FALSE;
7449
Bram Moolenaar362f3562009-11-03 16:20:34 +00007450 /* Make a copy, because the statusline may include a function call that
7451 * might change the option value and free the memory. */
7452 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007453 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007454 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007455 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007456 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007457 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007459 /* Make all characters printable. */
7460 p = transstr(buf);
7461 if (p != NULL)
7462 {
7463 vim_strncpy(buf, p, sizeof(buf) - 1);
7464 vim_free(p);
7465 }
7466
7467 /* fill up with "fillchar" */
7468 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007469 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 ++width;
7473 }
7474 buf[len] = NUL;
7475
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007476 /*
7477 * Draw each snippet with the specified highlighting.
7478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 curattr = attr;
7480 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007481 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007483 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 screen_puts_len(p, len, row, col, curattr);
7485 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007486 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007488 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007490 else if (hltab[n].userhl < 0)
7491 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007492#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007493 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7494 && wp->w_status_height != 0)
7495 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007496 else if (wp != NULL && bt_terminal(wp->w_buffer)
7497 && wp->w_status_height != 0)
7498 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007499#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007500 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007501 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007503 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007506
7507 if (wp == NULL)
7508 {
7509 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7510 col = 0;
7511 len = 0;
7512 p = buf;
7513 fillchar = 0;
7514 for (n = 0; tabtab[n].start != NULL; n++)
7515 {
7516 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7517 while (col < len)
7518 TabPageIdxs[col++] = fillchar;
7519 p = tabtab[n].start;
7520 fillchar = tabtab[n].userhl;
7521 }
7522 while (col < Columns)
7523 TabPageIdxs[col++] = fillchar;
7524 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007525
7526theend:
7527 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528}
7529
7530#endif /* FEAT_STL_OPT */
7531
7532/*
7533 * Output a single character directly to the screen and update ScreenLines.
7534 */
7535 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007536screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 char_u buf[MB_MAXBYTES + 1];
7539
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007540 if (has_mbyte)
7541 buf[(*mb_char2bytes)(c, buf)] = NUL;
7542 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007543 {
7544 buf[0] = c;
7545 buf[1] = NUL;
7546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 screen_puts(buf, row, col, attr);
7548}
7549
7550/*
7551 * Get a single character directly from ScreenLines into "bytes[]".
7552 * Also return its attribute in *attrp;
7553 */
7554 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007555screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556{
7557 unsigned off;
7558
7559 /* safety check */
7560 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7561 {
7562 off = LineOffset[row] + col;
7563 *attrp = ScreenAttrs[off];
7564 bytes[0] = ScreenLines[off];
7565 bytes[1] = NUL;
7566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 if (enc_utf8 && ScreenLinesUC[off] != 0)
7568 bytes[utfc_char2bytes(off, bytes)] = NUL;
7569 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7570 {
7571 bytes[0] = ScreenLines[off];
7572 bytes[1] = ScreenLines2[off];
7573 bytes[2] = NUL;
7574 }
7575 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7576 {
7577 bytes[1] = ScreenLines[off + 1];
7578 bytes[2] = NUL;
7579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
7581}
7582
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007583/*
7584 * Return TRUE if composing characters for screen posn "off" differs from
7585 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007586 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007587 */
7588 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007589screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007590{
7591 int i;
7592
7593 for (i = 0; i < Screen_mco; ++i)
7594 {
7595 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7596 return TRUE;
7597 if (u8cc[i] == 0)
7598 break;
7599 }
7600 return FALSE;
7601}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603/*
7604 * Put string '*text' on the screen at position 'row' and 'col', with
7605 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7606 * Note: only outputs within one row, message is truncated at screen boundary!
7607 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7608 */
7609 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007610screen_puts(
7611 char_u *text,
7612 int row,
7613 int col,
7614 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 screen_puts_len(text, -1, row, col, attr);
7617}
7618
7619/*
7620 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7621 * a NUL.
7622 */
7623 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007624screen_puts_len(
7625 char_u *text,
7626 int textlen,
7627 int row,
7628 int col,
7629 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
7631 unsigned off;
7632 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007633 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007635 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 int mbyte_blen = 1;
7637 int mbyte_cells = 1;
7638 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007639 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007641#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007643 int pc, nc, nc1;
7644 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007646 int force_redraw_this;
7647 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7651 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Bram Moolenaarc236c162008-07-13 17:41:49 +00007654 /* When drawing over the right halve of a double-wide char clear out the
7655 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007656 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007657#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007658 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007659#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007660 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007661 {
7662 ScreenLines[off - 1] = ' ';
7663 ScreenAttrs[off - 1] = 0;
7664 if (enc_utf8)
7665 {
7666 ScreenLinesUC[off - 1] = 0;
7667 ScreenLinesC[0][off - 1] = 0;
7668 }
7669 /* redraw the previous cell, make it empty */
7670 screen_char(off - 1, row, col - 1);
7671 /* force the cell at "col" to be redrawn */
7672 force_redraw_next = TRUE;
7673 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007674
Bram Moolenaar367329b2007-08-30 11:53:22 +00007675 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007676 while (col < screen_Columns
7677 && (len < 0 || (int)(ptr - text) < len)
7678 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {
7680 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 /* check if this is the first byte of a multibyte */
7682 if (has_mbyte)
7683 {
7684 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007685 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007687 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7689 mbyte_cells = 1;
7690 else if (enc_dbcs != 0)
7691 mbyte_cells = mbyte_blen;
7692 else /* enc_utf8 */
7693 {
7694 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007695 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 (int)((text + len) - ptr));
7697 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007698 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007700#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7702 {
7703 /* Do Arabic shaping. */
7704 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7705 {
7706 /* Past end of string to be displayed. */
7707 nc = NUL;
7708 nc1 = NUL;
7709 }
7710 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007711 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007712 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7713 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007714 nc1 = pcc[0];
7715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 pc = prev_c;
7717 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007718 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720 else
7721 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007722#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007723 if (col + mbyte_cells > screen_Columns)
7724 {
7725 /* Only 1 cell left, but character requires 2 cells:
7726 * display a '>' in the last column to avoid wrapping. */
7727 c = '>';
7728 mbyte_cells = 1;
7729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007733 force_redraw_this = force_redraw_next;
7734 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007735
7736 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 || (mbyte_cells == 2
7738 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7739 || (enc_dbcs == DBCS_JPNU
7740 && c == 0x8e
7741 && ScreenLines2[off] != ptr[1])
7742 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007743 && (ScreenLinesUC[off] !=
7744 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7745 || (ScreenLinesUC[off] != 0
7746 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007748 || exmode_active;
7749
Bram Moolenaara12a1612019-01-24 16:39:02 +01007750 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752#if defined(FEAT_GUI) || defined(UNIX)
7753 /* The bold trick makes a single row of pixels appear in the next
7754 * character. When a bold character is removed, the next
7755 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007756 * and for some xterms. */
7757 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758# ifdef FEAT_GUI
7759 gui.in_use
7760# endif
7761# if defined(FEAT_GUI) && defined(UNIX)
7762 ||
7763# endif
7764# ifdef UNIX
7765 term_is_xterm
7766# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007767 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007769 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007771 if (n > HL_ALL)
7772 n = syn_attr2attr(n);
7773 if (n & HL_BOLD)
7774 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 /* When at the end of the text and overwriting a two-cell
7778 * character with a one-cell character, need to clear the next
7779 * cell. Also when overwriting the left halve of a two-cell char
7780 * with the right halve of a two-cell char. Do this only once
7781 * (mb_off2cells() may return 2 on the right halve). */
7782 if (clear_next_cell)
7783 clear_next_cell = FALSE;
7784 else if (has_mbyte
7785 && (len < 0 ? ptr[mbyte_blen] == NUL
7786 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007787 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007789 && (*mb_off2cells)(off, max_off) == 1
7790 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 clear_next_cell = TRUE;
7792
7793 /* Make sure we never leave a second byte of a double-byte behind,
7794 * it confuses mb_off2cells(). */
7795 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007796 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007798 && (*mb_off2cells)(off, max_off) == 1
7799 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 ScreenLines[off] = c;
7802 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 if (enc_utf8)
7804 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007805 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 ScreenLinesUC[off] = 0;
7807 else
7808 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007809 int i;
7810
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007812 for (i = 0; i < Screen_mco; ++i)
7813 {
7814 ScreenLinesC[i][off] = u8cc[i];
7815 if (u8cc[i] == 0)
7816 break;
7817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819 if (mbyte_cells == 2)
7820 {
7821 ScreenLines[off + 1] = 0;
7822 ScreenAttrs[off + 1] = attr;
7823 }
7824 screen_char(off, row, col);
7825 }
7826 else if (mbyte_cells == 2)
7827 {
7828 ScreenLines[off + 1] = ptr[1];
7829 ScreenAttrs[off + 1] = attr;
7830 screen_char_2(off, row, col);
7831 }
7832 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7833 {
7834 ScreenLines2[off] = ptr[1];
7835 screen_char(off, row, col);
7836 }
7837 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 screen_char(off, row, col);
7839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 if (has_mbyte)
7841 {
7842 off += mbyte_cells;
7843 col += mbyte_cells;
7844 ptr += mbyte_blen;
7845 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007846 {
7847 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007849 len = -1;
7850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 }
7852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {
7854 ++off;
7855 ++col;
7856 ++ptr;
7857 }
7858 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007859
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007860 /* If we detected the next character needs to be redrawn, but the text
7861 * doesn't extend up to there, update the character here. */
7862 if (force_redraw_next && col < screen_Columns)
7863 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007864 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7865 screen_char_2(off, row, col);
7866 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007867 screen_char(off, row, col);
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869}
7870
7871#ifdef FEAT_SEARCH_EXTRA
7872/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007873 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 */
7875 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007876start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 if (p_hls && !no_hlsearch)
7879 {
7880 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007881 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007882# ifdef FEAT_RELTIME
7883 /* Set the time limit to 'redrawtime'. */
7884 profile_setlimit(p_rdt, &search_hl.tm);
7885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 }
7887}
7888
7889/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007890 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 */
7892 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007893end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 if (search_hl.rm.regprog != NULL)
7896 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007897 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 search_hl.rm.regprog = NULL;
7899 }
7900}
7901
7902/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007903 * Init for calling prepare_search_hl().
7904 */
7905 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007906init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007907{
7908 matchitem_T *cur;
7909
7910 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7911 * match */
7912 cur = wp->w_match_head;
7913 while (cur != NULL)
7914 {
7915 cur->hl.rm = cur->match;
7916 if (cur->hlg_id == 0)
7917 cur->hl.attr = 0;
7918 else
7919 cur->hl.attr = syn_id2attr(cur->hlg_id);
7920 cur->hl.buf = wp->w_buffer;
7921 cur->hl.lnum = 0;
7922 cur->hl.first_lnum = 0;
7923# ifdef FEAT_RELTIME
7924 /* Set the time limit to 'redrawtime'. */
7925 profile_setlimit(p_rdt, &(cur->hl.tm));
7926# endif
7927 cur = cur->next;
7928 }
7929 search_hl.buf = wp->w_buffer;
7930 search_hl.lnum = 0;
7931 search_hl.first_lnum = 0;
7932 /* time limit is set at the toplevel, for all windows */
7933}
7934
7935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 * Advance to the match in window "wp" line "lnum" or past it.
7937 */
7938 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007939prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007941 matchitem_T *cur; /* points to the match list */
7942 match_T *shl; /* points to search_hl or a match */
7943 int shl_flag; /* flag to indicate whether search_hl
7944 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007945 int pos_inprogress; /* marks that position match search is
7946 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 int n;
7948
7949 /*
7950 * When using a multi-line pattern, start searching at the top
7951 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007952 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007954 cur = wp->w_match_head;
7955 shl_flag = FALSE;
7956 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007958 if (shl_flag == FALSE)
7959 {
7960 shl = &search_hl;
7961 shl_flag = TRUE;
7962 }
7963 else
7964 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 if (shl->rm.regprog != NULL
7966 && shl->lnum == 0
7967 && re_multiline(shl->rm.regprog))
7968 {
7969 if (shl->first_lnum == 0)
7970 {
7971# ifdef FEAT_FOLDING
7972 for (shl->first_lnum = lnum;
7973 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7974 if (hasFoldingWin(wp, shl->first_lnum - 1,
7975 NULL, NULL, TRUE, NULL))
7976 break;
7977# else
7978 shl->first_lnum = wp->w_topline;
7979# endif
7980 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007981 if (cur != NULL)
7982 cur->pos.cur = 0;
7983 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007985 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7986 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007988 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7989 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007990 pos_inprogress = cur == NULL || cur->pos.cur == 0
7991 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 if (shl->lnum != 0)
7993 {
7994 shl->first_lnum = shl->lnum
7995 + shl->rm.endpos[0].lnum
7996 - shl->rm.startpos[0].lnum;
7997 n = shl->rm.endpos[0].col;
7998 }
7999 else
8000 {
8001 ++shl->first_lnum;
8002 n = 0;
8003 }
8004 }
8005 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008006 if (shl != &search_hl && cur != NULL)
8007 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 }
8009}
8010
8011/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008012 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 * Uses shl->buf.
8014 * Sets shl->lnum and shl->rm contents.
8015 * Note: Assumes a previous match is always before "lnum", unless
8016 * shl->lnum is zero.
8017 * Careful: Any pointers for buffer lines will become invalid.
8018 */
8019 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008020next_search_hl(
8021 win_T *win,
8022 match_T *shl, /* points to search_hl or a match */
8023 linenr_T lnum,
8024 colnr_T mincol, /* minimal column for a match */
8025 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 linenr_T l;
8028 colnr_T matchcol;
8029 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008030 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008032 // for :{range}s/pat only highlight inside the range
8033 if (lnum < search_first_line || lnum > search_last_line)
8034 {
8035 shl->lnum = 0;
8036 return;
8037 }
8038
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (shl->lnum != 0)
8040 {
8041 /* Check for three situations:
8042 * 1. If the "lnum" is below a previous match, start a new search.
8043 * 2. If the previous match includes "mincol", use it.
8044 * 3. Continue after the previous match.
8045 */
8046 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8047 if (lnum > l)
8048 shl->lnum = 0;
8049 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8050 return;
8051 }
8052
8053 /*
8054 * Repeat searching for a match until one is found that includes "mincol"
8055 * or none is found in this line.
8056 */
8057 called_emsg = FALSE;
8058 for (;;)
8059 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008060#ifdef FEAT_RELTIME
8061 /* Stop searching after passing the time limit. */
8062 if (profile_passed_limit(&(shl->tm)))
8063 {
8064 shl->lnum = 0; /* no match found in time */
8065 break;
8066 }
8067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 /* Three situations:
8069 * 1. No useful previous match: search from start of line.
8070 * 2. Not Vi compatible or empty match: continue at next character.
8071 * Break the loop if this is beyond the end of the line.
8072 * 3. Vi compatible searching: continue at end of previous match.
8073 */
8074 if (shl->lnum == 0)
8075 matchcol = 0;
8076 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8077 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008078 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008080 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008081
8082 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008083 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008084 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008086 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 shl->lnum = 0;
8088 break;
8089 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008090 if (has_mbyte)
8091 matchcol += mb_ptr2len(ml);
8092 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008093 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
8095 else
8096 matchcol = shl->rm.endpos[0].col;
8097
8098 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008099 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008101 /* Remember whether shl->rm is using a copy of the regprog in
8102 * cur->match. */
8103 int regprog_is_copy = (shl != &search_hl && cur != NULL
8104 && shl == &cur->hl
8105 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008106 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008107
Bram Moolenaarb3414592014-06-17 17:48:32 +02008108 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8109 matchcol,
8110#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008111 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008112#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008113 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008114#endif
8115 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008116 /* Copy the regprog, in case it got freed and recompiled. */
8117 if (regprog_is_copy)
8118 cur->match.regprog = cur->hl.rm.regprog;
8119
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008120 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008121 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008122 /* Error while handling regexp: stop using this regexp. */
8123 if (shl == &search_hl)
8124 {
8125 /* don't free regprog in the match list, it's a copy */
8126 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008127 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008128 }
8129 shl->rm.regprog = NULL;
8130 shl->lnum = 0;
8131 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8132 message */
8133 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008134 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008135 }
8136 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008137 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008138 else
8139 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 if (nmatched == 0)
8141 {
8142 shl->lnum = 0; /* no match found */
8143 break;
8144 }
8145 if (shl->rm.startpos[0].lnum > 0
8146 || shl->rm.startpos[0].col >= mincol
8147 || nmatched > 1
8148 || shl->rm.endpos[0].col > mincol)
8149 {
8150 shl->lnum += shl->rm.startpos[0].lnum;
8151 break; /* useful match found */
8152 }
8153 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008154
8155 // Restore called_emsg for assert_fails().
8156 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
Bram Moolenaar85077472016-10-16 14:35:48 +02008159/*
8160 * If there is a match fill "shl" and return one.
8161 * Return zero otherwise.
8162 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008163 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008164next_search_hl_pos(
8165 match_T *shl, /* points to a match */
8166 linenr_T lnum,
8167 posmatch_T *posmatch, /* match positions */
8168 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008169{
8170 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008171 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172
Bram Moolenaarb3414592014-06-17 17:48:32 +02008173 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8174 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008175 llpos_T *pos = &posmatch->pos[i];
8176
8177 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008179 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008181 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008182 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008183 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008185 /* if this match comes before the one at "found" then swap
8186 * them */
8187 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008189 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008190
Bram Moolenaar85077472016-10-16 14:35:48 +02008191 *pos = posmatch->pos[found];
8192 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193 }
8194 }
8195 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008196 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008197 }
8198 }
8199 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008200 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008201 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008202 colnr_T start = posmatch->pos[found].col == 0
8203 ? 0 : posmatch->pos[found].col - 1;
8204 colnr_T end = posmatch->pos[found].col == 0
8205 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008206
Bram Moolenaar85077472016-10-16 14:35:48 +02008207 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008208 shl->rm.startpos[0].lnum = 0;
8209 shl->rm.startpos[0].col = start;
8210 shl->rm.endpos[0].lnum = 0;
8211 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008212 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008213 posmatch->cur = found + 1;
8214 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008215 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008216 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008217}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008218#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008221screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 attrentry_T *aep = NULL;
8224
8225 screen_attr = attr;
8226 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008227#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 && termcap_active
8229#endif
8230 )
8231 {
8232#ifdef FEAT_GUI
8233 if (gui.in_use)
8234 {
8235 char buf[20];
8236
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008237 /* The GUI handles this internally. */
8238 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 OUT_STR(buf);
8240 }
8241 else
8242#endif
8243 {
8244 if (attr > HL_ALL) /* special HL attr. */
8245 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008246 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 aep = syn_cterm_attr2entry(attr);
8248 else
8249 aep = syn_term_attr2entry(attr);
8250 if (aep == NULL) /* did ":syntax clear" */
8251 attr = 0;
8252 else
8253 attr = aep->ae_attr;
8254 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008255 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008257 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008258#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008259 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8260 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8261 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008262#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008263 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008264 /* If the Normal FG color has BOLD attribute and the new HL
8265 * has a FG color defined, clear BOLD. */
8266 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008267 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008269 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008270 out_str(T_UCS);
8271 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008272 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8273 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008275 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008277 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008279 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008280 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281
8282 /*
8283 * Output the color or start string after bold etc., in case the
8284 * bold etc. override the color setting.
8285 */
8286 if (aep != NULL)
8287 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008288#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008289 /* When 'termguicolors' is set but fg or bg is unset,
8290 * fall back to the cterm colors. This helps for SpellBad,
8291 * where the GUI uses a red undercurl. */
8292 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008294 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008295 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008296 }
8297 else
8298#endif
8299 if (t_colors > 1)
8300 {
8301 if (aep->ae_u.cterm.fg_color)
8302 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8303 }
8304#ifdef FEAT_TERMGUICOLORS
8305 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8306 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008307 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008308 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
8310 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008311#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008312 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008314 if (aep->ae_u.cterm.bg_color)
8315 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8316 }
8317
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008318 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008319 {
8320 if (aep->ae_u.term.start != NULL)
8321 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 }
8323 }
8324 }
8325 }
8326}
8327
8328 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008329screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 int do_ME = FALSE; /* output T_ME code */
8332
8333 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008334#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 && termcap_active
8336#endif
8337 )
8338 {
8339#ifdef FEAT_GUI
8340 if (gui.in_use)
8341 {
8342 char buf[20];
8343
8344 /* use internal GUI code */
8345 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8346 OUT_STR(buf);
8347 }
8348 else
8349#endif
8350 {
8351 if (screen_attr > HL_ALL) /* special HL attr. */
8352 {
8353 attrentry_T *aep;
8354
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008355 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {
8357 /*
8358 * Assume that t_me restores the original colors!
8359 */
8360 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008361 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008362#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008363 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8364 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8365 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008366#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008367 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008368#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008369 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8370 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8371 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008372#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008373 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 do_ME = TRUE;
8375 }
8376 else
8377 {
8378 aep = syn_term_attr2entry(screen_attr);
8379 if (aep != NULL && aep->ae_u.term.stop != NULL)
8380 {
8381 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8382 do_ME = TRUE;
8383 else
8384 out_str(aep->ae_u.term.stop);
8385 }
8386 }
8387 if (aep == NULL) /* did ":syntax clear" */
8388 screen_attr = 0;
8389 else
8390 screen_attr = aep->ae_attr;
8391 }
8392
8393 /*
8394 * Often all ending-codes are equal to T_ME. Avoid outputting the
8395 * same sequence several times.
8396 */
8397 if (screen_attr & HL_STANDOUT)
8398 {
8399 if (STRCMP(T_SE, T_ME) == 0)
8400 do_ME = TRUE;
8401 else
8402 out_str(T_SE);
8403 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008404 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008405 {
8406 if (STRCMP(T_UCE, T_ME) == 0)
8407 do_ME = TRUE;
8408 else
8409 out_str(T_UCE);
8410 }
8411 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008412 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {
8414 if (STRCMP(T_UE, T_ME) == 0)
8415 do_ME = TRUE;
8416 else
8417 out_str(T_UE);
8418 }
8419 if (screen_attr & HL_ITALIC)
8420 {
8421 if (STRCMP(T_CZR, T_ME) == 0)
8422 do_ME = TRUE;
8423 else
8424 out_str(T_CZR);
8425 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008426 if (screen_attr & HL_STRIKETHROUGH)
8427 {
8428 if (STRCMP(T_STE, T_ME) == 0)
8429 do_ME = TRUE;
8430 else
8431 out_str(T_STE);
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8434 out_str(T_ME);
8435
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008436#ifdef FEAT_TERMGUICOLORS
8437 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008439 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008440 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008441 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008442 term_bg_rgb_color(cterm_normal_bg_gui_color);
8443 }
8444 else
8445#endif
8446 {
8447 if (t_colors > 1)
8448 {
8449 /* set Normal cterm colors */
8450 if (cterm_normal_fg_color != 0)
8451 term_fg_color(cterm_normal_fg_color - 1);
8452 if (cterm_normal_bg_color != 0)
8453 term_bg_color(cterm_normal_bg_color - 1);
8454 if (cterm_normal_fg_bold)
8455 out_str(T_MD);
8456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 }
8458 }
8459 }
8460 screen_attr = 0;
8461}
8462
8463/*
8464 * Reset the colors for a cterm. Used when leaving Vim.
8465 * The machine specific code may override this again.
8466 */
8467 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008468reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008470 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {
8472 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008473#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008474 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8475 || cterm_normal_bg_gui_color != INVALCOLOR)
8476 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008477#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 out_str(T_OP);
8482 screen_attr = -1;
8483 }
8484 if (cterm_normal_fg_bold)
8485 {
8486 out_str(T_ME);
8487 screen_attr = -1;
8488 }
8489 }
8490}
8491
8492/*
8493 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8494 * using the attributes from ScreenAttrs["off"].
8495 */
8496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008497screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
8499 int attr;
8500
8501 /* Check for illegal values, just in case (could happen just after
8502 * resizing). */
8503 if (row >= screen_Rows || col >= screen_Columns)
8504 return;
8505
Bram Moolenaarae654382019-01-17 21:09:05 +01008506#ifdef FEAT_INS_EXPAND
8507 if (pum_under_menu(row, col))
8508 return;
8509#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008510 /* Outputting a character in the last cell on the screen may scroll the
8511 * screen up. Only do it when the "xn" termcap property is set, otherwise
8512 * mark the character invalid (update it when scrolled up). */
8513 if (*T_XN == NUL
8514 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#ifdef FEAT_RIGHTLEFT
8516 /* account for first command-line character in rightleft mode */
8517 && !cmdmsg_rl
8518#endif
8519 )
8520 {
8521 ScreenAttrs[off] = (sattr_T)-1;
8522 return;
8523 }
8524
8525 /*
8526 * Stop highlighting first, so it's easier to move the cursor.
8527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (screen_char_attr != 0)
8529 attr = screen_char_attr;
8530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 attr = ScreenAttrs[off];
8532 if (screen_attr != attr)
8533 screen_stop_highlight();
8534
8535 windgoto(row, col);
8536
8537 if (screen_attr != attr)
8538 screen_start_highlight(attr);
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (enc_utf8 && ScreenLinesUC[off] != 0)
8541 {
8542 char_u buf[MB_MAXBYTES + 1];
8543
Bram Moolenaarcb070082016-04-02 22:14:51 +02008544 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008545 {
8546 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008547#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008548 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008549#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008550 )
8551 {
8552 /* Clear the two screen cells. If the character is actually
8553 * single width it won't change the second cell. */
8554 out_str((char_u *)" ");
8555 term_windgoto(row, col);
8556 }
8557 /* not sure where the cursor is after drawing the ambiguous width
8558 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008559 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008560 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008561 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008563
8564 /* Convert the UTF-8 character to bytes and write it. */
8565 buf[utfc_char2bytes(off, buf)] = NUL;
8566 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
8568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 /* double-byte character in single-width cell */
8573 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8574 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576
8577 screen_cur_col++;
8578}
8579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580/*
8581 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8582 * on the screen at position 'row' and 'col'.
8583 * The attributes of the first byte is used for all. This is required to
8584 * output the two bytes of a double-byte character with nothing in between.
8585 */
8586 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008587screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
8589 /* Check for illegal values (could be wrong when screen was resized). */
8590 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8591 return;
8592
8593 /* Outputting the last character on the screen may scrollup the screen.
8594 * Don't to it! Mark the character invalid (update it when scrolled up) */
8595 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8596 {
8597 ScreenAttrs[off] = (sattr_T)-1;
8598 return;
8599 }
8600
8601 /* Output the first byte normally (positions the cursor), then write the
8602 * second byte directly. */
8603 screen_char(off, row, col);
8604 out_char(ScreenLines[off + 1]);
8605 ++screen_cur_col;
8606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608/*
8609 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8610 * This uses the contents of ScreenLines[] and doesn't change it.
8611 */
8612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008613screen_draw_rectangle(
8614 int row,
8615 int col,
8616 int height,
8617 int width,
8618 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 int r, c;
8621 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008622 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008624 /* Can't use ScreenLines unless initialized */
8625 if (ScreenLines == NULL)
8626 return;
8627
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 if (invert)
8629 screen_char_attr = HL_INVERSE;
8630 for (r = row; r < row + height; ++r)
8631 {
8632 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008633 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 for (c = col; c < col + width; ++c)
8635 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008636 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 {
8638 screen_char_2(off + c, r, c);
8639 ++c;
8640 }
8641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {
8643 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008644 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 }
8647 }
8648 }
8649 screen_char_attr = 0;
8650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652/*
8653 * Redraw the characters for a vertically split window.
8654 */
8655 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008656redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657{
8658 int col;
8659 int width;
8660
8661# ifdef FEAT_CLIPBOARD
8662 clip_may_clear_selection(row, end - 1);
8663# endif
8664
8665 if (wp == NULL)
8666 {
8667 col = 0;
8668 width = Columns;
8669 }
8670 else
8671 {
8672 col = wp->w_wincol;
8673 width = wp->w_width;
8674 }
8675 screen_draw_rectangle(row, col, end - row, width, FALSE);
8676}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008678 static void
8679space_to_screenline(int off, int attr)
8680{
8681 ScreenLines[off] = ' ';
8682 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008683 if (enc_utf8)
8684 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008685}
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687/*
8688 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8689 * with character 'c1' in first column followed by 'c2' in the other columns.
8690 * Use attributes 'attr'.
8691 */
8692 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008693screen_fill(
8694 int start_row,
8695 int end_row,
8696 int start_col,
8697 int end_col,
8698 int c1,
8699 int c2,
8700 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 int row;
8703 int col;
8704 int off;
8705 int end_off;
8706 int did_delete;
8707 int c;
8708 int norm_term;
8709#if defined(FEAT_GUI) || defined(UNIX)
8710 int force_next = FALSE;
8711#endif
8712
8713 if (end_row > screen_Rows) /* safety check */
8714 end_row = screen_Rows;
8715 if (end_col > screen_Columns) /* safety check */
8716 end_col = screen_Columns;
8717 if (ScreenLines == NULL
8718 || start_row >= end_row
8719 || start_col >= end_col) /* nothing to do */
8720 return;
8721
8722 /* it's a "normal" terminal when not in a GUI or cterm */
8723 norm_term = (
8724#ifdef FEAT_GUI
8725 !gui.in_use &&
8726#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008727 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 for (row = start_row; row < end_row; ++row)
8729 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008730 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008731#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008732 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008733#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008734 )
8735 {
8736 /* When drawing over the right halve of a double-wide char clear
8737 * out the left halve. When drawing over the left halve of a
8738 * double wide-char clear out the right halve. Only needed in a
8739 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008740 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008741 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008742 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008743 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 /*
8746 * Try to use delete-line termcap code, when no attributes or in a
8747 * "normal" terminal, where a bold/italic space is just a
8748 * space.
8749 */
8750 did_delete = FALSE;
8751 if (c2 == ' '
8752 && end_col == Columns
8753 && can_clear(T_CE)
8754 && (attr == 0
8755 || (norm_term
8756 && attr <= HL_ALL
8757 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8758 {
8759 /*
8760 * check if we really need to clear something
8761 */
8762 col = start_col;
8763 if (c1 != ' ') /* don't clear first char */
8764 ++col;
8765
8766 off = LineOffset[row] + col;
8767 end_off = LineOffset[row] + end_col;
8768
8769 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (enc_utf8)
8771 while (off < end_off && ScreenLines[off] == ' '
8772 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8773 ++off;
8774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 while (off < end_off && ScreenLines[off] == ' '
8776 && ScreenAttrs[off] == 0)
8777 ++off;
8778 if (off < end_off) /* something to be cleared */
8779 {
8780 col = off - LineOffset[row];
8781 screen_stop_highlight();
8782 term_windgoto(row, col);/* clear rest of this screen line */
8783 out_str(T_CE);
8784 screen_start(); /* don't know where cursor is now */
8785 col = end_col - col;
8786 while (col--) /* clear chars in ScreenLines */
8787 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008788 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 ++off;
8790 }
8791 }
8792 did_delete = TRUE; /* the chars are cleared now */
8793 }
8794
8795 off = LineOffset[row] + start_col;
8796 c = c1;
8797 for (col = start_col; col < end_col; ++col)
8798 {
8799 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008800 || (enc_utf8 && (int)ScreenLinesUC[off]
8801 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 || ScreenAttrs[off] != attr
8803#if defined(FEAT_GUI) || defined(UNIX)
8804 || force_next
8805#endif
8806 )
8807 {
8808#if defined(FEAT_GUI) || defined(UNIX)
8809 /* The bold trick may make a single row of pixels appear in
8810 * the next character. When a bold character is removed, the
8811 * next character should be redrawn too. This happens for our
8812 * own GUI and for some xterms. */
8813 if (
8814# ifdef FEAT_GUI
8815 gui.in_use
8816# endif
8817# if defined(FEAT_GUI) && defined(UNIX)
8818 ||
8819# endif
8820# ifdef UNIX
8821 term_is_xterm
8822# endif
8823 )
8824 {
8825 if (ScreenLines[off] != ' '
8826 && (ScreenAttrs[off] > HL_ALL
8827 || ScreenAttrs[off] & HL_BOLD))
8828 force_next = TRUE;
8829 else
8830 force_next = FALSE;
8831 }
8832#endif
8833 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (enc_utf8)
8835 {
8836 if (c >= 0x80)
8837 {
8838 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008839 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
8841 else
8842 ScreenLinesUC[off] = 0;
8843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 ScreenAttrs[off] = attr;
8845 if (!did_delete || c != ' ')
8846 screen_char(off, row, col);
8847 }
8848 ++off;
8849 if (col == start_col)
8850 {
8851 if (did_delete)
8852 break;
8853 c = c2;
8854 }
8855 }
8856 if (end_col == Columns)
8857 LineWraps[row] = FALSE;
8858 if (row == Rows - 1) /* overwritten the command line */
8859 {
8860 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008861 if (start_col == 0 && end_col == Columns
8862 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008864 if (start_col == 0)
8865 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 }
8867 }
8868}
8869
8870/*
8871 * Check if there should be a delay. Used before clearing or redrawing the
8872 * screen or the command line.
8873 */
8874 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008875check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8878 && !did_wait_return
8879 && emsg_silent == 0)
8880 {
8881 out_flush();
8882 ui_delay(1000L, TRUE);
8883 emsg_on_display = FALSE;
8884 if (check_msg_scroll)
8885 msg_scroll = FALSE;
8886 }
8887}
8888
8889/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008890 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8891 */
8892 static void
8893clear_TabPageIdxs(void)
8894{
8895 int scol;
8896
8897 for (scol = 0; scol < Columns; ++scol)
8898 TabPageIdxs[scol] = 0;
8899}
8900
8901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008903 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 * Returns TRUE if there is a valid screen to write to.
8905 * Returns FALSE when starting up and screen not initialized yet.
8906 */
8907 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008908screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008910 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 return (ScreenLines != NULL);
8912}
8913
8914/*
8915 * Resize the shell to Rows and Columns.
8916 * Allocate ScreenLines[] and associated items.
8917 *
8918 * There may be some time between setting Rows and Columns and (re)allocating
8919 * ScreenLines[]. This happens when starting up and when (manually) changing
8920 * the shell size. Always use screen_Rows and screen_Columns to access items
8921 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8922 * final size of the shell is needed.
8923 */
8924 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008925screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 int new_row, old_row;
8928#ifdef FEAT_GUI
8929 int old_Rows;
8930#endif
8931 win_T *wp;
8932 int outofmem = FALSE;
8933 int len;
8934 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008936 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008938 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 sattr_T *new_ScreenAttrs;
8940 unsigned *new_LineOffset;
8941 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008942 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008943 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008945 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008946 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008948retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 /*
8950 * Allocation of the screen buffers is done only when the size changes and
8951 * when Rows and Columns have been set and we have started doing full
8952 * screen stuff.
8953 */
8954 if ((ScreenLines != NULL
8955 && Rows == screen_Rows
8956 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 && enc_utf8 == (ScreenLinesUC != NULL)
8958 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008959 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 || Rows == 0
8961 || Columns == 0
8962 || (!full_screen && ScreenLines == NULL))
8963 return;
8964
8965 /*
8966 * It's possible that we produce an out-of-memory message below, which
8967 * will cause this function to be called again. To break the loop, just
8968 * return here.
8969 */
8970 if (entered)
8971 return;
8972 entered = TRUE;
8973
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008974 /*
8975 * Note that the window sizes are updated before reallocating the arrays,
8976 * thus we must not redraw here!
8977 */
8978 ++RedrawingDisabled;
8979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 win_new_shellsize(); /* fit the windows in the new sized shell */
8981
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 comp_col(); /* recompute columns for shown command and ruler */
8983
8984 /*
8985 * We're changing the size of the screen.
8986 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8987 * - Move lines from the old arrays into the new arrays, clear extra
8988 * lines (unless the screen is going to be cleared).
8989 * - Free the old arrays.
8990 *
8991 * If anything fails, make ScreenLines NULL, so we don't do anything!
8992 * Continuing with the old ScreenLines may result in a crash, because the
8993 * size is wrong.
8994 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008995 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008997 if (aucmd_win != NULL)
8998 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008999#ifdef FEAT_TEXT_PROP
9000 // global popup windows
9001 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9002 win_free_lsize(wp);
9003 // tab-local popup windows
9004 FOR_ALL_TABPAGES(tp)
9005 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9006 win_free_lsize(wp);
9007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009009 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009010 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 if (enc_utf8)
9012 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009013 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009014 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009015 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9016 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
9018 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009019 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9020 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9021 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9022 new_LineWraps = LALLOC_MULT(char_u, Rows);
9023 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009025 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 {
9027 if (win_alloc_lines(wp) == FAIL)
9028 {
9029 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009030 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 }
9032 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009033 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9034 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009035 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009036#ifdef FEAT_TEXT_PROP
9037 // global popup windows
9038 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9039 if (win_alloc_lines(wp) == FAIL)
9040 {
9041 outofmem = TRUE;
9042 goto give_up;
9043 }
9044 // tab-local popup windows
9045 FOR_ALL_TABPAGES(tp)
9046 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9047 if (win_alloc_lines(wp) == FAIL)
9048 {
9049 outofmem = TRUE;
9050 goto give_up;
9051 }
9052#endif
9053
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009054give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009056 for (i = 0; i < p_mco; ++i)
9057 if (new_ScreenLinesC[i] == NULL)
9058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009060 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 || new_ScreenAttrs == NULL
9063 || new_LineOffset == NULL
9064 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009065 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 || outofmem)
9067 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009068 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009069 {
9070 /* guess the size */
9071 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9072
9073 /* Remember we did this to avoid getting outofmem messages over
9074 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009075 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009076 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009077 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009078 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009079 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009080 VIM_CLEAR(new_ScreenLinesC[i]);
9081 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009082 VIM_CLEAR(new_ScreenAttrs);
9083 VIM_CLEAR(new_LineOffset);
9084 VIM_CLEAR(new_LineWraps);
9085 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
9087 else
9088 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009089 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 for (new_row = 0; new_row < Rows; ++new_row)
9092 {
9093 new_LineOffset[new_row] = new_row * Columns;
9094 new_LineWraps[new_row] = FALSE;
9095
9096 /*
9097 * If the screen is not going to be cleared, copy as much as
9098 * possible from the old screen to the new one and clear the rest
9099 * (used when resizing the window at the "--more--" prompt or when
9100 * executing an external command, for the GUI).
9101 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009102 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 {
9104 (void)vim_memset(new_ScreenLines + new_row * Columns,
9105 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 if (enc_utf8)
9107 {
9108 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9109 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009110 for (i = 0; i < p_mco; ++i)
9111 (void)vim_memset(new_ScreenLinesC[i]
9112 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 0, (size_t)Columns * sizeof(u8char_T));
9114 }
9115 if (enc_dbcs == DBCS_JPNU)
9116 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9117 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9119 0, (size_t)Columns * sizeof(sattr_T));
9120 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009121 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
9123 if (screen_Columns < Columns)
9124 len = screen_Columns;
9125 else
9126 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009127 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009128 * may be invalid now. Also when p_mco changes. */
9129 if (!(enc_utf8 && ScreenLinesUC == NULL)
9130 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009131 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9132 ScreenLines + LineOffset[old_row],
9133 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009134 if (enc_utf8 && ScreenLinesUC != NULL
9135 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
9137 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9138 ScreenLinesUC + LineOffset[old_row],
9139 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009140 for (i = 0; i < p_mco; ++i)
9141 mch_memmove(new_ScreenLinesC[i]
9142 + new_LineOffset[new_row],
9143 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 (size_t)len * sizeof(u8char_T));
9145 }
9146 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9147 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9148 ScreenLines2 + LineOffset[old_row],
9149 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9151 ScreenAttrs + LineOffset[old_row],
9152 (size_t)len * sizeof(sattr_T));
9153 }
9154 }
9155 }
9156 /* Use the last line of the screen for the current line. */
9157 current_ScreenLine = new_ScreenLines + Rows * Columns;
9158 }
9159
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009160 free_screenlines();
9161
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009164 for (i = 0; i < p_mco; ++i)
9165 ScreenLinesC[i] = new_ScreenLinesC[i];
9166 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 ScreenAttrs = new_ScreenAttrs;
9169 LineOffset = new_LineOffset;
9170 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009171 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
9173 /* It's important that screen_Rows and screen_Columns reflect the actual
9174 * size of ScreenLines[]. Set them before calling anything. */
9175#ifdef FEAT_GUI
9176 old_Rows = screen_Rows;
9177#endif
9178 screen_Rows = Rows;
9179 screen_Columns = Columns;
9180
9181 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009182 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184#ifdef FEAT_GUI
9185 else if (gui.in_use
9186 && !gui.starting
9187 && ScreenLines != NULL
9188 && old_Rows != Rows)
9189 {
9190 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9191 /*
9192 * Adjust the position of the cursor, for when executing an external
9193 * command.
9194 */
9195 if (msg_row >= Rows) /* Rows got smaller */
9196 msg_row = Rows - 1; /* put cursor at last row */
9197 else if (Rows > old_Rows) /* Rows got bigger */
9198 msg_row += Rows - old_Rows; /* put cursor in same place */
9199 if (msg_col >= Columns) /* Columns got smaller */
9200 msg_col = Columns - 1; /* put cursor at last column */
9201 }
9202#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009203 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009206 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009207
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009208 /*
9209 * Do not apply autocommands more than 3 times to avoid an endless loop
9210 * in case applying autocommands always changes Rows or Columns.
9211 */
9212 if (starting == 0 && ++retry_count <= 3)
9213 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009214 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009215 /* In rare cases, autocommands may have altered Rows or Columns,
9216 * jump back to check if we need to allocate the screen again. */
9217 goto retry;
9218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219}
9220
9221 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009222free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009223{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009224 int i;
9225
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009226 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009227 for (i = 0; i < Screen_mco; ++i)
9228 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009229 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009230 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009231 vim_free(ScreenAttrs);
9232 vim_free(LineOffset);
9233 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009234 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009235}
9236
9237 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009238screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239{
9240 check_for_delay(FALSE);
9241 screenalloc(FALSE); /* allocate screen buffers if size changed */
9242 screenclear2(); /* clear the screen */
9243}
9244
9245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009246screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 int i;
9249
9250 if (starting == NO_SCREEN || ScreenLines == NULL
9251#ifdef FEAT_GUI
9252 || (gui.in_use && gui.starting)
9253#endif
9254 )
9255 return;
9256
9257#ifdef FEAT_GUI
9258 if (!gui.in_use)
9259#endif
9260 screen_attr = -1; /* force setting the Normal colors */
9261 screen_stop_highlight(); /* don't want highlighting here */
9262
9263#ifdef FEAT_CLIPBOARD
9264 /* disable selection without redrawing it */
9265 clip_scroll_selection(9999);
9266#endif
9267
9268 /* blank out ScreenLines */
9269 for (i = 0; i < Rows; ++i)
9270 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009271 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 LineWraps[i] = FALSE;
9273 }
9274
9275 if (can_clear(T_CL))
9276 {
9277 out_str(T_CL); /* clear the display */
9278 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009279 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 }
9281 else
9282 {
9283 /* can't clear the screen, mark all chars with invalid attributes */
9284 for (i = 0; i < Rows; ++i)
9285 lineinvalid(LineOffset[i], (int)Columns);
9286 clear_cmdline = TRUE;
9287 }
9288
9289 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9290
9291 win_rest_invalid(firstwin);
9292 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009293 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 if (must_redraw == CLEAR) /* no need to clear again */
9295 must_redraw = NOT_VALID;
9296 compute_cmdrow();
9297 msg_row = cmdline_row; /* put cursor on last line for messages */
9298 msg_col = 0;
9299 screen_start(); /* don't know where cursor is now */
9300 msg_scrolled = 0; /* can't scroll back */
9301 msg_didany = FALSE;
9302 msg_didout = FALSE;
9303}
9304
9305/*
9306 * Clear one line in ScreenLines.
9307 */
9308 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009309lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310{
9311 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 if (enc_utf8)
9313 (void)vim_memset(ScreenLinesUC + off, 0,
9314 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009315 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316}
9317
9318/*
9319 * Mark one line in ScreenLines invalid by setting the attributes to an
9320 * invalid value.
9321 */
9322 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009323lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324{
9325 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9326}
9327
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328/*
9329 * Copy part of a Screenline for vertically split window "wp".
9330 */
9331 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009332linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 unsigned off_to = LineOffset[to] + wp->w_wincol;
9335 unsigned off_from = LineOffset[from] + wp->w_wincol;
9336
9337 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9338 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 if (enc_utf8)
9340 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009341 int i;
9342
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9344 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009345 for (i = 0; i < p_mco; ++i)
9346 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9347 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 }
9349 if (enc_dbcs == DBCS_JPNU)
9350 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9351 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9353 wp->w_width * sizeof(sattr_T));
9354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
9356/*
9357 * Return TRUE if clearing with term string "p" would work.
9358 * It can't work when the string is empty or it won't set the right background.
9359 */
9360 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009361can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 return (*p != NUL && (t_colors <= 1
9364#ifdef FEAT_GUI
9365 || gui.in_use
9366#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009367#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009368 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009369 || (!p_tgc && cterm_normal_bg_color == 0)
9370#else
9371 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009372#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009373 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374}
9375
9376/*
9377 * Reset cursor position. Use whenever cursor was moved because of outputting
9378 * something directly to the screen (shell commands) or a terminal control
9379 * code.
9380 */
9381 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009382screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 screen_cur_row = screen_cur_col = 9999;
9385}
9386
9387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 * Move the cursor to position "row","col" in the screen.
9389 * This tries to find the most efficient way to move, minimizing the number of
9390 * characters sent to the terminal.
9391 */
9392 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009393windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009395 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 int i;
9397 int plan;
9398 int cost;
9399 int wouldbe_col;
9400 int noinvcurs;
9401 char_u *bs;
9402 int goto_cost;
9403 int attr;
9404
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009405#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9407
9408#define PLAN_LE 1
9409#define PLAN_CR 2
9410#define PLAN_NL 3
9411#define PLAN_WRITE 4
9412 /* Can't use ScreenLines unless initialized */
9413 if (ScreenLines == NULL)
9414 return;
9415
9416 if (col != screen_cur_col || row != screen_cur_row)
9417 {
9418 /* Check for valid position. */
9419 if (row < 0) /* window without text lines? */
9420 row = 0;
9421 if (row >= screen_Rows)
9422 row = screen_Rows - 1;
9423 if (col >= screen_Columns)
9424 col = screen_Columns - 1;
9425
9426 /* check if no cursor movement is allowed in highlight mode */
9427 if (screen_attr && *T_MS == NUL)
9428 noinvcurs = HIGHL_COST;
9429 else
9430 noinvcurs = 0;
9431 goto_cost = GOTO_COST + noinvcurs;
9432
9433 /*
9434 * Plan how to do the positioning:
9435 * 1. Use CR to move it to column 0, same row.
9436 * 2. Use T_LE to move it a few columns to the left.
9437 * 3. Use NL to move a few lines down, column 0.
9438 * 4. Move a few columns to the right with T_ND or by writing chars.
9439 *
9440 * Don't do this if the cursor went beyond the last column, the cursor
9441 * position is unknown then (some terminals wrap, some don't )
9442 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009443 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 * characters to move the cursor to the right.
9445 */
9446 if (row >= screen_cur_row && screen_cur_col < Columns)
9447 {
9448 /*
9449 * If the cursor is in the same row, bigger col, we can use CR
9450 * or T_LE.
9451 */
9452 bs = NULL; /* init for GCC */
9453 attr = screen_attr;
9454 if (row == screen_cur_row && col < screen_cur_col)
9455 {
9456 /* "le" is preferred over "bc", because "bc" is obsolete */
9457 if (*T_LE)
9458 bs = T_LE; /* "cursor left" */
9459 else
9460 bs = T_BC; /* "backspace character (old) */
9461 if (*bs)
9462 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9463 else
9464 cost = 999;
9465 if (col + 1 < cost) /* using CR is less characters */
9466 {
9467 plan = PLAN_CR;
9468 wouldbe_col = 0;
9469 cost = 1; /* CR is just one character */
9470 }
9471 else
9472 {
9473 plan = PLAN_LE;
9474 wouldbe_col = col;
9475 }
9476 if (noinvcurs) /* will stop highlighting */
9477 {
9478 cost += noinvcurs;
9479 attr = 0;
9480 }
9481 }
9482
9483 /*
9484 * If the cursor is above where we want to be, we can use CR LF.
9485 */
9486 else if (row > screen_cur_row)
9487 {
9488 plan = PLAN_NL;
9489 wouldbe_col = 0;
9490 cost = (row - screen_cur_row) * 2; /* CR LF */
9491 if (noinvcurs) /* will stop highlighting */
9492 {
9493 cost += noinvcurs;
9494 attr = 0;
9495 }
9496 }
9497
9498 /*
9499 * If the cursor is in the same row, smaller col, just use write.
9500 */
9501 else
9502 {
9503 plan = PLAN_WRITE;
9504 wouldbe_col = screen_cur_col;
9505 cost = 0;
9506 }
9507
9508 /*
9509 * Check if any characters that need to be written have the
9510 * correct attributes. Also avoid UTF-8 characters.
9511 */
9512 i = col - wouldbe_col;
9513 if (i > 0)
9514 cost += i;
9515 if (cost < goto_cost && i > 0)
9516 {
9517 /*
9518 * Check if the attributes are correct without additionally
9519 * stopping highlighting.
9520 */
9521 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9522 while (i && *p++ == attr)
9523 --i;
9524 if (i != 0)
9525 {
9526 /*
9527 * Try if it works when highlighting is stopped here.
9528 */
9529 if (*--p == 0)
9530 {
9531 cost += noinvcurs;
9532 while (i && *p++ == 0)
9533 --i;
9534 }
9535 if (i != 0)
9536 cost = 999; /* different attributes, don't do it */
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 if (enc_utf8)
9539 {
9540 /* Don't use an UTF-8 char for positioning, it's slow. */
9541 for (i = wouldbe_col; i < col; ++i)
9542 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9543 {
9544 cost = 999;
9545 break;
9546 }
9547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 }
9549
9550 /*
9551 * We can do it without term_windgoto()!
9552 */
9553 if (cost < goto_cost)
9554 {
9555 if (plan == PLAN_LE)
9556 {
9557 if (noinvcurs)
9558 screen_stop_highlight();
9559 while (screen_cur_col > col)
9560 {
9561 out_str(bs);
9562 --screen_cur_col;
9563 }
9564 }
9565 else if (plan == PLAN_CR)
9566 {
9567 if (noinvcurs)
9568 screen_stop_highlight();
9569 out_char('\r');
9570 screen_cur_col = 0;
9571 }
9572 else if (plan == PLAN_NL)
9573 {
9574 if (noinvcurs)
9575 screen_stop_highlight();
9576 while (screen_cur_row < row)
9577 {
9578 out_char('\n');
9579 ++screen_cur_row;
9580 }
9581 screen_cur_col = 0;
9582 }
9583
9584 i = col - screen_cur_col;
9585 if (i > 0)
9586 {
9587 /*
9588 * Use cursor-right if it's one character only. Avoids
9589 * removing a line of pixels from the last bold char, when
9590 * using the bold trick in the GUI.
9591 */
9592 if (T_ND[0] != NUL && T_ND[1] == NUL)
9593 {
9594 while (i-- > 0)
9595 out_char(*T_ND);
9596 }
9597 else
9598 {
9599 int off;
9600
9601 off = LineOffset[row] + screen_cur_col;
9602 while (i-- > 0)
9603 {
9604 if (ScreenAttrs[off] != screen_attr)
9605 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 if (enc_dbcs == DBCS_JPNU
9609 && ScreenLines[off] == 0x8e)
9610 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 ++off;
9612 }
9613 }
9614 }
9615 }
9616 }
9617 else
9618 cost = 999;
9619
9620 if (cost >= goto_cost)
9621 {
9622 if (noinvcurs)
9623 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009624 if (row == screen_cur_row && (col > screen_cur_col)
9625 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 term_cursor_right(col - screen_cur_col);
9627 else
9628 term_windgoto(row, col);
9629 }
9630 screen_cur_row = row;
9631 screen_cur_col = col;
9632 }
9633}
9634
9635/*
9636 * Set cursor to its position in the current window.
9637 */
9638 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009639setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009641 setcursor_mayforce(FALSE);
9642}
9643
9644/*
9645 * Set cursor to its position in the current window.
9646 * When "force" is TRUE also when not redrawing.
9647 */
9648 void
9649setcursor_mayforce(int force)
9650{
9651 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 {
9653 validate_cursor();
9654 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009655 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009657 /* With 'rightleft' set and the cursor on a double-wide
9658 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009659 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9660 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009661 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009662 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663#endif
9664 curwin->w_wcol));
9665 }
9666}
9667
9668
9669/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009670 * Insert 'line_count' lines at 'row' in window 'wp'.
9671 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9672 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 * scrolling.
9674 * Returns FAIL if the lines are not inserted, OK for success.
9675 */
9676 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009677win_ins_lines(
9678 win_T *wp,
9679 int row,
9680 int line_count,
9681 int invalid,
9682 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 int did_delete;
9685 int nextrow;
9686 int lastrow;
9687 int retval;
9688
9689 if (invalid)
9690 wp->w_lines_valid = 0;
9691
9692 if (wp->w_height < 5)
9693 return FAIL;
9694
9695 if (line_count > wp->w_height - row)
9696 line_count = wp->w_height - row;
9697
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009698 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 if (retval != MAYBE)
9700 return retval;
9701
9702 /*
9703 * If there is a next window or a status line, we first try to delete the
9704 * lines at the bottom to avoid messing what is after the window.
9705 * If this fails and there are following windows, don't do anything to avoid
9706 * messing up those windows, better just redraw.
9707 */
9708 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 if (wp->w_next != NULL || wp->w_status_height)
9710 {
9711 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009712 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 did_delete = TRUE;
9714 else if (wp->w_next)
9715 return FAIL;
9716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 /*
9718 * if no lines deleted, blank the lines that will end up below the window
9719 */
9720 if (!did_delete)
9721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009724 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 lastrow = nextrow + line_count;
9726 if (lastrow > Rows)
9727 lastrow = Rows;
9728 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009729 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 ' ', ' ', 0);
9731 }
9732
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009733 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 == FAIL)
9735 {
9736 /* deletion will have messed up other windows */
9737 if (did_delete)
9738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 win_rest_invalid(W_NEXT(wp));
9741 }
9742 return FAIL;
9743 }
9744
9745 return OK;
9746}
9747
9748/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009749 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9751 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9752 * scrolling
9753 * Return OK for success, FAIL if the lines are not deleted.
9754 */
9755 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009756win_del_lines(
9757 win_T *wp,
9758 int row,
9759 int line_count,
9760 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009761 int mayclear,
9762 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
9764 int retval;
9765
9766 if (invalid)
9767 wp->w_lines_valid = 0;
9768
9769 if (line_count > wp->w_height - row)
9770 line_count = wp->w_height - row;
9771
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009772 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (retval != MAYBE)
9774 return retval;
9775
9776 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009777 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 return FAIL;
9779
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 /*
9781 * If there are windows or status lines below, try to put them at the
9782 * correct place. If we can't do that, they have to be redrawn.
9783 */
9784 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9785 {
9786 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009787 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
9789 wp->w_redr_status = TRUE;
9790 win_rest_invalid(wp->w_next);
9791 }
9792 }
9793 /*
9794 * If this is the last window and there is no status line, redraw the
9795 * command line later.
9796 */
9797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 redraw_cmdline = TRUE;
9799 return OK;
9800}
9801
9802/*
9803 * Common code for win_ins_lines() and win_del_lines().
9804 * Returns OK or FAIL when the work has been done.
9805 * Returns MAYBE when not finished yet.
9806 */
9807 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009808win_do_lines(
9809 win_T *wp,
9810 int row,
9811 int line_count,
9812 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009813 int del,
9814 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815{
9816 int retval;
9817
9818 if (!redrawing() || line_count <= 0)
9819 return FAIL;
9820
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009821 /* When inserting lines would result in loss of command output, just redraw
9822 * the lines. */
9823 if (no_win_do_lines_ins && !del)
9824 return FAIL;
9825
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009827 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009829 if (!no_win_do_lines_ins)
9830 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 return FAIL;
9832 }
9833
9834 /*
9835 * Delete all remaining lines
9836 */
9837 if (row + line_count >= wp->w_height)
9838 {
9839 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009840 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 ' ', ' ', 0);
9842 return OK;
9843 }
9844
9845 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009846 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009848 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009850 if (!no_win_do_lines_ins)
9851 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
9853 /*
9854 * If the terminal can set a scroll region, use that.
9855 * Always do this in a vertically split window. This will redraw from
9856 * ScreenLines[] when t_CV isn't defined. That's faster than using
9857 * win_line().
9858 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009859 * a character in the lower right corner of the scroll region may cause a
9860 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009862 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 scroll_region_set(wp, row);
9866 if (del)
9867 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009868 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 else
9870 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009871 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 scroll_region_reset();
9874 return retval;
9875 }
9876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9878 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
9880 return MAYBE;
9881}
9882
9883/*
9884 * window 'wp' and everything after it is messed up, mark it for redraw
9885 */
9886 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009887win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 {
9891 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 wp->w_redr_status = TRUE;
9893 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 }
9895 redraw_cmdline = TRUE;
9896}
9897
9898/*
9899 * The rest of the routines in this file perform screen manipulations. The
9900 * given operation is performed physically on the screen. The corresponding
9901 * change is also made to the internal screen image. In this way, the editor
9902 * anticipates the effect of editing changes on the appearance of the screen.
9903 * That way, when we call screenupdate a complete redraw isn't usually
9904 * necessary. Another advantage is that we can keep adding code to anticipate
9905 * screen changes, and in the meantime, everything still works.
9906 */
9907
9908/*
9909 * types for inserting or deleting lines
9910 */
9911#define USE_T_CAL 1
9912#define USE_T_CDL 2
9913#define USE_T_AL 3
9914#define USE_T_CE 4
9915#define USE_T_DL 5
9916#define USE_T_SR 6
9917#define USE_NL 7
9918#define USE_T_CD 8
9919#define USE_REDRAW 9
9920
9921/*
9922 * insert lines on the screen and update ScreenLines[]
9923 * 'end' is the line after the scrolled part. Normally it is Rows.
9924 * When scrolling region used 'off' is the offset from the top for the region.
9925 * 'row' and 'end' are relative to the start of the region.
9926 *
9927 * return FAIL for failure, OK for success.
9928 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009929 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009930screen_ins_lines(
9931 int off,
9932 int row,
9933 int line_count,
9934 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009935 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009936 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937{
9938 int i;
9939 int j;
9940 unsigned temp;
9941 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009942 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 int type;
9944 int result_empty;
9945 int can_ce = can_clear(T_CE);
9946
9947 /*
9948 * FAIL if
9949 * - there is no valid screen
9950 * - the screen has to be redrawn completely
9951 * - the line count is less than one
9952 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009953 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009955 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9956#ifdef FEAT_CLIPBOARD
9957 || (clip_star.state != SELECT_CLEARED
9958 && redrawing_for_callback > 0)
9959#endif
9960 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 return FAIL;
9962
9963 /*
9964 * There are seven ways to insert lines:
9965 * 0. When in a vertically split window and t_CV isn't set, redraw the
9966 * characters from ScreenLines[].
9967 * 1. Use T_CD (clear to end of display) if it exists and the result of
9968 * the insert is just empty lines
9969 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9970 * present or line_count > 1. It looks better if we do all the inserts
9971 * at once.
9972 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9973 * insert is just empty lines and T_CE is not present or line_count >
9974 * 1.
9975 * 4. Use T_AL (insert line) if it exists.
9976 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9977 * just empty lines.
9978 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9979 * just empty lines.
9980 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9981 * the 'da' flag is not set or we have clear line capability.
9982 * 8. redraw the characters from ScreenLines[].
9983 *
9984 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9985 * the scrollbar for the window. It does have insert line, use that if it
9986 * exists.
9987 */
9988 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9990 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009991 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 type = USE_T_CD;
9993 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9994 type = USE_T_CAL;
9995 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9996 type = USE_T_CDL;
9997 else if (*T_AL != NUL)
9998 type = USE_T_AL;
9999 else if (can_ce && result_empty)
10000 type = USE_T_CE;
10001 else if (*T_DL != NUL && result_empty)
10002 type = USE_T_DL;
10003 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10004 type = USE_T_SR;
10005 else
10006 return FAIL;
10007
10008 /*
10009 * For clearing the lines screen_del_lines() is used. This will also take
10010 * care of t_db if necessary.
10011 */
10012 if (type == USE_T_CD || type == USE_T_CDL ||
10013 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010014 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
10016 /*
10017 * If text is retained below the screen, first clear or delete as many
10018 * lines at the bottom of the window as are about to be inserted so that
10019 * the deleted lines won't later surface during a screen_del_lines.
10020 */
10021 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010022 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023
10024#ifdef FEAT_CLIPBOARD
10025 /* Remove a modeless selection when inserting lines halfway the screen
10026 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010027 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010028 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 else
10030 clip_scroll_selection(-line_count);
10031#endif
10032
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033#ifdef FEAT_GUI
10034 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10035 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010036 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#endif
10038
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010039 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10040 cursor_col = wp->w_wincol;
10041
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 if (*T_CCS != NUL) /* cursor relative to region */
10043 cursor_row = row;
10044 else
10045 cursor_row = row + off;
10046
10047 /*
10048 * Shift LineOffset[] line_count down to reflect the inserted lines.
10049 * Clear the inserted lines in ScreenLines[].
10050 */
10051 row += off;
10052 end += off;
10053 for (i = 0; i < line_count; ++i)
10054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (wp != NULL && wp->w_width != Columns)
10056 {
10057 /* need to copy part of a line */
10058 j = end - 1 - i;
10059 while ((j -= line_count) >= row)
10060 linecopy(j + line_count, j, wp);
10061 j += line_count;
10062 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010063 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10064 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 else
10066 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10067 LineWraps[j] = FALSE;
10068 }
10069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 {
10071 j = end - 1 - i;
10072 temp = LineOffset[j];
10073 while ((j -= line_count) >= row)
10074 {
10075 LineOffset[j + line_count] = LineOffset[j];
10076 LineWraps[j + line_count] = LineWraps[j];
10077 }
10078 LineOffset[j + line_count] = temp;
10079 LineWraps[j + line_count] = FALSE;
10080 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010081 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 else
10083 lineinvalid(temp, (int)Columns);
10084 }
10085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
10087 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010088 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010089 if (clear_attr != 0)
10090 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 /* redraw the characters */
10093 if (type == USE_REDRAW)
10094 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010095 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 {
10097 term_append_lines(line_count);
10098 screen_start(); /* don't know where cursor is now */
10099 }
10100 else
10101 {
10102 for (i = 0; i < line_count; i++)
10103 {
10104 if (type == USE_T_AL)
10105 {
10106 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010107 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 out_str(T_AL);
10109 }
10110 else /* type == USE_T_SR */
10111 out_str(T_SR);
10112 screen_start(); /* don't know where cursor is now */
10113 }
10114 }
10115
10116 /*
10117 * With scroll-reverse and 'da' flag set we need to clear the lines that
10118 * have been scrolled down into the region.
10119 */
10120 if (type == USE_T_SR && *T_DA)
10121 {
10122 for (i = 0; i < line_count; ++i)
10123 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010124 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 out_str(T_CE);
10126 screen_start(); /* don't know where cursor is now */
10127 }
10128 }
10129
10130#ifdef FEAT_GUI
10131 gui_can_update_cursor();
10132 if (gui.in_use)
10133 out_flush(); /* always flush after a scroll */
10134#endif
10135 return OK;
10136}
10137
10138/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010139 * Delete lines on the screen and update ScreenLines[].
10140 * "end" is the line after the scrolled part. Normally it is Rows.
10141 * When scrolling region used "off" is the offset from the top for the region.
10142 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 *
10144 * Return OK for success, FAIL if the lines are not deleted.
10145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010147screen_del_lines(
10148 int off,
10149 int row,
10150 int line_count,
10151 int end,
10152 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010153 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010154 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
10156 int j;
10157 int i;
10158 unsigned temp;
10159 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010160 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 int cursor_end;
10162 int result_empty; /* result is empty until end of region */
10163 int can_delete; /* deleting line codes can be used */
10164 int type;
10165
10166 /*
10167 * FAIL if
10168 * - there is no valid screen
10169 * - the screen has to be redrawn completely
10170 * - the line count is less than one
10171 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010172 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010174 if (!screen_valid(TRUE) || line_count <= 0
10175 || (!force && line_count > p_ttyscroll)
10176#ifdef FEAT_CLIPBOARD
10177 || (clip_star.state != SELECT_CLEARED
10178 && redrawing_for_callback > 0)
10179#endif
10180 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 return FAIL;
10182
10183 /*
10184 * Check if the rest of the current region will become empty.
10185 */
10186 result_empty = row + line_count >= end;
10187
10188 /*
10189 * We can delete lines only when 'db' flag not set or when 'ce' option
10190 * available.
10191 */
10192 can_delete = (*T_DB == NUL || can_clear(T_CE));
10193
10194 /*
10195 * There are six ways to delete lines:
10196 * 0. When in a vertically split window and t_CV isn't set, redraw the
10197 * characters from ScreenLines[].
10198 * 1. Use T_CD if it exists and the result is empty.
10199 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10200 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10201 * none of the other ways work.
10202 * 4. Use T_CE (erase line) if the result is empty.
10203 * 5. Use T_DL (delete line) if it exists.
10204 * 6. redraw the characters from ScreenLines[].
10205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10207 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010208 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 type = USE_T_CD;
10210#if defined(__BEOS__) && defined(BEOS_DR8)
10211 /*
10212 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10213 * its internal termcap... this works okay for tests which test *T_DB !=
10214 * NUL. It has the disadvantage that the user cannot use any :set t_*
10215 * command to get T_DB (back) to empty_option, only :set term=... will do
10216 * the trick...
10217 * Anyway, this hack will hopefully go away with the next OS release.
10218 * (Olaf Seibert)
10219 */
10220 else if (row == 0 && T_DB == empty_option
10221 && (line_count == 1 || *T_CDL == NUL))
10222#else
10223 else if (row == 0 && (
10224#ifndef AMIGA
10225 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10226 * up, so use delete-line command */
10227 line_count == 1 ||
10228#endif
10229 *T_CDL == NUL))
10230#endif
10231 type = USE_NL;
10232 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10233 type = USE_T_CDL;
10234 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010235 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 type = USE_T_CE;
10237 else if (*T_DL != NUL && can_delete)
10238 type = USE_T_DL;
10239 else if (*T_CDL != NUL && can_delete)
10240 type = USE_T_CDL;
10241 else
10242 return FAIL;
10243
10244#ifdef FEAT_CLIPBOARD
10245 /* Remove a modeless selection when deleting lines halfway the screen or
10246 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010247 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010248 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 else
10250 clip_scroll_selection(line_count);
10251#endif
10252
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253#ifdef FEAT_GUI
10254 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10255 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010256 gui_dont_update_cursor(gui.cursor_row >= row + off
10257 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258#endif
10259
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010260 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10261 cursor_col = wp->w_wincol;
10262
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 if (*T_CCS != NUL) /* cursor relative to region */
10264 {
10265 cursor_row = row;
10266 cursor_end = end;
10267 }
10268 else
10269 {
10270 cursor_row = row + off;
10271 cursor_end = end + off;
10272 }
10273
10274 /*
10275 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10276 * Clear the inserted lines in ScreenLines[].
10277 */
10278 row += off;
10279 end += off;
10280 for (i = 0; i < line_count; ++i)
10281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 if (wp != NULL && wp->w_width != Columns)
10283 {
10284 /* need to copy part of a line */
10285 j = row + i;
10286 while ((j += line_count) <= end - 1)
10287 linecopy(j - line_count, j, wp);
10288 j -= line_count;
10289 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010290 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10291 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 else
10293 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10294 LineWraps[j] = FALSE;
10295 }
10296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 {
10298 /* whole width, moving the line pointers is faster */
10299 j = row + i;
10300 temp = LineOffset[j];
10301 while ((j += line_count) <= end - 1)
10302 {
10303 LineOffset[j - line_count] = LineOffset[j];
10304 LineWraps[j - line_count] = LineWraps[j];
10305 }
10306 LineOffset[j - line_count] = temp;
10307 LineWraps[j - line_count] = FALSE;
10308 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010309 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 else
10311 lineinvalid(temp, (int)Columns);
10312 }
10313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010315 if (screen_attr != clear_attr)
10316 screen_stop_highlight();
10317 if (clear_attr != 0)
10318 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 /* redraw the characters */
10321 if (type == USE_REDRAW)
10322 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010323 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010325 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 out_str(T_CD);
10327 screen_start(); /* don't know where cursor is now */
10328 }
10329 else if (type == USE_T_CDL)
10330 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010331 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 term_delete_lines(line_count);
10333 screen_start(); /* don't know where cursor is now */
10334 }
10335 /*
10336 * Deleting lines at top of the screen or scroll region: Just scroll
10337 * the whole screen (scroll region) up by outputting newlines on the
10338 * last line.
10339 */
10340 else if (type == USE_NL)
10341 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010342 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 for (i = line_count; --i >= 0; )
10344 out_char('\n'); /* cursor will remain on same line */
10345 }
10346 else
10347 {
10348 for (i = line_count; --i >= 0; )
10349 {
10350 if (type == USE_T_DL)
10351 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010352 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 out_str(T_DL); /* delete a line */
10354 }
10355 else /* type == USE_T_CE */
10356 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010357 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 out_str(T_CE); /* erase a line */
10359 }
10360 screen_start(); /* don't know where cursor is now */
10361 }
10362 }
10363
10364 /*
10365 * If the 'db' flag is set, we need to clear the lines that have been
10366 * scrolled up at the bottom of the region.
10367 */
10368 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10369 {
10370 for (i = line_count; i > 0; --i)
10371 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010372 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 out_str(T_CE); /* erase a line */
10374 screen_start(); /* don't know where cursor is now */
10375 }
10376 }
10377
10378#ifdef FEAT_GUI
10379 gui_can_update_cursor();
10380 if (gui.in_use)
10381 out_flush(); /* always flush after a scroll */
10382#endif
10383
10384 return OK;
10385}
10386
10387/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010388 * Return TRUE when postponing displaying the mode message: when not redrawing
10389 * or inside a mapping.
10390 */
10391 int
10392skip_showmode()
10393{
10394 // Call char_avail() only when we are going to show something, because it
10395 // takes a bit of time. redrawing() may also call char_avail_avail().
10396 if (global_busy
10397 || msg_silent != 0
10398 || !redrawing()
10399 || (char_avail() && !KeyTyped))
10400 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010401 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010402 return TRUE;
10403 }
10404 return FALSE;
10405}
10406
10407/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010408 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 *
10410 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10411 * If clear_cmdline is FALSE there may be a message there that needs to be
10412 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010413 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 * Return the length of the message (0 if no message).
10415 */
10416 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010417showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418{
10419 int need_clear;
10420 int length = 0;
10421 int do_mode;
10422 int attr;
10423 int nwr_save;
10424#ifdef FEAT_INS_EXPAND
10425 int sub_attr;
10426#endif
10427
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010428 do_mode = ((p_smd && msg_silent == 0)
10429 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010430 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010431 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010432 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010434 if (skip_showmode())
10435 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436
10437 nwr_save = need_wait_return;
10438
10439 /* wait a bit before overwriting an important message */
10440 check_for_delay(FALSE);
10441
10442 /* if the cmdline is more than one line high, erase top lines */
10443 need_clear = clear_cmdline;
10444 if (clear_cmdline && cmdline_row < Rows - 1)
10445 msg_clr_cmdline(); /* will reset clear_cmdline */
10446
10447 /* Position on the last line in the window, column 0 */
10448 msg_pos_mode();
10449 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010450 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 if (do_mode)
10452 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010453 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010455 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010456# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010457 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010458# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010459 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010460# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010461 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010462# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010463 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010465 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466# endif
10467#endif
10468#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10469 if (gui.in_use)
10470 {
10471 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010472 {
10473 /* HANGUL */
10474 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010475 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010476 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010477 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 }
10480#endif
10481#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010482 /* CTRL-X in Insert mode */
10483 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 {
10485 /* These messages can get long, avoid a wrap in a narrow
10486 * window. Prefer showing edit_submode_extra. */
10487 length = (Rows - msg_row) * Columns - 3;
10488 if (edit_submode_extra != NULL)
10489 length -= vim_strsize(edit_submode_extra);
10490 if (length > 0)
10491 {
10492 if (edit_submode_pre != NULL)
10493 length -= vim_strsize(edit_submode_pre);
10494 if (length - vim_strsize(edit_submode) > 0)
10495 {
10496 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010497 msg_puts_attr((char *)edit_submode_pre, attr);
10498 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 }
10500 if (edit_submode_extra != NULL)
10501 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010502 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010504 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 else
10506 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010507 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 }
10509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511 else
10512#endif
10513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010515 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010516 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010517 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 else if (State & INSERT)
10519 {
10520#ifdef FEAT_RIGHTLEFT
10521 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010522 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010524 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010526 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010527 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010529 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010531 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#ifdef FEAT_RIGHTLEFT
10533 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010534 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535#endif
10536#ifdef FEAT_KEYMAP
10537 if (State & LANGMAP)
10538 {
10539# ifdef FEAT_ARABIC
10540 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010541 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 else
10543# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010544 if (get_keymap_str(curwin, (char_u *)" (%s)",
10545 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010546 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 }
10548#endif
10549 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010550 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 if (VIsual_active)
10553 {
10554 char *p;
10555
10556 /* Don't concatenate separate words to avoid translation
10557 * problems. */
10558 switch ((VIsual_select ? 4 : 0)
10559 + (VIsual_mode == Ctrl_V) * 2
10560 + (VIsual_mode == 'V'))
10561 {
10562 case 0: p = N_(" VISUAL"); break;
10563 case 1: p = N_(" VISUAL LINE"); break;
10564 case 2: p = N_(" VISUAL BLOCK"); break;
10565 case 4: p = N_(" SELECT"); break;
10566 case 5: p = N_(" SELECT LINE"); break;
10567 default: p = N_(" SELECT BLOCK"); break;
10568 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010569 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010571 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010573
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 need_clear = TRUE;
10575 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010576 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577#ifdef FEAT_INS_EXPAND
10578 && edit_submode == NULL /* otherwise it gets too long */
10579#endif
10580 )
10581 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010582 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 need_clear = TRUE;
10584 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010585
10586 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010587 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 msg_clr_eos();
10589 msg_didout = FALSE; /* overwrite this message */
10590 length = msg_col;
10591 msg_col = 0;
10592 need_wait_return = nwr_save; /* never ask for hit-return for this */
10593 }
10594 else if (clear_cmdline && msg_silent == 0)
10595 /* Clear the whole command line. Will reset "clear_cmdline". */
10596 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010597 else if (redraw_mode)
10598 {
10599 msg_pos_mode();
10600 msg_clr_eos();
10601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602
10603#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 /* In Visual mode the size of the selected area must be redrawn. */
10605 if (VIsual_active)
10606 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
10608 /* If the last window has no status line, the ruler is after the mode
10609 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010610 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010611 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612#endif
10613 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010614 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 clear_cmdline = FALSE;
10616
10617 return length;
10618}
10619
10620/*
10621 * Position for a mode message.
10622 */
10623 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010624msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 msg_col = 0;
10627 msg_row = Rows - 1;
10628}
10629
10630/*
10631 * Delete mode message. Used when ESC is typed which is expected to end
10632 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010633 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 */
10635 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010636unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637{
10638 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010639 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 */
10641 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10642 redraw_cmdline = TRUE; /* delete mode later */
10643 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010644 clearmode();
10645}
10646
10647/*
10648 * Clear the mode message.
10649 */
10650 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010651clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010652{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010653 int save_msg_row = msg_row;
10654 int save_msg_col = msg_col;
10655
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010656 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010657 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010658 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010659 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010660
10661 msg_col = save_msg_col;
10662 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663}
10664
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010665 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010666recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010667{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010668 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010669 if (!shortmess(SHM_RECORDING))
10670 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010671 char s[4];
10672
10673 sprintf(s, " @%c", reg_recording);
10674 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010675 }
10676}
10677
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010678/*
10679 * Draw the tab pages line at the top of the Vim window.
10680 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010681 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010682draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010683{
10684 int tabcount = 0;
10685 tabpage_T *tp;
10686 int tabwidth;
10687 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010688 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010689 int attr;
10690 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010691 win_T *cwp;
10692 int wincount;
10693 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010694 int c;
10695 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010696 int attr_sel = HL_ATTR(HLF_TPS);
10697 int attr_nosel = HL_ATTR(HLF_TP);
10698 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010699 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010700 int room;
10701 int use_sep_chars = (t_colors < 8
10702#ifdef FEAT_GUI
10703 && !gui.in_use
10704#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010705#ifdef FEAT_TERMGUICOLORS
10706 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010707#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010708 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010709
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010710 if (ScreenLines == NULL)
10711 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010712 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010713
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010714#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010715 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010716 if (gui_use_tabline())
10717 {
10718 gui_update_tabline();
10719 return;
10720 }
10721#endif
10722
10723 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010724 return;
10725
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010726#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010727 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010728
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010729 /* Use the 'tabline' option if it's set. */
10730 if (*p_tal != NUL)
10731 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010732 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010733
10734 /* Check for an error. If there is one we would loop in redrawing the
10735 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010736 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010737 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010738 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010739 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010740 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010741 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010742 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010743 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010744#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010745 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010746 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010747 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010748
Bram Moolenaar238a5642006-02-21 22:12:05 +000010749 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10750 if (tabwidth < 6)
10751 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010752
Bram Moolenaar238a5642006-02-21 22:12:05 +000010753 attr = attr_nosel;
10754 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010755 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10756 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010757 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010758 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010759
Bram Moolenaar238a5642006-02-21 22:12:05 +000010760 if (tp->tp_topframe == topframe)
10761 attr = attr_sel;
10762 if (use_sep_chars && col > 0)
10763 screen_putchar('|', 0, col++, attr);
10764
10765 if (tp->tp_topframe != topframe)
10766 attr = attr_nosel;
10767
10768 screen_putchar(' ', 0, col++, attr);
10769
10770 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010771 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010772 cwp = curwin;
10773 wp = firstwin;
10774 }
10775 else
10776 {
10777 cwp = tp->tp_curwin;
10778 wp = tp->tp_firstwin;
10779 }
10780
10781 modified = FALSE;
10782 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10783 if (bufIsChanged(wp->w_buffer))
10784 modified = TRUE;
10785 if (modified || wincount > 1)
10786 {
10787 if (wincount > 1)
10788 {
10789 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010790 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010791 if (col + len >= Columns - 3)
10792 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010793 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010794#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010795 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010796#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010797 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010798#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010799 );
10800 col += len;
10801 }
10802 if (modified)
10803 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10804 screen_putchar(' ', 0, col++, attr);
10805 }
10806
10807 room = scol - col + tabwidth - 1;
10808 if (room > 0)
10809 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010810 /* Get buffer name in NameBuff[] */
10811 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010812 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 len = vim_strsize(NameBuff);
10814 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010815 if (has_mbyte)
10816 while (len > room)
10817 {
10818 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010819 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010820 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010821 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010822 {
10823 p += len - room;
10824 len = room;
10825 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010826 if (len > Columns - col - 1)
10827 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010828
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010829 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010830 col += len;
10831 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010832 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010833
10834 /* Store the tab page number in TabPageIdxs[], so that
10835 * jump_to_mouse() knows where each one is. */
10836 ++tabcount;
10837 while (scol < col)
10838 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010839 }
10840
Bram Moolenaar238a5642006-02-21 22:12:05 +000010841 if (use_sep_chars)
10842 c = '_';
10843 else
10844 c = ' ';
10845 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010846
10847 /* Put an "X" for closing the current tab if there are several. */
10848 if (first_tabpage->tp_next != NULL)
10849 {
10850 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10851 TabPageIdxs[Columns - 1] = -999;
10852 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010853 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010854
10855 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10856 * set. */
10857 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010858}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010859
10860/*
10861 * Get buffer name for "buf" into NameBuff[].
10862 * Takes care of special buffer names and translates special characters.
10863 */
10864 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010865get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010866{
10867 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010868 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010869 else
10870 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10871 trans_characters(NameBuff, MAXPATHL);
10872}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010873
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874/*
10875 * Get the character to use in a status line. Get its attributes in "*attr".
10876 */
10877 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010878fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
10880 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010881
10882#ifdef FEAT_TERMINAL
10883 if (bt_terminal(wp->w_buffer))
10884 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010885 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010886 {
10887 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010888 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010889 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010890 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010891 {
10892 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010893 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010894 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010895 }
10896 else
10897#endif
10898 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010900 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 fill = fill_stl;
10902 }
10903 else
10904 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010905 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 fill = fill_stlnc;
10907 }
10908 /* Use fill when there is highlighting, and highlighting of current
10909 * window differs, or the fillchars differ, or this is not the
10910 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010911 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010912 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 || (fill_stl != fill_stlnc)))
10914 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010915 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 return '^';
10917 return '=';
10918}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920/*
10921 * Get the character to use in a separator between vertically split windows.
10922 * Get its attributes in "*attr".
10923 */
10924 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010925fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010927 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 if (*attr == 0 && fill_vert == ' ')
10929 return '|';
10930 else
10931 return fill_vert;
10932}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933
10934/*
10935 * Return TRUE if redrawing should currently be done.
10936 */
10937 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010938redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010940#ifdef FEAT_EVAL
10941 if (disable_redraw_for_testing)
10942 return 0;
10943 else
10944#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010945 return ((!RedrawingDisabled
10946#ifdef FEAT_EVAL
10947 || ignore_redraw_flag_for_testing
10948#endif
10949 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950}
10951
10952/*
10953 * Return TRUE if printing messages should currently be done.
10954 */
10955 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010956messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957{
10958 return (!(p_lz && char_avail() && !KeyTyped));
10959}
10960
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010961#ifdef FEAT_MENU
10962/*
10963 * Draw the window toolbar.
10964 */
10965 static void
10966redraw_win_toolbar(win_T *wp)
10967{
10968 vimmenu_T *menu;
10969 int item_idx = 0;
10970 int item_count = 0;
10971 int col = 0;
10972 int next_col;
10973 int off = (int)(current_ScreenLine - ScreenLines);
10974 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10975 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10976
10977 vim_free(wp->w_winbar_items);
10978 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10979 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010980 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010981
10982 /* TODO: use fewer spaces if there is not enough room */
10983 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010984 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010985 {
10986 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010987 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010988 break;
10989 if (col > 1)
10990 {
10991 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010992 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010993 break;
10994 }
10995
10996 wp->w_winbar_items[item_idx].wb_startcol = col;
10997 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010998 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010999 break;
11000
11001 next_col = text_to_screenline(wp, menu->name, col);
11002 while (col < next_col)
11003 {
11004 ScreenAttrs[off + col] = button_attr;
11005 ++col;
11006 }
11007 wp->w_winbar_items[item_idx].wb_endcol = col;
11008 wp->w_winbar_items[item_idx].wb_menu = menu;
11009 ++item_idx;
11010
Bram Moolenaar02631462017-09-22 15:20:32 +020011011 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011012 break;
11013 space_to_screenline(off + col, button_attr);
11014 ++col;
11015 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011016 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011017 {
11018 space_to_screenline(off + col, fill_attr);
11019 ++col;
11020 }
11021 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11022
Bram Moolenaar02631462017-09-22 15:20:32 +020011023 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011024 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011025}
11026#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011027
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028/*
11029 * Show current status info in ruler and various other places
11030 * If always is FALSE, only show ruler if position has changed.
11031 */
11032 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011033showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
11035 if (!always && !redrawing())
11036 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011037#ifdef FEAT_INS_EXPAND
11038 if (pum_visible())
11039 {
11040 /* Don't redraw right now, do it later. */
11041 curwin->w_redr_status = TRUE;
11042 return;
11043 }
11044#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011045#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011046 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011047 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 else
11049#endif
11050#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011051 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052#endif
11053
11054#ifdef FEAT_TITLE
11055 if (need_maketitle
11056# ifdef FEAT_STL_OPT
11057 || (p_icon && (stl_syntax & STL_IN_ICON))
11058 || (p_title && (stl_syntax & STL_IN_TITLE))
11059# endif
11060 )
11061 maketitle();
11062#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011063 /* Redraw the tab pages line if needed. */
11064 if (redraw_tabline)
11065 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066}
11067
11068#ifdef FEAT_CMDL_INFO
11069 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011070win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011072#define RULER_BUF_LEN 70
11073 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 int row;
11075 int fillchar;
11076 int attr;
11077 int empty_line = FALSE;
11078 colnr_T virtcol;
11079 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011080 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 int this_ru_col;
11083 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011084 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085
11086 /* If 'ruler' off or redrawing disabled, don't do anything */
11087 if (!p_ru)
11088 return;
11089
11090 /*
11091 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11092 * after deleting lines, before cursor.lnum is corrected.
11093 */
11094 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11095 return;
11096
11097#ifdef FEAT_INS_EXPAND
11098 /* Don't draw the ruler while doing insert-completion, it might overwrite
11099 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 if (edit_submode != NULL)
11102 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011103 // Don't draw the ruler when the popup menu is visible, it may overlap.
11104 // Except when the popup menu will be redrawn anyway.
11105 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011106 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107#endif
11108
11109#ifdef FEAT_STL_OPT
11110 if (*p_ruf)
11111 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011112 int save_called_emsg = called_emsg;
11113
11114 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011116 if (called_emsg)
11117 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011118 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011119 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 return;
11121 }
11122#endif
11123
11124 /*
11125 * Check if not in Insert mode and the line is empty (will show "0-1").
11126 */
11127 if (!(State & INSERT)
11128 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11129 empty_line = TRUE;
11130
11131 /*
11132 * Only draw the ruler when something changed.
11133 */
11134 validate_virtcol_win(wp);
11135 if ( redraw_cmdline
11136 || always
11137 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11138 || wp->w_cursor.col != wp->w_ru_cursor.col
11139 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 || wp->w_topline != wp->w_ru_topline
11142 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11143#ifdef FEAT_DIFF
11144 || wp->w_topfill != wp->w_ru_topfill
11145#endif
11146 || empty_line != wp->w_ru_empty)
11147 {
11148 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 if (wp->w_status_height)
11150 {
11151 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011152 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011153 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011154 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 }
11156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 {
11158 row = Rows - 1;
11159 fillchar = ' ';
11160 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 width = Columns;
11162 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 }
11164
11165 /* In list mode virtcol needs to be recomputed */
11166 virtcol = wp->w_virtcol;
11167 if (wp->w_p_list && lcs_tab1 == NUL)
11168 {
11169 wp->w_p_list = FALSE;
11170 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11171 wp->w_p_list = TRUE;
11172 }
11173
11174 /*
11175 * Some sprintfs return the length, some return a pointer.
11176 * To avoid portability problems we use strlen() here.
11177 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011178 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11180 ? 0L
11181 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011182 len = STRLEN(buffer);
11183 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11185 (int)virtcol + 1);
11186
11187 /*
11188 * Add a "50%" if there is room for it.
11189 * On the last line, don't print in the last column (scrolls the
11190 * screen up on some terminals).
11191 */
11192 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011193 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 this_ru_col = ru_col - (Columns - width);
11198 if (this_ru_col < 0)
11199 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 /* Never use more than half the window/screen width, leave the other
11201 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011202 if (this_ru_col < (width + 1) / 2)
11203 this_ru_col = (width + 1) / 2;
11204 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011206 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011207 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 if (has_mbyte)
11210 i += (*mb_char2bytes)(fillchar, buffer + i);
11211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 buffer[i++] = fillchar;
11213 ++o;
11214 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011215 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 }
11217 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 if (has_mbyte)
11219 {
11220 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011221 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 {
11223 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011224 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225 {
11226 buffer[i] = NUL;
11227 break;
11228 }
11229 }
11230 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011231 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011232 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233
Bram Moolenaar4033c552017-09-16 20:54:51 +020011234 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 i = redraw_cmdline;
11236 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011237 this_ru_col + off + (int)STRLEN(buffer),
11238 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 fillchar, fillchar, attr);
11240 /* don't redraw the cmdline because of showing the ruler */
11241 redraw_cmdline = i;
11242 wp->w_ru_cursor = wp->w_cursor;
11243 wp->w_ru_virtcol = wp->w_virtcol;
11244 wp->w_ru_empty = empty_line;
11245 wp->w_ru_topline = wp->w_topline;
11246 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11247#ifdef FEAT_DIFF
11248 wp->w_ru_topfill = wp->w_topfill;
11249#endif
11250 }
11251}
11252#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011253
11254#if defined(FEAT_LINEBREAK) || defined(PROTO)
11255/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011256 * Return the width of the 'number' and 'relativenumber' column.
11257 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011258 * Otherwise it depends on 'numberwidth' and the line count.
11259 */
11260 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011261number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011262{
11263 int n;
11264 linenr_T lnum;
11265
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011266 if (wp->w_p_rnu && !wp->w_p_nu)
11267 /* cursor line shows "0" */
11268 lnum = wp->w_height;
11269 else
11270 /* cursor line shows absolute line number */
11271 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011272
Bram Moolenaar6b314672015-03-20 15:42:10 +010011273 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011274 return wp->w_nrwidth_width;
11275 wp->w_nrwidth_line_count = lnum;
11276
11277 n = 0;
11278 do
11279 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011280 lnum /= 10;
11281 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011282 } while (lnum > 0);
11283
11284 /* 'numberwidth' gives the minimal width plus one */
11285 if (n < wp->w_p_nuw - 1)
11286 n = wp->w_p_nuw - 1;
11287
11288 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011289 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011290 return n;
11291}
11292#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011293
Bram Moolenaar113e1072019-01-20 15:30:40 +010011294#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011295/*
11296 * Return the current cursor column. This is the actual position on the
11297 * screen. First column is 0.
11298 */
11299 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011300screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011301{
11302 return screen_cur_col;
11303}
11304
11305/*
11306 * Return the current cursor row. This is the actual position on the screen.
11307 * First row is 0.
11308 */
11309 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011310screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011311{
11312 return screen_cur_row;
11313}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011314#endif