blob: 644829ead29f7360c481b3c151689bf4e3f5e3c1 [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 Moolenaar202d9822019-06-11 21:56:30 +02001032 int redraw_all = FALSE;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001033
Bram Moolenaar202d9822019-06-11 21:56:30 +02001034 // Need to recompute when switching tabs.
1035 // Also recompute when the type is CLEAR or NOT_VALID, something basic
1036 // (such as the screen size) must have changed.
1037 if (popup_mask_tab != curtab || type >= NOT_VALID)
1038 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02001039 popup_mask_refresh = TRUE;
Bram Moolenaar202d9822019-06-11 21:56:30 +02001040 redraw_all = TRUE;
1041 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001042 if (!popup_mask_refresh)
1043 {
1044 // Check if any buffer has changed.
1045 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1046 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1047 popup_mask_refresh = TRUE;
1048 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1049 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1050 popup_mask_refresh = TRUE;
1051 if (!popup_mask_refresh)
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001052 return;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001053 }
1054
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001055 // Need to update the mask, something has changed.
Bram Moolenaar33796b32019-06-08 16:01:13 +02001056 popup_mask_refresh = FALSE;
1057 popup_mask_tab = curtab;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001058 popup_visible = FALSE;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001059
1060 // If redrawing everything, just update "popup_mask".
1061 // If redrawing only what is needed, update "popup_mask_next" and then
1062 // compare with "popup_mask" to see what changed.
1063 if (type >= SOME_VALID)
1064 mask = popup_mask;
1065 else
1066 mask = popup_mask_next;
1067 vim_memset(mask, 0, screen_Rows * screen_Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02001068
1069 // Find the window with the lowest zindex that hasn't been handled yet,
1070 // so that the window with a higher zindex overwrites the value in
1071 // popup_mask.
1072 popup_reset_handled();
1073 while ((wp = find_next_popup(TRUE)) != NULL)
1074 {
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001075 int height_extra, width_extra;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001076
1077 popup_visible = TRUE;
1078
1079 // Recompute the position if the text changed.
Bram Moolenaar202d9822019-06-11 21:56:30 +02001080 if (redraw_all
1081 || wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar33796b32019-06-08 16:01:13 +02001082 popup_adjust_position(wp);
1083
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001084 // the width and height are for the inside, add the padding and
Bram Moolenaar33796b32019-06-08 16:01:13 +02001085 // border
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001086 height_extra = wp->w_popup_padding[0] + wp->w_popup_border[0]
1087 + wp->w_popup_padding[2] + wp->w_popup_border[2];
1088 width_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
1089 + wp->w_popup_padding[1] + wp->w_popup_border[1];
Bram Moolenaar33796b32019-06-08 16:01:13 +02001090
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001091 for (line = wp->w_winrow;
1092 line < wp->w_winrow + wp->w_height + height_extra
Bram Moolenaar33796b32019-06-08 16:01:13 +02001093 && line < screen_Rows; ++line)
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001094 for (col = wp->w_wincol;
1095 col < wp->w_wincol + wp->w_width + width_extra
Bram Moolenaar33796b32019-06-08 16:01:13 +02001096 && col < screen_Columns; ++col)
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001097 mask[line * screen_Columns + col] = wp->w_zindex;
Bram Moolenaar33796b32019-06-08 16:01:13 +02001098 }
1099
Bram Moolenaar4c063a02019-06-10 21:24:12 +02001100 // Only check which lines are to be updated if not already
1101 // updating all lines.
1102 if (mask == popup_mask_next)
1103 for (line = 0; line < screen_Rows; ++line)
1104 {
1105 int col_done = 0;
1106
1107 for (col = 0; col < screen_Columns; ++col)
1108 {
1109 int off = line * screen_Columns + col;
1110
1111 if (popup_mask[off] != popup_mask_next[off])
1112 {
1113 popup_mask[off] = popup_mask_next[off];
1114
1115 // The screen position "line" / "col" needs to be redrawn.
1116 // Figure out what window that is and update w_redraw_top
1117 // and w_redr_bot. Only needs to be done for each window
1118 // line.
1119 if (col >= col_done)
1120 {
1121 linenr_T lnum;
1122 int line_cp = line;
1123 int col_cp = col;
1124
1125 // find the window where the row is in
1126 wp = mouse_find_win(&line_cp, &col_cp);
1127 if (wp != NULL)
1128 {
1129 if (line_cp >= wp->w_height)
1130 // In (or below) status line
1131 wp->w_redr_status = TRUE;
1132 // compute the position in the buffer line from the
1133 // position on the screen
1134 else if (mouse_comp_pos(wp, &line_cp, &col_cp,
1135 &lnum))
1136 // past bottom
1137 wp->w_redr_status = TRUE;
1138 else
1139 redrawWinline(wp, lnum);
1140
1141 // This line is going to be redrawn, no need to
1142 // check until the right side of the window.
1143 col_done = wp->w_wincol + wp->w_width - 1;
1144 }
1145 }
1146 }
1147 }
1148 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001149}
1150
1151/*
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001152 * Return a string of "len" spaces in IObuff.
1153 */
1154 static char_u *
1155get_spaces(int len)
1156{
1157 vim_memset(IObuff, ' ', (size_t)len);
1158 IObuff[len] = NUL;
1159 return IObuff;
1160}
1161
1162 static void
1163update_popups(void)
1164{
1165 win_T *wp;
1166 int top_off;
1167 int left_off;
1168 int total_width;
1169 int total_height;
1170 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001171 int border_attr[4];
Bram Moolenaar02e15072019-06-03 22:53:30 +02001172 int border_char[8];
Bram Moolenaar790498b2019-06-01 22:15:29 +02001173 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001174 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001175 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001176
1177 // Find the window with the lowest zindex that hasn't been updated yet,
1178 // so that the window with a higher zindex is drawn later, thus goes on
1179 // top.
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001180 popup_reset_handled();
1181 while ((wp = find_next_popup(TRUE)) != NULL)
1182 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02001183 // This drawing uses the zindex of the popup window, so that it's on
1184 // top of the text but doesn't draw when another popup with higher
1185 // zindex is on top of the character.
1186 screen_zindex = wp->w_zindex;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001187
1188 // adjust w_winrow and w_wincol for border and padding, since
1189 // win_update() doesn't handle them.
1190 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1191 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1192 wp->w_winrow += top_off;
1193 wp->w_wincol += left_off;
1194
1195 // Draw the popup text.
1196 win_update(wp);
1197
1198 wp->w_winrow -= top_off;
1199 wp->w_wincol -= left_off;
1200
1201 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1202 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1203 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1204 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1205 popup_attr = get_wcr_attr(wp);
1206
Bram Moolenaar3f6aeba2019-06-03 22:21:27 +02001207 // We can only use these line drawing characters when 'encoding' is
1208 // "utf-8" and 'ambiwidth' is "single".
Bram Moolenaar02e15072019-06-03 22:53:30 +02001209 if (enc_utf8 && *p_ambw == 's')
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001210 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001211 border_char[0] = border_char[2] = 0x2550;
1212 border_char[1] = border_char[3] = 0x2551;
1213 border_char[4] = 0x2554;
1214 border_char[5] = 0x2557;
1215 border_char[6] = 0x255d;
1216 border_char[7] = 0x255a;
1217 }
Bram Moolenaar02e15072019-06-03 22:53:30 +02001218 else
1219 {
1220 border_char[0] = border_char[2] = '-';
1221 border_char[1] = border_char[3] = '|';
1222 for (i = 4; i < 8; ++i)
1223 border_char[i] = '+';
1224 }
Bram Moolenaar790498b2019-06-01 22:15:29 +02001225 for (i = 0; i < 8; ++i)
1226 if (wp->w_border_char[i] != 0)
1227 border_char[i] = wp->w_border_char[i];
1228
1229 for (i = 0; i < 4; ++i)
1230 {
1231 border_attr[i] = popup_attr;
1232 if (wp->w_border_highlight[i] != NULL)
1233 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001234 }
1235
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001236 if (wp->w_popup_border[0] > 0)
1237 {
1238 // top border
1239 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1240 wp->w_wincol,
1241 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001242 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001243 ? border_char[4] : border_char[0],
1244 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001245 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001246 {
1247 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1248 screen_puts(buf, wp->w_winrow,
1249 wp->w_wincol + total_width - 1, border_attr[1]);
1250 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001251 }
1252
1253 if (wp->w_popup_padding[0] > 0)
1254 {
1255 // top padding
1256 row = wp->w_winrow + wp->w_popup_border[0];
1257 screen_fill(row, row + wp->w_popup_padding[0],
1258 wp->w_wincol + wp->w_popup_border[3],
1259 wp->w_wincol + total_width - wp->w_popup_border[1],
1260 ' ', ' ', popup_attr);
1261 }
1262
1263 for (row = wp->w_winrow + wp->w_popup_border[0];
1264 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1265 ++row)
1266 {
1267 // left border
1268 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001269 {
1270 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1271 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1272 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001273 // left padding
1274 if (wp->w_popup_padding[3] > 0)
1275 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1276 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1277 // right border
1278 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001279 {
1280 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1281 screen_puts(buf, row,
1282 wp->w_wincol + total_width - 1, border_attr[1]);
1283 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001284 // right padding
1285 if (wp->w_popup_padding[1] > 0)
1286 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1287 wp->w_wincol + wp->w_popup_border[3]
1288 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1289 }
1290
1291 if (wp->w_popup_padding[2] > 0)
1292 {
1293 // bottom padding
1294 row = wp->w_winrow + wp->w_popup_border[0]
1295 + wp->w_popup_padding[0] + wp->w_height;
1296 screen_fill(row, row + wp->w_popup_padding[2],
1297 wp->w_wincol + wp->w_popup_border[3],
1298 wp->w_wincol + total_width - wp->w_popup_border[1],
1299 ' ', ' ', popup_attr);
1300 }
1301
1302 if (wp->w_popup_border[2] > 0)
1303 {
1304 // bottom border
1305 row = wp->w_winrow + total_height - 1;
1306 screen_fill(row , row + 1,
1307 wp->w_wincol,
1308 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001309 wp->w_popup_border[3] != 0
1310 ? border_char[7] : border_char[2],
1311 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001312 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001313 {
1314 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1315 screen_puts(buf, row,
1316 wp->w_wincol + total_width - 1, border_attr[2]);
1317 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001318 }
Bram Moolenaar33796b32019-06-08 16:01:13 +02001319
1320 // Back to the normal zindex.
1321 screen_zindex = 0;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001322 }
1323}
1324#endif
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#if defined(FEAT_GUI) || defined(PROTO)
1327/*
1328 * Update a single window, its status line and maybe the command line msg.
1329 * Used for the GUI scrollbar.
1330 */
1331 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001332updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001334 /* return if already busy updating */
1335 if (updating_screen)
1336 return;
1337
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 update_prepare();
1339
1340#ifdef FEAT_CLIPBOARD
1341 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001342 if (clip_star.available && clip_isautosel_star())
1343 clip_update_selection(&clip_star);
1344 if (clip_plus.available && clip_isautosel_plus())
1345 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001349
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001350 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001351 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001352 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (wp->w_redr_status
1355# ifdef FEAT_CMDL_INFO
1356 || p_ru
1357# endif
1358# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001359 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360# endif
1361 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001362 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
Bram Moolenaar988c4332019-06-02 14:12:11 +02001364#ifdef FEAT_TEXT_PROP
1365 // Display popup windows on top of everything.
1366 update_popups();
1367#endif
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 update_finish();
1370}
1371#endif
1372
1373/*
1374 * Update a single window.
1375 *
1376 * This may cause the windows below it also to be redrawn (when clearing the
1377 * screen or scrolling lines).
1378 *
1379 * How the window is redrawn depends on wp->w_redr_type. Each type also
1380 * implies the one below it.
1381 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001382 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1384 * INVERTED redraw the changed part of the Visual area
1385 * INVERTED_ALL redraw the whole Visual area
1386 * VALID 1. scroll up/down to adjust for a changed w_topline
1387 * 2. update lines at the top when scrolled down
1388 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001389 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 * b_mod_top and b_mod_bot.
1391 * - if wp->w_redraw_top non-zero, redraw lines between
1392 * wp->w_redraw_top and wp->w_redr_bot.
1393 * - continue redrawing when syntax status is invalid.
1394 * 4. if scrolled up, update lines at the bottom.
1395 * This results in three areas that may need updating:
1396 * top: from first row to top_end (when scrolled down)
1397 * mid: from mid_start to mid_end (update inversion or changed text)
1398 * bot: from bot_start to last row (when scrolled up)
1399 */
1400 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001401win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 buf_T *buf = wp->w_buffer;
1404 int type;
1405 int top_end = 0; /* Below last row of the top area that needs
1406 updating. 0 when no top area updating. */
1407 int mid_start = 999;/* first row of the mid area that needs
1408 updating. 999 when no mid area updating. */
1409 int mid_end = 0; /* Below last row of the mid area that needs
1410 updating. 0 when no mid area updating. */
1411 int bot_start = 999;/* first row of the bot area that needs
1412 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int scrolled_down = FALSE; /* TRUE when scrolled down when
1414 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001416 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 int top_to_mod = FALSE; /* redraw above mod_top */
1418#endif
1419
1420 int row; /* current window row to display */
1421 linenr_T lnum; /* current buffer lnum to display */
1422 int idx; /* current index in w_lines[] */
1423 int srow; /* starting row of the current line */
1424
1425 int eof = FALSE; /* if TRUE, we hit the end of the file */
1426 int didline = FALSE; /* if TRUE, we finished the last line */
1427 int i;
1428 long j;
1429 static int recursive = FALSE; /* being called recursively */
1430 int old_botline = wp->w_botline;
1431#ifdef FEAT_FOLDING
1432 long fold_count;
1433#endif
1434#ifdef FEAT_SYN_HL
1435 /* remember what happened to the previous line, to know if
1436 * check_visual_highlight() can be used */
1437#define DID_NONE 1 /* didn't update a line */
1438#define DID_LINE 2 /* updated a normal line */
1439#define DID_FOLD 3 /* updated a folded line */
1440 int did_update = DID_NONE;
1441 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1442#endif
1443 linenr_T mod_top = 0;
1444 linenr_T mod_bot = 0;
1445#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1446 int save_got_int;
1447#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001448#ifdef SYN_TIME_LIMIT
1449 proftime_T syntax_tm;
1450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452 type = wp->w_redr_type;
1453
1454 if (type == NOT_VALID)
1455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 wp->w_lines_valid = 0;
1458 }
1459
1460 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001461 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
1463 wp->w_redr_type = 0;
1464 return;
1465 }
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 /* Window is zero-width: Only need to draw the separator. */
1468 if (wp->w_width == 0)
1469 {
1470 /* draw the vertical separator right of this window */
1471 draw_vsep_win(wp, 0);
1472 wp->w_redr_type = 0;
1473 return;
1474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001476#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001477 // If this window contains a terminal, redraw works completely differently.
1478 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001479 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001480 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001481# ifdef FEAT_MENU
1482 /* Draw the window toolbar, if there is one. */
1483 if (winbar_height(wp) > 0)
1484 redraw_win_toolbar(wp);
1485# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001486 wp->w_redr_type = 0;
1487 return;
1488 }
1489#endif
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001492 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493#endif
1494
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001495#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001496 /* Force redraw when width of 'number' or 'relativenumber' column
1497 * changes. */
1498 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001499 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001500 {
1501 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001502 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001503 }
1504 else
1505#endif
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1508 {
1509 /*
1510 * When there are both inserted/deleted lines and specific lines to be
1511 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1512 * everything (only happens when redrawing is off for while).
1513 */
1514 type = NOT_VALID;
1515 }
1516 else
1517 {
1518 /*
1519 * Set mod_top to the first line that needs displaying because of
1520 * changes. Set mod_bot to the first line after the changes.
1521 */
1522 mod_top = wp->w_redraw_top;
1523 if (wp->w_redraw_bot != 0)
1524 mod_bot = wp->w_redraw_bot + 1;
1525 else
1526 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (buf->b_mod_set)
1528 {
1529 if (mod_top == 0 || mod_top > buf->b_mod_top)
1530 {
1531 mod_top = buf->b_mod_top;
1532#ifdef FEAT_SYN_HL
1533 /* Need to redraw lines above the change that may be included
1534 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001535 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001537 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (mod_top < 1)
1539 mod_top = 1;
1540 }
1541#endif
1542 }
1543 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1544 mod_bot = buf->b_mod_bot;
1545
1546#ifdef FEAT_SEARCH_EXTRA
1547 /* When 'hlsearch' is on and using a multi-line search pattern, a
1548 * change in one line may make the Search highlighting in a
1549 * previous line invalid. Simple solution: redraw all visible
1550 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001551 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001553 if (search_hl.rm.regprog != NULL
1554 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001556 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001557 {
1558 cur = wp->w_match_head;
1559 while (cur != NULL)
1560 {
1561 if (cur->match.regprog != NULL
1562 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001563 {
1564 top_to_mod = TRUE;
1565 break;
1566 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001567 cur = cur->next;
1568 }
1569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#endif
1571 }
1572#ifdef FEAT_FOLDING
1573 if (mod_top != 0 && hasAnyFolding(wp))
1574 {
1575 linenr_T lnumt, lnumb;
1576
1577 /*
1578 * A change in a line can cause lines above it to become folded or
1579 * unfolded. Find the top most buffer line that may be affected.
1580 * If the line was previously folded and displayed, get the first
1581 * line of that fold. If the line is folded now, get the first
1582 * folded line. Use the minimum of these two.
1583 */
1584
1585 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1586 * the line below it. If there is no valid entry, use w_topline.
1587 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1588 * to this line. If there is no valid entry, use MAXLNUM. */
1589 lnumt = wp->w_topline;
1590 lnumb = MAXLNUM;
1591 for (i = 0; i < wp->w_lines_valid; ++i)
1592 if (wp->w_lines[i].wl_valid)
1593 {
1594 if (wp->w_lines[i].wl_lastlnum < mod_top)
1595 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1596 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1597 {
1598 lnumb = wp->w_lines[i].wl_lnum;
1599 /* When there is a fold column it might need updating
1600 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001601 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 ++lnumb;
1603 }
1604 }
1605
1606 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1607 if (mod_top > lnumt)
1608 mod_top = lnumt;
1609
1610 /* Now do the same for the bottom line (one above mod_bot). */
1611 --mod_bot;
1612 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1613 ++mod_bot;
1614 if (mod_bot < lnumb)
1615 mod_bot = lnumb;
1616 }
1617#endif
1618
1619 /* When a change starts above w_topline and the end is below
1620 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001621 * If the end of the change is above w_topline: do like no change was
1622 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (mod_top != 0 && mod_top < wp->w_topline)
1624 {
1625 if (mod_bot > wp->w_topline)
1626 mod_top = wp->w_topline;
1627#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001628 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 top_end = 1;
1630#endif
1631 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001632
1633 /* When line numbers are displayed need to redraw all lines below
1634 * inserted/deleted lines. */
1635 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1636 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001638 wp->w_redraw_top = 0; // reset for next time
1639 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 /*
1642 * When only displaying the lines at the top, set top_end. Used when
1643 * window has scrolled down for msg_scrolled.
1644 */
1645 if (type == REDRAW_TOP)
1646 {
1647 j = 0;
1648 for (i = 0; i < wp->w_lines_valid; ++i)
1649 {
1650 j += wp->w_lines[i].wl_size;
1651 if (j >= wp->w_upd_rows)
1652 {
1653 top_end = j;
1654 break;
1655 }
1656 }
1657 if (top_end == 0)
1658 /* not found (cannot happen?): redraw everything */
1659 type = NOT_VALID;
1660 else
1661 /* top area defined, the rest is VALID */
1662 type = VALID;
1663 }
1664
Bram Moolenaar367329b2007-08-30 11:53:22 +00001665 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001666 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1667 * non-zero and thus not FALSE) will indicate that screenclear() was not
1668 * called. */
1669 if (screen_cleared)
1670 screen_cleared = MAYBE;
1671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 /*
1673 * If there are no changes on the screen that require a complete redraw,
1674 * handle three cases:
1675 * 1: we are off the top of the screen by a few lines: scroll down
1676 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1677 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1678 * w_lines[] that needs updating.
1679 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001680 if ((type == VALID || type == SOME_VALID
1681 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#ifdef FEAT_DIFF
1683 && !wp->w_botfill && !wp->w_old_botfill
1684#endif
1685 )
1686 {
1687 if (mod_top != 0 && wp->w_topline == mod_top)
1688 {
1689 /*
1690 * w_topline is the first changed line, the scrolling will be done
1691 * further down.
1692 */
1693 }
1694 else if (wp->w_lines[0].wl_valid
1695 && (wp->w_topline < wp->w_lines[0].wl_lnum
1696#ifdef FEAT_DIFF
1697 || (wp->w_topline == wp->w_lines[0].wl_lnum
1698 && wp->w_topfill > wp->w_old_topfill)
1699#endif
1700 ))
1701 {
1702 /*
1703 * New topline is above old topline: May scroll down.
1704 */
1705#ifdef FEAT_FOLDING
1706 if (hasAnyFolding(wp))
1707 {
1708 linenr_T ln;
1709
1710 /* count the number of lines we are off, counting a sequence
1711 * of folded lines as one */
1712 j = 0;
1713 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1714 {
1715 ++j;
1716 if (j >= wp->w_height - 2)
1717 break;
1718 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1719 }
1720 }
1721 else
1722#endif
1723 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1724 if (j < wp->w_height - 2) /* not too far off */
1725 {
1726 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1727#ifdef FEAT_DIFF
1728 /* insert extra lines for previously invisible filler lines */
1729 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1730 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1731 - wp->w_old_topfill;
1732#endif
1733 if (i < wp->w_height - 2) /* less than a screen off */
1734 {
1735 /*
1736 * Try to insert the correct number of lines.
1737 * If not the last window, delete the lines at the bottom.
1738 * win_ins_lines may fail when the terminal can't do it.
1739 */
1740 if (i > 0)
1741 check_for_delay(FALSE);
1742 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1743 {
1744 if (wp->w_lines_valid != 0)
1745 {
1746 /* Need to update rows that are new, stop at the
1747 * first one that scrolled down. */
1748 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751 /* Move the entries that were scrolled, disable
1752 * the entries for the lines to be redrawn. */
1753 if ((wp->w_lines_valid += j) > wp->w_height)
1754 wp->w_lines_valid = wp->w_height;
1755 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1756 wp->w_lines[idx] = wp->w_lines[idx - j];
1757 while (idx >= 0)
1758 wp->w_lines[idx--].wl_valid = FALSE;
1759 }
1760 }
1761 else
1762 mid_start = 0; /* redraw all lines */
1763 }
1764 else
1765 mid_start = 0; /* redraw all lines */
1766 }
1767 else
1768 mid_start = 0; /* redraw all lines */
1769 }
1770 else
1771 {
1772 /*
1773 * New topline is at or below old topline: May scroll up.
1774 * When topline didn't change, find first entry in w_lines[] that
1775 * needs updating.
1776 */
1777
1778 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1779 j = -1;
1780 row = 0;
1781 for (i = 0; i < wp->w_lines_valid; i++)
1782 {
1783 if (wp->w_lines[i].wl_valid
1784 && wp->w_lines[i].wl_lnum == wp->w_topline)
1785 {
1786 j = i;
1787 break;
1788 }
1789 row += wp->w_lines[i].wl_size;
1790 }
1791 if (j == -1)
1792 {
1793 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1794 * lines */
1795 mid_start = 0;
1796 }
1797 else
1798 {
1799 /*
1800 * Try to delete the correct number of lines.
1801 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1802 */
1803#ifdef FEAT_DIFF
1804 /* If the topline didn't change, delete old filler lines,
1805 * otherwise delete filler lines of the new topline... */
1806 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1807 row += wp->w_old_topfill;
1808 else
1809 row += diff_check_fill(wp, wp->w_topline);
1810 /* ... but don't delete new filler lines. */
1811 row -= wp->w_topfill;
1812#endif
1813 if (row > 0)
1814 {
1815 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001816 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1817 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 bot_start = wp->w_height - row;
1819 else
1820 mid_start = 0; /* redraw all lines */
1821 }
1822 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1823 {
1824 /*
1825 * Skip the lines (below the deleted lines) that are still
1826 * valid and don't need redrawing. Copy their info
1827 * upwards, to compensate for the deleted lines. Set
1828 * bot_start to the first row that needs redrawing.
1829 */
1830 bot_start = 0;
1831 idx = 0;
1832 for (;;)
1833 {
1834 wp->w_lines[idx] = wp->w_lines[j];
1835 /* stop at line that didn't fit, unless it is still
1836 * valid (no lines deleted) */
1837 if (row > 0 && bot_start + row
1838 + (int)wp->w_lines[j].wl_size > wp->w_height)
1839 {
1840 wp->w_lines_valid = idx + 1;
1841 break;
1842 }
1843 bot_start += wp->w_lines[idx++].wl_size;
1844
1845 /* stop at the last valid entry in w_lines[].wl_size */
1846 if (++j >= wp->w_lines_valid)
1847 {
1848 wp->w_lines_valid = idx;
1849 break;
1850 }
1851 }
1852#ifdef FEAT_DIFF
1853 /* Correct the first entry for filler lines at the top
1854 * when it won't get updated below. */
1855 if (wp->w_p_diff && bot_start > 0)
1856 wp->w_lines[0].wl_size =
1857 plines_win_nofill(wp, wp->w_topline, TRUE)
1858 + wp->w_topfill;
1859#endif
1860 }
1861 }
1862 }
1863
1864 /* When starting redraw in the first line, redraw all lines. When
1865 * there is only one window it's probably faster to clear the screen
1866 * first. */
1867 if (mid_start == 0)
1868 {
1869 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001870 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001871 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001872 /* Clear the screen when it was not done by win_del_lines() or
1873 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1874 * then. */
1875 if (screen_cleared != TRUE)
1876 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001877 /* The screen was cleared, redraw the tab pages line. */
1878 if (redraw_tabline)
1879 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001882
1883 /* When win_del_lines() or win_ins_lines() caused the screen to be
1884 * cleared (only happens for the first window) or when screenclear()
1885 * was called directly above, "must_redraw" will have been set to
1886 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1887 if (screen_cleared == TRUE)
1888 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 }
1890 else
1891 {
1892 /* Not VALID or INVERTED: redraw all lines. */
1893 mid_start = 0;
1894 mid_end = wp->w_height;
1895 }
1896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001897 if (type == SOME_VALID)
1898 {
1899 /* SOME_VALID: redraw all lines. */
1900 mid_start = 0;
1901 mid_end = wp->w_height;
1902 type = NOT_VALID;
1903 }
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 /* check if we are updating or removing the inverted part */
1906 if ((VIsual_active && buf == curwin->w_buffer)
1907 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1908 {
1909 linenr_T from, to;
1910
1911 if (VIsual_active)
1912 {
1913 if (VIsual_active
1914 && (VIsual_mode != wp->w_old_visual_mode
1915 || type == INVERTED_ALL))
1916 {
1917 /*
1918 * If the type of Visual selection changed, redraw the whole
1919 * selection. Also when the ownership of the X selection is
1920 * gained or lost.
1921 */
1922 if (curwin->w_cursor.lnum < VIsual.lnum)
1923 {
1924 from = curwin->w_cursor.lnum;
1925 to = VIsual.lnum;
1926 }
1927 else
1928 {
1929 from = VIsual.lnum;
1930 to = curwin->w_cursor.lnum;
1931 }
1932 /* redraw more when the cursor moved as well */
1933 if (wp->w_old_cursor_lnum < from)
1934 from = wp->w_old_cursor_lnum;
1935 if (wp->w_old_cursor_lnum > to)
1936 to = wp->w_old_cursor_lnum;
1937 if (wp->w_old_visual_lnum < from)
1938 from = wp->w_old_visual_lnum;
1939 if (wp->w_old_visual_lnum > to)
1940 to = wp->w_old_visual_lnum;
1941 }
1942 else
1943 {
1944 /*
1945 * Find the line numbers that need to be updated: The lines
1946 * between the old cursor position and the current cursor
1947 * position. Also check if the Visual position changed.
1948 */
1949 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1950 {
1951 from = curwin->w_cursor.lnum;
1952 to = wp->w_old_cursor_lnum;
1953 }
1954 else
1955 {
1956 from = wp->w_old_cursor_lnum;
1957 to = curwin->w_cursor.lnum;
1958 if (from == 0) /* Visual mode just started */
1959 from = to;
1960 }
1961
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001962 if (VIsual.lnum != wp->w_old_visual_lnum
1963 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 if (wp->w_old_visual_lnum < from
1966 && wp->w_old_visual_lnum != 0)
1967 from = wp->w_old_visual_lnum;
1968 if (wp->w_old_visual_lnum > to)
1969 to = wp->w_old_visual_lnum;
1970 if (VIsual.lnum < from)
1971 from = VIsual.lnum;
1972 if (VIsual.lnum > to)
1973 to = VIsual.lnum;
1974 }
1975 }
1976
1977 /*
1978 * If in block mode and changed column or curwin->w_curswant:
1979 * update all lines.
1980 * First compute the actual start and end column.
1981 */
1982 if (VIsual_mode == Ctrl_V)
1983 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001984 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001985#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001986 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
Bram Moolenaar404406a2014-10-09 13:24:43 +02001988 if (curwin->w_p_lbr)
1989 ve_flags = VE_ALL;
1990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001992#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001993 ve_flags = save_ve_flags;
1994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 ++toc;
1996 if (curwin->w_curswant == MAXCOL)
1997 toc = MAXCOL;
1998
1999 if (fromc != wp->w_old_cursor_fcol
2000 || toc != wp->w_old_cursor_lcol)
2001 {
2002 if (from > VIsual.lnum)
2003 from = VIsual.lnum;
2004 if (to < VIsual.lnum)
2005 to = VIsual.lnum;
2006 }
2007 wp->w_old_cursor_fcol = fromc;
2008 wp->w_old_cursor_lcol = toc;
2009 }
2010 }
2011 else
2012 {
2013 /* Use the line numbers of the old Visual area. */
2014 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
2015 {
2016 from = wp->w_old_cursor_lnum;
2017 to = wp->w_old_visual_lnum;
2018 }
2019 else
2020 {
2021 from = wp->w_old_visual_lnum;
2022 to = wp->w_old_cursor_lnum;
2023 }
2024 }
2025
2026 /*
2027 * There is no need to update lines above the top of the window.
2028 */
2029 if (from < wp->w_topline)
2030 from = wp->w_topline;
2031
2032 /*
2033 * If we know the value of w_botline, use it to restrict the update to
2034 * the lines that are visible in the window.
2035 */
2036 if (wp->w_valid & VALID_BOTLINE)
2037 {
2038 if (from >= wp->w_botline)
2039 from = wp->w_botline - 1;
2040 if (to >= wp->w_botline)
2041 to = wp->w_botline - 1;
2042 }
2043
2044 /*
2045 * Find the minimal part to be updated.
2046 * Watch out for scrolling that made entries in w_lines[] invalid.
2047 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
2048 * top_end; need to redraw from top_end to the "to" line.
2049 * A middle mouse click with a Visual selection may change the text
2050 * above the Visual area and reset wl_valid, do count these for
2051 * mid_end (in srow).
2052 */
2053 if (mid_start > 0)
2054 {
2055 lnum = wp->w_topline;
2056 idx = 0;
2057 srow = 0;
2058 if (scrolled_down)
2059 mid_start = top_end;
2060 else
2061 mid_start = 0;
2062 while (lnum < from && idx < wp->w_lines_valid) /* find start */
2063 {
2064 if (wp->w_lines[idx].wl_valid)
2065 mid_start += wp->w_lines[idx].wl_size;
2066 else if (!scrolled_down)
2067 srow += wp->w_lines[idx].wl_size;
2068 ++idx;
2069# ifdef FEAT_FOLDING
2070 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
2071 lnum = wp->w_lines[idx].wl_lnum;
2072 else
2073# endif
2074 ++lnum;
2075 }
2076 srow += mid_start;
2077 mid_end = wp->w_height;
2078 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
2079 {
2080 if (wp->w_lines[idx].wl_valid
2081 && wp->w_lines[idx].wl_lnum >= to + 1)
2082 {
2083 /* Only update until first row of this line */
2084 mid_end = srow;
2085 break;
2086 }
2087 srow += wp->w_lines[idx].wl_size;
2088 }
2089 }
2090 }
2091
2092 if (VIsual_active && buf == curwin->w_buffer)
2093 {
2094 wp->w_old_visual_mode = VIsual_mode;
2095 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
2096 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00002097 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 wp->w_old_curswant = curwin->w_curswant;
2099 }
2100 else
2101 {
2102 wp->w_old_visual_mode = 0;
2103 wp->w_old_cursor_lnum = 0;
2104 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00002105 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107
2108#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2109 /* reset got_int, otherwise regexp won't work */
2110 save_got_int = got_int;
2111 got_int = 0;
2112#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002113#ifdef SYN_TIME_LIMIT
2114 /* Set the time limit to 'redrawtime'. */
2115 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002116 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02002117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#ifdef FEAT_FOLDING
2119 win_foldinfo.fi_level = 0;
2120#endif
2121
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002122#ifdef FEAT_MENU
2123 /*
2124 * Draw the window toolbar, if there is one.
2125 * TODO: only when needed.
2126 */
2127 if (winbar_height(wp) > 0)
2128 redraw_win_toolbar(wp);
2129#endif
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 /*
2132 * Update all the window rows.
2133 */
2134 idx = 0; /* first entry in w_lines[].wl_size */
2135 row = 0;
2136 srow = 0;
2137 lnum = wp->w_topline; /* first line shown in window */
2138 for (;;)
2139 {
2140 /* stop updating when reached the end of the window (check for _past_
2141 * the end of the window is at the end of the loop) */
2142 if (row == wp->w_height)
2143 {
2144 didline = TRUE;
2145 break;
2146 }
2147
2148 /* stop updating when hit the end of the file */
2149 if (lnum > buf->b_ml.ml_line_count)
2150 {
2151 eof = TRUE;
2152 break;
2153 }
2154
2155 /* Remember the starting row of the line that is going to be dealt
2156 * with. It is used further down when the line doesn't fit. */
2157 srow = row;
2158
2159 /*
2160 * Update a line when it is in an area that needs updating, when it
2161 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002162 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 * win_del_lines(), check if the current line includes it.
2164 * When syntax folding is being used, the saved syntax states will
2165 * already have been updated, we can't see where the syntax state is
2166 * the same again, just update until the end of the window.
2167 */
2168 if (row < top_end
2169 || (row >= mid_start && row < mid_end)
2170#ifdef FEAT_SEARCH_EXTRA
2171 || top_to_mod
2172#endif
2173 || idx >= wp->w_lines_valid
2174 || (row + wp->w_lines[idx].wl_size > bot_start)
2175 || (mod_top != 0
2176 && (lnum == mod_top
2177 || (lnum >= mod_top
2178 && (lnum < mod_bot
2179#ifdef FEAT_SYN_HL
2180 || did_update == DID_FOLD
2181 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002182 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 && (
2184# ifdef FEAT_FOLDING
2185 (foldmethodIsSyntax(wp)
2186 && hasAnyFolding(wp)) ||
2187# endif
2188 syntax_check_changed(lnum)))
2189#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002190#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002191 /* match in fixed position might need redraw
2192 * if lines were inserted or deleted */
2193 || (wp->w_match_head != NULL
2194 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 )))))
2197 {
2198#ifdef FEAT_SEARCH_EXTRA
2199 if (lnum == mod_top)
2200 top_to_mod = FALSE;
2201#endif
2202
2203 /*
2204 * When at start of changed lines: May scroll following lines
2205 * up or down to minimize redrawing.
2206 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002207 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 */
2209 if (lnum == mod_top
2210 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002211 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 int old_rows = 0;
2214 int new_rows = 0;
2215 int xtra_rows;
2216 linenr_T l;
2217
2218 /* Count the old number of window rows, using w_lines[], which
2219 * should still contain the sizes for the lines as they are
2220 * currently displayed. */
2221 for (i = idx; i < wp->w_lines_valid; ++i)
2222 {
2223 /* Only valid lines have a meaningful wl_lnum. Invalid
2224 * lines are part of the changed area. */
2225 if (wp->w_lines[i].wl_valid
2226 && wp->w_lines[i].wl_lnum == mod_bot)
2227 break;
2228 old_rows += wp->w_lines[i].wl_size;
2229#ifdef FEAT_FOLDING
2230 if (wp->w_lines[i].wl_valid
2231 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2232 {
2233 /* Must have found the last valid entry above mod_bot.
2234 * Add following invalid entries. */
2235 ++i;
2236 while (i < wp->w_lines_valid
2237 && !wp->w_lines[i].wl_valid)
2238 old_rows += wp->w_lines[i++].wl_size;
2239 break;
2240 }
2241#endif
2242 }
2243
2244 if (i >= wp->w_lines_valid)
2245 {
2246 /* We can't find a valid line below the changed lines,
2247 * need to redraw until the end of the window.
2248 * Inserting/deleting lines has no use. */
2249 bot_start = 0;
2250 }
2251 else
2252 {
2253 /* Able to count old number of rows: Count new window
2254 * rows, and may insert/delete lines */
2255 j = idx;
2256 for (l = lnum; l < mod_bot; ++l)
2257 {
2258#ifdef FEAT_FOLDING
2259 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2260 ++new_rows;
2261 else
2262#endif
2263#ifdef FEAT_DIFF
2264 if (l == wp->w_topline)
2265 new_rows += plines_win_nofill(wp, l, TRUE)
2266 + wp->w_topfill;
2267 else
2268#endif
2269 new_rows += plines_win(wp, l, TRUE);
2270 ++j;
2271 if (new_rows > wp->w_height - row - 2)
2272 {
2273 /* it's getting too much, must redraw the rest */
2274 new_rows = 9999;
2275 break;
2276 }
2277 }
2278 xtra_rows = new_rows - old_rows;
2279 if (xtra_rows < 0)
2280 {
2281 /* May scroll text up. If there is not enough
2282 * remaining text or scrolling fails, must redraw the
2283 * rest. If scrolling works, must redraw the text
2284 * below the scrolled text. */
2285 if (row - xtra_rows >= wp->w_height - 2)
2286 mod_bot = MAXLNUM;
2287 else
2288 {
2289 check_for_delay(FALSE);
2290 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002291 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 mod_bot = MAXLNUM;
2293 else
2294 bot_start = wp->w_height + xtra_rows;
2295 }
2296 }
2297 else if (xtra_rows > 0)
2298 {
2299 /* May scroll text down. If there is not enough
2300 * remaining text of scrolling fails, must redraw the
2301 * rest. */
2302 if (row + xtra_rows >= wp->w_height - 2)
2303 mod_bot = MAXLNUM;
2304 else
2305 {
2306 check_for_delay(FALSE);
2307 if (win_ins_lines(wp, row + old_rows,
2308 xtra_rows, FALSE, FALSE) == FAIL)
2309 mod_bot = MAXLNUM;
2310 else if (top_end > row + old_rows)
2311 /* Scrolled the part at the top that requires
2312 * updating down. */
2313 top_end += xtra_rows;
2314 }
2315 }
2316
2317 /* When not updating the rest, may need to move w_lines[]
2318 * entries. */
2319 if (mod_bot != MAXLNUM && i != j)
2320 {
2321 if (j < i)
2322 {
2323 int x = row + new_rows;
2324
2325 /* move entries in w_lines[] upwards */
2326 for (;;)
2327 {
2328 /* stop at last valid entry in w_lines[] */
2329 if (i >= wp->w_lines_valid)
2330 {
2331 wp->w_lines_valid = j;
2332 break;
2333 }
2334 wp->w_lines[j] = wp->w_lines[i];
2335 /* stop at a line that won't fit */
2336 if (x + (int)wp->w_lines[j].wl_size
2337 > wp->w_height)
2338 {
2339 wp->w_lines_valid = j + 1;
2340 break;
2341 }
2342 x += wp->w_lines[j++].wl_size;
2343 ++i;
2344 }
2345 if (bot_start > x)
2346 bot_start = x;
2347 }
2348 else /* j > i */
2349 {
2350 /* move entries in w_lines[] downwards */
2351 j -= i;
2352 wp->w_lines_valid += j;
2353 if (wp->w_lines_valid > wp->w_height)
2354 wp->w_lines_valid = wp->w_height;
2355 for (i = wp->w_lines_valid; i - j >= idx; --i)
2356 wp->w_lines[i] = wp->w_lines[i - j];
2357
2358 /* The w_lines[] entries for inserted lines are
2359 * now invalid, but wl_size may be used above.
2360 * Reset to zero. */
2361 while (i >= idx)
2362 {
2363 wp->w_lines[i].wl_size = 0;
2364 wp->w_lines[i--].wl_valid = FALSE;
2365 }
2366 }
2367 }
2368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370
2371#ifdef FEAT_FOLDING
2372 /*
2373 * When lines are folded, display one line for all of them.
2374 * Otherwise, display normally (can be several display lines when
2375 * 'wrap' is on).
2376 */
2377 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2378 if (fold_count != 0)
2379 {
2380 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2381 ++row;
2382 --fold_count;
2383 wp->w_lines[idx].wl_folded = TRUE;
2384 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2385# ifdef FEAT_SYN_HL
2386 did_update = DID_FOLD;
2387# endif
2388 }
2389 else
2390#endif
2391 if (idx < wp->w_lines_valid
2392 && wp->w_lines[idx].wl_valid
2393 && wp->w_lines[idx].wl_lnum == lnum
2394 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002395 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002396#ifdef FEAT_TEXT_PROP
2397 && !bt_popup(wp->w_buffer)
2398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 && srow + wp->w_lines[idx].wl_size > wp->w_height
2400#ifdef FEAT_DIFF
2401 && diff_check_fill(wp, lnum) == 0
2402#endif
2403 )
2404 {
2405 /* This line is not going to fit. Don't draw anything here,
2406 * will draw "@ " lines below. */
2407 row = wp->w_height + 1;
2408 }
2409 else
2410 {
2411#ifdef FEAT_SEARCH_EXTRA
2412 prepare_search_hl(wp, lnum);
2413#endif
2414#ifdef FEAT_SYN_HL
2415 /* Let the syntax stuff know we skipped a few lines. */
2416 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002417 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 syntax_end_parsing(syntax_last_parsed + 1);
2419#endif
2420
2421 /*
2422 * Display one line.
2423 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002424 row = win_line(wp, lnum, srow, wp->w_height,
2425 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427#ifdef FEAT_FOLDING
2428 wp->w_lines[idx].wl_folded = FALSE;
2429 wp->w_lines[idx].wl_lastlnum = lnum;
2430#endif
2431#ifdef FEAT_SYN_HL
2432 did_update = DID_LINE;
2433 syntax_last_parsed = lnum;
2434#endif
2435 }
2436
2437 wp->w_lines[idx].wl_lnum = lnum;
2438 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002439
2440 /* Past end of the window or end of the screen. Note that after
2441 * resizing wp->w_height may be end up too big. That's a problem
2442 * elsewhere, but prevent a crash here. */
2443 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002446 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2448 ++idx;
2449 break;
2450 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002451 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 wp->w_lines[idx].wl_size = row - srow;
2453 ++idx;
2454#ifdef FEAT_FOLDING
2455 lnum += fold_count + 1;
2456#else
2457 ++lnum;
2458#endif
2459 }
2460 else
2461 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002462 if (wp->w_p_rnu)
2463 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002464#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002465 // 'relativenumber' set: The text doesn't need to be drawn, but
2466 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002467 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2468 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002469 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002470 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002471#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002472 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002473 }
2474
2475 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 row += wp->w_lines[idx++].wl_size;
2477 if (row > wp->w_height) /* past end of screen */
2478 break;
2479#ifdef FEAT_FOLDING
2480 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2481#else
2482 ++lnum;
2483#endif
2484#ifdef FEAT_SYN_HL
2485 did_update = DID_NONE;
2486#endif
2487 }
2488
2489 if (lnum > buf->b_ml.ml_line_count)
2490 {
2491 eof = TRUE;
2492 break;
2493 }
2494 }
2495 /*
2496 * End of loop over all window lines.
2497 */
2498
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002499#ifdef FEAT_VTP
2500 /* Rewrite the character at the end of the screen line. */
2501 if (use_vtp())
2502 {
2503 int i;
2504
2505 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002506 if (enc_utf8)
2507 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2508 LineOffset[i] + screen_Columns) > 1)
2509 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2510 else
2511 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2512 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002513 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2514 }
2515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 if (idx > wp->w_lines_valid)
2518 wp->w_lines_valid = idx;
2519
2520#ifdef FEAT_SYN_HL
2521 /*
2522 * Let the syntax stuff know we stop parsing here.
2523 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002524 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 syntax_end_parsing(syntax_last_parsed + 1);
2526#endif
2527
2528 /*
2529 * If we didn't hit the end of the file, and we didn't finish the last
2530 * line we were working on, then the line didn't fit.
2531 */
2532 wp->w_empty_rows = 0;
2533#ifdef FEAT_DIFF
2534 wp->w_filler_rows = 0;
2535#endif
2536 if (!eof && !didline)
2537 {
2538 if (lnum == wp->w_topline)
2539 {
2540 /*
2541 * Single line that does not fit!
2542 * Don't overwrite it, it can be edited.
2543 */
2544 wp->w_botline = lnum + 1;
2545 }
2546#ifdef FEAT_DIFF
2547 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2548 {
2549 /* Window ends in filler lines. */
2550 wp->w_botline = lnum;
2551 wp->w_filler_rows = wp->w_height - srow;
2552 }
2553#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002554#ifdef FEAT_TEXT_PROP
2555 else if (bt_popup(wp->w_buffer))
2556 {
2557 // popup line that doesn't fit is left as-is
2558 wp->w_botline = lnum;
2559 }
2560#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002561 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2562 {
2563 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2564
2565 /*
2566 * Last line isn't finished: Display "@@@" in the last screen line.
2567 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002568 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002569 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002570 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002571 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002572 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002573 set_empty_rows(wp, srow);
2574 wp->w_botline = lnum;
2575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2577 {
2578 /*
2579 * Last line isn't finished: Display "@@@" at the end.
2580 */
2581 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2582 W_WINROW(wp) + wp->w_height,
2583 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002584 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 set_empty_rows(wp, srow);
2586 wp->w_botline = lnum;
2587 }
2588 else
2589 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002590 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 wp->w_botline = lnum;
2592 }
2593 }
2594 else
2595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (eof) /* we hit the end of the file */
2598 {
2599 wp->w_botline = buf->b_ml.ml_line_count + 1;
2600#ifdef FEAT_DIFF
2601 j = diff_check_fill(wp, wp->w_botline);
2602 if (j > 0 && !wp->w_botfill)
2603 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002604 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (char2cells(fill_diff) > 1)
2606 i = '-';
2607 else
2608 i = fill_diff;
2609 if (row + j > wp->w_height)
2610 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002611 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 row += j;
2613 }
2614#endif
2615 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002616 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 wp->w_botline = lnum;
2618
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002619 // Make sure the rest of the screen is blank
2620 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002621 win_draw_end(wp,
2622#ifdef FEAT_TEXT_PROP
2623 bt_popup(wp->w_buffer) ? ' ' :
2624#endif
2625 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002628#ifdef SYN_TIME_LIMIT
2629 syn_set_timeout(NULL);
2630#endif
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 /* Reset the type of redrawing required, the window has been updated. */
2633 wp->w_redr_type = 0;
2634#ifdef FEAT_DIFF
2635 wp->w_old_topfill = wp->w_topfill;
2636 wp->w_old_botfill = wp->w_botfill;
2637#endif
2638
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002639 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
2641 /*
2642 * There is a trick with w_botline. If we invalidate it on each
2643 * change that might modify it, this will cause a lot of expensive
2644 * calls to plines() in update_topline() each time. Therefore the
2645 * value of w_botline is often approximated, and this value is used to
2646 * compute the value of w_topline. If the value of w_botline was
2647 * wrong, check that the value of w_topline is correct (cursor is on
2648 * the visible part of the text). If it's not, we need to redraw
2649 * again. Mostly this just means scrolling up a few lines, so it
2650 * doesn't look too bad. Only do this for the current window (where
2651 * changes are relevant).
2652 */
2653 wp->w_valid |= VALID_BOTLINE;
2654 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2655 {
2656 recursive = TRUE;
2657 curwin->w_valid &= ~VALID_TOPLINE;
2658 update_topline(); /* may invalidate w_botline again */
2659 if (must_redraw != 0)
2660 {
2661 /* Don't update for changes in buffer again. */
2662 i = curbuf->b_mod_set;
2663 curbuf->b_mod_set = FALSE;
2664 win_update(curwin);
2665 must_redraw = 0;
2666 curbuf->b_mod_set = i;
2667 }
2668 recursive = FALSE;
2669 }
2670 }
2671
2672#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2673 /* restore got_int, unless CTRL-C was hit while redrawing */
2674 if (!got_int)
2675 got_int = save_got_int;
2676#endif
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002680 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2681 * Return the new offset.
2682 */
2683 static int
2684screen_fill_end(
2685 win_T *wp,
2686 int c1,
2687 int c2,
2688 int off,
2689 int width,
2690 int row,
2691 int endrow,
2692 int attr)
2693{
2694 int nn = off + width;
2695
2696 if (nn > wp->w_width)
2697 nn = wp->w_width;
2698#ifdef FEAT_RIGHTLEFT
2699 if (wp->w_p_rl)
2700 {
2701 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2702 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2703 c1, c2, attr);
2704 }
2705 else
2706#endif
2707 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2708 wp->w_wincol + off, (int)wp->w_wincol + nn,
2709 c1, c2, attr);
2710 return nn;
2711}
2712
2713/*
2714 * Clear lines near the end the window and mark the unused lines with "c1".
2715 * use "c2" as the filler character.
2716 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 */
2718 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002719win_draw_end(
2720 win_T *wp,
2721 int c1,
2722 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002723 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002724 int row,
2725 int endrow,
2726 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002729 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002730 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002731
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002732 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002733
2734 if (draw_margin)
2735 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002736#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002737 int fdc = compute_foldcolumn(wp, 0);
2738
2739 if (fdc > 0)
2740 // draw the fold column
2741 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002742 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002743#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002744#ifdef FEAT_SIGNS
2745 if (signcolumn_on(wp))
2746 // draw the sign column
2747 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002748 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002749#endif
2750 if ((wp->w_p_nu || wp->w_p_rnu)
2751 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2752 // draw the number column
2753 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002754 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757#ifdef FEAT_RIGHTLEFT
2758 if (wp->w_p_rl)
2759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002761 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002762 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002764 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002765 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767 else
2768#endif
2769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002771 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002772 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 set_empty_rows(wp, row);
2776}
2777
Bram Moolenaar1a384422010-07-14 19:53:30 +02002778#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002779/*
2780 * Advance **color_cols and return TRUE when there are columns to draw.
2781 */
2782 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002783advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002784{
2785 while (**color_cols >= 0 && vcol > **color_cols)
2786 ++*color_cols;
2787 return (**color_cols >= 0);
2788}
2789#endif
2790
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002791#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2792/*
2793 * Copy "text" to ScreenLines using "attr".
2794 * Returns the next screen column.
2795 */
2796 static int
2797text_to_screenline(win_T *wp, char_u *text, int col)
2798{
2799 int off = (int)(current_ScreenLine - ScreenLines);
2800
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002801 if (has_mbyte)
2802 {
2803 int cells;
2804 int u8c, u8cc[MAX_MCO];
2805 int i;
2806 int idx;
2807 int c_len;
2808 char_u *p;
2809# ifdef FEAT_ARABIC
2810 int prev_c = 0; /* previous Arabic character */
2811 int prev_c1 = 0; /* first composing char for prev_c */
2812# endif
2813
2814# ifdef FEAT_RIGHTLEFT
2815 if (wp->w_p_rl)
2816 idx = off;
2817 else
2818# endif
2819 idx = off + col;
2820
2821 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2822 for (p = text; *p != NUL; )
2823 {
2824 cells = (*mb_ptr2cells)(p);
2825 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002826 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002827# ifdef FEAT_RIGHTLEFT
2828 - (wp->w_p_rl ? col : 0)
2829# endif
2830 )
2831 break;
2832 ScreenLines[idx] = *p;
2833 if (enc_utf8)
2834 {
2835 u8c = utfc_ptr2char(p, u8cc);
2836 if (*p < 0x80 && u8cc[0] == 0)
2837 {
2838 ScreenLinesUC[idx] = 0;
2839#ifdef FEAT_ARABIC
2840 prev_c = u8c;
2841#endif
2842 }
2843 else
2844 {
2845#ifdef FEAT_ARABIC
2846 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2847 {
2848 /* Do Arabic shaping. */
2849 int pc, pc1, nc;
2850 int pcc[MAX_MCO];
2851 int firstbyte = *p;
2852
2853 /* The idea of what is the previous and next
2854 * character depends on 'rightleft'. */
2855 if (wp->w_p_rl)
2856 {
2857 pc = prev_c;
2858 pc1 = prev_c1;
2859 nc = utf_ptr2char(p + c_len);
2860 prev_c1 = u8cc[0];
2861 }
2862 else
2863 {
2864 pc = utfc_ptr2char(p + c_len, pcc);
2865 nc = prev_c;
2866 pc1 = pcc[0];
2867 }
2868 prev_c = u8c;
2869
2870 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2871 pc, pc1, nc);
2872 ScreenLines[idx] = firstbyte;
2873 }
2874 else
2875 prev_c = u8c;
2876#endif
2877 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002878 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002879 for (i = 0; i < Screen_mco; ++i)
2880 {
2881 ScreenLinesC[i][idx] = u8cc[i];
2882 if (u8cc[i] == 0)
2883 break;
2884 }
2885 }
2886 if (cells > 1)
2887 ScreenLines[idx + 1] = 0;
2888 }
2889 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2890 /* double-byte single width character */
2891 ScreenLines2[idx] = p[1];
2892 else if (cells > 1)
2893 /* double-width character */
2894 ScreenLines[idx + 1] = p[1];
2895 col += cells;
2896 idx += cells;
2897 p += c_len;
2898 }
2899 }
2900 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002901 {
2902 int len = (int)STRLEN(text);
2903
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 if (len > wp->w_width - col)
2905 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002906 if (len > 0)
2907 {
2908#ifdef FEAT_RIGHTLEFT
2909 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002910 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002911 else
2912#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002913 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002914 col += len;
2915 }
2916 }
2917 return col;
2918}
2919#endif
2920
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921#ifdef FEAT_FOLDING
2922/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002923 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2924 * space is available for window "wp", minus "col".
2925 */
2926 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002927compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002928{
2929 int fdc = wp->w_p_fdc;
2930 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002931 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002932
2933 if (fdc > wwidth - (col + wmw))
2934 fdc = wwidth - (col + wmw);
2935 return fdc;
2936}
2937
2938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 * Display one folded line.
2940 */
2941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002942fold_line(
2943 win_T *wp,
2944 long fold_count,
2945 foldinfo_T *foldinfo,
2946 linenr_T lnum,
2947 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002949 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 pos_T *top, *bot;
2951 linenr_T lnume = lnum + fold_count - 1;
2952 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002953 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 int col;
2956 int txtcol;
2957 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002958 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 /* Build the fold line:
2961 * 1. Add the cmdwin_type for the command-line window
2962 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002963 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 * 4. Compose the text
2965 * 5. Add the text
2966 * 6. set highlighting for the Visual area an other text
2967 */
2968 col = 0;
2969
2970 /*
2971 * 1. Add the cmdwin_type for the command-line window
2972 * Ignores 'rightleft', this window is never right-left.
2973 */
2974#ifdef FEAT_CMDWIN
2975 if (cmdwin_type != 0 && wp == curwin)
2976 {
2977 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002978 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (enc_utf8)
2980 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 ++col;
2982 }
2983#endif
2984
2985 /*
2986 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002987 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002989 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (fdc > 0)
2991 {
2992 fill_foldcolumn(buf, wp, TRUE, lnum);
2993#ifdef FEAT_RIGHTLEFT
2994 if (wp->w_p_rl)
2995 {
2996 int i;
2997
Bram Moolenaar02631462017-09-22 15:20:32 +02002998 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002999 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 /* reverse the fold column */
3001 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02003002 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 else
3005#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01003006 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 col += fdc;
3008 }
3009
3010#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02003011# define RL_MEMSET(p, v, l) \
3012 do { \
3013 if (wp->w_p_rl) \
3014 for (ri = 0; ri < l; ++ri) \
3015 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
3016 else \
3017 for (ri = 0; ri < l; ++ri) \
3018 ScreenAttrs[off + (p) + ri] = v; \
3019 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02003021# define RL_MEMSET(p, v, l) \
3022 do { \
3023 for (ri = 0; ri < l; ++ri) \
3024 ScreenAttrs[off + (p) + ri] = v; \
3025 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026#endif
3027
Bram Moolenaar64486672010-05-16 15:46:46 +02003028 /* Set all attributes of the 'number' or 'relativenumber' column and the
3029 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003030 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032#ifdef FEAT_SIGNS
3033 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003034 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003036 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 if (len > 0)
3038 {
3039 if (len > 2)
3040 len = 2;
3041# ifdef FEAT_RIGHTLEFT
3042 if (wp->w_p_rl)
3043 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02003044 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003045 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 else
3047# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01003048 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 col += len;
3050 }
3051 }
3052#endif
3053
3054 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02003055 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 */
Bram Moolenaar64486672010-05-16 15:46:46 +02003057 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
Bram Moolenaar02631462017-09-22 15:20:32 +02003059 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (len > 0)
3061 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003062 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02003063 long num;
3064 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003065
3066 if (len > w + 1)
3067 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02003068
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003069 if (wp->w_p_nu && !wp->w_p_rnu)
3070 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003071 num = (long)lnum;
3072 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003073 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003074 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003075 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003076 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003077 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003078 /* 'number' + 'relativenumber': cursor line shows absolute
3079 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003080 num = lnum;
3081 fmt = "%-*ld ";
3082 }
3083 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003084
Bram Moolenaar700e7342013-01-30 12:31:36 +01003085 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086#ifdef FEAT_RIGHTLEFT
3087 if (wp->w_p_rl)
3088 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02003089 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01003090 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
3092#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01003093 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 col += len;
3095 }
3096 }
3097
3098 /*
3099 * 4. Compose the folded-line string with 'foldtext', if set.
3100 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003101 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102
3103 txtcol = col; /* remember where text starts */
3104
3105 /*
3106 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
3107 * Right-left text is put in columns 0 - number-col, normal text is put
3108 * in columns number-col - window-width.
3109 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02003110 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111
3112 /* Fill the rest of the line with the fold filler */
3113#ifdef FEAT_RIGHTLEFT
3114 if (wp->w_p_rl)
3115 col -= txtcol;
3116#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02003117 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#ifdef FEAT_RIGHTLEFT
3119 - (wp->w_p_rl ? txtcol : 0)
3120#endif
3121 )
3122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (enc_utf8)
3124 {
3125 if (fill_fold >= 0x80)
3126 {
3127 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003128 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01003129 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003132 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003134 ScreenLines[off + col] = fill_fold;
3135 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003136 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003138 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003139 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
3141
3142 if (text != buf)
3143 vim_free(text);
3144
3145 /*
3146 * 6. set highlighting for the Visual area an other text.
3147 * If all folded lines are in the Visual area, highlight the line.
3148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3150 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003151 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
3153 /* Visual is after curwin->w_cursor */
3154 top = &curwin->w_cursor;
3155 bot = &VIsual;
3156 }
3157 else
3158 {
3159 /* Visual is before curwin->w_cursor */
3160 top = &VIsual;
3161 bot = &curwin->w_cursor;
3162 }
3163 if (lnum >= top->lnum
3164 && lnume <= bot->lnum
3165 && (VIsual_mode != 'v'
3166 || ((lnum > top->lnum
3167 || (lnum == top->lnum
3168 && top->col == 0))
3169 && (lnume < bot->lnum
3170 || (lnume == bot->lnum
3171 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003172 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 {
3174 if (VIsual_mode == Ctrl_V)
3175 {
3176 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003177 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003179 if (wp->w_old_cursor_lcol != MAXCOL
3180 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003181 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 len = wp->w_old_cursor_lcol;
3183 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003184 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003185 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003186 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
3188 }
3189 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003192 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 }
3195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003197#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003198 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3199 if (wp->w_p_cc_cols)
3200 {
3201 int i = 0;
3202 int j = wp->w_p_cc_cols[i];
3203 int old_txtcol = txtcol;
3204
3205 while (j > -1)
3206 {
3207 txtcol += j;
3208 if (wp->w_p_wrap)
3209 txtcol -= wp->w_skipcol;
3210 else
3211 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003212 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003213 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003214 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003215 txtcol = old_txtcol;
3216 j = wp->w_p_cc_cols[++i];
3217 }
3218 }
3219
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003220 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003221 if (wp->w_p_cuc)
3222 {
3223 txtcol += wp->w_virtcol;
3224 if (wp->w_p_wrap)
3225 txtcol -= wp->w_skipcol;
3226 else
3227 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003228 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003229 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003230 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003231 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
Bram Moolenaar02631462017-09-22 15:20:32 +02003234 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003235 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
3237 /*
3238 * Update w_cline_height and w_cline_folded if the cursor line was
3239 * updated (saves a call to plines() later).
3240 */
3241 if (wp == curwin
3242 && lnum <= curwin->w_cursor.lnum
3243 && lnume >= curwin->w_cursor.lnum)
3244 {
3245 curwin->w_cline_row = row;
3246 curwin->w_cline_height = 1;
3247 curwin->w_cline_folded = TRUE;
3248 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3249 }
3250}
3251
3252/*
3253 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3254 */
3255 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003256copy_text_attr(
3257 int off,
3258 char_u *buf,
3259 int len,
3260 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003262 int i;
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (enc_utf8)
3266 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003267 for (i = 0; i < len; ++i)
3268 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269}
3270
3271/*
3272 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003273 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 */
3275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003276fill_foldcolumn(
3277 char_u *p,
3278 win_T *wp,
3279 int closed, /* TRUE of FALSE */
3280 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 int i = 0;
3283 int level;
3284 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003285 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003286 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003289 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 level = win_foldinfo.fi_level;
3292 if (level > 0)
3293 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003294 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003295 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 /* If the column is too narrow, we start at the lowest level that
3298 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003299 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 if (first_level < 1)
3301 first_level = 1;
3302
Bram Moolenaar1c934292015-01-27 16:39:29 +01003303 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 if (win_foldinfo.fi_lnum == lnum
3306 && first_level + i >= win_foldinfo.fi_low_level)
3307 p[i] = '-';
3308 else if (first_level == 1)
3309 p[i] = '|';
3310 else if (first_level + i <= 9)
3311 p[i] = '0' + first_level + i;
3312 else
3313 p[i] = '>';
3314 if (first_level + i == level)
3315 break;
3316 }
3317 }
3318 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003319 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321#endif /* FEAT_FOLDING */
3322
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003323#ifdef FEAT_TEXT_PROP
3324static textprop_T *current_text_props = NULL;
3325static buf_T *current_buf = NULL;
3326
3327 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003328text_prop_compare(const void *s1, const void *s2)
3329{
3330 int idx1, idx2;
3331 proptype_T *pt1, *pt2;
3332 colnr_T col1, col2;
3333
3334 idx1 = *(int *)s1;
3335 idx2 = *(int *)s2;
3336 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3337 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3338 if (pt1 == pt2)
3339 return 0;
3340 if (pt1 == NULL)
3341 return -1;
3342 if (pt2 == NULL)
3343 return 1;
3344 if (pt1->pt_priority != pt2->pt_priority)
3345 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3346 col1 = current_text_props[idx1].tp_col;
3347 col2 = current_text_props[idx2].tp_col;
3348 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3349}
3350#endif
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352/*
3353 * Display line "lnum" of window 'wp' on the screen.
3354 * Start at row "startrow", stop when "endrow" is reached.
3355 * wp->w_virtcol needs to be valid.
3356 *
3357 * Return the number of last row the line occupies.
3358 */
3359 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003360win_line(
3361 win_T *wp,
3362 linenr_T lnum,
3363 int startrow,
3364 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 int nochange UNUSED, // not updating for changed text
3366 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003368 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3370 int c = 0; /* init for GCC */
3371 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003372#ifdef FEAT_LINEBREAK
3373 long vcol_sbr = -1; /* virtual column after showbreak */
3374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 long vcol_prev = -1; /* "vcol" of previous character */
3376 char_u *line; /* current line */
3377 char_u *ptr; /* current position in "line" */
3378 int row; /* row in the window, excl w_winrow */
3379 int screen_row; /* row on the screen, incl w_winrow */
3380
3381 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3382 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003383 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003384 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003386 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int extra_attr = 0; /* attributes when n_extra != 0 */
3388 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3389 displaying lcs_eol at end-of-line */
3390 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3391 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3392
3393 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3394 int saved_n_extra = 0;
3395 char_u *saved_p_extra = NULL;
3396 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003397 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 int saved_char_attr = 0;
3399
3400 int n_attr = 0; /* chars with special attr */
3401 int saved_attr2 = 0; /* char_attr saved for n_attr */
3402 int n_attr3 = 0; /* chars with overruling special attr */
3403 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3404
3405 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3406
3407 int fromcol, tocol; /* start/end of inverting */
3408 int fromcol_prev = -2; /* start of inverting after cursor */
3409 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003411 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 pos_T pos;
3413 long v;
3414
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003415 int char_attr = 0; // attributes for next character
3416 int attr_pri = FALSE; // char_attr has priority
3417 int area_highlighting = FALSE; // Visual or incsearch highlighting
3418 // in this line
3419 int vi_attr = 0; // attributes for Visual and incsearch
3420 // highlighting
3421 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003422 int win_attr = 0; // background for whole window, except
3423 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003424 int area_attr = 0; // attributes desired by highlighting
3425 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003427 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 int syntax_attr = 0; /* attributes desired by syntax */
3429 int has_syntax = FALSE; /* this buffer has syntax highl. */
3430 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003431 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003432 int draw_color_col = FALSE; /* highlight colorcolumn */
3433 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003434#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003435#ifdef FEAT_TEXT_PROP
3436 int text_prop_count;
3437 int text_prop_next = 0; // next text property to use
3438 textprop_T *text_props = NULL;
3439 int *text_prop_idxs = NULL;
3440 int text_props_active = 0;
3441 proptype_T *text_prop_type = NULL;
3442 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003443 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003444#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003445#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003446 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003447# define SPWORDLEN 150
3448 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003449 int nextlinecol = 0; /* column where nextline[] starts */
3450 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003451 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003452 int spell_attr = 0; /* attributes desired by spelling */
3453 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003454 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3455 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003456 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003457 static int cap_col = -1; /* column to check for Cap word */
3458 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003459 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003461 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int multi_attr = 0; /* attributes desired by multibyte */
3463 int mb_l = 1; /* multi-byte byte length */
3464 int mb_c = 0; /* decoded multi-byte character */
3465 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003466 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467#ifdef FEAT_DIFF
3468 int filler_lines; /* nr of filler lines to be drawn */
3469 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003470 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 int change_start = MAXCOL; /* first col of changed area */
3472 int change_end = -1; /* last col of changed area */
3473#endif
3474 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3475#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003476 int need_showbreak = FALSE; /* overlong line, skipping first x
3477 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003479#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003480 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003482 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483#endif
3484#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003485 matchitem_T *cur; /* points to the match list */
3486 match_T *shl; /* points to search_hl or a match */
3487 int shl_flag; /* flag to indicate whether search_hl
3488 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003489 int pos_inprogress; /* marks that position match search is
3490 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003491 int prevcol_hl_flag; /* flag to indicate whether prevcol
3492 equals startcol of search_hl or one
3493 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494#endif
3495#ifdef FEAT_ARABIC
3496 int prev_c = 0; /* previous Arabic character */
3497 int prev_c1 = 0; /* first composing char for prev_c */
3498#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003499#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003500 int did_line_attr = 0;
3501#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003502#ifdef FEAT_TERMINAL
3503 int get_term_attr = FALSE;
3504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 /* draw_state: items that are drawn in sequence: */
3507#define WL_START 0 /* nothing done yet */
3508#ifdef FEAT_CMDWIN
3509# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3510#else
3511# define WL_CMDLINE WL_START
3512#endif
3513#ifdef FEAT_FOLDING
3514# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3515#else
3516# define WL_FOLD WL_CMDLINE
3517#endif
3518#ifdef FEAT_SIGNS
3519# define WL_SIGN WL_FOLD + 1 /* column for signs */
3520#else
3521# define WL_SIGN WL_FOLD /* column for signs */
3522#endif
3523#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003524#ifdef FEAT_LINEBREAK
3525# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003527# define WL_BRI WL_NR
3528#endif
3529#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3530# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3531#else
3532# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#endif
3534#define WL_LINE WL_SBR + 1 /* text in the line */
3535 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003536#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 int feedback_col = 0;
3538 int feedback_old_attr = -1;
3539#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003540 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaar860cae12010-06-05 23:22:07 +02003542#ifdef FEAT_CONCEAL
3543 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003544 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003545 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003546 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003547 int is_concealing = FALSE;
3548 int boguscols = 0; /* nonexistent columns added to force
3549 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003550 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003551 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003552 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003553 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003554# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003555# define FIX_FOR_BOGUSCOLS \
3556 { \
3557 n_extra += vcol_off; \
3558 vcol -= vcol_off; \
3559 vcol_off = 0; \
3560 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003561 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003562 boguscols = 0; \
3563 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003564#else
3565# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 if (startrow > endrow) /* past the end already! */
3569 return startrow;
3570
3571 row = startrow;
3572 screen_row = row + W_WINROW(wp);
3573
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003574 if (!number_only)
3575 {
3576 /*
3577 * To speed up the loop below, set extra_check when there is linebreak,
3578 * trailing white space and/or syntax processing to be done.
3579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003581 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582#endif
3583#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003584 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003585# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003586 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003587# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003588 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003590 /* Prepare for syntax highlighting in this line. When there is an
3591 * error, stop syntax highlighting. */
3592 save_did_emsg = did_emsg;
3593 did_emsg = FALSE;
3594 syntax_start(wp, lnum);
3595 if (did_emsg)
3596 wp->w_s->b_syn_error = TRUE;
3597 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003598 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003599 did_emsg = save_did_emsg;
3600#ifdef SYN_TIME_LIMIT
3601 if (!wp->w_s->b_syn_slow)
3602#endif
3603 {
3604 has_syntax = TRUE;
3605 extra_check = TRUE;
3606 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003609
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003610 /* Check for columns to display for 'colorcolumn'. */
3611 color_cols = wp->w_p_cc_cols;
3612 if (color_cols != NULL)
3613 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003614#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003615
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003616#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003617 if (term_show_buffer(wp->w_buffer))
3618 {
3619 extra_check = TRUE;
3620 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003621 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003622 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003623#endif
3624
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003625#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003626 if (wp->w_p_spell
3627 && *wp->w_s->b_p_spl != NUL
3628 && wp->w_s->b_langp.ga_len > 0
3629 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003630 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003631 /* Prepare for spell checking. */
3632 has_spell = TRUE;
3633 extra_check = TRUE;
3634
Bram Moolenaar7701f302018-10-02 21:20:32 +02003635 /* Get the start of the next line, so that words that wrap to the
3636 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003637 * Trick: skip a few chars for C/shell/Vim comments */
3638 nextline[SPWORDLEN] = NUL;
3639 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3640 {
3641 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3642 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3643 }
3644
Bram Moolenaar7701f302018-10-02 21:20:32 +02003645 /* When a word wrapped from the previous line the start of the
3646 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003647 if (lnum == checked_lnum)
3648 cur_checked_col = checked_col;
3649 checked_lnum = 0;
3650
3651 /* When there was a sentence end in the previous line may require a
3652 * word starting with capital in this line. In line 1 always check
3653 * the first word. */
3654 if (lnum != capcol_lnum)
3655 cap_col = -1;
3656 if (lnum == 1)
3657 cap_col = 0;
3658 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#endif
3661
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003662 /*
3663 * handle visual active in this window
3664 */
3665 fromcol = -10;
3666 tocol = MAXCOL;
3667 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003669 /* Visual is after curwin->w_cursor */
3670 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003672 top = &curwin->w_cursor;
3673 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003675 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003677 top = &VIsual;
3678 bot = &curwin->w_cursor;
3679 }
3680 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3681 if (VIsual_mode == Ctrl_V) /* block mode */
3682 {
3683 if (lnum_in_visual_area)
3684 {
3685 fromcol = wp->w_old_cursor_fcol;
3686 tocol = wp->w_old_cursor_lcol;
3687 }
3688 }
3689 else /* non-block mode */
3690 {
3691 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003693 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003695 if (VIsual_mode == 'V') /* linewise */
3696 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 else
3698 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003699 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3700 if (gchar_pos(top) == NUL)
3701 tocol = fromcol + 1;
3702 }
3703 }
3704 if (VIsual_mode != 'V' && lnum == bot->lnum)
3705 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003706 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003707 {
3708 fromcol = -10;
3709 tocol = MAXCOL;
3710 }
3711 else if (bot->col == MAXCOL)
3712 tocol = MAXCOL;
3713 else
3714 {
3715 pos = *bot;
3716 if (*p_sel == 'e')
3717 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3718 else
3719 {
3720 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3721 ++tocol;
3722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724 }
3725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003727 /* Check if the character under the cursor should not be inverted */
3728 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003729#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003730 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003731#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003732 )
3733 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003735 /* if inverting in this line set area_highlighting */
3736 if (fromcol >= 0)
3737 {
3738 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003739 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003741 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003742 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003743 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003744 && clip_isautosel_plus()))
3745 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003750 /*
3751 * handle 'incsearch' and ":s///c" highlighting
3752 */
3753 else if (highlight_match
3754 && wp == curwin
3755 && lnum >= curwin->w_cursor.lnum
3756 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003758 if (lnum == curwin->w_cursor.lnum)
3759 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003760 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003761 else
3762 fromcol = 0;
3763 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3764 {
3765 pos.lnum = lnum;
3766 pos.col = search_match_endcol;
3767 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3768 }
3769 else
3770 tocol = MAXCOL;
3771 /* do at least one character; happens when past end of line */
3772 if (fromcol == tocol)
3773 tocol = fromcol + 1;
3774 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003775 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778
3779#ifdef FEAT_DIFF
3780 filler_lines = diff_check(wp, lnum);
3781 if (filler_lines < 0)
3782 {
3783 if (filler_lines == -1)
3784 {
3785 if (diff_find_change(wp, lnum, &change_start, &change_end))
3786 diff_hlf = HLF_ADD; /* added line */
3787 else if (change_start == 0)
3788 diff_hlf = HLF_TXD; /* changed text */
3789 else
3790 diff_hlf = HLF_CHD; /* changed line */
3791 }
3792 else
3793 diff_hlf = HLF_ADD; /* added line */
3794 filler_lines = 0;
3795 area_highlighting = TRUE;
3796 }
3797 if (lnum == wp->w_topline)
3798 filler_lines = wp->w_topfill;
3799 filler_todo = filler_lines;
3800#endif
3801
3802#ifdef LINE_ATTR
3803# ifdef FEAT_SIGNS
3804 /* If this line has a sign with line highlighting set line_attr. */
3805 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3806 if (v != 0)
3807 line_attr = sign_get_attr((int)v, TRUE);
3808# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003809# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003811 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003812 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813# endif
3814 if (line_attr != 0)
3815 area_highlighting = TRUE;
3816#endif
3817
3818 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3819 ptr = line;
3820
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003821#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003822 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003823 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003824 /* For checking first word with a capital skip white space. */
3825 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003826 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003827
Bram Moolenaar30abd282005-06-22 22:35:10 +00003828 /* To be able to spell-check over line boundaries copy the end of the
3829 * current line into nextline[]. Above the start of the next line was
3830 * copied to nextline[SPWORDLEN]. */
3831 if (nextline[SPWORDLEN] == NUL)
3832 {
3833 /* No next line or it is empty. */
3834 nextlinecol = MAXCOL;
3835 nextline_idx = 0;
3836 }
3837 else
3838 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003839 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003840 if (v < SPWORDLEN)
3841 {
3842 /* Short line, use it completely and append the start of the
3843 * next line. */
3844 nextlinecol = 0;
3845 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003846 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003847 nextline_idx = v + 1;
3848 }
3849 else
3850 {
3851 /* Long line, use only the last SPWORDLEN bytes. */
3852 nextlinecol = v - SPWORDLEN;
3853 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3854 nextline_idx = SPWORDLEN + 1;
3855 }
3856 }
3857 }
3858#endif
3859
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003860 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003862 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003863 extra_check = TRUE;
3864 /* find start of trailing whitespace */
3865 if (lcs_trail)
3866 {
3867 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003868 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003869 --trailcol;
3870 trailcol += (colnr_T) (ptr - line);
3871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003874 wcr_attr = get_wcr_attr(wp);
3875 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003876 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003877 win_attr = wcr_attr;
3878 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003879 }
3880#ifdef FEAT_TEXT_PROP
3881 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003882 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003883#endif
3884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 /*
3886 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3887 * first character to be displayed.
3888 */
3889 if (wp->w_p_wrap)
3890 v = wp->w_skipcol;
3891 else
3892 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003893 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 while (vcol < v && *ptr != NUL)
3898 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003899 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003902 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003905 /* When:
3906 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003907 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003908 * - 'virtualedit' is set, or
3909 * - the visual mode is active,
3910 * the end of the line may be before the start of the displayed part.
3911 */
3912 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003913#ifdef FEAT_SYN_HL
3914 wp->w_p_cuc || draw_color_col ||
3915#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003916 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003917 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 /* Handle a character that's not completely on the screen: Put ptr at
3921 * that character but skip the first few screen characters. */
3922 if (vcol > v)
3923 {
3924 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003926 /* If the character fits on the screen, don't need to skip it.
3927 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003928 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003929 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931
3932 /*
3933 * Adjust for when the inverted text is before the screen,
3934 * and when the start of the inverted text is before the screen.
3935 */
3936 if (tocol <= vcol)
3937 fromcol = 0;
3938 else if (fromcol >= 0 && fromcol < vcol)
3939 fromcol = vcol;
3940
3941#ifdef FEAT_LINEBREAK
3942 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3943 if (wp->w_p_wrap)
3944 need_showbreak = TRUE;
3945#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003946#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003947 /* When spell checking a word we need to figure out the start of the
3948 * word and if it's badly spelled or not. */
3949 if (has_spell)
3950 {
3951 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003952 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003953 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003954
3955 pos = wp->w_cursor;
3956 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003957 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003958 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003959
3960 /* spell_move_to() may call ml_get() and make "line" invalid */
3961 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3962 ptr = line + linecol;
3963
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003964 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003965 {
3966 /* no bad word found at line start, don't check until end of a
3967 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003968 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003969 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003970 }
3971 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003972 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003973 /* bad word found, use attributes until end of word */
3974 word_end = wp->w_cursor.col + len + 1;
3975
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003976 /* Turn index into actual attributes. */
3977 if (spell_hlf != HLF_COUNT)
3978 spell_attr = highlight_attr[spell_hlf];
3979 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003980 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003981
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003982# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003983 /* Need to restart syntax highlighting for this line. */
3984 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003985 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003986# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003987 }
3988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990
3991 /*
3992 * Correct highlighting for cursor that can't be disabled.
3993 * Avoids having to check this for each character.
3994 */
3995 if (fromcol >= 0)
3996 {
3997 if (noinvcur)
3998 {
3999 if ((colnr_T)fromcol == wp->w_virtcol)
4000 {
4001 /* highlighting starts at cursor, let it start just after the
4002 * cursor */
4003 fromcol_prev = fromcol;
4004 fromcol = -1;
4005 }
4006 else if ((colnr_T)fromcol < wp->w_virtcol)
4007 /* restart highlighting after the cursor */
4008 fromcol_prev = wp->w_virtcol;
4009 }
4010 if (fromcol >= tocol)
4011 fromcol = -1;
4012 }
4013
4014#ifdef FEAT_SEARCH_EXTRA
4015 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004016 * Handle highlighting the last used search pattern and matches.
4017 * Do this for both search_hl and the match list.
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004018 * Do not use search_hl in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004020 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004021 shl_flag = (screen_line_flags & SLF_POPUP);
4022 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004024 if (shl_flag == FALSE)
4025 {
4026 shl = &search_hl;
4027 shl_flag = TRUE;
4028 }
4029 else
4030 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004031 shl->startcol = MAXCOL;
4032 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02004034 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004035 v = (long)(ptr - line);
4036 if (cur != NULL)
4037 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004038 next_search_hl(wp, shl, lnum, (colnr_T)v,
4039 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004040
4041 /* Need to get the line again, a multi-line regexp may have made it
4042 * invalid. */
4043 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4044 ptr = line + v;
4045
4046 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02004048 if (shl->lnum == lnum)
4049 shl->startcol = shl->rm.startpos[0].col;
4050 else
4051 shl->startcol = 0;
4052 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
4053 - shl->rm.startpos[0].lnum)
4054 shl->endcol = shl->rm.endpos[0].col;
4055 else
4056 shl->endcol = MAXCOL;
4057 /* Highlight one character for an empty match. */
4058 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02004060 if (has_mbyte && line[shl->endcol] != NUL)
4061 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
4062 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02004063 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02004065 if ((long)shl->startcol < v) /* match at leftcol */
4066 {
4067 shl->attr_cur = shl->attr;
4068 search_attr = shl->attr;
4069 }
4070 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004072 if (shl != &search_hl && cur != NULL)
4073 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075#endif
4076
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004077#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004078 // Cursor line highlighting for 'cursorline' in the current window.
4079 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004080 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01004081 // Do not show the cursor line when Visual mode is active, because it's
4082 // not clear what is selected then. Do update w_last_cursorline.
4083 if (!(wp == curwin && VIsual_active))
4084 {
4085 line_attr = HL_ATTR(HLF_CUL);
4086 area_highlighting = TRUE;
4087 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01004088 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004089 }
4090#endif
4091
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004092#ifdef FEAT_TEXT_PROP
4093 {
4094 char_u *prop_start;
4095
4096 text_prop_count = get_text_props(wp->w_buffer, lnum,
4097 &prop_start, FALSE);
4098 if (text_prop_count > 0)
4099 {
4100 // Make a copy of the properties, so that they are properly
4101 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004102 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004103 if (text_props != NULL)
4104 mch_memmove(text_props, prop_start,
4105 text_prop_count * sizeof(textprop_T));
4106
4107 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004108 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004109 area_highlighting = TRUE;
4110 extra_check = TRUE;
4111 }
4112 }
4113#endif
4114
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004115 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118#ifdef FEAT_RIGHTLEFT
4119 if (wp->w_p_rl)
4120 {
4121 /* Rightleft window: process the text in the normal direction, but put
4122 * it in current_ScreenLine[] from right to left. Start at the
4123 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02004124 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004126 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128#endif
4129
4130 /*
4131 * Repeat for the whole displayed line.
4132 */
4133 for (;;)
4134 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02004135#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004136 int has_match_conc = 0; // match wants to conceal
4137 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 /* Skip this quickly when working on the text. */
4140 if (draw_state != WL_LINE)
4141 {
4142#ifdef FEAT_CMDWIN
4143 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4144 {
4145 draw_state = WL_CMDLINE;
4146 if (cmdwin_type != 0 && wp == curwin)
4147 {
4148 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004150 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004151 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004152 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154 }
4155#endif
4156
4157#ifdef FEAT_FOLDING
4158 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4159 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004160 int fdc = compute_foldcolumn(wp, 0);
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004163 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004165 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004166 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004167 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004168 p_extra_free = alloc(12 + 1);
4169
4170 if (p_extra_free != NULL)
4171 {
4172 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4173 n_extra = fdc;
4174 p_extra_free[n_extra] = NUL;
4175 p_extra = p_extra_free;
4176 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004177 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004178 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 }
4182#endif
4183
4184#ifdef FEAT_SIGNS
4185 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4186 {
4187 draw_state = WL_SIGN;
4188 /* Show the sign column when there are any signs in this
4189 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004190 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004192 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004194 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195# endif
4196
4197 /* Draw two cells with the sign value or blank. */
4198 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004199 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004200 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 n_extra = 2;
4202
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004203 if (row == startrow
4204#ifdef FEAT_DIFF
4205 + filler_lines && filler_todo <= 0
4206#endif
4207 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4210 SIGN_TEXT);
4211# ifdef FEAT_SIGN_ICONS
4212 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4213 SIGN_ICON);
4214 if (gui.in_use && icon_sign != 0)
4215 {
4216 /* Use the image in this position. */
4217 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004218 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219# ifdef FEAT_NETBEANS_INTG
4220 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004223 c_final = NUL;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225# endif
4226 char_attr = icon_sign;
4227 }
4228 else
4229# endif
4230 if (text_sign != 0)
4231 {
4232 p_extra = sign_get_text(text_sign);
4233 if (p_extra != NULL)
4234 {
4235 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004236 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004237 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 char_attr = sign_get_attr(text_sign, FALSE);
4240 }
4241 }
4242 }
4243 }
4244#endif
4245
4246 if (draw_state == WL_NR - 1 && n_extra == 0)
4247 {
4248 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004249 /* Display the absolute or relative line number. After the
4250 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4251 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 && (row == startrow
4253#ifdef FEAT_DIFF
4254 + filler_lines
4255#endif
4256 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4257 {
4258 /* Draw the line number (empty space after wrapping). */
4259 if (row == startrow
4260#ifdef FEAT_DIFF
4261 + filler_lines
4262#endif
4263 )
4264 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004265 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004266 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004267
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004268 if (wp->w_p_nu && !wp->w_p_rnu)
4269 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004270 num = (long)lnum;
4271 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004272 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004273 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004274 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004275 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004276 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004277 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004278 num = lnum;
4279 fmt = "%-*ld ";
4280 }
4281 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004282
Bram Moolenaar700e7342013-01-30 12:31:36 +01004283 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004284 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if (wp->w_skipcol > 0)
4286 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4287 *p_extra = '-';
4288#ifdef FEAT_RIGHTLEFT
4289 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004290 {
4291 char_u *p1, *p2;
4292 int t;
4293
4294 // like rl_mirror(), but keep the space at the end
4295 p2 = skiptowhite(extra) - 1;
4296 for (p1 = extra; p1 < p2; ++p1, --p2)
4297 {
4298 t = *p1;
4299 *p1 = *p2;
4300 *p2 = t;
4301 }
4302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303#endif
4304 p_extra = extra;
4305 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004306 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004311 c_final = NUL;
4312 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004313 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004314 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004315#ifdef FEAT_SYN_HL
4316 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004317 * the current line differently.
4318 * TODO: Can we use CursorLine instead of CursorLineNr
4319 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004320 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004321 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004322 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325 }
4326
Bram Moolenaar597a4222014-06-25 14:39:50 +02004327#ifdef FEAT_LINEBREAK
4328 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4329 && n_extra == 0 && *p_sbr != NUL)
4330 /* draw indent after showbreak value */
4331 draw_state = WL_BRI;
4332 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4333 /* After the showbreak, draw the breakindent */
4334 draw_state = WL_BRI - 1;
4335
4336 /* draw 'breakindent': indent wrapped text accordingly */
4337 if (draw_state == WL_BRI - 1 && n_extra == 0)
4338 {
4339 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004340 /* if need_showbreak is set, breakindent also applies */
4341 if (wp->w_p_bri && n_extra == 0
4342 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004343# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004344 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004345# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004346 )
4347 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004348 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004349# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004350 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004351 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004352 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004353# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004354 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4355 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004356 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004357# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004358 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004359# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004360 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004361 c_extra = ' ';
4362 n_extra = get_breakindent_win(wp,
4363 ml_get_buf(wp->w_buffer, lnum, FALSE));
4364 /* Correct end of highlighted area for 'breakindent',
4365 * required when 'linebreak' is also set. */
4366 if (tocol == vcol)
4367 tocol += n_extra;
4368 }
4369 }
4370#endif
4371
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4373 if (draw_state == WL_SBR - 1 && n_extra == 0)
4374 {
4375 draw_state = WL_SBR;
4376# ifdef FEAT_DIFF
4377 if (filler_todo > 0)
4378 {
4379 /* Draw "deleted" diff line(s). */
4380 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004383 c_final = NUL;
4384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004388 c_final = NUL;
4389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390# ifdef FEAT_RIGHTLEFT
4391 if (wp->w_p_rl)
4392 n_extra = col + 1;
4393 else
4394# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004395 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004396 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398# endif
4399# ifdef FEAT_LINEBREAK
4400 if (*p_sbr != NUL && need_showbreak)
4401 {
4402 /* Draw 'showbreak' at the start of each broken line. */
4403 p_extra = p_sbr;
4404 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004405 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004407 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004409 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 /* Correct end of highlighted area for 'showbreak',
4411 * required when 'linebreak' is also set. */
4412 if (tocol == vcol)
4413 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004414#ifdef FEAT_SYN_HL
4415 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004416 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004417 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004418 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421# endif
4422 }
4423#endif
4424
4425 if (draw_state == WL_LINE - 1 && n_extra == 0)
4426 {
4427 draw_state = WL_LINE;
4428 if (saved_n_extra)
4429 {
4430 /* Continue item from end of wrapped line. */
4431 n_extra = saved_n_extra;
4432 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004433 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 p_extra = saved_p_extra;
4435 char_attr = saved_char_attr;
4436 }
4437 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004438 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 }
4441
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004442 // When still displaying '$' of change command, stop at cursor.
4443 // When only displaying the (relative) line number and that's done,
4444 // stop here.
4445 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004446 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_DIFF
4448 && filler_todo <= 0
4449#endif
4450 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004451 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004453 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004454 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004455 /* Pretend we have finished updating the window. Except when
4456 * 'cursorcolumn' is set. */
4457#ifdef FEAT_SYN_HL
4458 if (wp->w_p_cuc)
4459 row = wp->w_cline_row + wp->w_cline_height;
4460 else
4461#endif
4462 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 break;
4464 }
4465
Bram Moolenaar637532b2019-01-03 21:44:40 +01004466 if (draw_state == WL_LINE && (area_highlighting
4467#ifdef FEAT_SPELL
4468 || has_spell
4469#endif
4470 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 /* handle Visual or match highlighting in this line */
4473 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4475 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004477 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004479 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 else if (area_attr != 0
4481 && (vcol == tocol
4482 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485#ifdef FEAT_SEARCH_EXTRA
4486 if (!n_extra)
4487 {
4488 /*
4489 * Check for start/end of search pattern match.
4490 * After end, check for start/end of next match.
4491 * When another match, have to check for start again.
4492 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004493 * Do this for 'search_hl' and the match list (ordered by
4494 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004496 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004497 cur = wp->w_match_head;
Bram Moolenaarac2450a2019-06-09 18:04:28 +02004498 shl_flag = (screen_line_flags & SLF_POPUP);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004499 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004501 if (shl_flag == FALSE
4502 && ((cur != NULL
4503 && cur->priority > SEARCH_HL_PRIORITY)
4504 || cur == NULL))
4505 {
4506 shl = &search_hl;
4507 shl_flag = TRUE;
4508 }
4509 else
4510 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004511 if (cur != NULL)
4512 cur->pos.cur = 0;
4513 pos_inprogress = TRUE;
4514 while (shl->rm.regprog != NULL
4515 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004517 if (shl->startcol != MAXCOL
4518 && v >= (long)shl->startcol
4519 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004521 int tmp_col = v + MB_PTR2LEN(ptr);
4522
4523 if (shl->endcol < tmp_col)
4524 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004526#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004527 // Match with the "Conceal" group results in hiding
4528 // the match.
4529 if (cur != NULL
4530 && shl != &search_hl
4531 && syn_name2id((char_u *)"Conceal")
4532 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004533 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004534 has_match_conc =
4535 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004536 match_conc = cur->conceal_char;
4537 }
4538 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004539 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004542 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
4544 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004545 next_search_hl(wp, shl, lnum, (colnr_T)v,
4546 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004547 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004548 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 /* Need to get the line again, a multi-line regexp
4551 * may have made it invalid. */
4552 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4553 ptr = line + v;
4554
4555 if (shl->lnum == lnum)
4556 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004557 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004559 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004561 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004563 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
4565 /* highlight empty match, try again after
4566 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004568 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004569 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004571 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
4573
4574 /* Loop to check if the match starts at the
4575 * current position */
4576 continue;
4577 }
4578 }
4579 break;
4580 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004581 if (shl != &search_hl && cur != NULL)
4582 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004584
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004585 /* Use attributes from match with highest priority among
4586 * 'search_hl' and the match list. */
4587 search_attr = search_hl.attr_cur;
4588 cur = wp->w_match_head;
4589 shl_flag = FALSE;
4590 while (cur != NULL || shl_flag == FALSE)
4591 {
4592 if (shl_flag == FALSE
4593 && ((cur != NULL
4594 && cur->priority > SEARCH_HL_PRIORITY)
4595 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004596 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004597 shl = &search_hl;
4598 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004599 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004600 else
4601 shl = &cur->hl;
4602 if (shl->attr_cur != 0)
4603 search_attr = shl->attr_cur;
4604 if (shl != &search_hl && cur != NULL)
4605 cur = cur->next;
4606 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004607 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004608 if (*ptr == NUL && (did_line_attr >= 1
4609 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004610 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612#endif
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004615 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004617 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4618 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004620 if (diff_hlf == HLF_TXD && ptr - line > change_end
4621 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004623 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004624 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004625 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
4627#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004628
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004629#ifdef FEAT_TEXT_PROP
4630 if (text_props != NULL)
4631 {
4632 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004633 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004634
4635 // Check if any active property ends.
4636 for (pi = 0; pi < text_props_active; ++pi)
4637 {
4638 int tpi = text_prop_idxs[pi];
4639
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004640 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004641 + text_props[tpi].tp_len)
4642 {
4643 if (pi + 1 < text_props_active)
4644 mch_memmove(text_prop_idxs + pi,
4645 text_prop_idxs + pi + 1,
4646 sizeof(int)
4647 * (text_props_active - (pi + 1)));
4648 --text_props_active;
4649 --pi;
4650 }
4651 }
4652
4653 // Add any text property that starts in this column.
4654 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004655 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004656 text_prop_idxs[text_props_active++] = text_prop_next++;
4657
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004658 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004659 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004660 if (text_props_active > 0)
4661 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004662 // Sort the properties on priority and/or starting last.
4663 // Then combine the attributes, highest priority last.
4664 current_text_props = text_props;
4665 current_buf = wp->w_buffer;
4666 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4667 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004668
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004669 for (pi = 0; pi < text_props_active; ++pi)
4670 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004671 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004672 proptype_T *pt = text_prop_type_by_id(
4673 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004674
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004675 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004676 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004677 int pt_attr = syn_id2attr(pt->pt_hl_id);
4678
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004679 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004680 text_prop_attr =
4681 hl_combine_attr(text_prop_attr, pt_attr);
4682 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004683 }
4684 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004685 }
4686 }
4687#endif
4688
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004689 /* Decide which of the highlight attributes to use. */
4690 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004691#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004692 if (area_attr != 0)
4693 char_attr = hl_combine_attr(line_attr, area_attr);
4694 else if (search_attr != 0)
4695 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004696# ifdef FEAT_TEXT_PROP
4697 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004698 {
4699 char_attr = hl_combine_attr(
4700 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4701 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004702# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004703 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004704 || vcol < fromcol || vcol_prev < fromcol_prev
4705 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004706 // Use line_attr when not in the Visual or 'incsearch' area
4707 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004708 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004709#else
4710 if (area_attr != 0)
4711 char_attr = area_attr;
4712 else if (search_attr != 0)
4713 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004714#endif
4715 else
4716 {
4717 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004718#ifdef FEAT_TEXT_PROP
4719 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004720 {
4721 if (text_prop_combine)
4722 char_attr = hl_combine_attr(
4723 syntax_attr, text_prop_attr);
4724 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004725 char_attr = hl_combine_attr(
4726 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004727 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004728 else
4729#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004730#ifdef FEAT_SYN_HL
4731 if (has_syntax)
4732 char_attr = syntax_attr;
4733 else
4734#endif
4735 char_attr = 0;
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004738 if (char_attr == 0)
4739 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 /*
4742 * Get the next character to put on the screen.
4743 */
4744 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004745 * The "p_extra" points to the extra stuff that is inserted to
4746 * represent special characters (non-printable stuff) and other
4747 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004748 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004749 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4750 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4752 */
4753 if (n_extra > 0)
4754 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004755 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004757 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004759 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004762 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004763 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765 else
4766 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 else
4769 {
4770 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (has_mbyte)
4772 {
4773 mb_c = c;
4774 if (enc_utf8)
4775 {
4776 /* If the UTF-8 character is more than one byte:
4777 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004778 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 mb_utf8 = FALSE;
4780 if (mb_l > n_extra)
4781 mb_l = 1;
4782 else if (mb_l > 1)
4783 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004784 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004786 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 }
4789 else
4790 {
4791 /* if this is a DBCS character, put it in "mb_c" */
4792 mb_l = MB_BYTE2LEN(c);
4793 if (mb_l >= n_extra)
4794 mb_l = 1;
4795 else if (mb_l > 1)
4796 mb_c = (c << 8) + p_extra[1];
4797 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004798 if (mb_l == 0) /* at the NUL at end-of-line */
4799 mb_l = 1;
4800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 /* If a double-width char doesn't fit display a '>' in the
4802 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004803 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804# ifdef FEAT_RIGHTLEFT
4805 wp->w_p_rl ? (col <= 0) :
4806# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004807 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 && (*mb_char2cells)(mb_c) == 2)
4809 {
4810 c = '>';
4811 mb_c = c;
4812 mb_l = 1;
4813 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004814 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 /* put the pointer back to output the double-width
4816 * character at the start of the next line. */
4817 ++n_extra;
4818 --p_extra;
4819 }
4820 else
4821 {
4822 n_extra -= mb_l - 1;
4823 p_extra += mb_l - 1;
4824 }
4825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 ++p_extra;
4827 }
4828 --n_extra;
4829 }
4830 else
4831 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004832#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004833 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004834#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004835
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004836 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004837 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 /*
4839 * Get a character from the line itself.
4840 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004841 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004842#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004843 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (has_mbyte)
4846 {
4847 mb_c = c;
4848 if (enc_utf8)
4849 {
4850 /* If the UTF-8 character is more than one byte: Decode it
4851 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004852 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 mb_utf8 = FALSE;
4854 if (mb_l > 1)
4855 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004856 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 /* Overlong encoded ASCII or ASCII with composing char
4858 * is displayed normally, except a NUL. */
4859 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004860 {
4861 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004862#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004863 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004864#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004867
4868 /* At start of the line we can have a composing char.
4869 * Draw it as a space with a composing char. */
4870 if (utf_iscomposing(mb_c))
4871 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004872 int i;
4873
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004874 for (i = Screen_mco - 1; i > 0; --i)
4875 u8cc[i] = u8cc[i - 1];
4876 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004877 mb_c = ' ';
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880
4881 if ((mb_l == 1 && c >= 0x80)
4882 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004883 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
4885 /*
4886 * Illegal UTF-8 byte: display as <xx>.
4887 * Non-BMP character : display as ? or fullwidth ?.
4888 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004889 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004890# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004891 if (wp->w_p_rl) /* reverse */
4892 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 p_extra = extra;
4895 c = *p_extra;
4896 mb_c = mb_ptr2char_adv(&p_extra);
4897 mb_utf8 = (c >= 0x80);
4898 n_extra = (int)STRLEN(p_extra);
4899 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004900 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (area_attr == 0 && search_attr == 0)
4902 {
4903 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004904 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 saved_attr2 = char_attr; /* save current attr */
4906 }
4907 }
4908 else if (mb_l == 0) /* at the NUL at end-of-line */
4909 mb_l = 1;
4910#ifdef FEAT_ARABIC
4911 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4912 {
4913 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004914 int pc, pc1, nc;
4915 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /* The idea of what is the previous and next
4918 * character depends on 'rightleft'. */
4919 if (wp->w_p_rl)
4920 {
4921 pc = prev_c;
4922 pc1 = prev_c1;
4923 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004924 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 else
4927 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004928 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004930 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 prev_c = mb_c;
4933
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004934 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936 else
4937 prev_c = mb_c;
4938#endif
4939 }
4940 else /* enc_dbcs */
4941 {
4942 mb_l = MB_BYTE2LEN(c);
4943 if (mb_l == 0) /* at the NUL at end-of-line */
4944 mb_l = 1;
4945 else if (mb_l > 1)
4946 {
4947 /* We assume a second byte below 32 is illegal.
4948 * Hopefully this is OK for all double-byte encodings!
4949 */
4950 if (ptr[1] >= 32)
4951 mb_c = (c << 8) + ptr[1];
4952 else
4953 {
4954 if (ptr[1] == NUL)
4955 {
4956 /* head byte at end of line */
4957 mb_l = 1;
4958 transchar_nonprint(extra, c);
4959 }
4960 else
4961 {
4962 /* illegal tail byte */
4963 mb_l = 2;
4964 STRCPY(extra, "XX");
4965 }
4966 p_extra = extra;
4967 n_extra = (int)STRLEN(extra) - 1;
4968 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004969 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 c = *p_extra++;
4971 if (area_attr == 0 && search_attr == 0)
4972 {
4973 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004974 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 saved_attr2 = char_attr; /* save current attr */
4976 }
4977 mb_c = c;
4978 }
4979 }
4980 }
4981 /* If a double-width char doesn't fit display a '>' in the
4982 * last column; the character is displayed at the start of the
4983 * next line. */
4984 if ((
4985# ifdef FEAT_RIGHTLEFT
4986 wp->w_p_rl ? (col <= 0) :
4987# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004988 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 && (*mb_char2cells)(mb_c) == 2)
4990 {
4991 c = '>';
4992 mb_c = c;
4993 mb_utf8 = FALSE;
4994 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004995 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004996 // Put pointer back so that the character will be
4997 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004999#ifdef FEAT_CONCEAL
5000 did_decrement_ptr = TRUE;
5001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
5003 else if (*ptr != NUL)
5004 ptr += mb_l - 1;
5005
5006 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02005007 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005008 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02005009 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005012 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005013 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 c = ' ';
5015 if (area_attr == 0 && search_attr == 0)
5016 {
5017 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005018 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 saved_attr2 = char_attr; /* save current attr */
5020 }
5021 mb_c = c;
5022 mb_utf8 = FALSE;
5023 mb_l = 1;
5024 }
5025
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 ++ptr;
5028
5029 if (extra_check)
5030 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005031#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00005032 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005033#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00005034
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02005035#ifdef FEAT_TERMINAL
5036 if (get_term_attr)
5037 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02005038 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02005039
5040 if (!attr_pri)
5041 char_attr = syntax_attr;
5042 else
5043 char_attr = hl_combine_attr(syntax_attr, char_attr);
5044 }
5045#endif
5046
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005047#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005048 // Get syntax attribute, unless still at the start of the line
5049 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00005050 v = (long)(ptr - line);
5051 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 /* Get the syntax attribute for the character. If there
5054 * is an error, disable syntax highlighting. */
5055 save_did_emsg = did_emsg;
5056 did_emsg = FALSE;
5057
Bram Moolenaar217ad922005-03-20 22:37:15 +00005058 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02005059# ifdef FEAT_SPELL
5060 has_spell ? &can_spell :
5061# endif
5062 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005065 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005066 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005067 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005068 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else
5071 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005072
5073 // combine syntax attribute with 'wincolor'
5074 if (win_attr != 0)
5075 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
5076
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005077#ifdef SYN_TIME_LIMIT
5078 if (wp->w_s->b_syn_slow)
5079 has_syntax = FALSE;
5080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082 /* Need to get the line again, a multi-line regexp may
5083 * have made it invalid. */
5084 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
5085 ptr = line + v;
5086
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005087# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02005088 // Text properties overrule syntax highlighting or combine.
5089 if (text_prop_attr == 0 || text_prop_combine)
5090# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005091 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02005092 int comb_attr = syntax_attr;
5093# ifdef FEAT_TEXT_PROP
5094 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
5095# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005096 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02005097 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005098 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02005099 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01005100 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02005101# ifdef FEAT_CONCEAL
5102 /* no concealing past the end of the line, it interferes
5103 * with line highlighting */
5104 if (c == NUL)
5105 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005106 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005107 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02005108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005110#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00005111
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005112#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005113 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00005114 * Only do this when there is no syntax highlighting, the
5115 * @Spell cluster is not used or the current syntax item
5116 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00005117 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00005118 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00005119 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005120 if (c != 0 && (
5121# ifdef FEAT_SYN_HL
5122 !has_syntax ||
5123# endif
5124 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00005125 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00005126 char_u *prev_ptr, *p;
5127 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005128 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00005129 if (has_mbyte)
5130 {
5131 prev_ptr = ptr - mb_l;
5132 v -= mb_l - 1;
5133 }
5134 else
Bram Moolenaare7566042005-06-17 22:00:15 +00005135 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00005136
5137 /* Use nextline[] if possible, it has the start of the
5138 * next line concatenated. */
5139 if ((prev_ptr - line) - nextlinecol >= 0)
5140 p = nextline + (prev_ptr - line) - nextlinecol;
5141 else
5142 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005143 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005144 len = spell_check(wp, p, &spell_hlf, &cap_col,
5145 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005146 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005147
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005148 /* In Insert mode only highlight a word that
5149 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005150 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005151 && (State & INSERT) != 0
5152 && wp->w_cursor.lnum == lnum
5153 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005154 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005155 && wp->w_cursor.col < (colnr_T)word_end)
5156 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005157 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005158 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005159 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005160
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005161 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005162 && (p - nextline) + len > nextline_idx)
5163 {
5164 /* Remember that the good word continues at the
5165 * start of the next line. */
5166 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005167 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005168 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005169
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005170 /* Turn index into actual attributes. */
5171 if (spell_hlf != HLF_COUNT)
5172 spell_attr = highlight_attr[spell_hlf];
5173
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005174 if (cap_col > 0)
5175 {
5176 if (p != prev_ptr
5177 && (p - nextline) + cap_col >= nextline_idx)
5178 {
5179 /* Remember that the word in the next line
5180 * must start with a capital. */
5181 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005182 cap_col = (int)((p - nextline) + cap_col
5183 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005184 }
5185 else
5186 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005187 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005188 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005189 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005190 }
5191 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005192 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005193 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005194 char_attr = hl_combine_attr(char_attr, spell_attr);
5195 else
5196 char_attr = hl_combine_attr(spell_attr, char_attr);
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#endif
5199#ifdef FEAT_LINEBREAK
5200 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005201 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005203 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005204 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005206 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005207 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005208
Bram Moolenaar597a4222014-06-25 14:39:50 +02005209 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005210 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005211 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005212 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005213# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005214 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005215 wp->w_buffer->b_p_vts_array) - 1;
5216# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005217 n_extra = (int)wp->w_buffer->b_p_ts
5218 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005219# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005220
Bram Moolenaar4df70292015-03-21 14:20:16 +01005221 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005222 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005223 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005224 {
5225#ifdef FEAT_CONCEAL
5226 if (c == TAB)
5227 /* See "Tab alignment" below. */
5228 FIX_FOR_BOGUSCOLS;
5229#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005230 if (!wp->w_p_list)
5231 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234#endif
5235
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005236 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5237 // But not when the character is followed by a composing
5238 // character (use mb_l to check that).
5239 if (wp->w_p_list
5240 && ((((c == 160 && mb_l == 1)
5241 || (mb_utf8
5242 && ((mb_c == 160 && mb_l == 2)
5243 || (mb_c == 0x202f && mb_l == 3))))
5244 && lcs_nbsp)
5245 || (c == ' '
5246 && mb_l == 1
5247 && lcs_space
5248 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005249 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005250 c = (c == ' ') ? lcs_space : lcs_nbsp;
5251 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005252 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005253 n_attr = 1;
5254 extra_attr = HL_ATTR(HLF_8);
5255 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005256 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005257 mb_c = c;
5258 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005259 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005260 mb_utf8 = TRUE;
5261 u8cc[0] = 0;
5262 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005263 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005264 else
5265 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005266 }
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5269 {
5270 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005271 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005274 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 saved_attr2 = char_attr; /* save current attr */
5276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005278 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005281 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005282 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284 else
5285 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 }
5288
5289 /*
5290 * Handling of non-printable characters.
5291 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005292 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 /*
5295 * when getting a character from the file, we may have to
5296 * turn it into something else on the way to putting it
5297 * into "ScreenLines".
5298 */
5299 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5300 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005301 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005302 long vcol_adjusted = vcol; /* removed showbreak length */
5303#ifdef FEAT_LINEBREAK
5304 /* only adjust the tab_len, when at the first column
5305 * after the showbreak value was drawn */
5306 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5307 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005310#ifdef FEAT_VARTABS
5311 tab_len = tabstop_padding(vcol_adjusted,
5312 wp->w_buffer->b_p_ts,
5313 wp->w_buffer->b_p_vts_array) - 1;
5314#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005315 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005316 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5317#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005318
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005319#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005320 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005321#endif
5322 /* tab amount depends on current column */
5323 n_extra = tab_len;
5324#ifdef FEAT_LINEBREAK
5325 else
5326 {
5327 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005328 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005329 int i;
5330 int saved_nextra = n_extra;
5331
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005332#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005333 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005334 /* there are characters to conceal */
5335 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005336 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5337 */
5338 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5339 && n_extra > tab_len)
5340 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005341#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005342
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005343 /* if n_extra > 0, it gives the number of chars, to
5344 * use for a tab, else we need to calculate the width
5345 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005346 len = (tab_len * mb_char2len(lcs_tab2));
5347 if (n_extra > 0)
5348 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005349 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005350 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005351 vim_memset(p, ' ', len);
5352 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005353 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005354 p_extra_free = p;
5355 for (i = 0; i < tab_len; i++)
5356 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005357 if (*p == NUL)
5358 {
5359 tab_len = i;
5360 break;
5361 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005362 mb_char2bytes(lcs_tab2, p);
5363 p += mb_char2len(lcs_tab2);
5364 n_extra += mb_char2len(lcs_tab2)
5365 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005366 }
5367 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005368#ifdef FEAT_CONCEAL
5369 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5370 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005371 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005372 n_extra -= vcol_off;
5373#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005374 }
5375#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005376#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005377 {
5378 int vc_saved = vcol_off;
5379
5380 /* Tab alignment should be identical regardless of
5381 * 'conceallevel' value. So tab compensates of all
5382 * previous concealed characters, and thus resets
5383 * vcol_off and boguscols accumulated so far in the
5384 * line. Note that the tab can be longer than
5385 * 'tabstop' when there are concealed characters. */
5386 FIX_FOR_BOGUSCOLS;
5387
5388 /* Make sure, the highlighting for the tab char will be
5389 * correctly set further below (effectively reverts the
5390 * FIX_FOR_BOGSUCOLS macro */
5391 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005392 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005393 tab_len += vc_saved;
5394 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 if (wp->w_p_list)
5398 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005399 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005400#ifdef FEAT_LINEBREAK
5401 if (wp->w_p_lbr)
5402 c_extra = NUL; /* using p_extra from above */
5403 else
5404#endif
5405 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005406 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005407 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005408 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005411 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005414 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005415 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 else
5419 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005420 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 c_extra = ' ';
5422 c = ' ';
5423 }
5424 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005425 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005426 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005427 || ((fromcol >= 0 || fromcol_prev >= 0)
5428 && tocol > vcol
5429 && VIsual_mode != Ctrl_V
5430 && (
5431# ifdef FEAT_RIGHTLEFT
5432 wp->w_p_rl ? (col >= 0) :
5433# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005434 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005435 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005436 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005437 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005438 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005440 /* Display a '$' after the line or highlight an extra
5441 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5443 /* For a diff line the highlighting continues after the
5444 * "$". */
5445 if (
5446# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005447 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448# ifdef LINE_ATTR
5449 &&
5450# endif
5451# endif
5452# ifdef LINE_ATTR
5453 line_attr == 0
5454# endif
5455 )
5456#endif
5457 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 /* In virtualedit, visual selections may extend
5459 * beyond end of line. */
5460 if (area_highlighting && virtual_active()
5461 && tocol != MAXCOL && vcol < tocol)
5462 n_extra = 0;
5463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
5465 p_extra = at_end_str;
5466 n_extra = 1;
5467 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005468 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
5470 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005471 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005472 c = lcs_eol;
5473 else
5474 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 lcs_eol_one = -1;
5476 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005477 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005479 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 n_attr = 1;
5481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005483 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005486 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005487 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489 else
5490 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 else if (c != NUL)
5493 {
5494 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005495 if (n_extra == 0)
5496 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#ifdef FEAT_RIGHTLEFT
5498 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5499 rl_mirror(p_extra); /* reverse "<12>" */
5500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005502 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005503#ifdef FEAT_LINEBREAK
5504 if (wp->w_p_lbr)
5505 {
5506 char_u *p;
5507
5508 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005509 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005510 vim_memset(p, ' ', n_extra);
5511 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5512 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005513 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005514 p_extra_free = p_extra = p;
5515 }
5516 else
5517#endif
5518 {
5519 n_extra = byte2cells(c) - 1;
5520 c = *p_extra++;
5521 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005522 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 {
5524 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005525 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 saved_attr2 = char_attr; /* save current attr */
5527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 else if (VIsual_active
5531 && (VIsual_mode == Ctrl_V
5532 || VIsual_mode == 'v')
5533 && virtual_active()
5534 && tocol != MAXCOL
5535 && vcol < tocol
5536 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005537#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005539#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005540 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 c = ' ';
5543 --ptr; /* put it back at the NUL */
5544 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005545#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else if ((
5547# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005548 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005550# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005551 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005552# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 ) && (
5555# ifdef FEAT_RIGHTLEFT
5556 wp->w_p_rl ? (col >= 0) :
5557# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005558 (col
5559# ifdef FEAT_CONCEAL
5560 - boguscols
5561# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005562 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 /* Highlight until the right side of the window */
5565 c = ' ';
5566 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005567
5568 /* Remember we do the char for line highlighting. */
5569 ++did_line_attr;
5570
5571 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005572 if (line_attr != 0 && char_attr == search_attr
5573 && (did_line_attr > 1
5574 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005575 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576# ifdef FEAT_DIFF
5577 if (diff_hlf == HLF_TXD)
5578 {
5579 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005580 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005581 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005582 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005583 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5584 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005585 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005589# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005590 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005591 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005592 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005593 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5594 char_attr = hl_combine_attr(char_attr,
5595 HL_ATTR(HLF_CUL));
5596 }
5597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599#endif
5600 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005601
5602#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005603 if ( wp->w_p_cole > 0
5604 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005605 conceal_cursor_line(wp))
5606 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005607 && !(lnum_in_visual_area
5608 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005609 {
5610 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005611 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005612 && (syn_get_sub_char() != NUL || match_conc
5613 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005614 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005615 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005616 /* First time at this concealed item: display one
5617 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005618 if (match_conc)
5619 c = match_conc;
5620 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005621 c = syn_get_sub_char();
5622 else if (lcs_conceal != NUL)
5623 c = lcs_conceal;
5624 else
5625 c = ' ';
5626
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005627 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005628
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005629 if (n_extra > 0)
5630 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005631 vcol += n_extra;
5632 if (wp->w_p_wrap && n_extra > 0)
5633 {
5634# ifdef FEAT_RIGHTLEFT
5635 if (wp->w_p_rl)
5636 {
5637 col -= n_extra;
5638 boguscols -= n_extra;
5639 }
5640 else
5641# endif
5642 {
5643 boguscols += n_extra;
5644 col += n_extra;
5645 }
5646 }
5647 n_extra = 0;
5648 n_attr = 0;
5649 }
5650 else if (n_skip == 0)
5651 {
5652 is_concealing = TRUE;
5653 n_skip = 1;
5654 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005655 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005656 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005657 {
5658 mb_utf8 = TRUE;
5659 u8cc[0] = 0;
5660 c = 0xc0;
5661 }
5662 else
5663 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005664 }
5665 else
5666 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005667 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005668 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005669 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005670
5671 if (n_skip > 0 && did_decrement_ptr)
5672 // not showing the '>', put pointer back to avoid getting stuck
5673 ++ptr;
5674
5675#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005678#ifdef FEAT_CONCEAL
5679 /* In the cursor line and we may be concealing characters: correct
5680 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005681 if (!did_wcol && draw_state == WL_LINE
5682 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005683 && conceal_cursor_line(wp)
5684 && (int)wp->w_virtcol <= vcol + n_skip)
5685 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005686# ifdef FEAT_RIGHTLEFT
5687 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005688 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005689 else
5690# endif
5691 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005692 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005693 did_wcol = TRUE;
5694 }
5695#endif
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 /* Don't override visual selection highlighting. */
5698 if (n_attr > 0
5699 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005700 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 char_attr = extra_attr;
5702
Bram Moolenaar81695252004-12-29 20:58:21 +00005703#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 /* XIM don't send preedit_start and preedit_end, but they send
5705 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5706 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005707 if (p_imst == IM_ON_THE_SPOT
5708 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005709 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 && (State & INSERT)
5711 && !p_imdisable
5712 && im_is_preediting()
5713 && draw_state == WL_LINE)
5714 {
5715 colnr_T tcol;
5716
5717 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005718 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 else
5720 tcol = preedit_end_col;
5721 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5722 {
5723 if (feedback_old_attr < 0)
5724 {
5725 feedback_col = 0;
5726 feedback_old_attr = char_attr;
5727 }
5728 char_attr = im_get_feedback_attr(feedback_col);
5729 if (char_attr < 0)
5730 char_attr = feedback_old_attr;
5731 feedback_col++;
5732 }
5733 else if (feedback_old_attr >= 0)
5734 {
5735 char_attr = feedback_old_attr;
5736 feedback_old_attr = -1;
5737 feedback_col = 0;
5738 }
5739 }
5740#endif
5741 /*
5742 * Handle the case where we are in column 0 but not on the first
5743 * character of the line and the user wants us to show us a
5744 * special character (via 'listchars' option "precedes:<char>".
5745 */
5746 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005747 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5749#ifdef FEAT_DIFF
5750 && filler_todo <= 0
5751#endif
5752 && draw_state > WL_NR
5753 && c != NUL)
5754 {
5755 c = lcs_prec;
5756 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005757 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5758 {
5759 /* Double-width character being overwritten by the "precedes"
5760 * character, need to fill up half the character. */
5761 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005762 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005763 n_extra = 1;
5764 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005765 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005768 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005771 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005772 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774 else
5775 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005776 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005779 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 n_attr3 = 1;
5781 }
5782 }
5783
5784 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005785 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005787 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005788#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005789 || did_line_attr == 1
5790#endif
5791 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005793#ifdef FEAT_SEARCH_EXTRA
5794 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005795
5796 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005797 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005798 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005799#endif
5800
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005801 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 * highlight match at end of line. If it's beyond the last
5803 * char on the screen, just overwrite that one (tricky!) Not
5804 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005805#ifdef FEAT_SEARCH_EXTRA
5806 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005807 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005808 prevcol_hl_flag = TRUE;
5809 else
5810 {
5811 cur = wp->w_match_head;
5812 while (cur != NULL)
5813 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005814 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005815 {
5816 prevcol_hl_flag = TRUE;
5817 break;
5818 }
5819 cur = cur->next;
5820 }
5821 }
5822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005824 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005825 && (VIsual_mode != Ctrl_V
5826 || lnum == VIsual.lnum
5827 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005828 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#ifdef FEAT_SEARCH_EXTRA
5830 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005831 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005832# ifdef FEAT_SYN_HL
5833 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5834 && !(wp == curwin && VIsual_active))
5835# endif
5836# ifdef FEAT_DIFF
5837 && diff_hlf == (hlf_T)0
5838# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005839# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005840 && did_line_attr <= 1
5841# endif
5842 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#endif
5844 ))
5845 {
5846 int n = 0;
5847
5848#ifdef FEAT_RIGHTLEFT
5849 if (wp->w_p_rl)
5850 {
5851 if (col < 0)
5852 n = 1;
5853 }
5854 else
5855#endif
5856 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005857 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 n = -1;
5859 }
5860 if (n != 0)
5861 {
5862 /* At the window boundary, highlight the last character
5863 * instead (better than nothing). */
5864 off += n;
5865 col += n;
5866 }
5867 else
5868 {
5869 /* Add a blank character to highlight. */
5870 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 if (enc_utf8)
5872 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
5874#ifdef FEAT_SEARCH_EXTRA
5875 if (area_attr == 0)
5876 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005877 /* Use attributes from match with highest priority among
5878 * 'search_hl' and the match list. */
5879 char_attr = search_hl.attr;
5880 cur = wp->w_match_head;
5881 shl_flag = FALSE;
5882 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005883 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005884 if (shl_flag == FALSE
5885 && ((cur != NULL
5886 && cur->priority > SEARCH_HL_PRIORITY)
5887 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005888 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005889 shl = &search_hl;
5890 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005891 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005892 else
5893 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005894 if ((ptr - line) - 1 == (long)shl->startcol
5895 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005896 char_attr = shl->attr;
5897 if (shl != &search_hl && cur != NULL)
5898 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
5901#endif
5902 ScreenAttrs[off] = char_attr;
5903#ifdef FEAT_RIGHTLEFT
5904 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005907 --off;
5908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 else
5910#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005913 ++off;
5914 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005915 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005916#ifdef FEAT_SYN_HL
5917 eol_hl_off = 1;
5918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
Bram Moolenaar91170f82006-05-05 21:15:17 +00005922 /*
5923 * At end of the text line.
5924 */
5925 if (c == NUL)
5926 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005927#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005928 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005929 if (wp->w_p_wrap)
5930 v = wp->w_skipcol;
5931 else
5932 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005933
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005934 /* check if line ends before left margin */
5935 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005936 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005937#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005938 // Get rid of the boguscols now, we want to draw until the right
5939 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005940 col -= boguscols;
5941 boguscols = 0;
5942#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005943
5944 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005945 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005946
5947 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005948 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5949 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005950 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005951 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005952 || draw_color_col
5953 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005954# ifdef FEAT_RIGHTLEFT
5955 && !wp->w_p_rl
5956# endif
5957 )
5958 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005959 int rightmost_vcol = 0;
5960 int i;
5961
5962 if (wp->w_p_cuc)
5963 rightmost_vcol = wp->w_virtcol;
5964 if (draw_color_col)
5965 /* determine rightmost colorcolumn to possibly draw */
5966 for (i = 0; color_cols[i] >= 0; ++i)
5967 if (rightmost_vcol < color_cols[i])
5968 rightmost_vcol = color_cols[i];
5969
Bram Moolenaar02631462017-09-22 15:20:32 +02005970 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005971 {
5972 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005973 if (enc_utf8)
5974 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005975 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005976 if (draw_color_col)
5977 draw_color_col = advance_color_col(VCOL_HLC,
5978 &color_cols);
5979
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005980 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005981 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005982 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005983 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005984 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005985 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005986
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005987 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005988 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005989
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005990 ++vcol;
5991 }
5992 }
5993#endif
5994
Bram Moolenaar53f81742017-09-22 14:35:51 +02005995 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005996 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 row++;
5998
5999 /*
6000 * Update w_cline_height and w_cline_folded if the cursor line was
6001 * updated (saves a call to plines() later).
6002 */
6003 if (wp == curwin && lnum == curwin->w_cursor.lnum)
6004 {
6005 curwin->w_cline_row = startrow;
6006 curwin->w_cline_height = row - startrow;
6007#ifdef FEAT_FOLDING
6008 curwin->w_cline_folded = FALSE;
6009#endif
6010 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
6011 }
6012
6013 break;
6014 }
6015
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02006016 // Show "extends" character from 'listchars' if beyond the line end and
6017 // 'list' is set.
6018 if (lcs_ext != NUL
6019 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 && !wp->w_p_wrap
6021#ifdef FEAT_DIFF
6022 && filler_todo <= 0
6023#endif
6024 && (
6025#ifdef FEAT_RIGHTLEFT
6026 wp->w_p_rl ? col == 0 :
6027#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006028 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00006030 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
6032 {
6033 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006034 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02006036 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
6038 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006039 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006040 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 }
6042 else
6043 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006046#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02006047 /* advance to the next 'colorcolumn' */
6048 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006049 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02006050
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006051 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02006052 * highlight the cursor position itself.
6053 * Also highlight the 'colorcolumn' if it is different than
6054 * 'cursorcolumn' */
6055 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02006056 if (draw_state == WL_LINE && !lnum_in_visual_area
6057 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006058 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006059 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02006060 && lnum != wp->w_cursor.lnum)
6061 {
6062 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006063 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006064 }
Bram Moolenaard160c342010-07-18 23:30:34 +02006065 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02006066 {
6067 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006068 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02006069 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006070 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006071#endif
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /*
6074 * Store character to be displayed.
6075 * Skip characters that are left of the screen for 'nowrap'.
6076 */
6077 vcol_prev = vcol;
6078 if (draw_state < WL_LINE || n_skip <= 0)
6079 {
6080 /*
6081 * Store the character.
6082 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006083#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
6085 {
6086 /* A double-wide character is: put first halve in left cell. */
6087 --off;
6088 --col;
6089 }
6090#endif
6091 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01006093 {
6094 if ((mb_c & 0xff00) == 0x8e00)
6095 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01006097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 else if (enc_utf8)
6099 {
6100 if (mb_utf8)
6101 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006102 int i;
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006105 if ((c & 0xff) == 0)
6106 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006107 for (i = 0; i < Screen_mco; ++i)
6108 {
6109 ScreenLinesC[i][off] = u8cc[i];
6110 if (u8cc[i] == 0)
6111 break;
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114 else
6115 ScreenLinesUC[off] = 0;
6116 }
6117 if (multi_attr)
6118 {
6119 ScreenAttrs[off] = multi_attr;
6120 multi_attr = 0;
6121 }
6122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 ScreenAttrs[off] = char_attr;
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6126 {
6127 /* Need to fill two screen columns. */
6128 ++off;
6129 ++col;
6130 if (enc_utf8)
6131 /* UTF-8: Put a 0 in the second screen char. */
6132 ScreenLines[off] = 0;
6133 else
6134 /* DBCS: Put second byte in the second screen char. */
6135 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01006136 if (draw_state > WL_NR
6137#ifdef FEAT_DIFF
6138 && filler_todo <= 0
6139#endif
6140 )
6141 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 /* When "tocol" is halfway a character, set it to the end of
6143 * the character, otherwise highlighting won't stop. */
6144 if (tocol == vcol)
6145 ++tocol;
6146#ifdef FEAT_RIGHTLEFT
6147 if (wp->w_p_rl)
6148 {
6149 /* now it's time to backup one cell */
6150 --off;
6151 --col;
6152 }
6153#endif
6154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155#ifdef FEAT_RIGHTLEFT
6156 if (wp->w_p_rl)
6157 {
6158 --off;
6159 --col;
6160 }
6161 else
6162#endif
6163 {
6164 ++off;
6165 ++col;
6166 }
6167 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006168#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006169 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006170 {
6171 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006172 ++vcol_off;
6173 if (n_extra > 0)
6174 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006175 if (wp->w_p_wrap)
6176 {
6177 /*
6178 * Special voodoo required if 'wrap' is on.
6179 *
6180 * Advance the column indicator to force the line
6181 * drawing to wrap early. This will make the line
6182 * take up the same screen space when parts are concealed,
6183 * so that cursor line computations aren't messed up.
6184 *
6185 * To avoid the fictitious advance of 'col' causing
6186 * trailing junk to be written out of the screen line
6187 * we are building, 'boguscols' keeps track of the number
6188 * of bad columns we have advanced.
6189 */
6190 if (n_extra > 0)
6191 {
6192 vcol += n_extra;
6193# ifdef FEAT_RIGHTLEFT
6194 if (wp->w_p_rl)
6195 {
6196 col -= n_extra;
6197 boguscols -= n_extra;
6198 }
6199 else
6200# endif
6201 {
6202 col += n_extra;
6203 boguscols += n_extra;
6204 }
6205 n_extra = 0;
6206 n_attr = 0;
6207 }
6208
6209
Bram Moolenaar860cae12010-06-05 23:22:07 +02006210 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6211 {
6212 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006213# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006214 if (wp->w_p_rl)
6215 {
6216 --boguscols;
6217 --col;
6218 }
6219 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006220# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006221 {
6222 ++boguscols;
6223 ++col;
6224 }
6225 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006226
6227# ifdef FEAT_RIGHTLEFT
6228 if (wp->w_p_rl)
6229 {
6230 --boguscols;
6231 --col;
6232 }
6233 else
6234# endif
6235 {
6236 ++boguscols;
6237 ++col;
6238 }
6239 }
6240 else
6241 {
6242 if (n_extra > 0)
6243 {
6244 vcol += n_extra;
6245 n_extra = 0;
6246 n_attr = 0;
6247 }
6248 }
6249
6250 }
6251#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 else
6253 --n_skip;
6254
Bram Moolenaar64486672010-05-16 15:46:46 +02006255 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6256 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006257 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258#ifdef FEAT_DIFF
6259 && filler_todo <= 0
6260#endif
6261 )
6262 ++vcol;
6263
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006264#ifdef FEAT_SYN_HL
6265 if (vcol_save_attr >= 0)
6266 char_attr = vcol_save_attr;
6267#endif
6268
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 /* restore attributes after "predeces" in 'listchars' */
6270 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6271 char_attr = saved_attr3;
6272
6273 /* restore attributes after last 'listchars' or 'number' char */
6274 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6275 char_attr = saved_attr2;
6276
6277 /*
6278 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006279 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 */
6281 if ((
6282#ifdef FEAT_RIGHTLEFT
6283 wp->w_p_rl ? (col < 0) :
6284#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006285 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 && (*ptr != NUL
6287#ifdef FEAT_DIFF
6288 || filler_todo > 0
6289#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006290 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6292 )
6293 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006294#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006295 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006296 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006297 boguscols = 0;
6298#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006299 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006300 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 ++row;
6303 ++screen_row;
6304
6305 /* When not wrapping and finished diff lines, or when displayed
6306 * '$' and highlighting until last column, break here. */
6307 if ((!wp->w_p_wrap
6308#ifdef FEAT_DIFF
6309 && filler_todo <= 0
6310#endif
6311 ) || lcs_eol_one == -1)
6312 break;
6313
6314 /* When the window is too narrow draw all "@" lines. */
6315 if (draw_state != WL_LINE
6316#ifdef FEAT_DIFF
6317 && filler_todo <= 0
6318#endif
6319 )
6320 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006321 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 row = endrow;
6324 }
6325
6326 /* When line got too long for screen break here. */
6327 if (row == endrow)
6328 {
6329 ++row;
6330 break;
6331 }
6332
6333 if (screen_cur_row == screen_row - 1
6334#ifdef FEAT_DIFF
6335 && filler_todo <= 0
6336#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006337 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 /* Remember that the line wraps, used for modeless copy. */
6340 LineWraps[screen_row - 1] = TRUE;
6341
6342 /*
6343 * Special trick to make copy/paste of wrapped lines work with
6344 * xterm/screen: write an extra character beyond the end of
6345 * the line. This will work with all terminal types
6346 * (regardless of the xn,am settings).
6347 * Only do this on a fast tty.
6348 * Only do this if the cursor is on the current line
6349 * (something has been written in it).
6350 * Don't do this for the GUI.
6351 * Don't do this for double-width characters.
6352 * Don't do this for a window not at the right screen border.
6353 */
6354 if (p_tf
6355#ifdef FEAT_GUI
6356 && !gui.in_use
6357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006359 && ((*mb_off2cells)(LineOffset[screen_row],
6360 LineOffset[screen_row] + screen_Columns)
6361 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006363 + (int)Columns - 2,
6364 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006365 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
6367 /* First make sure we are at the end of the screen line,
6368 * then output the same character again to let the
6369 * terminal know about the wrap. If the terminal doesn't
6370 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006371 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 screen_char(LineOffset[screen_row - 1]
6373 + (unsigned)Columns - 1,
6374 screen_row - 1, (int)(Columns - 1));
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 /* When there is a multi-byte character, just output a
6377 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006378 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6379 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 out_char(' ');
6381 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 out_char(ScreenLines[LineOffset[screen_row - 1]
6383 + (Columns - 1)]);
6384 /* force a redraw of the first char on the next line */
6385 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6386 screen_start(); /* don't know where cursor is now */
6387 }
6388 }
6389
6390 col = 0;
6391 off = (unsigned)(current_ScreenLine - ScreenLines);
6392#ifdef FEAT_RIGHTLEFT
6393 if (wp->w_p_rl)
6394 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006395 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 off += col;
6397 }
6398#endif
6399
6400 /* reset the drawing state for the start of a wrapped line */
6401 draw_state = WL_START;
6402 saved_n_extra = n_extra;
6403 saved_p_extra = p_extra;
6404 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006405 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 saved_char_attr = char_attr;
6407 n_extra = 0;
6408 lcs_prec_todo = lcs_prec;
6409#ifdef FEAT_LINEBREAK
6410# ifdef FEAT_DIFF
6411 if (filler_todo <= 0)
6412# endif
6413 need_showbreak = TRUE;
6414#endif
6415#ifdef FEAT_DIFF
6416 --filler_todo;
6417 /* When the filler lines are actually below the last line of the
6418 * file, don't draw the line itself, break here. */
6419 if (filler_todo == 0 && wp->w_botfill)
6420 break;
6421#endif
6422 }
6423
6424 } /* for every character in the line */
6425
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006426#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006427 /* After an empty line check first word for capital. */
6428 if (*skipwhite(line) == NUL)
6429 {
6430 capcol_lnum = lnum + 1;
6431 cap_col = 0;
6432 }
6433#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006434#ifdef FEAT_TEXT_PROP
6435 vim_free(text_props);
6436 vim_free(text_prop_idxs);
6437#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006438
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006439 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 return row;
6441}
6442
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006443/*
6444 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006445 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006446 */
6447 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006448comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006449{
6450 int i;
6451
6452 for (i = 0; i < Screen_mco; ++i)
6453 {
6454 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6455 return TRUE;
6456 if (ScreenLinesC[i][off_from] == 0)
6457 break;
6458 }
6459 return FALSE;
6460}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006461
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462/*
6463 * Check whether the given character needs redrawing:
6464 * - the (first byte of the) character is different
6465 * - the attributes are different
6466 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006467 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
6469 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006470char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 if (cols > 0
6473 && ((ScreenLines[off_from] != ScreenLines[off_to]
6474 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 || (enc_dbcs != 0
6476 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6477 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6478 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6479 : (cols > 1 && ScreenLines[off_from + 1]
6480 != ScreenLines[off_to + 1])))
6481 || (enc_utf8
6482 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6483 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006484 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006485 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6486 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006487 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 return TRUE;
6489 return FALSE;
6490}
6491
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006492#if defined(FEAT_TERMINAL) || defined(PROTO)
6493/*
6494 * Return the index in ScreenLines[] for the current screen line.
6495 */
6496 int
6497screen_get_current_line_off()
6498{
6499 return (int)(current_ScreenLine - ScreenLines);
6500}
6501#endif
6502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503/*
6504 * Move one "cooked" screen line to the screen, but only the characters that
6505 * have actually changed. Handle insert/delete character.
6506 * "coloff" gives the first column on the screen for this line.
6507 * "endcol" gives the columns where valid characters are.
6508 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6509 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006510 * "flags" can have bits:
6511 * SLF_POPUP popup window
6512 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6514 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6515 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006517screen_line(
6518 int row,
6519 int coloff,
6520 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006521 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006522 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 unsigned off_from;
6525 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006526 unsigned max_off_from;
6527 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 int force = FALSE; /* force update rest of the line */
6531 int redraw_this /* bool: does character need redraw? */
6532#ifdef FEAT_GUI
6533 = TRUE /* For GUI when while-loop empty */
6534#endif
6535 ;
6536 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 int clear_next = FALSE;
6538 int char_cells; /* 1: normal char */
6539 /* 2: occupies two display cells */
6540# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006542 /* Check for illegal row and col, just in case. */
6543 if (row >= Rows)
6544 row = Rows - 1;
6545 if (endcol > Columns)
6546 endcol = Columns;
6547
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548# ifdef FEAT_CLIPBOARD
6549 clip_may_clear_selection(row, row);
6550# endif
6551
6552 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6553 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006554 max_off_from = off_from + screen_Columns;
6555 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006558 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
6560 /* Clear rest first, because it's left of the text. */
6561 if (clear_width > 0)
6562 {
6563 while (col <= endcol && ScreenLines[off_to] == ' '
6564 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006565 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
6567 ++off_to;
6568 ++col;
6569 }
6570 if (col <= endcol)
6571 screen_fill(row, row + 1, col + coloff,
6572 endcol + coloff + 1, ' ', ' ', 0);
6573 }
6574 col = endcol + 1;
6575 off_to = LineOffset[row] + col + coloff;
6576 off_from += col;
6577 endcol = (clear_width > 0 ? clear_width : -clear_width);
6578 }
6579#endif /* FEAT_RIGHTLEFT */
6580
6581 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6582
6583 while (col < endcol)
6584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006586 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 else
6588 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
6590 redraw_this = redraw_next;
6591 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6592 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6593
6594#ifdef FEAT_GUI
6595 /* If the next character was bold, then redraw the current character to
6596 * remove any pixels that might have spilt over into us. This only
6597 * happens in the GUI.
6598 */
6599 if (redraw_next && gui.in_use)
6600 {
6601 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006602 if (hl > HL_ALL)
6603 hl = syn_attr2attr(hl);
6604 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 redraw_this = TRUE;
6606 }
6607#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006608#ifdef FEAT_TEXT_PROP
6609 // Skip if under a(nother) popup.
6610 if (popup_mask[row * screen_Columns + col + coloff] > screen_zindex)
6611 redraw_this = FALSE;
6612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614 if (redraw_this)
6615 {
6616 /*
6617 * Special handling when 'xs' termcap flag set (hpterm):
6618 * Attributes for characters are stored at the position where the
6619 * cursor is when writing the highlighting code. The
6620 * start-highlighting code must be written with the cursor on the
6621 * first highlighted character. The stop-highlighting code must
6622 * be written with the cursor just after the last highlighted
6623 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006624 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 * to clear the rest of the line, and force redrawing it
6626 * completely.
6627 */
6628 if ( p_wiv
6629 && !force
6630#ifdef FEAT_GUI
6631 && !gui.in_use
6632#endif
6633 && ScreenAttrs[off_to] != 0
6634 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6635 {
6636 /*
6637 * Need to remove highlighting attributes here.
6638 */
6639 windgoto(row, col + coloff);
6640 out_str(T_CE); /* clear rest of this screen line */
6641 screen_start(); /* don't know where cursor is now */
6642 force = TRUE; /* force redraw of rest of the line */
6643 redraw_next = TRUE; /* or else next char would miss out */
6644
6645 /*
6646 * If the previous character was highlighted, need to stop
6647 * highlighting at this character.
6648 */
6649 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6650 {
6651 screen_attr = ScreenAttrs[off_to - 1];
6652 term_windgoto(row, col + coloff);
6653 screen_stop_highlight();
6654 }
6655 else
6656 screen_attr = 0; /* highlighting has stopped */
6657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (enc_dbcs != 0)
6659 {
6660 /* Check if overwriting a double-byte with a single-byte or
6661 * the other way around requires another character to be
6662 * redrawn. For UTF-8 this isn't needed, because comparing
6663 * ScreenLinesUC[] is sufficient. */
6664 if (char_cells == 1
6665 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006666 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {
6668 /* Writing a single-cell character over a double-cell
6669 * character: need to redraw the next cell. */
6670 ScreenLines[off_to + 1] = 0;
6671 redraw_next = TRUE;
6672 }
6673 else if (char_cells == 2
6674 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006675 && (*mb_off2cells)(off_to, max_off_to) == 1
6676 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 /* Writing the second half of a double-cell character over
6679 * a double-cell character: need to redraw the second
6680 * cell. */
6681 ScreenLines[off_to + 2] = 0;
6682 redraw_next = TRUE;
6683 }
6684
6685 if (enc_dbcs == DBCS_JPNU)
6686 ScreenLines2[off_to] = ScreenLines2[off_from];
6687 }
6688 /* When writing a single-width character over a double-width
6689 * character and at the end of the redrawn text, need to clear out
6690 * the right halve of the old character.
6691 * Also required when writing the right halve of a double-width
6692 * char over the left halve of an existing one. */
6693 if (has_mbyte && col + char_cells == endcol
6694 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006695 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006697 && (*mb_off2cells)(off_to, max_off_to) == 1
6698 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 if (enc_utf8)
6703 {
6704 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6705 if (ScreenLinesUC[off_from] != 0)
6706 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006707 int i;
6708
6709 for (i = 0; i < Screen_mco; ++i)
6710 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712 }
6713 if (char_cells == 2)
6714 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006717 /* The bold trick makes a single column of pixels appear in the
6718 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 * character should be redrawn too. This happens for our own GUI
6720 * and for some xterms. */
6721 if (
6722# ifdef FEAT_GUI
6723 gui.in_use
6724# endif
6725# if defined(FEAT_GUI) && defined(UNIX)
6726 ||
6727# endif
6728# ifdef UNIX
6729 term_is_xterm
6730# endif
6731 )
6732 {
6733 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006734 if (hl > HL_ALL)
6735 hl = syn_attr2attr(hl);
6736 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 redraw_next = TRUE;
6738 }
6739#endif
6740 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006741
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006742 /* For simplicity set the attributes of second half of a
6743 * double-wide character equal to the first half. */
6744 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006746
6747 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 screen_char(off_to, row, col + coloff);
6751 }
6752 else if ( p_wiv
6753#ifdef FEAT_GUI
6754 && !gui.in_use
6755#endif
6756 && col + coloff > 0)
6757 {
6758 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6759 {
6760 /*
6761 * Don't output stop-highlight when moving the cursor, it will
6762 * stop the highlighting when it should continue.
6763 */
6764 screen_attr = 0;
6765 }
6766 else if (screen_attr != 0)
6767 screen_stop_highlight();
6768 }
6769
6770 off_to += CHAR_CELLS;
6771 off_from += CHAR_CELLS;
6772 col += CHAR_CELLS;
6773 }
6774
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 if (clear_next)
6776 {
6777 /* Clear the second half of a double-wide character of which the left
6778 * half was overwritten with a single-wide character. */
6779 ScreenLines[off_to] = ' ';
6780 if (enc_utf8)
6781 ScreenLinesUC[off_to] = 0;
6782 screen_char(off_to, row, col + coloff);
6783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
6785 if (clear_width > 0
6786#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006787 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788#endif
6789 )
6790 {
6791#ifdef FEAT_GUI
6792 int startCol = col;
6793#endif
6794
6795 /* blank out the rest of the line */
6796 while (col < clear_width && ScreenLines[off_to] == ' '
6797 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006798 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
6800 ++off_to;
6801 ++col;
6802 }
6803 if (col < clear_width)
6804 {
6805#ifdef FEAT_GUI
6806 /*
6807 * In the GUI, clearing the rest of the line may leave pixels
6808 * behind if the first character cleared was bold. Some bold
6809 * fonts spill over the left. In this case we redraw the previous
6810 * character too. If we didn't skip any blanks above, then we
6811 * only redraw if the character wasn't already redrawn anyway.
6812 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006813 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
6815 hl = ScreenAttrs[off_to];
6816 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006817 {
6818 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006819
Bram Moolenaar9c697322006-10-09 20:11:17 +00006820 if (enc_utf8)
6821 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6822 * that its width is 2. */
6823 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6824 else if (enc_dbcs != 0)
6825 {
6826 /* find previous character by counting from first
6827 * column and get its width. */
6828 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006829 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006830
6831 while (off < off_to)
6832 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006833 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006834 off += prev_cells;
6835 }
6836 }
6837
6838 if (enc_dbcs != 0 && prev_cells > 1)
6839 screen_char_2(off_to - prev_cells, row,
6840 col + coloff - prev_cells);
6841 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006842 screen_char(off_to - prev_cells, row,
6843 col + coloff - prev_cells);
6844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846#endif
6847 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6848 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 off_to += clear_width - col;
6850 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
6852 }
6853
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006854 if (clear_width > 0
6855#ifdef FEAT_TEXT_PROP
6856 && !(flags & SLF_POPUP) // no separator for popup window
6857#endif
6858 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006860 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006861 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006862 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006864#ifdef FEAT_TEXT_PROP
6865 if (popup_mask[row * screen_Columns + col + coloff]
6866 <= screen_zindex)
6867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006869 int c;
6870
6871 c = fillchar_vsep(&hl);
6872 if (ScreenLines[off_to] != (schar_T)c
6873 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6874 != (c >= 0x80 ? c : 0))
6875 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006877 ScreenLines[off_to] = c;
6878 ScreenAttrs[off_to] = hl;
6879 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006881 if (c >= 0x80)
6882 {
6883 ScreenLinesUC[off_to] = c;
6884 ScreenLinesC[0][off_to] = 0;
6885 }
6886 else
6887 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006889 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 }
6893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 LineWraps[row] = FALSE;
6895 }
6896}
6897
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006898#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006900 * Mirror text "str" for right-left displaying.
6901 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006903 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006904rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
6906 char_u *p1, *p2;
6907 int t;
6908
6909 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6910 {
6911 t = *p1;
6912 *p1 = *p2;
6913 *p2 = t;
6914 }
6915}
6916#endif
6917
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918/*
6919 * mark all status lines for redraw; used after first :cd
6920 */
6921 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006922status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 win_T *wp;
6925
Bram Moolenaar29323592016-07-24 22:04:11 +02006926 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 if (wp->w_status_height)
6928 {
6929 wp->w_redr_status = TRUE;
6930 redraw_later(VALID);
6931 }
6932}
6933
6934/*
6935 * mark all status lines of the current buffer for redraw
6936 */
6937 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006938status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 win_T *wp;
6941
Bram Moolenaar29323592016-07-24 22:04:11 +02006942 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6944 {
6945 wp->w_redr_status = TRUE;
6946 redraw_later(VALID);
6947 }
6948}
6949
6950/*
6951 * Redraw all status lines that need to be redrawn.
6952 */
6953 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006954redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955{
6956 win_T *wp;
6957
Bram Moolenaar29323592016-07-24 22:04:11 +02006958 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006960 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006961 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006962 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
Bram Moolenaar4033c552017-09-16 20:54:51 +02006965#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966/*
6967 * Redraw all status lines at the bottom of frame "frp".
6968 */
6969 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006970win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
6972 if (frp->fr_layout == FR_LEAF)
6973 frp->fr_win->w_redr_status = TRUE;
6974 else if (frp->fr_layout == FR_ROW)
6975 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006976 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 win_redraw_last_status(frp);
6978 }
6979 else /* frp->fr_layout == FR_COL */
6980 {
6981 frp = frp->fr_child;
6982 while (frp->fr_next != NULL)
6983 frp = frp->fr_next;
6984 win_redraw_last_status(frp);
6985 }
6986}
6987#endif
6988
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989/*
6990 * Draw the verticap separator right of window "wp" starting with line "row".
6991 */
6992 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006993draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994{
6995 int hl;
6996 int c;
6997
6998 if (wp->w_vsep_width)
6999 {
7000 /* draw the vertical separator right of this window */
7001 c = fillchar_vsep(&hl);
7002 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
7003 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
7004 c, ' ', hl);
7005 }
7006}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007009static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00007012 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 */
7014 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007015status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 int len = 0;
7018
7019#ifdef FEAT_MENU
7020 int emenu = (xp->xp_context == EXPAND_MENUS
7021 || xp->xp_context == EXPAND_MENUNAMES);
7022
7023 /* Check for menu separators - replace with '|'. */
7024 if (emenu && menu_is_separator(s))
7025 return 1;
7026#endif
7027
7028 while (*s != NUL)
7029 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007030 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00007031 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007032 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034
7035 return len;
7036}
7037
7038/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007039 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007040 * These are backslashes used for escaping. Do show backslashes in help tags.
7041 */
7042 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007043skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007044{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007045 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007046#ifdef FEAT_MENU
7047 || ((xp->xp_context == EXPAND_MENUS
7048 || xp->xp_context == EXPAND_MENUNAMES)
7049 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
7050#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007051 )
7052 {
7053#ifndef BACKSLASH_IN_FILENAME
7054 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
7055 return 2;
7056#endif
7057 return 1;
7058 }
7059 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00007060}
7061
7062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 * Show wildchar matches in the status line.
7064 * Show at least the "match" item.
7065 * We start at item 'first_match' in the list and show all matches that fit.
7066 *
7067 * If inversion is possible we use it. Else '=' characters are used.
7068 */
7069 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007070win_redr_status_matches(
7071 expand_T *xp,
7072 int num_matches,
7073 char_u **matches, /* list of matches */
7074 int match,
7075 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
7078 int row;
7079 char_u *buf;
7080 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007081 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 int fillchar;
7083 int attr;
7084 int i;
7085 int highlight = TRUE;
7086 char_u *selstart = NULL;
7087 int selstart_col = 0;
7088 char_u *selend = NULL;
7089 static int first_match = 0;
7090 int add_left = FALSE;
7091 char_u *s;
7092#ifdef FEAT_MENU
7093 int emenu;
7094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 if (matches == NULL) /* interrupted completion? */
7098 return;
7099
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007100 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02007101 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007102 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02007103 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (buf == NULL)
7105 return;
7106
7107 if (match == -1) /* don't show match but original text */
7108 {
7109 match = 0;
7110 highlight = FALSE;
7111 }
7112 /* count 1 for the ending ">" */
7113 clen = status_match_len(xp, L_MATCH(match)) + 3;
7114 if (match == 0)
7115 first_match = 0;
7116 else if (match < first_match)
7117 {
7118 /* jumping left, as far as we can go */
7119 first_match = match;
7120 add_left = TRUE;
7121 }
7122 else
7123 {
7124 /* check if match fits on the screen */
7125 for (i = first_match; i < match; ++i)
7126 clen += status_match_len(xp, L_MATCH(i)) + 2;
7127 if (first_match > 0)
7128 clen += 2;
7129 /* jumping right, put match at the left */
7130 if ((long)clen > Columns)
7131 {
7132 first_match = match;
7133 /* if showing the last match, we can add some on the left */
7134 clen = 2;
7135 for (i = match; i < num_matches; ++i)
7136 {
7137 clen += status_match_len(xp, L_MATCH(i)) + 2;
7138 if ((long)clen >= Columns)
7139 break;
7140 }
7141 if (i == num_matches)
7142 add_left = TRUE;
7143 }
7144 }
7145 if (add_left)
7146 while (first_match > 0)
7147 {
7148 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7149 if ((long)clen >= Columns)
7150 break;
7151 --first_match;
7152 }
7153
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007154 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 if (first_match == 0)
7157 {
7158 *buf = NUL;
7159 len = 0;
7160 }
7161 else
7162 {
7163 STRCPY(buf, "< ");
7164 len = 2;
7165 }
7166 clen = len;
7167
7168 i = first_match;
7169 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7170 {
7171 if (i == match)
7172 {
7173 selstart = buf + len;
7174 selstart_col = clen;
7175 }
7176
7177 s = L_MATCH(i);
7178 /* Check for menu separators - replace with '|' */
7179#ifdef FEAT_MENU
7180 emenu = (xp->xp_context == EXPAND_MENUS
7181 || xp->xp_context == EXPAND_MENUNAMES);
7182 if (emenu && menu_is_separator(s))
7183 {
7184 STRCPY(buf + len, transchar('|'));
7185 l = (int)STRLEN(buf + len);
7186 len += l;
7187 clen += l;
7188 }
7189 else
7190#endif
7191 for ( ; *s != NUL; ++s)
7192 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007193 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007195 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {
7197 STRNCPY(buf + len, s, l);
7198 s += l - 1;
7199 len += l;
7200 }
7201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 STRCPY(buf + len, transchar_byte(*s));
7204 len += (int)STRLEN(buf + len);
7205 }
7206 }
7207 if (i == match)
7208 selend = buf + len;
7209
7210 *(buf + len++) = ' ';
7211 *(buf + len++) = ' ';
7212 clen += 2;
7213 if (++i == num_matches)
7214 break;
7215 }
7216
7217 if (i != num_matches)
7218 {
7219 *(buf + len++) = '>';
7220 ++clen;
7221 }
7222
7223 buf[len] = NUL;
7224
7225 row = cmdline_row - 1;
7226 if (row >= 0)
7227 {
7228 if (wild_menu_showing == 0)
7229 {
7230 if (msg_scrolled > 0)
7231 {
7232 /* Put the wildmenu just above the command line. If there is
7233 * no room, scroll the screen one line up. */
7234 if (cmdline_row == Rows - 1)
7235 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007236 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 ++msg_scrolled;
7238 }
7239 else
7240 {
7241 ++cmdline_row;
7242 ++row;
7243 }
7244 wild_menu_showing = WM_SCROLLED;
7245 }
7246 else
7247 {
7248 /* Create status line if needed by setting 'laststatus' to 2.
7249 * Set 'winminheight' to zero to avoid that the window is
7250 * resized. */
7251 if (lastwin->w_status_height == 0)
7252 {
7253 save_p_ls = p_ls;
7254 save_p_wmh = p_wmh;
7255 p_ls = 2;
7256 p_wmh = 0;
7257 last_status(FALSE);
7258 }
7259 wild_menu_showing = WM_SHOWN;
7260 }
7261 }
7262
7263 screen_puts(buf, row, 0, attr);
7264 if (selstart != NULL && highlight)
7265 {
7266 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007267 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 }
7269
7270 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7271 }
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 vim_free(buf);
7275}
7276#endif
7277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278/*
7279 * Redraw the status line of window wp.
7280 *
7281 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007282 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7283 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007285 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007286win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288 int row;
7289 char_u *p;
7290 int len;
7291 int fillchar;
7292 int attr;
7293 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007294 static int busy = FALSE;
7295
7296 /* It's possible to get here recursively when 'statusline' (indirectly)
7297 * invokes ":redrawstatus". Simply ignore the call then. */
7298 if (busy)
7299 return;
7300 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
7302 wp->w_redr_status = FALSE;
7303 if (wp->w_status_height == 0)
7304 {
7305 /* no status line, can only be last window */
7306 redraw_cmdline = TRUE;
7307 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007308 else if (!redrawing()
7309#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007310 // don't update status line when popup menu is visible and may be
7311 // drawn over it, unless it will be redrawn later
7312 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007313#endif
7314 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
7316 /* Don't redraw right now, do it later. */
7317 wp->w_redr_status = TRUE;
7318 }
7319#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007320 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
7322 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007323 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 }
7325#endif
7326 else
7327 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007328 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007330 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 p = NameBuff;
7332 len = (int)STRLEN(p);
7333
Bram Moolenaard85f2712017-07-28 21:51:57 +02007334 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335#ifdef FEAT_QUICKFIX
7336 || wp->w_p_pvw
7337#endif
7338 || bufIsChanged(wp->w_buffer)
7339 || wp->w_buffer->b_p_ro)
7340 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007341 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007343 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 len += (int)STRLEN(p + len);
7345 }
7346#ifdef FEAT_QUICKFIX
7347 if (wp->w_p_pvw)
7348 {
7349 STRCPY(p + len, _("[Preview]"));
7350 len += (int)STRLEN(p + len);
7351 }
7352#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007353 if (bufIsChanged(wp->w_buffer)
7354#ifdef FEAT_TERMINAL
7355 && !bt_terminal(wp->w_buffer)
7356#endif
7357 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
7359 STRCPY(p + len, "[+]");
7360 len += 3;
7361 }
7362 if (wp->w_buffer->b_p_ro)
7363 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007364 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007365 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
7367
Bram Moolenaar02631462017-09-22 15:20:32 +02007368 this_ru_col = ru_col - (Columns - wp->w_width);
7369 if (this_ru_col < (wp->w_width + 1) / 2)
7370 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (this_ru_col <= 1)
7372 {
7373 p = (char_u *)"<"; /* No room for file name! */
7374 len = 1;
7375 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007376 else if (has_mbyte)
7377 {
7378 int clen = 0, i;
7379
7380 /* Count total number of display cells. */
7381 clen = mb_string2cells(p, -1);
7382
7383 /* Find first character that will fit.
7384 * Going from start to end is much faster for DBCS. */
7385 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7386 i += (*mb_ptr2len)(p + i))
7387 clen -= (*mb_ptr2cells)(p + i);
7388 len = clen;
7389 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007391 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007393 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
7395
Bram Moolenaara12a1612019-01-24 16:39:02 +01007396 }
7397 else if (len > this_ru_col - 1)
7398 {
7399 p += len - (this_ru_col - 1);
7400 *p = '<';
7401 len = this_ru_col - 1;
7402 }
7403
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007405 screen_puts(p, row, wp->w_wincol, attr);
7406 screen_fill(row, row + 1, len + wp->w_wincol,
7407 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007409 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7411 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007412 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
7414#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007415 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416#endif
7417 }
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 /*
7420 * May need to draw the character below the vertical separator.
7421 */
7422 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7423 {
7424 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007425 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 else
7427 fillchar = fillchar_vsep(&attr);
7428 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7429 attr);
7430 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007431 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
Bram Moolenaar238a5642006-02-21 22:12:05 +00007434#ifdef FEAT_STL_OPT
7435/*
7436 * Redraw the status line according to 'statusline' and take care of any
7437 * errors encountered.
7438 */
7439 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007440redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007441{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007442 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007443 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007444
7445 /* When called recursively return. This can happen when the statusline
7446 * contains an expression that triggers a redraw. */
7447 if (entered)
7448 return;
7449 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007450
Bram Moolenaara742e082016-04-05 21:10:38 +02007451 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007452 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007453 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007454 {
7455 /* When there is an error disable the statusline, otherwise the
7456 * display is messed up with errors and a redraw triggers the problem
7457 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007458 set_string_option_direct((char_u *)"statusline", -1,
7459 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007460 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007461 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007462 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007463 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007464}
7465#endif
7466
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467/*
7468 * Return TRUE if the status line of window "wp" is connected to the status
7469 * line of the window right of it. If not, then it's a vertical separator.
7470 * Only call if (wp->w_vsep_width != 0).
7471 */
7472 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007473stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 frame_T *fr;
7476
7477 fr = wp->w_frame;
7478 while (fr->fr_parent != NULL)
7479 {
7480 if (fr->fr_parent->fr_layout == FR_COL)
7481 {
7482 if (fr->fr_next != NULL)
7483 break;
7484 }
7485 else
7486 {
7487 if (fr->fr_next != NULL)
7488 return TRUE;
7489 }
7490 fr = fr->fr_parent;
7491 }
7492 return FALSE;
7493}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496/*
7497 * Get the value to show for the language mappings, active 'keymap'.
7498 */
7499 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007500get_keymap_str(
7501 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007502 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007503 char_u *buf, /* buffer for the result */
7504 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505{
7506 char_u *p;
7507
7508 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7509 return FALSE;
7510
7511 {
7512#ifdef FEAT_EVAL
7513 buf_T *old_curbuf = curbuf;
7514 win_T *old_curwin = curwin;
7515 char_u *s;
7516
7517 curbuf = wp->w_buffer;
7518 curwin = wp;
7519 STRCPY(buf, "b:keymap_name"); /* must be writable */
7520 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007521 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 --emsg_skip;
7523 curbuf = old_curbuf;
7524 curwin = old_curwin;
7525 if (p == NULL || *p == NUL)
7526#endif
7527 {
7528#ifdef FEAT_KEYMAP
7529 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7530 p = wp->w_buffer->b_p_keymap;
7531 else
7532#endif
7533 p = (char_u *)"lang";
7534 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007535 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 buf[0] = NUL;
7537#ifdef FEAT_EVAL
7538 vim_free(s);
7539#endif
7540 }
7541 return buf[0] != NUL;
7542}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543
7544#if defined(FEAT_STL_OPT) || defined(PROTO)
7545/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007546 * Redraw the status line or ruler of window "wp".
7547 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 */
7549 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007550win_redr_custom(
7551 win_T *wp,
7552 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007554 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 int attr;
7556 int curattr;
7557 int row;
7558 int col = 0;
7559 int maxwidth;
7560 int width;
7561 int n;
7562 int len;
7563 int fillchar;
7564 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007565 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007567 struct stl_hlrec hltab[STL_MAX_ITEM];
7568 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007569 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007570 win_T *ewp;
7571 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572
Bram Moolenaar1d633412013-12-11 15:52:01 +01007573 /* There is a tiny chance that this gets called recursively: When
7574 * redrawing a status line triggers redrawing the ruler or tabline.
7575 * Avoid trouble by not allowing recursion. */
7576 if (entered)
7577 return;
7578 entered = TRUE;
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007581 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007583 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007584 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007585 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007586 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007587 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007588 maxwidth = Columns;
7589# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007590 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007593 else
7594 {
7595 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007596 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007597 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007598
7599 if (draw_ruler)
7600 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007601 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007602 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007603 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007604 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007605 if (*++stl == '-')
7606 stl++;
7607 if (atoi((char *)stl))
7608 while (VIM_ISDIGIT(*stl))
7609 stl++;
7610 if (*stl++ != '(')
7611 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007612 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007613 col = ru_col - (Columns - wp->w_width);
7614 if (col < (wp->w_width + 1) / 2)
7615 col = (wp->w_width + 1) / 2;
7616 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007617 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007618 {
7619 row = Rows - 1;
7620 --maxwidth; /* writing in last column may cause scrolling */
7621 fillchar = ' ';
7622 attr = 0;
7623 }
7624
7625# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007626 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007627# endif
7628 }
7629 else
7630 {
7631 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007632 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007633 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007634 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007635# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007636 use_sandbox = was_set_insecurely((char_u *)"statusline",
7637 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007638# endif
7639 }
7640
Bram Moolenaar53f81742017-09-22 14:35:51 +02007641 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007642 }
7643
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007645 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaar61452852011-02-01 18:01:11 +01007647 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7648 * the cursor away and back. */
7649 ewp = wp == NULL ? curwin : wp;
7650 p_crb_save = ewp->w_p_crb;
7651 ewp->w_p_crb = FALSE;
7652
Bram Moolenaar362f3562009-11-03 16:20:34 +00007653 /* Make a copy, because the statusline may include a function call that
7654 * might change the option value and free the memory. */
7655 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007656 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007657 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007658 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007659 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007660 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007662 /* Make all characters printable. */
7663 p = transstr(buf);
7664 if (p != NULL)
7665 {
7666 vim_strncpy(buf, p, sizeof(buf) - 1);
7667 vim_free(p);
7668 }
7669
7670 /* fill up with "fillchar" */
7671 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007672 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 ++width;
7676 }
7677 buf[len] = NUL;
7678
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007679 /*
7680 * Draw each snippet with the specified highlighting.
7681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 curattr = attr;
7683 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007684 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007686 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 screen_puts_len(p, len, row, col, curattr);
7688 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007689 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007691 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007693 else if (hltab[n].userhl < 0)
7694 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007695#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007696 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7697 && wp->w_status_height != 0)
7698 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007699 else if (wp != NULL && bt_terminal(wp->w_buffer)
7700 && wp->w_status_height != 0)
7701 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007702#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007703 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007704 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007706 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007709
7710 if (wp == NULL)
7711 {
7712 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7713 col = 0;
7714 len = 0;
7715 p = buf;
7716 fillchar = 0;
7717 for (n = 0; tabtab[n].start != NULL; n++)
7718 {
7719 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7720 while (col < len)
7721 TabPageIdxs[col++] = fillchar;
7722 p = tabtab[n].start;
7723 fillchar = tabtab[n].userhl;
7724 }
7725 while (col < Columns)
7726 TabPageIdxs[col++] = fillchar;
7727 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007728
7729theend:
7730 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731}
7732
7733#endif /* FEAT_STL_OPT */
7734
7735/*
7736 * Output a single character directly to the screen and update ScreenLines.
7737 */
7738 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007739screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 char_u buf[MB_MAXBYTES + 1];
7742
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007743 if (has_mbyte)
7744 buf[(*mb_char2bytes)(c, buf)] = NUL;
7745 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007746 {
7747 buf[0] = c;
7748 buf[1] = NUL;
7749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 screen_puts(buf, row, col, attr);
7751}
7752
7753/*
7754 * Get a single character directly from ScreenLines into "bytes[]".
7755 * Also return its attribute in *attrp;
7756 */
7757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007758screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759{
7760 unsigned off;
7761
7762 /* safety check */
7763 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7764 {
7765 off = LineOffset[row] + col;
7766 *attrp = ScreenAttrs[off];
7767 bytes[0] = ScreenLines[off];
7768 bytes[1] = NUL;
7769
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 if (enc_utf8 && ScreenLinesUC[off] != 0)
7771 bytes[utfc_char2bytes(off, bytes)] = NUL;
7772 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7773 {
7774 bytes[0] = ScreenLines[off];
7775 bytes[1] = ScreenLines2[off];
7776 bytes[2] = NUL;
7777 }
7778 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7779 {
7780 bytes[1] = ScreenLines[off + 1];
7781 bytes[2] = NUL;
7782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784}
7785
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007786/*
7787 * Return TRUE if composing characters for screen posn "off" differs from
7788 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007789 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007790 */
7791 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007792screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007793{
7794 int i;
7795
7796 for (i = 0; i < Screen_mco; ++i)
7797 {
7798 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7799 return TRUE;
7800 if (u8cc[i] == 0)
7801 break;
7802 }
7803 return FALSE;
7804}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806/*
7807 * Put string '*text' on the screen at position 'row' and 'col', with
7808 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7809 * Note: only outputs within one row, message is truncated at screen boundary!
7810 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7811 */
7812 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007813screen_puts(
7814 char_u *text,
7815 int row,
7816 int col,
7817 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 screen_puts_len(text, -1, row, col, attr);
7820}
7821
7822/*
7823 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7824 * a NUL.
7825 */
7826 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007827screen_puts_len(
7828 char_u *text,
7829 int textlen,
7830 int row,
7831 int col,
7832 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834 unsigned off;
7835 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007836 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007838 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 int mbyte_blen = 1;
7840 int mbyte_cells = 1;
7841 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007842 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007844#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007846 int pc, nc, nc1;
7847 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007849 int force_redraw_this;
7850 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007851 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007853 // Safety check. The check for negative row and column is to fix issue
7854 // #4102. TODO: find out why row/col could be negative.
7855 if (ScreenLines == NULL
7856 || row >= screen_Rows || row < 0
7857 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007859 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860
Bram Moolenaarc236c162008-07-13 17:41:49 +00007861 /* When drawing over the right halve of a double-wide char clear out the
7862 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007863 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007864#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007865 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007866#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007867 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007868 {
7869 ScreenLines[off - 1] = ' ';
7870 ScreenAttrs[off - 1] = 0;
7871 if (enc_utf8)
7872 {
7873 ScreenLinesUC[off - 1] = 0;
7874 ScreenLinesC[0][off - 1] = 0;
7875 }
7876 /* redraw the previous cell, make it empty */
7877 screen_char(off - 1, row, col - 1);
7878 /* force the cell at "col" to be redrawn */
7879 force_redraw_next = TRUE;
7880 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007881
Bram Moolenaar367329b2007-08-30 11:53:22 +00007882 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007883 while (col < screen_Columns
7884 && (len < 0 || (int)(ptr - text) < len)
7885 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 /* check if this is the first byte of a multibyte */
7889 if (has_mbyte)
7890 {
7891 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007892 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007894 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7896 mbyte_cells = 1;
7897 else if (enc_dbcs != 0)
7898 mbyte_cells = mbyte_blen;
7899 else /* enc_utf8 */
7900 {
7901 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007902 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 (int)((text + len) - ptr));
7904 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007905 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007907#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7909 {
7910 /* Do Arabic shaping. */
7911 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7912 {
7913 /* Past end of string to be displayed. */
7914 nc = NUL;
7915 nc1 = NUL;
7916 }
7917 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007918 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007919 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7920 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007921 nc1 = pcc[0];
7922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 pc = prev_c;
7924 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007925 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 }
7927 else
7928 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007929#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007930 if (col + mbyte_cells > screen_Columns)
7931 {
7932 /* Only 1 cell left, but character requires 2 cells:
7933 * display a '>' in the last column to avoid wrapping. */
7934 c = '>';
7935 mbyte_cells = 1;
7936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 }
7938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007940 force_redraw_this = force_redraw_next;
7941 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007942
7943 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 || (mbyte_cells == 2
7945 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7946 || (enc_dbcs == DBCS_JPNU
7947 && c == 0x8e
7948 && ScreenLines2[off] != ptr[1])
7949 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007950 && (ScreenLinesUC[off] !=
7951 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7952 || (ScreenLinesUC[off] != 0
7953 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007955 || exmode_active;
7956
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007957 if ((need_redraw || force_redraw_this)
7958#ifdef FEAT_TEXT_PROP
7959 && popup_mask[row * screen_Columns + col] <= screen_zindex
7960#endif
7961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
7963#if defined(FEAT_GUI) || defined(UNIX)
7964 /* The bold trick makes a single row of pixels appear in the next
7965 * character. When a bold character is removed, the next
7966 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007967 * and for some xterms. */
7968 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969# ifdef FEAT_GUI
7970 gui.in_use
7971# endif
7972# if defined(FEAT_GUI) && defined(UNIX)
7973 ||
7974# endif
7975# ifdef UNIX
7976 term_is_xterm
7977# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007978 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007980 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007982 if (n > HL_ALL)
7983 n = syn_attr2attr(n);
7984 if (n & HL_BOLD)
7985 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
7987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 /* When at the end of the text and overwriting a two-cell
7989 * character with a one-cell character, need to clear the next
7990 * cell. Also when overwriting the left halve of a two-cell char
7991 * with the right halve of a two-cell char. Do this only once
7992 * (mb_off2cells() may return 2 on the right halve). */
7993 if (clear_next_cell)
7994 clear_next_cell = FALSE;
7995 else if (has_mbyte
7996 && (len < 0 ? ptr[mbyte_blen] == NUL
7997 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007998 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00008000 && (*mb_off2cells)(off, max_off) == 1
8001 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 clear_next_cell = TRUE;
8003
8004 /* Make sure we never leave a second byte of a double-byte behind,
8005 * it confuses mb_off2cells(). */
8006 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00008007 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00008009 && (*mb_off2cells)(off, max_off) == 1
8010 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 ScreenLines[off] = c;
8013 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if (enc_utf8)
8015 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008016 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 ScreenLinesUC[off] = 0;
8018 else
8019 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008020 int i;
8021
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008023 for (i = 0; i < Screen_mco; ++i)
8024 {
8025 ScreenLinesC[i][off] = u8cc[i];
8026 if (u8cc[i] == 0)
8027 break;
8028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030 if (mbyte_cells == 2)
8031 {
8032 ScreenLines[off + 1] = 0;
8033 ScreenAttrs[off + 1] = attr;
8034 }
8035 screen_char(off, row, col);
8036 }
8037 else if (mbyte_cells == 2)
8038 {
8039 ScreenLines[off + 1] = ptr[1];
8040 ScreenAttrs[off + 1] = attr;
8041 screen_char_2(off, row, col);
8042 }
8043 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
8044 {
8045 ScreenLines2[off] = ptr[1];
8046 screen_char(off, row, col);
8047 }
8048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 screen_char(off, row, col);
8050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 if (has_mbyte)
8052 {
8053 off += mbyte_cells;
8054 col += mbyte_cells;
8055 ptr += mbyte_blen;
8056 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008057 {
8058 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02008060 len = -1;
8061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 }
8063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
8065 ++off;
8066 ++col;
8067 ++ptr;
8068 }
8069 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008070
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008071 /* If we detected the next character needs to be redrawn, but the text
8072 * doesn't extend up to there, update the character here. */
8073 if (force_redraw_next && col < screen_Columns)
8074 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008075 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
8076 screen_char_2(off, row, col);
8077 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00008078 screen_char(off, row, col);
8079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080}
8081
8082#ifdef FEAT_SEARCH_EXTRA
8083/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008084 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 */
8086 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008087start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 if (p_hls && !no_hlsearch)
8090 {
8091 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01008092 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008093# ifdef FEAT_RELTIME
8094 /* Set the time limit to 'redrawtime'. */
8095 profile_setlimit(p_rdt, &search_hl.tm);
8096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
8098}
8099
8100/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008101 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 */
8103 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008104end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 if (search_hl.rm.regprog != NULL)
8107 {
Bram Moolenaar473de612013-06-08 18:19:48 +02008108 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 search_hl.rm.regprog = NULL;
8110 }
8111}
8112
8113/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008114 * Init for calling prepare_search_hl().
8115 */
8116 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008117init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02008118{
8119 matchitem_T *cur;
8120
8121 /* Setup for match and 'hlsearch' highlighting. Disable any previous
8122 * match */
8123 cur = wp->w_match_head;
8124 while (cur != NULL)
8125 {
8126 cur->hl.rm = cur->match;
8127 if (cur->hlg_id == 0)
8128 cur->hl.attr = 0;
8129 else
8130 cur->hl.attr = syn_id2attr(cur->hlg_id);
8131 cur->hl.buf = wp->w_buffer;
8132 cur->hl.lnum = 0;
8133 cur->hl.first_lnum = 0;
8134# ifdef FEAT_RELTIME
8135 /* Set the time limit to 'redrawtime'. */
8136 profile_setlimit(p_rdt, &(cur->hl.tm));
8137# endif
8138 cur = cur->next;
8139 }
8140 search_hl.buf = wp->w_buffer;
8141 search_hl.lnum = 0;
8142 search_hl.first_lnum = 0;
8143 /* time limit is set at the toplevel, for all windows */
8144}
8145
8146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 * Advance to the match in window "wp" line "lnum" or past it.
8148 */
8149 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008150prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008152 matchitem_T *cur; /* points to the match list */
8153 match_T *shl; /* points to search_hl or a match */
8154 int shl_flag; /* flag to indicate whether search_hl
8155 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008156 int pos_inprogress; /* marks that position match search is
8157 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 int n;
8159
8160 /*
8161 * When using a multi-line pattern, start searching at the top
8162 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008163 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008165 cur = wp->w_match_head;
8166 shl_flag = FALSE;
8167 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008169 if (shl_flag == FALSE)
8170 {
8171 shl = &search_hl;
8172 shl_flag = TRUE;
8173 }
8174 else
8175 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 if (shl->rm.regprog != NULL
8177 && shl->lnum == 0
8178 && re_multiline(shl->rm.regprog))
8179 {
8180 if (shl->first_lnum == 0)
8181 {
8182# ifdef FEAT_FOLDING
8183 for (shl->first_lnum = lnum;
8184 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8185 if (hasFoldingWin(wp, shl->first_lnum - 1,
8186 NULL, NULL, TRUE, NULL))
8187 break;
8188# else
8189 shl->first_lnum = wp->w_topline;
8190# endif
8191 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008192 if (cur != NULL)
8193 cur->pos.cur = 0;
8194 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008196 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8197 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008199 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8200 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008201 pos_inprogress = cur == NULL || cur->pos.cur == 0
8202 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 if (shl->lnum != 0)
8204 {
8205 shl->first_lnum = shl->lnum
8206 + shl->rm.endpos[0].lnum
8207 - shl->rm.startpos[0].lnum;
8208 n = shl->rm.endpos[0].col;
8209 }
8210 else
8211 {
8212 ++shl->first_lnum;
8213 n = 0;
8214 }
8215 }
8216 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008217 if (shl != &search_hl && cur != NULL)
8218 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220}
8221
8222/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008223 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 * Uses shl->buf.
8225 * Sets shl->lnum and shl->rm contents.
8226 * Note: Assumes a previous match is always before "lnum", unless
8227 * shl->lnum is zero.
8228 * Careful: Any pointers for buffer lines will become invalid.
8229 */
8230 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008231next_search_hl(
8232 win_T *win,
8233 match_T *shl, /* points to search_hl or a match */
8234 linenr_T lnum,
8235 colnr_T mincol, /* minimal column for a match */
8236 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237{
8238 linenr_T l;
8239 colnr_T matchcol;
8240 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008241 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008243 // for :{range}s/pat only highlight inside the range
8244 if (lnum < search_first_line || lnum > search_last_line)
8245 {
8246 shl->lnum = 0;
8247 return;
8248 }
8249
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (shl->lnum != 0)
8251 {
8252 /* Check for three situations:
8253 * 1. If the "lnum" is below a previous match, start a new search.
8254 * 2. If the previous match includes "mincol", use it.
8255 * 3. Continue after the previous match.
8256 */
8257 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8258 if (lnum > l)
8259 shl->lnum = 0;
8260 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8261 return;
8262 }
8263
8264 /*
8265 * Repeat searching for a match until one is found that includes "mincol"
8266 * or none is found in this line.
8267 */
8268 called_emsg = FALSE;
8269 for (;;)
8270 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008271#ifdef FEAT_RELTIME
8272 /* Stop searching after passing the time limit. */
8273 if (profile_passed_limit(&(shl->tm)))
8274 {
8275 shl->lnum = 0; /* no match found in time */
8276 break;
8277 }
8278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 /* Three situations:
8280 * 1. No useful previous match: search from start of line.
8281 * 2. Not Vi compatible or empty match: continue at next character.
8282 * Break the loop if this is beyond the end of the line.
8283 * 3. Vi compatible searching: continue at end of previous match.
8284 */
8285 if (shl->lnum == 0)
8286 matchcol = 0;
8287 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8288 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008289 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008291 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008292
8293 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008294 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008295 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008297 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 shl->lnum = 0;
8299 break;
8300 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008301 if (has_mbyte)
8302 matchcol += mb_ptr2len(ml);
8303 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008304 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 }
8306 else
8307 matchcol = shl->rm.endpos[0].col;
8308
8309 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008310 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008312 /* Remember whether shl->rm is using a copy of the regprog in
8313 * cur->match. */
8314 int regprog_is_copy = (shl != &search_hl && cur != NULL
8315 && shl == &cur->hl
8316 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008317 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008318
Bram Moolenaarb3414592014-06-17 17:48:32 +02008319 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8320 matchcol,
8321#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008322 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008323#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008324 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008325#endif
8326 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008327 /* Copy the regprog, in case it got freed and recompiled. */
8328 if (regprog_is_copy)
8329 cur->match.regprog = cur->hl.rm.regprog;
8330
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008331 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008332 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008333 /* Error while handling regexp: stop using this regexp. */
8334 if (shl == &search_hl)
8335 {
8336 /* don't free regprog in the match list, it's a copy */
8337 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008338 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008339 }
8340 shl->rm.regprog = NULL;
8341 shl->lnum = 0;
8342 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8343 message */
8344 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008345 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008346 }
8347 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008348 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008349 else
8350 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (nmatched == 0)
8352 {
8353 shl->lnum = 0; /* no match found */
8354 break;
8355 }
8356 if (shl->rm.startpos[0].lnum > 0
8357 || shl->rm.startpos[0].col >= mincol
8358 || nmatched > 1
8359 || shl->rm.endpos[0].col > mincol)
8360 {
8361 shl->lnum += shl->rm.startpos[0].lnum;
8362 break; /* useful match found */
8363 }
8364 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008365
8366 // Restore called_emsg for assert_fails().
8367 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
Bram Moolenaar85077472016-10-16 14:35:48 +02008370/*
8371 * If there is a match fill "shl" and return one.
8372 * Return zero otherwise.
8373 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008374 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008375next_search_hl_pos(
8376 match_T *shl, /* points to a match */
8377 linenr_T lnum,
8378 posmatch_T *posmatch, /* match positions */
8379 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008380{
8381 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008382 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008383
Bram Moolenaarb3414592014-06-17 17:48:32 +02008384 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8385 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008386 llpos_T *pos = &posmatch->pos[i];
8387
8388 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008389 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008390 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008391 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008392 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008393 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008394 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008395 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008396 /* if this match comes before the one at "found" then swap
8397 * them */
8398 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008399 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008400 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008401
Bram Moolenaar85077472016-10-16 14:35:48 +02008402 *pos = posmatch->pos[found];
8403 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008404 }
8405 }
8406 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008407 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008408 }
8409 }
8410 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008411 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008412 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008413 colnr_T start = posmatch->pos[found].col == 0
8414 ? 0 : posmatch->pos[found].col - 1;
8415 colnr_T end = posmatch->pos[found].col == 0
8416 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008417
Bram Moolenaar85077472016-10-16 14:35:48 +02008418 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008419 shl->rm.startpos[0].lnum = 0;
8420 shl->rm.startpos[0].col = start;
8421 shl->rm.endpos[0].lnum = 0;
8422 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008423 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008424 posmatch->cur = found + 1;
8425 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008426 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008427 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008428}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008429#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008430
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008432screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433{
8434 attrentry_T *aep = NULL;
8435
8436 screen_attr = attr;
8437 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008438#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 && termcap_active
8440#endif
8441 )
8442 {
8443#ifdef FEAT_GUI
8444 if (gui.in_use)
8445 {
8446 char buf[20];
8447
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008448 /* The GUI handles this internally. */
8449 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 OUT_STR(buf);
8451 }
8452 else
8453#endif
8454 {
8455 if (attr > HL_ALL) /* special HL attr. */
8456 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008457 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 aep = syn_cterm_attr2entry(attr);
8459 else
8460 aep = syn_term_attr2entry(attr);
8461 if (aep == NULL) /* did ":syntax clear" */
8462 attr = 0;
8463 else
8464 attr = aep->ae_attr;
8465 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008466 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008468 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008469#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008470 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8471 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8472 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008473#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008474 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008475 /* If the Normal FG color has BOLD attribute and the new HL
8476 * has a FG color defined, clear BOLD. */
8477 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008478 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008480 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008481 out_str(T_UCS);
8482 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008483 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8484 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008486 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008488 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008490 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008491 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
8493 /*
8494 * Output the color or start string after bold etc., in case the
8495 * bold etc. override the color setting.
8496 */
8497 if (aep != NULL)
8498 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008499#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008500 /* When 'termguicolors' is set but fg or bg is unset,
8501 * fall back to the cterm colors. This helps for SpellBad,
8502 * where the GUI uses a red undercurl. */
8503 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008505 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008506 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008507 }
8508 else
8509#endif
8510 if (t_colors > 1)
8511 {
8512 if (aep->ae_u.cterm.fg_color)
8513 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8514 }
8515#ifdef FEAT_TERMGUICOLORS
8516 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8517 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008518 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008519 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 }
8521 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008522#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008523 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008525 if (aep->ae_u.cterm.bg_color)
8526 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8527 }
8528
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008529 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008530 {
8531 if (aep->ae_u.term.start != NULL)
8532 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 }
8534 }
8535 }
8536 }
8537}
8538
8539 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008540screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
8542 int do_ME = FALSE; /* output T_ME code */
8543
8544 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008545#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 && termcap_active
8547#endif
8548 )
8549 {
8550#ifdef FEAT_GUI
8551 if (gui.in_use)
8552 {
8553 char buf[20];
8554
8555 /* use internal GUI code */
8556 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8557 OUT_STR(buf);
8558 }
8559 else
8560#endif
8561 {
8562 if (screen_attr > HL_ALL) /* special HL attr. */
8563 {
8564 attrentry_T *aep;
8565
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008566 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {
8568 /*
8569 * Assume that t_me restores the original colors!
8570 */
8571 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008572 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008573#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008574 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8575 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8576 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008577#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008578 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008579#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008580 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8581 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8582 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008583#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008584 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 do_ME = TRUE;
8586 }
8587 else
8588 {
8589 aep = syn_term_attr2entry(screen_attr);
8590 if (aep != NULL && aep->ae_u.term.stop != NULL)
8591 {
8592 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8593 do_ME = TRUE;
8594 else
8595 out_str(aep->ae_u.term.stop);
8596 }
8597 }
8598 if (aep == NULL) /* did ":syntax clear" */
8599 screen_attr = 0;
8600 else
8601 screen_attr = aep->ae_attr;
8602 }
8603
8604 /*
8605 * Often all ending-codes are equal to T_ME. Avoid outputting the
8606 * same sequence several times.
8607 */
8608 if (screen_attr & HL_STANDOUT)
8609 {
8610 if (STRCMP(T_SE, T_ME) == 0)
8611 do_ME = TRUE;
8612 else
8613 out_str(T_SE);
8614 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008615 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008616 {
8617 if (STRCMP(T_UCE, T_ME) == 0)
8618 do_ME = TRUE;
8619 else
8620 out_str(T_UCE);
8621 }
8622 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008623 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {
8625 if (STRCMP(T_UE, T_ME) == 0)
8626 do_ME = TRUE;
8627 else
8628 out_str(T_UE);
8629 }
8630 if (screen_attr & HL_ITALIC)
8631 {
8632 if (STRCMP(T_CZR, T_ME) == 0)
8633 do_ME = TRUE;
8634 else
8635 out_str(T_CZR);
8636 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008637 if (screen_attr & HL_STRIKETHROUGH)
8638 {
8639 if (STRCMP(T_STE, T_ME) == 0)
8640 do_ME = TRUE;
8641 else
8642 out_str(T_STE);
8643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8645 out_str(T_ME);
8646
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008647#ifdef FEAT_TERMGUICOLORS
8648 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008650 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008651 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008652 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008653 term_bg_rgb_color(cterm_normal_bg_gui_color);
8654 }
8655 else
8656#endif
8657 {
8658 if (t_colors > 1)
8659 {
8660 /* set Normal cterm colors */
8661 if (cterm_normal_fg_color != 0)
8662 term_fg_color(cterm_normal_fg_color - 1);
8663 if (cterm_normal_bg_color != 0)
8664 term_bg_color(cterm_normal_bg_color - 1);
8665 if (cterm_normal_fg_bold)
8666 out_str(T_MD);
8667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 }
8669 }
8670 }
8671 screen_attr = 0;
8672}
8673
8674/*
8675 * Reset the colors for a cterm. Used when leaving Vim.
8676 * The machine specific code may override this again.
8677 */
8678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008679reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008681 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 {
8683 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008684#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008685 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8686 || cterm_normal_bg_gui_color != INVALCOLOR)
8687 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008688#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {
8692 out_str(T_OP);
8693 screen_attr = -1;
8694 }
8695 if (cterm_normal_fg_bold)
8696 {
8697 out_str(T_ME);
8698 screen_attr = -1;
8699 }
8700 }
8701}
8702
8703/*
8704 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8705 * using the attributes from ScreenAttrs["off"].
8706 */
8707 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008708screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
8710 int attr;
8711
8712 /* Check for illegal values, just in case (could happen just after
8713 * resizing). */
8714 if (row >= screen_Rows || col >= screen_Columns)
8715 return;
8716
Bram Moolenaarae654382019-01-17 21:09:05 +01008717#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02008718 // Skip if under the popup menu.
8719 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
8720 if (pum_under_menu(row, col)
8721# ifdef FEAT_TEXT_PROP
8722 && screen_zindex <= POPUPMENU_ZINDEX
8723# endif
8724 )
Bram Moolenaarae654382019-01-17 21:09:05 +01008725 return;
8726#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008727#ifdef FEAT_TEXT_PROP
8728 // Skip if under a(nother) popup.
8729 if (popup_mask[row * screen_Columns + col] > screen_zindex)
8730 return;
8731#endif
8732
Bram Moolenaar494838a2015-02-10 19:20:37 +01008733 /* Outputting a character in the last cell on the screen may scroll the
8734 * screen up. Only do it when the "xn" termcap property is set, otherwise
8735 * mark the character invalid (update it when scrolled up). */
8736 if (*T_XN == NUL
8737 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#ifdef FEAT_RIGHTLEFT
8739 /* account for first command-line character in rightleft mode */
8740 && !cmdmsg_rl
8741#endif
8742 )
8743 {
8744 ScreenAttrs[off] = (sattr_T)-1;
8745 return;
8746 }
8747
8748 /*
8749 * Stop highlighting first, so it's easier to move the cursor.
8750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 if (screen_char_attr != 0)
8752 attr = screen_char_attr;
8753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 attr = ScreenAttrs[off];
8755 if (screen_attr != attr)
8756 screen_stop_highlight();
8757
8758 windgoto(row, col);
8759
8760 if (screen_attr != attr)
8761 screen_start_highlight(attr);
8762
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 if (enc_utf8 && ScreenLinesUC[off] != 0)
8764 {
8765 char_u buf[MB_MAXBYTES + 1];
8766
Bram Moolenaarcb070082016-04-02 22:14:51 +02008767 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008768 {
8769 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008770#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008771 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008772#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008773 )
8774 {
8775 /* Clear the two screen cells. If the character is actually
8776 * single width it won't change the second cell. */
8777 out_str((char_u *)" ");
8778 term_windgoto(row, col);
8779 }
8780 /* not sure where the cursor is after drawing the ambiguous width
8781 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008782 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008783 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008784 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008786
8787 /* Convert the UTF-8 character to bytes and write it. */
8788 buf[utfc_char2bytes(off, buf)] = NUL;
8789 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
8791 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 /* double-byte character in single-width cell */
8796 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8797 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 }
8799
8800 screen_cur_col++;
8801}
8802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803/*
8804 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8805 * on the screen at position 'row' and 'col'.
8806 * The attributes of the first byte is used for all. This is required to
8807 * output the two bytes of a double-byte character with nothing in between.
8808 */
8809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008810screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 /* Check for illegal values (could be wrong when screen was resized). */
8813 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8814 return;
8815
8816 /* Outputting the last character on the screen may scrollup the screen.
8817 * Don't to it! Mark the character invalid (update it when scrolled up) */
8818 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8819 {
8820 ScreenAttrs[off] = (sattr_T)-1;
8821 return;
8822 }
8823
8824 /* Output the first byte normally (positions the cursor), then write the
8825 * second byte directly. */
8826 screen_char(off, row, col);
8827 out_char(ScreenLines[off + 1]);
8828 ++screen_cur_col;
8829}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831/*
8832 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8833 * This uses the contents of ScreenLines[] and doesn't change it.
8834 */
8835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008836screen_draw_rectangle(
8837 int row,
8838 int col,
8839 int height,
8840 int width,
8841 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 int r, c;
8844 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008845 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008847 /* Can't use ScreenLines unless initialized */
8848 if (ScreenLines == NULL)
8849 return;
8850
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (invert)
8852 screen_char_attr = HL_INVERSE;
8853 for (r = row; r < row + height; ++r)
8854 {
8855 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008856 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 for (c = col; c < col + width; ++c)
8858 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008859 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
8861 screen_char_2(off + c, r, c);
8862 ++c;
8863 }
8864 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
8866 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008867 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870 }
8871 }
8872 screen_char_attr = 0;
8873}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875/*
8876 * Redraw the characters for a vertically split window.
8877 */
8878 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008879redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 int col;
8882 int width;
8883
8884# ifdef FEAT_CLIPBOARD
8885 clip_may_clear_selection(row, end - 1);
8886# endif
8887
8888 if (wp == NULL)
8889 {
8890 col = 0;
8891 width = Columns;
8892 }
8893 else
8894 {
8895 col = wp->w_wincol;
8896 width = wp->w_width;
8897 }
8898 screen_draw_rectangle(row, col, end - row, width, FALSE);
8899}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008901 static void
8902space_to_screenline(int off, int attr)
8903{
8904 ScreenLines[off] = ' ';
8905 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008906 if (enc_utf8)
8907 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008908}
8909
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910/*
8911 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8912 * with character 'c1' in first column followed by 'c2' in the other columns.
8913 * Use attributes 'attr'.
8914 */
8915 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008916screen_fill(
8917 int start_row,
8918 int end_row,
8919 int start_col,
8920 int end_col,
8921 int c1,
8922 int c2,
8923 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 int row;
8926 int col;
8927 int off;
8928 int end_off;
8929 int did_delete;
8930 int c;
8931 int norm_term;
8932#if defined(FEAT_GUI) || defined(UNIX)
8933 int force_next = FALSE;
8934#endif
8935
8936 if (end_row > screen_Rows) /* safety check */
8937 end_row = screen_Rows;
8938 if (end_col > screen_Columns) /* safety check */
8939 end_col = screen_Columns;
8940 if (ScreenLines == NULL
8941 || start_row >= end_row
8942 || start_col >= end_col) /* nothing to do */
8943 return;
8944
8945 /* it's a "normal" terminal when not in a GUI or cterm */
8946 norm_term = (
8947#ifdef FEAT_GUI
8948 !gui.in_use &&
8949#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008950 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 for (row = start_row; row < end_row; ++row)
8952 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008953 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008954#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008955 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008956#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008957 )
8958 {
8959 /* When drawing over the right halve of a double-wide char clear
8960 * out the left halve. When drawing over the left halve of a
8961 * double wide-char clear out the right halve. Only needed in a
8962 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008963 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008964 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008965 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008966 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 /*
8969 * Try to use delete-line termcap code, when no attributes or in a
8970 * "normal" terminal, where a bold/italic space is just a
8971 * space.
8972 */
8973 did_delete = FALSE;
8974 if (c2 == ' '
8975 && end_col == Columns
8976 && can_clear(T_CE)
8977 && (attr == 0
8978 || (norm_term
8979 && attr <= HL_ALL
8980 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8981 {
8982 /*
8983 * check if we really need to clear something
8984 */
8985 col = start_col;
8986 if (c1 != ' ') /* don't clear first char */
8987 ++col;
8988
8989 off = LineOffset[row] + col;
8990 end_off = LineOffset[row] + end_col;
8991
8992 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 if (enc_utf8)
8994 while (off < end_off && ScreenLines[off] == ' '
8995 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8996 ++off;
8997 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 while (off < end_off && ScreenLines[off] == ' '
8999 && ScreenAttrs[off] == 0)
9000 ++off;
9001 if (off < end_off) /* something to be cleared */
9002 {
9003 col = off - LineOffset[row];
9004 screen_stop_highlight();
9005 term_windgoto(row, col);/* clear rest of this screen line */
9006 out_str(T_CE);
9007 screen_start(); /* don't know where cursor is now */
9008 col = end_col - col;
9009 while (col--) /* clear chars in ScreenLines */
9010 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02009011 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 ++off;
9013 }
9014 }
9015 did_delete = TRUE; /* the chars are cleared now */
9016 }
9017
9018 off = LineOffset[row] + start_col;
9019 c = c1;
9020 for (col = start_col; col < end_col; ++col)
9021 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02009022 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009023 || (enc_utf8 && (int)ScreenLinesUC[off]
9024 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 || ScreenAttrs[off] != attr
9026#if defined(FEAT_GUI) || defined(UNIX)
9027 || force_next
9028#endif
9029 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02009030#ifdef FEAT_TEXT_PROP
9031 // Skip if under a(nother) popup.
9032 && popup_mask[row * screen_Columns + col] <= screen_zindex
9033#endif
9034 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 {
9036#if defined(FEAT_GUI) || defined(UNIX)
9037 /* The bold trick may make a single row of pixels appear in
9038 * the next character. When a bold character is removed, the
9039 * next character should be redrawn too. This happens for our
9040 * own GUI and for some xterms. */
9041 if (
9042# ifdef FEAT_GUI
9043 gui.in_use
9044# endif
9045# if defined(FEAT_GUI) && defined(UNIX)
9046 ||
9047# endif
9048# ifdef UNIX
9049 term_is_xterm
9050# endif
9051 )
9052 {
9053 if (ScreenLines[off] != ' '
9054 && (ScreenAttrs[off] > HL_ALL
9055 || ScreenAttrs[off] & HL_BOLD))
9056 force_next = TRUE;
9057 else
9058 force_next = FALSE;
9059 }
9060#endif
9061 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 if (enc_utf8)
9063 {
9064 if (c >= 0x80)
9065 {
9066 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009067 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 }
9069 else
9070 ScreenLinesUC[off] = 0;
9071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 ScreenAttrs[off] = attr;
9073 if (!did_delete || c != ' ')
9074 screen_char(off, row, col);
9075 }
9076 ++off;
9077 if (col == start_col)
9078 {
9079 if (did_delete)
9080 break;
9081 c = c2;
9082 }
9083 }
9084 if (end_col == Columns)
9085 LineWraps[row] = FALSE;
9086 if (row == Rows - 1) /* overwritten the command line */
9087 {
9088 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02009089 if (start_col == 0 && end_col == Columns
9090 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009092 if (start_col == 0)
9093 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 }
9095 }
9096}
9097
9098/*
9099 * Check if there should be a delay. Used before clearing or redrawing the
9100 * screen or the command line.
9101 */
9102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009103check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
9106 && !did_wait_return
9107 && emsg_silent == 0)
9108 {
9109 out_flush();
9110 ui_delay(1000L, TRUE);
9111 emsg_on_display = FALSE;
9112 if (check_msg_scroll)
9113 msg_scroll = FALSE;
9114 }
9115}
9116
9117/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009118 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
9119 */
9120 static void
9121clear_TabPageIdxs(void)
9122{
9123 int scol;
9124
9125 for (scol = 0; scol < Columns; ++scol)
9126 TabPageIdxs[scol] = 0;
9127}
9128
9129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009131 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 * Returns TRUE if there is a valid screen to write to.
9133 * Returns FALSE when starting up and screen not initialized yet.
9134 */
9135 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009136screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009138 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 return (ScreenLines != NULL);
9140}
9141
9142/*
9143 * Resize the shell to Rows and Columns.
9144 * Allocate ScreenLines[] and associated items.
9145 *
9146 * There may be some time between setting Rows and Columns and (re)allocating
9147 * ScreenLines[]. This happens when starting up and when (manually) changing
9148 * the shell size. Always use screen_Rows and screen_Columns to access items
9149 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
9150 * final size of the shell is needed.
9151 */
9152 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009153screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 int new_row, old_row;
9156#ifdef FEAT_GUI
9157 int old_Rows;
9158#endif
9159 win_T *wp;
9160 int outofmem = FALSE;
9161 int len;
9162 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009164 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009166 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 sattr_T *new_ScreenAttrs;
9168 unsigned *new_LineOffset;
9169 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009170 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009171#ifdef FEAT_TEXT_PROP
9172 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009173 short *new_popup_mask_next;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009174#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00009175 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009177 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009178 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009180retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 /*
9182 * Allocation of the screen buffers is done only when the size changes and
9183 * when Rows and Columns have been set and we have started doing full
9184 * screen stuff.
9185 */
9186 if ((ScreenLines != NULL
9187 && Rows == screen_Rows
9188 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 && enc_utf8 == (ScreenLinesUC != NULL)
9190 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009191 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 || Rows == 0
9193 || Columns == 0
9194 || (!full_screen && ScreenLines == NULL))
9195 return;
9196
9197 /*
9198 * It's possible that we produce an out-of-memory message below, which
9199 * will cause this function to be called again. To break the loop, just
9200 * return here.
9201 */
9202 if (entered)
9203 return;
9204 entered = TRUE;
9205
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009206 /*
9207 * Note that the window sizes are updated before reallocating the arrays,
9208 * thus we must not redraw here!
9209 */
9210 ++RedrawingDisabled;
9211
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 win_new_shellsize(); /* fit the windows in the new sized shell */
9213
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 comp_col(); /* recompute columns for shown command and ruler */
9215
9216 /*
9217 * We're changing the size of the screen.
9218 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9219 * - Move lines from the old arrays into the new arrays, clear extra
9220 * lines (unless the screen is going to be cleared).
9221 * - Free the old arrays.
9222 *
9223 * If anything fails, make ScreenLines NULL, so we don't do anything!
9224 * Continuing with the old ScreenLines may result in a crash, because the
9225 * size is wrong.
9226 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009227 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009229 if (aucmd_win != NULL)
9230 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009231#ifdef FEAT_TEXT_PROP
9232 // global popup windows
9233 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9234 win_free_lsize(wp);
9235 // tab-local popup windows
9236 FOR_ALL_TABPAGES(tp)
9237 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9238 win_free_lsize(wp);
9239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009241 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009242 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 if (enc_utf8)
9244 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009245 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009246 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009247 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9248 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009251 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9252 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9253 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9254 new_LineWraps = LALLOC_MULT(char_u, Rows);
9255 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009256#ifdef FEAT_TEXT_PROP
9257 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009258 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009261 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
9263 if (win_alloc_lines(wp) == FAIL)
9264 {
9265 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009266 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 }
9268 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009269 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9270 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009271 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009272#ifdef FEAT_TEXT_PROP
9273 // global popup windows
9274 for (wp = 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 // tab-local popup windows
9281 FOR_ALL_TABPAGES(tp)
9282 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9283 if (win_alloc_lines(wp) == FAIL)
9284 {
9285 outofmem = TRUE;
9286 goto give_up;
9287 }
9288#endif
9289
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009290give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009292 for (i = 0; i < p_mco; ++i)
9293 if (new_ScreenLinesC[i] == NULL)
9294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009296 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 || new_ScreenAttrs == NULL
9299 || new_LineOffset == NULL
9300 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009301 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009302#ifdef FEAT_TEXT_PROP
9303 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009304 || new_popup_mask_next == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02009305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 || outofmem)
9307 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009308 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009309 {
9310 /* guess the size */
9311 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9312
9313 /* Remember we did this to avoid getting outofmem messages over
9314 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009315 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009316 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009317 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009318 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009319 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009320 VIM_CLEAR(new_ScreenLinesC[i]);
9321 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009322 VIM_CLEAR(new_ScreenAttrs);
9323 VIM_CLEAR(new_LineOffset);
9324 VIM_CLEAR(new_LineWraps);
9325 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009326#ifdef FEAT_TEXT_PROP
9327 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009328 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 }
9331 else
9332 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009333 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009334
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 for (new_row = 0; new_row < Rows; ++new_row)
9336 {
9337 new_LineOffset[new_row] = new_row * Columns;
9338 new_LineWraps[new_row] = FALSE;
9339
9340 /*
9341 * If the screen is not going to be cleared, copy as much as
9342 * possible from the old screen to the new one and clear the rest
9343 * (used when resizing the window at the "--more--" prompt or when
9344 * executing an external command, for the GUI).
9345 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009346 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
9348 (void)vim_memset(new_ScreenLines + new_row * Columns,
9349 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 if (enc_utf8)
9351 {
9352 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9353 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009354 for (i = 0; i < p_mco; ++i)
9355 (void)vim_memset(new_ScreenLinesC[i]
9356 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 0, (size_t)Columns * sizeof(u8char_T));
9358 }
9359 if (enc_dbcs == DBCS_JPNU)
9360 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9361 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9363 0, (size_t)Columns * sizeof(sattr_T));
9364 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009365 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 {
9367 if (screen_Columns < Columns)
9368 len = screen_Columns;
9369 else
9370 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009371 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009372 * may be invalid now. Also when p_mco changes. */
9373 if (!(enc_utf8 && ScreenLinesUC == NULL)
9374 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009375 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9376 ScreenLines + LineOffset[old_row],
9377 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009378 if (enc_utf8 && ScreenLinesUC != NULL
9379 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 {
9381 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9382 ScreenLinesUC + LineOffset[old_row],
9383 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009384 for (i = 0; i < p_mco; ++i)
9385 mch_memmove(new_ScreenLinesC[i]
9386 + new_LineOffset[new_row],
9387 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 (size_t)len * sizeof(u8char_T));
9389 }
9390 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9391 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9392 ScreenLines2 + LineOffset[old_row],
9393 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9395 ScreenAttrs + LineOffset[old_row],
9396 (size_t)len * sizeof(sattr_T));
9397 }
9398 }
9399 }
9400 /* Use the last line of the screen for the current line. */
9401 current_ScreenLine = new_ScreenLines + Rows * Columns;
9402 }
9403
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009404 free_screenlines();
9405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009408 for (i = 0; i < p_mco; ++i)
9409 ScreenLinesC[i] = new_ScreenLinesC[i];
9410 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 ScreenAttrs = new_ScreenAttrs;
9413 LineOffset = new_LineOffset;
9414 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009415 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02009416#ifdef FEAT_TEXT_PROP
9417 popup_mask = new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009418 popup_mask_next = new_popup_mask_next;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02009419 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaar33796b32019-06-08 16:01:13 +02009420 popup_mask_refresh = TRUE;
9421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422
9423 /* It's important that screen_Rows and screen_Columns reflect the actual
9424 * size of ScreenLines[]. Set them before calling anything. */
9425#ifdef FEAT_GUI
9426 old_Rows = screen_Rows;
9427#endif
9428 screen_Rows = Rows;
9429 screen_Columns = Columns;
9430
9431 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009432 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434#ifdef FEAT_GUI
9435 else if (gui.in_use
9436 && !gui.starting
9437 && ScreenLines != NULL
9438 && old_Rows != Rows)
9439 {
9440 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9441 /*
9442 * Adjust the position of the cursor, for when executing an external
9443 * command.
9444 */
9445 if (msg_row >= Rows) /* Rows got smaller */
9446 msg_row = Rows - 1; /* put cursor at last row */
9447 else if (Rows > old_Rows) /* Rows got bigger */
9448 msg_row += Rows - old_Rows; /* put cursor in same place */
9449 if (msg_col >= Columns) /* Columns got smaller */
9450 msg_col = Columns - 1; /* put cursor at last column */
9451 }
9452#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009453 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009456 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009457
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009458 /*
9459 * Do not apply autocommands more than 3 times to avoid an endless loop
9460 * in case applying autocommands always changes Rows or Columns.
9461 */
9462 if (starting == 0 && ++retry_count <= 3)
9463 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009464 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009465 /* In rare cases, autocommands may have altered Rows or Columns,
9466 * jump back to check if we need to allocate the screen again. */
9467 goto retry;
9468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009472free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009473{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009474 int i;
9475
Bram Moolenaar33796b32019-06-08 16:01:13 +02009476 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009477 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009478 VIM_CLEAR(ScreenLinesC[i]);
9479 VIM_CLEAR(ScreenLines2);
9480 VIM_CLEAR(ScreenLines);
9481 VIM_CLEAR(ScreenAttrs);
9482 VIM_CLEAR(LineOffset);
9483 VIM_CLEAR(LineWraps);
9484 VIM_CLEAR(TabPageIdxs);
9485#ifdef FEAT_TEXT_PROP
9486 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009487 VIM_CLEAR(popup_mask_next);
Bram Moolenaar33796b32019-06-08 16:01:13 +02009488#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009489}
9490
9491 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009492screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493{
9494 check_for_delay(FALSE);
9495 screenalloc(FALSE); /* allocate screen buffers if size changed */
9496 screenclear2(); /* clear the screen */
9497}
9498
9499 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009500screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 int i;
9503
9504 if (starting == NO_SCREEN || ScreenLines == NULL
9505#ifdef FEAT_GUI
9506 || (gui.in_use && gui.starting)
9507#endif
9508 )
9509 return;
9510
9511#ifdef FEAT_GUI
9512 if (!gui.in_use)
9513#endif
9514 screen_attr = -1; /* force setting the Normal colors */
9515 screen_stop_highlight(); /* don't want highlighting here */
9516
9517#ifdef FEAT_CLIPBOARD
9518 /* disable selection without redrawing it */
9519 clip_scroll_selection(9999);
9520#endif
9521
9522 /* blank out ScreenLines */
9523 for (i = 0; i < Rows; ++i)
9524 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009525 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 LineWraps[i] = FALSE;
9527 }
9528
9529 if (can_clear(T_CL))
9530 {
9531 out_str(T_CL); /* clear the display */
9532 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009533 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 }
9535 else
9536 {
9537 /* can't clear the screen, mark all chars with invalid attributes */
9538 for (i = 0; i < Rows; ++i)
9539 lineinvalid(LineOffset[i], (int)Columns);
9540 clear_cmdline = TRUE;
9541 }
9542
9543 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9544
9545 win_rest_invalid(firstwin);
9546 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009547 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 if (must_redraw == CLEAR) /* no need to clear again */
9549 must_redraw = NOT_VALID;
9550 compute_cmdrow();
9551 msg_row = cmdline_row; /* put cursor on last line for messages */
9552 msg_col = 0;
9553 screen_start(); /* don't know where cursor is now */
9554 msg_scrolled = 0; /* can't scroll back */
9555 msg_didany = FALSE;
9556 msg_didout = FALSE;
9557}
9558
9559/*
9560 * Clear one line in ScreenLines.
9561 */
9562 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009563lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 if (enc_utf8)
9567 (void)vim_memset(ScreenLinesUC + off, 0,
9568 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009569 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570}
9571
9572/*
9573 * Mark one line in ScreenLines invalid by setting the attributes to an
9574 * invalid value.
9575 */
9576 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009577lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9580}
9581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582/*
9583 * Copy part of a Screenline for vertically split window "wp".
9584 */
9585 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009586linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 unsigned off_to = LineOffset[to] + wp->w_wincol;
9589 unsigned off_from = LineOffset[from] + wp->w_wincol;
9590
9591 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9592 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 if (enc_utf8)
9594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009595 int i;
9596
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9598 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009599 for (i = 0; i < p_mco; ++i)
9600 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9601 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602 }
9603 if (enc_dbcs == DBCS_JPNU)
9604 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9605 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9607 wp->w_width * sizeof(sattr_T));
9608}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609
9610/*
9611 * Return TRUE if clearing with term string "p" would work.
9612 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02009613 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 */
9615 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009616can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618 return (*p != NUL && (t_colors <= 1
9619#ifdef FEAT_GUI
9620 || gui.in_use
9621#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009622#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009623 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009624 || (!p_tgc && cterm_normal_bg_color == 0)
9625#else
9626 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009627#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009628 || *T_UT != NUL)
9629#ifdef FEAT_TEXT_PROP
9630 && !(p == T_CE && popup_visible)
9631#endif
9632 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633}
9634
9635/*
9636 * Reset cursor position. Use whenever cursor was moved because of outputting
9637 * something directly to the screen (shell commands) or a terminal control
9638 * code.
9639 */
9640 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009641screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642{
9643 screen_cur_row = screen_cur_col = 9999;
9644}
9645
9646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 * Move the cursor to position "row","col" in the screen.
9648 * This tries to find the most efficient way to move, minimizing the number of
9649 * characters sent to the terminal.
9650 */
9651 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009652windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009654 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 int i;
9656 int plan;
9657 int cost;
9658 int wouldbe_col;
9659 int noinvcurs;
9660 char_u *bs;
9661 int goto_cost;
9662 int attr;
9663
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009664#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9666
9667#define PLAN_LE 1
9668#define PLAN_CR 2
9669#define PLAN_NL 3
9670#define PLAN_WRITE 4
9671 /* Can't use ScreenLines unless initialized */
9672 if (ScreenLines == NULL)
9673 return;
9674
9675 if (col != screen_cur_col || row != screen_cur_row)
9676 {
9677 /* Check for valid position. */
9678 if (row < 0) /* window without text lines? */
9679 row = 0;
9680 if (row >= screen_Rows)
9681 row = screen_Rows - 1;
9682 if (col >= screen_Columns)
9683 col = screen_Columns - 1;
9684
9685 /* check if no cursor movement is allowed in highlight mode */
9686 if (screen_attr && *T_MS == NUL)
9687 noinvcurs = HIGHL_COST;
9688 else
9689 noinvcurs = 0;
9690 goto_cost = GOTO_COST + noinvcurs;
9691
9692 /*
9693 * Plan how to do the positioning:
9694 * 1. Use CR to move it to column 0, same row.
9695 * 2. Use T_LE to move it a few columns to the left.
9696 * 3. Use NL to move a few lines down, column 0.
9697 * 4. Move a few columns to the right with T_ND or by writing chars.
9698 *
9699 * Don't do this if the cursor went beyond the last column, the cursor
9700 * position is unknown then (some terminals wrap, some don't )
9701 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009702 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 * characters to move the cursor to the right.
9704 */
9705 if (row >= screen_cur_row && screen_cur_col < Columns)
9706 {
9707 /*
9708 * If the cursor is in the same row, bigger col, we can use CR
9709 * or T_LE.
9710 */
9711 bs = NULL; /* init for GCC */
9712 attr = screen_attr;
9713 if (row == screen_cur_row && col < screen_cur_col)
9714 {
9715 /* "le" is preferred over "bc", because "bc" is obsolete */
9716 if (*T_LE)
9717 bs = T_LE; /* "cursor left" */
9718 else
9719 bs = T_BC; /* "backspace character (old) */
9720 if (*bs)
9721 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9722 else
9723 cost = 999;
9724 if (col + 1 < cost) /* using CR is less characters */
9725 {
9726 plan = PLAN_CR;
9727 wouldbe_col = 0;
9728 cost = 1; /* CR is just one character */
9729 }
9730 else
9731 {
9732 plan = PLAN_LE;
9733 wouldbe_col = col;
9734 }
9735 if (noinvcurs) /* will stop highlighting */
9736 {
9737 cost += noinvcurs;
9738 attr = 0;
9739 }
9740 }
9741
9742 /*
9743 * If the cursor is above where we want to be, we can use CR LF.
9744 */
9745 else if (row > screen_cur_row)
9746 {
9747 plan = PLAN_NL;
9748 wouldbe_col = 0;
9749 cost = (row - screen_cur_row) * 2; /* CR LF */
9750 if (noinvcurs) /* will stop highlighting */
9751 {
9752 cost += noinvcurs;
9753 attr = 0;
9754 }
9755 }
9756
9757 /*
9758 * If the cursor is in the same row, smaller col, just use write.
9759 */
9760 else
9761 {
9762 plan = PLAN_WRITE;
9763 wouldbe_col = screen_cur_col;
9764 cost = 0;
9765 }
9766
9767 /*
9768 * Check if any characters that need to be written have the
9769 * correct attributes. Also avoid UTF-8 characters.
9770 */
9771 i = col - wouldbe_col;
9772 if (i > 0)
9773 cost += i;
9774 if (cost < goto_cost && i > 0)
9775 {
9776 /*
9777 * Check if the attributes are correct without additionally
9778 * stopping highlighting.
9779 */
9780 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9781 while (i && *p++ == attr)
9782 --i;
9783 if (i != 0)
9784 {
9785 /*
9786 * Try if it works when highlighting is stopped here.
9787 */
9788 if (*--p == 0)
9789 {
9790 cost += noinvcurs;
9791 while (i && *p++ == 0)
9792 --i;
9793 }
9794 if (i != 0)
9795 cost = 999; /* different attributes, don't do it */
9796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 if (enc_utf8)
9798 {
9799 /* Don't use an UTF-8 char for positioning, it's slow. */
9800 for (i = wouldbe_col; i < col; ++i)
9801 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9802 {
9803 cost = 999;
9804 break;
9805 }
9806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808
9809 /*
9810 * We can do it without term_windgoto()!
9811 */
9812 if (cost < goto_cost)
9813 {
9814 if (plan == PLAN_LE)
9815 {
9816 if (noinvcurs)
9817 screen_stop_highlight();
9818 while (screen_cur_col > col)
9819 {
9820 out_str(bs);
9821 --screen_cur_col;
9822 }
9823 }
9824 else if (plan == PLAN_CR)
9825 {
9826 if (noinvcurs)
9827 screen_stop_highlight();
9828 out_char('\r');
9829 screen_cur_col = 0;
9830 }
9831 else if (plan == PLAN_NL)
9832 {
9833 if (noinvcurs)
9834 screen_stop_highlight();
9835 while (screen_cur_row < row)
9836 {
9837 out_char('\n');
9838 ++screen_cur_row;
9839 }
9840 screen_cur_col = 0;
9841 }
9842
9843 i = col - screen_cur_col;
9844 if (i > 0)
9845 {
9846 /*
9847 * Use cursor-right if it's one character only. Avoids
9848 * removing a line of pixels from the last bold char, when
9849 * using the bold trick in the GUI.
9850 */
9851 if (T_ND[0] != NUL && T_ND[1] == NUL)
9852 {
9853 while (i-- > 0)
9854 out_char(*T_ND);
9855 }
9856 else
9857 {
9858 int off;
9859
9860 off = LineOffset[row] + screen_cur_col;
9861 while (i-- > 0)
9862 {
9863 if (ScreenAttrs[off] != screen_attr)
9864 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 if (enc_dbcs == DBCS_JPNU
9868 && ScreenLines[off] == 0x8e)
9869 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 ++off;
9871 }
9872 }
9873 }
9874 }
9875 }
9876 else
9877 cost = 999;
9878
9879 if (cost >= goto_cost)
9880 {
9881 if (noinvcurs)
9882 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009883 if (row == screen_cur_row && (col > screen_cur_col)
9884 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 term_cursor_right(col - screen_cur_col);
9886 else
9887 term_windgoto(row, col);
9888 }
9889 screen_cur_row = row;
9890 screen_cur_col = col;
9891 }
9892}
9893
9894/*
9895 * Set cursor to its position in the current window.
9896 */
9897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009898setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009900 setcursor_mayforce(FALSE);
9901}
9902
9903/*
9904 * Set cursor to its position in the current window.
9905 * When "force" is TRUE also when not redrawing.
9906 */
9907 void
9908setcursor_mayforce(int force)
9909{
9910 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 {
9912 validate_cursor();
9913 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009914 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009916 /* With 'rightleft' set and the cursor on a double-wide
9917 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009918 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9919 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009920 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009921 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922#endif
9923 curwin->w_wcol));
9924 }
9925}
9926
9927
9928/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009929 * Insert 'line_count' lines at 'row' in window 'wp'.
9930 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9931 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 * scrolling.
9933 * Returns FAIL if the lines are not inserted, OK for success.
9934 */
9935 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009936win_ins_lines(
9937 win_T *wp,
9938 int row,
9939 int line_count,
9940 int invalid,
9941 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 int did_delete;
9944 int nextrow;
9945 int lastrow;
9946 int retval;
9947
9948 if (invalid)
9949 wp->w_lines_valid = 0;
9950
9951 if (wp->w_height < 5)
9952 return FAIL;
9953
9954 if (line_count > wp->w_height - row)
9955 line_count = wp->w_height - row;
9956
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009957 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (retval != MAYBE)
9959 return retval;
9960
9961 /*
9962 * If there is a next window or a status line, we first try to delete the
9963 * lines at the bottom to avoid messing what is after the window.
9964 * If this fails and there are following windows, don't do anything to avoid
9965 * messing up those windows, better just redraw.
9966 */
9967 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 if (wp->w_next != NULL || wp->w_status_height)
9969 {
9970 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009971 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 did_delete = TRUE;
9973 else if (wp->w_next)
9974 return FAIL;
9975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976 /*
9977 * if no lines deleted, blank the lines that will end up below the window
9978 */
9979 if (!did_delete)
9980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009983 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984 lastrow = nextrow + line_count;
9985 if (lastrow > Rows)
9986 lastrow = Rows;
9987 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009988 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 ' ', ' ', 0);
9990 }
9991
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009992 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 == FAIL)
9994 {
9995 /* deletion will have messed up other windows */
9996 if (did_delete)
9997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 win_rest_invalid(W_NEXT(wp));
10000 }
10001 return FAIL;
10002 }
10003
10004 return OK;
10005}
10006
10007/*
Bram Moolenaar86033562017-07-12 20:24:41 +020010008 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
10010 * If "mayclear" is TRUE the screen will be cleared if it is faster than
10011 * scrolling
10012 * Return OK for success, FAIL if the lines are not deleted.
10013 */
10014 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010015win_del_lines(
10016 win_T *wp,
10017 int row,
10018 int line_count,
10019 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010020 int mayclear,
10021 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022{
10023 int retval;
10024
10025 if (invalid)
10026 wp->w_lines_valid = 0;
10027
10028 if (line_count > wp->w_height - row)
10029 line_count = wp->w_height - row;
10030
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010031 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 if (retval != MAYBE)
10033 return retval;
10034
10035 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010036 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 return FAIL;
10038
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 /*
10040 * If there are windows or status lines below, try to put them at the
10041 * correct place. If we can't do that, they have to be redrawn.
10042 */
10043 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
10044 {
10045 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010046 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 {
10048 wp->w_redr_status = TRUE;
10049 win_rest_invalid(wp->w_next);
10050 }
10051 }
10052 /*
10053 * If this is the last window and there is no status line, redraw the
10054 * command line later.
10055 */
10056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 redraw_cmdline = TRUE;
10058 return OK;
10059}
10060
10061/*
10062 * Common code for win_ins_lines() and win_del_lines().
10063 * Returns OK or FAIL when the work has been done.
10064 * Returns MAYBE when not finished yet.
10065 */
10066 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010067win_do_lines(
10068 win_T *wp,
10069 int row,
10070 int line_count,
10071 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010072 int del,
10073 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
10075 int retval;
10076
10077 if (!redrawing() || line_count <= 0)
10078 return FAIL;
10079
Bram Moolenaar33796b32019-06-08 16:01:13 +020010080 // When inserting lines would result in loss of command output, just redraw
10081 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010082 if (no_win_do_lines_ins && !del)
10083 return FAIL;
10084
Bram Moolenaar33796b32019-06-08 16:01:13 +020010085 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +020010086 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010088 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +020010089 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 return FAIL;
10091 }
10092
Bram Moolenaar33796b32019-06-08 16:01:13 +020010093#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +020010094 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +020010095 if (popup_visible)
10096 return FAIL;
10097#endif
10098
10099 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 if (row + line_count >= wp->w_height)
10101 {
10102 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +020010103 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 ' ', ' ', 0);
10105 return OK;
10106 }
10107
10108 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010109 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010111 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +020010113 if (!no_win_do_lines_ins)
10114 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115
10116 /*
10117 * If the terminal can set a scroll region, use that.
10118 * Always do this in a vertically split window. This will redraw from
10119 * ScreenLines[] when t_CV isn't defined. That's faster than using
10120 * win_line().
10121 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010122 * a character in the lower right corner of the scroll region may cause a
10123 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 */
Bram Moolenaar02631462017-09-22 15:20:32 +020010125 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 {
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_set(wp, row);
10129 if (del)
10130 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010131 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 else
10133 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010134 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 scroll_region_reset();
10137 return retval;
10138 }
10139
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
10141 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142
10143 return MAYBE;
10144}
10145
10146/*
10147 * window 'wp' and everything after it is messed up, mark it for redraw
10148 */
10149 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010150win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 {
10154 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 wp->w_redr_status = TRUE;
10156 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 }
10158 redraw_cmdline = TRUE;
10159}
10160
10161/*
10162 * The rest of the routines in this file perform screen manipulations. The
10163 * given operation is performed physically on the screen. The corresponding
10164 * change is also made to the internal screen image. In this way, the editor
10165 * anticipates the effect of editing changes on the appearance of the screen.
10166 * That way, when we call screenupdate a complete redraw isn't usually
10167 * necessary. Another advantage is that we can keep adding code to anticipate
10168 * screen changes, and in the meantime, everything still works.
10169 */
10170
10171/*
10172 * types for inserting or deleting lines
10173 */
10174#define USE_T_CAL 1
10175#define USE_T_CDL 2
10176#define USE_T_AL 3
10177#define USE_T_CE 4
10178#define USE_T_DL 5
10179#define USE_T_SR 6
10180#define USE_NL 7
10181#define USE_T_CD 8
10182#define USE_REDRAW 9
10183
10184/*
10185 * insert lines on the screen and update ScreenLines[]
10186 * 'end' is the line after the scrolled part. Normally it is Rows.
10187 * When scrolling region used 'off' is the offset from the top for the region.
10188 * 'row' and 'end' are relative to the start of the region.
10189 *
10190 * return FAIL for failure, OK for success.
10191 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000010192 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010193screen_ins_lines(
10194 int off,
10195 int row,
10196 int line_count,
10197 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010198 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +010010199 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200{
10201 int i;
10202 int j;
10203 unsigned temp;
10204 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010205 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 int type;
10207 int result_empty;
10208 int can_ce = can_clear(T_CE);
10209
10210 /*
10211 * FAIL if
10212 * - there is no valid screen
10213 * - the screen has to be redrawn completely
10214 * - the line count is less than one
10215 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010216 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +020010217 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 */
Bram Moolenaar33796b32019-06-08 16:01:13 +020010219 if (!screen_valid(TRUE)
10220 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010221#ifdef FEAT_CLIPBOARD
10222 || (clip_star.state != SELECT_CLEARED
10223 && redrawing_for_callback > 0)
10224#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +020010225#ifdef FEAT_TEXT_PROP
10226 || popup_visible
10227#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010228 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 return FAIL;
10230
10231 /*
10232 * There are seven ways to insert lines:
10233 * 0. When in a vertically split window and t_CV isn't set, redraw the
10234 * characters from ScreenLines[].
10235 * 1. Use T_CD (clear to end of display) if it exists and the result of
10236 * the insert is just empty lines
10237 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10238 * present or line_count > 1. It looks better if we do all the inserts
10239 * at once.
10240 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10241 * insert is just empty lines and T_CE is not present or line_count >
10242 * 1.
10243 * 4. Use T_AL (insert line) if it exists.
10244 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10245 * just empty lines.
10246 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10247 * just empty lines.
10248 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10249 * the 'da' flag is not set or we have clear line capability.
10250 * 8. redraw the characters from ScreenLines[].
10251 *
10252 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10253 * the scrollbar for the window. It does have insert line, use that if it
10254 * exists.
10255 */
10256 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10258 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010259 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 type = USE_T_CD;
10261 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10262 type = USE_T_CAL;
10263 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10264 type = USE_T_CDL;
10265 else if (*T_AL != NUL)
10266 type = USE_T_AL;
10267 else if (can_ce && result_empty)
10268 type = USE_T_CE;
10269 else if (*T_DL != NUL && result_empty)
10270 type = USE_T_DL;
10271 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10272 type = USE_T_SR;
10273 else
10274 return FAIL;
10275
10276 /*
10277 * For clearing the lines screen_del_lines() is used. This will also take
10278 * care of t_db if necessary.
10279 */
10280 if (type == USE_T_CD || type == USE_T_CDL ||
10281 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010282 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283
10284 /*
10285 * If text is retained below the screen, first clear or delete as many
10286 * lines at the bottom of the window as are about to be inserted so that
10287 * the deleted lines won't later surface during a screen_del_lines.
10288 */
10289 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010290 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
10292#ifdef FEAT_CLIPBOARD
10293 /* Remove a modeless selection when inserting lines halfway the screen
10294 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010295 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010296 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 else
10298 clip_scroll_selection(-line_count);
10299#endif
10300
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301#ifdef FEAT_GUI
10302 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10303 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010304 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#endif
10306
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010307 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10308 cursor_col = wp->w_wincol;
10309
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 if (*T_CCS != NUL) /* cursor relative to region */
10311 cursor_row = row;
10312 else
10313 cursor_row = row + off;
10314
10315 /*
10316 * Shift LineOffset[] line_count down to reflect the inserted lines.
10317 * Clear the inserted lines in ScreenLines[].
10318 */
10319 row += off;
10320 end += off;
10321 for (i = 0; i < line_count; ++i)
10322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 if (wp != NULL && wp->w_width != Columns)
10324 {
10325 /* need to copy part of a line */
10326 j = end - 1 - i;
10327 while ((j -= line_count) >= row)
10328 linecopy(j + line_count, j, wp);
10329 j += line_count;
10330 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010331 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10332 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 else
10334 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10335 LineWraps[j] = FALSE;
10336 }
10337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 {
10339 j = end - 1 - i;
10340 temp = LineOffset[j];
10341 while ((j -= line_count) >= row)
10342 {
10343 LineOffset[j + line_count] = LineOffset[j];
10344 LineWraps[j + line_count] = LineWraps[j];
10345 }
10346 LineOffset[j + line_count] = temp;
10347 LineWraps[j + line_count] = FALSE;
10348 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010349 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 else
10351 lineinvalid(temp, (int)Columns);
10352 }
10353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354
10355 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010356 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010357 if (clear_attr != 0)
10358 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 /* redraw the characters */
10361 if (type == USE_REDRAW)
10362 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010363 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 {
10365 term_append_lines(line_count);
10366 screen_start(); /* don't know where cursor is now */
10367 }
10368 else
10369 {
10370 for (i = 0; i < line_count; i++)
10371 {
10372 if (type == USE_T_AL)
10373 {
10374 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010375 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 out_str(T_AL);
10377 }
10378 else /* type == USE_T_SR */
10379 out_str(T_SR);
10380 screen_start(); /* don't know where cursor is now */
10381 }
10382 }
10383
10384 /*
10385 * With scroll-reverse and 'da' flag set we need to clear the lines that
10386 * have been scrolled down into the region.
10387 */
10388 if (type == USE_T_SR && *T_DA)
10389 {
10390 for (i = 0; i < line_count; ++i)
10391 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010392 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 out_str(T_CE);
10394 screen_start(); /* don't know where cursor is now */
10395 }
10396 }
10397
10398#ifdef FEAT_GUI
10399 gui_can_update_cursor();
10400 if (gui.in_use)
10401 out_flush(); /* always flush after a scroll */
10402#endif
10403 return OK;
10404}
10405
10406/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010407 * Delete lines on the screen and update ScreenLines[].
10408 * "end" is the line after the scrolled part. Normally it is Rows.
10409 * When scrolling region used "off" is the offset from the top for the region.
10410 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 *
10412 * Return OK for success, FAIL if the lines are not deleted.
10413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010415screen_del_lines(
10416 int off,
10417 int row,
10418 int line_count,
10419 int end,
10420 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010421 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010422 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423{
10424 int j;
10425 int i;
10426 unsigned temp;
10427 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010428 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 int cursor_end;
10430 int result_empty; /* result is empty until end of region */
10431 int can_delete; /* deleting line codes can be used */
10432 int type;
10433
10434 /*
10435 * FAIL if
10436 * - there is no valid screen
10437 * - the screen has to be redrawn completely
10438 * - the line count is less than one
10439 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010440 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010442 if (!screen_valid(TRUE) || line_count <= 0
10443 || (!force && line_count > p_ttyscroll)
10444#ifdef FEAT_CLIPBOARD
10445 || (clip_star.state != SELECT_CLEARED
10446 && redrawing_for_callback > 0)
10447#endif
10448 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 return FAIL;
10450
10451 /*
10452 * Check if the rest of the current region will become empty.
10453 */
10454 result_empty = row + line_count >= end;
10455
10456 /*
10457 * We can delete lines only when 'db' flag not set or when 'ce' option
10458 * available.
10459 */
10460 can_delete = (*T_DB == NUL || can_clear(T_CE));
10461
10462 /*
10463 * There are six ways to delete lines:
10464 * 0. When in a vertically split window and t_CV isn't set, redraw the
10465 * characters from ScreenLines[].
10466 * 1. Use T_CD if it exists and the result is empty.
10467 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10468 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10469 * none of the other ways work.
10470 * 4. Use T_CE (erase line) if the result is empty.
10471 * 5. Use T_DL (delete line) if it exists.
10472 * 6. redraw the characters from ScreenLines[].
10473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10475 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010476 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 type = USE_T_CD;
10478#if defined(__BEOS__) && defined(BEOS_DR8)
10479 /*
10480 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10481 * its internal termcap... this works okay for tests which test *T_DB !=
10482 * NUL. It has the disadvantage that the user cannot use any :set t_*
10483 * command to get T_DB (back) to empty_option, only :set term=... will do
10484 * the trick...
10485 * Anyway, this hack will hopefully go away with the next OS release.
10486 * (Olaf Seibert)
10487 */
10488 else if (row == 0 && T_DB == empty_option
10489 && (line_count == 1 || *T_CDL == NUL))
10490#else
10491 else if (row == 0 && (
10492#ifndef AMIGA
10493 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10494 * up, so use delete-line command */
10495 line_count == 1 ||
10496#endif
10497 *T_CDL == NUL))
10498#endif
10499 type = USE_NL;
10500 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10501 type = USE_T_CDL;
10502 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010503 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 type = USE_T_CE;
10505 else if (*T_DL != NUL && can_delete)
10506 type = USE_T_DL;
10507 else if (*T_CDL != NUL && can_delete)
10508 type = USE_T_CDL;
10509 else
10510 return FAIL;
10511
10512#ifdef FEAT_CLIPBOARD
10513 /* Remove a modeless selection when deleting lines halfway the screen or
10514 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010515 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010516 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 else
10518 clip_scroll_selection(line_count);
10519#endif
10520
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521#ifdef FEAT_GUI
10522 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10523 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010524 gui_dont_update_cursor(gui.cursor_row >= row + off
10525 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526#endif
10527
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010528 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10529 cursor_col = wp->w_wincol;
10530
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 if (*T_CCS != NUL) /* cursor relative to region */
10532 {
10533 cursor_row = row;
10534 cursor_end = end;
10535 }
10536 else
10537 {
10538 cursor_row = row + off;
10539 cursor_end = end + off;
10540 }
10541
10542 /*
10543 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10544 * Clear the inserted lines in ScreenLines[].
10545 */
10546 row += off;
10547 end += off;
10548 for (i = 0; i < line_count; ++i)
10549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 if (wp != NULL && wp->w_width != Columns)
10551 {
10552 /* need to copy part of a line */
10553 j = row + i;
10554 while ((j += line_count) <= end - 1)
10555 linecopy(j - line_count, j, wp);
10556 j -= line_count;
10557 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010558 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10559 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 else
10561 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10562 LineWraps[j] = FALSE;
10563 }
10564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 {
10566 /* whole width, moving the line pointers is faster */
10567 j = row + i;
10568 temp = LineOffset[j];
10569 while ((j += line_count) <= end - 1)
10570 {
10571 LineOffset[j - line_count] = LineOffset[j];
10572 LineWraps[j - line_count] = LineWraps[j];
10573 }
10574 LineOffset[j - line_count] = temp;
10575 LineWraps[j - line_count] = FALSE;
10576 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010577 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 else
10579 lineinvalid(temp, (int)Columns);
10580 }
10581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010583 if (screen_attr != clear_attr)
10584 screen_stop_highlight();
10585 if (clear_attr != 0)
10586 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 /* redraw the characters */
10589 if (type == USE_REDRAW)
10590 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010591 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010593 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 out_str(T_CD);
10595 screen_start(); /* don't know where cursor is now */
10596 }
10597 else if (type == USE_T_CDL)
10598 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010599 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 term_delete_lines(line_count);
10601 screen_start(); /* don't know where cursor is now */
10602 }
10603 /*
10604 * Deleting lines at top of the screen or scroll region: Just scroll
10605 * the whole screen (scroll region) up by outputting newlines on the
10606 * last line.
10607 */
10608 else if (type == USE_NL)
10609 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010610 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 for (i = line_count; --i >= 0; )
10612 out_char('\n'); /* cursor will remain on same line */
10613 }
10614 else
10615 {
10616 for (i = line_count; --i >= 0; )
10617 {
10618 if (type == USE_T_DL)
10619 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010620 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 out_str(T_DL); /* delete a line */
10622 }
10623 else /* type == USE_T_CE */
10624 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010625 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 out_str(T_CE); /* erase a line */
10627 }
10628 screen_start(); /* don't know where cursor is now */
10629 }
10630 }
10631
10632 /*
10633 * If the 'db' flag is set, we need to clear the lines that have been
10634 * scrolled up at the bottom of the region.
10635 */
10636 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10637 {
10638 for (i = line_count; i > 0; --i)
10639 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010640 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 out_str(T_CE); /* erase a line */
10642 screen_start(); /* don't know where cursor is now */
10643 }
10644 }
10645
10646#ifdef FEAT_GUI
10647 gui_can_update_cursor();
10648 if (gui.in_use)
10649 out_flush(); /* always flush after a scroll */
10650#endif
10651
10652 return OK;
10653}
10654
10655/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010656 * Return TRUE when postponing displaying the mode message: when not redrawing
10657 * or inside a mapping.
10658 */
10659 int
10660skip_showmode()
10661{
10662 // Call char_avail() only when we are going to show something, because it
10663 // takes a bit of time. redrawing() may also call char_avail_avail().
10664 if (global_busy
10665 || msg_silent != 0
10666 || !redrawing()
10667 || (char_avail() && !KeyTyped))
10668 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010669 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010670 return TRUE;
10671 }
10672 return FALSE;
10673}
10674
10675/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010676 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 *
10678 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10679 * If clear_cmdline is FALSE there may be a message there that needs to be
10680 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010681 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 * Return the length of the message (0 if no message).
10683 */
10684 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010685showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686{
10687 int need_clear;
10688 int length = 0;
10689 int do_mode;
10690 int attr;
10691 int nwr_save;
10692#ifdef FEAT_INS_EXPAND
10693 int sub_attr;
10694#endif
10695
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010696 do_mode = ((p_smd && msg_silent == 0)
10697 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010698 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010699 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010700 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010702 if (skip_showmode())
10703 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704
10705 nwr_save = need_wait_return;
10706
10707 /* wait a bit before overwriting an important message */
10708 check_for_delay(FALSE);
10709
10710 /* if the cmdline is more than one line high, erase top lines */
10711 need_clear = clear_cmdline;
10712 if (clear_cmdline && cmdline_row < Rows - 1)
10713 msg_clr_cmdline(); /* will reset clear_cmdline */
10714
10715 /* Position on the last line in the window, column 0 */
10716 msg_pos_mode();
10717 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010718 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (do_mode)
10720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010721 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010723 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010724# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010725 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010726# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010727 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010728# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010729 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010730# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010731 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010733 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734# endif
10735#endif
10736#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10737 if (gui.in_use)
10738 {
10739 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010740 {
10741 /* HANGUL */
10742 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010743 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010744 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010745 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 }
10748#endif
10749#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010750 /* CTRL-X in Insert mode */
10751 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 {
10753 /* These messages can get long, avoid a wrap in a narrow
10754 * window. Prefer showing edit_submode_extra. */
10755 length = (Rows - msg_row) * Columns - 3;
10756 if (edit_submode_extra != NULL)
10757 length -= vim_strsize(edit_submode_extra);
10758 if (length > 0)
10759 {
10760 if (edit_submode_pre != NULL)
10761 length -= vim_strsize(edit_submode_pre);
10762 if (length - vim_strsize(edit_submode) > 0)
10763 {
10764 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010765 msg_puts_attr((char *)edit_submode_pre, attr);
10766 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 }
10768 if (edit_submode_extra != NULL)
10769 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010770 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010772 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 else
10774 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010775 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 }
10777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 }
10779 else
10780#endif
10781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010783 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010784 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010785 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010786 else if (State & INSERT)
10787 {
10788#ifdef FEAT_RIGHTLEFT
10789 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010790 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010792 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010794 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010795 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010797 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010799 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800#ifdef FEAT_RIGHTLEFT
10801 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010802 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803#endif
10804#ifdef FEAT_KEYMAP
10805 if (State & LANGMAP)
10806 {
10807# ifdef FEAT_ARABIC
10808 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010809 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 else
10811# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010812 if (get_keymap_str(curwin, (char_u *)" (%s)",
10813 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010814 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 }
10816#endif
10817 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010818 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 if (VIsual_active)
10821 {
10822 char *p;
10823
10824 /* Don't concatenate separate words to avoid translation
10825 * problems. */
10826 switch ((VIsual_select ? 4 : 0)
10827 + (VIsual_mode == Ctrl_V) * 2
10828 + (VIsual_mode == 'V'))
10829 {
10830 case 0: p = N_(" VISUAL"); break;
10831 case 1: p = N_(" VISUAL LINE"); break;
10832 case 2: p = N_(" VISUAL BLOCK"); break;
10833 case 4: p = N_(" SELECT"); break;
10834 case 5: p = N_(" SELECT LINE"); break;
10835 default: p = N_(" SELECT BLOCK"); break;
10836 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010837 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010839 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010841
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 need_clear = TRUE;
10843 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010844 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845#ifdef FEAT_INS_EXPAND
10846 && edit_submode == NULL /* otherwise it gets too long */
10847#endif
10848 )
10849 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010850 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 need_clear = TRUE;
10852 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010853
10854 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010855 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 msg_clr_eos();
10857 msg_didout = FALSE; /* overwrite this message */
10858 length = msg_col;
10859 msg_col = 0;
10860 need_wait_return = nwr_save; /* never ask for hit-return for this */
10861 }
10862 else if (clear_cmdline && msg_silent == 0)
10863 /* Clear the whole command line. Will reset "clear_cmdline". */
10864 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010865 else if (redraw_mode)
10866 {
10867 msg_pos_mode();
10868 msg_clr_eos();
10869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870
10871#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 /* In Visual mode the size of the selected area must be redrawn. */
10873 if (VIsual_active)
10874 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875
10876 /* If the last window has no status line, the ruler is after the mode
10877 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010878 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010879 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880#endif
10881 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010882 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 clear_cmdline = FALSE;
10884
10885 return length;
10886}
10887
10888/*
10889 * Position for a mode message.
10890 */
10891 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010892msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
10894 msg_col = 0;
10895 msg_row = Rows - 1;
10896}
10897
10898/*
10899 * Delete mode message. Used when ESC is typed which is expected to end
10900 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010901 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 */
10903 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010904unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
10906 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010907 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 */
10909 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10910 redraw_cmdline = TRUE; /* delete mode later */
10911 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010912 clearmode();
10913}
10914
10915/*
10916 * Clear the mode message.
10917 */
10918 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010919clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010920{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010921 int save_msg_row = msg_row;
10922 int save_msg_col = msg_col;
10923
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010924 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010925 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010926 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010927 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010928
10929 msg_col = save_msg_col;
10930 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931}
10932
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010933 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010934recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010935{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010936 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010937 if (!shortmess(SHM_RECORDING))
10938 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010939 char s[4];
10940
10941 sprintf(s, " @%c", reg_recording);
10942 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010943 }
10944}
10945
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010946/*
10947 * Draw the tab pages line at the top of the Vim window.
10948 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010949 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010950draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010951{
10952 int tabcount = 0;
10953 tabpage_T *tp;
10954 int tabwidth;
10955 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010956 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010957 int attr;
10958 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010959 win_T *cwp;
10960 int wincount;
10961 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010962 int c;
10963 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010964 int attr_sel = HL_ATTR(HLF_TPS);
10965 int attr_nosel = HL_ATTR(HLF_TP);
10966 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010967 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010968 int room;
10969 int use_sep_chars = (t_colors < 8
10970#ifdef FEAT_GUI
10971 && !gui.in_use
10972#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010973#ifdef FEAT_TERMGUICOLORS
10974 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010975#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010976 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010977
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010978 if (ScreenLines == NULL)
10979 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010980 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010981
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010982#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010983 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010984 if (gui_use_tabline())
10985 {
10986 gui_update_tabline();
10987 return;
10988 }
10989#endif
10990
10991 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010992 return;
10993
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010994#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010995 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010996
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010997 /* Use the 'tabline' option if it's set. */
10998 if (*p_tal != NUL)
10999 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020011000 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011001
11002 /* Check for an error. If there is one we would loop in redrawing the
11003 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020011004 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011005 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020011006 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011007 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011008 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020011009 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011010 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000011011 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011012#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011013 {
Bram Moolenaar29323592016-07-24 22:04:11 +020011014 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011015 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011016
Bram Moolenaar238a5642006-02-21 22:12:05 +000011017 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
11018 if (tabwidth < 6)
11019 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011020
Bram Moolenaar238a5642006-02-21 22:12:05 +000011021 attr = attr_nosel;
11022 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011023 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
11024 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000011025 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011026 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011027
Bram Moolenaar238a5642006-02-21 22:12:05 +000011028 if (tp->tp_topframe == topframe)
11029 attr = attr_sel;
11030 if (use_sep_chars && col > 0)
11031 screen_putchar('|', 0, col++, attr);
11032
11033 if (tp->tp_topframe != topframe)
11034 attr = attr_nosel;
11035
11036 screen_putchar(' ', 0, col++, attr);
11037
11038 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000011039 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011040 cwp = curwin;
11041 wp = firstwin;
11042 }
11043 else
11044 {
11045 cwp = tp->tp_curwin;
11046 wp = tp->tp_firstwin;
11047 }
11048
11049 modified = FALSE;
11050 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
11051 if (bufIsChanged(wp->w_buffer))
11052 modified = TRUE;
11053 if (modified || wincount > 1)
11054 {
11055 if (wincount > 1)
11056 {
11057 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011058 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011059 if (col + len >= Columns - 3)
11060 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011061 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011062#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010011063 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011064#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020011065 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000011066#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000011067 );
11068 col += len;
11069 }
11070 if (modified)
11071 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
11072 screen_putchar(' ', 0, col++, attr);
11073 }
11074
11075 room = scol - col + tabwidth - 1;
11076 if (room > 0)
11077 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011078 /* Get buffer name in NameBuff[] */
11079 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011080 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011081 len = vim_strsize(NameBuff);
11082 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011083 if (has_mbyte)
11084 while (len > room)
11085 {
11086 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011087 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011088 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011089 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011090 {
11091 p += len - room;
11092 len = room;
11093 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000011094 if (len > Columns - col - 1)
11095 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000011096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011097 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000011098 col += len;
11099 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000011100 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011101
11102 /* Store the tab page number in TabPageIdxs[], so that
11103 * jump_to_mouse() knows where each one is. */
11104 ++tabcount;
11105 while (scol < col)
11106 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000011107 }
11108
Bram Moolenaar238a5642006-02-21 22:12:05 +000011109 if (use_sep_chars)
11110 c = '_';
11111 else
11112 c = ' ';
11113 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000011114
11115 /* Put an "X" for closing the current tab if there are several. */
11116 if (first_tabpage->tp_next != NULL)
11117 {
11118 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
11119 TabPageIdxs[Columns - 1] = -999;
11120 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011121 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000011122
11123 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
11124 * set. */
11125 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011126}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011127
11128/*
11129 * Get buffer name for "buf" into NameBuff[].
11130 * Takes care of special buffer names and translates special characters.
11131 */
11132 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011133get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011134{
11135 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020011136 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000011137 else
11138 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
11139 trans_characters(NameBuff, MAXPATHL);
11140}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000011141
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142/*
11143 * Get the character to use in a status line. Get its attributes in "*attr".
11144 */
11145 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011146fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
11148 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011149
11150#ifdef FEAT_TERMINAL
11151 if (bt_terminal(wp->w_buffer))
11152 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011153 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011154 {
11155 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011156 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011157 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011158 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011159 {
11160 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011161 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020011162 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011163 }
11164 else
11165#endif
11166 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011168 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 fill = fill_stl;
11170 }
11171 else
11172 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010011173 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 fill = fill_stlnc;
11175 }
11176 /* Use fill when there is highlighting, and highlighting of current
11177 * window differs, or the fillchars differ, or this is not the
11178 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010011179 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011180 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 || (fill_stl != fill_stlnc)))
11182 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011183 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 return '^';
11185 return '=';
11186}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188/*
11189 * Get the character to use in a separator between vertically split windows.
11190 * Get its attributes in "*attr".
11191 */
11192 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010011193fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194{
Bram Moolenaar8820b482017-03-16 17:23:31 +010011195 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 if (*attr == 0 && fill_vert == ' ')
11197 return '|';
11198 else
11199 return fill_vert;
11200}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201
11202/*
11203 * Return TRUE if redrawing should currently be done.
11204 */
11205 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011206redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011208#ifdef FEAT_EVAL
11209 if (disable_redraw_for_testing)
11210 return 0;
11211 else
11212#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011213 return ((!RedrawingDisabled
11214#ifdef FEAT_EVAL
11215 || ignore_redraw_flag_for_testing
11216#endif
11217 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218}
11219
11220/*
11221 * Return TRUE if printing messages should currently be done.
11222 */
11223 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011224messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225{
11226 return (!(p_lz && char_avail() && !KeyTyped));
11227}
11228
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011229#ifdef FEAT_MENU
11230/*
11231 * Draw the window toolbar.
11232 */
11233 static void
11234redraw_win_toolbar(win_T *wp)
11235{
11236 vimmenu_T *menu;
11237 int item_idx = 0;
11238 int item_count = 0;
11239 int col = 0;
11240 int next_col;
11241 int off = (int)(current_ScreenLine - ScreenLines);
11242 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11243 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11244
11245 vim_free(wp->w_winbar_items);
11246 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11247 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011248 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011249
11250 /* TODO: use fewer spaces if there is not enough room */
11251 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011252 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011253 {
11254 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011255 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011256 break;
11257 if (col > 1)
11258 {
11259 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011260 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011261 break;
11262 }
11263
11264 wp->w_winbar_items[item_idx].wb_startcol = col;
11265 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011266 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011267 break;
11268
11269 next_col = text_to_screenline(wp, menu->name, col);
11270 while (col < next_col)
11271 {
11272 ScreenAttrs[off + col] = button_attr;
11273 ++col;
11274 }
11275 wp->w_winbar_items[item_idx].wb_endcol = col;
11276 wp->w_winbar_items[item_idx].wb_menu = menu;
11277 ++item_idx;
11278
Bram Moolenaar02631462017-09-22 15:20:32 +020011279 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011280 break;
11281 space_to_screenline(off + col, button_attr);
11282 ++col;
11283 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011284 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011285 {
11286 space_to_screenline(off + col, fill_attr);
11287 ++col;
11288 }
11289 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11290
Bram Moolenaar02631462017-09-22 15:20:32 +020011291 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011292 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011293}
11294#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011295
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296/*
11297 * Show current status info in ruler and various other places
11298 * If always is FALSE, only show ruler if position has changed.
11299 */
11300 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011301showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302{
11303 if (!always && !redrawing())
11304 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011305#ifdef FEAT_INS_EXPAND
11306 if (pum_visible())
11307 {
11308 /* Don't redraw right now, do it later. */
11309 curwin->w_redr_status = TRUE;
11310 return;
11311 }
11312#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011313#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011314 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011315 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 else
11317#endif
11318#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011319 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320#endif
11321
11322#ifdef FEAT_TITLE
11323 if (need_maketitle
11324# ifdef FEAT_STL_OPT
11325 || (p_icon && (stl_syntax & STL_IN_ICON))
11326 || (p_title && (stl_syntax & STL_IN_TITLE))
11327# endif
11328 )
11329 maketitle();
11330#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011331 /* Redraw the tab pages line if needed. */
11332 if (redraw_tabline)
11333 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334}
11335
11336#ifdef FEAT_CMDL_INFO
11337 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011338win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011340#define RULER_BUF_LEN 70
11341 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 int row;
11343 int fillchar;
11344 int attr;
11345 int empty_line = FALSE;
11346 colnr_T virtcol;
11347 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011348 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 int this_ru_col;
11351 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011352 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353
11354 /* If 'ruler' off or redrawing disabled, don't do anything */
11355 if (!p_ru)
11356 return;
11357
11358 /*
11359 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11360 * after deleting lines, before cursor.lnum is corrected.
11361 */
11362 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11363 return;
11364
11365#ifdef FEAT_INS_EXPAND
11366 /* Don't draw the ruler while doing insert-completion, it might overwrite
11367 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 if (edit_submode != NULL)
11370 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011371 // Don't draw the ruler when the popup menu is visible, it may overlap.
11372 // Except when the popup menu will be redrawn anyway.
11373 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011374 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375#endif
11376
11377#ifdef FEAT_STL_OPT
11378 if (*p_ruf)
11379 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011380 int save_called_emsg = called_emsg;
11381
11382 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011384 if (called_emsg)
11385 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011386 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011387 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 return;
11389 }
11390#endif
11391
11392 /*
11393 * Check if not in Insert mode and the line is empty (will show "0-1").
11394 */
11395 if (!(State & INSERT)
11396 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11397 empty_line = TRUE;
11398
11399 /*
11400 * Only draw the ruler when something changed.
11401 */
11402 validate_virtcol_win(wp);
11403 if ( redraw_cmdline
11404 || always
11405 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11406 || wp->w_cursor.col != wp->w_ru_cursor.col
11407 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 || wp->w_topline != wp->w_ru_topline
11410 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11411#ifdef FEAT_DIFF
11412 || wp->w_topfill != wp->w_ru_topfill
11413#endif
11414 || empty_line != wp->w_ru_empty)
11415 {
11416 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 if (wp->w_status_height)
11418 {
11419 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011420 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011421 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011422 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 }
11424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 {
11426 row = Rows - 1;
11427 fillchar = ' ';
11428 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 width = Columns;
11430 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 }
11432
11433 /* In list mode virtcol needs to be recomputed */
11434 virtcol = wp->w_virtcol;
11435 if (wp->w_p_list && lcs_tab1 == NUL)
11436 {
11437 wp->w_p_list = FALSE;
11438 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11439 wp->w_p_list = TRUE;
11440 }
11441
11442 /*
11443 * Some sprintfs return the length, some return a pointer.
11444 * To avoid portability problems we use strlen() here.
11445 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011446 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11448 ? 0L
11449 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011450 len = STRLEN(buffer);
11451 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11453 (int)virtcol + 1);
11454
11455 /*
11456 * Add a "50%" if there is room for it.
11457 * On the last line, don't print in the last column (scrolls the
11458 * screen up on some terminals).
11459 */
11460 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011461 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 this_ru_col = ru_col - (Columns - width);
11466 if (this_ru_col < 0)
11467 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 /* Never use more than half the window/screen width, leave the other
11469 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011470 if (this_ru_col < (width + 1) / 2)
11471 this_ru_col = (width + 1) / 2;
11472 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011474 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011475 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 if (has_mbyte)
11478 i += (*mb_char2bytes)(fillchar, buffer + i);
11479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 buffer[i++] = fillchar;
11481 ++o;
11482 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011483 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 }
11485 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 if (has_mbyte)
11487 {
11488 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011489 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 {
11491 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011492 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 {
11494 buffer[i] = NUL;
11495 break;
11496 }
11497 }
11498 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011499 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011500 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501
Bram Moolenaar4033c552017-09-16 20:54:51 +020011502 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 i = redraw_cmdline;
11504 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011505 this_ru_col + off + (int)STRLEN(buffer),
11506 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 fillchar, fillchar, attr);
11508 /* don't redraw the cmdline because of showing the ruler */
11509 redraw_cmdline = i;
11510 wp->w_ru_cursor = wp->w_cursor;
11511 wp->w_ru_virtcol = wp->w_virtcol;
11512 wp->w_ru_empty = empty_line;
11513 wp->w_ru_topline = wp->w_topline;
11514 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11515#ifdef FEAT_DIFF
11516 wp->w_ru_topfill = wp->w_topfill;
11517#endif
11518 }
11519}
11520#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011521
11522#if defined(FEAT_LINEBREAK) || defined(PROTO)
11523/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011524 * Return the width of the 'number' and 'relativenumber' column.
11525 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011526 * Otherwise it depends on 'numberwidth' and the line count.
11527 */
11528 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011529number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011530{
11531 int n;
11532 linenr_T lnum;
11533
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011534 if (wp->w_p_rnu && !wp->w_p_nu)
11535 /* cursor line shows "0" */
11536 lnum = wp->w_height;
11537 else
11538 /* cursor line shows absolute line number */
11539 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011540
Bram Moolenaar6b314672015-03-20 15:42:10 +010011541 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011542 return wp->w_nrwidth_width;
11543 wp->w_nrwidth_line_count = lnum;
11544
11545 n = 0;
11546 do
11547 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011548 lnum /= 10;
11549 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011550 } while (lnum > 0);
11551
11552 /* 'numberwidth' gives the minimal width plus one */
11553 if (n < wp->w_p_nuw - 1)
11554 n = wp->w_p_nuw - 1;
11555
11556 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011557 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011558 return n;
11559}
11560#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011561
Bram Moolenaar113e1072019-01-20 15:30:40 +010011562#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011563/*
11564 * Return the current cursor column. This is the actual position on the
11565 * screen. First column is 0.
11566 */
11567 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011568screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011569{
11570 return screen_cur_col;
11571}
11572
11573/*
11574 * Return the current cursor row. This is the actual position on the screen.
11575 * First row is 0.
11576 */
11577 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011578screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011579{
11580 return screen_cur_row;
11581}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011582#endif