blob: 447dd411b875e9106c612235383e8905c4827e3a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200124#ifdef FEAT_TEXT_PROP
125static void update_popups(void);
126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100129static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void win_rest_invalid(win_T *wp);
156static void msg_pos_mode(void);
157static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200184// flags for screen_line()
185#define SLF_RIGHTLEFT 1
186#define SLF_POPUP 2
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100191 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200204 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100243 // This may be needed when switching tabs.
244 if (must_redraw < type)
245 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100252redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271redraw_buf_line_later(buf_T *buf, linenr_T lnum)
272{
273 win_T *wp;
274
275 FOR_ALL_WINDOWS(wp)
276 if (wp->w_buffer == buf && lnum >= wp->w_topline
277 && lnum < wp->w_botline)
278 redrawWinline(wp, lnum);
279}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100280#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100281
Bram Moolenaar113e1072019-01-20 15:30:40 +0100282#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100283 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284redraw_buf_and_status_later(buf_T *buf, int type)
285{
286 win_T *wp;
287
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200289 if (wild_menu_showing != 0)
290 /* Don't redraw while the command line completion is displayed, it
291 * would disappear. */
292 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294 FOR_ALL_WINDOWS(wp)
295 {
296 if (wp->w_buffer == buf)
297 {
298 redraw_win_later(wp, type);
299 wp->w_redr_status = TRUE;
300 }
301 }
302}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100303#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200304
Bram Moolenaar113e1072019-01-20 15:30:40 +0100305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 * Redraw as soon as possible. When the command line is not scrolled redraw
308 * right away and restore what was on the command line.
309 * Return a code indicating what happened.
310 */
311 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100312redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313{
314 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 int r;
317 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline; /* copy from ScreenLines[] */
319 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200323 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324
325 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 return ret;
328
329 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 rows = screen_Rows - cmdline_row;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200331 screenline = (schar_T *)lalloc(rows * cols * sizeof(schar_T), FALSE);
332 screenattr = (sattr_T *)lalloc(rows * cols * sizeof(sattr_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenline == NULL || screenattr == NULL)
334 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (enc_utf8)
336 {
337 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar51e14382019-05-25 20:21:28 +0200338 rows * cols * sizeof(u8char_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenlineUC == NULL)
340 ret = 2;
341 for (i = 0; i < p_mco; ++i)
342 {
343 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar51e14382019-05-25 20:21:28 +0200344 rows * cols * sizeof(u8char_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200345 if (screenlineC[i] == NULL)
346 ret = 2;
347 }
348 }
349 if (enc_dbcs == DBCS_JPNU)
350 {
Bram Moolenaar51e14382019-05-25 20:21:28 +0200351 screenline2 = (schar_T *)lalloc(rows * cols * sizeof(schar_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 if (screenline2 == NULL)
353 ret = 2;
354 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 if (enc_utf8)
368 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 mch_memmove(screenlineC[i] + r * cols,
374 ScreenLinesC[i] + LineOffset[cmdline_row + r],
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 }
377 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381 }
382
383 update_screen(0);
384 ret = 3;
385
386 if (must_redraw == 0)
387 {
388 int off = (int)(current_ScreenLine - ScreenLines);
389
390 /* Restore the text displayed in the command line area. */
391 for (r = 0; r < rows; ++r)
392 {
393 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200394 screenline + r * cols,
395 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenattr + r * cols,
398 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 if (enc_utf8)
400 {
401 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineUC + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 for (i = 0; i < p_mco; ++i)
405 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenlineC[i] + r * cols,
407 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408 }
409 if (enc_dbcs == DBCS_JPNU)
410 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200411 screenline2 + r * cols,
412 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200413 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 }
415 ret = 4;
416 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418
419 vim_free(screenline);
420 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 if (enc_utf8)
422 {
423 vim_free(screenlineUC);
424 for (i = 0; i < p_mco; ++i)
425 vim_free(screenlineC[i]);
426 }
427 if (enc_dbcs == DBCS_JPNU)
428 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200429
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200430 /* Show the intro message when appropriate. */
431 maybe_intro_message();
432
433 setcursor();
434
Bram Moolenaar2951b772013-07-03 12:45:31 +0200435 return ret;
436}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100437#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200438
439/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100440 * Invoked after an asynchronous callback is called.
441 * If an echo command was used the cursor needs to be put back where
442 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200443 * If "call_update_screen" is FALSE don't call update_screen() when at the
444 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 */
446 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200447redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100448{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200449 ++redrawing_for_callback;
450
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200452 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100453 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200454 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 // Don't redraw when in prompt_for_number().
456 if (cmdline_row > 0)
457 {
458 // Redrawing only works when the screen didn't scroll. Don't clear
459 // wildmenu entries.
460 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200463#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200464 && call_update_screen)
465 update_screen(0);
466
467 // Redraw in the same position, so that the user can continue
468 // editing the command.
469 redrawcmdline_ex(FALSE);
470 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200471 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200472 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100473 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200474 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200475 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100476 setcursor();
477 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100480 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200481 // Don't update the cursor when it is blinking and off to avoid
482 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100483 out_flush_cursor(FALSE, FALSE);
484 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100485#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100486 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200487
488 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100489}
490
491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 * Changed something in the current window, at buffer line "lnum", that
493 * requires that line and possibly other lines to be redrawn.
494 * Used when entering/leaving Insert mode with the cursor on a folded line.
495 * Used to remove the "$" from a change command.
496 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
497 * may become invalid and the whole window will have to be redrawn.
498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100500redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200501 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100502 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200504 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
505 wp->w_redraw_top = lnum;
506 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
507 wp->w_redraw_bot = lnum;
508 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509}
510
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200511 void
512reset_updating_screen(int may_resize_shell UNUSED)
513{
514 updating_screen = FALSE;
515#ifdef FEAT_GUI
516 if (may_resize_shell)
517 gui_may_resize_shell();
518#endif
519#ifdef FEAT_TERMINAL
520 term_check_channel_closed_recently();
521#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200522
523#ifdef HAVE_DROP_FILE
524 // If handle_drop() was called while updating_screen was TRUE need to
525 // handle the drop now.
526 handle_any_postponed_drop();
527#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200528}
529
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200531 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
533 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100534update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
536 redraw_curbuf_later(type);
537 update_screen(type);
538}
539
540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * Based on the current value of curwin->w_topline, transfer a screenfull
542 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 win_T *wp;
550 static int did_intro = FALSE;
551#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
552 int did_one;
553#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200555 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200556 int gui_cursor_col = 0;
557 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200558#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000561 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 if (type == VALID_NO_UPDATE)
566 {
567 no_update = TRUE;
568 type = 0;
569 }
570
Bram Moolenaara3347722019-05-11 21:14:24 +0200571#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200572 {
573 buf_T *buf;
574
575 // Before updating the screen, notify any listeners of changed text.
576 FOR_ALL_BUFFERS(buf)
577 invoke_listeners(buf);
578 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (must_redraw)
582 {
583 if (type < must_redraw) /* use maximal type */
584 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000585
586 /* must_redraw is reset here, so that when we run into some weird
587 * reason to redraw while busy redrawing (e.g., asynchronous
588 * scrolling), or update_topline() in win_update() will cause a
589 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 must_redraw = 0;
591 }
592
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200593 /* May need to update w_lines[]. */
594 if (curwin->w_lines_valid == 0 && type < NOT_VALID
595#ifdef FEAT_TERMINAL
596 && !term_do_update_window(curwin)
597#endif
598 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 type = NOT_VALID;
600
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000601 /* Postpone the redrawing when it's not needed and when being called
602 * recursively. */
603 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 redraw_later(type); /* remember type for next time */
606 must_redraw = type;
607 if (type > INVERTED_ALL)
608 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200611#ifdef FEAT_TEXT_PROP
612 // TODO: avoid redrawing everything when there is a popup window.
613 if (first_popupwin != NULL || first_tab_popupwin != NULL)
614 type = NOT_VALID;
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 updating_screen = TRUE;
618#ifdef FEAT_SYN_HL
619 ++display_tick; /* let syntax code know we're in a next round of
620 * display updating */
621#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200622 if (no_update)
623 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * if the screen was scrolled up when displaying a message, scroll it down
627 */
628 if (msg_scrolled)
629 {
630 clear_cmdline = TRUE;
631 if (msg_scrolled > Rows - 5) /* clearing is faster */
632 type = CLEAR;
633 else if (type != CLEAR)
634 {
635 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200636 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
637 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 type = CLEAR;
639 FOR_ALL_WINDOWS(wp)
640 {
641 if (W_WINROW(wp) < msg_scrolled)
642 {
643 if (W_WINROW(wp) + wp->w_height > msg_scrolled
644 && wp->w_redr_type < REDRAW_TOP
645 && wp->w_lines_valid > 0
646 && wp->w_topline == wp->w_lines[0].wl_lnum)
647 {
648 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
649 wp->w_redr_type = REDRAW_TOP;
650 }
651 else
652 {
653 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200654 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
655 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 }
659 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200660 if (!no_update)
661 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000662 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 msg_scrolled = 0;
665 need_wait_return = FALSE;
666 }
667
668 /* reset cmdline_row now (may have been changed temporarily) */
669 compute_cmdrow();
670
671 /* Check for changed highlighting */
672 if (need_highlight_changed)
673 highlight_changed();
674
675 if (type == CLEAR) /* first clear screen */
676 {
677 screenclear(); /* will reset clear_cmdline */
678 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200679 /* must_redraw may be set indirectly, avoid another redraw later */
680 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682
683 if (clear_cmdline) /* going to clear cmdline (done below) */
684 check_for_delay(FALSE);
685
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200687 /* Force redraw when width of 'number' or 'relativenumber' column
688 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200690 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
691 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000692 curwin->w_redr_type = NOT_VALID;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 /*
696 * Only start redrawing if there is really something to do.
697 */
698 if (type == INVERTED)
699 update_curswant();
700 if (curwin->w_redr_type < type
701 && !((type == VALID
702 && curwin->w_lines[0].wl_valid
703#ifdef FEAT_DIFF
704 && curwin->w_topfill == curwin->w_old_topfill
705 && curwin->w_botfill == curwin->w_old_botfill
706#endif
707 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000709 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
711 && curwin->w_old_visual_mode == VIsual_mode
712 && (curwin->w_valid & VALID_VIRTCOL)
713 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ))
715 curwin->w_redr_type = type;
716
Bram Moolenaar5a305422006-04-28 22:38:25 +0000717 /* Redraw the tab pages line if needed. */
718 if (redraw_tabline || type >= NOT_VALID)
719 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_SYN_HL
722 /*
723 * Correct stored syntax highlighting info for changes in each displayed
724 * buffer. Each buffer must only be done once.
725 */
726 FOR_ALL_WINDOWS(wp)
727 {
728 if (wp->w_buffer->b_mod_set)
729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 win_T *wwp;
731
732 /* Check if we already did this buffer. */
733 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
734 if (wwp->w_buffer == wp->w_buffer)
735 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200736 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 syn_stack_apply_changes(wp->w_buffer);
738 }
739 }
740#endif
741
742 /*
743 * Go from top to bottom through the windows, redrawing the ones that need
744 * it.
745 */
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 did_one = FALSE;
748#endif
749#ifdef FEAT_SEARCH_EXTRA
750 search_hl.rm.regprog = NULL;
751#endif
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (wp->w_redr_type != 0)
755 {
756 cursor_off();
757#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
758 if (!did_one)
759 {
760 did_one = TRUE;
761# ifdef FEAT_SEARCH_EXTRA
762 start_search_hl();
763# endif
764# ifdef FEAT_CLIPBOARD
765 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200766 if (clip_star.available && clip_isautosel_star())
767 clip_update_selection(&clip_star);
768 if (clip_plus.available && clip_isautosel_plus())
769 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770# endif
771#ifdef FEAT_GUI
772 /* Remove the cursor before starting to do anything, because
773 * scrolling may make it difficult to redraw the text under
774 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 {
777 gui_cursor_col = gui.cursor_col;
778 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200780 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 }
784#endif
785 win_update(wp);
786 }
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /* redraw status line after the window to minimize cursor movement */
789 if (wp->w_redr_status)
790 {
791 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200792 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795#if defined(FEAT_SEARCH_EXTRA)
796 end_search_hl();
797#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100798#ifdef FEAT_INS_EXPAND
799 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200800 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* Reset b_mod_set flags. Going through all windows is probably faster
804 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200808 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200812 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#ifdef FEAT_TEXT_PROP
824 // Display popup windows on top of the others.
825 update_popups();
826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_GUI
829 /* Redraw the cursor and update the scrollbars when all screen updating is
830 * done. */
831 if (gui.in_use)
832 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200833 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 mch_disable_flush();
836 out_flush(); /* required before updating the cursor */
837 mch_enable_flush();
838
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 /* Put the GUI position where the cursor was, gui_update_cursor()
840 * uses that. */
841 gui.col = gui_cursor_col;
842 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200843 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100845 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200846 screen_cur_col = gui.col;
847 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200848 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 else
850 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_scrollbars(FALSE);
852 }
853#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200854 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855}
856
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100857#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100858/*
859 * Prepare for updating one or more windows.
860 * Caller must check for "updating_screen" already set to avoid recursiveness.
861 */
862 static void
863update_prepare(void)
864{
865 cursor_off();
866 updating_screen = TRUE;
867#ifdef FEAT_GUI
868 /* Remove the cursor before starting to do anything, because scrolling may
869 * make it difficult to redraw the text under it. */
870 if (gui.in_use)
871 gui_undraw_cursor();
872#endif
873#ifdef FEAT_SEARCH_EXTRA
874 start_search_hl();
875#endif
876}
877
878/*
879 * Finish updating one or more windows.
880 */
881 static void
882update_finish(void)
883{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200884 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 showmode();
886
887# ifdef FEAT_SEARCH_EXTRA
888 end_search_hl();
889# endif
890
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200891 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892
893# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894 /* Redraw the cursor and update the scrollbars when all screen updating is
895 * done. */
896 if (gui.in_use)
897 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100898 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100899 gui_update_scrollbars(FALSE);
900 }
901# endif
902}
903#endif
904
Bram Moolenaar860cae12010-06-05 23:22:07 +0200905#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906/*
907 * Return TRUE if the cursor line in window "wp" may be concealed, according
908 * to the 'concealcursor' option.
909 */
910 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100911conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200912{
913 int c;
914
915 if (*wp->w_p_cocu == NUL)
916 return FALSE;
917 if (get_real_state() & VISUAL)
918 c = 'v';
919 else if (State & INSERT)
920 c = 'i';
921 else if (State & NORMAL)
922 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200923 else if (State & CMDLINE)
924 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200925 else
926 return FALSE;
927 return vim_strchr(wp->w_p_cocu, c) != NULL;
928}
929
930/*
931 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
932 */
933 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200934conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200935{
936 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
937 {
938 need_cursor_line_redraw = TRUE;
939 /* Need to recompute cursor column, e.g., when starting Visual mode
940 * without concealing. */
941 curs_columns(TRUE);
942 }
943}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#endif
945
Bram Moolenaar113e1072019-01-20 15:30:40 +0100946#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100948update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 win_T *wp;
951 int doit = FALSE;
952
953# ifdef FEAT_FOLDING
954 win_foldinfo.fi_level = 0;
955# endif
956
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100957 // update/delete a specific sign
958 redraw_buf_line_later(buf, lnum);
959
960 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (wp->w_redr_type != 0)
963 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100965 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200966 * happening (recursive call), messages on the screen or still starting up.
967 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100968 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200969 || State == ASKMORE || State == HITRETURN
970 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100971#ifdef FEAT_GUI
972 || gui.starting
973#endif
974 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 return;
976
977 /* update all windows that need updating */
978 update_prepare();
979
Bram Moolenaar29323592016-07-24 22:04:11 +0200980 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982 if (wp->w_redr_type != 0)
983 win_update(wp);
984 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200985 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 update_finish();
989}
990#endif
991
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200992#ifdef FEAT_TEXT_PROP
993 static void
994update_popups(void)
995{
996 win_T *wp;
997 win_T *lowest_wp;
998 int lowest_zindex;
999
1000 // Reset all the VALID_POPUP flags.
1001 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1002 wp->w_valid &= ~VALID_POPUP;
1003 for (wp = first_tab_popupwin; wp != NULL; wp = wp->w_next)
1004 wp->w_valid &= ~VALID_POPUP;
1005
1006 // TODO: don't redraw every popup every time.
1007 for (;;)
1008 {
1009 // Find the window with the lowest zindex that hasn't been updated yet,
1010 // so that the window with a higher zindex is drawn later, thus goes on
1011 // top.
1012 lowest_zindex = INT_MAX;
1013 lowest_wp = NULL;
1014 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1015 if ((wp->w_valid & VALID_POPUP) == 0
1016 && wp->w_zindex < lowest_zindex)
1017 {
1018 lowest_zindex = wp->w_zindex;
1019 lowest_wp = wp;
1020 }
1021 for (wp = first_tab_popupwin; wp != NULL; wp = wp->w_next)
1022 if ((wp->w_valid & VALID_POPUP) == 0
1023 && wp->w_zindex < lowest_zindex)
1024 {
1025 lowest_zindex = wp->w_zindex;
1026 lowest_wp = wp;
1027 }
1028
1029 if (lowest_wp == NULL)
1030 break;
1031 win_update(lowest_wp);
1032 lowest_wp->w_valid |= VALID_POPUP;
1033 }
1034}
1035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037#if defined(FEAT_GUI) || defined(PROTO)
1038/*
1039 * Update a single window, its status line and maybe the command line msg.
1040 * Used for the GUI scrollbar.
1041 */
1042 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001043updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001045 /* return if already busy updating */
1046 if (updating_screen)
1047 return;
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 update_prepare();
1050
1051#ifdef FEAT_CLIPBOARD
1052 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001053 if (clip_star.available && clip_isautosel_star())
1054 clip_update_selection(&clip_star);
1055 if (clip_plus.available && clip_isautosel_plus())
1056 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001058
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001060
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001061 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001062 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001063 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (wp->w_redr_status
1066# ifdef FEAT_CMDL_INFO
1067 || p_ru
1068# endif
1069# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001070 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071# endif
1072 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001073 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 update_finish();
1076}
1077#endif
1078
1079/*
1080 * Update a single window.
1081 *
1082 * This may cause the windows below it also to be redrawn (when clearing the
1083 * screen or scrolling lines).
1084 *
1085 * How the window is redrawn depends on wp->w_redr_type. Each type also
1086 * implies the one below it.
1087 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001088 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1090 * INVERTED redraw the changed part of the Visual area
1091 * INVERTED_ALL redraw the whole Visual area
1092 * VALID 1. scroll up/down to adjust for a changed w_topline
1093 * 2. update lines at the top when scrolled down
1094 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001095 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 * b_mod_top and b_mod_bot.
1097 * - if wp->w_redraw_top non-zero, redraw lines between
1098 * wp->w_redraw_top and wp->w_redr_bot.
1099 * - continue redrawing when syntax status is invalid.
1100 * 4. if scrolled up, update lines at the bottom.
1101 * This results in three areas that may need updating:
1102 * top: from first row to top_end (when scrolled down)
1103 * mid: from mid_start to mid_end (update inversion or changed text)
1104 * bot: from bot_start to last row (when scrolled up)
1105 */
1106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001107win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 buf_T *buf = wp->w_buffer;
1110 int type;
1111 int top_end = 0; /* Below last row of the top area that needs
1112 updating. 0 when no top area updating. */
1113 int mid_start = 999;/* first row of the mid area that needs
1114 updating. 999 when no mid area updating. */
1115 int mid_end = 0; /* Below last row of the mid area that needs
1116 updating. 0 when no mid area updating. */
1117 int bot_start = 999;/* first row of the bot area that needs
1118 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 int scrolled_down = FALSE; /* TRUE when scrolled down when
1120 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001122 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 int top_to_mod = FALSE; /* redraw above mod_top */
1124#endif
1125
1126 int row; /* current window row to display */
1127 linenr_T lnum; /* current buffer lnum to display */
1128 int idx; /* current index in w_lines[] */
1129 int srow; /* starting row of the current line */
1130
1131 int eof = FALSE; /* if TRUE, we hit the end of the file */
1132 int didline = FALSE; /* if TRUE, we finished the last line */
1133 int i;
1134 long j;
1135 static int recursive = FALSE; /* being called recursively */
1136 int old_botline = wp->w_botline;
1137#ifdef FEAT_FOLDING
1138 long fold_count;
1139#endif
1140#ifdef FEAT_SYN_HL
1141 /* remember what happened to the previous line, to know if
1142 * check_visual_highlight() can be used */
1143#define DID_NONE 1 /* didn't update a line */
1144#define DID_LINE 2 /* updated a normal line */
1145#define DID_FOLD 3 /* updated a folded line */
1146 int did_update = DID_NONE;
1147 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1148#endif
1149 linenr_T mod_top = 0;
1150 linenr_T mod_bot = 0;
1151#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1152 int save_got_int;
1153#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001154#ifdef SYN_TIME_LIMIT
1155 proftime_T syntax_tm;
1156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 type = wp->w_redr_type;
1159
1160 if (type == NOT_VALID)
1161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 wp->w_lines_valid = 0;
1164 }
1165
1166 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001167 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
1169 wp->w_redr_type = 0;
1170 return;
1171 }
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 /* Window is zero-width: Only need to draw the separator. */
1174 if (wp->w_width == 0)
1175 {
1176 /* draw the vertical separator right of this window */
1177 draw_vsep_win(wp, 0);
1178 wp->w_redr_type = 0;
1179 return;
1180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001182#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001183 // If this window contains a terminal, redraw works completely differently.
1184 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001185 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001186 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001187# ifdef FEAT_MENU
1188 /* Draw the window toolbar, if there is one. */
1189 if (winbar_height(wp) > 0)
1190 redraw_win_toolbar(wp);
1191# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001192 wp->w_redr_type = 0;
1193 return;
1194 }
1195#endif
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001198 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199#endif
1200
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001201#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001202 /* Force redraw when width of 'number' or 'relativenumber' column
1203 * changes. */
1204 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001205 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001206 {
1207 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001208 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001209 }
1210 else
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1214 {
1215 /*
1216 * When there are both inserted/deleted lines and specific lines to be
1217 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1218 * everything (only happens when redrawing is off for while).
1219 */
1220 type = NOT_VALID;
1221 }
1222 else
1223 {
1224 /*
1225 * Set mod_top to the first line that needs displaying because of
1226 * changes. Set mod_bot to the first line after the changes.
1227 */
1228 mod_top = wp->w_redraw_top;
1229 if (wp->w_redraw_bot != 0)
1230 mod_bot = wp->w_redraw_bot + 1;
1231 else
1232 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (buf->b_mod_set)
1234 {
1235 if (mod_top == 0 || mod_top > buf->b_mod_top)
1236 {
1237 mod_top = buf->b_mod_top;
1238#ifdef FEAT_SYN_HL
1239 /* Need to redraw lines above the change that may be included
1240 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001241 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001243 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (mod_top < 1)
1245 mod_top = 1;
1246 }
1247#endif
1248 }
1249 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1250 mod_bot = buf->b_mod_bot;
1251
1252#ifdef FEAT_SEARCH_EXTRA
1253 /* When 'hlsearch' is on and using a multi-line search pattern, a
1254 * change in one line may make the Search highlighting in a
1255 * previous line invalid. Simple solution: redraw all visible
1256 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001257 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001259 if (search_hl.rm.regprog != NULL
1260 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001262 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001263 {
1264 cur = wp->w_match_head;
1265 while (cur != NULL)
1266 {
1267 if (cur->match.regprog != NULL
1268 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001269 {
1270 top_to_mod = TRUE;
1271 break;
1272 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001273 cur = cur->next;
1274 }
1275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277 }
1278#ifdef FEAT_FOLDING
1279 if (mod_top != 0 && hasAnyFolding(wp))
1280 {
1281 linenr_T lnumt, lnumb;
1282
1283 /*
1284 * A change in a line can cause lines above it to become folded or
1285 * unfolded. Find the top most buffer line that may be affected.
1286 * If the line was previously folded and displayed, get the first
1287 * line of that fold. If the line is folded now, get the first
1288 * folded line. Use the minimum of these two.
1289 */
1290
1291 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1292 * the line below it. If there is no valid entry, use w_topline.
1293 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1294 * to this line. If there is no valid entry, use MAXLNUM. */
1295 lnumt = wp->w_topline;
1296 lnumb = MAXLNUM;
1297 for (i = 0; i < wp->w_lines_valid; ++i)
1298 if (wp->w_lines[i].wl_valid)
1299 {
1300 if (wp->w_lines[i].wl_lastlnum < mod_top)
1301 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1302 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1303 {
1304 lnumb = wp->w_lines[i].wl_lnum;
1305 /* When there is a fold column it might need updating
1306 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001307 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ++lnumb;
1309 }
1310 }
1311
1312 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1313 if (mod_top > lnumt)
1314 mod_top = lnumt;
1315
1316 /* Now do the same for the bottom line (one above mod_bot). */
1317 --mod_bot;
1318 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1319 ++mod_bot;
1320 if (mod_bot < lnumb)
1321 mod_bot = lnumb;
1322 }
1323#endif
1324
1325 /* When a change starts above w_topline and the end is below
1326 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001327 * If the end of the change is above w_topline: do like no change was
1328 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (mod_top != 0 && mod_top < wp->w_topline)
1330 {
1331 if (mod_bot > wp->w_topline)
1332 mod_top = wp->w_topline;
1333#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001334 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 top_end = 1;
1336#endif
1337 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001338
1339 /* When line numbers are displayed need to redraw all lines below
1340 * inserted/deleted lines. */
1341 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1342 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001344 wp->w_redraw_top = 0; // reset for next time
1345 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
1347 /*
1348 * When only displaying the lines at the top, set top_end. Used when
1349 * window has scrolled down for msg_scrolled.
1350 */
1351 if (type == REDRAW_TOP)
1352 {
1353 j = 0;
1354 for (i = 0; i < wp->w_lines_valid; ++i)
1355 {
1356 j += wp->w_lines[i].wl_size;
1357 if (j >= wp->w_upd_rows)
1358 {
1359 top_end = j;
1360 break;
1361 }
1362 }
1363 if (top_end == 0)
1364 /* not found (cannot happen?): redraw everything */
1365 type = NOT_VALID;
1366 else
1367 /* top area defined, the rest is VALID */
1368 type = VALID;
1369 }
1370
Bram Moolenaar367329b2007-08-30 11:53:22 +00001371 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001372 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1373 * non-zero and thus not FALSE) will indicate that screenclear() was not
1374 * called. */
1375 if (screen_cleared)
1376 screen_cleared = MAYBE;
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /*
1379 * If there are no changes on the screen that require a complete redraw,
1380 * handle three cases:
1381 * 1: we are off the top of the screen by a few lines: scroll down
1382 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1383 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1384 * w_lines[] that needs updating.
1385 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001386 if ((type == VALID || type == SOME_VALID
1387 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#ifdef FEAT_DIFF
1389 && !wp->w_botfill && !wp->w_old_botfill
1390#endif
1391 )
1392 {
1393 if (mod_top != 0 && wp->w_topline == mod_top)
1394 {
1395 /*
1396 * w_topline is the first changed line, the scrolling will be done
1397 * further down.
1398 */
1399 }
1400 else if (wp->w_lines[0].wl_valid
1401 && (wp->w_topline < wp->w_lines[0].wl_lnum
1402#ifdef FEAT_DIFF
1403 || (wp->w_topline == wp->w_lines[0].wl_lnum
1404 && wp->w_topfill > wp->w_old_topfill)
1405#endif
1406 ))
1407 {
1408 /*
1409 * New topline is above old topline: May scroll down.
1410 */
1411#ifdef FEAT_FOLDING
1412 if (hasAnyFolding(wp))
1413 {
1414 linenr_T ln;
1415
1416 /* count the number of lines we are off, counting a sequence
1417 * of folded lines as one */
1418 j = 0;
1419 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1420 {
1421 ++j;
1422 if (j >= wp->w_height - 2)
1423 break;
1424 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1425 }
1426 }
1427 else
1428#endif
1429 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1430 if (j < wp->w_height - 2) /* not too far off */
1431 {
1432 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1433#ifdef FEAT_DIFF
1434 /* insert extra lines for previously invisible filler lines */
1435 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1436 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1437 - wp->w_old_topfill;
1438#endif
1439 if (i < wp->w_height - 2) /* less than a screen off */
1440 {
1441 /*
1442 * Try to insert the correct number of lines.
1443 * If not the last window, delete the lines at the bottom.
1444 * win_ins_lines may fail when the terminal can't do it.
1445 */
1446 if (i > 0)
1447 check_for_delay(FALSE);
1448 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1449 {
1450 if (wp->w_lines_valid != 0)
1451 {
1452 /* Need to update rows that are new, stop at the
1453 * first one that scrolled down. */
1454 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
1457 /* Move the entries that were scrolled, disable
1458 * the entries for the lines to be redrawn. */
1459 if ((wp->w_lines_valid += j) > wp->w_height)
1460 wp->w_lines_valid = wp->w_height;
1461 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1462 wp->w_lines[idx] = wp->w_lines[idx - j];
1463 while (idx >= 0)
1464 wp->w_lines[idx--].wl_valid = FALSE;
1465 }
1466 }
1467 else
1468 mid_start = 0; /* redraw all lines */
1469 }
1470 else
1471 mid_start = 0; /* redraw all lines */
1472 }
1473 else
1474 mid_start = 0; /* redraw all lines */
1475 }
1476 else
1477 {
1478 /*
1479 * New topline is at or below old topline: May scroll up.
1480 * When topline didn't change, find first entry in w_lines[] that
1481 * needs updating.
1482 */
1483
1484 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1485 j = -1;
1486 row = 0;
1487 for (i = 0; i < wp->w_lines_valid; i++)
1488 {
1489 if (wp->w_lines[i].wl_valid
1490 && wp->w_lines[i].wl_lnum == wp->w_topline)
1491 {
1492 j = i;
1493 break;
1494 }
1495 row += wp->w_lines[i].wl_size;
1496 }
1497 if (j == -1)
1498 {
1499 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1500 * lines */
1501 mid_start = 0;
1502 }
1503 else
1504 {
1505 /*
1506 * Try to delete the correct number of lines.
1507 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1508 */
1509#ifdef FEAT_DIFF
1510 /* If the topline didn't change, delete old filler lines,
1511 * otherwise delete filler lines of the new topline... */
1512 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1513 row += wp->w_old_topfill;
1514 else
1515 row += diff_check_fill(wp, wp->w_topline);
1516 /* ... but don't delete new filler lines. */
1517 row -= wp->w_topfill;
1518#endif
1519 if (row > 0)
1520 {
1521 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001522 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1523 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 bot_start = wp->w_height - row;
1525 else
1526 mid_start = 0; /* redraw all lines */
1527 }
1528 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1529 {
1530 /*
1531 * Skip the lines (below the deleted lines) that are still
1532 * valid and don't need redrawing. Copy their info
1533 * upwards, to compensate for the deleted lines. Set
1534 * bot_start to the first row that needs redrawing.
1535 */
1536 bot_start = 0;
1537 idx = 0;
1538 for (;;)
1539 {
1540 wp->w_lines[idx] = wp->w_lines[j];
1541 /* stop at line that didn't fit, unless it is still
1542 * valid (no lines deleted) */
1543 if (row > 0 && bot_start + row
1544 + (int)wp->w_lines[j].wl_size > wp->w_height)
1545 {
1546 wp->w_lines_valid = idx + 1;
1547 break;
1548 }
1549 bot_start += wp->w_lines[idx++].wl_size;
1550
1551 /* stop at the last valid entry in w_lines[].wl_size */
1552 if (++j >= wp->w_lines_valid)
1553 {
1554 wp->w_lines_valid = idx;
1555 break;
1556 }
1557 }
1558#ifdef FEAT_DIFF
1559 /* Correct the first entry for filler lines at the top
1560 * when it won't get updated below. */
1561 if (wp->w_p_diff && bot_start > 0)
1562 wp->w_lines[0].wl_size =
1563 plines_win_nofill(wp, wp->w_topline, TRUE)
1564 + wp->w_topfill;
1565#endif
1566 }
1567 }
1568 }
1569
1570 /* When starting redraw in the first line, redraw all lines. When
1571 * there is only one window it's probably faster to clear the screen
1572 * first. */
1573 if (mid_start == 0)
1574 {
1575 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001576 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001577 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001578 /* Clear the screen when it was not done by win_del_lines() or
1579 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1580 * then. */
1581 if (screen_cleared != TRUE)
1582 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001583 /* The screen was cleared, redraw the tab pages line. */
1584 if (redraw_tabline)
1585 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001588
1589 /* When win_del_lines() or win_ins_lines() caused the screen to be
1590 * cleared (only happens for the first window) or when screenclear()
1591 * was called directly above, "must_redraw" will have been set to
1592 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1593 if (screen_cleared == TRUE)
1594 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else
1597 {
1598 /* Not VALID or INVERTED: redraw all lines. */
1599 mid_start = 0;
1600 mid_end = wp->w_height;
1601 }
1602
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001603 if (type == SOME_VALID)
1604 {
1605 /* SOME_VALID: redraw all lines. */
1606 mid_start = 0;
1607 mid_end = wp->w_height;
1608 type = NOT_VALID;
1609 }
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 /* check if we are updating or removing the inverted part */
1612 if ((VIsual_active && buf == curwin->w_buffer)
1613 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1614 {
1615 linenr_T from, to;
1616
1617 if (VIsual_active)
1618 {
1619 if (VIsual_active
1620 && (VIsual_mode != wp->w_old_visual_mode
1621 || type == INVERTED_ALL))
1622 {
1623 /*
1624 * If the type of Visual selection changed, redraw the whole
1625 * selection. Also when the ownership of the X selection is
1626 * gained or lost.
1627 */
1628 if (curwin->w_cursor.lnum < VIsual.lnum)
1629 {
1630 from = curwin->w_cursor.lnum;
1631 to = VIsual.lnum;
1632 }
1633 else
1634 {
1635 from = VIsual.lnum;
1636 to = curwin->w_cursor.lnum;
1637 }
1638 /* redraw more when the cursor moved as well */
1639 if (wp->w_old_cursor_lnum < from)
1640 from = wp->w_old_cursor_lnum;
1641 if (wp->w_old_cursor_lnum > to)
1642 to = wp->w_old_cursor_lnum;
1643 if (wp->w_old_visual_lnum < from)
1644 from = wp->w_old_visual_lnum;
1645 if (wp->w_old_visual_lnum > to)
1646 to = wp->w_old_visual_lnum;
1647 }
1648 else
1649 {
1650 /*
1651 * Find the line numbers that need to be updated: The lines
1652 * between the old cursor position and the current cursor
1653 * position. Also check if the Visual position changed.
1654 */
1655 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1656 {
1657 from = curwin->w_cursor.lnum;
1658 to = wp->w_old_cursor_lnum;
1659 }
1660 else
1661 {
1662 from = wp->w_old_cursor_lnum;
1663 to = curwin->w_cursor.lnum;
1664 if (from == 0) /* Visual mode just started */
1665 from = to;
1666 }
1667
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001668 if (VIsual.lnum != wp->w_old_visual_lnum
1669 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
1671 if (wp->w_old_visual_lnum < from
1672 && wp->w_old_visual_lnum != 0)
1673 from = wp->w_old_visual_lnum;
1674 if (wp->w_old_visual_lnum > to)
1675 to = wp->w_old_visual_lnum;
1676 if (VIsual.lnum < from)
1677 from = VIsual.lnum;
1678 if (VIsual.lnum > to)
1679 to = VIsual.lnum;
1680 }
1681 }
1682
1683 /*
1684 * If in block mode and changed column or curwin->w_curswant:
1685 * update all lines.
1686 * First compute the actual start and end column.
1687 */
1688 if (VIsual_mode == Ctrl_V)
1689 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001690 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001691#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001692 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaar404406a2014-10-09 13:24:43 +02001694 if (curwin->w_p_lbr)
1695 ve_flags = VE_ALL;
1696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001698#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001699 ve_flags = save_ve_flags;
1700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 ++toc;
1702 if (curwin->w_curswant == MAXCOL)
1703 toc = MAXCOL;
1704
1705 if (fromc != wp->w_old_cursor_fcol
1706 || toc != wp->w_old_cursor_lcol)
1707 {
1708 if (from > VIsual.lnum)
1709 from = VIsual.lnum;
1710 if (to < VIsual.lnum)
1711 to = VIsual.lnum;
1712 }
1713 wp->w_old_cursor_fcol = fromc;
1714 wp->w_old_cursor_lcol = toc;
1715 }
1716 }
1717 else
1718 {
1719 /* Use the line numbers of the old Visual area. */
1720 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1721 {
1722 from = wp->w_old_cursor_lnum;
1723 to = wp->w_old_visual_lnum;
1724 }
1725 else
1726 {
1727 from = wp->w_old_visual_lnum;
1728 to = wp->w_old_cursor_lnum;
1729 }
1730 }
1731
1732 /*
1733 * There is no need to update lines above the top of the window.
1734 */
1735 if (from < wp->w_topline)
1736 from = wp->w_topline;
1737
1738 /*
1739 * If we know the value of w_botline, use it to restrict the update to
1740 * the lines that are visible in the window.
1741 */
1742 if (wp->w_valid & VALID_BOTLINE)
1743 {
1744 if (from >= wp->w_botline)
1745 from = wp->w_botline - 1;
1746 if (to >= wp->w_botline)
1747 to = wp->w_botline - 1;
1748 }
1749
1750 /*
1751 * Find the minimal part to be updated.
1752 * Watch out for scrolling that made entries in w_lines[] invalid.
1753 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1754 * top_end; need to redraw from top_end to the "to" line.
1755 * A middle mouse click with a Visual selection may change the text
1756 * above the Visual area and reset wl_valid, do count these for
1757 * mid_end (in srow).
1758 */
1759 if (mid_start > 0)
1760 {
1761 lnum = wp->w_topline;
1762 idx = 0;
1763 srow = 0;
1764 if (scrolled_down)
1765 mid_start = top_end;
1766 else
1767 mid_start = 0;
1768 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1769 {
1770 if (wp->w_lines[idx].wl_valid)
1771 mid_start += wp->w_lines[idx].wl_size;
1772 else if (!scrolled_down)
1773 srow += wp->w_lines[idx].wl_size;
1774 ++idx;
1775# ifdef FEAT_FOLDING
1776 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1777 lnum = wp->w_lines[idx].wl_lnum;
1778 else
1779# endif
1780 ++lnum;
1781 }
1782 srow += mid_start;
1783 mid_end = wp->w_height;
1784 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1785 {
1786 if (wp->w_lines[idx].wl_valid
1787 && wp->w_lines[idx].wl_lnum >= to + 1)
1788 {
1789 /* Only update until first row of this line */
1790 mid_end = srow;
1791 break;
1792 }
1793 srow += wp->w_lines[idx].wl_size;
1794 }
1795 }
1796 }
1797
1798 if (VIsual_active && buf == curwin->w_buffer)
1799 {
1800 wp->w_old_visual_mode = VIsual_mode;
1801 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1802 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001803 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 wp->w_old_curswant = curwin->w_curswant;
1805 }
1806 else
1807 {
1808 wp->w_old_visual_mode = 0;
1809 wp->w_old_cursor_lnum = 0;
1810 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001811 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
1814#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1815 /* reset got_int, otherwise regexp won't work */
1816 save_got_int = got_int;
1817 got_int = 0;
1818#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001819#ifdef SYN_TIME_LIMIT
1820 /* Set the time limit to 'redrawtime'. */
1821 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001822 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#ifdef FEAT_FOLDING
1825 win_foldinfo.fi_level = 0;
1826#endif
1827
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001828#ifdef FEAT_MENU
1829 /*
1830 * Draw the window toolbar, if there is one.
1831 * TODO: only when needed.
1832 */
1833 if (winbar_height(wp) > 0)
1834 redraw_win_toolbar(wp);
1835#endif
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * Update all the window rows.
1839 */
1840 idx = 0; /* first entry in w_lines[].wl_size */
1841 row = 0;
1842 srow = 0;
1843 lnum = wp->w_topline; /* first line shown in window */
1844 for (;;)
1845 {
1846 /* stop updating when reached the end of the window (check for _past_
1847 * the end of the window is at the end of the loop) */
1848 if (row == wp->w_height)
1849 {
1850 didline = TRUE;
1851 break;
1852 }
1853
1854 /* stop updating when hit the end of the file */
1855 if (lnum > buf->b_ml.ml_line_count)
1856 {
1857 eof = TRUE;
1858 break;
1859 }
1860
1861 /* Remember the starting row of the line that is going to be dealt
1862 * with. It is used further down when the line doesn't fit. */
1863 srow = row;
1864
1865 /*
1866 * Update a line when it is in an area that needs updating, when it
1867 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001868 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 * win_del_lines(), check if the current line includes it.
1870 * When syntax folding is being used, the saved syntax states will
1871 * already have been updated, we can't see where the syntax state is
1872 * the same again, just update until the end of the window.
1873 */
1874 if (row < top_end
1875 || (row >= mid_start && row < mid_end)
1876#ifdef FEAT_SEARCH_EXTRA
1877 || top_to_mod
1878#endif
1879 || idx >= wp->w_lines_valid
1880 || (row + wp->w_lines[idx].wl_size > bot_start)
1881 || (mod_top != 0
1882 && (lnum == mod_top
1883 || (lnum >= mod_top
1884 && (lnum < mod_bot
1885#ifdef FEAT_SYN_HL
1886 || did_update == DID_FOLD
1887 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001888 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 && (
1890# ifdef FEAT_FOLDING
1891 (foldmethodIsSyntax(wp)
1892 && hasAnyFolding(wp)) ||
1893# endif
1894 syntax_check_changed(lnum)))
1895#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001896#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001897 /* match in fixed position might need redraw
1898 * if lines were inserted or deleted */
1899 || (wp->w_match_head != NULL
1900 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 )))))
1903 {
1904#ifdef FEAT_SEARCH_EXTRA
1905 if (lnum == mod_top)
1906 top_to_mod = FALSE;
1907#endif
1908
1909 /*
1910 * When at start of changed lines: May scroll following lines
1911 * up or down to minimize redrawing.
1912 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001913 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 */
1915 if (lnum == mod_top
1916 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001917 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 int old_rows = 0;
1920 int new_rows = 0;
1921 int xtra_rows;
1922 linenr_T l;
1923
1924 /* Count the old number of window rows, using w_lines[], which
1925 * should still contain the sizes for the lines as they are
1926 * currently displayed. */
1927 for (i = idx; i < wp->w_lines_valid; ++i)
1928 {
1929 /* Only valid lines have a meaningful wl_lnum. Invalid
1930 * lines are part of the changed area. */
1931 if (wp->w_lines[i].wl_valid
1932 && wp->w_lines[i].wl_lnum == mod_bot)
1933 break;
1934 old_rows += wp->w_lines[i].wl_size;
1935#ifdef FEAT_FOLDING
1936 if (wp->w_lines[i].wl_valid
1937 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1938 {
1939 /* Must have found the last valid entry above mod_bot.
1940 * Add following invalid entries. */
1941 ++i;
1942 while (i < wp->w_lines_valid
1943 && !wp->w_lines[i].wl_valid)
1944 old_rows += wp->w_lines[i++].wl_size;
1945 break;
1946 }
1947#endif
1948 }
1949
1950 if (i >= wp->w_lines_valid)
1951 {
1952 /* We can't find a valid line below the changed lines,
1953 * need to redraw until the end of the window.
1954 * Inserting/deleting lines has no use. */
1955 bot_start = 0;
1956 }
1957 else
1958 {
1959 /* Able to count old number of rows: Count new window
1960 * rows, and may insert/delete lines */
1961 j = idx;
1962 for (l = lnum; l < mod_bot; ++l)
1963 {
1964#ifdef FEAT_FOLDING
1965 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1966 ++new_rows;
1967 else
1968#endif
1969#ifdef FEAT_DIFF
1970 if (l == wp->w_topline)
1971 new_rows += plines_win_nofill(wp, l, TRUE)
1972 + wp->w_topfill;
1973 else
1974#endif
1975 new_rows += plines_win(wp, l, TRUE);
1976 ++j;
1977 if (new_rows > wp->w_height - row - 2)
1978 {
1979 /* it's getting too much, must redraw the rest */
1980 new_rows = 9999;
1981 break;
1982 }
1983 }
1984 xtra_rows = new_rows - old_rows;
1985 if (xtra_rows < 0)
1986 {
1987 /* May scroll text up. If there is not enough
1988 * remaining text or scrolling fails, must redraw the
1989 * rest. If scrolling works, must redraw the text
1990 * below the scrolled text. */
1991 if (row - xtra_rows >= wp->w_height - 2)
1992 mod_bot = MAXLNUM;
1993 else
1994 {
1995 check_for_delay(FALSE);
1996 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001997 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 mod_bot = MAXLNUM;
1999 else
2000 bot_start = wp->w_height + xtra_rows;
2001 }
2002 }
2003 else if (xtra_rows > 0)
2004 {
2005 /* May scroll text down. If there is not enough
2006 * remaining text of scrolling fails, must redraw the
2007 * rest. */
2008 if (row + xtra_rows >= wp->w_height - 2)
2009 mod_bot = MAXLNUM;
2010 else
2011 {
2012 check_for_delay(FALSE);
2013 if (win_ins_lines(wp, row + old_rows,
2014 xtra_rows, FALSE, FALSE) == FAIL)
2015 mod_bot = MAXLNUM;
2016 else if (top_end > row + old_rows)
2017 /* Scrolled the part at the top that requires
2018 * updating down. */
2019 top_end += xtra_rows;
2020 }
2021 }
2022
2023 /* When not updating the rest, may need to move w_lines[]
2024 * entries. */
2025 if (mod_bot != MAXLNUM && i != j)
2026 {
2027 if (j < i)
2028 {
2029 int x = row + new_rows;
2030
2031 /* move entries in w_lines[] upwards */
2032 for (;;)
2033 {
2034 /* stop at last valid entry in w_lines[] */
2035 if (i >= wp->w_lines_valid)
2036 {
2037 wp->w_lines_valid = j;
2038 break;
2039 }
2040 wp->w_lines[j] = wp->w_lines[i];
2041 /* stop at a line that won't fit */
2042 if (x + (int)wp->w_lines[j].wl_size
2043 > wp->w_height)
2044 {
2045 wp->w_lines_valid = j + 1;
2046 break;
2047 }
2048 x += wp->w_lines[j++].wl_size;
2049 ++i;
2050 }
2051 if (bot_start > x)
2052 bot_start = x;
2053 }
2054 else /* j > i */
2055 {
2056 /* move entries in w_lines[] downwards */
2057 j -= i;
2058 wp->w_lines_valid += j;
2059 if (wp->w_lines_valid > wp->w_height)
2060 wp->w_lines_valid = wp->w_height;
2061 for (i = wp->w_lines_valid; i - j >= idx; --i)
2062 wp->w_lines[i] = wp->w_lines[i - j];
2063
2064 /* The w_lines[] entries for inserted lines are
2065 * now invalid, but wl_size may be used above.
2066 * Reset to zero. */
2067 while (i >= idx)
2068 {
2069 wp->w_lines[i].wl_size = 0;
2070 wp->w_lines[i--].wl_valid = FALSE;
2071 }
2072 }
2073 }
2074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076
2077#ifdef FEAT_FOLDING
2078 /*
2079 * When lines are folded, display one line for all of them.
2080 * Otherwise, display normally (can be several display lines when
2081 * 'wrap' is on).
2082 */
2083 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2084 if (fold_count != 0)
2085 {
2086 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2087 ++row;
2088 --fold_count;
2089 wp->w_lines[idx].wl_folded = TRUE;
2090 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2091# ifdef FEAT_SYN_HL
2092 did_update = DID_FOLD;
2093# endif
2094 }
2095 else
2096#endif
2097 if (idx < wp->w_lines_valid
2098 && wp->w_lines[idx].wl_valid
2099 && wp->w_lines[idx].wl_lnum == lnum
2100 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002101 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 && srow + wp->w_lines[idx].wl_size > wp->w_height
2103#ifdef FEAT_DIFF
2104 && diff_check_fill(wp, lnum) == 0
2105#endif
2106 )
2107 {
2108 /* This line is not going to fit. Don't draw anything here,
2109 * will draw "@ " lines below. */
2110 row = wp->w_height + 1;
2111 }
2112 else
2113 {
2114#ifdef FEAT_SEARCH_EXTRA
2115 prepare_search_hl(wp, lnum);
2116#endif
2117#ifdef FEAT_SYN_HL
2118 /* Let the syntax stuff know we skipped a few lines. */
2119 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002120 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 syntax_end_parsing(syntax_last_parsed + 1);
2122#endif
2123
2124 /*
2125 * Display one line.
2126 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002127 row = win_line(wp, lnum, srow, wp->w_height,
2128 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
2130#ifdef FEAT_FOLDING
2131 wp->w_lines[idx].wl_folded = FALSE;
2132 wp->w_lines[idx].wl_lastlnum = lnum;
2133#endif
2134#ifdef FEAT_SYN_HL
2135 did_update = DID_LINE;
2136 syntax_last_parsed = lnum;
2137#endif
2138 }
2139
2140 wp->w_lines[idx].wl_lnum = lnum;
2141 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002142
2143 /* Past end of the window or end of the screen. Note that after
2144 * resizing wp->w_height may be end up too big. That's a problem
2145 * elsewhere, but prevent a crash here. */
2146 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
2148 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002149 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2151 ++idx;
2152 break;
2153 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002154 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 wp->w_lines[idx].wl_size = row - srow;
2156 ++idx;
2157#ifdef FEAT_FOLDING
2158 lnum += fold_count + 1;
2159#else
2160 ++lnum;
2161#endif
2162 }
2163 else
2164 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002165 if (wp->w_p_rnu)
2166 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002167#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002168 // 'relativenumber' set: The text doesn't need to be drawn, but
2169 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002170 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2171 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002172 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002173 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002174#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002175 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002176 }
2177
2178 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 row += wp->w_lines[idx++].wl_size;
2180 if (row > wp->w_height) /* past end of screen */
2181 break;
2182#ifdef FEAT_FOLDING
2183 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2184#else
2185 ++lnum;
2186#endif
2187#ifdef FEAT_SYN_HL
2188 did_update = DID_NONE;
2189#endif
2190 }
2191
2192 if (lnum > buf->b_ml.ml_line_count)
2193 {
2194 eof = TRUE;
2195 break;
2196 }
2197 }
2198 /*
2199 * End of loop over all window lines.
2200 */
2201
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002202#ifdef FEAT_VTP
2203 /* Rewrite the character at the end of the screen line. */
2204 if (use_vtp())
2205 {
2206 int i;
2207
2208 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002209 if (enc_utf8)
2210 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2211 LineOffset[i] + screen_Columns) > 1)
2212 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2213 else
2214 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2215 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002216 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2217 }
2218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 if (idx > wp->w_lines_valid)
2221 wp->w_lines_valid = idx;
2222
2223#ifdef FEAT_SYN_HL
2224 /*
2225 * Let the syntax stuff know we stop parsing here.
2226 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002227 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 syntax_end_parsing(syntax_last_parsed + 1);
2229#endif
2230
2231 /*
2232 * If we didn't hit the end of the file, and we didn't finish the last
2233 * line we were working on, then the line didn't fit.
2234 */
2235 wp->w_empty_rows = 0;
2236#ifdef FEAT_DIFF
2237 wp->w_filler_rows = 0;
2238#endif
2239 if (!eof && !didline)
2240 {
2241 if (lnum == wp->w_topline)
2242 {
2243 /*
2244 * Single line that does not fit!
2245 * Don't overwrite it, it can be edited.
2246 */
2247 wp->w_botline = lnum + 1;
2248 }
2249#ifdef FEAT_DIFF
2250 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2251 {
2252 /* Window ends in filler lines. */
2253 wp->w_botline = lnum;
2254 wp->w_filler_rows = wp->w_height - srow;
2255 }
2256#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002257 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2258 {
2259 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2260
2261 /*
2262 * Last line isn't finished: Display "@@@" in the last screen line.
2263 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002264 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002265 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002266 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002267 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002268 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 set_empty_rows(wp, srow);
2270 wp->w_botline = lnum;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2273 {
2274 /*
2275 * Last line isn't finished: Display "@@@" at the end.
2276 */
2277 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2278 W_WINROW(wp) + wp->w_height,
2279 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002280 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 set_empty_rows(wp, srow);
2282 wp->w_botline = lnum;
2283 }
2284 else
2285 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002286 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 wp->w_botline = lnum;
2288 }
2289 }
2290 else
2291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (eof) /* we hit the end of the file */
2294 {
2295 wp->w_botline = buf->b_ml.ml_line_count + 1;
2296#ifdef FEAT_DIFF
2297 j = diff_check_fill(wp, wp->w_botline);
2298 if (j > 0 && !wp->w_botfill)
2299 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002300 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 if (char2cells(fill_diff) > 1)
2302 i = '-';
2303 else
2304 i = fill_diff;
2305 if (row + j > wp->w_height)
2306 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002307 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 row += j;
2309 }
2310#endif
2311 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002312 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 wp->w_botline = lnum;
2314
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002315 // Make sure the rest of the screen is blank
2316 // put '~'s on rows that aren't part of the file.
2317 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002320#ifdef SYN_TIME_LIMIT
2321 syn_set_timeout(NULL);
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /* Reset the type of redrawing required, the window has been updated. */
2325 wp->w_redr_type = 0;
2326#ifdef FEAT_DIFF
2327 wp->w_old_topfill = wp->w_topfill;
2328 wp->w_old_botfill = wp->w_botfill;
2329#endif
2330
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002331 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 /*
2334 * There is a trick with w_botline. If we invalidate it on each
2335 * change that might modify it, this will cause a lot of expensive
2336 * calls to plines() in update_topline() each time. Therefore the
2337 * value of w_botline is often approximated, and this value is used to
2338 * compute the value of w_topline. If the value of w_botline was
2339 * wrong, check that the value of w_topline is correct (cursor is on
2340 * the visible part of the text). If it's not, we need to redraw
2341 * again. Mostly this just means scrolling up a few lines, so it
2342 * doesn't look too bad. Only do this for the current window (where
2343 * changes are relevant).
2344 */
2345 wp->w_valid |= VALID_BOTLINE;
2346 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2347 {
2348 recursive = TRUE;
2349 curwin->w_valid &= ~VALID_TOPLINE;
2350 update_topline(); /* may invalidate w_botline again */
2351 if (must_redraw != 0)
2352 {
2353 /* Don't update for changes in buffer again. */
2354 i = curbuf->b_mod_set;
2355 curbuf->b_mod_set = FALSE;
2356 win_update(curwin);
2357 must_redraw = 0;
2358 curbuf->b_mod_set = i;
2359 }
2360 recursive = FALSE;
2361 }
2362 }
2363
2364#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2365 /* restore got_int, unless CTRL-C was hit while redrawing */
2366 if (!got_int)
2367 got_int = save_got_int;
2368#endif
2369}
2370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002372 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2373 * Return the new offset.
2374 */
2375 static int
2376screen_fill_end(
2377 win_T *wp,
2378 int c1,
2379 int c2,
2380 int off,
2381 int width,
2382 int row,
2383 int endrow,
2384 int attr)
2385{
2386 int nn = off + width;
2387
2388 if (nn > wp->w_width)
2389 nn = wp->w_width;
2390#ifdef FEAT_RIGHTLEFT
2391 if (wp->w_p_rl)
2392 {
2393 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2394 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2395 c1, c2, attr);
2396 }
2397 else
2398#endif
2399 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2400 wp->w_wincol + off, (int)wp->w_wincol + nn,
2401 c1, c2, attr);
2402 return nn;
2403}
2404
2405/*
2406 * Clear lines near the end the window and mark the unused lines with "c1".
2407 * use "c2" as the filler character.
2408 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 */
2410 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002411win_draw_end(
2412 win_T *wp,
2413 int c1,
2414 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002415 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002416 int row,
2417 int endrow,
2418 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 int n = 0;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002421
2422 if (draw_margin)
2423 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002424#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002425 int fdc = compute_foldcolumn(wp, 0);
2426
2427 if (fdc > 0)
2428 // draw the fold column
2429 n = screen_fill_end(wp, ' ', ' ', n, fdc,
2430 row, endrow, HL_ATTR(HLF_FC));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002431#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002432#ifdef FEAT_SIGNS
2433 if (signcolumn_on(wp))
2434 // draw the sign column
2435 n = screen_fill_end(wp, ' ', ' ', n, 2,
2436 row, endrow, HL_ATTR(HLF_SC));
2437#endif
2438 if ((wp->w_p_nu || wp->w_p_rnu)
2439 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2440 // draw the number column
2441 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
2442 row, endrow, HL_ATTR(HLF_N));
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
2445#ifdef FEAT_RIGHTLEFT
2446 if (wp->w_p_rl)
2447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002449 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002450 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002453 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455 else
2456#endif
2457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002459 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002460 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 set_empty_rows(wp, row);
2464}
2465
Bram Moolenaar1a384422010-07-14 19:53:30 +02002466#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002467/*
2468 * Advance **color_cols and return TRUE when there are columns to draw.
2469 */
2470 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002471advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002472{
2473 while (**color_cols >= 0 && vcol > **color_cols)
2474 ++*color_cols;
2475 return (**color_cols >= 0);
2476}
2477#endif
2478
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002479#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2480/*
2481 * Copy "text" to ScreenLines using "attr".
2482 * Returns the next screen column.
2483 */
2484 static int
2485text_to_screenline(win_T *wp, char_u *text, int col)
2486{
2487 int off = (int)(current_ScreenLine - ScreenLines);
2488
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002489 if (has_mbyte)
2490 {
2491 int cells;
2492 int u8c, u8cc[MAX_MCO];
2493 int i;
2494 int idx;
2495 int c_len;
2496 char_u *p;
2497# ifdef FEAT_ARABIC
2498 int prev_c = 0; /* previous Arabic character */
2499 int prev_c1 = 0; /* first composing char for prev_c */
2500# endif
2501
2502# ifdef FEAT_RIGHTLEFT
2503 if (wp->w_p_rl)
2504 idx = off;
2505 else
2506# endif
2507 idx = off + col;
2508
2509 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2510 for (p = text; *p != NUL; )
2511 {
2512 cells = (*mb_ptr2cells)(p);
2513 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002514 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002515# ifdef FEAT_RIGHTLEFT
2516 - (wp->w_p_rl ? col : 0)
2517# endif
2518 )
2519 break;
2520 ScreenLines[idx] = *p;
2521 if (enc_utf8)
2522 {
2523 u8c = utfc_ptr2char(p, u8cc);
2524 if (*p < 0x80 && u8cc[0] == 0)
2525 {
2526 ScreenLinesUC[idx] = 0;
2527#ifdef FEAT_ARABIC
2528 prev_c = u8c;
2529#endif
2530 }
2531 else
2532 {
2533#ifdef FEAT_ARABIC
2534 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2535 {
2536 /* Do Arabic shaping. */
2537 int pc, pc1, nc;
2538 int pcc[MAX_MCO];
2539 int firstbyte = *p;
2540
2541 /* The idea of what is the previous and next
2542 * character depends on 'rightleft'. */
2543 if (wp->w_p_rl)
2544 {
2545 pc = prev_c;
2546 pc1 = prev_c1;
2547 nc = utf_ptr2char(p + c_len);
2548 prev_c1 = u8cc[0];
2549 }
2550 else
2551 {
2552 pc = utfc_ptr2char(p + c_len, pcc);
2553 nc = prev_c;
2554 pc1 = pcc[0];
2555 }
2556 prev_c = u8c;
2557
2558 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2559 pc, pc1, nc);
2560 ScreenLines[idx] = firstbyte;
2561 }
2562 else
2563 prev_c = u8c;
2564#endif
2565 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002566 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002567 for (i = 0; i < Screen_mco; ++i)
2568 {
2569 ScreenLinesC[i][idx] = u8cc[i];
2570 if (u8cc[i] == 0)
2571 break;
2572 }
2573 }
2574 if (cells > 1)
2575 ScreenLines[idx + 1] = 0;
2576 }
2577 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2578 /* double-byte single width character */
2579 ScreenLines2[idx] = p[1];
2580 else if (cells > 1)
2581 /* double-width character */
2582 ScreenLines[idx + 1] = p[1];
2583 col += cells;
2584 idx += cells;
2585 p += c_len;
2586 }
2587 }
2588 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002589 {
2590 int len = (int)STRLEN(text);
2591
Bram Moolenaar02631462017-09-22 15:20:32 +02002592 if (len > wp->w_width - col)
2593 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002594 if (len > 0)
2595 {
2596#ifdef FEAT_RIGHTLEFT
2597 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002598 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002599 else
2600#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002601 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002602 col += len;
2603 }
2604 }
2605 return col;
2606}
2607#endif
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609#ifdef FEAT_FOLDING
2610/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002611 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2612 * space is available for window "wp", minus "col".
2613 */
2614 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002615compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002616{
2617 int fdc = wp->w_p_fdc;
2618 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002619 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002620
2621 if (fdc > wwidth - (col + wmw))
2622 fdc = wwidth - (col + wmw);
2623 return fdc;
2624}
2625
2626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * Display one folded line.
2628 */
2629 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002630fold_line(
2631 win_T *wp,
2632 long fold_count,
2633 foldinfo_T *foldinfo,
2634 linenr_T lnum,
2635 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002637 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 pos_T *top, *bot;
2639 linenr_T lnume = lnum + fold_count - 1;
2640 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002641 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int col;
2644 int txtcol;
2645 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002646 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
2648 /* Build the fold line:
2649 * 1. Add the cmdwin_type for the command-line window
2650 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002651 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 * 4. Compose the text
2653 * 5. Add the text
2654 * 6. set highlighting for the Visual area an other text
2655 */
2656 col = 0;
2657
2658 /*
2659 * 1. Add the cmdwin_type for the command-line window
2660 * Ignores 'rightleft', this window is never right-left.
2661 */
2662#ifdef FEAT_CMDWIN
2663 if (cmdwin_type != 0 && wp == curwin)
2664 {
2665 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002666 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (enc_utf8)
2668 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 ++col;
2670 }
2671#endif
2672
2673 /*
2674 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002675 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002677 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (fdc > 0)
2679 {
2680 fill_foldcolumn(buf, wp, TRUE, lnum);
2681#ifdef FEAT_RIGHTLEFT
2682 if (wp->w_p_rl)
2683 {
2684 int i;
2685
Bram Moolenaar02631462017-09-22 15:20:32 +02002686 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002687 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /* reverse the fold column */
2689 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002690 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692 else
2693#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002694 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 col += fdc;
2696 }
2697
2698#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002699# define RL_MEMSET(p, v, l) \
2700 do { \
2701 if (wp->w_p_rl) \
2702 for (ri = 0; ri < l; ++ri) \
2703 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2704 else \
2705 for (ri = 0; ri < l; ++ri) \
2706 ScreenAttrs[off + (p) + ri] = v; \
2707 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002709# define RL_MEMSET(p, v, l) \
2710 do { \
2711 for (ri = 0; ri < l; ++ri) \
2712 ScreenAttrs[off + (p) + ri] = v; \
2713 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#endif
2715
Bram Moolenaar64486672010-05-16 15:46:46 +02002716 /* Set all attributes of the 'number' or 'relativenumber' column and the
2717 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002718 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720#ifdef FEAT_SIGNS
2721 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002722 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002724 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (len > 0)
2726 {
2727 if (len > 2)
2728 len = 2;
2729# ifdef FEAT_RIGHTLEFT
2730 if (wp->w_p_rl)
2731 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002732 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002733 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else
2735# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002736 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 col += len;
2738 }
2739 }
2740#endif
2741
2742 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002743 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002745 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002747 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 if (len > 0)
2749 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002750 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002751 long num;
2752 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002753
2754 if (len > w + 1)
2755 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002756
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002757 if (wp->w_p_nu && !wp->w_p_rnu)
2758 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002759 num = (long)lnum;
2760 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002761 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002762 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002763 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002764 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002765 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002766 /* 'number' + 'relativenumber': cursor line shows absolute
2767 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002768 num = lnum;
2769 fmt = "%-*ld ";
2770 }
2771 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002772
Bram Moolenaar700e7342013-01-30 12:31:36 +01002773 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#ifdef FEAT_RIGHTLEFT
2775 if (wp->w_p_rl)
2776 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002777 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002778 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 else
2780#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002781 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 col += len;
2783 }
2784 }
2785
2786 /*
2787 * 4. Compose the folded-line string with 'foldtext', if set.
2788 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002789 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 txtcol = col; /* remember where text starts */
2792
2793 /*
2794 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2795 * Right-left text is put in columns 0 - number-col, normal text is put
2796 * in columns number-col - window-width.
2797 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002798 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /* Fill the rest of the line with the fold filler */
2801#ifdef FEAT_RIGHTLEFT
2802 if (wp->w_p_rl)
2803 col -= txtcol;
2804#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002805 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806#ifdef FEAT_RIGHTLEFT
2807 - (wp->w_p_rl ? txtcol : 0)
2808#endif
2809 )
2810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 if (enc_utf8)
2812 {
2813 if (fill_fold >= 0x80)
2814 {
2815 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002816 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002817 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002822 ScreenLines[off + col] = fill_fold;
2823 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002824 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002826 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002827 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829
2830 if (text != buf)
2831 vim_free(text);
2832
2833 /*
2834 * 6. set highlighting for the Visual area an other text.
2835 * If all folded lines are in the Visual area, highlight the line.
2836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2838 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002839 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
2841 /* Visual is after curwin->w_cursor */
2842 top = &curwin->w_cursor;
2843 bot = &VIsual;
2844 }
2845 else
2846 {
2847 /* Visual is before curwin->w_cursor */
2848 top = &VIsual;
2849 bot = &curwin->w_cursor;
2850 }
2851 if (lnum >= top->lnum
2852 && lnume <= bot->lnum
2853 && (VIsual_mode != 'v'
2854 || ((lnum > top->lnum
2855 || (lnum == top->lnum
2856 && top->col == 0))
2857 && (lnume < bot->lnum
2858 || (lnume == bot->lnum
2859 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002860 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
2862 if (VIsual_mode == Ctrl_V)
2863 {
2864 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002865 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002867 if (wp->w_old_cursor_lcol != MAXCOL
2868 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002869 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 len = wp->w_old_cursor_lcol;
2871 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002872 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002873 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002874 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876 }
2877 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002880 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002885#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002886 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2887 if (wp->w_p_cc_cols)
2888 {
2889 int i = 0;
2890 int j = wp->w_p_cc_cols[i];
2891 int old_txtcol = txtcol;
2892
2893 while (j > -1)
2894 {
2895 txtcol += j;
2896 if (wp->w_p_wrap)
2897 txtcol -= wp->w_skipcol;
2898 else
2899 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002901 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002902 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002903 txtcol = old_txtcol;
2904 j = wp->w_p_cc_cols[++i];
2905 }
2906 }
2907
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002908 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002909 if (wp->w_p_cuc)
2910 {
2911 txtcol += wp->w_virtcol;
2912 if (wp->w_p_wrap)
2913 txtcol -= wp->w_skipcol;
2914 else
2915 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002916 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002917 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002918 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002919 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar02631462017-09-22 15:20:32 +02002922 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002923 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /*
2926 * Update w_cline_height and w_cline_folded if the cursor line was
2927 * updated (saves a call to plines() later).
2928 */
2929 if (wp == curwin
2930 && lnum <= curwin->w_cursor.lnum
2931 && lnume >= curwin->w_cursor.lnum)
2932 {
2933 curwin->w_cline_row = row;
2934 curwin->w_cline_height = 1;
2935 curwin->w_cline_folded = TRUE;
2936 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2937 }
2938}
2939
2940/*
2941 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2942 */
2943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002944copy_text_attr(
2945 int off,
2946 char_u *buf,
2947 int len,
2948 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002950 int i;
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (enc_utf8)
2954 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002955 for (i = 0; i < len; ++i)
2956 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957}
2958
2959/*
2960 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002961 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 */
2963 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002964fill_foldcolumn(
2965 char_u *p,
2966 win_T *wp,
2967 int closed, /* TRUE of FALSE */
2968 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 int i = 0;
2971 int level;
2972 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002973 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002974 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002977 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 level = win_foldinfo.fi_level;
2980 if (level > 0)
2981 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002982 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002983 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 /* If the column is too narrow, we start at the lowest level that
2986 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002987 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 if (first_level < 1)
2989 first_level = 1;
2990
Bram Moolenaar1c934292015-01-27 16:39:29 +01002991 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
2993 if (win_foldinfo.fi_lnum == lnum
2994 && first_level + i >= win_foldinfo.fi_low_level)
2995 p[i] = '-';
2996 else if (first_level == 1)
2997 p[i] = '|';
2998 else if (first_level + i <= 9)
2999 p[i] = '0' + first_level + i;
3000 else
3001 p[i] = '>';
3002 if (first_level + i == level)
3003 break;
3004 }
3005 }
3006 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003007 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008}
3009#endif /* FEAT_FOLDING */
3010
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003011#ifdef FEAT_TEXT_PROP
3012static textprop_T *current_text_props = NULL;
3013static buf_T *current_buf = NULL;
3014
3015 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003016text_prop_compare(const void *s1, const void *s2)
3017{
3018 int idx1, idx2;
3019 proptype_T *pt1, *pt2;
3020 colnr_T col1, col2;
3021
3022 idx1 = *(int *)s1;
3023 idx2 = *(int *)s2;
3024 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3025 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3026 if (pt1 == pt2)
3027 return 0;
3028 if (pt1 == NULL)
3029 return -1;
3030 if (pt2 == NULL)
3031 return 1;
3032 if (pt1->pt_priority != pt2->pt_priority)
3033 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3034 col1 = current_text_props[idx1].tp_col;
3035 col2 = current_text_props[idx2].tp_col;
3036 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3037}
3038#endif
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040/*
3041 * Display line "lnum" of window 'wp' on the screen.
3042 * Start at row "startrow", stop when "endrow" is reached.
3043 * wp->w_virtcol needs to be valid.
3044 *
3045 * Return the number of last row the line occupies.
3046 */
3047 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003048win_line(
3049 win_T *wp,
3050 linenr_T lnum,
3051 int startrow,
3052 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003053 int nochange UNUSED, // not updating for changed text
3054 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003056 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3058 int c = 0; /* init for GCC */
3059 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003060#ifdef FEAT_LINEBREAK
3061 long vcol_sbr = -1; /* virtual column after showbreak */
3062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 long vcol_prev = -1; /* "vcol" of previous character */
3064 char_u *line; /* current line */
3065 char_u *ptr; /* current position in "line" */
3066 int row; /* row in the window, excl w_winrow */
3067 int screen_row; /* row on the screen, incl w_winrow */
3068
3069 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3070 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003071 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003072 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003074 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 int extra_attr = 0; /* attributes when n_extra != 0 */
3076 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3077 displaying lcs_eol at end-of-line */
3078 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3079 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3080
3081 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3082 int saved_n_extra = 0;
3083 char_u *saved_p_extra = NULL;
3084 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003085 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int saved_char_attr = 0;
3087
3088 int n_attr = 0; /* chars with special attr */
3089 int saved_attr2 = 0; /* char_attr saved for n_attr */
3090 int n_attr3 = 0; /* chars with overruling special attr */
3091 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3092
3093 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3094
3095 int fromcol, tocol; /* start/end of inverting */
3096 int fromcol_prev = -2; /* start of inverting after cursor */
3097 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003099 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 pos_T pos;
3101 long v;
3102
3103 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003104 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3106 in this line */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003107 int vi_attr = 0; /* attributes for Visual and incsearch
3108 highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 int area_attr = 0; /* attributes desired by highlighting */
3110 int search_attr = 0; /* attributes desired by 'hlsearch' */
3111#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003112 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 int syntax_attr = 0; /* attributes desired by syntax */
3114 int has_syntax = FALSE; /* this buffer has syntax highl. */
3115 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003116 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003117 int draw_color_col = FALSE; /* highlight colorcolumn */
3118 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003119#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003120#ifdef FEAT_TEXT_PROP
3121 int text_prop_count;
3122 int text_prop_next = 0; // next text property to use
3123 textprop_T *text_props = NULL;
3124 int *text_prop_idxs = NULL;
3125 int text_props_active = 0;
3126 proptype_T *text_prop_type = NULL;
3127 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003128 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003129#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003130#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003131 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003132# define SPWORDLEN 150
3133 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003134 int nextlinecol = 0; /* column where nextline[] starts */
3135 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003136 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003137 int spell_attr = 0; /* attributes desired by spelling */
3138 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003139 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3140 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003141 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003142 static int cap_col = -1; /* column to check for Cap word */
3143 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003144 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003146 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 int multi_attr = 0; /* attributes desired by multibyte */
3148 int mb_l = 1; /* multi-byte byte length */
3149 int mb_c = 0; /* decoded multi-byte character */
3150 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003151 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#ifdef FEAT_DIFF
3153 int filler_lines; /* nr of filler lines to be drawn */
3154 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003155 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 int change_start = MAXCOL; /* first col of changed area */
3157 int change_end = -1; /* last col of changed area */
3158#endif
3159 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3160#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003161 int need_showbreak = FALSE; /* overlong line, skipping first x
3162 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003164#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003165 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003167 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
3169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003170 matchitem_T *cur; /* points to the match list */
3171 match_T *shl; /* points to search_hl or a match */
3172 int shl_flag; /* flag to indicate whether search_hl
3173 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003174 int pos_inprogress; /* marks that position match search is
3175 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003176 int prevcol_hl_flag; /* flag to indicate whether prevcol
3177 equals startcol of search_hl or one
3178 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#endif
3180#ifdef FEAT_ARABIC
3181 int prev_c = 0; /* previous Arabic character */
3182 int prev_c1 = 0; /* first composing char for prev_c */
3183#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003184#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003185 int did_line_attr = 0;
3186#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003187#ifdef FEAT_TERMINAL
3188 int get_term_attr = FALSE;
3189#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003190 int win_attr = 0; // background for whole window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 /* draw_state: items that are drawn in sequence: */
3193#define WL_START 0 /* nothing done yet */
3194#ifdef FEAT_CMDWIN
3195# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3196#else
3197# define WL_CMDLINE WL_START
3198#endif
3199#ifdef FEAT_FOLDING
3200# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3201#else
3202# define WL_FOLD WL_CMDLINE
3203#endif
3204#ifdef FEAT_SIGNS
3205# define WL_SIGN WL_FOLD + 1 /* column for signs */
3206#else
3207# define WL_SIGN WL_FOLD /* column for signs */
3208#endif
3209#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003210#ifdef FEAT_LINEBREAK
3211# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003213# define WL_BRI WL_NR
3214#endif
3215#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3216# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3217#else
3218# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220#define WL_LINE WL_SBR + 1 /* text in the line */
3221 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003222#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 int feedback_col = 0;
3224 int feedback_old_attr = -1;
3225#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003226 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227
Bram Moolenaar860cae12010-06-05 23:22:07 +02003228#ifdef FEAT_CONCEAL
3229 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003230 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003231 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003232 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003233 int is_concealing = FALSE;
3234 int boguscols = 0; /* nonexistent columns added to force
3235 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003236 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003237 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003238 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003239 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003240# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003241# define FIX_FOR_BOGUSCOLS \
3242 { \
3243 n_extra += vcol_off; \
3244 vcol -= vcol_off; \
3245 vcol_off = 0; \
3246 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003247 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003248 boguscols = 0; \
3249 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003250#else
3251# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254 if (startrow > endrow) /* past the end already! */
3255 return startrow;
3256
3257 row = startrow;
3258 screen_row = row + W_WINROW(wp);
3259
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003260 if (!number_only)
3261 {
3262 /*
3263 * To speed up the loop below, set extra_check when there is linebreak,
3264 * trailing white space and/or syntax processing to be done.
3265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003267 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268#endif
3269#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003270 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003271# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003272 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003273# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003274 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003276 /* Prepare for syntax highlighting in this line. When there is an
3277 * error, stop syntax highlighting. */
3278 save_did_emsg = did_emsg;
3279 did_emsg = FALSE;
3280 syntax_start(wp, lnum);
3281 if (did_emsg)
3282 wp->w_s->b_syn_error = TRUE;
3283 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003284 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003285 did_emsg = save_did_emsg;
3286#ifdef SYN_TIME_LIMIT
3287 if (!wp->w_s->b_syn_slow)
3288#endif
3289 {
3290 has_syntax = TRUE;
3291 extra_check = TRUE;
3292 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003295
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003296 /* Check for columns to display for 'colorcolumn'. */
3297 color_cols = wp->w_p_cc_cols;
3298 if (color_cols != NULL)
3299 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003300#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003301
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003302#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003303 if (term_show_buffer(wp->w_buffer))
3304 {
3305 extra_check = TRUE;
3306 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003307 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003308 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003309#endif
3310
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003311#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003312 if (wp->w_p_spell
3313 && *wp->w_s->b_p_spl != NUL
3314 && wp->w_s->b_langp.ga_len > 0
3315 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003316 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003317 /* Prepare for spell checking. */
3318 has_spell = TRUE;
3319 extra_check = TRUE;
3320
Bram Moolenaar7701f302018-10-02 21:20:32 +02003321 /* Get the start of the next line, so that words that wrap to the
3322 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003323 * Trick: skip a few chars for C/shell/Vim comments */
3324 nextline[SPWORDLEN] = NUL;
3325 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3326 {
3327 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3328 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3329 }
3330
Bram Moolenaar7701f302018-10-02 21:20:32 +02003331 /* When a word wrapped from the previous line the start of the
3332 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003333 if (lnum == checked_lnum)
3334 cur_checked_col = checked_col;
3335 checked_lnum = 0;
3336
3337 /* When there was a sentence end in the previous line may require a
3338 * word starting with capital in this line. In line 1 always check
3339 * the first word. */
3340 if (lnum != capcol_lnum)
3341 cap_col = -1;
3342 if (lnum == 1)
3343 cap_col = 0;
3344 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#endif
3347
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003348 /*
3349 * handle visual active in this window
3350 */
3351 fromcol = -10;
3352 tocol = MAXCOL;
3353 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003355 /* Visual is after curwin->w_cursor */
3356 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 top = &curwin->w_cursor;
3359 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 top = &VIsual;
3364 bot = &curwin->w_cursor;
3365 }
3366 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3367 if (VIsual_mode == Ctrl_V) /* block mode */
3368 {
3369 if (lnum_in_visual_area)
3370 {
3371 fromcol = wp->w_old_cursor_fcol;
3372 tocol = wp->w_old_cursor_lcol;
3373 }
3374 }
3375 else /* non-block mode */
3376 {
3377 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003379 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003381 if (VIsual_mode == 'V') /* linewise */
3382 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 else
3384 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3386 if (gchar_pos(top) == NUL)
3387 tocol = fromcol + 1;
3388 }
3389 }
3390 if (VIsual_mode != 'V' && lnum == bot->lnum)
3391 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003392 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 {
3394 fromcol = -10;
3395 tocol = MAXCOL;
3396 }
3397 else if (bot->col == MAXCOL)
3398 tocol = MAXCOL;
3399 else
3400 {
3401 pos = *bot;
3402 if (*p_sel == 'e')
3403 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3404 else
3405 {
3406 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3407 ++tocol;
3408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 }
3410 }
3411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003413 /* Check if the character under the cursor should not be inverted */
3414 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003415#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003416 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003417#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003418 )
3419 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 /* if inverting in this line set area_highlighting */
3422 if (fromcol >= 0)
3423 {
3424 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003425 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003427 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003428 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003430 && clip_isautosel_plus()))
3431 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003436 /*
3437 * handle 'incsearch' and ":s///c" highlighting
3438 */
3439 else if (highlight_match
3440 && wp == curwin
3441 && lnum >= curwin->w_cursor.lnum
3442 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 if (lnum == curwin->w_cursor.lnum)
3445 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003446 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003447 else
3448 fromcol = 0;
3449 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3450 {
3451 pos.lnum = lnum;
3452 pos.col = search_match_endcol;
3453 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3454 }
3455 else
3456 tocol = MAXCOL;
3457 /* do at least one character; happens when past end of line */
3458 if (fromcol == tocol)
3459 tocol = fromcol + 1;
3460 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003461 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464
3465#ifdef FEAT_DIFF
3466 filler_lines = diff_check(wp, lnum);
3467 if (filler_lines < 0)
3468 {
3469 if (filler_lines == -1)
3470 {
3471 if (diff_find_change(wp, lnum, &change_start, &change_end))
3472 diff_hlf = HLF_ADD; /* added line */
3473 else if (change_start == 0)
3474 diff_hlf = HLF_TXD; /* changed text */
3475 else
3476 diff_hlf = HLF_CHD; /* changed line */
3477 }
3478 else
3479 diff_hlf = HLF_ADD; /* added line */
3480 filler_lines = 0;
3481 area_highlighting = TRUE;
3482 }
3483 if (lnum == wp->w_topline)
3484 filler_lines = wp->w_topfill;
3485 filler_todo = filler_lines;
3486#endif
3487
3488#ifdef LINE_ATTR
3489# ifdef FEAT_SIGNS
3490 /* If this line has a sign with line highlighting set line_attr. */
3491 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3492 if (v != 0)
3493 line_attr = sign_get_attr((int)v, TRUE);
3494# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003495# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003497 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003498 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499# endif
3500 if (line_attr != 0)
3501 area_highlighting = TRUE;
3502#endif
3503
3504 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3505 ptr = line;
3506
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003507#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003508 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003509 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003510 /* For checking first word with a capital skip white space. */
3511 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003512 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003513
Bram Moolenaar30abd282005-06-22 22:35:10 +00003514 /* To be able to spell-check over line boundaries copy the end of the
3515 * current line into nextline[]. Above the start of the next line was
3516 * copied to nextline[SPWORDLEN]. */
3517 if (nextline[SPWORDLEN] == NUL)
3518 {
3519 /* No next line or it is empty. */
3520 nextlinecol = MAXCOL;
3521 nextline_idx = 0;
3522 }
3523 else
3524 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003525 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003526 if (v < SPWORDLEN)
3527 {
3528 /* Short line, use it completely and append the start of the
3529 * next line. */
3530 nextlinecol = 0;
3531 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003532 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003533 nextline_idx = v + 1;
3534 }
3535 else
3536 {
3537 /* Long line, use only the last SPWORDLEN bytes. */
3538 nextlinecol = v - SPWORDLEN;
3539 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3540 nextline_idx = SPWORDLEN + 1;
3541 }
3542 }
3543 }
3544#endif
3545
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003546 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003548 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003549 extra_check = TRUE;
3550 /* find start of trailing whitespace */
3551 if (lcs_trail)
3552 {
3553 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003554 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003555 --trailcol;
3556 trailcol += (colnr_T) (ptr - line);
3557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003560 if (*wp->w_p_wcr != NUL)
3561 {
3562 int attr = syn_name2attr(wp->w_p_wcr);
3563
3564 // 'wincolor' highlighting for the whole window
3565 if (attr != 0)
3566 {
3567 win_attr = attr;
3568 area_highlighting = TRUE;
3569 }
3570 }
3571#ifdef FEAT_TEXT_PROP
3572 if (bt_popup(wp->w_buffer))
3573 {
3574 screen_line_flags |= SLF_POPUP;
3575
3576 if (win_attr == 0)
3577 {
3578 win_attr = HL_ATTR(HLF_PNI);
3579 area_highlighting = TRUE;
3580 }
3581 }
3582#endif
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 /*
3585 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3586 * first character to be displayed.
3587 */
3588 if (wp->w_p_wrap)
3589 v = wp->w_skipcol;
3590 else
3591 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003592 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 while (vcol < v && *ptr != NUL)
3597 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003598 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003601 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003604 /* When:
3605 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003606 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003607 * - 'virtualedit' is set, or
3608 * - the visual mode is active,
3609 * the end of the line may be before the start of the displayed part.
3610 */
3611 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003612#ifdef FEAT_SYN_HL
3613 wp->w_p_cuc || draw_color_col ||
3614#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003615 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003616 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618
3619 /* Handle a character that's not completely on the screen: Put ptr at
3620 * that character but skip the first few screen characters. */
3621 if (vcol > v)
3622 {
3623 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003625 /* If the character fits on the screen, don't need to skip it.
3626 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003627 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003628 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630
3631 /*
3632 * Adjust for when the inverted text is before the screen,
3633 * and when the start of the inverted text is before the screen.
3634 */
3635 if (tocol <= vcol)
3636 fromcol = 0;
3637 else if (fromcol >= 0 && fromcol < vcol)
3638 fromcol = vcol;
3639
3640#ifdef FEAT_LINEBREAK
3641 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3642 if (wp->w_p_wrap)
3643 need_showbreak = TRUE;
3644#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003645#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003646 /* When spell checking a word we need to figure out the start of the
3647 * word and if it's badly spelled or not. */
3648 if (has_spell)
3649 {
3650 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003651 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003652 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003653
3654 pos = wp->w_cursor;
3655 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003656 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003657 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003658
3659 /* spell_move_to() may call ml_get() and make "line" invalid */
3660 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3661 ptr = line + linecol;
3662
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003663 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003664 {
3665 /* no bad word found at line start, don't check until end of a
3666 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003667 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003668 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003669 }
3670 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003671 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003672 /* bad word found, use attributes until end of word */
3673 word_end = wp->w_cursor.col + len + 1;
3674
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003675 /* Turn index into actual attributes. */
3676 if (spell_hlf != HLF_COUNT)
3677 spell_attr = highlight_attr[spell_hlf];
3678 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003679 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003680
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003681# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003682 /* Need to restart syntax highlighting for this line. */
3683 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003684 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003686 }
3687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689
3690 /*
3691 * Correct highlighting for cursor that can't be disabled.
3692 * Avoids having to check this for each character.
3693 */
3694 if (fromcol >= 0)
3695 {
3696 if (noinvcur)
3697 {
3698 if ((colnr_T)fromcol == wp->w_virtcol)
3699 {
3700 /* highlighting starts at cursor, let it start just after the
3701 * cursor */
3702 fromcol_prev = fromcol;
3703 fromcol = -1;
3704 }
3705 else if ((colnr_T)fromcol < wp->w_virtcol)
3706 /* restart highlighting after the cursor */
3707 fromcol_prev = wp->w_virtcol;
3708 }
3709 if (fromcol >= tocol)
3710 fromcol = -1;
3711 }
3712
3713#ifdef FEAT_SEARCH_EXTRA
3714 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003715 * Handle highlighting the last used search pattern and matches.
3716 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003717 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003719 cur = wp->w_match_head;
3720 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003721 while ((cur != NULL || shl_flag == FALSE) && !number_only
3722 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003724 if (shl_flag == FALSE)
3725 {
3726 shl = &search_hl;
3727 shl_flag = TRUE;
3728 }
3729 else
3730 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003731 shl->startcol = MAXCOL;
3732 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003734 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003735 v = (long)(ptr - line);
3736 if (cur != NULL)
3737 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003738 next_search_hl(wp, shl, lnum, (colnr_T)v,
3739 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003740
3741 /* Need to get the line again, a multi-line regexp may have made it
3742 * invalid. */
3743 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3744 ptr = line + v;
3745
3746 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003748 if (shl->lnum == lnum)
3749 shl->startcol = shl->rm.startpos[0].col;
3750 else
3751 shl->startcol = 0;
3752 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3753 - shl->rm.startpos[0].lnum)
3754 shl->endcol = shl->rm.endpos[0].col;
3755 else
3756 shl->endcol = MAXCOL;
3757 /* Highlight one character for an empty match. */
3758 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003760 if (has_mbyte && line[shl->endcol] != NUL)
3761 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3762 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003763 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003765 if ((long)shl->startcol < v) /* match at leftcol */
3766 {
3767 shl->attr_cur = shl->attr;
3768 search_attr = shl->attr;
3769 }
3770 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003772 if (shl != &search_hl && cur != NULL)
3773 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775#endif
3776
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003777#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003778 // Cursor line highlighting for 'cursorline' in the current window.
3779 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003780 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003781 // Do not show the cursor line when Visual mode is active, because it's
3782 // not clear what is selected then. Do update w_last_cursorline.
3783 if (!(wp == curwin && VIsual_active))
3784 {
3785 line_attr = HL_ATTR(HLF_CUL);
3786 area_highlighting = TRUE;
3787 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003788 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003789 }
3790#endif
3791
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003792#ifdef FEAT_TEXT_PROP
3793 {
3794 char_u *prop_start;
3795
3796 text_prop_count = get_text_props(wp->w_buffer, lnum,
3797 &prop_start, FALSE);
3798 if (text_prop_count > 0)
3799 {
3800 // Make a copy of the properties, so that they are properly
3801 // aligned.
3802 text_props = (textprop_T *)alloc(
3803 text_prop_count * sizeof(textprop_T));
3804 if (text_props != NULL)
3805 mch_memmove(text_props, prop_start,
3806 text_prop_count * sizeof(textprop_T));
3807
3808 // Allocate an array for the indexes.
3809 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3810 area_highlighting = TRUE;
3811 extra_check = TRUE;
3812 }
3813 }
3814#endif
3815
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003816 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#ifdef FEAT_RIGHTLEFT
3820 if (wp->w_p_rl)
3821 {
3822 /* Rightleft window: process the text in the normal direction, but put
3823 * it in current_ScreenLine[] from right to left. Start at the
3824 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003825 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003827 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829#endif
3830
3831 /*
3832 * Repeat for the whole displayed line.
3833 */
3834 for (;;)
3835 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003836#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003837 int has_match_conc = 0; // match wants to conceal
3838 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 /* Skip this quickly when working on the text. */
3841 if (draw_state != WL_LINE)
3842 {
3843#ifdef FEAT_CMDWIN
3844 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3845 {
3846 draw_state = WL_CMDLINE;
3847 if (cmdwin_type != 0 && wp == curwin)
3848 {
3849 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003851 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003852 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003853 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 }
3856#endif
3857
3858#ifdef FEAT_FOLDING
3859 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3860 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003861 int fdc = compute_foldcolumn(wp, 0);
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003864 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003866 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003867 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003868 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003869 p_extra_free = alloc(12 + 1);
3870
3871 if (p_extra_free != NULL)
3872 {
3873 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3874 n_extra = fdc;
3875 p_extra_free[n_extra] = NUL;
3876 p_extra = p_extra_free;
3877 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003878 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003879 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 }
3883#endif
3884
3885#ifdef FEAT_SIGNS
3886 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3887 {
3888 draw_state = WL_SIGN;
3889 /* Show the sign column when there are any signs in this
3890 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003891 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003893 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003895 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896# endif
3897
3898 /* Draw two cells with the sign value or blank. */
3899 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003900 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003901 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 n_extra = 2;
3903
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003904 if (row == startrow
3905#ifdef FEAT_DIFF
3906 + filler_lines && filler_todo <= 0
3907#endif
3908 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
3910 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3911 SIGN_TEXT);
3912# ifdef FEAT_SIGN_ICONS
3913 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3914 SIGN_ICON);
3915 if (gui.in_use && icon_sign != 0)
3916 {
3917 /* Use the image in this position. */
3918 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003919 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920# ifdef FEAT_NETBEANS_INTG
3921 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003924 c_final = NUL;
3925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926# endif
3927 char_attr = icon_sign;
3928 }
3929 else
3930# endif
3931 if (text_sign != 0)
3932 {
3933 p_extra = sign_get_text(text_sign);
3934 if (p_extra != NULL)
3935 {
3936 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003937 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003938 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
3940 char_attr = sign_get_attr(text_sign, FALSE);
3941 }
3942 }
3943 }
3944 }
3945#endif
3946
3947 if (draw_state == WL_NR - 1 && n_extra == 0)
3948 {
3949 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003950 /* Display the absolute or relative line number. After the
3951 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3952 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 && (row == startrow
3954#ifdef FEAT_DIFF
3955 + filler_lines
3956#endif
3957 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3958 {
3959 /* Draw the line number (empty space after wrapping). */
3960 if (row == startrow
3961#ifdef FEAT_DIFF
3962 + filler_lines
3963#endif
3964 )
3965 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003966 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003967 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003968
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003969 if (wp->w_p_nu && !wp->w_p_rnu)
3970 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003971 num = (long)lnum;
3972 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003973 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003974 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003975 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003976 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003977 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003978 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003979 num = lnum;
3980 fmt = "%-*ld ";
3981 }
3982 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003983
Bram Moolenaar700e7342013-01-30 12:31:36 +01003984 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003985 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (wp->w_skipcol > 0)
3987 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3988 *p_extra = '-';
3989#ifdef FEAT_RIGHTLEFT
3990 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003991 {
3992 char_u *p1, *p2;
3993 int t;
3994
3995 // like rl_mirror(), but keep the space at the end
3996 p2 = skiptowhite(extra) - 1;
3997 for (p1 = extra; p1 < p2; ++p1, --p2)
3998 {
3999 t = *p1;
4000 *p1 = *p2;
4001 *p2 = t;
4002 }
4003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004#endif
4005 p_extra = extra;
4006 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004007 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004012 c_final = NUL;
4013 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004014 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004015 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004016#ifdef FEAT_SYN_HL
4017 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004018 * the current line differently.
4019 * TODO: Can we use CursorLine instead of CursorLineNr
4020 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004021 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004022 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004023 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 }
4027
Bram Moolenaar597a4222014-06-25 14:39:50 +02004028#ifdef FEAT_LINEBREAK
4029 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4030 && n_extra == 0 && *p_sbr != NUL)
4031 /* draw indent after showbreak value */
4032 draw_state = WL_BRI;
4033 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4034 /* After the showbreak, draw the breakindent */
4035 draw_state = WL_BRI - 1;
4036
4037 /* draw 'breakindent': indent wrapped text accordingly */
4038 if (draw_state == WL_BRI - 1 && n_extra == 0)
4039 {
4040 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004041 /* if need_showbreak is set, breakindent also applies */
4042 if (wp->w_p_bri && n_extra == 0
4043 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004044# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004045 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004046# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004047 )
4048 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004049 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004050# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004051 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004052 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004053 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004054# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004055 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4056 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004057 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004058# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004059 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004060# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004061 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004062 c_extra = ' ';
4063 n_extra = get_breakindent_win(wp,
4064 ml_get_buf(wp->w_buffer, lnum, FALSE));
4065 /* Correct end of highlighted area for 'breakindent',
4066 * required when 'linebreak' is also set. */
4067 if (tocol == vcol)
4068 tocol += n_extra;
4069 }
4070 }
4071#endif
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4074 if (draw_state == WL_SBR - 1 && n_extra == 0)
4075 {
4076 draw_state = WL_SBR;
4077# ifdef FEAT_DIFF
4078 if (filler_todo > 0)
4079 {
4080 /* Draw "deleted" diff line(s). */
4081 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004084 c_final = NUL;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004089 c_final = NUL;
4090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091# ifdef FEAT_RIGHTLEFT
4092 if (wp->w_p_rl)
4093 n_extra = col + 1;
4094 else
4095# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004096 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004097 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099# endif
4100# ifdef FEAT_LINEBREAK
4101 if (*p_sbr != NUL && need_showbreak)
4102 {
4103 /* Draw 'showbreak' at the start of each broken line. */
4104 p_extra = p_sbr;
4105 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004106 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004108 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004110 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 /* Correct end of highlighted area for 'showbreak',
4112 * required when 'linebreak' is also set. */
4113 if (tocol == vcol)
4114 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004115#ifdef FEAT_SYN_HL
4116 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004117 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004118 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004119 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122# endif
4123 }
4124#endif
4125
4126 if (draw_state == WL_LINE - 1 && n_extra == 0)
4127 {
4128 draw_state = WL_LINE;
4129 if (saved_n_extra)
4130 {
4131 /* Continue item from end of wrapped line. */
4132 n_extra = saved_n_extra;
4133 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004134 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 p_extra = saved_p_extra;
4136 char_attr = saved_char_attr;
4137 }
4138 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004139 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 }
4142
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004143 // When still displaying '$' of change command, stop at cursor.
4144 // When only displaying the (relative) line number and that's done,
4145 // stop here.
4146 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004147 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148#ifdef FEAT_DIFF
4149 && filler_todo <= 0
4150#endif
4151 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004152 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004154 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004155 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004156 /* Pretend we have finished updating the window. Except when
4157 * 'cursorcolumn' is set. */
4158#ifdef FEAT_SYN_HL
4159 if (wp->w_p_cuc)
4160 row = wp->w_cline_row + wp->w_cline_height;
4161 else
4162#endif
4163 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 break;
4165 }
4166
Bram Moolenaar637532b2019-01-03 21:44:40 +01004167 if (draw_state == WL_LINE && (area_highlighting
4168#ifdef FEAT_SPELL
4169 || has_spell
4170#endif
4171 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 /* handle Visual or match highlighting in this line */
4174 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4176 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004178 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004180 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else if (area_attr != 0
4182 && (vcol == tocol
4183 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186#ifdef FEAT_SEARCH_EXTRA
4187 if (!n_extra)
4188 {
4189 /*
4190 * Check for start/end of search pattern match.
4191 * After end, check for start/end of next match.
4192 * When another match, have to check for start again.
4193 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004194 * Do this for 'search_hl' and the match list (ordered by
4195 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004197 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004198 cur = wp->w_match_head;
4199 shl_flag = FALSE;
4200 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004202 if (shl_flag == FALSE
4203 && ((cur != NULL
4204 && cur->priority > SEARCH_HL_PRIORITY)
4205 || cur == NULL))
4206 {
4207 shl = &search_hl;
4208 shl_flag = TRUE;
4209 }
4210 else
4211 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004212 if (cur != NULL)
4213 cur->pos.cur = 0;
4214 pos_inprogress = TRUE;
4215 while (shl->rm.regprog != NULL
4216 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004218 if (shl->startcol != MAXCOL
4219 && v >= (long)shl->startcol
4220 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004222 int tmp_col = v + MB_PTR2LEN(ptr);
4223
4224 if (shl->endcol < tmp_col)
4225 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004227#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004228 // Match with the "Conceal" group results in hiding
4229 // the match.
4230 if (cur != NULL
4231 && shl != &search_hl
4232 && syn_name2id((char_u *)"Conceal")
4233 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004234 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004235 has_match_conc =
4236 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004237 match_conc = cur->conceal_char;
4238 }
4239 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004240 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004243 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 {
4245 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004246 next_search_hl(wp, shl, lnum, (colnr_T)v,
4247 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004248 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004249 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /* Need to get the line again, a multi-line regexp
4252 * may have made it invalid. */
4253 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4254 ptr = line + v;
4255
4256 if (shl->lnum == lnum)
4257 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004258 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004260 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004262 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004264 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 {
4266 /* highlight empty match, try again after
4267 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004269 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004270 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004272 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274
4275 /* Loop to check if the match starts at the
4276 * current position */
4277 continue;
4278 }
4279 }
4280 break;
4281 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004282 if (shl != &search_hl && cur != NULL)
4283 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004285
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004286 /* Use attributes from match with highest priority among
4287 * 'search_hl' and the match list. */
4288 search_attr = search_hl.attr_cur;
4289 cur = wp->w_match_head;
4290 shl_flag = FALSE;
4291 while (cur != NULL || shl_flag == FALSE)
4292 {
4293 if (shl_flag == FALSE
4294 && ((cur != NULL
4295 && cur->priority > SEARCH_HL_PRIORITY)
4296 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004297 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004298 shl = &search_hl;
4299 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004300 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004301 else
4302 shl = &cur->hl;
4303 if (shl->attr_cur != 0)
4304 search_attr = shl->attr_cur;
4305 if (shl != &search_hl && cur != NULL)
4306 cur = cur->next;
4307 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004308 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004309 if (*ptr == NUL && (did_line_attr >= 1
4310 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004311 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313#endif
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004316 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004318 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4319 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004321 if (diff_hlf == HLF_TXD && ptr - line > change_end
4322 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004324 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004325 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004326 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004329
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004330#ifdef FEAT_TEXT_PROP
4331 if (text_props != NULL)
4332 {
4333 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004334 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004335
4336 // Check if any active property ends.
4337 for (pi = 0; pi < text_props_active; ++pi)
4338 {
4339 int tpi = text_prop_idxs[pi];
4340
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004341 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004342 + text_props[tpi].tp_len)
4343 {
4344 if (pi + 1 < text_props_active)
4345 mch_memmove(text_prop_idxs + pi,
4346 text_prop_idxs + pi + 1,
4347 sizeof(int)
4348 * (text_props_active - (pi + 1)));
4349 --text_props_active;
4350 --pi;
4351 }
4352 }
4353
4354 // Add any text property that starts in this column.
4355 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004356 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004357 text_prop_idxs[text_props_active++] = text_prop_next++;
4358
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004359 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004360 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004361 if (text_props_active > 0)
4362 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004363 // Sort the properties on priority and/or starting last.
4364 // Then combine the attributes, highest priority last.
4365 current_text_props = text_props;
4366 current_buf = wp->w_buffer;
4367 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4368 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004369
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004370 for (pi = 0; pi < text_props_active; ++pi)
4371 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004372 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004373 proptype_T *pt = text_prop_type_by_id(
4374 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004375
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004376 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004377 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004378 int pt_attr = syn_id2attr(pt->pt_hl_id);
4379
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004380 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004381 text_prop_attr =
4382 hl_combine_attr(text_prop_attr, pt_attr);
4383 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004384 }
4385 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004386 }
4387 }
4388#endif
4389
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004390 /* Decide which of the highlight attributes to use. */
4391 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004392#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004393 if (area_attr != 0)
4394 char_attr = hl_combine_attr(line_attr, area_attr);
4395 else if (search_attr != 0)
4396 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004397# ifdef FEAT_TEXT_PROP
4398 else if (text_prop_type != NULL)
4399 char_attr = hl_combine_attr(line_attr, text_prop_attr);
4400# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004401 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004402 || vcol < fromcol || vcol_prev < fromcol_prev
4403 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004404 // Use line_attr when not in the Visual or 'incsearch' area
4405 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004406 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004407#else
4408 if (area_attr != 0)
4409 char_attr = area_attr;
4410 else if (search_attr != 0)
4411 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004412#endif
4413 else
4414 {
4415 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004416#ifdef FEAT_TEXT_PROP
4417 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004418 {
4419 if (text_prop_combine)
4420 char_attr = hl_combine_attr(
4421 syntax_attr, text_prop_attr);
4422 else
4423 char_attr = text_prop_attr;
4424 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004425 else
4426#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004427#ifdef FEAT_SYN_HL
4428 if (has_syntax)
4429 char_attr = syntax_attr;
4430 else
4431#endif
4432 char_attr = 0;
4433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004435 if (char_attr == 0)
4436 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437
4438 /*
4439 * Get the next character to put on the screen.
4440 */
4441 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004442 * The "p_extra" points to the extra stuff that is inserted to
4443 * represent special characters (non-printable stuff) and other
4444 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004445 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004446 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4447 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4449 */
4450 if (n_extra > 0)
4451 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004452 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004454 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004456 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004459 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004460 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
4463 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 else
4466 {
4467 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (has_mbyte)
4469 {
4470 mb_c = c;
4471 if (enc_utf8)
4472 {
4473 /* If the UTF-8 character is more than one byte:
4474 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004475 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 mb_utf8 = FALSE;
4477 if (mb_l > n_extra)
4478 mb_l = 1;
4479 else if (mb_l > 1)
4480 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004483 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 }
4486 else
4487 {
4488 /* if this is a DBCS character, put it in "mb_c" */
4489 mb_l = MB_BYTE2LEN(c);
4490 if (mb_l >= n_extra)
4491 mb_l = 1;
4492 else if (mb_l > 1)
4493 mb_c = (c << 8) + p_extra[1];
4494 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004495 if (mb_l == 0) /* at the NUL at end-of-line */
4496 mb_l = 1;
4497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 /* If a double-width char doesn't fit display a '>' in the
4499 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004500 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501# ifdef FEAT_RIGHTLEFT
4502 wp->w_p_rl ? (col <= 0) :
4503# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004504 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 && (*mb_char2cells)(mb_c) == 2)
4506 {
4507 c = '>';
4508 mb_c = c;
4509 mb_l = 1;
4510 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004511 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /* put the pointer back to output the double-width
4513 * character at the start of the next line. */
4514 ++n_extra;
4515 --p_extra;
4516 }
4517 else
4518 {
4519 n_extra -= mb_l - 1;
4520 p_extra += mb_l - 1;
4521 }
4522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 ++p_extra;
4524 }
4525 --n_extra;
4526 }
4527 else
4528 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004529#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004530 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004531#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004532
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004533 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004534 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 /*
4536 * Get a character from the line itself.
4537 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004538 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004539#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004540 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (has_mbyte)
4543 {
4544 mb_c = c;
4545 if (enc_utf8)
4546 {
4547 /* If the UTF-8 character is more than one byte: Decode it
4548 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004549 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 mb_utf8 = FALSE;
4551 if (mb_l > 1)
4552 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004553 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 /* Overlong encoded ASCII or ASCII with composing char
4555 * is displayed normally, except a NUL. */
4556 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004557 {
4558 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004559#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004560 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004561#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004564
4565 /* At start of the line we can have a composing char.
4566 * Draw it as a space with a composing char. */
4567 if (utf_iscomposing(mb_c))
4568 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004569 int i;
4570
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004571 for (i = Screen_mco - 1; i > 0; --i)
4572 u8cc[i] = u8cc[i - 1];
4573 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004574 mb_c = ' ';
4575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577
4578 if ((mb_l == 1 && c >= 0x80)
4579 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004580 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 /*
4583 * Illegal UTF-8 byte: display as <xx>.
4584 * Non-BMP character : display as ? or fullwidth ?.
4585 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004586 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004587# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004588 if (wp->w_p_rl) /* reverse */
4589 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 p_extra = extra;
4592 c = *p_extra;
4593 mb_c = mb_ptr2char_adv(&p_extra);
4594 mb_utf8 = (c >= 0x80);
4595 n_extra = (int)STRLEN(p_extra);
4596 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004597 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (area_attr == 0 && search_attr == 0)
4599 {
4600 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004601 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 saved_attr2 = char_attr; /* save current attr */
4603 }
4604 }
4605 else if (mb_l == 0) /* at the NUL at end-of-line */
4606 mb_l = 1;
4607#ifdef FEAT_ARABIC
4608 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4609 {
4610 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004611 int pc, pc1, nc;
4612 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
4614 /* The idea of what is the previous and next
4615 * character depends on 'rightleft'. */
4616 if (wp->w_p_rl)
4617 {
4618 pc = prev_c;
4619 pc1 = prev_c1;
4620 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004621 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623 else
4624 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004625 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004627 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
4629 prev_c = mb_c;
4630
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004631 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 else
4634 prev_c = mb_c;
4635#endif
4636 }
4637 else /* enc_dbcs */
4638 {
4639 mb_l = MB_BYTE2LEN(c);
4640 if (mb_l == 0) /* at the NUL at end-of-line */
4641 mb_l = 1;
4642 else if (mb_l > 1)
4643 {
4644 /* We assume a second byte below 32 is illegal.
4645 * Hopefully this is OK for all double-byte encodings!
4646 */
4647 if (ptr[1] >= 32)
4648 mb_c = (c << 8) + ptr[1];
4649 else
4650 {
4651 if (ptr[1] == NUL)
4652 {
4653 /* head byte at end of line */
4654 mb_l = 1;
4655 transchar_nonprint(extra, c);
4656 }
4657 else
4658 {
4659 /* illegal tail byte */
4660 mb_l = 2;
4661 STRCPY(extra, "XX");
4662 }
4663 p_extra = extra;
4664 n_extra = (int)STRLEN(extra) - 1;
4665 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004666 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 c = *p_extra++;
4668 if (area_attr == 0 && search_attr == 0)
4669 {
4670 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004671 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 saved_attr2 = char_attr; /* save current attr */
4673 }
4674 mb_c = c;
4675 }
4676 }
4677 }
4678 /* If a double-width char doesn't fit display a '>' in the
4679 * last column; the character is displayed at the start of the
4680 * next line. */
4681 if ((
4682# ifdef FEAT_RIGHTLEFT
4683 wp->w_p_rl ? (col <= 0) :
4684# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004685 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 && (*mb_char2cells)(mb_c) == 2)
4687 {
4688 c = '>';
4689 mb_c = c;
4690 mb_utf8 = FALSE;
4691 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004692 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004693 // Put pointer back so that the character will be
4694 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004696#ifdef FEAT_CONCEAL
4697 did_decrement_ptr = TRUE;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 else if (*ptr != NUL)
4701 ptr += mb_l - 1;
4702
4703 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004704 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004705 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004706 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004709 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004710 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 c = ' ';
4712 if (area_attr == 0 && search_attr == 0)
4713 {
4714 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004715 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 saved_attr2 = char_attr; /* save current attr */
4717 }
4718 mb_c = c;
4719 mb_utf8 = FALSE;
4720 mb_l = 1;
4721 }
4722
4723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 ++ptr;
4725
4726 if (extra_check)
4727 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004728#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004729 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004730#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004731
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004732#ifdef FEAT_TERMINAL
4733 if (get_term_attr)
4734 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004735 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004736
4737 if (!attr_pri)
4738 char_attr = syntax_attr;
4739 else
4740 char_attr = hl_combine_attr(syntax_attr, char_attr);
4741 }
4742#endif
4743
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004744#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004745 // Get syntax attribute, unless still at the start of the line
4746 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004747 v = (long)(ptr - line);
4748 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 /* Get the syntax attribute for the character. If there
4751 * is an error, disable syntax highlighting. */
4752 save_did_emsg = did_emsg;
4753 did_emsg = FALSE;
4754
Bram Moolenaar217ad922005-03-20 22:37:15 +00004755 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004756# ifdef FEAT_SPELL
4757 has_spell ? &can_spell :
4758# endif
4759 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004762 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004763 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004764 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004765 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
4768 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004769
4770 // combine syntax attribute with 'wincolor'
4771 if (win_attr != 0)
4772 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4773
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004774#ifdef SYN_TIME_LIMIT
4775 if (wp->w_s->b_syn_slow)
4776 has_syntax = FALSE;
4777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 /* Need to get the line again, a multi-line regexp may
4780 * have made it invalid. */
4781 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4782 ptr = line + v;
4783
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004784# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004785 // Text properties overrule syntax highlighting or combine.
4786 if (text_prop_attr == 0 || text_prop_combine)
4787# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004788 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004789 int comb_attr = syntax_attr;
4790# ifdef FEAT_TEXT_PROP
4791 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4792# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004793 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004794 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004795 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004796 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004797 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004798# ifdef FEAT_CONCEAL
4799 /* no concealing past the end of the line, it interferes
4800 * with line highlighting */
4801 if (c == NUL)
4802 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004803 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004804 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004807#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004808
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004809#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004810 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004811 * Only do this when there is no syntax highlighting, the
4812 * @Spell cluster is not used or the current syntax item
4813 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004814 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004815 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004816 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004817 if (c != 0 && (
4818# ifdef FEAT_SYN_HL
4819 !has_syntax ||
4820# endif
4821 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004822 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004823 char_u *prev_ptr, *p;
4824 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004825 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004826 if (has_mbyte)
4827 {
4828 prev_ptr = ptr - mb_l;
4829 v -= mb_l - 1;
4830 }
4831 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004832 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004833
4834 /* Use nextline[] if possible, it has the start of the
4835 * next line concatenated. */
4836 if ((prev_ptr - line) - nextlinecol >= 0)
4837 p = nextline + (prev_ptr - line) - nextlinecol;
4838 else
4839 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004840 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004841 len = spell_check(wp, p, &spell_hlf, &cap_col,
4842 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004843 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004844
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004845 /* In Insert mode only highlight a word that
4846 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004847 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004848 && (State & INSERT) != 0
4849 && wp->w_cursor.lnum == lnum
4850 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004851 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004852 && wp->w_cursor.col < (colnr_T)word_end)
4853 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004854 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004855 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004856 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004857
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004858 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004859 && (p - nextline) + len > nextline_idx)
4860 {
4861 /* Remember that the good word continues at the
4862 * start of the next line. */
4863 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004864 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004865 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004866
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004867 /* Turn index into actual attributes. */
4868 if (spell_hlf != HLF_COUNT)
4869 spell_attr = highlight_attr[spell_hlf];
4870
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004871 if (cap_col > 0)
4872 {
4873 if (p != prev_ptr
4874 && (p - nextline) + cap_col >= nextline_idx)
4875 {
4876 /* Remember that the word in the next line
4877 * must start with a capital. */
4878 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004879 cap_col = (int)((p - nextline) + cap_col
4880 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004881 }
4882 else
4883 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004884 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004885 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004886 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004887 }
4888 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004889 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004890 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004891 char_attr = hl_combine_attr(char_attr, spell_attr);
4892 else
4893 char_attr = hl_combine_attr(spell_attr, char_attr);
4894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
4896#ifdef FEAT_LINEBREAK
4897 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004898 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004900 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004901 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004903 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004904 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004905
Bram Moolenaar597a4222014-06-25 14:39:50 +02004906 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004907 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004908 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004909 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004910# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004911 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004912 wp->w_buffer->b_p_vts_array) - 1;
4913# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004914 n_extra = (int)wp->w_buffer->b_p_ts
4915 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004916# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004917
Bram Moolenaar4df70292015-03-21 14:20:16 +01004918 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004919 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004920 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004921 {
4922#ifdef FEAT_CONCEAL
4923 if (c == TAB)
4924 /* See "Tab alignment" below. */
4925 FIX_FOR_BOGUSCOLS;
4926#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004927 if (!wp->w_p_list)
4928 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931#endif
4932
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004933 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4934 // But not when the character is followed by a composing
4935 // character (use mb_l to check that).
4936 if (wp->w_p_list
4937 && ((((c == 160 && mb_l == 1)
4938 || (mb_utf8
4939 && ((mb_c == 160 && mb_l == 2)
4940 || (mb_c == 0x202f && mb_l == 3))))
4941 && lcs_nbsp)
4942 || (c == ' '
4943 && mb_l == 1
4944 && lcs_space
4945 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004946 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004947 c = (c == ' ') ? lcs_space : lcs_nbsp;
4948 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004949 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004950 n_attr = 1;
4951 extra_attr = HL_ATTR(HLF_8);
4952 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004953 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004954 mb_c = c;
4955 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004956 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004957 mb_utf8 = TRUE;
4958 u8cc[0] = 0;
4959 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004960 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004961 else
4962 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004963 }
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4966 {
4967 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004968 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004971 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 saved_attr2 = char_attr; /* save current attr */
4973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004975 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004978 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004979 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 else
4982 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 }
4985
4986 /*
4987 * Handling of non-printable characters.
4988 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004989 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 /*
4992 * when getting a character from the file, we may have to
4993 * turn it into something else on the way to putting it
4994 * into "ScreenLines".
4995 */
4996 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4997 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004998 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004999 long vcol_adjusted = vcol; /* removed showbreak length */
5000#ifdef FEAT_LINEBREAK
5001 /* only adjust the tab_len, when at the first column
5002 * after the showbreak value was drawn */
5003 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5004 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005007#ifdef FEAT_VARTABS
5008 tab_len = tabstop_padding(vcol_adjusted,
5009 wp->w_buffer->b_p_ts,
5010 wp->w_buffer->b_p_vts_array) - 1;
5011#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005012 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005013 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5014#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005015
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005016#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005017 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005018#endif
5019 /* tab amount depends on current column */
5020 n_extra = tab_len;
5021#ifdef FEAT_LINEBREAK
5022 else
5023 {
5024 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005025 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005026 int i;
5027 int saved_nextra = n_extra;
5028
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005029#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005030 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005031 /* there are characters to conceal */
5032 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005033 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5034 */
5035 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5036 && n_extra > tab_len)
5037 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005038#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005039
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005040 /* if n_extra > 0, it gives the number of chars, to
5041 * use for a tab, else we need to calculate the width
5042 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043 len = (tab_len * mb_char2len(lcs_tab2));
5044 if (n_extra > 0)
5045 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005046 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005047 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005048 vim_memset(p, ' ', len);
5049 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005050 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005051 p_extra_free = p;
5052 for (i = 0; i < tab_len; i++)
5053 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005054 if (*p == NUL)
5055 {
5056 tab_len = i;
5057 break;
5058 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005059 mb_char2bytes(lcs_tab2, p);
5060 p += mb_char2len(lcs_tab2);
5061 n_extra += mb_char2len(lcs_tab2)
5062 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005063 }
5064 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005065#ifdef FEAT_CONCEAL
5066 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5067 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005068 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005069 n_extra -= vcol_off;
5070#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005071 }
5072#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005073#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005074 {
5075 int vc_saved = vcol_off;
5076
5077 /* Tab alignment should be identical regardless of
5078 * 'conceallevel' value. So tab compensates of all
5079 * previous concealed characters, and thus resets
5080 * vcol_off and boguscols accumulated so far in the
5081 * line. Note that the tab can be longer than
5082 * 'tabstop' when there are concealed characters. */
5083 FIX_FOR_BOGUSCOLS;
5084
5085 /* Make sure, the highlighting for the tab char will be
5086 * correctly set further below (effectively reverts the
5087 * FIX_FOR_BOGSUCOLS macro */
5088 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005089 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005090 tab_len += vc_saved;
5091 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (wp->w_p_list)
5095 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005096 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005097#ifdef FEAT_LINEBREAK
5098 if (wp->w_p_lbr)
5099 c_extra = NUL; /* using p_extra from above */
5100 else
5101#endif
5102 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005103 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005104 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005105 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005108 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
5110 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005111 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005112 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 else
5116 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005117 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 c_extra = ' ';
5119 c = ' ';
5120 }
5121 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005122 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005123 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005124 || ((fromcol >= 0 || fromcol_prev >= 0)
5125 && tocol > vcol
5126 && VIsual_mode != Ctrl_V
5127 && (
5128# ifdef FEAT_RIGHTLEFT
5129 wp->w_p_rl ? (col >= 0) :
5130# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005131 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005132 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005133 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005134 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005135 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005137 /* Display a '$' after the line or highlight an extra
5138 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5140 /* For a diff line the highlighting continues after the
5141 * "$". */
5142 if (
5143# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005144 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145# ifdef LINE_ATTR
5146 &&
5147# endif
5148# endif
5149# ifdef LINE_ATTR
5150 line_attr == 0
5151# endif
5152 )
5153#endif
5154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 /* In virtualedit, visual selections may extend
5156 * beyond end of line. */
5157 if (area_highlighting && virtual_active()
5158 && tocol != MAXCOL && vcol < tocol)
5159 n_extra = 0;
5160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 p_extra = at_end_str;
5163 n_extra = 1;
5164 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005165 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005168 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005169 c = lcs_eol;
5170 else
5171 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 lcs_eol_one = -1;
5173 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005174 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005176 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 n_attr = 1;
5178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005180 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005183 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005184 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 else
5187 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 else if (c != NUL)
5190 {
5191 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005192 if (n_extra == 0)
5193 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#ifdef FEAT_RIGHTLEFT
5195 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5196 rl_mirror(p_extra); /* reverse "<12>" */
5197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005199 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005200#ifdef FEAT_LINEBREAK
5201 if (wp->w_p_lbr)
5202 {
5203 char_u *p;
5204
5205 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005206 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005207 vim_memset(p, ' ', n_extra);
5208 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5209 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005210 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005211 p_extra_free = p_extra = p;
5212 }
5213 else
5214#endif
5215 {
5216 n_extra = byte2cells(c) - 1;
5217 c = *p_extra++;
5218 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005219 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
5221 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005222 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 saved_attr2 = char_attr; /* save current attr */
5224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else if (VIsual_active
5228 && (VIsual_mode == Ctrl_V
5229 || VIsual_mode == 'v')
5230 && virtual_active()
5231 && tocol != MAXCOL
5232 && vcol < tocol
5233 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005234#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005236#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005237 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 c = ' ';
5240 --ptr; /* put it back at the NUL */
5241 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005242#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else if ((
5244# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005245 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005247# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005248 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 ) && (
5252# ifdef FEAT_RIGHTLEFT
5253 wp->w_p_rl ? (col >= 0) :
5254# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005255 (col
5256# ifdef FEAT_CONCEAL
5257 - boguscols
5258# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005259 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 /* Highlight until the right side of the window */
5262 c = ' ';
5263 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005264
5265 /* Remember we do the char for line highlighting. */
5266 ++did_line_attr;
5267
5268 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005269 if (line_attr != 0 && char_attr == search_attr
5270 && (did_line_attr > 1
5271 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005272 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273# ifdef FEAT_DIFF
5274 if (diff_hlf == HLF_TXD)
5275 {
5276 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005277 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005278 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005279 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005280 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5281 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005282 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
5285# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005286# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005287 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005288 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005289 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005290 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5291 char_attr = hl_combine_attr(char_attr,
5292 HL_ATTR(HLF_CUL));
5293 }
5294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296#endif
5297 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005298
5299#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005300 if ( wp->w_p_cole > 0
5301 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005302 conceal_cursor_line(wp))
5303 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005304 && !(lnum_in_visual_area
5305 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005306 {
5307 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005308 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005309 && (syn_get_sub_char() != NUL || match_conc
5310 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005311 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005312 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005313 /* First time at this concealed item: display one
5314 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005315 if (match_conc)
5316 c = match_conc;
5317 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 c = syn_get_sub_char();
5319 else if (lcs_conceal != NUL)
5320 c = lcs_conceal;
5321 else
5322 c = ' ';
5323
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005324 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005325
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005326 if (n_extra > 0)
5327 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005328 vcol += n_extra;
5329 if (wp->w_p_wrap && n_extra > 0)
5330 {
5331# ifdef FEAT_RIGHTLEFT
5332 if (wp->w_p_rl)
5333 {
5334 col -= n_extra;
5335 boguscols -= n_extra;
5336 }
5337 else
5338# endif
5339 {
5340 boguscols += n_extra;
5341 col += n_extra;
5342 }
5343 }
5344 n_extra = 0;
5345 n_attr = 0;
5346 }
5347 else if (n_skip == 0)
5348 {
5349 is_concealing = TRUE;
5350 n_skip = 1;
5351 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005352 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005353 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005354 {
5355 mb_utf8 = TRUE;
5356 u8cc[0] = 0;
5357 c = 0xc0;
5358 }
5359 else
5360 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005361 }
5362 else
5363 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005364 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005365 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005366 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005367
5368 if (n_skip > 0 && did_decrement_ptr)
5369 // not showing the '>', put pointer back to avoid getting stuck
5370 ++ptr;
5371
5372#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005375#ifdef FEAT_CONCEAL
5376 /* In the cursor line and we may be concealing characters: correct
5377 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005378 if (!did_wcol && draw_state == WL_LINE
5379 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005380 && conceal_cursor_line(wp)
5381 && (int)wp->w_virtcol <= vcol + n_skip)
5382 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005383# ifdef FEAT_RIGHTLEFT
5384 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005385 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005386 else
5387# endif
5388 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005389 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005390 did_wcol = TRUE;
5391 }
5392#endif
5393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 /* Don't override visual selection highlighting. */
5395 if (n_attr > 0
5396 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005397 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 char_attr = extra_attr;
5399
Bram Moolenaar81695252004-12-29 20:58:21 +00005400#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 /* XIM don't send preedit_start and preedit_end, but they send
5402 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5403 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005404 if (p_imst == IM_ON_THE_SPOT
5405 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005406 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 && (State & INSERT)
5408 && !p_imdisable
5409 && im_is_preediting()
5410 && draw_state == WL_LINE)
5411 {
5412 colnr_T tcol;
5413
5414 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005415 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 else
5417 tcol = preedit_end_col;
5418 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5419 {
5420 if (feedback_old_attr < 0)
5421 {
5422 feedback_col = 0;
5423 feedback_old_attr = char_attr;
5424 }
5425 char_attr = im_get_feedback_attr(feedback_col);
5426 if (char_attr < 0)
5427 char_attr = feedback_old_attr;
5428 feedback_col++;
5429 }
5430 else if (feedback_old_attr >= 0)
5431 {
5432 char_attr = feedback_old_attr;
5433 feedback_old_attr = -1;
5434 feedback_col = 0;
5435 }
5436 }
5437#endif
5438 /*
5439 * Handle the case where we are in column 0 but not on the first
5440 * character of the line and the user wants us to show us a
5441 * special character (via 'listchars' option "precedes:<char>".
5442 */
5443 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005444 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5446#ifdef FEAT_DIFF
5447 && filler_todo <= 0
5448#endif
5449 && draw_state > WL_NR
5450 && c != NUL)
5451 {
5452 c = lcs_prec;
5453 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005454 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5455 {
5456 /* Double-width character being overwritten by the "precedes"
5457 * character, need to fill up half the character. */
5458 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005459 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005460 n_extra = 1;
5461 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005462 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005465 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005468 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005469 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471 else
5472 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005473 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 {
5475 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005476 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 n_attr3 = 1;
5478 }
5479 }
5480
5481 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005482 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005484 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005485#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005486 || did_line_attr == 1
5487#endif
5488 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005490#ifdef FEAT_SEARCH_EXTRA
5491 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005492
5493 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005494 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005495 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005496#endif
5497
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005498 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 * highlight match at end of line. If it's beyond the last
5500 * char on the screen, just overwrite that one (tricky!) Not
5501 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005502#ifdef FEAT_SEARCH_EXTRA
5503 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005504 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005505 prevcol_hl_flag = TRUE;
5506 else
5507 {
5508 cur = wp->w_match_head;
5509 while (cur != NULL)
5510 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005511 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005512 {
5513 prevcol_hl_flag = TRUE;
5514 break;
5515 }
5516 cur = cur->next;
5517 }
5518 }
5519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005521 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005522 && (VIsual_mode != Ctrl_V
5523 || lnum == VIsual.lnum
5524 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005525 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526#ifdef FEAT_SEARCH_EXTRA
5527 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005528 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005529# ifdef FEAT_SYN_HL
5530 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5531 && !(wp == curwin && VIsual_active))
5532# endif
5533# ifdef FEAT_DIFF
5534 && diff_hlf == (hlf_T)0
5535# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005536# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005537 && did_line_attr <= 1
5538# endif
5539 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#endif
5541 ))
5542 {
5543 int n = 0;
5544
5545#ifdef FEAT_RIGHTLEFT
5546 if (wp->w_p_rl)
5547 {
5548 if (col < 0)
5549 n = 1;
5550 }
5551 else
5552#endif
5553 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005554 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 n = -1;
5556 }
5557 if (n != 0)
5558 {
5559 /* At the window boundary, highlight the last character
5560 * instead (better than nothing). */
5561 off += n;
5562 col += n;
5563 }
5564 else
5565 {
5566 /* Add a blank character to highlight. */
5567 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 if (enc_utf8)
5569 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571#ifdef FEAT_SEARCH_EXTRA
5572 if (area_attr == 0)
5573 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005574 /* Use attributes from match with highest priority among
5575 * 'search_hl' and the match list. */
5576 char_attr = search_hl.attr;
5577 cur = wp->w_match_head;
5578 shl_flag = FALSE;
5579 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005580 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005581 if (shl_flag == FALSE
5582 && ((cur != NULL
5583 && cur->priority > SEARCH_HL_PRIORITY)
5584 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005585 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005586 shl = &search_hl;
5587 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005588 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005589 else
5590 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005591 if ((ptr - line) - 1 == (long)shl->startcol
5592 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005593 char_attr = shl->attr;
5594 if (shl != &search_hl && cur != NULL)
5595 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598#endif
5599 ScreenAttrs[off] = char_attr;
5600#ifdef FEAT_RIGHTLEFT
5601 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005604 --off;
5605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 else
5607#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005610 ++off;
5611 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005612 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005613#ifdef FEAT_SYN_HL
5614 eol_hl_off = 1;
5615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Bram Moolenaar91170f82006-05-05 21:15:17 +00005619 /*
5620 * At end of the text line.
5621 */
5622 if (c == NUL)
5623 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005625 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005626 if (wp->w_p_wrap)
5627 v = wp->w_skipcol;
5628 else
5629 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005630
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005631 /* check if line ends before left margin */
5632 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005633 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005634#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005635 // Get rid of the boguscols now, we want to draw until the right
5636 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005637 col -= boguscols;
5638 boguscols = 0;
5639#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005640
5641 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005642 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643
5644 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005645 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5646 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005647 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005648 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005649 || draw_color_col
5650 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005651# ifdef FEAT_RIGHTLEFT
5652 && !wp->w_p_rl
5653# endif
5654 )
5655 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005656 int rightmost_vcol = 0;
5657 int i;
5658
5659 if (wp->w_p_cuc)
5660 rightmost_vcol = wp->w_virtcol;
5661 if (draw_color_col)
5662 /* determine rightmost colorcolumn to possibly draw */
5663 for (i = 0; color_cols[i] >= 0; ++i)
5664 if (rightmost_vcol < color_cols[i])
5665 rightmost_vcol = color_cols[i];
5666
Bram Moolenaar02631462017-09-22 15:20:32 +02005667 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005668 {
5669 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005670 if (enc_utf8)
5671 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005672 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005673 if (draw_color_col)
5674 draw_color_col = advance_color_col(VCOL_HLC,
5675 &color_cols);
5676
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005677 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005678 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005679 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005680 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005681 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005682 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005683
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005684 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005685 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005686
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005687 ++vcol;
5688 }
5689 }
5690#endif
5691
Bram Moolenaar53f81742017-09-22 14:35:51 +02005692 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005693 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 row++;
5695
5696 /*
5697 * Update w_cline_height and w_cline_folded if the cursor line was
5698 * updated (saves a call to plines() later).
5699 */
5700 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5701 {
5702 curwin->w_cline_row = startrow;
5703 curwin->w_cline_height = row - startrow;
5704#ifdef FEAT_FOLDING
5705 curwin->w_cline_folded = FALSE;
5706#endif
5707 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5708 }
5709
5710 break;
5711 }
5712
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005713 // Show "extends" character from 'listchars' if beyond the line end and
5714 // 'list' is set.
5715 if (lcs_ext != NUL
5716 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 && !wp->w_p_wrap
5718#ifdef FEAT_DIFF
5719 && filler_todo <= 0
5720#endif
5721 && (
5722#ifdef FEAT_RIGHTLEFT
5723 wp->w_p_rl ? col == 0 :
5724#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005725 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005727 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5729 {
5730 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005731 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005733 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
5735 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005736 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005737 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 else
5740 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005743#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005744 /* advance to the next 'colorcolumn' */
5745 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005746 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005747
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005748 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005749 * highlight the cursor position itself.
5750 * Also highlight the 'colorcolumn' if it is different than
5751 * 'cursorcolumn' */
5752 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005753 if (draw_state == WL_LINE && !lnum_in_visual_area
5754 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005755 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005756 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005757 && lnum != wp->w_cursor.lnum)
5758 {
5759 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005760 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005761 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005762 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005763 {
5764 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005765 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005767 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005768#endif
5769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 /*
5771 * Store character to be displayed.
5772 * Skip characters that are left of the screen for 'nowrap'.
5773 */
5774 vcol_prev = vcol;
5775 if (draw_state < WL_LINE || n_skip <= 0)
5776 {
5777 /*
5778 * Store the character.
5779 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005780#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5782 {
5783 /* A double-wide character is: put first halve in left cell. */
5784 --off;
5785 --col;
5786 }
5787#endif
5788 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005790 {
5791 if ((mb_c & 0xff00) == 0x8e00)
5792 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else if (enc_utf8)
5796 {
5797 if (mb_utf8)
5798 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005799 int i;
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005802 if ((c & 0xff) == 0)
5803 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005804 for (i = 0; i < Screen_mco; ++i)
5805 {
5806 ScreenLinesC[i][off] = u8cc[i];
5807 if (u8cc[i] == 0)
5808 break;
5809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811 else
5812 ScreenLinesUC[off] = 0;
5813 }
5814 if (multi_attr)
5815 {
5816 ScreenAttrs[off] = multi_attr;
5817 multi_attr = 0;
5818 }
5819 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 ScreenAttrs[off] = char_attr;
5821
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5823 {
5824 /* Need to fill two screen columns. */
5825 ++off;
5826 ++col;
5827 if (enc_utf8)
5828 /* UTF-8: Put a 0 in the second screen char. */
5829 ScreenLines[off] = 0;
5830 else
5831 /* DBCS: Put second byte in the second screen char. */
5832 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005833 if (draw_state > WL_NR
5834#ifdef FEAT_DIFF
5835 && filler_todo <= 0
5836#endif
5837 )
5838 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 /* When "tocol" is halfway a character, set it to the end of
5840 * the character, otherwise highlighting won't stop. */
5841 if (tocol == vcol)
5842 ++tocol;
5843#ifdef FEAT_RIGHTLEFT
5844 if (wp->w_p_rl)
5845 {
5846 /* now it's time to backup one cell */
5847 --off;
5848 --col;
5849 }
5850#endif
5851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#ifdef FEAT_RIGHTLEFT
5853 if (wp->w_p_rl)
5854 {
5855 --off;
5856 --col;
5857 }
5858 else
5859#endif
5860 {
5861 ++off;
5862 ++col;
5863 }
5864 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005865#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005866 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005867 {
5868 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005869 ++vcol_off;
5870 if (n_extra > 0)
5871 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005872 if (wp->w_p_wrap)
5873 {
5874 /*
5875 * Special voodoo required if 'wrap' is on.
5876 *
5877 * Advance the column indicator to force the line
5878 * drawing to wrap early. This will make the line
5879 * take up the same screen space when parts are concealed,
5880 * so that cursor line computations aren't messed up.
5881 *
5882 * To avoid the fictitious advance of 'col' causing
5883 * trailing junk to be written out of the screen line
5884 * we are building, 'boguscols' keeps track of the number
5885 * of bad columns we have advanced.
5886 */
5887 if (n_extra > 0)
5888 {
5889 vcol += n_extra;
5890# ifdef FEAT_RIGHTLEFT
5891 if (wp->w_p_rl)
5892 {
5893 col -= n_extra;
5894 boguscols -= n_extra;
5895 }
5896 else
5897# endif
5898 {
5899 col += n_extra;
5900 boguscols += n_extra;
5901 }
5902 n_extra = 0;
5903 n_attr = 0;
5904 }
5905
5906
Bram Moolenaar860cae12010-06-05 23:22:07 +02005907 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5908 {
5909 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005910# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005911 if (wp->w_p_rl)
5912 {
5913 --boguscols;
5914 --col;
5915 }
5916 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005917# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005918 {
5919 ++boguscols;
5920 ++col;
5921 }
5922 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005923
5924# ifdef FEAT_RIGHTLEFT
5925 if (wp->w_p_rl)
5926 {
5927 --boguscols;
5928 --col;
5929 }
5930 else
5931# endif
5932 {
5933 ++boguscols;
5934 ++col;
5935 }
5936 }
5937 else
5938 {
5939 if (n_extra > 0)
5940 {
5941 vcol += n_extra;
5942 n_extra = 0;
5943 n_attr = 0;
5944 }
5945 }
5946
5947 }
5948#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 else
5950 --n_skip;
5951
Bram Moolenaar64486672010-05-16 15:46:46 +02005952 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5953 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005954 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955#ifdef FEAT_DIFF
5956 && filler_todo <= 0
5957#endif
5958 )
5959 ++vcol;
5960
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005961#ifdef FEAT_SYN_HL
5962 if (vcol_save_attr >= 0)
5963 char_attr = vcol_save_attr;
5964#endif
5965
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 /* restore attributes after "predeces" in 'listchars' */
5967 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5968 char_attr = saved_attr3;
5969
5970 /* restore attributes after last 'listchars' or 'number' char */
5971 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5972 char_attr = saved_attr2;
5973
5974 /*
5975 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005976 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 */
5978 if ((
5979#ifdef FEAT_RIGHTLEFT
5980 wp->w_p_rl ? (col < 0) :
5981#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005982 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 && (*ptr != NUL
5984#ifdef FEAT_DIFF
5985 || filler_todo > 0
5986#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005987 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5989 )
5990 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005991#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005992 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005993 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005994 boguscols = 0;
5995#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005996 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005997 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 ++row;
6000 ++screen_row;
6001
6002 /* When not wrapping and finished diff lines, or when displayed
6003 * '$' and highlighting until last column, break here. */
6004 if ((!wp->w_p_wrap
6005#ifdef FEAT_DIFF
6006 && filler_todo <= 0
6007#endif
6008 ) || lcs_eol_one == -1)
6009 break;
6010
6011 /* When the window is too narrow draw all "@" lines. */
6012 if (draw_state != WL_LINE
6013#ifdef FEAT_DIFF
6014 && filler_todo <= 0
6015#endif
6016 )
6017 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006018 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 row = endrow;
6021 }
6022
6023 /* When line got too long for screen break here. */
6024 if (row == endrow)
6025 {
6026 ++row;
6027 break;
6028 }
6029
6030 if (screen_cur_row == screen_row - 1
6031#ifdef FEAT_DIFF
6032 && filler_todo <= 0
6033#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006034 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 {
6036 /* Remember that the line wraps, used for modeless copy. */
6037 LineWraps[screen_row - 1] = TRUE;
6038
6039 /*
6040 * Special trick to make copy/paste of wrapped lines work with
6041 * xterm/screen: write an extra character beyond the end of
6042 * the line. This will work with all terminal types
6043 * (regardless of the xn,am settings).
6044 * Only do this on a fast tty.
6045 * Only do this if the cursor is on the current line
6046 * (something has been written in it).
6047 * Don't do this for the GUI.
6048 * Don't do this for double-width characters.
6049 * Don't do this for a window not at the right screen border.
6050 */
6051 if (p_tf
6052#ifdef FEAT_GUI
6053 && !gui.in_use
6054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006056 && ((*mb_off2cells)(LineOffset[screen_row],
6057 LineOffset[screen_row] + screen_Columns)
6058 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006060 + (int)Columns - 2,
6061 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006062 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 {
6064 /* First make sure we are at the end of the screen line,
6065 * then output the same character again to let the
6066 * terminal know about the wrap. If the terminal doesn't
6067 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006068 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 screen_char(LineOffset[screen_row - 1]
6070 + (unsigned)Columns - 1,
6071 screen_row - 1, (int)(Columns - 1));
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /* When there is a multi-byte character, just output a
6074 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006075 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6076 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 out_char(' ');
6078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 out_char(ScreenLines[LineOffset[screen_row - 1]
6080 + (Columns - 1)]);
6081 /* force a redraw of the first char on the next line */
6082 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6083 screen_start(); /* don't know where cursor is now */
6084 }
6085 }
6086
6087 col = 0;
6088 off = (unsigned)(current_ScreenLine - ScreenLines);
6089#ifdef FEAT_RIGHTLEFT
6090 if (wp->w_p_rl)
6091 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006092 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 off += col;
6094 }
6095#endif
6096
6097 /* reset the drawing state for the start of a wrapped line */
6098 draw_state = WL_START;
6099 saved_n_extra = n_extra;
6100 saved_p_extra = p_extra;
6101 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006102 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 saved_char_attr = char_attr;
6104 n_extra = 0;
6105 lcs_prec_todo = lcs_prec;
6106#ifdef FEAT_LINEBREAK
6107# ifdef FEAT_DIFF
6108 if (filler_todo <= 0)
6109# endif
6110 need_showbreak = TRUE;
6111#endif
6112#ifdef FEAT_DIFF
6113 --filler_todo;
6114 /* When the filler lines are actually below the last line of the
6115 * file, don't draw the line itself, break here. */
6116 if (filler_todo == 0 && wp->w_botfill)
6117 break;
6118#endif
6119 }
6120
6121 } /* for every character in the line */
6122
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006123#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006124 /* After an empty line check first word for capital. */
6125 if (*skipwhite(line) == NUL)
6126 {
6127 capcol_lnum = lnum + 1;
6128 cap_col = 0;
6129 }
6130#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006131#ifdef FEAT_TEXT_PROP
6132 vim_free(text_props);
6133 vim_free(text_prop_idxs);
6134#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006135
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006136 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 return row;
6138}
6139
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006140/*
6141 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006142 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143 */
6144 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006145comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006146{
6147 int i;
6148
6149 for (i = 0; i < Screen_mco; ++i)
6150 {
6151 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6152 return TRUE;
6153 if (ScreenLinesC[i][off_from] == 0)
6154 break;
6155 }
6156 return FALSE;
6157}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006158
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159/*
6160 * Check whether the given character needs redrawing:
6161 * - the (first byte of the) character is different
6162 * - the attributes are different
6163 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006164 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 */
6166 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006167char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
6169 if (cols > 0
6170 && ((ScreenLines[off_from] != ScreenLines[off_to]
6171 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 || (enc_dbcs != 0
6173 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6174 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6175 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6176 : (cols > 1 && ScreenLines[off_from + 1]
6177 != ScreenLines[off_to + 1])))
6178 || (enc_utf8
6179 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6180 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006181 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006182 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6183 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006184 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 return TRUE;
6186 return FALSE;
6187}
6188
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006189#if defined(FEAT_TERMINAL) || defined(PROTO)
6190/*
6191 * Return the index in ScreenLines[] for the current screen line.
6192 */
6193 int
6194screen_get_current_line_off()
6195{
6196 return (int)(current_ScreenLine - ScreenLines);
6197}
6198#endif
6199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200/*
6201 * Move one "cooked" screen line to the screen, but only the characters that
6202 * have actually changed. Handle insert/delete character.
6203 * "coloff" gives the first column on the screen for this line.
6204 * "endcol" gives the columns where valid characters are.
6205 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6206 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006207 * "flags" can have bits:
6208 * SLF_POPUP popup window
6209 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6211 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6212 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006214screen_line(
6215 int row,
6216 int coloff,
6217 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006218 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006219 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 unsigned off_from;
6222 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006223 unsigned max_off_from;
6224 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 int force = FALSE; /* force update rest of the line */
6228 int redraw_this /* bool: does character need redraw? */
6229#ifdef FEAT_GUI
6230 = TRUE /* For GUI when while-loop empty */
6231#endif
6232 ;
6233 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int clear_next = FALSE;
6235 int char_cells; /* 1: normal char */
6236 /* 2: occupies two display cells */
6237# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006239 /* Check for illegal row and col, just in case. */
6240 if (row >= Rows)
6241 row = Rows - 1;
6242 if (endcol > Columns)
6243 endcol = Columns;
6244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245# ifdef FEAT_CLIPBOARD
6246 clip_may_clear_selection(row, row);
6247# endif
6248
6249 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6250 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006251 max_off_from = off_from + screen_Columns;
6252 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
6254#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006255 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
6257 /* Clear rest first, because it's left of the text. */
6258 if (clear_width > 0)
6259 {
6260 while (col <= endcol && ScreenLines[off_to] == ' '
6261 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006262 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
6264 ++off_to;
6265 ++col;
6266 }
6267 if (col <= endcol)
6268 screen_fill(row, row + 1, col + coloff,
6269 endcol + coloff + 1, ' ', ' ', 0);
6270 }
6271 col = endcol + 1;
6272 off_to = LineOffset[row] + col + coloff;
6273 off_from += col;
6274 endcol = (clear_width > 0 ? clear_width : -clear_width);
6275 }
6276#endif /* FEAT_RIGHTLEFT */
6277
6278 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6279
6280 while (col < endcol)
6281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006283 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 else
6285 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
6287 redraw_this = redraw_next;
6288 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6289 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6290
6291#ifdef FEAT_GUI
6292 /* If the next character was bold, then redraw the current character to
6293 * remove any pixels that might have spilt over into us. This only
6294 * happens in the GUI.
6295 */
6296 if (redraw_next && gui.in_use)
6297 {
6298 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006299 if (hl > HL_ALL)
6300 hl = syn_attr2attr(hl);
6301 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 redraw_this = TRUE;
6303 }
6304#endif
6305
6306 if (redraw_this)
6307 {
6308 /*
6309 * Special handling when 'xs' termcap flag set (hpterm):
6310 * Attributes for characters are stored at the position where the
6311 * cursor is when writing the highlighting code. The
6312 * start-highlighting code must be written with the cursor on the
6313 * first highlighted character. The stop-highlighting code must
6314 * be written with the cursor just after the last highlighted
6315 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006316 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 * to clear the rest of the line, and force redrawing it
6318 * completely.
6319 */
6320 if ( p_wiv
6321 && !force
6322#ifdef FEAT_GUI
6323 && !gui.in_use
6324#endif
6325 && ScreenAttrs[off_to] != 0
6326 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6327 {
6328 /*
6329 * Need to remove highlighting attributes here.
6330 */
6331 windgoto(row, col + coloff);
6332 out_str(T_CE); /* clear rest of this screen line */
6333 screen_start(); /* don't know where cursor is now */
6334 force = TRUE; /* force redraw of rest of the line */
6335 redraw_next = TRUE; /* or else next char would miss out */
6336
6337 /*
6338 * If the previous character was highlighted, need to stop
6339 * highlighting at this character.
6340 */
6341 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6342 {
6343 screen_attr = ScreenAttrs[off_to - 1];
6344 term_windgoto(row, col + coloff);
6345 screen_stop_highlight();
6346 }
6347 else
6348 screen_attr = 0; /* highlighting has stopped */
6349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 if (enc_dbcs != 0)
6351 {
6352 /* Check if overwriting a double-byte with a single-byte or
6353 * the other way around requires another character to be
6354 * redrawn. For UTF-8 this isn't needed, because comparing
6355 * ScreenLinesUC[] is sufficient. */
6356 if (char_cells == 1
6357 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006358 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 {
6360 /* Writing a single-cell character over a double-cell
6361 * character: need to redraw the next cell. */
6362 ScreenLines[off_to + 1] = 0;
6363 redraw_next = TRUE;
6364 }
6365 else if (char_cells == 2
6366 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006367 && (*mb_off2cells)(off_to, max_off_to) == 1
6368 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 {
6370 /* Writing the second half of a double-cell character over
6371 * a double-cell character: need to redraw the second
6372 * cell. */
6373 ScreenLines[off_to + 2] = 0;
6374 redraw_next = TRUE;
6375 }
6376
6377 if (enc_dbcs == DBCS_JPNU)
6378 ScreenLines2[off_to] = ScreenLines2[off_from];
6379 }
6380 /* When writing a single-width character over a double-width
6381 * character and at the end of the redrawn text, need to clear out
6382 * the right halve of the old character.
6383 * Also required when writing the right halve of a double-width
6384 * char over the left halve of an existing one. */
6385 if (has_mbyte && col + char_cells == endcol
6386 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006387 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006389 && (*mb_off2cells)(off_to, max_off_to) == 1
6390 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (enc_utf8)
6395 {
6396 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6397 if (ScreenLinesUC[off_from] != 0)
6398 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006399 int i;
6400
6401 for (i = 0; i < Screen_mco; ++i)
6402 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
6404 }
6405 if (char_cells == 2)
6406 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006409 /* The bold trick makes a single column of pixels appear in the
6410 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 * character should be redrawn too. This happens for our own GUI
6412 * and for some xterms. */
6413 if (
6414# ifdef FEAT_GUI
6415 gui.in_use
6416# endif
6417# if defined(FEAT_GUI) && defined(UNIX)
6418 ||
6419# endif
6420# ifdef UNIX
6421 term_is_xterm
6422# endif
6423 )
6424 {
6425 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006426 if (hl > HL_ALL)
6427 hl = syn_attr2attr(hl);
6428 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 redraw_next = TRUE;
6430 }
6431#endif
6432 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006433
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006434 /* For simplicity set the attributes of second half of a
6435 * double-wide character equal to the first half. */
6436 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006438
6439 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 screen_char(off_to, row, col + coloff);
6443 }
6444 else if ( p_wiv
6445#ifdef FEAT_GUI
6446 && !gui.in_use
6447#endif
6448 && col + coloff > 0)
6449 {
6450 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6451 {
6452 /*
6453 * Don't output stop-highlight when moving the cursor, it will
6454 * stop the highlighting when it should continue.
6455 */
6456 screen_attr = 0;
6457 }
6458 else if (screen_attr != 0)
6459 screen_stop_highlight();
6460 }
6461
6462 off_to += CHAR_CELLS;
6463 off_from += CHAR_CELLS;
6464 col += CHAR_CELLS;
6465 }
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (clear_next)
6468 {
6469 /* Clear the second half of a double-wide character of which the left
6470 * half was overwritten with a single-wide character. */
6471 ScreenLines[off_to] = ' ';
6472 if (enc_utf8)
6473 ScreenLinesUC[off_to] = 0;
6474 screen_char(off_to, row, col + coloff);
6475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
6477 if (clear_width > 0
6478#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006479 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480#endif
6481 )
6482 {
6483#ifdef FEAT_GUI
6484 int startCol = col;
6485#endif
6486
6487 /* blank out the rest of the line */
6488 while (col < clear_width && ScreenLines[off_to] == ' '
6489 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006490 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
6492 ++off_to;
6493 ++col;
6494 }
6495 if (col < clear_width)
6496 {
6497#ifdef FEAT_GUI
6498 /*
6499 * In the GUI, clearing the rest of the line may leave pixels
6500 * behind if the first character cleared was bold. Some bold
6501 * fonts spill over the left. In this case we redraw the previous
6502 * character too. If we didn't skip any blanks above, then we
6503 * only redraw if the character wasn't already redrawn anyway.
6504 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006505 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
6507 hl = ScreenAttrs[off_to];
6508 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006509 {
6510 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006511
Bram Moolenaar9c697322006-10-09 20:11:17 +00006512 if (enc_utf8)
6513 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6514 * that its width is 2. */
6515 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6516 else if (enc_dbcs != 0)
6517 {
6518 /* find previous character by counting from first
6519 * column and get its width. */
6520 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006521 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006522
6523 while (off < off_to)
6524 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006525 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006526 off += prev_cells;
6527 }
6528 }
6529
6530 if (enc_dbcs != 0 && prev_cells > 1)
6531 screen_char_2(off_to - prev_cells, row,
6532 col + coloff - prev_cells);
6533 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006534 screen_char(off_to - prev_cells, row,
6535 col + coloff - prev_cells);
6536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
6538#endif
6539 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6540 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 off_to += clear_width - col;
6542 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544 }
6545
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006546 if (clear_width > 0
6547#ifdef FEAT_TEXT_PROP
6548 && !(flags & SLF_POPUP) // no separator for popup window
6549#endif
6550 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006552 // For a window that has a right neighbor, draw the separator char
6553 // right of the window contents.
6554 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 int c;
6557
6558 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006559 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006560 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6561 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 || ScreenAttrs[off_to] != hl)
6563 {
6564 ScreenLines[off_to] = c;
6565 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (enc_utf8)
6567 {
6568 if (c >= 0x80)
6569 {
6570 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006571 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573 else
6574 ScreenLinesUC[off_to] = 0;
6575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 screen_char(off_to, row, col + coloff);
6577 }
6578 }
6579 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 LineWraps[row] = FALSE;
6581 }
6582}
6583
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006584#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006586 * Mirror text "str" for right-left displaying.
6587 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006589 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006590rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 char_u *p1, *p2;
6593 int t;
6594
6595 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6596 {
6597 t = *p1;
6598 *p1 = *p2;
6599 *p2 = t;
6600 }
6601}
6602#endif
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604/*
6605 * mark all status lines for redraw; used after first :cd
6606 */
6607 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006608status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610 win_T *wp;
6611
Bram Moolenaar29323592016-07-24 22:04:11 +02006612 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 if (wp->w_status_height)
6614 {
6615 wp->w_redr_status = TRUE;
6616 redraw_later(VALID);
6617 }
6618}
6619
6620/*
6621 * mark all status lines of the current buffer for redraw
6622 */
6623 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006624status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 win_T *wp;
6627
Bram Moolenaar29323592016-07-24 22:04:11 +02006628 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6630 {
6631 wp->w_redr_status = TRUE;
6632 redraw_later(VALID);
6633 }
6634}
6635
6636/*
6637 * Redraw all status lines that need to be redrawn.
6638 */
6639 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006640redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 win_T *wp;
6643
Bram Moolenaar29323592016-07-24 22:04:11 +02006644 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006646 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006647 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006648 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
Bram Moolenaar4033c552017-09-16 20:54:51 +02006651#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652/*
6653 * Redraw all status lines at the bottom of frame "frp".
6654 */
6655 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006656win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 if (frp->fr_layout == FR_LEAF)
6659 frp->fr_win->w_redr_status = TRUE;
6660 else if (frp->fr_layout == FR_ROW)
6661 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006662 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 win_redraw_last_status(frp);
6664 }
6665 else /* frp->fr_layout == FR_COL */
6666 {
6667 frp = frp->fr_child;
6668 while (frp->fr_next != NULL)
6669 frp = frp->fr_next;
6670 win_redraw_last_status(frp);
6671 }
6672}
6673#endif
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675/*
6676 * Draw the verticap separator right of window "wp" starting with line "row".
6677 */
6678 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006679draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 int hl;
6682 int c;
6683
6684 if (wp->w_vsep_width)
6685 {
6686 /* draw the vertical separator right of this window */
6687 c = fillchar_vsep(&hl);
6688 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6689 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6690 c, ' ', hl);
6691 }
6692}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006695static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006698 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 */
6700 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006701status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 int len = 0;
6704
6705#ifdef FEAT_MENU
6706 int emenu = (xp->xp_context == EXPAND_MENUS
6707 || xp->xp_context == EXPAND_MENUNAMES);
6708
6709 /* Check for menu separators - replace with '|'. */
6710 if (emenu && menu_is_separator(s))
6711 return 1;
6712#endif
6713
6714 while (*s != NUL)
6715 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006716 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006717 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006718 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
6720
6721 return len;
6722}
6723
6724/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006725 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006726 * These are backslashes used for escaping. Do show backslashes in help tags.
6727 */
6728 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006729skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006730{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006731 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006732#ifdef FEAT_MENU
6733 || ((xp->xp_context == EXPAND_MENUS
6734 || xp->xp_context == EXPAND_MENUNAMES)
6735 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6736#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006737 )
6738 {
6739#ifndef BACKSLASH_IN_FILENAME
6740 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6741 return 2;
6742#endif
6743 return 1;
6744 }
6745 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006746}
6747
6748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 * Show wildchar matches in the status line.
6750 * Show at least the "match" item.
6751 * We start at item 'first_match' in the list and show all matches that fit.
6752 *
6753 * If inversion is possible we use it. Else '=' characters are used.
6754 */
6755 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006756win_redr_status_matches(
6757 expand_T *xp,
6758 int num_matches,
6759 char_u **matches, /* list of matches */
6760 int match,
6761 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6764 int row;
6765 char_u *buf;
6766 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006767 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 int fillchar;
6769 int attr;
6770 int i;
6771 int highlight = TRUE;
6772 char_u *selstart = NULL;
6773 int selstart_col = 0;
6774 char_u *selend = NULL;
6775 static int first_match = 0;
6776 int add_left = FALSE;
6777 char_u *s;
6778#ifdef FEAT_MENU
6779 int emenu;
6780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 if (matches == NULL) /* interrupted completion? */
6784 return;
6785
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006786 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006787 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006788 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006789 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (buf == NULL)
6791 return;
6792
6793 if (match == -1) /* don't show match but original text */
6794 {
6795 match = 0;
6796 highlight = FALSE;
6797 }
6798 /* count 1 for the ending ">" */
6799 clen = status_match_len(xp, L_MATCH(match)) + 3;
6800 if (match == 0)
6801 first_match = 0;
6802 else if (match < first_match)
6803 {
6804 /* jumping left, as far as we can go */
6805 first_match = match;
6806 add_left = TRUE;
6807 }
6808 else
6809 {
6810 /* check if match fits on the screen */
6811 for (i = first_match; i < match; ++i)
6812 clen += status_match_len(xp, L_MATCH(i)) + 2;
6813 if (first_match > 0)
6814 clen += 2;
6815 /* jumping right, put match at the left */
6816 if ((long)clen > Columns)
6817 {
6818 first_match = match;
6819 /* if showing the last match, we can add some on the left */
6820 clen = 2;
6821 for (i = match; i < num_matches; ++i)
6822 {
6823 clen += status_match_len(xp, L_MATCH(i)) + 2;
6824 if ((long)clen >= Columns)
6825 break;
6826 }
6827 if (i == num_matches)
6828 add_left = TRUE;
6829 }
6830 }
6831 if (add_left)
6832 while (first_match > 0)
6833 {
6834 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6835 if ((long)clen >= Columns)
6836 break;
6837 --first_match;
6838 }
6839
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006840 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
6842 if (first_match == 0)
6843 {
6844 *buf = NUL;
6845 len = 0;
6846 }
6847 else
6848 {
6849 STRCPY(buf, "< ");
6850 len = 2;
6851 }
6852 clen = len;
6853
6854 i = first_match;
6855 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6856 {
6857 if (i == match)
6858 {
6859 selstart = buf + len;
6860 selstart_col = clen;
6861 }
6862
6863 s = L_MATCH(i);
6864 /* Check for menu separators - replace with '|' */
6865#ifdef FEAT_MENU
6866 emenu = (xp->xp_context == EXPAND_MENUS
6867 || xp->xp_context == EXPAND_MENUNAMES);
6868 if (emenu && menu_is_separator(s))
6869 {
6870 STRCPY(buf + len, transchar('|'));
6871 l = (int)STRLEN(buf + len);
6872 len += l;
6873 clen += l;
6874 }
6875 else
6876#endif
6877 for ( ; *s != NUL; ++s)
6878 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006879 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006881 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
6883 STRNCPY(buf + len, s, l);
6884 s += l - 1;
6885 len += l;
6886 }
6887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {
6889 STRCPY(buf + len, transchar_byte(*s));
6890 len += (int)STRLEN(buf + len);
6891 }
6892 }
6893 if (i == match)
6894 selend = buf + len;
6895
6896 *(buf + len++) = ' ';
6897 *(buf + len++) = ' ';
6898 clen += 2;
6899 if (++i == num_matches)
6900 break;
6901 }
6902
6903 if (i != num_matches)
6904 {
6905 *(buf + len++) = '>';
6906 ++clen;
6907 }
6908
6909 buf[len] = NUL;
6910
6911 row = cmdline_row - 1;
6912 if (row >= 0)
6913 {
6914 if (wild_menu_showing == 0)
6915 {
6916 if (msg_scrolled > 0)
6917 {
6918 /* Put the wildmenu just above the command line. If there is
6919 * no room, scroll the screen one line up. */
6920 if (cmdline_row == Rows - 1)
6921 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006922 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 ++msg_scrolled;
6924 }
6925 else
6926 {
6927 ++cmdline_row;
6928 ++row;
6929 }
6930 wild_menu_showing = WM_SCROLLED;
6931 }
6932 else
6933 {
6934 /* Create status line if needed by setting 'laststatus' to 2.
6935 * Set 'winminheight' to zero to avoid that the window is
6936 * resized. */
6937 if (lastwin->w_status_height == 0)
6938 {
6939 save_p_ls = p_ls;
6940 save_p_wmh = p_wmh;
6941 p_ls = 2;
6942 p_wmh = 0;
6943 last_status(FALSE);
6944 }
6945 wild_menu_showing = WM_SHOWN;
6946 }
6947 }
6948
6949 screen_puts(buf, row, 0, attr);
6950 if (selstart != NULL && highlight)
6951 {
6952 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006953 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955
6956 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6957 }
6958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 vim_free(buf);
6961}
6962#endif
6963
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964/*
6965 * Redraw the status line of window wp.
6966 *
6967 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006968 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6969 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006971 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006972win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973{
6974 int row;
6975 char_u *p;
6976 int len;
6977 int fillchar;
6978 int attr;
6979 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006980 static int busy = FALSE;
6981
6982 /* It's possible to get here recursively when 'statusline' (indirectly)
6983 * invokes ":redrawstatus". Simply ignore the call then. */
6984 if (busy)
6985 return;
6986 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
6988 wp->w_redr_status = FALSE;
6989 if (wp->w_status_height == 0)
6990 {
6991 /* no status line, can only be last window */
6992 redraw_cmdline = TRUE;
6993 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006994 else if (!redrawing()
6995#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006996 // don't update status line when popup menu is visible and may be
6997 // drawn over it, unless it will be redrawn later
6998 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006999#endif
7000 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 {
7002 /* Don't redraw right now, do it later. */
7003 wp->w_redr_status = TRUE;
7004 }
7005#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007006 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
7008 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007009 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011#endif
7012 else
7013 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007014 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007016 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 p = NameBuff;
7018 len = (int)STRLEN(p);
7019
Bram Moolenaard85f2712017-07-28 21:51:57 +02007020 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021#ifdef FEAT_QUICKFIX
7022 || wp->w_p_pvw
7023#endif
7024 || bufIsChanged(wp->w_buffer)
7025 || wp->w_buffer->b_p_ro)
7026 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007027 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007029 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 len += (int)STRLEN(p + len);
7031 }
7032#ifdef FEAT_QUICKFIX
7033 if (wp->w_p_pvw)
7034 {
7035 STRCPY(p + len, _("[Preview]"));
7036 len += (int)STRLEN(p + len);
7037 }
7038#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007039 if (bufIsChanged(wp->w_buffer)
7040#ifdef FEAT_TERMINAL
7041 && !bt_terminal(wp->w_buffer)
7042#endif
7043 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
7045 STRCPY(p + len, "[+]");
7046 len += 3;
7047 }
7048 if (wp->w_buffer->b_p_ro)
7049 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007050 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007051 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
7053
Bram Moolenaar02631462017-09-22 15:20:32 +02007054 this_ru_col = ru_col - (Columns - wp->w_width);
7055 if (this_ru_col < (wp->w_width + 1) / 2)
7056 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 if (this_ru_col <= 1)
7058 {
7059 p = (char_u *)"<"; /* No room for file name! */
7060 len = 1;
7061 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007062 else if (has_mbyte)
7063 {
7064 int clen = 0, i;
7065
7066 /* Count total number of display cells. */
7067 clen = mb_string2cells(p, -1);
7068
7069 /* Find first character that will fit.
7070 * Going from start to end is much faster for DBCS. */
7071 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7072 i += (*mb_ptr2len)(p + i))
7073 clen -= (*mb_ptr2cells)(p + i);
7074 len = clen;
7075 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007077 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007079 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081
Bram Moolenaara12a1612019-01-24 16:39:02 +01007082 }
7083 else if (len > this_ru_col - 1)
7084 {
7085 p += len - (this_ru_col - 1);
7086 *p = '<';
7087 len = this_ru_col - 1;
7088 }
7089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007091 screen_puts(p, row, wp->w_wincol, attr);
7092 screen_fill(row, row + 1, len + wp->w_wincol,
7093 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007095 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7097 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007098 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007101 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102#endif
7103 }
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 /*
7106 * May need to draw the character below the vertical separator.
7107 */
7108 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7109 {
7110 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007111 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 else
7113 fillchar = fillchar_vsep(&attr);
7114 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7115 attr);
7116 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007117 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119
Bram Moolenaar238a5642006-02-21 22:12:05 +00007120#ifdef FEAT_STL_OPT
7121/*
7122 * Redraw the status line according to 'statusline' and take care of any
7123 * errors encountered.
7124 */
7125 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007126redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007127{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007128 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007129 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007130
7131 /* When called recursively return. This can happen when the statusline
7132 * contains an expression that triggers a redraw. */
7133 if (entered)
7134 return;
7135 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007136
Bram Moolenaara742e082016-04-05 21:10:38 +02007137 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007138 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007139 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007140 {
7141 /* When there is an error disable the statusline, otherwise the
7142 * display is messed up with errors and a redraw triggers the problem
7143 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007144 set_string_option_direct((char_u *)"statusline", -1,
7145 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007146 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007147 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007148 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007149 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007150}
7151#endif
7152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153/*
7154 * Return TRUE if the status line of window "wp" is connected to the status
7155 * line of the window right of it. If not, then it's a vertical separator.
7156 * Only call if (wp->w_vsep_width != 0).
7157 */
7158 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007159stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 frame_T *fr;
7162
7163 fr = wp->w_frame;
7164 while (fr->fr_parent != NULL)
7165 {
7166 if (fr->fr_parent->fr_layout == FR_COL)
7167 {
7168 if (fr->fr_next != NULL)
7169 break;
7170 }
7171 else
7172 {
7173 if (fr->fr_next != NULL)
7174 return TRUE;
7175 }
7176 fr = fr->fr_parent;
7177 }
7178 return FALSE;
7179}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182/*
7183 * Get the value to show for the language mappings, active 'keymap'.
7184 */
7185 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007186get_keymap_str(
7187 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007188 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007189 char_u *buf, /* buffer for the result */
7190 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 char_u *p;
7193
7194 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7195 return FALSE;
7196
7197 {
7198#ifdef FEAT_EVAL
7199 buf_T *old_curbuf = curbuf;
7200 win_T *old_curwin = curwin;
7201 char_u *s;
7202
7203 curbuf = wp->w_buffer;
7204 curwin = wp;
7205 STRCPY(buf, "b:keymap_name"); /* must be writable */
7206 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007207 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 --emsg_skip;
7209 curbuf = old_curbuf;
7210 curwin = old_curwin;
7211 if (p == NULL || *p == NUL)
7212#endif
7213 {
7214#ifdef FEAT_KEYMAP
7215 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7216 p = wp->w_buffer->b_p_keymap;
7217 else
7218#endif
7219 p = (char_u *)"lang";
7220 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007221 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 buf[0] = NUL;
7223#ifdef FEAT_EVAL
7224 vim_free(s);
7225#endif
7226 }
7227 return buf[0] != NUL;
7228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230#if defined(FEAT_STL_OPT) || defined(PROTO)
7231/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007232 * Redraw the status line or ruler of window "wp".
7233 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 */
7235 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007236win_redr_custom(
7237 win_T *wp,
7238 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007240 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 int attr;
7242 int curattr;
7243 int row;
7244 int col = 0;
7245 int maxwidth;
7246 int width;
7247 int n;
7248 int len;
7249 int fillchar;
7250 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007251 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007253 struct stl_hlrec hltab[STL_MAX_ITEM];
7254 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007255 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007256 win_T *ewp;
7257 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaar1d633412013-12-11 15:52:01 +01007259 /* There is a tiny chance that this gets called recursively: When
7260 * redrawing a status line triggers redrawing the ruler or tabline.
7261 * Avoid trouble by not allowing recursion. */
7262 if (entered)
7263 return;
7264 entered = TRUE;
7265
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007267 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007269 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007270 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007271 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007272 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007273 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007274 maxwidth = Columns;
7275# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007276 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007279 else
7280 {
7281 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007282 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007283 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284
7285 if (draw_ruler)
7286 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007287 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007288 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007289 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007290 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007291 if (*++stl == '-')
7292 stl++;
7293 if (atoi((char *)stl))
7294 while (VIM_ISDIGIT(*stl))
7295 stl++;
7296 if (*stl++ != '(')
7297 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007298 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007299 col = ru_col - (Columns - wp->w_width);
7300 if (col < (wp->w_width + 1) / 2)
7301 col = (wp->w_width + 1) / 2;
7302 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007304 {
7305 row = Rows - 1;
7306 --maxwidth; /* writing in last column may cause scrolling */
7307 fillchar = ' ';
7308 attr = 0;
7309 }
7310
7311# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007312 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007313# endif
7314 }
7315 else
7316 {
7317 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007318 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007320 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007321# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007322 use_sandbox = was_set_insecurely((char_u *)"statusline",
7323 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007324# endif
7325 }
7326
Bram Moolenaar53f81742017-09-22 14:35:51 +02007327 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328 }
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007331 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
Bram Moolenaar61452852011-02-01 18:01:11 +01007333 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7334 * the cursor away and back. */
7335 ewp = wp == NULL ? curwin : wp;
7336 p_crb_save = ewp->w_p_crb;
7337 ewp->w_p_crb = FALSE;
7338
Bram Moolenaar362f3562009-11-03 16:20:34 +00007339 /* Make a copy, because the statusline may include a function call that
7340 * might change the option value and free the memory. */
7341 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007342 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007343 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007344 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007345 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007346 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007348 /* Make all characters printable. */
7349 p = transstr(buf);
7350 if (p != NULL)
7351 {
7352 vim_strncpy(buf, p, sizeof(buf) - 1);
7353 vim_free(p);
7354 }
7355
7356 /* fill up with "fillchar" */
7357 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007358 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 ++width;
7362 }
7363 buf[len] = NUL;
7364
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007365 /*
7366 * Draw each snippet with the specified highlighting.
7367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 curattr = attr;
7369 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007370 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007372 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 screen_puts_len(p, len, row, col, curattr);
7374 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007375 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007379 else if (hltab[n].userhl < 0)
7380 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007381#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007382 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7383 && wp->w_status_height != 0)
7384 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007385 else if (wp != NULL && bt_terminal(wp->w_buffer)
7386 && wp->w_status_height != 0)
7387 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007388#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007389 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007390 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007392 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
7394 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007395
7396 if (wp == NULL)
7397 {
7398 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7399 col = 0;
7400 len = 0;
7401 p = buf;
7402 fillchar = 0;
7403 for (n = 0; tabtab[n].start != NULL; n++)
7404 {
7405 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7406 while (col < len)
7407 TabPageIdxs[col++] = fillchar;
7408 p = tabtab[n].start;
7409 fillchar = tabtab[n].userhl;
7410 }
7411 while (col < Columns)
7412 TabPageIdxs[col++] = fillchar;
7413 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007414
7415theend:
7416 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
7419#endif /* FEAT_STL_OPT */
7420
7421/*
7422 * Output a single character directly to the screen and update ScreenLines.
7423 */
7424 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007425screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 char_u buf[MB_MAXBYTES + 1];
7428
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007429 if (has_mbyte)
7430 buf[(*mb_char2bytes)(c, buf)] = NUL;
7431 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007432 {
7433 buf[0] = c;
7434 buf[1] = NUL;
7435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 screen_puts(buf, row, col, attr);
7437}
7438
7439/*
7440 * Get a single character directly from ScreenLines into "bytes[]".
7441 * Also return its attribute in *attrp;
7442 */
7443 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007444screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 unsigned off;
7447
7448 /* safety check */
7449 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7450 {
7451 off = LineOffset[row] + col;
7452 *attrp = ScreenAttrs[off];
7453 bytes[0] = ScreenLines[off];
7454 bytes[1] = NUL;
7455
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 if (enc_utf8 && ScreenLinesUC[off] != 0)
7457 bytes[utfc_char2bytes(off, bytes)] = NUL;
7458 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7459 {
7460 bytes[0] = ScreenLines[off];
7461 bytes[1] = ScreenLines2[off];
7462 bytes[2] = NUL;
7463 }
7464 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7465 {
7466 bytes[1] = ScreenLines[off + 1];
7467 bytes[2] = NUL;
7468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470}
7471
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007472/*
7473 * Return TRUE if composing characters for screen posn "off" differs from
7474 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007475 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007476 */
7477 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007478screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479{
7480 int i;
7481
7482 for (i = 0; i < Screen_mco; ++i)
7483 {
7484 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7485 return TRUE;
7486 if (u8cc[i] == 0)
7487 break;
7488 }
7489 return FALSE;
7490}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492/*
7493 * Put string '*text' on the screen at position 'row' and 'col', with
7494 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7495 * Note: only outputs within one row, message is truncated at screen boundary!
7496 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7497 */
7498 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007499screen_puts(
7500 char_u *text,
7501 int row,
7502 int col,
7503 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504{
7505 screen_puts_len(text, -1, row, col, attr);
7506}
7507
7508/*
7509 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7510 * a NUL.
7511 */
7512 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007513screen_puts_len(
7514 char_u *text,
7515 int textlen,
7516 int row,
7517 int col,
7518 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
7520 unsigned off;
7521 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007522 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007524 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 int mbyte_blen = 1;
7526 int mbyte_cells = 1;
7527 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007528 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007530#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007532 int pc, nc, nc1;
7533 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 int force_redraw_this;
7536 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007537 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
7539 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7540 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007541 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542
Bram Moolenaarc236c162008-07-13 17:41:49 +00007543 /* When drawing over the right halve of a double-wide char clear out the
7544 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007545 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007546#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007547 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007548#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007549 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 {
7551 ScreenLines[off - 1] = ' ';
7552 ScreenAttrs[off - 1] = 0;
7553 if (enc_utf8)
7554 {
7555 ScreenLinesUC[off - 1] = 0;
7556 ScreenLinesC[0][off - 1] = 0;
7557 }
7558 /* redraw the previous cell, make it empty */
7559 screen_char(off - 1, row, col - 1);
7560 /* force the cell at "col" to be redrawn */
7561 force_redraw_next = TRUE;
7562 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007563
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007565 while (col < screen_Columns
7566 && (len < 0 || (int)(ptr - text) < len)
7567 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
7569 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 /* check if this is the first byte of a multibyte */
7571 if (has_mbyte)
7572 {
7573 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007574 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007576 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7578 mbyte_cells = 1;
7579 else if (enc_dbcs != 0)
7580 mbyte_cells = mbyte_blen;
7581 else /* enc_utf8 */
7582 {
7583 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007584 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 (int)((text + len) - ptr));
7586 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007587 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007589#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7591 {
7592 /* Do Arabic shaping. */
7593 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7594 {
7595 /* Past end of string to be displayed. */
7596 nc = NUL;
7597 nc1 = NUL;
7598 }
7599 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007600 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007601 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7602 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 nc1 = pcc[0];
7604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 pc = prev_c;
7606 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007607 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609 else
7610 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007611#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007612 if (col + mbyte_cells > screen_Columns)
7613 {
7614 /* Only 1 cell left, but character requires 2 cells:
7615 * display a '>' in the last column to avoid wrapping. */
7616 c = '>';
7617 mbyte_cells = 1;
7618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
7620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007622 force_redraw_this = force_redraw_next;
7623 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007624
7625 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 || (mbyte_cells == 2
7627 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7628 || (enc_dbcs == DBCS_JPNU
7629 && c == 0x8e
7630 && ScreenLines2[off] != ptr[1])
7631 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007632 && (ScreenLinesUC[off] !=
7633 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7634 || (ScreenLinesUC[off] != 0
7635 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637 || exmode_active;
7638
Bram Moolenaara12a1612019-01-24 16:39:02 +01007639 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {
7641#if defined(FEAT_GUI) || defined(UNIX)
7642 /* The bold trick makes a single row of pixels appear in the next
7643 * character. When a bold character is removed, the next
7644 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007645 * and for some xterms. */
7646 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647# ifdef FEAT_GUI
7648 gui.in_use
7649# endif
7650# if defined(FEAT_GUI) && defined(UNIX)
7651 ||
7652# endif
7653# ifdef UNIX
7654 term_is_xterm
7655# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007656 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007658 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007660 if (n > HL_ALL)
7661 n = syn_attr2attr(n);
7662 if (n & HL_BOLD)
7663 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 /* When at the end of the text and overwriting a two-cell
7667 * character with a one-cell character, need to clear the next
7668 * cell. Also when overwriting the left halve of a two-cell char
7669 * with the right halve of a two-cell char. Do this only once
7670 * (mb_off2cells() may return 2 on the right halve). */
7671 if (clear_next_cell)
7672 clear_next_cell = FALSE;
7673 else if (has_mbyte
7674 && (len < 0 ? ptr[mbyte_blen] == NUL
7675 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007676 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007678 && (*mb_off2cells)(off, max_off) == 1
7679 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 clear_next_cell = TRUE;
7681
7682 /* Make sure we never leave a second byte of a double-byte behind,
7683 * it confuses mb_off2cells(). */
7684 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007685 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007687 && (*mb_off2cells)(off, max_off) == 1
7688 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 ScreenLines[off] = c;
7691 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 if (enc_utf8)
7693 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007694 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 ScreenLinesUC[off] = 0;
7696 else
7697 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007698 int i;
7699
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 for (i = 0; i < Screen_mco; ++i)
7702 {
7703 ScreenLinesC[i][off] = u8cc[i];
7704 if (u8cc[i] == 0)
7705 break;
7706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708 if (mbyte_cells == 2)
7709 {
7710 ScreenLines[off + 1] = 0;
7711 ScreenAttrs[off + 1] = attr;
7712 }
7713 screen_char(off, row, col);
7714 }
7715 else if (mbyte_cells == 2)
7716 {
7717 ScreenLines[off + 1] = ptr[1];
7718 ScreenAttrs[off + 1] = attr;
7719 screen_char_2(off, row, col);
7720 }
7721 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7722 {
7723 ScreenLines2[off] = ptr[1];
7724 screen_char(off, row, col);
7725 }
7726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 screen_char(off, row, col);
7728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 if (has_mbyte)
7730 {
7731 off += mbyte_cells;
7732 col += mbyte_cells;
7733 ptr += mbyte_blen;
7734 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007735 {
7736 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007738 len = -1;
7739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 }
7741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
7743 ++off;
7744 ++col;
7745 ++ptr;
7746 }
7747 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007748
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007749 /* If we detected the next character needs to be redrawn, but the text
7750 * doesn't extend up to there, update the character here. */
7751 if (force_redraw_next && col < screen_Columns)
7752 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007753 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7754 screen_char_2(off, row, col);
7755 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007756 screen_char(off, row, col);
7757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758}
7759
7760#ifdef FEAT_SEARCH_EXTRA
7761/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007762 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 */
7764 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007765start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 if (p_hls && !no_hlsearch)
7768 {
7769 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007770 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007771# ifdef FEAT_RELTIME
7772 /* Set the time limit to 'redrawtime'. */
7773 profile_setlimit(p_rdt, &search_hl.tm);
7774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776}
7777
7778/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007779 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 */
7781 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007782end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 if (search_hl.rm.regprog != NULL)
7785 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007786 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 search_hl.rm.regprog = NULL;
7788 }
7789}
7790
7791/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007792 * Init for calling prepare_search_hl().
7793 */
7794 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007795init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007796{
7797 matchitem_T *cur;
7798
7799 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7800 * match */
7801 cur = wp->w_match_head;
7802 while (cur != NULL)
7803 {
7804 cur->hl.rm = cur->match;
7805 if (cur->hlg_id == 0)
7806 cur->hl.attr = 0;
7807 else
7808 cur->hl.attr = syn_id2attr(cur->hlg_id);
7809 cur->hl.buf = wp->w_buffer;
7810 cur->hl.lnum = 0;
7811 cur->hl.first_lnum = 0;
7812# ifdef FEAT_RELTIME
7813 /* Set the time limit to 'redrawtime'. */
7814 profile_setlimit(p_rdt, &(cur->hl.tm));
7815# endif
7816 cur = cur->next;
7817 }
7818 search_hl.buf = wp->w_buffer;
7819 search_hl.lnum = 0;
7820 search_hl.first_lnum = 0;
7821 /* time limit is set at the toplevel, for all windows */
7822}
7823
7824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 * Advance to the match in window "wp" line "lnum" or past it.
7826 */
7827 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007828prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007830 matchitem_T *cur; /* points to the match list */
7831 match_T *shl; /* points to search_hl or a match */
7832 int shl_flag; /* flag to indicate whether search_hl
7833 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007834 int pos_inprogress; /* marks that position match search is
7835 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 int n;
7837
7838 /*
7839 * When using a multi-line pattern, start searching at the top
7840 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007841 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007843 cur = wp->w_match_head;
7844 shl_flag = FALSE;
7845 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007847 if (shl_flag == FALSE)
7848 {
7849 shl = &search_hl;
7850 shl_flag = TRUE;
7851 }
7852 else
7853 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (shl->rm.regprog != NULL
7855 && shl->lnum == 0
7856 && re_multiline(shl->rm.regprog))
7857 {
7858 if (shl->first_lnum == 0)
7859 {
7860# ifdef FEAT_FOLDING
7861 for (shl->first_lnum = lnum;
7862 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7863 if (hasFoldingWin(wp, shl->first_lnum - 1,
7864 NULL, NULL, TRUE, NULL))
7865 break;
7866# else
7867 shl->first_lnum = wp->w_topline;
7868# endif
7869 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007870 if (cur != NULL)
7871 cur->pos.cur = 0;
7872 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007874 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7875 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007877 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7878 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007879 pos_inprogress = cur == NULL || cur->pos.cur == 0
7880 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (shl->lnum != 0)
7882 {
7883 shl->first_lnum = shl->lnum
7884 + shl->rm.endpos[0].lnum
7885 - shl->rm.startpos[0].lnum;
7886 n = shl->rm.endpos[0].col;
7887 }
7888 else
7889 {
7890 ++shl->first_lnum;
7891 n = 0;
7892 }
7893 }
7894 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007895 if (shl != &search_hl && cur != NULL)
7896 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 }
7898}
7899
7900/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007901 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 * Uses shl->buf.
7903 * Sets shl->lnum and shl->rm contents.
7904 * Note: Assumes a previous match is always before "lnum", unless
7905 * shl->lnum is zero.
7906 * Careful: Any pointers for buffer lines will become invalid.
7907 */
7908 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007909next_search_hl(
7910 win_T *win,
7911 match_T *shl, /* points to search_hl or a match */
7912 linenr_T lnum,
7913 colnr_T mincol, /* minimal column for a match */
7914 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 linenr_T l;
7917 colnr_T matchcol;
7918 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007919 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007921 // for :{range}s/pat only highlight inside the range
7922 if (lnum < search_first_line || lnum > search_last_line)
7923 {
7924 shl->lnum = 0;
7925 return;
7926 }
7927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (shl->lnum != 0)
7929 {
7930 /* Check for three situations:
7931 * 1. If the "lnum" is below a previous match, start a new search.
7932 * 2. If the previous match includes "mincol", use it.
7933 * 3. Continue after the previous match.
7934 */
7935 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7936 if (lnum > l)
7937 shl->lnum = 0;
7938 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7939 return;
7940 }
7941
7942 /*
7943 * Repeat searching for a match until one is found that includes "mincol"
7944 * or none is found in this line.
7945 */
7946 called_emsg = FALSE;
7947 for (;;)
7948 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007949#ifdef FEAT_RELTIME
7950 /* Stop searching after passing the time limit. */
7951 if (profile_passed_limit(&(shl->tm)))
7952 {
7953 shl->lnum = 0; /* no match found in time */
7954 break;
7955 }
7956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 /* Three situations:
7958 * 1. No useful previous match: search from start of line.
7959 * 2. Not Vi compatible or empty match: continue at next character.
7960 * Break the loop if this is beyond the end of the line.
7961 * 3. Vi compatible searching: continue at end of previous match.
7962 */
7963 if (shl->lnum == 0)
7964 matchcol = 0;
7965 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7966 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007967 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007969 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007970
7971 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007972 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007973 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007975 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 shl->lnum = 0;
7977 break;
7978 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007979 if (has_mbyte)
7980 matchcol += mb_ptr2len(ml);
7981 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984 else
7985 matchcol = shl->rm.endpos[0].col;
7986
7987 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007988 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007990 /* Remember whether shl->rm is using a copy of the regprog in
7991 * cur->match. */
7992 int regprog_is_copy = (shl != &search_hl && cur != NULL
7993 && shl == &cur->hl
7994 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007995 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007996
Bram Moolenaarb3414592014-06-17 17:48:32 +02007997 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
7998 matchcol,
7999#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008000 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008001#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008002 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003#endif
8004 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008005 /* Copy the regprog, in case it got freed and recompiled. */
8006 if (regprog_is_copy)
8007 cur->match.regprog = cur->hl.rm.regprog;
8008
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008009 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008010 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011 /* Error while handling regexp: stop using this regexp. */
8012 if (shl == &search_hl)
8013 {
8014 /* don't free regprog in the match list, it's a copy */
8015 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008016 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008017 }
8018 shl->rm.regprog = NULL;
8019 shl->lnum = 0;
8020 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8021 message */
8022 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008023 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008024 }
8025 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008027 else
8028 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 if (nmatched == 0)
8030 {
8031 shl->lnum = 0; /* no match found */
8032 break;
8033 }
8034 if (shl->rm.startpos[0].lnum > 0
8035 || shl->rm.startpos[0].col >= mincol
8036 || nmatched > 1
8037 || shl->rm.endpos[0].col > mincol)
8038 {
8039 shl->lnum += shl->rm.startpos[0].lnum;
8040 break; /* useful match found */
8041 }
8042 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008043
8044 // Restore called_emsg for assert_fails().
8045 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
Bram Moolenaar85077472016-10-16 14:35:48 +02008048/*
8049 * If there is a match fill "shl" and return one.
8050 * Return zero otherwise.
8051 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008052 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008053next_search_hl_pos(
8054 match_T *shl, /* points to a match */
8055 linenr_T lnum,
8056 posmatch_T *posmatch, /* match positions */
8057 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058{
8059 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008060 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008061
Bram Moolenaarb3414592014-06-17 17:48:32 +02008062 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8063 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008064 llpos_T *pos = &posmatch->pos[i];
8065
8066 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008067 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008068 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008069 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008070 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008071 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008072 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008073 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008074 /* if this match comes before the one at "found" then swap
8075 * them */
8076 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008078 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008079
Bram Moolenaar85077472016-10-16 14:35:48 +02008080 *pos = posmatch->pos[found];
8081 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082 }
8083 }
8084 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008085 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 }
8087 }
8088 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008089 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008090 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008091 colnr_T start = posmatch->pos[found].col == 0
8092 ? 0 : posmatch->pos[found].col - 1;
8093 colnr_T end = posmatch->pos[found].col == 0
8094 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008095
Bram Moolenaar85077472016-10-16 14:35:48 +02008096 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097 shl->rm.startpos[0].lnum = 0;
8098 shl->rm.startpos[0].col = start;
8099 shl->rm.endpos[0].lnum = 0;
8100 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008101 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008102 posmatch->cur = found + 1;
8103 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008104 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008105 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008106}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008107#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008108
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008110screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111{
8112 attrentry_T *aep = NULL;
8113
8114 screen_attr = attr;
8115 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008116#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 && termcap_active
8118#endif
8119 )
8120 {
8121#ifdef FEAT_GUI
8122 if (gui.in_use)
8123 {
8124 char buf[20];
8125
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008126 /* The GUI handles this internally. */
8127 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 OUT_STR(buf);
8129 }
8130 else
8131#endif
8132 {
8133 if (attr > HL_ALL) /* special HL attr. */
8134 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008135 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 aep = syn_cterm_attr2entry(attr);
8137 else
8138 aep = syn_term_attr2entry(attr);
8139 if (aep == NULL) /* did ":syntax clear" */
8140 attr = 0;
8141 else
8142 attr = aep->ae_attr;
8143 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008144 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008146 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008147#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008148 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8149 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8150 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008151#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008152 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008153 /* If the Normal FG color has BOLD attribute and the new HL
8154 * has a FG color defined, clear BOLD. */
8155 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008156 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008158 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008159 out_str(T_UCS);
8160 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008161 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8162 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008164 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008166 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008168 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008169 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170
8171 /*
8172 * Output the color or start string after bold etc., in case the
8173 * bold etc. override the color setting.
8174 */
8175 if (aep != NULL)
8176 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008177#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008178 /* When 'termguicolors' is set but fg or bg is unset,
8179 * fall back to the cterm colors. This helps for SpellBad,
8180 * where the GUI uses a red undercurl. */
8181 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008183 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008184 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008185 }
8186 else
8187#endif
8188 if (t_colors > 1)
8189 {
8190 if (aep->ae_u.cterm.fg_color)
8191 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8192 }
8193#ifdef FEAT_TERMGUICOLORS
8194 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8195 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008196 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008197 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 }
8199 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008200#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008201 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008203 if (aep->ae_u.cterm.bg_color)
8204 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8205 }
8206
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008207 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008208 {
8209 if (aep->ae_u.term.start != NULL)
8210 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 }
8212 }
8213 }
8214 }
8215}
8216
8217 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008218screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 int do_ME = FALSE; /* output T_ME code */
8221
8222 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008223#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 && termcap_active
8225#endif
8226 )
8227 {
8228#ifdef FEAT_GUI
8229 if (gui.in_use)
8230 {
8231 char buf[20];
8232
8233 /* use internal GUI code */
8234 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8235 OUT_STR(buf);
8236 }
8237 else
8238#endif
8239 {
8240 if (screen_attr > HL_ALL) /* special HL attr. */
8241 {
8242 attrentry_T *aep;
8243
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008244 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
8246 /*
8247 * Assume that t_me restores the original colors!
8248 */
8249 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008250 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008251#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008252 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8253 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8254 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008255#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008256 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008257#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008258 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8259 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8260 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008261#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008262 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 do_ME = TRUE;
8264 }
8265 else
8266 {
8267 aep = syn_term_attr2entry(screen_attr);
8268 if (aep != NULL && aep->ae_u.term.stop != NULL)
8269 {
8270 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8271 do_ME = TRUE;
8272 else
8273 out_str(aep->ae_u.term.stop);
8274 }
8275 }
8276 if (aep == NULL) /* did ":syntax clear" */
8277 screen_attr = 0;
8278 else
8279 screen_attr = aep->ae_attr;
8280 }
8281
8282 /*
8283 * Often all ending-codes are equal to T_ME. Avoid outputting the
8284 * same sequence several times.
8285 */
8286 if (screen_attr & HL_STANDOUT)
8287 {
8288 if (STRCMP(T_SE, T_ME) == 0)
8289 do_ME = TRUE;
8290 else
8291 out_str(T_SE);
8292 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008293 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008294 {
8295 if (STRCMP(T_UCE, T_ME) == 0)
8296 do_ME = TRUE;
8297 else
8298 out_str(T_UCE);
8299 }
8300 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008301 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
8303 if (STRCMP(T_UE, T_ME) == 0)
8304 do_ME = TRUE;
8305 else
8306 out_str(T_UE);
8307 }
8308 if (screen_attr & HL_ITALIC)
8309 {
8310 if (STRCMP(T_CZR, T_ME) == 0)
8311 do_ME = TRUE;
8312 else
8313 out_str(T_CZR);
8314 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008315 if (screen_attr & HL_STRIKETHROUGH)
8316 {
8317 if (STRCMP(T_STE, T_ME) == 0)
8318 do_ME = TRUE;
8319 else
8320 out_str(T_STE);
8321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8323 out_str(T_ME);
8324
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008325#ifdef FEAT_TERMGUICOLORS
8326 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008328 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008329 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008330 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008331 term_bg_rgb_color(cterm_normal_bg_gui_color);
8332 }
8333 else
8334#endif
8335 {
8336 if (t_colors > 1)
8337 {
8338 /* set Normal cterm colors */
8339 if (cterm_normal_fg_color != 0)
8340 term_fg_color(cterm_normal_fg_color - 1);
8341 if (cterm_normal_bg_color != 0)
8342 term_bg_color(cterm_normal_bg_color - 1);
8343 if (cterm_normal_fg_bold)
8344 out_str(T_MD);
8345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 }
8347 }
8348 }
8349 screen_attr = 0;
8350}
8351
8352/*
8353 * Reset the colors for a cterm. Used when leaving Vim.
8354 * The machine specific code may override this again.
8355 */
8356 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008357reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008359 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {
8361 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008362#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008363 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8364 || cterm_normal_bg_gui_color != INVALCOLOR)
8365 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {
8370 out_str(T_OP);
8371 screen_attr = -1;
8372 }
8373 if (cterm_normal_fg_bold)
8374 {
8375 out_str(T_ME);
8376 screen_attr = -1;
8377 }
8378 }
8379}
8380
8381/*
8382 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8383 * using the attributes from ScreenAttrs["off"].
8384 */
8385 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008386screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
8388 int attr;
8389
8390 /* Check for illegal values, just in case (could happen just after
8391 * resizing). */
8392 if (row >= screen_Rows || col >= screen_Columns)
8393 return;
8394
Bram Moolenaarae654382019-01-17 21:09:05 +01008395#ifdef FEAT_INS_EXPAND
8396 if (pum_under_menu(row, col))
8397 return;
8398#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008399 /* Outputting a character in the last cell on the screen may scroll the
8400 * screen up. Only do it when the "xn" termcap property is set, otherwise
8401 * mark the character invalid (update it when scrolled up). */
8402 if (*T_XN == NUL
8403 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#ifdef FEAT_RIGHTLEFT
8405 /* account for first command-line character in rightleft mode */
8406 && !cmdmsg_rl
8407#endif
8408 )
8409 {
8410 ScreenAttrs[off] = (sattr_T)-1;
8411 return;
8412 }
8413
8414 /*
8415 * Stop highlighting first, so it's easier to move the cursor.
8416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 if (screen_char_attr != 0)
8418 attr = screen_char_attr;
8419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 attr = ScreenAttrs[off];
8421 if (screen_attr != attr)
8422 screen_stop_highlight();
8423
8424 windgoto(row, col);
8425
8426 if (screen_attr != attr)
8427 screen_start_highlight(attr);
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (enc_utf8 && ScreenLinesUC[off] != 0)
8430 {
8431 char_u buf[MB_MAXBYTES + 1];
8432
Bram Moolenaarcb070082016-04-02 22:14:51 +02008433 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008434 {
8435 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008436#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008437 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008438#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008439 )
8440 {
8441 /* Clear the two screen cells. If the character is actually
8442 * single width it won't change the second cell. */
8443 out_str((char_u *)" ");
8444 term_windgoto(row, col);
8445 }
8446 /* not sure where the cursor is after drawing the ambiguous width
8447 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008448 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008449 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008450 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008452
8453 /* Convert the UTF-8 character to bytes and write it. */
8454 buf[utfc_char2bytes(off, buf)] = NUL;
8455 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 }
8457 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 /* double-byte character in single-width cell */
8462 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8463 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 }
8465
8466 screen_cur_col++;
8467}
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469/*
8470 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8471 * on the screen at position 'row' and 'col'.
8472 * The attributes of the first byte is used for all. This is required to
8473 * output the two bytes of a double-byte character with nothing in between.
8474 */
8475 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008476screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 /* Check for illegal values (could be wrong when screen was resized). */
8479 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8480 return;
8481
8482 /* Outputting the last character on the screen may scrollup the screen.
8483 * Don't to it! Mark the character invalid (update it when scrolled up) */
8484 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8485 {
8486 ScreenAttrs[off] = (sattr_T)-1;
8487 return;
8488 }
8489
8490 /* Output the first byte normally (positions the cursor), then write the
8491 * second byte directly. */
8492 screen_char(off, row, col);
8493 out_char(ScreenLines[off + 1]);
8494 ++screen_cur_col;
8495}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497/*
8498 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8499 * This uses the contents of ScreenLines[] and doesn't change it.
8500 */
8501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008502screen_draw_rectangle(
8503 int row,
8504 int col,
8505 int height,
8506 int width,
8507 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 int r, c;
8510 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008511 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008513 /* Can't use ScreenLines unless initialized */
8514 if (ScreenLines == NULL)
8515 return;
8516
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 if (invert)
8518 screen_char_attr = HL_INVERSE;
8519 for (r = row; r < row + height; ++r)
8520 {
8521 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008522 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 for (c = col; c < col + width; ++c)
8524 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008525 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {
8527 screen_char_2(off + c, r, c);
8528 ++c;
8529 }
8530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {
8532 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008533 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536 }
8537 }
8538 screen_char_attr = 0;
8539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541/*
8542 * Redraw the characters for a vertically split window.
8543 */
8544 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008545redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 int col;
8548 int width;
8549
8550# ifdef FEAT_CLIPBOARD
8551 clip_may_clear_selection(row, end - 1);
8552# endif
8553
8554 if (wp == NULL)
8555 {
8556 col = 0;
8557 width = Columns;
8558 }
8559 else
8560 {
8561 col = wp->w_wincol;
8562 width = wp->w_width;
8563 }
8564 screen_draw_rectangle(row, col, end - row, width, FALSE);
8565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008567 static void
8568space_to_screenline(int off, int attr)
8569{
8570 ScreenLines[off] = ' ';
8571 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008572 if (enc_utf8)
8573 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008574}
8575
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576/*
8577 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8578 * with character 'c1' in first column followed by 'c2' in the other columns.
8579 * Use attributes 'attr'.
8580 */
8581 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008582screen_fill(
8583 int start_row,
8584 int end_row,
8585 int start_col,
8586 int end_col,
8587 int c1,
8588 int c2,
8589 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 int row;
8592 int col;
8593 int off;
8594 int end_off;
8595 int did_delete;
8596 int c;
8597 int norm_term;
8598#if defined(FEAT_GUI) || defined(UNIX)
8599 int force_next = FALSE;
8600#endif
8601
8602 if (end_row > screen_Rows) /* safety check */
8603 end_row = screen_Rows;
8604 if (end_col > screen_Columns) /* safety check */
8605 end_col = screen_Columns;
8606 if (ScreenLines == NULL
8607 || start_row >= end_row
8608 || start_col >= end_col) /* nothing to do */
8609 return;
8610
8611 /* it's a "normal" terminal when not in a GUI or cterm */
8612 norm_term = (
8613#ifdef FEAT_GUI
8614 !gui.in_use &&
8615#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008616 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 for (row = start_row; row < end_row; ++row)
8618 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008619 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008620#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008621 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008622#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008623 )
8624 {
8625 /* When drawing over the right halve of a double-wide char clear
8626 * out the left halve. When drawing over the left halve of a
8627 * double wide-char clear out the right halve. Only needed in a
8628 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008629 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008630 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008631 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008632 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 /*
8635 * Try to use delete-line termcap code, when no attributes or in a
8636 * "normal" terminal, where a bold/italic space is just a
8637 * space.
8638 */
8639 did_delete = FALSE;
8640 if (c2 == ' '
8641 && end_col == Columns
8642 && can_clear(T_CE)
8643 && (attr == 0
8644 || (norm_term
8645 && attr <= HL_ALL
8646 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8647 {
8648 /*
8649 * check if we really need to clear something
8650 */
8651 col = start_col;
8652 if (c1 != ' ') /* don't clear first char */
8653 ++col;
8654
8655 off = LineOffset[row] + col;
8656 end_off = LineOffset[row] + end_col;
8657
8658 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 if (enc_utf8)
8660 while (off < end_off && ScreenLines[off] == ' '
8661 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8662 ++off;
8663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 while (off < end_off && ScreenLines[off] == ' '
8665 && ScreenAttrs[off] == 0)
8666 ++off;
8667 if (off < end_off) /* something to be cleared */
8668 {
8669 col = off - LineOffset[row];
8670 screen_stop_highlight();
8671 term_windgoto(row, col);/* clear rest of this screen line */
8672 out_str(T_CE);
8673 screen_start(); /* don't know where cursor is now */
8674 col = end_col - col;
8675 while (col--) /* clear chars in ScreenLines */
8676 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008677 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 ++off;
8679 }
8680 }
8681 did_delete = TRUE; /* the chars are cleared now */
8682 }
8683
8684 off = LineOffset[row] + start_col;
8685 c = c1;
8686 for (col = start_col; col < end_col; ++col)
8687 {
8688 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008689 || (enc_utf8 && (int)ScreenLinesUC[off]
8690 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 || ScreenAttrs[off] != attr
8692#if defined(FEAT_GUI) || defined(UNIX)
8693 || force_next
8694#endif
8695 )
8696 {
8697#if defined(FEAT_GUI) || defined(UNIX)
8698 /* The bold trick may make a single row of pixels appear in
8699 * the next character. When a bold character is removed, the
8700 * next character should be redrawn too. This happens for our
8701 * own GUI and for some xterms. */
8702 if (
8703# ifdef FEAT_GUI
8704 gui.in_use
8705# endif
8706# if defined(FEAT_GUI) && defined(UNIX)
8707 ||
8708# endif
8709# ifdef UNIX
8710 term_is_xterm
8711# endif
8712 )
8713 {
8714 if (ScreenLines[off] != ' '
8715 && (ScreenAttrs[off] > HL_ALL
8716 || ScreenAttrs[off] & HL_BOLD))
8717 force_next = TRUE;
8718 else
8719 force_next = FALSE;
8720 }
8721#endif
8722 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 if (enc_utf8)
8724 {
8725 if (c >= 0x80)
8726 {
8727 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008728 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730 else
8731 ScreenLinesUC[off] = 0;
8732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 ScreenAttrs[off] = attr;
8734 if (!did_delete || c != ' ')
8735 screen_char(off, row, col);
8736 }
8737 ++off;
8738 if (col == start_col)
8739 {
8740 if (did_delete)
8741 break;
8742 c = c2;
8743 }
8744 }
8745 if (end_col == Columns)
8746 LineWraps[row] = FALSE;
8747 if (row == Rows - 1) /* overwritten the command line */
8748 {
8749 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008750 if (start_col == 0 && end_col == Columns
8751 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008753 if (start_col == 0)
8754 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 }
8756 }
8757}
8758
8759/*
8760 * Check if there should be a delay. Used before clearing or redrawing the
8761 * screen or the command line.
8762 */
8763 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008764check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8767 && !did_wait_return
8768 && emsg_silent == 0)
8769 {
8770 out_flush();
8771 ui_delay(1000L, TRUE);
8772 emsg_on_display = FALSE;
8773 if (check_msg_scroll)
8774 msg_scroll = FALSE;
8775 }
8776}
8777
8778/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008779 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8780 */
8781 static void
8782clear_TabPageIdxs(void)
8783{
8784 int scol;
8785
8786 for (scol = 0; scol < Columns; ++scol)
8787 TabPageIdxs[scol] = 0;
8788}
8789
8790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008792 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 * Returns TRUE if there is a valid screen to write to.
8794 * Returns FALSE when starting up and screen not initialized yet.
8795 */
8796 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008797screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008799 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 return (ScreenLines != NULL);
8801}
8802
8803/*
8804 * Resize the shell to Rows and Columns.
8805 * Allocate ScreenLines[] and associated items.
8806 *
8807 * There may be some time between setting Rows and Columns and (re)allocating
8808 * ScreenLines[]. This happens when starting up and when (manually) changing
8809 * the shell size. Always use screen_Rows and screen_Columns to access items
8810 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8811 * final size of the shell is needed.
8812 */
8813 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008814screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 int new_row, old_row;
8817#ifdef FEAT_GUI
8818 int old_Rows;
8819#endif
8820 win_T *wp;
8821 int outofmem = FALSE;
8822 int len;
8823 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008825 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008827 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 sattr_T *new_ScreenAttrs;
8829 unsigned *new_LineOffset;
8830 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008831 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008832 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008834 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008835 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008837retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 /*
8839 * Allocation of the screen buffers is done only when the size changes and
8840 * when Rows and Columns have been set and we have started doing full
8841 * screen stuff.
8842 */
8843 if ((ScreenLines != NULL
8844 && Rows == screen_Rows
8845 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 && enc_utf8 == (ScreenLinesUC != NULL)
8847 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008848 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 || Rows == 0
8850 || Columns == 0
8851 || (!full_screen && ScreenLines == NULL))
8852 return;
8853
8854 /*
8855 * It's possible that we produce an out-of-memory message below, which
8856 * will cause this function to be called again. To break the loop, just
8857 * return here.
8858 */
8859 if (entered)
8860 return;
8861 entered = TRUE;
8862
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008863 /*
8864 * Note that the window sizes are updated before reallocating the arrays,
8865 * thus we must not redraw here!
8866 */
8867 ++RedrawingDisabled;
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 win_new_shellsize(); /* fit the windows in the new sized shell */
8870
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 comp_col(); /* recompute columns for shown command and ruler */
8872
8873 /*
8874 * We're changing the size of the screen.
8875 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8876 * - Move lines from the old arrays into the new arrays, clear extra
8877 * lines (unless the screen is going to be cleared).
8878 * - Free the old arrays.
8879 *
8880 * If anything fails, make ScreenLines NULL, so we don't do anything!
8881 * Continuing with the old ScreenLines may result in a crash, because the
8882 * size is wrong.
8883 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008884 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008886 if (aucmd_win != NULL)
8887 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008889 new_ScreenLines = (schar_T *)lalloc(
8890 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008891 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 if (enc_utf8)
8893 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008894 new_ScreenLinesUC = (u8char_T *)lalloc(
8895 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008896 for (i = 0; i < p_mco; ++i)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008897 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear(
8898 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008901 new_ScreenLines2 = (schar_T *)lalloc(
8902 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
8903 new_ScreenAttrs = (sattr_T *)lalloc(
8904 (Rows + 1) * Columns * sizeof(sattr_T), FALSE);
8905 new_LineOffset = (unsigned *)lalloc(Rows * sizeof(unsigned), FALSE);
8906 new_LineWraps = (char_u *)lalloc(Rows * sizeof(char_u), FALSE);
8907 new_TabPageIdxs = (short *)lalloc(Columns * sizeof(short), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008909 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
8911 if (win_alloc_lines(wp) == FAIL)
8912 {
8913 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008914 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 }
8916 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008917 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8918 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008919 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008920give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008922 for (i = 0; i < p_mco; ++i)
8923 if (new_ScreenLinesC[i] == NULL)
8924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008926 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 || new_ScreenAttrs == NULL
8929 || new_LineOffset == NULL
8930 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008931 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 || outofmem)
8933 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008934 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008935 {
8936 /* guess the size */
8937 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8938
8939 /* Remember we did this to avoid getting outofmem messages over
8940 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008941 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008942 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008943 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008944 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008945 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(new_ScreenLinesC[i]);
8947 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008948 VIM_CLEAR(new_ScreenAttrs);
8949 VIM_CLEAR(new_LineOffset);
8950 VIM_CLEAR(new_LineWraps);
8951 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953 else
8954 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008955 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008956
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 for (new_row = 0; new_row < Rows; ++new_row)
8958 {
8959 new_LineOffset[new_row] = new_row * Columns;
8960 new_LineWraps[new_row] = FALSE;
8961
8962 /*
8963 * If the screen is not going to be cleared, copy as much as
8964 * possible from the old screen to the new one and clear the rest
8965 * (used when resizing the window at the "--more--" prompt or when
8966 * executing an external command, for the GUI).
8967 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008968 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 {
8970 (void)vim_memset(new_ScreenLines + new_row * Columns,
8971 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 if (enc_utf8)
8973 {
8974 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8975 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008976 for (i = 0; i < p_mco; ++i)
8977 (void)vim_memset(new_ScreenLinesC[i]
8978 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 0, (size_t)Columns * sizeof(u8char_T));
8980 }
8981 if (enc_dbcs == DBCS_JPNU)
8982 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8983 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8985 0, (size_t)Columns * sizeof(sattr_T));
8986 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008987 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 {
8989 if (screen_Columns < Columns)
8990 len = screen_Columns;
8991 else
8992 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008993 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008994 * may be invalid now. Also when p_mco changes. */
8995 if (!(enc_utf8 && ScreenLinesUC == NULL)
8996 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008997 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8998 ScreenLines + LineOffset[old_row],
8999 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009000 if (enc_utf8 && ScreenLinesUC != NULL
9001 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 {
9003 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9004 ScreenLinesUC + LineOffset[old_row],
9005 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009006 for (i = 0; i < p_mco; ++i)
9007 mch_memmove(new_ScreenLinesC[i]
9008 + new_LineOffset[new_row],
9009 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 (size_t)len * sizeof(u8char_T));
9011 }
9012 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9013 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9014 ScreenLines2 + LineOffset[old_row],
9015 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9017 ScreenAttrs + LineOffset[old_row],
9018 (size_t)len * sizeof(sattr_T));
9019 }
9020 }
9021 }
9022 /* Use the last line of the screen for the current line. */
9023 current_ScreenLine = new_ScreenLines + Rows * Columns;
9024 }
9025
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009026 free_screenlines();
9027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009030 for (i = 0; i < p_mco; ++i)
9031 ScreenLinesC[i] = new_ScreenLinesC[i];
9032 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 ScreenAttrs = new_ScreenAttrs;
9035 LineOffset = new_LineOffset;
9036 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009037 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038
9039 /* It's important that screen_Rows and screen_Columns reflect the actual
9040 * size of ScreenLines[]. Set them before calling anything. */
9041#ifdef FEAT_GUI
9042 old_Rows = screen_Rows;
9043#endif
9044 screen_Rows = Rows;
9045 screen_Columns = Columns;
9046
9047 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009048 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050#ifdef FEAT_GUI
9051 else if (gui.in_use
9052 && !gui.starting
9053 && ScreenLines != NULL
9054 && old_Rows != Rows)
9055 {
9056 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9057 /*
9058 * Adjust the position of the cursor, for when executing an external
9059 * command.
9060 */
9061 if (msg_row >= Rows) /* Rows got smaller */
9062 msg_row = Rows - 1; /* put cursor at last row */
9063 else if (Rows > old_Rows) /* Rows got bigger */
9064 msg_row += Rows - old_Rows; /* put cursor in same place */
9065 if (msg_col >= Columns) /* Columns got smaller */
9066 msg_col = Columns - 1; /* put cursor at last column */
9067 }
9068#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009069 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009072 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009073
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009074 /*
9075 * Do not apply autocommands more than 3 times to avoid an endless loop
9076 * in case applying autocommands always changes Rows or Columns.
9077 */
9078 if (starting == 0 && ++retry_count <= 3)
9079 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009080 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009081 /* In rare cases, autocommands may have altered Rows or Columns,
9082 * jump back to check if we need to allocate the screen again. */
9083 goto retry;
9084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009088free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009089{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009090 int i;
9091
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009092 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009093 for (i = 0; i < Screen_mco; ++i)
9094 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009095 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009096 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009097 vim_free(ScreenAttrs);
9098 vim_free(LineOffset);
9099 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009100 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009101}
9102
9103 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009104screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
9106 check_for_delay(FALSE);
9107 screenalloc(FALSE); /* allocate screen buffers if size changed */
9108 screenclear2(); /* clear the screen */
9109}
9110
9111 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009112screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 int i;
9115
9116 if (starting == NO_SCREEN || ScreenLines == NULL
9117#ifdef FEAT_GUI
9118 || (gui.in_use && gui.starting)
9119#endif
9120 )
9121 return;
9122
9123#ifdef FEAT_GUI
9124 if (!gui.in_use)
9125#endif
9126 screen_attr = -1; /* force setting the Normal colors */
9127 screen_stop_highlight(); /* don't want highlighting here */
9128
9129#ifdef FEAT_CLIPBOARD
9130 /* disable selection without redrawing it */
9131 clip_scroll_selection(9999);
9132#endif
9133
9134 /* blank out ScreenLines */
9135 for (i = 0; i < Rows; ++i)
9136 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009137 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 LineWraps[i] = FALSE;
9139 }
9140
9141 if (can_clear(T_CL))
9142 {
9143 out_str(T_CL); /* clear the display */
9144 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009145 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 }
9147 else
9148 {
9149 /* can't clear the screen, mark all chars with invalid attributes */
9150 for (i = 0; i < Rows; ++i)
9151 lineinvalid(LineOffset[i], (int)Columns);
9152 clear_cmdline = TRUE;
9153 }
9154
9155 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9156
9157 win_rest_invalid(firstwin);
9158 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009159 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 if (must_redraw == CLEAR) /* no need to clear again */
9161 must_redraw = NOT_VALID;
9162 compute_cmdrow();
9163 msg_row = cmdline_row; /* put cursor on last line for messages */
9164 msg_col = 0;
9165 screen_start(); /* don't know where cursor is now */
9166 msg_scrolled = 0; /* can't scroll back */
9167 msg_didany = FALSE;
9168 msg_didout = FALSE;
9169}
9170
9171/*
9172 * Clear one line in ScreenLines.
9173 */
9174 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009175lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 if (enc_utf8)
9179 (void)vim_memset(ScreenLinesUC + off, 0,
9180 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009181 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182}
9183
9184/*
9185 * Mark one line in ScreenLines invalid by setting the attributes to an
9186 * invalid value.
9187 */
9188 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009189lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190{
9191 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9192}
9193
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194/*
9195 * Copy part of a Screenline for vertically split window "wp".
9196 */
9197 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009198linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199{
9200 unsigned off_to = LineOffset[to] + wp->w_wincol;
9201 unsigned off_from = LineOffset[from] + wp->w_wincol;
9202
9203 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9204 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 if (enc_utf8)
9206 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009207 int i;
9208
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9210 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009211 for (i = 0; i < p_mco; ++i)
9212 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9213 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
9215 if (enc_dbcs == DBCS_JPNU)
9216 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9217 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9219 wp->w_width * sizeof(sattr_T));
9220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221
9222/*
9223 * Return TRUE if clearing with term string "p" would work.
9224 * It can't work when the string is empty or it won't set the right background.
9225 */
9226 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009227can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228{
9229 return (*p != NUL && (t_colors <= 1
9230#ifdef FEAT_GUI
9231 || gui.in_use
9232#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009233#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009234 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009235 || (!p_tgc && cterm_normal_bg_color == 0)
9236#else
9237 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009238#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009239 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240}
9241
9242/*
9243 * Reset cursor position. Use whenever cursor was moved because of outputting
9244 * something directly to the screen (shell commands) or a terminal control
9245 * code.
9246 */
9247 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009248screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
9250 screen_cur_row = screen_cur_col = 9999;
9251}
9252
9253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 * Move the cursor to position "row","col" in the screen.
9255 * This tries to find the most efficient way to move, minimizing the number of
9256 * characters sent to the terminal.
9257 */
9258 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009259windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009261 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 int i;
9263 int plan;
9264 int cost;
9265 int wouldbe_col;
9266 int noinvcurs;
9267 char_u *bs;
9268 int goto_cost;
9269 int attr;
9270
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009271#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9273
9274#define PLAN_LE 1
9275#define PLAN_CR 2
9276#define PLAN_NL 3
9277#define PLAN_WRITE 4
9278 /* Can't use ScreenLines unless initialized */
9279 if (ScreenLines == NULL)
9280 return;
9281
9282 if (col != screen_cur_col || row != screen_cur_row)
9283 {
9284 /* Check for valid position. */
9285 if (row < 0) /* window without text lines? */
9286 row = 0;
9287 if (row >= screen_Rows)
9288 row = screen_Rows - 1;
9289 if (col >= screen_Columns)
9290 col = screen_Columns - 1;
9291
9292 /* check if no cursor movement is allowed in highlight mode */
9293 if (screen_attr && *T_MS == NUL)
9294 noinvcurs = HIGHL_COST;
9295 else
9296 noinvcurs = 0;
9297 goto_cost = GOTO_COST + noinvcurs;
9298
9299 /*
9300 * Plan how to do the positioning:
9301 * 1. Use CR to move it to column 0, same row.
9302 * 2. Use T_LE to move it a few columns to the left.
9303 * 3. Use NL to move a few lines down, column 0.
9304 * 4. Move a few columns to the right with T_ND or by writing chars.
9305 *
9306 * Don't do this if the cursor went beyond the last column, the cursor
9307 * position is unknown then (some terminals wrap, some don't )
9308 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009309 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 * characters to move the cursor to the right.
9311 */
9312 if (row >= screen_cur_row && screen_cur_col < Columns)
9313 {
9314 /*
9315 * If the cursor is in the same row, bigger col, we can use CR
9316 * or T_LE.
9317 */
9318 bs = NULL; /* init for GCC */
9319 attr = screen_attr;
9320 if (row == screen_cur_row && col < screen_cur_col)
9321 {
9322 /* "le" is preferred over "bc", because "bc" is obsolete */
9323 if (*T_LE)
9324 bs = T_LE; /* "cursor left" */
9325 else
9326 bs = T_BC; /* "backspace character (old) */
9327 if (*bs)
9328 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9329 else
9330 cost = 999;
9331 if (col + 1 < cost) /* using CR is less characters */
9332 {
9333 plan = PLAN_CR;
9334 wouldbe_col = 0;
9335 cost = 1; /* CR is just one character */
9336 }
9337 else
9338 {
9339 plan = PLAN_LE;
9340 wouldbe_col = col;
9341 }
9342 if (noinvcurs) /* will stop highlighting */
9343 {
9344 cost += noinvcurs;
9345 attr = 0;
9346 }
9347 }
9348
9349 /*
9350 * If the cursor is above where we want to be, we can use CR LF.
9351 */
9352 else if (row > screen_cur_row)
9353 {
9354 plan = PLAN_NL;
9355 wouldbe_col = 0;
9356 cost = (row - screen_cur_row) * 2; /* CR LF */
9357 if (noinvcurs) /* will stop highlighting */
9358 {
9359 cost += noinvcurs;
9360 attr = 0;
9361 }
9362 }
9363
9364 /*
9365 * If the cursor is in the same row, smaller col, just use write.
9366 */
9367 else
9368 {
9369 plan = PLAN_WRITE;
9370 wouldbe_col = screen_cur_col;
9371 cost = 0;
9372 }
9373
9374 /*
9375 * Check if any characters that need to be written have the
9376 * correct attributes. Also avoid UTF-8 characters.
9377 */
9378 i = col - wouldbe_col;
9379 if (i > 0)
9380 cost += i;
9381 if (cost < goto_cost && i > 0)
9382 {
9383 /*
9384 * Check if the attributes are correct without additionally
9385 * stopping highlighting.
9386 */
9387 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9388 while (i && *p++ == attr)
9389 --i;
9390 if (i != 0)
9391 {
9392 /*
9393 * Try if it works when highlighting is stopped here.
9394 */
9395 if (*--p == 0)
9396 {
9397 cost += noinvcurs;
9398 while (i && *p++ == 0)
9399 --i;
9400 }
9401 if (i != 0)
9402 cost = 999; /* different attributes, don't do it */
9403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 if (enc_utf8)
9405 {
9406 /* Don't use an UTF-8 char for positioning, it's slow. */
9407 for (i = wouldbe_col; i < col; ++i)
9408 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9409 {
9410 cost = 999;
9411 break;
9412 }
9413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 }
9415
9416 /*
9417 * We can do it without term_windgoto()!
9418 */
9419 if (cost < goto_cost)
9420 {
9421 if (plan == PLAN_LE)
9422 {
9423 if (noinvcurs)
9424 screen_stop_highlight();
9425 while (screen_cur_col > col)
9426 {
9427 out_str(bs);
9428 --screen_cur_col;
9429 }
9430 }
9431 else if (plan == PLAN_CR)
9432 {
9433 if (noinvcurs)
9434 screen_stop_highlight();
9435 out_char('\r');
9436 screen_cur_col = 0;
9437 }
9438 else if (plan == PLAN_NL)
9439 {
9440 if (noinvcurs)
9441 screen_stop_highlight();
9442 while (screen_cur_row < row)
9443 {
9444 out_char('\n');
9445 ++screen_cur_row;
9446 }
9447 screen_cur_col = 0;
9448 }
9449
9450 i = col - screen_cur_col;
9451 if (i > 0)
9452 {
9453 /*
9454 * Use cursor-right if it's one character only. Avoids
9455 * removing a line of pixels from the last bold char, when
9456 * using the bold trick in the GUI.
9457 */
9458 if (T_ND[0] != NUL && T_ND[1] == NUL)
9459 {
9460 while (i-- > 0)
9461 out_char(*T_ND);
9462 }
9463 else
9464 {
9465 int off;
9466
9467 off = LineOffset[row] + screen_cur_col;
9468 while (i-- > 0)
9469 {
9470 if (ScreenAttrs[off] != screen_attr)
9471 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 if (enc_dbcs == DBCS_JPNU
9475 && ScreenLines[off] == 0x8e)
9476 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 ++off;
9478 }
9479 }
9480 }
9481 }
9482 }
9483 else
9484 cost = 999;
9485
9486 if (cost >= goto_cost)
9487 {
9488 if (noinvcurs)
9489 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009490 if (row == screen_cur_row && (col > screen_cur_col)
9491 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 term_cursor_right(col - screen_cur_col);
9493 else
9494 term_windgoto(row, col);
9495 }
9496 screen_cur_row = row;
9497 screen_cur_col = col;
9498 }
9499}
9500
9501/*
9502 * Set cursor to its position in the current window.
9503 */
9504 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009505setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009507 setcursor_mayforce(FALSE);
9508}
9509
9510/*
9511 * Set cursor to its position in the current window.
9512 * When "force" is TRUE also when not redrawing.
9513 */
9514 void
9515setcursor_mayforce(int force)
9516{
9517 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 {
9519 validate_cursor();
9520 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009521 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009523 /* With 'rightleft' set and the cursor on a double-wide
9524 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009525 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9526 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009527 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009528 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529#endif
9530 curwin->w_wcol));
9531 }
9532}
9533
9534
9535/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009536 * Insert 'line_count' lines at 'row' in window 'wp'.
9537 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9538 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 * scrolling.
9540 * Returns FAIL if the lines are not inserted, OK for success.
9541 */
9542 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009543win_ins_lines(
9544 win_T *wp,
9545 int row,
9546 int line_count,
9547 int invalid,
9548 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550 int did_delete;
9551 int nextrow;
9552 int lastrow;
9553 int retval;
9554
9555 if (invalid)
9556 wp->w_lines_valid = 0;
9557
9558 if (wp->w_height < 5)
9559 return FAIL;
9560
9561 if (line_count > wp->w_height - row)
9562 line_count = wp->w_height - row;
9563
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009564 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 if (retval != MAYBE)
9566 return retval;
9567
9568 /*
9569 * If there is a next window or a status line, we first try to delete the
9570 * lines at the bottom to avoid messing what is after the window.
9571 * If this fails and there are following windows, don't do anything to avoid
9572 * messing up those windows, better just redraw.
9573 */
9574 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 if (wp->w_next != NULL || wp->w_status_height)
9576 {
9577 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009578 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 did_delete = TRUE;
9580 else if (wp->w_next)
9581 return FAIL;
9582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 /*
9584 * if no lines deleted, blank the lines that will end up below the window
9585 */
9586 if (!did_delete)
9587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009590 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 lastrow = nextrow + line_count;
9592 if (lastrow > Rows)
9593 lastrow = Rows;
9594 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009595 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 ' ', ' ', 0);
9597 }
9598
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009599 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 == FAIL)
9601 {
9602 /* deletion will have messed up other windows */
9603 if (did_delete)
9604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 win_rest_invalid(W_NEXT(wp));
9607 }
9608 return FAIL;
9609 }
9610
9611 return OK;
9612}
9613
9614/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009615 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9617 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9618 * scrolling
9619 * Return OK for success, FAIL if the lines are not deleted.
9620 */
9621 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009622win_del_lines(
9623 win_T *wp,
9624 int row,
9625 int line_count,
9626 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009627 int mayclear,
9628 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 int retval;
9631
9632 if (invalid)
9633 wp->w_lines_valid = 0;
9634
9635 if (line_count > wp->w_height - row)
9636 line_count = wp->w_height - row;
9637
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009638 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 if (retval != MAYBE)
9640 return retval;
9641
9642 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009643 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 return FAIL;
9645
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 /*
9647 * If there are windows or status lines below, try to put them at the
9648 * correct place. If we can't do that, they have to be redrawn.
9649 */
9650 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9651 {
9652 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009653 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 {
9655 wp->w_redr_status = TRUE;
9656 win_rest_invalid(wp->w_next);
9657 }
9658 }
9659 /*
9660 * If this is the last window and there is no status line, redraw the
9661 * command line later.
9662 */
9663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 redraw_cmdline = TRUE;
9665 return OK;
9666}
9667
9668/*
9669 * Common code for win_ins_lines() and win_del_lines().
9670 * Returns OK or FAIL when the work has been done.
9671 * Returns MAYBE when not finished yet.
9672 */
9673 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009674win_do_lines(
9675 win_T *wp,
9676 int row,
9677 int line_count,
9678 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009679 int del,
9680 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682 int retval;
9683
9684 if (!redrawing() || line_count <= 0)
9685 return FAIL;
9686
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009687 /* When inserting lines would result in loss of command output, just redraw
9688 * the lines. */
9689 if (no_win_do_lines_ins && !del)
9690 return FAIL;
9691
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009693 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009695 if (!no_win_do_lines_ins)
9696 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 return FAIL;
9698 }
9699
9700 /*
9701 * Delete all remaining lines
9702 */
9703 if (row + line_count >= wp->w_height)
9704 {
9705 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009706 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 ' ', ' ', 0);
9708 return OK;
9709 }
9710
9711 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009712 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009714 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009716 if (!no_win_do_lines_ins)
9717 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
9719 /*
9720 * If the terminal can set a scroll region, use that.
9721 * Always do this in a vertically split window. This will redraw from
9722 * ScreenLines[] when t_CV isn't defined. That's faster than using
9723 * win_line().
9724 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009725 * a character in the lower right corner of the scroll region may cause a
9726 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009728 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 scroll_region_set(wp, row);
9732 if (del)
9733 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009734 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 else
9736 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009737 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 scroll_region_reset();
9740 return retval;
9741 }
9742
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9744 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745
9746 return MAYBE;
9747}
9748
9749/*
9750 * window 'wp' and everything after it is messed up, mark it for redraw
9751 */
9752 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009753win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 {
9757 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 wp->w_redr_status = TRUE;
9759 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 }
9761 redraw_cmdline = TRUE;
9762}
9763
9764/*
9765 * The rest of the routines in this file perform screen manipulations. The
9766 * given operation is performed physically on the screen. The corresponding
9767 * change is also made to the internal screen image. In this way, the editor
9768 * anticipates the effect of editing changes on the appearance of the screen.
9769 * That way, when we call screenupdate a complete redraw isn't usually
9770 * necessary. Another advantage is that we can keep adding code to anticipate
9771 * screen changes, and in the meantime, everything still works.
9772 */
9773
9774/*
9775 * types for inserting or deleting lines
9776 */
9777#define USE_T_CAL 1
9778#define USE_T_CDL 2
9779#define USE_T_AL 3
9780#define USE_T_CE 4
9781#define USE_T_DL 5
9782#define USE_T_SR 6
9783#define USE_NL 7
9784#define USE_T_CD 8
9785#define USE_REDRAW 9
9786
9787/*
9788 * insert lines on the screen and update ScreenLines[]
9789 * 'end' is the line after the scrolled part. Normally it is Rows.
9790 * When scrolling region used 'off' is the offset from the top for the region.
9791 * 'row' and 'end' are relative to the start of the region.
9792 *
9793 * return FAIL for failure, OK for success.
9794 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009795 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009796screen_ins_lines(
9797 int off,
9798 int row,
9799 int line_count,
9800 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009801 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009802 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803{
9804 int i;
9805 int j;
9806 unsigned temp;
9807 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009808 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 int type;
9810 int result_empty;
9811 int can_ce = can_clear(T_CE);
9812
9813 /*
9814 * FAIL if
9815 * - there is no valid screen
9816 * - the screen has to be redrawn completely
9817 * - the line count is less than one
9818 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009819 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009821 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9822#ifdef FEAT_CLIPBOARD
9823 || (clip_star.state != SELECT_CLEARED
9824 && redrawing_for_callback > 0)
9825#endif
9826 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 return FAIL;
9828
9829 /*
9830 * There are seven ways to insert lines:
9831 * 0. When in a vertically split window and t_CV isn't set, redraw the
9832 * characters from ScreenLines[].
9833 * 1. Use T_CD (clear to end of display) if it exists and the result of
9834 * the insert is just empty lines
9835 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9836 * present or line_count > 1. It looks better if we do all the inserts
9837 * at once.
9838 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9839 * insert is just empty lines and T_CE is not present or line_count >
9840 * 1.
9841 * 4. Use T_AL (insert line) if it exists.
9842 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9843 * just empty lines.
9844 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9845 * just empty lines.
9846 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9847 * the 'da' flag is not set or we have clear line capability.
9848 * 8. redraw the characters from ScreenLines[].
9849 *
9850 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9851 * the scrollbar for the window. It does have insert line, use that if it
9852 * exists.
9853 */
9854 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9856 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009857 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 type = USE_T_CD;
9859 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9860 type = USE_T_CAL;
9861 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9862 type = USE_T_CDL;
9863 else if (*T_AL != NUL)
9864 type = USE_T_AL;
9865 else if (can_ce && result_empty)
9866 type = USE_T_CE;
9867 else if (*T_DL != NUL && result_empty)
9868 type = USE_T_DL;
9869 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9870 type = USE_T_SR;
9871 else
9872 return FAIL;
9873
9874 /*
9875 * For clearing the lines screen_del_lines() is used. This will also take
9876 * care of t_db if necessary.
9877 */
9878 if (type == USE_T_CD || type == USE_T_CDL ||
9879 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009880 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
9882 /*
9883 * If text is retained below the screen, first clear or delete as many
9884 * lines at the bottom of the window as are about to be inserted so that
9885 * the deleted lines won't later surface during a screen_del_lines.
9886 */
9887 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009888 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
9890#ifdef FEAT_CLIPBOARD
9891 /* Remove a modeless selection when inserting lines halfway the screen
9892 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009893 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009894 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895 else
9896 clip_scroll_selection(-line_count);
9897#endif
9898
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899#ifdef FEAT_GUI
9900 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9901 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009902 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903#endif
9904
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009905 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9906 cursor_col = wp->w_wincol;
9907
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 if (*T_CCS != NUL) /* cursor relative to region */
9909 cursor_row = row;
9910 else
9911 cursor_row = row + off;
9912
9913 /*
9914 * Shift LineOffset[] line_count down to reflect the inserted lines.
9915 * Clear the inserted lines in ScreenLines[].
9916 */
9917 row += off;
9918 end += off;
9919 for (i = 0; i < line_count; ++i)
9920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (wp != NULL && wp->w_width != Columns)
9922 {
9923 /* need to copy part of a line */
9924 j = end - 1 - i;
9925 while ((j -= line_count) >= row)
9926 linecopy(j + line_count, j, wp);
9927 j += line_count;
9928 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009929 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9930 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 else
9932 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9933 LineWraps[j] = FALSE;
9934 }
9935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 {
9937 j = end - 1 - i;
9938 temp = LineOffset[j];
9939 while ((j -= line_count) >= row)
9940 {
9941 LineOffset[j + line_count] = LineOffset[j];
9942 LineWraps[j + line_count] = LineWraps[j];
9943 }
9944 LineOffset[j + line_count] = temp;
9945 LineWraps[j + line_count] = FALSE;
9946 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009947 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 else
9949 lineinvalid(temp, (int)Columns);
9950 }
9951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952
9953 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009954 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009955 if (clear_attr != 0)
9956 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 /* redraw the characters */
9959 if (type == USE_REDRAW)
9960 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009961 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 {
9963 term_append_lines(line_count);
9964 screen_start(); /* don't know where cursor is now */
9965 }
9966 else
9967 {
9968 for (i = 0; i < line_count; i++)
9969 {
9970 if (type == USE_T_AL)
9971 {
9972 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009973 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 out_str(T_AL);
9975 }
9976 else /* type == USE_T_SR */
9977 out_str(T_SR);
9978 screen_start(); /* don't know where cursor is now */
9979 }
9980 }
9981
9982 /*
9983 * With scroll-reverse and 'da' flag set we need to clear the lines that
9984 * have been scrolled down into the region.
9985 */
9986 if (type == USE_T_SR && *T_DA)
9987 {
9988 for (i = 0; i < line_count; ++i)
9989 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009990 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 out_str(T_CE);
9992 screen_start(); /* don't know where cursor is now */
9993 }
9994 }
9995
9996#ifdef FEAT_GUI
9997 gui_can_update_cursor();
9998 if (gui.in_use)
9999 out_flush(); /* always flush after a scroll */
10000#endif
10001 return OK;
10002}
10003
10004/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010005 * Delete lines on the screen and update ScreenLines[].
10006 * "end" is the line after the scrolled part. Normally it is Rows.
10007 * When scrolling region used "off" is the offset from the top for the region.
10008 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 *
10010 * Return OK for success, FAIL if the lines are not deleted.
10011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010013screen_del_lines(
10014 int off,
10015 int row,
10016 int line_count,
10017 int end,
10018 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010019 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010020 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 int j;
10023 int i;
10024 unsigned temp;
10025 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010026 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 int cursor_end;
10028 int result_empty; /* result is empty until end of region */
10029 int can_delete; /* deleting line codes can be used */
10030 int type;
10031
10032 /*
10033 * FAIL if
10034 * - there is no valid screen
10035 * - the screen has to be redrawn completely
10036 * - the line count is less than one
10037 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010038 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010040 if (!screen_valid(TRUE) || line_count <= 0
10041 || (!force && line_count > p_ttyscroll)
10042#ifdef FEAT_CLIPBOARD
10043 || (clip_star.state != SELECT_CLEARED
10044 && redrawing_for_callback > 0)
10045#endif
10046 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 return FAIL;
10048
10049 /*
10050 * Check if the rest of the current region will become empty.
10051 */
10052 result_empty = row + line_count >= end;
10053
10054 /*
10055 * We can delete lines only when 'db' flag not set or when 'ce' option
10056 * available.
10057 */
10058 can_delete = (*T_DB == NUL || can_clear(T_CE));
10059
10060 /*
10061 * There are six ways to delete lines:
10062 * 0. When in a vertically split window and t_CV isn't set, redraw the
10063 * characters from ScreenLines[].
10064 * 1. Use T_CD if it exists and the result is empty.
10065 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10066 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10067 * none of the other ways work.
10068 * 4. Use T_CE (erase line) if the result is empty.
10069 * 5. Use T_DL (delete line) if it exists.
10070 * 6. redraw the characters from ScreenLines[].
10071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10073 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010074 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 type = USE_T_CD;
10076#if defined(__BEOS__) && defined(BEOS_DR8)
10077 /*
10078 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10079 * its internal termcap... this works okay for tests which test *T_DB !=
10080 * NUL. It has the disadvantage that the user cannot use any :set t_*
10081 * command to get T_DB (back) to empty_option, only :set term=... will do
10082 * the trick...
10083 * Anyway, this hack will hopefully go away with the next OS release.
10084 * (Olaf Seibert)
10085 */
10086 else if (row == 0 && T_DB == empty_option
10087 && (line_count == 1 || *T_CDL == NUL))
10088#else
10089 else if (row == 0 && (
10090#ifndef AMIGA
10091 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10092 * up, so use delete-line command */
10093 line_count == 1 ||
10094#endif
10095 *T_CDL == NUL))
10096#endif
10097 type = USE_NL;
10098 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10099 type = USE_T_CDL;
10100 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010101 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 type = USE_T_CE;
10103 else if (*T_DL != NUL && can_delete)
10104 type = USE_T_DL;
10105 else if (*T_CDL != NUL && can_delete)
10106 type = USE_T_CDL;
10107 else
10108 return FAIL;
10109
10110#ifdef FEAT_CLIPBOARD
10111 /* Remove a modeless selection when deleting lines halfway the screen or
10112 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010113 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010114 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 else
10116 clip_scroll_selection(line_count);
10117#endif
10118
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119#ifdef FEAT_GUI
10120 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10121 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010122 gui_dont_update_cursor(gui.cursor_row >= row + off
10123 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124#endif
10125
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010126 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10127 cursor_col = wp->w_wincol;
10128
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 if (*T_CCS != NUL) /* cursor relative to region */
10130 {
10131 cursor_row = row;
10132 cursor_end = end;
10133 }
10134 else
10135 {
10136 cursor_row = row + off;
10137 cursor_end = end + off;
10138 }
10139
10140 /*
10141 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10142 * Clear the inserted lines in ScreenLines[].
10143 */
10144 row += off;
10145 end += off;
10146 for (i = 0; i < line_count; ++i)
10147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 if (wp != NULL && wp->w_width != Columns)
10149 {
10150 /* need to copy part of a line */
10151 j = row + i;
10152 while ((j += line_count) <= end - 1)
10153 linecopy(j - line_count, j, wp);
10154 j -= line_count;
10155 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010156 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10157 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 else
10159 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10160 LineWraps[j] = FALSE;
10161 }
10162 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 {
10164 /* whole width, moving the line pointers is faster */
10165 j = row + i;
10166 temp = LineOffset[j];
10167 while ((j += line_count) <= end - 1)
10168 {
10169 LineOffset[j - line_count] = LineOffset[j];
10170 LineWraps[j - line_count] = LineWraps[j];
10171 }
10172 LineOffset[j - line_count] = temp;
10173 LineWraps[j - line_count] = FALSE;
10174 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010175 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 else
10177 lineinvalid(temp, (int)Columns);
10178 }
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010181 if (screen_attr != clear_attr)
10182 screen_stop_highlight();
10183 if (clear_attr != 0)
10184 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 /* redraw the characters */
10187 if (type == USE_REDRAW)
10188 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010189 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010191 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192 out_str(T_CD);
10193 screen_start(); /* don't know where cursor is now */
10194 }
10195 else if (type == USE_T_CDL)
10196 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010197 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 term_delete_lines(line_count);
10199 screen_start(); /* don't know where cursor is now */
10200 }
10201 /*
10202 * Deleting lines at top of the screen or scroll region: Just scroll
10203 * the whole screen (scroll region) up by outputting newlines on the
10204 * last line.
10205 */
10206 else if (type == USE_NL)
10207 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010208 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 for (i = line_count; --i >= 0; )
10210 out_char('\n'); /* cursor will remain on same line */
10211 }
10212 else
10213 {
10214 for (i = line_count; --i >= 0; )
10215 {
10216 if (type == USE_T_DL)
10217 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010218 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 out_str(T_DL); /* delete a line */
10220 }
10221 else /* type == USE_T_CE */
10222 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010223 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 out_str(T_CE); /* erase a line */
10225 }
10226 screen_start(); /* don't know where cursor is now */
10227 }
10228 }
10229
10230 /*
10231 * If the 'db' flag is set, we need to clear the lines that have been
10232 * scrolled up at the bottom of the region.
10233 */
10234 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10235 {
10236 for (i = line_count; i > 0; --i)
10237 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010238 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 out_str(T_CE); /* erase a line */
10240 screen_start(); /* don't know where cursor is now */
10241 }
10242 }
10243
10244#ifdef FEAT_GUI
10245 gui_can_update_cursor();
10246 if (gui.in_use)
10247 out_flush(); /* always flush after a scroll */
10248#endif
10249
10250 return OK;
10251}
10252
10253/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010254 * Return TRUE when postponing displaying the mode message: when not redrawing
10255 * or inside a mapping.
10256 */
10257 int
10258skip_showmode()
10259{
10260 // Call char_avail() only when we are going to show something, because it
10261 // takes a bit of time. redrawing() may also call char_avail_avail().
10262 if (global_busy
10263 || msg_silent != 0
10264 || !redrawing()
10265 || (char_avail() && !KeyTyped))
10266 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010267 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010268 return TRUE;
10269 }
10270 return FALSE;
10271}
10272
10273/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010274 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 *
10276 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10277 * If clear_cmdline is FALSE there may be a message there that needs to be
10278 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010279 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 * Return the length of the message (0 if no message).
10281 */
10282 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010283showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 int need_clear;
10286 int length = 0;
10287 int do_mode;
10288 int attr;
10289 int nwr_save;
10290#ifdef FEAT_INS_EXPAND
10291 int sub_attr;
10292#endif
10293
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010294 do_mode = ((p_smd && msg_silent == 0)
10295 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010296 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010297 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010298 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010300 if (skip_showmode())
10301 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302
10303 nwr_save = need_wait_return;
10304
10305 /* wait a bit before overwriting an important message */
10306 check_for_delay(FALSE);
10307
10308 /* if the cmdline is more than one line high, erase top lines */
10309 need_clear = clear_cmdline;
10310 if (clear_cmdline && cmdline_row < Rows - 1)
10311 msg_clr_cmdline(); /* will reset clear_cmdline */
10312
10313 /* Position on the last line in the window, column 0 */
10314 msg_pos_mode();
10315 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010316 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 if (do_mode)
10318 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010319 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010321 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010322# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010323 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010324# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010325 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010326# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010327 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010328# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010329 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010331 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332# endif
10333#endif
10334#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10335 if (gui.in_use)
10336 {
10337 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010338 {
10339 /* HANGUL */
10340 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010341 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010342 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010343 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 }
10346#endif
10347#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010348 /* CTRL-X in Insert mode */
10349 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 {
10351 /* These messages can get long, avoid a wrap in a narrow
10352 * window. Prefer showing edit_submode_extra. */
10353 length = (Rows - msg_row) * Columns - 3;
10354 if (edit_submode_extra != NULL)
10355 length -= vim_strsize(edit_submode_extra);
10356 if (length > 0)
10357 {
10358 if (edit_submode_pre != NULL)
10359 length -= vim_strsize(edit_submode_pre);
10360 if (length - vim_strsize(edit_submode) > 0)
10361 {
10362 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010363 msg_puts_attr((char *)edit_submode_pre, attr);
10364 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366 if (edit_submode_extra != NULL)
10367 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010368 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010370 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 else
10372 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010373 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 }
10375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 }
10377 else
10378#endif
10379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010381 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010382 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010383 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 else if (State & INSERT)
10385 {
10386#ifdef FEAT_RIGHTLEFT
10387 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010388 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010390 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010392 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010393 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010395 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010397 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398#ifdef FEAT_RIGHTLEFT
10399 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010400 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#endif
10402#ifdef FEAT_KEYMAP
10403 if (State & LANGMAP)
10404 {
10405# ifdef FEAT_ARABIC
10406 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010407 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 else
10409# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010410 if (get_keymap_str(curwin, (char_u *)" (%s)",
10411 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010412 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 }
10414#endif
10415 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010416 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 if (VIsual_active)
10419 {
10420 char *p;
10421
10422 /* Don't concatenate separate words to avoid translation
10423 * problems. */
10424 switch ((VIsual_select ? 4 : 0)
10425 + (VIsual_mode == Ctrl_V) * 2
10426 + (VIsual_mode == 'V'))
10427 {
10428 case 0: p = N_(" VISUAL"); break;
10429 case 1: p = N_(" VISUAL LINE"); break;
10430 case 2: p = N_(" VISUAL BLOCK"); break;
10431 case 4: p = N_(" SELECT"); break;
10432 case 5: p = N_(" SELECT LINE"); break;
10433 default: p = N_(" SELECT BLOCK"); break;
10434 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010435 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010437 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010439
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440 need_clear = TRUE;
10441 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010442 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443#ifdef FEAT_INS_EXPAND
10444 && edit_submode == NULL /* otherwise it gets too long */
10445#endif
10446 )
10447 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010448 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 need_clear = TRUE;
10450 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010451
10452 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010453 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 msg_clr_eos();
10455 msg_didout = FALSE; /* overwrite this message */
10456 length = msg_col;
10457 msg_col = 0;
10458 need_wait_return = nwr_save; /* never ask for hit-return for this */
10459 }
10460 else if (clear_cmdline && msg_silent == 0)
10461 /* Clear the whole command line. Will reset "clear_cmdline". */
10462 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010463 else if (redraw_mode)
10464 {
10465 msg_pos_mode();
10466 msg_clr_eos();
10467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468
10469#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 /* In Visual mode the size of the selected area must be redrawn. */
10471 if (VIsual_active)
10472 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473
10474 /* If the last window has no status line, the ruler is after the mode
10475 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010476 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010477 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478#endif
10479 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010480 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 clear_cmdline = FALSE;
10482
10483 return length;
10484}
10485
10486/*
10487 * Position for a mode message.
10488 */
10489 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010490msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 msg_col = 0;
10493 msg_row = Rows - 1;
10494}
10495
10496/*
10497 * Delete mode message. Used when ESC is typed which is expected to end
10498 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010499 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 */
10501 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010502unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010505 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 */
10507 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10508 redraw_cmdline = TRUE; /* delete mode later */
10509 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010510 clearmode();
10511}
10512
10513/*
10514 * Clear the mode message.
10515 */
10516 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010517clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010518{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010519 int save_msg_row = msg_row;
10520 int save_msg_col = msg_col;
10521
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010522 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010523 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010524 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010525 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010526
10527 msg_col = save_msg_col;
10528 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010531 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010532recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010533{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010534 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010535 if (!shortmess(SHM_RECORDING))
10536 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010537 char s[4];
10538
10539 sprintf(s, " @%c", reg_recording);
10540 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010541 }
10542}
10543
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010544/*
10545 * Draw the tab pages line at the top of the Vim window.
10546 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010547 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010548draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010549{
10550 int tabcount = 0;
10551 tabpage_T *tp;
10552 int tabwidth;
10553 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010554 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010555 int attr;
10556 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010557 win_T *cwp;
10558 int wincount;
10559 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010560 int c;
10561 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010562 int attr_sel = HL_ATTR(HLF_TPS);
10563 int attr_nosel = HL_ATTR(HLF_TP);
10564 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010565 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010566 int room;
10567 int use_sep_chars = (t_colors < 8
10568#ifdef FEAT_GUI
10569 && !gui.in_use
10570#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010571#ifdef FEAT_TERMGUICOLORS
10572 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010573#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010574 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010575
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010576 if (ScreenLines == NULL)
10577 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010578 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010579
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010580#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010581 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010582 if (gui_use_tabline())
10583 {
10584 gui_update_tabline();
10585 return;
10586 }
10587#endif
10588
10589 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010590 return;
10591
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010592#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010593 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010594
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010595 /* Use the 'tabline' option if it's set. */
10596 if (*p_tal != NUL)
10597 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010598 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010599
10600 /* Check for an error. If there is one we would loop in redrawing the
10601 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010602 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010603 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010604 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010605 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010606 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010607 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010608 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010609 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010610#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010611 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010612 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010614
Bram Moolenaar238a5642006-02-21 22:12:05 +000010615 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10616 if (tabwidth < 6)
10617 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010618
Bram Moolenaar238a5642006-02-21 22:12:05 +000010619 attr = attr_nosel;
10620 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010621 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10622 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010623 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010625
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 if (tp->tp_topframe == topframe)
10627 attr = attr_sel;
10628 if (use_sep_chars && col > 0)
10629 screen_putchar('|', 0, col++, attr);
10630
10631 if (tp->tp_topframe != topframe)
10632 attr = attr_nosel;
10633
10634 screen_putchar(' ', 0, col++, attr);
10635
10636 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010637 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010638 cwp = curwin;
10639 wp = firstwin;
10640 }
10641 else
10642 {
10643 cwp = tp->tp_curwin;
10644 wp = tp->tp_firstwin;
10645 }
10646
10647 modified = FALSE;
10648 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10649 if (bufIsChanged(wp->w_buffer))
10650 modified = TRUE;
10651 if (modified || wincount > 1)
10652 {
10653 if (wincount > 1)
10654 {
10655 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010656 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010657 if (col + len >= Columns - 3)
10658 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010659 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010660#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010661 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010662#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010663 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010664#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010665 );
10666 col += len;
10667 }
10668 if (modified)
10669 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10670 screen_putchar(' ', 0, col++, attr);
10671 }
10672
10673 room = scol - col + tabwidth - 1;
10674 if (room > 0)
10675 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010676 /* Get buffer name in NameBuff[] */
10677 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010678 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010679 len = vim_strsize(NameBuff);
10680 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010681 if (has_mbyte)
10682 while (len > room)
10683 {
10684 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010685 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010686 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010687 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010688 {
10689 p += len - room;
10690 len = room;
10691 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010692 if (len > Columns - col - 1)
10693 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010695 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010696 col += len;
10697 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010698 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010699
10700 /* Store the tab page number in TabPageIdxs[], so that
10701 * jump_to_mouse() knows where each one is. */
10702 ++tabcount;
10703 while (scol < col)
10704 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010705 }
10706
Bram Moolenaar238a5642006-02-21 22:12:05 +000010707 if (use_sep_chars)
10708 c = '_';
10709 else
10710 c = ' ';
10711 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010712
10713 /* Put an "X" for closing the current tab if there are several. */
10714 if (first_tabpage->tp_next != NULL)
10715 {
10716 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10717 TabPageIdxs[Columns - 1] = -999;
10718 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010719 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010720
10721 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10722 * set. */
10723 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010724}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010725
10726/*
10727 * Get buffer name for "buf" into NameBuff[].
10728 * Takes care of special buffer names and translates special characters.
10729 */
10730 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010731get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010732{
10733 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010734 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010735 else
10736 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10737 trans_characters(NameBuff, MAXPATHL);
10738}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010739
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740/*
10741 * Get the character to use in a status line. Get its attributes in "*attr".
10742 */
10743 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010744fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
10746 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010747
10748#ifdef FEAT_TERMINAL
10749 if (bt_terminal(wp->w_buffer))
10750 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010751 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010752 {
10753 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010754 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010755 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010756 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010757 {
10758 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010760 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010761 }
10762 else
10763#endif
10764 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010766 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 fill = fill_stl;
10768 }
10769 else
10770 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010771 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 fill = fill_stlnc;
10773 }
10774 /* Use fill when there is highlighting, and highlighting of current
10775 * window differs, or the fillchars differ, or this is not the
10776 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010777 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010778 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 || (fill_stl != fill_stlnc)))
10780 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010781 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 return '^';
10783 return '=';
10784}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786/*
10787 * Get the character to use in a separator between vertically split windows.
10788 * Get its attributes in "*attr".
10789 */
10790 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010791fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010793 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 if (*attr == 0 && fill_vert == ' ')
10795 return '|';
10796 else
10797 return fill_vert;
10798}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799
10800/*
10801 * Return TRUE if redrawing should currently be done.
10802 */
10803 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010804redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010806#ifdef FEAT_EVAL
10807 if (disable_redraw_for_testing)
10808 return 0;
10809 else
10810#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010811 return ((!RedrawingDisabled
10812#ifdef FEAT_EVAL
10813 || ignore_redraw_flag_for_testing
10814#endif
10815 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816}
10817
10818/*
10819 * Return TRUE if printing messages should currently be done.
10820 */
10821 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010822messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823{
10824 return (!(p_lz && char_avail() && !KeyTyped));
10825}
10826
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010827#ifdef FEAT_MENU
10828/*
10829 * Draw the window toolbar.
10830 */
10831 static void
10832redraw_win_toolbar(win_T *wp)
10833{
10834 vimmenu_T *menu;
10835 int item_idx = 0;
10836 int item_count = 0;
10837 int col = 0;
10838 int next_col;
10839 int off = (int)(current_ScreenLine - ScreenLines);
10840 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10841 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10842
10843 vim_free(wp->w_winbar_items);
10844 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10845 ++item_count;
10846 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
Bram Moolenaar18a4ba22019-05-24 19:39:03 +020010847 sizeof(winbar_item_T) * (item_count + 1));
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010848
10849 /* TODO: use fewer spaces if there is not enough room */
10850 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010851 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010852 {
10853 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010854 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010855 break;
10856 if (col > 1)
10857 {
10858 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010859 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010860 break;
10861 }
10862
10863 wp->w_winbar_items[item_idx].wb_startcol = col;
10864 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010865 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010866 break;
10867
10868 next_col = text_to_screenline(wp, menu->name, col);
10869 while (col < next_col)
10870 {
10871 ScreenAttrs[off + col] = button_attr;
10872 ++col;
10873 }
10874 wp->w_winbar_items[item_idx].wb_endcol = col;
10875 wp->w_winbar_items[item_idx].wb_menu = menu;
10876 ++item_idx;
10877
Bram Moolenaar02631462017-09-22 15:20:32 +020010878 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010879 break;
10880 space_to_screenline(off + col, button_attr);
10881 ++col;
10882 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010883 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010884 {
10885 space_to_screenline(off + col, fill_attr);
10886 ++col;
10887 }
10888 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10889
Bram Moolenaar02631462017-09-22 15:20:32 +020010890 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010891 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010892}
10893#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010894
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895/*
10896 * Show current status info in ruler and various other places
10897 * If always is FALSE, only show ruler if position has changed.
10898 */
10899 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010900showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901{
10902 if (!always && !redrawing())
10903 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010904#ifdef FEAT_INS_EXPAND
10905 if (pum_visible())
10906 {
10907 /* Don't redraw right now, do it later. */
10908 curwin->w_redr_status = TRUE;
10909 return;
10910 }
10911#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010912#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010913 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010914 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 else
10916#endif
10917#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010918 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919#endif
10920
10921#ifdef FEAT_TITLE
10922 if (need_maketitle
10923# ifdef FEAT_STL_OPT
10924 || (p_icon && (stl_syntax & STL_IN_ICON))
10925 || (p_title && (stl_syntax & STL_IN_TITLE))
10926# endif
10927 )
10928 maketitle();
10929#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010930 /* Redraw the tab pages line if needed. */
10931 if (redraw_tabline)
10932 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933}
10934
10935#ifdef FEAT_CMDL_INFO
10936 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010937win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010939#define RULER_BUF_LEN 70
10940 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 int row;
10942 int fillchar;
10943 int attr;
10944 int empty_line = FALSE;
10945 colnr_T virtcol;
10946 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010947 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 int this_ru_col;
10950 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010951 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952
10953 /* If 'ruler' off or redrawing disabled, don't do anything */
10954 if (!p_ru)
10955 return;
10956
10957 /*
10958 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10959 * after deleting lines, before cursor.lnum is corrected.
10960 */
10961 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10962 return;
10963
10964#ifdef FEAT_INS_EXPAND
10965 /* Don't draw the ruler while doing insert-completion, it might overwrite
10966 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 if (edit_submode != NULL)
10969 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010970 // Don't draw the ruler when the popup menu is visible, it may overlap.
10971 // Except when the popup menu will be redrawn anyway.
10972 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010973 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974#endif
10975
10976#ifdef FEAT_STL_OPT
10977 if (*p_ruf)
10978 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010979 int save_called_emsg = called_emsg;
10980
10981 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010983 if (called_emsg)
10984 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010985 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010986 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 return;
10988 }
10989#endif
10990
10991 /*
10992 * Check if not in Insert mode and the line is empty (will show "0-1").
10993 */
10994 if (!(State & INSERT)
10995 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10996 empty_line = TRUE;
10997
10998 /*
10999 * Only draw the ruler when something changed.
11000 */
11001 validate_virtcol_win(wp);
11002 if ( redraw_cmdline
11003 || always
11004 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11005 || wp->w_cursor.col != wp->w_ru_cursor.col
11006 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 || wp->w_topline != wp->w_ru_topline
11009 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11010#ifdef FEAT_DIFF
11011 || wp->w_topfill != wp->w_ru_topfill
11012#endif
11013 || empty_line != wp->w_ru_empty)
11014 {
11015 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 if (wp->w_status_height)
11017 {
11018 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011019 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011020 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011021 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 }
11023 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 {
11025 row = Rows - 1;
11026 fillchar = ' ';
11027 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 width = Columns;
11029 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 }
11031
11032 /* In list mode virtcol needs to be recomputed */
11033 virtcol = wp->w_virtcol;
11034 if (wp->w_p_list && lcs_tab1 == NUL)
11035 {
11036 wp->w_p_list = FALSE;
11037 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11038 wp->w_p_list = TRUE;
11039 }
11040
11041 /*
11042 * Some sprintfs return the length, some return a pointer.
11043 * To avoid portability problems we use strlen() here.
11044 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011045 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11047 ? 0L
11048 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011049 len = STRLEN(buffer);
11050 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11052 (int)virtcol + 1);
11053
11054 /*
11055 * Add a "50%" if there is room for it.
11056 * On the last line, don't print in the last column (scrolls the
11057 * screen up on some terminals).
11058 */
11059 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011060 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 this_ru_col = ru_col - (Columns - width);
11065 if (this_ru_col < 0)
11066 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 /* Never use more than half the window/screen width, leave the other
11068 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011069 if (this_ru_col < (width + 1) / 2)
11070 this_ru_col = (width + 1) / 2;
11071 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011073 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011074 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 if (has_mbyte)
11077 i += (*mb_char2bytes)(fillchar, buffer + i);
11078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 buffer[i++] = fillchar;
11080 ++o;
11081 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011082 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 }
11084 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 if (has_mbyte)
11086 {
11087 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011088 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 {
11090 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011091 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 {
11093 buffer[i] = NUL;
11094 break;
11095 }
11096 }
11097 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011098 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011099 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100
Bram Moolenaar4033c552017-09-16 20:54:51 +020011101 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 i = redraw_cmdline;
11103 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011104 this_ru_col + off + (int)STRLEN(buffer),
11105 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106 fillchar, fillchar, attr);
11107 /* don't redraw the cmdline because of showing the ruler */
11108 redraw_cmdline = i;
11109 wp->w_ru_cursor = wp->w_cursor;
11110 wp->w_ru_virtcol = wp->w_virtcol;
11111 wp->w_ru_empty = empty_line;
11112 wp->w_ru_topline = wp->w_topline;
11113 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11114#ifdef FEAT_DIFF
11115 wp->w_ru_topfill = wp->w_topfill;
11116#endif
11117 }
11118}
11119#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011120
11121#if defined(FEAT_LINEBREAK) || defined(PROTO)
11122/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011123 * Return the width of the 'number' and 'relativenumber' column.
11124 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011125 * Otherwise it depends on 'numberwidth' and the line count.
11126 */
11127 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011128number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011129{
11130 int n;
11131 linenr_T lnum;
11132
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011133 if (wp->w_p_rnu && !wp->w_p_nu)
11134 /* cursor line shows "0" */
11135 lnum = wp->w_height;
11136 else
11137 /* cursor line shows absolute line number */
11138 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011139
Bram Moolenaar6b314672015-03-20 15:42:10 +010011140 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011141 return wp->w_nrwidth_width;
11142 wp->w_nrwidth_line_count = lnum;
11143
11144 n = 0;
11145 do
11146 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011147 lnum /= 10;
11148 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011149 } while (lnum > 0);
11150
11151 /* 'numberwidth' gives the minimal width plus one */
11152 if (n < wp->w_p_nuw - 1)
11153 n = wp->w_p_nuw - 1;
11154
11155 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011156 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011157 return n;
11158}
11159#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011160
Bram Moolenaar113e1072019-01-20 15:30:40 +010011161#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011162/*
11163 * Return the current cursor column. This is the actual position on the
11164 * screen. First column is 0.
11165 */
11166 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011167screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011168{
11169 return screen_cur_col;
11170}
11171
11172/*
11173 * Return the current cursor row. This is the actual position on the screen.
11174 * First row is 0.
11175 */
11176 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011177screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011178{
11179 return screen_cur_row;
11180}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011181#endif