blob: bdc4b47c3648a428027ce74c69f0b125efe48d6f [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 Moolenaarbbca7732019-07-24 18:13:16 +0200107static 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100124static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200125static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100126static 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 +0000127#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100128static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
129static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
130static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200132static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100133static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000134#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100135static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void start_search_hl(void);
139static void end_search_hl(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100141static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100143static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200144static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100145static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200146static 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 +0100147static void win_rest_invalid(win_T *wp);
148static void msg_pos_mode(void);
149static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200150static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200152#ifdef FEAT_MENU
153static void redraw_win_toolbar(win_T *wp);
154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100156static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200159static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160#endif
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/* Ugly global: overrule attribute used by screen_char() */
163static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200165#ifdef FEAT_RIGHTLEFT
166# define HAS_RIGHTLEFT(x) x
167#else
168# define HAS_RIGHTLEFT(x) FALSE
169#endif
170
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200171// flags for screen_line()
172#define SLF_RIGHTLEFT 1
173#define SLF_POPUP 2
174
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175/*
176 * Redraw the current window later, with update_screen(type).
177 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100178 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 */
180 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100181redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182{
183 redraw_win_later(curwin, type);
184}
185
186 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100187redraw_win_later(
188 win_T *wp,
189 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200191 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 wp->w_redr_type = type;
194 if (type >= NOT_VALID)
195 wp->w_lines_valid = 0;
196 if (must_redraw < type) /* must_redraw is the maximum of all windows */
197 must_redraw = type;
198 }
199}
200
201/*
202 * Force a complete redraw later. Also resets the highlighting. To be used
203 * after executing a shell command that messes up the screen.
204 */
205 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100206redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207{
208 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000209#ifdef FEAT_GUI
210 if (gui.in_use)
211 /* Use a code that will reset gui.highlight_mask in
212 * gui_stop_highlight(). */
213 screen_attr = HL_ALL + 1;
214 else
215#endif
216 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200217 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218}
219
220/*
221 * Mark all windows to be redrawn later.
222 */
223 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100224redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 win_T *wp;
227
228 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100230 // This may be needed when switching tabs.
231 if (must_redraw < type)
232 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233}
234
235/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000236 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
238 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100239redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240{
241 redraw_buf_later(curbuf, type);
242}
243
244 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100245redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246{
247 win_T *wp;
248
249 FOR_ALL_WINDOWS(wp)
250 {
251 if (wp->w_buffer == buf)
252 redraw_win_later(wp, type);
253 }
254}
255
Bram Moolenaar113e1072019-01-20 15:30:40 +0100256#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200257 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100258redraw_buf_line_later(buf_T *buf, linenr_T lnum)
259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 if (wp->w_buffer == buf && lnum >= wp->w_topline
264 && lnum < wp->w_botline)
265 redrawWinline(wp, lnum);
266}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100267#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100270 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200271redraw_buf_and_status_later(buf_T *buf, int type)
272{
273 win_T *wp;
274
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200275#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200276 if (wild_menu_showing != 0)
277 /* Don't redraw while the command line completion is displayed, it
278 * would disappear. */
279 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200280#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281 FOR_ALL_WINDOWS(wp)
282 {
283 if (wp->w_buffer == buf)
284 {
285 redraw_win_later(wp, type);
286 wp->w_redr_status = TRUE;
287 }
288 }
289}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291
Bram Moolenaar113e1072019-01-20 15:30:40 +0100292#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200294 * Redraw as soon as possible. When the command line is not scrolled redraw
295 * right away and restore what was on the command line.
296 * Return a code indicating what happened.
297 */
298 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100299redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200300{
301 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200302 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200303 int r;
304 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200305 schar_T *screenline; /* copy from ScreenLines[] */
306 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200308 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200309 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200310 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200311
312 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200313 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200314 return ret;
315
316 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200317 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200318 screenline = LALLOC_MULT(schar_T, rows * cols);
319 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 if (screenline == NULL || screenattr == NULL)
321 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 if (enc_utf8)
323 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200324 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200325 if (screenlineUC == NULL)
326 ret = 2;
327 for (i = 0; i < p_mco; ++i)
328 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200329 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 if (screenlineC[i] == NULL)
331 ret = 2;
332 }
333 }
334 if (enc_dbcs == DBCS_JPNU)
335 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200336 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200337 if (screenline2 == NULL)
338 ret = 2;
339 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200340
341 if (ret != 2)
342 {
343 /* Save the text displayed in the command line area. */
344 for (r = 0; r < rows; ++r)
345 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200346 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200348 (size_t)cols * sizeof(schar_T));
349 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200351 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 if (enc_utf8)
353 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200356 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200357 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200358 mch_memmove(screenlineC[i] + r * cols,
359 ScreenLinesC[i] + LineOffset[cmdline_row + r],
360 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200361 }
362 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200364 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 }
367
368 update_screen(0);
369 ret = 3;
370
371 if (must_redraw == 0)
372 {
373 int off = (int)(current_ScreenLine - ScreenLines);
374
375 /* Restore the text displayed in the command line area. */
376 for (r = 0; r < rows; ++r)
377 {
378 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200379 screenline + r * cols,
380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200382 screenattr + r * cols,
383 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 if (enc_utf8)
385 {
386 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200387 screenlineUC + r * cols,
388 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200389 for (i = 0; i < p_mco; ++i)
390 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200391 screenlineC[i] + r * cols,
392 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200393 }
394 if (enc_dbcs == DBCS_JPNU)
395 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200396 screenline2 + r * cols,
397 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200398 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 }
400 ret = 4;
401 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 }
403
404 vim_free(screenline);
405 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 if (enc_utf8)
407 {
408 vim_free(screenlineUC);
409 for (i = 0; i < p_mco; ++i)
410 vim_free(screenlineC[i]);
411 }
412 if (enc_dbcs == DBCS_JPNU)
413 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200415 /* Show the intro message when appropriate. */
416 maybe_intro_message();
417
418 setcursor();
419
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420 return ret;
421}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100422#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200423
424/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100425 * Invoked after an asynchronous callback is called.
426 * If an echo command was used the cursor needs to be put back where
427 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200428 * If "call_update_screen" is FALSE don't call update_screen() when at the
429 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100430 */
431 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200432redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100433{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200434 ++redrawing_for_callback;
435
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100436 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200437 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100438 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200439 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200440 // Don't redraw when in prompt_for_number().
441 if (cmdline_row > 0)
442 {
443 // Redrawing only works when the screen didn't scroll. Don't clear
444 // wildmenu entries.
445 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200446#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200447 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200448#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200449 && call_update_screen)
450 update_screen(0);
451
452 // Redraw in the same position, so that the user can continue
453 // editing the command.
454 redrawcmdline_ex(FALSE);
455 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200456 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200457 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100458 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200459 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200460 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100461 setcursor();
462 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100463 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100464#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100465 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200466 // Don't update the cursor when it is blinking and off to avoid
467 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100468 out_flush_cursor(FALSE, FALSE);
469 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100470#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100471 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200472
473 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100474}
475
476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * Changed something in the current window, at buffer line "lnum", that
478 * requires that line and possibly other lines to be redrawn.
479 * Used when entering/leaving Insert mode with the cursor on a folded line.
480 * Used to remove the "$" from a change command.
481 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
482 * may become invalid and the whole window will have to be redrawn.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100485redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200486 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100487 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200489 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
490 wp->w_redraw_top = lnum;
491 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
492 wp->w_redraw_bot = lnum;
493 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494}
495
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200496/*
497 * To be called when "updating_screen" was set before and now the postponed
498 * side effects may take place.
499 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200500 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200501after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200502{
503 updating_screen = FALSE;
504#ifdef FEAT_GUI
505 if (may_resize_shell)
506 gui_may_resize_shell();
507#endif
508#ifdef FEAT_TERMINAL
509 term_check_channel_closed_recently();
510#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200511
512#ifdef HAVE_DROP_FILE
513 // If handle_drop() was called while updating_screen was TRUE need to
514 // handle the drop now.
515 handle_any_postponed_drop();
516#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200517}
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200520 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
522 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100523update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 redraw_curbuf_later(type);
526 update_screen(type);
527}
528
529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * Based on the current value of curwin->w_topline, transfer a screenfull
531 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200532 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200534 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200535update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200537 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 win_T *wp;
539 static int did_intro = FALSE;
540#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
541 int did_one;
542#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200543#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200544 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200545 int gui_cursor_col = 0;
546 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200547#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000550 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200554 if (type == VALID_NO_UPDATE)
555 {
556 no_update = TRUE;
557 type = 0;
558 }
559
Bram Moolenaara3347722019-05-11 21:14:24 +0200560#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200561 {
562 buf_T *buf;
563
564 // Before updating the screen, notify any listeners of changed text.
565 FOR_ALL_BUFFERS(buf)
566 invoke_listeners(buf);
567 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200568#endif
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 if (must_redraw)
571 {
572 if (type < must_redraw) /* use maximal type */
573 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000574
575 /* must_redraw is reset here, so that when we run into some weird
576 * reason to redraw while busy redrawing (e.g., asynchronous
577 * scrolling), or update_topline() in win_update() will cause a
578 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 must_redraw = 0;
580 }
581
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200582 /* May need to update w_lines[]. */
583 if (curwin->w_lines_valid == 0 && type < NOT_VALID
584#ifdef FEAT_TERMINAL
585 && !term_do_update_window(curwin)
586#endif
587 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 type = NOT_VALID;
589
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000590 /* Postpone the redrawing when it's not needed and when being called
591 * recursively. */
592 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 redraw_later(type); /* remember type for next time */
595 must_redraw = type;
596 if (type > INVERTED_ALL)
597 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200598 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200600 updating_screen = TRUE;
Bram Moolenaar33796b32019-06-08 16:01:13 +0200601
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200602#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +0200603 // Update popup_mask if needed. This may set w_redraw_top and w_redraw_bot
604 // in some windows.
605 may_update_popup_mask(type);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#ifdef FEAT_SYN_HL
609 ++display_tick; /* let syntax code know we're in a next round of
610 * display updating */
611#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200612 if (no_update)
613 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615 /*
616 * if the screen was scrolled up when displaying a message, scroll it down
617 */
618 if (msg_scrolled)
619 {
620 clear_cmdline = TRUE;
621 if (msg_scrolled > Rows - 5) /* clearing is faster */
622 type = CLEAR;
623 else if (type != CLEAR)
624 {
625 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200626 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
627 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 type = CLEAR;
629 FOR_ALL_WINDOWS(wp)
630 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200631 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 if (W_WINROW(wp) + wp->w_height > msg_scrolled
634 && wp->w_redr_type < REDRAW_TOP
635 && wp->w_lines_valid > 0
636 && wp->w_topline == wp->w_lines[0].wl_lnum)
637 {
638 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
639 wp->w_redr_type = REDRAW_TOP;
640 }
641 else
642 {
643 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200644 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
645 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648 }
649 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200650 if (!no_update)
651 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000652 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 }
654 msg_scrolled = 0;
655 need_wait_return = FALSE;
656 }
657
658 /* reset cmdline_row now (may have been changed temporarily) */
659 compute_cmdrow();
660
661 /* Check for changed highlighting */
662 if (need_highlight_changed)
663 highlight_changed();
664
665 if (type == CLEAR) /* first clear screen */
666 {
667 screenclear(); /* will reset clear_cmdline */
668 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200669 /* must_redraw may be set indirectly, avoid another redraw later */
670 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 }
672
673 if (clear_cmdline) /* going to clear cmdline (done below) */
674 check_for_delay(FALSE);
675
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000676#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200677 /* Force redraw when width of 'number' or 'relativenumber' column
678 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000679 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200680 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
681 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000682 curwin->w_redr_type = NOT_VALID;
683#endif
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 /*
686 * Only start redrawing if there is really something to do.
687 */
688 if (type == INVERTED)
689 update_curswant();
690 if (curwin->w_redr_type < type
691 && !((type == VALID
692 && curwin->w_lines[0].wl_valid
693#ifdef FEAT_DIFF
694 && curwin->w_topfill == curwin->w_old_topfill
695 && curwin->w_botfill == curwin->w_old_botfill
696#endif
697 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000699 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
701 && curwin->w_old_visual_mode == VIsual_mode
702 && (curwin->w_valid & VALID_VIRTCOL)
703 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 ))
705 curwin->w_redr_type = type;
706
Bram Moolenaar5a305422006-04-28 22:38:25 +0000707 /* Redraw the tab pages line if needed. */
708 if (redraw_tabline || type >= NOT_VALID)
709 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711#ifdef FEAT_SYN_HL
712 /*
713 * Correct stored syntax highlighting info for changes in each displayed
714 * buffer. Each buffer must only be done once.
715 */
716 FOR_ALL_WINDOWS(wp)
717 {
718 if (wp->w_buffer->b_mod_set)
719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 win_T *wwp;
721
722 /* Check if we already did this buffer. */
723 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
724 if (wwp->w_buffer == wp->w_buffer)
725 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200726 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 syn_stack_apply_changes(wp->w_buffer);
728 }
729 }
730#endif
731
732 /*
733 * Go from top to bottom through the windows, redrawing the ones that need
734 * it.
735 */
736#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
737 did_one = FALSE;
738#endif
739#ifdef FEAT_SEARCH_EXTRA
740 search_hl.rm.regprog = NULL;
741#endif
742 FOR_ALL_WINDOWS(wp)
743 {
744 if (wp->w_redr_type != 0)
745 {
746 cursor_off();
747#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
748 if (!did_one)
749 {
750 did_one = TRUE;
751# ifdef FEAT_SEARCH_EXTRA
752 start_search_hl();
753# endif
754# ifdef FEAT_CLIPBOARD
755 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200756 if (clip_star.available && clip_isautosel_star())
757 clip_update_selection(&clip_star);
758 if (clip_plus.available && clip_isautosel_plus())
759 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760# endif
761#ifdef FEAT_GUI
762 /* Remove the cursor before starting to do anything, because
763 * scrolling may make it difficult to redraw the text under
764 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200765 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200766 {
767 gui_cursor_col = gui.cursor_col;
768 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200770 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 }
774#endif
775 win_update(wp);
776 }
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 /* redraw status line after the window to minimize cursor movement */
779 if (wp->w_redr_status)
780 {
781 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200782 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785#if defined(FEAT_SEARCH_EXTRA)
786 end_search_hl();
787#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100788#ifdef FEAT_INS_EXPAND
789 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200790 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 /* Reset b_mod_set flags. Going through all windows is probably faster
794 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200795 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200798 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800 /* Clear or redraw the command line. Done last, because scrolling may
801 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200802 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 showmode();
804
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200805 if (no_update)
806 --no_win_do_lines_ins;
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200809 if (!did_intro)
810 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 did_intro = TRUE;
812
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200814 // Display popup windows on top of the windows.
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200815 update_popups(win_update);
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200816#endif
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#ifdef FEAT_GUI
819 /* Redraw the cursor and update the scrollbars when all screen updating is
820 * done. */
821 if (gui.in_use)
822 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200823 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200824 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100825 mch_disable_flush();
826 out_flush(); /* required before updating the cursor */
827 mch_enable_flush();
828
Bram Moolenaar144445d2016-07-08 21:41:54 +0200829 /* Put the GUI position where the cursor was, gui_update_cursor()
830 * uses that. */
831 gui.col = gui_cursor_col;
832 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200833 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200836 screen_cur_col = gui.col;
837 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200838 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100839 else
840 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 gui_update_scrollbars(FALSE);
842 }
843#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200844 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845}
846
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100847#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100848/*
849 * Prepare for updating one or more windows.
850 * Caller must check for "updating_screen" already set to avoid recursiveness.
851 */
852 static void
853update_prepare(void)
854{
855 cursor_off();
856 updating_screen = TRUE;
857#ifdef FEAT_GUI
858 /* Remove the cursor before starting to do anything, because scrolling may
859 * make it difficult to redraw the text under it. */
860 if (gui.in_use)
861 gui_undraw_cursor();
862#endif
863#ifdef FEAT_SEARCH_EXTRA
864 start_search_hl();
865#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +0200866#ifdef FEAT_TEXT_PROP
867 // Update popup_mask if needed.
Bram Moolenaar68acb412019-06-26 03:40:36 +0200868 may_update_popup_mask(must_redraw);
Bram Moolenaar33796b32019-06-08 16:01:13 +0200869#endif
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100870}
871
872/*
873 * Finish updating one or more windows.
874 */
875 static void
876update_finish(void)
877{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200878 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100879 showmode();
880
881# ifdef FEAT_SEARCH_EXTRA
882 end_search_hl();
883# endif
884
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200885 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100886
887# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100888 /* Redraw the cursor and update the scrollbars when all screen updating is
889 * done. */
890 if (gui.in_use)
891 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100892 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100893 gui_update_scrollbars(FALSE);
894 }
895# endif
896}
897#endif
898
Bram Moolenaar860cae12010-06-05 23:22:07 +0200899#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200900/*
901 * Return TRUE if the cursor line in window "wp" may be concealed, according
902 * to the 'concealcursor' option.
903 */
904 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100905conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906{
907 int c;
908
909 if (*wp->w_p_cocu == NUL)
910 return FALSE;
911 if (get_real_state() & VISUAL)
912 c = 'v';
913 else if (State & INSERT)
914 c = 'i';
915 else if (State & NORMAL)
916 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200917 else if (State & CMDLINE)
918 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200919 else
920 return FALSE;
921 return vim_strchr(wp->w_p_cocu, c) != NULL;
922}
923
924/*
925 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
926 */
927 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200928conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200929{
930 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
931 {
932 need_cursor_line_redraw = TRUE;
933 /* Need to recompute cursor column, e.g., when starting Visual mode
934 * without concealing. */
935 curs_columns(TRUE);
936 }
937}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#endif
939
Bram Moolenaar113e1072019-01-20 15:30:40 +0100940#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100942update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943{
944 win_T *wp;
945 int doit = FALSE;
946
947# ifdef FEAT_FOLDING
948 win_foldinfo.fi_level = 0;
949# endif
950
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100951 // update/delete a specific sign
952 redraw_buf_line_later(buf, lnum);
953
954 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (wp->w_redr_type != 0)
957 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100959 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200960 * happening (recursive call), messages on the screen or still starting up.
961 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100962 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200963 || State == ASKMORE || State == HITRETURN
964 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100965#ifdef FEAT_GUI
966 || gui.starting
967#endif
968 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 return;
970
971 /* update all windows that need updating */
972 update_prepare();
973
Bram Moolenaar29323592016-07-24 22:04:11 +0200974 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
976 if (wp->w_redr_type != 0)
977 win_update(wp);
978 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200979 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981
982 update_finish();
983}
984#endif
985
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200986/*
987 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
988 * window then get the "Pmenu" highlight attribute.
989 */
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200990 int
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200991get_wcr_attr(win_T *wp)
992{
993 int wcr_attr = 0;
994
995 if (*wp->w_p_wcr != NUL)
996 wcr_attr = syn_name2attr(wp->w_p_wcr);
997#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200998 if (WIN_IS_POPUP(wp) && wcr_attr == 0)
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200999 wcr_attr = HL_ATTR(HLF_PNI);
1000#endif
1001 return wcr_attr;
1002}
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#if defined(FEAT_GUI) || defined(PROTO)
1005/*
1006 * Update a single window, its status line and maybe the command line msg.
1007 * Used for the GUI scrollbar.
1008 */
1009 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001010updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001012 /* return if already busy updating */
1013 if (updating_screen)
1014 return;
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 update_prepare();
1017
1018#ifdef FEAT_CLIPBOARD
1019 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001020 if (clip_star.available && clip_isautosel_star())
1021 clip_update_selection(&clip_star);
1022 if (clip_plus.available && clip_isautosel_plus())
1023 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001027
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001028 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001029 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001030 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (wp->w_redr_status
1033# ifdef FEAT_CMDL_INFO
1034 || p_ru
1035# endif
1036# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001037 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038# endif
1039 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001040 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar988c4332019-06-02 14:12:11 +02001042#ifdef FEAT_TEXT_PROP
1043 // Display popup windows on top of everything.
Bram Moolenaara540f8a2019-06-14 19:23:57 +02001044 update_popups(win_update);
Bram Moolenaar988c4332019-06-02 14:12:11 +02001045#endif
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 update_finish();
1048}
1049#endif
1050
1051/*
1052 * Update a single window.
1053 *
1054 * This may cause the windows below it also to be redrawn (when clearing the
1055 * screen or scrolling lines).
1056 *
1057 * How the window is redrawn depends on wp->w_redr_type. Each type also
1058 * implies the one below it.
1059 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001060 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1062 * INVERTED redraw the changed part of the Visual area
1063 * INVERTED_ALL redraw the whole Visual area
1064 * VALID 1. scroll up/down to adjust for a changed w_topline
1065 * 2. update lines at the top when scrolled down
1066 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001067 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 * b_mod_top and b_mod_bot.
1069 * - if wp->w_redraw_top non-zero, redraw lines between
1070 * wp->w_redraw_top and wp->w_redr_bot.
1071 * - continue redrawing when syntax status is invalid.
1072 * 4. if scrolled up, update lines at the bottom.
1073 * This results in three areas that may need updating:
1074 * top: from first row to top_end (when scrolled down)
1075 * mid: from mid_start to mid_end (update inversion or changed text)
1076 * bot: from bot_start to last row (when scrolled up)
1077 */
1078 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001079win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
1081 buf_T *buf = wp->w_buffer;
1082 int type;
1083 int top_end = 0; /* Below last row of the top area that needs
1084 updating. 0 when no top area updating. */
1085 int mid_start = 999;/* first row of the mid area that needs
1086 updating. 999 when no mid area updating. */
1087 int mid_end = 0; /* Below last row of the mid area that needs
1088 updating. 0 when no mid area updating. */
1089 int bot_start = 999;/* first row of the bot area that needs
1090 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 int scrolled_down = FALSE; /* TRUE when scrolled down when
1092 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001094 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 int top_to_mod = FALSE; /* redraw above mod_top */
1096#endif
1097
1098 int row; /* current window row to display */
1099 linenr_T lnum; /* current buffer lnum to display */
1100 int idx; /* current index in w_lines[] */
1101 int srow; /* starting row of the current line */
1102
1103 int eof = FALSE; /* if TRUE, we hit the end of the file */
1104 int didline = FALSE; /* if TRUE, we finished the last line */
1105 int i;
1106 long j;
1107 static int recursive = FALSE; /* being called recursively */
1108 int old_botline = wp->w_botline;
1109#ifdef FEAT_FOLDING
1110 long fold_count;
1111#endif
1112#ifdef FEAT_SYN_HL
1113 /* remember what happened to the previous line, to know if
1114 * check_visual_highlight() can be used */
1115#define DID_NONE 1 /* didn't update a line */
1116#define DID_LINE 2 /* updated a normal line */
1117#define DID_FOLD 3 /* updated a folded line */
1118 int did_update = DID_NONE;
1119 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1120#endif
1121 linenr_T mod_top = 0;
1122 linenr_T mod_bot = 0;
1123#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1124 int save_got_int;
1125#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001126#ifdef SYN_TIME_LIMIT
1127 proftime_T syntax_tm;
1128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 type = wp->w_redr_type;
1131
1132 if (type == NOT_VALID)
1133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 wp->w_lines_valid = 0;
1136 }
1137
1138 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001139 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
1141 wp->w_redr_type = 0;
1142 return;
1143 }
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 /* Window is zero-width: Only need to draw the separator. */
1146 if (wp->w_width == 0)
1147 {
1148 /* draw the vertical separator right of this window */
1149 draw_vsep_win(wp, 0);
1150 wp->w_redr_type = 0;
1151 return;
1152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001154#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001155 // If this window contains a terminal, redraw works completely differently.
1156 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001157 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001158 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001159# ifdef FEAT_MENU
1160 /* Draw the window toolbar, if there is one. */
1161 if (winbar_height(wp) > 0)
1162 redraw_win_toolbar(wp);
1163# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001164 wp->w_redr_type = 0;
1165 return;
1166 }
1167#endif
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02001170 init_search_hl(wp, &search_hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#endif
1172
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001173#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001174 /* Force redraw when width of 'number' or 'relativenumber' column
1175 * changes. */
1176 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001177 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001178 {
1179 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001180 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001181 }
1182 else
1183#endif
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1186 {
1187 /*
1188 * When there are both inserted/deleted lines and specific lines to be
1189 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1190 * everything (only happens when redrawing is off for while).
1191 */
1192 type = NOT_VALID;
1193 }
1194 else
1195 {
1196 /*
1197 * Set mod_top to the first line that needs displaying because of
1198 * changes. Set mod_bot to the first line after the changes.
1199 */
1200 mod_top = wp->w_redraw_top;
1201 if (wp->w_redraw_bot != 0)
1202 mod_bot = wp->w_redraw_bot + 1;
1203 else
1204 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (buf->b_mod_set)
1206 {
1207 if (mod_top == 0 || mod_top > buf->b_mod_top)
1208 {
1209 mod_top = buf->b_mod_top;
1210#ifdef FEAT_SYN_HL
1211 /* Need to redraw lines above the change that may be included
1212 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001213 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001215 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (mod_top < 1)
1217 mod_top = 1;
1218 }
1219#endif
1220 }
1221 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1222 mod_bot = buf->b_mod_bot;
1223
1224#ifdef FEAT_SEARCH_EXTRA
1225 /* When 'hlsearch' is on and using a multi-line search pattern, a
1226 * change in one line may make the Search highlighting in a
1227 * previous line invalid. Simple solution: redraw all visible
1228 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001229 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001231 if (search_hl.rm.regprog != NULL
1232 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001234 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001235 {
1236 cur = wp->w_match_head;
1237 while (cur != NULL)
1238 {
1239 if (cur->match.regprog != NULL
1240 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001241 {
1242 top_to_mod = TRUE;
1243 break;
1244 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001245 cur = cur->next;
1246 }
1247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248#endif
1249 }
1250#ifdef FEAT_FOLDING
1251 if (mod_top != 0 && hasAnyFolding(wp))
1252 {
1253 linenr_T lnumt, lnumb;
1254
1255 /*
1256 * A change in a line can cause lines above it to become folded or
1257 * unfolded. Find the top most buffer line that may be affected.
1258 * If the line was previously folded and displayed, get the first
1259 * line of that fold. If the line is folded now, get the first
1260 * folded line. Use the minimum of these two.
1261 */
1262
1263 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1264 * the line below it. If there is no valid entry, use w_topline.
1265 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1266 * to this line. If there is no valid entry, use MAXLNUM. */
1267 lnumt = wp->w_topline;
1268 lnumb = MAXLNUM;
1269 for (i = 0; i < wp->w_lines_valid; ++i)
1270 if (wp->w_lines[i].wl_valid)
1271 {
1272 if (wp->w_lines[i].wl_lastlnum < mod_top)
1273 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1274 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1275 {
1276 lnumb = wp->w_lines[i].wl_lnum;
1277 /* When there is a fold column it might need updating
1278 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001279 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 ++lnumb;
1281 }
1282 }
1283
1284 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1285 if (mod_top > lnumt)
1286 mod_top = lnumt;
1287
1288 /* Now do the same for the bottom line (one above mod_bot). */
1289 --mod_bot;
1290 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1291 ++mod_bot;
1292 if (mod_bot < lnumb)
1293 mod_bot = lnumb;
1294 }
1295#endif
1296
1297 /* When a change starts above w_topline and the end is below
1298 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001299 * If the end of the change is above w_topline: do like no change was
1300 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (mod_top != 0 && mod_top < wp->w_topline)
1302 {
1303 if (mod_bot > wp->w_topline)
1304 mod_top = wp->w_topline;
1305#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001306 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 top_end = 1;
1308#endif
1309 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001310
1311 /* When line numbers are displayed need to redraw all lines below
1312 * inserted/deleted lines. */
1313 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1314 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001316 wp->w_redraw_top = 0; // reset for next time
1317 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319 /*
1320 * When only displaying the lines at the top, set top_end. Used when
1321 * window has scrolled down for msg_scrolled.
1322 */
1323 if (type == REDRAW_TOP)
1324 {
1325 j = 0;
1326 for (i = 0; i < wp->w_lines_valid; ++i)
1327 {
1328 j += wp->w_lines[i].wl_size;
1329 if (j >= wp->w_upd_rows)
1330 {
1331 top_end = j;
1332 break;
1333 }
1334 }
1335 if (top_end == 0)
1336 /* not found (cannot happen?): redraw everything */
1337 type = NOT_VALID;
1338 else
1339 /* top area defined, the rest is VALID */
1340 type = VALID;
1341 }
1342
Bram Moolenaar367329b2007-08-30 11:53:22 +00001343 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001344 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1345 * non-zero and thus not FALSE) will indicate that screenclear() was not
1346 * called. */
1347 if (screen_cleared)
1348 screen_cleared = MAYBE;
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 /*
1351 * If there are no changes on the screen that require a complete redraw,
1352 * handle three cases:
1353 * 1: we are off the top of the screen by a few lines: scroll down
1354 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1355 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1356 * w_lines[] that needs updating.
1357 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001358 if ((type == VALID || type == SOME_VALID
1359 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#ifdef FEAT_DIFF
1361 && !wp->w_botfill && !wp->w_old_botfill
1362#endif
1363 )
1364 {
1365 if (mod_top != 0 && wp->w_topline == mod_top)
1366 {
1367 /*
1368 * w_topline is the first changed line, the scrolling will be done
1369 * further down.
1370 */
1371 }
1372 else if (wp->w_lines[0].wl_valid
1373 && (wp->w_topline < wp->w_lines[0].wl_lnum
1374#ifdef FEAT_DIFF
1375 || (wp->w_topline == wp->w_lines[0].wl_lnum
1376 && wp->w_topfill > wp->w_old_topfill)
1377#endif
1378 ))
1379 {
1380 /*
1381 * New topline is above old topline: May scroll down.
1382 */
1383#ifdef FEAT_FOLDING
1384 if (hasAnyFolding(wp))
1385 {
1386 linenr_T ln;
1387
1388 /* count the number of lines we are off, counting a sequence
1389 * of folded lines as one */
1390 j = 0;
1391 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1392 {
1393 ++j;
1394 if (j >= wp->w_height - 2)
1395 break;
1396 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1397 }
1398 }
1399 else
1400#endif
1401 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1402 if (j < wp->w_height - 2) /* not too far off */
1403 {
1404 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1405#ifdef FEAT_DIFF
1406 /* insert extra lines for previously invisible filler lines */
1407 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1408 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1409 - wp->w_old_topfill;
1410#endif
1411 if (i < wp->w_height - 2) /* less than a screen off */
1412 {
1413 /*
1414 * Try to insert the correct number of lines.
1415 * If not the last window, delete the lines at the bottom.
1416 * win_ins_lines may fail when the terminal can't do it.
1417 */
1418 if (i > 0)
1419 check_for_delay(FALSE);
1420 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1421 {
1422 if (wp->w_lines_valid != 0)
1423 {
1424 /* Need to update rows that are new, stop at the
1425 * first one that scrolled down. */
1426 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
1429 /* Move the entries that were scrolled, disable
1430 * the entries for the lines to be redrawn. */
1431 if ((wp->w_lines_valid += j) > wp->w_height)
1432 wp->w_lines_valid = wp->w_height;
1433 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1434 wp->w_lines[idx] = wp->w_lines[idx - j];
1435 while (idx >= 0)
1436 wp->w_lines[idx--].wl_valid = FALSE;
1437 }
1438 }
1439 else
1440 mid_start = 0; /* redraw all lines */
1441 }
1442 else
1443 mid_start = 0; /* redraw all lines */
1444 }
1445 else
1446 mid_start = 0; /* redraw all lines */
1447 }
1448 else
1449 {
1450 /*
1451 * New topline is at or below old topline: May scroll up.
1452 * When topline didn't change, find first entry in w_lines[] that
1453 * needs updating.
1454 */
1455
1456 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1457 j = -1;
1458 row = 0;
1459 for (i = 0; i < wp->w_lines_valid; i++)
1460 {
1461 if (wp->w_lines[i].wl_valid
1462 && wp->w_lines[i].wl_lnum == wp->w_topline)
1463 {
1464 j = i;
1465 break;
1466 }
1467 row += wp->w_lines[i].wl_size;
1468 }
1469 if (j == -1)
1470 {
1471 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1472 * lines */
1473 mid_start = 0;
1474 }
1475 else
1476 {
1477 /*
1478 * Try to delete the correct number of lines.
1479 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1480 */
1481#ifdef FEAT_DIFF
1482 /* If the topline didn't change, delete old filler lines,
1483 * otherwise delete filler lines of the new topline... */
1484 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1485 row += wp->w_old_topfill;
1486 else
1487 row += diff_check_fill(wp, wp->w_topline);
1488 /* ... but don't delete new filler lines. */
1489 row -= wp->w_topfill;
1490#endif
1491 if (row > 0)
1492 {
1493 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001494 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1495 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 bot_start = wp->w_height - row;
1497 else
1498 mid_start = 0; /* redraw all lines */
1499 }
1500 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1501 {
1502 /*
1503 * Skip the lines (below the deleted lines) that are still
1504 * valid and don't need redrawing. Copy their info
1505 * upwards, to compensate for the deleted lines. Set
1506 * bot_start to the first row that needs redrawing.
1507 */
1508 bot_start = 0;
1509 idx = 0;
1510 for (;;)
1511 {
1512 wp->w_lines[idx] = wp->w_lines[j];
1513 /* stop at line that didn't fit, unless it is still
1514 * valid (no lines deleted) */
1515 if (row > 0 && bot_start + row
1516 + (int)wp->w_lines[j].wl_size > wp->w_height)
1517 {
1518 wp->w_lines_valid = idx + 1;
1519 break;
1520 }
1521 bot_start += wp->w_lines[idx++].wl_size;
1522
1523 /* stop at the last valid entry in w_lines[].wl_size */
1524 if (++j >= wp->w_lines_valid)
1525 {
1526 wp->w_lines_valid = idx;
1527 break;
1528 }
1529 }
1530#ifdef FEAT_DIFF
1531 /* Correct the first entry for filler lines at the top
1532 * when it won't get updated below. */
1533 if (wp->w_p_diff && bot_start > 0)
1534 wp->w_lines[0].wl_size =
1535 plines_win_nofill(wp, wp->w_topline, TRUE)
1536 + wp->w_topfill;
1537#endif
1538 }
1539 }
1540 }
1541
1542 /* When starting redraw in the first line, redraw all lines. When
1543 * there is only one window it's probably faster to clear the screen
1544 * first. */
1545 if (mid_start == 0)
1546 {
1547 mid_end = wp->w_height;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001548 if (ONE_WINDOW && !WIN_IS_POPUP(wp))
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001549 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001550 /* Clear the screen when it was not done by win_del_lines() or
1551 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1552 * then. */
1553 if (screen_cleared != TRUE)
1554 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001555 /* The screen was cleared, redraw the tab pages line. */
1556 if (redraw_tabline)
1557 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001560
1561 /* When win_del_lines() or win_ins_lines() caused the screen to be
1562 * cleared (only happens for the first window) or when screenclear()
1563 * was called directly above, "must_redraw" will have been set to
1564 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1565 if (screen_cleared == TRUE)
1566 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 else
1569 {
1570 /* Not VALID or INVERTED: redraw all lines. */
1571 mid_start = 0;
1572 mid_end = wp->w_height;
1573 }
1574
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001575 if (type == SOME_VALID)
1576 {
1577 /* SOME_VALID: redraw all lines. */
1578 mid_start = 0;
1579 mid_end = wp->w_height;
1580 type = NOT_VALID;
1581 }
1582
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 /* check if we are updating or removing the inverted part */
1584 if ((VIsual_active && buf == curwin->w_buffer)
1585 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1586 {
1587 linenr_T from, to;
1588
1589 if (VIsual_active)
1590 {
1591 if (VIsual_active
1592 && (VIsual_mode != wp->w_old_visual_mode
1593 || type == INVERTED_ALL))
1594 {
1595 /*
1596 * If the type of Visual selection changed, redraw the whole
1597 * selection. Also when the ownership of the X selection is
1598 * gained or lost.
1599 */
1600 if (curwin->w_cursor.lnum < VIsual.lnum)
1601 {
1602 from = curwin->w_cursor.lnum;
1603 to = VIsual.lnum;
1604 }
1605 else
1606 {
1607 from = VIsual.lnum;
1608 to = curwin->w_cursor.lnum;
1609 }
1610 /* redraw more when the cursor moved as well */
1611 if (wp->w_old_cursor_lnum < from)
1612 from = wp->w_old_cursor_lnum;
1613 if (wp->w_old_cursor_lnum > to)
1614 to = wp->w_old_cursor_lnum;
1615 if (wp->w_old_visual_lnum < from)
1616 from = wp->w_old_visual_lnum;
1617 if (wp->w_old_visual_lnum > to)
1618 to = wp->w_old_visual_lnum;
1619 }
1620 else
1621 {
1622 /*
1623 * Find the line numbers that need to be updated: The lines
1624 * between the old cursor position and the current cursor
1625 * position. Also check if the Visual position changed.
1626 */
1627 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1628 {
1629 from = curwin->w_cursor.lnum;
1630 to = wp->w_old_cursor_lnum;
1631 }
1632 else
1633 {
1634 from = wp->w_old_cursor_lnum;
1635 to = curwin->w_cursor.lnum;
1636 if (from == 0) /* Visual mode just started */
1637 from = to;
1638 }
1639
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001640 if (VIsual.lnum != wp->w_old_visual_lnum
1641 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 {
1643 if (wp->w_old_visual_lnum < from
1644 && wp->w_old_visual_lnum != 0)
1645 from = wp->w_old_visual_lnum;
1646 if (wp->w_old_visual_lnum > to)
1647 to = wp->w_old_visual_lnum;
1648 if (VIsual.lnum < from)
1649 from = VIsual.lnum;
1650 if (VIsual.lnum > to)
1651 to = VIsual.lnum;
1652 }
1653 }
1654
1655 /*
1656 * If in block mode and changed column or curwin->w_curswant:
1657 * update all lines.
1658 * First compute the actual start and end column.
1659 */
1660 if (VIsual_mode == Ctrl_V)
1661 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001662 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001663#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001664 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaar404406a2014-10-09 13:24:43 +02001666 if (curwin->w_p_lbr)
1667 ve_flags = VE_ALL;
1668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001670#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001671 ve_flags = save_ve_flags;
1672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 ++toc;
1674 if (curwin->w_curswant == MAXCOL)
1675 toc = MAXCOL;
1676
1677 if (fromc != wp->w_old_cursor_fcol
1678 || toc != wp->w_old_cursor_lcol)
1679 {
1680 if (from > VIsual.lnum)
1681 from = VIsual.lnum;
1682 if (to < VIsual.lnum)
1683 to = VIsual.lnum;
1684 }
1685 wp->w_old_cursor_fcol = fromc;
1686 wp->w_old_cursor_lcol = toc;
1687 }
1688 }
1689 else
1690 {
1691 /* Use the line numbers of the old Visual area. */
1692 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1693 {
1694 from = wp->w_old_cursor_lnum;
1695 to = wp->w_old_visual_lnum;
1696 }
1697 else
1698 {
1699 from = wp->w_old_visual_lnum;
1700 to = wp->w_old_cursor_lnum;
1701 }
1702 }
1703
1704 /*
1705 * There is no need to update lines above the top of the window.
1706 */
1707 if (from < wp->w_topline)
1708 from = wp->w_topline;
1709
1710 /*
1711 * If we know the value of w_botline, use it to restrict the update to
1712 * the lines that are visible in the window.
1713 */
1714 if (wp->w_valid & VALID_BOTLINE)
1715 {
1716 if (from >= wp->w_botline)
1717 from = wp->w_botline - 1;
1718 if (to >= wp->w_botline)
1719 to = wp->w_botline - 1;
1720 }
1721
1722 /*
1723 * Find the minimal part to be updated.
1724 * Watch out for scrolling that made entries in w_lines[] invalid.
1725 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1726 * top_end; need to redraw from top_end to the "to" line.
1727 * A middle mouse click with a Visual selection may change the text
1728 * above the Visual area and reset wl_valid, do count these for
1729 * mid_end (in srow).
1730 */
1731 if (mid_start > 0)
1732 {
1733 lnum = wp->w_topline;
1734 idx = 0;
1735 srow = 0;
1736 if (scrolled_down)
1737 mid_start = top_end;
1738 else
1739 mid_start = 0;
1740 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1741 {
1742 if (wp->w_lines[idx].wl_valid)
1743 mid_start += wp->w_lines[idx].wl_size;
1744 else if (!scrolled_down)
1745 srow += wp->w_lines[idx].wl_size;
1746 ++idx;
1747# ifdef FEAT_FOLDING
1748 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1749 lnum = wp->w_lines[idx].wl_lnum;
1750 else
1751# endif
1752 ++lnum;
1753 }
1754 srow += mid_start;
1755 mid_end = wp->w_height;
1756 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1757 {
1758 if (wp->w_lines[idx].wl_valid
1759 && wp->w_lines[idx].wl_lnum >= to + 1)
1760 {
1761 /* Only update until first row of this line */
1762 mid_end = srow;
1763 break;
1764 }
1765 srow += wp->w_lines[idx].wl_size;
1766 }
1767 }
1768 }
1769
1770 if (VIsual_active && buf == curwin->w_buffer)
1771 {
1772 wp->w_old_visual_mode = VIsual_mode;
1773 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1774 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001775 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 wp->w_old_curswant = curwin->w_curswant;
1777 }
1778 else
1779 {
1780 wp->w_old_visual_mode = 0;
1781 wp->w_old_cursor_lnum = 0;
1782 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001783 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1787 /* reset got_int, otherwise regexp won't work */
1788 save_got_int = got_int;
1789 got_int = 0;
1790#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001791#ifdef SYN_TIME_LIMIT
1792 /* Set the time limit to 'redrawtime'. */
1793 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001794 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#ifdef FEAT_FOLDING
1797 win_foldinfo.fi_level = 0;
1798#endif
1799
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001800#ifdef FEAT_MENU
1801 /*
1802 * Draw the window toolbar, if there is one.
1803 * TODO: only when needed.
1804 */
1805 if (winbar_height(wp) > 0)
1806 redraw_win_toolbar(wp);
1807#endif
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 /*
1810 * Update all the window rows.
1811 */
1812 idx = 0; /* first entry in w_lines[].wl_size */
1813 row = 0;
1814 srow = 0;
1815 lnum = wp->w_topline; /* first line shown in window */
1816 for (;;)
1817 {
1818 /* stop updating when reached the end of the window (check for _past_
1819 * the end of the window is at the end of the loop) */
1820 if (row == wp->w_height)
1821 {
1822 didline = TRUE;
1823 break;
1824 }
1825
1826 /* stop updating when hit the end of the file */
1827 if (lnum > buf->b_ml.ml_line_count)
1828 {
1829 eof = TRUE;
1830 break;
1831 }
1832
1833 /* Remember the starting row of the line that is going to be dealt
1834 * with. It is used further down when the line doesn't fit. */
1835 srow = row;
1836
1837 /*
1838 * Update a line when it is in an area that needs updating, when it
1839 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001840 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 * win_del_lines(), check if the current line includes it.
1842 * When syntax folding is being used, the saved syntax states will
1843 * already have been updated, we can't see where the syntax state is
1844 * the same again, just update until the end of the window.
1845 */
1846 if (row < top_end
1847 || (row >= mid_start && row < mid_end)
1848#ifdef FEAT_SEARCH_EXTRA
1849 || top_to_mod
1850#endif
1851 || idx >= wp->w_lines_valid
1852 || (row + wp->w_lines[idx].wl_size > bot_start)
1853 || (mod_top != 0
1854 && (lnum == mod_top
1855 || (lnum >= mod_top
1856 && (lnum < mod_bot
1857#ifdef FEAT_SYN_HL
1858 || did_update == DID_FOLD
1859 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001860 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 && (
1862# ifdef FEAT_FOLDING
1863 (foldmethodIsSyntax(wp)
1864 && hasAnyFolding(wp)) ||
1865# endif
1866 syntax_check_changed(lnum)))
1867#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001868#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001869 /* match in fixed position might need redraw
1870 * if lines were inserted or deleted */
1871 || (wp->w_match_head != NULL
1872 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 )))))
1875 {
1876#ifdef FEAT_SEARCH_EXTRA
1877 if (lnum == mod_top)
1878 top_to_mod = FALSE;
1879#endif
1880
1881 /*
1882 * When at start of changed lines: May scroll following lines
1883 * up or down to minimize redrawing.
1884 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001885 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887 if (lnum == mod_top
1888 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001889 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
1891 int old_rows = 0;
1892 int new_rows = 0;
1893 int xtra_rows;
1894 linenr_T l;
1895
1896 /* Count the old number of window rows, using w_lines[], which
1897 * should still contain the sizes for the lines as they are
1898 * currently displayed. */
1899 for (i = idx; i < wp->w_lines_valid; ++i)
1900 {
1901 /* Only valid lines have a meaningful wl_lnum. Invalid
1902 * lines are part of the changed area. */
1903 if (wp->w_lines[i].wl_valid
1904 && wp->w_lines[i].wl_lnum == mod_bot)
1905 break;
1906 old_rows += wp->w_lines[i].wl_size;
1907#ifdef FEAT_FOLDING
1908 if (wp->w_lines[i].wl_valid
1909 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1910 {
1911 /* Must have found the last valid entry above mod_bot.
1912 * Add following invalid entries. */
1913 ++i;
1914 while (i < wp->w_lines_valid
1915 && !wp->w_lines[i].wl_valid)
1916 old_rows += wp->w_lines[i++].wl_size;
1917 break;
1918 }
1919#endif
1920 }
1921
1922 if (i >= wp->w_lines_valid)
1923 {
1924 /* We can't find a valid line below the changed lines,
1925 * need to redraw until the end of the window.
1926 * Inserting/deleting lines has no use. */
1927 bot_start = 0;
1928 }
1929 else
1930 {
1931 /* Able to count old number of rows: Count new window
1932 * rows, and may insert/delete lines */
1933 j = idx;
1934 for (l = lnum; l < mod_bot; ++l)
1935 {
1936#ifdef FEAT_FOLDING
1937 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1938 ++new_rows;
1939 else
1940#endif
1941#ifdef FEAT_DIFF
1942 if (l == wp->w_topline)
1943 new_rows += plines_win_nofill(wp, l, TRUE)
1944 + wp->w_topfill;
1945 else
1946#endif
1947 new_rows += plines_win(wp, l, TRUE);
1948 ++j;
1949 if (new_rows > wp->w_height - row - 2)
1950 {
1951 /* it's getting too much, must redraw the rest */
1952 new_rows = 9999;
1953 break;
1954 }
1955 }
1956 xtra_rows = new_rows - old_rows;
1957 if (xtra_rows < 0)
1958 {
1959 /* May scroll text up. If there is not enough
1960 * remaining text or scrolling fails, must redraw the
1961 * rest. If scrolling works, must redraw the text
1962 * below the scrolled text. */
1963 if (row - xtra_rows >= wp->w_height - 2)
1964 mod_bot = MAXLNUM;
1965 else
1966 {
1967 check_for_delay(FALSE);
1968 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001969 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 mod_bot = MAXLNUM;
1971 else
1972 bot_start = wp->w_height + xtra_rows;
1973 }
1974 }
1975 else if (xtra_rows > 0)
1976 {
1977 /* May scroll text down. If there is not enough
1978 * remaining text of scrolling fails, must redraw the
1979 * rest. */
1980 if (row + xtra_rows >= wp->w_height - 2)
1981 mod_bot = MAXLNUM;
1982 else
1983 {
1984 check_for_delay(FALSE);
1985 if (win_ins_lines(wp, row + old_rows,
1986 xtra_rows, FALSE, FALSE) == FAIL)
1987 mod_bot = MAXLNUM;
1988 else if (top_end > row + old_rows)
1989 /* Scrolled the part at the top that requires
1990 * updating down. */
1991 top_end += xtra_rows;
1992 }
1993 }
1994
1995 /* When not updating the rest, may need to move w_lines[]
1996 * entries. */
1997 if (mod_bot != MAXLNUM && i != j)
1998 {
1999 if (j < i)
2000 {
2001 int x = row + new_rows;
2002
2003 /* move entries in w_lines[] upwards */
2004 for (;;)
2005 {
2006 /* stop at last valid entry in w_lines[] */
2007 if (i >= wp->w_lines_valid)
2008 {
2009 wp->w_lines_valid = j;
2010 break;
2011 }
2012 wp->w_lines[j] = wp->w_lines[i];
2013 /* stop at a line that won't fit */
2014 if (x + (int)wp->w_lines[j].wl_size
2015 > wp->w_height)
2016 {
2017 wp->w_lines_valid = j + 1;
2018 break;
2019 }
2020 x += wp->w_lines[j++].wl_size;
2021 ++i;
2022 }
2023 if (bot_start > x)
2024 bot_start = x;
2025 }
2026 else /* j > i */
2027 {
2028 /* move entries in w_lines[] downwards */
2029 j -= i;
2030 wp->w_lines_valid += j;
2031 if (wp->w_lines_valid > wp->w_height)
2032 wp->w_lines_valid = wp->w_height;
2033 for (i = wp->w_lines_valid; i - j >= idx; --i)
2034 wp->w_lines[i] = wp->w_lines[i - j];
2035
2036 /* The w_lines[] entries for inserted lines are
2037 * now invalid, but wl_size may be used above.
2038 * Reset to zero. */
2039 while (i >= idx)
2040 {
2041 wp->w_lines[i].wl_size = 0;
2042 wp->w_lines[i--].wl_valid = FALSE;
2043 }
2044 }
2045 }
2046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048
2049#ifdef FEAT_FOLDING
2050 /*
2051 * When lines are folded, display one line for all of them.
2052 * Otherwise, display normally (can be several display lines when
2053 * 'wrap' is on).
2054 */
2055 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2056 if (fold_count != 0)
2057 {
2058 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2059 ++row;
2060 --fold_count;
2061 wp->w_lines[idx].wl_folded = TRUE;
2062 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2063# ifdef FEAT_SYN_HL
2064 did_update = DID_FOLD;
2065# endif
2066 }
2067 else
2068#endif
2069 if (idx < wp->w_lines_valid
2070 && wp->w_lines[idx].wl_valid
2071 && wp->w_lines[idx].wl_lnum == lnum
2072 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002073 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002074 && !WIN_IS_POPUP(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 && srow + wp->w_lines[idx].wl_size > wp->w_height
2076#ifdef FEAT_DIFF
2077 && diff_check_fill(wp, lnum) == 0
2078#endif
2079 )
2080 {
2081 /* This line is not going to fit. Don't draw anything here,
2082 * will draw "@ " lines below. */
2083 row = wp->w_height + 1;
2084 }
2085 else
2086 {
2087#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02002088 prepare_search_hl(wp, &search_hl, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#endif
2090#ifdef FEAT_SYN_HL
2091 /* Let the syntax stuff know we skipped a few lines. */
2092 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002093 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 syntax_end_parsing(syntax_last_parsed + 1);
2095#endif
2096
2097 /*
2098 * Display one line.
2099 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002100 row = win_line(wp, lnum, srow, wp->w_height,
2101 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
2103#ifdef FEAT_FOLDING
2104 wp->w_lines[idx].wl_folded = FALSE;
2105 wp->w_lines[idx].wl_lastlnum = lnum;
2106#endif
2107#ifdef FEAT_SYN_HL
2108 did_update = DID_LINE;
2109 syntax_last_parsed = lnum;
2110#endif
2111 }
2112
2113 wp->w_lines[idx].wl_lnum = lnum;
2114 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002115
2116 /* Past end of the window or end of the screen. Note that after
2117 * resizing wp->w_height may be end up too big. That's a problem
2118 * elsewhere, but prevent a crash here. */
2119 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
2121 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002122 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2124 ++idx;
2125 break;
2126 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002127 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 wp->w_lines[idx].wl_size = row - srow;
2129 ++idx;
2130#ifdef FEAT_FOLDING
2131 lnum += fold_count + 1;
2132#else
2133 ++lnum;
2134#endif
2135 }
2136 else
2137 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002138 if (wp->w_p_rnu)
2139 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002140#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002141 // 'relativenumber' set: The text doesn't need to be drawn, but
2142 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002143 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2144 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002145 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002146 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002147#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002148 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002149 }
2150
2151 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 row += wp->w_lines[idx++].wl_size;
2153 if (row > wp->w_height) /* past end of screen */
2154 break;
2155#ifdef FEAT_FOLDING
2156 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2157#else
2158 ++lnum;
2159#endif
2160#ifdef FEAT_SYN_HL
2161 did_update = DID_NONE;
2162#endif
2163 }
2164
2165 if (lnum > buf->b_ml.ml_line_count)
2166 {
2167 eof = TRUE;
2168 break;
2169 }
2170 }
2171 /*
2172 * End of loop over all window lines.
2173 */
2174
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002175#ifdef FEAT_VTP
2176 /* Rewrite the character at the end of the screen line. */
2177 if (use_vtp())
2178 {
2179 int i;
2180
2181 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002182 if (enc_utf8)
2183 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2184 LineOffset[i] + screen_Columns) > 1)
2185 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2186 else
2187 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2188 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002189 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2190 }
2191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 if (idx > wp->w_lines_valid)
2194 wp->w_lines_valid = idx;
2195
2196#ifdef FEAT_SYN_HL
2197 /*
2198 * Let the syntax stuff know we stop parsing here.
2199 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002200 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 syntax_end_parsing(syntax_last_parsed + 1);
2202#endif
2203
2204 /*
2205 * If we didn't hit the end of the file, and we didn't finish the last
2206 * line we were working on, then the line didn't fit.
2207 */
2208 wp->w_empty_rows = 0;
2209#ifdef FEAT_DIFF
2210 wp->w_filler_rows = 0;
2211#endif
2212 if (!eof && !didline)
2213 {
2214 if (lnum == wp->w_topline)
2215 {
2216 /*
2217 * Single line that does not fit!
2218 * Don't overwrite it, it can be edited.
2219 */
2220 wp->w_botline = lnum + 1;
2221 }
2222#ifdef FEAT_DIFF
2223 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2224 {
2225 /* Window ends in filler lines. */
2226 wp->w_botline = lnum;
2227 wp->w_filler_rows = wp->w_height - srow;
2228 }
2229#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002230#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002231 else if (WIN_IS_POPUP(wp))
Bram Moolenaar17146962019-05-30 00:12:11 +02002232 {
2233 // popup line that doesn't fit is left as-is
2234 wp->w_botline = lnum;
2235 }
2236#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002237 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2238 {
2239 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2240
2241 /*
2242 * Last line isn't finished: Display "@@@" in the last screen line.
2243 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002244 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002245 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002246 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002247 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002248 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002249 set_empty_rows(wp, srow);
2250 wp->w_botline = lnum;
2251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2253 {
2254 /*
2255 * Last line isn't finished: Display "@@@" at the end.
2256 */
2257 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2258 W_WINROW(wp) + wp->w_height,
2259 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002260 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 set_empty_rows(wp, srow);
2262 wp->w_botline = lnum;
2263 }
2264 else
2265 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002266 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 wp->w_botline = lnum;
2268 }
2269 }
2270 else
2271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (eof) /* we hit the end of the file */
2274 {
2275 wp->w_botline = buf->b_ml.ml_line_count + 1;
2276#ifdef FEAT_DIFF
2277 j = diff_check_fill(wp, wp->w_botline);
2278 if (j > 0 && !wp->w_botfill)
2279 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002280 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (char2cells(fill_diff) > 1)
2282 i = '-';
2283 else
2284 i = fill_diff;
2285 if (row + j > wp->w_height)
2286 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002287 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 row += j;
2289 }
2290#endif
2291 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002292 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 wp->w_botline = lnum;
2294
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002295 // Make sure the rest of the screen is blank
2296 // put '~'s on rows that aren't part of the file.
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002297 win_draw_end(wp, WIN_IS_POPUP(wp) ? ' ' : '~',
2298 ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002301#ifdef SYN_TIME_LIMIT
2302 syn_set_timeout(NULL);
2303#endif
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 /* Reset the type of redrawing required, the window has been updated. */
2306 wp->w_redr_type = 0;
2307#ifdef FEAT_DIFF
2308 wp->w_old_topfill = wp->w_topfill;
2309 wp->w_old_botfill = wp->w_botfill;
2310#endif
2311
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002312 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 /*
2315 * There is a trick with w_botline. If we invalidate it on each
2316 * change that might modify it, this will cause a lot of expensive
2317 * calls to plines() in update_topline() each time. Therefore the
2318 * value of w_botline is often approximated, and this value is used to
2319 * compute the value of w_topline. If the value of w_botline was
2320 * wrong, check that the value of w_topline is correct (cursor is on
2321 * the visible part of the text). If it's not, we need to redraw
2322 * again. Mostly this just means scrolling up a few lines, so it
2323 * doesn't look too bad. Only do this for the current window (where
2324 * changes are relevant).
2325 */
2326 wp->w_valid |= VALID_BOTLINE;
2327 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2328 {
2329 recursive = TRUE;
2330 curwin->w_valid &= ~VALID_TOPLINE;
2331 update_topline(); /* may invalidate w_botline again */
2332 if (must_redraw != 0)
2333 {
2334 /* Don't update for changes in buffer again. */
2335 i = curbuf->b_mod_set;
2336 curbuf->b_mod_set = FALSE;
2337 win_update(curwin);
2338 must_redraw = 0;
2339 curbuf->b_mod_set = i;
2340 }
2341 recursive = FALSE;
2342 }
2343 }
2344
2345#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2346 /* restore got_int, unless CTRL-C was hit while redrawing */
2347 if (!got_int)
2348 got_int = save_got_int;
2349#endif
2350}
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002353 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2354 * Return the new offset.
2355 */
2356 static int
2357screen_fill_end(
2358 win_T *wp,
2359 int c1,
2360 int c2,
2361 int off,
2362 int width,
2363 int row,
2364 int endrow,
2365 int attr)
2366{
2367 int nn = off + width;
2368
2369 if (nn > wp->w_width)
2370 nn = wp->w_width;
2371#ifdef FEAT_RIGHTLEFT
2372 if (wp->w_p_rl)
2373 {
2374 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2375 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2376 c1, c2, attr);
2377 }
2378 else
2379#endif
2380 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2381 wp->w_wincol + off, (int)wp->w_wincol + nn,
2382 c1, c2, attr);
2383 return nn;
2384}
2385
2386/*
2387 * Clear lines near the end the window and mark the unused lines with "c1".
2388 * use "c2" as the filler character.
2389 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 */
2391 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002392win_draw_end(
2393 win_T *wp,
2394 int c1,
2395 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002396 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002397 int row,
2398 int endrow,
2399 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002402 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002403 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002404
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002405 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002406
2407 if (draw_margin)
2408 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002409#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002410 int fdc = compute_foldcolumn(wp, 0);
2411
2412 if (fdc > 0)
2413 // draw the fold column
2414 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002415 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002416#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002417#ifdef FEAT_SIGNS
2418 if (signcolumn_on(wp))
2419 // draw the sign column
2420 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002421 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002422#endif
2423 if ((wp->w_p_nu || wp->w_p_rnu)
2424 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2425 // draw the number column
2426 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002427 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430#ifdef FEAT_RIGHTLEFT
2431 if (wp->w_p_rl)
2432 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002434 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002435 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002437 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002438 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 else
2441#endif
2442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002444 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002445 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 set_empty_rows(wp, row);
2449}
2450
Bram Moolenaar1a384422010-07-14 19:53:30 +02002451#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002452/*
2453 * Advance **color_cols and return TRUE when there are columns to draw.
2454 */
2455 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002456advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002457{
2458 while (**color_cols >= 0 && vcol > **color_cols)
2459 ++*color_cols;
2460 return (**color_cols >= 0);
2461}
2462#endif
2463
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002464#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2465/*
2466 * Copy "text" to ScreenLines using "attr".
2467 * Returns the next screen column.
2468 */
2469 static int
2470text_to_screenline(win_T *wp, char_u *text, int col)
2471{
2472 int off = (int)(current_ScreenLine - ScreenLines);
2473
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002474 if (has_mbyte)
2475 {
2476 int cells;
2477 int u8c, u8cc[MAX_MCO];
2478 int i;
2479 int idx;
2480 int c_len;
2481 char_u *p;
2482# ifdef FEAT_ARABIC
2483 int prev_c = 0; /* previous Arabic character */
2484 int prev_c1 = 0; /* first composing char for prev_c */
2485# endif
2486
2487# ifdef FEAT_RIGHTLEFT
2488 if (wp->w_p_rl)
2489 idx = off;
2490 else
2491# endif
2492 idx = off + col;
2493
2494 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2495 for (p = text; *p != NUL; )
2496 {
2497 cells = (*mb_ptr2cells)(p);
2498 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002499 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002500# ifdef FEAT_RIGHTLEFT
2501 - (wp->w_p_rl ? col : 0)
2502# endif
2503 )
2504 break;
2505 ScreenLines[idx] = *p;
2506 if (enc_utf8)
2507 {
2508 u8c = utfc_ptr2char(p, u8cc);
2509 if (*p < 0x80 && u8cc[0] == 0)
2510 {
2511 ScreenLinesUC[idx] = 0;
2512#ifdef FEAT_ARABIC
2513 prev_c = u8c;
2514#endif
2515 }
2516 else
2517 {
2518#ifdef FEAT_ARABIC
2519 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2520 {
2521 /* Do Arabic shaping. */
2522 int pc, pc1, nc;
2523 int pcc[MAX_MCO];
2524 int firstbyte = *p;
2525
2526 /* The idea of what is the previous and next
2527 * character depends on 'rightleft'. */
2528 if (wp->w_p_rl)
2529 {
2530 pc = prev_c;
2531 pc1 = prev_c1;
2532 nc = utf_ptr2char(p + c_len);
2533 prev_c1 = u8cc[0];
2534 }
2535 else
2536 {
2537 pc = utfc_ptr2char(p + c_len, pcc);
2538 nc = prev_c;
2539 pc1 = pcc[0];
2540 }
2541 prev_c = u8c;
2542
2543 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2544 pc, pc1, nc);
2545 ScreenLines[idx] = firstbyte;
2546 }
2547 else
2548 prev_c = u8c;
2549#endif
2550 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002551 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002552 for (i = 0; i < Screen_mco; ++i)
2553 {
2554 ScreenLinesC[i][idx] = u8cc[i];
2555 if (u8cc[i] == 0)
2556 break;
2557 }
2558 }
2559 if (cells > 1)
2560 ScreenLines[idx + 1] = 0;
2561 }
2562 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2563 /* double-byte single width character */
2564 ScreenLines2[idx] = p[1];
2565 else if (cells > 1)
2566 /* double-width character */
2567 ScreenLines[idx + 1] = p[1];
2568 col += cells;
2569 idx += cells;
2570 p += c_len;
2571 }
2572 }
2573 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002574 {
2575 int len = (int)STRLEN(text);
2576
Bram Moolenaar02631462017-09-22 15:20:32 +02002577 if (len > wp->w_width - col)
2578 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002579 if (len > 0)
2580 {
2581#ifdef FEAT_RIGHTLEFT
2582 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002583 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002584 else
2585#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002586 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002587 col += len;
2588 }
2589 }
2590 return col;
2591}
2592#endif
2593
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#ifdef FEAT_FOLDING
2595/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002596 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2597 * space is available for window "wp", minus "col".
2598 */
2599 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002600compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002601{
2602 int fdc = wp->w_p_fdc;
2603 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002604 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002605
2606 if (fdc > wwidth - (col + wmw))
2607 fdc = wwidth - (col + wmw);
2608 return fdc;
2609}
2610
2611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 * Display one folded line.
2613 */
2614 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002615fold_line(
2616 win_T *wp,
2617 long fold_count,
2618 foldinfo_T *foldinfo,
2619 linenr_T lnum,
2620 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002622 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 pos_T *top, *bot;
2624 linenr_T lnume = lnum + fold_count - 1;
2625 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002626 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 int col;
2629 int txtcol;
2630 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002631 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 /* Build the fold line:
2634 * 1. Add the cmdwin_type for the command-line window
2635 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002636 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 * 4. Compose the text
2638 * 5. Add the text
2639 * 6. set highlighting for the Visual area an other text
2640 */
2641 col = 0;
2642
2643 /*
2644 * 1. Add the cmdwin_type for the command-line window
2645 * Ignores 'rightleft', this window is never right-left.
2646 */
2647#ifdef FEAT_CMDWIN
2648 if (cmdwin_type != 0 && wp == curwin)
2649 {
2650 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002651 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (enc_utf8)
2653 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 ++col;
2655 }
2656#endif
2657
2658 /*
2659 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002660 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002662 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (fdc > 0)
2664 {
2665 fill_foldcolumn(buf, wp, TRUE, lnum);
2666#ifdef FEAT_RIGHTLEFT
2667 if (wp->w_p_rl)
2668 {
2669 int i;
2670
Bram Moolenaar02631462017-09-22 15:20:32 +02002671 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002672 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 /* reverse the fold column */
2674 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002675 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
2677 else
2678#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002679 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 col += fdc;
2681 }
2682
2683#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002684# define RL_MEMSET(p, v, l) \
2685 do { \
2686 if (wp->w_p_rl) \
2687 for (ri = 0; ri < l; ++ri) \
2688 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2689 else \
2690 for (ri = 0; ri < l; ++ri) \
2691 ScreenAttrs[off + (p) + ri] = v; \
2692 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002694# define RL_MEMSET(p, v, l) \
2695 do { \
2696 for (ri = 0; ri < l; ++ri) \
2697 ScreenAttrs[off + (p) + ri] = v; \
2698 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699#endif
2700
Bram Moolenaar64486672010-05-16 15:46:46 +02002701 /* Set all attributes of the 'number' or 'relativenumber' column and the
2702 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002703 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705#ifdef FEAT_SIGNS
2706 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002707 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002709 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 if (len > 0)
2711 {
2712 if (len > 2)
2713 len = 2;
2714# ifdef FEAT_RIGHTLEFT
2715 if (wp->w_p_rl)
2716 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002717 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002718 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 else
2720# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002721 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 col += len;
2723 }
2724 }
2725#endif
2726
2727 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002728 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002730 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002732 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (len > 0)
2734 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002735 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002736 long num;
2737 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002738
2739 if (len > w + 1)
2740 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002741
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002742 if (wp->w_p_nu && !wp->w_p_rnu)
2743 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002744 num = (long)lnum;
2745 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002746 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002747 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002748 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002749 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002750 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002751 /* 'number' + 'relativenumber': cursor line shows absolute
2752 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002753 num = lnum;
2754 fmt = "%-*ld ";
2755 }
2756 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002757
Bram Moolenaar700e7342013-01-30 12:31:36 +01002758 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759#ifdef FEAT_RIGHTLEFT
2760 if (wp->w_p_rl)
2761 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002762 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002763 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else
2765#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002766 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 col += len;
2768 }
2769 }
2770
2771 /*
2772 * 4. Compose the folded-line string with 'foldtext', if set.
2773 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002774 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776 txtcol = col; /* remember where text starts */
2777
2778 /*
2779 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2780 * Right-left text is put in columns 0 - number-col, normal text is put
2781 * in columns number-col - window-width.
2782 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002783 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /* Fill the rest of the line with the fold filler */
2786#ifdef FEAT_RIGHTLEFT
2787 if (wp->w_p_rl)
2788 col -= txtcol;
2789#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002790 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791#ifdef FEAT_RIGHTLEFT
2792 - (wp->w_p_rl ? txtcol : 0)
2793#endif
2794 )
2795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (enc_utf8)
2797 {
2798 if (fill_fold >= 0x80)
2799 {
2800 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002801 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002802 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002807 ScreenLines[off + col] = fill_fold;
2808 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002809 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002811 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002812 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814
2815 if (text != buf)
2816 vim_free(text);
2817
2818 /*
2819 * 6. set highlighting for the Visual area an other text.
2820 * If all folded lines are in the Visual area, highlight the line.
2821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2823 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002824 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
2826 /* Visual is after curwin->w_cursor */
2827 top = &curwin->w_cursor;
2828 bot = &VIsual;
2829 }
2830 else
2831 {
2832 /* Visual is before curwin->w_cursor */
2833 top = &VIsual;
2834 bot = &curwin->w_cursor;
2835 }
2836 if (lnum >= top->lnum
2837 && lnume <= bot->lnum
2838 && (VIsual_mode != 'v'
2839 || ((lnum > top->lnum
2840 || (lnum == top->lnum
2841 && top->col == 0))
2842 && (lnume < bot->lnum
2843 || (lnume == bot->lnum
2844 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002845 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
2847 if (VIsual_mode == Ctrl_V)
2848 {
2849 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002850 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002852 if (wp->w_old_cursor_lcol != MAXCOL
2853 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002854 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 len = wp->w_old_cursor_lcol;
2856 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002857 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002858 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002859 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
2861 }
2862 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002865 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002870#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002871 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2872 if (wp->w_p_cc_cols)
2873 {
2874 int i = 0;
2875 int j = wp->w_p_cc_cols[i];
2876 int old_txtcol = txtcol;
2877
2878 while (j > -1)
2879 {
2880 txtcol += j;
2881 if (wp->w_p_wrap)
2882 txtcol -= wp->w_skipcol;
2883 else
2884 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002885 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002886 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002887 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002888 txtcol = old_txtcol;
2889 j = wp->w_p_cc_cols[++i];
2890 }
2891 }
2892
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002893 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002894 if (wp->w_p_cuc)
2895 {
2896 txtcol += wp->w_virtcol;
2897 if (wp->w_p_wrap)
2898 txtcol -= wp->w_skipcol;
2899 else
2900 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002901 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002902 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002903 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002904 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar02631462017-09-22 15:20:32 +02002907 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002908 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 /*
2911 * Update w_cline_height and w_cline_folded if the cursor line was
2912 * updated (saves a call to plines() later).
2913 */
2914 if (wp == curwin
2915 && lnum <= curwin->w_cursor.lnum
2916 && lnume >= curwin->w_cursor.lnum)
2917 {
2918 curwin->w_cline_row = row;
2919 curwin->w_cline_height = 1;
2920 curwin->w_cline_folded = TRUE;
2921 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2922 }
2923}
2924
2925/*
2926 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2927 */
2928 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002929copy_text_attr(
2930 int off,
2931 char_u *buf,
2932 int len,
2933 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002935 int i;
2936
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (enc_utf8)
2939 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002940 for (i = 0; i < len; ++i)
2941 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943
2944/*
2945 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002946 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 */
2948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002949fill_foldcolumn(
2950 char_u *p,
2951 win_T *wp,
2952 int closed, /* TRUE of FALSE */
2953 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 int i = 0;
2956 int level;
2957 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002958 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002959 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
2961 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002962 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 level = win_foldinfo.fi_level;
2965 if (level > 0)
2966 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002967 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002968 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 /* If the column is too narrow, we start at the lowest level that
2971 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002972 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (first_level < 1)
2974 first_level = 1;
2975
Bram Moolenaar1c934292015-01-27 16:39:29 +01002976 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 if (win_foldinfo.fi_lnum == lnum
2979 && first_level + i >= win_foldinfo.fi_low_level)
2980 p[i] = '-';
2981 else if (first_level == 1)
2982 p[i] = '|';
2983 else if (first_level + i <= 9)
2984 p[i] = '0' + first_level + i;
2985 else
2986 p[i] = '>';
2987 if (first_level + i == level)
2988 break;
2989 }
2990 }
2991 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002992 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993}
2994#endif /* FEAT_FOLDING */
2995
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002996#ifdef FEAT_TEXT_PROP
2997static textprop_T *current_text_props = NULL;
2998static buf_T *current_buf = NULL;
2999
3000 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003001text_prop_compare(const void *s1, const void *s2)
3002{
3003 int idx1, idx2;
3004 proptype_T *pt1, *pt2;
3005 colnr_T col1, col2;
3006
3007 idx1 = *(int *)s1;
3008 idx2 = *(int *)s2;
3009 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3010 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3011 if (pt1 == pt2)
3012 return 0;
3013 if (pt1 == NULL)
3014 return -1;
3015 if (pt2 == NULL)
3016 return 1;
3017 if (pt1->pt_priority != pt2->pt_priority)
3018 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3019 col1 = current_text_props[idx1].tp_col;
3020 col2 = current_text_props[idx2].tp_col;
3021 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3022}
3023#endif
3024
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003025#ifdef FEAT_SIGNS
3026/*
3027 * Get information needed to display the sign in line 'lnum' in window 'wp'.
3028 * If 'nrcol' is TRUE, the sign is going to be displayed in the number column.
3029 * Otherwise the sign is going to be displayed in the sign column.
3030 */
3031 static void
3032get_sign_display_info(
3033 int nrcol,
3034 win_T *wp,
Bram Moolenaar4e038572019-07-04 18:28:35 +02003035 linenr_T lnum UNUSED,
3036 sign_attrs_T *sattr,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003037 int wcr_attr,
3038 int row,
3039 int startrow,
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003040 int filler_lines UNUSED,
3041 int filler_todo UNUSED,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003042 int *c_extrap,
3043 int *c_finalp,
3044 char_u *extra,
3045 char_u **pp_extra,
3046 int *n_extrap,
3047 int *char_attrp)
3048{
3049 int text_sign;
3050# ifdef FEAT_SIGN_ICONS
3051 int icon_sign;
3052# endif
3053
3054 // Draw two cells with the sign value or blank.
3055 *c_extrap = ' ';
3056 *c_finalp = NUL;
3057 if (nrcol)
3058 *n_extrap = number_width(wp) + 1;
3059 else
3060 {
3061 *char_attrp = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
3062 *n_extrap = 2;
3063 }
3064
3065 if (row == startrow
3066#ifdef FEAT_DIFF
3067 + filler_lines && filler_todo <= 0
3068#endif
3069 )
3070 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003071 text_sign = (sattr->text != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003072# ifdef FEAT_SIGN_ICONS
Bram Moolenaar4e038572019-07-04 18:28:35 +02003073 icon_sign = (sattr->icon != NULL) ? sattr->typenr : 0;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003074 if (gui.in_use && icon_sign != 0)
3075 {
3076 // Use the image in this position.
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003077 if (nrcol)
3078 {
3079 *c_extrap = NUL;
3080 sprintf((char *)extra, "%-*c ", number_width(wp), SIGN_BYTE);
3081 *pp_extra = extra;
3082 *n_extrap = (int)STRLEN(*pp_extra);
3083 }
3084 else
3085 *c_extrap = SIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003086# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4e038572019-07-04 18:28:35 +02003087 if (netbeans_active() && (buf_signcount(wp->w_buffer, lnum) > 1))
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003088 {
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003089 if (nrcol)
3090 {
3091 *c_extrap = NUL;
3092 sprintf((char *)extra, "%-*c ", number_width(wp),
3093 MULTISIGN_BYTE);
3094 *pp_extra = extra;
3095 *n_extrap = (int)STRLEN(*pp_extra);
3096 }
3097 else
3098 *c_extrap = MULTISIGN_BYTE;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003099 }
3100# endif
Bram Moolenaar4dff4ae2019-06-19 16:31:28 +02003101 *c_finalp = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003102 *char_attrp = icon_sign;
3103 }
3104 else
3105# endif
3106 if (text_sign != 0)
3107 {
Bram Moolenaar4e038572019-07-04 18:28:35 +02003108 *pp_extra = sattr->text;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003109 if (*pp_extra != NULL)
3110 {
3111 if (nrcol)
3112 {
Bram Moolenaard6bcff42019-07-18 12:48:16 +02003113 int n, width = number_width(wp) - 2;
3114
3115 for (n = 0; n < width; n++)
3116 extra[n] = ' ';
3117 extra[n] = 0;
3118 STRCAT(extra, *pp_extra);
3119 STRCAT(extra, " ");
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003120 *pp_extra = extra;
3121 }
3122 *c_extrap = NUL;
3123 *c_finalp = NUL;
3124 *n_extrap = (int)STRLEN(*pp_extra);
3125 }
Bram Moolenaar4e038572019-07-04 18:28:35 +02003126 *char_attrp = sattr->texthl;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003127 }
3128 }
3129}
3130#endif
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132/*
3133 * Display line "lnum" of window 'wp' on the screen.
3134 * Start at row "startrow", stop when "endrow" is reached.
3135 * wp->w_virtcol needs to be valid.
3136 *
3137 * Return the number of last row the line occupies.
3138 */
3139 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003140win_line(
3141 win_T *wp,
3142 linenr_T lnum,
3143 int startrow,
3144 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003145 int nochange UNUSED, // not updating for changed text
3146 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147{
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003148 int col = 0; // visual column on screen
3149 unsigned off; // offset in ScreenLines/ScreenAttrs
3150 int c = 0; // init for GCC
3151 long vcol = 0; // virtual column (for tabs)
Bram Moolenaard574ea22015-01-14 19:35:14 +01003152#ifdef FEAT_LINEBREAK
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003153 long vcol_sbr = -1; // virtual column after showbreak
Bram Moolenaard574ea22015-01-14 19:35:14 +01003154#endif
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003155 long vcol_prev = -1; // "vcol" of previous character
3156 char_u *line; // current line
3157 char_u *ptr; // current position in "line"
3158 int row; // row in the window, excl w_winrow
3159 int screen_row; // row on the screen, incl w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003161 char_u extra[21]; // "%ld " and 'fdc' must fit in here
3162 int n_extra = 0; // number of extra chars
3163 char_u *p_extra = NULL; // string of extra chars, plus NUL
3164 char_u *p_extra_free = NULL; // p_extra needs to be freed
3165 int c_extra = NUL; // extra chars, all the same
3166 int c_final = NUL; // final char, mandatory if set
3167 int extra_attr = 0; // attributes when n_extra != 0
3168 static char_u *at_end_str = (char_u *)""; // used for p_extra when
3169 // displaying lcs_eol at end-of-line
3170 int lcs_eol_one = lcs_eol; // lcs_eol until it's been used
3171 int lcs_prec_todo = lcs_prec; // lcs_prec until it's been used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
Bram Moolenaarf8a07122019-07-01 22:06:07 +02003173 // saved "extra" items for when draw_state becomes WL_LINE (again)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 int saved_n_extra = 0;
3175 char_u *saved_p_extra = NULL;
3176 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003177 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int saved_char_attr = 0;
3179
3180 int n_attr = 0; /* chars with special attr */
3181 int saved_attr2 = 0; /* char_attr saved for n_attr */
3182 int n_attr3 = 0; /* chars with overruling special attr */
3183 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3184
3185 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3186
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003187 int fromcol = -10; // start of inverting
3188 int tocol = MAXCOL; // end of inverting
3189 int fromcol_prev = -2; // start of inverting after cursor
3190 int noinvcur = FALSE; // don't invert the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003192 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 pos_T pos;
3194 long v;
3195
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003196 int char_attr = 0; // attributes for next character
3197 int attr_pri = FALSE; // char_attr has priority
3198 int area_highlighting = FALSE; // Visual or incsearch highlighting
3199 // in this line
3200 int vi_attr = 0; // attributes for Visual and incsearch
3201 // highlighting
3202 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003203 int win_attr = 0; // background for whole window, except
3204 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003205 int area_attr = 0; // attributes desired by highlighting
3206 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003208 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 int syntax_attr = 0; /* attributes desired by syntax */
3210 int has_syntax = FALSE; /* this buffer has syntax highl. */
3211 int save_did_emsg;
Bram Moolenaar1a384422010-07-14 19:53:30 +02003212 int draw_color_col = FALSE; /* highlight colorcolumn */
3213 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003214#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02003215 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003216#ifdef FEAT_TEXT_PROP
3217 int text_prop_count;
3218 int text_prop_next = 0; // next text property to use
3219 textprop_T *text_props = NULL;
3220 int *text_prop_idxs = NULL;
3221 int text_props_active = 0;
3222 proptype_T *text_prop_type = NULL;
3223 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003224 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003225#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003226#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003227 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003228# define SPWORDLEN 150
3229 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003230 int nextlinecol = 0; /* column where nextline[] starts */
3231 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003232 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003233 int spell_attr = 0; /* attributes desired by spelling */
3234 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003235 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3236 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003237 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003238 static int cap_col = -1; /* column to check for Cap word */
3239 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003240 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003242 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int multi_attr = 0; /* attributes desired by multibyte */
3244 int mb_l = 1; /* multi-byte byte length */
3245 int mb_c = 0; /* decoded multi-byte character */
3246 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003247 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003248#if defined(FEAT_DIFF) || defined(FEAT_SIGNS)
Bram Moolenaarbf8c3ad2019-06-19 14:28:43 +02003249 int filler_lines = 0; /* nr of filler lines to be drawn */
3250 int filler_todo = 0; /* nr of filler lines still to do + 1 */
Bram Moolenaare9726e32019-06-19 18:01:21 +02003251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003253 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 int change_start = MAXCOL; /* first col of changed area */
3255 int change_end = -1; /* last col of changed area */
3256#endif
3257 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3258#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003259 int need_showbreak = FALSE; /* overlong line, skipping first x
3260 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003262#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003263 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003265 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266#endif
Bram Moolenaarb4d9b892019-07-04 22:59:06 +02003267#ifdef FEAT_SIGNS
3268 int sign_present = FALSE;
3269 sign_attrs_T sattr;
3270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#ifdef FEAT_ARABIC
3272 int prev_c = 0; /* previous Arabic character */
3273 int prev_c1 = 0; /* first composing char for prev_c */
3274#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003275#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003276 int did_line_attr = 0;
3277#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003278#ifdef FEAT_TERMINAL
3279 int get_term_attr = FALSE;
3280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 /* draw_state: items that are drawn in sequence: */
3283#define WL_START 0 /* nothing done yet */
3284#ifdef FEAT_CMDWIN
3285# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3286#else
3287# define WL_CMDLINE WL_START
3288#endif
3289#ifdef FEAT_FOLDING
3290# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3291#else
3292# define WL_FOLD WL_CMDLINE
3293#endif
3294#ifdef FEAT_SIGNS
3295# define WL_SIGN WL_FOLD + 1 /* column for signs */
3296#else
3297# define WL_SIGN WL_FOLD /* column for signs */
3298#endif
3299#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003300#ifdef FEAT_LINEBREAK
3301# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003303# define WL_BRI WL_NR
3304#endif
3305#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3306# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3307#else
3308# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
3310#define WL_LINE WL_SBR + 1 /* text in the line */
3311 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003312#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 int feedback_col = 0;
3314 int feedback_old_attr = -1;
3315#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003316 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
Bram Moolenaar860cae12010-06-05 23:22:07 +02003318#ifdef FEAT_CONCEAL
3319 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003320 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003321 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003322 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003323 int is_concealing = FALSE;
3324 int boguscols = 0; /* nonexistent columns added to force
3325 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003326 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003327 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003328 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003329 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003330# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003331# define FIX_FOR_BOGUSCOLS \
3332 { \
3333 n_extra += vcol_off; \
3334 vcol -= vcol_off; \
3335 vcol_off = 0; \
3336 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003337 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003338 boguscols = 0; \
3339 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003340#else
3341# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
3344 if (startrow > endrow) /* past the end already! */
3345 return startrow;
3346
3347 row = startrow;
3348 screen_row = row + W_WINROW(wp);
3349
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003350 if (!number_only)
3351 {
3352 /*
3353 * To speed up the loop below, set extra_check when there is linebreak,
3354 * trailing white space and/or syntax processing to be done.
3355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
3359#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003360 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003361# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003362 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003363# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003364 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003366 /* Prepare for syntax highlighting in this line. When there is an
3367 * error, stop syntax highlighting. */
3368 save_did_emsg = did_emsg;
3369 did_emsg = FALSE;
3370 syntax_start(wp, lnum);
3371 if (did_emsg)
3372 wp->w_s->b_syn_error = TRUE;
3373 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003374 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003375 did_emsg = save_did_emsg;
3376#ifdef SYN_TIME_LIMIT
3377 if (!wp->w_s->b_syn_slow)
3378#endif
3379 {
3380 has_syntax = TRUE;
3381 extra_check = TRUE;
3382 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003385
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003386 /* Check for columns to display for 'colorcolumn'. */
3387 color_cols = wp->w_p_cc_cols;
3388 if (color_cols != NULL)
3389 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003390#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003391
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003392#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003393 if (term_show_buffer(wp->w_buffer))
3394 {
3395 extra_check = TRUE;
3396 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003397 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003398 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003399#endif
3400
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003401#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003402 if (wp->w_p_spell
3403 && *wp->w_s->b_p_spl != NUL
3404 && wp->w_s->b_langp.ga_len > 0
3405 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003406 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 /* Prepare for spell checking. */
3408 has_spell = TRUE;
3409 extra_check = TRUE;
3410
Bram Moolenaar7701f302018-10-02 21:20:32 +02003411 /* Get the start of the next line, so that words that wrap to the
3412 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003413 * Trick: skip a few chars for C/shell/Vim comments */
3414 nextline[SPWORDLEN] = NUL;
3415 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3416 {
3417 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3418 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3419 }
3420
Bram Moolenaar7701f302018-10-02 21:20:32 +02003421 /* When a word wrapped from the previous line the start of the
3422 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003423 if (lnum == checked_lnum)
3424 cur_checked_col = checked_col;
3425 checked_lnum = 0;
3426
3427 /* When there was a sentence end in the previous line may require a
3428 * word starting with capital in this line. In line 1 always check
3429 * the first word. */
3430 if (lnum != capcol_lnum)
3431 cap_col = -1;
3432 if (lnum == 1)
3433 cap_col = 0;
3434 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436#endif
3437
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 /*
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003439 * handle Visual active in this window
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003440 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003441 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003445 // Visual is after curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003446 top = &curwin->w_cursor;
3447 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003451 // Visual is before curwin->w_cursor
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 top = &VIsual;
3453 bot = &curwin->w_cursor;
3454 }
3455 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003456 if (VIsual_mode == Ctrl_V)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003457 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003458 // block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 if (lnum_in_visual_area)
3460 {
3461 fromcol = wp->w_old_cursor_fcol;
3462 tocol = wp->w_old_cursor_lcol;
3463 }
3464 }
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003465 else
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003466 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003467 // non-block mode
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003468 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003470 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaarec572ad2019-07-07 14:26:59 +02003472 if (VIsual_mode == 'V') // linewise
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003473 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 else
3475 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003476 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3477 if (gchar_pos(top) == NUL)
3478 tocol = fromcol + 1;
3479 }
3480 }
3481 if (VIsual_mode != 'V' && lnum == bot->lnum)
3482 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003483 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003484 {
3485 fromcol = -10;
3486 tocol = MAXCOL;
3487 }
3488 else if (bot->col == MAXCOL)
3489 tocol = MAXCOL;
3490 else
3491 {
3492 pos = *bot;
3493 if (*p_sel == 'e')
3494 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3495 else
3496 {
3497 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3498 ++tocol;
3499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501 }
3502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003504 /* Check if the character under the cursor should not be inverted */
3505 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003506#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003507 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003508#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003509 )
3510 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003512 /* if inverting in this line set area_highlighting */
3513 if (fromcol >= 0)
3514 {
3515 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003516 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003518 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003519 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003520 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003521 && clip_isautosel_plus()))
3522 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003527 /*
3528 * handle 'incsearch' and ":s///c" highlighting
3529 */
3530 else if (highlight_match
3531 && wp == curwin
3532 && lnum >= curwin->w_cursor.lnum
3533 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003535 if (lnum == curwin->w_cursor.lnum)
3536 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003537 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003538 else
3539 fromcol = 0;
3540 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3541 {
3542 pos.lnum = lnum;
3543 pos.col = search_match_endcol;
3544 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3545 }
3546 else
3547 tocol = MAXCOL;
3548 /* do at least one character; happens when past end of line */
3549 if (fromcol == tocol)
3550 tocol = fromcol + 1;
3551 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003552 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555
3556#ifdef FEAT_DIFF
3557 filler_lines = diff_check(wp, lnum);
3558 if (filler_lines < 0)
3559 {
3560 if (filler_lines == -1)
3561 {
3562 if (diff_find_change(wp, lnum, &change_start, &change_end))
3563 diff_hlf = HLF_ADD; /* added line */
3564 else if (change_start == 0)
3565 diff_hlf = HLF_TXD; /* changed text */
3566 else
3567 diff_hlf = HLF_CHD; /* changed line */
3568 }
3569 else
3570 diff_hlf = HLF_ADD; /* added line */
3571 filler_lines = 0;
3572 area_highlighting = TRUE;
3573 }
3574 if (lnum == wp->w_topline)
3575 filler_lines = wp->w_topfill;
3576 filler_todo = filler_lines;
3577#endif
3578
Bram Moolenaar4e038572019-07-04 18:28:35 +02003579#ifdef FEAT_SIGNS
3580 sign_present = buf_get_signattrs(wp->w_buffer, lnum, &sattr);
3581#endif
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583#ifdef LINE_ATTR
3584# ifdef FEAT_SIGNS
3585 /* If this line has a sign with line highlighting set line_attr. */
Bram Moolenaar4e038572019-07-04 18:28:35 +02003586 if (sign_present)
3587 line_attr = sattr.linehl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003589# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003591 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003592 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593# endif
3594 if (line_attr != 0)
3595 area_highlighting = TRUE;
3596#endif
3597
3598 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3599 ptr = line;
3600
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003601#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003602 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003603 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003604 /* For checking first word with a capital skip white space. */
3605 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003606 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003607
Bram Moolenaar30abd282005-06-22 22:35:10 +00003608 /* To be able to spell-check over line boundaries copy the end of the
3609 * current line into nextline[]. Above the start of the next line was
3610 * copied to nextline[SPWORDLEN]. */
3611 if (nextline[SPWORDLEN] == NUL)
3612 {
3613 /* No next line or it is empty. */
3614 nextlinecol = MAXCOL;
3615 nextline_idx = 0;
3616 }
3617 else
3618 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003619 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003620 if (v < SPWORDLEN)
3621 {
3622 /* Short line, use it completely and append the start of the
3623 * next line. */
3624 nextlinecol = 0;
3625 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003626 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003627 nextline_idx = v + 1;
3628 }
3629 else
3630 {
3631 /* Long line, use only the last SPWORDLEN bytes. */
3632 nextlinecol = v - SPWORDLEN;
3633 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3634 nextline_idx = SPWORDLEN + 1;
3635 }
3636 }
3637 }
3638#endif
3639
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003640 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003642 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003643 extra_check = TRUE;
3644 /* find start of trailing whitespace */
3645 if (lcs_trail)
3646 {
3647 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003648 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003649 --trailcol;
3650 trailcol += (colnr_T) (ptr - line);
3651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003654 wcr_attr = get_wcr_attr(wp);
3655 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003656 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003657 win_attr = wcr_attr;
3658 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003659 }
3660#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02003661 if (WIN_IS_POPUP(wp))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003662 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003663#endif
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 /*
3666 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3667 * first character to be displayed.
3668 */
3669 if (wp->w_p_wrap)
3670 v = wp->w_skipcol;
3671 else
3672 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003673 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 while (vcol < v && *ptr != NUL)
3678 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003679 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003682 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003685 /* When:
3686 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003687 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003688 * - 'virtualedit' is set, or
3689 * - the visual mode is active,
3690 * the end of the line may be before the start of the displayed part.
3691 */
3692 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003693#ifdef FEAT_SYN_HL
3694 wp->w_p_cuc || draw_color_col ||
3695#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003696 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003697 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 /* Handle a character that's not completely on the screen: Put ptr at
3701 * that character but skip the first few screen characters. */
3702 if (vcol > v)
3703 {
3704 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003706 /* If the character fits on the screen, don't need to skip it.
3707 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003708 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003709 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711
3712 /*
3713 * Adjust for when the inverted text is before the screen,
3714 * and when the start of the inverted text is before the screen.
3715 */
3716 if (tocol <= vcol)
3717 fromcol = 0;
3718 else if (fromcol >= 0 && fromcol < vcol)
3719 fromcol = vcol;
3720
3721#ifdef FEAT_LINEBREAK
3722 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3723 if (wp->w_p_wrap)
3724 need_showbreak = TRUE;
3725#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003726#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003727 /* When spell checking a word we need to figure out the start of the
3728 * word and if it's badly spelled or not. */
3729 if (has_spell)
3730 {
3731 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003732 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003733 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003734
3735 pos = wp->w_cursor;
3736 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003737 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003738 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003739
3740 /* spell_move_to() may call ml_get() and make "line" invalid */
3741 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3742 ptr = line + linecol;
3743
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003744 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003745 {
3746 /* no bad word found at line start, don't check until end of a
3747 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003748 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003749 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003750 }
3751 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003752 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003753 /* bad word found, use attributes until end of word */
3754 word_end = wp->w_cursor.col + len + 1;
3755
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003756 /* Turn index into actual attributes. */
3757 if (spell_hlf != HLF_COUNT)
3758 spell_attr = highlight_attr[spell_hlf];
3759 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003760 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003761
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003762# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003763 /* Need to restart syntax highlighting for this line. */
3764 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003765 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003766# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003767 }
3768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770
3771 /*
3772 * Correct highlighting for cursor that can't be disabled.
3773 * Avoids having to check this for each character.
3774 */
3775 if (fromcol >= 0)
3776 {
3777 if (noinvcur)
3778 {
3779 if ((colnr_T)fromcol == wp->w_virtcol)
3780 {
3781 /* highlighting starts at cursor, let it start just after the
3782 * cursor */
3783 fromcol_prev = fromcol;
3784 fromcol = -1;
3785 }
3786 else if ((colnr_T)fromcol < wp->w_virtcol)
3787 /* restart highlighting after the cursor */
3788 fromcol_prev = wp->w_virtcol;
3789 }
3790 if (fromcol >= tocol)
3791 fromcol = -1;
3792 }
3793
3794#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003795 if (!number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003797 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02003798 area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
3799 &line, &search_hl, &search_attr);
3800 ptr = line + v; // "line" may have been updated
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802#endif
3803
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003804#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003805 // Cursor line highlighting for 'cursorline' in the current window.
3806 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003807 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003808 // Do not show the cursor line when Visual mode is active, because it's
3809 // not clear what is selected then. Do update w_last_cursorline.
3810 if (!(wp == curwin && VIsual_active))
3811 {
3812 line_attr = HL_ATTR(HLF_CUL);
3813 area_highlighting = TRUE;
3814 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003815 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003816 }
3817#endif
3818
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003819#ifdef FEAT_TEXT_PROP
3820 {
3821 char_u *prop_start;
3822
3823 text_prop_count = get_text_props(wp->w_buffer, lnum,
3824 &prop_start, FALSE);
3825 if (text_prop_count > 0)
3826 {
3827 // Make a copy of the properties, so that they are properly
3828 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003829 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003830 if (text_props != NULL)
3831 mch_memmove(text_props, prop_start,
3832 text_prop_count * sizeof(textprop_T));
3833
3834 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003835 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003836 area_highlighting = TRUE;
3837 extra_check = TRUE;
3838 }
3839 }
3840#endif
3841
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003842 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845#ifdef FEAT_RIGHTLEFT
3846 if (wp->w_p_rl)
3847 {
3848 /* Rightleft window: process the text in the normal direction, but put
3849 * it in current_ScreenLine[] from right to left. Start at the
3850 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003851 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003853 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855#endif
3856
3857 /*
3858 * Repeat for the whole displayed line.
3859 */
3860 for (;;)
3861 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003862#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003863 int has_match_conc = 0; // match wants to conceal
3864 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 /* Skip this quickly when working on the text. */
3867 if (draw_state != WL_LINE)
3868 {
3869#ifdef FEAT_CMDWIN
3870 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3871 {
3872 draw_state = WL_CMDLINE;
3873 if (cmdwin_type != 0 && wp == curwin)
3874 {
3875 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003877 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003878 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003879 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881 }
3882#endif
3883
3884#ifdef FEAT_FOLDING
3885 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3886 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003887 int fdc = compute_foldcolumn(wp, 0);
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003890 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003892 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003893 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003894 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003895 p_extra_free = alloc(12 + 1);
3896
3897 if (p_extra_free != NULL)
3898 {
3899 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3900 n_extra = fdc;
3901 p_extra_free[n_extra] = NUL;
3902 p_extra = p_extra_free;
3903 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003904 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003905 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908 }
3909#endif
3910
3911#ifdef FEAT_SIGNS
3912 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3913 {
3914 draw_state = WL_SIGN;
3915 /* Show the sign column when there are any signs in this
3916 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003917 if (signcolumn_on(wp))
Bram Moolenaar4e038572019-07-04 18:28:35 +02003918 get_sign_display_info(FALSE, wp, lnum, &sattr, wcr_attr,
3919 row, startrow, filler_lines, filler_todo, &c_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003920 &c_final, extra, &p_extra, &n_extra, &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922#endif
3923
3924 if (draw_state == WL_NR - 1 && n_extra == 0)
3925 {
3926 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003927 /* Display the absolute or relative line number. After the
3928 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3929 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 && (row == startrow
3931#ifdef FEAT_DIFF
3932 + filler_lines
3933#endif
3934 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3935 {
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003936#ifdef FEAT_SIGNS
3937 // If 'signcolumn' is set to 'number' and a sign is present
3938 // in 'lnum', then display the sign instead of the line
3939 // number.
3940 if ((*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')
Bram Moolenaar4e038572019-07-04 18:28:35 +02003941 && sign_present)
3942 get_sign_display_info(TRUE, wp, lnum, &sattr, wcr_attr,
3943 row, startrow, filler_lines, filler_todo,
3944 &c_extra, &c_final, extra, &p_extra, &n_extra,
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003945 &char_attr);
3946 else
3947#endif
3948 {
3949 /* Draw the line number (empty space after wrapping). */
3950 if (row == startrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951#ifdef FEAT_DIFF
3952 + filler_lines
3953#endif
3954 )
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003955 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003956 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003957 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003958
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003959 if (wp->w_p_nu && !wp->w_p_rnu)
3960 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003961 num = (long)lnum;
3962 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003963 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003964 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003965 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003966 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003967 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003968 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003969 num = lnum;
3970 fmt = "%-*ld ";
3971 }
3972 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003973
Bram Moolenaar700e7342013-01-30 12:31:36 +01003974 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003975 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (wp->w_skipcol > 0)
3977 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3978 *p_extra = '-';
3979#ifdef FEAT_RIGHTLEFT
3980 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003981 {
3982 char_u *p1, *p2;
3983 int t;
3984
3985 // like rl_mirror(), but keep the space at the end
3986 p2 = skiptowhite(extra) - 1;
3987 for (p1 = extra; p1 < p2; ++p1, --p2)
3988 {
3989 t = *p1;
3990 *p1 = *p2;
3991 *p2 = t;
3992 }
3993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994#endif
3995 p_extra = extra;
3996 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003997 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02003998 }
3999 else
4000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004002 c_final = NUL;
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004003 }
4004 n_extra = number_width(wp) + 1;
4005 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004006#ifdef FEAT_SYN_HL
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004007 /* When 'cursorline' is set highlight the line number of
4008 * the current line differently.
4009 * TODO: Can we use CursorLine instead of CursorLineNr
4010 * when CursorLineNr isn't set? */
4011 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004012 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004013 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004014#endif
Bram Moolenaar394c5d82019-06-17 21:48:05 +02004015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 }
4018
Bram Moolenaar597a4222014-06-25 14:39:50 +02004019#ifdef FEAT_LINEBREAK
4020 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4021 && n_extra == 0 && *p_sbr != NUL)
4022 /* draw indent after showbreak value */
4023 draw_state = WL_BRI;
4024 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4025 /* After the showbreak, draw the breakindent */
4026 draw_state = WL_BRI - 1;
4027
4028 /* draw 'breakindent': indent wrapped text accordingly */
4029 if (draw_state == WL_BRI - 1 && n_extra == 0)
4030 {
4031 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004032 /* if need_showbreak is set, breakindent also applies */
4033 if (wp->w_p_bri && n_extra == 0
4034 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004035# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004036 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004037# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004038 )
4039 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004040 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004041# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004042 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004043 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004044 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004045# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004046 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4047 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004048 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004049# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004050 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004051# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004052 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004053 c_extra = ' ';
4054 n_extra = get_breakindent_win(wp,
4055 ml_get_buf(wp->w_buffer, lnum, FALSE));
4056 /* Correct end of highlighted area for 'breakindent',
4057 * required when 'linebreak' is also set. */
4058 if (tocol == vcol)
4059 tocol += n_extra;
4060 }
4061 }
4062#endif
4063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4065 if (draw_state == WL_SBR - 1 && n_extra == 0)
4066 {
4067 draw_state = WL_SBR;
4068# ifdef FEAT_DIFF
4069 if (filler_todo > 0)
4070 {
4071 /* Draw "deleted" diff line(s). */
4072 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004075 c_final = NUL;
4076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004080 c_final = NUL;
4081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082# ifdef FEAT_RIGHTLEFT
4083 if (wp->w_p_rl)
4084 n_extra = col + 1;
4085 else
4086# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004087 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004088 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090# endif
4091# ifdef FEAT_LINEBREAK
4092 if (*p_sbr != NUL && need_showbreak)
4093 {
4094 /* Draw 'showbreak' at the start of each broken line. */
4095 p_extra = p_sbr;
4096 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004097 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004099 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004101 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /* Correct end of highlighted area for 'showbreak',
4103 * required when 'linebreak' is also set. */
4104 if (tocol == vcol)
4105 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004106#ifdef FEAT_SYN_HL
4107 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004108 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004109 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004110 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113# endif
4114 }
4115#endif
4116
4117 if (draw_state == WL_LINE - 1 && n_extra == 0)
4118 {
4119 draw_state = WL_LINE;
4120 if (saved_n_extra)
4121 {
4122 /* Continue item from end of wrapped line. */
4123 n_extra = saved_n_extra;
4124 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004125 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 p_extra = saved_p_extra;
4127 char_attr = saved_char_attr;
4128 }
4129 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004130 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132 }
4133
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004134 // When still displaying '$' of change command, stop at cursor.
4135 // When only displaying the (relative) line number and that's done,
4136 // stop here.
4137 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004138 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#ifdef FEAT_DIFF
4140 && filler_todo <= 0
4141#endif
4142 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004143 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004145 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004146 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004147 /* Pretend we have finished updating the window. Except when
4148 * 'cursorcolumn' is set. */
4149#ifdef FEAT_SYN_HL
4150 if (wp->w_p_cuc)
4151 row = wp->w_cline_row + wp->w_cline_height;
4152 else
4153#endif
4154 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 break;
4156 }
4157
Bram Moolenaar637532b2019-01-03 21:44:40 +01004158 if (draw_state == WL_LINE && (area_highlighting
4159#ifdef FEAT_SPELL
4160 || has_spell
4161#endif
4162 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
4164 /* handle Visual or match highlighting in this line */
4165 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4167 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004169 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004171 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 else if (area_attr != 0
4173 && (vcol == tocol
4174 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177#ifdef FEAT_SEARCH_EXTRA
4178 if (!n_extra)
4179 {
4180 /*
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004181 * Check for start/end of 'hlsearch' and other matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 * After end, check for start/end of next match.
4183 * When another match, have to check for start again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004185 v = (long)(ptr - line);
Bram Moolenaarbbca7732019-07-24 18:13:16 +02004186 search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line,
4187 &search_hl, &has_match_conc, &match_conc,
4188 did_line_attr, lcs_eol_one);
4189 ptr = line + v; // "line" may have been changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 }
4191#endif
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004194 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004196 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4197 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004199 if (diff_hlf == HLF_TXD && ptr - line > change_end
4200 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004202 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004203 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004204 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
4206#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004207
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004208#ifdef FEAT_TEXT_PROP
4209 if (text_props != NULL)
4210 {
4211 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004212 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004213
Bram Moolenaara956bf62019-06-19 17:34:24 +02004214 if (n_extra > 0)
4215 --bcol; // still working on the previous char, e.g. Tab
4216
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004217 // Check if any active property ends.
4218 for (pi = 0; pi < text_props_active; ++pi)
4219 {
4220 int tpi = text_prop_idxs[pi];
4221
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004222 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004223 + text_props[tpi].tp_len)
4224 {
4225 if (pi + 1 < text_props_active)
4226 mch_memmove(text_prop_idxs + pi,
4227 text_prop_idxs + pi + 1,
4228 sizeof(int)
4229 * (text_props_active - (pi + 1)));
4230 --text_props_active;
4231 --pi;
4232 }
4233 }
4234
4235 // Add any text property that starts in this column.
4236 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004237 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004238 text_prop_idxs[text_props_active++] = text_prop_next++;
4239
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004240 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004241 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004242 if (text_props_active > 0)
4243 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004244 // Sort the properties on priority and/or starting last.
4245 // Then combine the attributes, highest priority last.
4246 current_text_props = text_props;
4247 current_buf = wp->w_buffer;
4248 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4249 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004250
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004251 for (pi = 0; pi < text_props_active; ++pi)
4252 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004253 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004254 proptype_T *pt = text_prop_type_by_id(
4255 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004256
Bram Moolenaard74af422019-06-28 21:38:00 +02004257 if (pt != NULL && pt->pt_hl_id > 0)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004258 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004259 int pt_attr = syn_id2attr(pt->pt_hl_id);
4260
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004261 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004262 text_prop_attr =
4263 hl_combine_attr(text_prop_attr, pt_attr);
4264 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004265 }
4266 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004267 }
4268 }
4269#endif
4270
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004271 /* Decide which of the highlight attributes to use. */
4272 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004273#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004274 if (area_attr != 0)
4275 char_attr = hl_combine_attr(line_attr, area_attr);
4276 else if (search_attr != 0)
4277 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004278# ifdef FEAT_TEXT_PROP
4279 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004280 {
4281 char_attr = hl_combine_attr(
4282 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4283 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004284# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004285 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004286 || vcol < fromcol || vcol_prev < fromcol_prev
4287 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004288 // Use line_attr when not in the Visual or 'incsearch' area
4289 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004290 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004291#else
4292 if (area_attr != 0)
4293 char_attr = area_attr;
4294 else if (search_attr != 0)
4295 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004296#endif
4297 else
4298 {
4299 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004300#ifdef FEAT_TEXT_PROP
4301 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004302 {
4303 if (text_prop_combine)
4304 char_attr = hl_combine_attr(
4305 syntax_attr, text_prop_attr);
4306 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004307 char_attr = hl_combine_attr(
4308 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004309 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004310 else
4311#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004312#ifdef FEAT_SYN_HL
4313 if (has_syntax)
4314 char_attr = syntax_attr;
4315 else
4316#endif
4317 char_attr = 0;
4318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004320 if (char_attr == 0)
4321 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the next character to put on the screen.
4325 */
4326 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004327 * The "p_extra" points to the extra stuff that is inserted to
4328 * represent special characters (non-printable stuff) and other
4329 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004330 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004331 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4332 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4334 */
4335 if (n_extra > 0)
4336 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004337 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004339 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004341 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 {
4343 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004344 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004345 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 else
4348 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 else
4351 {
4352 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (has_mbyte)
4354 {
4355 mb_c = c;
4356 if (enc_utf8)
4357 {
4358 /* If the UTF-8 character is more than one byte:
4359 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004360 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 mb_utf8 = FALSE;
4362 if (mb_l > n_extra)
4363 mb_l = 1;
4364 else if (mb_l > 1)
4365 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004366 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004368 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
4371 else
4372 {
4373 /* if this is a DBCS character, put it in "mb_c" */
4374 mb_l = MB_BYTE2LEN(c);
4375 if (mb_l >= n_extra)
4376 mb_l = 1;
4377 else if (mb_l > 1)
4378 mb_c = (c << 8) + p_extra[1];
4379 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004380 if (mb_l == 0) /* at the NUL at end-of-line */
4381 mb_l = 1;
4382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /* If a double-width char doesn't fit display a '>' in the
4384 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004385 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386# ifdef FEAT_RIGHTLEFT
4387 wp->w_p_rl ? (col <= 0) :
4388# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004389 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 && (*mb_char2cells)(mb_c) == 2)
4391 {
4392 c = '>';
4393 mb_c = c;
4394 mb_l = 1;
4395 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004396 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 /* put the pointer back to output the double-width
4398 * character at the start of the next line. */
4399 ++n_extra;
4400 --p_extra;
4401 }
4402 else
4403 {
4404 n_extra -= mb_l - 1;
4405 p_extra += mb_l - 1;
4406 }
4407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 ++p_extra;
4409 }
4410 --n_extra;
4411 }
4412 else
4413 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004414#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004415 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004416#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004417
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004418 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004419 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 /*
4421 * Get a character from the line itself.
4422 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004423 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004424#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004425 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 if (has_mbyte)
4428 {
4429 mb_c = c;
4430 if (enc_utf8)
4431 {
4432 /* If the UTF-8 character is more than one byte: Decode it
4433 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004434 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 mb_utf8 = FALSE;
4436 if (mb_l > 1)
4437 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004438 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /* Overlong encoded ASCII or ASCII with composing char
4440 * is displayed normally, except a NUL. */
4441 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004442 {
4443 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004444#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004445 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004446#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004449
4450 /* At start of the line we can have a composing char.
4451 * Draw it as a space with a composing char. */
4452 if (utf_iscomposing(mb_c))
4453 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004454 int i;
4455
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004456 for (i = Screen_mco - 1; i > 0; --i)
4457 u8cc[i] = u8cc[i - 1];
4458 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004459 mb_c = ' ';
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462
4463 if ((mb_l == 1 && c >= 0x80)
4464 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004465 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
4467 /*
4468 * Illegal UTF-8 byte: display as <xx>.
4469 * Non-BMP character : display as ? or fullwidth ?.
4470 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004471 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004472# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004473 if (wp->w_p_rl) /* reverse */
4474 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 p_extra = extra;
4477 c = *p_extra;
4478 mb_c = mb_ptr2char_adv(&p_extra);
4479 mb_utf8 = (c >= 0x80);
4480 n_extra = (int)STRLEN(p_extra);
4481 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004482 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (area_attr == 0 && search_attr == 0)
4484 {
4485 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 saved_attr2 = char_attr; /* save current attr */
4488 }
4489 }
4490 else if (mb_l == 0) /* at the NUL at end-of-line */
4491 mb_l = 1;
4492#ifdef FEAT_ARABIC
4493 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4494 {
4495 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004496 int pc, pc1, nc;
4497 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 /* The idea of what is the previous and next
4500 * character depends on 'rightleft'. */
4501 if (wp->w_p_rl)
4502 {
4503 pc = prev_c;
4504 pc1 = prev_c1;
4505 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004506 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 else
4509 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004510 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004512 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 prev_c = mb_c;
4515
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004516 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518 else
4519 prev_c = mb_c;
4520#endif
4521 }
4522 else /* enc_dbcs */
4523 {
4524 mb_l = MB_BYTE2LEN(c);
4525 if (mb_l == 0) /* at the NUL at end-of-line */
4526 mb_l = 1;
4527 else if (mb_l > 1)
4528 {
4529 /* We assume a second byte below 32 is illegal.
4530 * Hopefully this is OK for all double-byte encodings!
4531 */
4532 if (ptr[1] >= 32)
4533 mb_c = (c << 8) + ptr[1];
4534 else
4535 {
4536 if (ptr[1] == NUL)
4537 {
4538 /* head byte at end of line */
4539 mb_l = 1;
4540 transchar_nonprint(extra, c);
4541 }
4542 else
4543 {
4544 /* illegal tail byte */
4545 mb_l = 2;
4546 STRCPY(extra, "XX");
4547 }
4548 p_extra = extra;
4549 n_extra = (int)STRLEN(extra) - 1;
4550 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004551 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 c = *p_extra++;
4553 if (area_attr == 0 && search_attr == 0)
4554 {
4555 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004556 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 saved_attr2 = char_attr; /* save current attr */
4558 }
4559 mb_c = c;
4560 }
4561 }
4562 }
4563 /* If a double-width char doesn't fit display a '>' in the
4564 * last column; the character is displayed at the start of the
4565 * next line. */
4566 if ((
4567# ifdef FEAT_RIGHTLEFT
4568 wp->w_p_rl ? (col <= 0) :
4569# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004570 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 && (*mb_char2cells)(mb_c) == 2)
4572 {
4573 c = '>';
4574 mb_c = c;
4575 mb_utf8 = FALSE;
4576 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004577 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004578 // Put pointer back so that the character will be
4579 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004581#ifdef FEAT_CONCEAL
4582 did_decrement_ptr = TRUE;
4583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 else if (*ptr != NUL)
4586 ptr += mb_l - 1;
4587
4588 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004589 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004590 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004591 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004594 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004595 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 c = ' ';
4597 if (area_attr == 0 && search_attr == 0)
4598 {
4599 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004600 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 saved_attr2 = char_attr; /* save current attr */
4602 }
4603 mb_c = c;
4604 mb_utf8 = FALSE;
4605 mb_l = 1;
4606 }
4607
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 ++ptr;
4610
4611 if (extra_check)
4612 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004613#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004614 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004615#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004616
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004617#ifdef FEAT_TERMINAL
4618 if (get_term_attr)
4619 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004620 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004621
4622 if (!attr_pri)
4623 char_attr = syntax_attr;
4624 else
4625 char_attr = hl_combine_attr(syntax_attr, char_attr);
4626 }
4627#endif
4628
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004629#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004630 // Get syntax attribute, unless still at the start of the line
4631 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004632 v = (long)(ptr - line);
4633 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 /* Get the syntax attribute for the character. If there
4636 * is an error, disable syntax highlighting. */
4637 save_did_emsg = did_emsg;
4638 did_emsg = FALSE;
4639
Bram Moolenaar217ad922005-03-20 22:37:15 +00004640 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004641# ifdef FEAT_SPELL
4642 has_spell ? &can_spell :
4643# endif
4644 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
4646 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004647 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004648 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004649 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004650 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 else
4653 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004654
4655 // combine syntax attribute with 'wincolor'
4656 if (win_attr != 0)
4657 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4658
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004659#ifdef SYN_TIME_LIMIT
4660 if (wp->w_s->b_syn_slow)
4661 has_syntax = FALSE;
4662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 /* Need to get the line again, a multi-line regexp may
4665 * have made it invalid. */
4666 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4667 ptr = line + v;
4668
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004669# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004670 // Text properties overrule syntax highlighting or combine.
4671 if (text_prop_attr == 0 || text_prop_combine)
4672# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004673 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004674 int comb_attr = syntax_attr;
4675# ifdef FEAT_TEXT_PROP
4676 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4677# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004678 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004679 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004680 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004681 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004682 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004683# ifdef FEAT_CONCEAL
4684 /* no concealing past the end of the line, it interferes
4685 * with line highlighting */
4686 if (c == NUL)
4687 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004688 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004689 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004692#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004693
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004694#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004695 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004696 * Only do this when there is no syntax highlighting, the
4697 * @Spell cluster is not used or the current syntax item
4698 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004699 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004700 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004701 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004702 if (c != 0 && (
4703# ifdef FEAT_SYN_HL
4704 !has_syntax ||
4705# endif
4706 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004707 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004708 char_u *prev_ptr, *p;
4709 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004710 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004711 if (has_mbyte)
4712 {
4713 prev_ptr = ptr - mb_l;
4714 v -= mb_l - 1;
4715 }
4716 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004717 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004718
4719 /* Use nextline[] if possible, it has the start of the
4720 * next line concatenated. */
4721 if ((prev_ptr - line) - nextlinecol >= 0)
4722 p = nextline + (prev_ptr - line) - nextlinecol;
4723 else
4724 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004725 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004726 len = spell_check(wp, p, &spell_hlf, &cap_col,
4727 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004728 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004729
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004730 /* In Insert mode only highlight a word that
4731 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004732 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004733 && (State & INSERT) != 0
4734 && wp->w_cursor.lnum == lnum
4735 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004736 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004737 && wp->w_cursor.col < (colnr_T)word_end)
4738 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004739 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004740 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004741 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004742
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004743 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004744 && (p - nextline) + len > nextline_idx)
4745 {
4746 /* Remember that the good word continues at the
4747 * start of the next line. */
4748 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004749 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004750 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004751
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004752 /* Turn index into actual attributes. */
4753 if (spell_hlf != HLF_COUNT)
4754 spell_attr = highlight_attr[spell_hlf];
4755
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004756 if (cap_col > 0)
4757 {
4758 if (p != prev_ptr
4759 && (p - nextline) + cap_col >= nextline_idx)
4760 {
4761 /* Remember that the word in the next line
4762 * must start with a capital. */
4763 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004764 cap_col = (int)((p - nextline) + cap_col
4765 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004766 }
4767 else
4768 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004769 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004770 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004771 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004772 }
4773 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004774 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004775 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004776 char_attr = hl_combine_attr(char_attr, spell_attr);
4777 else
4778 char_attr = hl_combine_attr(spell_attr, char_attr);
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#endif
4781#ifdef FEAT_LINEBREAK
4782 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004783 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004785 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004786 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004788 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004789 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004790
Bram Moolenaar597a4222014-06-25 14:39:50 +02004791 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004792 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004793 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004794 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004795# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004796 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004797 wp->w_buffer->b_p_vts_array) - 1;
4798# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004799 n_extra = (int)wp->w_buffer->b_p_ts
4800 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004801# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004802
Bram Moolenaar4df70292015-03-21 14:20:16 +01004803 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004804 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004805 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004806 {
4807#ifdef FEAT_CONCEAL
4808 if (c == TAB)
4809 /* See "Tab alignment" below. */
4810 FIX_FOR_BOGUSCOLS;
4811#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004812 if (!wp->w_p_list)
4813 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816#endif
4817
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004818 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4819 // But not when the character is followed by a composing
4820 // character (use mb_l to check that).
4821 if (wp->w_p_list
4822 && ((((c == 160 && mb_l == 1)
4823 || (mb_utf8
4824 && ((mb_c == 160 && mb_l == 2)
4825 || (mb_c == 0x202f && mb_l == 3))))
4826 && lcs_nbsp)
4827 || (c == ' '
4828 && mb_l == 1
4829 && lcs_space
4830 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004831 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004832 c = (c == ' ') ? lcs_space : lcs_nbsp;
4833 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004834 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004835 n_attr = 1;
4836 extra_attr = HL_ATTR(HLF_8);
4837 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004838 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004839 mb_c = c;
4840 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004841 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004842 mb_utf8 = TRUE;
4843 u8cc[0] = 0;
4844 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004845 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004846 else
4847 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004848 }
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4851 {
4852 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004853 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
4855 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004856 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 saved_attr2 = char_attr; /* save current attr */
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004860 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
4862 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004863 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004864 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 else
4867 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 }
4870
4871 /*
4872 * Handling of non-printable characters.
4873 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004874 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
4876 /*
4877 * when getting a character from the file, we may have to
4878 * turn it into something else on the way to putting it
4879 * into "ScreenLines".
4880 */
4881 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4882 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004883 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004884 long vcol_adjusted = vcol; /* removed showbreak length */
4885#ifdef FEAT_LINEBREAK
4886 /* only adjust the tab_len, when at the first column
4887 * after the showbreak value was drawn */
4888 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4889 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004892#ifdef FEAT_VARTABS
4893 tab_len = tabstop_padding(vcol_adjusted,
4894 wp->w_buffer->b_p_ts,
4895 wp->w_buffer->b_p_vts_array) - 1;
4896#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004897 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004898 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4899#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004900
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004901#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004902 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004903#endif
4904 /* tab amount depends on current column */
4905 n_extra = tab_len;
4906#ifdef FEAT_LINEBREAK
4907 else
4908 {
4909 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01004910 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004911 int i;
4912 int saved_nextra = n_extra;
4913
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004914#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004915 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004916 /* there are characters to conceal */
4917 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004918 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
4919 */
4920 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
4921 && n_extra > tab_len)
4922 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004923#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004924
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004925 /* if n_extra > 0, it gives the number of chars, to
4926 * use for a tab, else we need to calculate the width
4927 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004928 len = (tab_len * mb_char2len(lcs_tab2));
4929 if (n_extra > 0)
4930 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004931 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02004932 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004933 vim_memset(p, ' ', len);
4934 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004935 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004936 p_extra_free = p;
4937 for (i = 0; i < tab_len; i++)
4938 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004939 if (*p == NUL)
4940 {
4941 tab_len = i;
4942 break;
4943 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004944 mb_char2bytes(lcs_tab2, p);
4945 p += mb_char2len(lcs_tab2);
4946 n_extra += mb_char2len(lcs_tab2)
4947 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004948 }
4949 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004950#ifdef FEAT_CONCEAL
4951 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
4952 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004953 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02004954 n_extra -= vcol_off;
4955#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004956 }
4957#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004958#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004959 {
4960 int vc_saved = vcol_off;
4961
4962 /* Tab alignment should be identical regardless of
4963 * 'conceallevel' value. So tab compensates of all
4964 * previous concealed characters, and thus resets
4965 * vcol_off and boguscols accumulated so far in the
4966 * line. Note that the tab can be longer than
4967 * 'tabstop' when there are concealed characters. */
4968 FIX_FOR_BOGUSCOLS;
4969
4970 /* Make sure, the highlighting for the tab char will be
4971 * correctly set further below (effectively reverts the
4972 * FIX_FOR_BOGSUCOLS macro */
4973 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01004974 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01004975 tab_len += vc_saved;
4976 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01004977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 if (wp->w_p_list)
4980 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004981 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004982#ifdef FEAT_LINEBREAK
4983 if (wp->w_p_lbr)
4984 c_extra = NUL; /* using p_extra from above */
4985 else
4986#endif
4987 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004988 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004989 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004990 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004993 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
4995 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004996 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004997 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
5000 else
5001 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005002 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 c_extra = ' ';
5004 c = ' ';
5005 }
5006 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005007 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005008 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005009 || ((fromcol >= 0 || fromcol_prev >= 0)
5010 && tocol > vcol
5011 && VIsual_mode != Ctrl_V
5012 && (
5013# ifdef FEAT_RIGHTLEFT
5014 wp->w_p_rl ? (col >= 0) :
5015# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005016 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005017 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005018 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005019 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005020 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005022 /* Display a '$' after the line or highlight an extra
5023 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5025 /* For a diff line the highlighting continues after the
5026 * "$". */
5027 if (
5028# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005029 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030# ifdef LINE_ATTR
5031 &&
5032# endif
5033# endif
5034# ifdef LINE_ATTR
5035 line_attr == 0
5036# endif
5037 )
5038#endif
5039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /* In virtualedit, visual selections may extend
5041 * beyond end of line. */
5042 if (area_highlighting && virtual_active()
5043 && tocol != MAXCOL && vcol < tocol)
5044 n_extra = 0;
5045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 p_extra = at_end_str;
5048 n_extra = 1;
5049 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005050 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005053 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005054 c = lcs_eol;
5055 else
5056 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 lcs_eol_one = -1;
5058 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005059 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005061 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 n_attr = 1;
5063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005065 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005068 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005069 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 else
5072 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 else if (c != NUL)
5075 {
5076 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005077 if (n_extra == 0)
5078 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#ifdef FEAT_RIGHTLEFT
5080 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5081 rl_mirror(p_extra); /* reverse "<12>" */
5082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005084 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005085#ifdef FEAT_LINEBREAK
5086 if (wp->w_p_lbr)
5087 {
5088 char_u *p;
5089
5090 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005091 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005092 vim_memset(p, ' ', n_extra);
5093 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5094 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005095 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005096 p_extra_free = p_extra = p;
5097 }
5098 else
5099#endif
5100 {
5101 n_extra = byte2cells(c) - 1;
5102 c = *p_extra++;
5103 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005104 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005107 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 saved_attr2 = char_attr; /* save current attr */
5109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 else if (VIsual_active
5113 && (VIsual_mode == Ctrl_V
5114 || VIsual_mode == 'v')
5115 && virtual_active()
5116 && tocol != MAXCOL
5117 && vcol < tocol
5118 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005119#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005121#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005122 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 c = ' ';
5125 --ptr; /* put it back at the NUL */
5126 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005127#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else if ((
5129# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005130 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005132# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005133 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 ) && (
5137# ifdef FEAT_RIGHTLEFT
5138 wp->w_p_rl ? (col >= 0) :
5139# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005140 (col
5141# ifdef FEAT_CONCEAL
5142 - boguscols
5143# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005144 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 /* Highlight until the right side of the window */
5147 c = ' ';
5148 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005149
5150 /* Remember we do the char for line highlighting. */
5151 ++did_line_attr;
5152
5153 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005154 if (line_attr != 0 && char_attr == search_attr
5155 && (did_line_attr > 1
5156 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005157 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158# ifdef FEAT_DIFF
5159 if (diff_hlf == HLF_TXD)
5160 {
5161 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005162 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005163 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005164 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005165 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5166 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005167 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005171# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005172 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005173 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005174 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005175 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5176 char_attr = hl_combine_attr(char_attr,
5177 HL_ATTR(HLF_CUL));
Bram Moolenaarcb5ff342019-07-20 16:51:19 +02005178 else if (line_attr)
5179 char_attr = hl_combine_attr(char_attr, line_attr);
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005180 }
5181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183#endif
5184 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005185
5186#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005187 if ( wp->w_p_cole > 0
5188 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005189 conceal_cursor_line(wp))
5190 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005191 && !(lnum_in_visual_area
5192 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005193 {
5194 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005195 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005196 && (syn_get_sub_char() != NUL || match_conc
5197 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005198 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005199 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005200 /* First time at this concealed item: display one
5201 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005202 if (match_conc)
5203 c = match_conc;
5204 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005205 c = syn_get_sub_char();
5206 else if (lcs_conceal != NUL)
5207 c = lcs_conceal;
5208 else
5209 c = ' ';
5210
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005211 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005212
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005213 if (n_extra > 0)
5214 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005215 vcol += n_extra;
5216 if (wp->w_p_wrap && n_extra > 0)
5217 {
5218# ifdef FEAT_RIGHTLEFT
5219 if (wp->w_p_rl)
5220 {
5221 col -= n_extra;
5222 boguscols -= n_extra;
5223 }
5224 else
5225# endif
5226 {
5227 boguscols += n_extra;
5228 col += n_extra;
5229 }
5230 }
5231 n_extra = 0;
5232 n_attr = 0;
5233 }
5234 else if (n_skip == 0)
5235 {
5236 is_concealing = TRUE;
5237 n_skip = 1;
5238 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005239 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005240 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005241 {
5242 mb_utf8 = TRUE;
5243 u8cc[0] = 0;
5244 c = 0xc0;
5245 }
5246 else
5247 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005248 }
5249 else
5250 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005251 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005252 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005253 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005254
5255 if (n_skip > 0 && did_decrement_ptr)
5256 // not showing the '>', put pointer back to avoid getting stuck
5257 ++ptr;
5258
5259#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005262#ifdef FEAT_CONCEAL
5263 /* In the cursor line and we may be concealing characters: correct
5264 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005265 if (!did_wcol && draw_state == WL_LINE
5266 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005267 && conceal_cursor_line(wp)
5268 && (int)wp->w_virtcol <= vcol + n_skip)
5269 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005270# ifdef FEAT_RIGHTLEFT
5271 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005272 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005273 else
5274# endif
5275 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005276 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005277 did_wcol = TRUE;
5278 }
5279#endif
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /* Don't override visual selection highlighting. */
5282 if (n_attr > 0
5283 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005284 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 char_attr = extra_attr;
5286
Bram Moolenaar81695252004-12-29 20:58:21 +00005287#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 /* XIM don't send preedit_start and preedit_end, but they send
5289 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5290 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005291 if (p_imst == IM_ON_THE_SPOT
5292 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005293 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 && (State & INSERT)
5295 && !p_imdisable
5296 && im_is_preediting()
5297 && draw_state == WL_LINE)
5298 {
5299 colnr_T tcol;
5300
5301 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005302 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
5304 tcol = preedit_end_col;
5305 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5306 {
5307 if (feedback_old_attr < 0)
5308 {
5309 feedback_col = 0;
5310 feedback_old_attr = char_attr;
5311 }
5312 char_attr = im_get_feedback_attr(feedback_col);
5313 if (char_attr < 0)
5314 char_attr = feedback_old_attr;
5315 feedback_col++;
5316 }
5317 else if (feedback_old_attr >= 0)
5318 {
5319 char_attr = feedback_old_attr;
5320 feedback_old_attr = -1;
5321 feedback_col = 0;
5322 }
5323 }
5324#endif
5325 /*
5326 * Handle the case where we are in column 0 but not on the first
5327 * character of the line and the user wants us to show us a
5328 * special character (via 'listchars' option "precedes:<char>".
5329 */
5330 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005331 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5333#ifdef FEAT_DIFF
5334 && filler_todo <= 0
5335#endif
5336 && draw_state > WL_NR
5337 && c != NUL)
5338 {
5339 c = lcs_prec;
5340 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005341 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5342 {
5343 /* Double-width character being overwritten by the "precedes"
5344 * character, need to fill up half the character. */
5345 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005346 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005347 n_extra = 1;
5348 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005349 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005352 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005355 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005356 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 }
5358 else
5359 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005360 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005363 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 n_attr3 = 1;
5365 }
5366 }
5367
5368 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005369 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 */
Bram Moolenaarf914a332019-07-20 15:09:56 +02005371 if ((c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005372#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005373 || did_line_attr == 1
5374#endif
Bram Moolenaarf914a332019-07-20 15:09:56 +02005375 ) && eol_hl_off == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005377#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005378 // flag to indicate whether prevcol equals startcol of search_hl or
5379 // one of the matches
5380 int prevcol_hl_flag = get_prevcol_hl_flag(wp, &search_hl,
5381 (long)(ptr - line) - (c == NUL));
Bram Moolenaar91170f82006-05-05 21:15:17 +00005382#endif
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005383 // Invert at least one char, used for Visual and empty line or
5384 // highlight match at end of line. If it's beyond the last
5385 // char on the screen, just overwrite that one (tricky!) Not
5386 // needed when a '$' was displayed for 'list'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005388 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005389 && (VIsual_mode != Ctrl_V
5390 || lnum == VIsual.lnum
5391 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005392 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005394 // highlight 'hlsearch' match at end of line
5395 || (prevcol_hl_flag
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005396# ifdef FEAT_SYN_HL
5397 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5398 && !(wp == curwin && VIsual_active))
5399# endif
5400# ifdef FEAT_DIFF
5401 && diff_hlf == (hlf_T)0
5402# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005403# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005404 && did_line_attr <= 1
5405# endif
5406 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
5408 ))
5409 {
5410 int n = 0;
5411
5412#ifdef FEAT_RIGHTLEFT
5413 if (wp->w_p_rl)
5414 {
5415 if (col < 0)
5416 n = 1;
5417 }
5418 else
5419#endif
5420 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005421 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 n = -1;
5423 }
5424 if (n != 0)
5425 {
5426 /* At the window boundary, highlight the last character
5427 * instead (better than nothing). */
5428 off += n;
5429 col += n;
5430 }
5431 else
5432 {
5433 /* Add a blank character to highlight. */
5434 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (enc_utf8)
5436 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438#ifdef FEAT_SEARCH_EXTRA
5439 if (area_attr == 0)
5440 {
Bram Moolenaarbbca7732019-07-24 18:13:16 +02005441 // Use attributes from match with highest priority among
5442 // 'search_hl' and the match list.
5443 get_search_match_hl(wp, &search_hl,
5444 (long)(ptr - line), &char_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 }
5446#endif
5447 ScreenAttrs[off] = char_attr;
5448#ifdef FEAT_RIGHTLEFT
5449 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005452 --off;
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 else
5455#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005458 ++off;
5459 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005460 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005461 eol_hl_off = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
Bram Moolenaar91170f82006-05-05 21:15:17 +00005465 /*
5466 * At end of the text line.
5467 */
5468 if (c == NUL)
5469 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005470#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005471 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005472 if (wp->w_p_wrap)
5473 v = wp->w_skipcol;
5474 else
5475 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005476
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005477 /* check if line ends before left margin */
5478 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005479 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005480#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005481 // Get rid of the boguscols now, we want to draw until the right
5482 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005483 col -= boguscols;
5484 boguscols = 0;
5485#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005486
5487 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005488 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005489
5490 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005491 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5492 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005493 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005494 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005495 || draw_color_col
5496 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005497# ifdef FEAT_RIGHTLEFT
5498 && !wp->w_p_rl
5499# endif
5500 )
5501 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005502 int rightmost_vcol = 0;
5503 int i;
5504
5505 if (wp->w_p_cuc)
5506 rightmost_vcol = wp->w_virtcol;
5507 if (draw_color_col)
5508 /* determine rightmost colorcolumn to possibly draw */
5509 for (i = 0; color_cols[i] >= 0; ++i)
5510 if (rightmost_vcol < color_cols[i])
5511 rightmost_vcol = color_cols[i];
5512
Bram Moolenaar02631462017-09-22 15:20:32 +02005513 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005514 {
5515 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005516 if (enc_utf8)
5517 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005518 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005519 if (draw_color_col)
5520 draw_color_col = advance_color_col(VCOL_HLC,
5521 &color_cols);
5522
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005523 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005524 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005525 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005526 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005527 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005528 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005529
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005530 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005531 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005532
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005533 ++vcol;
5534 }
5535 }
5536#endif
5537
Bram Moolenaar53f81742017-09-22 14:35:51 +02005538 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005539 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 row++;
5541
5542 /*
5543 * Update w_cline_height and w_cline_folded if the cursor line was
5544 * updated (saves a call to plines() later).
5545 */
5546 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5547 {
5548 curwin->w_cline_row = startrow;
5549 curwin->w_cline_height = row - startrow;
5550#ifdef FEAT_FOLDING
5551 curwin->w_cline_folded = FALSE;
5552#endif
5553 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5554 }
5555
5556 break;
5557 }
5558
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005559 // Show "extends" character from 'listchars' if beyond the line end and
5560 // 'list' is set.
5561 if (lcs_ext != NUL
5562 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 && !wp->w_p_wrap
5564#ifdef FEAT_DIFF
5565 && filler_todo <= 0
5566#endif
5567 && (
5568#ifdef FEAT_RIGHTLEFT
5569 wp->w_p_rl ? col == 0 :
5570#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005571 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005573 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5575 {
5576 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005577 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005579 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
5581 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005582 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005583 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 else
5586 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005589#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005590 /* advance to the next 'colorcolumn' */
5591 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005592 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005593
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005594 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005595 * highlight the cursor position itself.
5596 * Also highlight the 'colorcolumn' if it is different than
5597 * 'cursorcolumn' */
5598 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005599 if (draw_state == WL_LINE && !lnum_in_visual_area
5600 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005601 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005602 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005603 && lnum != wp->w_cursor.lnum)
5604 {
5605 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005606 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005607 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005608 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005609 {
5610 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005611 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005612 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005613 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005614#endif
5615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 /*
5617 * Store character to be displayed.
5618 * Skip characters that are left of the screen for 'nowrap'.
5619 */
5620 vcol_prev = vcol;
5621 if (draw_state < WL_LINE || n_skip <= 0)
5622 {
5623 /*
5624 * Store the character.
5625 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005626#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5628 {
5629 /* A double-wide character is: put first halve in left cell. */
5630 --off;
5631 --col;
5632 }
5633#endif
5634 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005636 {
5637 if ((mb_c & 0xff00) == 0x8e00)
5638 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else if (enc_utf8)
5642 {
5643 if (mb_utf8)
5644 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005645 int i;
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005648 if ((c & 0xff) == 0)
5649 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005650 for (i = 0; i < Screen_mco; ++i)
5651 {
5652 ScreenLinesC[i][off] = u8cc[i];
5653 if (u8cc[i] == 0)
5654 break;
5655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 else
5658 ScreenLinesUC[off] = 0;
5659 }
5660 if (multi_attr)
5661 {
5662 ScreenAttrs[off] = multi_attr;
5663 multi_attr = 0;
5664 }
5665 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 ScreenAttrs[off] = char_attr;
5667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5669 {
5670 /* Need to fill two screen columns. */
5671 ++off;
5672 ++col;
5673 if (enc_utf8)
5674 /* UTF-8: Put a 0 in the second screen char. */
5675 ScreenLines[off] = 0;
5676 else
5677 /* DBCS: Put second byte in the second screen char. */
5678 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005679 if (draw_state > WL_NR
5680#ifdef FEAT_DIFF
5681 && filler_todo <= 0
5682#endif
5683 )
5684 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 /* When "tocol" is halfway a character, set it to the end of
5686 * the character, otherwise highlighting won't stop. */
5687 if (tocol == vcol)
5688 ++tocol;
5689#ifdef FEAT_RIGHTLEFT
5690 if (wp->w_p_rl)
5691 {
5692 /* now it's time to backup one cell */
5693 --off;
5694 --col;
5695 }
5696#endif
5697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698#ifdef FEAT_RIGHTLEFT
5699 if (wp->w_p_rl)
5700 {
5701 --off;
5702 --col;
5703 }
5704 else
5705#endif
5706 {
5707 ++off;
5708 ++col;
5709 }
5710 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005711#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005712 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005713 {
5714 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005715 ++vcol_off;
5716 if (n_extra > 0)
5717 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005718 if (wp->w_p_wrap)
5719 {
5720 /*
5721 * Special voodoo required if 'wrap' is on.
5722 *
5723 * Advance the column indicator to force the line
5724 * drawing to wrap early. This will make the line
5725 * take up the same screen space when parts are concealed,
5726 * so that cursor line computations aren't messed up.
5727 *
5728 * To avoid the fictitious advance of 'col' causing
5729 * trailing junk to be written out of the screen line
5730 * we are building, 'boguscols' keeps track of the number
5731 * of bad columns we have advanced.
5732 */
5733 if (n_extra > 0)
5734 {
5735 vcol += n_extra;
5736# ifdef FEAT_RIGHTLEFT
5737 if (wp->w_p_rl)
5738 {
5739 col -= n_extra;
5740 boguscols -= n_extra;
5741 }
5742 else
5743# endif
5744 {
5745 col += n_extra;
5746 boguscols += n_extra;
5747 }
5748 n_extra = 0;
5749 n_attr = 0;
5750 }
5751
5752
Bram Moolenaar860cae12010-06-05 23:22:07 +02005753 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5754 {
5755 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005756# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005757 if (wp->w_p_rl)
5758 {
5759 --boguscols;
5760 --col;
5761 }
5762 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005763# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005764 {
5765 ++boguscols;
5766 ++col;
5767 }
5768 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005769
5770# ifdef FEAT_RIGHTLEFT
5771 if (wp->w_p_rl)
5772 {
5773 --boguscols;
5774 --col;
5775 }
5776 else
5777# endif
5778 {
5779 ++boguscols;
5780 ++col;
5781 }
5782 }
5783 else
5784 {
5785 if (n_extra > 0)
5786 {
5787 vcol += n_extra;
5788 n_extra = 0;
5789 n_attr = 0;
5790 }
5791 }
5792
5793 }
5794#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
5796 --n_skip;
5797
Bram Moolenaar64486672010-05-16 15:46:46 +02005798 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5799 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005800 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801#ifdef FEAT_DIFF
5802 && filler_todo <= 0
5803#endif
5804 )
5805 ++vcol;
5806
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005807#ifdef FEAT_SYN_HL
5808 if (vcol_save_attr >= 0)
5809 char_attr = vcol_save_attr;
5810#endif
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 /* restore attributes after "predeces" in 'listchars' */
5813 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5814 char_attr = saved_attr3;
5815
5816 /* restore attributes after last 'listchars' or 'number' char */
5817 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5818 char_attr = saved_attr2;
5819
5820 /*
5821 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005822 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 */
5824 if ((
5825#ifdef FEAT_RIGHTLEFT
5826 wp->w_p_rl ? (col < 0) :
5827#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005828 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 && (*ptr != NUL
5830#ifdef FEAT_DIFF
5831 || filler_todo > 0
5832#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005833 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5835 )
5836 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005837#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005838 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005839 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005840 boguscols = 0;
5841#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005842 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005843 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02005844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 ++row;
5846 ++screen_row;
5847
5848 /* When not wrapping and finished diff lines, or when displayed
5849 * '$' and highlighting until last column, break here. */
5850 if ((!wp->w_p_wrap
5851#ifdef FEAT_DIFF
5852 && filler_todo <= 0
5853#endif
5854 ) || lcs_eol_one == -1)
5855 break;
5856
5857 /* When the window is too narrow draw all "@" lines. */
5858 if (draw_state != WL_LINE
5859#ifdef FEAT_DIFF
5860 && filler_todo <= 0
5861#endif
5862 )
5863 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01005864 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 row = endrow;
5867 }
5868
5869 /* When line got too long for screen break here. */
5870 if (row == endrow)
5871 {
5872 ++row;
5873 break;
5874 }
5875
5876 if (screen_cur_row == screen_row - 1
5877#ifdef FEAT_DIFF
5878 && filler_todo <= 0
5879#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005880 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
5882 /* Remember that the line wraps, used for modeless copy. */
5883 LineWraps[screen_row - 1] = TRUE;
5884
5885 /*
5886 * Special trick to make copy/paste of wrapped lines work with
5887 * xterm/screen: write an extra character beyond the end of
5888 * the line. This will work with all terminal types
5889 * (regardless of the xn,am settings).
5890 * Only do this on a fast tty.
5891 * Only do this if the cursor is on the current line
5892 * (something has been written in it).
5893 * Don't do this for the GUI.
5894 * Don't do this for double-width characters.
5895 * Don't do this for a window not at the right screen border.
5896 */
5897 if (p_tf
5898#ifdef FEAT_GUI
5899 && !gui.in_use
5900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00005902 && ((*mb_off2cells)(LineOffset[screen_row],
5903 LineOffset[screen_row] + screen_Columns)
5904 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00005906 + (int)Columns - 2,
5907 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01005908 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 /* First make sure we are at the end of the screen line,
5911 * then output the same character again to let the
5912 * terminal know about the wrap. If the terminal doesn't
5913 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02005914 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 screen_char(LineOffset[screen_row - 1]
5916 + (unsigned)Columns - 1,
5917 screen_row - 1, (int)(Columns - 1));
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 /* When there is a multi-byte character, just output a
5920 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00005921 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
5922 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 out_char(' ');
5924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 out_char(ScreenLines[LineOffset[screen_row - 1]
5926 + (Columns - 1)]);
5927 /* force a redraw of the first char on the next line */
5928 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
5929 screen_start(); /* don't know where cursor is now */
5930 }
5931 }
5932
5933 col = 0;
5934 off = (unsigned)(current_ScreenLine - ScreenLines);
5935#ifdef FEAT_RIGHTLEFT
5936 if (wp->w_p_rl)
5937 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005938 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 off += col;
5940 }
5941#endif
5942
5943 /* reset the drawing state for the start of a wrapped line */
5944 draw_state = WL_START;
5945 saved_n_extra = n_extra;
5946 saved_p_extra = p_extra;
5947 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005948 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 saved_char_attr = char_attr;
5950 n_extra = 0;
5951 lcs_prec_todo = lcs_prec;
5952#ifdef FEAT_LINEBREAK
5953# ifdef FEAT_DIFF
5954 if (filler_todo <= 0)
5955# endif
5956 need_showbreak = TRUE;
5957#endif
5958#ifdef FEAT_DIFF
5959 --filler_todo;
5960 /* When the filler lines are actually below the last line of the
5961 * file, don't draw the line itself, break here. */
5962 if (filler_todo == 0 && wp->w_botfill)
5963 break;
5964#endif
5965 }
5966
5967 } /* for every character in the line */
5968
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005969#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005970 /* After an empty line check first word for capital. */
5971 if (*skipwhite(line) == NUL)
5972 {
5973 capcol_lnum = lnum + 1;
5974 cap_col = 0;
5975 }
5976#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01005977#ifdef FEAT_TEXT_PROP
5978 vim_free(text_props);
5979 vim_free(text_prop_idxs);
5980#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005981
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005982 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 return row;
5984}
5985
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005986/*
5987 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01005988 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005989 */
5990 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005991comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005992{
5993 int i;
5994
5995 for (i = 0; i < Screen_mco; ++i)
5996 {
5997 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
5998 return TRUE;
5999 if (ScreenLinesC[i][off_from] == 0)
6000 break;
6001 }
6002 return FALSE;
6003}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006004
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005/*
6006 * Check whether the given character needs redrawing:
6007 * - the (first byte of the) character is different
6008 * - the attributes are different
6009 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006010 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 */
6012 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006013char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 if (cols > 0
6016 && ((ScreenLines[off_from] != ScreenLines[off_to]
6017 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 || (enc_dbcs != 0
6019 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6020 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6021 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6022 : (cols > 1 && ScreenLines[off_from + 1]
6023 != ScreenLines[off_to + 1])))
6024 || (enc_utf8
6025 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6026 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006027 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006028 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6029 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006030 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 return TRUE;
6032 return FALSE;
6033}
6034
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006035#if defined(FEAT_TERMINAL) || defined(PROTO)
6036/*
6037 * Return the index in ScreenLines[] for the current screen line.
6038 */
6039 int
6040screen_get_current_line_off()
6041{
6042 return (int)(current_ScreenLine - ScreenLines);
6043}
6044#endif
6045
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006046#ifdef FEAT_TEXT_PROP
6047/*
6048 * Return TRUE if this position has a higher level popup or this cell is
6049 * transparent in the current popup.
6050 */
6051 static int
6052blocked_by_popup(int row, int col)
6053{
6054 int off;
6055
6056 if (!popup_visible)
6057 return FALSE;
6058 off = row * screen_Columns + col;
6059 return popup_mask[off] > screen_zindex || popup_transparent[off];
6060}
6061#endif
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063/*
6064 * Move one "cooked" screen line to the screen, but only the characters that
6065 * have actually changed. Handle insert/delete character.
6066 * "coloff" gives the first column on the screen for this line.
6067 * "endcol" gives the columns where valid characters are.
6068 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6069 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006070 * "flags" can have bits:
6071 * SLF_POPUP popup window
6072 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6074 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6075 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006076 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006077screen_line(
6078 int row,
6079 int coloff,
6080 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006081 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006082 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 unsigned off_from;
6085 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006086 unsigned max_off_from;
6087 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 int force = FALSE; /* force update rest of the line */
6091 int redraw_this /* bool: does character need redraw? */
6092#ifdef FEAT_GUI
6093 = TRUE /* For GUI when while-loop empty */
6094#endif
6095 ;
6096 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 int clear_next = FALSE;
6098 int char_cells; /* 1: normal char */
6099 /* 2: occupies two display cells */
6100# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006102 /* Check for illegal row and col, just in case. */
6103 if (row >= Rows)
6104 row = Rows - 1;
6105 if (endcol > Columns)
6106 endcol = Columns;
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108# ifdef FEAT_CLIPBOARD
6109 clip_may_clear_selection(row, row);
6110# endif
6111
6112 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6113 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006114 max_off_from = off_from + screen_Columns;
6115 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006118 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120 /* Clear rest first, because it's left of the text. */
6121 if (clear_width > 0)
6122 {
6123 while (col <= endcol && ScreenLines[off_to] == ' '
6124 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006125 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127 ++off_to;
6128 ++col;
6129 }
6130 if (col <= endcol)
6131 screen_fill(row, row + 1, col + coloff,
6132 endcol + coloff + 1, ' ', ' ', 0);
6133 }
6134 col = endcol + 1;
6135 off_to = LineOffset[row] + col + coloff;
6136 off_from += col;
6137 endcol = (clear_width > 0 ? clear_width : -clear_width);
6138 }
6139#endif /* FEAT_RIGHTLEFT */
6140
6141 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6142
6143 while (col < endcol)
6144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006146 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 else
6148 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 redraw_this = redraw_next;
6151 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6152 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6153
6154#ifdef FEAT_GUI
6155 /* If the next character was bold, then redraw the current character to
6156 * remove any pixels that might have spilt over into us. This only
6157 * happens in the GUI.
6158 */
6159 if (redraw_next && gui.in_use)
6160 {
6161 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006162 if (hl > HL_ALL)
6163 hl = syn_attr2attr(hl);
6164 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 redraw_this = TRUE;
6166 }
6167#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02006168#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006169 if (blocked_by_popup(row, col + coloff))
Bram Moolenaar33796b32019-06-08 16:01:13 +02006170 redraw_this = FALSE;
6171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (redraw_this)
6173 {
6174 /*
6175 * Special handling when 'xs' termcap flag set (hpterm):
6176 * Attributes for characters are stored at the position where the
6177 * cursor is when writing the highlighting code. The
6178 * start-highlighting code must be written with the cursor on the
6179 * first highlighted character. The stop-highlighting code must
6180 * be written with the cursor just after the last highlighted
6181 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006182 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 * to clear the rest of the line, and force redrawing it
6184 * completely.
6185 */
6186 if ( p_wiv
6187 && !force
6188#ifdef FEAT_GUI
6189 && !gui.in_use
6190#endif
6191 && ScreenAttrs[off_to] != 0
6192 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6193 {
6194 /*
6195 * Need to remove highlighting attributes here.
6196 */
6197 windgoto(row, col + coloff);
6198 out_str(T_CE); /* clear rest of this screen line */
6199 screen_start(); /* don't know where cursor is now */
6200 force = TRUE; /* force redraw of rest of the line */
6201 redraw_next = TRUE; /* or else next char would miss out */
6202
6203 /*
6204 * If the previous character was highlighted, need to stop
6205 * highlighting at this character.
6206 */
6207 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6208 {
6209 screen_attr = ScreenAttrs[off_to - 1];
6210 term_windgoto(row, col + coloff);
6211 screen_stop_highlight();
6212 }
6213 else
6214 screen_attr = 0; /* highlighting has stopped */
6215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (enc_dbcs != 0)
6217 {
6218 /* Check if overwriting a double-byte with a single-byte or
6219 * the other way around requires another character to be
6220 * redrawn. For UTF-8 this isn't needed, because comparing
6221 * ScreenLinesUC[] is sufficient. */
6222 if (char_cells == 1
6223 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006224 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
6226 /* Writing a single-cell character over a double-cell
6227 * character: need to redraw the next cell. */
6228 ScreenLines[off_to + 1] = 0;
6229 redraw_next = TRUE;
6230 }
6231 else if (char_cells == 2
6232 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006233 && (*mb_off2cells)(off_to, max_off_to) == 1
6234 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
6236 /* Writing the second half of a double-cell character over
6237 * a double-cell character: need to redraw the second
6238 * cell. */
6239 ScreenLines[off_to + 2] = 0;
6240 redraw_next = TRUE;
6241 }
6242
6243 if (enc_dbcs == DBCS_JPNU)
6244 ScreenLines2[off_to] = ScreenLines2[off_from];
6245 }
6246 /* When writing a single-width character over a double-width
6247 * character and at the end of the redrawn text, need to clear out
6248 * the right halve of the old character.
6249 * Also required when writing the right halve of a double-width
6250 * char over the left halve of an existing one. */
6251 if (has_mbyte && col + char_cells == endcol
6252 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006253 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006255 && (*mb_off2cells)(off_to, max_off_to) == 1
6256 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
6259 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (enc_utf8)
6261 {
6262 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6263 if (ScreenLinesUC[off_from] != 0)
6264 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006265 int i;
6266
6267 for (i = 0; i < Screen_mco; ++i)
6268 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
6270 }
6271 if (char_cells == 2)
6272 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006275 /* The bold trick makes a single column of pixels appear in the
6276 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 * character should be redrawn too. This happens for our own GUI
6278 * and for some xterms. */
6279 if (
6280# ifdef FEAT_GUI
6281 gui.in_use
6282# endif
6283# if defined(FEAT_GUI) && defined(UNIX)
6284 ||
6285# endif
6286# ifdef UNIX
6287 term_is_xterm
6288# endif
6289 )
6290 {
6291 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006292 if (hl > HL_ALL)
6293 hl = syn_attr2attr(hl);
6294 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 redraw_next = TRUE;
6296 }
6297#endif
6298 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006299
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006300 /* For simplicity set the attributes of second half of a
6301 * double-wide character equal to the first half. */
6302 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006304
6305 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 screen_char(off_to, row, col + coloff);
6309 }
6310 else if ( p_wiv
6311#ifdef FEAT_GUI
6312 && !gui.in_use
6313#endif
6314 && col + coloff > 0)
6315 {
6316 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6317 {
6318 /*
6319 * Don't output stop-highlight when moving the cursor, it will
6320 * stop the highlighting when it should continue.
6321 */
6322 screen_attr = 0;
6323 }
6324 else if (screen_attr != 0)
6325 screen_stop_highlight();
6326 }
6327
6328 off_to += CHAR_CELLS;
6329 off_from += CHAR_CELLS;
6330 col += CHAR_CELLS;
6331 }
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (clear_next)
6334 {
6335 /* Clear the second half of a double-wide character of which the left
6336 * half was overwritten with a single-wide character. */
6337 ScreenLines[off_to] = ' ';
6338 if (enc_utf8)
6339 ScreenLinesUC[off_to] = 0;
6340 screen_char(off_to, row, col + coloff);
6341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343 if (clear_width > 0
6344#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006345 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346#endif
6347 )
6348 {
6349#ifdef FEAT_GUI
6350 int startCol = col;
6351#endif
6352
6353 /* blank out the rest of the line */
6354 while (col < clear_width && ScreenLines[off_to] == ' '
6355 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006356 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 ++off_to;
6359 ++col;
6360 }
6361 if (col < clear_width)
6362 {
6363#ifdef FEAT_GUI
6364 /*
6365 * In the GUI, clearing the rest of the line may leave pixels
6366 * behind if the first character cleared was bold. Some bold
6367 * fonts spill over the left. In this case we redraw the previous
6368 * character too. If we didn't skip any blanks above, then we
6369 * only redraw if the character wasn't already redrawn anyway.
6370 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006371 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
6373 hl = ScreenAttrs[off_to];
6374 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006375 {
6376 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006377
Bram Moolenaar9c697322006-10-09 20:11:17 +00006378 if (enc_utf8)
6379 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6380 * that its width is 2. */
6381 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6382 else if (enc_dbcs != 0)
6383 {
6384 /* find previous character by counting from first
6385 * column and get its width. */
6386 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006387 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006388
6389 while (off < off_to)
6390 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006391 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006392 off += prev_cells;
6393 }
6394 }
6395
6396 if (enc_dbcs != 0 && prev_cells > 1)
6397 screen_char_2(off_to - prev_cells, row,
6398 col + coloff - prev_cells);
6399 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006400 screen_char(off_to - prev_cells, row,
6401 col + coloff - prev_cells);
6402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
6404#endif
6405 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6406 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 off_to += clear_width - col;
6408 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410 }
6411
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006412 if (clear_width > 0
6413#ifdef FEAT_TEXT_PROP
6414 && !(flags & SLF_POPUP) // no separator for popup window
6415#endif
6416 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006418 // For a window that has a right neighbor, draw the separator char
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006419 // right of the window contents. But not on top of a popup window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006420 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006422#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02006423 if (!blocked_by_popup(row, col + coloff))
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006426 int c;
6427
6428 c = fillchar_vsep(&hl);
6429 if (ScreenLines[off_to] != (schar_T)c
6430 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6431 != (c >= 0x80 ? c : 0))
6432 || ScreenAttrs[off_to] != hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006434 ScreenLines[off_to] = c;
6435 ScreenAttrs[off_to] = hl;
6436 if (enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006438 if (c >= 0x80)
6439 {
6440 ScreenLinesUC[off_to] = c;
6441 ScreenLinesC[0][off_to] = 0;
6442 }
6443 else
6444 ScreenLinesUC[off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 }
Bram Moolenaaraef5c622019-06-08 17:25:33 +02006446 screen_char(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 }
6449 }
6450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 LineWraps[row] = FALSE;
6452 }
6453}
6454
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006455#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006457 * Mirror text "str" for right-left displaying.
6458 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006460 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006461rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463 char_u *p1, *p2;
6464 int t;
6465
6466 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6467 {
6468 t = *p1;
6469 *p1 = *p2;
6470 *p2 = t;
6471 }
6472}
6473#endif
6474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475/*
6476 * mark all status lines for redraw; used after first :cd
6477 */
6478 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006479status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 win_T *wp;
6482
Bram Moolenaar29323592016-07-24 22:04:11 +02006483 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (wp->w_status_height)
6485 {
6486 wp->w_redr_status = TRUE;
6487 redraw_later(VALID);
6488 }
6489}
6490
6491/*
6492 * mark all status lines of the current buffer for redraw
6493 */
6494 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006495status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 win_T *wp;
6498
Bram Moolenaar29323592016-07-24 22:04:11 +02006499 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6501 {
6502 wp->w_redr_status = TRUE;
6503 redraw_later(VALID);
6504 }
6505}
6506
6507/*
6508 * Redraw all status lines that need to be redrawn.
6509 */
6510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006511redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 win_T *wp;
6514
Bram Moolenaar29323592016-07-24 22:04:11 +02006515 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006517 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006518 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006519 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar4033c552017-09-16 20:54:51 +02006522#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523/*
6524 * Redraw all status lines at the bottom of frame "frp".
6525 */
6526 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006527win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 if (frp->fr_layout == FR_LEAF)
6530 frp->fr_win->w_redr_status = TRUE;
6531 else if (frp->fr_layout == FR_ROW)
6532 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006533 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 win_redraw_last_status(frp);
6535 }
6536 else /* frp->fr_layout == FR_COL */
6537 {
6538 frp = frp->fr_child;
6539 while (frp->fr_next != NULL)
6540 frp = frp->fr_next;
6541 win_redraw_last_status(frp);
6542 }
6543}
6544#endif
6545
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546/*
6547 * Draw the verticap separator right of window "wp" starting with line "row".
6548 */
6549 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006550draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
6552 int hl;
6553 int c;
6554
6555 if (wp->w_vsep_width)
6556 {
6557 /* draw the vertical separator right of this window */
6558 c = fillchar_vsep(&hl);
6559 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6560 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6561 c, ' ', hl);
6562 }
6563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
6565#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006566static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006569 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 */
6571 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006572status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 int len = 0;
6575
6576#ifdef FEAT_MENU
6577 int emenu = (xp->xp_context == EXPAND_MENUS
6578 || xp->xp_context == EXPAND_MENUNAMES);
6579
6580 /* Check for menu separators - replace with '|'. */
6581 if (emenu && menu_is_separator(s))
6582 return 1;
6583#endif
6584
6585 while (*s != NUL)
6586 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006587 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006588 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006589 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
6591
6592 return len;
6593}
6594
6595/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006596 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006597 * These are backslashes used for escaping. Do show backslashes in help tags.
6598 */
6599 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006600skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006601{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006602 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006603#ifdef FEAT_MENU
6604 || ((xp->xp_context == EXPAND_MENUS
6605 || xp->xp_context == EXPAND_MENUNAMES)
6606 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6607#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006608 )
6609 {
6610#ifndef BACKSLASH_IN_FILENAME
6611 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6612 return 2;
6613#endif
6614 return 1;
6615 }
6616 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006617}
6618
6619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 * Show wildchar matches in the status line.
6621 * Show at least the "match" item.
6622 * We start at item 'first_match' in the list and show all matches that fit.
6623 *
6624 * If inversion is possible we use it. Else '=' characters are used.
6625 */
6626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006627win_redr_status_matches(
6628 expand_T *xp,
6629 int num_matches,
6630 char_u **matches, /* list of matches */
6631 int match,
6632 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633{
6634#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6635 int row;
6636 char_u *buf;
6637 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006638 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 int fillchar;
6640 int attr;
6641 int i;
6642 int highlight = TRUE;
6643 char_u *selstart = NULL;
6644 int selstart_col = 0;
6645 char_u *selend = NULL;
6646 static int first_match = 0;
6647 int add_left = FALSE;
6648 char_u *s;
6649#ifdef FEAT_MENU
6650 int emenu;
6651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 if (matches == NULL) /* interrupted completion? */
6655 return;
6656
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006657 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006658 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006659 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006660 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 if (buf == NULL)
6662 return;
6663
6664 if (match == -1) /* don't show match but original text */
6665 {
6666 match = 0;
6667 highlight = FALSE;
6668 }
6669 /* count 1 for the ending ">" */
6670 clen = status_match_len(xp, L_MATCH(match)) + 3;
6671 if (match == 0)
6672 first_match = 0;
6673 else if (match < first_match)
6674 {
6675 /* jumping left, as far as we can go */
6676 first_match = match;
6677 add_left = TRUE;
6678 }
6679 else
6680 {
6681 /* check if match fits on the screen */
6682 for (i = first_match; i < match; ++i)
6683 clen += status_match_len(xp, L_MATCH(i)) + 2;
6684 if (first_match > 0)
6685 clen += 2;
6686 /* jumping right, put match at the left */
6687 if ((long)clen > Columns)
6688 {
6689 first_match = match;
6690 /* if showing the last match, we can add some on the left */
6691 clen = 2;
6692 for (i = match; i < num_matches; ++i)
6693 {
6694 clen += status_match_len(xp, L_MATCH(i)) + 2;
6695 if ((long)clen >= Columns)
6696 break;
6697 }
6698 if (i == num_matches)
6699 add_left = TRUE;
6700 }
6701 }
6702 if (add_left)
6703 while (first_match > 0)
6704 {
6705 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6706 if ((long)clen >= Columns)
6707 break;
6708 --first_match;
6709 }
6710
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006711 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712
6713 if (first_match == 0)
6714 {
6715 *buf = NUL;
6716 len = 0;
6717 }
6718 else
6719 {
6720 STRCPY(buf, "< ");
6721 len = 2;
6722 }
6723 clen = len;
6724
6725 i = first_match;
6726 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6727 {
6728 if (i == match)
6729 {
6730 selstart = buf + len;
6731 selstart_col = clen;
6732 }
6733
6734 s = L_MATCH(i);
6735 /* Check for menu separators - replace with '|' */
6736#ifdef FEAT_MENU
6737 emenu = (xp->xp_context == EXPAND_MENUS
6738 || xp->xp_context == EXPAND_MENUNAMES);
6739 if (emenu && menu_is_separator(s))
6740 {
6741 STRCPY(buf + len, transchar('|'));
6742 l = (int)STRLEN(buf + len);
6743 len += l;
6744 clen += l;
6745 }
6746 else
6747#endif
6748 for ( ; *s != NUL; ++s)
6749 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006750 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006752 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
6754 STRNCPY(buf + len, s, l);
6755 s += l - 1;
6756 len += l;
6757 }
6758 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {
6760 STRCPY(buf + len, transchar_byte(*s));
6761 len += (int)STRLEN(buf + len);
6762 }
6763 }
6764 if (i == match)
6765 selend = buf + len;
6766
6767 *(buf + len++) = ' ';
6768 *(buf + len++) = ' ';
6769 clen += 2;
6770 if (++i == num_matches)
6771 break;
6772 }
6773
6774 if (i != num_matches)
6775 {
6776 *(buf + len++) = '>';
6777 ++clen;
6778 }
6779
6780 buf[len] = NUL;
6781
6782 row = cmdline_row - 1;
6783 if (row >= 0)
6784 {
6785 if (wild_menu_showing == 0)
6786 {
6787 if (msg_scrolled > 0)
6788 {
6789 /* Put the wildmenu just above the command line. If there is
6790 * no room, scroll the screen one line up. */
6791 if (cmdline_row == Rows - 1)
6792 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006793 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 ++msg_scrolled;
6795 }
6796 else
6797 {
6798 ++cmdline_row;
6799 ++row;
6800 }
6801 wild_menu_showing = WM_SCROLLED;
6802 }
6803 else
6804 {
6805 /* Create status line if needed by setting 'laststatus' to 2.
6806 * Set 'winminheight' to zero to avoid that the window is
6807 * resized. */
6808 if (lastwin->w_status_height == 0)
6809 {
6810 save_p_ls = p_ls;
6811 save_p_wmh = p_wmh;
6812 p_ls = 2;
6813 p_wmh = 0;
6814 last_status(FALSE);
6815 }
6816 wild_menu_showing = WM_SHOWN;
6817 }
6818 }
6819
6820 screen_puts(buf, row, 0, attr);
6821 if (selstart != NULL && highlight)
6822 {
6823 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006824 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826
6827 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6828 }
6829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 vim_free(buf);
6832}
6833#endif
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835/*
6836 * Redraw the status line of window wp.
6837 *
6838 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006839 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6840 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006842 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006843win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 int row;
6846 char_u *p;
6847 int len;
6848 int fillchar;
6849 int attr;
6850 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006851 static int busy = FALSE;
6852
6853 /* It's possible to get here recursively when 'statusline' (indirectly)
6854 * invokes ":redrawstatus". Simply ignore the call then. */
6855 if (busy)
6856 return;
6857 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
6859 wp->w_redr_status = FALSE;
6860 if (wp->w_status_height == 0)
6861 {
6862 /* no status line, can only be last window */
6863 redraw_cmdline = TRUE;
6864 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006865 else if (!redrawing()
6866#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006867 // don't update status line when popup menu is visible and may be
6868 // drawn over it, unless it will be redrawn later
6869 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00006870#endif
6871 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 /* Don't redraw right now, do it later. */
6874 wp->w_redr_status = TRUE;
6875 }
6876#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006877 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 {
6879 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00006880 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 }
6882#endif
6883 else
6884 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006885 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006887 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 p = NameBuff;
6889 len = (int)STRLEN(p);
6890
Bram Moolenaard85f2712017-07-28 21:51:57 +02006891 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892#ifdef FEAT_QUICKFIX
6893 || wp->w_p_pvw
6894#endif
6895 || bufIsChanged(wp->w_buffer)
6896 || wp->w_buffer->b_p_ro)
6897 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02006898 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006900 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 len += (int)STRLEN(p + len);
6902 }
6903#ifdef FEAT_QUICKFIX
6904 if (wp->w_p_pvw)
6905 {
6906 STRCPY(p + len, _("[Preview]"));
6907 len += (int)STRLEN(p + len);
6908 }
6909#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02006910 if (bufIsChanged(wp->w_buffer)
6911#ifdef FEAT_TERMINAL
6912 && !bt_terminal(wp->w_buffer)
6913#endif
6914 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
6916 STRCPY(p + len, "[+]");
6917 len += 3;
6918 }
6919 if (wp->w_buffer->b_p_ro)
6920 {
Bram Moolenaar23584032013-06-07 20:17:11 +02006921 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01006922 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 }
6924
Bram Moolenaar02631462017-09-22 15:20:32 +02006925 this_ru_col = ru_col - (Columns - wp->w_width);
6926 if (this_ru_col < (wp->w_width + 1) / 2)
6927 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 if (this_ru_col <= 1)
6929 {
6930 p = (char_u *)"<"; /* No room for file name! */
6931 len = 1;
6932 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01006933 else if (has_mbyte)
6934 {
6935 int clen = 0, i;
6936
6937 /* Count total number of display cells. */
6938 clen = mb_string2cells(p, -1);
6939
6940 /* Find first character that will fit.
6941 * Going from start to end is much faster for DBCS. */
6942 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
6943 i += (*mb_ptr2len)(p + i))
6944 clen -= (*mb_ptr2cells)(p + i);
6945 len = clen;
6946 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01006948 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01006950 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952
Bram Moolenaara12a1612019-01-24 16:39:02 +01006953 }
6954 else if (len > this_ru_col - 1)
6955 {
6956 p += len - (this_ru_col - 1);
6957 *p = '<';
6958 len = this_ru_col - 1;
6959 }
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02006962 screen_puts(p, row, wp->w_wincol, attr);
6963 screen_fill(row, row + 1, len + wp->w_wincol,
6964 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02006966 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
6968 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006969 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02006972 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973#endif
6974 }
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 /*
6977 * May need to draw the character below the vertical separator.
6978 */
6979 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
6980 {
6981 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006982 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 else
6984 fillchar = fillchar_vsep(&attr);
6985 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
6986 attr);
6987 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006988 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989}
6990
Bram Moolenaar238a5642006-02-21 22:12:05 +00006991#ifdef FEAT_STL_OPT
6992/*
6993 * Redraw the status line according to 'statusline' and take care of any
6994 * errors encountered.
6995 */
6996 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006997redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00006998{
Bram Moolenaar362f3562009-11-03 16:20:34 +00006999 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007000 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007001
7002 /* When called recursively return. This can happen when the statusline
7003 * contains an expression that triggers a redraw. */
7004 if (entered)
7005 return;
7006 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007007
Bram Moolenaara742e082016-04-05 21:10:38 +02007008 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007009 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007010 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007011 {
7012 /* When there is an error disable the statusline, otherwise the
7013 * display is messed up with errors and a redraw triggers the problem
7014 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007015 set_string_option_direct((char_u *)"statusline", -1,
7016 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007017 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007018 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007019 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007020 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007021}
7022#endif
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024/*
7025 * Return TRUE if the status line of window "wp" is connected to the status
7026 * line of the window right of it. If not, then it's a vertical separator.
7027 * Only call if (wp->w_vsep_width != 0).
7028 */
7029 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007030stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 frame_T *fr;
7033
7034 fr = wp->w_frame;
7035 while (fr->fr_parent != NULL)
7036 {
7037 if (fr->fr_parent->fr_layout == FR_COL)
7038 {
7039 if (fr->fr_next != NULL)
7040 break;
7041 }
7042 else
7043 {
7044 if (fr->fr_next != NULL)
7045 return TRUE;
7046 }
7047 fr = fr->fr_parent;
7048 }
7049 return FALSE;
7050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053/*
7054 * Get the value to show for the language mappings, active 'keymap'.
7055 */
7056 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007057get_keymap_str(
7058 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007059 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007060 char_u *buf, /* buffer for the result */
7061 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 char_u *p;
7064
7065 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7066 return FALSE;
7067
7068 {
7069#ifdef FEAT_EVAL
7070 buf_T *old_curbuf = curbuf;
7071 win_T *old_curwin = curwin;
7072 char_u *s;
7073
7074 curbuf = wp->w_buffer;
7075 curwin = wp;
7076 STRCPY(buf, "b:keymap_name"); /* must be writable */
7077 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007078 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 --emsg_skip;
7080 curbuf = old_curbuf;
7081 curwin = old_curwin;
7082 if (p == NULL || *p == NUL)
7083#endif
7084 {
7085#ifdef FEAT_KEYMAP
7086 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7087 p = wp->w_buffer->b_p_keymap;
7088 else
7089#endif
7090 p = (char_u *)"lang";
7091 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007092 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 buf[0] = NUL;
7094#ifdef FEAT_EVAL
7095 vim_free(s);
7096#endif
7097 }
7098 return buf[0] != NUL;
7099}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100
7101#if defined(FEAT_STL_OPT) || defined(PROTO)
7102/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007103 * Redraw the status line or ruler of window "wp".
7104 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 */
7106 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007107win_redr_custom(
7108 win_T *wp,
7109 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007111 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 int attr;
7113 int curattr;
7114 int row;
7115 int col = 0;
7116 int maxwidth;
7117 int width;
7118 int n;
7119 int len;
7120 int fillchar;
7121 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007122 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007124 struct stl_hlrec hltab[STL_MAX_ITEM];
7125 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007126 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007127 win_T *ewp;
7128 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129
Bram Moolenaar1d633412013-12-11 15:52:01 +01007130 /* There is a tiny chance that this gets called recursively: When
7131 * redrawing a status line triggers redrawing the ruler or tabline.
7132 * Avoid trouble by not allowing recursion. */
7133 if (entered)
7134 return;
7135 entered = TRUE;
7136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007138 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007140 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007141 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007142 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007143 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007144 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007145 maxwidth = Columns;
7146# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007147 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007150 else
7151 {
7152 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007153 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007154 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007155
7156 if (draw_ruler)
7157 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007159 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007160 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007161 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 if (*++stl == '-')
7163 stl++;
7164 if (atoi((char *)stl))
7165 while (VIM_ISDIGIT(*stl))
7166 stl++;
7167 if (*stl++ != '(')
7168 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007169 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007170 col = ru_col - (Columns - wp->w_width);
7171 if (col < (wp->w_width + 1) / 2)
7172 col = (wp->w_width + 1) / 2;
7173 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007174 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007175 {
7176 row = Rows - 1;
7177 --maxwidth; /* writing in last column may cause scrolling */
7178 fillchar = ' ';
7179 attr = 0;
7180 }
7181
7182# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007183 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007184# endif
7185 }
7186 else
7187 {
7188 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007190 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007191 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007192# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007193 use_sandbox = was_set_insecurely((char_u *)"statusline",
7194 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007195# endif
7196 }
7197
Bram Moolenaar53f81742017-09-22 14:35:51 +02007198 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007199 }
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007202 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
Bram Moolenaar61452852011-02-01 18:01:11 +01007204 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7205 * the cursor away and back. */
7206 ewp = wp == NULL ? curwin : wp;
7207 p_crb_save = ewp->w_p_crb;
7208 ewp->w_p_crb = FALSE;
7209
Bram Moolenaar362f3562009-11-03 16:20:34 +00007210 /* Make a copy, because the statusline may include a function call that
7211 * might change the option value and free the memory. */
7212 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007213 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007214 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007215 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007216 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007217 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007219 /* Make all characters printable. */
7220 p = transstr(buf);
7221 if (p != NULL)
7222 {
7223 vim_strncpy(buf, p, sizeof(buf) - 1);
7224 vim_free(p);
7225 }
7226
7227 /* fill up with "fillchar" */
7228 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007229 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 ++width;
7233 }
7234 buf[len] = NUL;
7235
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007236 /*
7237 * Draw each snippet with the specified highlighting.
7238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 curattr = attr;
7240 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007241 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007243 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 screen_puts_len(p, len, row, col, curattr);
7245 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007246 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007248 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007250 else if (hltab[n].userhl < 0)
7251 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007252#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007253 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7254 && wp->w_status_height != 0)
7255 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007256 else if (wp != NULL && bt_terminal(wp->w_buffer)
7257 && wp->w_status_height != 0)
7258 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007259#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007260 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007261 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007263 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007266
7267 if (wp == NULL)
7268 {
7269 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7270 col = 0;
7271 len = 0;
7272 p = buf;
7273 fillchar = 0;
7274 for (n = 0; tabtab[n].start != NULL; n++)
7275 {
7276 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7277 while (col < len)
7278 TabPageIdxs[col++] = fillchar;
7279 p = tabtab[n].start;
7280 fillchar = tabtab[n].userhl;
7281 }
7282 while (col < Columns)
7283 TabPageIdxs[col++] = fillchar;
7284 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007285
7286theend:
7287 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288}
7289
7290#endif /* FEAT_STL_OPT */
7291
7292/*
7293 * Output a single character directly to the screen and update ScreenLines.
7294 */
7295 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007296screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 char_u buf[MB_MAXBYTES + 1];
7299
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007300 if (has_mbyte)
7301 buf[(*mb_char2bytes)(c, buf)] = NUL;
7302 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007303 {
7304 buf[0] = c;
7305 buf[1] = NUL;
7306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 screen_puts(buf, row, col, attr);
7308}
7309
7310/*
7311 * Get a single character directly from ScreenLines into "bytes[]".
7312 * Also return its attribute in *attrp;
7313 */
7314 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007315screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 unsigned off;
7318
7319 /* safety check */
7320 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7321 {
7322 off = LineOffset[row] + col;
7323 *attrp = ScreenAttrs[off];
7324 bytes[0] = ScreenLines[off];
7325 bytes[1] = NUL;
7326
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 if (enc_utf8 && ScreenLinesUC[off] != 0)
7328 bytes[utfc_char2bytes(off, bytes)] = NUL;
7329 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7330 {
7331 bytes[0] = ScreenLines[off];
7332 bytes[1] = ScreenLines2[off];
7333 bytes[2] = NUL;
7334 }
7335 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7336 {
7337 bytes[1] = ScreenLines[off + 1];
7338 bytes[2] = NUL;
7339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341}
7342
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007343/*
7344 * Return TRUE if composing characters for screen posn "off" differs from
7345 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007346 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007347 */
7348 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007349screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007350{
7351 int i;
7352
7353 for (i = 0; i < Screen_mco; ++i)
7354 {
7355 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7356 return TRUE;
7357 if (u8cc[i] == 0)
7358 break;
7359 }
7360 return FALSE;
7361}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363/*
7364 * Put string '*text' on the screen at position 'row' and 'col', with
7365 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7366 * Note: only outputs within one row, message is truncated at screen boundary!
7367 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7368 */
7369 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007370screen_puts(
7371 char_u *text,
7372 int row,
7373 int col,
7374 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 screen_puts_len(text, -1, row, col, attr);
7377}
7378
7379/*
7380 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7381 * a NUL.
7382 */
7383 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007384screen_puts_len(
7385 char_u *text,
7386 int textlen,
7387 int row,
7388 int col,
7389 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 unsigned off;
7392 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007393 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007395 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int mbyte_blen = 1;
7397 int mbyte_cells = 1;
7398 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007399 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007401#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007403 int pc, nc, nc1;
7404 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007406 int force_redraw_this;
7407 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007408 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007410 // Safety check. The check for negative row and column is to fix issue
7411 // #4102. TODO: find out why row/col could be negative.
7412 if (ScreenLines == NULL
7413 || row >= screen_Rows || row < 0
7414 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007416 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
Bram Moolenaarc236c162008-07-13 17:41:49 +00007418 /* When drawing over the right halve of a double-wide char clear out the
7419 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007420 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007421#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007422 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007423#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007424 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007425 {
7426 ScreenLines[off - 1] = ' ';
7427 ScreenAttrs[off - 1] = 0;
7428 if (enc_utf8)
7429 {
7430 ScreenLinesUC[off - 1] = 0;
7431 ScreenLinesC[0][off - 1] = 0;
7432 }
7433 /* redraw the previous cell, make it empty */
7434 screen_char(off - 1, row, col - 1);
7435 /* force the cell at "col" to be redrawn */
7436 force_redraw_next = TRUE;
7437 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007438
Bram Moolenaar367329b2007-08-30 11:53:22 +00007439 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007440 while (col < screen_Columns
7441 && (len < 0 || (int)(ptr - text) < len)
7442 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 {
7444 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 /* check if this is the first byte of a multibyte */
7446 if (has_mbyte)
7447 {
7448 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007449 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007451 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7453 mbyte_cells = 1;
7454 else if (enc_dbcs != 0)
7455 mbyte_cells = mbyte_blen;
7456 else /* enc_utf8 */
7457 {
7458 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007459 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 (int)((text + len) - ptr));
7461 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007462 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007464#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7466 {
7467 /* Do Arabic shaping. */
7468 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7469 {
7470 /* Past end of string to be displayed. */
7471 nc = NUL;
7472 nc1 = NUL;
7473 }
7474 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007475 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007476 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7477 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007478 nc1 = pcc[0];
7479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 pc = prev_c;
7481 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484 else
7485 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007486#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007487 if (col + mbyte_cells > screen_Columns)
7488 {
7489 /* Only 1 cell left, but character requires 2 cells:
7490 * display a '>' in the last column to avoid wrapping. */
7491 c = '>';
7492 mbyte_cells = 1;
7493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 }
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007497 force_redraw_this = force_redraw_next;
7498 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007499
7500 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 || (mbyte_cells == 2
7502 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7503 || (enc_dbcs == DBCS_JPNU
7504 && c == 0x8e
7505 && ScreenLines2[off] != ptr[1])
7506 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007507 && (ScreenLinesUC[off] !=
7508 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7509 || (ScreenLinesUC[off] != 0
7510 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007512 || exmode_active;
7513
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007514 if ((need_redraw || force_redraw_this)
7515#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007516 && !blocked_by_popup(row, col)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +02007517#endif
7518 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 {
7520#if defined(FEAT_GUI) || defined(UNIX)
7521 /* The bold trick makes a single row of pixels appear in the next
7522 * character. When a bold character is removed, the next
7523 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007524 * and for some xterms. */
7525 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526# ifdef FEAT_GUI
7527 gui.in_use
7528# endif
7529# if defined(FEAT_GUI) && defined(UNIX)
7530 ||
7531# endif
7532# ifdef UNIX
7533 term_is_xterm
7534# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007535 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007537 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007539 if (n > HL_ALL)
7540 n = syn_attr2attr(n);
7541 if (n & HL_BOLD)
7542 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 /* When at the end of the text and overwriting a two-cell
7546 * character with a one-cell character, need to clear the next
7547 * cell. Also when overwriting the left halve of a two-cell char
7548 * with the right halve of a two-cell char. Do this only once
7549 * (mb_off2cells() may return 2 on the right halve). */
7550 if (clear_next_cell)
7551 clear_next_cell = FALSE;
7552 else if (has_mbyte
7553 && (len < 0 ? ptr[mbyte_blen] == NUL
7554 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007555 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007557 && (*mb_off2cells)(off, max_off) == 1
7558 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 clear_next_cell = TRUE;
7560
7561 /* Make sure we never leave a second byte of a double-byte behind,
7562 * it confuses mb_off2cells(). */
7563 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007564 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007566 && (*mb_off2cells)(off, max_off) == 1
7567 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 ScreenLines[off] = c;
7570 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 if (enc_utf8)
7572 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007573 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 ScreenLinesUC[off] = 0;
7575 else
7576 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007577 int i;
7578
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007580 for (i = 0; i < Screen_mco; ++i)
7581 {
7582 ScreenLinesC[i][off] = u8cc[i];
7583 if (u8cc[i] == 0)
7584 break;
7585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587 if (mbyte_cells == 2)
7588 {
7589 ScreenLines[off + 1] = 0;
7590 ScreenAttrs[off + 1] = attr;
7591 }
7592 screen_char(off, row, col);
7593 }
7594 else if (mbyte_cells == 2)
7595 {
7596 ScreenLines[off + 1] = ptr[1];
7597 ScreenAttrs[off + 1] = attr;
7598 screen_char_2(off, row, col);
7599 }
7600 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7601 {
7602 ScreenLines2[off] = ptr[1];
7603 screen_char(off, row, col);
7604 }
7605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 screen_char(off, row, col);
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (has_mbyte)
7609 {
7610 off += mbyte_cells;
7611 col += mbyte_cells;
7612 ptr += mbyte_blen;
7613 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007614 {
7615 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007617 len = -1;
7618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 }
7620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {
7622 ++off;
7623 ++col;
7624 ++ptr;
7625 }
7626 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007627
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007628 /* If we detected the next character needs to be redrawn, but the text
7629 * doesn't extend up to there, update the character here. */
7630 if (force_redraw_next && col < screen_Columns)
7631 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007632 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7633 screen_char_2(off, row, col);
7634 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007635 screen_char(off, row, col);
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637}
7638
7639#ifdef FEAT_SEARCH_EXTRA
7640/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007641 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 */
7643 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007644start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 if (p_hls && !no_hlsearch)
7647 {
7648 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007649 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007650# ifdef FEAT_RELTIME
7651 /* Set the time limit to 'redrawtime'. */
7652 profile_setlimit(p_rdt, &search_hl.tm);
7653# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655}
7656
7657/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007658 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 */
7660 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007661end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 if (search_hl.rm.regprog != NULL)
7664 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007665 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 search_hl.rm.regprog = NULL;
7667 }
7668}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02007669#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02007670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007672screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674 attrentry_T *aep = NULL;
7675
7676 screen_attr = attr;
7677 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01007678#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 && termcap_active
7680#endif
7681 )
7682 {
7683#ifdef FEAT_GUI
7684 if (gui.in_use)
7685 {
7686 char buf[20];
7687
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007688 /* The GUI handles this internally. */
7689 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 OUT_STR(buf);
7691 }
7692 else
7693#endif
7694 {
7695 if (attr > HL_ALL) /* special HL attr. */
7696 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007697 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 aep = syn_cterm_attr2entry(attr);
7699 else
7700 aep = syn_term_attr2entry(attr);
7701 if (aep == NULL) /* did ":syntax clear" */
7702 attr = 0;
7703 else
7704 attr = aep->ae_attr;
7705 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007706 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007708 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007709#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007710 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7711 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7712 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007713#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007714 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007715 /* If the Normal FG color has BOLD attribute and the new HL
7716 * has a FG color defined, clear BOLD. */
7717 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007718 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007720 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007721 out_str(T_UCS);
7722 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01007723 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
7724 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007726 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007728 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01007730 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007731 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732
7733 /*
7734 * Output the color or start string after bold etc., in case the
7735 * bold etc. override the color setting.
7736 */
7737 if (aep != NULL)
7738 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007739#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007740 /* When 'termguicolors' is set but fg or bg is unset,
7741 * fall back to the cterm colors. This helps for SpellBad,
7742 * where the GUI uses a red undercurl. */
7743 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007745 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007746 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007747 }
7748 else
7749#endif
7750 if (t_colors > 1)
7751 {
7752 if (aep->ae_u.cterm.fg_color)
7753 term_fg_color(aep->ae_u.cterm.fg_color - 1);
7754 }
7755#ifdef FEAT_TERMGUICOLORS
7756 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
7757 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007758 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007759 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 }
7761 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007762#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007763 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007765 if (aep->ae_u.cterm.bg_color)
7766 term_bg_color(aep->ae_u.cterm.bg_color - 1);
7767 }
7768
Bram Moolenaarf708ac52018-03-12 21:48:32 +01007769 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007770 {
7771 if (aep->ae_u.term.start != NULL)
7772 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
7774 }
7775 }
7776 }
7777}
7778
7779 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007780screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 int do_ME = FALSE; /* output T_ME code */
7783
7784 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01007785#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 && termcap_active
7787#endif
7788 )
7789 {
7790#ifdef FEAT_GUI
7791 if (gui.in_use)
7792 {
7793 char buf[20];
7794
7795 /* use internal GUI code */
7796 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
7797 OUT_STR(buf);
7798 }
7799 else
7800#endif
7801 {
7802 if (screen_attr > HL_ALL) /* special HL attr. */
7803 {
7804 attrentry_T *aep;
7805
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007806 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 {
7808 /*
7809 * Assume that t_me restores the original colors!
7810 */
7811 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007812 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007813#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007814 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
7815 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
7816 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007817#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007818 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007819#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007820 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
7821 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
7822 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007823#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01007824 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 do_ME = TRUE;
7826 }
7827 else
7828 {
7829 aep = syn_term_attr2entry(screen_attr);
7830 if (aep != NULL && aep->ae_u.term.stop != NULL)
7831 {
7832 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
7833 do_ME = TRUE;
7834 else
7835 out_str(aep->ae_u.term.stop);
7836 }
7837 }
7838 if (aep == NULL) /* did ":syntax clear" */
7839 screen_attr = 0;
7840 else
7841 screen_attr = aep->ae_attr;
7842 }
7843
7844 /*
7845 * Often all ending-codes are equal to T_ME. Avoid outputting the
7846 * same sequence several times.
7847 */
7848 if (screen_attr & HL_STANDOUT)
7849 {
7850 if (STRCMP(T_SE, T_ME) == 0)
7851 do_ME = TRUE;
7852 else
7853 out_str(T_SE);
7854 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01007855 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01007856 {
7857 if (STRCMP(T_UCE, T_ME) == 0)
7858 do_ME = TRUE;
7859 else
7860 out_str(T_UCE);
7861 }
7862 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01007863 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {
7865 if (STRCMP(T_UE, T_ME) == 0)
7866 do_ME = TRUE;
7867 else
7868 out_str(T_UE);
7869 }
7870 if (screen_attr & HL_ITALIC)
7871 {
7872 if (STRCMP(T_CZR, T_ME) == 0)
7873 do_ME = TRUE;
7874 else
7875 out_str(T_CZR);
7876 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02007877 if (screen_attr & HL_STRIKETHROUGH)
7878 {
7879 if (STRCMP(T_STE, T_ME) == 0)
7880 do_ME = TRUE;
7881 else
7882 out_str(T_STE);
7883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
7885 out_str(T_ME);
7886
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007887#ifdef FEAT_TERMGUICOLORS
7888 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007890 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007891 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007892 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007893 term_bg_rgb_color(cterm_normal_bg_gui_color);
7894 }
7895 else
7896#endif
7897 {
7898 if (t_colors > 1)
7899 {
7900 /* set Normal cterm colors */
7901 if (cterm_normal_fg_color != 0)
7902 term_fg_color(cterm_normal_fg_color - 1);
7903 if (cterm_normal_bg_color != 0)
7904 term_bg_color(cterm_normal_bg_color - 1);
7905 if (cterm_normal_fg_bold)
7906 out_str(T_MD);
7907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 }
7909 }
7910 }
7911 screen_attr = 0;
7912}
7913
7914/*
7915 * Reset the colors for a cterm. Used when leaving Vim.
7916 * The machine specific code may override this again.
7917 */
7918 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007919reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007921 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {
7923 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02007924#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007925 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
7926 || cterm_normal_bg_gui_color != INVALCOLOR)
7927 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007928#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02007930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {
7932 out_str(T_OP);
7933 screen_attr = -1;
7934 }
7935 if (cterm_normal_fg_bold)
7936 {
7937 out_str(T_ME);
7938 screen_attr = -1;
7939 }
7940 }
7941}
7942
7943/*
7944 * Put character ScreenLines["off"] on the screen at position "row" and "col",
7945 * using the attributes from ScreenAttrs["off"].
7946 */
7947 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007948screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 int attr;
7951
7952 /* Check for illegal values, just in case (could happen just after
7953 * resizing). */
7954 if (row >= screen_Rows || col >= screen_Columns)
7955 return;
7956
Bram Moolenaarae654382019-01-17 21:09:05 +01007957#ifdef FEAT_INS_EXPAND
Bram Moolenaar33796b32019-06-08 16:01:13 +02007958 // Skip if under the popup menu.
7959 // Popup windows with zindex higher than POPUPMENU_ZINDEX go on top.
7960 if (pum_under_menu(row, col)
7961# ifdef FEAT_TEXT_PROP
7962 && screen_zindex <= POPUPMENU_ZINDEX
7963# endif
7964 )
Bram Moolenaarae654382019-01-17 21:09:05 +01007965 return;
7966#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02007967#ifdef FEAT_TEXT_PROP
Bram Moolenaarc662ec92019-06-23 00:15:57 +02007968 if (blocked_by_popup(row, col))
Bram Moolenaar33796b32019-06-08 16:01:13 +02007969 return;
7970#endif
7971
Bram Moolenaar494838a2015-02-10 19:20:37 +01007972 /* Outputting a character in the last cell on the screen may scroll the
7973 * screen up. Only do it when the "xn" termcap property is set, otherwise
7974 * mark the character invalid (update it when scrolled up). */
7975 if (*T_XN == NUL
7976 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977#ifdef FEAT_RIGHTLEFT
7978 /* account for first command-line character in rightleft mode */
7979 && !cmdmsg_rl
7980#endif
7981 )
7982 {
7983 ScreenAttrs[off] = (sattr_T)-1;
7984 return;
7985 }
7986
7987 /*
7988 * Stop highlighting first, so it's easier to move the cursor.
7989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 if (screen_char_attr != 0)
7991 attr = screen_char_attr;
7992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 attr = ScreenAttrs[off];
7994 if (screen_attr != attr)
7995 screen_stop_highlight();
7996
7997 windgoto(row, col);
7998
7999 if (screen_attr != attr)
8000 screen_start_highlight(attr);
8001
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 if (enc_utf8 && ScreenLinesUC[off] != 0)
8003 {
8004 char_u buf[MB_MAXBYTES + 1];
8005
Bram Moolenaarcb070082016-04-02 22:14:51 +02008006 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008007 {
8008 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008009#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008010 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008011#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008012 )
8013 {
8014 /* Clear the two screen cells. If the character is actually
8015 * single width it won't change the second cell. */
8016 out_str((char_u *)" ");
8017 term_windgoto(row, col);
8018 }
8019 /* not sure where the cursor is after drawing the ambiguous width
8020 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008021 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008022 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008023 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008025
8026 /* Convert the UTF-8 character to bytes and write it. */
8027 buf[utfc_char2bytes(off, buf)] = NUL;
8028 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 /* double-byte character in single-width cell */
8035 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8036 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038
8039 screen_cur_col++;
8040}
8041
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042/*
8043 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8044 * on the screen at position 'row' and 'col'.
8045 * The attributes of the first byte is used for all. This is required to
8046 * output the two bytes of a double-byte character with nothing in between.
8047 */
8048 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008049screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050{
8051 /* Check for illegal values (could be wrong when screen was resized). */
8052 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8053 return;
8054
8055 /* Outputting the last character on the screen may scrollup the screen.
8056 * Don't to it! Mark the character invalid (update it when scrolled up) */
8057 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8058 {
8059 ScreenAttrs[off] = (sattr_T)-1;
8060 return;
8061 }
8062
8063 /* Output the first byte normally (positions the cursor), then write the
8064 * second byte directly. */
8065 screen_char(off, row, col);
8066 out_char(ScreenLines[off + 1]);
8067 ++screen_cur_col;
8068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070/*
8071 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8072 * This uses the contents of ScreenLines[] and doesn't change it.
8073 */
8074 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008075screen_draw_rectangle(
8076 int row,
8077 int col,
8078 int height,
8079 int width,
8080 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081{
8082 int r, c;
8083 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008084 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008086 /* Can't use ScreenLines unless initialized */
8087 if (ScreenLines == NULL)
8088 return;
8089
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 if (invert)
8091 screen_char_attr = HL_INVERSE;
8092 for (r = row; r < row + height; ++r)
8093 {
8094 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008095 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 for (c = col; c < col + width; ++c)
8097 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008098 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
8100 screen_char_2(off + c, r, c);
8101 ++c;
8102 }
8103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {
8105 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008106 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
8109 }
8110 }
8111 screen_char_attr = 0;
8112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114/*
8115 * Redraw the characters for a vertically split window.
8116 */
8117 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008118redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119{
8120 int col;
8121 int width;
8122
8123# ifdef FEAT_CLIPBOARD
8124 clip_may_clear_selection(row, end - 1);
8125# endif
8126
8127 if (wp == NULL)
8128 {
8129 col = 0;
8130 width = Columns;
8131 }
8132 else
8133 {
8134 col = wp->w_wincol;
8135 width = wp->w_width;
8136 }
8137 screen_draw_rectangle(row, col, end - row, width, FALSE);
8138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008140 static void
8141space_to_screenline(int off, int attr)
8142{
8143 ScreenLines[off] = ' ';
8144 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008145 if (enc_utf8)
8146 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008147}
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149/*
8150 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8151 * with character 'c1' in first column followed by 'c2' in the other columns.
8152 * Use attributes 'attr'.
8153 */
8154 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008155screen_fill(
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008156 int start_row,
8157 int end_row,
8158 int start_col,
8159 int end_col,
8160 int c1,
8161 int c2,
8162 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163{
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008164 int row;
8165 int col;
8166 int off;
8167 int end_off;
8168 int did_delete;
8169 int c;
8170 int norm_term;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008172 int force_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173#endif
8174
8175 if (end_row > screen_Rows) /* safety check */
8176 end_row = screen_Rows;
8177 if (end_col > screen_Columns) /* safety check */
8178 end_col = screen_Columns;
8179 if (ScreenLines == NULL
8180 || start_row >= end_row
8181 || start_col >= end_col) /* nothing to do */
8182 return;
8183
8184 /* it's a "normal" terminal when not in a GUI or cterm */
8185 norm_term = (
8186#ifdef FEAT_GUI
8187 !gui.in_use &&
8188#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008189 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 for (row = start_row; row < end_row; ++row)
8191 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008192 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008193#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008194 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008195#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008196 )
8197 {
8198 /* When drawing over the right halve of a double-wide char clear
8199 * out the left halve. When drawing over the left halve of a
8200 * double wide-char clear out the right halve. Only needed in a
8201 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008202 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008203 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008204 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008205 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 /*
8208 * Try to use delete-line termcap code, when no attributes or in a
8209 * "normal" terminal, where a bold/italic space is just a
8210 * space.
8211 */
8212 did_delete = FALSE;
8213 if (c2 == ' '
8214 && end_col == Columns
8215 && can_clear(T_CE)
8216 && (attr == 0
8217 || (norm_term
8218 && attr <= HL_ALL
8219 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8220 {
8221 /*
8222 * check if we really need to clear something
8223 */
8224 col = start_col;
8225 if (c1 != ' ') /* don't clear first char */
8226 ++col;
8227
8228 off = LineOffset[row] + col;
8229 end_off = LineOffset[row] + end_col;
8230
8231 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 if (enc_utf8)
8233 while (off < end_off && ScreenLines[off] == ' '
8234 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8235 ++off;
8236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 while (off < end_off && ScreenLines[off] == ' '
8238 && ScreenAttrs[off] == 0)
8239 ++off;
8240 if (off < end_off) /* something to be cleared */
8241 {
8242 col = off - LineOffset[row];
8243 screen_stop_highlight();
8244 term_windgoto(row, col);/* clear rest of this screen line */
8245 out_str(T_CE);
8246 screen_start(); /* don't know where cursor is now */
8247 col = end_col - col;
8248 while (col--) /* clear chars in ScreenLines */
8249 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008250 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 ++off;
8252 }
8253 }
8254 did_delete = TRUE; /* the chars are cleared now */
8255 }
8256
8257 off = LineOffset[row] + start_col;
8258 c = c1;
8259 for (col = start_col; col < end_col; ++col)
8260 {
Bram Moolenaar33796b32019-06-08 16:01:13 +02008261 if ((ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008262 || (enc_utf8 && (int)ScreenLinesUC[off]
8263 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 || ScreenAttrs[off] != attr
8265#if defined(FEAT_GUI) || defined(UNIX)
8266 || force_next
8267#endif
8268 )
Bram Moolenaar33796b32019-06-08 16:01:13 +02008269#ifdef FEAT_TEXT_PROP
8270 // Skip if under a(nother) popup.
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008271 && !blocked_by_popup(row, col)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008272#endif
8273 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
8275#if defined(FEAT_GUI) || defined(UNIX)
8276 /* The bold trick may make a single row of pixels appear in
8277 * the next character. When a bold character is removed, the
8278 * next character should be redrawn too. This happens for our
8279 * own GUI and for some xterms. */
8280 if (
8281# ifdef FEAT_GUI
8282 gui.in_use
8283# endif
8284# if defined(FEAT_GUI) && defined(UNIX)
8285 ||
8286# endif
8287# ifdef UNIX
8288 term_is_xterm
8289# endif
8290 )
8291 {
8292 if (ScreenLines[off] != ' '
8293 && (ScreenAttrs[off] > HL_ALL
8294 || ScreenAttrs[off] & HL_BOLD))
8295 force_next = TRUE;
8296 else
8297 force_next = FALSE;
8298 }
8299#endif
8300 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 if (enc_utf8)
8302 {
8303 if (c >= 0x80)
8304 {
8305 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008306 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
8309 ScreenLinesUC[off] = 0;
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 ScreenAttrs[off] = attr;
8312 if (!did_delete || c != ' ')
8313 screen_char(off, row, col);
8314 }
8315 ++off;
8316 if (col == start_col)
8317 {
8318 if (did_delete)
8319 break;
8320 c = c2;
8321 }
8322 }
8323 if (end_col == Columns)
8324 LineWraps[row] = FALSE;
8325 if (row == Rows - 1) /* overwritten the command line */
8326 {
8327 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008328 if (start_col == 0 && end_col == Columns
8329 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008331 if (start_col == 0)
8332 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334 }
8335}
8336
8337/*
8338 * Check if there should be a delay. Used before clearing or redrawing the
8339 * screen or the command line.
8340 */
8341 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008342check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8345 && !did_wait_return
8346 && emsg_silent == 0)
8347 {
8348 out_flush();
8349 ui_delay(1000L, TRUE);
8350 emsg_on_display = FALSE;
8351 if (check_msg_scroll)
8352 msg_scroll = FALSE;
8353 }
8354}
8355
8356/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008357 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8358 */
8359 static void
8360clear_TabPageIdxs(void)
8361{
8362 int scol;
8363
8364 for (scol = 0; scol < Columns; ++scol)
8365 TabPageIdxs[scol] = 0;
8366}
8367
8368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008370 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 * Returns TRUE if there is a valid screen to write to.
8372 * Returns FALSE when starting up and screen not initialized yet.
8373 */
8374 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008375screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008377 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 return (ScreenLines != NULL);
8379}
8380
8381/*
8382 * Resize the shell to Rows and Columns.
8383 * Allocate ScreenLines[] and associated items.
8384 *
8385 * There may be some time between setting Rows and Columns and (re)allocating
8386 * ScreenLines[]. This happens when starting up and when (manually) changing
8387 * the shell size. Always use screen_Rows and screen_Columns to access items
8388 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8389 * final size of the shell is needed.
8390 */
8391 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008392screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
8394 int new_row, old_row;
8395#ifdef FEAT_GUI
8396 int old_Rows;
8397#endif
8398 win_T *wp;
8399 int outofmem = FALSE;
8400 int len;
8401 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008403 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008405 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 sattr_T *new_ScreenAttrs;
8407 unsigned *new_LineOffset;
8408 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008409 short *new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008410#ifdef FEAT_TEXT_PROP
8411 short *new_popup_mask;
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008412 short *new_popup_mask_next;
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008413 char *new_popup_transparent;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008414#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00008415 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008417 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008418 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008420retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 /*
8422 * Allocation of the screen buffers is done only when the size changes and
8423 * when Rows and Columns have been set and we have started doing full
8424 * screen stuff.
8425 */
8426 if ((ScreenLines != NULL
8427 && Rows == screen_Rows
8428 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 && enc_utf8 == (ScreenLinesUC != NULL)
8430 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008431 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 || Rows == 0
8433 || Columns == 0
8434 || (!full_screen && ScreenLines == NULL))
8435 return;
8436
8437 /*
8438 * It's possible that we produce an out-of-memory message below, which
8439 * will cause this function to be called again. To break the loop, just
8440 * return here.
8441 */
8442 if (entered)
8443 return;
8444 entered = TRUE;
8445
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008446 /*
8447 * Note that the window sizes are updated before reallocating the arrays,
8448 * thus we must not redraw here!
8449 */
8450 ++RedrawingDisabled;
8451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 win_new_shellsize(); /* fit the windows in the new sized shell */
8453
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 comp_col(); /* recompute columns for shown command and ruler */
8455
8456 /*
8457 * We're changing the size of the screen.
8458 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8459 * - Move lines from the old arrays into the new arrays, clear extra
8460 * lines (unless the screen is going to be cleared).
8461 * - Free the old arrays.
8462 *
8463 * If anything fails, make ScreenLines NULL, so we don't do anything!
8464 * Continuing with the old ScreenLines may result in a crash, because the
8465 * size is wrong.
8466 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008467 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008469 if (aucmd_win != NULL)
8470 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008471#ifdef FEAT_TEXT_PROP
8472 // global popup windows
8473 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8474 win_free_lsize(wp);
8475 // tab-local popup windows
8476 FOR_ALL_TABPAGES(tp)
8477 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8478 win_free_lsize(wp);
8479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008481 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008482 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 if (enc_utf8)
8484 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008485 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008486 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008487 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8488 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008491 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8492 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8493 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8494 new_LineWraps = LALLOC_MULT(char_u, Rows);
8495 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008496#ifdef FEAT_TEXT_PROP
8497 new_popup_mask = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008498 new_popup_mask_next = LALLOC_MULT(short, Rows * Columns);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008499 new_popup_transparent = LALLOC_MULT(char, Rows * Columns);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008502 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {
8504 if (win_alloc_lines(wp) == FAIL)
8505 {
8506 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008507 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 }
8509 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008510 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8511 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008512 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02008513#ifdef FEAT_TEXT_PROP
8514 // global popup windows
8515 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
8516 if (win_alloc_lines(wp) == FAIL)
8517 {
8518 outofmem = TRUE;
8519 goto give_up;
8520 }
8521 // tab-local popup windows
8522 FOR_ALL_TABPAGES(tp)
8523 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
8524 if (win_alloc_lines(wp) == FAIL)
8525 {
8526 outofmem = TRUE;
8527 goto give_up;
8528 }
8529#endif
8530
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008531give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008533 for (i = 0; i < p_mco; ++i)
8534 if (new_ScreenLinesC[i] == NULL)
8535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008537 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 || new_ScreenAttrs == NULL
8540 || new_LineOffset == NULL
8541 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008542 || new_TabPageIdxs == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008543#ifdef FEAT_TEXT_PROP
8544 || new_popup_mask == NULL
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008545 || new_popup_mask_next == NULL
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008546 || new_popup_transparent == NULL
Bram Moolenaar33796b32019-06-08 16:01:13 +02008547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 || outofmem)
8549 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008550 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008551 {
8552 /* guess the size */
8553 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8554
8555 /* Remember we did this to avoid getting outofmem messages over
8556 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008557 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008558 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008559 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008560 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008561 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008562 VIM_CLEAR(new_ScreenLinesC[i]);
8563 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008564 VIM_CLEAR(new_ScreenAttrs);
8565 VIM_CLEAR(new_LineOffset);
8566 VIM_CLEAR(new_LineWraps);
8567 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008568#ifdef FEAT_TEXT_PROP
8569 VIM_CLEAR(new_popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008570 VIM_CLEAR(new_popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008571 VIM_CLEAR(new_popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 }
8574 else
8575 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008576 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 for (new_row = 0; new_row < Rows; ++new_row)
8579 {
8580 new_LineOffset[new_row] = new_row * Columns;
8581 new_LineWraps[new_row] = FALSE;
8582
8583 /*
8584 * If the screen is not going to be cleared, copy as much as
8585 * possible from the old screen to the new one and clear the rest
8586 * (used when resizing the window at the "--more--" prompt or when
8587 * executing an external command, for the GUI).
8588 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008589 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 {
8591 (void)vim_memset(new_ScreenLines + new_row * Columns,
8592 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (enc_utf8)
8594 {
8595 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8596 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008597 for (i = 0; i < p_mco; ++i)
8598 (void)vim_memset(new_ScreenLinesC[i]
8599 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 0, (size_t)Columns * sizeof(u8char_T));
8601 }
8602 if (enc_dbcs == DBCS_JPNU)
8603 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8604 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8606 0, (size_t)Columns * sizeof(sattr_T));
8607 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008608 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {
8610 if (screen_Columns < Columns)
8611 len = screen_Columns;
8612 else
8613 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00008614 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008615 * may be invalid now. Also when p_mco changes. */
8616 if (!(enc_utf8 && ScreenLinesUC == NULL)
8617 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00008618 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
8619 ScreenLines + LineOffset[old_row],
8620 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008621 if (enc_utf8 && ScreenLinesUC != NULL
8622 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {
8624 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
8625 ScreenLinesUC + LineOffset[old_row],
8626 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008627 for (i = 0; i < p_mco; ++i)
8628 mch_memmove(new_ScreenLinesC[i]
8629 + new_LineOffset[new_row],
8630 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 (size_t)len * sizeof(u8char_T));
8632 }
8633 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
8634 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
8635 ScreenLines2 + LineOffset[old_row],
8636 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
8638 ScreenAttrs + LineOffset[old_row],
8639 (size_t)len * sizeof(sattr_T));
8640 }
8641 }
8642 }
8643 /* Use the last line of the screen for the current line. */
8644 current_ScreenLine = new_ScreenLines + Rows * Columns;
8645 }
8646
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008647 free_screenlines();
8648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008651 for (i = 0; i < p_mco; ++i)
8652 ScreenLinesC[i] = new_ScreenLinesC[i];
8653 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 ScreenAttrs = new_ScreenAttrs;
8656 LineOffset = new_LineOffset;
8657 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008658 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar33796b32019-06-08 16:01:13 +02008659#ifdef FEAT_TEXT_PROP
8660 popup_mask = new_popup_mask;
Bram Moolenaar1748c7f2019-06-08 16:55:15 +02008661 vim_memset(popup_mask, 0, Rows * Columns * sizeof(short));
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008662 popup_mask_next = new_popup_mask_next;
8663 popup_transparent = new_popup_transparent;
8664 vim_memset(popup_transparent, 0, Rows * Columns * sizeof(char));
Bram Moolenaar33796b32019-06-08 16:01:13 +02008665 popup_mask_refresh = TRUE;
8666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667
8668 /* It's important that screen_Rows and screen_Columns reflect the actual
8669 * size of ScreenLines[]. Set them before calling anything. */
8670#ifdef FEAT_GUI
8671 old_Rows = screen_Rows;
8672#endif
8673 screen_Rows = Rows;
8674 screen_Columns = Columns;
8675
8676 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008677 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679#ifdef FEAT_GUI
8680 else if (gui.in_use
8681 && !gui.starting
8682 && ScreenLines != NULL
8683 && old_Rows != Rows)
8684 {
8685 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
8686 /*
8687 * Adjust the position of the cursor, for when executing an external
8688 * command.
8689 */
8690 if (msg_row >= Rows) /* Rows got smaller */
8691 msg_row = Rows - 1; /* put cursor at last row */
8692 else if (Rows > old_Rows) /* Rows got bigger */
8693 msg_row += Rows - old_Rows; /* put cursor in same place */
8694 if (msg_col >= Columns) /* Columns got smaller */
8695 msg_col = Columns - 1; /* put cursor at last column */
8696 }
8697#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008698 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008701 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008702
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008703 /*
8704 * Do not apply autocommands more than 3 times to avoid an endless loop
8705 * in case applying autocommands always changes Rows or Columns.
8706 */
8707 if (starting == 0 && ++retry_count <= 3)
8708 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00008709 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008710 /* In rare cases, autocommands may have altered Rows or Columns,
8711 * jump back to check if we need to allocate the screen again. */
8712 goto retry;
8713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714}
8715
8716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008717free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008718{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008719 int i;
8720
Bram Moolenaar33796b32019-06-08 16:01:13 +02008721 VIM_CLEAR(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008722 for (i = 0; i < Screen_mco; ++i)
Bram Moolenaar33796b32019-06-08 16:01:13 +02008723 VIM_CLEAR(ScreenLinesC[i]);
8724 VIM_CLEAR(ScreenLines2);
8725 VIM_CLEAR(ScreenLines);
8726 VIM_CLEAR(ScreenAttrs);
8727 VIM_CLEAR(LineOffset);
8728 VIM_CLEAR(LineWraps);
8729 VIM_CLEAR(TabPageIdxs);
8730#ifdef FEAT_TEXT_PROP
8731 VIM_CLEAR(popup_mask);
Bram Moolenaar4c063a02019-06-10 21:24:12 +02008732 VIM_CLEAR(popup_mask_next);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02008733 VIM_CLEAR(popup_transparent);
Bram Moolenaar33796b32019-06-08 16:01:13 +02008734#endif
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00008735}
8736
8737 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008738screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
8740 check_for_delay(FALSE);
8741 screenalloc(FALSE); /* allocate screen buffers if size changed */
8742 screenclear2(); /* clear the screen */
8743}
8744
8745 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008746screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
8748 int i;
8749
8750 if (starting == NO_SCREEN || ScreenLines == NULL
8751#ifdef FEAT_GUI
8752 || (gui.in_use && gui.starting)
8753#endif
8754 )
8755 return;
8756
8757#ifdef FEAT_GUI
8758 if (!gui.in_use)
8759#endif
8760 screen_attr = -1; /* force setting the Normal colors */
8761 screen_stop_highlight(); /* don't want highlighting here */
8762
8763#ifdef FEAT_CLIPBOARD
8764 /* disable selection without redrawing it */
8765 clip_scroll_selection(9999);
8766#endif
8767
8768 /* blank out ScreenLines */
8769 for (i = 0; i < Rows; ++i)
8770 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008771 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 LineWraps[i] = FALSE;
8773 }
8774
8775 if (can_clear(T_CL))
8776 {
8777 out_str(T_CL); /* clear the display */
8778 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008779 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 }
8781 else
8782 {
8783 /* can't clear the screen, mark all chars with invalid attributes */
8784 for (i = 0; i < Rows; ++i)
8785 lineinvalid(LineOffset[i], (int)Columns);
8786 clear_cmdline = TRUE;
8787 }
8788
8789 screen_cleared = TRUE; /* can use contents of ScreenLines now */
8790
8791 win_rest_invalid(firstwin);
8792 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00008793 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 if (must_redraw == CLEAR) /* no need to clear again */
8795 must_redraw = NOT_VALID;
8796 compute_cmdrow();
8797 msg_row = cmdline_row; /* put cursor on last line for messages */
8798 msg_col = 0;
8799 screen_start(); /* don't know where cursor is now */
8800 msg_scrolled = 0; /* can't scroll back */
8801 msg_didany = FALSE;
8802 msg_didout = FALSE;
8803}
8804
8805/*
8806 * Clear one line in ScreenLines.
8807 */
8808 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008809lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
8811 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (enc_utf8)
8813 (void)vim_memset(ScreenLinesUC + off, 0,
8814 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02008815 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816}
8817
8818/*
8819 * Mark one line in ScreenLines invalid by setting the attributes to an
8820 * invalid value.
8821 */
8822 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008823lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
8826}
8827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828/*
8829 * Copy part of a Screenline for vertically split window "wp".
8830 */
8831 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008832linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833{
8834 unsigned off_to = LineOffset[to] + wp->w_wincol;
8835 unsigned off_from = LineOffset[from] + wp->w_wincol;
8836
8837 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
8838 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 if (enc_utf8)
8840 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008841 int i;
8842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
8844 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008845 for (i = 0; i < p_mco; ++i)
8846 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
8847 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 }
8849 if (enc_dbcs == DBCS_JPNU)
8850 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
8851 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
8853 wp->w_width * sizeof(sattr_T));
8854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
8856/*
8857 * Return TRUE if clearing with term string "p" would work.
8858 * It can't work when the string is empty or it won't set the right background.
Bram Moolenaar33796b32019-06-08 16:01:13 +02008859 * Don't clear to end-of-line when there are popups, it may cause flicker.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 */
8861 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008862can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
8864 return (*p != NUL && (t_colors <= 1
8865#ifdef FEAT_GUI
8866 || gui.in_use
8867#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008868#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008869 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02008870 || (!p_tgc && cterm_normal_bg_color == 0)
8871#else
8872 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008873#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02008874 || *T_UT != NUL)
8875#ifdef FEAT_TEXT_PROP
8876 && !(p == T_CE && popup_visible)
8877#endif
8878 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880
8881/*
8882 * Reset cursor position. Use whenever cursor was moved because of outputting
8883 * something directly to the screen (shell commands) or a terminal control
8884 * code.
8885 */
8886 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008887screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 screen_cur_row = screen_cur_col = 9999;
8890}
8891
8892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 * Move the cursor to position "row","col" in the screen.
8894 * This tries to find the most efficient way to move, minimizing the number of
8895 * characters sent to the terminal.
8896 */
8897 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008898windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008900 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 int i;
8902 int plan;
8903 int cost;
8904 int wouldbe_col;
8905 int noinvcurs;
8906 char_u *bs;
8907 int goto_cost;
8908 int attr;
8909
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008910#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
8912
8913#define PLAN_LE 1
8914#define PLAN_CR 2
8915#define PLAN_NL 3
8916#define PLAN_WRITE 4
8917 /* Can't use ScreenLines unless initialized */
8918 if (ScreenLines == NULL)
8919 return;
8920
8921 if (col != screen_cur_col || row != screen_cur_row)
8922 {
8923 /* Check for valid position. */
8924 if (row < 0) /* window without text lines? */
8925 row = 0;
8926 if (row >= screen_Rows)
8927 row = screen_Rows - 1;
8928 if (col >= screen_Columns)
8929 col = screen_Columns - 1;
8930
8931 /* check if no cursor movement is allowed in highlight mode */
8932 if (screen_attr && *T_MS == NUL)
8933 noinvcurs = HIGHL_COST;
8934 else
8935 noinvcurs = 0;
8936 goto_cost = GOTO_COST + noinvcurs;
8937
8938 /*
8939 * Plan how to do the positioning:
8940 * 1. Use CR to move it to column 0, same row.
8941 * 2. Use T_LE to move it a few columns to the left.
8942 * 3. Use NL to move a few lines down, column 0.
8943 * 4. Move a few columns to the right with T_ND or by writing chars.
8944 *
8945 * Don't do this if the cursor went beyond the last column, the cursor
8946 * position is unknown then (some terminals wrap, some don't )
8947 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008948 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * characters to move the cursor to the right.
8950 */
8951 if (row >= screen_cur_row && screen_cur_col < Columns)
8952 {
8953 /*
8954 * If the cursor is in the same row, bigger col, we can use CR
8955 * or T_LE.
8956 */
8957 bs = NULL; /* init for GCC */
8958 attr = screen_attr;
8959 if (row == screen_cur_row && col < screen_cur_col)
8960 {
8961 /* "le" is preferred over "bc", because "bc" is obsolete */
8962 if (*T_LE)
8963 bs = T_LE; /* "cursor left" */
8964 else
8965 bs = T_BC; /* "backspace character (old) */
8966 if (*bs)
8967 cost = (screen_cur_col - col) * (int)STRLEN(bs);
8968 else
8969 cost = 999;
8970 if (col + 1 < cost) /* using CR is less characters */
8971 {
8972 plan = PLAN_CR;
8973 wouldbe_col = 0;
8974 cost = 1; /* CR is just one character */
8975 }
8976 else
8977 {
8978 plan = PLAN_LE;
8979 wouldbe_col = col;
8980 }
8981 if (noinvcurs) /* will stop highlighting */
8982 {
8983 cost += noinvcurs;
8984 attr = 0;
8985 }
8986 }
8987
8988 /*
8989 * If the cursor is above where we want to be, we can use CR LF.
8990 */
8991 else if (row > screen_cur_row)
8992 {
8993 plan = PLAN_NL;
8994 wouldbe_col = 0;
8995 cost = (row - screen_cur_row) * 2; /* CR LF */
8996 if (noinvcurs) /* will stop highlighting */
8997 {
8998 cost += noinvcurs;
8999 attr = 0;
9000 }
9001 }
9002
9003 /*
9004 * If the cursor is in the same row, smaller col, just use write.
9005 */
9006 else
9007 {
9008 plan = PLAN_WRITE;
9009 wouldbe_col = screen_cur_col;
9010 cost = 0;
9011 }
9012
9013 /*
9014 * Check if any characters that need to be written have the
9015 * correct attributes. Also avoid UTF-8 characters.
9016 */
9017 i = col - wouldbe_col;
9018 if (i > 0)
9019 cost += i;
9020 if (cost < goto_cost && i > 0)
9021 {
9022 /*
9023 * Check if the attributes are correct without additionally
9024 * stopping highlighting.
9025 */
9026 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9027 while (i && *p++ == attr)
9028 --i;
9029 if (i != 0)
9030 {
9031 /*
9032 * Try if it works when highlighting is stopped here.
9033 */
9034 if (*--p == 0)
9035 {
9036 cost += noinvcurs;
9037 while (i && *p++ == 0)
9038 --i;
9039 }
9040 if (i != 0)
9041 cost = 999; /* different attributes, don't do it */
9042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 if (enc_utf8)
9044 {
9045 /* Don't use an UTF-8 char for positioning, it's slow. */
9046 for (i = wouldbe_col; i < col; ++i)
9047 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9048 {
9049 cost = 999;
9050 break;
9051 }
9052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 }
9054
9055 /*
9056 * We can do it without term_windgoto()!
9057 */
9058 if (cost < goto_cost)
9059 {
9060 if (plan == PLAN_LE)
9061 {
9062 if (noinvcurs)
9063 screen_stop_highlight();
9064 while (screen_cur_col > col)
9065 {
9066 out_str(bs);
9067 --screen_cur_col;
9068 }
9069 }
9070 else if (plan == PLAN_CR)
9071 {
9072 if (noinvcurs)
9073 screen_stop_highlight();
9074 out_char('\r');
9075 screen_cur_col = 0;
9076 }
9077 else if (plan == PLAN_NL)
9078 {
9079 if (noinvcurs)
9080 screen_stop_highlight();
9081 while (screen_cur_row < row)
9082 {
9083 out_char('\n');
9084 ++screen_cur_row;
9085 }
9086 screen_cur_col = 0;
9087 }
9088
9089 i = col - screen_cur_col;
9090 if (i > 0)
9091 {
9092 /*
9093 * Use cursor-right if it's one character only. Avoids
9094 * removing a line of pixels from the last bold char, when
9095 * using the bold trick in the GUI.
9096 */
9097 if (T_ND[0] != NUL && T_ND[1] == NUL)
9098 {
9099 while (i-- > 0)
9100 out_char(*T_ND);
9101 }
9102 else
9103 {
9104 int off;
9105
9106 off = LineOffset[row] + screen_cur_col;
9107 while (i-- > 0)
9108 {
9109 if (ScreenAttrs[off] != screen_attr)
9110 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 if (enc_dbcs == DBCS_JPNU
9114 && ScreenLines[off] == 0x8e)
9115 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 ++off;
9117 }
9118 }
9119 }
9120 }
9121 }
9122 else
9123 cost = 999;
9124
9125 if (cost >= goto_cost)
9126 {
9127 if (noinvcurs)
9128 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009129 if (row == screen_cur_row && (col > screen_cur_col)
9130 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 term_cursor_right(col - screen_cur_col);
9132 else
9133 term_windgoto(row, col);
9134 }
9135 screen_cur_row = row;
9136 screen_cur_col = col;
9137 }
9138}
9139
9140/*
9141 * Set cursor to its position in the current window.
9142 */
9143 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009144setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009146 setcursor_mayforce(FALSE);
9147}
9148
9149/*
9150 * Set cursor to its position in the current window.
9151 * When "force" is TRUE also when not redrawing.
9152 */
9153 void
9154setcursor_mayforce(int force)
9155{
9156 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 {
9158 validate_cursor();
9159 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009160 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009162 /* With 'rightleft' set and the cursor on a double-wide
9163 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009164 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9165 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009166 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009167 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#endif
9169 curwin->w_wcol));
9170 }
9171}
9172
9173
9174/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009175 * Insert 'line_count' lines at 'row' in window 'wp'.
9176 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9177 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 * scrolling.
9179 * Returns FAIL if the lines are not inserted, OK for success.
9180 */
9181 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009182win_ins_lines(
9183 win_T *wp,
9184 int row,
9185 int line_count,
9186 int invalid,
9187 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
9189 int did_delete;
9190 int nextrow;
9191 int lastrow;
9192 int retval;
9193
9194 if (invalid)
9195 wp->w_lines_valid = 0;
9196
9197 if (wp->w_height < 5)
9198 return FAIL;
9199
9200 if (line_count > wp->w_height - row)
9201 line_count = wp->w_height - row;
9202
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009203 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 if (retval != MAYBE)
9205 return retval;
9206
9207 /*
9208 * If there is a next window or a status line, we first try to delete the
9209 * lines at the bottom to avoid messing what is after the window.
9210 * If this fails and there are following windows, don't do anything to avoid
9211 * messing up those windows, better just redraw.
9212 */
9213 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 if (wp->w_next != NULL || wp->w_status_height)
9215 {
9216 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009217 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 did_delete = TRUE;
9219 else if (wp->w_next)
9220 return FAIL;
9221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 /*
9223 * if no lines deleted, blank the lines that will end up below the window
9224 */
9225 if (!did_delete)
9226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009229 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 lastrow = nextrow + line_count;
9231 if (lastrow > Rows)
9232 lastrow = Rows;
9233 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009234 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 ' ', ' ', 0);
9236 }
9237
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009238 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 == FAIL)
9240 {
9241 /* deletion will have messed up other windows */
9242 if (did_delete)
9243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 win_rest_invalid(W_NEXT(wp));
9246 }
9247 return FAIL;
9248 }
9249
9250 return OK;
9251}
9252
9253/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009254 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9256 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9257 * scrolling
9258 * Return OK for success, FAIL if the lines are not deleted.
9259 */
9260 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009261win_del_lines(
9262 win_T *wp,
9263 int row,
9264 int line_count,
9265 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009266 int mayclear,
9267 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
9269 int retval;
9270
9271 if (invalid)
9272 wp->w_lines_valid = 0;
9273
9274 if (line_count > wp->w_height - row)
9275 line_count = wp->w_height - row;
9276
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009277 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 if (retval != MAYBE)
9279 return retval;
9280
9281 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009282 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 return FAIL;
9284
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 /*
9286 * If there are windows or status lines below, try to put them at the
9287 * correct place. If we can't do that, they have to be redrawn.
9288 */
9289 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9290 {
9291 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009292 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 {
9294 wp->w_redr_status = TRUE;
9295 win_rest_invalid(wp->w_next);
9296 }
9297 }
9298 /*
9299 * If this is the last window and there is no status line, redraw the
9300 * command line later.
9301 */
9302 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 redraw_cmdline = TRUE;
9304 return OK;
9305}
9306
9307/*
9308 * Common code for win_ins_lines() and win_del_lines().
9309 * Returns OK or FAIL when the work has been done.
9310 * Returns MAYBE when not finished yet.
9311 */
9312 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009313win_do_lines(
9314 win_T *wp,
9315 int row,
9316 int line_count,
9317 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009318 int del,
9319 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 int retval;
9322
9323 if (!redrawing() || line_count <= 0)
9324 return FAIL;
9325
Bram Moolenaar33796b32019-06-08 16:01:13 +02009326 // When inserting lines would result in loss of command output, just redraw
9327 // the lines.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009328 if (no_win_do_lines_ins && !del)
9329 return FAIL;
9330
Bram Moolenaar33796b32019-06-08 16:01:13 +02009331 // only a few lines left: redraw is faster
Bram Moolenaar4033c552017-09-16 20:54:51 +02009332 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009334 if (!no_win_do_lines_ins)
Bram Moolenaar33796b32019-06-08 16:01:13 +02009335 screenclear(); // will set wp->w_lines_valid to 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 return FAIL;
9337 }
9338
Bram Moolenaar33796b32019-06-08 16:01:13 +02009339#ifdef FEAT_TEXT_PROP
Bram Moolenaar4c063a02019-06-10 21:24:12 +02009340 // this doesn't work when there are popups visible
Bram Moolenaar33796b32019-06-08 16:01:13 +02009341 if (popup_visible)
9342 return FAIL;
9343#endif
9344
9345 // Delete all remaining lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 if (row + line_count >= wp->w_height)
9347 {
9348 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009349 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 ' ', ' ', 0);
9351 return OK;
9352 }
9353
9354 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009355 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009357 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009359 if (!no_win_do_lines_ins)
9360 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361
9362 /*
9363 * If the terminal can set a scroll region, use that.
9364 * Always do this in a vertically split window. This will redraw from
9365 * ScreenLines[] when t_CV isn't defined. That's faster than using
9366 * win_line().
9367 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009368 * a character in the lower right corner of the scroll region may cause a
9369 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009371 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 scroll_region_set(wp, row);
9375 if (del)
9376 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009377 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 else
9379 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009380 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 scroll_region_reset();
9383 return retval;
9384 }
9385
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388
9389 return MAYBE;
9390}
9391
9392/*
9393 * window 'wp' and everything after it is messed up, mark it for redraw
9394 */
9395 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009396win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 {
9400 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 wp->w_redr_status = TRUE;
9402 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
9404 redraw_cmdline = TRUE;
9405}
9406
9407/*
9408 * The rest of the routines in this file perform screen manipulations. The
9409 * given operation is performed physically on the screen. The corresponding
9410 * change is also made to the internal screen image. In this way, the editor
9411 * anticipates the effect of editing changes on the appearance of the screen.
9412 * That way, when we call screenupdate a complete redraw isn't usually
9413 * necessary. Another advantage is that we can keep adding code to anticipate
9414 * screen changes, and in the meantime, everything still works.
9415 */
9416
9417/*
9418 * types for inserting or deleting lines
9419 */
9420#define USE_T_CAL 1
9421#define USE_T_CDL 2
9422#define USE_T_AL 3
9423#define USE_T_CE 4
9424#define USE_T_DL 5
9425#define USE_T_SR 6
9426#define USE_NL 7
9427#define USE_T_CD 8
9428#define USE_REDRAW 9
9429
9430/*
9431 * insert lines on the screen and update ScreenLines[]
9432 * 'end' is the line after the scrolled part. Normally it is Rows.
9433 * When scrolling region used 'off' is the offset from the top for the region.
9434 * 'row' and 'end' are relative to the start of the region.
9435 *
9436 * return FAIL for failure, OK for success.
9437 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009438 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009439screen_ins_lines(
9440 int off,
9441 int row,
9442 int line_count,
9443 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009444 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009445 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 int i;
9448 int j;
9449 unsigned temp;
9450 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009451 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 int type;
9453 int result_empty;
9454 int can_ce = can_clear(T_CE);
9455
9456 /*
9457 * FAIL if
9458 * - there is no valid screen
9459 * - the screen has to be redrawn completely
9460 * - the line count is less than one
9461 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009462 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar33796b32019-06-08 16:01:13 +02009463 * - there is a popup window
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 */
Bram Moolenaar33796b32019-06-08 16:01:13 +02009465 if (!screen_valid(TRUE)
9466 || line_count <= 0 || line_count > p_ttyscroll
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009467#ifdef FEAT_CLIPBOARD
9468 || (clip_star.state != SELECT_CLEARED
9469 && redrawing_for_callback > 0)
9470#endif
Bram Moolenaar33796b32019-06-08 16:01:13 +02009471#ifdef FEAT_TEXT_PROP
9472 || popup_visible
9473#endif
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009474 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 return FAIL;
9476
9477 /*
9478 * There are seven ways to insert lines:
9479 * 0. When in a vertically split window and t_CV isn't set, redraw the
9480 * characters from ScreenLines[].
9481 * 1. Use T_CD (clear to end of display) if it exists and the result of
9482 * the insert is just empty lines
9483 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9484 * present or line_count > 1. It looks better if we do all the inserts
9485 * at once.
9486 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9487 * insert is just empty lines and T_CE is not present or line_count >
9488 * 1.
9489 * 4. Use T_AL (insert line) if it exists.
9490 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9491 * just empty lines.
9492 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9493 * just empty lines.
9494 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9495 * the 'da' flag is not set or we have clear line capability.
9496 * 8. redraw the characters from ScreenLines[].
9497 *
9498 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9499 * the scrollbar for the window. It does have insert line, use that if it
9500 * exists.
9501 */
9502 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9504 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009505 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 type = USE_T_CD;
9507 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9508 type = USE_T_CAL;
9509 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9510 type = USE_T_CDL;
9511 else if (*T_AL != NUL)
9512 type = USE_T_AL;
9513 else if (can_ce && result_empty)
9514 type = USE_T_CE;
9515 else if (*T_DL != NUL && result_empty)
9516 type = USE_T_DL;
9517 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9518 type = USE_T_SR;
9519 else
9520 return FAIL;
9521
9522 /*
9523 * For clearing the lines screen_del_lines() is used. This will also take
9524 * care of t_db if necessary.
9525 */
9526 if (type == USE_T_CD || type == USE_T_CDL ||
9527 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009528 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529
9530 /*
9531 * If text is retained below the screen, first clear or delete as many
9532 * lines at the bottom of the window as are about to be inserted so that
9533 * the deleted lines won't later surface during a screen_del_lines.
9534 */
9535 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009536 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
9538#ifdef FEAT_CLIPBOARD
9539 /* Remove a modeless selection when inserting lines halfway the screen
9540 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009541 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009542 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 else
9544 clip_scroll_selection(-line_count);
9545#endif
9546
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547#ifdef FEAT_GUI
9548 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9549 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009550 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551#endif
9552
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009553 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9554 cursor_col = wp->w_wincol;
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (*T_CCS != NUL) /* cursor relative to region */
9557 cursor_row = row;
9558 else
9559 cursor_row = row + off;
9560
9561 /*
9562 * Shift LineOffset[] line_count down to reflect the inserted lines.
9563 * Clear the inserted lines in ScreenLines[].
9564 */
9565 row += off;
9566 end += off;
9567 for (i = 0; i < line_count; ++i)
9568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 if (wp != NULL && wp->w_width != Columns)
9570 {
9571 /* need to copy part of a line */
9572 j = end - 1 - i;
9573 while ((j -= line_count) >= row)
9574 linecopy(j + line_count, j, wp);
9575 j += line_count;
9576 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009577 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9578 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 else
9580 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9581 LineWraps[j] = FALSE;
9582 }
9583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 {
9585 j = end - 1 - i;
9586 temp = LineOffset[j];
9587 while ((j -= line_count) >= row)
9588 {
9589 LineOffset[j + line_count] = LineOffset[j];
9590 LineWraps[j + line_count] = LineWraps[j];
9591 }
9592 LineOffset[j + line_count] = temp;
9593 LineWraps[j + line_count] = FALSE;
9594 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009595 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 else
9597 lineinvalid(temp, (int)Columns);
9598 }
9599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600
9601 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009602 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009603 if (clear_attr != 0)
9604 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 /* redraw the characters */
9607 if (type == USE_REDRAW)
9608 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009609 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 {
9611 term_append_lines(line_count);
9612 screen_start(); /* don't know where cursor is now */
9613 }
9614 else
9615 {
9616 for (i = 0; i < line_count; i++)
9617 {
9618 if (type == USE_T_AL)
9619 {
9620 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009621 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 out_str(T_AL);
9623 }
9624 else /* type == USE_T_SR */
9625 out_str(T_SR);
9626 screen_start(); /* don't know where cursor is now */
9627 }
9628 }
9629
9630 /*
9631 * With scroll-reverse and 'da' flag set we need to clear the lines that
9632 * have been scrolled down into the region.
9633 */
9634 if (type == USE_T_SR && *T_DA)
9635 {
9636 for (i = 0; i < line_count; ++i)
9637 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009638 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 out_str(T_CE);
9640 screen_start(); /* don't know where cursor is now */
9641 }
9642 }
9643
9644#ifdef FEAT_GUI
9645 gui_can_update_cursor();
9646 if (gui.in_use)
9647 out_flush(); /* always flush after a scroll */
9648#endif
9649 return OK;
9650}
9651
9652/*
Bram Moolenaar107abd22016-08-12 14:08:25 +02009653 * Delete lines on the screen and update ScreenLines[].
9654 * "end" is the line after the scrolled part. Normally it is Rows.
9655 * When scrolling region used "off" is the offset from the top for the region.
9656 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 *
9658 * Return OK for success, FAIL if the lines are not deleted.
9659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009661screen_del_lines(
9662 int off,
9663 int row,
9664 int line_count,
9665 int end,
9666 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009667 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +01009668 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670 int j;
9671 int i;
9672 unsigned temp;
9673 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009674 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 int cursor_end;
9676 int result_empty; /* result is empty until end of region */
9677 int can_delete; /* deleting line codes can be used */
9678 int type;
9679
9680 /*
9681 * FAIL if
9682 * - there is no valid screen
9683 * - the screen has to be redrawn completely
9684 * - the line count is less than one
9685 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009686 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009688 if (!screen_valid(TRUE) || line_count <= 0
9689 || (!force && line_count > p_ttyscroll)
9690#ifdef FEAT_CLIPBOARD
9691 || (clip_star.state != SELECT_CLEARED
9692 && redrawing_for_callback > 0)
9693#endif
9694 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 return FAIL;
9696
9697 /*
9698 * Check if the rest of the current region will become empty.
9699 */
9700 result_empty = row + line_count >= end;
9701
9702 /*
9703 * We can delete lines only when 'db' flag not set or when 'ce' option
9704 * available.
9705 */
9706 can_delete = (*T_DB == NUL || can_clear(T_CE));
9707
9708 /*
9709 * There are six ways to delete lines:
9710 * 0. When in a vertically split window and t_CV isn't set, redraw the
9711 * characters from ScreenLines[].
9712 * 1. Use T_CD if it exists and the result is empty.
9713 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
9714 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
9715 * none of the other ways work.
9716 * 4. Use T_CE (erase line) if the result is empty.
9717 * 5. Use T_DL (delete line) if it exists.
9718 * 6. redraw the characters from ScreenLines[].
9719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9721 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009722 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 type = USE_T_CD;
9724#if defined(__BEOS__) && defined(BEOS_DR8)
9725 /*
9726 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
9727 * its internal termcap... this works okay for tests which test *T_DB !=
9728 * NUL. It has the disadvantage that the user cannot use any :set t_*
9729 * command to get T_DB (back) to empty_option, only :set term=... will do
9730 * the trick...
9731 * Anyway, this hack will hopefully go away with the next OS release.
9732 * (Olaf Seibert)
9733 */
9734 else if (row == 0 && T_DB == empty_option
9735 && (line_count == 1 || *T_CDL == NUL))
9736#else
9737 else if (row == 0 && (
9738#ifndef AMIGA
9739 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
9740 * up, so use delete-line command */
9741 line_count == 1 ||
9742#endif
9743 *T_CDL == NUL))
9744#endif
9745 type = USE_NL;
9746 else if (*T_CDL != NUL && line_count > 1 && can_delete)
9747 type = USE_T_CDL;
9748 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +02009749 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 type = USE_T_CE;
9751 else if (*T_DL != NUL && can_delete)
9752 type = USE_T_DL;
9753 else if (*T_CDL != NUL && can_delete)
9754 type = USE_T_CDL;
9755 else
9756 return FAIL;
9757
9758#ifdef FEAT_CLIPBOARD
9759 /* Remove a modeless selection when deleting lines halfway the screen or
9760 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009761 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009762 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 else
9764 clip_scroll_selection(line_count);
9765#endif
9766
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#ifdef FEAT_GUI
9768 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9769 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009770 gui_dont_update_cursor(gui.cursor_row >= row + off
9771 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772#endif
9773
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009774 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9775 cursor_col = wp->w_wincol;
9776
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 if (*T_CCS != NUL) /* cursor relative to region */
9778 {
9779 cursor_row = row;
9780 cursor_end = end;
9781 }
9782 else
9783 {
9784 cursor_row = row + off;
9785 cursor_end = end + off;
9786 }
9787
9788 /*
9789 * Now shift LineOffset[] line_count up to reflect the deleted lines.
9790 * Clear the inserted lines in ScreenLines[].
9791 */
9792 row += off;
9793 end += off;
9794 for (i = 0; i < line_count; ++i)
9795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 if (wp != NULL && wp->w_width != Columns)
9797 {
9798 /* need to copy part of a line */
9799 j = row + i;
9800 while ((j += line_count) <= end - 1)
9801 linecopy(j - line_count, j, wp);
9802 j -= line_count;
9803 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009804 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9805 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else
9807 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9808 LineWraps[j] = FALSE;
9809 }
9810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 {
9812 /* whole width, moving the line pointers is faster */
9813 j = row + i;
9814 temp = LineOffset[j];
9815 while ((j += line_count) <= end - 1)
9816 {
9817 LineOffset[j - line_count] = LineOffset[j];
9818 LineWraps[j - line_count] = LineWraps[j];
9819 }
9820 LineOffset[j - line_count] = temp;
9821 LineWraps[j - line_count] = FALSE;
9822 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009823 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 else
9825 lineinvalid(temp, (int)Columns);
9826 }
9827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009829 if (screen_attr != clear_attr)
9830 screen_stop_highlight();
9831 if (clear_attr != 0)
9832 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 /* redraw the characters */
9835 if (type == USE_REDRAW)
9836 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009837 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009839 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 out_str(T_CD);
9841 screen_start(); /* don't know where cursor is now */
9842 }
9843 else if (type == USE_T_CDL)
9844 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009845 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 term_delete_lines(line_count);
9847 screen_start(); /* don't know where cursor is now */
9848 }
9849 /*
9850 * Deleting lines at top of the screen or scroll region: Just scroll
9851 * the whole screen (scroll region) up by outputting newlines on the
9852 * last line.
9853 */
9854 else if (type == USE_NL)
9855 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009856 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 for (i = line_count; --i >= 0; )
9858 out_char('\n'); /* cursor will remain on same line */
9859 }
9860 else
9861 {
9862 for (i = line_count; --i >= 0; )
9863 {
9864 if (type == USE_T_DL)
9865 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009866 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 out_str(T_DL); /* delete a line */
9868 }
9869 else /* type == USE_T_CE */
9870 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009871 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 out_str(T_CE); /* erase a line */
9873 }
9874 screen_start(); /* don't know where cursor is now */
9875 }
9876 }
9877
9878 /*
9879 * If the 'db' flag is set, we need to clear the lines that have been
9880 * scrolled up at the bottom of the region.
9881 */
9882 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
9883 {
9884 for (i = line_count; i > 0; --i)
9885 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009886 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 out_str(T_CE); /* erase a line */
9888 screen_start(); /* don't know where cursor is now */
9889 }
9890 }
9891
9892#ifdef FEAT_GUI
9893 gui_can_update_cursor();
9894 if (gui.in_use)
9895 out_flush(); /* always flush after a scroll */
9896#endif
9897
9898 return OK;
9899}
9900
9901/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009902 * Return TRUE when postponing displaying the mode message: when not redrawing
9903 * or inside a mapping.
9904 */
9905 int
9906skip_showmode()
9907{
9908 // Call char_avail() only when we are going to show something, because it
9909 // takes a bit of time. redrawing() may also call char_avail_avail().
9910 if (global_busy
9911 || msg_silent != 0
9912 || !redrawing()
9913 || (char_avail() && !KeyTyped))
9914 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009915 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009916 return TRUE;
9917 }
9918 return FALSE;
9919}
9920
9921/*
Bram Moolenaar81226e02018-02-20 21:44:45 +01009922 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 *
9924 * If clear_cmdline is TRUE, clear the rest of the cmdline.
9925 * If clear_cmdline is FALSE there may be a message there that needs to be
9926 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +02009927 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 * Return the length of the message (0 if no message).
9929 */
9930 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009931showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932{
9933 int need_clear;
9934 int length = 0;
9935 int do_mode;
9936 int attr;
9937 int nwr_save;
9938#ifdef FEAT_INS_EXPAND
9939 int sub_attr;
9940#endif
9941
Bram Moolenaar7df351e2006-01-23 22:30:28 +00009942 do_mode = ((p_smd && msg_silent == 0)
9943 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +02009944 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009945 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009946 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +01009948 if (skip_showmode())
9949 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
9951 nwr_save = need_wait_return;
9952
9953 /* wait a bit before overwriting an important message */
9954 check_for_delay(FALSE);
9955
9956 /* if the cmdline is more than one line high, erase top lines */
9957 need_clear = clear_cmdline;
9958 if (clear_cmdline && cmdline_row < Rows - 1)
9959 msg_clr_cmdline(); /* will reset clear_cmdline */
9960
9961 /* Position on the last line in the window, column 0 */
9962 msg_pos_mode();
9963 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +01009964 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 if (do_mode)
9966 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009967 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +00009969 if (
Bram Moolenaar09092152010-08-08 16:38:42 +02009970# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +00009971 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +02009972# else
Bram Moolenaarc236c162008-07-13 17:41:49 +00009973 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +00009974# endif
Bram Moolenaar09092152010-08-08 16:38:42 +02009975 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +02009976# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +01009977 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978# else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009979 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980# endif
9981#endif
9982#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
9983 if (gui.in_use)
9984 {
9985 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009986 {
9987 /* HANGUL */
9988 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +01009989 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009990 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01009991 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +01009992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 }
9994#endif
9995#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +02009996 /* CTRL-X in Insert mode */
9997 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 {
9999 /* These messages can get long, avoid a wrap in a narrow
10000 * window. Prefer showing edit_submode_extra. */
10001 length = (Rows - msg_row) * Columns - 3;
10002 if (edit_submode_extra != NULL)
10003 length -= vim_strsize(edit_submode_extra);
10004 if (length > 0)
10005 {
10006 if (edit_submode_pre != NULL)
10007 length -= vim_strsize(edit_submode_pre);
10008 if (length - vim_strsize(edit_submode) > 0)
10009 {
10010 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010011 msg_puts_attr((char *)edit_submode_pre, attr);
10012 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 }
10014 if (edit_submode_extra != NULL)
10015 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010016 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010018 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 else
10020 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010021 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 }
10023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 }
10025 else
10026#endif
10027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010029 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010030 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010031 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else if (State & INSERT)
10033 {
10034#ifdef FEAT_RIGHTLEFT
10035 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010036 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010038 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010040 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010041 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010043 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010045 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046#ifdef FEAT_RIGHTLEFT
10047 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010048 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049#endif
10050#ifdef FEAT_KEYMAP
10051 if (State & LANGMAP)
10052 {
10053# ifdef FEAT_ARABIC
10054 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010055 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 else
10057# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010058 if (get_keymap_str(curwin, (char_u *)" (%s)",
10059 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010060 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062#endif
10063 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010064 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 if (VIsual_active)
10067 {
10068 char *p;
10069
10070 /* Don't concatenate separate words to avoid translation
10071 * problems. */
10072 switch ((VIsual_select ? 4 : 0)
10073 + (VIsual_mode == Ctrl_V) * 2
10074 + (VIsual_mode == 'V'))
10075 {
10076 case 0: p = N_(" VISUAL"); break;
10077 case 1: p = N_(" VISUAL LINE"); break;
10078 case 2: p = N_(" VISUAL BLOCK"); break;
10079 case 4: p = N_(" SELECT"); break;
10080 case 5: p = N_(" SELECT LINE"); break;
10081 default: p = N_(" SELECT BLOCK"); break;
10082 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010083 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010085 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010087
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 need_clear = TRUE;
10089 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010090 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091#ifdef FEAT_INS_EXPAND
10092 && edit_submode == NULL /* otherwise it gets too long */
10093#endif
10094 )
10095 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010096 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 need_clear = TRUE;
10098 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010099
10100 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010101 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 msg_clr_eos();
10103 msg_didout = FALSE; /* overwrite this message */
10104 length = msg_col;
10105 msg_col = 0;
10106 need_wait_return = nwr_save; /* never ask for hit-return for this */
10107 }
10108 else if (clear_cmdline && msg_silent == 0)
10109 /* Clear the whole command line. Will reset "clear_cmdline". */
10110 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010111 else if (redraw_mode)
10112 {
10113 msg_pos_mode();
10114 msg_clr_eos();
10115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
10117#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 /* In Visual mode the size of the selected area must be redrawn. */
10119 if (VIsual_active)
10120 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121
10122 /* If the last window has no status line, the ruler is after the mode
10123 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010124 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010125 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#endif
10127 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010128 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 clear_cmdline = FALSE;
10130
10131 return length;
10132}
10133
10134/*
10135 * Position for a mode message.
10136 */
10137 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010138msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140 msg_col = 0;
10141 msg_row = Rows - 1;
10142}
10143
10144/*
10145 * Delete mode message. Used when ESC is typed which is expected to end
10146 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010147 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 */
10149 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010150unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151{
10152 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010153 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 */
10155 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10156 redraw_cmdline = TRUE; /* delete mode later */
10157 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010158 clearmode();
10159}
10160
10161/*
10162 * Clear the mode message.
10163 */
10164 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010165clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010166{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010167 int save_msg_row = msg_row;
10168 int save_msg_col = msg_col;
10169
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010170 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010171 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010172 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010173 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010174
10175 msg_col = save_msg_col;
10176 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177}
10178
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010179 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010180recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010181{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010182 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010183 if (!shortmess(SHM_RECORDING))
10184 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010185 char s[4];
10186
10187 sprintf(s, " @%c", reg_recording);
10188 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010189 }
10190}
10191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010192/*
10193 * Draw the tab pages line at the top of the Vim window.
10194 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010195 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010196draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010197{
10198 int tabcount = 0;
10199 tabpage_T *tp;
10200 int tabwidth;
10201 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010202 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010203 int attr;
10204 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010205 win_T *cwp;
10206 int wincount;
10207 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010208 int c;
10209 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010210 int attr_sel = HL_ATTR(HLF_TPS);
10211 int attr_nosel = HL_ATTR(HLF_TP);
10212 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010213 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010214 int room;
10215 int use_sep_chars = (t_colors < 8
10216#ifdef FEAT_GUI
10217 && !gui.in_use
10218#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010219#ifdef FEAT_TERMGUICOLORS
10220 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010221#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010222 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010223
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010224 if (ScreenLines == NULL)
10225 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010226 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010227
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010228#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010229 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010230 if (gui_use_tabline())
10231 {
10232 gui_update_tabline();
10233 return;
10234 }
10235#endif
10236
10237 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010238 return;
10239
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010240#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010241 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010242
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010243 /* Use the 'tabline' option if it's set. */
10244 if (*p_tal != NUL)
10245 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010246 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010247
10248 /* Check for an error. If there is one we would loop in redrawing the
10249 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010250 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010251 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010252 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010253 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010254 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010255 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010256 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010257 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010258#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010259 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010260 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010261 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010262
Bram Moolenaar238a5642006-02-21 22:12:05 +000010263 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10264 if (tabwidth < 6)
10265 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010266
Bram Moolenaar238a5642006-02-21 22:12:05 +000010267 attr = attr_nosel;
10268 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010269 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10270 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010271 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010272 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010273
Bram Moolenaar238a5642006-02-21 22:12:05 +000010274 if (tp->tp_topframe == topframe)
10275 attr = attr_sel;
10276 if (use_sep_chars && col > 0)
10277 screen_putchar('|', 0, col++, attr);
10278
10279 if (tp->tp_topframe != topframe)
10280 attr = attr_nosel;
10281
10282 screen_putchar(' ', 0, col++, attr);
10283
10284 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010285 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010286 cwp = curwin;
10287 wp = firstwin;
10288 }
10289 else
10290 {
10291 cwp = tp->tp_curwin;
10292 wp = tp->tp_firstwin;
10293 }
10294
10295 modified = FALSE;
10296 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10297 if (bufIsChanged(wp->w_buffer))
10298 modified = TRUE;
10299 if (modified || wincount > 1)
10300 {
10301 if (wincount > 1)
10302 {
10303 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010304 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010305 if (col + len >= Columns - 3)
10306 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010307 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010308#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010309 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010310#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010311 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010312#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010313 );
10314 col += len;
10315 }
10316 if (modified)
10317 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10318 screen_putchar(' ', 0, col++, attr);
10319 }
10320
10321 room = scol - col + tabwidth - 1;
10322 if (room > 0)
10323 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010324 /* Get buffer name in NameBuff[] */
10325 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010326 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010327 len = vim_strsize(NameBuff);
10328 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010329 if (has_mbyte)
10330 while (len > room)
10331 {
10332 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010333 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010334 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010335 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010336 {
10337 p += len - room;
10338 len = room;
10339 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010340 if (len > Columns - col - 1)
10341 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010343 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010344 col += len;
10345 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010346 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010347
10348 /* Store the tab page number in TabPageIdxs[], so that
10349 * jump_to_mouse() knows where each one is. */
10350 ++tabcount;
10351 while (scol < col)
10352 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010353 }
10354
Bram Moolenaar238a5642006-02-21 22:12:05 +000010355 if (use_sep_chars)
10356 c = '_';
10357 else
10358 c = ' ';
10359 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010360
10361 /* Put an "X" for closing the current tab if there are several. */
10362 if (first_tabpage->tp_next != NULL)
10363 {
10364 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10365 TabPageIdxs[Columns - 1] = -999;
10366 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010367 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010368
10369 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10370 * set. */
10371 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010372}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010373
10374/*
10375 * Get buffer name for "buf" into NameBuff[].
10376 * Takes care of special buffer names and translates special characters.
10377 */
10378 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010379get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010380{
10381 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010382 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010383 else
10384 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10385 trans_characters(NameBuff, MAXPATHL);
10386}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010387
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388/*
10389 * Get the character to use in a status line. Get its attributes in "*attr".
10390 */
10391 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010392fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
10394 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010395
10396#ifdef FEAT_TERMINAL
10397 if (bt_terminal(wp->w_buffer))
10398 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010399 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010400 {
10401 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010402 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010403 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010404 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010405 {
10406 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010407 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010408 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010409 }
10410 else
10411#endif
10412 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010414 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 fill = fill_stl;
10416 }
10417 else
10418 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010419 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 fill = fill_stlnc;
10421 }
10422 /* Use fill when there is highlighting, and highlighting of current
10423 * window differs, or the fillchars differ, or this is not the
10424 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010425 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010426 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 || (fill_stl != fill_stlnc)))
10428 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010429 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 return '^';
10431 return '=';
10432}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434/*
10435 * Get the character to use in a separator between vertically split windows.
10436 * Get its attributes in "*attr".
10437 */
10438 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010439fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010441 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442 if (*attr == 0 && fill_vert == ' ')
10443 return '|';
10444 else
10445 return fill_vert;
10446}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447
10448/*
10449 * Return TRUE if redrawing should currently be done.
10450 */
10451 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010452redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010454#ifdef FEAT_EVAL
10455 if (disable_redraw_for_testing)
10456 return 0;
10457 else
10458#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010459 return ((!RedrawingDisabled
10460#ifdef FEAT_EVAL
10461 || ignore_redraw_flag_for_testing
10462#endif
10463 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
10467 * Return TRUE if printing messages should currently be done.
10468 */
10469 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010470messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
10472 return (!(p_lz && char_avail() && !KeyTyped));
10473}
10474
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010475#ifdef FEAT_MENU
10476/*
10477 * Draw the window toolbar.
10478 */
10479 static void
10480redraw_win_toolbar(win_T *wp)
10481{
10482 vimmenu_T *menu;
10483 int item_idx = 0;
10484 int item_count = 0;
10485 int col = 0;
10486 int next_col;
10487 int off = (int)(current_ScreenLine - ScreenLines);
10488 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10489 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10490
10491 vim_free(wp->w_winbar_items);
10492 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10493 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010494 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010495
10496 /* TODO: use fewer spaces if there is not enough room */
10497 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010498 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010499 {
10500 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010501 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010502 break;
10503 if (col > 1)
10504 {
10505 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010506 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010507 break;
10508 }
10509
10510 wp->w_winbar_items[item_idx].wb_startcol = col;
10511 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010512 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010513 break;
10514
10515 next_col = text_to_screenline(wp, menu->name, col);
10516 while (col < next_col)
10517 {
10518 ScreenAttrs[off + col] = button_attr;
10519 ++col;
10520 }
10521 wp->w_winbar_items[item_idx].wb_endcol = col;
10522 wp->w_winbar_items[item_idx].wb_menu = menu;
10523 ++item_idx;
10524
Bram Moolenaar02631462017-09-22 15:20:32 +020010525 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010526 break;
10527 space_to_screenline(off + col, button_attr);
10528 ++col;
10529 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010530 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010531 {
10532 space_to_screenline(off + col, fill_attr);
10533 ++col;
10534 }
10535 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10536
Bram Moolenaar02631462017-09-22 15:20:32 +020010537 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010538 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010539}
10540#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010541
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542/*
10543 * Show current status info in ruler and various other places
10544 * If always is FALSE, only show ruler if position has changed.
10545 */
10546 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010547showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
10549 if (!always && !redrawing())
10550 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010551#ifdef FEAT_INS_EXPAND
10552 if (pum_visible())
10553 {
10554 /* Don't redraw right now, do it later. */
10555 curwin->w_redr_status = TRUE;
10556 return;
10557 }
10558#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010559#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010560 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010561 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 else
10563#endif
10564#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010565 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566#endif
10567
10568#ifdef FEAT_TITLE
10569 if (need_maketitle
10570# ifdef FEAT_STL_OPT
10571 || (p_icon && (stl_syntax & STL_IN_ICON))
10572 || (p_title && (stl_syntax & STL_IN_TITLE))
10573# endif
10574 )
10575 maketitle();
10576#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010577 /* Redraw the tab pages line if needed. */
10578 if (redraw_tabline)
10579 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580}
10581
10582#ifdef FEAT_CMDL_INFO
10583 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010584win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010586#define RULER_BUF_LEN 70
10587 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 int row;
10589 int fillchar;
10590 int attr;
10591 int empty_line = FALSE;
10592 colnr_T virtcol;
10593 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010594 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 int this_ru_col;
10597 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010598 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599
10600 /* If 'ruler' off or redrawing disabled, don't do anything */
10601 if (!p_ru)
10602 return;
10603
10604 /*
10605 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10606 * after deleting lines, before cursor.lnum is corrected.
10607 */
10608 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10609 return;
10610
10611#ifdef FEAT_INS_EXPAND
10612 /* Don't draw the ruler while doing insert-completion, it might overwrite
10613 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 if (edit_submode != NULL)
10616 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010617 // Don't draw the ruler when the popup menu is visible, it may overlap.
10618 // Except when the popup menu will be redrawn anyway.
10619 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010620 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621#endif
10622
10623#ifdef FEAT_STL_OPT
10624 if (*p_ruf)
10625 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010626 int save_called_emsg = called_emsg;
10627
10628 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 if (called_emsg)
10631 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010632 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010633 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 return;
10635 }
10636#endif
10637
10638 /*
10639 * Check if not in Insert mode and the line is empty (will show "0-1").
10640 */
10641 if (!(State & INSERT)
10642 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
10643 empty_line = TRUE;
10644
10645 /*
10646 * Only draw the ruler when something changed.
10647 */
10648 validate_virtcol_win(wp);
10649 if ( redraw_cmdline
10650 || always
10651 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
10652 || wp->w_cursor.col != wp->w_ru_cursor.col
10653 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 || wp->w_topline != wp->w_ru_topline
10656 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
10657#ifdef FEAT_DIFF
10658 || wp->w_topfill != wp->w_ru_topfill
10659#endif
10660 || empty_line != wp->w_ru_empty)
10661 {
10662 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 if (wp->w_status_height)
10664 {
10665 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010666 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020010667 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020010668 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 }
10670 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671 {
10672 row = Rows - 1;
10673 fillchar = ' ';
10674 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 width = Columns;
10676 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 }
10678
10679 /* In list mode virtcol needs to be recomputed */
10680 virtcol = wp->w_virtcol;
10681 if (wp->w_p_list && lcs_tab1 == NUL)
10682 {
10683 wp->w_p_list = FALSE;
10684 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
10685 wp->w_p_list = TRUE;
10686 }
10687
10688 /*
10689 * Some sprintfs return the length, some return a pointer.
10690 * To avoid portability problems we use strlen() here.
10691 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010692 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
10694 ? 0L
10695 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010696 len = STRLEN(buffer);
10697 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 empty_line ? 0 : (int)wp->w_cursor.col + 1,
10699 (int)virtcol + 1);
10700
10701 /*
10702 * Add a "50%" if there is room for it.
10703 * On the last line, don't print in the last column (scrolls the
10704 * screen up on some terminals).
10705 */
10706 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010707 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711 this_ru_col = ru_col - (Columns - width);
10712 if (this_ru_col < 0)
10713 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 /* Never use more than half the window/screen width, leave the other
10715 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010716 if (this_ru_col < (width + 1) / 2)
10717 this_ru_col = (width + 1) / 2;
10718 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010010720 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010721 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 if (has_mbyte)
10724 i += (*mb_char2bytes)(fillchar, buffer + i);
10725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 buffer[i++] = fillchar;
10727 ++o;
10728 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010729 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 }
10731 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 if (has_mbyte)
10733 {
10734 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010735 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 {
10737 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010738 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 {
10740 buffer[i] = NUL;
10741 break;
10742 }
10743 }
10744 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010745 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020010746 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747
Bram Moolenaar4033c552017-09-16 20:54:51 +020010748 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 i = redraw_cmdline;
10750 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020010751 this_ru_col + off + (int)STRLEN(buffer),
10752 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 fillchar, fillchar, attr);
10754 /* don't redraw the cmdline because of showing the ruler */
10755 redraw_cmdline = i;
10756 wp->w_ru_cursor = wp->w_cursor;
10757 wp->w_ru_virtcol = wp->w_virtcol;
10758 wp->w_ru_empty = empty_line;
10759 wp->w_ru_topline = wp->w_topline;
10760 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
10761#ifdef FEAT_DIFF
10762 wp->w_ru_topfill = wp->w_topfill;
10763#endif
10764 }
10765}
10766#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010767
10768#if defined(FEAT_LINEBREAK) || defined(PROTO)
10769/*
Bram Moolenaar64486672010-05-16 15:46:46 +020010770 * Return the width of the 'number' and 'relativenumber' column.
10771 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010772 * Otherwise it depends on 'numberwidth' and the line count.
10773 */
10774 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010775number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010776{
10777 int n;
10778 linenr_T lnum;
10779
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020010780 if (wp->w_p_rnu && !wp->w_p_nu)
10781 /* cursor line shows "0" */
10782 lnum = wp->w_height;
10783 else
10784 /* cursor line shows absolute line number */
10785 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020010786
Bram Moolenaar6b314672015-03-20 15:42:10 +010010787 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010788 return wp->w_nrwidth_width;
10789 wp->w_nrwidth_line_count = lnum;
10790
10791 n = 0;
10792 do
10793 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000010794 lnum /= 10;
10795 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010796 } while (lnum > 0);
10797
10798 /* 'numberwidth' gives the minimal width plus one */
10799 if (n < wp->w_p_nuw - 1)
10800 n = wp->w_p_nuw - 1;
10801
Bram Moolenaare4b407f2019-07-04 11:59:28 +020010802# ifdef FEAT_SIGNS
10803 // If 'signcolumn' is set to 'number' and there is a sign to display, then
10804 // the minimal width for the number column is 2.
10805 if (n < 2 && (wp->w_buffer->b_signlist != NULL)
10806 && (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u'))
10807 n = 2;
10808# endif
10809
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010810 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010010811 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000010812 return n;
10813}
10814#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010815
Bram Moolenaar113e1072019-01-20 15:30:40 +010010816#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010817/*
10818 * Return the current cursor column. This is the actual position on the
10819 * screen. First column is 0.
10820 */
10821 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010822screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010823{
10824 return screen_cur_col;
10825}
10826
10827/*
10828 * Return the current cursor row. This is the actual position on the screen.
10829 * First row is 0.
10830 */
10831 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010832screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010010833{
10834 return screen_cur_row;
10835}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010836#endif