blob: 79baebe77ae3861033b17c1782e3a4486a9357cc [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 Moolenaar2951b772013-07-03 12:45:31 +0200331 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200332 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200334 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (screenline == NULL || screenattr == NULL)
336 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (enc_utf8)
338 {
339 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200340 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200341 if (screenlineUC == NULL)
342 ret = 2;
343 for (i = 0; i < p_mco; ++i)
344 {
345 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200346 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 if (screenlineC[i] == NULL)
348 ret = 2;
349 }
350 }
351 if (enc_dbcs == DBCS_JPNU)
352 {
353 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 if (screenline2 == NULL)
356 ret = 2;
357 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200358
359 if (ret != 2)
360 {
361 /* Save the text displayed in the command line area. */
362 for (r = 0; r < rows; ++r)
363 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(schar_T));
367 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 if (enc_utf8)
371 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200373 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenlineC[i] + r * cols,
377 ScreenLinesC[i] + LineOffset[cmdline_row + r],
378 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200381 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200382 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 }
385
386 update_screen(0);
387 ret = 3;
388
389 if (must_redraw == 0)
390 {
391 int off = (int)(current_ScreenLine - ScreenLines);
392
393 /* Restore the text displayed in the command line area. */
394 for (r = 0; r < rows; ++r)
395 {
396 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenline + r * cols,
398 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenattr + r * cols,
401 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 if (enc_utf8)
403 {
404 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineUC + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 for (i = 0; i < p_mco; ++i)
408 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineC[i] + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 }
412 if (enc_dbcs == DBCS_JPNU)
413 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200414 screenline2 + r * cols,
415 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200416 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418 ret = 4;
419 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 }
421
422 vim_free(screenline);
423 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200424 if (enc_utf8)
425 {
426 vim_free(screenlineUC);
427 for (i = 0; i < p_mco; ++i)
428 vim_free(screenlineC[i]);
429 }
430 if (enc_dbcs == DBCS_JPNU)
431 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200432
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200433 /* Show the intro message when appropriate. */
434 maybe_intro_message();
435
436 setcursor();
437
Bram Moolenaar2951b772013-07-03 12:45:31 +0200438 return ret;
439}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100440#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200441
442/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 * Invoked after an asynchronous callback is called.
444 * If an echo command was used the cursor needs to be put back where
445 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200446 * If "call_update_screen" is FALSE don't call update_screen() when at the
447 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100448 */
449 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200450redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100451{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200452 ++redrawing_for_callback;
453
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100454 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100456 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200457 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200458 // Don't redraw when in prompt_for_number().
459 if (cmdline_row > 0)
460 {
461 // Redrawing only works when the screen didn't scroll. Don't clear
462 // wildmenu entries.
463 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200464#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200466#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200467 && call_update_screen)
468 update_screen(0);
469
470 // Redraw in the same position, so that the user can continue
471 // editing the command.
472 redrawcmdline_ex(FALSE);
473 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200474 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200475 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100476 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200477 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200478 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100479 setcursor();
480 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100481 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100482#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100483 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200484 // Don't update the cursor when it is blinking and off to avoid
485 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100486 out_flush_cursor(FALSE, FALSE);
487 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100488#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100489 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200490
491 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100492}
493
494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 * Changed something in the current window, at buffer line "lnum", that
496 * requires that line and possibly other lines to be redrawn.
497 * Used when entering/leaving Insert mode with the cursor on a folded line.
498 * Used to remove the "$" from a change command.
499 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
500 * may become invalid and the whole window will have to be redrawn.
501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100503redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200504 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100505 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200507 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
508 wp->w_redraw_top = lnum;
509 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
510 wp->w_redraw_bot = lnum;
511 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512}
513
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200514 void
515reset_updating_screen(int may_resize_shell UNUSED)
516{
517 updating_screen = FALSE;
518#ifdef FEAT_GUI
519 if (may_resize_shell)
520 gui_may_resize_shell();
521#endif
522#ifdef FEAT_TERMINAL
523 term_check_channel_closed_recently();
524#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200525
526#ifdef HAVE_DROP_FILE
527 // If handle_drop() was called while updating_screen was TRUE need to
528 // handle the drop now.
529 handle_any_postponed_drop();
530#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200531}
532
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200534 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 */
536 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100537update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 redraw_curbuf_later(type);
540 update_screen(type);
541}
542
543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 * Based on the current value of curwin->w_topline, transfer a screenfull
545 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200546 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200548 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200549update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200551 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 win_T *wp;
553 static int did_intro = FALSE;
554#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
555 int did_one;
556#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200557#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200558 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200559 int gui_cursor_col = 0;
560 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200561#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200562 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000564 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200566 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200568 if (type == VALID_NO_UPDATE)
569 {
570 no_update = TRUE;
571 type = 0;
572 }
573
Bram Moolenaara3347722019-05-11 21:14:24 +0200574#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200575 {
576 buf_T *buf;
577
578 // Before updating the screen, notify any listeners of changed text.
579 FOR_ALL_BUFFERS(buf)
580 invoke_listeners(buf);
581 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200582#endif
583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 if (must_redraw)
585 {
586 if (type < must_redraw) /* use maximal type */
587 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000588
589 /* must_redraw is reset here, so that when we run into some weird
590 * reason to redraw while busy redrawing (e.g., asynchronous
591 * scrolling), or update_topline() in win_update() will cause a
592 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 must_redraw = 0;
594 }
595
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200596 /* May need to update w_lines[]. */
597 if (curwin->w_lines_valid == 0 && type < NOT_VALID
598#ifdef FEAT_TERMINAL
599 && !term_do_update_window(curwin)
600#endif
601 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 type = NOT_VALID;
603
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000604 /* Postpone the redrawing when it's not needed and when being called
605 * recursively. */
606 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 {
608 redraw_later(type); /* remember type for next time */
609 must_redraw = type;
610 if (type > INVERTED_ALL)
611 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200612 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200614#ifdef FEAT_TEXT_PROP
615 // TODO: avoid redrawing everything when there is a popup window.
616 if (first_popupwin != NULL || first_tab_popupwin != NULL)
617 type = NOT_VALID;
618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 updating_screen = TRUE;
621#ifdef FEAT_SYN_HL
622 ++display_tick; /* let syntax code know we're in a next round of
623 * display updating */
624#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200625 if (no_update)
626 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628 /*
629 * if the screen was scrolled up when displaying a message, scroll it down
630 */
631 if (msg_scrolled)
632 {
633 clear_cmdline = TRUE;
634 if (msg_scrolled > Rows - 5) /* clearing is faster */
635 type = CLEAR;
636 else if (type != CLEAR)
637 {
638 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200639 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
640 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 type = CLEAR;
642 FOR_ALL_WINDOWS(wp)
643 {
644 if (W_WINROW(wp) < msg_scrolled)
645 {
646 if (W_WINROW(wp) + wp->w_height > msg_scrolled
647 && wp->w_redr_type < REDRAW_TOP
648 && wp->w_lines_valid > 0
649 && wp->w_topline == wp->w_lines[0].wl_lnum)
650 {
651 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
652 wp->w_redr_type = REDRAW_TOP;
653 }
654 else
655 {
656 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200657 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
658 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661 }
662 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200663 if (!no_update)
664 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000665 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667 msg_scrolled = 0;
668 need_wait_return = FALSE;
669 }
670
671 /* reset cmdline_row now (may have been changed temporarily) */
672 compute_cmdrow();
673
674 /* Check for changed highlighting */
675 if (need_highlight_changed)
676 highlight_changed();
677
678 if (type == CLEAR) /* first clear screen */
679 {
680 screenclear(); /* will reset clear_cmdline */
681 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200682 /* must_redraw may be set indirectly, avoid another redraw later */
683 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685
686 if (clear_cmdline) /* going to clear cmdline (done below) */
687 check_for_delay(FALSE);
688
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200690 /* Force redraw when width of 'number' or 'relativenumber' column
691 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000692 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200693 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
694 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000695 curwin->w_redr_type = NOT_VALID;
696#endif
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 /*
699 * Only start redrawing if there is really something to do.
700 */
701 if (type == INVERTED)
702 update_curswant();
703 if (curwin->w_redr_type < type
704 && !((type == VALID
705 && curwin->w_lines[0].wl_valid
706#ifdef FEAT_DIFF
707 && curwin->w_topfill == curwin->w_old_topfill
708 && curwin->w_botfill == curwin->w_old_botfill
709#endif
710 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000712 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
714 && curwin->w_old_visual_mode == VIsual_mode
715 && (curwin->w_valid & VALID_VIRTCOL)
716 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 ))
718 curwin->w_redr_type = type;
719
Bram Moolenaar5a305422006-04-28 22:38:25 +0000720 /* Redraw the tab pages line if needed. */
721 if (redraw_tabline || type >= NOT_VALID)
722 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#ifdef FEAT_SYN_HL
725 /*
726 * Correct stored syntax highlighting info for changes in each displayed
727 * buffer. Each buffer must only be done once.
728 */
729 FOR_ALL_WINDOWS(wp)
730 {
731 if (wp->w_buffer->b_mod_set)
732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 win_T *wwp;
734
735 /* Check if we already did this buffer. */
736 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
737 if (wwp->w_buffer == wp->w_buffer)
738 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200739 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 syn_stack_apply_changes(wp->w_buffer);
741 }
742 }
743#endif
744
745 /*
746 * Go from top to bottom through the windows, redrawing the ones that need
747 * it.
748 */
749#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
750 did_one = FALSE;
751#endif
752#ifdef FEAT_SEARCH_EXTRA
753 search_hl.rm.regprog = NULL;
754#endif
755 FOR_ALL_WINDOWS(wp)
756 {
757 if (wp->w_redr_type != 0)
758 {
759 cursor_off();
760#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
761 if (!did_one)
762 {
763 did_one = TRUE;
764# ifdef FEAT_SEARCH_EXTRA
765 start_search_hl();
766# endif
767# ifdef FEAT_CLIPBOARD
768 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200769 if (clip_star.available && clip_isautosel_star())
770 clip_update_selection(&clip_star);
771 if (clip_plus.available && clip_isautosel_plus())
772 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# endif
774#ifdef FEAT_GUI
775 /* Remove the cursor before starting to do anything, because
776 * scrolling may make it difficult to redraw the text under
777 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200778 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200779 {
780 gui_cursor_col = gui.cursor_col;
781 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200783 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785#endif
786 }
787#endif
788 win_update(wp);
789 }
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 /* redraw status line after the window to minimize cursor movement */
792 if (wp->w_redr_status)
793 {
794 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200795 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 }
798#if defined(FEAT_SEARCH_EXTRA)
799 end_search_hl();
800#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100801#ifdef FEAT_INS_EXPAND
802 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200803 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 /* Reset b_mod_set flags. Going through all windows is probably faster
807 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200808 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200811 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
813 /* Clear or redraw the command line. Done last, because scrolling may
814 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200815 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 showmode();
817
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200818 if (no_update)
819 --no_win_do_lines_ins;
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200822 if (!did_intro)
823 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 did_intro = TRUE;
825
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200826#ifdef FEAT_TEXT_PROP
827 // Display popup windows on top of the others.
828 update_popups();
829#endif
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#ifdef FEAT_GUI
832 /* Redraw the cursor and update the scrollbars when all screen updating is
833 * done. */
834 if (gui.in_use)
835 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200836 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200837 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100838 mch_disable_flush();
839 out_flush(); /* required before updating the cursor */
840 mch_enable_flush();
841
Bram Moolenaar144445d2016-07-08 21:41:54 +0200842 /* Put the GUI position where the cursor was, gui_update_cursor()
843 * uses that. */
844 gui.col = gui_cursor_col;
845 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200846 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100848 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200849 screen_cur_col = gui.col;
850 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200851 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100852 else
853 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 gui_update_scrollbars(FALSE);
855 }
856#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200857 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858}
859
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100860#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100861/*
862 * Prepare for updating one or more windows.
863 * Caller must check for "updating_screen" already set to avoid recursiveness.
864 */
865 static void
866update_prepare(void)
867{
868 cursor_off();
869 updating_screen = TRUE;
870#ifdef FEAT_GUI
871 /* Remove the cursor before starting to do anything, because scrolling may
872 * make it difficult to redraw the text under it. */
873 if (gui.in_use)
874 gui_undraw_cursor();
875#endif
876#ifdef FEAT_SEARCH_EXTRA
877 start_search_hl();
878#endif
879}
880
881/*
882 * Finish updating one or more windows.
883 */
884 static void
885update_finish(void)
886{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200887 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100888 showmode();
889
890# ifdef FEAT_SEARCH_EXTRA
891 end_search_hl();
892# endif
893
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200894 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100895
896# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100897 /* Redraw the cursor and update the scrollbars when all screen updating is
898 * done. */
899 if (gui.in_use)
900 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100901 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100902 gui_update_scrollbars(FALSE);
903 }
904# endif
905}
906#endif
907
Bram Moolenaar860cae12010-06-05 23:22:07 +0200908#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200909/*
910 * Return TRUE if the cursor line in window "wp" may be concealed, according
911 * to the 'concealcursor' option.
912 */
913 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100914conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200915{
916 int c;
917
918 if (*wp->w_p_cocu == NUL)
919 return FALSE;
920 if (get_real_state() & VISUAL)
921 c = 'v';
922 else if (State & INSERT)
923 c = 'i';
924 else if (State & NORMAL)
925 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200926 else if (State & CMDLINE)
927 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200928 else
929 return FALSE;
930 return vim_strchr(wp->w_p_cocu, c) != NULL;
931}
932
933/*
934 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
935 */
936 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200937conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200938{
939 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
940 {
941 need_cursor_line_redraw = TRUE;
942 /* Need to recompute cursor column, e.g., when starting Visual mode
943 * without concealing. */
944 curs_columns(TRUE);
945 }
946}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947#endif
948
Bram Moolenaar113e1072019-01-20 15:30:40 +0100949#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100951update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
953 win_T *wp;
954 int doit = FALSE;
955
956# ifdef FEAT_FOLDING
957 win_foldinfo.fi_level = 0;
958# endif
959
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100960 // update/delete a specific sign
961 redraw_buf_line_later(buf, lnum);
962
963 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (wp->w_redr_type != 0)
966 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100968 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200969 * happening (recursive call), messages on the screen or still starting up.
970 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100971 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200972 || State == ASKMORE || State == HITRETURN
973 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100974#ifdef FEAT_GUI
975 || gui.starting
976#endif
977 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 return;
979
980 /* update all windows that need updating */
981 update_prepare();
982
Bram Moolenaar29323592016-07-24 22:04:11 +0200983 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
985 if (wp->w_redr_type != 0)
986 win_update(wp);
987 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200988 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
991 update_finish();
992}
993#endif
994
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200995#ifdef FEAT_TEXT_PROP
996 static void
997update_popups(void)
998{
999 win_T *wp;
1000 win_T *lowest_wp;
1001 int lowest_zindex;
1002
1003 // Reset all the VALID_POPUP flags.
1004 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1005 wp->w_valid &= ~VALID_POPUP;
1006 for (wp = first_tab_popupwin; wp != NULL; wp = wp->w_next)
1007 wp->w_valid &= ~VALID_POPUP;
1008
1009 // TODO: don't redraw every popup every time.
1010 for (;;)
1011 {
1012 // Find the window with the lowest zindex that hasn't been updated yet,
1013 // so that the window with a higher zindex is drawn later, thus goes on
1014 // top.
1015 lowest_zindex = INT_MAX;
1016 lowest_wp = NULL;
1017 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1018 if ((wp->w_valid & VALID_POPUP) == 0
1019 && wp->w_zindex < lowest_zindex)
1020 {
1021 lowest_zindex = wp->w_zindex;
1022 lowest_wp = wp;
1023 }
1024 for (wp = first_tab_popupwin; wp != NULL; wp = wp->w_next)
1025 if ((wp->w_valid & VALID_POPUP) == 0
1026 && wp->w_zindex < lowest_zindex)
1027 {
1028 lowest_zindex = wp->w_zindex;
1029 lowest_wp = wp;
1030 }
1031
1032 if (lowest_wp == NULL)
1033 break;
1034 win_update(lowest_wp);
1035 lowest_wp->w_valid |= VALID_POPUP;
1036 }
1037}
1038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
1040#if defined(FEAT_GUI) || defined(PROTO)
1041/*
1042 * Update a single window, its status line and maybe the command line msg.
1043 * Used for the GUI scrollbar.
1044 */
1045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001046updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001048 /* return if already busy updating */
1049 if (updating_screen)
1050 return;
1051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 update_prepare();
1053
1054#ifdef FEAT_CLIPBOARD
1055 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001056 if (clip_star.available && clip_isautosel_star())
1057 clip_update_selection(&clip_star);
1058 if (clip_plus.available && clip_isautosel_plus())
1059 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001063
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001064 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001065 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001066 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001067
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (wp->w_redr_status
1069# ifdef FEAT_CMDL_INFO
1070 || p_ru
1071# endif
1072# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001073 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074# endif
1075 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001076 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 update_finish();
1079}
1080#endif
1081
1082/*
1083 * Update a single window.
1084 *
1085 * This may cause the windows below it also to be redrawn (when clearing the
1086 * screen or scrolling lines).
1087 *
1088 * How the window is redrawn depends on wp->w_redr_type. Each type also
1089 * implies the one below it.
1090 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001091 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1093 * INVERTED redraw the changed part of the Visual area
1094 * INVERTED_ALL redraw the whole Visual area
1095 * VALID 1. scroll up/down to adjust for a changed w_topline
1096 * 2. update lines at the top when scrolled down
1097 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001098 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 * b_mod_top and b_mod_bot.
1100 * - if wp->w_redraw_top non-zero, redraw lines between
1101 * wp->w_redraw_top and wp->w_redr_bot.
1102 * - continue redrawing when syntax status is invalid.
1103 * 4. if scrolled up, update lines at the bottom.
1104 * This results in three areas that may need updating:
1105 * top: from first row to top_end (when scrolled down)
1106 * mid: from mid_start to mid_end (update inversion or changed text)
1107 * bot: from bot_start to last row (when scrolled up)
1108 */
1109 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001110win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 buf_T *buf = wp->w_buffer;
1113 int type;
1114 int top_end = 0; /* Below last row of the top area that needs
1115 updating. 0 when no top area updating. */
1116 int mid_start = 999;/* first row of the mid area that needs
1117 updating. 999 when no mid area updating. */
1118 int mid_end = 0; /* Below last row of the mid area that needs
1119 updating. 0 when no mid area updating. */
1120 int bot_start = 999;/* first row of the bot area that needs
1121 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 int scrolled_down = FALSE; /* TRUE when scrolled down when
1123 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001125 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 int top_to_mod = FALSE; /* redraw above mod_top */
1127#endif
1128
1129 int row; /* current window row to display */
1130 linenr_T lnum; /* current buffer lnum to display */
1131 int idx; /* current index in w_lines[] */
1132 int srow; /* starting row of the current line */
1133
1134 int eof = FALSE; /* if TRUE, we hit the end of the file */
1135 int didline = FALSE; /* if TRUE, we finished the last line */
1136 int i;
1137 long j;
1138 static int recursive = FALSE; /* being called recursively */
1139 int old_botline = wp->w_botline;
1140#ifdef FEAT_FOLDING
1141 long fold_count;
1142#endif
1143#ifdef FEAT_SYN_HL
1144 /* remember what happened to the previous line, to know if
1145 * check_visual_highlight() can be used */
1146#define DID_NONE 1 /* didn't update a line */
1147#define DID_LINE 2 /* updated a normal line */
1148#define DID_FOLD 3 /* updated a folded line */
1149 int did_update = DID_NONE;
1150 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1151#endif
1152 linenr_T mod_top = 0;
1153 linenr_T mod_bot = 0;
1154#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1155 int save_got_int;
1156#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001157#ifdef SYN_TIME_LIMIT
1158 proftime_T syntax_tm;
1159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 type = wp->w_redr_type;
1162
1163 if (type == NOT_VALID)
1164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 wp->w_lines_valid = 0;
1167 }
1168
1169 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001170 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 wp->w_redr_type = 0;
1173 return;
1174 }
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 /* Window is zero-width: Only need to draw the separator. */
1177 if (wp->w_width == 0)
1178 {
1179 /* draw the vertical separator right of this window */
1180 draw_vsep_win(wp, 0);
1181 wp->w_redr_type = 0;
1182 return;
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001185#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001186 // If this window contains a terminal, redraw works completely differently.
1187 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001188 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001189 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001190# ifdef FEAT_MENU
1191 /* Draw the window toolbar, if there is one. */
1192 if (winbar_height(wp) > 0)
1193 redraw_win_toolbar(wp);
1194# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001195 wp->w_redr_type = 0;
1196 return;
1197 }
1198#endif
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001201 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202#endif
1203
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001204#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001205 /* Force redraw when width of 'number' or 'relativenumber' column
1206 * changes. */
1207 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001208 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001209 {
1210 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001211 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001212 }
1213 else
1214#endif
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1217 {
1218 /*
1219 * When there are both inserted/deleted lines and specific lines to be
1220 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1221 * everything (only happens when redrawing is off for while).
1222 */
1223 type = NOT_VALID;
1224 }
1225 else
1226 {
1227 /*
1228 * Set mod_top to the first line that needs displaying because of
1229 * changes. Set mod_bot to the first line after the changes.
1230 */
1231 mod_top = wp->w_redraw_top;
1232 if (wp->w_redraw_bot != 0)
1233 mod_bot = wp->w_redraw_bot + 1;
1234 else
1235 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (buf->b_mod_set)
1237 {
1238 if (mod_top == 0 || mod_top > buf->b_mod_top)
1239 {
1240 mod_top = buf->b_mod_top;
1241#ifdef FEAT_SYN_HL
1242 /* Need to redraw lines above the change that may be included
1243 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001244 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001246 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (mod_top < 1)
1248 mod_top = 1;
1249 }
1250#endif
1251 }
1252 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1253 mod_bot = buf->b_mod_bot;
1254
1255#ifdef FEAT_SEARCH_EXTRA
1256 /* When 'hlsearch' is on and using a multi-line search pattern, a
1257 * change in one line may make the Search highlighting in a
1258 * previous line invalid. Simple solution: redraw all visible
1259 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001260 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001262 if (search_hl.rm.regprog != NULL
1263 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001265 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001266 {
1267 cur = wp->w_match_head;
1268 while (cur != NULL)
1269 {
1270 if (cur->match.regprog != NULL
1271 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001272 {
1273 top_to_mod = TRUE;
1274 break;
1275 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001276 cur = cur->next;
1277 }
1278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279#endif
1280 }
1281#ifdef FEAT_FOLDING
1282 if (mod_top != 0 && hasAnyFolding(wp))
1283 {
1284 linenr_T lnumt, lnumb;
1285
1286 /*
1287 * A change in a line can cause lines above it to become folded or
1288 * unfolded. Find the top most buffer line that may be affected.
1289 * If the line was previously folded and displayed, get the first
1290 * line of that fold. If the line is folded now, get the first
1291 * folded line. Use the minimum of these two.
1292 */
1293
1294 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1295 * the line below it. If there is no valid entry, use w_topline.
1296 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1297 * to this line. If there is no valid entry, use MAXLNUM. */
1298 lnumt = wp->w_topline;
1299 lnumb = MAXLNUM;
1300 for (i = 0; i < wp->w_lines_valid; ++i)
1301 if (wp->w_lines[i].wl_valid)
1302 {
1303 if (wp->w_lines[i].wl_lastlnum < mod_top)
1304 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1305 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1306 {
1307 lnumb = wp->w_lines[i].wl_lnum;
1308 /* When there is a fold column it might need updating
1309 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001310 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 ++lnumb;
1312 }
1313 }
1314
1315 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1316 if (mod_top > lnumt)
1317 mod_top = lnumt;
1318
1319 /* Now do the same for the bottom line (one above mod_bot). */
1320 --mod_bot;
1321 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1322 ++mod_bot;
1323 if (mod_bot < lnumb)
1324 mod_bot = lnumb;
1325 }
1326#endif
1327
1328 /* When a change starts above w_topline and the end is below
1329 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001330 * If the end of the change is above w_topline: do like no change was
1331 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 if (mod_top != 0 && mod_top < wp->w_topline)
1333 {
1334 if (mod_bot > wp->w_topline)
1335 mod_top = wp->w_topline;
1336#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001337 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 top_end = 1;
1339#endif
1340 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001341
1342 /* When line numbers are displayed need to redraw all lines below
1343 * inserted/deleted lines. */
1344 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1345 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001347 wp->w_redraw_top = 0; // reset for next time
1348 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
1350 /*
1351 * When only displaying the lines at the top, set top_end. Used when
1352 * window has scrolled down for msg_scrolled.
1353 */
1354 if (type == REDRAW_TOP)
1355 {
1356 j = 0;
1357 for (i = 0; i < wp->w_lines_valid; ++i)
1358 {
1359 j += wp->w_lines[i].wl_size;
1360 if (j >= wp->w_upd_rows)
1361 {
1362 top_end = j;
1363 break;
1364 }
1365 }
1366 if (top_end == 0)
1367 /* not found (cannot happen?): redraw everything */
1368 type = NOT_VALID;
1369 else
1370 /* top area defined, the rest is VALID */
1371 type = VALID;
1372 }
1373
Bram Moolenaar367329b2007-08-30 11:53:22 +00001374 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001375 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1376 * non-zero and thus not FALSE) will indicate that screenclear() was not
1377 * called. */
1378 if (screen_cleared)
1379 screen_cleared = MAYBE;
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 /*
1382 * If there are no changes on the screen that require a complete redraw,
1383 * handle three cases:
1384 * 1: we are off the top of the screen by a few lines: scroll down
1385 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1386 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1387 * w_lines[] that needs updating.
1388 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001389 if ((type == VALID || type == SOME_VALID
1390 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#ifdef FEAT_DIFF
1392 && !wp->w_botfill && !wp->w_old_botfill
1393#endif
1394 )
1395 {
1396 if (mod_top != 0 && wp->w_topline == mod_top)
1397 {
1398 /*
1399 * w_topline is the first changed line, the scrolling will be done
1400 * further down.
1401 */
1402 }
1403 else if (wp->w_lines[0].wl_valid
1404 && (wp->w_topline < wp->w_lines[0].wl_lnum
1405#ifdef FEAT_DIFF
1406 || (wp->w_topline == wp->w_lines[0].wl_lnum
1407 && wp->w_topfill > wp->w_old_topfill)
1408#endif
1409 ))
1410 {
1411 /*
1412 * New topline is above old topline: May scroll down.
1413 */
1414#ifdef FEAT_FOLDING
1415 if (hasAnyFolding(wp))
1416 {
1417 linenr_T ln;
1418
1419 /* count the number of lines we are off, counting a sequence
1420 * of folded lines as one */
1421 j = 0;
1422 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1423 {
1424 ++j;
1425 if (j >= wp->w_height - 2)
1426 break;
1427 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1428 }
1429 }
1430 else
1431#endif
1432 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1433 if (j < wp->w_height - 2) /* not too far off */
1434 {
1435 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1436#ifdef FEAT_DIFF
1437 /* insert extra lines for previously invisible filler lines */
1438 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1439 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1440 - wp->w_old_topfill;
1441#endif
1442 if (i < wp->w_height - 2) /* less than a screen off */
1443 {
1444 /*
1445 * Try to insert the correct number of lines.
1446 * If not the last window, delete the lines at the bottom.
1447 * win_ins_lines may fail when the terminal can't do it.
1448 */
1449 if (i > 0)
1450 check_for_delay(FALSE);
1451 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1452 {
1453 if (wp->w_lines_valid != 0)
1454 {
1455 /* Need to update rows that are new, stop at the
1456 * first one that scrolled down. */
1457 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
1460 /* Move the entries that were scrolled, disable
1461 * the entries for the lines to be redrawn. */
1462 if ((wp->w_lines_valid += j) > wp->w_height)
1463 wp->w_lines_valid = wp->w_height;
1464 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1465 wp->w_lines[idx] = wp->w_lines[idx - j];
1466 while (idx >= 0)
1467 wp->w_lines[idx--].wl_valid = FALSE;
1468 }
1469 }
1470 else
1471 mid_start = 0; /* redraw all lines */
1472 }
1473 else
1474 mid_start = 0; /* redraw all lines */
1475 }
1476 else
1477 mid_start = 0; /* redraw all lines */
1478 }
1479 else
1480 {
1481 /*
1482 * New topline is at or below old topline: May scroll up.
1483 * When topline didn't change, find first entry in w_lines[] that
1484 * needs updating.
1485 */
1486
1487 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1488 j = -1;
1489 row = 0;
1490 for (i = 0; i < wp->w_lines_valid; i++)
1491 {
1492 if (wp->w_lines[i].wl_valid
1493 && wp->w_lines[i].wl_lnum == wp->w_topline)
1494 {
1495 j = i;
1496 break;
1497 }
1498 row += wp->w_lines[i].wl_size;
1499 }
1500 if (j == -1)
1501 {
1502 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1503 * lines */
1504 mid_start = 0;
1505 }
1506 else
1507 {
1508 /*
1509 * Try to delete the correct number of lines.
1510 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1511 */
1512#ifdef FEAT_DIFF
1513 /* If the topline didn't change, delete old filler lines,
1514 * otherwise delete filler lines of the new topline... */
1515 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1516 row += wp->w_old_topfill;
1517 else
1518 row += diff_check_fill(wp, wp->w_topline);
1519 /* ... but don't delete new filler lines. */
1520 row -= wp->w_topfill;
1521#endif
1522 if (row > 0)
1523 {
1524 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001525 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1526 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 bot_start = wp->w_height - row;
1528 else
1529 mid_start = 0; /* redraw all lines */
1530 }
1531 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1532 {
1533 /*
1534 * Skip the lines (below the deleted lines) that are still
1535 * valid and don't need redrawing. Copy their info
1536 * upwards, to compensate for the deleted lines. Set
1537 * bot_start to the first row that needs redrawing.
1538 */
1539 bot_start = 0;
1540 idx = 0;
1541 for (;;)
1542 {
1543 wp->w_lines[idx] = wp->w_lines[j];
1544 /* stop at line that didn't fit, unless it is still
1545 * valid (no lines deleted) */
1546 if (row > 0 && bot_start + row
1547 + (int)wp->w_lines[j].wl_size > wp->w_height)
1548 {
1549 wp->w_lines_valid = idx + 1;
1550 break;
1551 }
1552 bot_start += wp->w_lines[idx++].wl_size;
1553
1554 /* stop at the last valid entry in w_lines[].wl_size */
1555 if (++j >= wp->w_lines_valid)
1556 {
1557 wp->w_lines_valid = idx;
1558 break;
1559 }
1560 }
1561#ifdef FEAT_DIFF
1562 /* Correct the first entry for filler lines at the top
1563 * when it won't get updated below. */
1564 if (wp->w_p_diff && bot_start > 0)
1565 wp->w_lines[0].wl_size =
1566 plines_win_nofill(wp, wp->w_topline, TRUE)
1567 + wp->w_topfill;
1568#endif
1569 }
1570 }
1571 }
1572
1573 /* When starting redraw in the first line, redraw all lines. When
1574 * there is only one window it's probably faster to clear the screen
1575 * first. */
1576 if (mid_start == 0)
1577 {
1578 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001579 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001580 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001581 /* Clear the screen when it was not done by win_del_lines() or
1582 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1583 * then. */
1584 if (screen_cleared != TRUE)
1585 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001586 /* The screen was cleared, redraw the tab pages line. */
1587 if (redraw_tabline)
1588 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001591
1592 /* When win_del_lines() or win_ins_lines() caused the screen to be
1593 * cleared (only happens for the first window) or when screenclear()
1594 * was called directly above, "must_redraw" will have been set to
1595 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1596 if (screen_cleared == TRUE)
1597 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
1601 /* Not VALID or INVERTED: redraw all lines. */
1602 mid_start = 0;
1603 mid_end = wp->w_height;
1604 }
1605
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001606 if (type == SOME_VALID)
1607 {
1608 /* SOME_VALID: redraw all lines. */
1609 mid_start = 0;
1610 mid_end = wp->w_height;
1611 type = NOT_VALID;
1612 }
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 /* check if we are updating or removing the inverted part */
1615 if ((VIsual_active && buf == curwin->w_buffer)
1616 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1617 {
1618 linenr_T from, to;
1619
1620 if (VIsual_active)
1621 {
1622 if (VIsual_active
1623 && (VIsual_mode != wp->w_old_visual_mode
1624 || type == INVERTED_ALL))
1625 {
1626 /*
1627 * If the type of Visual selection changed, redraw the whole
1628 * selection. Also when the ownership of the X selection is
1629 * gained or lost.
1630 */
1631 if (curwin->w_cursor.lnum < VIsual.lnum)
1632 {
1633 from = curwin->w_cursor.lnum;
1634 to = VIsual.lnum;
1635 }
1636 else
1637 {
1638 from = VIsual.lnum;
1639 to = curwin->w_cursor.lnum;
1640 }
1641 /* redraw more when the cursor moved as well */
1642 if (wp->w_old_cursor_lnum < from)
1643 from = wp->w_old_cursor_lnum;
1644 if (wp->w_old_cursor_lnum > to)
1645 to = wp->w_old_cursor_lnum;
1646 if (wp->w_old_visual_lnum < from)
1647 from = wp->w_old_visual_lnum;
1648 if (wp->w_old_visual_lnum > to)
1649 to = wp->w_old_visual_lnum;
1650 }
1651 else
1652 {
1653 /*
1654 * Find the line numbers that need to be updated: The lines
1655 * between the old cursor position and the current cursor
1656 * position. Also check if the Visual position changed.
1657 */
1658 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1659 {
1660 from = curwin->w_cursor.lnum;
1661 to = wp->w_old_cursor_lnum;
1662 }
1663 else
1664 {
1665 from = wp->w_old_cursor_lnum;
1666 to = curwin->w_cursor.lnum;
1667 if (from == 0) /* Visual mode just started */
1668 from = to;
1669 }
1670
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001671 if (VIsual.lnum != wp->w_old_visual_lnum
1672 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 if (wp->w_old_visual_lnum < from
1675 && wp->w_old_visual_lnum != 0)
1676 from = wp->w_old_visual_lnum;
1677 if (wp->w_old_visual_lnum > to)
1678 to = wp->w_old_visual_lnum;
1679 if (VIsual.lnum < from)
1680 from = VIsual.lnum;
1681 if (VIsual.lnum > to)
1682 to = VIsual.lnum;
1683 }
1684 }
1685
1686 /*
1687 * If in block mode and changed column or curwin->w_curswant:
1688 * update all lines.
1689 * First compute the actual start and end column.
1690 */
1691 if (VIsual_mode == Ctrl_V)
1692 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001693 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001694#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001695 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
Bram Moolenaar404406a2014-10-09 13:24:43 +02001697 if (curwin->w_p_lbr)
1698 ve_flags = VE_ALL;
1699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001701#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001702 ve_flags = save_ve_flags;
1703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 ++toc;
1705 if (curwin->w_curswant == MAXCOL)
1706 toc = MAXCOL;
1707
1708 if (fromc != wp->w_old_cursor_fcol
1709 || toc != wp->w_old_cursor_lcol)
1710 {
1711 if (from > VIsual.lnum)
1712 from = VIsual.lnum;
1713 if (to < VIsual.lnum)
1714 to = VIsual.lnum;
1715 }
1716 wp->w_old_cursor_fcol = fromc;
1717 wp->w_old_cursor_lcol = toc;
1718 }
1719 }
1720 else
1721 {
1722 /* Use the line numbers of the old Visual area. */
1723 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1724 {
1725 from = wp->w_old_cursor_lnum;
1726 to = wp->w_old_visual_lnum;
1727 }
1728 else
1729 {
1730 from = wp->w_old_visual_lnum;
1731 to = wp->w_old_cursor_lnum;
1732 }
1733 }
1734
1735 /*
1736 * There is no need to update lines above the top of the window.
1737 */
1738 if (from < wp->w_topline)
1739 from = wp->w_topline;
1740
1741 /*
1742 * If we know the value of w_botline, use it to restrict the update to
1743 * the lines that are visible in the window.
1744 */
1745 if (wp->w_valid & VALID_BOTLINE)
1746 {
1747 if (from >= wp->w_botline)
1748 from = wp->w_botline - 1;
1749 if (to >= wp->w_botline)
1750 to = wp->w_botline - 1;
1751 }
1752
1753 /*
1754 * Find the minimal part to be updated.
1755 * Watch out for scrolling that made entries in w_lines[] invalid.
1756 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1757 * top_end; need to redraw from top_end to the "to" line.
1758 * A middle mouse click with a Visual selection may change the text
1759 * above the Visual area and reset wl_valid, do count these for
1760 * mid_end (in srow).
1761 */
1762 if (mid_start > 0)
1763 {
1764 lnum = wp->w_topline;
1765 idx = 0;
1766 srow = 0;
1767 if (scrolled_down)
1768 mid_start = top_end;
1769 else
1770 mid_start = 0;
1771 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1772 {
1773 if (wp->w_lines[idx].wl_valid)
1774 mid_start += wp->w_lines[idx].wl_size;
1775 else if (!scrolled_down)
1776 srow += wp->w_lines[idx].wl_size;
1777 ++idx;
1778# ifdef FEAT_FOLDING
1779 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1780 lnum = wp->w_lines[idx].wl_lnum;
1781 else
1782# endif
1783 ++lnum;
1784 }
1785 srow += mid_start;
1786 mid_end = wp->w_height;
1787 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1788 {
1789 if (wp->w_lines[idx].wl_valid
1790 && wp->w_lines[idx].wl_lnum >= to + 1)
1791 {
1792 /* Only update until first row of this line */
1793 mid_end = srow;
1794 break;
1795 }
1796 srow += wp->w_lines[idx].wl_size;
1797 }
1798 }
1799 }
1800
1801 if (VIsual_active && buf == curwin->w_buffer)
1802 {
1803 wp->w_old_visual_mode = VIsual_mode;
1804 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1805 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001806 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 wp->w_old_curswant = curwin->w_curswant;
1808 }
1809 else
1810 {
1811 wp->w_old_visual_mode = 0;
1812 wp->w_old_cursor_lnum = 0;
1813 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001814 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1818 /* reset got_int, otherwise regexp won't work */
1819 save_got_int = got_int;
1820 got_int = 0;
1821#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001822#ifdef SYN_TIME_LIMIT
1823 /* Set the time limit to 'redrawtime'. */
1824 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001825 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#ifdef FEAT_FOLDING
1828 win_foldinfo.fi_level = 0;
1829#endif
1830
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001831#ifdef FEAT_MENU
1832 /*
1833 * Draw the window toolbar, if there is one.
1834 * TODO: only when needed.
1835 */
1836 if (winbar_height(wp) > 0)
1837 redraw_win_toolbar(wp);
1838#endif
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 /*
1841 * Update all the window rows.
1842 */
1843 idx = 0; /* first entry in w_lines[].wl_size */
1844 row = 0;
1845 srow = 0;
1846 lnum = wp->w_topline; /* first line shown in window */
1847 for (;;)
1848 {
1849 /* stop updating when reached the end of the window (check for _past_
1850 * the end of the window is at the end of the loop) */
1851 if (row == wp->w_height)
1852 {
1853 didline = TRUE;
1854 break;
1855 }
1856
1857 /* stop updating when hit the end of the file */
1858 if (lnum > buf->b_ml.ml_line_count)
1859 {
1860 eof = TRUE;
1861 break;
1862 }
1863
1864 /* Remember the starting row of the line that is going to be dealt
1865 * with. It is used further down when the line doesn't fit. */
1866 srow = row;
1867
1868 /*
1869 * Update a line when it is in an area that needs updating, when it
1870 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001871 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 * win_del_lines(), check if the current line includes it.
1873 * When syntax folding is being used, the saved syntax states will
1874 * already have been updated, we can't see where the syntax state is
1875 * the same again, just update until the end of the window.
1876 */
1877 if (row < top_end
1878 || (row >= mid_start && row < mid_end)
1879#ifdef FEAT_SEARCH_EXTRA
1880 || top_to_mod
1881#endif
1882 || idx >= wp->w_lines_valid
1883 || (row + wp->w_lines[idx].wl_size > bot_start)
1884 || (mod_top != 0
1885 && (lnum == mod_top
1886 || (lnum >= mod_top
1887 && (lnum < mod_bot
1888#ifdef FEAT_SYN_HL
1889 || did_update == DID_FOLD
1890 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001891 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 && (
1893# ifdef FEAT_FOLDING
1894 (foldmethodIsSyntax(wp)
1895 && hasAnyFolding(wp)) ||
1896# endif
1897 syntax_check_changed(lnum)))
1898#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001899#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001900 /* match in fixed position might need redraw
1901 * if lines were inserted or deleted */
1902 || (wp->w_match_head != NULL
1903 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 )))))
1906 {
1907#ifdef FEAT_SEARCH_EXTRA
1908 if (lnum == mod_top)
1909 top_to_mod = FALSE;
1910#endif
1911
1912 /*
1913 * When at start of changed lines: May scroll following lines
1914 * up or down to minimize redrawing.
1915 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001916 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 */
1918 if (lnum == mod_top
1919 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001920 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
1922 int old_rows = 0;
1923 int new_rows = 0;
1924 int xtra_rows;
1925 linenr_T l;
1926
1927 /* Count the old number of window rows, using w_lines[], which
1928 * should still contain the sizes for the lines as they are
1929 * currently displayed. */
1930 for (i = idx; i < wp->w_lines_valid; ++i)
1931 {
1932 /* Only valid lines have a meaningful wl_lnum. Invalid
1933 * lines are part of the changed area. */
1934 if (wp->w_lines[i].wl_valid
1935 && wp->w_lines[i].wl_lnum == mod_bot)
1936 break;
1937 old_rows += wp->w_lines[i].wl_size;
1938#ifdef FEAT_FOLDING
1939 if (wp->w_lines[i].wl_valid
1940 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1941 {
1942 /* Must have found the last valid entry above mod_bot.
1943 * Add following invalid entries. */
1944 ++i;
1945 while (i < wp->w_lines_valid
1946 && !wp->w_lines[i].wl_valid)
1947 old_rows += wp->w_lines[i++].wl_size;
1948 break;
1949 }
1950#endif
1951 }
1952
1953 if (i >= wp->w_lines_valid)
1954 {
1955 /* We can't find a valid line below the changed lines,
1956 * need to redraw until the end of the window.
1957 * Inserting/deleting lines has no use. */
1958 bot_start = 0;
1959 }
1960 else
1961 {
1962 /* Able to count old number of rows: Count new window
1963 * rows, and may insert/delete lines */
1964 j = idx;
1965 for (l = lnum; l < mod_bot; ++l)
1966 {
1967#ifdef FEAT_FOLDING
1968 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1969 ++new_rows;
1970 else
1971#endif
1972#ifdef FEAT_DIFF
1973 if (l == wp->w_topline)
1974 new_rows += plines_win_nofill(wp, l, TRUE)
1975 + wp->w_topfill;
1976 else
1977#endif
1978 new_rows += plines_win(wp, l, TRUE);
1979 ++j;
1980 if (new_rows > wp->w_height - row - 2)
1981 {
1982 /* it's getting too much, must redraw the rest */
1983 new_rows = 9999;
1984 break;
1985 }
1986 }
1987 xtra_rows = new_rows - old_rows;
1988 if (xtra_rows < 0)
1989 {
1990 /* May scroll text up. If there is not enough
1991 * remaining text or scrolling fails, must redraw the
1992 * rest. If scrolling works, must redraw the text
1993 * below the scrolled text. */
1994 if (row - xtra_rows >= wp->w_height - 2)
1995 mod_bot = MAXLNUM;
1996 else
1997 {
1998 check_for_delay(FALSE);
1999 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002000 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 mod_bot = MAXLNUM;
2002 else
2003 bot_start = wp->w_height + xtra_rows;
2004 }
2005 }
2006 else if (xtra_rows > 0)
2007 {
2008 /* May scroll text down. If there is not enough
2009 * remaining text of scrolling fails, must redraw the
2010 * rest. */
2011 if (row + xtra_rows >= wp->w_height - 2)
2012 mod_bot = MAXLNUM;
2013 else
2014 {
2015 check_for_delay(FALSE);
2016 if (win_ins_lines(wp, row + old_rows,
2017 xtra_rows, FALSE, FALSE) == FAIL)
2018 mod_bot = MAXLNUM;
2019 else if (top_end > row + old_rows)
2020 /* Scrolled the part at the top that requires
2021 * updating down. */
2022 top_end += xtra_rows;
2023 }
2024 }
2025
2026 /* When not updating the rest, may need to move w_lines[]
2027 * entries. */
2028 if (mod_bot != MAXLNUM && i != j)
2029 {
2030 if (j < i)
2031 {
2032 int x = row + new_rows;
2033
2034 /* move entries in w_lines[] upwards */
2035 for (;;)
2036 {
2037 /* stop at last valid entry in w_lines[] */
2038 if (i >= wp->w_lines_valid)
2039 {
2040 wp->w_lines_valid = j;
2041 break;
2042 }
2043 wp->w_lines[j] = wp->w_lines[i];
2044 /* stop at a line that won't fit */
2045 if (x + (int)wp->w_lines[j].wl_size
2046 > wp->w_height)
2047 {
2048 wp->w_lines_valid = j + 1;
2049 break;
2050 }
2051 x += wp->w_lines[j++].wl_size;
2052 ++i;
2053 }
2054 if (bot_start > x)
2055 bot_start = x;
2056 }
2057 else /* j > i */
2058 {
2059 /* move entries in w_lines[] downwards */
2060 j -= i;
2061 wp->w_lines_valid += j;
2062 if (wp->w_lines_valid > wp->w_height)
2063 wp->w_lines_valid = wp->w_height;
2064 for (i = wp->w_lines_valid; i - j >= idx; --i)
2065 wp->w_lines[i] = wp->w_lines[i - j];
2066
2067 /* The w_lines[] entries for inserted lines are
2068 * now invalid, but wl_size may be used above.
2069 * Reset to zero. */
2070 while (i >= idx)
2071 {
2072 wp->w_lines[i].wl_size = 0;
2073 wp->w_lines[i--].wl_valid = FALSE;
2074 }
2075 }
2076 }
2077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079
2080#ifdef FEAT_FOLDING
2081 /*
2082 * When lines are folded, display one line for all of them.
2083 * Otherwise, display normally (can be several display lines when
2084 * 'wrap' is on).
2085 */
2086 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2087 if (fold_count != 0)
2088 {
2089 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2090 ++row;
2091 --fold_count;
2092 wp->w_lines[idx].wl_folded = TRUE;
2093 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2094# ifdef FEAT_SYN_HL
2095 did_update = DID_FOLD;
2096# endif
2097 }
2098 else
2099#endif
2100 if (idx < wp->w_lines_valid
2101 && wp->w_lines[idx].wl_valid
2102 && wp->w_lines[idx].wl_lnum == lnum
2103 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002104 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 && srow + wp->w_lines[idx].wl_size > wp->w_height
2106#ifdef FEAT_DIFF
2107 && diff_check_fill(wp, lnum) == 0
2108#endif
2109 )
2110 {
2111 /* This line is not going to fit. Don't draw anything here,
2112 * will draw "@ " lines below. */
2113 row = wp->w_height + 1;
2114 }
2115 else
2116 {
2117#ifdef FEAT_SEARCH_EXTRA
2118 prepare_search_hl(wp, lnum);
2119#endif
2120#ifdef FEAT_SYN_HL
2121 /* Let the syntax stuff know we skipped a few lines. */
2122 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002123 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 syntax_end_parsing(syntax_last_parsed + 1);
2125#endif
2126
2127 /*
2128 * Display one line.
2129 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002130 row = win_line(wp, lnum, srow, wp->w_height,
2131 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133#ifdef FEAT_FOLDING
2134 wp->w_lines[idx].wl_folded = FALSE;
2135 wp->w_lines[idx].wl_lastlnum = lnum;
2136#endif
2137#ifdef FEAT_SYN_HL
2138 did_update = DID_LINE;
2139 syntax_last_parsed = lnum;
2140#endif
2141 }
2142
2143 wp->w_lines[idx].wl_lnum = lnum;
2144 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002145
2146 /* Past end of the window or end of the screen. Note that after
2147 * resizing wp->w_height may be end up too big. That's a problem
2148 * elsewhere, but prevent a crash here. */
2149 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002152 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2154 ++idx;
2155 break;
2156 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002157 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 wp->w_lines[idx].wl_size = row - srow;
2159 ++idx;
2160#ifdef FEAT_FOLDING
2161 lnum += fold_count + 1;
2162#else
2163 ++lnum;
2164#endif
2165 }
2166 else
2167 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002168 if (wp->w_p_rnu)
2169 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002170#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002171 // 'relativenumber' set: The text doesn't need to be drawn, but
2172 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002173 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2174 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002175 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002176 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002177#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002178 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002179 }
2180
2181 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 row += wp->w_lines[idx++].wl_size;
2183 if (row > wp->w_height) /* past end of screen */
2184 break;
2185#ifdef FEAT_FOLDING
2186 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2187#else
2188 ++lnum;
2189#endif
2190#ifdef FEAT_SYN_HL
2191 did_update = DID_NONE;
2192#endif
2193 }
2194
2195 if (lnum > buf->b_ml.ml_line_count)
2196 {
2197 eof = TRUE;
2198 break;
2199 }
2200 }
2201 /*
2202 * End of loop over all window lines.
2203 */
2204
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002205#ifdef FEAT_VTP
2206 /* Rewrite the character at the end of the screen line. */
2207 if (use_vtp())
2208 {
2209 int i;
2210
2211 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002212 if (enc_utf8)
2213 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2214 LineOffset[i] + screen_Columns) > 1)
2215 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2216 else
2217 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2218 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002219 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2220 }
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 if (idx > wp->w_lines_valid)
2224 wp->w_lines_valid = idx;
2225
2226#ifdef FEAT_SYN_HL
2227 /*
2228 * Let the syntax stuff know we stop parsing here.
2229 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002230 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 syntax_end_parsing(syntax_last_parsed + 1);
2232#endif
2233
2234 /*
2235 * If we didn't hit the end of the file, and we didn't finish the last
2236 * line we were working on, then the line didn't fit.
2237 */
2238 wp->w_empty_rows = 0;
2239#ifdef FEAT_DIFF
2240 wp->w_filler_rows = 0;
2241#endif
2242 if (!eof && !didline)
2243 {
2244 if (lnum == wp->w_topline)
2245 {
2246 /*
2247 * Single line that does not fit!
2248 * Don't overwrite it, it can be edited.
2249 */
2250 wp->w_botline = lnum + 1;
2251 }
2252#ifdef FEAT_DIFF
2253 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2254 {
2255 /* Window ends in filler lines. */
2256 wp->w_botline = lnum;
2257 wp->w_filler_rows = wp->w_height - srow;
2258 }
2259#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002260 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2261 {
2262 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2263
2264 /*
2265 * Last line isn't finished: Display "@@@" in the last screen line.
2266 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002267 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002268 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002269 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002270 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002271 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002272 set_empty_rows(wp, srow);
2273 wp->w_botline = lnum;
2274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2276 {
2277 /*
2278 * Last line isn't finished: Display "@@@" at the end.
2279 */
2280 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2281 W_WINROW(wp) + wp->w_height,
2282 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002283 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 set_empty_rows(wp, srow);
2285 wp->w_botline = lnum;
2286 }
2287 else
2288 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002289 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 wp->w_botline = lnum;
2291 }
2292 }
2293 else
2294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 if (eof) /* we hit the end of the file */
2297 {
2298 wp->w_botline = buf->b_ml.ml_line_count + 1;
2299#ifdef FEAT_DIFF
2300 j = diff_check_fill(wp, wp->w_botline);
2301 if (j > 0 && !wp->w_botfill)
2302 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002303 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (char2cells(fill_diff) > 1)
2305 i = '-';
2306 else
2307 i = fill_diff;
2308 if (row + j > wp->w_height)
2309 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002310 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 row += j;
2312 }
2313#endif
2314 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002315 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 wp->w_botline = lnum;
2317
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002318 // Make sure the rest of the screen is blank
2319 // put '~'s on rows that aren't part of the file.
2320 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 }
2322
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002323#ifdef SYN_TIME_LIMIT
2324 syn_set_timeout(NULL);
2325#endif
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 /* Reset the type of redrawing required, the window has been updated. */
2328 wp->w_redr_type = 0;
2329#ifdef FEAT_DIFF
2330 wp->w_old_topfill = wp->w_topfill;
2331 wp->w_old_botfill = wp->w_botfill;
2332#endif
2333
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002334 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
2336 /*
2337 * There is a trick with w_botline. If we invalidate it on each
2338 * change that might modify it, this will cause a lot of expensive
2339 * calls to plines() in update_topline() each time. Therefore the
2340 * value of w_botline is often approximated, and this value is used to
2341 * compute the value of w_topline. If the value of w_botline was
2342 * wrong, check that the value of w_topline is correct (cursor is on
2343 * the visible part of the text). If it's not, we need to redraw
2344 * again. Mostly this just means scrolling up a few lines, so it
2345 * doesn't look too bad. Only do this for the current window (where
2346 * changes are relevant).
2347 */
2348 wp->w_valid |= VALID_BOTLINE;
2349 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2350 {
2351 recursive = TRUE;
2352 curwin->w_valid &= ~VALID_TOPLINE;
2353 update_topline(); /* may invalidate w_botline again */
2354 if (must_redraw != 0)
2355 {
2356 /* Don't update for changes in buffer again. */
2357 i = curbuf->b_mod_set;
2358 curbuf->b_mod_set = FALSE;
2359 win_update(curwin);
2360 must_redraw = 0;
2361 curbuf->b_mod_set = i;
2362 }
2363 recursive = FALSE;
2364 }
2365 }
2366
2367#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2368 /* restore got_int, unless CTRL-C was hit while redrawing */
2369 if (!got_int)
2370 got_int = save_got_int;
2371#endif
2372}
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002375 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2376 * Return the new offset.
2377 */
2378 static int
2379screen_fill_end(
2380 win_T *wp,
2381 int c1,
2382 int c2,
2383 int off,
2384 int width,
2385 int row,
2386 int endrow,
2387 int attr)
2388{
2389 int nn = off + width;
2390
2391 if (nn > wp->w_width)
2392 nn = wp->w_width;
2393#ifdef FEAT_RIGHTLEFT
2394 if (wp->w_p_rl)
2395 {
2396 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2397 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2398 c1, c2, attr);
2399 }
2400 else
2401#endif
2402 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2403 wp->w_wincol + off, (int)wp->w_wincol + nn,
2404 c1, c2, attr);
2405 return nn;
2406}
2407
2408/*
2409 * Clear lines near the end the window and mark the unused lines with "c1".
2410 * use "c2" as the filler character.
2411 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
2413 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002414win_draw_end(
2415 win_T *wp,
2416 int c1,
2417 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002418 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002419 int row,
2420 int endrow,
2421 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 int n = 0;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002424
2425 if (draw_margin)
2426 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002427#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002428 int fdc = compute_foldcolumn(wp, 0);
2429
2430 if (fdc > 0)
2431 // draw the fold column
2432 n = screen_fill_end(wp, ' ', ' ', n, fdc,
2433 row, endrow, HL_ATTR(HLF_FC));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002434#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002435#ifdef FEAT_SIGNS
2436 if (signcolumn_on(wp))
2437 // draw the sign column
2438 n = screen_fill_end(wp, ' ', ' ', n, 2,
2439 row, endrow, HL_ATTR(HLF_SC));
2440#endif
2441 if ((wp->w_p_nu || wp->w_p_rnu)
2442 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2443 // draw the number column
2444 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
2445 row, endrow, HL_ATTR(HLF_N));
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448#ifdef FEAT_RIGHTLEFT
2449 if (wp->w_p_rl)
2450 {
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 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002453 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002455 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002456 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458 else
2459#endif
2460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002462 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002463 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 set_empty_rows(wp, row);
2467}
2468
Bram Moolenaar1a384422010-07-14 19:53:30 +02002469#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002470/*
2471 * Advance **color_cols and return TRUE when there are columns to draw.
2472 */
2473 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002474advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002475{
2476 while (**color_cols >= 0 && vcol > **color_cols)
2477 ++*color_cols;
2478 return (**color_cols >= 0);
2479}
2480#endif
2481
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002482#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2483/*
2484 * Copy "text" to ScreenLines using "attr".
2485 * Returns the next screen column.
2486 */
2487 static int
2488text_to_screenline(win_T *wp, char_u *text, int col)
2489{
2490 int off = (int)(current_ScreenLine - ScreenLines);
2491
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002492 if (has_mbyte)
2493 {
2494 int cells;
2495 int u8c, u8cc[MAX_MCO];
2496 int i;
2497 int idx;
2498 int c_len;
2499 char_u *p;
2500# ifdef FEAT_ARABIC
2501 int prev_c = 0; /* previous Arabic character */
2502 int prev_c1 = 0; /* first composing char for prev_c */
2503# endif
2504
2505# ifdef FEAT_RIGHTLEFT
2506 if (wp->w_p_rl)
2507 idx = off;
2508 else
2509# endif
2510 idx = off + col;
2511
2512 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2513 for (p = text; *p != NUL; )
2514 {
2515 cells = (*mb_ptr2cells)(p);
2516 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002517 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002518# ifdef FEAT_RIGHTLEFT
2519 - (wp->w_p_rl ? col : 0)
2520# endif
2521 )
2522 break;
2523 ScreenLines[idx] = *p;
2524 if (enc_utf8)
2525 {
2526 u8c = utfc_ptr2char(p, u8cc);
2527 if (*p < 0x80 && u8cc[0] == 0)
2528 {
2529 ScreenLinesUC[idx] = 0;
2530#ifdef FEAT_ARABIC
2531 prev_c = u8c;
2532#endif
2533 }
2534 else
2535 {
2536#ifdef FEAT_ARABIC
2537 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2538 {
2539 /* Do Arabic shaping. */
2540 int pc, pc1, nc;
2541 int pcc[MAX_MCO];
2542 int firstbyte = *p;
2543
2544 /* The idea of what is the previous and next
2545 * character depends on 'rightleft'. */
2546 if (wp->w_p_rl)
2547 {
2548 pc = prev_c;
2549 pc1 = prev_c1;
2550 nc = utf_ptr2char(p + c_len);
2551 prev_c1 = u8cc[0];
2552 }
2553 else
2554 {
2555 pc = utfc_ptr2char(p + c_len, pcc);
2556 nc = prev_c;
2557 pc1 = pcc[0];
2558 }
2559 prev_c = u8c;
2560
2561 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2562 pc, pc1, nc);
2563 ScreenLines[idx] = firstbyte;
2564 }
2565 else
2566 prev_c = u8c;
2567#endif
2568 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002569 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002570 for (i = 0; i < Screen_mco; ++i)
2571 {
2572 ScreenLinesC[i][idx] = u8cc[i];
2573 if (u8cc[i] == 0)
2574 break;
2575 }
2576 }
2577 if (cells > 1)
2578 ScreenLines[idx + 1] = 0;
2579 }
2580 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2581 /* double-byte single width character */
2582 ScreenLines2[idx] = p[1];
2583 else if (cells > 1)
2584 /* double-width character */
2585 ScreenLines[idx + 1] = p[1];
2586 col += cells;
2587 idx += cells;
2588 p += c_len;
2589 }
2590 }
2591 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002592 {
2593 int len = (int)STRLEN(text);
2594
Bram Moolenaar02631462017-09-22 15:20:32 +02002595 if (len > wp->w_width - col)
2596 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002597 if (len > 0)
2598 {
2599#ifdef FEAT_RIGHTLEFT
2600 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002601 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002602 else
2603#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002604 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002605 col += len;
2606 }
2607 }
2608 return col;
2609}
2610#endif
2611
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612#ifdef FEAT_FOLDING
2613/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002614 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2615 * space is available for window "wp", minus "col".
2616 */
2617 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002618compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002619{
2620 int fdc = wp->w_p_fdc;
2621 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002622 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002623
2624 if (fdc > wwidth - (col + wmw))
2625 fdc = wwidth - (col + wmw);
2626 return fdc;
2627}
2628
2629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 * Display one folded line.
2631 */
2632 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002633fold_line(
2634 win_T *wp,
2635 long fold_count,
2636 foldinfo_T *foldinfo,
2637 linenr_T lnum,
2638 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002640 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 pos_T *top, *bot;
2642 linenr_T lnume = lnum + fold_count - 1;
2643 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002644 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int col;
2647 int txtcol;
2648 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002649 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /* Build the fold line:
2652 * 1. Add the cmdwin_type for the command-line window
2653 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002654 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 * 4. Compose the text
2656 * 5. Add the text
2657 * 6. set highlighting for the Visual area an other text
2658 */
2659 col = 0;
2660
2661 /*
2662 * 1. Add the cmdwin_type for the command-line window
2663 * Ignores 'rightleft', this window is never right-left.
2664 */
2665#ifdef FEAT_CMDWIN
2666 if (cmdwin_type != 0 && wp == curwin)
2667 {
2668 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002669 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 if (enc_utf8)
2671 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 ++col;
2673 }
2674#endif
2675
2676 /*
2677 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002678 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002680 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (fdc > 0)
2682 {
2683 fill_foldcolumn(buf, wp, TRUE, lnum);
2684#ifdef FEAT_RIGHTLEFT
2685 if (wp->w_p_rl)
2686 {
2687 int i;
2688
Bram Moolenaar02631462017-09-22 15:20:32 +02002689 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002690 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 /* reverse the fold column */
2692 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002693 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695 else
2696#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002697 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 col += fdc;
2699 }
2700
2701#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002702# define RL_MEMSET(p, v, l) \
2703 do { \
2704 if (wp->w_p_rl) \
2705 for (ri = 0; ri < l; ++ri) \
2706 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2707 else \
2708 for (ri = 0; ri < l; ++ri) \
2709 ScreenAttrs[off + (p) + ri] = v; \
2710 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002712# define RL_MEMSET(p, v, l) \
2713 do { \
2714 for (ri = 0; ri < l; ++ri) \
2715 ScreenAttrs[off + (p) + ri] = v; \
2716 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717#endif
2718
Bram Moolenaar64486672010-05-16 15:46:46 +02002719 /* Set all attributes of the 'number' or 'relativenumber' column and the
2720 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002721 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723#ifdef FEAT_SIGNS
2724 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002725 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002727 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (len > 0)
2729 {
2730 if (len > 2)
2731 len = 2;
2732# ifdef FEAT_RIGHTLEFT
2733 if (wp->w_p_rl)
2734 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002735 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002736 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else
2738# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002739 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 col += len;
2741 }
2742 }
2743#endif
2744
2745 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002746 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002748 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002750 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (len > 0)
2752 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002753 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002754 long num;
2755 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002756
2757 if (len > w + 1)
2758 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002759
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002760 if (wp->w_p_nu && !wp->w_p_rnu)
2761 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002762 num = (long)lnum;
2763 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002764 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002765 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002766 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002767 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002768 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002769 /* 'number' + 'relativenumber': cursor line shows absolute
2770 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002771 num = lnum;
2772 fmt = "%-*ld ";
2773 }
2774 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002775
Bram Moolenaar700e7342013-01-30 12:31:36 +01002776 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777#ifdef FEAT_RIGHTLEFT
2778 if (wp->w_p_rl)
2779 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002780 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002781 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else
2783#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002784 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 col += len;
2786 }
2787 }
2788
2789 /*
2790 * 4. Compose the folded-line string with 'foldtext', if set.
2791 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002792 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 txtcol = col; /* remember where text starts */
2795
2796 /*
2797 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2798 * Right-left text is put in columns 0 - number-col, normal text is put
2799 * in columns number-col - window-width.
2800 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002801 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
2803 /* Fill the rest of the line with the fold filler */
2804#ifdef FEAT_RIGHTLEFT
2805 if (wp->w_p_rl)
2806 col -= txtcol;
2807#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002808 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#ifdef FEAT_RIGHTLEFT
2810 - (wp->w_p_rl ? txtcol : 0)
2811#endif
2812 )
2813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (enc_utf8)
2815 {
2816 if (fill_fold >= 0x80)
2817 {
2818 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002819 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002820 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002825 ScreenLines[off + col] = fill_fold;
2826 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002827 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002829 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002830 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832
2833 if (text != buf)
2834 vim_free(text);
2835
2836 /*
2837 * 6. set highlighting for the Visual area an other text.
2838 * If all folded lines are in the Visual area, highlight the line.
2839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2841 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002842 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
2844 /* Visual is after curwin->w_cursor */
2845 top = &curwin->w_cursor;
2846 bot = &VIsual;
2847 }
2848 else
2849 {
2850 /* Visual is before curwin->w_cursor */
2851 top = &VIsual;
2852 bot = &curwin->w_cursor;
2853 }
2854 if (lnum >= top->lnum
2855 && lnume <= bot->lnum
2856 && (VIsual_mode != 'v'
2857 || ((lnum > top->lnum
2858 || (lnum == top->lnum
2859 && top->col == 0))
2860 && (lnume < bot->lnum
2861 || (lnume == bot->lnum
2862 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002863 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
2865 if (VIsual_mode == Ctrl_V)
2866 {
2867 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002868 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002870 if (wp->w_old_cursor_lcol != MAXCOL
2871 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002872 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 len = wp->w_old_cursor_lcol;
2874 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002875 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002876 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002877 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 }
2879 }
2880 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002883 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
2886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002888#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002889 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2890 if (wp->w_p_cc_cols)
2891 {
2892 int i = 0;
2893 int j = wp->w_p_cc_cols[i];
2894 int old_txtcol = txtcol;
2895
2896 while (j > -1)
2897 {
2898 txtcol += j;
2899 if (wp->w_p_wrap)
2900 txtcol -= wp->w_skipcol;
2901 else
2902 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002904 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002905 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002906 txtcol = old_txtcol;
2907 j = wp->w_p_cc_cols[++i];
2908 }
2909 }
2910
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002911 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002912 if (wp->w_p_cuc)
2913 {
2914 txtcol += wp->w_virtcol;
2915 if (wp->w_p_wrap)
2916 txtcol -= wp->w_skipcol;
2917 else
2918 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002919 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002920 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002921 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002922 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
Bram Moolenaar02631462017-09-22 15:20:32 +02002925 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002926 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
2928 /*
2929 * Update w_cline_height and w_cline_folded if the cursor line was
2930 * updated (saves a call to plines() later).
2931 */
2932 if (wp == curwin
2933 && lnum <= curwin->w_cursor.lnum
2934 && lnume >= curwin->w_cursor.lnum)
2935 {
2936 curwin->w_cline_row = row;
2937 curwin->w_cline_height = 1;
2938 curwin->w_cline_folded = TRUE;
2939 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2940 }
2941}
2942
2943/*
2944 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2945 */
2946 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002947copy_text_attr(
2948 int off,
2949 char_u *buf,
2950 int len,
2951 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002953 int i;
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (enc_utf8)
2957 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002958 for (i = 0; i < len; ++i)
2959 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
2962/*
2963 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002964 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 */
2966 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002967fill_foldcolumn(
2968 char_u *p,
2969 win_T *wp,
2970 int closed, /* TRUE of FALSE */
2971 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
2973 int i = 0;
2974 int level;
2975 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002976 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002977 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002980 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982 level = win_foldinfo.fi_level;
2983 if (level > 0)
2984 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002985 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002986 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002987
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 /* If the column is too narrow, we start at the lowest level that
2989 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002990 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (first_level < 1)
2992 first_level = 1;
2993
Bram Moolenaar1c934292015-01-27 16:39:29 +01002994 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
2996 if (win_foldinfo.fi_lnum == lnum
2997 && first_level + i >= win_foldinfo.fi_low_level)
2998 p[i] = '-';
2999 else if (first_level == 1)
3000 p[i] = '|';
3001 else if (first_level + i <= 9)
3002 p[i] = '0' + first_level + i;
3003 else
3004 p[i] = '>';
3005 if (first_level + i == level)
3006 break;
3007 }
3008 }
3009 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003010 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011}
3012#endif /* FEAT_FOLDING */
3013
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003014#ifdef FEAT_TEXT_PROP
3015static textprop_T *current_text_props = NULL;
3016static buf_T *current_buf = NULL;
3017
3018 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003019text_prop_compare(const void *s1, const void *s2)
3020{
3021 int idx1, idx2;
3022 proptype_T *pt1, *pt2;
3023 colnr_T col1, col2;
3024
3025 idx1 = *(int *)s1;
3026 idx2 = *(int *)s2;
3027 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3028 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3029 if (pt1 == pt2)
3030 return 0;
3031 if (pt1 == NULL)
3032 return -1;
3033 if (pt2 == NULL)
3034 return 1;
3035 if (pt1->pt_priority != pt2->pt_priority)
3036 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3037 col1 = current_text_props[idx1].tp_col;
3038 col2 = current_text_props[idx2].tp_col;
3039 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3040}
3041#endif
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
3044 * Display line "lnum" of window 'wp' on the screen.
3045 * Start at row "startrow", stop when "endrow" is reached.
3046 * wp->w_virtcol needs to be valid.
3047 *
3048 * Return the number of last row the line occupies.
3049 */
3050 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003051win_line(
3052 win_T *wp,
3053 linenr_T lnum,
3054 int startrow,
3055 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003056 int nochange UNUSED, // not updating for changed text
3057 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003059 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3061 int c = 0; /* init for GCC */
3062 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003063#ifdef FEAT_LINEBREAK
3064 long vcol_sbr = -1; /* virtual column after showbreak */
3065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 long vcol_prev = -1; /* "vcol" of previous character */
3067 char_u *line; /* current line */
3068 char_u *ptr; /* current position in "line" */
3069 int row; /* row in the window, excl w_winrow */
3070 int screen_row; /* row on the screen, incl w_winrow */
3071
3072 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3073 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003074 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003075 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003077 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 int extra_attr = 0; /* attributes when n_extra != 0 */
3079 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3080 displaying lcs_eol at end-of-line */
3081 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3082 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3083
3084 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3085 int saved_n_extra = 0;
3086 char_u *saved_p_extra = NULL;
3087 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003088 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int saved_char_attr = 0;
3090
3091 int n_attr = 0; /* chars with special attr */
3092 int saved_attr2 = 0; /* char_attr saved for n_attr */
3093 int n_attr3 = 0; /* chars with overruling special attr */
3094 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3095
3096 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3097
3098 int fromcol, tocol; /* start/end of inverting */
3099 int fromcol_prev = -2; /* start of inverting after cursor */
3100 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003102 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 pos_T pos;
3104 long v;
3105
3106 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003107 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3109 in this line */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003110 int vi_attr = 0; /* attributes for Visual and incsearch
3111 highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 int area_attr = 0; /* attributes desired by highlighting */
3113 int search_attr = 0; /* attributes desired by 'hlsearch' */
3114#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003115 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int syntax_attr = 0; /* attributes desired by syntax */
3117 int has_syntax = FALSE; /* this buffer has syntax highl. */
3118 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003119 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003120 int draw_color_col = FALSE; /* highlight colorcolumn */
3121 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003122#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003123#ifdef FEAT_TEXT_PROP
3124 int text_prop_count;
3125 int text_prop_next = 0; // next text property to use
3126 textprop_T *text_props = NULL;
3127 int *text_prop_idxs = NULL;
3128 int text_props_active = 0;
3129 proptype_T *text_prop_type = NULL;
3130 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003131 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003132#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003133#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003134 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003135# define SPWORDLEN 150
3136 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003137 int nextlinecol = 0; /* column where nextline[] starts */
3138 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003139 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003140 int spell_attr = 0; /* attributes desired by spelling */
3141 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003142 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3143 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003144 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003145 static int cap_col = -1; /* column to check for Cap word */
3146 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003147 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003149 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 int multi_attr = 0; /* attributes desired by multibyte */
3151 int mb_l = 1; /* multi-byte byte length */
3152 int mb_c = 0; /* decoded multi-byte character */
3153 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003154 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#ifdef FEAT_DIFF
3156 int filler_lines; /* nr of filler lines to be drawn */
3157 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003158 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 int change_start = MAXCOL; /* first col of changed area */
3160 int change_end = -1; /* last col of changed area */
3161#endif
3162 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3163#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003164 int need_showbreak = FALSE; /* overlong line, skipping first x
3165 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003167#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003168 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003170 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#endif
3172#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003173 matchitem_T *cur; /* points to the match list */
3174 match_T *shl; /* points to search_hl or a match */
3175 int shl_flag; /* flag to indicate whether search_hl
3176 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003177 int pos_inprogress; /* marks that position match search is
3178 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003179 int prevcol_hl_flag; /* flag to indicate whether prevcol
3180 equals startcol of search_hl or one
3181 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182#endif
3183#ifdef FEAT_ARABIC
3184 int prev_c = 0; /* previous Arabic character */
3185 int prev_c1 = 0; /* first composing char for prev_c */
3186#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003187#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003188 int did_line_attr = 0;
3189#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003190#ifdef FEAT_TERMINAL
3191 int get_term_attr = FALSE;
3192#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003193 int win_attr = 0; // background for whole window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /* draw_state: items that are drawn in sequence: */
3196#define WL_START 0 /* nothing done yet */
3197#ifdef FEAT_CMDWIN
3198# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3199#else
3200# define WL_CMDLINE WL_START
3201#endif
3202#ifdef FEAT_FOLDING
3203# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3204#else
3205# define WL_FOLD WL_CMDLINE
3206#endif
3207#ifdef FEAT_SIGNS
3208# define WL_SIGN WL_FOLD + 1 /* column for signs */
3209#else
3210# define WL_SIGN WL_FOLD /* column for signs */
3211#endif
3212#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003213#ifdef FEAT_LINEBREAK
3214# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003216# define WL_BRI WL_NR
3217#endif
3218#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3219# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3220#else
3221# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222#endif
3223#define WL_LINE WL_SBR + 1 /* text in the line */
3224 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003225#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 int feedback_col = 0;
3227 int feedback_old_attr = -1;
3228#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003229 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
Bram Moolenaar860cae12010-06-05 23:22:07 +02003231#ifdef FEAT_CONCEAL
3232 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003233 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003234 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003235 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003236 int is_concealing = FALSE;
3237 int boguscols = 0; /* nonexistent columns added to force
3238 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003239 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003240 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003241 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003242 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003243# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003244# define FIX_FOR_BOGUSCOLS \
3245 { \
3246 n_extra += vcol_off; \
3247 vcol -= vcol_off; \
3248 vcol_off = 0; \
3249 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003250 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003251 boguscols = 0; \
3252 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003253#else
3254# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
3257 if (startrow > endrow) /* past the end already! */
3258 return startrow;
3259
3260 row = startrow;
3261 screen_row = row + W_WINROW(wp);
3262
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003263 if (!number_only)
3264 {
3265 /*
3266 * To speed up the loop below, set extra_check when there is linebreak,
3267 * trailing white space and/or syntax processing to be done.
3268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003270 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#endif
3272#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003273 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003274# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003275 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003276# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003277 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003279 /* Prepare for syntax highlighting in this line. When there is an
3280 * error, stop syntax highlighting. */
3281 save_did_emsg = did_emsg;
3282 did_emsg = FALSE;
3283 syntax_start(wp, lnum);
3284 if (did_emsg)
3285 wp->w_s->b_syn_error = TRUE;
3286 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003287 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003288 did_emsg = save_did_emsg;
3289#ifdef SYN_TIME_LIMIT
3290 if (!wp->w_s->b_syn_slow)
3291#endif
3292 {
3293 has_syntax = TRUE;
3294 extra_check = TRUE;
3295 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003298
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003299 /* Check for columns to display for 'colorcolumn'. */
3300 color_cols = wp->w_p_cc_cols;
3301 if (color_cols != NULL)
3302 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003303#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003304
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003305#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003306 if (term_show_buffer(wp->w_buffer))
3307 {
3308 extra_check = TRUE;
3309 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003310 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003311 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003312#endif
3313
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003314#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003315 if (wp->w_p_spell
3316 && *wp->w_s->b_p_spl != NUL
3317 && wp->w_s->b_langp.ga_len > 0
3318 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003319 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 /* Prepare for spell checking. */
3321 has_spell = TRUE;
3322 extra_check = TRUE;
3323
Bram Moolenaar7701f302018-10-02 21:20:32 +02003324 /* Get the start of the next line, so that words that wrap to the
3325 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003326 * Trick: skip a few chars for C/shell/Vim comments */
3327 nextline[SPWORDLEN] = NUL;
3328 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3329 {
3330 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3331 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3332 }
3333
Bram Moolenaar7701f302018-10-02 21:20:32 +02003334 /* When a word wrapped from the previous line the start of the
3335 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003336 if (lnum == checked_lnum)
3337 cur_checked_col = checked_col;
3338 checked_lnum = 0;
3339
3340 /* When there was a sentence end in the previous line may require a
3341 * word starting with capital in this line. In line 1 always check
3342 * the first word. */
3343 if (lnum != capcol_lnum)
3344 cap_col = -1;
3345 if (lnum == 1)
3346 cap_col = 0;
3347 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
3350
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003351 /*
3352 * handle visual active in this window
3353 */
3354 fromcol = -10;
3355 tocol = MAXCOL;
3356 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003358 /* Visual is after curwin->w_cursor */
3359 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003361 top = &curwin->w_cursor;
3362 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003364 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 top = &VIsual;
3367 bot = &curwin->w_cursor;
3368 }
3369 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3370 if (VIsual_mode == Ctrl_V) /* block mode */
3371 {
3372 if (lnum_in_visual_area)
3373 {
3374 fromcol = wp->w_old_cursor_fcol;
3375 tocol = wp->w_old_cursor_lcol;
3376 }
3377 }
3378 else /* non-block mode */
3379 {
3380 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003384 if (VIsual_mode == 'V') /* linewise */
3385 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 else
3387 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003388 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3389 if (gchar_pos(top) == NUL)
3390 tocol = fromcol + 1;
3391 }
3392 }
3393 if (VIsual_mode != 'V' && lnum == bot->lnum)
3394 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003395 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003396 {
3397 fromcol = -10;
3398 tocol = MAXCOL;
3399 }
3400 else if (bot->col == MAXCOL)
3401 tocol = MAXCOL;
3402 else
3403 {
3404 pos = *bot;
3405 if (*p_sel == 'e')
3406 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3407 else
3408 {
3409 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3410 ++tocol;
3411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413 }
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003416 /* Check if the character under the cursor should not be inverted */
3417 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003418#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003420#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 )
3422 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 /* if inverting in this line set area_highlighting */
3425 if (fromcol >= 0)
3426 {
3427 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003428 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003430 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003431 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003432 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003433 && clip_isautosel_plus()))
3434 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003439 /*
3440 * handle 'incsearch' and ":s///c" highlighting
3441 */
3442 else if (highlight_match
3443 && wp == curwin
3444 && lnum >= curwin->w_cursor.lnum
3445 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003447 if (lnum == curwin->w_cursor.lnum)
3448 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003449 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 else
3451 fromcol = 0;
3452 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3453 {
3454 pos.lnum = lnum;
3455 pos.col = search_match_endcol;
3456 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3457 }
3458 else
3459 tocol = MAXCOL;
3460 /* do at least one character; happens when past end of line */
3461 if (fromcol == tocol)
3462 tocol = fromcol + 1;
3463 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003464 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467
3468#ifdef FEAT_DIFF
3469 filler_lines = diff_check(wp, lnum);
3470 if (filler_lines < 0)
3471 {
3472 if (filler_lines == -1)
3473 {
3474 if (diff_find_change(wp, lnum, &change_start, &change_end))
3475 diff_hlf = HLF_ADD; /* added line */
3476 else if (change_start == 0)
3477 diff_hlf = HLF_TXD; /* changed text */
3478 else
3479 diff_hlf = HLF_CHD; /* changed line */
3480 }
3481 else
3482 diff_hlf = HLF_ADD; /* added line */
3483 filler_lines = 0;
3484 area_highlighting = TRUE;
3485 }
3486 if (lnum == wp->w_topline)
3487 filler_lines = wp->w_topfill;
3488 filler_todo = filler_lines;
3489#endif
3490
3491#ifdef LINE_ATTR
3492# ifdef FEAT_SIGNS
3493 /* If this line has a sign with line highlighting set line_attr. */
3494 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3495 if (v != 0)
3496 line_attr = sign_get_attr((int)v, TRUE);
3497# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003498# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003500 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003501 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502# endif
3503 if (line_attr != 0)
3504 area_highlighting = TRUE;
3505#endif
3506
3507 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3508 ptr = line;
3509
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003510#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003511 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003512 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003513 /* For checking first word with a capital skip white space. */
3514 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003515 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003516
Bram Moolenaar30abd282005-06-22 22:35:10 +00003517 /* To be able to spell-check over line boundaries copy the end of the
3518 * current line into nextline[]. Above the start of the next line was
3519 * copied to nextline[SPWORDLEN]. */
3520 if (nextline[SPWORDLEN] == NUL)
3521 {
3522 /* No next line or it is empty. */
3523 nextlinecol = MAXCOL;
3524 nextline_idx = 0;
3525 }
3526 else
3527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003529 if (v < SPWORDLEN)
3530 {
3531 /* Short line, use it completely and append the start of the
3532 * next line. */
3533 nextlinecol = 0;
3534 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003535 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003536 nextline_idx = v + 1;
3537 }
3538 else
3539 {
3540 /* Long line, use only the last SPWORDLEN bytes. */
3541 nextlinecol = v - SPWORDLEN;
3542 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3543 nextline_idx = SPWORDLEN + 1;
3544 }
3545 }
3546 }
3547#endif
3548
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003549 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003551 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003552 extra_check = TRUE;
3553 /* find start of trailing whitespace */
3554 if (lcs_trail)
3555 {
3556 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003557 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003558 --trailcol;
3559 trailcol += (colnr_T) (ptr - line);
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003563 if (*wp->w_p_wcr != NUL)
3564 {
3565 int attr = syn_name2attr(wp->w_p_wcr);
3566
3567 // 'wincolor' highlighting for the whole window
3568 if (attr != 0)
3569 {
3570 win_attr = attr;
3571 area_highlighting = TRUE;
3572 }
3573 }
3574#ifdef FEAT_TEXT_PROP
3575 if (bt_popup(wp->w_buffer))
3576 {
3577 screen_line_flags |= SLF_POPUP;
3578
3579 if (win_attr == 0)
3580 {
3581 win_attr = HL_ATTR(HLF_PNI);
3582 area_highlighting = TRUE;
3583 }
3584 }
3585#endif
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 /*
3588 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3589 * first character to be displayed.
3590 */
3591 if (wp->w_p_wrap)
3592 v = wp->w_skipcol;
3593 else
3594 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003595 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 while (vcol < v && *ptr != NUL)
3600 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003601 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003604 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003607 /* When:
3608 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003609 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003610 * - 'virtualedit' is set, or
3611 * - the visual mode is active,
3612 * the end of the line may be before the start of the displayed part.
3613 */
3614 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003615#ifdef FEAT_SYN_HL
3616 wp->w_p_cuc || draw_color_col ||
3617#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003618 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003619 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 /* Handle a character that's not completely on the screen: Put ptr at
3623 * that character but skip the first few screen characters. */
3624 if (vcol > v)
3625 {
3626 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003628 /* If the character fits on the screen, don't need to skip it.
3629 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003630 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003631 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633
3634 /*
3635 * Adjust for when the inverted text is before the screen,
3636 * and when the start of the inverted text is before the screen.
3637 */
3638 if (tocol <= vcol)
3639 fromcol = 0;
3640 else if (fromcol >= 0 && fromcol < vcol)
3641 fromcol = vcol;
3642
3643#ifdef FEAT_LINEBREAK
3644 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3645 if (wp->w_p_wrap)
3646 need_showbreak = TRUE;
3647#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003648#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003649 /* When spell checking a word we need to figure out the start of the
3650 * word and if it's badly spelled or not. */
3651 if (has_spell)
3652 {
3653 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003654 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003655 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003656
3657 pos = wp->w_cursor;
3658 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003659 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003660 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003661
3662 /* spell_move_to() may call ml_get() and make "line" invalid */
3663 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3664 ptr = line + linecol;
3665
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003666 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003667 {
3668 /* no bad word found at line start, don't check until end of a
3669 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003670 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003671 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003672 }
3673 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003674 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003675 /* bad word found, use attributes until end of word */
3676 word_end = wp->w_cursor.col + len + 1;
3677
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003678 /* Turn index into actual attributes. */
3679 if (spell_hlf != HLF_COUNT)
3680 spell_attr = highlight_attr[spell_hlf];
3681 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003682 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003683
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003684# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003685 /* Need to restart syntax highlighting for this line. */
3686 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003687 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003688# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003689 }
3690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692
3693 /*
3694 * Correct highlighting for cursor that can't be disabled.
3695 * Avoids having to check this for each character.
3696 */
3697 if (fromcol >= 0)
3698 {
3699 if (noinvcur)
3700 {
3701 if ((colnr_T)fromcol == wp->w_virtcol)
3702 {
3703 /* highlighting starts at cursor, let it start just after the
3704 * cursor */
3705 fromcol_prev = fromcol;
3706 fromcol = -1;
3707 }
3708 else if ((colnr_T)fromcol < wp->w_virtcol)
3709 /* restart highlighting after the cursor */
3710 fromcol_prev = wp->w_virtcol;
3711 }
3712 if (fromcol >= tocol)
3713 fromcol = -1;
3714 }
3715
3716#ifdef FEAT_SEARCH_EXTRA
3717 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003718 * Handle highlighting the last used search pattern and matches.
3719 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003720 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003722 cur = wp->w_match_head;
3723 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003724 while ((cur != NULL || shl_flag == FALSE) && !number_only
3725 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003727 if (shl_flag == FALSE)
3728 {
3729 shl = &search_hl;
3730 shl_flag = TRUE;
3731 }
3732 else
3733 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003734 shl->startcol = MAXCOL;
3735 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003737 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003738 v = (long)(ptr - line);
3739 if (cur != NULL)
3740 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003741 next_search_hl(wp, shl, lnum, (colnr_T)v,
3742 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003743
3744 /* Need to get the line again, a multi-line regexp may have made it
3745 * invalid. */
3746 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3747 ptr = line + v;
3748
3749 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003751 if (shl->lnum == lnum)
3752 shl->startcol = shl->rm.startpos[0].col;
3753 else
3754 shl->startcol = 0;
3755 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3756 - shl->rm.startpos[0].lnum)
3757 shl->endcol = shl->rm.endpos[0].col;
3758 else
3759 shl->endcol = MAXCOL;
3760 /* Highlight one character for an empty match. */
3761 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003763 if (has_mbyte && line[shl->endcol] != NUL)
3764 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3765 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003766 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003768 if ((long)shl->startcol < v) /* match at leftcol */
3769 {
3770 shl->attr_cur = shl->attr;
3771 search_attr = shl->attr;
3772 }
3773 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003775 if (shl != &search_hl && cur != NULL)
3776 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778#endif
3779
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003780#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003781 // Cursor line highlighting for 'cursorline' in the current window.
3782 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003783 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003784 // Do not show the cursor line when Visual mode is active, because it's
3785 // not clear what is selected then. Do update w_last_cursorline.
3786 if (!(wp == curwin && VIsual_active))
3787 {
3788 line_attr = HL_ATTR(HLF_CUL);
3789 area_highlighting = TRUE;
3790 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003791 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003792 }
3793#endif
3794
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003795#ifdef FEAT_TEXT_PROP
3796 {
3797 char_u *prop_start;
3798
3799 text_prop_count = get_text_props(wp->w_buffer, lnum,
3800 &prop_start, FALSE);
3801 if (text_prop_count > 0)
3802 {
3803 // Make a copy of the properties, so that they are properly
3804 // aligned.
3805 text_props = (textprop_T *)alloc(
3806 text_prop_count * sizeof(textprop_T));
3807 if (text_props != NULL)
3808 mch_memmove(text_props, prop_start,
3809 text_prop_count * sizeof(textprop_T));
3810
3811 // Allocate an array for the indexes.
3812 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3813 area_highlighting = TRUE;
3814 extra_check = TRUE;
3815 }
3816 }
3817#endif
3818
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003819 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822#ifdef FEAT_RIGHTLEFT
3823 if (wp->w_p_rl)
3824 {
3825 /* Rightleft window: process the text in the normal direction, but put
3826 * it in current_ScreenLine[] from right to left. Start at the
3827 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003828 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003830 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832#endif
3833
3834 /*
3835 * Repeat for the whole displayed line.
3836 */
3837 for (;;)
3838 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003839#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003840 int has_match_conc = 0; // match wants to conceal
3841 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 /* Skip this quickly when working on the text. */
3844 if (draw_state != WL_LINE)
3845 {
3846#ifdef FEAT_CMDWIN
3847 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3848 {
3849 draw_state = WL_CMDLINE;
3850 if (cmdwin_type != 0 && wp == curwin)
3851 {
3852 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003854 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003855 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003856 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 }
3859#endif
3860
3861#ifdef FEAT_FOLDING
3862 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3863 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003864 int fdc = compute_foldcolumn(wp, 0);
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003867 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003869 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003870 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003871 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003872 p_extra_free = alloc(12 + 1);
3873
3874 if (p_extra_free != NULL)
3875 {
3876 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3877 n_extra = fdc;
3878 p_extra_free[n_extra] = NUL;
3879 p_extra = p_extra_free;
3880 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003881 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003882 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 }
3885 }
3886#endif
3887
3888#ifdef FEAT_SIGNS
3889 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3890 {
3891 draw_state = WL_SIGN;
3892 /* Show the sign column when there are any signs in this
3893 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003894 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003896 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003898 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899# endif
3900
3901 /* Draw two cells with the sign value or blank. */
3902 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003903 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003904 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 n_extra = 2;
3906
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003907 if (row == startrow
3908#ifdef FEAT_DIFF
3909 + filler_lines && filler_todo <= 0
3910#endif
3911 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
3913 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3914 SIGN_TEXT);
3915# ifdef FEAT_SIGN_ICONS
3916 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3917 SIGN_ICON);
3918 if (gui.in_use && icon_sign != 0)
3919 {
3920 /* Use the image in this position. */
3921 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003922 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# ifdef FEAT_NETBEANS_INTG
3924 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003927 c_final = NUL;
3928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929# endif
3930 char_attr = icon_sign;
3931 }
3932 else
3933# endif
3934 if (text_sign != 0)
3935 {
3936 p_extra = sign_get_text(text_sign);
3937 if (p_extra != NULL)
3938 {
3939 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003940 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003941 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 char_attr = sign_get_attr(text_sign, FALSE);
3944 }
3945 }
3946 }
3947 }
3948#endif
3949
3950 if (draw_state == WL_NR - 1 && n_extra == 0)
3951 {
3952 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003953 /* Display the absolute or relative line number. After the
3954 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3955 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 && (row == startrow
3957#ifdef FEAT_DIFF
3958 + filler_lines
3959#endif
3960 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3961 {
3962 /* Draw the line number (empty space after wrapping). */
3963 if (row == startrow
3964#ifdef FEAT_DIFF
3965 + filler_lines
3966#endif
3967 )
3968 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003969 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003970 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003971
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003972 if (wp->w_p_nu && !wp->w_p_rnu)
3973 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003974 num = (long)lnum;
3975 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003976 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003977 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003978 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003979 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003980 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003981 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003982 num = lnum;
3983 fmt = "%-*ld ";
3984 }
3985 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003986
Bram Moolenaar700e7342013-01-30 12:31:36 +01003987 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003988 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (wp->w_skipcol > 0)
3990 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3991 *p_extra = '-';
3992#ifdef FEAT_RIGHTLEFT
3993 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003994 {
3995 char_u *p1, *p2;
3996 int t;
3997
3998 // like rl_mirror(), but keep the space at the end
3999 p2 = skiptowhite(extra) - 1;
4000 for (p1 = extra; p1 < p2; ++p1, --p2)
4001 {
4002 t = *p1;
4003 *p1 = *p2;
4004 *p2 = t;
4005 }
4006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007#endif
4008 p_extra = extra;
4009 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004010 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004015 c_final = NUL;
4016 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004017 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004018 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004019#ifdef FEAT_SYN_HL
4020 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004021 * the current line differently.
4022 * TODO: Can we use CursorLine instead of CursorLineNr
4023 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004024 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004025 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004026 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 }
4030
Bram Moolenaar597a4222014-06-25 14:39:50 +02004031#ifdef FEAT_LINEBREAK
4032 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4033 && n_extra == 0 && *p_sbr != NUL)
4034 /* draw indent after showbreak value */
4035 draw_state = WL_BRI;
4036 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4037 /* After the showbreak, draw the breakindent */
4038 draw_state = WL_BRI - 1;
4039
4040 /* draw 'breakindent': indent wrapped text accordingly */
4041 if (draw_state == WL_BRI - 1 && n_extra == 0)
4042 {
4043 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004044 /* if need_showbreak is set, breakindent also applies */
4045 if (wp->w_p_bri && n_extra == 0
4046 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004047# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004048 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004049# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004050 )
4051 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004052 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004053# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004054 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004055 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004056 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004057# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004058 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4059 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004060 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004061# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004062 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004063# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004064 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004065 c_extra = ' ';
4066 n_extra = get_breakindent_win(wp,
4067 ml_get_buf(wp->w_buffer, lnum, FALSE));
4068 /* Correct end of highlighted area for 'breakindent',
4069 * required when 'linebreak' is also set. */
4070 if (tocol == vcol)
4071 tocol += n_extra;
4072 }
4073 }
4074#endif
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4077 if (draw_state == WL_SBR - 1 && n_extra == 0)
4078 {
4079 draw_state = WL_SBR;
4080# ifdef FEAT_DIFF
4081 if (filler_todo > 0)
4082 {
4083 /* Draw "deleted" diff line(s). */
4084 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004087 c_final = NUL;
4088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004092 c_final = NUL;
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094# ifdef FEAT_RIGHTLEFT
4095 if (wp->w_p_rl)
4096 n_extra = col + 1;
4097 else
4098# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004099 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004100 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102# endif
4103# ifdef FEAT_LINEBREAK
4104 if (*p_sbr != NUL && need_showbreak)
4105 {
4106 /* Draw 'showbreak' at the start of each broken line. */
4107 p_extra = p_sbr;
4108 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004109 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004111 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004113 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 /* Correct end of highlighted area for 'showbreak',
4115 * required when 'linebreak' is also set. */
4116 if (tocol == vcol)
4117 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004118#ifdef FEAT_SYN_HL
4119 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004120 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004121 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004122 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125# endif
4126 }
4127#endif
4128
4129 if (draw_state == WL_LINE - 1 && n_extra == 0)
4130 {
4131 draw_state = WL_LINE;
4132 if (saved_n_extra)
4133 {
4134 /* Continue item from end of wrapped line. */
4135 n_extra = saved_n_extra;
4136 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004137 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 p_extra = saved_p_extra;
4139 char_attr = saved_char_attr;
4140 }
4141 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004142 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
4145
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004146 // When still displaying '$' of change command, stop at cursor.
4147 // When only displaying the (relative) line number and that's done,
4148 // stop here.
4149 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004150 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#ifdef FEAT_DIFF
4152 && filler_todo <= 0
4153#endif
4154 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004155 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004157 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004158 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004159 /* Pretend we have finished updating the window. Except when
4160 * 'cursorcolumn' is set. */
4161#ifdef FEAT_SYN_HL
4162 if (wp->w_p_cuc)
4163 row = wp->w_cline_row + wp->w_cline_height;
4164 else
4165#endif
4166 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169
Bram Moolenaar637532b2019-01-03 21:44:40 +01004170 if (draw_state == WL_LINE && (area_highlighting
4171#ifdef FEAT_SPELL
4172 || has_spell
4173#endif
4174 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 /* handle Visual or match highlighting in this line */
4177 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4179 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004181 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004183 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else if (area_attr != 0
4185 && (vcol == tocol
4186 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189#ifdef FEAT_SEARCH_EXTRA
4190 if (!n_extra)
4191 {
4192 /*
4193 * Check for start/end of search pattern match.
4194 * After end, check for start/end of next match.
4195 * When another match, have to check for start again.
4196 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004197 * Do this for 'search_hl' and the match list (ordered by
4198 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004200 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004201 cur = wp->w_match_head;
4202 shl_flag = FALSE;
4203 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004205 if (shl_flag == FALSE
4206 && ((cur != NULL
4207 && cur->priority > SEARCH_HL_PRIORITY)
4208 || cur == NULL))
4209 {
4210 shl = &search_hl;
4211 shl_flag = TRUE;
4212 }
4213 else
4214 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004215 if (cur != NULL)
4216 cur->pos.cur = 0;
4217 pos_inprogress = TRUE;
4218 while (shl->rm.regprog != NULL
4219 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004221 if (shl->startcol != MAXCOL
4222 && v >= (long)shl->startcol
4223 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004225 int tmp_col = v + MB_PTR2LEN(ptr);
4226
4227 if (shl->endcol < tmp_col)
4228 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004230#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004231 // Match with the "Conceal" group results in hiding
4232 // the match.
4233 if (cur != NULL
4234 && shl != &search_hl
4235 && syn_name2id((char_u *)"Conceal")
4236 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004237 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004238 has_match_conc =
4239 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004240 match_conc = cur->conceal_char;
4241 }
4242 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004243 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004246 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004249 next_search_hl(wp, shl, lnum, (colnr_T)v,
4250 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004251 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004252 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 /* Need to get the line again, a multi-line regexp
4255 * may have made it invalid. */
4256 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4257 ptr = line + v;
4258
4259 if (shl->lnum == lnum)
4260 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004261 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004263 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004265 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004267 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 /* highlight empty match, try again after
4270 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004272 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004273 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004275 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277
4278 /* Loop to check if the match starts at the
4279 * current position */
4280 continue;
4281 }
4282 }
4283 break;
4284 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004285 if (shl != &search_hl && cur != NULL)
4286 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004288
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004289 /* Use attributes from match with highest priority among
4290 * 'search_hl' and the match list. */
4291 search_attr = search_hl.attr_cur;
4292 cur = wp->w_match_head;
4293 shl_flag = FALSE;
4294 while (cur != NULL || shl_flag == FALSE)
4295 {
4296 if (shl_flag == FALSE
4297 && ((cur != NULL
4298 && cur->priority > SEARCH_HL_PRIORITY)
4299 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004300 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004301 shl = &search_hl;
4302 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004303 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004304 else
4305 shl = &cur->hl;
4306 if (shl->attr_cur != 0)
4307 search_attr = shl->attr_cur;
4308 if (shl != &search_hl && cur != NULL)
4309 cur = cur->next;
4310 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004311 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004312 if (*ptr == NUL && (did_line_attr >= 1
4313 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004314 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316#endif
4317
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004319 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004321 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4322 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004324 if (diff_hlf == HLF_TXD && ptr - line > change_end
4325 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004327 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004328 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004329 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004332
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004333#ifdef FEAT_TEXT_PROP
4334 if (text_props != NULL)
4335 {
4336 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004337 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004338
4339 // Check if any active property ends.
4340 for (pi = 0; pi < text_props_active; ++pi)
4341 {
4342 int tpi = text_prop_idxs[pi];
4343
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004344 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004345 + text_props[tpi].tp_len)
4346 {
4347 if (pi + 1 < text_props_active)
4348 mch_memmove(text_prop_idxs + pi,
4349 text_prop_idxs + pi + 1,
4350 sizeof(int)
4351 * (text_props_active - (pi + 1)));
4352 --text_props_active;
4353 --pi;
4354 }
4355 }
4356
4357 // Add any text property that starts in this column.
4358 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004359 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004360 text_prop_idxs[text_props_active++] = text_prop_next++;
4361
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004362 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004363 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004364 if (text_props_active > 0)
4365 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004366 // Sort the properties on priority and/or starting last.
4367 // Then combine the attributes, highest priority last.
4368 current_text_props = text_props;
4369 current_buf = wp->w_buffer;
4370 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4371 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004372
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004373 for (pi = 0; pi < text_props_active; ++pi)
4374 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004375 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004376 proptype_T *pt = text_prop_type_by_id(
4377 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004378
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004379 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004380 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004381 int pt_attr = syn_id2attr(pt->pt_hl_id);
4382
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004383 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004384 text_prop_attr =
4385 hl_combine_attr(text_prop_attr, pt_attr);
4386 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004387 }
4388 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004389 }
4390 }
4391#endif
4392
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004393 /* Decide which of the highlight attributes to use. */
4394 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004395#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004396 if (area_attr != 0)
4397 char_attr = hl_combine_attr(line_attr, area_attr);
4398 else if (search_attr != 0)
4399 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004400# ifdef FEAT_TEXT_PROP
4401 else if (text_prop_type != NULL)
4402 char_attr = hl_combine_attr(line_attr, text_prop_attr);
4403# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004404 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004405 || vcol < fromcol || vcol_prev < fromcol_prev
4406 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004407 // Use line_attr when not in the Visual or 'incsearch' area
4408 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004409 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004410#else
4411 if (area_attr != 0)
4412 char_attr = area_attr;
4413 else if (search_attr != 0)
4414 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004415#endif
4416 else
4417 {
4418 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004419#ifdef FEAT_TEXT_PROP
4420 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004421 {
4422 if (text_prop_combine)
4423 char_attr = hl_combine_attr(
4424 syntax_attr, text_prop_attr);
4425 else
4426 char_attr = text_prop_attr;
4427 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004428 else
4429#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004430#ifdef FEAT_SYN_HL
4431 if (has_syntax)
4432 char_attr = syntax_attr;
4433 else
4434#endif
4435 char_attr = 0;
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004438 if (char_attr == 0)
4439 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 /*
4442 * Get the next character to put on the screen.
4443 */
4444 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004445 * The "p_extra" points to the extra stuff that is inserted to
4446 * represent special characters (non-printable stuff) and other
4447 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004448 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004449 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4450 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4452 */
4453 if (n_extra > 0)
4454 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004455 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004457 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004459 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
4461 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004462 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004463 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 else
4466 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else
4469 {
4470 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 if (has_mbyte)
4472 {
4473 mb_c = c;
4474 if (enc_utf8)
4475 {
4476 /* If the UTF-8 character is more than one byte:
4477 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004478 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 mb_utf8 = FALSE;
4480 if (mb_l > n_extra)
4481 mb_l = 1;
4482 else if (mb_l > 1)
4483 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004484 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004486 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 }
4489 else
4490 {
4491 /* if this is a DBCS character, put it in "mb_c" */
4492 mb_l = MB_BYTE2LEN(c);
4493 if (mb_l >= n_extra)
4494 mb_l = 1;
4495 else if (mb_l > 1)
4496 mb_c = (c << 8) + p_extra[1];
4497 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004498 if (mb_l == 0) /* at the NUL at end-of-line */
4499 mb_l = 1;
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 /* If a double-width char doesn't fit display a '>' in the
4502 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004503 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504# ifdef FEAT_RIGHTLEFT
4505 wp->w_p_rl ? (col <= 0) :
4506# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004507 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 && (*mb_char2cells)(mb_c) == 2)
4509 {
4510 c = '>';
4511 mb_c = c;
4512 mb_l = 1;
4513 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004514 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /* put the pointer back to output the double-width
4516 * character at the start of the next line. */
4517 ++n_extra;
4518 --p_extra;
4519 }
4520 else
4521 {
4522 n_extra -= mb_l - 1;
4523 p_extra += mb_l - 1;
4524 }
4525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 ++p_extra;
4527 }
4528 --n_extra;
4529 }
4530 else
4531 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004532#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004533 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004534#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004535
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004536 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004537 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 /*
4539 * Get a character from the line itself.
4540 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004541 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004542#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004543 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (has_mbyte)
4546 {
4547 mb_c = c;
4548 if (enc_utf8)
4549 {
4550 /* If the UTF-8 character is more than one byte: Decode it
4551 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004552 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 mb_utf8 = FALSE;
4554 if (mb_l > 1)
4555 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004556 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 /* Overlong encoded ASCII or ASCII with composing char
4558 * is displayed normally, except a NUL. */
4559 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004560 {
4561 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004562#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004563 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004564#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004567
4568 /* At start of the line we can have a composing char.
4569 * Draw it as a space with a composing char. */
4570 if (utf_iscomposing(mb_c))
4571 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004572 int i;
4573
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004574 for (i = Screen_mco - 1; i > 0; --i)
4575 u8cc[i] = u8cc[i - 1];
4576 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004577 mb_c = ' ';
4578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580
4581 if ((mb_l == 1 && c >= 0x80)
4582 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004583 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 /*
4586 * Illegal UTF-8 byte: display as <xx>.
4587 * Non-BMP character : display as ? or fullwidth ?.
4588 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004589 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004590# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004591 if (wp->w_p_rl) /* reverse */
4592 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 p_extra = extra;
4595 c = *p_extra;
4596 mb_c = mb_ptr2char_adv(&p_extra);
4597 mb_utf8 = (c >= 0x80);
4598 n_extra = (int)STRLEN(p_extra);
4599 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004600 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (area_attr == 0 && search_attr == 0)
4602 {
4603 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004604 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 saved_attr2 = char_attr; /* save current attr */
4606 }
4607 }
4608 else if (mb_l == 0) /* at the NUL at end-of-line */
4609 mb_l = 1;
4610#ifdef FEAT_ARABIC
4611 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4612 {
4613 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004614 int pc, pc1, nc;
4615 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 /* The idea of what is the previous and next
4618 * character depends on 'rightleft'. */
4619 if (wp->w_p_rl)
4620 {
4621 pc = prev_c;
4622 pc1 = prev_c1;
4623 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004624 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else
4627 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004628 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 prev_c = mb_c;
4633
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636 else
4637 prev_c = mb_c;
4638#endif
4639 }
4640 else /* enc_dbcs */
4641 {
4642 mb_l = MB_BYTE2LEN(c);
4643 if (mb_l == 0) /* at the NUL at end-of-line */
4644 mb_l = 1;
4645 else if (mb_l > 1)
4646 {
4647 /* We assume a second byte below 32 is illegal.
4648 * Hopefully this is OK for all double-byte encodings!
4649 */
4650 if (ptr[1] >= 32)
4651 mb_c = (c << 8) + ptr[1];
4652 else
4653 {
4654 if (ptr[1] == NUL)
4655 {
4656 /* head byte at end of line */
4657 mb_l = 1;
4658 transchar_nonprint(extra, c);
4659 }
4660 else
4661 {
4662 /* illegal tail byte */
4663 mb_l = 2;
4664 STRCPY(extra, "XX");
4665 }
4666 p_extra = extra;
4667 n_extra = (int)STRLEN(extra) - 1;
4668 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004669 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 c = *p_extra++;
4671 if (area_attr == 0 && search_attr == 0)
4672 {
4673 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004674 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 saved_attr2 = char_attr; /* save current attr */
4676 }
4677 mb_c = c;
4678 }
4679 }
4680 }
4681 /* If a double-width char doesn't fit display a '>' in the
4682 * last column; the character is displayed at the start of the
4683 * next line. */
4684 if ((
4685# ifdef FEAT_RIGHTLEFT
4686 wp->w_p_rl ? (col <= 0) :
4687# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004688 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 && (*mb_char2cells)(mb_c) == 2)
4690 {
4691 c = '>';
4692 mb_c = c;
4693 mb_utf8 = FALSE;
4694 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004695 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004696 // Put pointer back so that the character will be
4697 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004699#ifdef FEAT_CONCEAL
4700 did_decrement_ptr = TRUE;
4701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 else if (*ptr != NUL)
4704 ptr += mb_l - 1;
4705
4706 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004707 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004708 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004709 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004712 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004713 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 c = ' ';
4715 if (area_attr == 0 && search_attr == 0)
4716 {
4717 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004718 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 saved_attr2 = char_attr; /* save current attr */
4720 }
4721 mb_c = c;
4722 mb_utf8 = FALSE;
4723 mb_l = 1;
4724 }
4725
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 ++ptr;
4728
4729 if (extra_check)
4730 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004731#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004732 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004733#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004734
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004735#ifdef FEAT_TERMINAL
4736 if (get_term_attr)
4737 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004738 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004739
4740 if (!attr_pri)
4741 char_attr = syntax_attr;
4742 else
4743 char_attr = hl_combine_attr(syntax_attr, char_attr);
4744 }
4745#endif
4746
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004747#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004748 // Get syntax attribute, unless still at the start of the line
4749 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004750 v = (long)(ptr - line);
4751 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 /* Get the syntax attribute for the character. If there
4754 * is an error, disable syntax highlighting. */
4755 save_did_emsg = did_emsg;
4756 did_emsg = FALSE;
4757
Bram Moolenaar217ad922005-03-20 22:37:15 +00004758 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004759# ifdef FEAT_SPELL
4760 has_spell ? &can_spell :
4761# endif
4762 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004765 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004766 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004767 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004768 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
4771 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004772
4773 // combine syntax attribute with 'wincolor'
4774 if (win_attr != 0)
4775 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4776
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004777#ifdef SYN_TIME_LIMIT
4778 if (wp->w_s->b_syn_slow)
4779 has_syntax = FALSE;
4780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 /* Need to get the line again, a multi-line regexp may
4783 * have made it invalid. */
4784 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4785 ptr = line + v;
4786
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004787# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004788 // Text properties overrule syntax highlighting or combine.
4789 if (text_prop_attr == 0 || text_prop_combine)
4790# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004791 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004792 int comb_attr = syntax_attr;
4793# ifdef FEAT_TEXT_PROP
4794 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4795# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004796 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004797 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004798 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004799 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004800 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004801# ifdef FEAT_CONCEAL
4802 /* no concealing past the end of the line, it interferes
4803 * with line highlighting */
4804 if (c == NUL)
4805 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004806 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004807 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004810#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004811
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004812#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004813 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004814 * Only do this when there is no syntax highlighting, the
4815 * @Spell cluster is not used or the current syntax item
4816 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004817 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004818 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004819 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004820 if (c != 0 && (
4821# ifdef FEAT_SYN_HL
4822 !has_syntax ||
4823# endif
4824 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004825 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004826 char_u *prev_ptr, *p;
4827 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004828 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004829 if (has_mbyte)
4830 {
4831 prev_ptr = ptr - mb_l;
4832 v -= mb_l - 1;
4833 }
4834 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004835 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004836
4837 /* Use nextline[] if possible, it has the start of the
4838 * next line concatenated. */
4839 if ((prev_ptr - line) - nextlinecol >= 0)
4840 p = nextline + (prev_ptr - line) - nextlinecol;
4841 else
4842 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004843 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004844 len = spell_check(wp, p, &spell_hlf, &cap_col,
4845 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004846 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004847
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004848 /* In Insert mode only highlight a word that
4849 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004850 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004851 && (State & INSERT) != 0
4852 && wp->w_cursor.lnum == lnum
4853 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004854 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004855 && wp->w_cursor.col < (colnr_T)word_end)
4856 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004857 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004858 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004859 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004860
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004861 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004862 && (p - nextline) + len > nextline_idx)
4863 {
4864 /* Remember that the good word continues at the
4865 * start of the next line. */
4866 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004867 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004868 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004869
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004870 /* Turn index into actual attributes. */
4871 if (spell_hlf != HLF_COUNT)
4872 spell_attr = highlight_attr[spell_hlf];
4873
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004874 if (cap_col > 0)
4875 {
4876 if (p != prev_ptr
4877 && (p - nextline) + cap_col >= nextline_idx)
4878 {
4879 /* Remember that the word in the next line
4880 * must start with a capital. */
4881 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004882 cap_col = (int)((p - nextline) + cap_col
4883 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004884 }
4885 else
4886 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004887 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004888 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004889 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004890 }
4891 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004892 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004893 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004894 char_attr = hl_combine_attr(char_attr, spell_attr);
4895 else
4896 char_attr = hl_combine_attr(spell_attr, char_attr);
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
4899#ifdef FEAT_LINEBREAK
4900 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004901 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004903 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004904 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004906 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004907 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004908
Bram Moolenaar597a4222014-06-25 14:39:50 +02004909 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004910 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004911 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004912 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004913# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004914 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004915 wp->w_buffer->b_p_vts_array) - 1;
4916# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004917 n_extra = (int)wp->w_buffer->b_p_ts
4918 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004919# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004920
Bram Moolenaar4df70292015-03-21 14:20:16 +01004921 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004922 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004923 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004924 {
4925#ifdef FEAT_CONCEAL
4926 if (c == TAB)
4927 /* See "Tab alignment" below. */
4928 FIX_FOR_BOGUSCOLS;
4929#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004930 if (!wp->w_p_list)
4931 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934#endif
4935
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004936 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4937 // But not when the character is followed by a composing
4938 // character (use mb_l to check that).
4939 if (wp->w_p_list
4940 && ((((c == 160 && mb_l == 1)
4941 || (mb_utf8
4942 && ((mb_c == 160 && mb_l == 2)
4943 || (mb_c == 0x202f && mb_l == 3))))
4944 && lcs_nbsp)
4945 || (c == ' '
4946 && mb_l == 1
4947 && lcs_space
4948 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004949 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004950 c = (c == ' ') ? lcs_space : lcs_nbsp;
4951 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004952 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004953 n_attr = 1;
4954 extra_attr = HL_ATTR(HLF_8);
4955 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004956 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004957 mb_c = c;
4958 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004959 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004960 mb_utf8 = TRUE;
4961 u8cc[0] = 0;
4962 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004963 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004964 else
4965 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004966 }
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4969 {
4970 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004971 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004974 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 saved_attr2 = char_attr; /* save current attr */
4976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004978 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004981 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004982 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 else
4985 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 }
4988
4989 /*
4990 * Handling of non-printable characters.
4991 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004992 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
4994 /*
4995 * when getting a character from the file, we may have to
4996 * turn it into something else on the way to putting it
4997 * into "ScreenLines".
4998 */
4999 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5000 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005001 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005002 long vcol_adjusted = vcol; /* removed showbreak length */
5003#ifdef FEAT_LINEBREAK
5004 /* only adjust the tab_len, when at the first column
5005 * after the showbreak value was drawn */
5006 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5007 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005010#ifdef FEAT_VARTABS
5011 tab_len = tabstop_padding(vcol_adjusted,
5012 wp->w_buffer->b_p_ts,
5013 wp->w_buffer->b_p_vts_array) - 1;
5014#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005015 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005016 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5017#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005018
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005019#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005020 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005021#endif
5022 /* tab amount depends on current column */
5023 n_extra = tab_len;
5024#ifdef FEAT_LINEBREAK
5025 else
5026 {
5027 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005028 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005029 int i;
5030 int saved_nextra = n_extra;
5031
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005032#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005033 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005034 /* there are characters to conceal */
5035 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005036 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5037 */
5038 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5039 && n_extra > tab_len)
5040 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005041#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005042
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005043 /* if n_extra > 0, it gives the number of chars, to
5044 * use for a tab, else we need to calculate the width
5045 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005046 len = (tab_len * mb_char2len(lcs_tab2));
5047 if (n_extra > 0)
5048 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005049 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005050 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005051 vim_memset(p, ' ', len);
5052 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005053 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005054 p_extra_free = p;
5055 for (i = 0; i < tab_len; i++)
5056 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005057 if (*p == NUL)
5058 {
5059 tab_len = i;
5060 break;
5061 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005062 mb_char2bytes(lcs_tab2, p);
5063 p += mb_char2len(lcs_tab2);
5064 n_extra += mb_char2len(lcs_tab2)
5065 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005066 }
5067 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005068#ifdef FEAT_CONCEAL
5069 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5070 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005071 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005072 n_extra -= vcol_off;
5073#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005074 }
5075#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005076#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005077 {
5078 int vc_saved = vcol_off;
5079
5080 /* Tab alignment should be identical regardless of
5081 * 'conceallevel' value. So tab compensates of all
5082 * previous concealed characters, and thus resets
5083 * vcol_off and boguscols accumulated so far in the
5084 * line. Note that the tab can be longer than
5085 * 'tabstop' when there are concealed characters. */
5086 FIX_FOR_BOGUSCOLS;
5087
5088 /* Make sure, the highlighting for the tab char will be
5089 * correctly set further below (effectively reverts the
5090 * FIX_FOR_BOGSUCOLS macro */
5091 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005092 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005093 tab_len += vc_saved;
5094 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (wp->w_p_list)
5098 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005099 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005100#ifdef FEAT_LINEBREAK
5101 if (wp->w_p_lbr)
5102 c_extra = NUL; /* using p_extra from above */
5103 else
5104#endif
5105 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005106 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005107 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005108 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005111 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005114 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005115 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118 else
5119 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005120 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 c_extra = ' ';
5122 c = ' ';
5123 }
5124 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005125 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005126 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005127 || ((fromcol >= 0 || fromcol_prev >= 0)
5128 && tocol > vcol
5129 && VIsual_mode != Ctrl_V
5130 && (
5131# ifdef FEAT_RIGHTLEFT
5132 wp->w_p_rl ? (col >= 0) :
5133# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005134 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005135 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005136 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005137 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005138 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005140 /* Display a '$' after the line or highlight an extra
5141 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5143 /* For a diff line the highlighting continues after the
5144 * "$". */
5145 if (
5146# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005147 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148# ifdef LINE_ATTR
5149 &&
5150# endif
5151# endif
5152# ifdef LINE_ATTR
5153 line_attr == 0
5154# endif
5155 )
5156#endif
5157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 /* In virtualedit, visual selections may extend
5159 * beyond end of line. */
5160 if (area_highlighting && virtual_active()
5161 && tocol != MAXCOL && vcol < tocol)
5162 n_extra = 0;
5163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 p_extra = at_end_str;
5166 n_extra = 1;
5167 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005168 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005171 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005172 c = lcs_eol;
5173 else
5174 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 lcs_eol_one = -1;
5176 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005177 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005179 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 n_attr = 1;
5181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005183 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
5185 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005186 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005187 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 else
5190 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 else if (c != NUL)
5193 {
5194 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005195 if (n_extra == 0)
5196 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197#ifdef FEAT_RIGHTLEFT
5198 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5199 rl_mirror(p_extra); /* reverse "<12>" */
5200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005202 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005203#ifdef FEAT_LINEBREAK
5204 if (wp->w_p_lbr)
5205 {
5206 char_u *p;
5207
5208 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005209 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005210 vim_memset(p, ' ', n_extra);
5211 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5212 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005213 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005214 p_extra_free = p_extra = p;
5215 }
5216 else
5217#endif
5218 {
5219 n_extra = byte2cells(c) - 1;
5220 c = *p_extra++;
5221 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005222 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005225 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 saved_attr2 = char_attr; /* save current attr */
5227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 else if (VIsual_active
5231 && (VIsual_mode == Ctrl_V
5232 || VIsual_mode == 'v')
5233 && virtual_active()
5234 && tocol != MAXCOL
5235 && vcol < tocol
5236 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005237#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005239#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005240 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 c = ' ';
5243 --ptr; /* put it back at the NUL */
5244 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005245#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 else if ((
5247# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005248 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005250# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005251 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ) && (
5255# ifdef FEAT_RIGHTLEFT
5256 wp->w_p_rl ? (col >= 0) :
5257# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005258 (col
5259# ifdef FEAT_CONCEAL
5260 - boguscols
5261# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005262 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
5264 /* Highlight until the right side of the window */
5265 c = ' ';
5266 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005267
5268 /* Remember we do the char for line highlighting. */
5269 ++did_line_attr;
5270
5271 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005272 if (line_attr != 0 && char_attr == search_attr
5273 && (did_line_attr > 1
5274 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005275 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276# ifdef FEAT_DIFF
5277 if (diff_hlf == HLF_TXD)
5278 {
5279 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005280 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005281 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005282 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005283 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5284 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005285 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
5288# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005289# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005290 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005291 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005292 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005293 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5294 char_attr = hl_combine_attr(char_attr,
5295 HL_ATTR(HLF_CUL));
5296 }
5297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
5299#endif
5300 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005301
5302#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005303 if ( wp->w_p_cole > 0
5304 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005305 conceal_cursor_line(wp))
5306 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005307 && !(lnum_in_visual_area
5308 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005309 {
5310 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005311 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005312 && (syn_get_sub_char() != NUL || match_conc
5313 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005314 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005315 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005316 /* First time at this concealed item: display one
5317 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005318 if (match_conc)
5319 c = match_conc;
5320 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 c = syn_get_sub_char();
5322 else if (lcs_conceal != NUL)
5323 c = lcs_conceal;
5324 else
5325 c = ' ';
5326
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005327 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005328
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005329 if (n_extra > 0)
5330 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005331 vcol += n_extra;
5332 if (wp->w_p_wrap && n_extra > 0)
5333 {
5334# ifdef FEAT_RIGHTLEFT
5335 if (wp->w_p_rl)
5336 {
5337 col -= n_extra;
5338 boguscols -= n_extra;
5339 }
5340 else
5341# endif
5342 {
5343 boguscols += n_extra;
5344 col += n_extra;
5345 }
5346 }
5347 n_extra = 0;
5348 n_attr = 0;
5349 }
5350 else if (n_skip == 0)
5351 {
5352 is_concealing = TRUE;
5353 n_skip = 1;
5354 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005355 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005356 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005357 {
5358 mb_utf8 = TRUE;
5359 u8cc[0] = 0;
5360 c = 0xc0;
5361 }
5362 else
5363 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005364 }
5365 else
5366 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005367 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005368 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005369 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005370
5371 if (n_skip > 0 && did_decrement_ptr)
5372 // not showing the '>', put pointer back to avoid getting stuck
5373 ++ptr;
5374
5375#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005378#ifdef FEAT_CONCEAL
5379 /* In the cursor line and we may be concealing characters: correct
5380 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005381 if (!did_wcol && draw_state == WL_LINE
5382 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005383 && conceal_cursor_line(wp)
5384 && (int)wp->w_virtcol <= vcol + n_skip)
5385 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005386# ifdef FEAT_RIGHTLEFT
5387 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005388 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005389 else
5390# endif
5391 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005392 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005393 did_wcol = TRUE;
5394 }
5395#endif
5396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 /* Don't override visual selection highlighting. */
5398 if (n_attr > 0
5399 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005400 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 char_attr = extra_attr;
5402
Bram Moolenaar81695252004-12-29 20:58:21 +00005403#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 /* XIM don't send preedit_start and preedit_end, but they send
5405 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5406 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005407 if (p_imst == IM_ON_THE_SPOT
5408 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005409 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 && (State & INSERT)
5411 && !p_imdisable
5412 && im_is_preediting()
5413 && draw_state == WL_LINE)
5414 {
5415 colnr_T tcol;
5416
5417 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005418 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 else
5420 tcol = preedit_end_col;
5421 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5422 {
5423 if (feedback_old_attr < 0)
5424 {
5425 feedback_col = 0;
5426 feedback_old_attr = char_attr;
5427 }
5428 char_attr = im_get_feedback_attr(feedback_col);
5429 if (char_attr < 0)
5430 char_attr = feedback_old_attr;
5431 feedback_col++;
5432 }
5433 else if (feedback_old_attr >= 0)
5434 {
5435 char_attr = feedback_old_attr;
5436 feedback_old_attr = -1;
5437 feedback_col = 0;
5438 }
5439 }
5440#endif
5441 /*
5442 * Handle the case where we are in column 0 but not on the first
5443 * character of the line and the user wants us to show us a
5444 * special character (via 'listchars' option "precedes:<char>".
5445 */
5446 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005447 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5449#ifdef FEAT_DIFF
5450 && filler_todo <= 0
5451#endif
5452 && draw_state > WL_NR
5453 && c != NUL)
5454 {
5455 c = lcs_prec;
5456 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005457 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5458 {
5459 /* Double-width character being overwritten by the "precedes"
5460 * character, need to fill up half the character. */
5461 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005462 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005463 n_extra = 1;
5464 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005465 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005468 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005471 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005472 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 else
5475 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005476 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
5478 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005479 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 n_attr3 = 1;
5481 }
5482 }
5483
5484 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005485 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005487 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005488#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005489 || did_line_attr == 1
5490#endif
5491 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005493#ifdef FEAT_SEARCH_EXTRA
5494 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005495
5496 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005497 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005498 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499#endif
5500
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005501 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 * highlight match at end of line. If it's beyond the last
5503 * char on the screen, just overwrite that one (tricky!) Not
5504 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005505#ifdef FEAT_SEARCH_EXTRA
5506 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005507 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005508 prevcol_hl_flag = TRUE;
5509 else
5510 {
5511 cur = wp->w_match_head;
5512 while (cur != NULL)
5513 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005514 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005515 {
5516 prevcol_hl_flag = TRUE;
5517 break;
5518 }
5519 cur = cur->next;
5520 }
5521 }
5522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005524 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005525 && (VIsual_mode != Ctrl_V
5526 || lnum == VIsual.lnum
5527 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005528 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#ifdef FEAT_SEARCH_EXTRA
5530 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005531 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005532# ifdef FEAT_SYN_HL
5533 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5534 && !(wp == curwin && VIsual_active))
5535# endif
5536# ifdef FEAT_DIFF
5537 && diff_hlf == (hlf_T)0
5538# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005539# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005540 && did_line_attr <= 1
5541# endif
5542 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#endif
5544 ))
5545 {
5546 int n = 0;
5547
5548#ifdef FEAT_RIGHTLEFT
5549 if (wp->w_p_rl)
5550 {
5551 if (col < 0)
5552 n = 1;
5553 }
5554 else
5555#endif
5556 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005557 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 n = -1;
5559 }
5560 if (n != 0)
5561 {
5562 /* At the window boundary, highlight the last character
5563 * instead (better than nothing). */
5564 off += n;
5565 col += n;
5566 }
5567 else
5568 {
5569 /* Add a blank character to highlight. */
5570 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 if (enc_utf8)
5572 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574#ifdef FEAT_SEARCH_EXTRA
5575 if (area_attr == 0)
5576 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005577 /* Use attributes from match with highest priority among
5578 * 'search_hl' and the match list. */
5579 char_attr = search_hl.attr;
5580 cur = wp->w_match_head;
5581 shl_flag = FALSE;
5582 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005583 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005584 if (shl_flag == FALSE
5585 && ((cur != NULL
5586 && cur->priority > SEARCH_HL_PRIORITY)
5587 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005588 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005589 shl = &search_hl;
5590 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005591 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005592 else
5593 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005594 if ((ptr - line) - 1 == (long)shl->startcol
5595 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005596 char_attr = shl->attr;
5597 if (shl != &search_hl && cur != NULL)
5598 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
5601#endif
5602 ScreenAttrs[off] = char_attr;
5603#ifdef FEAT_RIGHTLEFT
5604 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005607 --off;
5608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 else
5610#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005613 ++off;
5614 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005615 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005616#ifdef FEAT_SYN_HL
5617 eol_hl_off = 1;
5618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar91170f82006-05-05 21:15:17 +00005622 /*
5623 * At end of the text line.
5624 */
5625 if (c == NUL)
5626 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005627#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005628 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005629 if (wp->w_p_wrap)
5630 v = wp->w_skipcol;
5631 else
5632 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005633
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005634 /* check if line ends before left margin */
5635 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005636 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005637#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005638 // Get rid of the boguscols now, we want to draw until the right
5639 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005640 col -= boguscols;
5641 boguscols = 0;
5642#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005643
5644 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005645 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005646
5647 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005648 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5649 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005650 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005651 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005652 || draw_color_col
5653 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005654# ifdef FEAT_RIGHTLEFT
5655 && !wp->w_p_rl
5656# endif
5657 )
5658 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005659 int rightmost_vcol = 0;
5660 int i;
5661
5662 if (wp->w_p_cuc)
5663 rightmost_vcol = wp->w_virtcol;
5664 if (draw_color_col)
5665 /* determine rightmost colorcolumn to possibly draw */
5666 for (i = 0; color_cols[i] >= 0; ++i)
5667 if (rightmost_vcol < color_cols[i])
5668 rightmost_vcol = color_cols[i];
5669
Bram Moolenaar02631462017-09-22 15:20:32 +02005670 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005671 {
5672 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005673 if (enc_utf8)
5674 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005675 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005676 if (draw_color_col)
5677 draw_color_col = advance_color_col(VCOL_HLC,
5678 &color_cols);
5679
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005680 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005681 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005682 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005683 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005684 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005685 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005686
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005687 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005688 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005689
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005690 ++vcol;
5691 }
5692 }
5693#endif
5694
Bram Moolenaar53f81742017-09-22 14:35:51 +02005695 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005696 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 row++;
5698
5699 /*
5700 * Update w_cline_height and w_cline_folded if the cursor line was
5701 * updated (saves a call to plines() later).
5702 */
5703 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5704 {
5705 curwin->w_cline_row = startrow;
5706 curwin->w_cline_height = row - startrow;
5707#ifdef FEAT_FOLDING
5708 curwin->w_cline_folded = FALSE;
5709#endif
5710 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5711 }
5712
5713 break;
5714 }
5715
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005716 // Show "extends" character from 'listchars' if beyond the line end and
5717 // 'list' is set.
5718 if (lcs_ext != NUL
5719 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 && !wp->w_p_wrap
5721#ifdef FEAT_DIFF
5722 && filler_todo <= 0
5723#endif
5724 && (
5725#ifdef FEAT_RIGHTLEFT
5726 wp->w_p_rl ? col == 0 :
5727#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005728 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005730 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5732 {
5733 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005734 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005736 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {
5738 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005739 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005740 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742 else
5743 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005746#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005747 /* advance to the next 'colorcolumn' */
5748 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005749 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005750
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005751 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005752 * highlight the cursor position itself.
5753 * Also highlight the 'colorcolumn' if it is different than
5754 * 'cursorcolumn' */
5755 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005756 if (draw_state == WL_LINE && !lnum_in_visual_area
5757 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005758 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005759 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005760 && lnum != wp->w_cursor.lnum)
5761 {
5762 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005763 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005765 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005766 {
5767 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005768 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005769 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005770 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005771#endif
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 /*
5774 * Store character to be displayed.
5775 * Skip characters that are left of the screen for 'nowrap'.
5776 */
5777 vcol_prev = vcol;
5778 if (draw_state < WL_LINE || n_skip <= 0)
5779 {
5780 /*
5781 * Store the character.
5782 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005783#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5785 {
5786 /* A double-wide character is: put first halve in left cell. */
5787 --off;
5788 --col;
5789 }
5790#endif
5791 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005793 {
5794 if ((mb_c & 0xff00) == 0x8e00)
5795 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 else if (enc_utf8)
5799 {
5800 if (mb_utf8)
5801 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005802 int i;
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005805 if ((c & 0xff) == 0)
5806 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005807 for (i = 0; i < Screen_mco; ++i)
5808 {
5809 ScreenLinesC[i][off] = u8cc[i];
5810 if (u8cc[i] == 0)
5811 break;
5812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
5814 else
5815 ScreenLinesUC[off] = 0;
5816 }
5817 if (multi_attr)
5818 {
5819 ScreenAttrs[off] = multi_attr;
5820 multi_attr = 0;
5821 }
5822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 ScreenAttrs[off] = char_attr;
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5826 {
5827 /* Need to fill two screen columns. */
5828 ++off;
5829 ++col;
5830 if (enc_utf8)
5831 /* UTF-8: Put a 0 in the second screen char. */
5832 ScreenLines[off] = 0;
5833 else
5834 /* DBCS: Put second byte in the second screen char. */
5835 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005836 if (draw_state > WL_NR
5837#ifdef FEAT_DIFF
5838 && filler_todo <= 0
5839#endif
5840 )
5841 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 /* When "tocol" is halfway a character, set it to the end of
5843 * the character, otherwise highlighting won't stop. */
5844 if (tocol == vcol)
5845 ++tocol;
5846#ifdef FEAT_RIGHTLEFT
5847 if (wp->w_p_rl)
5848 {
5849 /* now it's time to backup one cell */
5850 --off;
5851 --col;
5852 }
5853#endif
5854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#ifdef FEAT_RIGHTLEFT
5856 if (wp->w_p_rl)
5857 {
5858 --off;
5859 --col;
5860 }
5861 else
5862#endif
5863 {
5864 ++off;
5865 ++col;
5866 }
5867 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005868#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005869 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005870 {
5871 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005872 ++vcol_off;
5873 if (n_extra > 0)
5874 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005875 if (wp->w_p_wrap)
5876 {
5877 /*
5878 * Special voodoo required if 'wrap' is on.
5879 *
5880 * Advance the column indicator to force the line
5881 * drawing to wrap early. This will make the line
5882 * take up the same screen space when parts are concealed,
5883 * so that cursor line computations aren't messed up.
5884 *
5885 * To avoid the fictitious advance of 'col' causing
5886 * trailing junk to be written out of the screen line
5887 * we are building, 'boguscols' keeps track of the number
5888 * of bad columns we have advanced.
5889 */
5890 if (n_extra > 0)
5891 {
5892 vcol += n_extra;
5893# ifdef FEAT_RIGHTLEFT
5894 if (wp->w_p_rl)
5895 {
5896 col -= n_extra;
5897 boguscols -= n_extra;
5898 }
5899 else
5900# endif
5901 {
5902 col += n_extra;
5903 boguscols += n_extra;
5904 }
5905 n_extra = 0;
5906 n_attr = 0;
5907 }
5908
5909
Bram Moolenaar860cae12010-06-05 23:22:07 +02005910 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5911 {
5912 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005913# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005914 if (wp->w_p_rl)
5915 {
5916 --boguscols;
5917 --col;
5918 }
5919 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005920# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005921 {
5922 ++boguscols;
5923 ++col;
5924 }
5925 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005926
5927# ifdef FEAT_RIGHTLEFT
5928 if (wp->w_p_rl)
5929 {
5930 --boguscols;
5931 --col;
5932 }
5933 else
5934# endif
5935 {
5936 ++boguscols;
5937 ++col;
5938 }
5939 }
5940 else
5941 {
5942 if (n_extra > 0)
5943 {
5944 vcol += n_extra;
5945 n_extra = 0;
5946 n_attr = 0;
5947 }
5948 }
5949
5950 }
5951#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 else
5953 --n_skip;
5954
Bram Moolenaar64486672010-05-16 15:46:46 +02005955 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5956 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005957 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958#ifdef FEAT_DIFF
5959 && filler_todo <= 0
5960#endif
5961 )
5962 ++vcol;
5963
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005964#ifdef FEAT_SYN_HL
5965 if (vcol_save_attr >= 0)
5966 char_attr = vcol_save_attr;
5967#endif
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 /* restore attributes after "predeces" in 'listchars' */
5970 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5971 char_attr = saved_attr3;
5972
5973 /* restore attributes after last 'listchars' or 'number' char */
5974 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5975 char_attr = saved_attr2;
5976
5977 /*
5978 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005979 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 */
5981 if ((
5982#ifdef FEAT_RIGHTLEFT
5983 wp->w_p_rl ? (col < 0) :
5984#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005985 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 && (*ptr != NUL
5987#ifdef FEAT_DIFF
5988 || filler_todo > 0
5989#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005990 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5992 )
5993 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005994#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005995 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005996 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005997 boguscols = 0;
5998#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005999 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006000 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 ++row;
6003 ++screen_row;
6004
6005 /* When not wrapping and finished diff lines, or when displayed
6006 * '$' and highlighting until last column, break here. */
6007 if ((!wp->w_p_wrap
6008#ifdef FEAT_DIFF
6009 && filler_todo <= 0
6010#endif
6011 ) || lcs_eol_one == -1)
6012 break;
6013
6014 /* When the window is too narrow draw all "@" lines. */
6015 if (draw_state != WL_LINE
6016#ifdef FEAT_DIFF
6017 && filler_todo <= 0
6018#endif
6019 )
6020 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006021 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 row = endrow;
6024 }
6025
6026 /* When line got too long for screen break here. */
6027 if (row == endrow)
6028 {
6029 ++row;
6030 break;
6031 }
6032
6033 if (screen_cur_row == screen_row - 1
6034#ifdef FEAT_DIFF
6035 && filler_todo <= 0
6036#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006037 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 {
6039 /* Remember that the line wraps, used for modeless copy. */
6040 LineWraps[screen_row - 1] = TRUE;
6041
6042 /*
6043 * Special trick to make copy/paste of wrapped lines work with
6044 * xterm/screen: write an extra character beyond the end of
6045 * the line. This will work with all terminal types
6046 * (regardless of the xn,am settings).
6047 * Only do this on a fast tty.
6048 * Only do this if the cursor is on the current line
6049 * (something has been written in it).
6050 * Don't do this for the GUI.
6051 * Don't do this for double-width characters.
6052 * Don't do this for a window not at the right screen border.
6053 */
6054 if (p_tf
6055#ifdef FEAT_GUI
6056 && !gui.in_use
6057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006059 && ((*mb_off2cells)(LineOffset[screen_row],
6060 LineOffset[screen_row] + screen_Columns)
6061 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006063 + (int)Columns - 2,
6064 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006065 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 {
6067 /* First make sure we are at the end of the screen line,
6068 * then output the same character again to let the
6069 * terminal know about the wrap. If the terminal doesn't
6070 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006071 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 screen_char(LineOffset[screen_row - 1]
6073 + (unsigned)Columns - 1,
6074 screen_row - 1, (int)(Columns - 1));
6075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 /* When there is a multi-byte character, just output a
6077 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006078 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6079 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 out_char(' ');
6081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 out_char(ScreenLines[LineOffset[screen_row - 1]
6083 + (Columns - 1)]);
6084 /* force a redraw of the first char on the next line */
6085 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6086 screen_start(); /* don't know where cursor is now */
6087 }
6088 }
6089
6090 col = 0;
6091 off = (unsigned)(current_ScreenLine - ScreenLines);
6092#ifdef FEAT_RIGHTLEFT
6093 if (wp->w_p_rl)
6094 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006095 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 off += col;
6097 }
6098#endif
6099
6100 /* reset the drawing state for the start of a wrapped line */
6101 draw_state = WL_START;
6102 saved_n_extra = n_extra;
6103 saved_p_extra = p_extra;
6104 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006105 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 saved_char_attr = char_attr;
6107 n_extra = 0;
6108 lcs_prec_todo = lcs_prec;
6109#ifdef FEAT_LINEBREAK
6110# ifdef FEAT_DIFF
6111 if (filler_todo <= 0)
6112# endif
6113 need_showbreak = TRUE;
6114#endif
6115#ifdef FEAT_DIFF
6116 --filler_todo;
6117 /* When the filler lines are actually below the last line of the
6118 * file, don't draw the line itself, break here. */
6119 if (filler_todo == 0 && wp->w_botfill)
6120 break;
6121#endif
6122 }
6123
6124 } /* for every character in the line */
6125
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006126#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006127 /* After an empty line check first word for capital. */
6128 if (*skipwhite(line) == NUL)
6129 {
6130 capcol_lnum = lnum + 1;
6131 cap_col = 0;
6132 }
6133#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006134#ifdef FEAT_TEXT_PROP
6135 vim_free(text_props);
6136 vim_free(text_prop_idxs);
6137#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006138
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006139 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 return row;
6141}
6142
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143/*
6144 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006145 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006146 */
6147 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006148comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006149{
6150 int i;
6151
6152 for (i = 0; i < Screen_mco; ++i)
6153 {
6154 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6155 return TRUE;
6156 if (ScreenLinesC[i][off_from] == 0)
6157 break;
6158 }
6159 return FALSE;
6160}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006161
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162/*
6163 * Check whether the given character needs redrawing:
6164 * - the (first byte of the) character is different
6165 * - the attributes are different
6166 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006167 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 */
6169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006170char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 if (cols > 0
6173 && ((ScreenLines[off_from] != ScreenLines[off_to]
6174 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 || (enc_dbcs != 0
6176 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6177 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6178 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6179 : (cols > 1 && ScreenLines[off_from + 1]
6180 != ScreenLines[off_to + 1])))
6181 || (enc_utf8
6182 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6183 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006184 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006185 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6186 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006187 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return TRUE;
6189 return FALSE;
6190}
6191
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006192#if defined(FEAT_TERMINAL) || defined(PROTO)
6193/*
6194 * Return the index in ScreenLines[] for the current screen line.
6195 */
6196 int
6197screen_get_current_line_off()
6198{
6199 return (int)(current_ScreenLine - ScreenLines);
6200}
6201#endif
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203/*
6204 * Move one "cooked" screen line to the screen, but only the characters that
6205 * have actually changed. Handle insert/delete character.
6206 * "coloff" gives the first column on the screen for this line.
6207 * "endcol" gives the columns where valid characters are.
6208 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6209 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006210 * "flags" can have bits:
6211 * SLF_POPUP popup window
6212 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6214 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6215 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006216 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006217screen_line(
6218 int row,
6219 int coloff,
6220 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006221 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006222 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 unsigned off_from;
6225 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006226 unsigned max_off_from;
6227 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 int force = FALSE; /* force update rest of the line */
6231 int redraw_this /* bool: does character need redraw? */
6232#ifdef FEAT_GUI
6233 = TRUE /* For GUI when while-loop empty */
6234#endif
6235 ;
6236 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 int clear_next = FALSE;
6238 int char_cells; /* 1: normal char */
6239 /* 2: occupies two display cells */
6240# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006242 /* Check for illegal row and col, just in case. */
6243 if (row >= Rows)
6244 row = Rows - 1;
6245 if (endcol > Columns)
6246 endcol = Columns;
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248# ifdef FEAT_CLIPBOARD
6249 clip_may_clear_selection(row, row);
6250# endif
6251
6252 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6253 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006254 max_off_from = off_from + screen_Columns;
6255 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006258 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
6260 /* Clear rest first, because it's left of the text. */
6261 if (clear_width > 0)
6262 {
6263 while (col <= endcol && ScreenLines[off_to] == ' '
6264 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006265 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 {
6267 ++off_to;
6268 ++col;
6269 }
6270 if (col <= endcol)
6271 screen_fill(row, row + 1, col + coloff,
6272 endcol + coloff + 1, ' ', ' ', 0);
6273 }
6274 col = endcol + 1;
6275 off_to = LineOffset[row] + col + coloff;
6276 off_from += col;
6277 endcol = (clear_width > 0 ? clear_width : -clear_width);
6278 }
6279#endif /* FEAT_RIGHTLEFT */
6280
6281 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6282
6283 while (col < endcol)
6284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006286 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 else
6288 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
6290 redraw_this = redraw_next;
6291 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6292 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6293
6294#ifdef FEAT_GUI
6295 /* If the next character was bold, then redraw the current character to
6296 * remove any pixels that might have spilt over into us. This only
6297 * happens in the GUI.
6298 */
6299 if (redraw_next && gui.in_use)
6300 {
6301 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006302 if (hl > HL_ALL)
6303 hl = syn_attr2attr(hl);
6304 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 redraw_this = TRUE;
6306 }
6307#endif
6308
6309 if (redraw_this)
6310 {
6311 /*
6312 * Special handling when 'xs' termcap flag set (hpterm):
6313 * Attributes for characters are stored at the position where the
6314 * cursor is when writing the highlighting code. The
6315 * start-highlighting code must be written with the cursor on the
6316 * first highlighted character. The stop-highlighting code must
6317 * be written with the cursor just after the last highlighted
6318 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006319 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 * to clear the rest of the line, and force redrawing it
6321 * completely.
6322 */
6323 if ( p_wiv
6324 && !force
6325#ifdef FEAT_GUI
6326 && !gui.in_use
6327#endif
6328 && ScreenAttrs[off_to] != 0
6329 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6330 {
6331 /*
6332 * Need to remove highlighting attributes here.
6333 */
6334 windgoto(row, col + coloff);
6335 out_str(T_CE); /* clear rest of this screen line */
6336 screen_start(); /* don't know where cursor is now */
6337 force = TRUE; /* force redraw of rest of the line */
6338 redraw_next = TRUE; /* or else next char would miss out */
6339
6340 /*
6341 * If the previous character was highlighted, need to stop
6342 * highlighting at this character.
6343 */
6344 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6345 {
6346 screen_attr = ScreenAttrs[off_to - 1];
6347 term_windgoto(row, col + coloff);
6348 screen_stop_highlight();
6349 }
6350 else
6351 screen_attr = 0; /* highlighting has stopped */
6352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (enc_dbcs != 0)
6354 {
6355 /* Check if overwriting a double-byte with a single-byte or
6356 * the other way around requires another character to be
6357 * redrawn. For UTF-8 this isn't needed, because comparing
6358 * ScreenLinesUC[] is sufficient. */
6359 if (char_cells == 1
6360 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006361 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 {
6363 /* Writing a single-cell character over a double-cell
6364 * character: need to redraw the next cell. */
6365 ScreenLines[off_to + 1] = 0;
6366 redraw_next = TRUE;
6367 }
6368 else if (char_cells == 2
6369 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006370 && (*mb_off2cells)(off_to, max_off_to) == 1
6371 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
6373 /* Writing the second half of a double-cell character over
6374 * a double-cell character: need to redraw the second
6375 * cell. */
6376 ScreenLines[off_to + 2] = 0;
6377 redraw_next = TRUE;
6378 }
6379
6380 if (enc_dbcs == DBCS_JPNU)
6381 ScreenLines2[off_to] = ScreenLines2[off_from];
6382 }
6383 /* When writing a single-width character over a double-width
6384 * character and at the end of the redrawn text, need to clear out
6385 * the right halve of the old character.
6386 * Also required when writing the right halve of a double-width
6387 * char over the left halve of an existing one. */
6388 if (has_mbyte && col + char_cells == endcol
6389 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006390 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006392 && (*mb_off2cells)(off_to, max_off_to) == 1
6393 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395
6396 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (enc_utf8)
6398 {
6399 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6400 if (ScreenLinesUC[off_from] != 0)
6401 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006402 int i;
6403
6404 for (i = 0; i < Screen_mco; ++i)
6405 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407 }
6408 if (char_cells == 2)
6409 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006412 /* The bold trick makes a single column of pixels appear in the
6413 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * character should be redrawn too. This happens for our own GUI
6415 * and for some xterms. */
6416 if (
6417# ifdef FEAT_GUI
6418 gui.in_use
6419# endif
6420# if defined(FEAT_GUI) && defined(UNIX)
6421 ||
6422# endif
6423# ifdef UNIX
6424 term_is_xterm
6425# endif
6426 )
6427 {
6428 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006429 if (hl > HL_ALL)
6430 hl = syn_attr2attr(hl);
6431 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 redraw_next = TRUE;
6433 }
6434#endif
6435 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006436
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006437 /* For simplicity set the attributes of second half of a
6438 * double-wide character equal to the first half. */
6439 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006441
6442 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 screen_char(off_to, row, col + coloff);
6446 }
6447 else if ( p_wiv
6448#ifdef FEAT_GUI
6449 && !gui.in_use
6450#endif
6451 && col + coloff > 0)
6452 {
6453 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6454 {
6455 /*
6456 * Don't output stop-highlight when moving the cursor, it will
6457 * stop the highlighting when it should continue.
6458 */
6459 screen_attr = 0;
6460 }
6461 else if (screen_attr != 0)
6462 screen_stop_highlight();
6463 }
6464
6465 off_to += CHAR_CELLS;
6466 off_from += CHAR_CELLS;
6467 col += CHAR_CELLS;
6468 }
6469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (clear_next)
6471 {
6472 /* Clear the second half of a double-wide character of which the left
6473 * half was overwritten with a single-wide character. */
6474 ScreenLines[off_to] = ' ';
6475 if (enc_utf8)
6476 ScreenLinesUC[off_to] = 0;
6477 screen_char(off_to, row, col + coloff);
6478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479
6480 if (clear_width > 0
6481#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006482 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483#endif
6484 )
6485 {
6486#ifdef FEAT_GUI
6487 int startCol = col;
6488#endif
6489
6490 /* blank out the rest of the line */
6491 while (col < clear_width && ScreenLines[off_to] == ' '
6492 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006493 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 ++off_to;
6496 ++col;
6497 }
6498 if (col < clear_width)
6499 {
6500#ifdef FEAT_GUI
6501 /*
6502 * In the GUI, clearing the rest of the line may leave pixels
6503 * behind if the first character cleared was bold. Some bold
6504 * fonts spill over the left. In this case we redraw the previous
6505 * character too. If we didn't skip any blanks above, then we
6506 * only redraw if the character wasn't already redrawn anyway.
6507 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006508 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
6510 hl = ScreenAttrs[off_to];
6511 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006512 {
6513 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006514
Bram Moolenaar9c697322006-10-09 20:11:17 +00006515 if (enc_utf8)
6516 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6517 * that its width is 2. */
6518 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6519 else if (enc_dbcs != 0)
6520 {
6521 /* find previous character by counting from first
6522 * column and get its width. */
6523 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006524 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006525
6526 while (off < off_to)
6527 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006528 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006529 off += prev_cells;
6530 }
6531 }
6532
6533 if (enc_dbcs != 0 && prev_cells > 1)
6534 screen_char_2(off_to - prev_cells, row,
6535 col + coloff - prev_cells);
6536 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006537 screen_char(off_to - prev_cells, row,
6538 col + coloff - prev_cells);
6539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 }
6541#endif
6542 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6543 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 off_to += clear_width - col;
6545 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547 }
6548
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006549 if (clear_width > 0
6550#ifdef FEAT_TEXT_PROP
6551 && !(flags & SLF_POPUP) // no separator for popup window
6552#endif
6553 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006555 // For a window that has a right neighbor, draw the separator char
6556 // right of the window contents.
6557 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
6559 int c;
6560
6561 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006562 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006563 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6564 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 || ScreenAttrs[off_to] != hl)
6566 {
6567 ScreenLines[off_to] = c;
6568 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 if (enc_utf8)
6570 {
6571 if (c >= 0x80)
6572 {
6573 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006574 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 }
6576 else
6577 ScreenLinesUC[off_to] = 0;
6578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 screen_char(off_to, row, col + coloff);
6580 }
6581 }
6582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 LineWraps[row] = FALSE;
6584 }
6585}
6586
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006587#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006589 * Mirror text "str" for right-left displaying.
6590 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006592 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006593rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
6595 char_u *p1, *p2;
6596 int t;
6597
6598 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6599 {
6600 t = *p1;
6601 *p1 = *p2;
6602 *p2 = t;
6603 }
6604}
6605#endif
6606
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607/*
6608 * mark all status lines for redraw; used after first :cd
6609 */
6610 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006611status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
6613 win_T *wp;
6614
Bram Moolenaar29323592016-07-24 22:04:11 +02006615 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (wp->w_status_height)
6617 {
6618 wp->w_redr_status = TRUE;
6619 redraw_later(VALID);
6620 }
6621}
6622
6623/*
6624 * mark all status lines of the current buffer for redraw
6625 */
6626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006627status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 win_T *wp;
6630
Bram Moolenaar29323592016-07-24 22:04:11 +02006631 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6633 {
6634 wp->w_redr_status = TRUE;
6635 redraw_later(VALID);
6636 }
6637}
6638
6639/*
6640 * Redraw all status lines that need to be redrawn.
6641 */
6642 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006643redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 win_T *wp;
6646
Bram Moolenaar29323592016-07-24 22:04:11 +02006647 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006649 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006650 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006651 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
Bram Moolenaar4033c552017-09-16 20:54:51 +02006654#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655/*
6656 * Redraw all status lines at the bottom of frame "frp".
6657 */
6658 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006659win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 if (frp->fr_layout == FR_LEAF)
6662 frp->fr_win->w_redr_status = TRUE;
6663 else if (frp->fr_layout == FR_ROW)
6664 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006665 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 win_redraw_last_status(frp);
6667 }
6668 else /* frp->fr_layout == FR_COL */
6669 {
6670 frp = frp->fr_child;
6671 while (frp->fr_next != NULL)
6672 frp = frp->fr_next;
6673 win_redraw_last_status(frp);
6674 }
6675}
6676#endif
6677
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678/*
6679 * Draw the verticap separator right of window "wp" starting with line "row".
6680 */
6681 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006682draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 int hl;
6685 int c;
6686
6687 if (wp->w_vsep_width)
6688 {
6689 /* draw the vertical separator right of this window */
6690 c = fillchar_vsep(&hl);
6691 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6692 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6693 c, ' ', hl);
6694 }
6695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006698static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
6700/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006701 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006704status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 int len = 0;
6707
6708#ifdef FEAT_MENU
6709 int emenu = (xp->xp_context == EXPAND_MENUS
6710 || xp->xp_context == EXPAND_MENUNAMES);
6711
6712 /* Check for menu separators - replace with '|'. */
6713 if (emenu && menu_is_separator(s))
6714 return 1;
6715#endif
6716
6717 while (*s != NUL)
6718 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006719 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006720 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006721 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723
6724 return len;
6725}
6726
6727/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006728 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006729 * These are backslashes used for escaping. Do show backslashes in help tags.
6730 */
6731 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006732skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006733{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006734 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006735#ifdef FEAT_MENU
6736 || ((xp->xp_context == EXPAND_MENUS
6737 || xp->xp_context == EXPAND_MENUNAMES)
6738 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6739#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006740 )
6741 {
6742#ifndef BACKSLASH_IN_FILENAME
6743 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6744 return 2;
6745#endif
6746 return 1;
6747 }
6748 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006749}
6750
6751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 * Show wildchar matches in the status line.
6753 * Show at least the "match" item.
6754 * We start at item 'first_match' in the list and show all matches that fit.
6755 *
6756 * If inversion is possible we use it. Else '=' characters are used.
6757 */
6758 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006759win_redr_status_matches(
6760 expand_T *xp,
6761 int num_matches,
6762 char_u **matches, /* list of matches */
6763 int match,
6764 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6767 int row;
6768 char_u *buf;
6769 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006770 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 int fillchar;
6772 int attr;
6773 int i;
6774 int highlight = TRUE;
6775 char_u *selstart = NULL;
6776 int selstart_col = 0;
6777 char_u *selend = NULL;
6778 static int first_match = 0;
6779 int add_left = FALSE;
6780 char_u *s;
6781#ifdef FEAT_MENU
6782 int emenu;
6783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 if (matches == NULL) /* interrupted completion? */
6787 return;
6788
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006789 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006790 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006791 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006792 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (buf == NULL)
6794 return;
6795
6796 if (match == -1) /* don't show match but original text */
6797 {
6798 match = 0;
6799 highlight = FALSE;
6800 }
6801 /* count 1 for the ending ">" */
6802 clen = status_match_len(xp, L_MATCH(match)) + 3;
6803 if (match == 0)
6804 first_match = 0;
6805 else if (match < first_match)
6806 {
6807 /* jumping left, as far as we can go */
6808 first_match = match;
6809 add_left = TRUE;
6810 }
6811 else
6812 {
6813 /* check if match fits on the screen */
6814 for (i = first_match; i < match; ++i)
6815 clen += status_match_len(xp, L_MATCH(i)) + 2;
6816 if (first_match > 0)
6817 clen += 2;
6818 /* jumping right, put match at the left */
6819 if ((long)clen > Columns)
6820 {
6821 first_match = match;
6822 /* if showing the last match, we can add some on the left */
6823 clen = 2;
6824 for (i = match; i < num_matches; ++i)
6825 {
6826 clen += status_match_len(xp, L_MATCH(i)) + 2;
6827 if ((long)clen >= Columns)
6828 break;
6829 }
6830 if (i == num_matches)
6831 add_left = TRUE;
6832 }
6833 }
6834 if (add_left)
6835 while (first_match > 0)
6836 {
6837 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6838 if ((long)clen >= Columns)
6839 break;
6840 --first_match;
6841 }
6842
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006843 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844
6845 if (first_match == 0)
6846 {
6847 *buf = NUL;
6848 len = 0;
6849 }
6850 else
6851 {
6852 STRCPY(buf, "< ");
6853 len = 2;
6854 }
6855 clen = len;
6856
6857 i = first_match;
6858 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6859 {
6860 if (i == match)
6861 {
6862 selstart = buf + len;
6863 selstart_col = clen;
6864 }
6865
6866 s = L_MATCH(i);
6867 /* Check for menu separators - replace with '|' */
6868#ifdef FEAT_MENU
6869 emenu = (xp->xp_context == EXPAND_MENUS
6870 || xp->xp_context == EXPAND_MENUNAMES);
6871 if (emenu && menu_is_separator(s))
6872 {
6873 STRCPY(buf + len, transchar('|'));
6874 l = (int)STRLEN(buf + len);
6875 len += l;
6876 clen += l;
6877 }
6878 else
6879#endif
6880 for ( ; *s != NUL; ++s)
6881 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006882 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006884 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 STRNCPY(buf + len, s, l);
6887 s += l - 1;
6888 len += l;
6889 }
6890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
6892 STRCPY(buf + len, transchar_byte(*s));
6893 len += (int)STRLEN(buf + len);
6894 }
6895 }
6896 if (i == match)
6897 selend = buf + len;
6898
6899 *(buf + len++) = ' ';
6900 *(buf + len++) = ' ';
6901 clen += 2;
6902 if (++i == num_matches)
6903 break;
6904 }
6905
6906 if (i != num_matches)
6907 {
6908 *(buf + len++) = '>';
6909 ++clen;
6910 }
6911
6912 buf[len] = NUL;
6913
6914 row = cmdline_row - 1;
6915 if (row >= 0)
6916 {
6917 if (wild_menu_showing == 0)
6918 {
6919 if (msg_scrolled > 0)
6920 {
6921 /* Put the wildmenu just above the command line. If there is
6922 * no room, scroll the screen one line up. */
6923 if (cmdline_row == Rows - 1)
6924 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006925 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 ++msg_scrolled;
6927 }
6928 else
6929 {
6930 ++cmdline_row;
6931 ++row;
6932 }
6933 wild_menu_showing = WM_SCROLLED;
6934 }
6935 else
6936 {
6937 /* Create status line if needed by setting 'laststatus' to 2.
6938 * Set 'winminheight' to zero to avoid that the window is
6939 * resized. */
6940 if (lastwin->w_status_height == 0)
6941 {
6942 save_p_ls = p_ls;
6943 save_p_wmh = p_wmh;
6944 p_ls = 2;
6945 p_wmh = 0;
6946 last_status(FALSE);
6947 }
6948 wild_menu_showing = WM_SHOWN;
6949 }
6950 }
6951
6952 screen_puts(buf, row, 0, attr);
6953 if (selstart != NULL && highlight)
6954 {
6955 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006956 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958
6959 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6960 }
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 vim_free(buf);
6964}
6965#endif
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967/*
6968 * Redraw the status line of window wp.
6969 *
6970 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006971 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6972 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006974 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006975win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 int row;
6978 char_u *p;
6979 int len;
6980 int fillchar;
6981 int attr;
6982 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006983 static int busy = FALSE;
6984
6985 /* It's possible to get here recursively when 'statusline' (indirectly)
6986 * invokes ":redrawstatus". Simply ignore the call then. */
6987 if (busy)
6988 return;
6989 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 wp->w_redr_status = FALSE;
6992 if (wp->w_status_height == 0)
6993 {
6994 /* no status line, can only be last window */
6995 redraw_cmdline = TRUE;
6996 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006997 else if (!redrawing()
6998#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006999 // don't update status line when popup menu is visible and may be
7000 // drawn over it, unless it will be redrawn later
7001 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007002#endif
7003 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 {
7005 /* Don't redraw right now, do it later. */
7006 wp->w_redr_status = TRUE;
7007 }
7008#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007009 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
7011 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007012 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 }
7014#endif
7015 else
7016 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007017 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007019 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 p = NameBuff;
7021 len = (int)STRLEN(p);
7022
Bram Moolenaard85f2712017-07-28 21:51:57 +02007023 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024#ifdef FEAT_QUICKFIX
7025 || wp->w_p_pvw
7026#endif
7027 || bufIsChanged(wp->w_buffer)
7028 || wp->w_buffer->b_p_ro)
7029 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007030 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007032 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 len += (int)STRLEN(p + len);
7034 }
7035#ifdef FEAT_QUICKFIX
7036 if (wp->w_p_pvw)
7037 {
7038 STRCPY(p + len, _("[Preview]"));
7039 len += (int)STRLEN(p + len);
7040 }
7041#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007042 if (bufIsChanged(wp->w_buffer)
7043#ifdef FEAT_TERMINAL
7044 && !bt_terminal(wp->w_buffer)
7045#endif
7046 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {
7048 STRCPY(p + len, "[+]");
7049 len += 3;
7050 }
7051 if (wp->w_buffer->b_p_ro)
7052 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007053 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007054 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
7056
Bram Moolenaar02631462017-09-22 15:20:32 +02007057 this_ru_col = ru_col - (Columns - wp->w_width);
7058 if (this_ru_col < (wp->w_width + 1) / 2)
7059 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 if (this_ru_col <= 1)
7061 {
7062 p = (char_u *)"<"; /* No room for file name! */
7063 len = 1;
7064 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007065 else if (has_mbyte)
7066 {
7067 int clen = 0, i;
7068
7069 /* Count total number of display cells. */
7070 clen = mb_string2cells(p, -1);
7071
7072 /* Find first character that will fit.
7073 * Going from start to end is much faster for DBCS. */
7074 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7075 i += (*mb_ptr2len)(p + i))
7076 clen -= (*mb_ptr2cells)(p + i);
7077 len = clen;
7078 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007080 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007082 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 }
7084
Bram Moolenaara12a1612019-01-24 16:39:02 +01007085 }
7086 else if (len > this_ru_col - 1)
7087 {
7088 p += len - (this_ru_col - 1);
7089 *p = '<';
7090 len = this_ru_col - 1;
7091 }
7092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007094 screen_puts(p, row, wp->w_wincol, attr);
7095 screen_fill(row, row + 1, len + wp->w_wincol,
7096 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007098 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7100 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007101 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
7103#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007104 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105#endif
7106 }
7107
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 /*
7109 * May need to draw the character below the vertical separator.
7110 */
7111 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7112 {
7113 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007114 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 else
7116 fillchar = fillchar_vsep(&attr);
7117 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7118 attr);
7119 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007120 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121}
7122
Bram Moolenaar238a5642006-02-21 22:12:05 +00007123#ifdef FEAT_STL_OPT
7124/*
7125 * Redraw the status line according to 'statusline' and take care of any
7126 * errors encountered.
7127 */
7128 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007129redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007130{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007131 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007132 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007133
7134 /* When called recursively return. This can happen when the statusline
7135 * contains an expression that triggers a redraw. */
7136 if (entered)
7137 return;
7138 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007139
Bram Moolenaara742e082016-04-05 21:10:38 +02007140 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007141 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007142 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007143 {
7144 /* When there is an error disable the statusline, otherwise the
7145 * display is messed up with errors and a redraw triggers the problem
7146 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007147 set_string_option_direct((char_u *)"statusline", -1,
7148 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007149 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007150 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007151 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007152 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007153}
7154#endif
7155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156/*
7157 * Return TRUE if the status line of window "wp" is connected to the status
7158 * line of the window right of it. If not, then it's a vertical separator.
7159 * Only call if (wp->w_vsep_width != 0).
7160 */
7161 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007162stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163{
7164 frame_T *fr;
7165
7166 fr = wp->w_frame;
7167 while (fr->fr_parent != NULL)
7168 {
7169 if (fr->fr_parent->fr_layout == FR_COL)
7170 {
7171 if (fr->fr_next != NULL)
7172 break;
7173 }
7174 else
7175 {
7176 if (fr->fr_next != NULL)
7177 return TRUE;
7178 }
7179 fr = fr->fr_parent;
7180 }
7181 return FALSE;
7182}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185/*
7186 * Get the value to show for the language mappings, active 'keymap'.
7187 */
7188 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007189get_keymap_str(
7190 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007191 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007192 char_u *buf, /* buffer for the result */
7193 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194{
7195 char_u *p;
7196
7197 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7198 return FALSE;
7199
7200 {
7201#ifdef FEAT_EVAL
7202 buf_T *old_curbuf = curbuf;
7203 win_T *old_curwin = curwin;
7204 char_u *s;
7205
7206 curbuf = wp->w_buffer;
7207 curwin = wp;
7208 STRCPY(buf, "b:keymap_name"); /* must be writable */
7209 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007210 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 --emsg_skip;
7212 curbuf = old_curbuf;
7213 curwin = old_curwin;
7214 if (p == NULL || *p == NUL)
7215#endif
7216 {
7217#ifdef FEAT_KEYMAP
7218 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7219 p = wp->w_buffer->b_p_keymap;
7220 else
7221#endif
7222 p = (char_u *)"lang";
7223 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007224 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 buf[0] = NUL;
7226#ifdef FEAT_EVAL
7227 vim_free(s);
7228#endif
7229 }
7230 return buf[0] != NUL;
7231}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233#if defined(FEAT_STL_OPT) || defined(PROTO)
7234/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007235 * Redraw the status line or ruler of window "wp".
7236 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 */
7238 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007239win_redr_custom(
7240 win_T *wp,
7241 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007243 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 int attr;
7245 int curattr;
7246 int row;
7247 int col = 0;
7248 int maxwidth;
7249 int width;
7250 int n;
7251 int len;
7252 int fillchar;
7253 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007254 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007256 struct stl_hlrec hltab[STL_MAX_ITEM];
7257 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007258 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007259 win_T *ewp;
7260 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
Bram Moolenaar1d633412013-12-11 15:52:01 +01007262 /* There is a tiny chance that this gets called recursively: When
7263 * redrawing a status line triggers redrawing the ruler or tabline.
7264 * Avoid trouble by not allowing recursion. */
7265 if (entered)
7266 return;
7267 entered = TRUE;
7268
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007270 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007272 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007273 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007274 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007275 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007276 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007277 maxwidth = Columns;
7278# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007279 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007282 else
7283 {
7284 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007285 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007286 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007287
7288 if (draw_ruler)
7289 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007290 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007292 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007293 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007294 if (*++stl == '-')
7295 stl++;
7296 if (atoi((char *)stl))
7297 while (VIM_ISDIGIT(*stl))
7298 stl++;
7299 if (*stl++ != '(')
7300 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007301 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007302 col = ru_col - (Columns - wp->w_width);
7303 if (col < (wp->w_width + 1) / 2)
7304 col = (wp->w_width + 1) / 2;
7305 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007306 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007307 {
7308 row = Rows - 1;
7309 --maxwidth; /* writing in last column may cause scrolling */
7310 fillchar = ' ';
7311 attr = 0;
7312 }
7313
7314# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007315 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007316# endif
7317 }
7318 else
7319 {
7320 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007321 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007322 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007323 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007324# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007325 use_sandbox = was_set_insecurely((char_u *)"statusline",
7326 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007327# endif
7328 }
7329
Bram Moolenaar53f81742017-09-22 14:35:51 +02007330 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331 }
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007334 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaar61452852011-02-01 18:01:11 +01007336 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7337 * the cursor away and back. */
7338 ewp = wp == NULL ? curwin : wp;
7339 p_crb_save = ewp->w_p_crb;
7340 ewp->w_p_crb = FALSE;
7341
Bram Moolenaar362f3562009-11-03 16:20:34 +00007342 /* Make a copy, because the statusline may include a function call that
7343 * might change the option value and free the memory. */
7344 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007345 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007346 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007347 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007348 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007349 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007351 /* Make all characters printable. */
7352 p = transstr(buf);
7353 if (p != NULL)
7354 {
7355 vim_strncpy(buf, p, sizeof(buf) - 1);
7356 vim_free(p);
7357 }
7358
7359 /* fill up with "fillchar" */
7360 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007361 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 ++width;
7365 }
7366 buf[len] = NUL;
7367
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007368 /*
7369 * Draw each snippet with the specified highlighting.
7370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 curattr = attr;
7372 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007373 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007375 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 screen_puts_len(p, len, row, col, curattr);
7377 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007378 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007380 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 else if (hltab[n].userhl < 0)
7383 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007384#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007385 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7386 && wp->w_status_height != 0)
7387 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007388 else if (wp != NULL && bt_terminal(wp->w_buffer)
7389 && wp->w_status_height != 0)
7390 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007391#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007392 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007393 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007395 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007398
7399 if (wp == NULL)
7400 {
7401 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7402 col = 0;
7403 len = 0;
7404 p = buf;
7405 fillchar = 0;
7406 for (n = 0; tabtab[n].start != NULL; n++)
7407 {
7408 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7409 while (col < len)
7410 TabPageIdxs[col++] = fillchar;
7411 p = tabtab[n].start;
7412 fillchar = tabtab[n].userhl;
7413 }
7414 while (col < Columns)
7415 TabPageIdxs[col++] = fillchar;
7416 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007417
7418theend:
7419 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420}
7421
7422#endif /* FEAT_STL_OPT */
7423
7424/*
7425 * Output a single character directly to the screen and update ScreenLines.
7426 */
7427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007428screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 char_u buf[MB_MAXBYTES + 1];
7431
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007432 if (has_mbyte)
7433 buf[(*mb_char2bytes)(c, buf)] = NUL;
7434 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007435 {
7436 buf[0] = c;
7437 buf[1] = NUL;
7438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 screen_puts(buf, row, col, attr);
7440}
7441
7442/*
7443 * Get a single character directly from ScreenLines into "bytes[]".
7444 * Also return its attribute in *attrp;
7445 */
7446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007447screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 unsigned off;
7450
7451 /* safety check */
7452 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7453 {
7454 off = LineOffset[row] + col;
7455 *attrp = ScreenAttrs[off];
7456 bytes[0] = ScreenLines[off];
7457 bytes[1] = NUL;
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (enc_utf8 && ScreenLinesUC[off] != 0)
7460 bytes[utfc_char2bytes(off, bytes)] = NUL;
7461 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7462 {
7463 bytes[0] = ScreenLines[off];
7464 bytes[1] = ScreenLines2[off];
7465 bytes[2] = NUL;
7466 }
7467 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7468 {
7469 bytes[1] = ScreenLines[off + 1];
7470 bytes[2] = NUL;
7471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
7473}
7474
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007475/*
7476 * Return TRUE if composing characters for screen posn "off" differs from
7477 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007478 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007479 */
7480 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007481screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482{
7483 int i;
7484
7485 for (i = 0; i < Screen_mco; ++i)
7486 {
7487 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7488 return TRUE;
7489 if (u8cc[i] == 0)
7490 break;
7491 }
7492 return FALSE;
7493}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495/*
7496 * Put string '*text' on the screen at position 'row' and 'col', with
7497 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7498 * Note: only outputs within one row, message is truncated at screen boundary!
7499 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7500 */
7501 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007502screen_puts(
7503 char_u *text,
7504 int row,
7505 int col,
7506 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
7508 screen_puts_len(text, -1, row, col, attr);
7509}
7510
7511/*
7512 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7513 * a NUL.
7514 */
7515 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007516screen_puts_len(
7517 char_u *text,
7518 int textlen,
7519 int row,
7520 int col,
7521 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 unsigned off;
7524 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007525 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007527 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 int mbyte_blen = 1;
7529 int mbyte_cells = 1;
7530 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007531 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007533#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007535 int pc, nc, nc1;
7536 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007538 int force_redraw_this;
7539 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007540 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541
7542 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7543 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007544 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
Bram Moolenaarc236c162008-07-13 17:41:49 +00007546 /* When drawing over the right halve of a double-wide char clear out the
7547 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007548 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007549#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007550 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007551#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007552 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007553 {
7554 ScreenLines[off - 1] = ' ';
7555 ScreenAttrs[off - 1] = 0;
7556 if (enc_utf8)
7557 {
7558 ScreenLinesUC[off - 1] = 0;
7559 ScreenLinesC[0][off - 1] = 0;
7560 }
7561 /* redraw the previous cell, make it empty */
7562 screen_char(off - 1, row, col - 1);
7563 /* force the cell at "col" to be redrawn */
7564 force_redraw_next = TRUE;
7565 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007566
Bram Moolenaar367329b2007-08-30 11:53:22 +00007567 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007568 while (col < screen_Columns
7569 && (len < 0 || (int)(ptr - text) < len)
7570 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 {
7572 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 /* check if this is the first byte of a multibyte */
7574 if (has_mbyte)
7575 {
7576 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007577 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007579 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7581 mbyte_cells = 1;
7582 else if (enc_dbcs != 0)
7583 mbyte_cells = mbyte_blen;
7584 else /* enc_utf8 */
7585 {
7586 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007587 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 (int)((text + len) - ptr));
7589 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007590 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007592#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7594 {
7595 /* Do Arabic shaping. */
7596 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7597 {
7598 /* Past end of string to be displayed. */
7599 nc = NUL;
7600 nc1 = NUL;
7601 }
7602 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007603 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007604 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7605 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007606 nc1 = pcc[0];
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 pc = prev_c;
7609 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007610 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612 else
7613 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007614#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007615 if (col + mbyte_cells > screen_Columns)
7616 {
7617 /* Only 1 cell left, but character requires 2 cells:
7618 * display a '>' in the last column to avoid wrapping. */
7619 c = '>';
7620 mbyte_cells = 1;
7621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 }
7623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007625 force_redraw_this = force_redraw_next;
7626 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007627
7628 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 || (mbyte_cells == 2
7630 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7631 || (enc_dbcs == DBCS_JPNU
7632 && c == 0x8e
7633 && ScreenLines2[off] != ptr[1])
7634 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007635 && (ScreenLinesUC[off] !=
7636 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7637 || (ScreenLinesUC[off] != 0
7638 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007640 || exmode_active;
7641
Bram Moolenaara12a1612019-01-24 16:39:02 +01007642 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
7644#if defined(FEAT_GUI) || defined(UNIX)
7645 /* The bold trick makes a single row of pixels appear in the next
7646 * character. When a bold character is removed, the next
7647 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007648 * and for some xterms. */
7649 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650# ifdef FEAT_GUI
7651 gui.in_use
7652# endif
7653# if defined(FEAT_GUI) && defined(UNIX)
7654 ||
7655# endif
7656# ifdef UNIX
7657 term_is_xterm
7658# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007659 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007661 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007663 if (n > HL_ALL)
7664 n = syn_attr2attr(n);
7665 if (n & HL_BOLD)
7666 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
7668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 /* When at the end of the text and overwriting a two-cell
7670 * character with a one-cell character, need to clear the next
7671 * cell. Also when overwriting the left halve of a two-cell char
7672 * with the right halve of a two-cell char. Do this only once
7673 * (mb_off2cells() may return 2 on the right halve). */
7674 if (clear_next_cell)
7675 clear_next_cell = FALSE;
7676 else if (has_mbyte
7677 && (len < 0 ? ptr[mbyte_blen] == NUL
7678 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007679 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007681 && (*mb_off2cells)(off, max_off) == 1
7682 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 clear_next_cell = TRUE;
7684
7685 /* Make sure we never leave a second byte of a double-byte behind,
7686 * it confuses mb_off2cells(). */
7687 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007688 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007690 && (*mb_off2cells)(off, max_off) == 1
7691 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 ScreenLines[off] = c;
7694 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 if (enc_utf8)
7696 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007697 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 ScreenLinesUC[off] = 0;
7699 else
7700 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 int i;
7702
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007704 for (i = 0; i < Screen_mco; ++i)
7705 {
7706 ScreenLinesC[i][off] = u8cc[i];
7707 if (u8cc[i] == 0)
7708 break;
7709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 if (mbyte_cells == 2)
7712 {
7713 ScreenLines[off + 1] = 0;
7714 ScreenAttrs[off + 1] = attr;
7715 }
7716 screen_char(off, row, col);
7717 }
7718 else if (mbyte_cells == 2)
7719 {
7720 ScreenLines[off + 1] = ptr[1];
7721 ScreenAttrs[off + 1] = attr;
7722 screen_char_2(off, row, col);
7723 }
7724 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7725 {
7726 ScreenLines2[off] = ptr[1];
7727 screen_char(off, row, col);
7728 }
7729 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 screen_char(off, row, col);
7731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (has_mbyte)
7733 {
7734 off += mbyte_cells;
7735 col += mbyte_cells;
7736 ptr += mbyte_blen;
7737 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007738 {
7739 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007741 len = -1;
7742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
7744 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {
7746 ++off;
7747 ++col;
7748 ++ptr;
7749 }
7750 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007751
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007752 /* If we detected the next character needs to be redrawn, but the text
7753 * doesn't extend up to there, update the character here. */
7754 if (force_redraw_next && col < screen_Columns)
7755 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007756 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7757 screen_char_2(off, row, col);
7758 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007759 screen_char(off, row, col);
7760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761}
7762
7763#ifdef FEAT_SEARCH_EXTRA
7764/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007765 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 */
7767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007768start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769{
7770 if (p_hls && !no_hlsearch)
7771 {
7772 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007773 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007774# ifdef FEAT_RELTIME
7775 /* Set the time limit to 'redrawtime'. */
7776 profile_setlimit(p_rdt, &search_hl.tm);
7777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779}
7780
7781/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007782 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 */
7784 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007785end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 if (search_hl.rm.regprog != NULL)
7788 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007789 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 search_hl.rm.regprog = NULL;
7791 }
7792}
7793
7794/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007795 * Init for calling prepare_search_hl().
7796 */
7797 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007798init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007799{
7800 matchitem_T *cur;
7801
7802 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7803 * match */
7804 cur = wp->w_match_head;
7805 while (cur != NULL)
7806 {
7807 cur->hl.rm = cur->match;
7808 if (cur->hlg_id == 0)
7809 cur->hl.attr = 0;
7810 else
7811 cur->hl.attr = syn_id2attr(cur->hlg_id);
7812 cur->hl.buf = wp->w_buffer;
7813 cur->hl.lnum = 0;
7814 cur->hl.first_lnum = 0;
7815# ifdef FEAT_RELTIME
7816 /* Set the time limit to 'redrawtime'. */
7817 profile_setlimit(p_rdt, &(cur->hl.tm));
7818# endif
7819 cur = cur->next;
7820 }
7821 search_hl.buf = wp->w_buffer;
7822 search_hl.lnum = 0;
7823 search_hl.first_lnum = 0;
7824 /* time limit is set at the toplevel, for all windows */
7825}
7826
7827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 * Advance to the match in window "wp" line "lnum" or past it.
7829 */
7830 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007831prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007833 matchitem_T *cur; /* points to the match list */
7834 match_T *shl; /* points to search_hl or a match */
7835 int shl_flag; /* flag to indicate whether search_hl
7836 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007837 int pos_inprogress; /* marks that position match search is
7838 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 int n;
7840
7841 /*
7842 * When using a multi-line pattern, start searching at the top
7843 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007844 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007846 cur = wp->w_match_head;
7847 shl_flag = FALSE;
7848 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007850 if (shl_flag == FALSE)
7851 {
7852 shl = &search_hl;
7853 shl_flag = TRUE;
7854 }
7855 else
7856 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 if (shl->rm.regprog != NULL
7858 && shl->lnum == 0
7859 && re_multiline(shl->rm.regprog))
7860 {
7861 if (shl->first_lnum == 0)
7862 {
7863# ifdef FEAT_FOLDING
7864 for (shl->first_lnum = lnum;
7865 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7866 if (hasFoldingWin(wp, shl->first_lnum - 1,
7867 NULL, NULL, TRUE, NULL))
7868 break;
7869# else
7870 shl->first_lnum = wp->w_topline;
7871# endif
7872 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007873 if (cur != NULL)
7874 cur->pos.cur = 0;
7875 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007877 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7878 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007880 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7881 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882 pos_inprogress = cur == NULL || cur->pos.cur == 0
7883 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (shl->lnum != 0)
7885 {
7886 shl->first_lnum = shl->lnum
7887 + shl->rm.endpos[0].lnum
7888 - shl->rm.startpos[0].lnum;
7889 n = shl->rm.endpos[0].col;
7890 }
7891 else
7892 {
7893 ++shl->first_lnum;
7894 n = 0;
7895 }
7896 }
7897 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007898 if (shl != &search_hl && cur != NULL)
7899 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 }
7901}
7902
7903/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007904 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 * Uses shl->buf.
7906 * Sets shl->lnum and shl->rm contents.
7907 * Note: Assumes a previous match is always before "lnum", unless
7908 * shl->lnum is zero.
7909 * Careful: Any pointers for buffer lines will become invalid.
7910 */
7911 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007912next_search_hl(
7913 win_T *win,
7914 match_T *shl, /* points to search_hl or a match */
7915 linenr_T lnum,
7916 colnr_T mincol, /* minimal column for a match */
7917 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 linenr_T l;
7920 colnr_T matchcol;
7921 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007922 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007924 // for :{range}s/pat only highlight inside the range
7925 if (lnum < search_first_line || lnum > search_last_line)
7926 {
7927 shl->lnum = 0;
7928 return;
7929 }
7930
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (shl->lnum != 0)
7932 {
7933 /* Check for three situations:
7934 * 1. If the "lnum" is below a previous match, start a new search.
7935 * 2. If the previous match includes "mincol", use it.
7936 * 3. Continue after the previous match.
7937 */
7938 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7939 if (lnum > l)
7940 shl->lnum = 0;
7941 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7942 return;
7943 }
7944
7945 /*
7946 * Repeat searching for a match until one is found that includes "mincol"
7947 * or none is found in this line.
7948 */
7949 called_emsg = FALSE;
7950 for (;;)
7951 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007952#ifdef FEAT_RELTIME
7953 /* Stop searching after passing the time limit. */
7954 if (profile_passed_limit(&(shl->tm)))
7955 {
7956 shl->lnum = 0; /* no match found in time */
7957 break;
7958 }
7959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 /* Three situations:
7961 * 1. No useful previous match: search from start of line.
7962 * 2. Not Vi compatible or empty match: continue at next character.
7963 * Break the loop if this is beyond the end of the line.
7964 * 3. Vi compatible searching: continue at end of previous match.
7965 */
7966 if (shl->lnum == 0)
7967 matchcol = 0;
7968 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7969 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007970 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007972 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007973
7974 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007975 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007976 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007978 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 shl->lnum = 0;
7980 break;
7981 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982 if (has_mbyte)
7983 matchcol += mb_ptr2len(ml);
7984 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007985 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987 else
7988 matchcol = shl->rm.endpos[0].col;
7989
7990 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007991 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007993 /* Remember whether shl->rm is using a copy of the regprog in
7994 * cur->match. */
7995 int regprog_is_copy = (shl != &search_hl && cur != NULL
7996 && shl == &cur->hl
7997 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02007998 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01007999
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8001 matchcol,
8002#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008003 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008004#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008005 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008006#endif
8007 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008008 /* Copy the regprog, in case it got freed and recompiled. */
8009 if (regprog_is_copy)
8010 cur->match.regprog = cur->hl.rm.regprog;
8011
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008012 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008013 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008014 /* Error while handling regexp: stop using this regexp. */
8015 if (shl == &search_hl)
8016 {
8017 /* don't free regprog in the match list, it's a copy */
8018 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008019 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008020 }
8021 shl->rm.regprog = NULL;
8022 shl->lnum = 0;
8023 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8024 message */
8025 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008026 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008027 }
8028 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008029 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008030 else
8031 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (nmatched == 0)
8033 {
8034 shl->lnum = 0; /* no match found */
8035 break;
8036 }
8037 if (shl->rm.startpos[0].lnum > 0
8038 || shl->rm.startpos[0].col >= mincol
8039 || nmatched > 1
8040 || shl->rm.endpos[0].col > mincol)
8041 {
8042 shl->lnum += shl->rm.startpos[0].lnum;
8043 break; /* useful match found */
8044 }
8045 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008046
8047 // Restore called_emsg for assert_fails().
8048 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050
Bram Moolenaar85077472016-10-16 14:35:48 +02008051/*
8052 * If there is a match fill "shl" and return one.
8053 * Return zero otherwise.
8054 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008055 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008056next_search_hl_pos(
8057 match_T *shl, /* points to a match */
8058 linenr_T lnum,
8059 posmatch_T *posmatch, /* match positions */
8060 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008061{
8062 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008063 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008064
Bram Moolenaarb3414592014-06-17 17:48:32 +02008065 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8066 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008067 llpos_T *pos = &posmatch->pos[i];
8068
8069 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008070 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008071 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008072 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008073 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008077 /* if this match comes before the one at "found" then swap
8078 * them */
8079 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008080 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008081 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082
Bram Moolenaar85077472016-10-16 14:35:48 +02008083 *pos = posmatch->pos[found];
8084 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008085 }
8086 }
8087 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008088 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008089 }
8090 }
8091 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008092 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008093 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008094 colnr_T start = posmatch->pos[found].col == 0
8095 ? 0 : posmatch->pos[found].col - 1;
8096 colnr_T end = posmatch->pos[found].col == 0
8097 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098
Bram Moolenaar85077472016-10-16 14:35:48 +02008099 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008100 shl->rm.startpos[0].lnum = 0;
8101 shl->rm.startpos[0].col = start;
8102 shl->rm.endpos[0].lnum = 0;
8103 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008104 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008105 posmatch->cur = found + 1;
8106 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008107 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008108 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008109}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008110#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008111
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008113screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115 attrentry_T *aep = NULL;
8116
8117 screen_attr = attr;
8118 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008119#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 && termcap_active
8121#endif
8122 )
8123 {
8124#ifdef FEAT_GUI
8125 if (gui.in_use)
8126 {
8127 char buf[20];
8128
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008129 /* The GUI handles this internally. */
8130 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 OUT_STR(buf);
8132 }
8133 else
8134#endif
8135 {
8136 if (attr > HL_ALL) /* special HL attr. */
8137 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008138 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 aep = syn_cterm_attr2entry(attr);
8140 else
8141 aep = syn_term_attr2entry(attr);
8142 if (aep == NULL) /* did ":syntax clear" */
8143 attr = 0;
8144 else
8145 attr = aep->ae_attr;
8146 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008147 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008149 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008150#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008151 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8152 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8153 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008154#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008155 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008156 /* If the Normal FG color has BOLD attribute and the new HL
8157 * has a FG color defined, clear BOLD. */
8158 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008159 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008161 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008162 out_str(T_UCS);
8163 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008164 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8165 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008167 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008169 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008171 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008172 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173
8174 /*
8175 * Output the color or start string after bold etc., in case the
8176 * bold etc. override the color setting.
8177 */
8178 if (aep != NULL)
8179 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008180#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008181 /* When 'termguicolors' is set but fg or bg is unset,
8182 * fall back to the cterm colors. This helps for SpellBad,
8183 * where the GUI uses a red undercurl. */
8184 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008186 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008187 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008188 }
8189 else
8190#endif
8191 if (t_colors > 1)
8192 {
8193 if (aep->ae_u.cterm.fg_color)
8194 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8195 }
8196#ifdef FEAT_TERMGUICOLORS
8197 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8198 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008199 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008200 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 }
8202 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008203#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008204 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008206 if (aep->ae_u.cterm.bg_color)
8207 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8208 }
8209
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008210 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008211 {
8212 if (aep->ae_u.term.start != NULL)
8213 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 }
8215 }
8216 }
8217 }
8218}
8219
8220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008221screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 int do_ME = FALSE; /* output T_ME code */
8224
8225 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008226#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 && termcap_active
8228#endif
8229 )
8230 {
8231#ifdef FEAT_GUI
8232 if (gui.in_use)
8233 {
8234 char buf[20];
8235
8236 /* use internal GUI code */
8237 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8238 OUT_STR(buf);
8239 }
8240 else
8241#endif
8242 {
8243 if (screen_attr > HL_ALL) /* special HL attr. */
8244 {
8245 attrentry_T *aep;
8246
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008247 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 /*
8250 * Assume that t_me restores the original colors!
8251 */
8252 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008253 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008254#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008255 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8256 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8257 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008258#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008259 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008260#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008261 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8262 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8263 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008264#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 do_ME = TRUE;
8267 }
8268 else
8269 {
8270 aep = syn_term_attr2entry(screen_attr);
8271 if (aep != NULL && aep->ae_u.term.stop != NULL)
8272 {
8273 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8274 do_ME = TRUE;
8275 else
8276 out_str(aep->ae_u.term.stop);
8277 }
8278 }
8279 if (aep == NULL) /* did ":syntax clear" */
8280 screen_attr = 0;
8281 else
8282 screen_attr = aep->ae_attr;
8283 }
8284
8285 /*
8286 * Often all ending-codes are equal to T_ME. Avoid outputting the
8287 * same sequence several times.
8288 */
8289 if (screen_attr & HL_STANDOUT)
8290 {
8291 if (STRCMP(T_SE, T_ME) == 0)
8292 do_ME = TRUE;
8293 else
8294 out_str(T_SE);
8295 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008296 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008297 {
8298 if (STRCMP(T_UCE, T_ME) == 0)
8299 do_ME = TRUE;
8300 else
8301 out_str(T_UCE);
8302 }
8303 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008304 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {
8306 if (STRCMP(T_UE, T_ME) == 0)
8307 do_ME = TRUE;
8308 else
8309 out_str(T_UE);
8310 }
8311 if (screen_attr & HL_ITALIC)
8312 {
8313 if (STRCMP(T_CZR, T_ME) == 0)
8314 do_ME = TRUE;
8315 else
8316 out_str(T_CZR);
8317 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008318 if (screen_attr & HL_STRIKETHROUGH)
8319 {
8320 if (STRCMP(T_STE, T_ME) == 0)
8321 do_ME = TRUE;
8322 else
8323 out_str(T_STE);
8324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8326 out_str(T_ME);
8327
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008328#ifdef FEAT_TERMGUICOLORS
8329 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008331 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008332 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008333 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008334 term_bg_rgb_color(cterm_normal_bg_gui_color);
8335 }
8336 else
8337#endif
8338 {
8339 if (t_colors > 1)
8340 {
8341 /* set Normal cterm colors */
8342 if (cterm_normal_fg_color != 0)
8343 term_fg_color(cterm_normal_fg_color - 1);
8344 if (cterm_normal_bg_color != 0)
8345 term_bg_color(cterm_normal_bg_color - 1);
8346 if (cterm_normal_fg_bold)
8347 out_str(T_MD);
8348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350 }
8351 }
8352 screen_attr = 0;
8353}
8354
8355/*
8356 * Reset the colors for a cterm. Used when leaving Vim.
8357 * The machine specific code may override this again.
8358 */
8359 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008360reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008362 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {
8364 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008365#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008366 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8367 || cterm_normal_bg_gui_color != INVALCOLOR)
8368 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {
8373 out_str(T_OP);
8374 screen_attr = -1;
8375 }
8376 if (cterm_normal_fg_bold)
8377 {
8378 out_str(T_ME);
8379 screen_attr = -1;
8380 }
8381 }
8382}
8383
8384/*
8385 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8386 * using the attributes from ScreenAttrs["off"].
8387 */
8388 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008389screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391 int attr;
8392
8393 /* Check for illegal values, just in case (could happen just after
8394 * resizing). */
8395 if (row >= screen_Rows || col >= screen_Columns)
8396 return;
8397
Bram Moolenaarae654382019-01-17 21:09:05 +01008398#ifdef FEAT_INS_EXPAND
8399 if (pum_under_menu(row, col))
8400 return;
8401#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008402 /* Outputting a character in the last cell on the screen may scroll the
8403 * screen up. Only do it when the "xn" termcap property is set, otherwise
8404 * mark the character invalid (update it when scrolled up). */
8405 if (*T_XN == NUL
8406 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407#ifdef FEAT_RIGHTLEFT
8408 /* account for first command-line character in rightleft mode */
8409 && !cmdmsg_rl
8410#endif
8411 )
8412 {
8413 ScreenAttrs[off] = (sattr_T)-1;
8414 return;
8415 }
8416
8417 /*
8418 * Stop highlighting first, so it's easier to move the cursor.
8419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 if (screen_char_attr != 0)
8421 attr = screen_char_attr;
8422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 attr = ScreenAttrs[off];
8424 if (screen_attr != attr)
8425 screen_stop_highlight();
8426
8427 windgoto(row, col);
8428
8429 if (screen_attr != attr)
8430 screen_start_highlight(attr);
8431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (enc_utf8 && ScreenLinesUC[off] != 0)
8433 {
8434 char_u buf[MB_MAXBYTES + 1];
8435
Bram Moolenaarcb070082016-04-02 22:14:51 +02008436 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008437 {
8438 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008439#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008440 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008441#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008442 )
8443 {
8444 /* Clear the two screen cells. If the character is actually
8445 * single width it won't change the second cell. */
8446 out_str((char_u *)" ");
8447 term_windgoto(row, col);
8448 }
8449 /* not sure where the cursor is after drawing the ambiguous width
8450 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008451 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008452 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008453 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008455
8456 /* Convert the UTF-8 character to bytes and write it. */
8457 buf[utfc_char2bytes(off, buf)] = NUL;
8458 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 /* double-byte character in single-width cell */
8465 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8466 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 }
8468
8469 screen_cur_col++;
8470}
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472/*
8473 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8474 * on the screen at position 'row' and 'col'.
8475 * The attributes of the first byte is used for all. This is required to
8476 * output the two bytes of a double-byte character with nothing in between.
8477 */
8478 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008479screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480{
8481 /* Check for illegal values (could be wrong when screen was resized). */
8482 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8483 return;
8484
8485 /* Outputting the last character on the screen may scrollup the screen.
8486 * Don't to it! Mark the character invalid (update it when scrolled up) */
8487 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8488 {
8489 ScreenAttrs[off] = (sattr_T)-1;
8490 return;
8491 }
8492
8493 /* Output the first byte normally (positions the cursor), then write the
8494 * second byte directly. */
8495 screen_char(off, row, col);
8496 out_char(ScreenLines[off + 1]);
8497 ++screen_cur_col;
8498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500/*
8501 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8502 * This uses the contents of ScreenLines[] and doesn't change it.
8503 */
8504 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008505screen_draw_rectangle(
8506 int row,
8507 int col,
8508 int height,
8509 int width,
8510 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
8512 int r, c;
8513 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008514 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008516 /* Can't use ScreenLines unless initialized */
8517 if (ScreenLines == NULL)
8518 return;
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (invert)
8521 screen_char_attr = HL_INVERSE;
8522 for (r = row; r < row + height; ++r)
8523 {
8524 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008525 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 for (c = col; c < col + width; ++c)
8527 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008528 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 {
8530 screen_char_2(off + c, r, c);
8531 ++c;
8532 }
8533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {
8535 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008536 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 }
8539 }
8540 }
8541 screen_char_attr = 0;
8542}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544/*
8545 * Redraw the characters for a vertically split window.
8546 */
8547 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008548redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
8550 int col;
8551 int width;
8552
8553# ifdef FEAT_CLIPBOARD
8554 clip_may_clear_selection(row, end - 1);
8555# endif
8556
8557 if (wp == NULL)
8558 {
8559 col = 0;
8560 width = Columns;
8561 }
8562 else
8563 {
8564 col = wp->w_wincol;
8565 width = wp->w_width;
8566 }
8567 screen_draw_rectangle(row, col, end - row, width, FALSE);
8568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008570 static void
8571space_to_screenline(int off, int attr)
8572{
8573 ScreenLines[off] = ' ';
8574 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008575 if (enc_utf8)
8576 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008577}
8578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579/*
8580 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8581 * with character 'c1' in first column followed by 'c2' in the other columns.
8582 * Use attributes 'attr'.
8583 */
8584 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008585screen_fill(
8586 int start_row,
8587 int end_row,
8588 int start_col,
8589 int end_col,
8590 int c1,
8591 int c2,
8592 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 int row;
8595 int col;
8596 int off;
8597 int end_off;
8598 int did_delete;
8599 int c;
8600 int norm_term;
8601#if defined(FEAT_GUI) || defined(UNIX)
8602 int force_next = FALSE;
8603#endif
8604
8605 if (end_row > screen_Rows) /* safety check */
8606 end_row = screen_Rows;
8607 if (end_col > screen_Columns) /* safety check */
8608 end_col = screen_Columns;
8609 if (ScreenLines == NULL
8610 || start_row >= end_row
8611 || start_col >= end_col) /* nothing to do */
8612 return;
8613
8614 /* it's a "normal" terminal when not in a GUI or cterm */
8615 norm_term = (
8616#ifdef FEAT_GUI
8617 !gui.in_use &&
8618#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008619 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 for (row = start_row; row < end_row; ++row)
8621 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008622 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008623#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008624 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008625#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008626 )
8627 {
8628 /* When drawing over the right halve of a double-wide char clear
8629 * out the left halve. When drawing over the left halve of a
8630 * double wide-char clear out the right halve. Only needed in a
8631 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008632 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008633 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008634 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008635 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 /*
8638 * Try to use delete-line termcap code, when no attributes or in a
8639 * "normal" terminal, where a bold/italic space is just a
8640 * space.
8641 */
8642 did_delete = FALSE;
8643 if (c2 == ' '
8644 && end_col == Columns
8645 && can_clear(T_CE)
8646 && (attr == 0
8647 || (norm_term
8648 && attr <= HL_ALL
8649 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8650 {
8651 /*
8652 * check if we really need to clear something
8653 */
8654 col = start_col;
8655 if (c1 != ' ') /* don't clear first char */
8656 ++col;
8657
8658 off = LineOffset[row] + col;
8659 end_off = LineOffset[row] + end_col;
8660
8661 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 if (enc_utf8)
8663 while (off < end_off && ScreenLines[off] == ' '
8664 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8665 ++off;
8666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 while (off < end_off && ScreenLines[off] == ' '
8668 && ScreenAttrs[off] == 0)
8669 ++off;
8670 if (off < end_off) /* something to be cleared */
8671 {
8672 col = off - LineOffset[row];
8673 screen_stop_highlight();
8674 term_windgoto(row, col);/* clear rest of this screen line */
8675 out_str(T_CE);
8676 screen_start(); /* don't know where cursor is now */
8677 col = end_col - col;
8678 while (col--) /* clear chars in ScreenLines */
8679 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008680 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 ++off;
8682 }
8683 }
8684 did_delete = TRUE; /* the chars are cleared now */
8685 }
8686
8687 off = LineOffset[row] + start_col;
8688 c = c1;
8689 for (col = start_col; col < end_col; ++col)
8690 {
8691 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008692 || (enc_utf8 && (int)ScreenLinesUC[off]
8693 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 || ScreenAttrs[off] != attr
8695#if defined(FEAT_GUI) || defined(UNIX)
8696 || force_next
8697#endif
8698 )
8699 {
8700#if defined(FEAT_GUI) || defined(UNIX)
8701 /* The bold trick may make a single row of pixels appear in
8702 * the next character. When a bold character is removed, the
8703 * next character should be redrawn too. This happens for our
8704 * own GUI and for some xterms. */
8705 if (
8706# ifdef FEAT_GUI
8707 gui.in_use
8708# endif
8709# if defined(FEAT_GUI) && defined(UNIX)
8710 ||
8711# endif
8712# ifdef UNIX
8713 term_is_xterm
8714# endif
8715 )
8716 {
8717 if (ScreenLines[off] != ' '
8718 && (ScreenAttrs[off] > HL_ALL
8719 || ScreenAttrs[off] & HL_BOLD))
8720 force_next = TRUE;
8721 else
8722 force_next = FALSE;
8723 }
8724#endif
8725 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 if (enc_utf8)
8727 {
8728 if (c >= 0x80)
8729 {
8730 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008731 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 }
8733 else
8734 ScreenLinesUC[off] = 0;
8735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 ScreenAttrs[off] = attr;
8737 if (!did_delete || c != ' ')
8738 screen_char(off, row, col);
8739 }
8740 ++off;
8741 if (col == start_col)
8742 {
8743 if (did_delete)
8744 break;
8745 c = c2;
8746 }
8747 }
8748 if (end_col == Columns)
8749 LineWraps[row] = FALSE;
8750 if (row == Rows - 1) /* overwritten the command line */
8751 {
8752 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008753 if (start_col == 0 && end_col == Columns
8754 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008756 if (start_col == 0)
8757 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
8759 }
8760}
8761
8762/*
8763 * Check if there should be a delay. Used before clearing or redrawing the
8764 * screen or the command line.
8765 */
8766 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008767check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8770 && !did_wait_return
8771 && emsg_silent == 0)
8772 {
8773 out_flush();
8774 ui_delay(1000L, TRUE);
8775 emsg_on_display = FALSE;
8776 if (check_msg_scroll)
8777 msg_scroll = FALSE;
8778 }
8779}
8780
8781/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008782 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8783 */
8784 static void
8785clear_TabPageIdxs(void)
8786{
8787 int scol;
8788
8789 for (scol = 0; scol < Columns; ++scol)
8790 TabPageIdxs[scol] = 0;
8791}
8792
8793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008795 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 * Returns TRUE if there is a valid screen to write to.
8797 * Returns FALSE when starting up and screen not initialized yet.
8798 */
8799 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008800screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008802 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 return (ScreenLines != NULL);
8804}
8805
8806/*
8807 * Resize the shell to Rows and Columns.
8808 * Allocate ScreenLines[] and associated items.
8809 *
8810 * There may be some time between setting Rows and Columns and (re)allocating
8811 * ScreenLines[]. This happens when starting up and when (manually) changing
8812 * the shell size. Always use screen_Rows and screen_Columns to access items
8813 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8814 * final size of the shell is needed.
8815 */
8816 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008817screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
8819 int new_row, old_row;
8820#ifdef FEAT_GUI
8821 int old_Rows;
8822#endif
8823 win_T *wp;
8824 int outofmem = FALSE;
8825 int len;
8826 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008828 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008830 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 sattr_T *new_ScreenAttrs;
8832 unsigned *new_LineOffset;
8833 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008834 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008835 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008837 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008838 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008840retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 /*
8842 * Allocation of the screen buffers is done only when the size changes and
8843 * when Rows and Columns have been set and we have started doing full
8844 * screen stuff.
8845 */
8846 if ((ScreenLines != NULL
8847 && Rows == screen_Rows
8848 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 && enc_utf8 == (ScreenLinesUC != NULL)
8850 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008851 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 || Rows == 0
8853 || Columns == 0
8854 || (!full_screen && ScreenLines == NULL))
8855 return;
8856
8857 /*
8858 * It's possible that we produce an out-of-memory message below, which
8859 * will cause this function to be called again. To break the loop, just
8860 * return here.
8861 */
8862 if (entered)
8863 return;
8864 entered = TRUE;
8865
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008866 /*
8867 * Note that the window sizes are updated before reallocating the arrays,
8868 * thus we must not redraw here!
8869 */
8870 ++RedrawingDisabled;
8871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 win_new_shellsize(); /* fit the windows in the new sized shell */
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 comp_col(); /* recompute columns for shown command and ruler */
8875
8876 /*
8877 * We're changing the size of the screen.
8878 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8879 * - Move lines from the old arrays into the new arrays, clear extra
8880 * lines (unless the screen is going to be cleared).
8881 * - Free the old arrays.
8882 *
8883 * If anything fails, make ScreenLines NULL, so we don't do anything!
8884 * Continuing with the old ScreenLines may result in a crash, because the
8885 * size is wrong.
8886 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008887 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008889 if (aucmd_win != NULL)
8890 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008892 new_ScreenLines = (schar_T *)lalloc(
8893 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008894 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 if (enc_utf8)
8896 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008897 new_ScreenLinesUC = (u8char_T *)lalloc(
8898 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008899 for (i = 0; i < p_mco; ++i)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008900 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear(
8901 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 }
8903 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008904 new_ScreenLines2 = (schar_T *)lalloc(
8905 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
8906 new_ScreenAttrs = (sattr_T *)lalloc(
8907 (Rows + 1) * Columns * sizeof(sattr_T), FALSE);
8908 new_LineOffset = (unsigned *)lalloc(Rows * sizeof(unsigned), FALSE);
8909 new_LineWraps = (char_u *)lalloc(Rows * sizeof(char_u), FALSE);
8910 new_TabPageIdxs = (short *)lalloc(Columns * sizeof(short), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008912 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
8914 if (win_alloc_lines(wp) == FAIL)
8915 {
8916 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008917 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008920 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8921 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008922 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008923give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008925 for (i = 0; i < p_mco; ++i)
8926 if (new_ScreenLinesC[i] == NULL)
8927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008929 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 || new_ScreenAttrs == NULL
8932 || new_LineOffset == NULL
8933 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008934 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 || outofmem)
8936 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008937 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008938 {
8939 /* guess the size */
8940 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8941
8942 /* Remember we did this to avoid getting outofmem messages over
8943 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008944 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008945 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008946 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008947 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008948 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008949 VIM_CLEAR(new_ScreenLinesC[i]);
8950 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008951 VIM_CLEAR(new_ScreenAttrs);
8952 VIM_CLEAR(new_LineOffset);
8953 VIM_CLEAR(new_LineWraps);
8954 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 }
8956 else
8957 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008958 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008959
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 for (new_row = 0; new_row < Rows; ++new_row)
8961 {
8962 new_LineOffset[new_row] = new_row * Columns;
8963 new_LineWraps[new_row] = FALSE;
8964
8965 /*
8966 * If the screen is not going to be cleared, copy as much as
8967 * possible from the old screen to the new one and clear the rest
8968 * (used when resizing the window at the "--more--" prompt or when
8969 * executing an external command, for the GUI).
8970 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008971 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 {
8973 (void)vim_memset(new_ScreenLines + new_row * Columns,
8974 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 if (enc_utf8)
8976 {
8977 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8978 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008979 for (i = 0; i < p_mco; ++i)
8980 (void)vim_memset(new_ScreenLinesC[i]
8981 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 0, (size_t)Columns * sizeof(u8char_T));
8983 }
8984 if (enc_dbcs == DBCS_JPNU)
8985 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8986 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8988 0, (size_t)Columns * sizeof(sattr_T));
8989 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008990 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 {
8992 if (screen_Columns < Columns)
8993 len = screen_Columns;
8994 else
8995 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008996 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008997 * may be invalid now. Also when p_mco changes. */
8998 if (!(enc_utf8 && ScreenLinesUC == NULL)
8999 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009000 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9001 ScreenLines + LineOffset[old_row],
9002 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009003 if (enc_utf8 && ScreenLinesUC != NULL
9004 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 {
9006 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9007 ScreenLinesUC + LineOffset[old_row],
9008 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 for (i = 0; i < p_mco; ++i)
9010 mch_memmove(new_ScreenLinesC[i]
9011 + new_LineOffset[new_row],
9012 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 (size_t)len * sizeof(u8char_T));
9014 }
9015 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9016 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9017 ScreenLines2 + LineOffset[old_row],
9018 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9020 ScreenAttrs + LineOffset[old_row],
9021 (size_t)len * sizeof(sattr_T));
9022 }
9023 }
9024 }
9025 /* Use the last line of the screen for the current line. */
9026 current_ScreenLine = new_ScreenLines + Rows * Columns;
9027 }
9028
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009029 free_screenlines();
9030
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009033 for (i = 0; i < p_mco; ++i)
9034 ScreenLinesC[i] = new_ScreenLinesC[i];
9035 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 ScreenAttrs = new_ScreenAttrs;
9038 LineOffset = new_LineOffset;
9039 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009040 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
9042 /* It's important that screen_Rows and screen_Columns reflect the actual
9043 * size of ScreenLines[]. Set them before calling anything. */
9044#ifdef FEAT_GUI
9045 old_Rows = screen_Rows;
9046#endif
9047 screen_Rows = Rows;
9048 screen_Columns = Columns;
9049
9050 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009051 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053#ifdef FEAT_GUI
9054 else if (gui.in_use
9055 && !gui.starting
9056 && ScreenLines != NULL
9057 && old_Rows != Rows)
9058 {
9059 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9060 /*
9061 * Adjust the position of the cursor, for when executing an external
9062 * command.
9063 */
9064 if (msg_row >= Rows) /* Rows got smaller */
9065 msg_row = Rows - 1; /* put cursor at last row */
9066 else if (Rows > old_Rows) /* Rows got bigger */
9067 msg_row += Rows - old_Rows; /* put cursor in same place */
9068 if (msg_col >= Columns) /* Columns got smaller */
9069 msg_col = Columns - 1; /* put cursor at last column */
9070 }
9071#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009072 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009075 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009076
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009077 /*
9078 * Do not apply autocommands more than 3 times to avoid an endless loop
9079 * in case applying autocommands always changes Rows or Columns.
9080 */
9081 if (starting == 0 && ++retry_count <= 3)
9082 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009083 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009084 /* In rare cases, autocommands may have altered Rows or Columns,
9085 * jump back to check if we need to allocate the screen again. */
9086 goto retry;
9087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088}
9089
9090 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009091free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009092{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009093 int i;
9094
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009095 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009096 for (i = 0; i < Screen_mco; ++i)
9097 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009098 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009099 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009100 vim_free(ScreenAttrs);
9101 vim_free(LineOffset);
9102 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009103 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009104}
9105
9106 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009107screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 check_for_delay(FALSE);
9110 screenalloc(FALSE); /* allocate screen buffers if size changed */
9111 screenclear2(); /* clear the screen */
9112}
9113
9114 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009115screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116{
9117 int i;
9118
9119 if (starting == NO_SCREEN || ScreenLines == NULL
9120#ifdef FEAT_GUI
9121 || (gui.in_use && gui.starting)
9122#endif
9123 )
9124 return;
9125
9126#ifdef FEAT_GUI
9127 if (!gui.in_use)
9128#endif
9129 screen_attr = -1; /* force setting the Normal colors */
9130 screen_stop_highlight(); /* don't want highlighting here */
9131
9132#ifdef FEAT_CLIPBOARD
9133 /* disable selection without redrawing it */
9134 clip_scroll_selection(9999);
9135#endif
9136
9137 /* blank out ScreenLines */
9138 for (i = 0; i < Rows; ++i)
9139 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009140 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 LineWraps[i] = FALSE;
9142 }
9143
9144 if (can_clear(T_CL))
9145 {
9146 out_str(T_CL); /* clear the display */
9147 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009148 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 }
9150 else
9151 {
9152 /* can't clear the screen, mark all chars with invalid attributes */
9153 for (i = 0; i < Rows; ++i)
9154 lineinvalid(LineOffset[i], (int)Columns);
9155 clear_cmdline = TRUE;
9156 }
9157
9158 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9159
9160 win_rest_invalid(firstwin);
9161 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009162 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 if (must_redraw == CLEAR) /* no need to clear again */
9164 must_redraw = NOT_VALID;
9165 compute_cmdrow();
9166 msg_row = cmdline_row; /* put cursor on last line for messages */
9167 msg_col = 0;
9168 screen_start(); /* don't know where cursor is now */
9169 msg_scrolled = 0; /* can't scroll back */
9170 msg_didany = FALSE;
9171 msg_didout = FALSE;
9172}
9173
9174/*
9175 * Clear one line in ScreenLines.
9176 */
9177 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009178lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 if (enc_utf8)
9182 (void)vim_memset(ScreenLinesUC + off, 0,
9183 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009184 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185}
9186
9187/*
9188 * Mark one line in ScreenLines invalid by setting the attributes to an
9189 * invalid value.
9190 */
9191 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009192lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9195}
9196
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197/*
9198 * Copy part of a Screenline for vertically split window "wp".
9199 */
9200 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009201linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 unsigned off_to = LineOffset[to] + wp->w_wincol;
9204 unsigned off_from = LineOffset[from] + wp->w_wincol;
9205
9206 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9207 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 if (enc_utf8)
9209 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009210 int i;
9211
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9213 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009214 for (i = 0; i < p_mco; ++i)
9215 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9216 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 }
9218 if (enc_dbcs == DBCS_JPNU)
9219 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9220 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9222 wp->w_width * sizeof(sattr_T));
9223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225/*
9226 * Return TRUE if clearing with term string "p" would work.
9227 * It can't work when the string is empty or it won't set the right background.
9228 */
9229 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009230can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 return (*p != NUL && (t_colors <= 1
9233#ifdef FEAT_GUI
9234 || gui.in_use
9235#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009236#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009237 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009238 || (!p_tgc && cterm_normal_bg_color == 0)
9239#else
9240 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009241#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009242 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243}
9244
9245/*
9246 * Reset cursor position. Use whenever cursor was moved because of outputting
9247 * something directly to the screen (shell commands) or a terminal control
9248 * code.
9249 */
9250 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009251screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252{
9253 screen_cur_row = screen_cur_col = 9999;
9254}
9255
9256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 * Move the cursor to position "row","col" in the screen.
9258 * This tries to find the most efficient way to move, minimizing the number of
9259 * characters sent to the terminal.
9260 */
9261 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009262windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009264 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 int i;
9266 int plan;
9267 int cost;
9268 int wouldbe_col;
9269 int noinvcurs;
9270 char_u *bs;
9271 int goto_cost;
9272 int attr;
9273
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009274#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9276
9277#define PLAN_LE 1
9278#define PLAN_CR 2
9279#define PLAN_NL 3
9280#define PLAN_WRITE 4
9281 /* Can't use ScreenLines unless initialized */
9282 if (ScreenLines == NULL)
9283 return;
9284
9285 if (col != screen_cur_col || row != screen_cur_row)
9286 {
9287 /* Check for valid position. */
9288 if (row < 0) /* window without text lines? */
9289 row = 0;
9290 if (row >= screen_Rows)
9291 row = screen_Rows - 1;
9292 if (col >= screen_Columns)
9293 col = screen_Columns - 1;
9294
9295 /* check if no cursor movement is allowed in highlight mode */
9296 if (screen_attr && *T_MS == NUL)
9297 noinvcurs = HIGHL_COST;
9298 else
9299 noinvcurs = 0;
9300 goto_cost = GOTO_COST + noinvcurs;
9301
9302 /*
9303 * Plan how to do the positioning:
9304 * 1. Use CR to move it to column 0, same row.
9305 * 2. Use T_LE to move it a few columns to the left.
9306 * 3. Use NL to move a few lines down, column 0.
9307 * 4. Move a few columns to the right with T_ND or by writing chars.
9308 *
9309 * Don't do this if the cursor went beyond the last column, the cursor
9310 * position is unknown then (some terminals wrap, some don't )
9311 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009312 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 * characters to move the cursor to the right.
9314 */
9315 if (row >= screen_cur_row && screen_cur_col < Columns)
9316 {
9317 /*
9318 * If the cursor is in the same row, bigger col, we can use CR
9319 * or T_LE.
9320 */
9321 bs = NULL; /* init for GCC */
9322 attr = screen_attr;
9323 if (row == screen_cur_row && col < screen_cur_col)
9324 {
9325 /* "le" is preferred over "bc", because "bc" is obsolete */
9326 if (*T_LE)
9327 bs = T_LE; /* "cursor left" */
9328 else
9329 bs = T_BC; /* "backspace character (old) */
9330 if (*bs)
9331 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9332 else
9333 cost = 999;
9334 if (col + 1 < cost) /* using CR is less characters */
9335 {
9336 plan = PLAN_CR;
9337 wouldbe_col = 0;
9338 cost = 1; /* CR is just one character */
9339 }
9340 else
9341 {
9342 plan = PLAN_LE;
9343 wouldbe_col = col;
9344 }
9345 if (noinvcurs) /* will stop highlighting */
9346 {
9347 cost += noinvcurs;
9348 attr = 0;
9349 }
9350 }
9351
9352 /*
9353 * If the cursor is above where we want to be, we can use CR LF.
9354 */
9355 else if (row > screen_cur_row)
9356 {
9357 plan = PLAN_NL;
9358 wouldbe_col = 0;
9359 cost = (row - screen_cur_row) * 2; /* CR LF */
9360 if (noinvcurs) /* will stop highlighting */
9361 {
9362 cost += noinvcurs;
9363 attr = 0;
9364 }
9365 }
9366
9367 /*
9368 * If the cursor is in the same row, smaller col, just use write.
9369 */
9370 else
9371 {
9372 plan = PLAN_WRITE;
9373 wouldbe_col = screen_cur_col;
9374 cost = 0;
9375 }
9376
9377 /*
9378 * Check if any characters that need to be written have the
9379 * correct attributes. Also avoid UTF-8 characters.
9380 */
9381 i = col - wouldbe_col;
9382 if (i > 0)
9383 cost += i;
9384 if (cost < goto_cost && i > 0)
9385 {
9386 /*
9387 * Check if the attributes are correct without additionally
9388 * stopping highlighting.
9389 */
9390 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9391 while (i && *p++ == attr)
9392 --i;
9393 if (i != 0)
9394 {
9395 /*
9396 * Try if it works when highlighting is stopped here.
9397 */
9398 if (*--p == 0)
9399 {
9400 cost += noinvcurs;
9401 while (i && *p++ == 0)
9402 --i;
9403 }
9404 if (i != 0)
9405 cost = 999; /* different attributes, don't do it */
9406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 if (enc_utf8)
9408 {
9409 /* Don't use an UTF-8 char for positioning, it's slow. */
9410 for (i = wouldbe_col; i < col; ++i)
9411 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9412 {
9413 cost = 999;
9414 break;
9415 }
9416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 }
9418
9419 /*
9420 * We can do it without term_windgoto()!
9421 */
9422 if (cost < goto_cost)
9423 {
9424 if (plan == PLAN_LE)
9425 {
9426 if (noinvcurs)
9427 screen_stop_highlight();
9428 while (screen_cur_col > col)
9429 {
9430 out_str(bs);
9431 --screen_cur_col;
9432 }
9433 }
9434 else if (plan == PLAN_CR)
9435 {
9436 if (noinvcurs)
9437 screen_stop_highlight();
9438 out_char('\r');
9439 screen_cur_col = 0;
9440 }
9441 else if (plan == PLAN_NL)
9442 {
9443 if (noinvcurs)
9444 screen_stop_highlight();
9445 while (screen_cur_row < row)
9446 {
9447 out_char('\n');
9448 ++screen_cur_row;
9449 }
9450 screen_cur_col = 0;
9451 }
9452
9453 i = col - screen_cur_col;
9454 if (i > 0)
9455 {
9456 /*
9457 * Use cursor-right if it's one character only. Avoids
9458 * removing a line of pixels from the last bold char, when
9459 * using the bold trick in the GUI.
9460 */
9461 if (T_ND[0] != NUL && T_ND[1] == NUL)
9462 {
9463 while (i-- > 0)
9464 out_char(*T_ND);
9465 }
9466 else
9467 {
9468 int off;
9469
9470 off = LineOffset[row] + screen_cur_col;
9471 while (i-- > 0)
9472 {
9473 if (ScreenAttrs[off] != screen_attr)
9474 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (enc_dbcs == DBCS_JPNU
9478 && ScreenLines[off] == 0x8e)
9479 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 ++off;
9481 }
9482 }
9483 }
9484 }
9485 }
9486 else
9487 cost = 999;
9488
9489 if (cost >= goto_cost)
9490 {
9491 if (noinvcurs)
9492 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009493 if (row == screen_cur_row && (col > screen_cur_col)
9494 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 term_cursor_right(col - screen_cur_col);
9496 else
9497 term_windgoto(row, col);
9498 }
9499 screen_cur_row = row;
9500 screen_cur_col = col;
9501 }
9502}
9503
9504/*
9505 * Set cursor to its position in the current window.
9506 */
9507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009508setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009510 setcursor_mayforce(FALSE);
9511}
9512
9513/*
9514 * Set cursor to its position in the current window.
9515 * When "force" is TRUE also when not redrawing.
9516 */
9517 void
9518setcursor_mayforce(int force)
9519{
9520 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
9522 validate_cursor();
9523 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009524 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009526 /* With 'rightleft' set and the cursor on a double-wide
9527 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009528 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9529 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009530 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009531 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532#endif
9533 curwin->w_wcol));
9534 }
9535}
9536
9537
9538/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009539 * Insert 'line_count' lines at 'row' in window 'wp'.
9540 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9541 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 * scrolling.
9543 * Returns FAIL if the lines are not inserted, OK for success.
9544 */
9545 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009546win_ins_lines(
9547 win_T *wp,
9548 int row,
9549 int line_count,
9550 int invalid,
9551 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 int did_delete;
9554 int nextrow;
9555 int lastrow;
9556 int retval;
9557
9558 if (invalid)
9559 wp->w_lines_valid = 0;
9560
9561 if (wp->w_height < 5)
9562 return FAIL;
9563
9564 if (line_count > wp->w_height - row)
9565 line_count = wp->w_height - row;
9566
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009567 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 if (retval != MAYBE)
9569 return retval;
9570
9571 /*
9572 * If there is a next window or a status line, we first try to delete the
9573 * lines at the bottom to avoid messing what is after the window.
9574 * If this fails and there are following windows, don't do anything to avoid
9575 * messing up those windows, better just redraw.
9576 */
9577 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 if (wp->w_next != NULL || wp->w_status_height)
9579 {
9580 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009581 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 did_delete = TRUE;
9583 else if (wp->w_next)
9584 return FAIL;
9585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 /*
9587 * if no lines deleted, blank the lines that will end up below the window
9588 */
9589 if (!did_delete)
9590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009593 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 lastrow = nextrow + line_count;
9595 if (lastrow > Rows)
9596 lastrow = Rows;
9597 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009598 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 ' ', ' ', 0);
9600 }
9601
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009602 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 == FAIL)
9604 {
9605 /* deletion will have messed up other windows */
9606 if (did_delete)
9607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 win_rest_invalid(W_NEXT(wp));
9610 }
9611 return FAIL;
9612 }
9613
9614 return OK;
9615}
9616
9617/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009618 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9620 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9621 * scrolling
9622 * Return OK for success, FAIL if the lines are not deleted.
9623 */
9624 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009625win_del_lines(
9626 win_T *wp,
9627 int row,
9628 int line_count,
9629 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009630 int mayclear,
9631 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633 int retval;
9634
9635 if (invalid)
9636 wp->w_lines_valid = 0;
9637
9638 if (line_count > wp->w_height - row)
9639 line_count = wp->w_height - row;
9640
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009641 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 if (retval != MAYBE)
9643 return retval;
9644
9645 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009646 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 return FAIL;
9648
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 /*
9650 * If there are windows or status lines below, try to put them at the
9651 * correct place. If we can't do that, they have to be redrawn.
9652 */
9653 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9654 {
9655 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009656 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
9658 wp->w_redr_status = TRUE;
9659 win_rest_invalid(wp->w_next);
9660 }
9661 }
9662 /*
9663 * If this is the last window and there is no status line, redraw the
9664 * command line later.
9665 */
9666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 redraw_cmdline = TRUE;
9668 return OK;
9669}
9670
9671/*
9672 * Common code for win_ins_lines() and win_del_lines().
9673 * Returns OK or FAIL when the work has been done.
9674 * Returns MAYBE when not finished yet.
9675 */
9676 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009677win_do_lines(
9678 win_T *wp,
9679 int row,
9680 int line_count,
9681 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009682 int del,
9683 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684{
9685 int retval;
9686
9687 if (!redrawing() || line_count <= 0)
9688 return FAIL;
9689
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009690 /* When inserting lines would result in loss of command output, just redraw
9691 * the lines. */
9692 if (no_win_do_lines_ins && !del)
9693 return FAIL;
9694
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009696 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009698 if (!no_win_do_lines_ins)
9699 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 return FAIL;
9701 }
9702
9703 /*
9704 * Delete all remaining lines
9705 */
9706 if (row + line_count >= wp->w_height)
9707 {
9708 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009709 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 ' ', ' ', 0);
9711 return OK;
9712 }
9713
9714 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009715 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009717 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009719 if (!no_win_do_lines_ins)
9720 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
9722 /*
9723 * If the terminal can set a scroll region, use that.
9724 * Always do this in a vertically split window. This will redraw from
9725 * ScreenLines[] when t_CV isn't defined. That's faster than using
9726 * win_line().
9727 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009728 * a character in the lower right corner of the scroll region may cause a
9729 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009731 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 scroll_region_set(wp, row);
9735 if (del)
9736 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009737 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 else
9739 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009740 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 scroll_region_reset();
9743 return retval;
9744 }
9745
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748
9749 return MAYBE;
9750}
9751
9752/*
9753 * window 'wp' and everything after it is messed up, mark it for redraw
9754 */
9755 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009756win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 {
9760 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 wp->w_redr_status = TRUE;
9762 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 }
9764 redraw_cmdline = TRUE;
9765}
9766
9767/*
9768 * The rest of the routines in this file perform screen manipulations. The
9769 * given operation is performed physically on the screen. The corresponding
9770 * change is also made to the internal screen image. In this way, the editor
9771 * anticipates the effect of editing changes on the appearance of the screen.
9772 * That way, when we call screenupdate a complete redraw isn't usually
9773 * necessary. Another advantage is that we can keep adding code to anticipate
9774 * screen changes, and in the meantime, everything still works.
9775 */
9776
9777/*
9778 * types for inserting or deleting lines
9779 */
9780#define USE_T_CAL 1
9781#define USE_T_CDL 2
9782#define USE_T_AL 3
9783#define USE_T_CE 4
9784#define USE_T_DL 5
9785#define USE_T_SR 6
9786#define USE_NL 7
9787#define USE_T_CD 8
9788#define USE_REDRAW 9
9789
9790/*
9791 * insert lines on the screen and update ScreenLines[]
9792 * 'end' is the line after the scrolled part. Normally it is Rows.
9793 * When scrolling region used 'off' is the offset from the top for the region.
9794 * 'row' and 'end' are relative to the start of the region.
9795 *
9796 * return FAIL for failure, OK for success.
9797 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009798 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009799screen_ins_lines(
9800 int off,
9801 int row,
9802 int line_count,
9803 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009804 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009805 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 int i;
9808 int j;
9809 unsigned temp;
9810 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009811 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 int type;
9813 int result_empty;
9814 int can_ce = can_clear(T_CE);
9815
9816 /*
9817 * FAIL if
9818 * - there is no valid screen
9819 * - the screen has to be redrawn completely
9820 * - the line count is less than one
9821 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009822 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009824 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9825#ifdef FEAT_CLIPBOARD
9826 || (clip_star.state != SELECT_CLEARED
9827 && redrawing_for_callback > 0)
9828#endif
9829 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 return FAIL;
9831
9832 /*
9833 * There are seven ways to insert lines:
9834 * 0. When in a vertically split window and t_CV isn't set, redraw the
9835 * characters from ScreenLines[].
9836 * 1. Use T_CD (clear to end of display) if it exists and the result of
9837 * the insert is just empty lines
9838 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9839 * present or line_count > 1. It looks better if we do all the inserts
9840 * at once.
9841 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9842 * insert is just empty lines and T_CE is not present or line_count >
9843 * 1.
9844 * 4. Use T_AL (insert line) if it exists.
9845 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9846 * just empty lines.
9847 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9848 * just empty lines.
9849 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9850 * the 'da' flag is not set or we have clear line capability.
9851 * 8. redraw the characters from ScreenLines[].
9852 *
9853 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9854 * the scrollbar for the window. It does have insert line, use that if it
9855 * exists.
9856 */
9857 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9859 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009860 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 type = USE_T_CD;
9862 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9863 type = USE_T_CAL;
9864 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9865 type = USE_T_CDL;
9866 else if (*T_AL != NUL)
9867 type = USE_T_AL;
9868 else if (can_ce && result_empty)
9869 type = USE_T_CE;
9870 else if (*T_DL != NUL && result_empty)
9871 type = USE_T_DL;
9872 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9873 type = USE_T_SR;
9874 else
9875 return FAIL;
9876
9877 /*
9878 * For clearing the lines screen_del_lines() is used. This will also take
9879 * care of t_db if necessary.
9880 */
9881 if (type == USE_T_CD || type == USE_T_CDL ||
9882 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009883 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
9885 /*
9886 * If text is retained below the screen, first clear or delete as many
9887 * lines at the bottom of the window as are about to be inserted so that
9888 * the deleted lines won't later surface during a screen_del_lines.
9889 */
9890 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009891 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892
9893#ifdef FEAT_CLIPBOARD
9894 /* Remove a modeless selection when inserting lines halfway the screen
9895 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009896 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009897 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 else
9899 clip_scroll_selection(-line_count);
9900#endif
9901
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902#ifdef FEAT_GUI
9903 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9904 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009905 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906#endif
9907
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009908 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9909 cursor_col = wp->w_wincol;
9910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 if (*T_CCS != NUL) /* cursor relative to region */
9912 cursor_row = row;
9913 else
9914 cursor_row = row + off;
9915
9916 /*
9917 * Shift LineOffset[] line_count down to reflect the inserted lines.
9918 * Clear the inserted lines in ScreenLines[].
9919 */
9920 row += off;
9921 end += off;
9922 for (i = 0; i < line_count; ++i)
9923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 if (wp != NULL && wp->w_width != Columns)
9925 {
9926 /* need to copy part of a line */
9927 j = end - 1 - i;
9928 while ((j -= line_count) >= row)
9929 linecopy(j + line_count, j, wp);
9930 j += line_count;
9931 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009932 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9933 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 else
9935 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9936 LineWraps[j] = FALSE;
9937 }
9938 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 {
9940 j = end - 1 - i;
9941 temp = LineOffset[j];
9942 while ((j -= line_count) >= row)
9943 {
9944 LineOffset[j + line_count] = LineOffset[j];
9945 LineWraps[j + line_count] = LineWraps[j];
9946 }
9947 LineOffset[j + line_count] = temp;
9948 LineWraps[j + line_count] = FALSE;
9949 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009950 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 else
9952 lineinvalid(temp, (int)Columns);
9953 }
9954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
9956 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009957 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009958 if (clear_attr != 0)
9959 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 /* redraw the characters */
9962 if (type == USE_REDRAW)
9963 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009964 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 {
9966 term_append_lines(line_count);
9967 screen_start(); /* don't know where cursor is now */
9968 }
9969 else
9970 {
9971 for (i = 0; i < line_count; i++)
9972 {
9973 if (type == USE_T_AL)
9974 {
9975 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009976 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 out_str(T_AL);
9978 }
9979 else /* type == USE_T_SR */
9980 out_str(T_SR);
9981 screen_start(); /* don't know where cursor is now */
9982 }
9983 }
9984
9985 /*
9986 * With scroll-reverse and 'da' flag set we need to clear the lines that
9987 * have been scrolled down into the region.
9988 */
9989 if (type == USE_T_SR && *T_DA)
9990 {
9991 for (i = 0; i < line_count; ++i)
9992 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009993 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 out_str(T_CE);
9995 screen_start(); /* don't know where cursor is now */
9996 }
9997 }
9998
9999#ifdef FEAT_GUI
10000 gui_can_update_cursor();
10001 if (gui.in_use)
10002 out_flush(); /* always flush after a scroll */
10003#endif
10004 return OK;
10005}
10006
10007/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010008 * Delete lines on the screen and update ScreenLines[].
10009 * "end" is the line after the scrolled part. Normally it is Rows.
10010 * When scrolling region used "off" is the offset from the top for the region.
10011 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 *
10013 * Return OK for success, FAIL if the lines are not deleted.
10014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010016screen_del_lines(
10017 int off,
10018 int row,
10019 int line_count,
10020 int end,
10021 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010022 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010023 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024{
10025 int j;
10026 int i;
10027 unsigned temp;
10028 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010029 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 int cursor_end;
10031 int result_empty; /* result is empty until end of region */
10032 int can_delete; /* deleting line codes can be used */
10033 int type;
10034
10035 /*
10036 * FAIL if
10037 * - there is no valid screen
10038 * - the screen has to be redrawn completely
10039 * - the line count is less than one
10040 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010041 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010043 if (!screen_valid(TRUE) || line_count <= 0
10044 || (!force && line_count > p_ttyscroll)
10045#ifdef FEAT_CLIPBOARD
10046 || (clip_star.state != SELECT_CLEARED
10047 && redrawing_for_callback > 0)
10048#endif
10049 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 return FAIL;
10051
10052 /*
10053 * Check if the rest of the current region will become empty.
10054 */
10055 result_empty = row + line_count >= end;
10056
10057 /*
10058 * We can delete lines only when 'db' flag not set or when 'ce' option
10059 * available.
10060 */
10061 can_delete = (*T_DB == NUL || can_clear(T_CE));
10062
10063 /*
10064 * There are six ways to delete lines:
10065 * 0. When in a vertically split window and t_CV isn't set, redraw the
10066 * characters from ScreenLines[].
10067 * 1. Use T_CD if it exists and the result is empty.
10068 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10069 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10070 * none of the other ways work.
10071 * 4. Use T_CE (erase line) if the result is empty.
10072 * 5. Use T_DL (delete line) if it exists.
10073 * 6. redraw the characters from ScreenLines[].
10074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10076 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010077 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 type = USE_T_CD;
10079#if defined(__BEOS__) && defined(BEOS_DR8)
10080 /*
10081 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10082 * its internal termcap... this works okay for tests which test *T_DB !=
10083 * NUL. It has the disadvantage that the user cannot use any :set t_*
10084 * command to get T_DB (back) to empty_option, only :set term=... will do
10085 * the trick...
10086 * Anyway, this hack will hopefully go away with the next OS release.
10087 * (Olaf Seibert)
10088 */
10089 else if (row == 0 && T_DB == empty_option
10090 && (line_count == 1 || *T_CDL == NUL))
10091#else
10092 else if (row == 0 && (
10093#ifndef AMIGA
10094 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10095 * up, so use delete-line command */
10096 line_count == 1 ||
10097#endif
10098 *T_CDL == NUL))
10099#endif
10100 type = USE_NL;
10101 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10102 type = USE_T_CDL;
10103 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010104 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 type = USE_T_CE;
10106 else if (*T_DL != NUL && can_delete)
10107 type = USE_T_DL;
10108 else if (*T_CDL != NUL && can_delete)
10109 type = USE_T_CDL;
10110 else
10111 return FAIL;
10112
10113#ifdef FEAT_CLIPBOARD
10114 /* Remove a modeless selection when deleting lines halfway the screen or
10115 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010116 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010117 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 else
10119 clip_scroll_selection(line_count);
10120#endif
10121
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122#ifdef FEAT_GUI
10123 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10124 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010125 gui_dont_update_cursor(gui.cursor_row >= row + off
10126 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127#endif
10128
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010129 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10130 cursor_col = wp->w_wincol;
10131
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 if (*T_CCS != NUL) /* cursor relative to region */
10133 {
10134 cursor_row = row;
10135 cursor_end = end;
10136 }
10137 else
10138 {
10139 cursor_row = row + off;
10140 cursor_end = end + off;
10141 }
10142
10143 /*
10144 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10145 * Clear the inserted lines in ScreenLines[].
10146 */
10147 row += off;
10148 end += off;
10149 for (i = 0; i < line_count; ++i)
10150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 if (wp != NULL && wp->w_width != Columns)
10152 {
10153 /* need to copy part of a line */
10154 j = row + i;
10155 while ((j += line_count) <= end - 1)
10156 linecopy(j - line_count, j, wp);
10157 j -= line_count;
10158 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010159 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10160 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 else
10162 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10163 LineWraps[j] = FALSE;
10164 }
10165 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 {
10167 /* whole width, moving the line pointers is faster */
10168 j = row + i;
10169 temp = LineOffset[j];
10170 while ((j += line_count) <= end - 1)
10171 {
10172 LineOffset[j - line_count] = LineOffset[j];
10173 LineWraps[j - line_count] = LineWraps[j];
10174 }
10175 LineOffset[j - line_count] = temp;
10176 LineWraps[j - line_count] = FALSE;
10177 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010178 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 else
10180 lineinvalid(temp, (int)Columns);
10181 }
10182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010184 if (screen_attr != clear_attr)
10185 screen_stop_highlight();
10186 if (clear_attr != 0)
10187 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 /* redraw the characters */
10190 if (type == USE_REDRAW)
10191 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010192 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010194 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 out_str(T_CD);
10196 screen_start(); /* don't know where cursor is now */
10197 }
10198 else if (type == USE_T_CDL)
10199 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010200 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 term_delete_lines(line_count);
10202 screen_start(); /* don't know where cursor is now */
10203 }
10204 /*
10205 * Deleting lines at top of the screen or scroll region: Just scroll
10206 * the whole screen (scroll region) up by outputting newlines on the
10207 * last line.
10208 */
10209 else if (type == USE_NL)
10210 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010211 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 for (i = line_count; --i >= 0; )
10213 out_char('\n'); /* cursor will remain on same line */
10214 }
10215 else
10216 {
10217 for (i = line_count; --i >= 0; )
10218 {
10219 if (type == USE_T_DL)
10220 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010221 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 out_str(T_DL); /* delete a line */
10223 }
10224 else /* type == USE_T_CE */
10225 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010226 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 out_str(T_CE); /* erase a line */
10228 }
10229 screen_start(); /* don't know where cursor is now */
10230 }
10231 }
10232
10233 /*
10234 * If the 'db' flag is set, we need to clear the lines that have been
10235 * scrolled up at the bottom of the region.
10236 */
10237 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10238 {
10239 for (i = line_count; i > 0; --i)
10240 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010241 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 out_str(T_CE); /* erase a line */
10243 screen_start(); /* don't know where cursor is now */
10244 }
10245 }
10246
10247#ifdef FEAT_GUI
10248 gui_can_update_cursor();
10249 if (gui.in_use)
10250 out_flush(); /* always flush after a scroll */
10251#endif
10252
10253 return OK;
10254}
10255
10256/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010257 * Return TRUE when postponing displaying the mode message: when not redrawing
10258 * or inside a mapping.
10259 */
10260 int
10261skip_showmode()
10262{
10263 // Call char_avail() only when we are going to show something, because it
10264 // takes a bit of time. redrawing() may also call char_avail_avail().
10265 if (global_busy
10266 || msg_silent != 0
10267 || !redrawing()
10268 || (char_avail() && !KeyTyped))
10269 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010270 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010271 return TRUE;
10272 }
10273 return FALSE;
10274}
10275
10276/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010277 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 *
10279 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10280 * If clear_cmdline is FALSE there may be a message there that needs to be
10281 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010282 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 * Return the length of the message (0 if no message).
10284 */
10285 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010286showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287{
10288 int need_clear;
10289 int length = 0;
10290 int do_mode;
10291 int attr;
10292 int nwr_save;
10293#ifdef FEAT_INS_EXPAND
10294 int sub_attr;
10295#endif
10296
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010297 do_mode = ((p_smd && msg_silent == 0)
10298 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010299 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010300 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010301 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010303 if (skip_showmode())
10304 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305
10306 nwr_save = need_wait_return;
10307
10308 /* wait a bit before overwriting an important message */
10309 check_for_delay(FALSE);
10310
10311 /* if the cmdline is more than one line high, erase top lines */
10312 need_clear = clear_cmdline;
10313 if (clear_cmdline && cmdline_row < Rows - 1)
10314 msg_clr_cmdline(); /* will reset clear_cmdline */
10315
10316 /* Position on the last line in the window, column 0 */
10317 msg_pos_mode();
10318 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010319 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (do_mode)
10321 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010322 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010324 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010325# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010326 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010327# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010328 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010329# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010330 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010331# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010332 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010334 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335# endif
10336#endif
10337#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10338 if (gui.in_use)
10339 {
10340 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010341 {
10342 /* HANGUL */
10343 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010344 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010345 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010346 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 }
10349#endif
10350#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010351 /* CTRL-X in Insert mode */
10352 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 {
10354 /* These messages can get long, avoid a wrap in a narrow
10355 * window. Prefer showing edit_submode_extra. */
10356 length = (Rows - msg_row) * Columns - 3;
10357 if (edit_submode_extra != NULL)
10358 length -= vim_strsize(edit_submode_extra);
10359 if (length > 0)
10360 {
10361 if (edit_submode_pre != NULL)
10362 length -= vim_strsize(edit_submode_pre);
10363 if (length - vim_strsize(edit_submode) > 0)
10364 {
10365 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010366 msg_puts_attr((char *)edit_submode_pre, attr);
10367 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 }
10369 if (edit_submode_extra != NULL)
10370 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010371 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010373 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 else
10375 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010376 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 }
10378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 }
10380 else
10381#endif
10382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010384 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010385 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010386 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 else if (State & INSERT)
10388 {
10389#ifdef FEAT_RIGHTLEFT
10390 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010391 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010393 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010395 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010396 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010398 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010400 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401#ifdef FEAT_RIGHTLEFT
10402 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010403 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404#endif
10405#ifdef FEAT_KEYMAP
10406 if (State & LANGMAP)
10407 {
10408# ifdef FEAT_ARABIC
10409 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010410 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 else
10412# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010413 if (get_keymap_str(curwin, (char_u *)" (%s)",
10414 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010415 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 }
10417#endif
10418 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010419 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 if (VIsual_active)
10422 {
10423 char *p;
10424
10425 /* Don't concatenate separate words to avoid translation
10426 * problems. */
10427 switch ((VIsual_select ? 4 : 0)
10428 + (VIsual_mode == Ctrl_V) * 2
10429 + (VIsual_mode == 'V'))
10430 {
10431 case 0: p = N_(" VISUAL"); break;
10432 case 1: p = N_(" VISUAL LINE"); break;
10433 case 2: p = N_(" VISUAL BLOCK"); break;
10434 case 4: p = N_(" SELECT"); break;
10435 case 5: p = N_(" SELECT LINE"); break;
10436 default: p = N_(" SELECT BLOCK"); break;
10437 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010438 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010440 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010442
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 need_clear = TRUE;
10444 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010445 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446#ifdef FEAT_INS_EXPAND
10447 && edit_submode == NULL /* otherwise it gets too long */
10448#endif
10449 )
10450 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010451 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 need_clear = TRUE;
10453 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010454
10455 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010456 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 msg_clr_eos();
10458 msg_didout = FALSE; /* overwrite this message */
10459 length = msg_col;
10460 msg_col = 0;
10461 need_wait_return = nwr_save; /* never ask for hit-return for this */
10462 }
10463 else if (clear_cmdline && msg_silent == 0)
10464 /* Clear the whole command line. Will reset "clear_cmdline". */
10465 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010466 else if (redraw_mode)
10467 {
10468 msg_pos_mode();
10469 msg_clr_eos();
10470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471
10472#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 /* In Visual mode the size of the selected area must be redrawn. */
10474 if (VIsual_active)
10475 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476
10477 /* If the last window has no status line, the ruler is after the mode
10478 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010479 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010480 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481#endif
10482 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010483 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 clear_cmdline = FALSE;
10485
10486 return length;
10487}
10488
10489/*
10490 * Position for a mode message.
10491 */
10492 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010493msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 msg_col = 0;
10496 msg_row = Rows - 1;
10497}
10498
10499/*
10500 * Delete mode message. Used when ESC is typed which is expected to end
10501 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010502 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 */
10504 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010505unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010508 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 */
10510 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10511 redraw_cmdline = TRUE; /* delete mode later */
10512 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010513 clearmode();
10514}
10515
10516/*
10517 * Clear the mode message.
10518 */
10519 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010520clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010521{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010522 int save_msg_row = msg_row;
10523 int save_msg_col = msg_col;
10524
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010525 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010526 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010527 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010528 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010529
10530 msg_col = save_msg_col;
10531 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532}
10533
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010534 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010535recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010536{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010537 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010538 if (!shortmess(SHM_RECORDING))
10539 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010540 char s[4];
10541
10542 sprintf(s, " @%c", reg_recording);
10543 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010544 }
10545}
10546
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010547/*
10548 * Draw the tab pages line at the top of the Vim window.
10549 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010550 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010551draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010552{
10553 int tabcount = 0;
10554 tabpage_T *tp;
10555 int tabwidth;
10556 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010557 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010558 int attr;
10559 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010560 win_T *cwp;
10561 int wincount;
10562 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010563 int c;
10564 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010565 int attr_sel = HL_ATTR(HLF_TPS);
10566 int attr_nosel = HL_ATTR(HLF_TP);
10567 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010568 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010569 int room;
10570 int use_sep_chars = (t_colors < 8
10571#ifdef FEAT_GUI
10572 && !gui.in_use
10573#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010574#ifdef FEAT_TERMGUICOLORS
10575 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010576#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010577 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010578
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010579 if (ScreenLines == NULL)
10580 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010581 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010582
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010583#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010584 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010585 if (gui_use_tabline())
10586 {
10587 gui_update_tabline();
10588 return;
10589 }
10590#endif
10591
10592 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010593 return;
10594
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010595#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010596 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010597
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010598 /* Use the 'tabline' option if it's set. */
10599 if (*p_tal != NUL)
10600 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010601 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010602
10603 /* Check for an error. If there is one we would loop in redrawing the
10604 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010605 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010606 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010607 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010608 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010609 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010610 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010611 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010612 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010613#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010614 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010615 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010616 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010617
Bram Moolenaar238a5642006-02-21 22:12:05 +000010618 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10619 if (tabwidth < 6)
10620 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010621
Bram Moolenaar238a5642006-02-21 22:12:05 +000010622 attr = attr_nosel;
10623 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010624 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10625 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010626 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010628
Bram Moolenaar238a5642006-02-21 22:12:05 +000010629 if (tp->tp_topframe == topframe)
10630 attr = attr_sel;
10631 if (use_sep_chars && col > 0)
10632 screen_putchar('|', 0, col++, attr);
10633
10634 if (tp->tp_topframe != topframe)
10635 attr = attr_nosel;
10636
10637 screen_putchar(' ', 0, col++, attr);
10638
10639 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010640 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 cwp = curwin;
10642 wp = firstwin;
10643 }
10644 else
10645 {
10646 cwp = tp->tp_curwin;
10647 wp = tp->tp_firstwin;
10648 }
10649
10650 modified = FALSE;
10651 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10652 if (bufIsChanged(wp->w_buffer))
10653 modified = TRUE;
10654 if (modified || wincount > 1)
10655 {
10656 if (wincount > 1)
10657 {
10658 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010659 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010660 if (col + len >= Columns - 3)
10661 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010662 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010663#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010664 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010665#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010666 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010667#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010668 );
10669 col += len;
10670 }
10671 if (modified)
10672 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10673 screen_putchar(' ', 0, col++, attr);
10674 }
10675
10676 room = scol - col + tabwidth - 1;
10677 if (room > 0)
10678 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010679 /* Get buffer name in NameBuff[] */
10680 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010681 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010682 len = vim_strsize(NameBuff);
10683 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010684 if (has_mbyte)
10685 while (len > room)
10686 {
10687 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010688 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010689 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010690 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010691 {
10692 p += len - room;
10693 len = room;
10694 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010695 if (len > Columns - col - 1)
10696 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010697
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010698 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010699 col += len;
10700 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010701 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010702
10703 /* Store the tab page number in TabPageIdxs[], so that
10704 * jump_to_mouse() knows where each one is. */
10705 ++tabcount;
10706 while (scol < col)
10707 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010708 }
10709
Bram Moolenaar238a5642006-02-21 22:12:05 +000010710 if (use_sep_chars)
10711 c = '_';
10712 else
10713 c = ' ';
10714 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010715
10716 /* Put an "X" for closing the current tab if there are several. */
10717 if (first_tabpage->tp_next != NULL)
10718 {
10719 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10720 TabPageIdxs[Columns - 1] = -999;
10721 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010722 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010723
10724 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10725 * set. */
10726 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010727}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010728
10729/*
10730 * Get buffer name for "buf" into NameBuff[].
10731 * Takes care of special buffer names and translates special characters.
10732 */
10733 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010734get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010735{
10736 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010737 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010738 else
10739 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10740 trans_characters(NameBuff, MAXPATHL);
10741}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010742
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743/*
10744 * Get the character to use in a status line. Get its attributes in "*attr".
10745 */
10746 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010747fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
10749 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010750
10751#ifdef FEAT_TERMINAL
10752 if (bt_terminal(wp->w_buffer))
10753 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010754 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010755 {
10756 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010757 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010758 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010760 {
10761 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010763 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010764 }
10765 else
10766#endif
10767 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010769 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 fill = fill_stl;
10771 }
10772 else
10773 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010774 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 fill = fill_stlnc;
10776 }
10777 /* Use fill when there is highlighting, and highlighting of current
10778 * window differs, or the fillchars differ, or this is not the
10779 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010780 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010781 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 || (fill_stl != fill_stlnc)))
10783 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010784 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 return '^';
10786 return '=';
10787}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789/*
10790 * Get the character to use in a separator between vertically split windows.
10791 * Get its attributes in "*attr".
10792 */
10793 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010794fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010796 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 if (*attr == 0 && fill_vert == ' ')
10798 return '|';
10799 else
10800 return fill_vert;
10801}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802
10803/*
10804 * Return TRUE if redrawing should currently be done.
10805 */
10806 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010807redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010809#ifdef FEAT_EVAL
10810 if (disable_redraw_for_testing)
10811 return 0;
10812 else
10813#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010814 return ((!RedrawingDisabled
10815#ifdef FEAT_EVAL
10816 || ignore_redraw_flag_for_testing
10817#endif
10818 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819}
10820
10821/*
10822 * Return TRUE if printing messages should currently be done.
10823 */
10824 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010825messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826{
10827 return (!(p_lz && char_avail() && !KeyTyped));
10828}
10829
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010830#ifdef FEAT_MENU
10831/*
10832 * Draw the window toolbar.
10833 */
10834 static void
10835redraw_win_toolbar(win_T *wp)
10836{
10837 vimmenu_T *menu;
10838 int item_idx = 0;
10839 int item_count = 0;
10840 int col = 0;
10841 int next_col;
10842 int off = (int)(current_ScreenLine - ScreenLines);
10843 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10844 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10845
10846 vim_free(wp->w_winbar_items);
10847 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10848 ++item_count;
10849 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
Bram Moolenaar18a4ba22019-05-24 19:39:03 +020010850 sizeof(winbar_item_T) * (item_count + 1));
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010851
10852 /* TODO: use fewer spaces if there is not enough room */
10853 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010854 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010855 {
10856 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010857 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010858 break;
10859 if (col > 1)
10860 {
10861 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010862 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010863 break;
10864 }
10865
10866 wp->w_winbar_items[item_idx].wb_startcol = col;
10867 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010868 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010869 break;
10870
10871 next_col = text_to_screenline(wp, menu->name, col);
10872 while (col < next_col)
10873 {
10874 ScreenAttrs[off + col] = button_attr;
10875 ++col;
10876 }
10877 wp->w_winbar_items[item_idx].wb_endcol = col;
10878 wp->w_winbar_items[item_idx].wb_menu = menu;
10879 ++item_idx;
10880
Bram Moolenaar02631462017-09-22 15:20:32 +020010881 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010882 break;
10883 space_to_screenline(off + col, button_attr);
10884 ++col;
10885 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010886 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010887 {
10888 space_to_screenline(off + col, fill_attr);
10889 ++col;
10890 }
10891 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10892
Bram Moolenaar02631462017-09-22 15:20:32 +020010893 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010894 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010895}
10896#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010897
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898/*
10899 * Show current status info in ruler and various other places
10900 * If always is FALSE, only show ruler if position has changed.
10901 */
10902 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010903showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904{
10905 if (!always && !redrawing())
10906 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010907#ifdef FEAT_INS_EXPAND
10908 if (pum_visible())
10909 {
10910 /* Don't redraw right now, do it later. */
10911 curwin->w_redr_status = TRUE;
10912 return;
10913 }
10914#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010915#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010916 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010917 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918 else
10919#endif
10920#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010921 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922#endif
10923
10924#ifdef FEAT_TITLE
10925 if (need_maketitle
10926# ifdef FEAT_STL_OPT
10927 || (p_icon && (stl_syntax & STL_IN_ICON))
10928 || (p_title && (stl_syntax & STL_IN_TITLE))
10929# endif
10930 )
10931 maketitle();
10932#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010933 /* Redraw the tab pages line if needed. */
10934 if (redraw_tabline)
10935 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936}
10937
10938#ifdef FEAT_CMDL_INFO
10939 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010940win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010942#define RULER_BUF_LEN 70
10943 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 int row;
10945 int fillchar;
10946 int attr;
10947 int empty_line = FALSE;
10948 colnr_T virtcol;
10949 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010950 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 int this_ru_col;
10953 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010954 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955
10956 /* If 'ruler' off or redrawing disabled, don't do anything */
10957 if (!p_ru)
10958 return;
10959
10960 /*
10961 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10962 * after deleting lines, before cursor.lnum is corrected.
10963 */
10964 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10965 return;
10966
10967#ifdef FEAT_INS_EXPAND
10968 /* Don't draw the ruler while doing insert-completion, it might overwrite
10969 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 if (edit_submode != NULL)
10972 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010973 // Don't draw the ruler when the popup menu is visible, it may overlap.
10974 // Except when the popup menu will be redrawn anyway.
10975 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010976 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977#endif
10978
10979#ifdef FEAT_STL_OPT
10980 if (*p_ruf)
10981 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010982 int save_called_emsg = called_emsg;
10983
10984 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010986 if (called_emsg)
10987 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010988 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010989 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 return;
10991 }
10992#endif
10993
10994 /*
10995 * Check if not in Insert mode and the line is empty (will show "0-1").
10996 */
10997 if (!(State & INSERT)
10998 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10999 empty_line = TRUE;
11000
11001 /*
11002 * Only draw the ruler when something changed.
11003 */
11004 validate_virtcol_win(wp);
11005 if ( redraw_cmdline
11006 || always
11007 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11008 || wp->w_cursor.col != wp->w_ru_cursor.col
11009 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 || wp->w_topline != wp->w_ru_topline
11012 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11013#ifdef FEAT_DIFF
11014 || wp->w_topfill != wp->w_ru_topfill
11015#endif
11016 || empty_line != wp->w_ru_empty)
11017 {
11018 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 if (wp->w_status_height)
11020 {
11021 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011022 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011023 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011024 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 }
11026 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 {
11028 row = Rows - 1;
11029 fillchar = ' ';
11030 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 width = Columns;
11032 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033 }
11034
11035 /* In list mode virtcol needs to be recomputed */
11036 virtcol = wp->w_virtcol;
11037 if (wp->w_p_list && lcs_tab1 == NUL)
11038 {
11039 wp->w_p_list = FALSE;
11040 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11041 wp->w_p_list = TRUE;
11042 }
11043
11044 /*
11045 * Some sprintfs return the length, some return a pointer.
11046 * To avoid portability problems we use strlen() here.
11047 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011048 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11050 ? 0L
11051 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011052 len = STRLEN(buffer);
11053 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11055 (int)virtcol + 1);
11056
11057 /*
11058 * Add a "50%" if there is room for it.
11059 * On the last line, don't print in the last column (scrolls the
11060 * screen up on some terminals).
11061 */
11062 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011063 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 this_ru_col = ru_col - (Columns - width);
11068 if (this_ru_col < 0)
11069 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 /* Never use more than half the window/screen width, leave the other
11071 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011072 if (this_ru_col < (width + 1) / 2)
11073 this_ru_col = (width + 1) / 2;
11074 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011076 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011077 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 if (has_mbyte)
11080 i += (*mb_char2bytes)(fillchar, buffer + i);
11081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 buffer[i++] = fillchar;
11083 ++o;
11084 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011085 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 }
11087 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 if (has_mbyte)
11089 {
11090 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011091 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 {
11093 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011094 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 {
11096 buffer[i] = NUL;
11097 break;
11098 }
11099 }
11100 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011101 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011102 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
Bram Moolenaar4033c552017-09-16 20:54:51 +020011104 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 i = redraw_cmdline;
11106 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011107 this_ru_col + off + (int)STRLEN(buffer),
11108 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 fillchar, fillchar, attr);
11110 /* don't redraw the cmdline because of showing the ruler */
11111 redraw_cmdline = i;
11112 wp->w_ru_cursor = wp->w_cursor;
11113 wp->w_ru_virtcol = wp->w_virtcol;
11114 wp->w_ru_empty = empty_line;
11115 wp->w_ru_topline = wp->w_topline;
11116 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11117#ifdef FEAT_DIFF
11118 wp->w_ru_topfill = wp->w_topfill;
11119#endif
11120 }
11121}
11122#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011123
11124#if defined(FEAT_LINEBREAK) || defined(PROTO)
11125/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011126 * Return the width of the 'number' and 'relativenumber' column.
11127 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011128 * Otherwise it depends on 'numberwidth' and the line count.
11129 */
11130 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011131number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011132{
11133 int n;
11134 linenr_T lnum;
11135
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011136 if (wp->w_p_rnu && !wp->w_p_nu)
11137 /* cursor line shows "0" */
11138 lnum = wp->w_height;
11139 else
11140 /* cursor line shows absolute line number */
11141 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011142
Bram Moolenaar6b314672015-03-20 15:42:10 +010011143 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011144 return wp->w_nrwidth_width;
11145 wp->w_nrwidth_line_count = lnum;
11146
11147 n = 0;
11148 do
11149 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011150 lnum /= 10;
11151 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011152 } while (lnum > 0);
11153
11154 /* 'numberwidth' gives the minimal width plus one */
11155 if (n < wp->w_p_nuw - 1)
11156 n = wp->w_p_nuw - 1;
11157
11158 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011159 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011160 return n;
11161}
11162#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011163
Bram Moolenaar113e1072019-01-20 15:30:40 +010011164#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011165/*
11166 * Return the current cursor column. This is the actual position on the
11167 * screen. First column is 0.
11168 */
11169 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011170screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011171{
11172 return screen_cur_col;
11173}
11174
11175/*
11176 * Return the current cursor row. This is the actual position on the screen.
11177 * First row is 0.
11178 */
11179 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011180screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011181{
11182 return screen_cur_row;
11183}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011184#endif