blob: 1ffc729df4f3d8f699ff5edd34ed87dd8fe88ecb [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
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200125static void may_update_popup_mask(int type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200126static void update_popups(void);
127#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200129static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100130static 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 +0000131#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100132static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
133static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
134static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200136static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100137static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000138#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200142# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void start_search_hl(void);
144static void end_search_hl(void);
145static void init_search_hl(win_T *wp);
146static void prepare_search_hl(win_T *wp, linenr_T lnum);
147static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
148static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100154static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200155static 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 +0100156static void win_rest_invalid(win_T *wp);
157static void msg_pos_mode(void);
158static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200159static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100160static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200161#ifdef FEAT_MENU
162static void redraw_win_toolbar(win_T *wp);
163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100165static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#endif
167#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200168static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169#endif
170
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171/* Ugly global: overrule attribute used by screen_char() */
172static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200174#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
175/* Can limit syntax highlight time to 'redrawtime'. */
176# define SYN_TIME_LIMIT 1
177#endif
178
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200179#ifdef FEAT_RIGHTLEFT
180# define HAS_RIGHTLEFT(x) x
181#else
182# define HAS_RIGHTLEFT(x) FALSE
183#endif
184
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200185// flags for screen_line()
186#define SLF_RIGHTLEFT 1
187#define SLF_POPUP 2
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189/*
190 * Redraw the current window later, with update_screen(type).
191 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100192 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 */
194 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100195redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196{
197 redraw_win_later(curwin, type);
198}
199
200 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100201redraw_win_later(
202 win_T *wp,
203 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200205 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 wp->w_redr_type = type;
208 if (type >= NOT_VALID)
209 wp->w_lines_valid = 0;
210 if (must_redraw < type) /* must_redraw is the maximum of all windows */
211 must_redraw = type;
212 }
213}
214
215/*
216 * Force a complete redraw later. Also resets the highlighting. To be used
217 * after executing a shell command that messes up the screen.
218 */
219 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100220redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221{
222 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000223#ifdef FEAT_GUI
224 if (gui.in_use)
225 /* Use a code that will reset gui.highlight_mask in
226 * gui_stop_highlight(). */
227 screen_attr = HL_ALL + 1;
228 else
229#endif
230 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200231 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232}
233
234/*
235 * Mark all windows to be redrawn later.
236 */
237 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100238redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239{
240 win_T *wp;
241
242 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100244 // This may be needed when switching tabs.
245 if (must_redraw < type)
246 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247}
248
249/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000250 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 */
252 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100253redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255 redraw_buf_later(curbuf, type);
256}
257
258 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100259redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 win_T *wp;
262
263 FOR_ALL_WINDOWS(wp)
264 {
265 if (wp->w_buffer == buf)
266 redraw_win_later(wp, type);
267 }
268}
269
Bram Moolenaar113e1072019-01-20 15:30:40 +0100270#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200271 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100272redraw_buf_line_later(buf_T *buf, linenr_T lnum)
273{
274 win_T *wp;
275
276 FOR_ALL_WINDOWS(wp)
277 if (wp->w_buffer == buf && lnum >= wp->w_topline
278 && lnum < wp->w_botline)
279 redrawWinline(wp, lnum);
280}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100281#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100282
Bram Moolenaar113e1072019-01-20 15:30:40 +0100283#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100284 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200285redraw_buf_and_status_later(buf_T *buf, int type)
286{
287 win_T *wp;
288
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200289#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200290 if (wild_menu_showing != 0)
291 /* Don't redraw while the command line completion is displayed, it
292 * would disappear. */
293 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200294#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200295 FOR_ALL_WINDOWS(wp)
296 {
297 if (wp->w_buffer == buf)
298 {
299 redraw_win_later(wp, type);
300 wp->w_redr_status = TRUE;
301 }
302 }
303}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100304#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200305
Bram Moolenaar113e1072019-01-20 15:30:40 +0100306#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200308 * Redraw as soon as possible. When the command line is not scrolled redraw
309 * right away and restore what was on the command line.
310 * Return a code indicating what happened.
311 */
312 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100313redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314{
315 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200316 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317 int r;
318 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200319 schar_T *screenline; /* copy from ScreenLines[] */
320 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200321 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200322 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200323 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200324 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325
326 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200327 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200328 return ret;
329
330 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200331 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200332 screenline = LALLOC_MULT(schar_T, rows * cols);
333 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 if (screenline == NULL || screenattr == NULL)
335 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200336 if (enc_utf8)
337 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200338 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenlineUC == NULL)
340 ret = 2;
341 for (i = 0; i < p_mco; ++i)
342 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200343 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200344 if (screenlineC[i] == NULL)
345 ret = 2;
346 }
347 }
348 if (enc_dbcs == DBCS_JPNU)
349 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200350 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200351 if (screenline2 == NULL)
352 ret = 2;
353 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200354
355 if (ret != 2)
356 {
357 /* Save the text displayed in the command line area. */
358 for (r = 0; r < rows; ++r)
359 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200360 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200362 (size_t)cols * sizeof(schar_T));
363 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 if (enc_utf8)
367 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200368 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200372 mch_memmove(screenlineC[i] + r * cols,
373 ScreenLinesC[i] + LineOffset[cmdline_row + r],
374 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375 }
376 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200377 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200378 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200380 }
381
382 update_screen(0);
383 ret = 3;
384
385 if (must_redraw == 0)
386 {
387 int off = (int)(current_ScreenLine - ScreenLines);
388
389 /* Restore the text displayed in the command line area. */
390 for (r = 0; r < rows; ++r)
391 {
392 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200393 screenline + r * cols,
394 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200395 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenattr + r * cols,
397 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200398 if (enc_utf8)
399 {
400 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200401 screenlineUC + r * cols,
402 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200403 for (i = 0; i < p_mco; ++i)
404 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200405 screenlineC[i] + r * cols,
406 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200407 }
408 if (enc_dbcs == DBCS_JPNU)
409 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200410 screenline2 + r * cols,
411 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200412 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200413 }
414 ret = 4;
415 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200416 }
417
418 vim_free(screenline);
419 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 if (enc_utf8)
421 {
422 vim_free(screenlineUC);
423 for (i = 0; i < p_mco; ++i)
424 vim_free(screenlineC[i]);
425 }
426 if (enc_dbcs == DBCS_JPNU)
427 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200428
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200429 /* Show the intro message when appropriate. */
430 maybe_intro_message();
431
432 setcursor();
433
Bram Moolenaar2951b772013-07-03 12:45:31 +0200434 return ret;
435}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100436#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200437
438/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100439 * Invoked after an asynchronous callback is called.
440 * If an echo command was used the cursor needs to be put back where
441 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200442 * If "call_update_screen" is FALSE don't call update_screen() when at the
443 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100444 */
445 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200446redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100447{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200448 ++redrawing_for_callback;
449
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100450 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200451 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100452 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200453 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200454 // Don't redraw when in prompt_for_number().
455 if (cmdline_row > 0)
456 {
457 // Redrawing only works when the screen didn't scroll. Don't clear
458 // wildmenu entries.
459 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200460#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200461 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200462#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200463 && call_update_screen)
464 update_screen(0);
465
466 // Redraw in the same position, so that the user can continue
467 // editing the command.
468 redrawcmdline_ex(FALSE);
469 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200470 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200471 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100472 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200473 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200474 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100475 setcursor();
476 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100479 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200480 // Don't update the cursor when it is blinking and off to avoid
481 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100482 out_flush_cursor(FALSE, FALSE);
483 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100484#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100485 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200486
487 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100488}
489
490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 * Changed something in the current window, at buffer line "lnum", that
492 * requires that line and possibly other lines to be redrawn.
493 * Used when entering/leaving Insert mode with the cursor on a folded line.
494 * Used to remove the "$" from a change command.
495 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
496 * may become invalid and the whole window will have to be redrawn.
497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100499redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200500 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100501 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200503 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
504 wp->w_redraw_top = lnum;
505 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
506 wp->w_redraw_bot = lnum;
507 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508}
509
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200510/*
511 * To be called when "updating_screen" was set before and now the postponed
512 * side effects may take place.
513 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200514 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200515after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200516{
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 Moolenaar33796b32019-06-08 16:01:13 +0200614
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200615#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200616 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
617 // in some windows.
618 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621 updating_screen = TRUE;
622#ifdef FEAT_SYN_HL
623 ++display_tick; /* let syntax code know we're in a next round of
624 * display updating */
625#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200626 if (no_update)
627 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
629 /*
630 * if the screen was scrolled up when displaying a message, scroll it down
631 */
632 if (msg_scrolled)
633 {
634 clear_cmdline = TRUE;
635 if (msg_scrolled > Rows - 5) /* clearing is faster */
636 type = CLEAR;
637 else if (type != CLEAR)
638 {
639 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200640 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
641 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 type = CLEAR;
643 FOR_ALL_WINDOWS(wp)
644 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200645 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
647 if (W_WINROW(wp) + wp->w_height > msg_scrolled
648 && wp->w_redr_type < REDRAW_TOP
649 && wp->w_lines_valid > 0
650 && wp->w_topline == wp->w_lines[0].wl_lnum)
651 {
652 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
653 wp->w_redr_type = REDRAW_TOP;
654 }
655 else
656 {
657 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200658 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
659 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 }
663 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200664 if (!no_update)
665 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000666 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668 msg_scrolled = 0;
669 need_wait_return = FALSE;
670 }
671
672 /* reset cmdline_row now (may have been changed temporarily) */
673 compute_cmdrow();
674
675 /* Check for changed highlighting */
676 if (need_highlight_changed)
677 highlight_changed();
678
679 if (type == CLEAR) /* first clear screen */
680 {
681 screenclear(); /* will reset clear_cmdline */
682 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200683 /* must_redraw may be set indirectly, avoid another redraw later */
684 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686
687 if (clear_cmdline) /* going to clear cmdline (done below) */
688 check_for_delay(FALSE);
689
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000690#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200691 /* Force redraw when width of 'number' or 'relativenumber' column
692 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000693 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200694 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
695 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000696 curwin->w_redr_type = NOT_VALID;
697#endif
698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 /*
700 * Only start redrawing if there is really something to do.
701 */
702 if (type == INVERTED)
703 update_curswant();
704 if (curwin->w_redr_type < type
705 && !((type == VALID
706 && curwin->w_lines[0].wl_valid
707#ifdef FEAT_DIFF
708 && curwin->w_topfill == curwin->w_old_topfill
709 && curwin->w_botfill == curwin->w_old_botfill
710#endif
711 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000713 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
715 && curwin->w_old_visual_mode == VIsual_mode
716 && (curwin->w_valid & VALID_VIRTCOL)
717 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 ))
719 curwin->w_redr_type = type;
720
Bram Moolenaar5a305422006-04-28 22:38:25 +0000721 /* Redraw the tab pages line if needed. */
722 if (redraw_tabline || type >= NOT_VALID)
723 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_SYN_HL
726 /*
727 * Correct stored syntax highlighting info for changes in each displayed
728 * buffer. Each buffer must only be done once.
729 */
730 FOR_ALL_WINDOWS(wp)
731 {
732 if (wp->w_buffer->b_mod_set)
733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 win_T *wwp;
735
736 /* Check if we already did this buffer. */
737 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
738 if (wwp->w_buffer == wp->w_buffer)
739 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200740 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 syn_stack_apply_changes(wp->w_buffer);
742 }
743 }
744#endif
745
746 /*
747 * Go from top to bottom through the windows, redrawing the ones that need
748 * it.
749 */
750#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
751 did_one = FALSE;
752#endif
753#ifdef FEAT_SEARCH_EXTRA
754 search_hl.rm.regprog = NULL;
755#endif
756 FOR_ALL_WINDOWS(wp)
757 {
758 if (wp->w_redr_type != 0)
759 {
760 cursor_off();
761#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
762 if (!did_one)
763 {
764 did_one = TRUE;
765# ifdef FEAT_SEARCH_EXTRA
766 start_search_hl();
767# endif
768# ifdef FEAT_CLIPBOARD
769 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200770 if (clip_star.available && clip_isautosel_star())
771 clip_update_selection(&clip_star);
772 if (clip_plus.available && clip_isautosel_plus())
773 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774# endif
775#ifdef FEAT_GUI
776 /* Remove the cursor before starting to do anything, because
777 * scrolling may make it difficult to redraw the text under
778 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200779 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200780 {
781 gui_cursor_col = gui.cursor_col;
782 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200784 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786#endif
787 }
788#endif
789 win_update(wp);
790 }
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 /* redraw status line after the window to minimize cursor movement */
793 if (wp->w_redr_status)
794 {
795 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200796 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799#if defined(FEAT_SEARCH_EXTRA)
800 end_search_hl();
801#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100802#ifdef FEAT_INS_EXPAND
803 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200804 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 /* Reset b_mod_set flags. Going through all windows is probably faster
808 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200809 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200812 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Clear or redraw the command line. Done last, because scrolling may
815 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200816 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 showmode();
818
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200819 if (no_update)
820 --no_win_do_lines_ins;
821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200823 if (!did_intro)
824 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 did_intro = TRUE;
826
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200827#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200828 // Display popup windows on top of the windows.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200829 update_popups();
830#endif
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#ifdef FEAT_GUI
833 /* Redraw the cursor and update the scrollbars when all screen updating is
834 * done. */
835 if (gui.in_use)
836 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200837 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200838 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 mch_disable_flush();
840 out_flush(); /* required before updating the cursor */
841 mch_enable_flush();
842
Bram Moolenaar144445d2016-07-08 21:41:54 +0200843 /* Put the GUI position where the cursor was, gui_update_cursor()
844 * uses that. */
845 gui.col = gui_cursor_col;
846 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200847 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200850 screen_cur_col = gui.col;
851 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200852 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100853 else
854 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 gui_update_scrollbars(FALSE);
856 }
857#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200858 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859}
860
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100861#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100862/*
863 * Prepare for updating one or more windows.
864 * Caller must check for "updating_screen" already set to avoid recursiveness.
865 */
866 static void
867update_prepare(void)
868{
869 cursor_off();
870 updating_screen = TRUE;
871#ifdef FEAT_GUI
872 /* Remove the cursor before starting to do anything, because scrolling may
873 * make it difficult to redraw the text under it. */
874 if (gui.in_use)
875 gui_undraw_cursor();
876#endif
877#ifdef FEAT_SEARCH_EXTRA
878 start_search_hl();
879#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200880#ifdef FEAT_TEXT_PROP
881 // Update popup_mask if needed.
882 may_update_popup_mask(0);
883#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884}
885
886/*
887 * Finish updating one or more windows.
888 */
889 static void
890update_finish(void)
891{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200892 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100893 showmode();
894
895# ifdef FEAT_SEARCH_EXTRA
896 end_search_hl();
897# endif
898
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200899 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100900
901# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100902 /* Redraw the cursor and update the scrollbars when all screen updating is
903 * done. */
904 if (gui.in_use)
905 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100906 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100907 gui_update_scrollbars(FALSE);
908 }
909# endif
910}
911#endif
912
Bram Moolenaar860cae12010-06-05 23:22:07 +0200913#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200914/*
915 * Return TRUE if the cursor line in window "wp" may be concealed, according
916 * to the 'concealcursor' option.
917 */
918 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100919conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200920{
921 int c;
922
923 if (*wp->w_p_cocu == NUL)
924 return FALSE;
925 if (get_real_state() & VISUAL)
926 c = 'v';
927 else if (State & INSERT)
928 c = 'i';
929 else if (State & NORMAL)
930 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200931 else if (State & CMDLINE)
932 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200933 else
934 return FALSE;
935 return vim_strchr(wp->w_p_cocu, c) != NULL;
936}
937
938/*
939 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
940 */
941 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200942conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200943{
944 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
945 {
946 need_cursor_line_redraw = TRUE;
947 /* Need to recompute cursor column, e.g., when starting Visual mode
948 * without concealing. */
949 curs_columns(TRUE);
950 }
951}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#endif
953
Bram Moolenaar113e1072019-01-20 15:30:40 +0100954#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100956update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957{
958 win_T *wp;
959 int doit = FALSE;
960
961# ifdef FEAT_FOLDING
962 win_foldinfo.fi_level = 0;
963# endif
964
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100965 // update/delete a specific sign
966 redraw_buf_line_later(buf, lnum);
967
968 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (wp->w_redr_type != 0)
971 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100973 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200974 * happening (recursive call), messages on the screen or still starting up.
975 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100976 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200977 || State == ASKMORE || State == HITRETURN
978 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100979#ifdef FEAT_GUI
980 || gui.starting
981#endif
982 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 return;
984
985 /* update all windows that need updating */
986 update_prepare();
987
Bram Moolenaar29323592016-07-24 22:04:11 +0200988 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990 if (wp->w_redr_type != 0)
991 win_update(wp);
992 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200993 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 update_finish();
997}
998#endif
999
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001000/*
1001 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1002 * window then get the "Pmenu" highlight attribute.
1003 */
1004 static int
1005get_wcr_attr(win_T *wp)
1006{
1007 int wcr_attr = 0;
1008
1009 if (*wp->w_p_wcr != NUL)
1010 wcr_attr = syn_name2attr(wp->w_p_wcr);
1011#ifdef FEAT_TEXT_PROP
1012 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1013 wcr_attr = HL_ATTR(HLF_PNI);
1014#endif
1015 return wcr_attr;
1016}
1017
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001018#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001019
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001020/*
Bram Moolenaar33796b32019-06-08 16:01:13 +02001021 * Update "popup_mask" if needed.
1022 * Also recomputes the popup size and positions.
1023 * Also updates "popup_visible".
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001024 * Also marks window lines for redrawing.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001025 */
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001026 static void
1027may_update_popup_mask(int type)
Bram Moolenaar33796b32019-06-08 16:01:13 +02001028{
Bram Moolenaar33796b32019-06-08 16:01:13 +02001029 win_T *wp;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001030 short *mask;
1031 int line, col;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001032
1033 if (popup_mask_tab != curtab)
1034 popup_mask_refresh = TRUE;
1035 if (!popup_mask_refresh)
1036 {
1037 // Check if any buffer has changed.
1038 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1039 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1040 popup_mask_refresh = TRUE;
1041 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1042 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1043 popup_mask_refresh = TRUE;
1044 if (!popup_mask_refresh)
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001045 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001046 }
1047
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001048 // Need to update the mask, something has changed.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001049 popup_mask_refresh = FALSE;
1050 popup_mask_tab = curtab;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001051 popup_visible = FALSE;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001052
1053 // If redrawing everything, just update "popup_mask".
1054 // If redrawing only what is needed, update "popup_mask_next" and then
1055 // compare with "popup_mask" to see what changed.
1056 if (type >= SOME_VALID)
1057 mask = popup_mask;
1058 else
1059 mask = popup_mask_next;
1060 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02001061
1062 // Find the window with the lowest zindex that hasn't been handled yet,
1063 // so that the window with a higher zindex overwrites the value in
1064 // popup_mask.
1065 popup_reset_handled();
1066 while ((wp = find_next_popup(TRUE)) != NULL)
1067 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001068 int height_extra, width_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001069
1070 popup_visible = TRUE;
1071
1072 // Recompute the position if the text changed.
1073 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1074 popup_adjust_position(wp);
1075
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001076 // the width and height are for the inside, add the padding and
Bram Moolenaar33796b32019-06-08 16:01:13 +02001077 // border
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001078 height_extra = wp->w_popup_padding[0] + wp->w_popup_border[0]
1079 + wp->w_popup_padding[2] + wp->w_popup_border[2];
1080 width_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
1081 + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaar33796b32019-06-08 16:01:13 +02001082
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001083 for (line = wp->w_winrow;
1084 line < wp->w_winrow + wp->w_height + height_extra
Bram Moolenaar33796b32019-06-08 16:01:13 +02001085 && line < screen_Rows; ++line)
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001086 for (col = wp->w_wincol;
1087 col < wp->w_wincol + wp->w_width + width_extra
Bram Moolenaar33796b32019-06-08 16:01:13 +02001088 && col < screen_Columns; ++col)
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001089 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001090 }
1091
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001092 // Only check which lines are to be updated if not already
1093 // updating all lines.
1094 if (mask == popup_mask_next)
1095 for (line = 0; line < screen_Rows; ++line)
1096 {
1097 int col_done = 0;
1098
1099 for (col = 0; col < screen_Columns; ++col)
1100 {
1101 int off = line * screen_Columns + col;
1102
1103 if (popup_mask[off] != popup_mask_next[off])
1104 {
1105 popup_mask[off] = popup_mask_next[off];
1106
1107 // The screen position "line" / "col" needs to be redrawn.
1108 // Figure out what window that is and update w_redraw_top
1109 // and w_redr_bot. Only needs to be done for each window
1110 // line.
1111 if (col >= col_done)
1112 {
1113 linenr_T lnum;
1114 int line_cp = line;
1115 int col_cp = col;
1116
1117 // find the window where the row is in
1118 wp = mouse_find_win(&line_cp, &col_cp);
1119 if (wp != NULL)
1120 {
1121 if (line_cp >= wp->w_height)
1122 // In (or below) status line
1123 wp->w_redr_status = TRUE;
1124 // compute the position in the buffer line from the
1125 // position on the screen
1126 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1127 &lnum))
1128 // past bottom
1129 wp->w_redr_status = TRUE;
1130 else
1131 redrawWinline(wp, lnum);
1132
1133 // This line is going to be redrawn, no need to
1134 // check until the right side of the window.
1135 col_done = wp->w_wincol + wp->w_width - 1;
1136 }
1137 }
1138 }
1139 }
1140 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001141}
1142
1143/*
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001144 * Return a string of "len" spaces in IObuff.
1145 */
1146 static char_u *
1147get_spaces(int len)
1148{
1149 vim_memset(IObuff, ' ', (size_t)len);
1150 IObuff[len] = NUL;
1151 return IObuff;
1152}
1153
1154 static void
1155update_popups(void)
1156{
1157 win_T *wp;
1158 int top_off;
1159 int left_off;
1160 int total_width;
1161 int total_height;
1162 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001163 int border_attr[4];
Bram Moolenaar02e15072019-06-03 22:53:30 +02001164 int border_char[8];
Bram Moolenaar790498b2019-06-01 22:15:29 +02001165 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001166 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001167 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001168
1169 // Find the window with the lowest zindex that hasn't been updated yet,
1170 // so that the window with a higher zindex is drawn later, thus goes on
1171 // top.
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001172 popup_reset_handled();
1173 while ((wp = find_next_popup(TRUE)) != NULL)
1174 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02001175 // This drawing uses the zindex of the popup window, so that it's on
1176 // top of the text but doesn't draw when another popup with higher
1177 // zindex is on top of the character.
1178 screen_zindex = wp->w_zindex;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001179
1180 // adjust w_winrow and w_wincol for border and padding, since
1181 // win_update() doesn't handle them.
1182 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1183 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1184 wp->w_winrow += top_off;
1185 wp->w_wincol += left_off;
1186
1187 // Draw the popup text.
1188 win_update(wp);
1189
1190 wp->w_winrow -= top_off;
1191 wp->w_wincol -= left_off;
1192
1193 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1194 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1195 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1196 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1197 popup_attr = get_wcr_attr(wp);
1198
Bram Moolenaar3f6aeba2019-06-03 22:21:27 +02001199 // We can only use these line drawing characters when 'encoding' is
1200 // "utf-8" and 'ambiwidth' is "single".
Bram Moolenaar02e15072019-06-03 22:53:30 +02001201 if (enc_utf8 && *p_ambw == 's')
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001202 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001203 border_char[0] = border_char[2] = 0x2550;
1204 border_char[1] = border_char[3] = 0x2551;
1205 border_char[4] = 0x2554;
1206 border_char[5] = 0x2557;
1207 border_char[6] = 0x255d;
1208 border_char[7] = 0x255a;
1209 }
Bram Moolenaar02e15072019-06-03 22:53:30 +02001210 else
1211 {
1212 border_char[0] = border_char[2] = '-';
1213 border_char[1] = border_char[3] = '|';
1214 for (i = 4; i < 8; ++i)
1215 border_char[i] = '+';
1216 }
Bram Moolenaar790498b2019-06-01 22:15:29 +02001217 for (i = 0; i < 8; ++i)
1218 if (wp->w_border_char[i] != 0)
1219 border_char[i] = wp->w_border_char[i];
1220
1221 for (i = 0; i < 4; ++i)
1222 {
1223 border_attr[i] = popup_attr;
1224 if (wp->w_border_highlight[i] != NULL)
1225 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001226 }
1227
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001228 if (wp->w_popup_border[0] > 0)
1229 {
1230 // top border
1231 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1232 wp->w_wincol,
1233 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001234 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001235 ? border_char[4] : border_char[0],
1236 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001237 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001238 {
1239 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1240 screen_puts(buf, wp->w_winrow,
1241 wp->w_wincol + total_width - 1, border_attr[1]);
1242 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001243 }
1244
1245 if (wp->w_popup_padding[0] > 0)
1246 {
1247 // top padding
1248 row = wp->w_winrow + wp->w_popup_border[0];
1249 screen_fill(row, row + wp->w_popup_padding[0],
1250 wp->w_wincol + wp->w_popup_border[3],
1251 wp->w_wincol + total_width - wp->w_popup_border[1],
1252 ' ', ' ', popup_attr);
1253 }
1254
1255 for (row = wp->w_winrow + wp->w_popup_border[0];
1256 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1257 ++row)
1258 {
1259 // left border
1260 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001261 {
1262 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1263 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1264 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001265 // left padding
1266 if (wp->w_popup_padding[3] > 0)
1267 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1268 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1269 // right border
1270 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001271 {
1272 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1273 screen_puts(buf, row,
1274 wp->w_wincol + total_width - 1, border_attr[1]);
1275 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001276 // right padding
1277 if (wp->w_popup_padding[1] > 0)
1278 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1279 wp->w_wincol + wp->w_popup_border[3]
1280 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1281 }
1282
1283 if (wp->w_popup_padding[2] > 0)
1284 {
1285 // bottom padding
1286 row = wp->w_winrow + wp->w_popup_border[0]
1287 + wp->w_popup_padding[0] + wp->w_height;
1288 screen_fill(row, row + wp->w_popup_padding[2],
1289 wp->w_wincol + wp->w_popup_border[3],
1290 wp->w_wincol + total_width - wp->w_popup_border[1],
1291 ' ', ' ', popup_attr);
1292 }
1293
1294 if (wp->w_popup_border[2] > 0)
1295 {
1296 // bottom border
1297 row = wp->w_winrow + total_height - 1;
1298 screen_fill(row , row + 1,
1299 wp->w_wincol,
1300 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001301 wp->w_popup_border[3] != 0
1302 ? border_char[7] : border_char[2],
1303 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001304 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001305 {
1306 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1307 screen_puts(buf, row,
1308 wp->w_wincol + total_width - 1, border_attr[2]);
1309 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001310 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001311
1312 // Back to the normal zindex.
1313 screen_zindex = 0;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001314 }
1315}
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#if defined(FEAT_GUI) || defined(PROTO)
1319/*
1320 * Update a single window, its status line and maybe the command line msg.
1321 * Used for the GUI scrollbar.
1322 */
1323 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001324updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001326 /* return if already busy updating */
1327 if (updating_screen)
1328 return;
1329
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 update_prepare();
1331
1332#ifdef FEAT_CLIPBOARD
1333 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001334 if (clip_star.available && clip_isautosel_star())
1335 clip_update_selection(&clip_star);
1336 if (clip_plus.available && clip_isautosel_plus())
1337 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001341
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001342 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001343 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001344 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (wp->w_redr_status
1347# ifdef FEAT_CMDL_INFO
1348 || p_ru
1349# endif
1350# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001351 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352# endif
1353 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001354 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar988c4332019-06-02 14:12:11 +02001356#ifdef FEAT_TEXT_PROP
1357 // Display popup windows on top of everything.
1358 update_popups();
1359#endif
1360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 update_finish();
1362}
1363#endif
1364
1365/*
1366 * Update a single window.
1367 *
1368 * This may cause the windows below it also to be redrawn (when clearing the
1369 * screen or scrolling lines).
1370 *
1371 * How the window is redrawn depends on wp->w_redr_type. Each type also
1372 * implies the one below it.
1373 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001374 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1376 * INVERTED redraw the changed part of the Visual area
1377 * INVERTED_ALL redraw the whole Visual area
1378 * VALID 1. scroll up/down to adjust for a changed w_topline
1379 * 2. update lines at the top when scrolled down
1380 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001381 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 * b_mod_top and b_mod_bot.
1383 * - if wp->w_redraw_top non-zero, redraw lines between
1384 * wp->w_redraw_top and wp->w_redr_bot.
1385 * - continue redrawing when syntax status is invalid.
1386 * 4. if scrolled up, update lines at the bottom.
1387 * This results in three areas that may need updating:
1388 * top: from first row to top_end (when scrolled down)
1389 * mid: from mid_start to mid_end (update inversion or changed text)
1390 * bot: from bot_start to last row (when scrolled up)
1391 */
1392 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001393win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 buf_T *buf = wp->w_buffer;
1396 int type;
1397 int top_end = 0; /* Below last row of the top area that needs
1398 updating. 0 when no top area updating. */
1399 int mid_start = 999;/* first row of the mid area that needs
1400 updating. 999 when no mid area updating. */
1401 int mid_end = 0; /* Below last row of the mid area that needs
1402 updating. 0 when no mid area updating. */
1403 int bot_start = 999;/* first row of the bot area that needs
1404 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 int scrolled_down = FALSE; /* TRUE when scrolled down when
1406 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001408 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 int top_to_mod = FALSE; /* redraw above mod_top */
1410#endif
1411
1412 int row; /* current window row to display */
1413 linenr_T lnum; /* current buffer lnum to display */
1414 int idx; /* current index in w_lines[] */
1415 int srow; /* starting row of the current line */
1416
1417 int eof = FALSE; /* if TRUE, we hit the end of the file */
1418 int didline = FALSE; /* if TRUE, we finished the last line */
1419 int i;
1420 long j;
1421 static int recursive = FALSE; /* being called recursively */
1422 int old_botline = wp->w_botline;
1423#ifdef FEAT_FOLDING
1424 long fold_count;
1425#endif
1426#ifdef FEAT_SYN_HL
1427 /* remember what happened to the previous line, to know if
1428 * check_visual_highlight() can be used */
1429#define DID_NONE 1 /* didn't update a line */
1430#define DID_LINE 2 /* updated a normal line */
1431#define DID_FOLD 3 /* updated a folded line */
1432 int did_update = DID_NONE;
1433 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1434#endif
1435 linenr_T mod_top = 0;
1436 linenr_T mod_bot = 0;
1437#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1438 int save_got_int;
1439#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001440#ifdef SYN_TIME_LIMIT
1441 proftime_T syntax_tm;
1442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444 type = wp->w_redr_type;
1445
1446 if (type == NOT_VALID)
1447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 wp->w_lines_valid = 0;
1450 }
1451
1452 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001453 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 {
1455 wp->w_redr_type = 0;
1456 return;
1457 }
1458
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 /* Window is zero-width: Only need to draw the separator. */
1460 if (wp->w_width == 0)
1461 {
1462 /* draw the vertical separator right of this window */
1463 draw_vsep_win(wp, 0);
1464 wp->w_redr_type = 0;
1465 return;
1466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001468#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001469 // If this window contains a terminal, redraw works completely differently.
1470 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001471 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001472 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001473# ifdef FEAT_MENU
1474 /* Draw the window toolbar, if there is one. */
1475 if (winbar_height(wp) > 0)
1476 redraw_win_toolbar(wp);
1477# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001478 wp->w_redr_type = 0;
1479 return;
1480 }
1481#endif
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001484 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#endif
1486
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001487#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001488 /* Force redraw when width of 'number' or 'relativenumber' column
1489 * changes. */
1490 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001491 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001492 {
1493 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001494 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001495 }
1496 else
1497#endif
1498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1500 {
1501 /*
1502 * When there are both inserted/deleted lines and specific lines to be
1503 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1504 * everything (only happens when redrawing is off for while).
1505 */
1506 type = NOT_VALID;
1507 }
1508 else
1509 {
1510 /*
1511 * Set mod_top to the first line that needs displaying because of
1512 * changes. Set mod_bot to the first line after the changes.
1513 */
1514 mod_top = wp->w_redraw_top;
1515 if (wp->w_redraw_bot != 0)
1516 mod_bot = wp->w_redraw_bot + 1;
1517 else
1518 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (buf->b_mod_set)
1520 {
1521 if (mod_top == 0 || mod_top > buf->b_mod_top)
1522 {
1523 mod_top = buf->b_mod_top;
1524#ifdef FEAT_SYN_HL
1525 /* Need to redraw lines above the change that may be included
1526 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001527 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001529 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (mod_top < 1)
1531 mod_top = 1;
1532 }
1533#endif
1534 }
1535 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1536 mod_bot = buf->b_mod_bot;
1537
1538#ifdef FEAT_SEARCH_EXTRA
1539 /* When 'hlsearch' is on and using a multi-line search pattern, a
1540 * change in one line may make the Search highlighting in a
1541 * previous line invalid. Simple solution: redraw all visible
1542 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001543 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001545 if (search_hl.rm.regprog != NULL
1546 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001548 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001549 {
1550 cur = wp->w_match_head;
1551 while (cur != NULL)
1552 {
1553 if (cur->match.regprog != NULL
1554 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001555 {
1556 top_to_mod = TRUE;
1557 break;
1558 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001559 cur = cur->next;
1560 }
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#endif
1563 }
1564#ifdef FEAT_FOLDING
1565 if (mod_top != 0 && hasAnyFolding(wp))
1566 {
1567 linenr_T lnumt, lnumb;
1568
1569 /*
1570 * A change in a line can cause lines above it to become folded or
1571 * unfolded. Find the top most buffer line that may be affected.
1572 * If the line was previously folded and displayed, get the first
1573 * line of that fold. If the line is folded now, get the first
1574 * folded line. Use the minimum of these two.
1575 */
1576
1577 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1578 * the line below it. If there is no valid entry, use w_topline.
1579 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1580 * to this line. If there is no valid entry, use MAXLNUM. */
1581 lnumt = wp->w_topline;
1582 lnumb = MAXLNUM;
1583 for (i = 0; i < wp->w_lines_valid; ++i)
1584 if (wp->w_lines[i].wl_valid)
1585 {
1586 if (wp->w_lines[i].wl_lastlnum < mod_top)
1587 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1588 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1589 {
1590 lnumb = wp->w_lines[i].wl_lnum;
1591 /* When there is a fold column it might need updating
1592 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001593 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 ++lnumb;
1595 }
1596 }
1597
1598 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1599 if (mod_top > lnumt)
1600 mod_top = lnumt;
1601
1602 /* Now do the same for the bottom line (one above mod_bot). */
1603 --mod_bot;
1604 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1605 ++mod_bot;
1606 if (mod_bot < lnumb)
1607 mod_bot = lnumb;
1608 }
1609#endif
1610
1611 /* When a change starts above w_topline and the end is below
1612 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001613 * If the end of the change is above w_topline: do like no change was
1614 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (mod_top != 0 && mod_top < wp->w_topline)
1616 {
1617 if (mod_bot > wp->w_topline)
1618 mod_top = wp->w_topline;
1619#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001620 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 top_end = 1;
1622#endif
1623 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001624
1625 /* When line numbers are displayed need to redraw all lines below
1626 * inserted/deleted lines. */
1627 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1628 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001630 wp->w_redraw_top = 0; // reset for next time
1631 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 /*
1634 * When only displaying the lines at the top, set top_end. Used when
1635 * window has scrolled down for msg_scrolled.
1636 */
1637 if (type == REDRAW_TOP)
1638 {
1639 j = 0;
1640 for (i = 0; i < wp->w_lines_valid; ++i)
1641 {
1642 j += wp->w_lines[i].wl_size;
1643 if (j >= wp->w_upd_rows)
1644 {
1645 top_end = j;
1646 break;
1647 }
1648 }
1649 if (top_end == 0)
1650 /* not found (cannot happen?): redraw everything */
1651 type = NOT_VALID;
1652 else
1653 /* top area defined, the rest is VALID */
1654 type = VALID;
1655 }
1656
Bram Moolenaar367329b2007-08-30 11:53:22 +00001657 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001658 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1659 * non-zero and thus not FALSE) will indicate that screenclear() was not
1660 * called. */
1661 if (screen_cleared)
1662 screen_cleared = MAYBE;
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 /*
1665 * If there are no changes on the screen that require a complete redraw,
1666 * handle three cases:
1667 * 1: we are off the top of the screen by a few lines: scroll down
1668 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1669 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1670 * w_lines[] that needs updating.
1671 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001672 if ((type == VALID || type == SOME_VALID
1673 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674#ifdef FEAT_DIFF
1675 && !wp->w_botfill && !wp->w_old_botfill
1676#endif
1677 )
1678 {
1679 if (mod_top != 0 && wp->w_topline == mod_top)
1680 {
1681 /*
1682 * w_topline is the first changed line, the scrolling will be done
1683 * further down.
1684 */
1685 }
1686 else if (wp->w_lines[0].wl_valid
1687 && (wp->w_topline < wp->w_lines[0].wl_lnum
1688#ifdef FEAT_DIFF
1689 || (wp->w_topline == wp->w_lines[0].wl_lnum
1690 && wp->w_topfill > wp->w_old_topfill)
1691#endif
1692 ))
1693 {
1694 /*
1695 * New topline is above old topline: May scroll down.
1696 */
1697#ifdef FEAT_FOLDING
1698 if (hasAnyFolding(wp))
1699 {
1700 linenr_T ln;
1701
1702 /* count the number of lines we are off, counting a sequence
1703 * of folded lines as one */
1704 j = 0;
1705 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1706 {
1707 ++j;
1708 if (j >= wp->w_height - 2)
1709 break;
1710 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1711 }
1712 }
1713 else
1714#endif
1715 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1716 if (j < wp->w_height - 2) /* not too far off */
1717 {
1718 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1719#ifdef FEAT_DIFF
1720 /* insert extra lines for previously invisible filler lines */
1721 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1722 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1723 - wp->w_old_topfill;
1724#endif
1725 if (i < wp->w_height - 2) /* less than a screen off */
1726 {
1727 /*
1728 * Try to insert the correct number of lines.
1729 * If not the last window, delete the lines at the bottom.
1730 * win_ins_lines may fail when the terminal can't do it.
1731 */
1732 if (i > 0)
1733 check_for_delay(FALSE);
1734 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1735 {
1736 if (wp->w_lines_valid != 0)
1737 {
1738 /* Need to update rows that are new, stop at the
1739 * first one that scrolled down. */
1740 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 /* Move the entries that were scrolled, disable
1744 * the entries for the lines to be redrawn. */
1745 if ((wp->w_lines_valid += j) > wp->w_height)
1746 wp->w_lines_valid = wp->w_height;
1747 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1748 wp->w_lines[idx] = wp->w_lines[idx - j];
1749 while (idx >= 0)
1750 wp->w_lines[idx--].wl_valid = FALSE;
1751 }
1752 }
1753 else
1754 mid_start = 0; /* redraw all lines */
1755 }
1756 else
1757 mid_start = 0; /* redraw all lines */
1758 }
1759 else
1760 mid_start = 0; /* redraw all lines */
1761 }
1762 else
1763 {
1764 /*
1765 * New topline is at or below old topline: May scroll up.
1766 * When topline didn't change, find first entry in w_lines[] that
1767 * needs updating.
1768 */
1769
1770 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1771 j = -1;
1772 row = 0;
1773 for (i = 0; i < wp->w_lines_valid; i++)
1774 {
1775 if (wp->w_lines[i].wl_valid
1776 && wp->w_lines[i].wl_lnum == wp->w_topline)
1777 {
1778 j = i;
1779 break;
1780 }
1781 row += wp->w_lines[i].wl_size;
1782 }
1783 if (j == -1)
1784 {
1785 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1786 * lines */
1787 mid_start = 0;
1788 }
1789 else
1790 {
1791 /*
1792 * Try to delete the correct number of lines.
1793 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1794 */
1795#ifdef FEAT_DIFF
1796 /* If the topline didn't change, delete old filler lines,
1797 * otherwise delete filler lines of the new topline... */
1798 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1799 row += wp->w_old_topfill;
1800 else
1801 row += diff_check_fill(wp, wp->w_topline);
1802 /* ... but don't delete new filler lines. */
1803 row -= wp->w_topfill;
1804#endif
1805 if (row > 0)
1806 {
1807 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001808 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1809 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 bot_start = wp->w_height - row;
1811 else
1812 mid_start = 0; /* redraw all lines */
1813 }
1814 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1815 {
1816 /*
1817 * Skip the lines (below the deleted lines) that are still
1818 * valid and don't need redrawing. Copy their info
1819 * upwards, to compensate for the deleted lines. Set
1820 * bot_start to the first row that needs redrawing.
1821 */
1822 bot_start = 0;
1823 idx = 0;
1824 for (;;)
1825 {
1826 wp->w_lines[idx] = wp->w_lines[j];
1827 /* stop at line that didn't fit, unless it is still
1828 * valid (no lines deleted) */
1829 if (row > 0 && bot_start + row
1830 + (int)wp->w_lines[j].wl_size > wp->w_height)
1831 {
1832 wp->w_lines_valid = idx + 1;
1833 break;
1834 }
1835 bot_start += wp->w_lines[idx++].wl_size;
1836
1837 /* stop at the last valid entry in w_lines[].wl_size */
1838 if (++j >= wp->w_lines_valid)
1839 {
1840 wp->w_lines_valid = idx;
1841 break;
1842 }
1843 }
1844#ifdef FEAT_DIFF
1845 /* Correct the first entry for filler lines at the top
1846 * when it won't get updated below. */
1847 if (wp->w_p_diff && bot_start > 0)
1848 wp->w_lines[0].wl_size =
1849 plines_win_nofill(wp, wp->w_topline, TRUE)
1850 + wp->w_topfill;
1851#endif
1852 }
1853 }
1854 }
1855
1856 /* When starting redraw in the first line, redraw all lines. When
1857 * there is only one window it's probably faster to clear the screen
1858 * first. */
1859 if (mid_start == 0)
1860 {
1861 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001862 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001863 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001864 /* Clear the screen when it was not done by win_del_lines() or
1865 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1866 * then. */
1867 if (screen_cleared != TRUE)
1868 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001869 /* The screen was cleared, redraw the tab pages line. */
1870 if (redraw_tabline)
1871 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001874
1875 /* When win_del_lines() or win_ins_lines() caused the screen to be
1876 * cleared (only happens for the first window) or when screenclear()
1877 * was called directly above, "must_redraw" will have been set to
1878 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1879 if (screen_cleared == TRUE)
1880 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882 else
1883 {
1884 /* Not VALID or INVERTED: redraw all lines. */
1885 mid_start = 0;
1886 mid_end = wp->w_height;
1887 }
1888
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001889 if (type == SOME_VALID)
1890 {
1891 /* SOME_VALID: redraw all lines. */
1892 mid_start = 0;
1893 mid_end = wp->w_height;
1894 type = NOT_VALID;
1895 }
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 /* check if we are updating or removing the inverted part */
1898 if ((VIsual_active && buf == curwin->w_buffer)
1899 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1900 {
1901 linenr_T from, to;
1902
1903 if (VIsual_active)
1904 {
1905 if (VIsual_active
1906 && (VIsual_mode != wp->w_old_visual_mode
1907 || type == INVERTED_ALL))
1908 {
1909 /*
1910 * If the type of Visual selection changed, redraw the whole
1911 * selection. Also when the ownership of the X selection is
1912 * gained or lost.
1913 */
1914 if (curwin->w_cursor.lnum < VIsual.lnum)
1915 {
1916 from = curwin->w_cursor.lnum;
1917 to = VIsual.lnum;
1918 }
1919 else
1920 {
1921 from = VIsual.lnum;
1922 to = curwin->w_cursor.lnum;
1923 }
1924 /* redraw more when the cursor moved as well */
1925 if (wp->w_old_cursor_lnum < from)
1926 from = wp->w_old_cursor_lnum;
1927 if (wp->w_old_cursor_lnum > to)
1928 to = wp->w_old_cursor_lnum;
1929 if (wp->w_old_visual_lnum < from)
1930 from = wp->w_old_visual_lnum;
1931 if (wp->w_old_visual_lnum > to)
1932 to = wp->w_old_visual_lnum;
1933 }
1934 else
1935 {
1936 /*
1937 * Find the line numbers that need to be updated: The lines
1938 * between the old cursor position and the current cursor
1939 * position. Also check if the Visual position changed.
1940 */
1941 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1942 {
1943 from = curwin->w_cursor.lnum;
1944 to = wp->w_old_cursor_lnum;
1945 }
1946 else
1947 {
1948 from = wp->w_old_cursor_lnum;
1949 to = curwin->w_cursor.lnum;
1950 if (from == 0) /* Visual mode just started */
1951 from = to;
1952 }
1953
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001954 if (VIsual.lnum != wp->w_old_visual_lnum
1955 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 if (wp->w_old_visual_lnum < from
1958 && wp->w_old_visual_lnum != 0)
1959 from = wp->w_old_visual_lnum;
1960 if (wp->w_old_visual_lnum > to)
1961 to = wp->w_old_visual_lnum;
1962 if (VIsual.lnum < from)
1963 from = VIsual.lnum;
1964 if (VIsual.lnum > to)
1965 to = VIsual.lnum;
1966 }
1967 }
1968
1969 /*
1970 * If in block mode and changed column or curwin->w_curswant:
1971 * update all lines.
1972 * First compute the actual start and end column.
1973 */
1974 if (VIsual_mode == Ctrl_V)
1975 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001976 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001977#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001978 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaar404406a2014-10-09 13:24:43 +02001980 if (curwin->w_p_lbr)
1981 ve_flags = VE_ALL;
1982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001984#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001985 ve_flags = save_ve_flags;
1986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 ++toc;
1988 if (curwin->w_curswant == MAXCOL)
1989 toc = MAXCOL;
1990
1991 if (fromc != wp->w_old_cursor_fcol
1992 || toc != wp->w_old_cursor_lcol)
1993 {
1994 if (from > VIsual.lnum)
1995 from = VIsual.lnum;
1996 if (to < VIsual.lnum)
1997 to = VIsual.lnum;
1998 }
1999 wp->w_old_cursor_fcol = fromc;
2000 wp->w_old_cursor_lcol = toc;
2001 }
2002 }
2003 else
2004 {
2005 /* Use the line numbers of the old Visual area. */
2006 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2007 {
2008 from = wp->w_old_cursor_lnum;
2009 to = wp->w_old_visual_lnum;
2010 }
2011 else
2012 {
2013 from = wp->w_old_visual_lnum;
2014 to = wp->w_old_cursor_lnum;
2015 }
2016 }
2017
2018 /*
2019 * There is no need to update lines above the top of the window.
2020 */
2021 if (from < wp->w_topline)
2022 from = wp->w_topline;
2023
2024 /*
2025 * If we know the value of w_botline, use it to restrict the update to
2026 * the lines that are visible in the window.
2027 */
2028 if (wp->w_valid & VALID_BOTLINE)
2029 {
2030 if (from >= wp->w_botline)
2031 from = wp->w_botline - 1;
2032 if (to >= wp->w_botline)
2033 to = wp->w_botline - 1;
2034 }
2035
2036 /*
2037 * Find the minimal part to be updated.
2038 * Watch out for scrolling that made entries in w_lines[] invalid.
2039 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2040 * top_end; need to redraw from top_end to the "to" line.
2041 * A middle mouse click with a Visual selection may change the text
2042 * above the Visual area and reset wl_valid, do count these for
2043 * mid_end (in srow).
2044 */
2045 if (mid_start > 0)
2046 {
2047 lnum = wp->w_topline;
2048 idx = 0;
2049 srow = 0;
2050 if (scrolled_down)
2051 mid_start = top_end;
2052 else
2053 mid_start = 0;
2054 while (lnum < from && idx < wp->w_lines_valid) /* find start */
2055 {
2056 if (wp->w_lines[idx].wl_valid)
2057 mid_start += wp->w_lines[idx].wl_size;
2058 else if (!scrolled_down)
2059 srow += wp->w_lines[idx].wl_size;
2060 ++idx;
2061# ifdef FEAT_FOLDING
2062 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2063 lnum = wp->w_lines[idx].wl_lnum;
2064 else
2065# endif
2066 ++lnum;
2067 }
2068 srow += mid_start;
2069 mid_end = wp->w_height;
2070 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
2071 {
2072 if (wp->w_lines[idx].wl_valid
2073 && wp->w_lines[idx].wl_lnum >= to + 1)
2074 {
2075 /* Only update until first row of this line */
2076 mid_end = srow;
2077 break;
2078 }
2079 srow += wp->w_lines[idx].wl_size;
2080 }
2081 }
2082 }
2083
2084 if (VIsual_active && buf == curwin->w_buffer)
2085 {
2086 wp->w_old_visual_mode = VIsual_mode;
2087 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2088 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00002089 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 wp->w_old_curswant = curwin->w_curswant;
2091 }
2092 else
2093 {
2094 wp->w_old_visual_mode = 0;
2095 wp->w_old_cursor_lnum = 0;
2096 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00002097 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2101 /* reset got_int, otherwise regexp won't work */
2102 save_got_int = got_int;
2103 got_int = 0;
2104#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002105#ifdef SYN_TIME_LIMIT
2106 /* Set the time limit to 'redrawtime'. */
2107 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002108 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110#ifdef FEAT_FOLDING
2111 win_foldinfo.fi_level = 0;
2112#endif
2113
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002114#ifdef FEAT_MENU
2115 /*
2116 * Draw the window toolbar, if there is one.
2117 * TODO: only when needed.
2118 */
2119 if (winbar_height(wp) > 0)
2120 redraw_win_toolbar(wp);
2121#endif
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 /*
2124 * Update all the window rows.
2125 */
2126 idx = 0; /* first entry in w_lines[].wl_size */
2127 row = 0;
2128 srow = 0;
2129 lnum = wp->w_topline; /* first line shown in window */
2130 for (;;)
2131 {
2132 /* stop updating when reached the end of the window (check for _past_
2133 * the end of the window is at the end of the loop) */
2134 if (row == wp->w_height)
2135 {
2136 didline = TRUE;
2137 break;
2138 }
2139
2140 /* stop updating when hit the end of the file */
2141 if (lnum > buf->b_ml.ml_line_count)
2142 {
2143 eof = TRUE;
2144 break;
2145 }
2146
2147 /* Remember the starting row of the line that is going to be dealt
2148 * with. It is used further down when the line doesn't fit. */
2149 srow = row;
2150
2151 /*
2152 * Update a line when it is in an area that needs updating, when it
2153 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002154 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 * win_del_lines(), check if the current line includes it.
2156 * When syntax folding is being used, the saved syntax states will
2157 * already have been updated, we can't see where the syntax state is
2158 * the same again, just update until the end of the window.
2159 */
2160 if (row < top_end
2161 || (row >= mid_start && row < mid_end)
2162#ifdef FEAT_SEARCH_EXTRA
2163 || top_to_mod
2164#endif
2165 || idx >= wp->w_lines_valid
2166 || (row + wp->w_lines[idx].wl_size > bot_start)
2167 || (mod_top != 0
2168 && (lnum == mod_top
2169 || (lnum >= mod_top
2170 && (lnum < mod_bot
2171#ifdef FEAT_SYN_HL
2172 || did_update == DID_FOLD
2173 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002174 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 && (
2176# ifdef FEAT_FOLDING
2177 (foldmethodIsSyntax(wp)
2178 && hasAnyFolding(wp)) ||
2179# endif
2180 syntax_check_changed(lnum)))
2181#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002182#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002183 /* match in fixed position might need redraw
2184 * if lines were inserted or deleted */
2185 || (wp->w_match_head != NULL
2186 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 )))))
2189 {
2190#ifdef FEAT_SEARCH_EXTRA
2191 if (lnum == mod_top)
2192 top_to_mod = FALSE;
2193#endif
2194
2195 /*
2196 * When at start of changed lines: May scroll following lines
2197 * up or down to minimize redrawing.
2198 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002199 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
2201 if (lnum == mod_top
2202 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002203 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
2205 int old_rows = 0;
2206 int new_rows = 0;
2207 int xtra_rows;
2208 linenr_T l;
2209
2210 /* Count the old number of window rows, using w_lines[], which
2211 * should still contain the sizes for the lines as they are
2212 * currently displayed. */
2213 for (i = idx; i < wp->w_lines_valid; ++i)
2214 {
2215 /* Only valid lines have a meaningful wl_lnum. Invalid
2216 * lines are part of the changed area. */
2217 if (wp->w_lines[i].wl_valid
2218 && wp->w_lines[i].wl_lnum == mod_bot)
2219 break;
2220 old_rows += wp->w_lines[i].wl_size;
2221#ifdef FEAT_FOLDING
2222 if (wp->w_lines[i].wl_valid
2223 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2224 {
2225 /* Must have found the last valid entry above mod_bot.
2226 * Add following invalid entries. */
2227 ++i;
2228 while (i < wp->w_lines_valid
2229 && !wp->w_lines[i].wl_valid)
2230 old_rows += wp->w_lines[i++].wl_size;
2231 break;
2232 }
2233#endif
2234 }
2235
2236 if (i >= wp->w_lines_valid)
2237 {
2238 /* We can't find a valid line below the changed lines,
2239 * need to redraw until the end of the window.
2240 * Inserting/deleting lines has no use. */
2241 bot_start = 0;
2242 }
2243 else
2244 {
2245 /* Able to count old number of rows: Count new window
2246 * rows, and may insert/delete lines */
2247 j = idx;
2248 for (l = lnum; l < mod_bot; ++l)
2249 {
2250#ifdef FEAT_FOLDING
2251 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2252 ++new_rows;
2253 else
2254#endif
2255#ifdef FEAT_DIFF
2256 if (l == wp->w_topline)
2257 new_rows += plines_win_nofill(wp, l, TRUE)
2258 + wp->w_topfill;
2259 else
2260#endif
2261 new_rows += plines_win(wp, l, TRUE);
2262 ++j;
2263 if (new_rows > wp->w_height - row - 2)
2264 {
2265 /* it's getting too much, must redraw the rest */
2266 new_rows = 9999;
2267 break;
2268 }
2269 }
2270 xtra_rows = new_rows - old_rows;
2271 if (xtra_rows < 0)
2272 {
2273 /* May scroll text up. If there is not enough
2274 * remaining text or scrolling fails, must redraw the
2275 * rest. If scrolling works, must redraw the text
2276 * below the scrolled text. */
2277 if (row - xtra_rows >= wp->w_height - 2)
2278 mod_bot = MAXLNUM;
2279 else
2280 {
2281 check_for_delay(FALSE);
2282 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002283 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 mod_bot = MAXLNUM;
2285 else
2286 bot_start = wp->w_height + xtra_rows;
2287 }
2288 }
2289 else if (xtra_rows > 0)
2290 {
2291 /* May scroll text down. If there is not enough
2292 * remaining text of scrolling fails, must redraw the
2293 * rest. */
2294 if (row + xtra_rows >= wp->w_height - 2)
2295 mod_bot = MAXLNUM;
2296 else
2297 {
2298 check_for_delay(FALSE);
2299 if (win_ins_lines(wp, row + old_rows,
2300 xtra_rows, FALSE, FALSE) == FAIL)
2301 mod_bot = MAXLNUM;
2302 else if (top_end > row + old_rows)
2303 /* Scrolled the part at the top that requires
2304 * updating down. */
2305 top_end += xtra_rows;
2306 }
2307 }
2308
2309 /* When not updating the rest, may need to move w_lines[]
2310 * entries. */
2311 if (mod_bot != MAXLNUM && i != j)
2312 {
2313 if (j < i)
2314 {
2315 int x = row + new_rows;
2316
2317 /* move entries in w_lines[] upwards */
2318 for (;;)
2319 {
2320 /* stop at last valid entry in w_lines[] */
2321 if (i >= wp->w_lines_valid)
2322 {
2323 wp->w_lines_valid = j;
2324 break;
2325 }
2326 wp->w_lines[j] = wp->w_lines[i];
2327 /* stop at a line that won't fit */
2328 if (x + (int)wp->w_lines[j].wl_size
2329 > wp->w_height)
2330 {
2331 wp->w_lines_valid = j + 1;
2332 break;
2333 }
2334 x += wp->w_lines[j++].wl_size;
2335 ++i;
2336 }
2337 if (bot_start > x)
2338 bot_start = x;
2339 }
2340 else /* j > i */
2341 {
2342 /* move entries in w_lines[] downwards */
2343 j -= i;
2344 wp->w_lines_valid += j;
2345 if (wp->w_lines_valid > wp->w_height)
2346 wp->w_lines_valid = wp->w_height;
2347 for (i = wp->w_lines_valid; i - j >= idx; --i)
2348 wp->w_lines[i] = wp->w_lines[i - j];
2349
2350 /* The w_lines[] entries for inserted lines are
2351 * now invalid, but wl_size may be used above.
2352 * Reset to zero. */
2353 while (i >= idx)
2354 {
2355 wp->w_lines[i].wl_size = 0;
2356 wp->w_lines[i--].wl_valid = FALSE;
2357 }
2358 }
2359 }
2360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362
2363#ifdef FEAT_FOLDING
2364 /*
2365 * When lines are folded, display one line for all of them.
2366 * Otherwise, display normally (can be several display lines when
2367 * 'wrap' is on).
2368 */
2369 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2370 if (fold_count != 0)
2371 {
2372 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2373 ++row;
2374 --fold_count;
2375 wp->w_lines[idx].wl_folded = TRUE;
2376 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2377# ifdef FEAT_SYN_HL
2378 did_update = DID_FOLD;
2379# endif
2380 }
2381 else
2382#endif
2383 if (idx < wp->w_lines_valid
2384 && wp->w_lines[idx].wl_valid
2385 && wp->w_lines[idx].wl_lnum == lnum
2386 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002387 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002388#ifdef FEAT_TEXT_PROP
2389 && !bt_popup(wp->w_buffer)
2390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 && srow + wp->w_lines[idx].wl_size > wp->w_height
2392#ifdef FEAT_DIFF
2393 && diff_check_fill(wp, lnum) == 0
2394#endif
2395 )
2396 {
2397 /* This line is not going to fit. Don't draw anything here,
2398 * will draw "@ " lines below. */
2399 row = wp->w_height + 1;
2400 }
2401 else
2402 {
2403#ifdef FEAT_SEARCH_EXTRA
2404 prepare_search_hl(wp, lnum);
2405#endif
2406#ifdef FEAT_SYN_HL
2407 /* Let the syntax stuff know we skipped a few lines. */
2408 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002409 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 syntax_end_parsing(syntax_last_parsed + 1);
2411#endif
2412
2413 /*
2414 * Display one line.
2415 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002416 row = win_line(wp, lnum, srow, wp->w_height,
2417 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419#ifdef FEAT_FOLDING
2420 wp->w_lines[idx].wl_folded = FALSE;
2421 wp->w_lines[idx].wl_lastlnum = lnum;
2422#endif
2423#ifdef FEAT_SYN_HL
2424 did_update = DID_LINE;
2425 syntax_last_parsed = lnum;
2426#endif
2427 }
2428
2429 wp->w_lines[idx].wl_lnum = lnum;
2430 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002431
2432 /* Past end of the window or end of the screen. Note that after
2433 * resizing wp->w_height may be end up too big. That's a problem
2434 * elsewhere, but prevent a crash here. */
2435 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002438 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2440 ++idx;
2441 break;
2442 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002443 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 wp->w_lines[idx].wl_size = row - srow;
2445 ++idx;
2446#ifdef FEAT_FOLDING
2447 lnum += fold_count + 1;
2448#else
2449 ++lnum;
2450#endif
2451 }
2452 else
2453 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002454 if (wp->w_p_rnu)
2455 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002456#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002457 // 'relativenumber' set: The text doesn't need to be drawn, but
2458 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002459 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2460 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002461 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002462 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002463#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002464 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002465 }
2466
2467 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 row += wp->w_lines[idx++].wl_size;
2469 if (row > wp->w_height) /* past end of screen */
2470 break;
2471#ifdef FEAT_FOLDING
2472 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2473#else
2474 ++lnum;
2475#endif
2476#ifdef FEAT_SYN_HL
2477 did_update = DID_NONE;
2478#endif
2479 }
2480
2481 if (lnum > buf->b_ml.ml_line_count)
2482 {
2483 eof = TRUE;
2484 break;
2485 }
2486 }
2487 /*
2488 * End of loop over all window lines.
2489 */
2490
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002491#ifdef FEAT_VTP
2492 /* Rewrite the character at the end of the screen line. */
2493 if (use_vtp())
2494 {
2495 int i;
2496
2497 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002498 if (enc_utf8)
2499 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2500 LineOffset[i] + screen_Columns) > 1)
2501 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2502 else
2503 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2504 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002505 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2506 }
2507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 if (idx > wp->w_lines_valid)
2510 wp->w_lines_valid = idx;
2511
2512#ifdef FEAT_SYN_HL
2513 /*
2514 * Let the syntax stuff know we stop parsing here.
2515 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002516 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 syntax_end_parsing(syntax_last_parsed + 1);
2518#endif
2519
2520 /*
2521 * If we didn't hit the end of the file, and we didn't finish the last
2522 * line we were working on, then the line didn't fit.
2523 */
2524 wp->w_empty_rows = 0;
2525#ifdef FEAT_DIFF
2526 wp->w_filler_rows = 0;
2527#endif
2528 if (!eof && !didline)
2529 {
2530 if (lnum == wp->w_topline)
2531 {
2532 /*
2533 * Single line that does not fit!
2534 * Don't overwrite it, it can be edited.
2535 */
2536 wp->w_botline = lnum + 1;
2537 }
2538#ifdef FEAT_DIFF
2539 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2540 {
2541 /* Window ends in filler lines. */
2542 wp->w_botline = lnum;
2543 wp->w_filler_rows = wp->w_height - srow;
2544 }
2545#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002546#ifdef FEAT_TEXT_PROP
2547 else if (bt_popup(wp->w_buffer))
2548 {
2549 // popup line that doesn't fit is left as-is
2550 wp->w_botline = lnum;
2551 }
2552#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002553 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2554 {
2555 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2556
2557 /*
2558 * Last line isn't finished: Display "@@@" in the last screen line.
2559 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002560 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002561 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002562 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002563 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002564 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002565 set_empty_rows(wp, srow);
2566 wp->w_botline = lnum;
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2569 {
2570 /*
2571 * Last line isn't finished: Display "@@@" at the end.
2572 */
2573 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2574 W_WINROW(wp) + wp->w_height,
2575 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002576 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 set_empty_rows(wp, srow);
2578 wp->w_botline = lnum;
2579 }
2580 else
2581 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002582 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 wp->w_botline = lnum;
2584 }
2585 }
2586 else
2587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (eof) /* we hit the end of the file */
2590 {
2591 wp->w_botline = buf->b_ml.ml_line_count + 1;
2592#ifdef FEAT_DIFF
2593 j = diff_check_fill(wp, wp->w_botline);
2594 if (j > 0 && !wp->w_botfill)
2595 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002596 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (char2cells(fill_diff) > 1)
2598 i = '-';
2599 else
2600 i = fill_diff;
2601 if (row + j > wp->w_height)
2602 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002603 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 row += j;
2605 }
2606#endif
2607 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002608 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 wp->w_botline = lnum;
2610
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002611 // Make sure the rest of the screen is blank
2612 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002613 win_draw_end(wp,
2614#ifdef FEAT_TEXT_PROP
2615 bt_popup(wp->w_buffer) ? ' ' :
2616#endif
2617 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002620#ifdef SYN_TIME_LIMIT
2621 syn_set_timeout(NULL);
2622#endif
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 /* Reset the type of redrawing required, the window has been updated. */
2625 wp->w_redr_type = 0;
2626#ifdef FEAT_DIFF
2627 wp->w_old_topfill = wp->w_topfill;
2628 wp->w_old_botfill = wp->w_botfill;
2629#endif
2630
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002631 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 /*
2634 * There is a trick with w_botline. If we invalidate it on each
2635 * change that might modify it, this will cause a lot of expensive
2636 * calls to plines() in update_topline() each time. Therefore the
2637 * value of w_botline is often approximated, and this value is used to
2638 * compute the value of w_topline. If the value of w_botline was
2639 * wrong, check that the value of w_topline is correct (cursor is on
2640 * the visible part of the text). If it's not, we need to redraw
2641 * again. Mostly this just means scrolling up a few lines, so it
2642 * doesn't look too bad. Only do this for the current window (where
2643 * changes are relevant).
2644 */
2645 wp->w_valid |= VALID_BOTLINE;
2646 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2647 {
2648 recursive = TRUE;
2649 curwin->w_valid &= ~VALID_TOPLINE;
2650 update_topline(); /* may invalidate w_botline again */
2651 if (must_redraw != 0)
2652 {
2653 /* Don't update for changes in buffer again. */
2654 i = curbuf->b_mod_set;
2655 curbuf->b_mod_set = FALSE;
2656 win_update(curwin);
2657 must_redraw = 0;
2658 curbuf->b_mod_set = i;
2659 }
2660 recursive = FALSE;
2661 }
2662 }
2663
2664#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2665 /* restore got_int, unless CTRL-C was hit while redrawing */
2666 if (!got_int)
2667 got_int = save_got_int;
2668#endif
2669}
2670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002672 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2673 * Return the new offset.
2674 */
2675 static int
2676screen_fill_end(
2677 win_T *wp,
2678 int c1,
2679 int c2,
2680 int off,
2681 int width,
2682 int row,
2683 int endrow,
2684 int attr)
2685{
2686 int nn = off + width;
2687
2688 if (nn > wp->w_width)
2689 nn = wp->w_width;
2690#ifdef FEAT_RIGHTLEFT
2691 if (wp->w_p_rl)
2692 {
2693 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2694 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2695 c1, c2, attr);
2696 }
2697 else
2698#endif
2699 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2700 wp->w_wincol + off, (int)wp->w_wincol + nn,
2701 c1, c2, attr);
2702 return nn;
2703}
2704
2705/*
2706 * Clear lines near the end the window and mark the unused lines with "c1".
2707 * use "c2" as the filler character.
2708 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
2710 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002711win_draw_end(
2712 win_T *wp,
2713 int c1,
2714 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002715 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002716 int row,
2717 int endrow,
2718 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002721 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002722 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002723
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002724 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002725
2726 if (draw_margin)
2727 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002728#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002729 int fdc = compute_foldcolumn(wp, 0);
2730
2731 if (fdc > 0)
2732 // draw the fold column
2733 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002734 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002735#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002736#ifdef FEAT_SIGNS
2737 if (signcolumn_on(wp))
2738 // draw the sign column
2739 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002740 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002741#endif
2742 if ((wp->w_p_nu || wp->w_p_rnu)
2743 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2744 // draw the number column
2745 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002746 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749#ifdef FEAT_RIGHTLEFT
2750 if (wp->w_p_rl)
2751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002753 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002754 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002756 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002757 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759 else
2760#endif
2761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002763 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002764 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002766
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 set_empty_rows(wp, row);
2768}
2769
Bram Moolenaar1a384422010-07-14 19:53:30 +02002770#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002771/*
2772 * Advance **color_cols and return TRUE when there are columns to draw.
2773 */
2774 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002775advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002776{
2777 while (**color_cols >= 0 && vcol > **color_cols)
2778 ++*color_cols;
2779 return (**color_cols >= 0);
2780}
2781#endif
2782
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002783#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2784/*
2785 * Copy "text" to ScreenLines using "attr".
2786 * Returns the next screen column.
2787 */
2788 static int
2789text_to_screenline(win_T *wp, char_u *text, int col)
2790{
2791 int off = (int)(current_ScreenLine - ScreenLines);
2792
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002793 if (has_mbyte)
2794 {
2795 int cells;
2796 int u8c, u8cc[MAX_MCO];
2797 int i;
2798 int idx;
2799 int c_len;
2800 char_u *p;
2801# ifdef FEAT_ARABIC
2802 int prev_c = 0; /* previous Arabic character */
2803 int prev_c1 = 0; /* first composing char for prev_c */
2804# endif
2805
2806# ifdef FEAT_RIGHTLEFT
2807 if (wp->w_p_rl)
2808 idx = off;
2809 else
2810# endif
2811 idx = off + col;
2812
2813 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2814 for (p = text; *p != NUL; )
2815 {
2816 cells = (*mb_ptr2cells)(p);
2817 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002818 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002819# ifdef FEAT_RIGHTLEFT
2820 - (wp->w_p_rl ? col : 0)
2821# endif
2822 )
2823 break;
2824 ScreenLines[idx] = *p;
2825 if (enc_utf8)
2826 {
2827 u8c = utfc_ptr2char(p, u8cc);
2828 if (*p < 0x80 && u8cc[0] == 0)
2829 {
2830 ScreenLinesUC[idx] = 0;
2831#ifdef FEAT_ARABIC
2832 prev_c = u8c;
2833#endif
2834 }
2835 else
2836 {
2837#ifdef FEAT_ARABIC
2838 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2839 {
2840 /* Do Arabic shaping. */
2841 int pc, pc1, nc;
2842 int pcc[MAX_MCO];
2843 int firstbyte = *p;
2844
2845 /* The idea of what is the previous and next
2846 * character depends on 'rightleft'. */
2847 if (wp->w_p_rl)
2848 {
2849 pc = prev_c;
2850 pc1 = prev_c1;
2851 nc = utf_ptr2char(p + c_len);
2852 prev_c1 = u8cc[0];
2853 }
2854 else
2855 {
2856 pc = utfc_ptr2char(p + c_len, pcc);
2857 nc = prev_c;
2858 pc1 = pcc[0];
2859 }
2860 prev_c = u8c;
2861
2862 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2863 pc, pc1, nc);
2864 ScreenLines[idx] = firstbyte;
2865 }
2866 else
2867 prev_c = u8c;
2868#endif
2869 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002870 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002871 for (i = 0; i < Screen_mco; ++i)
2872 {
2873 ScreenLinesC[i][idx] = u8cc[i];
2874 if (u8cc[i] == 0)
2875 break;
2876 }
2877 }
2878 if (cells > 1)
2879 ScreenLines[idx + 1] = 0;
2880 }
2881 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2882 /* double-byte single width character */
2883 ScreenLines2[idx] = p[1];
2884 else if (cells > 1)
2885 /* double-width character */
2886 ScreenLines[idx + 1] = p[1];
2887 col += cells;
2888 idx += cells;
2889 p += c_len;
2890 }
2891 }
2892 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002893 {
2894 int len = (int)STRLEN(text);
2895
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 if (len > wp->w_width - col)
2897 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002898 if (len > 0)
2899 {
2900#ifdef FEAT_RIGHTLEFT
2901 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002902 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002903 else
2904#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002905 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002906 col += len;
2907 }
2908 }
2909 return col;
2910}
2911#endif
2912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#ifdef FEAT_FOLDING
2914/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002915 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2916 * space is available for window "wp", minus "col".
2917 */
2918 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002919compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002920{
2921 int fdc = wp->w_p_fdc;
2922 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002923 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002924
2925 if (fdc > wwidth - (col + wmw))
2926 fdc = wwidth - (col + wmw);
2927 return fdc;
2928}
2929
2930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 * Display one folded line.
2932 */
2933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002934fold_line(
2935 win_T *wp,
2936 long fold_count,
2937 foldinfo_T *foldinfo,
2938 linenr_T lnum,
2939 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002941 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 pos_T *top, *bot;
2943 linenr_T lnume = lnum + fold_count - 1;
2944 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002945 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 int col;
2948 int txtcol;
2949 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002950 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 /* Build the fold line:
2953 * 1. Add the cmdwin_type for the command-line window
2954 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002955 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 * 4. Compose the text
2957 * 5. Add the text
2958 * 6. set highlighting for the Visual area an other text
2959 */
2960 col = 0;
2961
2962 /*
2963 * 1. Add the cmdwin_type for the command-line window
2964 * Ignores 'rightleft', this window is never right-left.
2965 */
2966#ifdef FEAT_CMDWIN
2967 if (cmdwin_type != 0 && wp == curwin)
2968 {
2969 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002970 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (enc_utf8)
2972 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 ++col;
2974 }
2975#endif
2976
2977 /*
2978 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002979 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002981 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 if (fdc > 0)
2983 {
2984 fill_foldcolumn(buf, wp, TRUE, lnum);
2985#ifdef FEAT_RIGHTLEFT
2986 if (wp->w_p_rl)
2987 {
2988 int i;
2989
Bram Moolenaar02631462017-09-22 15:20:32 +02002990 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002991 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 /* reverse the fold column */
2993 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002994 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 else
2997#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002998 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 col += fdc;
3000 }
3001
3002#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02003003# define RL_MEMSET(p, v, l) \
3004 do { \
3005 if (wp->w_p_rl) \
3006 for (ri = 0; ri < l; ++ri) \
3007 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
3008 else \
3009 for (ri = 0; ri < l; ++ri) \
3010 ScreenAttrs[off + (p) + ri] = v; \
3011 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02003013# define RL_MEMSET(p, v, l) \
3014 do { \
3015 for (ri = 0; ri < l; ++ri) \
3016 ScreenAttrs[off + (p) + ri] = v; \
3017 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018#endif
3019
Bram Moolenaar64486672010-05-16 15:46:46 +02003020 /* Set all attributes of the 'number' or 'relativenumber' column and the
3021 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003022 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024#ifdef FEAT_SIGNS
3025 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003026 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003028 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 if (len > 0)
3030 {
3031 if (len > 2)
3032 len = 2;
3033# ifdef FEAT_RIGHTLEFT
3034 if (wp->w_p_rl)
3035 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02003036 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003037 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else
3039# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01003040 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 col += len;
3042 }
3043 }
3044#endif
3045
3046 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02003047 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 */
Bram Moolenaar64486672010-05-16 15:46:46 +02003049 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003051 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 if (len > 0)
3053 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003054 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003055 long num;
3056 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003057
3058 if (len > w + 1)
3059 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02003060
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003061 if (wp->w_p_nu && !wp->w_p_rnu)
3062 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003063 num = (long)lnum;
3064 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003065 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003066 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003067 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003068 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003069 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003070 /* 'number' + 'relativenumber': cursor line shows absolute
3071 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003072 num = lnum;
3073 fmt = "%-*ld ";
3074 }
3075 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003076
Bram Moolenaar700e7342013-01-30 12:31:36 +01003077 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078#ifdef FEAT_RIGHTLEFT
3079 if (wp->w_p_rl)
3080 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02003081 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003082 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 else
3084#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01003085 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 col += len;
3087 }
3088 }
3089
3090 /*
3091 * 4. Compose the folded-line string with 'foldtext', if set.
3092 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003093 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094
3095 txtcol = col; /* remember where text starts */
3096
3097 /*
3098 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
3099 * Right-left text is put in columns 0 - number-col, normal text is put
3100 * in columns number-col - window-width.
3101 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003102 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104 /* Fill the rest of the line with the fold filler */
3105#ifdef FEAT_RIGHTLEFT
3106 if (wp->w_p_rl)
3107 col -= txtcol;
3108#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003109 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#ifdef FEAT_RIGHTLEFT
3111 - (wp->w_p_rl ? txtcol : 0)
3112#endif
3113 )
3114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 if (enc_utf8)
3116 {
3117 if (fill_fold >= 0x80)
3118 {
3119 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003120 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01003121 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 }
3123 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003126 ScreenLines[off + col] = fill_fold;
3127 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003128 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003130 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003131 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 }
3133
3134 if (text != buf)
3135 vim_free(text);
3136
3137 /*
3138 * 6. set highlighting for the Visual area an other text.
3139 * If all folded lines are in the Visual area, highlight the line.
3140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3142 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003143 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 /* Visual is after curwin->w_cursor */
3146 top = &curwin->w_cursor;
3147 bot = &VIsual;
3148 }
3149 else
3150 {
3151 /* Visual is before curwin->w_cursor */
3152 top = &VIsual;
3153 bot = &curwin->w_cursor;
3154 }
3155 if (lnum >= top->lnum
3156 && lnume <= bot->lnum
3157 && (VIsual_mode != 'v'
3158 || ((lnum > top->lnum
3159 || (lnum == top->lnum
3160 && top->col == 0))
3161 && (lnume < bot->lnum
3162 || (lnume == bot->lnum
3163 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003164 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 if (VIsual_mode == Ctrl_V)
3167 {
3168 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003169 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003171 if (wp->w_old_cursor_lcol != MAXCOL
3172 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003173 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 len = wp->w_old_cursor_lcol;
3175 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003176 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003177 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003178 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180 }
3181 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003184 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003189#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003190 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3191 if (wp->w_p_cc_cols)
3192 {
3193 int i = 0;
3194 int j = wp->w_p_cc_cols[i];
3195 int old_txtcol = txtcol;
3196
3197 while (j > -1)
3198 {
3199 txtcol += j;
3200 if (wp->w_p_wrap)
3201 txtcol -= wp->w_skipcol;
3202 else
3203 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003204 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003205 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003206 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003207 txtcol = old_txtcol;
3208 j = wp->w_p_cc_cols[++i];
3209 }
3210 }
3211
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003212 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003213 if (wp->w_p_cuc)
3214 {
3215 txtcol += wp->w_virtcol;
3216 if (wp->w_p_wrap)
3217 txtcol -= wp->w_skipcol;
3218 else
3219 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003220 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003221 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003222 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003223 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
Bram Moolenaar02631462017-09-22 15:20:32 +02003226 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003227 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
3229 /*
3230 * Update w_cline_height and w_cline_folded if the cursor line was
3231 * updated (saves a call to plines() later).
3232 */
3233 if (wp == curwin
3234 && lnum <= curwin->w_cursor.lnum
3235 && lnume >= curwin->w_cursor.lnum)
3236 {
3237 curwin->w_cline_row = row;
3238 curwin->w_cline_height = 1;
3239 curwin->w_cline_folded = TRUE;
3240 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3241 }
3242}
3243
3244/*
3245 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3246 */
3247 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003248copy_text_attr(
3249 int off,
3250 char_u *buf,
3251 int len,
3252 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003254 int i;
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (enc_utf8)
3258 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003259 for (i = 0; i < len; ++i)
3260 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261}
3262
3263/*
3264 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003265 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
3267 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003268fill_foldcolumn(
3269 char_u *p,
3270 win_T *wp,
3271 int closed, /* TRUE of FALSE */
3272 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 int i = 0;
3275 int level;
3276 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003277 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003278 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
3280 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003281 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 level = win_foldinfo.fi_level;
3284 if (level > 0)
3285 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003286 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003287 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 /* If the column is too narrow, we start at the lowest level that
3290 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003291 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (first_level < 1)
3293 first_level = 1;
3294
Bram Moolenaar1c934292015-01-27 16:39:29 +01003295 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 if (win_foldinfo.fi_lnum == lnum
3298 && first_level + i >= win_foldinfo.fi_low_level)
3299 p[i] = '-';
3300 else if (first_level == 1)
3301 p[i] = '|';
3302 else if (first_level + i <= 9)
3303 p[i] = '0' + first_level + i;
3304 else
3305 p[i] = '>';
3306 if (first_level + i == level)
3307 break;
3308 }
3309 }
3310 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003311 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313#endif /* FEAT_FOLDING */
3314
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003315#ifdef FEAT_TEXT_PROP
3316static textprop_T *current_text_props = NULL;
3317static buf_T *current_buf = NULL;
3318
3319 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003320text_prop_compare(const void *s1, const void *s2)
3321{
3322 int idx1, idx2;
3323 proptype_T *pt1, *pt2;
3324 colnr_T col1, col2;
3325
3326 idx1 = *(int *)s1;
3327 idx2 = *(int *)s2;
3328 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3329 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3330 if (pt1 == pt2)
3331 return 0;
3332 if (pt1 == NULL)
3333 return -1;
3334 if (pt2 == NULL)
3335 return 1;
3336 if (pt1->pt_priority != pt2->pt_priority)
3337 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3338 col1 = current_text_props[idx1].tp_col;
3339 col2 = current_text_props[idx2].tp_col;
3340 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3341}
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * Display line "lnum" of window 'wp' on the screen.
3346 * Start at row "startrow", stop when "endrow" is reached.
3347 * wp->w_virtcol needs to be valid.
3348 *
3349 * Return the number of last row the line occupies.
3350 */
3351 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003352win_line(
3353 win_T *wp,
3354 linenr_T lnum,
3355 int startrow,
3356 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 int nochange UNUSED, // not updating for changed text
3358 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003360 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3362 int c = 0; /* init for GCC */
3363 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003364#ifdef FEAT_LINEBREAK
3365 long vcol_sbr = -1; /* virtual column after showbreak */
3366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 long vcol_prev = -1; /* "vcol" of previous character */
3368 char_u *line; /* current line */
3369 char_u *ptr; /* current position in "line" */
3370 int row; /* row in the window, excl w_winrow */
3371 int screen_row; /* row on the screen, incl w_winrow */
3372
3373 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3374 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003375 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003376 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003378 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 int extra_attr = 0; /* attributes when n_extra != 0 */
3380 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3381 displaying lcs_eol at end-of-line */
3382 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3383 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3384
3385 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3386 int saved_n_extra = 0;
3387 char_u *saved_p_extra = NULL;
3388 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003389 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 int saved_char_attr = 0;
3391
3392 int n_attr = 0; /* chars with special attr */
3393 int saved_attr2 = 0; /* char_attr saved for n_attr */
3394 int n_attr3 = 0; /* chars with overruling special attr */
3395 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3396
3397 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3398
3399 int fromcol, tocol; /* start/end of inverting */
3400 int fromcol_prev = -2; /* start of inverting after cursor */
3401 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003403 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 pos_T pos;
3405 long v;
3406
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003407 int char_attr = 0; // attributes for next character
3408 int attr_pri = FALSE; // char_attr has priority
3409 int area_highlighting = FALSE; // Visual or incsearch highlighting
3410 // in this line
3411 int vi_attr = 0; // attributes for Visual and incsearch
3412 // highlighting
3413 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003414 int win_attr = 0; // background for whole window, except
3415 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003416 int area_attr = 0; // attributes desired by highlighting
3417 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003419 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 int syntax_attr = 0; /* attributes desired by syntax */
3421 int has_syntax = FALSE; /* this buffer has syntax highl. */
3422 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003423 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003424 int draw_color_col = FALSE; /* highlight colorcolumn */
3425 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003426#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003427#ifdef FEAT_TEXT_PROP
3428 int text_prop_count;
3429 int text_prop_next = 0; // next text property to use
3430 textprop_T *text_props = NULL;
3431 int *text_prop_idxs = NULL;
3432 int text_props_active = 0;
3433 proptype_T *text_prop_type = NULL;
3434 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003435 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003436#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003437#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003438 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003439# define SPWORDLEN 150
3440 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003441 int nextlinecol = 0; /* column where nextline[] starts */
3442 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003443 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003444 int spell_attr = 0; /* attributes desired by spelling */
3445 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003446 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3447 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003448 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003449 static int cap_col = -1; /* column to check for Cap word */
3450 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003451 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003453 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int multi_attr = 0; /* attributes desired by multibyte */
3455 int mb_l = 1; /* multi-byte byte length */
3456 int mb_c = 0; /* decoded multi-byte character */
3457 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003458 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#ifdef FEAT_DIFF
3460 int filler_lines; /* nr of filler lines to be drawn */
3461 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003462 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 int change_start = MAXCOL; /* first col of changed area */
3464 int change_end = -1; /* last col of changed area */
3465#endif
3466 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3467#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003468 int need_showbreak = FALSE; /* overlong line, skipping first x
3469 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003471#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003472 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003474 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#endif
3476#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003477 matchitem_T *cur; /* points to the match list */
3478 match_T *shl; /* points to search_hl or a match */
3479 int shl_flag; /* flag to indicate whether search_hl
3480 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003481 int pos_inprogress; /* marks that position match search is
3482 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003483 int prevcol_hl_flag; /* flag to indicate whether prevcol
3484 equals startcol of search_hl or one
3485 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#endif
3487#ifdef FEAT_ARABIC
3488 int prev_c = 0; /* previous Arabic character */
3489 int prev_c1 = 0; /* first composing char for prev_c */
3490#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003491#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003492 int did_line_attr = 0;
3493#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003494#ifdef FEAT_TERMINAL
3495 int get_term_attr = FALSE;
3496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 /* draw_state: items that are drawn in sequence: */
3499#define WL_START 0 /* nothing done yet */
3500#ifdef FEAT_CMDWIN
3501# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3502#else
3503# define WL_CMDLINE WL_START
3504#endif
3505#ifdef FEAT_FOLDING
3506# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3507#else
3508# define WL_FOLD WL_CMDLINE
3509#endif
3510#ifdef FEAT_SIGNS
3511# define WL_SIGN WL_FOLD + 1 /* column for signs */
3512#else
3513# define WL_SIGN WL_FOLD /* column for signs */
3514#endif
3515#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003516#ifdef FEAT_LINEBREAK
3517# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003519# define WL_BRI WL_NR
3520#endif
3521#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3522# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3523#else
3524# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525#endif
3526#define WL_LINE WL_SBR + 1 /* text in the line */
3527 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003528#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 int feedback_col = 0;
3530 int feedback_old_attr = -1;
3531#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003532 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaar860cae12010-06-05 23:22:07 +02003534#ifdef FEAT_CONCEAL
3535 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003536 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003537 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003538 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003539 int is_concealing = FALSE;
3540 int boguscols = 0; /* nonexistent columns added to force
3541 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003542 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003543 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003544 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003545 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003546# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003547# define FIX_FOR_BOGUSCOLS \
3548 { \
3549 n_extra += vcol_off; \
3550 vcol -= vcol_off; \
3551 vcol_off = 0; \
3552 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003553 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003554 boguscols = 0; \
3555 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003556#else
3557# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 if (startrow > endrow) /* past the end already! */
3561 return startrow;
3562
3563 row = startrow;
3564 screen_row = row + W_WINROW(wp);
3565
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003566 if (!number_only)
3567 {
3568 /*
3569 * To speed up the loop below, set extra_check when there is linebreak,
3570 * trailing white space and/or syntax processing to be done.
3571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003573 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574#endif
3575#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003576 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003577# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003578 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003579# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003580 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003582 /* Prepare for syntax highlighting in this line. When there is an
3583 * error, stop syntax highlighting. */
3584 save_did_emsg = did_emsg;
3585 did_emsg = FALSE;
3586 syntax_start(wp, lnum);
3587 if (did_emsg)
3588 wp->w_s->b_syn_error = TRUE;
3589 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003590 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003591 did_emsg = save_did_emsg;
3592#ifdef SYN_TIME_LIMIT
3593 if (!wp->w_s->b_syn_slow)
3594#endif
3595 {
3596 has_syntax = TRUE;
3597 extra_check = TRUE;
3598 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003601
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003602 /* Check for columns to display for 'colorcolumn'. */
3603 color_cols = wp->w_p_cc_cols;
3604 if (color_cols != NULL)
3605 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003606#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003607
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003608#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003609 if (term_show_buffer(wp->w_buffer))
3610 {
3611 extra_check = TRUE;
3612 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003613 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003614 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003615#endif
3616
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003617#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003618 if (wp->w_p_spell
3619 && *wp->w_s->b_p_spl != NUL
3620 && wp->w_s->b_langp.ga_len > 0
3621 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003622 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003623 /* Prepare for spell checking. */
3624 has_spell = TRUE;
3625 extra_check = TRUE;
3626
Bram Moolenaar7701f302018-10-02 21:20:32 +02003627 /* Get the start of the next line, so that words that wrap to the
3628 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003629 * Trick: skip a few chars for C/shell/Vim comments */
3630 nextline[SPWORDLEN] = NUL;
3631 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3632 {
3633 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3634 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3635 }
3636
Bram Moolenaar7701f302018-10-02 21:20:32 +02003637 /* When a word wrapped from the previous line the start of the
3638 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003639 if (lnum == checked_lnum)
3640 cur_checked_col = checked_col;
3641 checked_lnum = 0;
3642
3643 /* When there was a sentence end in the previous line may require a
3644 * word starting with capital in this line. In line 1 always check
3645 * the first word. */
3646 if (lnum != capcol_lnum)
3647 cap_col = -1;
3648 if (lnum == 1)
3649 cap_col = 0;
3650 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652#endif
3653
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003654 /*
3655 * handle visual active in this window
3656 */
3657 fromcol = -10;
3658 tocol = MAXCOL;
3659 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003661 /* Visual is after curwin->w_cursor */
3662 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003664 top = &curwin->w_cursor;
3665 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003667 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003669 top = &VIsual;
3670 bot = &curwin->w_cursor;
3671 }
3672 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3673 if (VIsual_mode == Ctrl_V) /* block mode */
3674 {
3675 if (lnum_in_visual_area)
3676 {
3677 fromcol = wp->w_old_cursor_fcol;
3678 tocol = wp->w_old_cursor_lcol;
3679 }
3680 }
3681 else /* non-block mode */
3682 {
3683 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003685 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003687 if (VIsual_mode == 'V') /* linewise */
3688 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 else
3690 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003691 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3692 if (gchar_pos(top) == NUL)
3693 tocol = fromcol + 1;
3694 }
3695 }
3696 if (VIsual_mode != 'V' && lnum == bot->lnum)
3697 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003698 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003699 {
3700 fromcol = -10;
3701 tocol = MAXCOL;
3702 }
3703 else if (bot->col == MAXCOL)
3704 tocol = MAXCOL;
3705 else
3706 {
3707 pos = *bot;
3708 if (*p_sel == 'e')
3709 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3710 else
3711 {
3712 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3713 ++tocol;
3714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 }
3717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003719 /* Check if the character under the cursor should not be inverted */
3720 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003721#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003722 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003723#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003724 )
3725 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003727 /* if inverting in this line set area_highlighting */
3728 if (fromcol >= 0)
3729 {
3730 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003731 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003733 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003734 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003735 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003736 && clip_isautosel_plus()))
3737 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003742 /*
3743 * handle 'incsearch' and ":s///c" highlighting
3744 */
3745 else if (highlight_match
3746 && wp == curwin
3747 && lnum >= curwin->w_cursor.lnum
3748 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003750 if (lnum == curwin->w_cursor.lnum)
3751 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003752 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003753 else
3754 fromcol = 0;
3755 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3756 {
3757 pos.lnum = lnum;
3758 pos.col = search_match_endcol;
3759 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3760 }
3761 else
3762 tocol = MAXCOL;
3763 /* do at least one character; happens when past end of line */
3764 if (fromcol == tocol)
3765 tocol = fromcol + 1;
3766 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003767 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770
3771#ifdef FEAT_DIFF
3772 filler_lines = diff_check(wp, lnum);
3773 if (filler_lines < 0)
3774 {
3775 if (filler_lines == -1)
3776 {
3777 if (diff_find_change(wp, lnum, &change_start, &change_end))
3778 diff_hlf = HLF_ADD; /* added line */
3779 else if (change_start == 0)
3780 diff_hlf = HLF_TXD; /* changed text */
3781 else
3782 diff_hlf = HLF_CHD; /* changed line */
3783 }
3784 else
3785 diff_hlf = HLF_ADD; /* added line */
3786 filler_lines = 0;
3787 area_highlighting = TRUE;
3788 }
3789 if (lnum == wp->w_topline)
3790 filler_lines = wp->w_topfill;
3791 filler_todo = filler_lines;
3792#endif
3793
3794#ifdef LINE_ATTR
3795# ifdef FEAT_SIGNS
3796 /* If this line has a sign with line highlighting set line_attr. */
3797 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3798 if (v != 0)
3799 line_attr = sign_get_attr((int)v, TRUE);
3800# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003801# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003803 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003804 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805# endif
3806 if (line_attr != 0)
3807 area_highlighting = TRUE;
3808#endif
3809
3810 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3811 ptr = line;
3812
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003813#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003814 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003815 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003816 /* For checking first word with a capital skip white space. */
3817 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003818 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003819
Bram Moolenaar30abd282005-06-22 22:35:10 +00003820 /* To be able to spell-check over line boundaries copy the end of the
3821 * current line into nextline[]. Above the start of the next line was
3822 * copied to nextline[SPWORDLEN]. */
3823 if (nextline[SPWORDLEN] == NUL)
3824 {
3825 /* No next line or it is empty. */
3826 nextlinecol = MAXCOL;
3827 nextline_idx = 0;
3828 }
3829 else
3830 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003831 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003832 if (v < SPWORDLEN)
3833 {
3834 /* Short line, use it completely and append the start of the
3835 * next line. */
3836 nextlinecol = 0;
3837 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003838 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003839 nextline_idx = v + 1;
3840 }
3841 else
3842 {
3843 /* Long line, use only the last SPWORDLEN bytes. */
3844 nextlinecol = v - SPWORDLEN;
3845 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3846 nextline_idx = SPWORDLEN + 1;
3847 }
3848 }
3849 }
3850#endif
3851
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003852 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003854 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003855 extra_check = TRUE;
3856 /* find start of trailing whitespace */
3857 if (lcs_trail)
3858 {
3859 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003860 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003861 --trailcol;
3862 trailcol += (colnr_T) (ptr - line);
3863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003866 wcr_attr = get_wcr_attr(wp);
3867 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003868 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003869 win_attr = wcr_attr;
3870 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003871 }
3872#ifdef FEAT_TEXT_PROP
3873 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003874 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003875#endif
3876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 /*
3878 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3879 * first character to be displayed.
3880 */
3881 if (wp->w_p_wrap)
3882 v = wp->w_skipcol;
3883 else
3884 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003885 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 while (vcol < v && *ptr != NUL)
3890 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003891 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003894 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003897 /* When:
3898 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003899 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003900 * - 'virtualedit' is set, or
3901 * - the visual mode is active,
3902 * the end of the line may be before the start of the displayed part.
3903 */
3904 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003905#ifdef FEAT_SYN_HL
3906 wp->w_p_cuc || draw_color_col ||
3907#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003908 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003909 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
3912 /* Handle a character that's not completely on the screen: Put ptr at
3913 * that character but skip the first few screen characters. */
3914 if (vcol > v)
3915 {
3916 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003918 /* If the character fits on the screen, don't need to skip it.
3919 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003920 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003921 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923
3924 /*
3925 * Adjust for when the inverted text is before the screen,
3926 * and when the start of the inverted text is before the screen.
3927 */
3928 if (tocol <= vcol)
3929 fromcol = 0;
3930 else if (fromcol >= 0 && fromcol < vcol)
3931 fromcol = vcol;
3932
3933#ifdef FEAT_LINEBREAK
3934 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3935 if (wp->w_p_wrap)
3936 need_showbreak = TRUE;
3937#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003938#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003939 /* When spell checking a word we need to figure out the start of the
3940 * word and if it's badly spelled or not. */
3941 if (has_spell)
3942 {
3943 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003944 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003945 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003946
3947 pos = wp->w_cursor;
3948 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003949 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003950 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003951
3952 /* spell_move_to() may call ml_get() and make "line" invalid */
3953 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3954 ptr = line + linecol;
3955
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003956 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003957 {
3958 /* no bad word found at line start, don't check until end of a
3959 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003960 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003961 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003962 }
3963 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003964 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003965 /* bad word found, use attributes until end of word */
3966 word_end = wp->w_cursor.col + len + 1;
3967
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003968 /* Turn index into actual attributes. */
3969 if (spell_hlf != HLF_COUNT)
3970 spell_attr = highlight_attr[spell_hlf];
3971 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003972 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003973
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003974# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003975 /* Need to restart syntax highlighting for this line. */
3976 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003977 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003978# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003979 }
3980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982
3983 /*
3984 * Correct highlighting for cursor that can't be disabled.
3985 * Avoids having to check this for each character.
3986 */
3987 if (fromcol >= 0)
3988 {
3989 if (noinvcur)
3990 {
3991 if ((colnr_T)fromcol == wp->w_virtcol)
3992 {
3993 /* highlighting starts at cursor, let it start just after the
3994 * cursor */
3995 fromcol_prev = fromcol;
3996 fromcol = -1;
3997 }
3998 else if ((colnr_T)fromcol < wp->w_virtcol)
3999 /* restart highlighting after the cursor */
4000 fromcol_prev = wp->w_virtcol;
4001 }
4002 if (fromcol >= tocol)
4003 fromcol = -1;
4004 }
4005
4006#ifdef FEAT_SEARCH_EXTRA
4007 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004008 * Handle highlighting the last used search pattern and matches.
4009 * Do this for both search_hl and the match list.
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004010 * Do not use search_hl in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004012 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004013 shl_flag = (screen_line_flags & SLF_POPUP);
4014 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004016 if (shl_flag == FALSE)
4017 {
4018 shl = &search_hl;
4019 shl_flag = TRUE;
4020 }
4021 else
4022 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004023 shl->startcol = MAXCOL;
4024 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02004026 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004027 v = (long)(ptr - line);
4028 if (cur != NULL)
4029 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004030 next_search_hl(wp, shl, lnum, (colnr_T)v,
4031 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004032
4033 /* Need to get the line again, a multi-line regexp may have made it
4034 * invalid. */
4035 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4036 ptr = line + v;
4037
4038 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02004040 if (shl->lnum == lnum)
4041 shl->startcol = shl->rm.startpos[0].col;
4042 else
4043 shl->startcol = 0;
4044 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
4045 - shl->rm.startpos[0].lnum)
4046 shl->endcol = shl->rm.endpos[0].col;
4047 else
4048 shl->endcol = MAXCOL;
4049 /* Highlight one character for an empty match. */
4050 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02004052 if (has_mbyte && line[shl->endcol] != NUL)
4053 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
4054 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02004055 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02004057 if ((long)shl->startcol < v) /* match at leftcol */
4058 {
4059 shl->attr_cur = shl->attr;
4060 search_attr = shl->attr;
4061 }
4062 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004064 if (shl != &search_hl && cur != NULL)
4065 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067#endif
4068
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004069#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004070 // Cursor line highlighting for 'cursorline' in the current window.
4071 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004072 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004073 // Do not show the cursor line when Visual mode is active, because it's
4074 // not clear what is selected then. Do update w_last_cursorline.
4075 if (!(wp == curwin && VIsual_active))
4076 {
4077 line_attr = HL_ATTR(HLF_CUL);
4078 area_highlighting = TRUE;
4079 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01004080 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004081 }
4082#endif
4083
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004084#ifdef FEAT_TEXT_PROP
4085 {
4086 char_u *prop_start;
4087
4088 text_prop_count = get_text_props(wp->w_buffer, lnum,
4089 &prop_start, FALSE);
4090 if (text_prop_count > 0)
4091 {
4092 // Make a copy of the properties, so that they are properly
4093 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004094 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004095 if (text_props != NULL)
4096 mch_memmove(text_props, prop_start,
4097 text_prop_count * sizeof(textprop_T));
4098
4099 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004100 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004101 area_highlighting = TRUE;
4102 extra_check = TRUE;
4103 }
4104 }
4105#endif
4106
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004107 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_RIGHTLEFT
4111 if (wp->w_p_rl)
4112 {
4113 /* Rightleft window: process the text in the normal direction, but put
4114 * it in current_ScreenLine[] from right to left. Start at the
4115 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02004116 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004118 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120#endif
4121
4122 /*
4123 * Repeat for the whole displayed line.
4124 */
4125 for (;;)
4126 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02004127#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004128 int has_match_conc = 0; // match wants to conceal
4129 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 /* Skip this quickly when working on the text. */
4132 if (draw_state != WL_LINE)
4133 {
4134#ifdef FEAT_CMDWIN
4135 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4136 {
4137 draw_state = WL_CMDLINE;
4138 if (cmdwin_type != 0 && wp == curwin)
4139 {
4140 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004142 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004143 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004144 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 }
4147#endif
4148
4149#ifdef FEAT_FOLDING
4150 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4151 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004152 int fdc = compute_foldcolumn(wp, 0);
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004155 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004157 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004158 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004159 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004160 p_extra_free = alloc(12 + 1);
4161
4162 if (p_extra_free != NULL)
4163 {
4164 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4165 n_extra = fdc;
4166 p_extra_free[n_extra] = NUL;
4167 p_extra = p_extra_free;
4168 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004169 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004170 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 }
4174#endif
4175
4176#ifdef FEAT_SIGNS
4177 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4178 {
4179 draw_state = WL_SIGN;
4180 /* Show the sign column when there are any signs in this
4181 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004182 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004184 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004186 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187# endif
4188
4189 /* Draw two cells with the sign value or blank. */
4190 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004191 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004192 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 n_extra = 2;
4194
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004195 if (row == startrow
4196#ifdef FEAT_DIFF
4197 + filler_lines && filler_todo <= 0
4198#endif
4199 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4202 SIGN_TEXT);
4203# ifdef FEAT_SIGN_ICONS
4204 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4205 SIGN_ICON);
4206 if (gui.in_use && icon_sign != 0)
4207 {
4208 /* Use the image in this position. */
4209 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004210 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211# ifdef FEAT_NETBEANS_INTG
4212 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004215 c_final = NUL;
4216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217# endif
4218 char_attr = icon_sign;
4219 }
4220 else
4221# endif
4222 if (text_sign != 0)
4223 {
4224 p_extra = sign_get_text(text_sign);
4225 if (p_extra != NULL)
4226 {
4227 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004228 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004229 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 char_attr = sign_get_attr(text_sign, FALSE);
4232 }
4233 }
4234 }
4235 }
4236#endif
4237
4238 if (draw_state == WL_NR - 1 && n_extra == 0)
4239 {
4240 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004241 /* Display the absolute or relative line number. After the
4242 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4243 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 && (row == startrow
4245#ifdef FEAT_DIFF
4246 + filler_lines
4247#endif
4248 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4249 {
4250 /* Draw the line number (empty space after wrapping). */
4251 if (row == startrow
4252#ifdef FEAT_DIFF
4253 + filler_lines
4254#endif
4255 )
4256 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004257 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004258 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004259
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004260 if (wp->w_p_nu && !wp->w_p_rnu)
4261 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004262 num = (long)lnum;
4263 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004264 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004265 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004266 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004267 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004268 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004269 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004270 num = lnum;
4271 fmt = "%-*ld ";
4272 }
4273 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004274
Bram Moolenaar700e7342013-01-30 12:31:36 +01004275 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004276 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (wp->w_skipcol > 0)
4278 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4279 *p_extra = '-';
4280#ifdef FEAT_RIGHTLEFT
4281 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004282 {
4283 char_u *p1, *p2;
4284 int t;
4285
4286 // like rl_mirror(), but keep the space at the end
4287 p2 = skiptowhite(extra) - 1;
4288 for (p1 = extra; p1 < p2; ++p1, --p2)
4289 {
4290 t = *p1;
4291 *p1 = *p2;
4292 *p2 = t;
4293 }
4294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif
4296 p_extra = extra;
4297 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004298 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004303 c_final = NUL;
4304 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004305 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004306 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004307#ifdef FEAT_SYN_HL
4308 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004309 * the current line differently.
4310 * TODO: Can we use CursorLine instead of CursorLineNr
4311 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004312 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004313 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004314 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 }
4318
Bram Moolenaar597a4222014-06-25 14:39:50 +02004319#ifdef FEAT_LINEBREAK
4320 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4321 && n_extra == 0 && *p_sbr != NUL)
4322 /* draw indent after showbreak value */
4323 draw_state = WL_BRI;
4324 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4325 /* After the showbreak, draw the breakindent */
4326 draw_state = WL_BRI - 1;
4327
4328 /* draw 'breakindent': indent wrapped text accordingly */
4329 if (draw_state == WL_BRI - 1 && n_extra == 0)
4330 {
4331 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004332 /* if need_showbreak is set, breakindent also applies */
4333 if (wp->w_p_bri && n_extra == 0
4334 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004335# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004336 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004337# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004338 )
4339 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004340 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004341# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004342 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004343 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004344 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004345# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004346 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4347 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004348 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004349# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004350 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004351# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004352 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004353 c_extra = ' ';
4354 n_extra = get_breakindent_win(wp,
4355 ml_get_buf(wp->w_buffer, lnum, FALSE));
4356 /* Correct end of highlighted area for 'breakindent',
4357 * required when 'linebreak' is also set. */
4358 if (tocol == vcol)
4359 tocol += n_extra;
4360 }
4361 }
4362#endif
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4365 if (draw_state == WL_SBR - 1 && n_extra == 0)
4366 {
4367 draw_state = WL_SBR;
4368# ifdef FEAT_DIFF
4369 if (filler_todo > 0)
4370 {
4371 /* Draw "deleted" diff line(s). */
4372 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004375 c_final = NUL;
4376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004380 c_final = NUL;
4381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382# ifdef FEAT_RIGHTLEFT
4383 if (wp->w_p_rl)
4384 n_extra = col + 1;
4385 else
4386# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004387 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004388 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390# endif
4391# ifdef FEAT_LINEBREAK
4392 if (*p_sbr != NUL && need_showbreak)
4393 {
4394 /* Draw 'showbreak' at the start of each broken line. */
4395 p_extra = p_sbr;
4396 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004397 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004399 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004401 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /* Correct end of highlighted area for 'showbreak',
4403 * required when 'linebreak' is also set. */
4404 if (tocol == vcol)
4405 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004406#ifdef FEAT_SYN_HL
4407 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004408 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004409 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004410 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413# endif
4414 }
4415#endif
4416
4417 if (draw_state == WL_LINE - 1 && n_extra == 0)
4418 {
4419 draw_state = WL_LINE;
4420 if (saved_n_extra)
4421 {
4422 /* Continue item from end of wrapped line. */
4423 n_extra = saved_n_extra;
4424 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004425 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 p_extra = saved_p_extra;
4427 char_attr = saved_char_attr;
4428 }
4429 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004430 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 }
4433
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004434 // When still displaying '$' of change command, stop at cursor.
4435 // When only displaying the (relative) line number and that's done,
4436 // stop here.
4437 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004438 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439#ifdef FEAT_DIFF
4440 && filler_todo <= 0
4441#endif
4442 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004443 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004445 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004446 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004447 /* Pretend we have finished updating the window. Except when
4448 * 'cursorcolumn' is set. */
4449#ifdef FEAT_SYN_HL
4450 if (wp->w_p_cuc)
4451 row = wp->w_cline_row + wp->w_cline_height;
4452 else
4453#endif
4454 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 break;
4456 }
4457
Bram Moolenaar637532b2019-01-03 21:44:40 +01004458 if (draw_state == WL_LINE && (area_highlighting
4459#ifdef FEAT_SPELL
4460 || has_spell
4461#endif
4462 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
4464 /* handle Visual or match highlighting in this line */
4465 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4467 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004469 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004471 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 else if (area_attr != 0
4473 && (vcol == tocol
4474 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477#ifdef FEAT_SEARCH_EXTRA
4478 if (!n_extra)
4479 {
4480 /*
4481 * Check for start/end of search pattern match.
4482 * After end, check for start/end of next match.
4483 * When another match, have to check for start again.
4484 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004485 * Do this for 'search_hl' and the match list (ordered by
4486 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004488 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004489 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004490 shl_flag = (screen_line_flags & SLF_POPUP);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004491 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004493 if (shl_flag == FALSE
4494 && ((cur != NULL
4495 && cur->priority > SEARCH_HL_PRIORITY)
4496 || cur == NULL))
4497 {
4498 shl = &search_hl;
4499 shl_flag = TRUE;
4500 }
4501 else
4502 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004503 if (cur != NULL)
4504 cur->pos.cur = 0;
4505 pos_inprogress = TRUE;
4506 while (shl->rm.regprog != NULL
4507 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004509 if (shl->startcol != MAXCOL
4510 && v >= (long)shl->startcol
4511 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004513 int tmp_col = v + MB_PTR2LEN(ptr);
4514
4515 if (shl->endcol < tmp_col)
4516 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004518#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004519 // Match with the "Conceal" group results in hiding
4520 // the match.
4521 if (cur != NULL
4522 && shl != &search_hl
4523 && syn_name2id((char_u *)"Conceal")
4524 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004525 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004526 has_match_conc =
4527 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004528 match_conc = cur->conceal_char;
4529 }
4530 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004531 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004534 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004537 next_search_hl(wp, shl, lnum, (colnr_T)v,
4538 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004539 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004540 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
4542 /* Need to get the line again, a multi-line regexp
4543 * may have made it invalid. */
4544 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4545 ptr = line + v;
4546
4547 if (shl->lnum == lnum)
4548 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004549 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004551 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004553 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004555 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 /* highlight empty match, try again after
4558 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004560 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004561 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004563 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565
4566 /* Loop to check if the match starts at the
4567 * current position */
4568 continue;
4569 }
4570 }
4571 break;
4572 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004573 if (shl != &search_hl && cur != NULL)
4574 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004576
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004577 /* Use attributes from match with highest priority among
4578 * 'search_hl' and the match list. */
4579 search_attr = search_hl.attr_cur;
4580 cur = wp->w_match_head;
4581 shl_flag = FALSE;
4582 while (cur != NULL || shl_flag == FALSE)
4583 {
4584 if (shl_flag == FALSE
4585 && ((cur != NULL
4586 && cur->priority > SEARCH_HL_PRIORITY)
4587 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004588 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004589 shl = &search_hl;
4590 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004591 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004592 else
4593 shl = &cur->hl;
4594 if (shl->attr_cur != 0)
4595 search_attr = shl->attr_cur;
4596 if (shl != &search_hl && cur != NULL)
4597 cur = cur->next;
4598 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004599 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004600 if (*ptr == NUL && (did_line_attr >= 1
4601 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004602 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004607 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004609 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4610 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004612 if (diff_hlf == HLF_TXD && ptr - line > change_end
4613 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004615 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004616 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004617 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004620
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004621#ifdef FEAT_TEXT_PROP
4622 if (text_props != NULL)
4623 {
4624 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004625 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004626
4627 // Check if any active property ends.
4628 for (pi = 0; pi < text_props_active; ++pi)
4629 {
4630 int tpi = text_prop_idxs[pi];
4631
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004632 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004633 + text_props[tpi].tp_len)
4634 {
4635 if (pi + 1 < text_props_active)
4636 mch_memmove(text_prop_idxs + pi,
4637 text_prop_idxs + pi + 1,
4638 sizeof(int)
4639 * (text_props_active - (pi + 1)));
4640 --text_props_active;
4641 --pi;
4642 }
4643 }
4644
4645 // Add any text property that starts in this column.
4646 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004647 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004648 text_prop_idxs[text_props_active++] = text_prop_next++;
4649
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004650 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004651 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004652 if (text_props_active > 0)
4653 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004654 // Sort the properties on priority and/or starting last.
4655 // Then combine the attributes, highest priority last.
4656 current_text_props = text_props;
4657 current_buf = wp->w_buffer;
4658 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4659 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004660
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004661 for (pi = 0; pi < text_props_active; ++pi)
4662 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004663 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004664 proptype_T *pt = text_prop_type_by_id(
4665 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004666
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004667 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004668 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004669 int pt_attr = syn_id2attr(pt->pt_hl_id);
4670
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004671 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004672 text_prop_attr =
4673 hl_combine_attr(text_prop_attr, pt_attr);
4674 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004675 }
4676 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004677 }
4678 }
4679#endif
4680
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004681 /* Decide which of the highlight attributes to use. */
4682 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004683#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004684 if (area_attr != 0)
4685 char_attr = hl_combine_attr(line_attr, area_attr);
4686 else if (search_attr != 0)
4687 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004688# ifdef FEAT_TEXT_PROP
4689 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004690 {
4691 char_attr = hl_combine_attr(
4692 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4693 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004694# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004695 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004696 || vcol < fromcol || vcol_prev < fromcol_prev
4697 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004698 // Use line_attr when not in the Visual or 'incsearch' area
4699 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004700 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004701#else
4702 if (area_attr != 0)
4703 char_attr = area_attr;
4704 else if (search_attr != 0)
4705 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004706#endif
4707 else
4708 {
4709 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004710#ifdef FEAT_TEXT_PROP
4711 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004712 {
4713 if (text_prop_combine)
4714 char_attr = hl_combine_attr(
4715 syntax_attr, text_prop_attr);
4716 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004717 char_attr = hl_combine_attr(
4718 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004719 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004720 else
4721#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004722#ifdef FEAT_SYN_HL
4723 if (has_syntax)
4724 char_attr = syntax_attr;
4725 else
4726#endif
4727 char_attr = 0;
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004730 if (char_attr == 0)
4731 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
4733 /*
4734 * Get the next character to put on the screen.
4735 */
4736 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004737 * The "p_extra" points to the extra stuff that is inserted to
4738 * represent special characters (non-printable stuff) and other
4739 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004740 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004741 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4742 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4744 */
4745 if (n_extra > 0)
4746 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004747 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004749 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004751 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004754 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004755 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 else
4758 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 else
4761 {
4762 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (has_mbyte)
4764 {
4765 mb_c = c;
4766 if (enc_utf8)
4767 {
4768 /* If the UTF-8 character is more than one byte:
4769 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004770 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 mb_utf8 = FALSE;
4772 if (mb_l > n_extra)
4773 mb_l = 1;
4774 else if (mb_l > 1)
4775 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004776 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004778 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 }
4781 else
4782 {
4783 /* if this is a DBCS character, put it in "mb_c" */
4784 mb_l = MB_BYTE2LEN(c);
4785 if (mb_l >= n_extra)
4786 mb_l = 1;
4787 else if (mb_l > 1)
4788 mb_c = (c << 8) + p_extra[1];
4789 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004790 if (mb_l == 0) /* at the NUL at end-of-line */
4791 mb_l = 1;
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 /* If a double-width char doesn't fit display a '>' in the
4794 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004795 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796# ifdef FEAT_RIGHTLEFT
4797 wp->w_p_rl ? (col <= 0) :
4798# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004799 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 && (*mb_char2cells)(mb_c) == 2)
4801 {
4802 c = '>';
4803 mb_c = c;
4804 mb_l = 1;
4805 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004806 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 /* put the pointer back to output the double-width
4808 * character at the start of the next line. */
4809 ++n_extra;
4810 --p_extra;
4811 }
4812 else
4813 {
4814 n_extra -= mb_l - 1;
4815 p_extra += mb_l - 1;
4816 }
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 ++p_extra;
4819 }
4820 --n_extra;
4821 }
4822 else
4823 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004824#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004825 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004826#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004827
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004828 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004829 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 /*
4831 * Get a character from the line itself.
4832 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004833 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004834#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004835 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if (has_mbyte)
4838 {
4839 mb_c = c;
4840 if (enc_utf8)
4841 {
4842 /* If the UTF-8 character is more than one byte: Decode it
4843 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004844 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 mb_utf8 = FALSE;
4846 if (mb_l > 1)
4847 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004848 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 /* Overlong encoded ASCII or ASCII with composing char
4850 * is displayed normally, except a NUL. */
4851 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004852 {
4853 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004854#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004855 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004856#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004859
4860 /* At start of the line we can have a composing char.
4861 * Draw it as a space with a composing char. */
4862 if (utf_iscomposing(mb_c))
4863 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004864 int i;
4865
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004866 for (i = Screen_mco - 1; i > 0; --i)
4867 u8cc[i] = u8cc[i - 1];
4868 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004869 mb_c = ' ';
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872
4873 if ((mb_l == 1 && c >= 0x80)
4874 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004875 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877 /*
4878 * Illegal UTF-8 byte: display as <xx>.
4879 * Non-BMP character : display as ? or fullwidth ?.
4880 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004881 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004882# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004883 if (wp->w_p_rl) /* reverse */
4884 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 p_extra = extra;
4887 c = *p_extra;
4888 mb_c = mb_ptr2char_adv(&p_extra);
4889 mb_utf8 = (c >= 0x80);
4890 n_extra = (int)STRLEN(p_extra);
4891 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004892 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (area_attr == 0 && search_attr == 0)
4894 {
4895 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004896 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 saved_attr2 = char_attr; /* save current attr */
4898 }
4899 }
4900 else if (mb_l == 0) /* at the NUL at end-of-line */
4901 mb_l = 1;
4902#ifdef FEAT_ARABIC
4903 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4904 {
4905 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004906 int pc, pc1, nc;
4907 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909 /* The idea of what is the previous and next
4910 * character depends on 'rightleft'. */
4911 if (wp->w_p_rl)
4912 {
4913 pc = prev_c;
4914 pc1 = prev_c1;
4915 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004916 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004920 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004922 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 prev_c = mb_c;
4925
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004926 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
4929 prev_c = mb_c;
4930#endif
4931 }
4932 else /* enc_dbcs */
4933 {
4934 mb_l = MB_BYTE2LEN(c);
4935 if (mb_l == 0) /* at the NUL at end-of-line */
4936 mb_l = 1;
4937 else if (mb_l > 1)
4938 {
4939 /* We assume a second byte below 32 is illegal.
4940 * Hopefully this is OK for all double-byte encodings!
4941 */
4942 if (ptr[1] >= 32)
4943 mb_c = (c << 8) + ptr[1];
4944 else
4945 {
4946 if (ptr[1] == NUL)
4947 {
4948 /* head byte at end of line */
4949 mb_l = 1;
4950 transchar_nonprint(extra, c);
4951 }
4952 else
4953 {
4954 /* illegal tail byte */
4955 mb_l = 2;
4956 STRCPY(extra, "XX");
4957 }
4958 p_extra = extra;
4959 n_extra = (int)STRLEN(extra) - 1;
4960 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004961 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 c = *p_extra++;
4963 if (area_attr == 0 && search_attr == 0)
4964 {
4965 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004966 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 saved_attr2 = char_attr; /* save current attr */
4968 }
4969 mb_c = c;
4970 }
4971 }
4972 }
4973 /* If a double-width char doesn't fit display a '>' in the
4974 * last column; the character is displayed at the start of the
4975 * next line. */
4976 if ((
4977# ifdef FEAT_RIGHTLEFT
4978 wp->w_p_rl ? (col <= 0) :
4979# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004980 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 && (*mb_char2cells)(mb_c) == 2)
4982 {
4983 c = '>';
4984 mb_c = c;
4985 mb_utf8 = FALSE;
4986 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004987 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004988 // Put pointer back so that the character will be
4989 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004991#ifdef FEAT_CONCEAL
4992 did_decrement_ptr = TRUE;
4993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 else if (*ptr != NUL)
4996 ptr += mb_l - 1;
4997
4998 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004999 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005000 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02005001 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005004 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005005 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 c = ' ';
5007 if (area_attr == 0 && search_attr == 0)
5008 {
5009 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005010 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 saved_attr2 = char_attr; /* save current attr */
5012 }
5013 mb_c = c;
5014 mb_utf8 = FALSE;
5015 mb_l = 1;
5016 }
5017
5018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 ++ptr;
5020
5021 if (extra_check)
5022 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005023#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005024 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005025#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00005026
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02005027#ifdef FEAT_TERMINAL
5028 if (get_term_attr)
5029 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02005030 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02005031
5032 if (!attr_pri)
5033 char_attr = syntax_attr;
5034 else
5035 char_attr = hl_combine_attr(syntax_attr, char_attr);
5036 }
5037#endif
5038
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005039#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005040 // Get syntax attribute, unless still at the start of the line
5041 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00005042 v = (long)(ptr - line);
5043 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 /* Get the syntax attribute for the character. If there
5046 * is an error, disable syntax highlighting. */
5047 save_did_emsg = did_emsg;
5048 did_emsg = FALSE;
5049
Bram Moolenaar217ad922005-03-20 22:37:15 +00005050 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005051# ifdef FEAT_SPELL
5052 has_spell ? &can_spell :
5053# endif
5054 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005057 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005058 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005059 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005060 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else
5063 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005064
5065 // combine syntax attribute with 'wincolor'
5066 if (win_attr != 0)
5067 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
5068
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005069#ifdef SYN_TIME_LIMIT
5070 if (wp->w_s->b_syn_slow)
5071 has_syntax = FALSE;
5072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /* Need to get the line again, a multi-line regexp may
5075 * have made it invalid. */
5076 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
5077 ptr = line + v;
5078
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005079# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02005080 // Text properties overrule syntax highlighting or combine.
5081 if (text_prop_attr == 0 || text_prop_combine)
5082# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005083 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02005084 int comb_attr = syntax_attr;
5085# ifdef FEAT_TEXT_PROP
5086 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
5087# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005088 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02005089 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005090 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02005091 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005092 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02005093# ifdef FEAT_CONCEAL
5094 /* no concealing past the end of the line, it interferes
5095 * with line highlighting */
5096 if (c == NUL)
5097 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005098 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005099 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02005100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005102#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00005103
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005104#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005105 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00005106 * Only do this when there is no syntax highlighting, the
5107 * @Spell cluster is not used or the current syntax item
5108 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00005109 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00005110 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005111 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005112 if (c != 0 && (
5113# ifdef FEAT_SYN_HL
5114 !has_syntax ||
5115# endif
5116 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00005117 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00005118 char_u *prev_ptr, *p;
5119 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005120 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00005121 if (has_mbyte)
5122 {
5123 prev_ptr = ptr - mb_l;
5124 v -= mb_l - 1;
5125 }
5126 else
Bram Moolenaare7566042005-06-17 22:00:15 +00005127 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00005128
5129 /* Use nextline[] if possible, it has the start of the
5130 * next line concatenated. */
5131 if ((prev_ptr - line) - nextlinecol >= 0)
5132 p = nextline + (prev_ptr - line) - nextlinecol;
5133 else
5134 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005135 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005136 len = spell_check(wp, p, &spell_hlf, &cap_col,
5137 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005138 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005139
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005140 /* In Insert mode only highlight a word that
5141 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005142 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005143 && (State & INSERT) != 0
5144 && wp->w_cursor.lnum == lnum
5145 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005146 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005147 && wp->w_cursor.col < (colnr_T)word_end)
5148 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005149 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005150 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005151 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005152
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005153 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005154 && (p - nextline) + len > nextline_idx)
5155 {
5156 /* Remember that the good word continues at the
5157 * start of the next line. */
5158 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005159 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005160 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005161
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005162 /* Turn index into actual attributes. */
5163 if (spell_hlf != HLF_COUNT)
5164 spell_attr = highlight_attr[spell_hlf];
5165
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005166 if (cap_col > 0)
5167 {
5168 if (p != prev_ptr
5169 && (p - nextline) + cap_col >= nextline_idx)
5170 {
5171 /* Remember that the word in the next line
5172 * must start with a capital. */
5173 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005174 cap_col = (int)((p - nextline) + cap_col
5175 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005176 }
5177 else
5178 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005179 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005180 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005181 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005182 }
5183 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005184 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005185 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005186 char_attr = hl_combine_attr(char_attr, spell_attr);
5187 else
5188 char_attr = hl_combine_attr(spell_attr, char_attr);
5189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190#endif
5191#ifdef FEAT_LINEBREAK
5192 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005193 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005195 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005196 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005198 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005199 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005200
Bram Moolenaar597a4222014-06-25 14:39:50 +02005201 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005202 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005203 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005204 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005205# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005206 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005207 wp->w_buffer->b_p_vts_array) - 1;
5208# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005209 n_extra = (int)wp->w_buffer->b_p_ts
5210 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005211# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005212
Bram Moolenaar4df70292015-03-21 14:20:16 +01005213 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005214 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005215 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005216 {
5217#ifdef FEAT_CONCEAL
5218 if (c == TAB)
5219 /* See "Tab alignment" below. */
5220 FIX_FOR_BOGUSCOLS;
5221#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005222 if (!wp->w_p_list)
5223 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226#endif
5227
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005228 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5229 // But not when the character is followed by a composing
5230 // character (use mb_l to check that).
5231 if (wp->w_p_list
5232 && ((((c == 160 && mb_l == 1)
5233 || (mb_utf8
5234 && ((mb_c == 160 && mb_l == 2)
5235 || (mb_c == 0x202f && mb_l == 3))))
5236 && lcs_nbsp)
5237 || (c == ' '
5238 && mb_l == 1
5239 && lcs_space
5240 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005241 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005242 c = (c == ' ') ? lcs_space : lcs_nbsp;
5243 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005244 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005245 n_attr = 1;
5246 extra_attr = HL_ATTR(HLF_8);
5247 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005248 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005249 mb_c = c;
5250 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005251 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005252 mb_utf8 = TRUE;
5253 u8cc[0] = 0;
5254 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005255 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005256 else
5257 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005258 }
5259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5261 {
5262 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005263 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
5265 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005266 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 saved_attr2 = char_attr; /* save current attr */
5268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005270 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 {
5272 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005273 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005274 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 else
5277 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 }
5280
5281 /*
5282 * Handling of non-printable characters.
5283 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005284 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 /*
5287 * when getting a character from the file, we may have to
5288 * turn it into something else on the way to putting it
5289 * into "ScreenLines".
5290 */
5291 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5292 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005293 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005294 long vcol_adjusted = vcol; /* removed showbreak length */
5295#ifdef FEAT_LINEBREAK
5296 /* only adjust the tab_len, when at the first column
5297 * after the showbreak value was drawn */
5298 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5299 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005302#ifdef FEAT_VARTABS
5303 tab_len = tabstop_padding(vcol_adjusted,
5304 wp->w_buffer->b_p_ts,
5305 wp->w_buffer->b_p_vts_array) - 1;
5306#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005307 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005308 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5309#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005310
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005311#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005312 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005313#endif
5314 /* tab amount depends on current column */
5315 n_extra = tab_len;
5316#ifdef FEAT_LINEBREAK
5317 else
5318 {
5319 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005320 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005321 int i;
5322 int saved_nextra = n_extra;
5323
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005324#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005325 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005326 /* there are characters to conceal */
5327 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005328 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5329 */
5330 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5331 && n_extra > tab_len)
5332 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005333#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005334
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005335 /* if n_extra > 0, it gives the number of chars, to
5336 * use for a tab, else we need to calculate the width
5337 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005338 len = (tab_len * mb_char2len(lcs_tab2));
5339 if (n_extra > 0)
5340 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005341 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005342 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005343 vim_memset(p, ' ', len);
5344 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005345 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005346 p_extra_free = p;
5347 for (i = 0; i < tab_len; i++)
5348 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005349 if (*p == NUL)
5350 {
5351 tab_len = i;
5352 break;
5353 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005354 mb_char2bytes(lcs_tab2, p);
5355 p += mb_char2len(lcs_tab2);
5356 n_extra += mb_char2len(lcs_tab2)
5357 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005358 }
5359 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005360#ifdef FEAT_CONCEAL
5361 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5362 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005363 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005364 n_extra -= vcol_off;
5365#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005366 }
5367#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005368#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005369 {
5370 int vc_saved = vcol_off;
5371
5372 /* Tab alignment should be identical regardless of
5373 * 'conceallevel' value. So tab compensates of all
5374 * previous concealed characters, and thus resets
5375 * vcol_off and boguscols accumulated so far in the
5376 * line. Note that the tab can be longer than
5377 * 'tabstop' when there are concealed characters. */
5378 FIX_FOR_BOGUSCOLS;
5379
5380 /* Make sure, the highlighting for the tab char will be
5381 * correctly set further below (effectively reverts the
5382 * FIX_FOR_BOGSUCOLS macro */
5383 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005384 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005385 tab_len += vc_saved;
5386 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 if (wp->w_p_list)
5390 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005391 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005392#ifdef FEAT_LINEBREAK
5393 if (wp->w_p_lbr)
5394 c_extra = NUL; /* using p_extra from above */
5395 else
5396#endif
5397 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005398 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005399 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005400 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005403 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
5405 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005406 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005407 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410 else
5411 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005412 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 c_extra = ' ';
5414 c = ' ';
5415 }
5416 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005417 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005418 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005419 || ((fromcol >= 0 || fromcol_prev >= 0)
5420 && tocol > vcol
5421 && VIsual_mode != Ctrl_V
5422 && (
5423# ifdef FEAT_RIGHTLEFT
5424 wp->w_p_rl ? (col >= 0) :
5425# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005426 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005427 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005428 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005429 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005430 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005432 /* Display a '$' after the line or highlight an extra
5433 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5435 /* For a diff line the highlighting continues after the
5436 * "$". */
5437 if (
5438# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005439 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440# ifdef LINE_ATTR
5441 &&
5442# endif
5443# endif
5444# ifdef LINE_ATTR
5445 line_attr == 0
5446# endif
5447 )
5448#endif
5449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 /* In virtualedit, visual selections may extend
5451 * beyond end of line. */
5452 if (area_highlighting && virtual_active()
5453 && tocol != MAXCOL && vcol < tocol)
5454 n_extra = 0;
5455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
5457 p_extra = at_end_str;
5458 n_extra = 1;
5459 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005460 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005463 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005464 c = lcs_eol;
5465 else
5466 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 lcs_eol_one = -1;
5468 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005469 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005471 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 n_attr = 1;
5473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005475 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005478 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005479 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481 else
5482 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else if (c != NUL)
5485 {
5486 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005487 if (n_extra == 0)
5488 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489#ifdef FEAT_RIGHTLEFT
5490 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5491 rl_mirror(p_extra); /* reverse "<12>" */
5492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005494 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005495#ifdef FEAT_LINEBREAK
5496 if (wp->w_p_lbr)
5497 {
5498 char_u *p;
5499
5500 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005501 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005502 vim_memset(p, ' ', n_extra);
5503 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5504 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005505 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005506 p_extra_free = p_extra = p;
5507 }
5508 else
5509#endif
5510 {
5511 n_extra = byte2cells(c) - 1;
5512 c = *p_extra++;
5513 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005514 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
5516 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005517 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 saved_attr2 = char_attr; /* save current attr */
5519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 else if (VIsual_active
5523 && (VIsual_mode == Ctrl_V
5524 || VIsual_mode == 'v')
5525 && virtual_active()
5526 && tocol != MAXCOL
5527 && vcol < tocol
5528 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005529#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005531#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005532 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 c = ' ';
5535 --ptr; /* put it back at the NUL */
5536 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005537#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 else if ((
5539# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005540 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005542# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005543 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 ) && (
5547# ifdef FEAT_RIGHTLEFT
5548 wp->w_p_rl ? (col >= 0) :
5549# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005550 (col
5551# ifdef FEAT_CONCEAL
5552 - boguscols
5553# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005554 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
5556 /* Highlight until the right side of the window */
5557 c = ' ';
5558 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005559
5560 /* Remember we do the char for line highlighting. */
5561 ++did_line_attr;
5562
5563 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005564 if (line_attr != 0 && char_attr == search_attr
5565 && (did_line_attr > 1
5566 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005567 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568# ifdef FEAT_DIFF
5569 if (diff_hlf == HLF_TXD)
5570 {
5571 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005572 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005573 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005574 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005575 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5576 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005577 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005581# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005582 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005583 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005584 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005585 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5586 char_attr = hl_combine_attr(char_attr,
5587 HL_ATTR(HLF_CUL));
5588 }
5589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
5591#endif
5592 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005593
5594#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005595 if ( wp->w_p_cole > 0
5596 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005597 conceal_cursor_line(wp))
5598 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005599 && !(lnum_in_visual_area
5600 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005601 {
5602 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005603 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005604 && (syn_get_sub_char() != NUL || match_conc
5605 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005606 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005607 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005608 /* First time at this concealed item: display one
5609 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005610 if (match_conc)
5611 c = match_conc;
5612 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005613 c = syn_get_sub_char();
5614 else if (lcs_conceal != NUL)
5615 c = lcs_conceal;
5616 else
5617 c = ' ';
5618
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005619 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005620
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005621 if (n_extra > 0)
5622 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005623 vcol += n_extra;
5624 if (wp->w_p_wrap && n_extra > 0)
5625 {
5626# ifdef FEAT_RIGHTLEFT
5627 if (wp->w_p_rl)
5628 {
5629 col -= n_extra;
5630 boguscols -= n_extra;
5631 }
5632 else
5633# endif
5634 {
5635 boguscols += n_extra;
5636 col += n_extra;
5637 }
5638 }
5639 n_extra = 0;
5640 n_attr = 0;
5641 }
5642 else if (n_skip == 0)
5643 {
5644 is_concealing = TRUE;
5645 n_skip = 1;
5646 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005647 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005648 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005649 {
5650 mb_utf8 = TRUE;
5651 u8cc[0] = 0;
5652 c = 0xc0;
5653 }
5654 else
5655 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005656 }
5657 else
5658 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005659 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005660 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005661 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005662
5663 if (n_skip > 0 && did_decrement_ptr)
5664 // not showing the '>', put pointer back to avoid getting stuck
5665 ++ptr;
5666
5667#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005670#ifdef FEAT_CONCEAL
5671 /* In the cursor line and we may be concealing characters: correct
5672 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005673 if (!did_wcol && draw_state == WL_LINE
5674 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005675 && conceal_cursor_line(wp)
5676 && (int)wp->w_virtcol <= vcol + n_skip)
5677 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005678# ifdef FEAT_RIGHTLEFT
5679 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005680 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005681 else
5682# endif
5683 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005684 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005685 did_wcol = TRUE;
5686 }
5687#endif
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 /* Don't override visual selection highlighting. */
5690 if (n_attr > 0
5691 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005692 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 char_attr = extra_attr;
5694
Bram Moolenaar81695252004-12-29 20:58:21 +00005695#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* XIM don't send preedit_start and preedit_end, but they send
5697 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5698 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005699 if (p_imst == IM_ON_THE_SPOT
5700 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005701 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 && (State & INSERT)
5703 && !p_imdisable
5704 && im_is_preediting()
5705 && draw_state == WL_LINE)
5706 {
5707 colnr_T tcol;
5708
5709 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005710 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 else
5712 tcol = preedit_end_col;
5713 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5714 {
5715 if (feedback_old_attr < 0)
5716 {
5717 feedback_col = 0;
5718 feedback_old_attr = char_attr;
5719 }
5720 char_attr = im_get_feedback_attr(feedback_col);
5721 if (char_attr < 0)
5722 char_attr = feedback_old_attr;
5723 feedback_col++;
5724 }
5725 else if (feedback_old_attr >= 0)
5726 {
5727 char_attr = feedback_old_attr;
5728 feedback_old_attr = -1;
5729 feedback_col = 0;
5730 }
5731 }
5732#endif
5733 /*
5734 * Handle the case where we are in column 0 but not on the first
5735 * character of the line and the user wants us to show us a
5736 * special character (via 'listchars' option "precedes:<char>".
5737 */
5738 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005739 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5741#ifdef FEAT_DIFF
5742 && filler_todo <= 0
5743#endif
5744 && draw_state > WL_NR
5745 && c != NUL)
5746 {
5747 c = lcs_prec;
5748 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005749 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5750 {
5751 /* Double-width character being overwritten by the "precedes"
5752 * character, need to fill up half the character. */
5753 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005754 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005755 n_extra = 1;
5756 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005757 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005760 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005763 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005764 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766 else
5767 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005768 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005771 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 n_attr3 = 1;
5773 }
5774 }
5775
5776 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005777 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005779 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005780#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005781 || did_line_attr == 1
5782#endif
5783 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005785#ifdef FEAT_SEARCH_EXTRA
5786 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005787
5788 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005789 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005790 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005791#endif
5792
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005793 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 * highlight match at end of line. If it's beyond the last
5795 * char on the screen, just overwrite that one (tricky!) Not
5796 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005797#ifdef FEAT_SEARCH_EXTRA
5798 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005799 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005800 prevcol_hl_flag = TRUE;
5801 else
5802 {
5803 cur = wp->w_match_head;
5804 while (cur != NULL)
5805 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005806 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005807 {
5808 prevcol_hl_flag = TRUE;
5809 break;
5810 }
5811 cur = cur->next;
5812 }
5813 }
5814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005816 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005817 && (VIsual_mode != Ctrl_V
5818 || lnum == VIsual.lnum
5819 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005820 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821#ifdef FEAT_SEARCH_EXTRA
5822 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005823 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005824# ifdef FEAT_SYN_HL
5825 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5826 && !(wp == curwin && VIsual_active))
5827# endif
5828# ifdef FEAT_DIFF
5829 && diff_hlf == (hlf_T)0
5830# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005831# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005832 && did_line_attr <= 1
5833# endif
5834 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#endif
5836 ))
5837 {
5838 int n = 0;
5839
5840#ifdef FEAT_RIGHTLEFT
5841 if (wp->w_p_rl)
5842 {
5843 if (col < 0)
5844 n = 1;
5845 }
5846 else
5847#endif
5848 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005849 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 n = -1;
5851 }
5852 if (n != 0)
5853 {
5854 /* At the window boundary, highlight the last character
5855 * instead (better than nothing). */
5856 off += n;
5857 col += n;
5858 }
5859 else
5860 {
5861 /* Add a blank character to highlight. */
5862 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 if (enc_utf8)
5864 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
5866#ifdef FEAT_SEARCH_EXTRA
5867 if (area_attr == 0)
5868 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005869 /* Use attributes from match with highest priority among
5870 * 'search_hl' and the match list. */
5871 char_attr = search_hl.attr;
5872 cur = wp->w_match_head;
5873 shl_flag = FALSE;
5874 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005875 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005876 if (shl_flag == FALSE
5877 && ((cur != NULL
5878 && cur->priority > SEARCH_HL_PRIORITY)
5879 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005880 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005881 shl = &search_hl;
5882 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005883 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005884 else
5885 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005886 if ((ptr - line) - 1 == (long)shl->startcol
5887 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005888 char_attr = shl->attr;
5889 if (shl != &search_hl && cur != NULL)
5890 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
5893#endif
5894 ScreenAttrs[off] = char_attr;
5895#ifdef FEAT_RIGHTLEFT
5896 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005899 --off;
5900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 else
5902#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005905 ++off;
5906 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005907 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005908#ifdef FEAT_SYN_HL
5909 eol_hl_off = 1;
5910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar91170f82006-05-05 21:15:17 +00005914 /*
5915 * At end of the text line.
5916 */
5917 if (c == NUL)
5918 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005919#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005920 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005921 if (wp->w_p_wrap)
5922 v = wp->w_skipcol;
5923 else
5924 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005925
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005926 /* check if line ends before left margin */
5927 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005928 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005929#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005930 // Get rid of the boguscols now, we want to draw until the right
5931 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005932 col -= boguscols;
5933 boguscols = 0;
5934#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005935
5936 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005937 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005938
5939 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005940 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5941 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005942 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005943 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005944 || draw_color_col
5945 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005946# ifdef FEAT_RIGHTLEFT
5947 && !wp->w_p_rl
5948# endif
5949 )
5950 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005951 int rightmost_vcol = 0;
5952 int i;
5953
5954 if (wp->w_p_cuc)
5955 rightmost_vcol = wp->w_virtcol;
5956 if (draw_color_col)
5957 /* determine rightmost colorcolumn to possibly draw */
5958 for (i = 0; color_cols[i] >= 0; ++i)
5959 if (rightmost_vcol < color_cols[i])
5960 rightmost_vcol = color_cols[i];
5961
Bram Moolenaar02631462017-09-22 15:20:32 +02005962 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005963 {
5964 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005965 if (enc_utf8)
5966 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005967 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005968 if (draw_color_col)
5969 draw_color_col = advance_color_col(VCOL_HLC,
5970 &color_cols);
5971
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005972 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005973 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005974 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005975 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005976 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005977 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005978
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005979 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005980 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005981
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005982 ++vcol;
5983 }
5984 }
5985#endif
5986
Bram Moolenaar53f81742017-09-22 14:35:51 +02005987 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005988 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 row++;
5990
5991 /*
5992 * Update w_cline_height and w_cline_folded if the cursor line was
5993 * updated (saves a call to plines() later).
5994 */
5995 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5996 {
5997 curwin->w_cline_row = startrow;
5998 curwin->w_cline_height = row - startrow;
5999#ifdef FEAT_FOLDING
6000 curwin->w_cline_folded = FALSE;
6001#endif
6002 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
6003 }
6004
6005 break;
6006 }
6007
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02006008 // Show "extends" character from 'listchars' if beyond the line end and
6009 // 'list' is set.
6010 if (lcs_ext != NUL
6011 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 && !wp->w_p_wrap
6013#ifdef FEAT_DIFF
6014 && filler_todo <= 0
6015#endif
6016 && (
6017#ifdef FEAT_RIGHTLEFT
6018 wp->w_p_rl ? col == 0 :
6019#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006020 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00006022 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
6024 {
6025 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006026 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02006028 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 {
6030 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006032 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 }
6034 else
6035 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 }
6037
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006038#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02006039 /* advance to the next 'colorcolumn' */
6040 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006041 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006042
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006043 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02006044 * highlight the cursor position itself.
6045 * Also highlight the 'colorcolumn' if it is different than
6046 * 'cursorcolumn' */
6047 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02006048 if (draw_state == WL_LINE && !lnum_in_visual_area
6049 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006050 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006051 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02006052 && lnum != wp->w_cursor.lnum)
6053 {
6054 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006055 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006056 }
Bram Moolenaard160c342010-07-18 23:30:34 +02006057 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02006058 {
6059 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006060 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006061 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006062 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006063#endif
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 /*
6066 * Store character to be displayed.
6067 * Skip characters that are left of the screen for 'nowrap'.
6068 */
6069 vcol_prev = vcol;
6070 if (draw_state < WL_LINE || n_skip <= 0)
6071 {
6072 /*
6073 * Store the character.
6074 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006075#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
6077 {
6078 /* A double-wide character is: put first halve in left cell. */
6079 --off;
6080 --col;
6081 }
6082#endif
6083 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01006085 {
6086 if ((mb_c & 0xff00) == 0x8e00)
6087 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01006089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 else if (enc_utf8)
6091 {
6092 if (mb_utf8)
6093 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006094 int i;
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006097 if ((c & 0xff) == 0)
6098 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006099 for (i = 0; i < Screen_mco; ++i)
6100 {
6101 ScreenLinesC[i][off] = u8cc[i];
6102 if (u8cc[i] == 0)
6103 break;
6104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106 else
6107 ScreenLinesUC[off] = 0;
6108 }
6109 if (multi_attr)
6110 {
6111 ScreenAttrs[off] = multi_attr;
6112 multi_attr = 0;
6113 }
6114 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 ScreenAttrs[off] = char_attr;
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6118 {
6119 /* Need to fill two screen columns. */
6120 ++off;
6121 ++col;
6122 if (enc_utf8)
6123 /* UTF-8: Put a 0 in the second screen char. */
6124 ScreenLines[off] = 0;
6125 else
6126 /* DBCS: Put second byte in the second screen char. */
6127 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01006128 if (draw_state > WL_NR
6129#ifdef FEAT_DIFF
6130 && filler_todo <= 0
6131#endif
6132 )
6133 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 /* When "tocol" is halfway a character, set it to the end of
6135 * the character, otherwise highlighting won't stop. */
6136 if (tocol == vcol)
6137 ++tocol;
6138#ifdef FEAT_RIGHTLEFT
6139 if (wp->w_p_rl)
6140 {
6141 /* now it's time to backup one cell */
6142 --off;
6143 --col;
6144 }
6145#endif
6146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147#ifdef FEAT_RIGHTLEFT
6148 if (wp->w_p_rl)
6149 {
6150 --off;
6151 --col;
6152 }
6153 else
6154#endif
6155 {
6156 ++off;
6157 ++col;
6158 }
6159 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006160#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006161 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006162 {
6163 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006164 ++vcol_off;
6165 if (n_extra > 0)
6166 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006167 if (wp->w_p_wrap)
6168 {
6169 /*
6170 * Special voodoo required if 'wrap' is on.
6171 *
6172 * Advance the column indicator to force the line
6173 * drawing to wrap early. This will make the line
6174 * take up the same screen space when parts are concealed,
6175 * so that cursor line computations aren't messed up.
6176 *
6177 * To avoid the fictitious advance of 'col' causing
6178 * trailing junk to be written out of the screen line
6179 * we are building, 'boguscols' keeps track of the number
6180 * of bad columns we have advanced.
6181 */
6182 if (n_extra > 0)
6183 {
6184 vcol += n_extra;
6185# ifdef FEAT_RIGHTLEFT
6186 if (wp->w_p_rl)
6187 {
6188 col -= n_extra;
6189 boguscols -= n_extra;
6190 }
6191 else
6192# endif
6193 {
6194 col += n_extra;
6195 boguscols += n_extra;
6196 }
6197 n_extra = 0;
6198 n_attr = 0;
6199 }
6200
6201
Bram Moolenaar860cae12010-06-05 23:22:07 +02006202 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6203 {
6204 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006205# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006206 if (wp->w_p_rl)
6207 {
6208 --boguscols;
6209 --col;
6210 }
6211 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006212# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006213 {
6214 ++boguscols;
6215 ++col;
6216 }
6217 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006218
6219# ifdef FEAT_RIGHTLEFT
6220 if (wp->w_p_rl)
6221 {
6222 --boguscols;
6223 --col;
6224 }
6225 else
6226# endif
6227 {
6228 ++boguscols;
6229 ++col;
6230 }
6231 }
6232 else
6233 {
6234 if (n_extra > 0)
6235 {
6236 vcol += n_extra;
6237 n_extra = 0;
6238 n_attr = 0;
6239 }
6240 }
6241
6242 }
6243#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 else
6245 --n_skip;
6246
Bram Moolenaar64486672010-05-16 15:46:46 +02006247 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6248 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006249 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250#ifdef FEAT_DIFF
6251 && filler_todo <= 0
6252#endif
6253 )
6254 ++vcol;
6255
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006256#ifdef FEAT_SYN_HL
6257 if (vcol_save_attr >= 0)
6258 char_attr = vcol_save_attr;
6259#endif
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 /* restore attributes after "predeces" in 'listchars' */
6262 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6263 char_attr = saved_attr3;
6264
6265 /* restore attributes after last 'listchars' or 'number' char */
6266 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6267 char_attr = saved_attr2;
6268
6269 /*
6270 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006271 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 */
6273 if ((
6274#ifdef FEAT_RIGHTLEFT
6275 wp->w_p_rl ? (col < 0) :
6276#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006277 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 && (*ptr != NUL
6279#ifdef FEAT_DIFF
6280 || filler_todo > 0
6281#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006282 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6284 )
6285 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006286#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006287 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006288 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006289 boguscols = 0;
6290#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006291 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006292 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 ++row;
6295 ++screen_row;
6296
6297 /* When not wrapping and finished diff lines, or when displayed
6298 * '$' and highlighting until last column, break here. */
6299 if ((!wp->w_p_wrap
6300#ifdef FEAT_DIFF
6301 && filler_todo <= 0
6302#endif
6303 ) || lcs_eol_one == -1)
6304 break;
6305
6306 /* When the window is too narrow draw all "@" lines. */
6307 if (draw_state != WL_LINE
6308#ifdef FEAT_DIFF
6309 && filler_todo <= 0
6310#endif
6311 )
6312 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006313 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 row = endrow;
6316 }
6317
6318 /* When line got too long for screen break here. */
6319 if (row == endrow)
6320 {
6321 ++row;
6322 break;
6323 }
6324
6325 if (screen_cur_row == screen_row - 1
6326#ifdef FEAT_DIFF
6327 && filler_todo <= 0
6328#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006329 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
6331 /* Remember that the line wraps, used for modeless copy. */
6332 LineWraps[screen_row - 1] = TRUE;
6333
6334 /*
6335 * Special trick to make copy/paste of wrapped lines work with
6336 * xterm/screen: write an extra character beyond the end of
6337 * the line. This will work with all terminal types
6338 * (regardless of the xn,am settings).
6339 * Only do this on a fast tty.
6340 * Only do this if the cursor is on the current line
6341 * (something has been written in it).
6342 * Don't do this for the GUI.
6343 * Don't do this for double-width characters.
6344 * Don't do this for a window not at the right screen border.
6345 */
6346 if (p_tf
6347#ifdef FEAT_GUI
6348 && !gui.in_use
6349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006351 && ((*mb_off2cells)(LineOffset[screen_row],
6352 LineOffset[screen_row] + screen_Columns)
6353 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006355 + (int)Columns - 2,
6356 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006357 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 {
6359 /* First make sure we are at the end of the screen line,
6360 * then output the same character again to let the
6361 * terminal know about the wrap. If the terminal doesn't
6362 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006363 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 screen_char(LineOffset[screen_row - 1]
6365 + (unsigned)Columns - 1,
6366 screen_row - 1, (int)(Columns - 1));
6367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 /* When there is a multi-byte character, just output a
6369 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006370 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6371 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 out_char(' ');
6373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 out_char(ScreenLines[LineOffset[screen_row - 1]
6375 + (Columns - 1)]);
6376 /* force a redraw of the first char on the next line */
6377 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6378 screen_start(); /* don't know where cursor is now */
6379 }
6380 }
6381
6382 col = 0;
6383 off = (unsigned)(current_ScreenLine - ScreenLines);
6384#ifdef FEAT_RIGHTLEFT
6385 if (wp->w_p_rl)
6386 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006387 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 off += col;
6389 }
6390#endif
6391
6392 /* reset the drawing state for the start of a wrapped line */
6393 draw_state = WL_START;
6394 saved_n_extra = n_extra;
6395 saved_p_extra = p_extra;
6396 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006397 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 saved_char_attr = char_attr;
6399 n_extra = 0;
6400 lcs_prec_todo = lcs_prec;
6401#ifdef FEAT_LINEBREAK
6402# ifdef FEAT_DIFF
6403 if (filler_todo <= 0)
6404# endif
6405 need_showbreak = TRUE;
6406#endif
6407#ifdef FEAT_DIFF
6408 --filler_todo;
6409 /* When the filler lines are actually below the last line of the
6410 * file, don't draw the line itself, break here. */
6411 if (filler_todo == 0 && wp->w_botfill)
6412 break;
6413#endif
6414 }
6415
6416 } /* for every character in the line */
6417
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006418#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006419 /* After an empty line check first word for capital. */
6420 if (*skipwhite(line) == NUL)
6421 {
6422 capcol_lnum = lnum + 1;
6423 cap_col = 0;
6424 }
6425#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006426#ifdef FEAT_TEXT_PROP
6427 vim_free(text_props);
6428 vim_free(text_prop_idxs);
6429#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006430
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006431 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 return row;
6433}
6434
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006435/*
6436 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006437 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006438 */
6439 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006440comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006441{
6442 int i;
6443
6444 for (i = 0; i < Screen_mco; ++i)
6445 {
6446 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6447 return TRUE;
6448 if (ScreenLinesC[i][off_from] == 0)
6449 break;
6450 }
6451 return FALSE;
6452}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454/*
6455 * Check whether the given character needs redrawing:
6456 * - the (first byte of the) character is different
6457 * - the attributes are different
6458 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006459 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 */
6461 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006462char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 if (cols > 0
6465 && ((ScreenLines[off_from] != ScreenLines[off_to]
6466 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 || (enc_dbcs != 0
6468 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6469 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6470 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6471 : (cols > 1 && ScreenLines[off_from + 1]
6472 != ScreenLines[off_to + 1])))
6473 || (enc_utf8
6474 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6475 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006476 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006477 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6478 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006479 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 return TRUE;
6481 return FALSE;
6482}
6483
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006484#if defined(FEAT_TERMINAL) || defined(PROTO)
6485/*
6486 * Return the index in ScreenLines[] for the current screen line.
6487 */
6488 int
6489screen_get_current_line_off()
6490{
6491 return (int)(current_ScreenLine - ScreenLines);
6492}
6493#endif
6494
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495/*
6496 * Move one "cooked" screen line to the screen, but only the characters that
6497 * have actually changed. Handle insert/delete character.
6498 * "coloff" gives the first column on the screen for this line.
6499 * "endcol" gives the columns where valid characters are.
6500 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6501 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006502 * "flags" can have bits:
6503 * SLF_POPUP popup window
6504 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6506 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6507 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006509screen_line(
6510 int row,
6511 int coloff,
6512 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006513 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006514 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 unsigned off_from;
6517 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006518 unsigned max_off_from;
6519 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 int force = FALSE; /* force update rest of the line */
6523 int redraw_this /* bool: does character need redraw? */
6524#ifdef FEAT_GUI
6525 = TRUE /* For GUI when while-loop empty */
6526#endif
6527 ;
6528 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 int clear_next = FALSE;
6530 int char_cells; /* 1: normal char */
6531 /* 2: occupies two display cells */
6532# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006534 /* Check for illegal row and col, just in case. */
6535 if (row >= Rows)
6536 row = Rows - 1;
6537 if (endcol > Columns)
6538 endcol = Columns;
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540# ifdef FEAT_CLIPBOARD
6541 clip_may_clear_selection(row, row);
6542# endif
6543
6544 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6545 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006546 max_off_from = off_from + screen_Columns;
6547 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006550 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
6552 /* Clear rest first, because it's left of the text. */
6553 if (clear_width > 0)
6554 {
6555 while (col <= endcol && ScreenLines[off_to] == ' '
6556 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006557 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 {
6559 ++off_to;
6560 ++col;
6561 }
6562 if (col <= endcol)
6563 screen_fill(row, row + 1, col + coloff,
6564 endcol + coloff + 1, ' ', ' ', 0);
6565 }
6566 col = endcol + 1;
6567 off_to = LineOffset[row] + col + coloff;
6568 off_from += col;
6569 endcol = (clear_width > 0 ? clear_width : -clear_width);
6570 }
6571#endif /* FEAT_RIGHTLEFT */
6572
6573 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6574
6575 while (col < endcol)
6576 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006578 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 else
6580 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 redraw_this = redraw_next;
6583 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6584 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6585
6586#ifdef FEAT_GUI
6587 /* If the next character was bold, then redraw the current character to
6588 * remove any pixels that might have spilt over into us. This only
6589 * happens in the GUI.
6590 */
6591 if (redraw_next && gui.in_use)
6592 {
6593 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006594 if (hl > HL_ALL)
6595 hl = syn_attr2attr(hl);
6596 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 redraw_this = TRUE;
6598 }
6599#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006600#ifdef FEAT_TEXT_PROP
6601 // Skip if under a(nother) popup.
6602 if (popup_mask[row * screen_Columns + col + coloff] > screen_zindex)
6603 redraw_this = FALSE;
6604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605
6606 if (redraw_this)
6607 {
6608 /*
6609 * Special handling when 'xs' termcap flag set (hpterm):
6610 * Attributes for characters are stored at the position where the
6611 * cursor is when writing the highlighting code. The
6612 * start-highlighting code must be written with the cursor on the
6613 * first highlighted character. The stop-highlighting code must
6614 * be written with the cursor just after the last highlighted
6615 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006616 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 * to clear the rest of the line, and force redrawing it
6618 * completely.
6619 */
6620 if ( p_wiv
6621 && !force
6622#ifdef FEAT_GUI
6623 && !gui.in_use
6624#endif
6625 && ScreenAttrs[off_to] != 0
6626 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6627 {
6628 /*
6629 * Need to remove highlighting attributes here.
6630 */
6631 windgoto(row, col + coloff);
6632 out_str(T_CE); /* clear rest of this screen line */
6633 screen_start(); /* don't know where cursor is now */
6634 force = TRUE; /* force redraw of rest of the line */
6635 redraw_next = TRUE; /* or else next char would miss out */
6636
6637 /*
6638 * If the previous character was highlighted, need to stop
6639 * highlighting at this character.
6640 */
6641 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6642 {
6643 screen_attr = ScreenAttrs[off_to - 1];
6644 term_windgoto(row, col + coloff);
6645 screen_stop_highlight();
6646 }
6647 else
6648 screen_attr = 0; /* highlighting has stopped */
6649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (enc_dbcs != 0)
6651 {
6652 /* Check if overwriting a double-byte with a single-byte or
6653 * the other way around requires another character to be
6654 * redrawn. For UTF-8 this isn't needed, because comparing
6655 * ScreenLinesUC[] is sufficient. */
6656 if (char_cells == 1
6657 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006658 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
6660 /* Writing a single-cell character over a double-cell
6661 * character: need to redraw the next cell. */
6662 ScreenLines[off_to + 1] = 0;
6663 redraw_next = TRUE;
6664 }
6665 else if (char_cells == 2
6666 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006667 && (*mb_off2cells)(off_to, max_off_to) == 1
6668 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
6670 /* Writing the second half of a double-cell character over
6671 * a double-cell character: need to redraw the second
6672 * cell. */
6673 ScreenLines[off_to + 2] = 0;
6674 redraw_next = TRUE;
6675 }
6676
6677 if (enc_dbcs == DBCS_JPNU)
6678 ScreenLines2[off_to] = ScreenLines2[off_from];
6679 }
6680 /* When writing a single-width character over a double-width
6681 * character and at the end of the redrawn text, need to clear out
6682 * the right halve of the old character.
6683 * Also required when writing the right halve of a double-width
6684 * char over the left halve of an existing one. */
6685 if (has_mbyte && col + char_cells == endcol
6686 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006687 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006689 && (*mb_off2cells)(off_to, max_off_to) == 1
6690 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 if (enc_utf8)
6695 {
6696 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6697 if (ScreenLinesUC[off_from] != 0)
6698 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006699 int i;
6700
6701 for (i = 0; i < Screen_mco; ++i)
6702 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 }
6704 }
6705 if (char_cells == 2)
6706 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006709 /* The bold trick makes a single column of pixels appear in the
6710 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 * character should be redrawn too. This happens for our own GUI
6712 * and for some xterms. */
6713 if (
6714# ifdef FEAT_GUI
6715 gui.in_use
6716# endif
6717# if defined(FEAT_GUI) && defined(UNIX)
6718 ||
6719# endif
6720# ifdef UNIX
6721 term_is_xterm
6722# endif
6723 )
6724 {
6725 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006726 if (hl > HL_ALL)
6727 hl = syn_attr2attr(hl);
6728 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 redraw_next = TRUE;
6730 }
6731#endif
6732 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006733
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006734 /* For simplicity set the attributes of second half of a
6735 * double-wide character equal to the first half. */
6736 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006738
6739 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 screen_char(off_to, row, col + coloff);
6743 }
6744 else if ( p_wiv
6745#ifdef FEAT_GUI
6746 && !gui.in_use
6747#endif
6748 && col + coloff > 0)
6749 {
6750 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6751 {
6752 /*
6753 * Don't output stop-highlight when moving the cursor, it will
6754 * stop the highlighting when it should continue.
6755 */
6756 screen_attr = 0;
6757 }
6758 else if (screen_attr != 0)
6759 screen_stop_highlight();
6760 }
6761
6762 off_to += CHAR_CELLS;
6763 off_from += CHAR_CELLS;
6764 col += CHAR_CELLS;
6765 }
6766
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 if (clear_next)
6768 {
6769 /* Clear the second half of a double-wide character of which the left
6770 * half was overwritten with a single-wide character. */
6771 ScreenLines[off_to] = ' ';
6772 if (enc_utf8)
6773 ScreenLinesUC[off_to] = 0;
6774 screen_char(off_to, row, col + coloff);
6775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
6777 if (clear_width > 0
6778#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006779 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780#endif
6781 )
6782 {
6783#ifdef FEAT_GUI
6784 int startCol = col;
6785#endif
6786
6787 /* blank out the rest of the line */
6788 while (col < clear_width && ScreenLines[off_to] == ' '
6789 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006790 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 ++off_to;
6793 ++col;
6794 }
6795 if (col < clear_width)
6796 {
6797#ifdef FEAT_GUI
6798 /*
6799 * In the GUI, clearing the rest of the line may leave pixels
6800 * behind if the first character cleared was bold. Some bold
6801 * fonts spill over the left. In this case we redraw the previous
6802 * character too. If we didn't skip any blanks above, then we
6803 * only redraw if the character wasn't already redrawn anyway.
6804 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006805 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807 hl = ScreenAttrs[off_to];
6808 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006809 {
6810 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006811
Bram Moolenaar9c697322006-10-09 20:11:17 +00006812 if (enc_utf8)
6813 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6814 * that its width is 2. */
6815 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6816 else if (enc_dbcs != 0)
6817 {
6818 /* find previous character by counting from first
6819 * column and get its width. */
6820 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006821 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006822
6823 while (off < off_to)
6824 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006825 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006826 off += prev_cells;
6827 }
6828 }
6829
6830 if (enc_dbcs != 0 && prev_cells > 1)
6831 screen_char_2(off_to - prev_cells, row,
6832 col + coloff - prev_cells);
6833 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006834 screen_char(off_to - prev_cells, row,
6835 col + coloff - prev_cells);
6836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838#endif
6839 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6840 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 off_to += clear_width - col;
6842 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 }
6844 }
6845
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006846 if (clear_width > 0
6847#ifdef FEAT_TEXT_PROP
6848 && !(flags & SLF_POPUP) // no separator for popup window
6849#endif
6850 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006852 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006853 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006854 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006856#ifdef FEAT_TEXT_PROP
6857 if (popup_mask[row * screen_Columns + col + coloff]
6858 <= screen_zindex)
6859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006861 int c;
6862
6863 c = fillchar_vsep(&hl);
6864 if (ScreenLines[off_to] != (schar_T)c
6865 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6866 != (c >= 0x80 ? c : 0))
6867 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006869 ScreenLines[off_to] = c;
6870 ScreenAttrs[off_to] = hl;
6871 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006873 if (c >= 0x80)
6874 {
6875 ScreenLinesUC[off_to] = c;
6876 ScreenLinesC[0][off_to] = 0;
6877 }
6878 else
6879 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006881 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
6884 }
6885 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 LineWraps[row] = FALSE;
6887 }
6888}
6889
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006890#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006892 * Mirror text "str" for right-left displaying.
6893 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006896rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 char_u *p1, *p2;
6899 int t;
6900
6901 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6902 {
6903 t = *p1;
6904 *p1 = *p2;
6905 *p2 = t;
6906 }
6907}
6908#endif
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910/*
6911 * mark all status lines for redraw; used after first :cd
6912 */
6913 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006914status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 win_T *wp;
6917
Bram Moolenaar29323592016-07-24 22:04:11 +02006918 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (wp->w_status_height)
6920 {
6921 wp->w_redr_status = TRUE;
6922 redraw_later(VALID);
6923 }
6924}
6925
6926/*
6927 * mark all status lines of the current buffer for redraw
6928 */
6929 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006930status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931{
6932 win_T *wp;
6933
Bram Moolenaar29323592016-07-24 22:04:11 +02006934 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6936 {
6937 wp->w_redr_status = TRUE;
6938 redraw_later(VALID);
6939 }
6940}
6941
6942/*
6943 * Redraw all status lines that need to be redrawn.
6944 */
6945 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006946redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 win_T *wp;
6949
Bram Moolenaar29323592016-07-24 22:04:11 +02006950 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006952 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006953 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006954 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
Bram Moolenaar4033c552017-09-16 20:54:51 +02006957#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958/*
6959 * Redraw all status lines at the bottom of frame "frp".
6960 */
6961 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006962win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 if (frp->fr_layout == FR_LEAF)
6965 frp->fr_win->w_redr_status = TRUE;
6966 else if (frp->fr_layout == FR_ROW)
6967 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006968 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 win_redraw_last_status(frp);
6970 }
6971 else /* frp->fr_layout == FR_COL */
6972 {
6973 frp = frp->fr_child;
6974 while (frp->fr_next != NULL)
6975 frp = frp->fr_next;
6976 win_redraw_last_status(frp);
6977 }
6978}
6979#endif
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981/*
6982 * Draw the verticap separator right of window "wp" starting with line "row".
6983 */
6984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006985draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 int hl;
6988 int c;
6989
6990 if (wp->w_vsep_width)
6991 {
6992 /* draw the vertical separator right of this window */
6993 c = fillchar_vsep(&hl);
6994 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6995 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6996 c, ' ', hl);
6997 }
6998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007001static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00007004 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 */
7006 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007007status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 int len = 0;
7010
7011#ifdef FEAT_MENU
7012 int emenu = (xp->xp_context == EXPAND_MENUS
7013 || xp->xp_context == EXPAND_MENUNAMES);
7014
7015 /* Check for menu separators - replace with '|'. */
7016 if (emenu && menu_is_separator(s))
7017 return 1;
7018#endif
7019
7020 while (*s != NUL)
7021 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007022 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00007023 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007024 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026
7027 return len;
7028}
7029
7030/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007031 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007032 * These are backslashes used for escaping. Do show backslashes in help tags.
7033 */
7034 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007035skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007036{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007037 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007038#ifdef FEAT_MENU
7039 || ((xp->xp_context == EXPAND_MENUS
7040 || xp->xp_context == EXPAND_MENUNAMES)
7041 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
7042#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007043 )
7044 {
7045#ifndef BACKSLASH_IN_FILENAME
7046 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
7047 return 2;
7048#endif
7049 return 1;
7050 }
7051 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007052}
7053
7054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 * Show wildchar matches in the status line.
7056 * Show at least the "match" item.
7057 * We start at item 'first_match' in the list and show all matches that fit.
7058 *
7059 * If inversion is possible we use it. Else '=' characters are used.
7060 */
7061 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007062win_redr_status_matches(
7063 expand_T *xp,
7064 int num_matches,
7065 char_u **matches, /* list of matches */
7066 int match,
7067 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068{
7069#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
7070 int row;
7071 char_u *buf;
7072 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007073 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 int fillchar;
7075 int attr;
7076 int i;
7077 int highlight = TRUE;
7078 char_u *selstart = NULL;
7079 int selstart_col = 0;
7080 char_u *selend = NULL;
7081 static int first_match = 0;
7082 int add_left = FALSE;
7083 char_u *s;
7084#ifdef FEAT_MENU
7085 int emenu;
7086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
7089 if (matches == NULL) /* interrupted completion? */
7090 return;
7091
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007092 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02007093 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007094 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02007095 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (buf == NULL)
7097 return;
7098
7099 if (match == -1) /* don't show match but original text */
7100 {
7101 match = 0;
7102 highlight = FALSE;
7103 }
7104 /* count 1 for the ending ">" */
7105 clen = status_match_len(xp, L_MATCH(match)) + 3;
7106 if (match == 0)
7107 first_match = 0;
7108 else if (match < first_match)
7109 {
7110 /* jumping left, as far as we can go */
7111 first_match = match;
7112 add_left = TRUE;
7113 }
7114 else
7115 {
7116 /* check if match fits on the screen */
7117 for (i = first_match; i < match; ++i)
7118 clen += status_match_len(xp, L_MATCH(i)) + 2;
7119 if (first_match > 0)
7120 clen += 2;
7121 /* jumping right, put match at the left */
7122 if ((long)clen > Columns)
7123 {
7124 first_match = match;
7125 /* if showing the last match, we can add some on the left */
7126 clen = 2;
7127 for (i = match; i < num_matches; ++i)
7128 {
7129 clen += status_match_len(xp, L_MATCH(i)) + 2;
7130 if ((long)clen >= Columns)
7131 break;
7132 }
7133 if (i == num_matches)
7134 add_left = TRUE;
7135 }
7136 }
7137 if (add_left)
7138 while (first_match > 0)
7139 {
7140 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7141 if ((long)clen >= Columns)
7142 break;
7143 --first_match;
7144 }
7145
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007146 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 if (first_match == 0)
7149 {
7150 *buf = NUL;
7151 len = 0;
7152 }
7153 else
7154 {
7155 STRCPY(buf, "< ");
7156 len = 2;
7157 }
7158 clen = len;
7159
7160 i = first_match;
7161 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7162 {
7163 if (i == match)
7164 {
7165 selstart = buf + len;
7166 selstart_col = clen;
7167 }
7168
7169 s = L_MATCH(i);
7170 /* Check for menu separators - replace with '|' */
7171#ifdef FEAT_MENU
7172 emenu = (xp->xp_context == EXPAND_MENUS
7173 || xp->xp_context == EXPAND_MENUNAMES);
7174 if (emenu && menu_is_separator(s))
7175 {
7176 STRCPY(buf + len, transchar('|'));
7177 l = (int)STRLEN(buf + len);
7178 len += l;
7179 clen += l;
7180 }
7181 else
7182#endif
7183 for ( ; *s != NUL; ++s)
7184 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007185 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007187 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
7189 STRNCPY(buf + len, s, l);
7190 s += l - 1;
7191 len += l;
7192 }
7193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 STRCPY(buf + len, transchar_byte(*s));
7196 len += (int)STRLEN(buf + len);
7197 }
7198 }
7199 if (i == match)
7200 selend = buf + len;
7201
7202 *(buf + len++) = ' ';
7203 *(buf + len++) = ' ';
7204 clen += 2;
7205 if (++i == num_matches)
7206 break;
7207 }
7208
7209 if (i != num_matches)
7210 {
7211 *(buf + len++) = '>';
7212 ++clen;
7213 }
7214
7215 buf[len] = NUL;
7216
7217 row = cmdline_row - 1;
7218 if (row >= 0)
7219 {
7220 if (wild_menu_showing == 0)
7221 {
7222 if (msg_scrolled > 0)
7223 {
7224 /* Put the wildmenu just above the command line. If there is
7225 * no room, scroll the screen one line up. */
7226 if (cmdline_row == Rows - 1)
7227 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007228 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 ++msg_scrolled;
7230 }
7231 else
7232 {
7233 ++cmdline_row;
7234 ++row;
7235 }
7236 wild_menu_showing = WM_SCROLLED;
7237 }
7238 else
7239 {
7240 /* Create status line if needed by setting 'laststatus' to 2.
7241 * Set 'winminheight' to zero to avoid that the window is
7242 * resized. */
7243 if (lastwin->w_status_height == 0)
7244 {
7245 save_p_ls = p_ls;
7246 save_p_wmh = p_wmh;
7247 p_ls = 2;
7248 p_wmh = 0;
7249 last_status(FALSE);
7250 }
7251 wild_menu_showing = WM_SHOWN;
7252 }
7253 }
7254
7255 screen_puts(buf, row, 0, attr);
7256 if (selstart != NULL && highlight)
7257 {
7258 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007259 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261
7262 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7263 }
7264
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 vim_free(buf);
7267}
7268#endif
7269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270/*
7271 * Redraw the status line of window wp.
7272 *
7273 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007274 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7275 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007277 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007278win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
7280 int row;
7281 char_u *p;
7282 int len;
7283 int fillchar;
7284 int attr;
7285 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007286 static int busy = FALSE;
7287
7288 /* It's possible to get here recursively when 'statusline' (indirectly)
7289 * invokes ":redrawstatus". Simply ignore the call then. */
7290 if (busy)
7291 return;
7292 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293
7294 wp->w_redr_status = FALSE;
7295 if (wp->w_status_height == 0)
7296 {
7297 /* no status line, can only be last window */
7298 redraw_cmdline = TRUE;
7299 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007300 else if (!redrawing()
7301#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007302 // don't update status line when popup menu is visible and may be
7303 // drawn over it, unless it will be redrawn later
7304 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007305#endif
7306 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 {
7308 /* Don't redraw right now, do it later. */
7309 wp->w_redr_status = TRUE;
7310 }
7311#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007312 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 {
7314 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007315 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 }
7317#endif
7318 else
7319 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007320 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007322 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 p = NameBuff;
7324 len = (int)STRLEN(p);
7325
Bram Moolenaard85f2712017-07-28 21:51:57 +02007326 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#ifdef FEAT_QUICKFIX
7328 || wp->w_p_pvw
7329#endif
7330 || bufIsChanged(wp->w_buffer)
7331 || wp->w_buffer->b_p_ro)
7332 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007333 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007335 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 len += (int)STRLEN(p + len);
7337 }
7338#ifdef FEAT_QUICKFIX
7339 if (wp->w_p_pvw)
7340 {
7341 STRCPY(p + len, _("[Preview]"));
7342 len += (int)STRLEN(p + len);
7343 }
7344#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007345 if (bufIsChanged(wp->w_buffer)
7346#ifdef FEAT_TERMINAL
7347 && !bt_terminal(wp->w_buffer)
7348#endif
7349 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 {
7351 STRCPY(p + len, "[+]");
7352 len += 3;
7353 }
7354 if (wp->w_buffer->b_p_ro)
7355 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007356 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007357 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 }
7359
Bram Moolenaar02631462017-09-22 15:20:32 +02007360 this_ru_col = ru_col - (Columns - wp->w_width);
7361 if (this_ru_col < (wp->w_width + 1) / 2)
7362 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (this_ru_col <= 1)
7364 {
7365 p = (char_u *)"<"; /* No room for file name! */
7366 len = 1;
7367 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007368 else if (has_mbyte)
7369 {
7370 int clen = 0, i;
7371
7372 /* Count total number of display cells. */
7373 clen = mb_string2cells(p, -1);
7374
7375 /* Find first character that will fit.
7376 * Going from start to end is much faster for DBCS. */
7377 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7378 i += (*mb_ptr2len)(p + i))
7379 clen -= (*mb_ptr2cells)(p + i);
7380 len = clen;
7381 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007383 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007385 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 }
7387
Bram Moolenaara12a1612019-01-24 16:39:02 +01007388 }
7389 else if (len > this_ru_col - 1)
7390 {
7391 p += len - (this_ru_col - 1);
7392 *p = '<';
7393 len = this_ru_col - 1;
7394 }
7395
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007397 screen_puts(p, row, wp->w_wincol, attr);
7398 screen_fill(row, row + 1, len + wp->w_wincol,
7399 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007401 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7403 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007404 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007407 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408#endif
7409 }
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 /*
7412 * May need to draw the character below the vertical separator.
7413 */
7414 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7415 {
7416 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007417 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 else
7419 fillchar = fillchar_vsep(&attr);
7420 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7421 attr);
7422 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007423 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
Bram Moolenaar238a5642006-02-21 22:12:05 +00007426#ifdef FEAT_STL_OPT
7427/*
7428 * Redraw the status line according to 'statusline' and take care of any
7429 * errors encountered.
7430 */
7431 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007432redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007433{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007434 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007435 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007436
7437 /* When called recursively return. This can happen when the statusline
7438 * contains an expression that triggers a redraw. */
7439 if (entered)
7440 return;
7441 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007442
Bram Moolenaara742e082016-04-05 21:10:38 +02007443 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007444 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007445 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007446 {
7447 /* When there is an error disable the statusline, otherwise the
7448 * display is messed up with errors and a redraw triggers the problem
7449 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007450 set_string_option_direct((char_u *)"statusline", -1,
7451 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007452 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007453 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007454 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007455 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007456}
7457#endif
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459/*
7460 * Return TRUE if the status line of window "wp" is connected to the status
7461 * line of the window right of it. If not, then it's a vertical separator.
7462 * Only call if (wp->w_vsep_width != 0).
7463 */
7464 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007465stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
7467 frame_T *fr;
7468
7469 fr = wp->w_frame;
7470 while (fr->fr_parent != NULL)
7471 {
7472 if (fr->fr_parent->fr_layout == FR_COL)
7473 {
7474 if (fr->fr_next != NULL)
7475 break;
7476 }
7477 else
7478 {
7479 if (fr->fr_next != NULL)
7480 return TRUE;
7481 }
7482 fr = fr->fr_parent;
7483 }
7484 return FALSE;
7485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488/*
7489 * Get the value to show for the language mappings, active 'keymap'.
7490 */
7491 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007492get_keymap_str(
7493 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007494 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007495 char_u *buf, /* buffer for the result */
7496 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 char_u *p;
7499
7500 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7501 return FALSE;
7502
7503 {
7504#ifdef FEAT_EVAL
7505 buf_T *old_curbuf = curbuf;
7506 win_T *old_curwin = curwin;
7507 char_u *s;
7508
7509 curbuf = wp->w_buffer;
7510 curwin = wp;
7511 STRCPY(buf, "b:keymap_name"); /* must be writable */
7512 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007513 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 --emsg_skip;
7515 curbuf = old_curbuf;
7516 curwin = old_curwin;
7517 if (p == NULL || *p == NUL)
7518#endif
7519 {
7520#ifdef FEAT_KEYMAP
7521 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7522 p = wp->w_buffer->b_p_keymap;
7523 else
7524#endif
7525 p = (char_u *)"lang";
7526 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007527 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 buf[0] = NUL;
7529#ifdef FEAT_EVAL
7530 vim_free(s);
7531#endif
7532 }
7533 return buf[0] != NUL;
7534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
7536#if defined(FEAT_STL_OPT) || defined(PROTO)
7537/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007538 * Redraw the status line or ruler of window "wp".
7539 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 */
7541 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007542win_redr_custom(
7543 win_T *wp,
7544 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007546 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 int attr;
7548 int curattr;
7549 int row;
7550 int col = 0;
7551 int maxwidth;
7552 int width;
7553 int n;
7554 int len;
7555 int fillchar;
7556 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007557 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007559 struct stl_hlrec hltab[STL_MAX_ITEM];
7560 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007561 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007562 win_T *ewp;
7563 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564
Bram Moolenaar1d633412013-12-11 15:52:01 +01007565 /* There is a tiny chance that this gets called recursively: When
7566 * redrawing a status line triggers redrawing the ruler or tabline.
7567 * Avoid trouble by not allowing recursion. */
7568 if (entered)
7569 return;
7570 entered = TRUE;
7571
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007573 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007575 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007576 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007577 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007578 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007579 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007580 maxwidth = Columns;
7581# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007582 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007583# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007585 else
7586 {
7587 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007588 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007589 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007590
7591 if (draw_ruler)
7592 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007593 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007594 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007595 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007596 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007597 if (*++stl == '-')
7598 stl++;
7599 if (atoi((char *)stl))
7600 while (VIM_ISDIGIT(*stl))
7601 stl++;
7602 if (*stl++ != '(')
7603 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007604 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007605 col = ru_col - (Columns - wp->w_width);
7606 if (col < (wp->w_width + 1) / 2)
7607 col = (wp->w_width + 1) / 2;
7608 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007609 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007610 {
7611 row = Rows - 1;
7612 --maxwidth; /* writing in last column may cause scrolling */
7613 fillchar = ' ';
7614 attr = 0;
7615 }
7616
7617# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007618 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007619# endif
7620 }
7621 else
7622 {
7623 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007624 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007625 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007626 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007627# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007628 use_sandbox = was_set_insecurely((char_u *)"statusline",
7629 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007630# endif
7631 }
7632
Bram Moolenaar53f81742017-09-22 14:35:51 +02007633 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007634 }
7635
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007637 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
Bram Moolenaar61452852011-02-01 18:01:11 +01007639 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7640 * the cursor away and back. */
7641 ewp = wp == NULL ? curwin : wp;
7642 p_crb_save = ewp->w_p_crb;
7643 ewp->w_p_crb = FALSE;
7644
Bram Moolenaar362f3562009-11-03 16:20:34 +00007645 /* Make a copy, because the statusline may include a function call that
7646 * might change the option value and free the memory. */
7647 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007648 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007649 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007650 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007651 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007652 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007654 /* Make all characters printable. */
7655 p = transstr(buf);
7656 if (p != NULL)
7657 {
7658 vim_strncpy(buf, p, sizeof(buf) - 1);
7659 vim_free(p);
7660 }
7661
7662 /* fill up with "fillchar" */
7663 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007664 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 ++width;
7668 }
7669 buf[len] = NUL;
7670
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007671 /*
7672 * Draw each snippet with the specified highlighting.
7673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 curattr = attr;
7675 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007676 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007678 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 screen_puts_len(p, len, row, col, curattr);
7680 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007681 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007683 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007685 else if (hltab[n].userhl < 0)
7686 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007687#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007688 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7689 && wp->w_status_height != 0)
7690 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007691 else if (wp != NULL && bt_terminal(wp->w_buffer)
7692 && wp->w_status_height != 0)
7693 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007694#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007695 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007696 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007698 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 }
7700 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007701
7702 if (wp == NULL)
7703 {
7704 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7705 col = 0;
7706 len = 0;
7707 p = buf;
7708 fillchar = 0;
7709 for (n = 0; tabtab[n].start != NULL; n++)
7710 {
7711 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7712 while (col < len)
7713 TabPageIdxs[col++] = fillchar;
7714 p = tabtab[n].start;
7715 fillchar = tabtab[n].userhl;
7716 }
7717 while (col < Columns)
7718 TabPageIdxs[col++] = fillchar;
7719 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007720
7721theend:
7722 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
7725#endif /* FEAT_STL_OPT */
7726
7727/*
7728 * Output a single character directly to the screen and update ScreenLines.
7729 */
7730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007731screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 char_u buf[MB_MAXBYTES + 1];
7734
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007735 if (has_mbyte)
7736 buf[(*mb_char2bytes)(c, buf)] = NUL;
7737 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007738 {
7739 buf[0] = c;
7740 buf[1] = NUL;
7741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 screen_puts(buf, row, col, attr);
7743}
7744
7745/*
7746 * Get a single character directly from ScreenLines into "bytes[]".
7747 * Also return its attribute in *attrp;
7748 */
7749 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007750screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 unsigned off;
7753
7754 /* safety check */
7755 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7756 {
7757 off = LineOffset[row] + col;
7758 *attrp = ScreenAttrs[off];
7759 bytes[0] = ScreenLines[off];
7760 bytes[1] = NUL;
7761
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 if (enc_utf8 && ScreenLinesUC[off] != 0)
7763 bytes[utfc_char2bytes(off, bytes)] = NUL;
7764 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7765 {
7766 bytes[0] = ScreenLines[off];
7767 bytes[1] = ScreenLines2[off];
7768 bytes[2] = NUL;
7769 }
7770 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7771 {
7772 bytes[1] = ScreenLines[off + 1];
7773 bytes[2] = NUL;
7774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776}
7777
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007778/*
7779 * Return TRUE if composing characters for screen posn "off" differs from
7780 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007781 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007782 */
7783 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007784screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007785{
7786 int i;
7787
7788 for (i = 0; i < Screen_mco; ++i)
7789 {
7790 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7791 return TRUE;
7792 if (u8cc[i] == 0)
7793 break;
7794 }
7795 return FALSE;
7796}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798/*
7799 * Put string '*text' on the screen at position 'row' and 'col', with
7800 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7801 * Note: only outputs within one row, message is truncated at screen boundary!
7802 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7803 */
7804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007805screen_puts(
7806 char_u *text,
7807 int row,
7808 int col,
7809 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 screen_puts_len(text, -1, row, col, attr);
7812}
7813
7814/*
7815 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7816 * a NUL.
7817 */
7818 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007819screen_puts_len(
7820 char_u *text,
7821 int textlen,
7822 int row,
7823 int col,
7824 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 unsigned off;
7827 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007828 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007830 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 int mbyte_blen = 1;
7832 int mbyte_cells = 1;
7833 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007834 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007836#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007838 int pc, nc, nc1;
7839 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007841 int force_redraw_this;
7842 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007843 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007845 // Safety check. The check for negative row and column is to fix issue
7846 // #4102. TODO: find out why row/col could be negative.
7847 if (ScreenLines == NULL
7848 || row >= screen_Rows || row < 0
7849 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007851 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
Bram Moolenaarc236c162008-07-13 17:41:49 +00007853 /* When drawing over the right halve of a double-wide char clear out the
7854 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007855 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007856#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007857 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007858#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007859 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007860 {
7861 ScreenLines[off - 1] = ' ';
7862 ScreenAttrs[off - 1] = 0;
7863 if (enc_utf8)
7864 {
7865 ScreenLinesUC[off - 1] = 0;
7866 ScreenLinesC[0][off - 1] = 0;
7867 }
7868 /* redraw the previous cell, make it empty */
7869 screen_char(off - 1, row, col - 1);
7870 /* force the cell at "col" to be redrawn */
7871 force_redraw_next = TRUE;
7872 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007873
Bram Moolenaar367329b2007-08-30 11:53:22 +00007874 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007875 while (col < screen_Columns
7876 && (len < 0 || (int)(ptr - text) < len)
7877 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
7879 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 /* check if this is the first byte of a multibyte */
7881 if (has_mbyte)
7882 {
7883 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007884 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007886 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7888 mbyte_cells = 1;
7889 else if (enc_dbcs != 0)
7890 mbyte_cells = mbyte_blen;
7891 else /* enc_utf8 */
7892 {
7893 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007894 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 (int)((text + len) - ptr));
7896 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007897 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007899#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7901 {
7902 /* Do Arabic shaping. */
7903 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7904 {
7905 /* Past end of string to be displayed. */
7906 nc = NUL;
7907 nc1 = NUL;
7908 }
7909 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007910 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007911 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7912 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007913 nc1 = pcc[0];
7914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 pc = prev_c;
7916 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007917 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 }
7919 else
7920 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007921#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007922 if (col + mbyte_cells > screen_Columns)
7923 {
7924 /* Only 1 cell left, but character requires 2 cells:
7925 * display a '>' in the last column to avoid wrapping. */
7926 c = '>';
7927 mbyte_cells = 1;
7928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007932 force_redraw_this = force_redraw_next;
7933 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007934
7935 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 || (mbyte_cells == 2
7937 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7938 || (enc_dbcs == DBCS_JPNU
7939 && c == 0x8e
7940 && ScreenLines2[off] != ptr[1])
7941 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007942 && (ScreenLinesUC[off] !=
7943 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7944 || (ScreenLinesUC[off] != 0
7945 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007947 || exmode_active;
7948
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007949 if ((need_redraw || force_redraw_this)
7950#ifdef FEAT_TEXT_PROP
7951 && popup_mask[row * screen_Columns + col] <= screen_zindex
7952#endif
7953 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
7955#if defined(FEAT_GUI) || defined(UNIX)
7956 /* The bold trick makes a single row of pixels appear in the next
7957 * character. When a bold character is removed, the next
7958 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007959 * and for some xterms. */
7960 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961# ifdef FEAT_GUI
7962 gui.in_use
7963# endif
7964# if defined(FEAT_GUI) && defined(UNIX)
7965 ||
7966# endif
7967# ifdef UNIX
7968 term_is_xterm
7969# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007970 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007972 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007974 if (n > HL_ALL)
7975 n = syn_attr2attr(n);
7976 if (n & HL_BOLD)
7977 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 }
7979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 /* When at the end of the text and overwriting a two-cell
7981 * character with a one-cell character, need to clear the next
7982 * cell. Also when overwriting the left halve of a two-cell char
7983 * with the right halve of a two-cell char. Do this only once
7984 * (mb_off2cells() may return 2 on the right halve). */
7985 if (clear_next_cell)
7986 clear_next_cell = FALSE;
7987 else if (has_mbyte
7988 && (len < 0 ? ptr[mbyte_blen] == NUL
7989 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007990 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007992 && (*mb_off2cells)(off, max_off) == 1
7993 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 clear_next_cell = TRUE;
7995
7996 /* Make sure we never leave a second byte of a double-byte behind,
7997 * it confuses mb_off2cells(). */
7998 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007999 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00008001 && (*mb_off2cells)(off, max_off) == 1
8002 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 ScreenLines[off] = c;
8005 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (enc_utf8)
8007 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008008 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 ScreenLinesUC[off] = 0;
8010 else
8011 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008012 int i;
8013
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008015 for (i = 0; i < Screen_mco; ++i)
8016 {
8017 ScreenLinesC[i][off] = u8cc[i];
8018 if (u8cc[i] == 0)
8019 break;
8020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022 if (mbyte_cells == 2)
8023 {
8024 ScreenLines[off + 1] = 0;
8025 ScreenAttrs[off + 1] = attr;
8026 }
8027 screen_char(off, row, col);
8028 }
8029 else if (mbyte_cells == 2)
8030 {
8031 ScreenLines[off + 1] = ptr[1];
8032 ScreenAttrs[off + 1] = attr;
8033 screen_char_2(off, row, col);
8034 }
8035 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
8036 {
8037 ScreenLines2[off] = ptr[1];
8038 screen_char(off, row, col);
8039 }
8040 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 screen_char(off, row, col);
8042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (has_mbyte)
8044 {
8045 off += mbyte_cells;
8046 col += mbyte_cells;
8047 ptr += mbyte_blen;
8048 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008049 {
8050 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008052 len = -1;
8053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 }
8055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {
8057 ++off;
8058 ++col;
8059 ++ptr;
8060 }
8061 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008062
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008063 /* If we detected the next character needs to be redrawn, but the text
8064 * doesn't extend up to there, update the character here. */
8065 if (force_redraw_next && col < screen_Columns)
8066 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008067 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
8068 screen_char_2(off, row, col);
8069 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008070 screen_char(off, row, col);
8071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072}
8073
8074#ifdef FEAT_SEARCH_EXTRA
8075/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008076 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 */
8078 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008079start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 if (p_hls && !no_hlsearch)
8082 {
8083 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008084 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008085# ifdef FEAT_RELTIME
8086 /* Set the time limit to 'redrawtime'. */
8087 profile_setlimit(p_rdt, &search_hl.tm);
8088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 }
8090}
8091
8092/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008093 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 */
8095 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008096end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097{
8098 if (search_hl.rm.regprog != NULL)
8099 {
Bram Moolenaar473de612013-06-08 18:19:48 +02008100 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 search_hl.rm.regprog = NULL;
8102 }
8103}
8104
8105/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008106 * Init for calling prepare_search_hl().
8107 */
8108 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008109init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008110{
8111 matchitem_T *cur;
8112
8113 /* Setup for match and 'hlsearch' highlighting. Disable any previous
8114 * match */
8115 cur = wp->w_match_head;
8116 while (cur != NULL)
8117 {
8118 cur->hl.rm = cur->match;
8119 if (cur->hlg_id == 0)
8120 cur->hl.attr = 0;
8121 else
8122 cur->hl.attr = syn_id2attr(cur->hlg_id);
8123 cur->hl.buf = wp->w_buffer;
8124 cur->hl.lnum = 0;
8125 cur->hl.first_lnum = 0;
8126# ifdef FEAT_RELTIME
8127 /* Set the time limit to 'redrawtime'. */
8128 profile_setlimit(p_rdt, &(cur->hl.tm));
8129# endif
8130 cur = cur->next;
8131 }
8132 search_hl.buf = wp->w_buffer;
8133 search_hl.lnum = 0;
8134 search_hl.first_lnum = 0;
8135 /* time limit is set at the toplevel, for all windows */
8136}
8137
8138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 * Advance to the match in window "wp" line "lnum" or past it.
8140 */
8141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008142prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008144 matchitem_T *cur; /* points to the match list */
8145 match_T *shl; /* points to search_hl or a match */
8146 int shl_flag; /* flag to indicate whether search_hl
8147 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008148 int pos_inprogress; /* marks that position match search is
8149 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 int n;
8151
8152 /*
8153 * When using a multi-line pattern, start searching at the top
8154 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008155 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008157 cur = wp->w_match_head;
8158 shl_flag = FALSE;
8159 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008161 if (shl_flag == FALSE)
8162 {
8163 shl = &search_hl;
8164 shl_flag = TRUE;
8165 }
8166 else
8167 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (shl->rm.regprog != NULL
8169 && shl->lnum == 0
8170 && re_multiline(shl->rm.regprog))
8171 {
8172 if (shl->first_lnum == 0)
8173 {
8174# ifdef FEAT_FOLDING
8175 for (shl->first_lnum = lnum;
8176 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8177 if (hasFoldingWin(wp, shl->first_lnum - 1,
8178 NULL, NULL, TRUE, NULL))
8179 break;
8180# else
8181 shl->first_lnum = wp->w_topline;
8182# endif
8183 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184 if (cur != NULL)
8185 cur->pos.cur = 0;
8186 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8189 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008191 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8192 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193 pos_inprogress = cur == NULL || cur->pos.cur == 0
8194 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 if (shl->lnum != 0)
8196 {
8197 shl->first_lnum = shl->lnum
8198 + shl->rm.endpos[0].lnum
8199 - shl->rm.startpos[0].lnum;
8200 n = shl->rm.endpos[0].col;
8201 }
8202 else
8203 {
8204 ++shl->first_lnum;
8205 n = 0;
8206 }
8207 }
8208 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008209 if (shl != &search_hl && cur != NULL)
8210 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 }
8212}
8213
8214/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008215 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 * Uses shl->buf.
8217 * Sets shl->lnum and shl->rm contents.
8218 * Note: Assumes a previous match is always before "lnum", unless
8219 * shl->lnum is zero.
8220 * Careful: Any pointers for buffer lines will become invalid.
8221 */
8222 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008223next_search_hl(
8224 win_T *win,
8225 match_T *shl, /* points to search_hl or a match */
8226 linenr_T lnum,
8227 colnr_T mincol, /* minimal column for a match */
8228 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 linenr_T l;
8231 colnr_T matchcol;
8232 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008233 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008235 // for :{range}s/pat only highlight inside the range
8236 if (lnum < search_first_line || lnum > search_last_line)
8237 {
8238 shl->lnum = 0;
8239 return;
8240 }
8241
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (shl->lnum != 0)
8243 {
8244 /* Check for three situations:
8245 * 1. If the "lnum" is below a previous match, start a new search.
8246 * 2. If the previous match includes "mincol", use it.
8247 * 3. Continue after the previous match.
8248 */
8249 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8250 if (lnum > l)
8251 shl->lnum = 0;
8252 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8253 return;
8254 }
8255
8256 /*
8257 * Repeat searching for a match until one is found that includes "mincol"
8258 * or none is found in this line.
8259 */
8260 called_emsg = FALSE;
8261 for (;;)
8262 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008263#ifdef FEAT_RELTIME
8264 /* Stop searching after passing the time limit. */
8265 if (profile_passed_limit(&(shl->tm)))
8266 {
8267 shl->lnum = 0; /* no match found in time */
8268 break;
8269 }
8270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 /* Three situations:
8272 * 1. No useful previous match: search from start of line.
8273 * 2. Not Vi compatible or empty match: continue at next character.
8274 * Break the loop if this is beyond the end of the line.
8275 * 3. Vi compatible searching: continue at end of previous match.
8276 */
8277 if (shl->lnum == 0)
8278 matchcol = 0;
8279 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8280 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008281 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008283 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008284
8285 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008286 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008287 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008289 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 shl->lnum = 0;
8291 break;
8292 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008293 if (has_mbyte)
8294 matchcol += mb_ptr2len(ml);
8295 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008296 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 }
8298 else
8299 matchcol = shl->rm.endpos[0].col;
8300
8301 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008302 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008304 /* Remember whether shl->rm is using a copy of the regprog in
8305 * cur->match. */
8306 int regprog_is_copy = (shl != &search_hl && cur != NULL
8307 && shl == &cur->hl
8308 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008309 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008310
Bram Moolenaarb3414592014-06-17 17:48:32 +02008311 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8312 matchcol,
8313#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008314 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008315#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008316 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008317#endif
8318 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008319 /* Copy the regprog, in case it got freed and recompiled. */
8320 if (regprog_is_copy)
8321 cur->match.regprog = cur->hl.rm.regprog;
8322
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008323 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008324 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008325 /* Error while handling regexp: stop using this regexp. */
8326 if (shl == &search_hl)
8327 {
8328 /* don't free regprog in the match list, it's a copy */
8329 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008330 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008331 }
8332 shl->rm.regprog = NULL;
8333 shl->lnum = 0;
8334 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8335 message */
8336 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008337 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008338 }
8339 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008340 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008341 else
8342 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 if (nmatched == 0)
8344 {
8345 shl->lnum = 0; /* no match found */
8346 break;
8347 }
8348 if (shl->rm.startpos[0].lnum > 0
8349 || shl->rm.startpos[0].col >= mincol
8350 || nmatched > 1
8351 || shl->rm.endpos[0].col > mincol)
8352 {
8353 shl->lnum += shl->rm.startpos[0].lnum;
8354 break; /* useful match found */
8355 }
8356 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008357
8358 // Restore called_emsg for assert_fails().
8359 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaar85077472016-10-16 14:35:48 +02008362/*
8363 * If there is a match fill "shl" and return one.
8364 * Return zero otherwise.
8365 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008366 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008367next_search_hl_pos(
8368 match_T *shl, /* points to a match */
8369 linenr_T lnum,
8370 posmatch_T *posmatch, /* match positions */
8371 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008372{
8373 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008374 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008375
Bram Moolenaarb3414592014-06-17 17:48:32 +02008376 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8377 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008378 llpos_T *pos = &posmatch->pos[i];
8379
8380 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008381 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008382 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008383 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008384 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008385 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008386 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008387 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008388 /* if this match comes before the one at "found" then swap
8389 * them */
8390 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008391 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008392 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008393
Bram Moolenaar85077472016-10-16 14:35:48 +02008394 *pos = posmatch->pos[found];
8395 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008396 }
8397 }
8398 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008399 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008400 }
8401 }
8402 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008403 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008404 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008405 colnr_T start = posmatch->pos[found].col == 0
8406 ? 0 : posmatch->pos[found].col - 1;
8407 colnr_T end = posmatch->pos[found].col == 0
8408 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008409
Bram Moolenaar85077472016-10-16 14:35:48 +02008410 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008411 shl->rm.startpos[0].lnum = 0;
8412 shl->rm.startpos[0].col = start;
8413 shl->rm.endpos[0].lnum = 0;
8414 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008415 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008416 posmatch->cur = found + 1;
8417 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008418 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008419 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008420}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008421#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008422
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008424screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 attrentry_T *aep = NULL;
8427
8428 screen_attr = attr;
8429 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008430#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 && termcap_active
8432#endif
8433 )
8434 {
8435#ifdef FEAT_GUI
8436 if (gui.in_use)
8437 {
8438 char buf[20];
8439
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008440 /* The GUI handles this internally. */
8441 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 OUT_STR(buf);
8443 }
8444 else
8445#endif
8446 {
8447 if (attr > HL_ALL) /* special HL attr. */
8448 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008449 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 aep = syn_cterm_attr2entry(attr);
8451 else
8452 aep = syn_term_attr2entry(attr);
8453 if (aep == NULL) /* did ":syntax clear" */
8454 attr = 0;
8455 else
8456 attr = aep->ae_attr;
8457 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008458 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008460 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008461#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008462 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8463 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8464 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008465#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008466 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008467 /* If the Normal FG color has BOLD attribute and the new HL
8468 * has a FG color defined, clear BOLD. */
8469 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008470 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008472 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008473 out_str(T_UCS);
8474 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008475 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8476 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008478 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008480 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008482 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008483 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
8485 /*
8486 * Output the color or start string after bold etc., in case the
8487 * bold etc. override the color setting.
8488 */
8489 if (aep != NULL)
8490 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008491#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008492 /* When 'termguicolors' is set but fg or bg is unset,
8493 * fall back to the cterm colors. This helps for SpellBad,
8494 * where the GUI uses a red undercurl. */
8495 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008497 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008498 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008499 }
8500 else
8501#endif
8502 if (t_colors > 1)
8503 {
8504 if (aep->ae_u.cterm.fg_color)
8505 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8506 }
8507#ifdef FEAT_TERMGUICOLORS
8508 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8509 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008510 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008511 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
8513 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008514#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008515 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008517 if (aep->ae_u.cterm.bg_color)
8518 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8519 }
8520
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008521 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008522 {
8523 if (aep->ae_u.term.start != NULL)
8524 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 }
8526 }
8527 }
8528 }
8529}
8530
8531 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008532screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 int do_ME = FALSE; /* output T_ME code */
8535
8536 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008537#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 && termcap_active
8539#endif
8540 )
8541 {
8542#ifdef FEAT_GUI
8543 if (gui.in_use)
8544 {
8545 char buf[20];
8546
8547 /* use internal GUI code */
8548 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8549 OUT_STR(buf);
8550 }
8551 else
8552#endif
8553 {
8554 if (screen_attr > HL_ALL) /* special HL attr. */
8555 {
8556 attrentry_T *aep;
8557
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008558 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
8560 /*
8561 * Assume that t_me restores the original colors!
8562 */
8563 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008564 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008565#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008566 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8567 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8568 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008569#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008570 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008571#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008572 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8573 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8574 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008575#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008576 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 do_ME = TRUE;
8578 }
8579 else
8580 {
8581 aep = syn_term_attr2entry(screen_attr);
8582 if (aep != NULL && aep->ae_u.term.stop != NULL)
8583 {
8584 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8585 do_ME = TRUE;
8586 else
8587 out_str(aep->ae_u.term.stop);
8588 }
8589 }
8590 if (aep == NULL) /* did ":syntax clear" */
8591 screen_attr = 0;
8592 else
8593 screen_attr = aep->ae_attr;
8594 }
8595
8596 /*
8597 * Often all ending-codes are equal to T_ME. Avoid outputting the
8598 * same sequence several times.
8599 */
8600 if (screen_attr & HL_STANDOUT)
8601 {
8602 if (STRCMP(T_SE, T_ME) == 0)
8603 do_ME = TRUE;
8604 else
8605 out_str(T_SE);
8606 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008607 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008608 {
8609 if (STRCMP(T_UCE, T_ME) == 0)
8610 do_ME = TRUE;
8611 else
8612 out_str(T_UCE);
8613 }
8614 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008615 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 if (STRCMP(T_UE, T_ME) == 0)
8618 do_ME = TRUE;
8619 else
8620 out_str(T_UE);
8621 }
8622 if (screen_attr & HL_ITALIC)
8623 {
8624 if (STRCMP(T_CZR, T_ME) == 0)
8625 do_ME = TRUE;
8626 else
8627 out_str(T_CZR);
8628 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008629 if (screen_attr & HL_STRIKETHROUGH)
8630 {
8631 if (STRCMP(T_STE, T_ME) == 0)
8632 do_ME = TRUE;
8633 else
8634 out_str(T_STE);
8635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8637 out_str(T_ME);
8638
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008639#ifdef FEAT_TERMGUICOLORS
8640 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008642 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008643 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008644 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008645 term_bg_rgb_color(cterm_normal_bg_gui_color);
8646 }
8647 else
8648#endif
8649 {
8650 if (t_colors > 1)
8651 {
8652 /* set Normal cterm colors */
8653 if (cterm_normal_fg_color != 0)
8654 term_fg_color(cterm_normal_fg_color - 1);
8655 if (cterm_normal_bg_color != 0)
8656 term_bg_color(cterm_normal_bg_color - 1);
8657 if (cterm_normal_fg_bold)
8658 out_str(T_MD);
8659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 }
8661 }
8662 }
8663 screen_attr = 0;
8664}
8665
8666/*
8667 * Reset the colors for a cterm. Used when leaving Vim.
8668 * The machine specific code may override this again.
8669 */
8670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008671reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008673 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {
8675 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008676#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008677 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8678 || cterm_normal_bg_gui_color != INVALCOLOR)
8679 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008680#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {
8684 out_str(T_OP);
8685 screen_attr = -1;
8686 }
8687 if (cterm_normal_fg_bold)
8688 {
8689 out_str(T_ME);
8690 screen_attr = -1;
8691 }
8692 }
8693}
8694
8695/*
8696 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8697 * using the attributes from ScreenAttrs["off"].
8698 */
8699 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008700screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701{
8702 int attr;
8703
8704 /* Check for illegal values, just in case (could happen just after
8705 * resizing). */
8706 if (row >= screen_Rows || col >= screen_Columns)
8707 return;
8708
Bram Moolenaarae654382019-01-17 21:09:05 +01008709#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008710 // Skip if under the popup menu.
8711 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8712 if (pum_under_menu(row, col)
8713# ifdef FEAT_TEXT_PROP
8714 && screen_zindex <= POPUPMENU_ZINDEX
8715# endif
8716 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008717 return;
8718#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008719#ifdef FEAT_TEXT_PROP
8720 // Skip if under a(nother) popup.
8721 if (popup_mask[row * screen_Columns + col] > screen_zindex)
8722 return;
8723#endif
8724
Bram Moolenaar494838a2015-02-10 19:20:37 +01008725 /* Outputting a character in the last cell on the screen may scroll the
8726 * screen up. Only do it when the "xn" termcap property is set, otherwise
8727 * mark the character invalid (update it when scrolled up). */
8728 if (*T_XN == NUL
8729 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730#ifdef FEAT_RIGHTLEFT
8731 /* account for first command-line character in rightleft mode */
8732 && !cmdmsg_rl
8733#endif
8734 )
8735 {
8736 ScreenAttrs[off] = (sattr_T)-1;
8737 return;
8738 }
8739
8740 /*
8741 * Stop highlighting first, so it's easier to move the cursor.
8742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 if (screen_char_attr != 0)
8744 attr = screen_char_attr;
8745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 attr = ScreenAttrs[off];
8747 if (screen_attr != attr)
8748 screen_stop_highlight();
8749
8750 windgoto(row, col);
8751
8752 if (screen_attr != attr)
8753 screen_start_highlight(attr);
8754
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 if (enc_utf8 && ScreenLinesUC[off] != 0)
8756 {
8757 char_u buf[MB_MAXBYTES + 1];
8758
Bram Moolenaarcb070082016-04-02 22:14:51 +02008759 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008760 {
8761 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008762#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008763 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008764#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008765 )
8766 {
8767 /* Clear the two screen cells. If the character is actually
8768 * single width it won't change the second cell. */
8769 out_str((char_u *)" ");
8770 term_windgoto(row, col);
8771 }
8772 /* not sure where the cursor is after drawing the ambiguous width
8773 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008774 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008775 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008776 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008778
8779 /* Convert the UTF-8 character to bytes and write it. */
8780 buf[utfc_char2bytes(off, buf)] = NUL;
8781 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 }
8783 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 /* double-byte character in single-width cell */
8788 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8789 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
8791
8792 screen_cur_col++;
8793}
8794
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795/*
8796 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8797 * on the screen at position 'row' and 'col'.
8798 * The attributes of the first byte is used for all. This is required to
8799 * output the two bytes of a double-byte character with nothing in between.
8800 */
8801 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008802screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 /* Check for illegal values (could be wrong when screen was resized). */
8805 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8806 return;
8807
8808 /* Outputting the last character on the screen may scrollup the screen.
8809 * Don't to it! Mark the character invalid (update it when scrolled up) */
8810 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8811 {
8812 ScreenAttrs[off] = (sattr_T)-1;
8813 return;
8814 }
8815
8816 /* Output the first byte normally (positions the cursor), then write the
8817 * second byte directly. */
8818 screen_char(off, row, col);
8819 out_char(ScreenLines[off + 1]);
8820 ++screen_cur_col;
8821}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*
8824 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8825 * This uses the contents of ScreenLines[] and doesn't change it.
8826 */
8827 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008828screen_draw_rectangle(
8829 int row,
8830 int col,
8831 int height,
8832 int width,
8833 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 int r, c;
8836 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008837 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008839 /* Can't use ScreenLines unless initialized */
8840 if (ScreenLines == NULL)
8841 return;
8842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (invert)
8844 screen_char_attr = HL_INVERSE;
8845 for (r = row; r < row + height; ++r)
8846 {
8847 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008848 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 for (c = col; c < col + width; ++c)
8850 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008851 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {
8853 screen_char_2(off + c, r, c);
8854 ++c;
8855 }
8856 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
8858 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008859 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
8862 }
8863 }
8864 screen_char_attr = 0;
8865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867/*
8868 * Redraw the characters for a vertically split window.
8869 */
8870 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008871redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 int col;
8874 int width;
8875
8876# ifdef FEAT_CLIPBOARD
8877 clip_may_clear_selection(row, end - 1);
8878# endif
8879
8880 if (wp == NULL)
8881 {
8882 col = 0;
8883 width = Columns;
8884 }
8885 else
8886 {
8887 col = wp->w_wincol;
8888 width = wp->w_width;
8889 }
8890 screen_draw_rectangle(row, col, end - row, width, FALSE);
8891}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008893 static void
8894space_to_screenline(int off, int attr)
8895{
8896 ScreenLines[off] = ' ';
8897 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008898 if (enc_utf8)
8899 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008900}
8901
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902/*
8903 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8904 * with character 'c1' in first column followed by 'c2' in the other columns.
8905 * Use attributes 'attr'.
8906 */
8907 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008908screen_fill(
8909 int start_row,
8910 int end_row,
8911 int start_col,
8912 int end_col,
8913 int c1,
8914 int c2,
8915 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916{
8917 int row;
8918 int col;
8919 int off;
8920 int end_off;
8921 int did_delete;
8922 int c;
8923 int norm_term;
8924#if defined(FEAT_GUI) || defined(UNIX)
8925 int force_next = FALSE;
8926#endif
8927
8928 if (end_row > screen_Rows) /* safety check */
8929 end_row = screen_Rows;
8930 if (end_col > screen_Columns) /* safety check */
8931 end_col = screen_Columns;
8932 if (ScreenLines == NULL
8933 || start_row >= end_row
8934 || start_col >= end_col) /* nothing to do */
8935 return;
8936
8937 /* it's a "normal" terminal when not in a GUI or cterm */
8938 norm_term = (
8939#ifdef FEAT_GUI
8940 !gui.in_use &&
8941#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008942 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 for (row = start_row; row < end_row; ++row)
8944 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008945 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008946#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008947 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008948#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008949 )
8950 {
8951 /* When drawing over the right halve of a double-wide char clear
8952 * out the left halve. When drawing over the left halve of a
8953 * double wide-char clear out the right halve. Only needed in a
8954 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008955 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008956 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008957 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008958 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 /*
8961 * Try to use delete-line termcap code, when no attributes or in a
8962 * "normal" terminal, where a bold/italic space is just a
8963 * space.
8964 */
8965 did_delete = FALSE;
8966 if (c2 == ' '
8967 && end_col == Columns
8968 && can_clear(T_CE)
8969 && (attr == 0
8970 || (norm_term
8971 && attr <= HL_ALL
8972 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8973 {
8974 /*
8975 * check if we really need to clear something
8976 */
8977 col = start_col;
8978 if (c1 != ' ') /* don't clear first char */
8979 ++col;
8980
8981 off = LineOffset[row] + col;
8982 end_off = LineOffset[row] + end_col;
8983
8984 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 if (enc_utf8)
8986 while (off < end_off && ScreenLines[off] == ' '
8987 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8988 ++off;
8989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 while (off < end_off && ScreenLines[off] == ' '
8991 && ScreenAttrs[off] == 0)
8992 ++off;
8993 if (off < end_off) /* something to be cleared */
8994 {
8995 col = off - LineOffset[row];
8996 screen_stop_highlight();
8997 term_windgoto(row, col);/* clear rest of this screen line */
8998 out_str(T_CE);
8999 screen_start(); /* don't know where cursor is now */
9000 col = end_col - col;
9001 while (col--) /* clear chars in ScreenLines */
9002 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02009003 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 ++off;
9005 }
9006 }
9007 did_delete = TRUE; /* the chars are cleared now */
9008 }
9009
9010 off = LineOffset[row] + start_col;
9011 c = c1;
9012 for (col = start_col; col < end_col; ++col)
9013 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02009014 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009015 || (enc_utf8 && (int)ScreenLinesUC[off]
9016 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 || ScreenAttrs[off] != attr
9018#if defined(FEAT_GUI) || defined(UNIX)
9019 || force_next
9020#endif
9021 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02009022#ifdef FEAT_TEXT_PROP
9023 // Skip if under a(nother) popup.
9024 && popup_mask[row * screen_Columns + col] <= screen_zindex
9025#endif
9026 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
9028#if defined(FEAT_GUI) || defined(UNIX)
9029 /* The bold trick may make a single row of pixels appear in
9030 * the next character. When a bold character is removed, the
9031 * next character should be redrawn too. This happens for our
9032 * own GUI and for some xterms. */
9033 if (
9034# ifdef FEAT_GUI
9035 gui.in_use
9036# endif
9037# if defined(FEAT_GUI) && defined(UNIX)
9038 ||
9039# endif
9040# ifdef UNIX
9041 term_is_xterm
9042# endif
9043 )
9044 {
9045 if (ScreenLines[off] != ' '
9046 && (ScreenAttrs[off] > HL_ALL
9047 || ScreenAttrs[off] & HL_BOLD))
9048 force_next = TRUE;
9049 else
9050 force_next = FALSE;
9051 }
9052#endif
9053 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 if (enc_utf8)
9055 {
9056 if (c >= 0x80)
9057 {
9058 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009059 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 else
9062 ScreenLinesUC[off] = 0;
9063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 ScreenAttrs[off] = attr;
9065 if (!did_delete || c != ' ')
9066 screen_char(off, row, col);
9067 }
9068 ++off;
9069 if (col == start_col)
9070 {
9071 if (did_delete)
9072 break;
9073 c = c2;
9074 }
9075 }
9076 if (end_col == Columns)
9077 LineWraps[row] = FALSE;
9078 if (row == Rows - 1) /* overwritten the command line */
9079 {
9080 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02009081 if (start_col == 0 && end_col == Columns
9082 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009084 if (start_col == 0)
9085 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 }
9087 }
9088}
9089
9090/*
9091 * Check if there should be a delay. Used before clearing or redrawing the
9092 * screen or the command line.
9093 */
9094 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009095check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
9098 && !did_wait_return
9099 && emsg_silent == 0)
9100 {
9101 out_flush();
9102 ui_delay(1000L, TRUE);
9103 emsg_on_display = FALSE;
9104 if (check_msg_scroll)
9105 msg_scroll = FALSE;
9106 }
9107}
9108
9109/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009110 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
9111 */
9112 static void
9113clear_TabPageIdxs(void)
9114{
9115 int scol;
9116
9117 for (scol = 0; scol < Columns; ++scol)
9118 TabPageIdxs[scol] = 0;
9119}
9120
9121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009123 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 * Returns TRUE if there is a valid screen to write to.
9125 * Returns FALSE when starting up and screen not initialized yet.
9126 */
9127 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009128screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009130 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 return (ScreenLines != NULL);
9132}
9133
9134/*
9135 * Resize the shell to Rows and Columns.
9136 * Allocate ScreenLines[] and associated items.
9137 *
9138 * There may be some time between setting Rows and Columns and (re)allocating
9139 * ScreenLines[]. This happens when starting up and when (manually) changing
9140 * the shell size. Always use screen_Rows and screen_Columns to access items
9141 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
9142 * final size of the shell is needed.
9143 */
9144 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009145screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 int new_row, old_row;
9148#ifdef FEAT_GUI
9149 int old_Rows;
9150#endif
9151 win_T *wp;
9152 int outofmem = FALSE;
9153 int len;
9154 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009156 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009158 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 sattr_T *new_ScreenAttrs;
9160 unsigned *new_LineOffset;
9161 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009162 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009163#ifdef FEAT_TEXT_PROP
9164 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009165 short *new_popup_mask_next;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009166#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00009167 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009169 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009170 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009172retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 /*
9174 * Allocation of the screen buffers is done only when the size changes and
9175 * when Rows and Columns have been set and we have started doing full
9176 * screen stuff.
9177 */
9178 if ((ScreenLines != NULL
9179 && Rows == screen_Rows
9180 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 && enc_utf8 == (ScreenLinesUC != NULL)
9182 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009183 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 || Rows == 0
9185 || Columns == 0
9186 || (!full_screen && ScreenLines == NULL))
9187 return;
9188
9189 /*
9190 * It's possible that we produce an out-of-memory message below, which
9191 * will cause this function to be called again. To break the loop, just
9192 * return here.
9193 */
9194 if (entered)
9195 return;
9196 entered = TRUE;
9197
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009198 /*
9199 * Note that the window sizes are updated before reallocating the arrays,
9200 * thus we must not redraw here!
9201 */
9202 ++RedrawingDisabled;
9203
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 win_new_shellsize(); /* fit the windows in the new sized shell */
9205
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 comp_col(); /* recompute columns for shown command and ruler */
9207
9208 /*
9209 * We're changing the size of the screen.
9210 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9211 * - Move lines from the old arrays into the new arrays, clear extra
9212 * lines (unless the screen is going to be cleared).
9213 * - Free the old arrays.
9214 *
9215 * If anything fails, make ScreenLines NULL, so we don't do anything!
9216 * Continuing with the old ScreenLines may result in a crash, because the
9217 * size is wrong.
9218 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009219 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009221 if (aucmd_win != NULL)
9222 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009223#ifdef FEAT_TEXT_PROP
9224 // global popup windows
9225 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9226 win_free_lsize(wp);
9227 // tab-local popup windows
9228 FOR_ALL_TABPAGES(tp)
9229 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9230 win_free_lsize(wp);
9231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009233 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009234 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (enc_utf8)
9236 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009237 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009238 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009239 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9240 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 }
9242 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009243 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9244 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9245 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9246 new_LineWraps = LALLOC_MULT(char_u, Rows);
9247 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009248#ifdef FEAT_TEXT_PROP
9249 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009250 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009253 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
9255 if (win_alloc_lines(wp) == FAIL)
9256 {
9257 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009258 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
9260 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009261 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9262 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009263 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009264#ifdef FEAT_TEXT_PROP
9265 // global popup windows
9266 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9267 if (win_alloc_lines(wp) == FAIL)
9268 {
9269 outofmem = TRUE;
9270 goto give_up;
9271 }
9272 // tab-local popup windows
9273 FOR_ALL_TABPAGES(tp)
9274 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9275 if (win_alloc_lines(wp) == FAIL)
9276 {
9277 outofmem = TRUE;
9278 goto give_up;
9279 }
9280#endif
9281
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009282give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009284 for (i = 0; i < p_mco; ++i)
9285 if (new_ScreenLinesC[i] == NULL)
9286 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009288 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 || new_ScreenAttrs == NULL
9291 || new_LineOffset == NULL
9292 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009293 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009294#ifdef FEAT_TEXT_PROP
9295 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009296 || new_popup_mask_next == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 || outofmem)
9299 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009300 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009301 {
9302 /* guess the size */
9303 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9304
9305 /* Remember we did this to avoid getting outofmem messages over
9306 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009307 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009308 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009309 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009310 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009311 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009312 VIM_CLEAR(new_ScreenLinesC[i]);
9313 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009314 VIM_CLEAR(new_ScreenAttrs);
9315 VIM_CLEAR(new_LineOffset);
9316 VIM_CLEAR(new_LineWraps);
9317 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009318#ifdef FEAT_TEXT_PROP
9319 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009320 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 }
9323 else
9324 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009325 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009326
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 for (new_row = 0; new_row < Rows; ++new_row)
9328 {
9329 new_LineOffset[new_row] = new_row * Columns;
9330 new_LineWraps[new_row] = FALSE;
9331
9332 /*
9333 * If the screen is not going to be cleared, copy as much as
9334 * possible from the old screen to the new one and clear the rest
9335 * (used when resizing the window at the "--more--" prompt or when
9336 * executing an external command, for the GUI).
9337 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009338 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 {
9340 (void)vim_memset(new_ScreenLines + new_row * Columns,
9341 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 if (enc_utf8)
9343 {
9344 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9345 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009346 for (i = 0; i < p_mco; ++i)
9347 (void)vim_memset(new_ScreenLinesC[i]
9348 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 0, (size_t)Columns * sizeof(u8char_T));
9350 }
9351 if (enc_dbcs == DBCS_JPNU)
9352 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9353 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9355 0, (size_t)Columns * sizeof(sattr_T));
9356 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009357 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 {
9359 if (screen_Columns < Columns)
9360 len = screen_Columns;
9361 else
9362 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009363 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009364 * may be invalid now. Also when p_mco changes. */
9365 if (!(enc_utf8 && ScreenLinesUC == NULL)
9366 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009367 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9368 ScreenLines + LineOffset[old_row],
9369 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009370 if (enc_utf8 && ScreenLinesUC != NULL
9371 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
9373 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9374 ScreenLinesUC + LineOffset[old_row],
9375 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009376 for (i = 0; i < p_mco; ++i)
9377 mch_memmove(new_ScreenLinesC[i]
9378 + new_LineOffset[new_row],
9379 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 (size_t)len * sizeof(u8char_T));
9381 }
9382 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9383 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9384 ScreenLines2 + LineOffset[old_row],
9385 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9387 ScreenAttrs + LineOffset[old_row],
9388 (size_t)len * sizeof(sattr_T));
9389 }
9390 }
9391 }
9392 /* Use the last line of the screen for the current line. */
9393 current_ScreenLine = new_ScreenLines + Rows * Columns;
9394 }
9395
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009396 free_screenlines();
9397
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009400 for (i = 0; i < p_mco; ++i)
9401 ScreenLinesC[i] = new_ScreenLinesC[i];
9402 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 ScreenAttrs = new_ScreenAttrs;
9405 LineOffset = new_LineOffset;
9406 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009407 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009408#ifdef FEAT_TEXT_PROP
9409 popup_mask = new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009410 popup_mask_next = new_popup_mask_next;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009411 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009412 popup_mask_refresh = TRUE;
9413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414
9415 /* It's important that screen_Rows and screen_Columns reflect the actual
9416 * size of ScreenLines[]. Set them before calling anything. */
9417#ifdef FEAT_GUI
9418 old_Rows = screen_Rows;
9419#endif
9420 screen_Rows = Rows;
9421 screen_Columns = Columns;
9422
9423 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009424 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426#ifdef FEAT_GUI
9427 else if (gui.in_use
9428 && !gui.starting
9429 && ScreenLines != NULL
9430 && old_Rows != Rows)
9431 {
9432 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9433 /*
9434 * Adjust the position of the cursor, for when executing an external
9435 * command.
9436 */
9437 if (msg_row >= Rows) /* Rows got smaller */
9438 msg_row = Rows - 1; /* put cursor at last row */
9439 else if (Rows > old_Rows) /* Rows got bigger */
9440 msg_row += Rows - old_Rows; /* put cursor in same place */
9441 if (msg_col >= Columns) /* Columns got smaller */
9442 msg_col = Columns - 1; /* put cursor at last column */
9443 }
9444#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009445 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009448 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009449
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009450 /*
9451 * Do not apply autocommands more than 3 times to avoid an endless loop
9452 * in case applying autocommands always changes Rows or Columns.
9453 */
9454 if (starting == 0 && ++retry_count <= 3)
9455 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009456 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009457 /* In rare cases, autocommands may have altered Rows or Columns,
9458 * jump back to check if we need to allocate the screen again. */
9459 goto retry;
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461}
9462
9463 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009464free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009465{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009466 int i;
9467
Bram Moolenaar33796b32019-06-08 16:01:13 +02009468 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009469 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009470 VIM_CLEAR(ScreenLinesC[i]);
9471 VIM_CLEAR(ScreenLines2);
9472 VIM_CLEAR(ScreenLines);
9473 VIM_CLEAR(ScreenAttrs);
9474 VIM_CLEAR(LineOffset);
9475 VIM_CLEAR(LineWraps);
9476 VIM_CLEAR(TabPageIdxs);
9477#ifdef FEAT_TEXT_PROP
9478 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009479 VIM_CLEAR(popup_mask_next);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009480#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009481}
9482
9483 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009484screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 check_for_delay(FALSE);
9487 screenalloc(FALSE); /* allocate screen buffers if size changed */
9488 screenclear2(); /* clear the screen */
9489}
9490
9491 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009492screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 int i;
9495
9496 if (starting == NO_SCREEN || ScreenLines == NULL
9497#ifdef FEAT_GUI
9498 || (gui.in_use && gui.starting)
9499#endif
9500 )
9501 return;
9502
9503#ifdef FEAT_GUI
9504 if (!gui.in_use)
9505#endif
9506 screen_attr = -1; /* force setting the Normal colors */
9507 screen_stop_highlight(); /* don't want highlighting here */
9508
9509#ifdef FEAT_CLIPBOARD
9510 /* disable selection without redrawing it */
9511 clip_scroll_selection(9999);
9512#endif
9513
9514 /* blank out ScreenLines */
9515 for (i = 0; i < Rows; ++i)
9516 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009517 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 LineWraps[i] = FALSE;
9519 }
9520
9521 if (can_clear(T_CL))
9522 {
9523 out_str(T_CL); /* clear the display */
9524 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009525 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 }
9527 else
9528 {
9529 /* can't clear the screen, mark all chars with invalid attributes */
9530 for (i = 0; i < Rows; ++i)
9531 lineinvalid(LineOffset[i], (int)Columns);
9532 clear_cmdline = TRUE;
9533 }
9534
9535 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9536
9537 win_rest_invalid(firstwin);
9538 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009539 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 if (must_redraw == CLEAR) /* no need to clear again */
9541 must_redraw = NOT_VALID;
9542 compute_cmdrow();
9543 msg_row = cmdline_row; /* put cursor on last line for messages */
9544 msg_col = 0;
9545 screen_start(); /* don't know where cursor is now */
9546 msg_scrolled = 0; /* can't scroll back */
9547 msg_didany = FALSE;
9548 msg_didout = FALSE;
9549}
9550
9551/*
9552 * Clear one line in ScreenLines.
9553 */
9554 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009555lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556{
9557 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 if (enc_utf8)
9559 (void)vim_memset(ScreenLinesUC + off, 0,
9560 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009561 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562}
9563
9564/*
9565 * Mark one line in ScreenLines invalid by setting the attributes to an
9566 * invalid value.
9567 */
9568 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009569lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9572}
9573
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574/*
9575 * Copy part of a Screenline for vertically split window "wp".
9576 */
9577 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009578linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 unsigned off_to = LineOffset[to] + wp->w_wincol;
9581 unsigned off_from = LineOffset[from] + wp->w_wincol;
9582
9583 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9584 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 if (enc_utf8)
9586 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009587 int i;
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9590 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009591 for (i = 0; i < p_mco; ++i)
9592 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9593 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 }
9595 if (enc_dbcs == DBCS_JPNU)
9596 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9597 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9599 wp->w_width * sizeof(sattr_T));
9600}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601
9602/*
9603 * Return TRUE if clearing with term string "p" would work.
9604 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009605 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 */
9607 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009608can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
9610 return (*p != NUL && (t_colors <= 1
9611#ifdef FEAT_GUI
9612 || gui.in_use
9613#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009614#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009615 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009616 || (!p_tgc && cterm_normal_bg_color == 0)
9617#else
9618 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009619#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009620 || *T_UT != NUL)
9621#ifdef FEAT_TEXT_PROP
9622 && !(p == T_CE && popup_visible)
9623#endif
9624 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625}
9626
9627/*
9628 * Reset cursor position. Use whenever cursor was moved because of outputting
9629 * something directly to the screen (shell commands) or a terminal control
9630 * code.
9631 */
9632 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009633screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 screen_cur_row = screen_cur_col = 9999;
9636}
9637
9638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 * Move the cursor to position "row","col" in the screen.
9640 * This tries to find the most efficient way to move, minimizing the number of
9641 * characters sent to the terminal.
9642 */
9643 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009644windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009646 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 int i;
9648 int plan;
9649 int cost;
9650 int wouldbe_col;
9651 int noinvcurs;
9652 char_u *bs;
9653 int goto_cost;
9654 int attr;
9655
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009656#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9658
9659#define PLAN_LE 1
9660#define PLAN_CR 2
9661#define PLAN_NL 3
9662#define PLAN_WRITE 4
9663 /* Can't use ScreenLines unless initialized */
9664 if (ScreenLines == NULL)
9665 return;
9666
9667 if (col != screen_cur_col || row != screen_cur_row)
9668 {
9669 /* Check for valid position. */
9670 if (row < 0) /* window without text lines? */
9671 row = 0;
9672 if (row >= screen_Rows)
9673 row = screen_Rows - 1;
9674 if (col >= screen_Columns)
9675 col = screen_Columns - 1;
9676
9677 /* check if no cursor movement is allowed in highlight mode */
9678 if (screen_attr && *T_MS == NUL)
9679 noinvcurs = HIGHL_COST;
9680 else
9681 noinvcurs = 0;
9682 goto_cost = GOTO_COST + noinvcurs;
9683
9684 /*
9685 * Plan how to do the positioning:
9686 * 1. Use CR to move it to column 0, same row.
9687 * 2. Use T_LE to move it a few columns to the left.
9688 * 3. Use NL to move a few lines down, column 0.
9689 * 4. Move a few columns to the right with T_ND or by writing chars.
9690 *
9691 * Don't do this if the cursor went beyond the last column, the cursor
9692 * position is unknown then (some terminals wrap, some don't )
9693 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009694 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 * characters to move the cursor to the right.
9696 */
9697 if (row >= screen_cur_row && screen_cur_col < Columns)
9698 {
9699 /*
9700 * If the cursor is in the same row, bigger col, we can use CR
9701 * or T_LE.
9702 */
9703 bs = NULL; /* init for GCC */
9704 attr = screen_attr;
9705 if (row == screen_cur_row && col < screen_cur_col)
9706 {
9707 /* "le" is preferred over "bc", because "bc" is obsolete */
9708 if (*T_LE)
9709 bs = T_LE; /* "cursor left" */
9710 else
9711 bs = T_BC; /* "backspace character (old) */
9712 if (*bs)
9713 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9714 else
9715 cost = 999;
9716 if (col + 1 < cost) /* using CR is less characters */
9717 {
9718 plan = PLAN_CR;
9719 wouldbe_col = 0;
9720 cost = 1; /* CR is just one character */
9721 }
9722 else
9723 {
9724 plan = PLAN_LE;
9725 wouldbe_col = col;
9726 }
9727 if (noinvcurs) /* will stop highlighting */
9728 {
9729 cost += noinvcurs;
9730 attr = 0;
9731 }
9732 }
9733
9734 /*
9735 * If the cursor is above where we want to be, we can use CR LF.
9736 */
9737 else if (row > screen_cur_row)
9738 {
9739 plan = PLAN_NL;
9740 wouldbe_col = 0;
9741 cost = (row - screen_cur_row) * 2; /* CR LF */
9742 if (noinvcurs) /* will stop highlighting */
9743 {
9744 cost += noinvcurs;
9745 attr = 0;
9746 }
9747 }
9748
9749 /*
9750 * If the cursor is in the same row, smaller col, just use write.
9751 */
9752 else
9753 {
9754 plan = PLAN_WRITE;
9755 wouldbe_col = screen_cur_col;
9756 cost = 0;
9757 }
9758
9759 /*
9760 * Check if any characters that need to be written have the
9761 * correct attributes. Also avoid UTF-8 characters.
9762 */
9763 i = col - wouldbe_col;
9764 if (i > 0)
9765 cost += i;
9766 if (cost < goto_cost && i > 0)
9767 {
9768 /*
9769 * Check if the attributes are correct without additionally
9770 * stopping highlighting.
9771 */
9772 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9773 while (i && *p++ == attr)
9774 --i;
9775 if (i != 0)
9776 {
9777 /*
9778 * Try if it works when highlighting is stopped here.
9779 */
9780 if (*--p == 0)
9781 {
9782 cost += noinvcurs;
9783 while (i && *p++ == 0)
9784 --i;
9785 }
9786 if (i != 0)
9787 cost = 999; /* different attributes, don't do it */
9788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 if (enc_utf8)
9790 {
9791 /* Don't use an UTF-8 char for positioning, it's slow. */
9792 for (i = wouldbe_col; i < col; ++i)
9793 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9794 {
9795 cost = 999;
9796 break;
9797 }
9798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 }
9800
9801 /*
9802 * We can do it without term_windgoto()!
9803 */
9804 if (cost < goto_cost)
9805 {
9806 if (plan == PLAN_LE)
9807 {
9808 if (noinvcurs)
9809 screen_stop_highlight();
9810 while (screen_cur_col > col)
9811 {
9812 out_str(bs);
9813 --screen_cur_col;
9814 }
9815 }
9816 else if (plan == PLAN_CR)
9817 {
9818 if (noinvcurs)
9819 screen_stop_highlight();
9820 out_char('\r');
9821 screen_cur_col = 0;
9822 }
9823 else if (plan == PLAN_NL)
9824 {
9825 if (noinvcurs)
9826 screen_stop_highlight();
9827 while (screen_cur_row < row)
9828 {
9829 out_char('\n');
9830 ++screen_cur_row;
9831 }
9832 screen_cur_col = 0;
9833 }
9834
9835 i = col - screen_cur_col;
9836 if (i > 0)
9837 {
9838 /*
9839 * Use cursor-right if it's one character only. Avoids
9840 * removing a line of pixels from the last bold char, when
9841 * using the bold trick in the GUI.
9842 */
9843 if (T_ND[0] != NUL && T_ND[1] == NUL)
9844 {
9845 while (i-- > 0)
9846 out_char(*T_ND);
9847 }
9848 else
9849 {
9850 int off;
9851
9852 off = LineOffset[row] + screen_cur_col;
9853 while (i-- > 0)
9854 {
9855 if (ScreenAttrs[off] != screen_attr)
9856 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 if (enc_dbcs == DBCS_JPNU
9860 && ScreenLines[off] == 0x8e)
9861 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 ++off;
9863 }
9864 }
9865 }
9866 }
9867 }
9868 else
9869 cost = 999;
9870
9871 if (cost >= goto_cost)
9872 {
9873 if (noinvcurs)
9874 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009875 if (row == screen_cur_row && (col > screen_cur_col)
9876 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 term_cursor_right(col - screen_cur_col);
9878 else
9879 term_windgoto(row, col);
9880 }
9881 screen_cur_row = row;
9882 screen_cur_col = col;
9883 }
9884}
9885
9886/*
9887 * Set cursor to its position in the current window.
9888 */
9889 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009890setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009892 setcursor_mayforce(FALSE);
9893}
9894
9895/*
9896 * Set cursor to its position in the current window.
9897 * When "force" is TRUE also when not redrawing.
9898 */
9899 void
9900setcursor_mayforce(int force)
9901{
9902 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 {
9904 validate_cursor();
9905 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009906 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009908 /* With 'rightleft' set and the cursor on a double-wide
9909 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009910 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9911 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009912 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009913 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914#endif
9915 curwin->w_wcol));
9916 }
9917}
9918
9919
9920/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009921 * Insert 'line_count' lines at 'row' in window 'wp'.
9922 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9923 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 * scrolling.
9925 * Returns FAIL if the lines are not inserted, OK for success.
9926 */
9927 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009928win_ins_lines(
9929 win_T *wp,
9930 int row,
9931 int line_count,
9932 int invalid,
9933 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934{
9935 int did_delete;
9936 int nextrow;
9937 int lastrow;
9938 int retval;
9939
9940 if (invalid)
9941 wp->w_lines_valid = 0;
9942
9943 if (wp->w_height < 5)
9944 return FAIL;
9945
9946 if (line_count > wp->w_height - row)
9947 line_count = wp->w_height - row;
9948
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009949 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (retval != MAYBE)
9951 return retval;
9952
9953 /*
9954 * If there is a next window or a status line, we first try to delete the
9955 * lines at the bottom to avoid messing what is after the window.
9956 * If this fails and there are following windows, don't do anything to avoid
9957 * messing up those windows, better just redraw.
9958 */
9959 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 if (wp->w_next != NULL || wp->w_status_height)
9961 {
9962 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009963 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 did_delete = TRUE;
9965 else if (wp->w_next)
9966 return FAIL;
9967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 /*
9969 * if no lines deleted, blank the lines that will end up below the window
9970 */
9971 if (!did_delete)
9972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009975 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 lastrow = nextrow + line_count;
9977 if (lastrow > Rows)
9978 lastrow = Rows;
9979 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009980 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 ' ', ' ', 0);
9982 }
9983
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009984 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 == FAIL)
9986 {
9987 /* deletion will have messed up other windows */
9988 if (did_delete)
9989 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 win_rest_invalid(W_NEXT(wp));
9992 }
9993 return FAIL;
9994 }
9995
9996 return OK;
9997}
9998
9999/*
Bram Moolenaar86033562017-07-12 20:24:41 +020010000 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
10002 * If "mayclear" is TRUE the screen will be cleared if it is faster than
10003 * scrolling
10004 * Return OK for success, FAIL if the lines are not deleted.
10005 */
10006 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010007win_del_lines(
10008 win_T *wp,
10009 int row,
10010 int line_count,
10011 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010012 int mayclear,
10013 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
10015 int retval;
10016
10017 if (invalid)
10018 wp->w_lines_valid = 0;
10019
10020 if (line_count > wp->w_height - row)
10021 line_count = wp->w_height - row;
10022
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010023 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (retval != MAYBE)
10025 return retval;
10026
10027 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010028 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 return FAIL;
10030
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 /*
10032 * If there are windows or status lines below, try to put them at the
10033 * correct place. If we can't do that, they have to be redrawn.
10034 */
10035 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
10036 {
10037 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010038 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 {
10040 wp->w_redr_status = TRUE;
10041 win_rest_invalid(wp->w_next);
10042 }
10043 }
10044 /*
10045 * If this is the last window and there is no status line, redraw the
10046 * command line later.
10047 */
10048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 redraw_cmdline = TRUE;
10050 return OK;
10051}
10052
10053/*
10054 * Common code for win_ins_lines() and win_del_lines().
10055 * Returns OK or FAIL when the work has been done.
10056 * Returns MAYBE when not finished yet.
10057 */
10058 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010059win_do_lines(
10060 win_T *wp,
10061 int row,
10062 int line_count,
10063 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010064 int del,
10065 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
10067 int retval;
10068
10069 if (!redrawing() || line_count <= 0)
10070 return FAIL;
10071
Bram Moolenaar33796b32019-06-08 16:01:13 +020010072 // When inserting lines would result in loss of command output, just redraw
10073 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010074 if (no_win_do_lines_ins && !del)
10075 return FAIL;
10076
Bram Moolenaar33796b32019-06-08 16:01:13 +020010077 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +020010078 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010080 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +020010081 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 return FAIL;
10083 }
10084
Bram Moolenaar33796b32019-06-08 16:01:13 +020010085#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +020010086 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +020010087 if (popup_visible)
10088 return FAIL;
10089#endif
10090
10091 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 if (row + line_count >= wp->w_height)
10093 {
10094 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +020010095 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 ' ', ' ', 0);
10097 return OK;
10098 }
10099
10100 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010101 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010103 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010105 if (!no_win_do_lines_ins)
10106 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107
10108 /*
10109 * If the terminal can set a scroll region, use that.
10110 * Always do this in a vertically split window. This will redraw from
10111 * ScreenLines[] when t_CV isn't defined. That's faster than using
10112 * win_line().
10113 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010114 * a character in the lower right corner of the scroll region may cause a
10115 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 */
Bram Moolenaar02631462017-09-22 15:20:32 +020010117 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 scroll_region_set(wp, row);
10121 if (del)
10122 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010123 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 else
10125 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010126 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 scroll_region_reset();
10129 return retval;
10130 }
10131
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
10133 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134
10135 return MAYBE;
10136}
10137
10138/*
10139 * window 'wp' and everything after it is messed up, mark it for redraw
10140 */
10141 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010142win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
10146 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 wp->w_redr_status = TRUE;
10148 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 }
10150 redraw_cmdline = TRUE;
10151}
10152
10153/*
10154 * The rest of the routines in this file perform screen manipulations. The
10155 * given operation is performed physically on the screen. The corresponding
10156 * change is also made to the internal screen image. In this way, the editor
10157 * anticipates the effect of editing changes on the appearance of the screen.
10158 * That way, when we call screenupdate a complete redraw isn't usually
10159 * necessary. Another advantage is that we can keep adding code to anticipate
10160 * screen changes, and in the meantime, everything still works.
10161 */
10162
10163/*
10164 * types for inserting or deleting lines
10165 */
10166#define USE_T_CAL 1
10167#define USE_T_CDL 2
10168#define USE_T_AL 3
10169#define USE_T_CE 4
10170#define USE_T_DL 5
10171#define USE_T_SR 6
10172#define USE_NL 7
10173#define USE_T_CD 8
10174#define USE_REDRAW 9
10175
10176/*
10177 * insert lines on the screen and update ScreenLines[]
10178 * 'end' is the line after the scrolled part. Normally it is Rows.
10179 * When scrolling region used 'off' is the offset from the top for the region.
10180 * 'row' and 'end' are relative to the start of the region.
10181 *
10182 * return FAIL for failure, OK for success.
10183 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000010184 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010185screen_ins_lines(
10186 int off,
10187 int row,
10188 int line_count,
10189 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010190 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +010010191 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192{
10193 int i;
10194 int j;
10195 unsigned temp;
10196 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010197 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 int type;
10199 int result_empty;
10200 int can_ce = can_clear(T_CE);
10201
10202 /*
10203 * FAIL if
10204 * - there is no valid screen
10205 * - the screen has to be redrawn completely
10206 * - the line count is less than one
10207 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010208 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +020010209 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 */
Bram Moolenaar33796b32019-06-08 16:01:13 +020010211 if (!screen_valid(TRUE)
10212 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010213#ifdef FEAT_CLIPBOARD
10214 || (clip_star.state != SELECT_CLEARED
10215 && redrawing_for_callback > 0)
10216#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +020010217#ifdef FEAT_TEXT_PROP
10218 || popup_visible
10219#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010220 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 return FAIL;
10222
10223 /*
10224 * There are seven ways to insert lines:
10225 * 0. When in a vertically split window and t_CV isn't set, redraw the
10226 * characters from ScreenLines[].
10227 * 1. Use T_CD (clear to end of display) if it exists and the result of
10228 * the insert is just empty lines
10229 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10230 * present or line_count > 1. It looks better if we do all the inserts
10231 * at once.
10232 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10233 * insert is just empty lines and T_CE is not present or line_count >
10234 * 1.
10235 * 4. Use T_AL (insert line) if it exists.
10236 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10237 * just empty lines.
10238 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10239 * just empty lines.
10240 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10241 * the 'da' flag is not set or we have clear line capability.
10242 * 8. redraw the characters from ScreenLines[].
10243 *
10244 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10245 * the scrollbar for the window. It does have insert line, use that if it
10246 * exists.
10247 */
10248 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10250 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010251 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 type = USE_T_CD;
10253 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10254 type = USE_T_CAL;
10255 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10256 type = USE_T_CDL;
10257 else if (*T_AL != NUL)
10258 type = USE_T_AL;
10259 else if (can_ce && result_empty)
10260 type = USE_T_CE;
10261 else if (*T_DL != NUL && result_empty)
10262 type = USE_T_DL;
10263 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10264 type = USE_T_SR;
10265 else
10266 return FAIL;
10267
10268 /*
10269 * For clearing the lines screen_del_lines() is used. This will also take
10270 * care of t_db if necessary.
10271 */
10272 if (type == USE_T_CD || type == USE_T_CDL ||
10273 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010274 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275
10276 /*
10277 * If text is retained below the screen, first clear or delete as many
10278 * lines at the bottom of the window as are about to be inserted so that
10279 * the deleted lines won't later surface during a screen_del_lines.
10280 */
10281 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010282 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283
10284#ifdef FEAT_CLIPBOARD
10285 /* Remove a modeless selection when inserting lines halfway the screen
10286 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010287 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010288 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 else
10290 clip_scroll_selection(-line_count);
10291#endif
10292
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293#ifdef FEAT_GUI
10294 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10295 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010296 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#endif
10298
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010299 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10300 cursor_col = wp->w_wincol;
10301
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 if (*T_CCS != NUL) /* cursor relative to region */
10303 cursor_row = row;
10304 else
10305 cursor_row = row + off;
10306
10307 /*
10308 * Shift LineOffset[] line_count down to reflect the inserted lines.
10309 * Clear the inserted lines in ScreenLines[].
10310 */
10311 row += off;
10312 end += off;
10313 for (i = 0; i < line_count; ++i)
10314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 if (wp != NULL && wp->w_width != Columns)
10316 {
10317 /* need to copy part of a line */
10318 j = end - 1 - i;
10319 while ((j -= line_count) >= row)
10320 linecopy(j + line_count, j, wp);
10321 j += line_count;
10322 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010323 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10324 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 else
10326 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10327 LineWraps[j] = FALSE;
10328 }
10329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 {
10331 j = end - 1 - i;
10332 temp = LineOffset[j];
10333 while ((j -= line_count) >= row)
10334 {
10335 LineOffset[j + line_count] = LineOffset[j];
10336 LineWraps[j + line_count] = LineWraps[j];
10337 }
10338 LineOffset[j + line_count] = temp;
10339 LineWraps[j + line_count] = FALSE;
10340 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010341 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 else
10343 lineinvalid(temp, (int)Columns);
10344 }
10345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346
10347 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010348 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010349 if (clear_attr != 0)
10350 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 /* redraw the characters */
10353 if (type == USE_REDRAW)
10354 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010355 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 {
10357 term_append_lines(line_count);
10358 screen_start(); /* don't know where cursor is now */
10359 }
10360 else
10361 {
10362 for (i = 0; i < line_count; i++)
10363 {
10364 if (type == USE_T_AL)
10365 {
10366 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010367 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 out_str(T_AL);
10369 }
10370 else /* type == USE_T_SR */
10371 out_str(T_SR);
10372 screen_start(); /* don't know where cursor is now */
10373 }
10374 }
10375
10376 /*
10377 * With scroll-reverse and 'da' flag set we need to clear the lines that
10378 * have been scrolled down into the region.
10379 */
10380 if (type == USE_T_SR && *T_DA)
10381 {
10382 for (i = 0; i < line_count; ++i)
10383 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010384 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 out_str(T_CE);
10386 screen_start(); /* don't know where cursor is now */
10387 }
10388 }
10389
10390#ifdef FEAT_GUI
10391 gui_can_update_cursor();
10392 if (gui.in_use)
10393 out_flush(); /* always flush after a scroll */
10394#endif
10395 return OK;
10396}
10397
10398/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010399 * Delete lines on the screen and update ScreenLines[].
10400 * "end" is the line after the scrolled part. Normally it is Rows.
10401 * When scrolling region used "off" is the offset from the top for the region.
10402 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 *
10404 * Return OK for success, FAIL if the lines are not deleted.
10405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010407screen_del_lines(
10408 int off,
10409 int row,
10410 int line_count,
10411 int end,
10412 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010413 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010414 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415{
10416 int j;
10417 int i;
10418 unsigned temp;
10419 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010420 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 int cursor_end;
10422 int result_empty; /* result is empty until end of region */
10423 int can_delete; /* deleting line codes can be used */
10424 int type;
10425
10426 /*
10427 * FAIL if
10428 * - there is no valid screen
10429 * - the screen has to be redrawn completely
10430 * - the line count is less than one
10431 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010432 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010434 if (!screen_valid(TRUE) || line_count <= 0
10435 || (!force && line_count > p_ttyscroll)
10436#ifdef FEAT_CLIPBOARD
10437 || (clip_star.state != SELECT_CLEARED
10438 && redrawing_for_callback > 0)
10439#endif
10440 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 return FAIL;
10442
10443 /*
10444 * Check if the rest of the current region will become empty.
10445 */
10446 result_empty = row + line_count >= end;
10447
10448 /*
10449 * We can delete lines only when 'db' flag not set or when 'ce' option
10450 * available.
10451 */
10452 can_delete = (*T_DB == NUL || can_clear(T_CE));
10453
10454 /*
10455 * There are six ways to delete lines:
10456 * 0. When in a vertically split window and t_CV isn't set, redraw the
10457 * characters from ScreenLines[].
10458 * 1. Use T_CD if it exists and the result is empty.
10459 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10460 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10461 * none of the other ways work.
10462 * 4. Use T_CE (erase line) if the result is empty.
10463 * 5. Use T_DL (delete line) if it exists.
10464 * 6. redraw the characters from ScreenLines[].
10465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10467 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010468 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 type = USE_T_CD;
10470#if defined(__BEOS__) && defined(BEOS_DR8)
10471 /*
10472 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10473 * its internal termcap... this works okay for tests which test *T_DB !=
10474 * NUL. It has the disadvantage that the user cannot use any :set t_*
10475 * command to get T_DB (back) to empty_option, only :set term=... will do
10476 * the trick...
10477 * Anyway, this hack will hopefully go away with the next OS release.
10478 * (Olaf Seibert)
10479 */
10480 else if (row == 0 && T_DB == empty_option
10481 && (line_count == 1 || *T_CDL == NUL))
10482#else
10483 else if (row == 0 && (
10484#ifndef AMIGA
10485 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10486 * up, so use delete-line command */
10487 line_count == 1 ||
10488#endif
10489 *T_CDL == NUL))
10490#endif
10491 type = USE_NL;
10492 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10493 type = USE_T_CDL;
10494 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010495 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 type = USE_T_CE;
10497 else if (*T_DL != NUL && can_delete)
10498 type = USE_T_DL;
10499 else if (*T_CDL != NUL && can_delete)
10500 type = USE_T_CDL;
10501 else
10502 return FAIL;
10503
10504#ifdef FEAT_CLIPBOARD
10505 /* Remove a modeless selection when deleting lines halfway the screen or
10506 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010507 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010508 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 else
10510 clip_scroll_selection(line_count);
10511#endif
10512
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513#ifdef FEAT_GUI
10514 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10515 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010516 gui_dont_update_cursor(gui.cursor_row >= row + off
10517 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518#endif
10519
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010520 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10521 cursor_col = wp->w_wincol;
10522
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 if (*T_CCS != NUL) /* cursor relative to region */
10524 {
10525 cursor_row = row;
10526 cursor_end = end;
10527 }
10528 else
10529 {
10530 cursor_row = row + off;
10531 cursor_end = end + off;
10532 }
10533
10534 /*
10535 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10536 * Clear the inserted lines in ScreenLines[].
10537 */
10538 row += off;
10539 end += off;
10540 for (i = 0; i < line_count; ++i)
10541 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 if (wp != NULL && wp->w_width != Columns)
10543 {
10544 /* need to copy part of a line */
10545 j = row + i;
10546 while ((j += line_count) <= end - 1)
10547 linecopy(j - line_count, j, wp);
10548 j -= line_count;
10549 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010550 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10551 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 else
10553 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10554 LineWraps[j] = FALSE;
10555 }
10556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 {
10558 /* whole width, moving the line pointers is faster */
10559 j = row + i;
10560 temp = LineOffset[j];
10561 while ((j += line_count) <= end - 1)
10562 {
10563 LineOffset[j - line_count] = LineOffset[j];
10564 LineWraps[j - line_count] = LineWraps[j];
10565 }
10566 LineOffset[j - line_count] = temp;
10567 LineWraps[j - line_count] = FALSE;
10568 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010569 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 else
10571 lineinvalid(temp, (int)Columns);
10572 }
10573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010575 if (screen_attr != clear_attr)
10576 screen_stop_highlight();
10577 if (clear_attr != 0)
10578 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 /* redraw the characters */
10581 if (type == USE_REDRAW)
10582 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010583 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010585 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 out_str(T_CD);
10587 screen_start(); /* don't know where cursor is now */
10588 }
10589 else if (type == USE_T_CDL)
10590 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010591 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 term_delete_lines(line_count);
10593 screen_start(); /* don't know where cursor is now */
10594 }
10595 /*
10596 * Deleting lines at top of the screen or scroll region: Just scroll
10597 * the whole screen (scroll region) up by outputting newlines on the
10598 * last line.
10599 */
10600 else if (type == USE_NL)
10601 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010602 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 for (i = line_count; --i >= 0; )
10604 out_char('\n'); /* cursor will remain on same line */
10605 }
10606 else
10607 {
10608 for (i = line_count; --i >= 0; )
10609 {
10610 if (type == USE_T_DL)
10611 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010612 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 out_str(T_DL); /* delete a line */
10614 }
10615 else /* type == USE_T_CE */
10616 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010617 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 out_str(T_CE); /* erase a line */
10619 }
10620 screen_start(); /* don't know where cursor is now */
10621 }
10622 }
10623
10624 /*
10625 * If the 'db' flag is set, we need to clear the lines that have been
10626 * scrolled up at the bottom of the region.
10627 */
10628 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10629 {
10630 for (i = line_count; i > 0; --i)
10631 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010632 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 out_str(T_CE); /* erase a line */
10634 screen_start(); /* don't know where cursor is now */
10635 }
10636 }
10637
10638#ifdef FEAT_GUI
10639 gui_can_update_cursor();
10640 if (gui.in_use)
10641 out_flush(); /* always flush after a scroll */
10642#endif
10643
10644 return OK;
10645}
10646
10647/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010648 * Return TRUE when postponing displaying the mode message: when not redrawing
10649 * or inside a mapping.
10650 */
10651 int
10652skip_showmode()
10653{
10654 // Call char_avail() only when we are going to show something, because it
10655 // takes a bit of time. redrawing() may also call char_avail_avail().
10656 if (global_busy
10657 || msg_silent != 0
10658 || !redrawing()
10659 || (char_avail() && !KeyTyped))
10660 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010661 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010662 return TRUE;
10663 }
10664 return FALSE;
10665}
10666
10667/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010668 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 *
10670 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10671 * If clear_cmdline is FALSE there may be a message there that needs to be
10672 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010673 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674 * Return the length of the message (0 if no message).
10675 */
10676 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010677showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679 int need_clear;
10680 int length = 0;
10681 int do_mode;
10682 int attr;
10683 int nwr_save;
10684#ifdef FEAT_INS_EXPAND
10685 int sub_attr;
10686#endif
10687
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010688 do_mode = ((p_smd && msg_silent == 0)
10689 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010690 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010691 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010692 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010694 if (skip_showmode())
10695 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
10697 nwr_save = need_wait_return;
10698
10699 /* wait a bit before overwriting an important message */
10700 check_for_delay(FALSE);
10701
10702 /* if the cmdline is more than one line high, erase top lines */
10703 need_clear = clear_cmdline;
10704 if (clear_cmdline && cmdline_row < Rows - 1)
10705 msg_clr_cmdline(); /* will reset clear_cmdline */
10706
10707 /* Position on the last line in the window, column 0 */
10708 msg_pos_mode();
10709 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010710 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 if (do_mode)
10712 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010713 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010715 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010716# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010717 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010718# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010719 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010720# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010721 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010722# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010723 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010725 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726# endif
10727#endif
10728#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10729 if (gui.in_use)
10730 {
10731 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010732 {
10733 /* HANGUL */
10734 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010735 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010736 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010737 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 }
10740#endif
10741#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010742 /* CTRL-X in Insert mode */
10743 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 {
10745 /* These messages can get long, avoid a wrap in a narrow
10746 * window. Prefer showing edit_submode_extra. */
10747 length = (Rows - msg_row) * Columns - 3;
10748 if (edit_submode_extra != NULL)
10749 length -= vim_strsize(edit_submode_extra);
10750 if (length > 0)
10751 {
10752 if (edit_submode_pre != NULL)
10753 length -= vim_strsize(edit_submode_pre);
10754 if (length - vim_strsize(edit_submode) > 0)
10755 {
10756 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010757 msg_puts_attr((char *)edit_submode_pre, attr);
10758 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 }
10760 if (edit_submode_extra != NULL)
10761 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010762 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010764 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 else
10766 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010767 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 }
10769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 }
10771 else
10772#endif
10773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010775 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010776 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010777 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 else if (State & INSERT)
10779 {
10780#ifdef FEAT_RIGHTLEFT
10781 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010782 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010784 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010786 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010787 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010789 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010791 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792#ifdef FEAT_RIGHTLEFT
10793 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010794 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795#endif
10796#ifdef FEAT_KEYMAP
10797 if (State & LANGMAP)
10798 {
10799# ifdef FEAT_ARABIC
10800 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010801 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 else
10803# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010804 if (get_keymap_str(curwin, (char_u *)" (%s)",
10805 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010806 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 }
10808#endif
10809 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010810 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 if (VIsual_active)
10813 {
10814 char *p;
10815
10816 /* Don't concatenate separate words to avoid translation
10817 * problems. */
10818 switch ((VIsual_select ? 4 : 0)
10819 + (VIsual_mode == Ctrl_V) * 2
10820 + (VIsual_mode == 'V'))
10821 {
10822 case 0: p = N_(" VISUAL"); break;
10823 case 1: p = N_(" VISUAL LINE"); break;
10824 case 2: p = N_(" VISUAL BLOCK"); break;
10825 case 4: p = N_(" SELECT"); break;
10826 case 5: p = N_(" SELECT LINE"); break;
10827 default: p = N_(" SELECT BLOCK"); break;
10828 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010829 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010831 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010833
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 need_clear = TRUE;
10835 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010836 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837#ifdef FEAT_INS_EXPAND
10838 && edit_submode == NULL /* otherwise it gets too long */
10839#endif
10840 )
10841 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010842 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 need_clear = TRUE;
10844 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010845
10846 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010847 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 msg_clr_eos();
10849 msg_didout = FALSE; /* overwrite this message */
10850 length = msg_col;
10851 msg_col = 0;
10852 need_wait_return = nwr_save; /* never ask for hit-return for this */
10853 }
10854 else if (clear_cmdline && msg_silent == 0)
10855 /* Clear the whole command line. Will reset "clear_cmdline". */
10856 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010857 else if (redraw_mode)
10858 {
10859 msg_pos_mode();
10860 msg_clr_eos();
10861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862
10863#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 /* In Visual mode the size of the selected area must be redrawn. */
10865 if (VIsual_active)
10866 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867
10868 /* If the last window has no status line, the ruler is after the mode
10869 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010870 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010871 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872#endif
10873 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010874 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 clear_cmdline = FALSE;
10876
10877 return length;
10878}
10879
10880/*
10881 * Position for a mode message.
10882 */
10883 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010884msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886 msg_col = 0;
10887 msg_row = Rows - 1;
10888}
10889
10890/*
10891 * Delete mode message. Used when ESC is typed which is expected to end
10892 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010893 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 */
10895 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010896unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897{
10898 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010899 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 */
10901 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10902 redraw_cmdline = TRUE; /* delete mode later */
10903 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010904 clearmode();
10905}
10906
10907/*
10908 * Clear the mode message.
10909 */
10910 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010911clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010912{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010913 int save_msg_row = msg_row;
10914 int save_msg_col = msg_col;
10915
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010916 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010917 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010918 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010919 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010920
10921 msg_col = save_msg_col;
10922 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923}
10924
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010925 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010926recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010927{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010928 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010929 if (!shortmess(SHM_RECORDING))
10930 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010931 char s[4];
10932
10933 sprintf(s, " @%c", reg_recording);
10934 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010935 }
10936}
10937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010938/*
10939 * Draw the tab pages line at the top of the Vim window.
10940 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010941 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010942draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010943{
10944 int tabcount = 0;
10945 tabpage_T *tp;
10946 int tabwidth;
10947 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010948 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010949 int attr;
10950 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010951 win_T *cwp;
10952 int wincount;
10953 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010954 int c;
10955 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010956 int attr_sel = HL_ATTR(HLF_TPS);
10957 int attr_nosel = HL_ATTR(HLF_TP);
10958 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010959 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010960 int room;
10961 int use_sep_chars = (t_colors < 8
10962#ifdef FEAT_GUI
10963 && !gui.in_use
10964#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010965#ifdef FEAT_TERMGUICOLORS
10966 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010967#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010968 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010969
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010970 if (ScreenLines == NULL)
10971 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010972 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010973
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010974#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010975 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010976 if (gui_use_tabline())
10977 {
10978 gui_update_tabline();
10979 return;
10980 }
10981#endif
10982
10983 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010984 return;
10985
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010986#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010987 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010988
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010989 /* Use the 'tabline' option if it's set. */
10990 if (*p_tal != NUL)
10991 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010992 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010993
10994 /* Check for an error. If there is one we would loop in redrawing the
10995 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010996 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010997 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010998 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010999 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011000 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020011001 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011002 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000011003 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011004#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011005 {
Bram Moolenaar29323592016-07-24 22:04:11 +020011006 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011007 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011008
Bram Moolenaar238a5642006-02-21 22:12:05 +000011009 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
11010 if (tabwidth < 6)
11011 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011012
Bram Moolenaar238a5642006-02-21 22:12:05 +000011013 attr = attr_nosel;
11014 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011015 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
11016 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000011017 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011018 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011019
Bram Moolenaar238a5642006-02-21 22:12:05 +000011020 if (tp->tp_topframe == topframe)
11021 attr = attr_sel;
11022 if (use_sep_chars && col > 0)
11023 screen_putchar('|', 0, col++, attr);
11024
11025 if (tp->tp_topframe != topframe)
11026 attr = attr_nosel;
11027
11028 screen_putchar(' ', 0, col++, attr);
11029
11030 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000011031 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011032 cwp = curwin;
11033 wp = firstwin;
11034 }
11035 else
11036 {
11037 cwp = tp->tp_curwin;
11038 wp = tp->tp_firstwin;
11039 }
11040
11041 modified = FALSE;
11042 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
11043 if (bufIsChanged(wp->w_buffer))
11044 modified = TRUE;
11045 if (modified || wincount > 1)
11046 {
11047 if (wincount > 1)
11048 {
11049 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011050 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011051 if (col + len >= Columns - 3)
11052 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011053 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011054#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010011055 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011056#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020011057 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011058#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000011059 );
11060 col += len;
11061 }
11062 if (modified)
11063 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
11064 screen_putchar(' ', 0, col++, attr);
11065 }
11066
11067 room = scol - col + tabwidth - 1;
11068 if (room > 0)
11069 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011070 /* Get buffer name in NameBuff[] */
11071 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011072 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011073 len = vim_strsize(NameBuff);
11074 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011075 if (has_mbyte)
11076 while (len > room)
11077 {
11078 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011079 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011080 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011081 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011082 {
11083 p += len - room;
11084 len = room;
11085 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011086 if (len > Columns - col - 1)
11087 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011089 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000011090 col += len;
11091 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000011092 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011093
11094 /* Store the tab page number in TabPageIdxs[], so that
11095 * jump_to_mouse() knows where each one is. */
11096 ++tabcount;
11097 while (scol < col)
11098 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011099 }
11100
Bram Moolenaar238a5642006-02-21 22:12:05 +000011101 if (use_sep_chars)
11102 c = '_';
11103 else
11104 c = ' ';
11105 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000011106
11107 /* Put an "X" for closing the current tab if there are several. */
11108 if (first_tabpage->tp_next != NULL)
11109 {
11110 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
11111 TabPageIdxs[Columns - 1] = -999;
11112 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011113 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000011114
11115 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
11116 * set. */
11117 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011118}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011119
11120/*
11121 * Get buffer name for "buf" into NameBuff[].
11122 * Takes care of special buffer names and translates special characters.
11123 */
11124 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011125get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011126{
11127 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020011128 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011129 else
11130 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
11131 trans_characters(NameBuff, MAXPATHL);
11132}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011133
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134/*
11135 * Get the character to use in a status line. Get its attributes in "*attr".
11136 */
11137 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011138fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139{
11140 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011141
11142#ifdef FEAT_TERMINAL
11143 if (bt_terminal(wp->w_buffer))
11144 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011145 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011146 {
11147 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011148 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011149 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011150 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011151 {
11152 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011153 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011154 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011155 }
11156 else
11157#endif
11158 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011160 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 fill = fill_stl;
11162 }
11163 else
11164 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011165 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 fill = fill_stlnc;
11167 }
11168 /* Use fill when there is highlighting, and highlighting of current
11169 * window differs, or the fillchars differ, or this is not the
11170 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010011171 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011172 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 || (fill_stl != fill_stlnc)))
11174 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011175 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 return '^';
11177 return '=';
11178}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180/*
11181 * Get the character to use in a separator between vertically split windows.
11182 * Get its attributes in "*attr".
11183 */
11184 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010011185fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186{
Bram Moolenaar8820b482017-03-16 17:23:31 +010011187 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 if (*attr == 0 && fill_vert == ' ')
11189 return '|';
11190 else
11191 return fill_vert;
11192}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193
11194/*
11195 * Return TRUE if redrawing should currently be done.
11196 */
11197 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011198redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011200#ifdef FEAT_EVAL
11201 if (disable_redraw_for_testing)
11202 return 0;
11203 else
11204#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011205 return ((!RedrawingDisabled
11206#ifdef FEAT_EVAL
11207 || ignore_redraw_flag_for_testing
11208#endif
11209 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210}
11211
11212/*
11213 * Return TRUE if printing messages should currently be done.
11214 */
11215 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011216messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217{
11218 return (!(p_lz && char_avail() && !KeyTyped));
11219}
11220
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011221#ifdef FEAT_MENU
11222/*
11223 * Draw the window toolbar.
11224 */
11225 static void
11226redraw_win_toolbar(win_T *wp)
11227{
11228 vimmenu_T *menu;
11229 int item_idx = 0;
11230 int item_count = 0;
11231 int col = 0;
11232 int next_col;
11233 int off = (int)(current_ScreenLine - ScreenLines);
11234 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11235 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11236
11237 vim_free(wp->w_winbar_items);
11238 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11239 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011240 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011241
11242 /* TODO: use fewer spaces if there is not enough room */
11243 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011244 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011245 {
11246 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011247 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011248 break;
11249 if (col > 1)
11250 {
11251 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011252 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011253 break;
11254 }
11255
11256 wp->w_winbar_items[item_idx].wb_startcol = col;
11257 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011258 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011259 break;
11260
11261 next_col = text_to_screenline(wp, menu->name, col);
11262 while (col < next_col)
11263 {
11264 ScreenAttrs[off + col] = button_attr;
11265 ++col;
11266 }
11267 wp->w_winbar_items[item_idx].wb_endcol = col;
11268 wp->w_winbar_items[item_idx].wb_menu = menu;
11269 ++item_idx;
11270
Bram Moolenaar02631462017-09-22 15:20:32 +020011271 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011272 break;
11273 space_to_screenline(off + col, button_attr);
11274 ++col;
11275 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011276 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011277 {
11278 space_to_screenline(off + col, fill_attr);
11279 ++col;
11280 }
11281 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11282
Bram Moolenaar02631462017-09-22 15:20:32 +020011283 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011284 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011285}
11286#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011287
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288/*
11289 * Show current status info in ruler and various other places
11290 * If always is FALSE, only show ruler if position has changed.
11291 */
11292 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011293showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
11295 if (!always && !redrawing())
11296 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011297#ifdef FEAT_INS_EXPAND
11298 if (pum_visible())
11299 {
11300 /* Don't redraw right now, do it later. */
11301 curwin->w_redr_status = TRUE;
11302 return;
11303 }
11304#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011305#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011306 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011307 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 else
11309#endif
11310#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011311 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312#endif
11313
11314#ifdef FEAT_TITLE
11315 if (need_maketitle
11316# ifdef FEAT_STL_OPT
11317 || (p_icon && (stl_syntax & STL_IN_ICON))
11318 || (p_title && (stl_syntax & STL_IN_TITLE))
11319# endif
11320 )
11321 maketitle();
11322#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011323 /* Redraw the tab pages line if needed. */
11324 if (redraw_tabline)
11325 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326}
11327
11328#ifdef FEAT_CMDL_INFO
11329 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011330win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011332#define RULER_BUF_LEN 70
11333 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 int row;
11335 int fillchar;
11336 int attr;
11337 int empty_line = FALSE;
11338 colnr_T virtcol;
11339 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011340 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 int this_ru_col;
11343 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011344 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345
11346 /* If 'ruler' off or redrawing disabled, don't do anything */
11347 if (!p_ru)
11348 return;
11349
11350 /*
11351 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11352 * after deleting lines, before cursor.lnum is corrected.
11353 */
11354 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11355 return;
11356
11357#ifdef FEAT_INS_EXPAND
11358 /* Don't draw the ruler while doing insert-completion, it might overwrite
11359 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 if (edit_submode != NULL)
11362 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011363 // Don't draw the ruler when the popup menu is visible, it may overlap.
11364 // Except when the popup menu will be redrawn anyway.
11365 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011366 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367#endif
11368
11369#ifdef FEAT_STL_OPT
11370 if (*p_ruf)
11371 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011372 int save_called_emsg = called_emsg;
11373
11374 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011376 if (called_emsg)
11377 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011378 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011379 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 return;
11381 }
11382#endif
11383
11384 /*
11385 * Check if not in Insert mode and the line is empty (will show "0-1").
11386 */
11387 if (!(State & INSERT)
11388 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11389 empty_line = TRUE;
11390
11391 /*
11392 * Only draw the ruler when something changed.
11393 */
11394 validate_virtcol_win(wp);
11395 if ( redraw_cmdline
11396 || always
11397 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11398 || wp->w_cursor.col != wp->w_ru_cursor.col
11399 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 || wp->w_topline != wp->w_ru_topline
11402 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11403#ifdef FEAT_DIFF
11404 || wp->w_topfill != wp->w_ru_topfill
11405#endif
11406 || empty_line != wp->w_ru_empty)
11407 {
11408 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 if (wp->w_status_height)
11410 {
11411 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011412 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011413 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011414 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 }
11416 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 {
11418 row = Rows - 1;
11419 fillchar = ' ';
11420 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 width = Columns;
11422 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 }
11424
11425 /* In list mode virtcol needs to be recomputed */
11426 virtcol = wp->w_virtcol;
11427 if (wp->w_p_list && lcs_tab1 == NUL)
11428 {
11429 wp->w_p_list = FALSE;
11430 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11431 wp->w_p_list = TRUE;
11432 }
11433
11434 /*
11435 * Some sprintfs return the length, some return a pointer.
11436 * To avoid portability problems we use strlen() here.
11437 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011438 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11440 ? 0L
11441 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011442 len = STRLEN(buffer);
11443 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11445 (int)virtcol + 1);
11446
11447 /*
11448 * Add a "50%" if there is room for it.
11449 * On the last line, don't print in the last column (scrolls the
11450 * screen up on some terminals).
11451 */
11452 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011453 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 this_ru_col = ru_col - (Columns - width);
11458 if (this_ru_col < 0)
11459 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 /* Never use more than half the window/screen width, leave the other
11461 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011462 if (this_ru_col < (width + 1) / 2)
11463 this_ru_col = (width + 1) / 2;
11464 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011466 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011467 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 if (has_mbyte)
11470 i += (*mb_char2bytes)(fillchar, buffer + i);
11471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 buffer[i++] = fillchar;
11473 ++o;
11474 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011475 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 }
11477 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 if (has_mbyte)
11479 {
11480 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011481 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 {
11483 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011484 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 {
11486 buffer[i] = NUL;
11487 break;
11488 }
11489 }
11490 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011491 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011492 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493
Bram Moolenaar4033c552017-09-16 20:54:51 +020011494 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 i = redraw_cmdline;
11496 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011497 this_ru_col + off + (int)STRLEN(buffer),
11498 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 fillchar, fillchar, attr);
11500 /* don't redraw the cmdline because of showing the ruler */
11501 redraw_cmdline = i;
11502 wp->w_ru_cursor = wp->w_cursor;
11503 wp->w_ru_virtcol = wp->w_virtcol;
11504 wp->w_ru_empty = empty_line;
11505 wp->w_ru_topline = wp->w_topline;
11506 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11507#ifdef FEAT_DIFF
11508 wp->w_ru_topfill = wp->w_topfill;
11509#endif
11510 }
11511}
11512#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011513
11514#if defined(FEAT_LINEBREAK) || defined(PROTO)
11515/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011516 * Return the width of the 'number' and 'relativenumber' column.
11517 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011518 * Otherwise it depends on 'numberwidth' and the line count.
11519 */
11520 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011521number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011522{
11523 int n;
11524 linenr_T lnum;
11525
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011526 if (wp->w_p_rnu && !wp->w_p_nu)
11527 /* cursor line shows "0" */
11528 lnum = wp->w_height;
11529 else
11530 /* cursor line shows absolute line number */
11531 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011532
Bram Moolenaar6b314672015-03-20 15:42:10 +010011533 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011534 return wp->w_nrwidth_width;
11535 wp->w_nrwidth_line_count = lnum;
11536
11537 n = 0;
11538 do
11539 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011540 lnum /= 10;
11541 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011542 } while (lnum > 0);
11543
11544 /* 'numberwidth' gives the minimal width plus one */
11545 if (n < wp->w_p_nuw - 1)
11546 n = wp->w_p_nuw - 1;
11547
11548 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011549 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011550 return n;
11551}
11552#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011553
Bram Moolenaar113e1072019-01-20 15:30:40 +010011554#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011555/*
11556 * Return the current cursor column. This is the actual position on the
11557 * screen. First column is 0.
11558 */
11559 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011560screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011561{
11562 return screen_cur_col;
11563}
11564
11565/*
11566 * Return the current cursor row. This is the actual position on the screen.
11567 * First row is 0.
11568 */
11569 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011570screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011571{
11572 return screen_cur_row;
11573}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011574#endif