blob: 62f9bd10368c9abd154d93fff67d34d5e8316f3c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram 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 Moolenaarbaaa7e92016-01-29 22:47:03 +0100126static void win_draw_end(win_T *wp, int c1, int c2, 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 Moolenaarde993ea2014-06-17 23:18:01 +0200138# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100139static void start_search_hl(void);
140static void end_search_hl(void);
141static void init_search_hl(win_T *wp);
142static void prepare_search_hl(win_T *wp, linenr_T lnum);
143static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
144static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100146static void screen_char(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100148static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200151static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100152static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200153static 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 +0100154static void win_rest_invalid(win_T *wp);
155static void msg_pos_mode(void);
156static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200157static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100158static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200159#ifdef FEAT_MENU
160static void redraw_win_toolbar(win_T *wp);
161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100163static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200166static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#endif
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169/* Ugly global: overrule attribute used by screen_char() */
170static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200172#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
173/* Can limit syntax highlight time to 'redrawtime'. */
174# define SYN_TIME_LIMIT 1
175#endif
176
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200177#ifdef FEAT_RIGHTLEFT
178# define HAS_RIGHTLEFT(x) x
179#else
180# define HAS_RIGHTLEFT(x) FALSE
181#endif
182
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183/*
184 * Redraw the current window later, with update_screen(type).
185 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100186 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 */
188 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100189redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
191 redraw_win_later(curwin, type);
192}
193
194 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100195redraw_win_later(
196 win_T *wp,
197 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200199 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {
201 wp->w_redr_type = type;
202 if (type >= NOT_VALID)
203 wp->w_lines_valid = 0;
204 if (must_redraw < type) /* must_redraw is the maximum of all windows */
205 must_redraw = type;
206 }
207}
208
209/*
210 * Force a complete redraw later. Also resets the highlighting. To be used
211 * after executing a shell command that messes up the screen.
212 */
213 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100214redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000217#ifdef FEAT_GUI
218 if (gui.in_use)
219 /* Use a code that will reset gui.highlight_mask in
220 * gui_stop_highlight(). */
221 screen_attr = HL_ALL + 1;
222 else
223#endif
224 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200225 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226}
227
228/*
229 * Mark all windows to be redrawn later.
230 */
231 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100232redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 win_T *wp;
235
236 FOR_ALL_WINDOWS(wp)
237 {
238 redraw_win_later(wp, type);
239 }
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100240 // This may be needed when switching tabs.
241 if (must_redraw < type)
242 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243}
244
245/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000246 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 */
248 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100249redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250{
251 redraw_buf_later(curbuf, type);
252}
253
254 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100255redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
257 win_T *wp;
258
259 FOR_ALL_WINDOWS(wp)
260 {
261 if (wp->w_buffer == buf)
262 redraw_win_later(wp, type);
263 }
264}
265
Bram Moolenaar113e1072019-01-20 15:30:40 +0100266#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200267 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100268redraw_buf_line_later(buf_T *buf, linenr_T lnum)
269{
270 win_T *wp;
271
272 FOR_ALL_WINDOWS(wp)
273 if (wp->w_buffer == buf && lnum >= wp->w_topline
274 && lnum < wp->w_botline)
275 redrawWinline(wp, lnum);
276}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100277#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100278
Bram Moolenaar113e1072019-01-20 15:30:40 +0100279#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100280 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200281redraw_buf_and_status_later(buf_T *buf, int type)
282{
283 win_T *wp;
284
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200285#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200286 if (wild_menu_showing != 0)
287 /* Don't redraw while the command line completion is displayed, it
288 * would disappear. */
289 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200290#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200291 FOR_ALL_WINDOWS(wp)
292 {
293 if (wp->w_buffer == buf)
294 {
295 redraw_win_later(wp, type);
296 wp->w_redr_status = TRUE;
297 }
298 }
299}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100300#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200301
Bram Moolenaar113e1072019-01-20 15:30:40 +0100302#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200304 * Redraw as soon as possible. When the command line is not scrolled redraw
305 * right away and restore what was on the command line.
306 * Return a code indicating what happened.
307 */
308 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100309redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200310{
311 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200312 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313 int r;
314 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200315 schar_T *screenline; /* copy from ScreenLines[] */
316 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200317#ifdef FEAT_MBYTE
318 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200319 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322#endif
323
324 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200325 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200326 return ret;
327
328 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200329 rows = screen_Rows - cmdline_row;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200330 screenline = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200331 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200332 screenattr = (sattr_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200333 (long_u)(rows * cols * sizeof(sattr_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200334 if (screenline == NULL || screenattr == NULL)
335 ret = 2;
336#ifdef FEAT_MBYTE
337 if (enc_utf8)
338 {
339 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200340 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200341 if (screenlineUC == NULL)
342 ret = 2;
343 for (i = 0; i < p_mco; ++i)
344 {
345 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200346 (long_u)(rows * cols * sizeof(u8char_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200347 if (screenlineC[i] == NULL)
348 ret = 2;
349 }
350 }
351 if (enc_dbcs == DBCS_JPNU)
352 {
353 screenline2 = (schar_T *)lalloc(
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200354 (long_u)(rows * cols * sizeof(schar_T)), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355 if (screenline2 == NULL)
356 ret = 2;
357 }
358#endif
359
360 if (ret != 2)
361 {
362 /* Save the text displayed in the command line area. */
363 for (r = 0; r < rows; ++r)
364 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200365 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200366 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 (size_t)cols * sizeof(schar_T));
368 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200369 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200370 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200371#ifdef FEAT_MBYTE
372 if (enc_utf8)
373 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200374 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200375 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 mch_memmove(screenlineC[i] + r * cols,
379 ScreenLinesC[i] + LineOffset[cmdline_row + r],
380 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381 }
382 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200383 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200384 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200385 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200386#endif
387 }
388
389 update_screen(0);
390 ret = 3;
391
392 if (must_redraw == 0)
393 {
394 int off = (int)(current_ScreenLine - ScreenLines);
395
396 /* Restore the text displayed in the command line area. */
397 for (r = 0; r < rows; ++r)
398 {
399 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenline + r * cols,
401 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200403 screenattr + r * cols,
404 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200405#ifdef FEAT_MBYTE
406 if (enc_utf8)
407 {
408 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenlineUC + r * cols,
410 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200411 for (i = 0; i < p_mco; ++i)
412 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200413 screenlineC[i] + r * cols,
414 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416 if (enc_dbcs == DBCS_JPNU)
417 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200418 screenline2 + r * cols,
419 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200420#endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200421 screen_line(cmdline_row + r, 0, cols, cols, FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200422 }
423 ret = 4;
424 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200425 }
426
427 vim_free(screenline);
428 vim_free(screenattr);
429#ifdef FEAT_MBYTE
430 if (enc_utf8)
431 {
432 vim_free(screenlineUC);
433 for (i = 0; i < p_mco; ++i)
434 vim_free(screenlineC[i]);
435 }
436 if (enc_dbcs == DBCS_JPNU)
437 vim_free(screenline2);
438#endif
439
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200440 /* Show the intro message when appropriate. */
441 maybe_intro_message();
442
443 setcursor();
444
Bram Moolenaar2951b772013-07-03 12:45:31 +0200445 return ret;
446}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100447#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200448
449/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100450 * Invoked after an asynchronous callback is called.
451 * If an echo command was used the cursor needs to be put back where
452 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200453 * If "call_update_screen" is FALSE don't call update_screen() when at the
454 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100455 */
456 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200457redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100458{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200459 ++redrawing_for_callback;
460
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100461 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100463 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200464 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200465 // Don't redraw when in prompt_for_number().
466 if (cmdline_row > 0)
467 {
468 // Redrawing only works when the screen didn't scroll. Don't clear
469 // wildmenu entries.
470 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200471#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200473#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200474 && call_update_screen)
475 update_screen(0);
476
477 // Redraw in the same position, so that the user can continue
478 // editing the command.
479 redrawcmdline_ex(FALSE);
480 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200481 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200482 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100483 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200484 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200485 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100486 setcursor();
487 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100488 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100489#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100490 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200491 // Don't update the cursor when it is blinking and off to avoid
492 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100493 out_flush_cursor(FALSE, FALSE);
494 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100495#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100496 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200497
498 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100499}
500
501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 * Changed something in the current window, at buffer line "lnum", that
503 * requires that line and possibly other lines to be redrawn.
504 * Used when entering/leaving Insert mode with the cursor on a folded line.
505 * Used to remove the "$" from a change command.
506 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
507 * may become invalid and the whole window will have to be redrawn.
508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100510redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200511 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100512 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200514 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
515 wp->w_redraw_top = lnum;
516 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
517 wp->w_redraw_bot = lnum;
518 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200521 void
522reset_updating_screen(int may_resize_shell UNUSED)
523{
524 updating_screen = FALSE;
525#ifdef FEAT_GUI
526 if (may_resize_shell)
527 gui_may_resize_shell();
528#endif
529#ifdef FEAT_TERMINAL
530 term_check_channel_closed_recently();
531#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200532
533#ifdef HAVE_DROP_FILE
534 // If handle_drop() was called while updating_screen was TRUE need to
535 // handle the drop now.
536 handle_any_postponed_drop();
537#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200538}
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200541 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
543 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100544update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 redraw_curbuf_later(type);
547 update_screen(type);
548}
549
550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 * Based on the current value of curwin->w_topline, transfer a screenfull
552 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200553 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200555 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200556update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200558 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 win_T *wp;
560 static int did_intro = FALSE;
561#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
562 int did_one;
563#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200564#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200565 int did_undraw = FALSE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200566 int gui_cursor_col;
567 int gui_cursor_row;
568#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200569 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000571 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200575 if (type == VALID_NO_UPDATE)
576 {
577 no_update = TRUE;
578 type = 0;
579 }
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (must_redraw)
582 {
583 if (type < must_redraw) /* use maximal type */
584 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000585
586 /* must_redraw is reset here, so that when we run into some weird
587 * reason to redraw while busy redrawing (e.g., asynchronous
588 * scrolling), or update_topline() in win_update() will cause a
589 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 must_redraw = 0;
591 }
592
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200593 /* May need to update w_lines[]. */
594 if (curwin->w_lines_valid == 0 && type < NOT_VALID
595#ifdef FEAT_TERMINAL
596 && !term_do_update_window(curwin)
597#endif
598 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 type = NOT_VALID;
600
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000601 /* Postpone the redrawing when it's not needed and when being called
602 * recursively. */
603 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 redraw_later(type); /* remember type for next time */
606 must_redraw = type;
607 if (type > INVERTED_ALL)
608 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611
612 updating_screen = TRUE;
613#ifdef FEAT_SYN_HL
614 ++display_tick; /* let syntax code know we're in a next round of
615 * display updating */
616#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200617 if (no_update)
618 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 /*
621 * if the screen was scrolled up when displaying a message, scroll it down
622 */
623 if (msg_scrolled)
624 {
625 clear_cmdline = TRUE;
626 if (msg_scrolled > Rows - 5) /* clearing is faster */
627 type = CLEAR;
628 else if (type != CLEAR)
629 {
630 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200631 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
632 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 type = CLEAR;
634 FOR_ALL_WINDOWS(wp)
635 {
636 if (W_WINROW(wp) < msg_scrolled)
637 {
638 if (W_WINROW(wp) + wp->w_height > msg_scrolled
639 && wp->w_redr_type < REDRAW_TOP
640 && wp->w_lines_valid > 0
641 && wp->w_topline == wp->w_lines[0].wl_lnum)
642 {
643 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
644 wp->w_redr_type = REDRAW_TOP;
645 }
646 else
647 {
648 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200649 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
650 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 }
654 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200655 if (!no_update)
656 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000657 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 msg_scrolled = 0;
660 need_wait_return = FALSE;
661 }
662
663 /* reset cmdline_row now (may have been changed temporarily) */
664 compute_cmdrow();
665
666 /* Check for changed highlighting */
667 if (need_highlight_changed)
668 highlight_changed();
669
670 if (type == CLEAR) /* first clear screen */
671 {
672 screenclear(); /* will reset clear_cmdline */
673 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200674 /* must_redraw may be set indirectly, avoid another redraw later */
675 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
677
678 if (clear_cmdline) /* going to clear cmdline (done below) */
679 check_for_delay(FALSE);
680
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000681#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200682 /* Force redraw when width of 'number' or 'relativenumber' column
683 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000684 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200685 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
686 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000687 curwin->w_redr_type = NOT_VALID;
688#endif
689
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 /*
691 * Only start redrawing if there is really something to do.
692 */
693 if (type == INVERTED)
694 update_curswant();
695 if (curwin->w_redr_type < type
696 && !((type == VALID
697 && curwin->w_lines[0].wl_valid
698#ifdef FEAT_DIFF
699 && curwin->w_topfill == curwin->w_old_topfill
700 && curwin->w_botfill == curwin->w_old_botfill
701#endif
702 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000704 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
706 && curwin->w_old_visual_mode == VIsual_mode
707 && (curwin->w_valid & VALID_VIRTCOL)
708 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 ))
710 curwin->w_redr_type = type;
711
Bram Moolenaar5a305422006-04-28 22:38:25 +0000712 /* Redraw the tab pages line if needed. */
713 if (redraw_tabline || type >= NOT_VALID)
714 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_SYN_HL
717 /*
718 * Correct stored syntax highlighting info for changes in each displayed
719 * buffer. Each buffer must only be done once.
720 */
721 FOR_ALL_WINDOWS(wp)
722 {
723 if (wp->w_buffer->b_mod_set)
724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 win_T *wwp;
726
727 /* Check if we already did this buffer. */
728 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
729 if (wwp->w_buffer == wp->w_buffer)
730 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200731 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 syn_stack_apply_changes(wp->w_buffer);
733 }
734 }
735#endif
736
737 /*
738 * Go from top to bottom through the windows, redrawing the ones that need
739 * it.
740 */
741#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
742 did_one = FALSE;
743#endif
744#ifdef FEAT_SEARCH_EXTRA
745 search_hl.rm.regprog = NULL;
746#endif
747 FOR_ALL_WINDOWS(wp)
748 {
749 if (wp->w_redr_type != 0)
750 {
751 cursor_off();
752#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
753 if (!did_one)
754 {
755 did_one = TRUE;
756# ifdef FEAT_SEARCH_EXTRA
757 start_search_hl();
758# endif
759# ifdef FEAT_CLIPBOARD
760 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200761 if (clip_star.available && clip_isautosel_star())
762 clip_update_selection(&clip_star);
763 if (clip_plus.available && clip_isautosel_plus())
764 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765# endif
766#ifdef FEAT_GUI
767 /* Remove the cursor before starting to do anything, because
768 * scrolling may make it difficult to redraw the text under
769 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200770 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200771 {
772 gui_cursor_col = gui.cursor_col;
773 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 }
779#endif
780 win_update(wp);
781 }
782
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 /* redraw status line after the window to minimize cursor movement */
784 if (wp->w_redr_status)
785 {
786 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200787 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
790#if defined(FEAT_SEARCH_EXTRA)
791 end_search_hl();
792#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100793#ifdef FEAT_INS_EXPAND
794 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200795 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /* Reset b_mod_set flags. Going through all windows is probably faster
799 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200800 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200803 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /* Clear or redraw the command line. Done last, because scrolling may
806 * mess up the command line. */
807 if (clear_cmdline || redraw_cmdline)
808 showmode();
809
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200810 if (no_update)
811 --no_win_do_lines_ins;
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200814 if (!did_intro)
815 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 did_intro = TRUE;
817
818#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# ifdef FEAT_MBYTE
834 gui.col = mb_fix_col(gui.col, gui.row);
835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200838 screen_cur_col = gui.col;
839 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200840 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100841 else
842 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 gui_update_scrollbars(FALSE);
844 }
845#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200846 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847}
848
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100849#if defined(FEAT_SIGNS) || defined(FEAT_GUI) || defined(FEAT_CONCEAL)
850/*
851 * Prepare for updating one or more windows.
852 * Caller must check for "updating_screen" already set to avoid recursiveness.
853 */
854 static void
855update_prepare(void)
856{
857 cursor_off();
858 updating_screen = TRUE;
859#ifdef FEAT_GUI
860 /* Remove the cursor before starting to do anything, because scrolling may
861 * make it difficult to redraw the text under it. */
862 if (gui.in_use)
863 gui_undraw_cursor();
864#endif
865#ifdef FEAT_SEARCH_EXTRA
866 start_search_hl();
867#endif
868}
869
870/*
871 * Finish updating one or more windows.
872 */
873 static void
874update_finish(void)
875{
876 if (redraw_cmdline)
877 showmode();
878
879# ifdef FEAT_SEARCH_EXTRA
880 end_search_hl();
881# endif
882
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200883 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100884
885# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100886 /* Redraw the cursor and update the scrollbars when all screen updating is
887 * done. */
888 if (gui.in_use)
889 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100890 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100891 gui_update_scrollbars(FALSE);
892 }
893# endif
894}
895#endif
896
Bram Moolenaar860cae12010-06-05 23:22:07 +0200897#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200898/*
899 * Return TRUE if the cursor line in window "wp" may be concealed, according
900 * to the 'concealcursor' option.
901 */
902 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100903conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904{
905 int c;
906
907 if (*wp->w_p_cocu == NUL)
908 return FALSE;
909 if (get_real_state() & VISUAL)
910 c = 'v';
911 else if (State & INSERT)
912 c = 'i';
913 else if (State & NORMAL)
914 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200915 else if (State & CMDLINE)
916 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200917 else
918 return FALSE;
919 return vim_strchr(wp->w_p_cocu, c) != NULL;
920}
921
922/*
923 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
924 */
925 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200926conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200927{
928 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
929 {
930 need_cursor_line_redraw = TRUE;
931 /* Need to recompute cursor column, e.g., when starting Visual mode
932 * without concealing. */
933 curs_columns(TRUE);
934 }
935}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936#endif
937
Bram Moolenaar113e1072019-01-20 15:30:40 +0100938#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100940update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
942 win_T *wp;
943 int doit = FALSE;
944
945# ifdef FEAT_FOLDING
946 win_foldinfo.fi_level = 0;
947# endif
948
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100949 // update/delete a specific sign
950 redraw_buf_line_later(buf, lnum);
951
952 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (wp->w_redr_type != 0)
955 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100957 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200958 * happening (recursive call), messages on the screen or still starting up.
959 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100960 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200961 || State == ASKMORE || State == HITRETURN
962 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100963#ifdef FEAT_GUI
964 || gui.starting
965#endif
966 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 return;
968
969 /* update all windows that need updating */
970 update_prepare();
971
Bram Moolenaar29323592016-07-24 22:04:11 +0200972 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 {
974 if (wp->w_redr_type != 0)
975 win_update(wp);
976 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200977 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
980 update_finish();
981}
982#endif
983
984
985#if defined(FEAT_GUI) || defined(PROTO)
986/*
987 * Update a single window, its status line and maybe the command line msg.
988 * Used for the GUI scrollbar.
989 */
990 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100991updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992{
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000993 /* return if already busy updating */
994 if (updating_screen)
995 return;
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 update_prepare();
998
999#ifdef FEAT_CLIPBOARD
1000 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001001 if (clip_star.available && clip_isautosel_star())
1002 clip_update_selection(&clip_star);
1003 if (clip_plus.available && clip_isautosel_plus())
1004 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001008
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001009 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001010 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001011 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (wp->w_redr_status
1014# ifdef FEAT_CMDL_INFO
1015 || p_ru
1016# endif
1017# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001018 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019# endif
1020 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001021 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
1023 update_finish();
1024}
1025#endif
1026
1027/*
1028 * Update a single window.
1029 *
1030 * This may cause the windows below it also to be redrawn (when clearing the
1031 * screen or scrolling lines).
1032 *
1033 * How the window is redrawn depends on wp->w_redr_type. Each type also
1034 * implies the one below it.
1035 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001036 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1038 * INVERTED redraw the changed part of the Visual area
1039 * INVERTED_ALL redraw the whole Visual area
1040 * VALID 1. scroll up/down to adjust for a changed w_topline
1041 * 2. update lines at the top when scrolled down
1042 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001043 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 * b_mod_top and b_mod_bot.
1045 * - if wp->w_redraw_top non-zero, redraw lines between
1046 * wp->w_redraw_top and wp->w_redr_bot.
1047 * - continue redrawing when syntax status is invalid.
1048 * 4. if scrolled up, update lines at the bottom.
1049 * This results in three areas that may need updating:
1050 * top: from first row to top_end (when scrolled down)
1051 * mid: from mid_start to mid_end (update inversion or changed text)
1052 * bot: from bot_start to last row (when scrolled up)
1053 */
1054 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001055win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
1057 buf_T *buf = wp->w_buffer;
1058 int type;
1059 int top_end = 0; /* Below last row of the top area that needs
1060 updating. 0 when no top area updating. */
1061 int mid_start = 999;/* first row of the mid area that needs
1062 updating. 999 when no mid area updating. */
1063 int mid_end = 0; /* Below last row of the mid area that needs
1064 updating. 0 when no mid area updating. */
1065 int bot_start = 999;/* first row of the bot area that needs
1066 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 int scrolled_down = FALSE; /* TRUE when scrolled down when
1068 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001070 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 int top_to_mod = FALSE; /* redraw above mod_top */
1072#endif
1073
1074 int row; /* current window row to display */
1075 linenr_T lnum; /* current buffer lnum to display */
1076 int idx; /* current index in w_lines[] */
1077 int srow; /* starting row of the current line */
1078
1079 int eof = FALSE; /* if TRUE, we hit the end of the file */
1080 int didline = FALSE; /* if TRUE, we finished the last line */
1081 int i;
1082 long j;
1083 static int recursive = FALSE; /* being called recursively */
1084 int old_botline = wp->w_botline;
1085#ifdef FEAT_FOLDING
1086 long fold_count;
1087#endif
1088#ifdef FEAT_SYN_HL
1089 /* remember what happened to the previous line, to know if
1090 * check_visual_highlight() can be used */
1091#define DID_NONE 1 /* didn't update a line */
1092#define DID_LINE 2 /* updated a normal line */
1093#define DID_FOLD 3 /* updated a folded line */
1094 int did_update = DID_NONE;
1095 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1096#endif
1097 linenr_T mod_top = 0;
1098 linenr_T mod_bot = 0;
1099#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1100 int save_got_int;
1101#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001102#ifdef SYN_TIME_LIMIT
1103 proftime_T syntax_tm;
1104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 type = wp->w_redr_type;
1107
1108 if (type == NOT_VALID)
1109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 wp->w_lines_valid = 0;
1112 }
1113
1114 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001115 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 wp->w_redr_type = 0;
1118 return;
1119 }
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 /* Window is zero-width: Only need to draw the separator. */
1122 if (wp->w_width == 0)
1123 {
1124 /* draw the vertical separator right of this window */
1125 draw_vsep_win(wp, 0);
1126 wp->w_redr_type = 0;
1127 return;
1128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001130#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001131 // If this window contains a terminal, redraw works completely differently.
1132 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001133 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001134 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001135# ifdef FEAT_MENU
1136 /* Draw the window toolbar, if there is one. */
1137 if (winbar_height(wp) > 0)
1138 redraw_win_toolbar(wp);
1139# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001140 wp->w_redr_type = 0;
1141 return;
1142 }
1143#endif
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001146 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#endif
1148
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001149#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001150 /* Force redraw when width of 'number' or 'relativenumber' column
1151 * changes. */
1152 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001153 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001154 {
1155 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001156 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001157 }
1158 else
1159#endif
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1162 {
1163 /*
1164 * When there are both inserted/deleted lines and specific lines to be
1165 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1166 * everything (only happens when redrawing is off for while).
1167 */
1168 type = NOT_VALID;
1169 }
1170 else
1171 {
1172 /*
1173 * Set mod_top to the first line that needs displaying because of
1174 * changes. Set mod_bot to the first line after the changes.
1175 */
1176 mod_top = wp->w_redraw_top;
1177 if (wp->w_redraw_bot != 0)
1178 mod_bot = wp->w_redraw_bot + 1;
1179 else
1180 mod_bot = 0;
1181 wp->w_redraw_top = 0; /* reset for next time */
1182 wp->w_redraw_bot = 0;
1183 if (buf->b_mod_set)
1184 {
1185 if (mod_top == 0 || mod_top > buf->b_mod_top)
1186 {
1187 mod_top = buf->b_mod_top;
1188#ifdef FEAT_SYN_HL
1189 /* Need to redraw lines above the change that may be included
1190 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001191 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001193 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (mod_top < 1)
1195 mod_top = 1;
1196 }
1197#endif
1198 }
1199 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1200 mod_bot = buf->b_mod_bot;
1201
1202#ifdef FEAT_SEARCH_EXTRA
1203 /* When 'hlsearch' is on and using a multi-line search pattern, a
1204 * change in one line may make the Search highlighting in a
1205 * previous line invalid. Simple solution: redraw all visible
1206 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001207 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001209 if (search_hl.rm.regprog != NULL
1210 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001212 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001213 {
1214 cur = wp->w_match_head;
1215 while (cur != NULL)
1216 {
1217 if (cur->match.regprog != NULL
1218 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001219 {
1220 top_to_mod = TRUE;
1221 break;
1222 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001223 cur = cur->next;
1224 }
1225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226#endif
1227 }
1228#ifdef FEAT_FOLDING
1229 if (mod_top != 0 && hasAnyFolding(wp))
1230 {
1231 linenr_T lnumt, lnumb;
1232
1233 /*
1234 * A change in a line can cause lines above it to become folded or
1235 * unfolded. Find the top most buffer line that may be affected.
1236 * If the line was previously folded and displayed, get the first
1237 * line of that fold. If the line is folded now, get the first
1238 * folded line. Use the minimum of these two.
1239 */
1240
1241 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1242 * the line below it. If there is no valid entry, use w_topline.
1243 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1244 * to this line. If there is no valid entry, use MAXLNUM. */
1245 lnumt = wp->w_topline;
1246 lnumb = MAXLNUM;
1247 for (i = 0; i < wp->w_lines_valid; ++i)
1248 if (wp->w_lines[i].wl_valid)
1249 {
1250 if (wp->w_lines[i].wl_lastlnum < mod_top)
1251 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1252 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1253 {
1254 lnumb = wp->w_lines[i].wl_lnum;
1255 /* When there is a fold column it might need updating
1256 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001257 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 ++lnumb;
1259 }
1260 }
1261
1262 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1263 if (mod_top > lnumt)
1264 mod_top = lnumt;
1265
1266 /* Now do the same for the bottom line (one above mod_bot). */
1267 --mod_bot;
1268 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1269 ++mod_bot;
1270 if (mod_bot < lnumb)
1271 mod_bot = lnumb;
1272 }
1273#endif
1274
1275 /* When a change starts above w_topline and the end is below
1276 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001277 * If the end of the change is above w_topline: do like no change was
1278 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (mod_top != 0 && mod_top < wp->w_topline)
1280 {
1281 if (mod_bot > wp->w_topline)
1282 mod_top = wp->w_topline;
1283#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001284 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 top_end = 1;
1286#endif
1287 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001288
1289 /* When line numbers are displayed need to redraw all lines below
1290 * inserted/deleted lines. */
1291 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1292 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294
1295 /*
1296 * When only displaying the lines at the top, set top_end. Used when
1297 * window has scrolled down for msg_scrolled.
1298 */
1299 if (type == REDRAW_TOP)
1300 {
1301 j = 0;
1302 for (i = 0; i < wp->w_lines_valid; ++i)
1303 {
1304 j += wp->w_lines[i].wl_size;
1305 if (j >= wp->w_upd_rows)
1306 {
1307 top_end = j;
1308 break;
1309 }
1310 }
1311 if (top_end == 0)
1312 /* not found (cannot happen?): redraw everything */
1313 type = NOT_VALID;
1314 else
1315 /* top area defined, the rest is VALID */
1316 type = VALID;
1317 }
1318
Bram Moolenaar367329b2007-08-30 11:53:22 +00001319 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001320 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1321 * non-zero and thus not FALSE) will indicate that screenclear() was not
1322 * called. */
1323 if (screen_cleared)
1324 screen_cleared = MAYBE;
1325
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 /*
1327 * If there are no changes on the screen that require a complete redraw,
1328 * handle three cases:
1329 * 1: we are off the top of the screen by a few lines: scroll down
1330 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1331 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1332 * w_lines[] that needs updating.
1333 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001334 if ((type == VALID || type == SOME_VALID
1335 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#ifdef FEAT_DIFF
1337 && !wp->w_botfill && !wp->w_old_botfill
1338#endif
1339 )
1340 {
1341 if (mod_top != 0 && wp->w_topline == mod_top)
1342 {
1343 /*
1344 * w_topline is the first changed line, the scrolling will be done
1345 * further down.
1346 */
1347 }
1348 else if (wp->w_lines[0].wl_valid
1349 && (wp->w_topline < wp->w_lines[0].wl_lnum
1350#ifdef FEAT_DIFF
1351 || (wp->w_topline == wp->w_lines[0].wl_lnum
1352 && wp->w_topfill > wp->w_old_topfill)
1353#endif
1354 ))
1355 {
1356 /*
1357 * New topline is above old topline: May scroll down.
1358 */
1359#ifdef FEAT_FOLDING
1360 if (hasAnyFolding(wp))
1361 {
1362 linenr_T ln;
1363
1364 /* count the number of lines we are off, counting a sequence
1365 * of folded lines as one */
1366 j = 0;
1367 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1368 {
1369 ++j;
1370 if (j >= wp->w_height - 2)
1371 break;
1372 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1373 }
1374 }
1375 else
1376#endif
1377 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1378 if (j < wp->w_height - 2) /* not too far off */
1379 {
1380 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1381#ifdef FEAT_DIFF
1382 /* insert extra lines for previously invisible filler lines */
1383 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1384 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1385 - wp->w_old_topfill;
1386#endif
1387 if (i < wp->w_height - 2) /* less than a screen off */
1388 {
1389 /*
1390 * Try to insert the correct number of lines.
1391 * If not the last window, delete the lines at the bottom.
1392 * win_ins_lines may fail when the terminal can't do it.
1393 */
1394 if (i > 0)
1395 check_for_delay(FALSE);
1396 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1397 {
1398 if (wp->w_lines_valid != 0)
1399 {
1400 /* Need to update rows that are new, stop at the
1401 * first one that scrolled down. */
1402 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
1405 /* Move the entries that were scrolled, disable
1406 * the entries for the lines to be redrawn. */
1407 if ((wp->w_lines_valid += j) > wp->w_height)
1408 wp->w_lines_valid = wp->w_height;
1409 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1410 wp->w_lines[idx] = wp->w_lines[idx - j];
1411 while (idx >= 0)
1412 wp->w_lines[idx--].wl_valid = FALSE;
1413 }
1414 }
1415 else
1416 mid_start = 0; /* redraw all lines */
1417 }
1418 else
1419 mid_start = 0; /* redraw all lines */
1420 }
1421 else
1422 mid_start = 0; /* redraw all lines */
1423 }
1424 else
1425 {
1426 /*
1427 * New topline is at or below old topline: May scroll up.
1428 * When topline didn't change, find first entry in w_lines[] that
1429 * needs updating.
1430 */
1431
1432 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1433 j = -1;
1434 row = 0;
1435 for (i = 0; i < wp->w_lines_valid; i++)
1436 {
1437 if (wp->w_lines[i].wl_valid
1438 && wp->w_lines[i].wl_lnum == wp->w_topline)
1439 {
1440 j = i;
1441 break;
1442 }
1443 row += wp->w_lines[i].wl_size;
1444 }
1445 if (j == -1)
1446 {
1447 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1448 * lines */
1449 mid_start = 0;
1450 }
1451 else
1452 {
1453 /*
1454 * Try to delete the correct number of lines.
1455 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1456 */
1457#ifdef FEAT_DIFF
1458 /* If the topline didn't change, delete old filler lines,
1459 * otherwise delete filler lines of the new topline... */
1460 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1461 row += wp->w_old_topfill;
1462 else
1463 row += diff_check_fill(wp, wp->w_topline);
1464 /* ... but don't delete new filler lines. */
1465 row -= wp->w_topfill;
1466#endif
1467 if (row > 0)
1468 {
1469 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001470 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1471 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 bot_start = wp->w_height - row;
1473 else
1474 mid_start = 0; /* redraw all lines */
1475 }
1476 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1477 {
1478 /*
1479 * Skip the lines (below the deleted lines) that are still
1480 * valid and don't need redrawing. Copy their info
1481 * upwards, to compensate for the deleted lines. Set
1482 * bot_start to the first row that needs redrawing.
1483 */
1484 bot_start = 0;
1485 idx = 0;
1486 for (;;)
1487 {
1488 wp->w_lines[idx] = wp->w_lines[j];
1489 /* stop at line that didn't fit, unless it is still
1490 * valid (no lines deleted) */
1491 if (row > 0 && bot_start + row
1492 + (int)wp->w_lines[j].wl_size > wp->w_height)
1493 {
1494 wp->w_lines_valid = idx + 1;
1495 break;
1496 }
1497 bot_start += wp->w_lines[idx++].wl_size;
1498
1499 /* stop at the last valid entry in w_lines[].wl_size */
1500 if (++j >= wp->w_lines_valid)
1501 {
1502 wp->w_lines_valid = idx;
1503 break;
1504 }
1505 }
1506#ifdef FEAT_DIFF
1507 /* Correct the first entry for filler lines at the top
1508 * when it won't get updated below. */
1509 if (wp->w_p_diff && bot_start > 0)
1510 wp->w_lines[0].wl_size =
1511 plines_win_nofill(wp, wp->w_topline, TRUE)
1512 + wp->w_topfill;
1513#endif
1514 }
1515 }
1516 }
1517
1518 /* When starting redraw in the first line, redraw all lines. When
1519 * there is only one window it's probably faster to clear the screen
1520 * first. */
1521 if (mid_start == 0)
1522 {
1523 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001524 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001525 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001526 /* Clear the screen when it was not done by win_del_lines() or
1527 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1528 * then. */
1529 if (screen_cleared != TRUE)
1530 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001531 /* The screen was cleared, redraw the tab pages line. */
1532 if (redraw_tabline)
1533 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001536
1537 /* When win_del_lines() or win_ins_lines() caused the screen to be
1538 * cleared (only happens for the first window) or when screenclear()
1539 * was called directly above, "must_redraw" will have been set to
1540 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1541 if (screen_cleared == TRUE)
1542 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544 else
1545 {
1546 /* Not VALID or INVERTED: redraw all lines. */
1547 mid_start = 0;
1548 mid_end = wp->w_height;
1549 }
1550
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001551 if (type == SOME_VALID)
1552 {
1553 /* SOME_VALID: redraw all lines. */
1554 mid_start = 0;
1555 mid_end = wp->w_height;
1556 type = NOT_VALID;
1557 }
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 /* check if we are updating or removing the inverted part */
1560 if ((VIsual_active && buf == curwin->w_buffer)
1561 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1562 {
1563 linenr_T from, to;
1564
1565 if (VIsual_active)
1566 {
1567 if (VIsual_active
1568 && (VIsual_mode != wp->w_old_visual_mode
1569 || type == INVERTED_ALL))
1570 {
1571 /*
1572 * If the type of Visual selection changed, redraw the whole
1573 * selection. Also when the ownership of the X selection is
1574 * gained or lost.
1575 */
1576 if (curwin->w_cursor.lnum < VIsual.lnum)
1577 {
1578 from = curwin->w_cursor.lnum;
1579 to = VIsual.lnum;
1580 }
1581 else
1582 {
1583 from = VIsual.lnum;
1584 to = curwin->w_cursor.lnum;
1585 }
1586 /* redraw more when the cursor moved as well */
1587 if (wp->w_old_cursor_lnum < from)
1588 from = wp->w_old_cursor_lnum;
1589 if (wp->w_old_cursor_lnum > to)
1590 to = wp->w_old_cursor_lnum;
1591 if (wp->w_old_visual_lnum < from)
1592 from = wp->w_old_visual_lnum;
1593 if (wp->w_old_visual_lnum > to)
1594 to = wp->w_old_visual_lnum;
1595 }
1596 else
1597 {
1598 /*
1599 * Find the line numbers that need to be updated: The lines
1600 * between the old cursor position and the current cursor
1601 * position. Also check if the Visual position changed.
1602 */
1603 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1604 {
1605 from = curwin->w_cursor.lnum;
1606 to = wp->w_old_cursor_lnum;
1607 }
1608 else
1609 {
1610 from = wp->w_old_cursor_lnum;
1611 to = curwin->w_cursor.lnum;
1612 if (from == 0) /* Visual mode just started */
1613 from = to;
1614 }
1615
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001616 if (VIsual.lnum != wp->w_old_visual_lnum
1617 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 if (wp->w_old_visual_lnum < from
1620 && wp->w_old_visual_lnum != 0)
1621 from = wp->w_old_visual_lnum;
1622 if (wp->w_old_visual_lnum > to)
1623 to = wp->w_old_visual_lnum;
1624 if (VIsual.lnum < from)
1625 from = VIsual.lnum;
1626 if (VIsual.lnum > to)
1627 to = VIsual.lnum;
1628 }
1629 }
1630
1631 /*
1632 * If in block mode and changed column or curwin->w_curswant:
1633 * update all lines.
1634 * First compute the actual start and end column.
1635 */
1636 if (VIsual_mode == Ctrl_V)
1637 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001638 colnr_T fromc, toc;
1639#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1640 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar404406a2014-10-09 13:24:43 +02001642 if (curwin->w_p_lbr)
1643 ve_flags = VE_ALL;
1644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar404406a2014-10-09 13:24:43 +02001646#if defined(FEAT_VIRTUALEDIT) && defined(FEAT_LINEBREAK)
1647 ve_flags = save_ve_flags;
1648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 ++toc;
1650 if (curwin->w_curswant == MAXCOL)
1651 toc = MAXCOL;
1652
1653 if (fromc != wp->w_old_cursor_fcol
1654 || toc != wp->w_old_cursor_lcol)
1655 {
1656 if (from > VIsual.lnum)
1657 from = VIsual.lnum;
1658 if (to < VIsual.lnum)
1659 to = VIsual.lnum;
1660 }
1661 wp->w_old_cursor_fcol = fromc;
1662 wp->w_old_cursor_lcol = toc;
1663 }
1664 }
1665 else
1666 {
1667 /* Use the line numbers of the old Visual area. */
1668 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1669 {
1670 from = wp->w_old_cursor_lnum;
1671 to = wp->w_old_visual_lnum;
1672 }
1673 else
1674 {
1675 from = wp->w_old_visual_lnum;
1676 to = wp->w_old_cursor_lnum;
1677 }
1678 }
1679
1680 /*
1681 * There is no need to update lines above the top of the window.
1682 */
1683 if (from < wp->w_topline)
1684 from = wp->w_topline;
1685
1686 /*
1687 * If we know the value of w_botline, use it to restrict the update to
1688 * the lines that are visible in the window.
1689 */
1690 if (wp->w_valid & VALID_BOTLINE)
1691 {
1692 if (from >= wp->w_botline)
1693 from = wp->w_botline - 1;
1694 if (to >= wp->w_botline)
1695 to = wp->w_botline - 1;
1696 }
1697
1698 /*
1699 * Find the minimal part to be updated.
1700 * Watch out for scrolling that made entries in w_lines[] invalid.
1701 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1702 * top_end; need to redraw from top_end to the "to" line.
1703 * A middle mouse click with a Visual selection may change the text
1704 * above the Visual area and reset wl_valid, do count these for
1705 * mid_end (in srow).
1706 */
1707 if (mid_start > 0)
1708 {
1709 lnum = wp->w_topline;
1710 idx = 0;
1711 srow = 0;
1712 if (scrolled_down)
1713 mid_start = top_end;
1714 else
1715 mid_start = 0;
1716 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1717 {
1718 if (wp->w_lines[idx].wl_valid)
1719 mid_start += wp->w_lines[idx].wl_size;
1720 else if (!scrolled_down)
1721 srow += wp->w_lines[idx].wl_size;
1722 ++idx;
1723# ifdef FEAT_FOLDING
1724 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1725 lnum = wp->w_lines[idx].wl_lnum;
1726 else
1727# endif
1728 ++lnum;
1729 }
1730 srow += mid_start;
1731 mid_end = wp->w_height;
1732 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1733 {
1734 if (wp->w_lines[idx].wl_valid
1735 && wp->w_lines[idx].wl_lnum >= to + 1)
1736 {
1737 /* Only update until first row of this line */
1738 mid_end = srow;
1739 break;
1740 }
1741 srow += wp->w_lines[idx].wl_size;
1742 }
1743 }
1744 }
1745
1746 if (VIsual_active && buf == curwin->w_buffer)
1747 {
1748 wp->w_old_visual_mode = VIsual_mode;
1749 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1750 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001751 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 wp->w_old_curswant = curwin->w_curswant;
1753 }
1754 else
1755 {
1756 wp->w_old_visual_mode = 0;
1757 wp->w_old_cursor_lnum = 0;
1758 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001759 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1763 /* reset got_int, otherwise regexp won't work */
1764 save_got_int = got_int;
1765 got_int = 0;
1766#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001767#ifdef SYN_TIME_LIMIT
1768 /* Set the time limit to 'redrawtime'. */
1769 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001770 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#ifdef FEAT_FOLDING
1773 win_foldinfo.fi_level = 0;
1774#endif
1775
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001776#ifdef FEAT_MENU
1777 /*
1778 * Draw the window toolbar, if there is one.
1779 * TODO: only when needed.
1780 */
1781 if (winbar_height(wp) > 0)
1782 redraw_win_toolbar(wp);
1783#endif
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /*
1786 * Update all the window rows.
1787 */
1788 idx = 0; /* first entry in w_lines[].wl_size */
1789 row = 0;
1790 srow = 0;
1791 lnum = wp->w_topline; /* first line shown in window */
1792 for (;;)
1793 {
1794 /* stop updating when reached the end of the window (check for _past_
1795 * the end of the window is at the end of the loop) */
1796 if (row == wp->w_height)
1797 {
1798 didline = TRUE;
1799 break;
1800 }
1801
1802 /* stop updating when hit the end of the file */
1803 if (lnum > buf->b_ml.ml_line_count)
1804 {
1805 eof = TRUE;
1806 break;
1807 }
1808
1809 /* Remember the starting row of the line that is going to be dealt
1810 * with. It is used further down when the line doesn't fit. */
1811 srow = row;
1812
1813 /*
1814 * Update a line when it is in an area that needs updating, when it
1815 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001816 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 * win_del_lines(), check if the current line includes it.
1818 * When syntax folding is being used, the saved syntax states will
1819 * already have been updated, we can't see where the syntax state is
1820 * the same again, just update until the end of the window.
1821 */
1822 if (row < top_end
1823 || (row >= mid_start && row < mid_end)
1824#ifdef FEAT_SEARCH_EXTRA
1825 || top_to_mod
1826#endif
1827 || idx >= wp->w_lines_valid
1828 || (row + wp->w_lines[idx].wl_size > bot_start)
1829 || (mod_top != 0
1830 && (lnum == mod_top
1831 || (lnum >= mod_top
1832 && (lnum < mod_bot
1833#ifdef FEAT_SYN_HL
1834 || did_update == DID_FOLD
1835 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001836 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 && (
1838# ifdef FEAT_FOLDING
1839 (foldmethodIsSyntax(wp)
1840 && hasAnyFolding(wp)) ||
1841# endif
1842 syntax_check_changed(lnum)))
1843#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001844#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001845 /* match in fixed position might need redraw
1846 * if lines were inserted or deleted */
1847 || (wp->w_match_head != NULL
1848 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 )))))
1851 {
1852#ifdef FEAT_SEARCH_EXTRA
1853 if (lnum == mod_top)
1854 top_to_mod = FALSE;
1855#endif
1856
1857 /*
1858 * When at start of changed lines: May scroll following lines
1859 * up or down to minimize redrawing.
1860 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001861 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 */
1863 if (lnum == mod_top
1864 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001865 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 int old_rows = 0;
1868 int new_rows = 0;
1869 int xtra_rows;
1870 linenr_T l;
1871
1872 /* Count the old number of window rows, using w_lines[], which
1873 * should still contain the sizes for the lines as they are
1874 * currently displayed. */
1875 for (i = idx; i < wp->w_lines_valid; ++i)
1876 {
1877 /* Only valid lines have a meaningful wl_lnum. Invalid
1878 * lines are part of the changed area. */
1879 if (wp->w_lines[i].wl_valid
1880 && wp->w_lines[i].wl_lnum == mod_bot)
1881 break;
1882 old_rows += wp->w_lines[i].wl_size;
1883#ifdef FEAT_FOLDING
1884 if (wp->w_lines[i].wl_valid
1885 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1886 {
1887 /* Must have found the last valid entry above mod_bot.
1888 * Add following invalid entries. */
1889 ++i;
1890 while (i < wp->w_lines_valid
1891 && !wp->w_lines[i].wl_valid)
1892 old_rows += wp->w_lines[i++].wl_size;
1893 break;
1894 }
1895#endif
1896 }
1897
1898 if (i >= wp->w_lines_valid)
1899 {
1900 /* We can't find a valid line below the changed lines,
1901 * need to redraw until the end of the window.
1902 * Inserting/deleting lines has no use. */
1903 bot_start = 0;
1904 }
1905 else
1906 {
1907 /* Able to count old number of rows: Count new window
1908 * rows, and may insert/delete lines */
1909 j = idx;
1910 for (l = lnum; l < mod_bot; ++l)
1911 {
1912#ifdef FEAT_FOLDING
1913 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1914 ++new_rows;
1915 else
1916#endif
1917#ifdef FEAT_DIFF
1918 if (l == wp->w_topline)
1919 new_rows += plines_win_nofill(wp, l, TRUE)
1920 + wp->w_topfill;
1921 else
1922#endif
1923 new_rows += plines_win(wp, l, TRUE);
1924 ++j;
1925 if (new_rows > wp->w_height - row - 2)
1926 {
1927 /* it's getting too much, must redraw the rest */
1928 new_rows = 9999;
1929 break;
1930 }
1931 }
1932 xtra_rows = new_rows - old_rows;
1933 if (xtra_rows < 0)
1934 {
1935 /* May scroll text up. If there is not enough
1936 * remaining text or scrolling fails, must redraw the
1937 * rest. If scrolling works, must redraw the text
1938 * below the scrolled text. */
1939 if (row - xtra_rows >= wp->w_height - 2)
1940 mod_bot = MAXLNUM;
1941 else
1942 {
1943 check_for_delay(FALSE);
1944 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001945 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 mod_bot = MAXLNUM;
1947 else
1948 bot_start = wp->w_height + xtra_rows;
1949 }
1950 }
1951 else if (xtra_rows > 0)
1952 {
1953 /* May scroll text down. If there is not enough
1954 * remaining text of scrolling fails, must redraw the
1955 * rest. */
1956 if (row + xtra_rows >= wp->w_height - 2)
1957 mod_bot = MAXLNUM;
1958 else
1959 {
1960 check_for_delay(FALSE);
1961 if (win_ins_lines(wp, row + old_rows,
1962 xtra_rows, FALSE, FALSE) == FAIL)
1963 mod_bot = MAXLNUM;
1964 else if (top_end > row + old_rows)
1965 /* Scrolled the part at the top that requires
1966 * updating down. */
1967 top_end += xtra_rows;
1968 }
1969 }
1970
1971 /* When not updating the rest, may need to move w_lines[]
1972 * entries. */
1973 if (mod_bot != MAXLNUM && i != j)
1974 {
1975 if (j < i)
1976 {
1977 int x = row + new_rows;
1978
1979 /* move entries in w_lines[] upwards */
1980 for (;;)
1981 {
1982 /* stop at last valid entry in w_lines[] */
1983 if (i >= wp->w_lines_valid)
1984 {
1985 wp->w_lines_valid = j;
1986 break;
1987 }
1988 wp->w_lines[j] = wp->w_lines[i];
1989 /* stop at a line that won't fit */
1990 if (x + (int)wp->w_lines[j].wl_size
1991 > wp->w_height)
1992 {
1993 wp->w_lines_valid = j + 1;
1994 break;
1995 }
1996 x += wp->w_lines[j++].wl_size;
1997 ++i;
1998 }
1999 if (bot_start > x)
2000 bot_start = x;
2001 }
2002 else /* j > i */
2003 {
2004 /* move entries in w_lines[] downwards */
2005 j -= i;
2006 wp->w_lines_valid += j;
2007 if (wp->w_lines_valid > wp->w_height)
2008 wp->w_lines_valid = wp->w_height;
2009 for (i = wp->w_lines_valid; i - j >= idx; --i)
2010 wp->w_lines[i] = wp->w_lines[i - j];
2011
2012 /* The w_lines[] entries for inserted lines are
2013 * now invalid, but wl_size may be used above.
2014 * Reset to zero. */
2015 while (i >= idx)
2016 {
2017 wp->w_lines[i].wl_size = 0;
2018 wp->w_lines[i--].wl_valid = FALSE;
2019 }
2020 }
2021 }
2022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024
2025#ifdef FEAT_FOLDING
2026 /*
2027 * When lines are folded, display one line for all of them.
2028 * Otherwise, display normally (can be several display lines when
2029 * 'wrap' is on).
2030 */
2031 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2032 if (fold_count != 0)
2033 {
2034 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2035 ++row;
2036 --fold_count;
2037 wp->w_lines[idx].wl_folded = TRUE;
2038 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2039# ifdef FEAT_SYN_HL
2040 did_update = DID_FOLD;
2041# endif
2042 }
2043 else
2044#endif
2045 if (idx < wp->w_lines_valid
2046 && wp->w_lines[idx].wl_valid
2047 && wp->w_lines[idx].wl_lnum == lnum
2048 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002049 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 && srow + wp->w_lines[idx].wl_size > wp->w_height
2051#ifdef FEAT_DIFF
2052 && diff_check_fill(wp, lnum) == 0
2053#endif
2054 )
2055 {
2056 /* This line is not going to fit. Don't draw anything here,
2057 * will draw "@ " lines below. */
2058 row = wp->w_height + 1;
2059 }
2060 else
2061 {
2062#ifdef FEAT_SEARCH_EXTRA
2063 prepare_search_hl(wp, lnum);
2064#endif
2065#ifdef FEAT_SYN_HL
2066 /* Let the syntax stuff know we skipped a few lines. */
2067 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002068 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 syntax_end_parsing(syntax_last_parsed + 1);
2070#endif
2071
2072 /*
2073 * Display one line.
2074 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002075 row = win_line(wp, lnum, srow, wp->w_height,
2076 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078#ifdef FEAT_FOLDING
2079 wp->w_lines[idx].wl_folded = FALSE;
2080 wp->w_lines[idx].wl_lastlnum = lnum;
2081#endif
2082#ifdef FEAT_SYN_HL
2083 did_update = DID_LINE;
2084 syntax_last_parsed = lnum;
2085#endif
2086 }
2087
2088 wp->w_lines[idx].wl_lnum = lnum;
2089 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002090
2091 /* Past end of the window or end of the screen. Note that after
2092 * resizing wp->w_height may be end up too big. That's a problem
2093 * elsewhere, but prevent a crash here. */
2094 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 {
2096 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002097 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2099 ++idx;
2100 break;
2101 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002102 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 wp->w_lines[idx].wl_size = row - srow;
2104 ++idx;
2105#ifdef FEAT_FOLDING
2106 lnum += fold_count + 1;
2107#else
2108 ++lnum;
2109#endif
2110 }
2111 else
2112 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002113 if (wp->w_p_rnu)
2114 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002115#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002116 // 'relativenumber' set: The text doesn't need to be drawn, but
2117 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002118 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2119 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002120 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002121 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002122#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002123 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002124 }
2125
2126 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 row += wp->w_lines[idx++].wl_size;
2128 if (row > wp->w_height) /* past end of screen */
2129 break;
2130#ifdef FEAT_FOLDING
2131 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2132#else
2133 ++lnum;
2134#endif
2135#ifdef FEAT_SYN_HL
2136 did_update = DID_NONE;
2137#endif
2138 }
2139
2140 if (lnum > buf->b_ml.ml_line_count)
2141 {
2142 eof = TRUE;
2143 break;
2144 }
2145 }
2146 /*
2147 * End of loop over all window lines.
2148 */
2149
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002150#ifdef FEAT_VTP
2151 /* Rewrite the character at the end of the screen line. */
2152 if (use_vtp())
2153 {
2154 int i;
2155
2156 for (i = 0; i < Rows; ++i)
2157# ifdef FEAT_MBYTE
2158 if (enc_utf8)
2159 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2160 LineOffset[i] + screen_Columns) > 1)
2161 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2162 else
2163 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2164 else
2165# endif
2166 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2167 }
2168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170 if (idx > wp->w_lines_valid)
2171 wp->w_lines_valid = idx;
2172
2173#ifdef FEAT_SYN_HL
2174 /*
2175 * Let the syntax stuff know we stop parsing here.
2176 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002177 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 syntax_end_parsing(syntax_last_parsed + 1);
2179#endif
2180
2181 /*
2182 * If we didn't hit the end of the file, and we didn't finish the last
2183 * line we were working on, then the line didn't fit.
2184 */
2185 wp->w_empty_rows = 0;
2186#ifdef FEAT_DIFF
2187 wp->w_filler_rows = 0;
2188#endif
2189 if (!eof && !didline)
2190 {
2191 if (lnum == wp->w_topline)
2192 {
2193 /*
2194 * Single line that does not fit!
2195 * Don't overwrite it, it can be edited.
2196 */
2197 wp->w_botline = lnum + 1;
2198 }
2199#ifdef FEAT_DIFF
2200 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2201 {
2202 /* Window ends in filler lines. */
2203 wp->w_botline = lnum;
2204 wp->w_filler_rows = wp->w_height - srow;
2205 }
2206#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002207 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2208 {
2209 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2210
2211 /*
2212 * Last line isn't finished: Display "@@@" in the last screen line.
2213 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002214 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002215 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002216 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002217 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002218 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002219 set_empty_rows(wp, srow);
2220 wp->w_botline = lnum;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2223 {
2224 /*
2225 * Last line isn't finished: Display "@@@" at the end.
2226 */
2227 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2228 W_WINROW(wp) + wp->w_height,
2229 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002230 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 set_empty_rows(wp, srow);
2232 wp->w_botline = lnum;
2233 }
2234 else
2235 {
2236 win_draw_end(wp, '@', ' ', srow, wp->w_height, HLF_AT);
2237 wp->w_botline = lnum;
2238 }
2239 }
2240 else
2241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (eof) /* we hit the end of the file */
2244 {
2245 wp->w_botline = buf->b_ml.ml_line_count + 1;
2246#ifdef FEAT_DIFF
2247 j = diff_check_fill(wp, wp->w_botline);
2248 if (j > 0 && !wp->w_botfill)
2249 {
2250 /*
2251 * Display filler lines at the end of the file
2252 */
2253 if (char2cells(fill_diff) > 1)
2254 i = '-';
2255 else
2256 i = fill_diff;
2257 if (row + j > wp->w_height)
2258 j = wp->w_height - row;
2259 win_draw_end(wp, i, i, row, row + (int)j, HLF_DED);
2260 row += j;
2261 }
2262#endif
2263 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002264 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 wp->w_botline = lnum;
2266
2267 /* make sure the rest of the screen is blank */
2268 /* put '~'s on rows that aren't part of the file. */
Bram Moolenaar58b85342016-08-14 19:54:54 +02002269 win_draw_end(wp, '~', ' ', row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002272#ifdef SYN_TIME_LIMIT
2273 syn_set_timeout(NULL);
2274#endif
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* Reset the type of redrawing required, the window has been updated. */
2277 wp->w_redr_type = 0;
2278#ifdef FEAT_DIFF
2279 wp->w_old_topfill = wp->w_topfill;
2280 wp->w_old_botfill = wp->w_botfill;
2281#endif
2282
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002283 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 /*
2286 * There is a trick with w_botline. If we invalidate it on each
2287 * change that might modify it, this will cause a lot of expensive
2288 * calls to plines() in update_topline() each time. Therefore the
2289 * value of w_botline is often approximated, and this value is used to
2290 * compute the value of w_topline. If the value of w_botline was
2291 * wrong, check that the value of w_topline is correct (cursor is on
2292 * the visible part of the text). If it's not, we need to redraw
2293 * again. Mostly this just means scrolling up a few lines, so it
2294 * doesn't look too bad. Only do this for the current window (where
2295 * changes are relevant).
2296 */
2297 wp->w_valid |= VALID_BOTLINE;
2298 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2299 {
2300 recursive = TRUE;
2301 curwin->w_valid &= ~VALID_TOPLINE;
2302 update_topline(); /* may invalidate w_botline again */
2303 if (must_redraw != 0)
2304 {
2305 /* Don't update for changes in buffer again. */
2306 i = curbuf->b_mod_set;
2307 curbuf->b_mod_set = FALSE;
2308 win_update(curwin);
2309 must_redraw = 0;
2310 curbuf->b_mod_set = i;
2311 }
2312 recursive = FALSE;
2313 }
2314 }
2315
2316#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2317 /* restore got_int, unless CTRL-C was hit while redrawing */
2318 if (!got_int)
2319 got_int = save_got_int;
2320#endif
2321}
2322
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323/*
2324 * Clear the rest of the window and mark the unused lines with "c1". use "c2"
2325 * as the filler character.
2326 */
2327 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002328win_draw_end(
2329 win_T *wp,
2330 int c1,
2331 int c2,
2332 int row,
2333 int endrow,
2334 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336#if defined(FEAT_FOLDING) || defined(FEAT_SIGNS) || defined(FEAT_CMDWIN)
2337 int n = 0;
2338# define FDC_OFF n
2339#else
2340# define FDC_OFF 0
2341#endif
Bram Moolenaar1c934292015-01-27 16:39:29 +01002342#ifdef FEAT_FOLDING
2343 int fdc = compute_foldcolumn(wp, 0);
2344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346#ifdef FEAT_RIGHTLEFT
2347 if (wp->w_p_rl)
2348 {
2349 /* No check for cmdline window: should never be right-left. */
2350# ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002351 n = fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 if (n > 0)
2354 {
2355 /* draw the fold column at the right */
Bram Moolenaar02631462017-09-22 15:20:32 +02002356 if (n > wp->w_width)
2357 n = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2359 W_ENDCOL(wp) - n, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002360 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362# endif
2363# ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002364 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 int nn = n + 2;
2367
2368 /* draw the sign column left of the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002369 if (nn > wp->w_width)
2370 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2372 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002373 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 n = nn;
2375 }
2376# endif
2377 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002378 wp->w_wincol, W_ENDCOL(wp) - 1 - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002379 c2, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2381 W_ENDCOL(wp) - 1 - FDC_OFF, W_ENDCOL(wp) - FDC_OFF,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002382 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384 else
2385#endif
2386 {
2387#ifdef FEAT_CMDWIN
2388 if (cmdwin_type != 0 && wp == curwin)
2389 {
2390 /* draw the cmdline character in the leftmost column */
2391 n = 1;
2392 if (n > wp->w_width)
2393 n = wp->w_width;
2394 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002395 wp->w_wincol, (int)wp->w_wincol + n,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002396 cmdwin_type, ' ', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398#endif
2399#ifdef FEAT_FOLDING
Bram Moolenaar1c934292015-01-27 16:39:29 +01002400 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002402 int nn = n + fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /* draw the fold column at the left */
Bram Moolenaar02631462017-09-22 15:20:32 +02002405 if (nn > wp->w_width)
2406 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002408 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002409 ' ', ' ', HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 n = nn;
2411 }
2412#endif
2413#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002414 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 int nn = n + 2;
2417
2418 /* draw the sign column after the fold column */
Bram Moolenaar02631462017-09-22 15:20:32 +02002419 if (nn > wp->w_width)
2420 nn = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002422 wp->w_wincol + n, (int)wp->w_wincol + nn,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002423 ' ', ' ', HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 n = nn;
2425 }
2426#endif
2427 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002428 wp->w_wincol + FDC_OFF, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 c1, c2, HL_ATTR(hl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 set_empty_rows(wp, row);
2432}
2433
Bram Moolenaar1a384422010-07-14 19:53:30 +02002434#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002435/*
2436 * Advance **color_cols and return TRUE when there are columns to draw.
2437 */
2438 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002439advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002440{
2441 while (**color_cols >= 0 && vcol > **color_cols)
2442 ++*color_cols;
2443 return (**color_cols >= 0);
2444}
2445#endif
2446
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002447#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2448/*
2449 * Copy "text" to ScreenLines using "attr".
2450 * Returns the next screen column.
2451 */
2452 static int
2453text_to_screenline(win_T *wp, char_u *text, int col)
2454{
2455 int off = (int)(current_ScreenLine - ScreenLines);
2456
2457#ifdef FEAT_MBYTE
2458 if (has_mbyte)
2459 {
2460 int cells;
2461 int u8c, u8cc[MAX_MCO];
2462 int i;
2463 int idx;
2464 int c_len;
2465 char_u *p;
2466# ifdef FEAT_ARABIC
2467 int prev_c = 0; /* previous Arabic character */
2468 int prev_c1 = 0; /* first composing char for prev_c */
2469# endif
2470
2471# ifdef FEAT_RIGHTLEFT
2472 if (wp->w_p_rl)
2473 idx = off;
2474 else
2475# endif
2476 idx = off + col;
2477
2478 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2479 for (p = text; *p != NUL; )
2480 {
2481 cells = (*mb_ptr2cells)(p);
2482 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002483 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002484# ifdef FEAT_RIGHTLEFT
2485 - (wp->w_p_rl ? col : 0)
2486# endif
2487 )
2488 break;
2489 ScreenLines[idx] = *p;
2490 if (enc_utf8)
2491 {
2492 u8c = utfc_ptr2char(p, u8cc);
2493 if (*p < 0x80 && u8cc[0] == 0)
2494 {
2495 ScreenLinesUC[idx] = 0;
2496#ifdef FEAT_ARABIC
2497 prev_c = u8c;
2498#endif
2499 }
2500 else
2501 {
2502#ifdef FEAT_ARABIC
2503 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2504 {
2505 /* Do Arabic shaping. */
2506 int pc, pc1, nc;
2507 int pcc[MAX_MCO];
2508 int firstbyte = *p;
2509
2510 /* The idea of what is the previous and next
2511 * character depends on 'rightleft'. */
2512 if (wp->w_p_rl)
2513 {
2514 pc = prev_c;
2515 pc1 = prev_c1;
2516 nc = utf_ptr2char(p + c_len);
2517 prev_c1 = u8cc[0];
2518 }
2519 else
2520 {
2521 pc = utfc_ptr2char(p + c_len, pcc);
2522 nc = prev_c;
2523 pc1 = pcc[0];
2524 }
2525 prev_c = u8c;
2526
2527 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2528 pc, pc1, nc);
2529 ScreenLines[idx] = firstbyte;
2530 }
2531 else
2532 prev_c = u8c;
2533#endif
2534 /* Non-BMP character: display as ? or fullwidth ?. */
2535#ifdef UNICODE16
2536 if (u8c >= 0x10000)
2537 ScreenLinesUC[idx] = (cells == 2) ? 0xff1f : (int)'?';
2538 else
2539#endif
2540 ScreenLinesUC[idx] = u8c;
2541 for (i = 0; i < Screen_mco; ++i)
2542 {
2543 ScreenLinesC[i][idx] = u8cc[i];
2544 if (u8cc[i] == 0)
2545 break;
2546 }
2547 }
2548 if (cells > 1)
2549 ScreenLines[idx + 1] = 0;
2550 }
2551 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2552 /* double-byte single width character */
2553 ScreenLines2[idx] = p[1];
2554 else if (cells > 1)
2555 /* double-width character */
2556 ScreenLines[idx + 1] = p[1];
2557 col += cells;
2558 idx += cells;
2559 p += c_len;
2560 }
2561 }
2562 else
2563#endif
2564 {
2565 int len = (int)STRLEN(text);
2566
Bram Moolenaar02631462017-09-22 15:20:32 +02002567 if (len > wp->w_width - col)
2568 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002569 if (len > 0)
2570 {
2571#ifdef FEAT_RIGHTLEFT
2572 if (wp->w_p_rl)
2573 STRNCPY(current_ScreenLine, text, len);
2574 else
2575#endif
2576 STRNCPY(current_ScreenLine + col, text, len);
2577 col += len;
2578 }
2579 }
2580 return col;
2581}
2582#endif
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584#ifdef FEAT_FOLDING
2585/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002586 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2587 * space is available for window "wp", minus "col".
2588 */
2589 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002590compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002591{
2592 int fdc = wp->w_p_fdc;
2593 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002594 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002595
2596 if (fdc > wwidth - (col + wmw))
2597 fdc = wwidth - (col + wmw);
2598 return fdc;
2599}
2600
2601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 * Display one folded line.
2603 */
2604 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002605fold_line(
2606 win_T *wp,
2607 long fold_count,
2608 foldinfo_T *foldinfo,
2609 linenr_T lnum,
2610 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002612 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 pos_T *top, *bot;
2614 linenr_T lnume = lnum + fold_count - 1;
2615 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002616 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 int col;
2619 int txtcol;
2620 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002621 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 /* Build the fold line:
2624 * 1. Add the cmdwin_type for the command-line window
2625 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002626 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * 4. Compose the text
2628 * 5. Add the text
2629 * 6. set highlighting for the Visual area an other text
2630 */
2631 col = 0;
2632
2633 /*
2634 * 1. Add the cmdwin_type for the command-line window
2635 * Ignores 'rightleft', this window is never right-left.
2636 */
2637#ifdef FEAT_CMDWIN
2638 if (cmdwin_type != 0 && wp == curwin)
2639 {
2640 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002641 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642#ifdef FEAT_MBYTE
2643 if (enc_utf8)
2644 ScreenLinesUC[off] = 0;
2645#endif
2646 ++col;
2647 }
2648#endif
2649
2650 /*
2651 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002652 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002654 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (fdc > 0)
2656 {
2657 fill_foldcolumn(buf, wp, TRUE, lnum);
2658#ifdef FEAT_RIGHTLEFT
2659 if (wp->w_p_rl)
2660 {
2661 int i;
2662
Bram Moolenaar02631462017-09-22 15:20:32 +02002663 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002664 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 /* reverse the fold column */
2666 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002667 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 else
2670#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002671 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 col += fdc;
2673 }
2674
2675#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002676# define RL_MEMSET(p, v, l) \
2677 do { \
2678 if (wp->w_p_rl) \
2679 for (ri = 0; ri < l; ++ri) \
2680 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2681 else \
2682 for (ri = 0; ri < l; ++ri) \
2683 ScreenAttrs[off + (p) + ri] = v; \
2684 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002686# define RL_MEMSET(p, v, l) \
2687 do { \
2688 for (ri = 0; ri < l; ++ri) \
2689 ScreenAttrs[off + (p) + ri] = v; \
2690 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691#endif
2692
Bram Moolenaar64486672010-05-16 15:46:46 +02002693 /* Set all attributes of the 'number' or 'relativenumber' column and the
2694 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002695 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697#ifdef FEAT_SIGNS
2698 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002699 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002701 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (len > 0)
2703 {
2704 if (len > 2)
2705 len = 2;
2706# ifdef FEAT_RIGHTLEFT
2707 if (wp->w_p_rl)
2708 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002709 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002710 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else
2712# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002713 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 col += len;
2715 }
2716 }
2717#endif
2718
2719 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002720 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002722 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002724 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (len > 0)
2726 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002727 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002728 long num;
2729 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002730
2731 if (len > w + 1)
2732 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002733
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002734 if (wp->w_p_nu && !wp->w_p_rnu)
2735 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002736 num = (long)lnum;
2737 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002738 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002739 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002740 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002741 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002742 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002743 /* 'number' + 'relativenumber': cursor line shows absolute
2744 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002745 num = lnum;
2746 fmt = "%-*ld ";
2747 }
2748 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002749
Bram Moolenaar700e7342013-01-30 12:31:36 +01002750 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#ifdef FEAT_RIGHTLEFT
2752 if (wp->w_p_rl)
2753 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002754 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002755 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
2757#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002758 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 col += len;
2760 }
2761 }
2762
2763 /*
2764 * 4. Compose the folded-line string with 'foldtext', if set.
2765 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002766 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 txtcol = col; /* remember where text starts */
2769
2770 /*
2771 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2772 * Right-left text is put in columns 0 - number-col, normal text is put
2773 * in columns number-col - window-width.
2774 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002775 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777 /* Fill the rest of the line with the fold filler */
2778#ifdef FEAT_RIGHTLEFT
2779 if (wp->w_p_rl)
2780 col -= txtcol;
2781#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002782 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783#ifdef FEAT_RIGHTLEFT
2784 - (wp->w_p_rl ? txtcol : 0)
2785#endif
2786 )
2787 {
2788#ifdef FEAT_MBYTE
2789 if (enc_utf8)
2790 {
2791 if (fill_fold >= 0x80)
2792 {
2793 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002794 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002795 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002800 ScreenLines[off + col] = fill_fold;
2801 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002802 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805#endif
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002806 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808
2809 if (text != buf)
2810 vim_free(text);
2811
2812 /*
2813 * 6. set highlighting for the Visual area an other text.
2814 * If all folded lines are in the Visual area, highlight the line.
2815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2817 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002818 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
2820 /* Visual is after curwin->w_cursor */
2821 top = &curwin->w_cursor;
2822 bot = &VIsual;
2823 }
2824 else
2825 {
2826 /* Visual is before curwin->w_cursor */
2827 top = &VIsual;
2828 bot = &curwin->w_cursor;
2829 }
2830 if (lnum >= top->lnum
2831 && lnume <= bot->lnum
2832 && (VIsual_mode != 'v'
2833 || ((lnum > top->lnum
2834 || (lnum == top->lnum
2835 && top->col == 0))
2836 && (lnume < bot->lnum
2837 || (lnume == bot->lnum
2838 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002839 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
2841 if (VIsual_mode == Ctrl_V)
2842 {
2843 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002844 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002846 if (wp->w_old_cursor_lcol != MAXCOL
2847 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002848 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 len = wp->w_old_cursor_lcol;
2850 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002851 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002852 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002853 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 }
2856 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002859 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
2862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002864#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002865 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2866 if (wp->w_p_cc_cols)
2867 {
2868 int i = 0;
2869 int j = wp->w_p_cc_cols[i];
2870 int old_txtcol = txtcol;
2871
2872 while (j > -1)
2873 {
2874 txtcol += j;
2875 if (wp->w_p_wrap)
2876 txtcol -= wp->w_skipcol;
2877 else
2878 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002879 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002880 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002881 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002882 txtcol = old_txtcol;
2883 j = wp->w_p_cc_cols[++i];
2884 }
2885 }
2886
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002887 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002888 if (wp->w_p_cuc)
2889 {
2890 txtcol += wp->w_virtcol;
2891 if (wp->w_p_wrap)
2892 txtcol -= wp->w_skipcol;
2893 else
2894 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002895 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002896 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002897 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002898 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900
Bram Moolenaar02631462017-09-22 15:20:32 +02002901 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
2902 (int)wp->w_width, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 /*
2905 * Update w_cline_height and w_cline_folded if the cursor line was
2906 * updated (saves a call to plines() later).
2907 */
2908 if (wp == curwin
2909 && lnum <= curwin->w_cursor.lnum
2910 && lnume >= curwin->w_cursor.lnum)
2911 {
2912 curwin->w_cline_row = row;
2913 curwin->w_cline_height = 1;
2914 curwin->w_cline_folded = TRUE;
2915 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2916 }
2917}
2918
2919/*
2920 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2921 */
2922 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002923copy_text_attr(
2924 int off,
2925 char_u *buf,
2926 int len,
2927 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002929 int i;
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 mch_memmove(ScreenLines + off, buf, (size_t)len);
2932# ifdef FEAT_MBYTE
2933 if (enc_utf8)
2934 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
2935# endif
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002936 for (i = 0; i < len; ++i)
2937 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938}
2939
2940/*
2941 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002942 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 */
2944 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002945fill_foldcolumn(
2946 char_u *p,
2947 win_T *wp,
2948 int closed, /* TRUE of FALSE */
2949 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 int i = 0;
2952 int level;
2953 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002954 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002955 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002958 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 level = win_foldinfo.fi_level;
2961 if (level > 0)
2962 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002963 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002964 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 /* If the column is too narrow, we start at the lowest level that
2967 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002968 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 if (first_level < 1)
2970 first_level = 1;
2971
Bram Moolenaar1c934292015-01-27 16:39:29 +01002972 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
2974 if (win_foldinfo.fi_lnum == lnum
2975 && first_level + i >= win_foldinfo.fi_low_level)
2976 p[i] = '-';
2977 else if (first_level == 1)
2978 p[i] = '|';
2979 else if (first_level + i <= 9)
2980 p[i] = '0' + first_level + i;
2981 else
2982 p[i] = '>';
2983 if (first_level + i == level)
2984 break;
2985 }
2986 }
2987 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002988 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989}
2990#endif /* FEAT_FOLDING */
2991
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01002992#ifdef FEAT_TEXT_PROP
2993static textprop_T *current_text_props = NULL;
2994static buf_T *current_buf = NULL;
2995
2996 static int
2997#ifdef __BORLANDC__
2998_RTLENTRYF
2999#endif
3000text_prop_compare(const void *s1, const void *s2)
3001{
3002 int idx1, idx2;
3003 proptype_T *pt1, *pt2;
3004 colnr_T col1, col2;
3005
3006 idx1 = *(int *)s1;
3007 idx2 = *(int *)s2;
3008 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3009 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3010 if (pt1 == pt2)
3011 return 0;
3012 if (pt1 == NULL)
3013 return -1;
3014 if (pt2 == NULL)
3015 return 1;
3016 if (pt1->pt_priority != pt2->pt_priority)
3017 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3018 col1 = current_text_props[idx1].tp_col;
3019 col2 = current_text_props[idx2].tp_col;
3020 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3021}
3022#endif
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024/*
3025 * Display line "lnum" of window 'wp' on the screen.
3026 * Start at row "startrow", stop when "endrow" is reached.
3027 * wp->w_virtcol needs to be valid.
3028 *
3029 * Return the number of last row the line occupies.
3030 */
3031 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003032win_line(
3033 win_T *wp,
3034 linenr_T lnum,
3035 int startrow,
3036 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003037 int nochange UNUSED, // not updating for changed text
3038 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003040 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3042 int c = 0; /* init for GCC */
3043 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003044#ifdef FEAT_LINEBREAK
3045 long vcol_sbr = -1; /* virtual column after showbreak */
3046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 long vcol_prev = -1; /* "vcol" of previous character */
3048 char_u *line; /* current line */
3049 char_u *ptr; /* current position in "line" */
3050 int row; /* row in the window, excl w_winrow */
3051 int screen_row; /* row on the screen, incl w_winrow */
3052
3053 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3054 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003055 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003056 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003058 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 int extra_attr = 0; /* attributes when n_extra != 0 */
3060 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3061 displaying lcs_eol at end-of-line */
3062 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3063 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3064
3065 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3066 int saved_n_extra = 0;
3067 char_u *saved_p_extra = NULL;
3068 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003069 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 int saved_char_attr = 0;
3071
3072 int n_attr = 0; /* chars with special attr */
3073 int saved_attr2 = 0; /* char_attr saved for n_attr */
3074 int n_attr3 = 0; /* chars with overruling special attr */
3075 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3076
3077 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3078
3079 int fromcol, tocol; /* start/end of inverting */
3080 int fromcol_prev = -2; /* start of inverting after cursor */
3081 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003083 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 pos_T pos;
3085 long v;
3086
3087 int char_attr = 0; /* attributes for next character */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003088 int attr_pri = FALSE; /* char_attr has priority */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 int area_highlighting = FALSE; /* Visual or incsearch highlighting
3090 in this line */
3091 int attr = 0; /* attributes for area highlighting */
3092 int area_attr = 0; /* attributes desired by highlighting */
3093 int search_attr = 0; /* attributes desired by 'hlsearch' */
3094#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003095 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 int syntax_attr = 0; /* attributes desired by syntax */
3097 int has_syntax = FALSE; /* this buffer has syntax highl. */
3098 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003099 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003100 int draw_color_col = FALSE; /* highlight colorcolumn */
3101 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003102#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003103#ifdef FEAT_TEXT_PROP
3104 int text_prop_count;
3105 int text_prop_next = 0; // next text property to use
3106 textprop_T *text_props = NULL;
3107 int *text_prop_idxs = NULL;
3108 int text_props_active = 0;
3109 proptype_T *text_prop_type = NULL;
3110 int text_prop_attr = 0;
3111#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003112#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003113 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003114# define SPWORDLEN 150
3115 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003116 int nextlinecol = 0; /* column where nextline[] starts */
3117 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003118 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003119 int spell_attr = 0; /* attributes desired by spelling */
3120 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003121 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3122 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003123 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003124 static int cap_col = -1; /* column to check for Cap word */
3125 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003126 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003128 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129#ifdef FEAT_MBYTE
3130 int multi_attr = 0; /* attributes desired by multibyte */
3131 int mb_l = 1; /* multi-byte byte length */
3132 int mb_c = 0; /* decoded multi-byte character */
3133 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003134 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135#endif
3136#ifdef FEAT_DIFF
3137 int filler_lines; /* nr of filler lines to be drawn */
3138 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003139 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int change_start = MAXCOL; /* first col of changed area */
3141 int change_end = -1; /* last col of changed area */
3142#endif
3143 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3144#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003145 int need_showbreak = FALSE; /* overlong line, skipping first x
3146 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003148#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003149 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003151 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152#endif
3153#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003154 matchitem_T *cur; /* points to the match list */
3155 match_T *shl; /* points to search_hl or a match */
3156 int shl_flag; /* flag to indicate whether search_hl
3157 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003158 int pos_inprogress; /* marks that position match search is
3159 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003160 int prevcol_hl_flag; /* flag to indicate whether prevcol
3161 equals startcol of search_hl or one
3162 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
3164#ifdef FEAT_ARABIC
3165 int prev_c = 0; /* previous Arabic character */
3166 int prev_c1 = 0; /* first composing char for prev_c */
3167#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003168#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003169 int did_line_attr = 0;
3170#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003171#ifdef FEAT_TERMINAL
3172 int get_term_attr = FALSE;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02003173 int term_attr = 0; /* background for terminal window */
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 /* draw_state: items that are drawn in sequence: */
3177#define WL_START 0 /* nothing done yet */
3178#ifdef FEAT_CMDWIN
3179# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3180#else
3181# define WL_CMDLINE WL_START
3182#endif
3183#ifdef FEAT_FOLDING
3184# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3185#else
3186# define WL_FOLD WL_CMDLINE
3187#endif
3188#ifdef FEAT_SIGNS
3189# define WL_SIGN WL_FOLD + 1 /* column for signs */
3190#else
3191# define WL_SIGN WL_FOLD /* column for signs */
3192#endif
3193#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003194#ifdef FEAT_LINEBREAK
3195# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003197# define WL_BRI WL_NR
3198#endif
3199#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3200# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3201#else
3202# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203#endif
3204#define WL_LINE WL_SBR + 1 /* text in the line */
3205 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003206#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 int feedback_col = 0;
3208 int feedback_old_attr = -1;
3209#endif
3210
Bram Moolenaar860cae12010-06-05 23:22:07 +02003211#ifdef FEAT_CONCEAL
3212 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003213 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003214 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003215 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003216 int is_concealing = FALSE;
3217 int boguscols = 0; /* nonexistent columns added to force
3218 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003219 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003220 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003221 int match_conc = 0; /* cchar for match functions */
3222 int has_match_conc = 0; /* match wants to conceal */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003223 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003224# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003225# define FIX_FOR_BOGUSCOLS \
3226 { \
3227 n_extra += vcol_off; \
3228 vcol -= vcol_off; \
3229 vcol_off = 0; \
3230 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003231 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003232 boguscols = 0; \
3233 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003234#else
3235# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 if (startrow > endrow) /* past the end already! */
3239 return startrow;
3240
3241 row = startrow;
3242 screen_row = row + W_WINROW(wp);
3243
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003244 if (!number_only)
3245 {
3246 /*
3247 * To speed up the loop below, set extra_check when there is linebreak,
3248 * trailing white space and/or syntax processing to be done.
3249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003251 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252#endif
3253#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003254 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003255# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003256 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003257# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003258 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003260 /* Prepare for syntax highlighting in this line. When there is an
3261 * error, stop syntax highlighting. */
3262 save_did_emsg = did_emsg;
3263 did_emsg = FALSE;
3264 syntax_start(wp, lnum);
3265 if (did_emsg)
3266 wp->w_s->b_syn_error = TRUE;
3267 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003268 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003269 did_emsg = save_did_emsg;
3270#ifdef SYN_TIME_LIMIT
3271 if (!wp->w_s->b_syn_slow)
3272#endif
3273 {
3274 has_syntax = TRUE;
3275 extra_check = TRUE;
3276 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003279
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003280 /* Check for columns to display for 'colorcolumn'. */
3281 color_cols = wp->w_p_cc_cols;
3282 if (color_cols != NULL)
3283 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003284#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003285
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003286#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003287 if (term_show_buffer(wp->w_buffer))
3288 {
3289 extra_check = TRUE;
3290 get_term_attr = TRUE;
3291 term_attr = term_get_attr(wp->w_buffer, lnum, -1);
3292 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003293#endif
3294
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003295#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003296 if (wp->w_p_spell
3297 && *wp->w_s->b_p_spl != NUL
3298 && wp->w_s->b_langp.ga_len > 0
3299 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003300 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003301 /* Prepare for spell checking. */
3302 has_spell = TRUE;
3303 extra_check = TRUE;
3304
Bram Moolenaar7701f302018-10-02 21:20:32 +02003305 /* Get the start of the next line, so that words that wrap to the
3306 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003307 * Trick: skip a few chars for C/shell/Vim comments */
3308 nextline[SPWORDLEN] = NUL;
3309 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3310 {
3311 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3312 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3313 }
3314
Bram Moolenaar7701f302018-10-02 21:20:32 +02003315 /* When a word wrapped from the previous line the start of the
3316 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003317 if (lnum == checked_lnum)
3318 cur_checked_col = checked_col;
3319 checked_lnum = 0;
3320
3321 /* When there was a sentence end in the previous line may require a
3322 * word starting with capital in this line. In line 1 always check
3323 * the first word. */
3324 if (lnum != capcol_lnum)
3325 cap_col = -1;
3326 if (lnum == 1)
3327 cap_col = 0;
3328 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#endif
3331
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003332 /*
3333 * handle visual active in this window
3334 */
3335 fromcol = -10;
3336 tocol = MAXCOL;
3337 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 /* Visual is after curwin->w_cursor */
3340 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003342 top = &curwin->w_cursor;
3343 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003345 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003347 top = &VIsual;
3348 bot = &curwin->w_cursor;
3349 }
3350 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3351 if (VIsual_mode == Ctrl_V) /* block mode */
3352 {
3353 if (lnum_in_visual_area)
3354 {
3355 fromcol = wp->w_old_cursor_fcol;
3356 tocol = wp->w_old_cursor_lcol;
3357 }
3358 }
3359 else /* non-block mode */
3360 {
3361 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003363 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003365 if (VIsual_mode == 'V') /* linewise */
3366 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else
3368 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003369 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3370 if (gchar_pos(top) == NUL)
3371 tocol = fromcol + 1;
3372 }
3373 }
3374 if (VIsual_mode != 'V' && lnum == bot->lnum)
3375 {
3376 if (*p_sel == 'e' && bot->col == 0
3377#ifdef FEAT_VIRTUALEDIT
3378 && bot->coladd == 0
3379#endif
3380 )
3381 {
3382 fromcol = -10;
3383 tocol = MAXCOL;
3384 }
3385 else if (bot->col == MAXCOL)
3386 tocol = MAXCOL;
3387 else
3388 {
3389 pos = *bot;
3390 if (*p_sel == 'e')
3391 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3392 else
3393 {
3394 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3395 ++tocol;
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 }
3399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 /* Check if the character under the cursor should not be inverted */
3402 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003403#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003404 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003405#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003406 )
3407 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003409 /* if inverting in this line set area_highlighting */
3410 if (fromcol >= 0)
3411 {
3412 area_highlighting = TRUE;
3413 attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003415 if ((clip_star.available && !clip_star.owned
3416 && clip_isautosel_star())
3417 || (clip_plus.available && !clip_plus.owned
3418 && clip_isautosel_plus()))
3419 attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003424 /*
3425 * handle 'incsearch' and ":s///c" highlighting
3426 */
3427 else if (highlight_match
3428 && wp == curwin
3429 && lnum >= curwin->w_cursor.lnum
3430 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003432 if (lnum == curwin->w_cursor.lnum)
3433 getvcol(curwin, &(curwin->w_cursor),
3434 (colnr_T *)&fromcol, NULL, NULL);
3435 else
3436 fromcol = 0;
3437 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3438 {
3439 pos.lnum = lnum;
3440 pos.col = search_match_endcol;
3441 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3442 }
3443 else
3444 tocol = MAXCOL;
3445 /* do at least one character; happens when past end of line */
3446 if (fromcol == tocol)
3447 tocol = fromcol + 1;
3448 area_highlighting = TRUE;
3449 attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452
3453#ifdef FEAT_DIFF
3454 filler_lines = diff_check(wp, lnum);
3455 if (filler_lines < 0)
3456 {
3457 if (filler_lines == -1)
3458 {
3459 if (diff_find_change(wp, lnum, &change_start, &change_end))
3460 diff_hlf = HLF_ADD; /* added line */
3461 else if (change_start == 0)
3462 diff_hlf = HLF_TXD; /* changed text */
3463 else
3464 diff_hlf = HLF_CHD; /* changed line */
3465 }
3466 else
3467 diff_hlf = HLF_ADD; /* added line */
3468 filler_lines = 0;
3469 area_highlighting = TRUE;
3470 }
3471 if (lnum == wp->w_topline)
3472 filler_lines = wp->w_topfill;
3473 filler_todo = filler_lines;
3474#endif
3475
3476#ifdef LINE_ATTR
3477# ifdef FEAT_SIGNS
3478 /* If this line has a sign with line highlighting set line_attr. */
3479 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3480 if (v != 0)
3481 line_attr = sign_get_attr((int)v, TRUE);
3482# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003483# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003485 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003486 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487# endif
3488 if (line_attr != 0)
3489 area_highlighting = TRUE;
3490#endif
3491
3492 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3493 ptr = line;
3494
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003495#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003496 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003497 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003498 /* For checking first word with a capital skip white space. */
3499 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003500 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003501
Bram Moolenaar30abd282005-06-22 22:35:10 +00003502 /* To be able to spell-check over line boundaries copy the end of the
3503 * current line into nextline[]. Above the start of the next line was
3504 * copied to nextline[SPWORDLEN]. */
3505 if (nextline[SPWORDLEN] == NUL)
3506 {
3507 /* No next line or it is empty. */
3508 nextlinecol = MAXCOL;
3509 nextline_idx = 0;
3510 }
3511 else
3512 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003513 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003514 if (v < SPWORDLEN)
3515 {
3516 /* Short line, use it completely and append the start of the
3517 * next line. */
3518 nextlinecol = 0;
3519 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003520 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003521 nextline_idx = v + 1;
3522 }
3523 else
3524 {
3525 /* Long line, use only the last SPWORDLEN bytes. */
3526 nextlinecol = v - SPWORDLEN;
3527 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3528 nextline_idx = SPWORDLEN + 1;
3529 }
3530 }
3531 }
3532#endif
3533
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003534 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003536 if (lcs_space || lcs_trail)
3537 extra_check = TRUE;
3538 /* find start of trailing whitespace */
3539 if (lcs_trail)
3540 {
3541 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003542 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003543 --trailcol;
3544 trailcol += (colnr_T) (ptr - line);
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547
3548 /*
3549 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3550 * first character to be displayed.
3551 */
3552 if (wp->w_p_wrap)
3553 v = wp->w_skipcol;
3554 else
3555 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003556 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558#ifdef FEAT_MBYTE
3559 char_u *prev_ptr = ptr;
3560#endif
3561 while (vcol < v && *ptr != NUL)
3562 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003563 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 vcol += c;
3565#ifdef FEAT_MBYTE
3566 prev_ptr = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#endif
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003568 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003571 /* When:
3572 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003573 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003574 * - 'virtualedit' is set, or
3575 * - the visual mode is active,
3576 * the end of the line may be before the start of the displayed part.
3577 */
3578 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003579#ifdef FEAT_SYN_HL
3580 wp->w_p_cuc || draw_color_col ||
3581#endif
3582#ifdef FEAT_VIRTUALEDIT
3583 virtual_active() ||
3584#endif
3585 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 vcol = v;
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 /* Handle a character that's not completely on the screen: Put ptr at
3591 * that character but skip the first few screen characters. */
3592 if (vcol > v)
3593 {
3594 vcol -= c;
3595#ifdef FEAT_MBYTE
3596 ptr = prev_ptr;
3597#else
3598 --ptr;
3599#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003600 /* If the character fits on the screen, don't need to skip it.
3601 * Except for a TAB. */
3602 if ((
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003603#ifdef FEAT_MBYTE
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003604 (*mb_ptr2cells)(ptr) >= c ||
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003605#endif
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003606 *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003607 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609
3610 /*
3611 * Adjust for when the inverted text is before the screen,
3612 * and when the start of the inverted text is before the screen.
3613 */
3614 if (tocol <= vcol)
3615 fromcol = 0;
3616 else if (fromcol >= 0 && fromcol < vcol)
3617 fromcol = vcol;
3618
3619#ifdef FEAT_LINEBREAK
3620 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3621 if (wp->w_p_wrap)
3622 need_showbreak = TRUE;
3623#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003624#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003625 /* When spell checking a word we need to figure out the start of the
3626 * word and if it's badly spelled or not. */
3627 if (has_spell)
3628 {
3629 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003630 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003631 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003632
3633 pos = wp->w_cursor;
3634 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003635 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003636 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003637
3638 /* spell_move_to() may call ml_get() and make "line" invalid */
3639 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3640 ptr = line + linecol;
3641
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003642 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003643 {
3644 /* no bad word found at line start, don't check until end of a
3645 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003646 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003647 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003648 }
3649 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003650 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003651 /* bad word found, use attributes until end of word */
3652 word_end = wp->w_cursor.col + len + 1;
3653
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003654 /* Turn index into actual attributes. */
3655 if (spell_hlf != HLF_COUNT)
3656 spell_attr = highlight_attr[spell_hlf];
3657 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003658 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003659
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003660# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003661 /* Need to restart syntax highlighting for this line. */
3662 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003663 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003664# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003665 }
3666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668
3669 /*
3670 * Correct highlighting for cursor that can't be disabled.
3671 * Avoids having to check this for each character.
3672 */
3673 if (fromcol >= 0)
3674 {
3675 if (noinvcur)
3676 {
3677 if ((colnr_T)fromcol == wp->w_virtcol)
3678 {
3679 /* highlighting starts at cursor, let it start just after the
3680 * cursor */
3681 fromcol_prev = fromcol;
3682 fromcol = -1;
3683 }
3684 else if ((colnr_T)fromcol < wp->w_virtcol)
3685 /* restart highlighting after the cursor */
3686 fromcol_prev = wp->w_virtcol;
3687 }
3688 if (fromcol >= tocol)
3689 fromcol = -1;
3690 }
3691
3692#ifdef FEAT_SEARCH_EXTRA
3693 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003694 * Handle highlighting the last used search pattern and matches.
3695 * Do this for both search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003697 cur = wp->w_match_head;
3698 shl_flag = FALSE;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003699 while ((cur != NULL || shl_flag == FALSE) && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003701 if (shl_flag == FALSE)
3702 {
3703 shl = &search_hl;
3704 shl_flag = TRUE;
3705 }
3706 else
3707 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003708 shl->startcol = MAXCOL;
3709 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003711 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003712 v = (long)(ptr - line);
3713 if (cur != NULL)
3714 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003715 next_search_hl(wp, shl, lnum, (colnr_T)v,
3716 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003717
3718 /* Need to get the line again, a multi-line regexp may have made it
3719 * invalid. */
3720 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3721 ptr = line + v;
3722
3723 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003725 if (shl->lnum == lnum)
3726 shl->startcol = shl->rm.startpos[0].col;
3727 else
3728 shl->startcol = 0;
3729 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3730 - shl->rm.startpos[0].lnum)
3731 shl->endcol = shl->rm.endpos[0].col;
3732 else
3733 shl->endcol = MAXCOL;
3734 /* Highlight one character for an empty match. */
3735 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737#ifdef FEAT_MBYTE
Bram Moolenaarb3414592014-06-17 17:48:32 +02003738 if (has_mbyte && line[shl->endcol] != NUL)
3739 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3740 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02003742 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003744 if ((long)shl->startcol < v) /* match at leftcol */
3745 {
3746 shl->attr_cur = shl->attr;
3747 search_attr = shl->attr;
3748 }
3749 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003751 if (shl != &search_hl && cur != NULL)
3752 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754#endif
3755
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003756#ifdef FEAT_SYN_HL
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02003757 /* Cursor line highlighting for 'cursorline' in the current window. Not
3758 * when Visual mode is active, because it's not clear what is selected
3759 * then. */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003760 if (wp->w_p_cul && lnum == wp->w_cursor.lnum
Bram Moolenaarb3414592014-06-17 17:48:32 +02003761 && !(wp == curwin && VIsual_active))
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003762 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003763 line_attr = HL_ATTR(HLF_CUL);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003764 area_highlighting = TRUE;
3765 }
3766#endif
3767
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003768#ifdef FEAT_TEXT_PROP
3769 {
3770 char_u *prop_start;
3771
3772 text_prop_count = get_text_props(wp->w_buffer, lnum,
3773 &prop_start, FALSE);
3774 if (text_prop_count > 0)
3775 {
3776 // Make a copy of the properties, so that they are properly
3777 // aligned.
3778 text_props = (textprop_T *)alloc(
3779 text_prop_count * sizeof(textprop_T));
3780 if (text_props != NULL)
3781 mch_memmove(text_props, prop_start,
3782 text_prop_count * sizeof(textprop_T));
3783
3784 // Allocate an array for the indexes.
3785 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3786 area_highlighting = TRUE;
3787 extra_check = TRUE;
3788 }
3789 }
3790#endif
3791
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003792 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 col = 0;
3794#ifdef FEAT_RIGHTLEFT
3795 if (wp->w_p_rl)
3796 {
3797 /* Rightleft window: process the text in the normal direction, but put
3798 * it in current_ScreenLine[] from right to left. Start at the
3799 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003800 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 off += col;
3802 }
3803#endif
3804
3805 /*
3806 * Repeat for the whole displayed line.
3807 */
3808 for (;;)
3809 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003810#ifdef FEAT_CONCEAL
Bram Moolenaar4d585022016-04-14 19:50:22 +02003811 has_match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 /* Skip this quickly when working on the text. */
3814 if (draw_state != WL_LINE)
3815 {
3816#ifdef FEAT_CMDWIN
3817 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3818 {
3819 draw_state = WL_CMDLINE;
3820 if (cmdwin_type != 0 && wp == curwin)
3821 {
3822 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003824 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003825 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003826 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 }
3829#endif
3830
3831#ifdef FEAT_FOLDING
3832 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3833 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003834 int fdc = compute_foldcolumn(wp, 0);
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003837 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003839 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003840 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003841 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003842 p_extra_free = alloc(12 + 1);
3843
3844 if (p_extra_free != NULL)
3845 {
3846 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3847 n_extra = fdc;
3848 p_extra_free[n_extra] = NUL;
3849 p_extra = p_extra_free;
3850 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003851 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003852 char_attr = HL_ATTR(HLF_FC);
Bram Moolenaar62706602016-12-09 19:28:48 +01003853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 }
3856#endif
3857
3858#ifdef FEAT_SIGNS
3859 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3860 {
3861 draw_state = WL_SIGN;
3862 /* Show the sign column when there are any signs in this
3863 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003864 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003866 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003868 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869# endif
3870
3871 /* Draw two cells with the sign value or blank. */
3872 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003873 c_final = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003874 char_attr = HL_ATTR(HLF_SC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 n_extra = 2;
3876
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003877 if (row == startrow
3878#ifdef FEAT_DIFF
3879 + filler_lines && filler_todo <= 0
3880#endif
3881 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
3883 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3884 SIGN_TEXT);
3885# ifdef FEAT_SIGN_ICONS
3886 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3887 SIGN_ICON);
3888 if (gui.in_use && icon_sign != 0)
3889 {
3890 /* Use the image in this position. */
3891 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003892 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893# ifdef FEAT_NETBEANS_INTG
3894 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003897 c_final = NUL;
3898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899# endif
3900 char_attr = icon_sign;
3901 }
3902 else
3903# endif
3904 if (text_sign != 0)
3905 {
3906 p_extra = sign_get_text(text_sign);
3907 if (p_extra != NULL)
3908 {
3909 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003910 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003911 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 char_attr = sign_get_attr(text_sign, FALSE);
3914 }
3915 }
3916 }
3917 }
3918#endif
3919
3920 if (draw_state == WL_NR - 1 && n_extra == 0)
3921 {
3922 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003923 /* Display the absolute or relative line number. After the
3924 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3925 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 && (row == startrow
3927#ifdef FEAT_DIFF
3928 + filler_lines
3929#endif
3930 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3931 {
3932 /* Draw the line number (empty space after wrapping). */
3933 if (row == startrow
3934#ifdef FEAT_DIFF
3935 + filler_lines
3936#endif
3937 )
3938 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003939 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003940 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003941
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003942 if (wp->w_p_nu && !wp->w_p_rnu)
3943 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003944 num = (long)lnum;
3945 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003946 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003947 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003948 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003949 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003950 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003951 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003952 num = lnum;
3953 fmt = "%-*ld ";
3954 }
3955 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003956
Bram Moolenaar700e7342013-01-30 12:31:36 +01003957 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003958 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (wp->w_skipcol > 0)
3960 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3961 *p_extra = '-';
3962#ifdef FEAT_RIGHTLEFT
3963 if (wp->w_p_rl) /* reverse line numbers */
3964 rl_mirror(extra);
3965#endif
3966 p_extra = extra;
3967 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003968 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
3970 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01003971 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003973 c_final = NUL;
3974 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00003975 n_extra = number_width(wp) + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003976 char_attr = HL_ATTR(HLF_N);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003977#ifdef FEAT_SYN_HL
3978 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01003979 * the current line differently.
3980 * TODO: Can we use CursorLine instead of CursorLineNr
3981 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02003982 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003983 && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01003984 char_attr = HL_ATTR(HLF_CLN);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 }
3988
Bram Moolenaar597a4222014-06-25 14:39:50 +02003989#ifdef FEAT_LINEBREAK
3990 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
3991 && n_extra == 0 && *p_sbr != NUL)
3992 /* draw indent after showbreak value */
3993 draw_state = WL_BRI;
3994 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
3995 /* After the showbreak, draw the breakindent */
3996 draw_state = WL_BRI - 1;
3997
3998 /* draw 'breakindent': indent wrapped text accordingly */
3999 if (draw_state == WL_BRI - 1 && n_extra == 0)
4000 {
4001 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004002 /* if need_showbreak is set, breakindent also applies */
4003 if (wp->w_p_bri && n_extra == 0
4004 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004005# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004006 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004007# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004008 )
4009 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004010 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004011# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004012 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004013 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004014 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004015# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004016 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4017 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004018 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004019# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004020 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004021# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004022 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004023 c_extra = ' ';
4024 n_extra = get_breakindent_win(wp,
4025 ml_get_buf(wp->w_buffer, lnum, FALSE));
4026 /* Correct end of highlighted area for 'breakindent',
4027 * required when 'linebreak' is also set. */
4028 if (tocol == vcol)
4029 tocol += n_extra;
4030 }
4031 }
4032#endif
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4035 if (draw_state == WL_SBR - 1 && n_extra == 0)
4036 {
4037 draw_state = WL_SBR;
4038# ifdef FEAT_DIFF
4039 if (filler_todo > 0)
4040 {
4041 /* Draw "deleted" diff line(s). */
4042 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004045 c_final = NUL;
4046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004048 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004050 c_final = NUL;
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052# ifdef FEAT_RIGHTLEFT
4053 if (wp->w_p_rl)
4054 n_extra = col + 1;
4055 else
4056# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004057 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004058 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060# endif
4061# ifdef FEAT_LINEBREAK
4062 if (*p_sbr != NUL && need_showbreak)
4063 {
4064 /* Draw 'showbreak' at the start of each broken line. */
4065 p_extra = p_sbr;
4066 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004067 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004069 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004071 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 /* Correct end of highlighted area for 'showbreak',
4073 * required when 'linebreak' is also set. */
4074 if (tocol == vcol)
4075 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004076#ifdef FEAT_SYN_HL
4077 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004078 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004079 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004080 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083# endif
4084 }
4085#endif
4086
4087 if (draw_state == WL_LINE - 1 && n_extra == 0)
4088 {
4089 draw_state = WL_LINE;
4090 if (saved_n_extra)
4091 {
4092 /* Continue item from end of wrapped line. */
4093 n_extra = saved_n_extra;
4094 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004095 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 p_extra = saved_p_extra;
4097 char_attr = saved_char_attr;
4098 }
4099 else
4100 char_attr = 0;
4101 }
4102 }
4103
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004104 // When still displaying '$' of change command, stop at cursor.
4105 // When only displaying the (relative) line number and that's done,
4106 // stop here.
4107 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004108 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109#ifdef FEAT_DIFF
4110 && filler_todo <= 0
4111#endif
4112 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004113 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004115 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaarc0aa4822017-07-16 14:04:29 +02004116 HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004117 /* Pretend we have finished updating the window. Except when
4118 * 'cursorcolumn' is set. */
4119#ifdef FEAT_SYN_HL
4120 if (wp->w_p_cuc)
4121 row = wp->w_cline_row + wp->w_cline_height;
4122 else
4123#endif
4124 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 break;
4126 }
4127
Bram Moolenaar637532b2019-01-03 21:44:40 +01004128 if (draw_state == WL_LINE && (area_highlighting
4129#ifdef FEAT_SPELL
4130 || has_spell
4131#endif
4132 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134 /* handle Visual or match highlighting in this line */
4135 if (vcol == fromcol
4136#ifdef FEAT_MBYTE
4137 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4138 && (*mb_ptr2cells)(ptr) > 1)
4139#endif
4140 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004141 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 && vcol < tocol))
4143 area_attr = attr; /* start highlighting */
4144 else if (area_attr != 0
4145 && (vcol == tocol
4146 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149#ifdef FEAT_SEARCH_EXTRA
4150 if (!n_extra)
4151 {
4152 /*
4153 * Check for start/end of search pattern match.
4154 * After end, check for start/end of next match.
4155 * When another match, have to check for start again.
4156 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004157 * Do this for 'search_hl' and the match list (ordered by
4158 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004160 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004161 cur = wp->w_match_head;
4162 shl_flag = FALSE;
4163 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004165 if (shl_flag == FALSE
4166 && ((cur != NULL
4167 && cur->priority > SEARCH_HL_PRIORITY)
4168 || cur == NULL))
4169 {
4170 shl = &search_hl;
4171 shl_flag = TRUE;
4172 }
4173 else
4174 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004175 if (cur != NULL)
4176 cur->pos.cur = 0;
4177 pos_inprogress = TRUE;
4178 while (shl->rm.regprog != NULL
4179 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004181 if (shl->startcol != MAXCOL
4182 && v >= (long)shl->startcol
4183 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004185#ifdef FEAT_MBYTE
4186 int tmp_col = v + MB_PTR2LEN(ptr);
4187
4188 if (shl->endcol < tmp_col)
4189 shl->endcol = tmp_col;
4190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004192#ifdef FEAT_CONCEAL
4193 if (cur != NULL && syn_name2id((char_u *)"Conceal")
4194 == cur->hlg_id)
4195 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004196 has_match_conc =
4197 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004198 match_conc = cur->conceal_char;
4199 }
4200 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004201 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004204 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
4206 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004207 next_search_hl(wp, shl, lnum, (colnr_T)v,
4208 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004209 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004210 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 /* Need to get the line again, a multi-line regexp
4213 * may have made it invalid. */
4214 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4215 ptr = line + v;
4216
4217 if (shl->lnum == lnum)
4218 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004219 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004221 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004223 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004225 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 /* highlight empty match, try again after
4228 * it */
4229#ifdef FEAT_MBYTE
4230 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004231 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004232 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 else
4234#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004235 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237
4238 /* Loop to check if the match starts at the
4239 * current position */
4240 continue;
4241 }
4242 }
4243 break;
4244 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004245 if (shl != &search_hl && cur != NULL)
4246 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004248
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004249 /* Use attributes from match with highest priority among
4250 * 'search_hl' and the match list. */
4251 search_attr = search_hl.attr_cur;
4252 cur = wp->w_match_head;
4253 shl_flag = FALSE;
4254 while (cur != NULL || shl_flag == FALSE)
4255 {
4256 if (shl_flag == FALSE
4257 && ((cur != NULL
4258 && cur->priority > SEARCH_HL_PRIORITY)
4259 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004260 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004261 shl = &search_hl;
4262 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004263 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004264 else
4265 shl = &cur->hl;
4266 if (shl->attr_cur != 0)
4267 search_attr = shl->attr_cur;
4268 if (shl != &search_hl && cur != NULL)
4269 cur = cur->next;
4270 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004271 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004272 if (*ptr == NUL && (did_line_attr >= 1
4273 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004274 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276#endif
4277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004279 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004281 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4282 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004284 if (diff_hlf == HLF_TXD && ptr - line > change_end
4285 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004287 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004288 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004289 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004292
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004293#ifdef FEAT_TEXT_PROP
4294 if (text_props != NULL)
4295 {
4296 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004297 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004298
4299 // Check if any active property ends.
4300 for (pi = 0; pi < text_props_active; ++pi)
4301 {
4302 int tpi = text_prop_idxs[pi];
4303
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004304 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004305 + text_props[tpi].tp_len)
4306 {
4307 if (pi + 1 < text_props_active)
4308 mch_memmove(text_prop_idxs + pi,
4309 text_prop_idxs + pi + 1,
4310 sizeof(int)
4311 * (text_props_active - (pi + 1)));
4312 --text_props_active;
4313 --pi;
4314 }
4315 }
4316
4317 // Add any text property that starts in this column.
4318 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004319 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004320 text_prop_idxs[text_props_active++] = text_prop_next++;
4321
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004322 text_prop_attr = 0;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004323 if (text_props_active > 0)
4324 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004325 // Sort the properties on priority and/or starting last.
4326 // Then combine the attributes, highest priority last.
4327 current_text_props = text_props;
4328 current_buf = wp->w_buffer;
4329 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4330 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004331
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004332 for (pi = 0; pi < text_props_active; ++pi)
4333 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004334 int tpi = text_prop_idxs[pi];
4335 proptype_T *pt = text_prop_type_by_id(wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004336
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004337 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004338 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004339 int pt_attr = syn_id2attr(pt->pt_hl_id);
4340
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004341 text_prop_type = pt;
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004342 if (text_prop_attr == 0)
4343 text_prop_attr = pt_attr;
4344 else
4345 text_prop_attr = hl_combine_attr(text_prop_attr, pt_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004346 }
4347 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004348 }
4349 }
4350#endif
4351
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004352 /* Decide which of the highlight attributes to use. */
4353 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004354#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004355 if (area_attr != 0)
4356 char_attr = hl_combine_attr(line_attr, area_attr);
4357 else if (search_attr != 0)
4358 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004359 /* Use line_attr when not in the Visual or 'incsearch' area
4360 * (area_attr may be 0 when "noinvcur" is set). */
4361 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004362 || vcol < fromcol || vcol_prev < fromcol_prev
4363 || vcol >= tocol))
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004364 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004365#else
4366 if (area_attr != 0)
4367 char_attr = area_attr;
4368 else if (search_attr != 0)
4369 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004370#endif
4371 else
4372 {
4373 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004374#ifdef FEAT_TEXT_PROP
4375 if (text_prop_type != NULL)
4376 char_attr = text_prop_attr;
4377 else
4378#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004379#ifdef FEAT_SYN_HL
4380 if (has_syntax)
4381 char_attr = syntax_attr;
4382 else
4383#endif
4384 char_attr = 0;
4385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
4387
4388 /*
4389 * Get the next character to put on the screen.
4390 */
4391 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004392 * The "p_extra" points to the extra stuff that is inserted to
4393 * represent special characters (non-printable stuff) and other
4394 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004395 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004396 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4397 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4399 */
4400 if (n_extra > 0)
4401 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004402 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004404 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_MBYTE
4406 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004407 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
4409 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004410 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004411 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413 else
4414 mb_utf8 = FALSE;
4415#endif
4416 }
4417 else
4418 {
4419 c = *p_extra;
4420#ifdef FEAT_MBYTE
4421 if (has_mbyte)
4422 {
4423 mb_c = c;
4424 if (enc_utf8)
4425 {
4426 /* If the UTF-8 character is more than one byte:
4427 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004428 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 mb_utf8 = FALSE;
4430 if (mb_l > n_extra)
4431 mb_l = 1;
4432 else if (mb_l > 1)
4433 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004434 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004436 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 }
4439 else
4440 {
4441 /* if this is a DBCS character, put it in "mb_c" */
4442 mb_l = MB_BYTE2LEN(c);
4443 if (mb_l >= n_extra)
4444 mb_l = 1;
4445 else if (mb_l > 1)
4446 mb_c = (c << 8) + p_extra[1];
4447 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004448 if (mb_l == 0) /* at the NUL at end-of-line */
4449 mb_l = 1;
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /* If a double-width char doesn't fit display a '>' in the
4452 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004453 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454# ifdef FEAT_RIGHTLEFT
4455 wp->w_p_rl ? (col <= 0) :
4456# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004457 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 && (*mb_char2cells)(mb_c) == 2)
4459 {
4460 c = '>';
4461 mb_c = c;
4462 mb_l = 1;
4463 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004464 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 /* put the pointer back to output the double-width
4466 * character at the start of the next line. */
4467 ++n_extra;
4468 --p_extra;
4469 }
4470 else
4471 {
4472 n_extra -= mb_l - 1;
4473 p_extra += mb_l - 1;
4474 }
4475 }
4476#endif
4477 ++p_extra;
4478 }
4479 --n_extra;
4480 }
4481 else
4482 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004483#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004484 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004485#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004486
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004487 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004488 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 /*
4490 * Get a character from the line itself.
4491 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004492 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004493#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004494 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496#ifdef FEAT_MBYTE
4497 if (has_mbyte)
4498 {
4499 mb_c = c;
4500 if (enc_utf8)
4501 {
4502 /* If the UTF-8 character is more than one byte: Decode it
4503 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004504 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 mb_utf8 = FALSE;
4506 if (mb_l > 1)
4507 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004508 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 /* Overlong encoded ASCII or ASCII with composing char
4510 * is displayed normally, except a NUL. */
4511 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004512 {
4513 c = mb_c;
4514# ifdef FEAT_LINEBREAK
4515 c0 = mb_c;
4516# endif
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004519
4520 /* At start of the line we can have a composing char.
4521 * Draw it as a space with a composing char. */
4522 if (utf_iscomposing(mb_c))
4523 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004524 int i;
4525
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004526 for (i = Screen_mco - 1; i > 0; --i)
4527 u8cc[i] = u8cc[i - 1];
4528 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004529 mb_c = ' ';
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532
4533 if ((mb_l == 1 && c >= 0x80)
4534 || (mb_l >= 1 && mb_c == 0)
4535 || (mb_l > 1 && (!vim_isprintc(mb_c)
Bram Moolenaar11936362007-09-17 20:39:42 +00004536# ifdef UNICODE16
4537 || mb_c >= 0x10000
4538# endif
4539 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
4541 /*
4542 * Illegal UTF-8 byte: display as <xx>.
4543 * Non-BMP character : display as ? or fullwidth ?.
4544 */
Bram Moolenaar11936362007-09-17 20:39:42 +00004545# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (mb_c < 0x10000)
Bram Moolenaar11936362007-09-17 20:39:42 +00004547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004550# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (wp->w_p_rl) /* reverse */
4552 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004553# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
Bram Moolenaar11936362007-09-17 20:39:42 +00004555# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 else if (utf_char2cells(mb_c) != 2)
4557 STRCPY(extra, "?");
4558 else
4559 /* 0xff1f in UTF-8: full-width '?' */
4560 STRCPY(extra, "\357\274\237");
Bram Moolenaar11936362007-09-17 20:39:42 +00004561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
4563 p_extra = extra;
4564 c = *p_extra;
4565 mb_c = mb_ptr2char_adv(&p_extra);
4566 mb_utf8 = (c >= 0x80);
4567 n_extra = (int)STRLEN(p_extra);
4568 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004569 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (area_attr == 0 && search_attr == 0)
4571 {
4572 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004573 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 saved_attr2 = char_attr; /* save current attr */
4575 }
4576 }
4577 else if (mb_l == 0) /* at the NUL at end-of-line */
4578 mb_l = 1;
4579#ifdef FEAT_ARABIC
4580 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4581 {
4582 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004583 int pc, pc1, nc;
4584 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
4586 /* The idea of what is the previous and next
4587 * character depends on 'rightleft'. */
4588 if (wp->w_p_rl)
4589 {
4590 pc = prev_c;
4591 pc1 = prev_c1;
4592 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004593 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 else
4596 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004597 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004599 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 prev_c = mb_c;
4602
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605 else
4606 prev_c = mb_c;
4607#endif
4608 }
4609 else /* enc_dbcs */
4610 {
4611 mb_l = MB_BYTE2LEN(c);
4612 if (mb_l == 0) /* at the NUL at end-of-line */
4613 mb_l = 1;
4614 else if (mb_l > 1)
4615 {
4616 /* We assume a second byte below 32 is illegal.
4617 * Hopefully this is OK for all double-byte encodings!
4618 */
4619 if (ptr[1] >= 32)
4620 mb_c = (c << 8) + ptr[1];
4621 else
4622 {
4623 if (ptr[1] == NUL)
4624 {
4625 /* head byte at end of line */
4626 mb_l = 1;
4627 transchar_nonprint(extra, c);
4628 }
4629 else
4630 {
4631 /* illegal tail byte */
4632 mb_l = 2;
4633 STRCPY(extra, "XX");
4634 }
4635 p_extra = extra;
4636 n_extra = (int)STRLEN(extra) - 1;
4637 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004638 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 c = *p_extra++;
4640 if (area_attr == 0 && search_attr == 0)
4641 {
4642 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004643 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 saved_attr2 = char_attr; /* save current attr */
4645 }
4646 mb_c = c;
4647 }
4648 }
4649 }
4650 /* If a double-width char doesn't fit display a '>' in the
4651 * last column; the character is displayed at the start of the
4652 * next line. */
4653 if ((
4654# ifdef FEAT_RIGHTLEFT
4655 wp->w_p_rl ? (col <= 0) :
4656# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004657 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 && (*mb_char2cells)(mb_c) == 2)
4659 {
4660 c = '>';
4661 mb_c = c;
4662 mb_utf8 = FALSE;
4663 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004664 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /* Put pointer back so that the character will be
4666 * displayed at the start of the next line. */
4667 --ptr;
4668 }
4669 else if (*ptr != NUL)
4670 ptr += mb_l - 1;
4671
4672 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004673 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004674 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004675 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004678 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004679 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 c = ' ';
4681 if (area_attr == 0 && search_attr == 0)
4682 {
4683 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004684 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 saved_attr2 = char_attr; /* save current attr */
4686 }
4687 mb_c = c;
4688 mb_utf8 = FALSE;
4689 mb_l = 1;
4690 }
4691
4692 }
4693#endif
4694 ++ptr;
4695
4696 if (extra_check)
4697 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004698#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004699 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004700#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004701
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004702#ifdef FEAT_TERMINAL
4703 if (get_term_attr)
4704 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004705 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004706
4707 if (!attr_pri)
4708 char_attr = syntax_attr;
4709 else
4710 char_attr = hl_combine_attr(syntax_attr, char_attr);
4711 }
4712#endif
4713
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004714#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004715 // Get syntax attribute, unless still at the start of the line
4716 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004717 v = (long)(ptr - line);
4718 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 /* Get the syntax attribute for the character. If there
4721 * is an error, disable syntax highlighting. */
4722 save_did_emsg = did_emsg;
4723 did_emsg = FALSE;
4724
Bram Moolenaar217ad922005-03-20 22:37:15 +00004725 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004726# ifdef FEAT_SPELL
4727 has_spell ? &can_spell :
4728# endif
4729 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004732 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004733 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004734 has_syntax = FALSE;
4735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 else
4737 did_emsg = save_did_emsg;
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004738#ifdef SYN_TIME_LIMIT
4739 if (wp->w_s->b_syn_slow)
4740 has_syntax = FALSE;
4741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
4743 /* Need to get the line again, a multi-line regexp may
4744 * have made it invalid. */
4745 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4746 ptr = line + v;
4747
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004748# ifdef FEAT_TEXT_PROP
4749 // Text properties overrule syntax highlighting.
4750 if (text_prop_attr == 0)
4751#endif
4752 {
4753 if (!attr_pri)
4754 char_attr = syntax_attr;
4755 else
4756 char_attr = hl_combine_attr(syntax_attr, char_attr);
4757 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004758# ifdef FEAT_CONCEAL
4759 /* no concealing past the end of the line, it interferes
4760 * with line highlighting */
4761 if (c == NUL)
4762 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004763 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004764 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004767#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004768
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004769#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004770 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004771 * Only do this when there is no syntax highlighting, the
4772 * @Spell cluster is not used or the current syntax item
4773 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004774 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004775 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004776 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004777 if (c != 0 && (
4778# ifdef FEAT_SYN_HL
4779 !has_syntax ||
4780# endif
4781 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004782 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004783 char_u *prev_ptr, *p;
4784 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004785 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004786# ifdef FEAT_MBYTE
Bram Moolenaare7566042005-06-17 22:00:15 +00004787 if (has_mbyte)
4788 {
4789 prev_ptr = ptr - mb_l;
4790 v -= mb_l - 1;
4791 }
4792 else
Bram Moolenaar217ad922005-03-20 22:37:15 +00004793# endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004794 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004795
4796 /* Use nextline[] if possible, it has the start of the
4797 * next line concatenated. */
4798 if ((prev_ptr - line) - nextlinecol >= 0)
4799 p = nextline + (prev_ptr - line) - nextlinecol;
4800 else
4801 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004802 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004803 len = spell_check(wp, p, &spell_hlf, &cap_col,
4804 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004805 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004806
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004807 /* In Insert mode only highlight a word that
4808 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004809 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004810 && (State & INSERT) != 0
4811 && wp->w_cursor.lnum == lnum
4812 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004813 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004814 && wp->w_cursor.col < (colnr_T)word_end)
4815 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004816 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004817 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004818 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004819
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004820 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004821 && (p - nextline) + len > nextline_idx)
4822 {
4823 /* Remember that the good word continues at the
4824 * start of the next line. */
4825 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004826 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004827 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004828
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004829 /* Turn index into actual attributes. */
4830 if (spell_hlf != HLF_COUNT)
4831 spell_attr = highlight_attr[spell_hlf];
4832
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004833 if (cap_col > 0)
4834 {
4835 if (p != prev_ptr
4836 && (p - nextline) + cap_col >= nextline_idx)
4837 {
4838 /* Remember that the word in the next line
4839 * must start with a capital. */
4840 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004841 cap_col = (int)((p - nextline) + cap_col
4842 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004843 }
4844 else
4845 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004846 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004847 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004848 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004849 }
4850 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004851 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004852 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004853 char_attr = hl_combine_attr(char_attr, spell_attr);
4854 else
4855 char_attr = hl_combine_attr(spell_attr, char_attr);
4856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#endif
4858#ifdef FEAT_LINEBREAK
4859 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004860 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004862 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004863 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004865# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004866 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004867# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004868 char_u *p = ptr - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004870 mb_off +
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004872 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004873
Bram Moolenaar597a4222014-06-25 14:39:50 +02004874 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004875 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004876 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004877 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004878# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004879 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004880 wp->w_buffer->b_p_vts_array) - 1;
4881# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004882 n_extra = (int)wp->w_buffer->b_p_ts
4883 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004884# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004885
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004886# ifdef FEAT_MBYTE
Bram Moolenaar4df70292015-03-21 14:20:16 +01004887 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004888# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 c_extra = ' ';
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004890# endif
Bram Moolenaar83a52172019-01-16 22:41:54 +01004891 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004892 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004893 {
4894#ifdef FEAT_CONCEAL
4895 if (c == TAB)
4896 /* See "Tab alignment" below. */
4897 FIX_FOR_BOGUSCOLS;
4898#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004899 if (!wp->w_p_list)
4900 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903#endif
4904
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004905 /* 'list': change char 160 to lcs_nbsp and space to lcs_space.
4906 */
4907 if (wp->w_p_list
4908 && (((c == 160
4909#ifdef FEAT_MBYTE
4910 || (mb_utf8 && (mb_c == 160 || mb_c == 0x202f))
4911#endif
4912 ) && lcs_nbsp)
4913 || (c == ' ' && lcs_space && ptr - line <= trailcol)))
4914 {
4915 c = (c == ' ') ? lcs_space : lcs_nbsp;
4916 if (area_attr == 0 && search_attr == 0)
4917 {
4918 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004919 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004920 saved_attr2 = char_attr; /* save current attr */
4921 }
4922#ifdef FEAT_MBYTE
4923 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004924 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004925 {
4926 mb_utf8 = TRUE;
4927 u8cc[0] = 0;
4928 c = 0xc0;
4929 }
4930 else
4931 mb_utf8 = FALSE;
4932#endif
4933 }
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4936 {
4937 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004938 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
4940 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004941 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 saved_attr2 = char_attr; /* save current attr */
4943 }
4944#ifdef FEAT_MBYTE
4945 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004946 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004949 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004950 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
4953 mb_utf8 = FALSE;
4954#endif
4955 }
4956 }
4957
4958 /*
4959 * Handling of non-printable characters.
4960 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01004961 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 /*
4964 * when getting a character from the file, we may have to
4965 * turn it into something else on the way to putting it
4966 * into "ScreenLines".
4967 */
4968 if (c == TAB && (!wp->w_p_list || lcs_tab1))
4969 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004970 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004971 long vcol_adjusted = vcol; /* removed showbreak length */
4972#ifdef FEAT_LINEBREAK
4973 /* only adjust the tab_len, when at the first column
4974 * after the showbreak value was drawn */
4975 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
4976 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
4977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004979#ifdef FEAT_VARTABS
4980 tab_len = tabstop_padding(vcol_adjusted,
4981 wp->w_buffer->b_p_ts,
4982 wp->w_buffer->b_p_vts_array) - 1;
4983#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004984 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004985 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
4986#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01004987
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004988#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02004989 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004990#endif
4991 /* tab amount depends on current column */
4992 n_extra = tab_len;
4993#ifdef FEAT_LINEBREAK
4994 else
4995 {
4996 char_u *p;
4997 int len = n_extra;
4998 int i;
4999 int saved_nextra = n_extra;
5000
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005001#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005002 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005003 /* there are characters to conceal */
5004 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005005 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5006 */
5007 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5008 && n_extra > tab_len)
5009 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005010#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005011
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005012 /* if n_extra > 0, it gives the number of chars, to
5013 * use for a tab, else we need to calculate the width
5014 * for a tab */
5015#ifdef FEAT_MBYTE
5016 len = (tab_len * mb_char2len(lcs_tab2));
5017 if (n_extra > 0)
5018 len += n_extra - tab_len;
5019#endif
5020 c = lcs_tab1;
5021 p = alloc((unsigned)(len + 1));
5022 vim_memset(p, ' ', len);
5023 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005024 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005025 p_extra_free = p;
5026 for (i = 0; i < tab_len; i++)
5027 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005028 if (*p == NUL)
5029 {
5030 tab_len = i;
5031 break;
5032 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005033#ifdef FEAT_MBYTE
5034 mb_char2bytes(lcs_tab2, p);
5035 p += mb_char2len(lcs_tab2);
5036 n_extra += mb_char2len(lcs_tab2)
5037 - (saved_nextra > 0 ? 1 : 0);
5038#else
5039 p[i] = lcs_tab2;
5040#endif
5041 }
5042 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005043#ifdef FEAT_CONCEAL
5044 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5045 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005046 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005047 n_extra -= vcol_off;
5048#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005049 }
5050#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005051#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005052 {
5053 int vc_saved = vcol_off;
5054
5055 /* Tab alignment should be identical regardless of
5056 * 'conceallevel' value. So tab compensates of all
5057 * previous concealed characters, and thus resets
5058 * vcol_off and boguscols accumulated so far in the
5059 * line. Note that the tab can be longer than
5060 * 'tabstop' when there are concealed characters. */
5061 FIX_FOR_BOGUSCOLS;
5062
5063 /* Make sure, the highlighting for the tab char will be
5064 * correctly set further below (effectively reverts the
5065 * FIX_FOR_BOGSUCOLS macro */
5066 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005067 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005068 tab_len += vc_saved;
5069 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071#ifdef FEAT_MBYTE
5072 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5073#endif
5074 if (wp->w_p_list)
5075 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005076 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005077#ifdef FEAT_LINEBREAK
5078 if (wp->w_p_lbr)
5079 c_extra = NUL; /* using p_extra from above */
5080 else
5081#endif
5082 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005083 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005084 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005085 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 saved_attr2 = char_attr; /* save current attr */
5087#ifdef FEAT_MBYTE
5088 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005089 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005092 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005093 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095#endif
5096 }
5097 else
5098 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005099 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 c_extra = ' ';
5101 c = ' ';
5102 }
5103 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005104 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005105 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005106 || ((fromcol >= 0 || fromcol_prev >= 0)
5107 && tocol > vcol
5108 && VIsual_mode != Ctrl_V
5109 && (
5110# ifdef FEAT_RIGHTLEFT
5111 wp->w_p_rl ? (col >= 0) :
5112# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005113 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005114 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005115 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005116 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005117 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005119 /* Display a '$' after the line or highlight an extra
5120 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5122 /* For a diff line the highlighting continues after the
5123 * "$". */
5124 if (
5125# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005126 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127# ifdef LINE_ATTR
5128 &&
5129# endif
5130# endif
5131# ifdef LINE_ATTR
5132 line_attr == 0
5133# endif
5134 )
5135#endif
5136 {
5137#ifdef FEAT_VIRTUALEDIT
5138 /* In virtualedit, visual selections may extend
5139 * beyond end of line. */
5140 if (area_highlighting && virtual_active()
5141 && tocol != MAXCOL && vcol < tocol)
5142 n_extra = 0;
5143 else
5144#endif
5145 {
5146 p_extra = at_end_str;
5147 n_extra = 1;
5148 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005149 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005152 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005153 c = lcs_eol;
5154 else
5155 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 lcs_eol_one = -1;
5157 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005158 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005160 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 n_attr = 1;
5162 }
5163#ifdef FEAT_MBYTE
5164 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005165 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005168 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005169 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 else
5172 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5173#endif
5174 }
5175 else if (c != NUL)
5176 {
5177 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005178 if (n_extra == 0)
5179 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_RIGHTLEFT
5181 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5182 rl_mirror(p_extra); /* reverse "<12>" */
5183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005185 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005186#ifdef FEAT_LINEBREAK
5187 if (wp->w_p_lbr)
5188 {
5189 char_u *p;
5190
5191 c = *p_extra;
5192 p = alloc((unsigned)n_extra + 1);
5193 vim_memset(p, ' ', n_extra);
5194 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5195 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005196 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005197 p_extra_free = p_extra = p;
5198 }
5199 else
5200#endif
5201 {
5202 n_extra = byte2cells(c) - 1;
5203 c = *p_extra++;
5204 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005205 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005208 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 saved_attr2 = char_attr; /* save current attr */
5210 }
5211#ifdef FEAT_MBYTE
5212 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5213#endif
5214 }
5215#ifdef FEAT_VIRTUALEDIT
5216 else if (VIsual_active
5217 && (VIsual_mode == Ctrl_V
5218 || VIsual_mode == 'v')
5219 && virtual_active()
5220 && tocol != MAXCOL
5221 && vcol < tocol
5222 && (
5223# ifdef FEAT_RIGHTLEFT
5224 wp->w_p_rl ? (col >= 0) :
5225# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005226 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
5228 c = ' ';
5229 --ptr; /* put it back at the NUL */
5230 }
5231#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005232#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 else if ((
5234# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005235 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005237# ifdef FEAT_TERMINAL
5238 term_attr != 0 ||
5239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 ) && (
5242# ifdef FEAT_RIGHTLEFT
5243 wp->w_p_rl ? (col >= 0) :
5244# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005245 (col
5246# ifdef FEAT_CONCEAL
5247 - boguscols
5248# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005249 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 /* Highlight until the right side of the window */
5252 c = ' ';
5253 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005254
5255 /* Remember we do the char for line highlighting. */
5256 ++did_line_attr;
5257
5258 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005259 if (line_attr != 0 && char_attr == search_attr
5260 && (did_line_attr > 1
5261 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005262 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263# ifdef FEAT_DIFF
5264 if (diff_hlf == HLF_TXD)
5265 {
5266 diff_hlf = HLF_CHD;
5267 if (attr == 0 || char_attr != attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005268 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005269 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005270 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5271 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005272 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005276# ifdef FEAT_TERMINAL
5277 if (term_attr != 0)
5278 {
5279 char_attr = term_attr;
5280 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5281 char_attr = hl_combine_attr(char_attr,
5282 HL_ATTR(HLF_CUL));
5283 }
5284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286#endif
5287 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005288
5289#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005290 if ( wp->w_p_cole > 0
5291 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaar6561d522015-07-21 15:48:27 +02005292 conceal_cursor_line(wp) )
Bram Moolenaar4d585022016-04-14 19:50:22 +02005293 && ( (syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005294 && !(lnum_in_visual_area
5295 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005296 {
5297 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005298 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005299 && (syn_get_sub_char() != NUL || match_conc
5300 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005301 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005302 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005303 /* First time at this concealed item: display one
5304 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005305 if (match_conc)
5306 c = match_conc;
5307 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005308 c = syn_get_sub_char();
5309 else if (lcs_conceal != NUL)
5310 c = lcs_conceal;
5311 else
5312 c = ' ';
5313
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005314 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005315
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005316 if (n_extra > 0)
5317 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 vcol += n_extra;
5319 if (wp->w_p_wrap && n_extra > 0)
5320 {
5321# ifdef FEAT_RIGHTLEFT
5322 if (wp->w_p_rl)
5323 {
5324 col -= n_extra;
5325 boguscols -= n_extra;
5326 }
5327 else
5328# endif
5329 {
5330 boguscols += n_extra;
5331 col += n_extra;
5332 }
5333 }
5334 n_extra = 0;
5335 n_attr = 0;
5336 }
5337 else if (n_skip == 0)
5338 {
5339 is_concealing = TRUE;
5340 n_skip = 1;
5341 }
5342# ifdef FEAT_MBYTE
5343 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005344 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005345 {
5346 mb_utf8 = TRUE;
5347 u8cc[0] = 0;
5348 c = 0xc0;
5349 }
5350 else
5351 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5352# endif
5353 }
5354 else
5355 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005356 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005357 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005358 }
5359#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005362#ifdef FEAT_CONCEAL
5363 /* In the cursor line and we may be concealing characters: correct
5364 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005365 if (!did_wcol && draw_state == WL_LINE
5366 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005367 && conceal_cursor_line(wp)
5368 && (int)wp->w_virtcol <= vcol + n_skip)
5369 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005370# ifdef FEAT_RIGHTLEFT
5371 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005372 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005373 else
5374# endif
5375 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005376 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005377 did_wcol = TRUE;
5378 }
5379#endif
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 /* Don't override visual selection highlighting. */
5382 if (n_attr > 0
5383 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005384 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 char_attr = extra_attr;
5386
Bram Moolenaar81695252004-12-29 20:58:21 +00005387#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 /* XIM don't send preedit_start and preedit_end, but they send
5389 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5390 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005391 if (p_imst == IM_ON_THE_SPOT
5392 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005393 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 && (State & INSERT)
5395 && !p_imdisable
5396 && im_is_preediting()
5397 && draw_state == WL_LINE)
5398 {
5399 colnr_T tcol;
5400
5401 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005402 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 else
5404 tcol = preedit_end_col;
5405 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5406 {
5407 if (feedback_old_attr < 0)
5408 {
5409 feedback_col = 0;
5410 feedback_old_attr = char_attr;
5411 }
5412 char_attr = im_get_feedback_attr(feedback_col);
5413 if (char_attr < 0)
5414 char_attr = feedback_old_attr;
5415 feedback_col++;
5416 }
5417 else if (feedback_old_attr >= 0)
5418 {
5419 char_attr = feedback_old_attr;
5420 feedback_old_attr = -1;
5421 feedback_col = 0;
5422 }
5423 }
5424#endif
5425 /*
5426 * Handle the case where we are in column 0 but not on the first
5427 * character of the line and the user wants us to show us a
5428 * special character (via 'listchars' option "precedes:<char>".
5429 */
5430 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005431 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5433#ifdef FEAT_DIFF
5434 && filler_todo <= 0
5435#endif
5436 && draw_state > WL_NR
5437 && c != NUL)
5438 {
5439 c = lcs_prec;
5440 lcs_prec_todo = NUL;
5441#ifdef FEAT_MBYTE
Bram Moolenaar5641f382012-06-13 18:06:36 +02005442 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5443 {
5444 /* Double-width character being overwritten by the "precedes"
5445 * character, need to fill up half the character. */
5446 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005447 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005448 n_extra = 1;
5449 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005450 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005453 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 {
5455 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005456 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005457 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 else
5460 mb_utf8 = FALSE; /* don't draw as UTF-8 */
5461#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005462 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
5464 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005465 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 n_attr3 = 1;
5467 }
5468 }
5469
5470 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005471 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005473 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005474#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005475 || did_line_attr == 1
5476#endif
5477 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005479#ifdef FEAT_SEARCH_EXTRA
5480 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005481
5482 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005483 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005484 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005485#endif
5486
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005487 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 * highlight match at end of line. If it's beyond the last
5489 * char on the screen, just overwrite that one (tricky!) Not
5490 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005491#ifdef FEAT_SEARCH_EXTRA
5492 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005493 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005494 prevcol_hl_flag = TRUE;
5495 else
5496 {
5497 cur = wp->w_match_head;
5498 while (cur != NULL)
5499 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005500 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005501 {
5502 prevcol_hl_flag = TRUE;
5503 break;
5504 }
5505 cur = cur->next;
5506 }
5507 }
5508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005510 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005511 && (VIsual_mode != Ctrl_V
5512 || lnum == VIsual.lnum
5513 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005514 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#ifdef FEAT_SEARCH_EXTRA
5516 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005517 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005518# ifdef FEAT_SYN_HL
5519 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5520 && !(wp == curwin && VIsual_active))
5521# endif
5522# ifdef FEAT_DIFF
5523 && diff_hlf == (hlf_T)0
5524# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005525# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005526 && did_line_attr <= 1
5527# endif
5528 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#endif
5530 ))
5531 {
5532 int n = 0;
5533
5534#ifdef FEAT_RIGHTLEFT
5535 if (wp->w_p_rl)
5536 {
5537 if (col < 0)
5538 n = 1;
5539 }
5540 else
5541#endif
5542 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005543 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 n = -1;
5545 }
5546 if (n != 0)
5547 {
5548 /* At the window boundary, highlight the last character
5549 * instead (better than nothing). */
5550 off += n;
5551 col += n;
5552 }
5553 else
5554 {
5555 /* Add a blank character to highlight. */
5556 ScreenLines[off] = ' ';
5557#ifdef FEAT_MBYTE
5558 if (enc_utf8)
5559 ScreenLinesUC[off] = 0;
5560#endif
5561 }
5562#ifdef FEAT_SEARCH_EXTRA
5563 if (area_attr == 0)
5564 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005565 /* Use attributes from match with highest priority among
5566 * 'search_hl' and the match list. */
5567 char_attr = search_hl.attr;
5568 cur = wp->w_match_head;
5569 shl_flag = FALSE;
5570 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005571 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005572 if (shl_flag == FALSE
5573 && ((cur != NULL
5574 && cur->priority > SEARCH_HL_PRIORITY)
5575 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005576 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005577 shl = &search_hl;
5578 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005579 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005580 else
5581 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005582 if ((ptr - line) - 1 == (long)shl->startcol
5583 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005584 char_attr = shl->attr;
5585 if (shl != &search_hl && cur != NULL)
5586 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589#endif
5590 ScreenAttrs[off] = char_attr;
5591#ifdef FEAT_RIGHTLEFT
5592 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005595 --off;
5596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 else
5598#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005601 ++off;
5602 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005603 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005604#ifdef FEAT_SYN_HL
5605 eol_hl_off = 1;
5606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar91170f82006-05-05 21:15:17 +00005610 /*
5611 * At end of the text line.
5612 */
5613 if (c == NUL)
5614 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005615#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005616 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005617 if (wp->w_p_wrap)
5618 v = wp->w_skipcol;
5619 else
5620 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005621
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005622 /* check if line ends before left margin */
5623 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005624 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005625#ifdef FEAT_CONCEAL
5626 /* Get rid of the boguscols now, we want to draw until the right
5627 * edge for 'cursorcolumn'. */
5628 col -= boguscols;
5629 boguscols = 0;
5630#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005631
5632 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005633 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005634
5635 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005636 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5637 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005638 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005639 && lnum != wp->w_cursor.lnum)
5640 || draw_color_col)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005641# ifdef FEAT_RIGHTLEFT
5642 && !wp->w_p_rl
5643# endif
5644 )
5645 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005646 int rightmost_vcol = 0;
5647 int i;
5648
5649 if (wp->w_p_cuc)
5650 rightmost_vcol = wp->w_virtcol;
5651 if (draw_color_col)
5652 /* determine rightmost colorcolumn to possibly draw */
5653 for (i = 0; color_cols[i] >= 0; ++i)
5654 if (rightmost_vcol < color_cols[i])
5655 rightmost_vcol = color_cols[i];
5656
Bram Moolenaar02631462017-09-22 15:20:32 +02005657 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005658 {
5659 ScreenLines[off] = ' ';
5660#ifdef FEAT_MBYTE
5661 if (enc_utf8)
5662 ScreenLinesUC[off] = 0;
5663#endif
5664 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005665 if (draw_color_col)
5666 draw_color_col = advance_color_col(VCOL_HLC,
5667 &color_cols);
5668
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005669 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005670 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005671 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005672 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005673 else
5674 ScreenAttrs[off++] = 0;
5675
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005676 if (VCOL_HLC >= rightmost_vcol)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005677 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005678
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005679 ++vcol;
5680 }
5681 }
5682#endif
5683
Bram Moolenaar53f81742017-09-22 14:35:51 +02005684 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005685 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 row++;
5687
5688 /*
5689 * Update w_cline_height and w_cline_folded if the cursor line was
5690 * updated (saves a call to plines() later).
5691 */
5692 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5693 {
5694 curwin->w_cline_row = startrow;
5695 curwin->w_cline_height = row - startrow;
5696#ifdef FEAT_FOLDING
5697 curwin->w_cline_folded = FALSE;
5698#endif
5699 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5700 }
5701
5702 break;
5703 }
5704
5705 /* line continues beyond line end */
5706 if (lcs_ext
5707 && !wp->w_p_wrap
5708#ifdef FEAT_DIFF
5709 && filler_todo <= 0
5710#endif
5711 && (
5712#ifdef FEAT_RIGHTLEFT
5713 wp->w_p_rl ? col == 0 :
5714#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005715 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005717 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5719 {
5720 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005721 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722#ifdef FEAT_MBYTE
5723 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005724 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005727 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005728 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730 else
5731 mb_utf8 = FALSE;
5732#endif
5733 }
5734
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005735#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005736 /* advance to the next 'colorcolumn' */
5737 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005738 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005739
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005740 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005741 * highlight the cursor position itself.
5742 * Also highlight the 'colorcolumn' if it is different than
5743 * 'cursorcolumn' */
5744 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005745 if (draw_state == WL_LINE && !lnum_in_visual_area
5746 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005747 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005748 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005749 && lnum != wp->w_cursor.lnum)
5750 {
5751 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005752 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005753 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005754 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005755 {
5756 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005757 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005758 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005759 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005760#endif
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 /*
5763 * Store character to be displayed.
5764 * Skip characters that are left of the screen for 'nowrap'.
5765 */
5766 vcol_prev = vcol;
5767 if (draw_state < WL_LINE || n_skip <= 0)
5768 {
5769 /*
5770 * Store the character.
5771 */
5772#if defined(FEAT_RIGHTLEFT) && defined(FEAT_MBYTE)
5773 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5774 {
5775 /* A double-wide character is: put first halve in left cell. */
5776 --off;
5777 --col;
5778 }
5779#endif
5780 ScreenLines[off] = c;
5781#ifdef FEAT_MBYTE
5782 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005783 {
5784 if ((mb_c & 0xff00) == 0x8e00)
5785 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 else if (enc_utf8)
5789 {
5790 if (mb_utf8)
5791 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005792 int i;
5793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005795 if ((c & 0xff) == 0)
5796 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005797 for (i = 0; i < Screen_mco; ++i)
5798 {
5799 ScreenLinesC[i][off] = u8cc[i];
5800 if (u8cc[i] == 0)
5801 break;
5802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
5804 else
5805 ScreenLinesUC[off] = 0;
5806 }
5807 if (multi_attr)
5808 {
5809 ScreenAttrs[off] = multi_attr;
5810 multi_attr = 0;
5811 }
5812 else
5813#endif
5814 ScreenAttrs[off] = char_attr;
5815
5816#ifdef FEAT_MBYTE
5817 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5818 {
5819 /* Need to fill two screen columns. */
5820 ++off;
5821 ++col;
5822 if (enc_utf8)
5823 /* UTF-8: Put a 0 in the second screen char. */
5824 ScreenLines[off] = 0;
5825 else
5826 /* DBCS: Put second byte in the second screen char. */
5827 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005828 if (draw_state > WL_NR
5829#ifdef FEAT_DIFF
5830 && filler_todo <= 0
5831#endif
5832 )
5833 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 /* When "tocol" is halfway a character, set it to the end of
5835 * the character, otherwise highlighting won't stop. */
5836 if (tocol == vcol)
5837 ++tocol;
5838#ifdef FEAT_RIGHTLEFT
5839 if (wp->w_p_rl)
5840 {
5841 /* now it's time to backup one cell */
5842 --off;
5843 --col;
5844 }
5845#endif
5846 }
5847#endif
5848#ifdef FEAT_RIGHTLEFT
5849 if (wp->w_p_rl)
5850 {
5851 --off;
5852 --col;
5853 }
5854 else
5855#endif
5856 {
5857 ++off;
5858 ++col;
5859 }
5860 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005861#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005862 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005863 {
5864 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005865 ++vcol_off;
5866 if (n_extra > 0)
5867 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005868 if (wp->w_p_wrap)
5869 {
5870 /*
5871 * Special voodoo required if 'wrap' is on.
5872 *
5873 * Advance the column indicator to force the line
5874 * drawing to wrap early. This will make the line
5875 * take up the same screen space when parts are concealed,
5876 * so that cursor line computations aren't messed up.
5877 *
5878 * To avoid the fictitious advance of 'col' causing
5879 * trailing junk to be written out of the screen line
5880 * we are building, 'boguscols' keeps track of the number
5881 * of bad columns we have advanced.
5882 */
5883 if (n_extra > 0)
5884 {
5885 vcol += n_extra;
5886# ifdef FEAT_RIGHTLEFT
5887 if (wp->w_p_rl)
5888 {
5889 col -= n_extra;
5890 boguscols -= n_extra;
5891 }
5892 else
5893# endif
5894 {
5895 col += n_extra;
5896 boguscols += n_extra;
5897 }
5898 n_extra = 0;
5899 n_attr = 0;
5900 }
5901
5902
5903# ifdef FEAT_MBYTE
5904 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5905 {
5906 /* Need to fill two screen columns. */
5907# ifdef FEAT_RIGHTLEFT
5908 if (wp->w_p_rl)
5909 {
5910 --boguscols;
5911 --col;
5912 }
5913 else
5914# endif
5915 {
5916 ++boguscols;
5917 ++col;
5918 }
5919 }
5920# endif
5921
5922# ifdef FEAT_RIGHTLEFT
5923 if (wp->w_p_rl)
5924 {
5925 --boguscols;
5926 --col;
5927 }
5928 else
5929# endif
5930 {
5931 ++boguscols;
5932 ++col;
5933 }
5934 }
5935 else
5936 {
5937 if (n_extra > 0)
5938 {
5939 vcol += n_extra;
5940 n_extra = 0;
5941 n_attr = 0;
5942 }
5943 }
5944
5945 }
5946#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 else
5948 --n_skip;
5949
Bram Moolenaar64486672010-05-16 15:46:46 +02005950 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5951 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005952 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953#ifdef FEAT_DIFF
5954 && filler_todo <= 0
5955#endif
5956 )
5957 ++vcol;
5958
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005959#ifdef FEAT_SYN_HL
5960 if (vcol_save_attr >= 0)
5961 char_attr = vcol_save_attr;
5962#endif
5963
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 /* restore attributes after "predeces" in 'listchars' */
5965 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5966 char_attr = saved_attr3;
5967
5968 /* restore attributes after last 'listchars' or 'number' char */
5969 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5970 char_attr = saved_attr2;
5971
5972 /*
5973 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005974 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 */
5976 if ((
5977#ifdef FEAT_RIGHTLEFT
5978 wp->w_p_rl ? (col < 0) :
5979#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005980 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 && (*ptr != NUL
5982#ifdef FEAT_DIFF
5983 || filler_todo > 0
5984#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005985 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
5987 )
5988 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02005989#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02005990 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar02631462017-09-22 15:20:32 +02005991 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005992 boguscols = 0;
5993#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02005994 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar02631462017-09-22 15:20:32 +02005995 (int)wp->w_width, HAS_RIGHTLEFT(wp->w_p_rl));
Bram Moolenaar860cae12010-06-05 23:22:07 +02005996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 ++row;
5998 ++screen_row;
5999
6000 /* When not wrapping and finished diff lines, or when displayed
6001 * '$' and highlighting until last column, break here. */
6002 if ((!wp->w_p_wrap
6003#ifdef FEAT_DIFF
6004 && filler_todo <= 0
6005#endif
6006 ) || lcs_eol_one == -1)
6007 break;
6008
6009 /* When the window is too narrow draw all "@" lines. */
6010 if (draw_state != WL_LINE
6011#ifdef FEAT_DIFF
6012 && filler_todo <= 0
6013#endif
6014 )
6015 {
6016 win_draw_end(wp, '@', ' ', row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 row = endrow;
6019 }
6020
6021 /* When line got too long for screen break here. */
6022 if (row == endrow)
6023 {
6024 ++row;
6025 break;
6026 }
6027
6028 if (screen_cur_row == screen_row - 1
6029#ifdef FEAT_DIFF
6030 && filler_todo <= 0
6031#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006032 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 {
6034 /* Remember that the line wraps, used for modeless copy. */
6035 LineWraps[screen_row - 1] = TRUE;
6036
6037 /*
6038 * Special trick to make copy/paste of wrapped lines work with
6039 * xterm/screen: write an extra character beyond the end of
6040 * the line. This will work with all terminal types
6041 * (regardless of the xn,am settings).
6042 * Only do this on a fast tty.
6043 * Only do this if the cursor is on the current line
6044 * (something has been written in it).
6045 * Don't do this for the GUI.
6046 * Don't do this for double-width characters.
6047 * Don't do this for a window not at the right screen border.
6048 */
6049 if (p_tf
6050#ifdef FEAT_GUI
6051 && !gui.in_use
6052#endif
6053#ifdef FEAT_MBYTE
6054 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006055 && ((*mb_off2cells)(LineOffset[screen_row],
6056 LineOffset[screen_row] + screen_Columns)
6057 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006059 + (int)Columns - 2,
6060 LineOffset[screen_row] + screen_Columns)
6061 == 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062#endif
6063 )
6064 {
6065 /* First make sure we are at the end of the screen line,
6066 * then output the same character again to let the
6067 * terminal know about the wrap. If the terminal doesn't
6068 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006069 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 screen_char(LineOffset[screen_row - 1]
6071 + (unsigned)Columns - 1,
6072 screen_row - 1, (int)(Columns - 1));
6073
6074#ifdef FEAT_MBYTE
6075 /* When there is a multi-byte character, just output a
6076 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006077 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6078 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 out_char(' ');
6080 else
6081#endif
6082 out_char(ScreenLines[LineOffset[screen_row - 1]
6083 + (Columns - 1)]);
6084 /* force a redraw of the first char on the next line */
6085 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6086 screen_start(); /* don't know where cursor is now */
6087 }
6088 }
6089
6090 col = 0;
6091 off = (unsigned)(current_ScreenLine - ScreenLines);
6092#ifdef FEAT_RIGHTLEFT
6093 if (wp->w_p_rl)
6094 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006095 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 off += col;
6097 }
6098#endif
6099
6100 /* reset the drawing state for the start of a wrapped line */
6101 draw_state = WL_START;
6102 saved_n_extra = n_extra;
6103 saved_p_extra = p_extra;
6104 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006105 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 saved_char_attr = char_attr;
6107 n_extra = 0;
6108 lcs_prec_todo = lcs_prec;
6109#ifdef FEAT_LINEBREAK
6110# ifdef FEAT_DIFF
6111 if (filler_todo <= 0)
6112# endif
6113 need_showbreak = TRUE;
6114#endif
6115#ifdef FEAT_DIFF
6116 --filler_todo;
6117 /* When the filler lines are actually below the last line of the
6118 * file, don't draw the line itself, break here. */
6119 if (filler_todo == 0 && wp->w_botfill)
6120 break;
6121#endif
6122 }
6123
6124 } /* for every character in the line */
6125
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006126#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006127 /* After an empty line check first word for capital. */
6128 if (*skipwhite(line) == NUL)
6129 {
6130 capcol_lnum = lnum + 1;
6131 cap_col = 0;
6132 }
6133#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006134#ifdef FEAT_TEXT_PROP
6135 vim_free(text_props);
6136 vim_free(text_prop_idxs);
6137#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006138
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006139 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 return row;
6141}
6142
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006144/*
6145 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006146 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006147 */
6148 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006149comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006150{
6151 int i;
6152
6153 for (i = 0; i < Screen_mco; ++i)
6154 {
6155 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6156 return TRUE;
6157 if (ScreenLinesC[i][off_from] == 0)
6158 break;
6159 }
6160 return FALSE;
6161}
6162#endif
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164/*
6165 * Check whether the given character needs redrawing:
6166 * - the (first byte of the) character is different
6167 * - the attributes are different
6168 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006169 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 */
6171 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006172char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 if (cols > 0
6175 && ((ScreenLines[off_from] != ScreenLines[off_to]
6176 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
6177
6178#ifdef FEAT_MBYTE
6179 || (enc_dbcs != 0
6180 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6181 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6182 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6183 : (cols > 1 && ScreenLines[off_from + 1]
6184 != ScreenLines[off_to + 1])))
6185 || (enc_utf8
6186 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6187 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006188 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006189 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6190 && ScreenLines[off_from + 1]
6191 != ScreenLines[off_to + 1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192#endif
6193 ))
6194 return TRUE;
6195 return FALSE;
6196}
6197
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006198#if defined(FEAT_TERMINAL) || defined(PROTO)
6199/*
6200 * Return the index in ScreenLines[] for the current screen line.
6201 */
6202 int
6203screen_get_current_line_off()
6204{
6205 return (int)(current_ScreenLine - ScreenLines);
6206}
6207#endif
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209/*
6210 * Move one "cooked" screen line to the screen, but only the characters that
6211 * have actually changed. Handle insert/delete character.
6212 * "coloff" gives the first column on the screen for this line.
6213 * "endcol" gives the columns where valid characters are.
6214 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6215 * needs to be cleared, negative otherwise.
6216 * "rlflag" is TRUE in a rightleft window:
6217 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6218 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6219 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006220 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006221screen_line(
6222 int row,
6223 int coloff,
6224 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006225 int clear_width,
6226 int rlflag UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 unsigned off_from;
6229 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006230#ifdef FEAT_MBYTE
6231 unsigned max_off_from;
6232 unsigned max_off_to;
6233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 int force = FALSE; /* force update rest of the line */
6237 int redraw_this /* bool: does character need redraw? */
6238#ifdef FEAT_GUI
6239 = TRUE /* For GUI when while-loop empty */
6240#endif
6241 ;
6242 int redraw_next; /* redraw_this for next character */
6243#ifdef FEAT_MBYTE
6244 int clear_next = FALSE;
6245 int char_cells; /* 1: normal char */
6246 /* 2: occupies two display cells */
6247# define CHAR_CELLS char_cells
6248#else
6249# define CHAR_CELLS 1
6250#endif
6251
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006252 /* Check for illegal row and col, just in case. */
6253 if (row >= Rows)
6254 row = Rows - 1;
6255 if (endcol > Columns)
6256 endcol = Columns;
6257
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258# ifdef FEAT_CLIPBOARD
6259 clip_may_clear_selection(row, row);
6260# endif
6261
6262 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6263 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006264#ifdef FEAT_MBYTE
6265 max_off_from = off_from + screen_Columns;
6266 max_off_to = LineOffset[row] + screen_Columns;
6267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
6269#ifdef FEAT_RIGHTLEFT
6270 if (rlflag)
6271 {
6272 /* Clear rest first, because it's left of the text. */
6273 if (clear_width > 0)
6274 {
6275 while (col <= endcol && ScreenLines[off_to] == ' '
6276 && ScreenAttrs[off_to] == 0
6277# ifdef FEAT_MBYTE
6278 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6279# endif
6280 )
6281 {
6282 ++off_to;
6283 ++col;
6284 }
6285 if (col <= endcol)
6286 screen_fill(row, row + 1, col + coloff,
6287 endcol + coloff + 1, ' ', ' ', 0);
6288 }
6289 col = endcol + 1;
6290 off_to = LineOffset[row] + col + coloff;
6291 off_from += col;
6292 endcol = (clear_width > 0 ? clear_width : -clear_width);
6293 }
6294#endif /* FEAT_RIGHTLEFT */
6295
6296 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6297
6298 while (col < endcol)
6299 {
6300#ifdef FEAT_MBYTE
6301 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006302 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 else
6304 char_cells = 1;
6305#endif
6306
6307 redraw_this = redraw_next;
6308 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6309 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6310
6311#ifdef FEAT_GUI
6312 /* If the next character was bold, then redraw the current character to
6313 * remove any pixels that might have spilt over into us. This only
6314 * happens in the GUI.
6315 */
6316 if (redraw_next && gui.in_use)
6317 {
6318 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006319 if (hl > HL_ALL)
6320 hl = syn_attr2attr(hl);
6321 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 redraw_this = TRUE;
6323 }
6324#endif
6325
6326 if (redraw_this)
6327 {
6328 /*
6329 * Special handling when 'xs' termcap flag set (hpterm):
6330 * Attributes for characters are stored at the position where the
6331 * cursor is when writing the highlighting code. The
6332 * start-highlighting code must be written with the cursor on the
6333 * first highlighted character. The stop-highlighting code must
6334 * be written with the cursor just after the last highlighted
6335 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006336 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 * to clear the rest of the line, and force redrawing it
6338 * completely.
6339 */
6340 if ( p_wiv
6341 && !force
6342#ifdef FEAT_GUI
6343 && !gui.in_use
6344#endif
6345 && ScreenAttrs[off_to] != 0
6346 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6347 {
6348 /*
6349 * Need to remove highlighting attributes here.
6350 */
6351 windgoto(row, col + coloff);
6352 out_str(T_CE); /* clear rest of this screen line */
6353 screen_start(); /* don't know where cursor is now */
6354 force = TRUE; /* force redraw of rest of the line */
6355 redraw_next = TRUE; /* or else next char would miss out */
6356
6357 /*
6358 * If the previous character was highlighted, need to stop
6359 * highlighting at this character.
6360 */
6361 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6362 {
6363 screen_attr = ScreenAttrs[off_to - 1];
6364 term_windgoto(row, col + coloff);
6365 screen_stop_highlight();
6366 }
6367 else
6368 screen_attr = 0; /* highlighting has stopped */
6369 }
6370#ifdef FEAT_MBYTE
6371 if (enc_dbcs != 0)
6372 {
6373 /* Check if overwriting a double-byte with a single-byte or
6374 * the other way around requires another character to be
6375 * redrawn. For UTF-8 this isn't needed, because comparing
6376 * ScreenLinesUC[] is sufficient. */
6377 if (char_cells == 1
6378 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006379 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 {
6381 /* Writing a single-cell character over a double-cell
6382 * character: need to redraw the next cell. */
6383 ScreenLines[off_to + 1] = 0;
6384 redraw_next = TRUE;
6385 }
6386 else if (char_cells == 2
6387 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006388 && (*mb_off2cells)(off_to, max_off_to) == 1
6389 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
6391 /* Writing the second half of a double-cell character over
6392 * a double-cell character: need to redraw the second
6393 * cell. */
6394 ScreenLines[off_to + 2] = 0;
6395 redraw_next = TRUE;
6396 }
6397
6398 if (enc_dbcs == DBCS_JPNU)
6399 ScreenLines2[off_to] = ScreenLines2[off_from];
6400 }
6401 /* When writing a single-width character over a double-width
6402 * character and at the end of the redrawn text, need to clear out
6403 * the right halve of the old character.
6404 * Also required when writing the right halve of a double-width
6405 * char over the left halve of an existing one. */
6406 if (has_mbyte && col + char_cells == endcol
6407 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006408 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006410 && (*mb_off2cells)(off_to, max_off_to) == 1
6411 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 clear_next = TRUE;
6413#endif
6414
6415 ScreenLines[off_to] = ScreenLines[off_from];
6416#ifdef FEAT_MBYTE
6417 if (enc_utf8)
6418 {
6419 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6420 if (ScreenLinesUC[off_from] != 0)
6421 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006422 int i;
6423
6424 for (i = 0; i < Screen_mco; ++i)
6425 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427 }
6428 if (char_cells == 2)
6429 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
6430#endif
6431
6432#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006433 /* The bold trick makes a single column of pixels appear in the
6434 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 * character should be redrawn too. This happens for our own GUI
6436 * and for some xterms. */
6437 if (
6438# ifdef FEAT_GUI
6439 gui.in_use
6440# endif
6441# if defined(FEAT_GUI) && defined(UNIX)
6442 ||
6443# endif
6444# ifdef UNIX
6445 term_is_xterm
6446# endif
6447 )
6448 {
6449 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006450 if (hl > HL_ALL)
6451 hl = syn_attr2attr(hl);
6452 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 redraw_next = TRUE;
6454 }
6455#endif
6456 ScreenAttrs[off_to] = ScreenAttrs[off_from];
6457#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006458 /* For simplicity set the attributes of second half of a
6459 * double-wide character equal to the first half. */
6460 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462
6463 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 else
6466#endif
6467 screen_char(off_to, row, col + coloff);
6468 }
6469 else if ( p_wiv
6470#ifdef FEAT_GUI
6471 && !gui.in_use
6472#endif
6473 && col + coloff > 0)
6474 {
6475 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6476 {
6477 /*
6478 * Don't output stop-highlight when moving the cursor, it will
6479 * stop the highlighting when it should continue.
6480 */
6481 screen_attr = 0;
6482 }
6483 else if (screen_attr != 0)
6484 screen_stop_highlight();
6485 }
6486
6487 off_to += CHAR_CELLS;
6488 off_from += CHAR_CELLS;
6489 col += CHAR_CELLS;
6490 }
6491
6492#ifdef FEAT_MBYTE
6493 if (clear_next)
6494 {
6495 /* Clear the second half of a double-wide character of which the left
6496 * half was overwritten with a single-wide character. */
6497 ScreenLines[off_to] = ' ';
6498 if (enc_utf8)
6499 ScreenLinesUC[off_to] = 0;
6500 screen_char(off_to, row, col + coloff);
6501 }
6502#endif
6503
6504 if (clear_width > 0
6505#ifdef FEAT_RIGHTLEFT
6506 && !rlflag
6507#endif
6508 )
6509 {
6510#ifdef FEAT_GUI
6511 int startCol = col;
6512#endif
6513
6514 /* blank out the rest of the line */
6515 while (col < clear_width && ScreenLines[off_to] == ' '
6516 && ScreenAttrs[off_to] == 0
6517#ifdef FEAT_MBYTE
6518 && (!enc_utf8 || ScreenLinesUC[off_to] == 0)
6519#endif
6520 )
6521 {
6522 ++off_to;
6523 ++col;
6524 }
6525 if (col < clear_width)
6526 {
6527#ifdef FEAT_GUI
6528 /*
6529 * In the GUI, clearing the rest of the line may leave pixels
6530 * behind if the first character cleared was bold. Some bold
6531 * fonts spill over the left. In this case we redraw the previous
6532 * character too. If we didn't skip any blanks above, then we
6533 * only redraw if the character wasn't already redrawn anyway.
6534 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006535 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 hl = ScreenAttrs[off_to];
6538 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006539 {
6540 int prev_cells = 1;
6541# ifdef FEAT_MBYTE
6542 if (enc_utf8)
6543 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6544 * that its width is 2. */
6545 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6546 else if (enc_dbcs != 0)
6547 {
6548 /* find previous character by counting from first
6549 * column and get its width. */
6550 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006551 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006552
6553 while (off < off_to)
6554 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006555 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006556 off += prev_cells;
6557 }
6558 }
6559
6560 if (enc_dbcs != 0 && prev_cells > 1)
6561 screen_char_2(off_to - prev_cells, row,
6562 col + coloff - prev_cells);
6563 else
6564# endif
6565 screen_char(off_to - prev_cells, row,
6566 col + coloff - prev_cells);
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569#endif
6570 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6571 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 off_to += clear_width - col;
6573 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575 }
6576
6577 if (clear_width > 0)
6578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 /* For a window that's left of another, draw the separator char. */
6580 if (col + coloff < Columns)
6581 {
6582 int c;
6583
6584 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006585 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar4033c552017-09-16 20:54:51 +02006586#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006587 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6588 != (c >= 0x80 ? c : 0))
Bram Moolenaar4033c552017-09-16 20:54:51 +02006589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 || ScreenAttrs[off_to] != hl)
6591 {
6592 ScreenLines[off_to] = c;
6593 ScreenAttrs[off_to] = hl;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006594#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 if (enc_utf8)
6596 {
6597 if (c >= 0x80)
6598 {
6599 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006600 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
6602 else
6603 ScreenLinesUC[off_to] = 0;
6604 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 screen_char(off_to, row, col + coloff);
6607 }
6608 }
6609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 LineWraps[row] = FALSE;
6611 }
6612}
6613
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006614#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006616 * Mirror text "str" for right-left displaying.
6617 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006619 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006620rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 char_u *p1, *p2;
6623 int t;
6624
6625 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6626 {
6627 t = *p1;
6628 *p1 = *p2;
6629 *p2 = t;
6630 }
6631}
6632#endif
6633
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634/*
6635 * mark all status lines for redraw; used after first :cd
6636 */
6637 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006638status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 win_T *wp;
6641
Bram Moolenaar29323592016-07-24 22:04:11 +02006642 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 if (wp->w_status_height)
6644 {
6645 wp->w_redr_status = TRUE;
6646 redraw_later(VALID);
6647 }
6648}
6649
6650/*
6651 * mark all status lines of the current buffer for redraw
6652 */
6653 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006654status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 win_T *wp;
6657
Bram Moolenaar29323592016-07-24 22:04:11 +02006658 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6660 {
6661 wp->w_redr_status = TRUE;
6662 redraw_later(VALID);
6663 }
6664}
6665
6666/*
6667 * Redraw all status lines that need to be redrawn.
6668 */
6669 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006670redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 win_T *wp;
6673
Bram Moolenaar29323592016-07-24 22:04:11 +02006674 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006676 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006677 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006678 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
Bram Moolenaar4033c552017-09-16 20:54:51 +02006681#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682/*
6683 * Redraw all status lines at the bottom of frame "frp".
6684 */
6685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006686win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 if (frp->fr_layout == FR_LEAF)
6689 frp->fr_win->w_redr_status = TRUE;
6690 else if (frp->fr_layout == FR_ROW)
6691 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006692 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 win_redraw_last_status(frp);
6694 }
6695 else /* frp->fr_layout == FR_COL */
6696 {
6697 frp = frp->fr_child;
6698 while (frp->fr_next != NULL)
6699 frp = frp->fr_next;
6700 win_redraw_last_status(frp);
6701 }
6702}
6703#endif
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705/*
6706 * Draw the verticap separator right of window "wp" starting with line "row".
6707 */
6708 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006709draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 int hl;
6712 int c;
6713
6714 if (wp->w_vsep_width)
6715 {
6716 /* draw the vertical separator right of this window */
6717 c = fillchar_vsep(&hl);
6718 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6719 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6720 c, ' ', hl);
6721 }
6722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006725static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006728 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 */
6730 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006731status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 int len = 0;
6734
6735#ifdef FEAT_MENU
6736 int emenu = (xp->xp_context == EXPAND_MENUS
6737 || xp->xp_context == EXPAND_MENUNAMES);
6738
6739 /* Check for menu separators - replace with '|'. */
6740 if (emenu && menu_is_separator(s))
6741 return 1;
6742#endif
6743
6744 while (*s != NUL)
6745 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006746 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006747 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006748 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750
6751 return len;
6752}
6753
6754/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006755 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006756 * These are backslashes used for escaping. Do show backslashes in help tags.
6757 */
6758 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006759skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006760{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006761 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006762#ifdef FEAT_MENU
6763 || ((xp->xp_context == EXPAND_MENUS
6764 || xp->xp_context == EXPAND_MENUNAMES)
6765 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6766#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006767 )
6768 {
6769#ifndef BACKSLASH_IN_FILENAME
6770 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6771 return 2;
6772#endif
6773 return 1;
6774 }
6775 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006776}
6777
6778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 * Show wildchar matches in the status line.
6780 * Show at least the "match" item.
6781 * We start at item 'first_match' in the list and show all matches that fit.
6782 *
6783 * If inversion is possible we use it. Else '=' characters are used.
6784 */
6785 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006786win_redr_status_matches(
6787 expand_T *xp,
6788 int num_matches,
6789 char_u **matches, /* list of matches */
6790 int match,
6791 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6794 int row;
6795 char_u *buf;
6796 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006797 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 int fillchar;
6799 int attr;
6800 int i;
6801 int highlight = TRUE;
6802 char_u *selstart = NULL;
6803 int selstart_col = 0;
6804 char_u *selend = NULL;
6805 static int first_match = 0;
6806 int add_left = FALSE;
6807 char_u *s;
6808#ifdef FEAT_MENU
6809 int emenu;
6810#endif
6811#if defined(FEAT_MBYTE) || defined(FEAT_MENU)
6812 int l;
6813#endif
6814
6815 if (matches == NULL) /* interrupted completion? */
6816 return;
6817
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006818#ifdef FEAT_MBYTE
6819 if (has_mbyte)
6820 buf = alloc((unsigned)Columns * MB_MAXBYTES + 1);
6821 else
6822#endif
6823 buf = alloc((unsigned)Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 if (buf == NULL)
6825 return;
6826
6827 if (match == -1) /* don't show match but original text */
6828 {
6829 match = 0;
6830 highlight = FALSE;
6831 }
6832 /* count 1 for the ending ">" */
6833 clen = status_match_len(xp, L_MATCH(match)) + 3;
6834 if (match == 0)
6835 first_match = 0;
6836 else if (match < first_match)
6837 {
6838 /* jumping left, as far as we can go */
6839 first_match = match;
6840 add_left = TRUE;
6841 }
6842 else
6843 {
6844 /* check if match fits on the screen */
6845 for (i = first_match; i < match; ++i)
6846 clen += status_match_len(xp, L_MATCH(i)) + 2;
6847 if (first_match > 0)
6848 clen += 2;
6849 /* jumping right, put match at the left */
6850 if ((long)clen > Columns)
6851 {
6852 first_match = match;
6853 /* if showing the last match, we can add some on the left */
6854 clen = 2;
6855 for (i = match; i < num_matches; ++i)
6856 {
6857 clen += status_match_len(xp, L_MATCH(i)) + 2;
6858 if ((long)clen >= Columns)
6859 break;
6860 }
6861 if (i == num_matches)
6862 add_left = TRUE;
6863 }
6864 }
6865 if (add_left)
6866 while (first_match > 0)
6867 {
6868 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6869 if ((long)clen >= Columns)
6870 break;
6871 --first_match;
6872 }
6873
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006874 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
6876 if (first_match == 0)
6877 {
6878 *buf = NUL;
6879 len = 0;
6880 }
6881 else
6882 {
6883 STRCPY(buf, "< ");
6884 len = 2;
6885 }
6886 clen = len;
6887
6888 i = first_match;
6889 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6890 {
6891 if (i == match)
6892 {
6893 selstart = buf + len;
6894 selstart_col = clen;
6895 }
6896
6897 s = L_MATCH(i);
6898 /* Check for menu separators - replace with '|' */
6899#ifdef FEAT_MENU
6900 emenu = (xp->xp_context == EXPAND_MENUS
6901 || xp->xp_context == EXPAND_MENUNAMES);
6902 if (emenu && menu_is_separator(s))
6903 {
6904 STRCPY(buf + len, transchar('|'));
6905 l = (int)STRLEN(buf + len);
6906 len += l;
6907 clen += l;
6908 }
6909 else
6910#endif
6911 for ( ; *s != NUL; ++s)
6912 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006913 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 clen += ptr2cells(s);
6915#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006916 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
6918 STRNCPY(buf + len, s, l);
6919 s += l - 1;
6920 len += l;
6921 }
6922 else
6923#endif
6924 {
6925 STRCPY(buf + len, transchar_byte(*s));
6926 len += (int)STRLEN(buf + len);
6927 }
6928 }
6929 if (i == match)
6930 selend = buf + len;
6931
6932 *(buf + len++) = ' ';
6933 *(buf + len++) = ' ';
6934 clen += 2;
6935 if (++i == num_matches)
6936 break;
6937 }
6938
6939 if (i != num_matches)
6940 {
6941 *(buf + len++) = '>';
6942 ++clen;
6943 }
6944
6945 buf[len] = NUL;
6946
6947 row = cmdline_row - 1;
6948 if (row >= 0)
6949 {
6950 if (wild_menu_showing == 0)
6951 {
6952 if (msg_scrolled > 0)
6953 {
6954 /* Put the wildmenu just above the command line. If there is
6955 * no room, scroll the screen one line up. */
6956 if (cmdline_row == Rows - 1)
6957 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006958 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 ++msg_scrolled;
6960 }
6961 else
6962 {
6963 ++cmdline_row;
6964 ++row;
6965 }
6966 wild_menu_showing = WM_SCROLLED;
6967 }
6968 else
6969 {
6970 /* Create status line if needed by setting 'laststatus' to 2.
6971 * Set 'winminheight' to zero to avoid that the window is
6972 * resized. */
6973 if (lastwin->w_status_height == 0)
6974 {
6975 save_p_ls = p_ls;
6976 save_p_wmh = p_wmh;
6977 p_ls = 2;
6978 p_wmh = 0;
6979 last_status(FALSE);
6980 }
6981 wild_menu_showing = WM_SHOWN;
6982 }
6983 }
6984
6985 screen_puts(buf, row, 0, attr);
6986 if (selstart != NULL && highlight)
6987 {
6988 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006989 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
6991
6992 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6993 }
6994
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 vim_free(buf);
6997}
6998#endif
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000/*
7001 * Redraw the status line of window wp.
7002 *
7003 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007004 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7005 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007007 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007008win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 int row;
7011 char_u *p;
7012 int len;
7013 int fillchar;
7014 int attr;
7015 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007016 static int busy = FALSE;
7017
7018 /* It's possible to get here recursively when 'statusline' (indirectly)
7019 * invokes ":redrawstatus". Simply ignore the call then. */
7020 if (busy)
7021 return;
7022 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 wp->w_redr_status = FALSE;
7025 if (wp->w_status_height == 0)
7026 {
7027 /* no status line, can only be last window */
7028 redraw_cmdline = TRUE;
7029 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007030 else if (!redrawing()
7031#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007032 // don't update status line when popup menu is visible and may be
7033 // drawn over it, unless it will be redrawn later
7034 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007035#endif
7036 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 /* Don't redraw right now, do it later. */
7039 wp->w_redr_status = TRUE;
7040 }
7041#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007042 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007045 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047#endif
7048 else
7049 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007050 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007052 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 p = NameBuff;
7054 len = (int)STRLEN(p);
7055
Bram Moolenaard85f2712017-07-28 21:51:57 +02007056 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057#ifdef FEAT_QUICKFIX
7058 || wp->w_p_pvw
7059#endif
7060 || bufIsChanged(wp->w_buffer)
7061 || wp->w_buffer->b_p_ro)
7062 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007063 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007065 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 len += (int)STRLEN(p + len);
7067 }
7068#ifdef FEAT_QUICKFIX
7069 if (wp->w_p_pvw)
7070 {
7071 STRCPY(p + len, _("[Preview]"));
7072 len += (int)STRLEN(p + len);
7073 }
7074#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007075 if (bufIsChanged(wp->w_buffer)
7076#ifdef FEAT_TERMINAL
7077 && !bt_terminal(wp->w_buffer)
7078#endif
7079 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
7081 STRCPY(p + len, "[+]");
7082 len += 3;
7083 }
7084 if (wp->w_buffer->b_p_ro)
7085 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007086 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007087 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089
Bram Moolenaar02631462017-09-22 15:20:32 +02007090 this_ru_col = ru_col - (Columns - wp->w_width);
7091 if (this_ru_col < (wp->w_width + 1) / 2)
7092 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 if (this_ru_col <= 1)
7094 {
7095 p = (char_u *)"<"; /* No room for file name! */
7096 len = 1;
7097 }
7098 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099#ifdef FEAT_MBYTE
7100 if (has_mbyte)
7101 {
7102 int clen = 0, i;
7103
7104 /* Count total number of display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02007105 clen = mb_string2cells(p, -1);
7106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 /* Find first character that will fit.
7108 * Going from start to end is much faster for DBCS. */
7109 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007110 i += (*mb_ptr2len)(p + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 clen -= (*mb_ptr2cells)(p + i);
7112 len = clen;
7113 if (i > 0)
7114 {
7115 p = p + i - 1;
7116 *p = '<';
7117 ++len;
7118 }
7119
7120 }
7121 else
7122#endif
7123 if (len > this_ru_col - 1)
7124 {
7125 p += len - (this_ru_col - 1);
7126 *p = '<';
7127 len = this_ru_col - 1;
7128 }
7129
7130 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007131 screen_puts(p, row, wp->w_wincol, attr);
7132 screen_fill(row, row + 1, len + wp->w_wincol,
7133 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007135 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7137 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007138 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
7140#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007141 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142#endif
7143 }
7144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 /*
7146 * May need to draw the character below the vertical separator.
7147 */
7148 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7149 {
7150 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007151 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 else
7153 fillchar = fillchar_vsep(&attr);
7154 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7155 attr);
7156 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007157 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
Bram Moolenaar238a5642006-02-21 22:12:05 +00007160#ifdef FEAT_STL_OPT
7161/*
7162 * Redraw the status line according to 'statusline' and take care of any
7163 * errors encountered.
7164 */
7165 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007166redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007167{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007168 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007169 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007170
7171 /* When called recursively return. This can happen when the statusline
7172 * contains an expression that triggers a redraw. */
7173 if (entered)
7174 return;
7175 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007176
Bram Moolenaara742e082016-04-05 21:10:38 +02007177 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007178 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007179 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007180 {
7181 /* When there is an error disable the statusline, otherwise the
7182 * display is messed up with errors and a redraw triggers the problem
7183 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007184 set_string_option_direct((char_u *)"statusline", -1,
7185 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007186 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007187 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007188 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007190}
7191#endif
7192
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193/*
7194 * Return TRUE if the status line of window "wp" is connected to the status
7195 * line of the window right of it. If not, then it's a vertical separator.
7196 * Only call if (wp->w_vsep_width != 0).
7197 */
7198 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007199stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 frame_T *fr;
7202
7203 fr = wp->w_frame;
7204 while (fr->fr_parent != NULL)
7205 {
7206 if (fr->fr_parent->fr_layout == FR_COL)
7207 {
7208 if (fr->fr_next != NULL)
7209 break;
7210 }
7211 else
7212 {
7213 if (fr->fr_next != NULL)
7214 return TRUE;
7215 }
7216 fr = fr->fr_parent;
7217 }
7218 return FALSE;
7219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222/*
7223 * Get the value to show for the language mappings, active 'keymap'.
7224 */
7225 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007226get_keymap_str(
7227 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007228 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007229 char_u *buf, /* buffer for the result */
7230 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 char_u *p;
7233
7234 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7235 return FALSE;
7236
7237 {
7238#ifdef FEAT_EVAL
7239 buf_T *old_curbuf = curbuf;
7240 win_T *old_curwin = curwin;
7241 char_u *s;
7242
7243 curbuf = wp->w_buffer;
7244 curwin = wp;
7245 STRCPY(buf, "b:keymap_name"); /* must be writable */
7246 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007247 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 --emsg_skip;
7249 curbuf = old_curbuf;
7250 curwin = old_curwin;
7251 if (p == NULL || *p == NUL)
7252#endif
7253 {
7254#ifdef FEAT_KEYMAP
7255 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7256 p = wp->w_buffer->b_p_keymap;
7257 else
7258#endif
7259 p = (char_u *)"lang";
7260 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007261 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 buf[0] = NUL;
7263#ifdef FEAT_EVAL
7264 vim_free(s);
7265#endif
7266 }
7267 return buf[0] != NUL;
7268}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270#if defined(FEAT_STL_OPT) || defined(PROTO)
7271/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007272 * Redraw the status line or ruler of window "wp".
7273 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 */
7275 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007276win_redr_custom(
7277 win_T *wp,
7278 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007280 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 int attr;
7282 int curattr;
7283 int row;
7284 int col = 0;
7285 int maxwidth;
7286 int width;
7287 int n;
7288 int len;
7289 int fillchar;
7290 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007291 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007293 struct stl_hlrec hltab[STL_MAX_ITEM];
7294 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007295 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007296 win_T *ewp;
7297 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
Bram Moolenaar1d633412013-12-11 15:52:01 +01007299 /* There is a tiny chance that this gets called recursively: When
7300 * redrawing a status line triggers redrawing the ruler or tabline.
7301 * Avoid trouble by not allowing recursion. */
7302 if (entered)
7303 return;
7304 entered = TRUE;
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007307 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007309 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007310 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007311 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007312 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007313 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007314 maxwidth = Columns;
7315# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007316 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 else
7320 {
7321 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007322 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007323 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007324
7325 if (draw_ruler)
7326 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007329 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007330 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007331 if (*++stl == '-')
7332 stl++;
7333 if (atoi((char *)stl))
7334 while (VIM_ISDIGIT(*stl))
7335 stl++;
7336 if (*stl++ != '(')
7337 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007338 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007339 col = ru_col - (Columns - wp->w_width);
7340 if (col < (wp->w_width + 1) / 2)
7341 col = (wp->w_width + 1) / 2;
7342 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007343 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007344 {
7345 row = Rows - 1;
7346 --maxwidth; /* writing in last column may cause scrolling */
7347 fillchar = ' ';
7348 attr = 0;
7349 }
7350
7351# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007352 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007353# endif
7354 }
7355 else
7356 {
7357 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007358 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007359 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007360 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007361# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007362 use_sandbox = was_set_insecurely((char_u *)"statusline",
7363 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007364# endif
7365 }
7366
Bram Moolenaar53f81742017-09-22 14:35:51 +02007367 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007368 }
7369
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007371 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
Bram Moolenaar61452852011-02-01 18:01:11 +01007373 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7374 * the cursor away and back. */
7375 ewp = wp == NULL ? curwin : wp;
7376 p_crb_save = ewp->w_p_crb;
7377 ewp->w_p_crb = FALSE;
7378
Bram Moolenaar362f3562009-11-03 16:20:34 +00007379 /* Make a copy, because the statusline may include a function call that
7380 * might change the option value and free the memory. */
7381 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007382 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007383 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007384 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007385 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007386 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007388 /* Make all characters printable. */
7389 p = transstr(buf);
7390 if (p != NULL)
7391 {
7392 vim_strncpy(buf, p, sizeof(buf) - 1);
7393 vim_free(p);
7394 }
7395
7396 /* fill up with "fillchar" */
7397 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007398 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
7400#ifdef FEAT_MBYTE
7401 len += (*mb_char2bytes)(fillchar, buf + len);
7402#else
7403 buf[len++] = fillchar;
7404#endif
7405 ++width;
7406 }
7407 buf[len] = NUL;
7408
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007409 /*
7410 * Draw each snippet with the specified highlighting.
7411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 curattr = attr;
7413 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007414 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007416 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 screen_puts_len(p, len, row, col, curattr);
7418 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007419 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007421 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007423 else if (hltab[n].userhl < 0)
7424 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007425#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007426 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7427 && wp->w_status_height != 0)
7428 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007429 else if (wp != NULL && bt_terminal(wp->w_buffer)
7430 && wp->w_status_height != 0)
7431 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007432#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007433 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007434 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007436 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 }
7438 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007439
7440 if (wp == NULL)
7441 {
7442 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7443 col = 0;
7444 len = 0;
7445 p = buf;
7446 fillchar = 0;
7447 for (n = 0; tabtab[n].start != NULL; n++)
7448 {
7449 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7450 while (col < len)
7451 TabPageIdxs[col++] = fillchar;
7452 p = tabtab[n].start;
7453 fillchar = tabtab[n].userhl;
7454 }
7455 while (col < Columns)
7456 TabPageIdxs[col++] = fillchar;
7457 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007458
7459theend:
7460 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461}
7462
7463#endif /* FEAT_STL_OPT */
7464
7465/*
7466 * Output a single character directly to the screen and update ScreenLines.
7467 */
7468 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007469screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 char_u buf[MB_MAXBYTES + 1];
7472
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007473#ifdef FEAT_MBYTE
7474 if (has_mbyte)
7475 buf[(*mb_char2bytes)(c, buf)] = NUL;
7476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#endif
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007478 {
7479 buf[0] = c;
7480 buf[1] = NUL;
7481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 screen_puts(buf, row, col, attr);
7483}
7484
7485/*
7486 * Get a single character directly from ScreenLines into "bytes[]".
7487 * Also return its attribute in *attrp;
7488 */
7489 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007490screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 unsigned off;
7493
7494 /* safety check */
7495 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7496 {
7497 off = LineOffset[row] + col;
7498 *attrp = ScreenAttrs[off];
7499 bytes[0] = ScreenLines[off];
7500 bytes[1] = NUL;
7501
7502#ifdef FEAT_MBYTE
7503 if (enc_utf8 && ScreenLinesUC[off] != 0)
7504 bytes[utfc_char2bytes(off, bytes)] = NUL;
7505 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7506 {
7507 bytes[0] = ScreenLines[off];
7508 bytes[1] = ScreenLines2[off];
7509 bytes[2] = NUL;
7510 }
7511 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7512 {
7513 bytes[1] = ScreenLines[off + 1];
7514 bytes[2] = NUL;
7515 }
7516#endif
7517 }
7518}
7519
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007520#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007521/*
7522 * Return TRUE if composing characters for screen posn "off" differs from
7523 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007524 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007525 */
7526 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007527screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007528{
7529 int i;
7530
7531 for (i = 0; i < Screen_mco; ++i)
7532 {
7533 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7534 return TRUE;
7535 if (u8cc[i] == 0)
7536 break;
7537 }
7538 return FALSE;
7539}
7540#endif
7541
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542/*
7543 * Put string '*text' on the screen at position 'row' and 'col', with
7544 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7545 * Note: only outputs within one row, message is truncated at screen boundary!
7546 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7547 */
7548 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007549screen_puts(
7550 char_u *text,
7551 int row,
7552 int col,
7553 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
7555 screen_puts_len(text, -1, row, col, attr);
7556}
7557
7558/*
7559 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7560 * a NUL.
7561 */
7562 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007563screen_puts_len(
7564 char_u *text,
7565 int textlen,
7566 int row,
7567 int col,
7568 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 unsigned off;
7571 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007572 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 int c;
7574#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00007575 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 int mbyte_blen = 1;
7577 int mbyte_cells = 1;
7578 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007579 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 int clear_next_cell = FALSE;
7581# ifdef FEAT_ARABIC
7582 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007583 int pc, nc, nc1;
7584 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585# endif
7586#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007587#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7588 int force_redraw_this;
7589 int force_redraw_next = FALSE;
7590#endif
7591 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592
7593 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7594 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007595 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
Bram Moolenaarc236c162008-07-13 17:41:49 +00007597#ifdef FEAT_MBYTE
7598 /* When drawing over the right halve of a double-wide char clear out the
7599 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007600 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaarc236c162008-07-13 17:41:49 +00007601# ifdef FEAT_GUI
7602 && !gui.in_use
7603# endif
7604 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007605 {
7606 ScreenLines[off - 1] = ' ';
7607 ScreenAttrs[off - 1] = 0;
7608 if (enc_utf8)
7609 {
7610 ScreenLinesUC[off - 1] = 0;
7611 ScreenLinesC[0][off - 1] = 0;
7612 }
7613 /* redraw the previous cell, make it empty */
7614 screen_char(off - 1, row, col - 1);
7615 /* force the cell at "col" to be redrawn */
7616 force_redraw_next = TRUE;
7617 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007618#endif
7619
Bram Moolenaar367329b2007-08-30 11:53:22 +00007620#ifdef FEAT_MBYTE
7621 max_off = LineOffset[row] + screen_Columns;
7622#endif
Bram Moolenaara064ac82007-08-05 18:10:54 +00007623 while (col < screen_Columns
7624 && (len < 0 || (int)(ptr - text) < len)
7625 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 {
7627 c = *ptr;
7628#ifdef FEAT_MBYTE
7629 /* check if this is the first byte of a multibyte */
7630 if (has_mbyte)
7631 {
7632 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007633 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007635 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7637 mbyte_cells = 1;
7638 else if (enc_dbcs != 0)
7639 mbyte_cells = mbyte_blen;
7640 else /* enc_utf8 */
7641 {
7642 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007643 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 (int)((text + len) - ptr));
7645 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007646 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaar11936362007-09-17 20:39:42 +00007648# ifdef UNICODE16
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 /* Non-BMP character: display as ? or fullwidth ?. */
7650 if (u8c >= 0x10000)
7651 {
7652 u8c = (mbyte_cells == 2) ? 0xff1f : (int)'?';
7653 if (attr == 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +01007654 attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
Bram Moolenaar11936362007-09-17 20:39:42 +00007656# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657# ifdef FEAT_ARABIC
7658 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7659 {
7660 /* Do Arabic shaping. */
7661 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7662 {
7663 /* Past end of string to be displayed. */
7664 nc = NUL;
7665 nc1 = NUL;
7666 }
7667 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007668 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007669 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7670 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007671 nc1 = pcc[0];
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 pc = prev_c;
7674 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007675 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
7677 else
7678 prev_c = u8c;
7679# endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007680 if (col + mbyte_cells > screen_Columns)
7681 {
7682 /* Only 1 cell left, but character requires 2 cells:
7683 * display a '>' in the last column to avoid wrapping. */
7684 c = '>';
7685 mbyte_cells = 1;
7686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 }
7689#endif
7690
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007691#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7692 force_redraw_this = force_redraw_next;
7693 force_redraw_next = FALSE;
7694#endif
7695
7696 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697#ifdef FEAT_MBYTE
7698 || (mbyte_cells == 2
7699 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7700 || (enc_dbcs == DBCS_JPNU
7701 && c == 0x8e
7702 && ScreenLines2[off] != ptr[1])
7703 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007704 && (ScreenLinesUC[off] !=
7705 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7706 || (ScreenLinesUC[off] != 0
7707 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708#endif
7709 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007710 || exmode_active;
7711
7712 if (need_redraw
7713#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7714 || force_redraw_this
7715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 )
7717 {
7718#if defined(FEAT_GUI) || defined(UNIX)
7719 /* The bold trick makes a single row of pixels appear in the next
7720 * character. When a bold character is removed, the next
7721 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007722 * and for some xterms. */
7723 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724# ifdef FEAT_GUI
7725 gui.in_use
7726# endif
7727# if defined(FEAT_GUI) && defined(UNIX)
7728 ||
7729# endif
7730# ifdef UNIX
7731 term_is_xterm
7732# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007733 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007735 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007737 if (n > HL_ALL)
7738 n = syn_attr2attr(n);
7739 if (n & HL_BOLD)
7740 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 }
7742#endif
7743#ifdef FEAT_MBYTE
7744 /* When at the end of the text and overwriting a two-cell
7745 * character with a one-cell character, need to clear the next
7746 * cell. Also when overwriting the left halve of a two-cell char
7747 * with the right halve of a two-cell char. Do this only once
7748 * (mb_off2cells() may return 2 on the right halve). */
7749 if (clear_next_cell)
7750 clear_next_cell = FALSE;
7751 else if (has_mbyte
7752 && (len < 0 ? ptr[mbyte_blen] == NUL
7753 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007754 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007756 && (*mb_off2cells)(off, max_off) == 1
7757 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 clear_next_cell = TRUE;
7759
7760 /* Make sure we never leave a second byte of a double-byte behind,
7761 * it confuses mb_off2cells(). */
7762 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007763 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007765 && (*mb_off2cells)(off, max_off) == 1
7766 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 ScreenLines[off + mbyte_blen] = 0;
7768#endif
7769 ScreenLines[off] = c;
7770 ScreenAttrs[off] = attr;
7771#ifdef FEAT_MBYTE
7772 if (enc_utf8)
7773 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007774 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 ScreenLinesUC[off] = 0;
7776 else
7777 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007778 int i;
7779
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007781 for (i = 0; i < Screen_mco; ++i)
7782 {
7783 ScreenLinesC[i][off] = u8cc[i];
7784 if (u8cc[i] == 0)
7785 break;
7786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
7788 if (mbyte_cells == 2)
7789 {
7790 ScreenLines[off + 1] = 0;
7791 ScreenAttrs[off + 1] = attr;
7792 }
7793 screen_char(off, row, col);
7794 }
7795 else if (mbyte_cells == 2)
7796 {
7797 ScreenLines[off + 1] = ptr[1];
7798 ScreenAttrs[off + 1] = attr;
7799 screen_char_2(off, row, col);
7800 }
7801 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7802 {
7803 ScreenLines2[off] = ptr[1];
7804 screen_char(off, row, col);
7805 }
7806 else
7807#endif
7808 screen_char(off, row, col);
7809 }
7810#ifdef FEAT_MBYTE
7811 if (has_mbyte)
7812 {
7813 off += mbyte_cells;
7814 col += mbyte_cells;
7815 ptr += mbyte_blen;
7816 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007817 {
7818 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007820 len = -1;
7821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 }
7823 else
7824#endif
7825 {
7826 ++off;
7827 ++col;
7828 ++ptr;
7829 }
7830 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007831
7832#if defined(FEAT_MBYTE) || defined(FEAT_GUI) || defined(UNIX)
7833 /* If we detected the next character needs to be redrawn, but the text
7834 * doesn't extend up to there, update the character here. */
7835 if (force_redraw_next && col < screen_Columns)
7836 {
7837# ifdef FEAT_MBYTE
7838 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7839 screen_char_2(off, row, col);
7840 else
7841# endif
7842 screen_char(off, row, col);
7843 }
7844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845}
7846
7847#ifdef FEAT_SEARCH_EXTRA
7848/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007849 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 */
7851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007852start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
7854 if (p_hls && !no_hlsearch)
7855 {
7856 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007857 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007858# ifdef FEAT_RELTIME
7859 /* Set the time limit to 'redrawtime'. */
7860 profile_setlimit(p_rdt, &search_hl.tm);
7861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 }
7863}
7864
7865/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007866 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 */
7868 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007869end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 if (search_hl.rm.regprog != NULL)
7872 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007873 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 search_hl.rm.regprog = NULL;
7875 }
7876}
7877
7878/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007879 * Init for calling prepare_search_hl().
7880 */
7881 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007882init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007883{
7884 matchitem_T *cur;
7885
7886 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7887 * match */
7888 cur = wp->w_match_head;
7889 while (cur != NULL)
7890 {
7891 cur->hl.rm = cur->match;
7892 if (cur->hlg_id == 0)
7893 cur->hl.attr = 0;
7894 else
7895 cur->hl.attr = syn_id2attr(cur->hlg_id);
7896 cur->hl.buf = wp->w_buffer;
7897 cur->hl.lnum = 0;
7898 cur->hl.first_lnum = 0;
7899# ifdef FEAT_RELTIME
7900 /* Set the time limit to 'redrawtime'. */
7901 profile_setlimit(p_rdt, &(cur->hl.tm));
7902# endif
7903 cur = cur->next;
7904 }
7905 search_hl.buf = wp->w_buffer;
7906 search_hl.lnum = 0;
7907 search_hl.first_lnum = 0;
7908 /* time limit is set at the toplevel, for all windows */
7909}
7910
7911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 * Advance to the match in window "wp" line "lnum" or past it.
7913 */
7914 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007915prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007917 matchitem_T *cur; /* points to the match list */
7918 match_T *shl; /* points to search_hl or a match */
7919 int shl_flag; /* flag to indicate whether search_hl
7920 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007921 int pos_inprogress; /* marks that position match search is
7922 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 int n;
7924
7925 /*
7926 * When using a multi-line pattern, start searching at the top
7927 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007928 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007930 cur = wp->w_match_head;
7931 shl_flag = FALSE;
7932 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007934 if (shl_flag == FALSE)
7935 {
7936 shl = &search_hl;
7937 shl_flag = TRUE;
7938 }
7939 else
7940 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (shl->rm.regprog != NULL
7942 && shl->lnum == 0
7943 && re_multiline(shl->rm.regprog))
7944 {
7945 if (shl->first_lnum == 0)
7946 {
7947# ifdef FEAT_FOLDING
7948 for (shl->first_lnum = lnum;
7949 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7950 if (hasFoldingWin(wp, shl->first_lnum - 1,
7951 NULL, NULL, TRUE, NULL))
7952 break;
7953# else
7954 shl->first_lnum = wp->w_topline;
7955# endif
7956 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007957 if (cur != NULL)
7958 cur->pos.cur = 0;
7959 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007961 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7962 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007964 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7965 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007966 pos_inprogress = cur == NULL || cur->pos.cur == 0
7967 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 if (shl->lnum != 0)
7969 {
7970 shl->first_lnum = shl->lnum
7971 + shl->rm.endpos[0].lnum
7972 - shl->rm.startpos[0].lnum;
7973 n = shl->rm.endpos[0].col;
7974 }
7975 else
7976 {
7977 ++shl->first_lnum;
7978 n = 0;
7979 }
7980 }
7981 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007982 if (shl != &search_hl && cur != NULL)
7983 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 }
7985}
7986
7987/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007988 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 * Uses shl->buf.
7990 * Sets shl->lnum and shl->rm contents.
7991 * Note: Assumes a previous match is always before "lnum", unless
7992 * shl->lnum is zero.
7993 * Careful: Any pointers for buffer lines will become invalid.
7994 */
7995 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007996next_search_hl(
7997 win_T *win,
7998 match_T *shl, /* points to search_hl or a match */
7999 linenr_T lnum,
8000 colnr_T mincol, /* minimal column for a match */
8001 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003 linenr_T l;
8004 colnr_T matchcol;
8005 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008006 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008008 // for :{range}s/pat only highlight inside the range
8009 if (lnum < search_first_line || lnum > search_last_line)
8010 {
8011 shl->lnum = 0;
8012 return;
8013 }
8014
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (shl->lnum != 0)
8016 {
8017 /* Check for three situations:
8018 * 1. If the "lnum" is below a previous match, start a new search.
8019 * 2. If the previous match includes "mincol", use it.
8020 * 3. Continue after the previous match.
8021 */
8022 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8023 if (lnum > l)
8024 shl->lnum = 0;
8025 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8026 return;
8027 }
8028
8029 /*
8030 * Repeat searching for a match until one is found that includes "mincol"
8031 * or none is found in this line.
8032 */
8033 called_emsg = FALSE;
8034 for (;;)
8035 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008036#ifdef FEAT_RELTIME
8037 /* Stop searching after passing the time limit. */
8038 if (profile_passed_limit(&(shl->tm)))
8039 {
8040 shl->lnum = 0; /* no match found in time */
8041 break;
8042 }
8043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 /* Three situations:
8045 * 1. No useful previous match: search from start of line.
8046 * 2. Not Vi compatible or empty match: continue at next character.
8047 * Break the loop if this is beyond the end of the line.
8048 * 3. Vi compatible searching: continue at end of previous match.
8049 */
8050 if (shl->lnum == 0)
8051 matchcol = 0;
8052 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8053 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008054 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008056 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008057
8058 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008059 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008060 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008062 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 shl->lnum = 0;
8064 break;
8065 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008066#ifdef FEAT_MBYTE
8067 if (has_mbyte)
8068 matchcol += mb_ptr2len(ml);
8069 else
8070#endif
8071 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 }
8073 else
8074 matchcol = shl->rm.endpos[0].col;
8075
8076 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008079 /* Remember whether shl->rm is using a copy of the regprog in
8080 * cur->match. */
8081 int regprog_is_copy = (shl != &search_hl && cur != NULL
8082 && shl == &cur->hl
8083 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008084 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008085
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8087 matchcol,
8088#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008089 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008090#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008091 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008092#endif
8093 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008094 /* Copy the regprog, in case it got freed and recompiled. */
8095 if (regprog_is_copy)
8096 cur->match.regprog = cur->hl.rm.regprog;
8097
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008098 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008099 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008100 /* Error while handling regexp: stop using this regexp. */
8101 if (shl == &search_hl)
8102 {
8103 /* don't free regprog in the match list, it's a copy */
8104 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008105 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008106 }
8107 shl->rm.regprog = NULL;
8108 shl->lnum = 0;
8109 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8110 message */
8111 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008112 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008113 }
8114 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008115 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008116 else
8117 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (nmatched == 0)
8119 {
8120 shl->lnum = 0; /* no match found */
8121 break;
8122 }
8123 if (shl->rm.startpos[0].lnum > 0
8124 || shl->rm.startpos[0].col >= mincol
8125 || nmatched > 1
8126 || shl->rm.endpos[0].col > mincol)
8127 {
8128 shl->lnum += shl->rm.startpos[0].lnum;
8129 break; /* useful match found */
8130 }
8131 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008132
8133 // Restore called_emsg for assert_fails().
8134 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaar85077472016-10-16 14:35:48 +02008137/*
8138 * If there is a match fill "shl" and return one.
8139 * Return zero otherwise.
8140 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008141 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008142next_search_hl_pos(
8143 match_T *shl, /* points to a match */
8144 linenr_T lnum,
8145 posmatch_T *posmatch, /* match positions */
8146 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008147{
8148 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008149 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008150
Bram Moolenaarb3414592014-06-17 17:48:32 +02008151 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8152 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008153 llpos_T *pos = &posmatch->pos[i];
8154
8155 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008156 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008157 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008158 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008159 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008160 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008161 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008162 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008163 /* if this match comes before the one at "found" then swap
8164 * them */
8165 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008167 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008168
Bram Moolenaar85077472016-10-16 14:35:48 +02008169 *pos = posmatch->pos[found];
8170 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008171 }
8172 }
8173 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008174 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175 }
8176 }
8177 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008178 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008179 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008180 colnr_T start = posmatch->pos[found].col == 0
8181 ? 0 : posmatch->pos[found].col - 1;
8182 colnr_T end = posmatch->pos[found].col == 0
8183 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008184
Bram Moolenaar85077472016-10-16 14:35:48 +02008185 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008186 shl->rm.startpos[0].lnum = 0;
8187 shl->rm.startpos[0].col = start;
8188 shl->rm.endpos[0].lnum = 0;
8189 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008190 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008191 posmatch->cur = found + 1;
8192 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008193 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008194 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008196#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008199screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200{
8201 attrentry_T *aep = NULL;
8202
8203 screen_attr = attr;
8204 if (full_screen
8205#ifdef WIN3264
8206 && termcap_active
8207#endif
8208 )
8209 {
8210#ifdef FEAT_GUI
8211 if (gui.in_use)
8212 {
8213 char buf[20];
8214
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008215 /* The GUI handles this internally. */
8216 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 OUT_STR(buf);
8218 }
8219 else
8220#endif
8221 {
8222 if (attr > HL_ALL) /* special HL attr. */
8223 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008224 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 aep = syn_cterm_attr2entry(attr);
8226 else
8227 aep = syn_term_attr2entry(attr);
8228 if (aep == NULL) /* did ":syntax clear" */
8229 attr = 0;
8230 else
8231 attr = aep->ae_attr;
8232 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008233 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008235 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008236#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008237 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8238 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8239 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008240#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008241 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008242 /* If the Normal FG color has BOLD attribute and the new HL
8243 * has a FG color defined, clear BOLD. */
8244 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008245 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008247 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008248 out_str(T_UCS);
8249 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008250 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8251 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008253 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008255 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008257 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008258 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259
8260 /*
8261 * Output the color or start string after bold etc., in case the
8262 * bold etc. override the color setting.
8263 */
8264 if (aep != NULL)
8265 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008266#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008267 /* When 'termguicolors' is set but fg or bg is unset,
8268 * fall back to the cterm colors. This helps for SpellBad,
8269 * where the GUI uses a red undercurl. */
8270 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008272 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008273 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008274 }
8275 else
8276#endif
8277 if (t_colors > 1)
8278 {
8279 if (aep->ae_u.cterm.fg_color)
8280 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8281 }
8282#ifdef FEAT_TERMGUICOLORS
8283 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8284 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008285 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008286 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 }
8288 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008289#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008290 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008292 if (aep->ae_u.cterm.bg_color)
8293 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8294 }
8295
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008296 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008297 {
8298 if (aep->ae_u.term.start != NULL)
8299 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301 }
8302 }
8303 }
8304}
8305
8306 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008307screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
8309 int do_ME = FALSE; /* output T_ME code */
8310
8311 if (screen_attr != 0
8312#ifdef WIN3264
8313 && termcap_active
8314#endif
8315 )
8316 {
8317#ifdef FEAT_GUI
8318 if (gui.in_use)
8319 {
8320 char buf[20];
8321
8322 /* use internal GUI code */
8323 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8324 OUT_STR(buf);
8325 }
8326 else
8327#endif
8328 {
8329 if (screen_attr > HL_ALL) /* special HL attr. */
8330 {
8331 attrentry_T *aep;
8332
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008333 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {
8335 /*
8336 * Assume that t_me restores the original colors!
8337 */
8338 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008339 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008340#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008341 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8342 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8343 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008344#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008345 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008346#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008347 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8348 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8349 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008350#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008351 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 do_ME = TRUE;
8353 }
8354 else
8355 {
8356 aep = syn_term_attr2entry(screen_attr);
8357 if (aep != NULL && aep->ae_u.term.stop != NULL)
8358 {
8359 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8360 do_ME = TRUE;
8361 else
8362 out_str(aep->ae_u.term.stop);
8363 }
8364 }
8365 if (aep == NULL) /* did ":syntax clear" */
8366 screen_attr = 0;
8367 else
8368 screen_attr = aep->ae_attr;
8369 }
8370
8371 /*
8372 * Often all ending-codes are equal to T_ME. Avoid outputting the
8373 * same sequence several times.
8374 */
8375 if (screen_attr & HL_STANDOUT)
8376 {
8377 if (STRCMP(T_SE, T_ME) == 0)
8378 do_ME = TRUE;
8379 else
8380 out_str(T_SE);
8381 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008382 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008383 {
8384 if (STRCMP(T_UCE, T_ME) == 0)
8385 do_ME = TRUE;
8386 else
8387 out_str(T_UCE);
8388 }
8389 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008390 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {
8392 if (STRCMP(T_UE, T_ME) == 0)
8393 do_ME = TRUE;
8394 else
8395 out_str(T_UE);
8396 }
8397 if (screen_attr & HL_ITALIC)
8398 {
8399 if (STRCMP(T_CZR, T_ME) == 0)
8400 do_ME = TRUE;
8401 else
8402 out_str(T_CZR);
8403 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008404 if (screen_attr & HL_STRIKETHROUGH)
8405 {
8406 if (STRCMP(T_STE, T_ME) == 0)
8407 do_ME = TRUE;
8408 else
8409 out_str(T_STE);
8410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8412 out_str(T_ME);
8413
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008414#ifdef FEAT_TERMGUICOLORS
8415 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008417 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008418 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008419 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008420 term_bg_rgb_color(cterm_normal_bg_gui_color);
8421 }
8422 else
8423#endif
8424 {
8425 if (t_colors > 1)
8426 {
8427 /* set Normal cterm colors */
8428 if (cterm_normal_fg_color != 0)
8429 term_fg_color(cterm_normal_fg_color - 1);
8430 if (cterm_normal_bg_color != 0)
8431 term_bg_color(cterm_normal_bg_color - 1);
8432 if (cterm_normal_fg_bold)
8433 out_str(T_MD);
8434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 }
8436 }
8437 }
8438 screen_attr = 0;
8439}
8440
8441/*
8442 * Reset the colors for a cterm. Used when leaving Vim.
8443 * The machine specific code may override this again.
8444 */
8445 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008446reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008448 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {
8450 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008451#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008452 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8453 || cterm_normal_bg_gui_color != INVALCOLOR)
8454 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008455#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 out_str(T_OP);
8460 screen_attr = -1;
8461 }
8462 if (cterm_normal_fg_bold)
8463 {
8464 out_str(T_ME);
8465 screen_attr = -1;
8466 }
8467 }
8468}
8469
8470/*
8471 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8472 * using the attributes from ScreenAttrs["off"].
8473 */
8474 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008475screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
8477 int attr;
8478
8479 /* Check for illegal values, just in case (could happen just after
8480 * resizing). */
8481 if (row >= screen_Rows || col >= screen_Columns)
8482 return;
8483
Bram Moolenaarae654382019-01-17 21:09:05 +01008484#ifdef FEAT_INS_EXPAND
8485 if (pum_under_menu(row, col))
8486 return;
8487#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008488 /* Outputting a character in the last cell on the screen may scroll the
8489 * screen up. Only do it when the "xn" termcap property is set, otherwise
8490 * mark the character invalid (update it when scrolled up). */
8491 if (*T_XN == NUL
8492 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493#ifdef FEAT_RIGHTLEFT
8494 /* account for first command-line character in rightleft mode */
8495 && !cmdmsg_rl
8496#endif
8497 )
8498 {
8499 ScreenAttrs[off] = (sattr_T)-1;
8500 return;
8501 }
8502
8503 /*
8504 * Stop highlighting first, so it's easier to move the cursor.
8505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 if (screen_char_attr != 0)
8507 attr = screen_char_attr;
8508 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 attr = ScreenAttrs[off];
8510 if (screen_attr != attr)
8511 screen_stop_highlight();
8512
8513 windgoto(row, col);
8514
8515 if (screen_attr != attr)
8516 screen_start_highlight(attr);
8517
8518#ifdef FEAT_MBYTE
8519 if (enc_utf8 && ScreenLinesUC[off] != 0)
8520 {
8521 char_u buf[MB_MAXBYTES + 1];
8522
Bram Moolenaarcb070082016-04-02 22:14:51 +02008523 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008524 {
8525 if (*p_ambw == 'd'
8526# ifdef FEAT_GUI
8527 && !gui.in_use
8528# endif
8529 )
8530 {
8531 /* Clear the two screen cells. If the character is actually
8532 * single width it won't change the second cell. */
8533 out_str((char_u *)" ");
8534 term_windgoto(row, col);
8535 }
8536 /* not sure where the cursor is after drawing the ambiguous width
8537 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008538 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008539 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008540 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008542
8543 /* Convert the UTF-8 character to bytes and write it. */
8544 buf[utfc_char2bytes(off, buf)] = NUL;
8545 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 }
8547 else
8548#endif
8549 {
8550#ifdef FEAT_MBYTE
8551 out_flush_check();
8552#endif
8553 out_char(ScreenLines[off]);
8554#ifdef FEAT_MBYTE
8555 /* double-byte character in single-width cell */
8556 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8557 out_char(ScreenLines2[off]);
8558#endif
8559 }
8560
8561 screen_cur_col++;
8562}
8563
8564#ifdef FEAT_MBYTE
8565
8566/*
8567 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8568 * on the screen at position 'row' and 'col'.
8569 * The attributes of the first byte is used for all. This is required to
8570 * output the two bytes of a double-byte character with nothing in between.
8571 */
8572 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008573screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 /* Check for illegal values (could be wrong when screen was resized). */
8576 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8577 return;
8578
8579 /* Outputting the last character on the screen may scrollup the screen.
8580 * Don't to it! Mark the character invalid (update it when scrolled up) */
8581 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8582 {
8583 ScreenAttrs[off] = (sattr_T)-1;
8584 return;
8585 }
8586
8587 /* Output the first byte normally (positions the cursor), then write the
8588 * second byte directly. */
8589 screen_char(off, row, col);
8590 out_char(ScreenLines[off + 1]);
8591 ++screen_cur_col;
8592}
8593#endif
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595/*
8596 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8597 * This uses the contents of ScreenLines[] and doesn't change it.
8598 */
8599 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008600screen_draw_rectangle(
8601 int row,
8602 int col,
8603 int height,
8604 int width,
8605 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 int r, c;
8608 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008609#ifdef FEAT_MBYTE
8610 int max_off;
8611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008613 /* Can't use ScreenLines unless initialized */
8614 if (ScreenLines == NULL)
8615 return;
8616
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (invert)
8618 screen_char_attr = HL_INVERSE;
8619 for (r = row; r < row + height; ++r)
8620 {
8621 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008622#ifdef FEAT_MBYTE
8623 max_off = off + screen_Columns;
8624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 for (c = col; c < col + width; ++c)
8626 {
8627#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008628 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {
8630 screen_char_2(off + c, r, c);
8631 ++c;
8632 }
8633 else
8634#endif
8635 {
8636 screen_char(off + c, r, c);
8637#ifdef FEAT_MBYTE
Bram Moolenaar367329b2007-08-30 11:53:22 +00008638 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 ++c;
8640#endif
8641 }
8642 }
8643 }
8644 screen_char_attr = 0;
8645}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647/*
8648 * Redraw the characters for a vertically split window.
8649 */
8650 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008651redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 int col;
8654 int width;
8655
8656# ifdef FEAT_CLIPBOARD
8657 clip_may_clear_selection(row, end - 1);
8658# endif
8659
8660 if (wp == NULL)
8661 {
8662 col = 0;
8663 width = Columns;
8664 }
8665 else
8666 {
8667 col = wp->w_wincol;
8668 width = wp->w_width;
8669 }
8670 screen_draw_rectangle(row, col, end - row, width, FALSE);
8671}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008673 static void
8674space_to_screenline(int off, int attr)
8675{
8676 ScreenLines[off] = ' ';
8677 ScreenAttrs[off] = attr;
8678# ifdef FEAT_MBYTE
8679 if (enc_utf8)
8680 ScreenLinesUC[off] = 0;
8681# endif
8682}
8683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684/*
8685 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8686 * with character 'c1' in first column followed by 'c2' in the other columns.
8687 * Use attributes 'attr'.
8688 */
8689 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008690screen_fill(
8691 int start_row,
8692 int end_row,
8693 int start_col,
8694 int end_col,
8695 int c1,
8696 int c2,
8697 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 int row;
8700 int col;
8701 int off;
8702 int end_off;
8703 int did_delete;
8704 int c;
8705 int norm_term;
8706#if defined(FEAT_GUI) || defined(UNIX)
8707 int force_next = FALSE;
8708#endif
8709
8710 if (end_row > screen_Rows) /* safety check */
8711 end_row = screen_Rows;
8712 if (end_col > screen_Columns) /* safety check */
8713 end_col = screen_Columns;
8714 if (ScreenLines == NULL
8715 || start_row >= end_row
8716 || start_col >= end_col) /* nothing to do */
8717 return;
8718
8719 /* it's a "normal" terminal when not in a GUI or cterm */
8720 norm_term = (
8721#ifdef FEAT_GUI
8722 !gui.in_use &&
8723#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008724 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 for (row = start_row; row < end_row; ++row)
8726 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008727#ifdef FEAT_MBYTE
8728 if (has_mbyte
8729# ifdef FEAT_GUI
8730 && !gui.in_use
8731# endif
8732 )
8733 {
8734 /* When drawing over the right halve of a double-wide char clear
8735 * out the left halve. When drawing over the left halve of a
8736 * double wide-char clear out the right halve. Only needed in a
8737 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008738 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008739 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008740 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008741 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008742 }
8743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 /*
8745 * Try to use delete-line termcap code, when no attributes or in a
8746 * "normal" terminal, where a bold/italic space is just a
8747 * space.
8748 */
8749 did_delete = FALSE;
8750 if (c2 == ' '
8751 && end_col == Columns
8752 && can_clear(T_CE)
8753 && (attr == 0
8754 || (norm_term
8755 && attr <= HL_ALL
8756 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8757 {
8758 /*
8759 * check if we really need to clear something
8760 */
8761 col = start_col;
8762 if (c1 != ' ') /* don't clear first char */
8763 ++col;
8764
8765 off = LineOffset[row] + col;
8766 end_off = LineOffset[row] + end_col;
8767
8768 /* skip blanks (used often, keep it fast!) */
8769#ifdef FEAT_MBYTE
8770 if (enc_utf8)
8771 while (off < end_off && ScreenLines[off] == ' '
8772 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8773 ++off;
8774 else
8775#endif
8776 while (off < end_off && ScreenLines[off] == ' '
8777 && ScreenAttrs[off] == 0)
8778 ++off;
8779 if (off < end_off) /* something to be cleared */
8780 {
8781 col = off - LineOffset[row];
8782 screen_stop_highlight();
8783 term_windgoto(row, col);/* clear rest of this screen line */
8784 out_str(T_CE);
8785 screen_start(); /* don't know where cursor is now */
8786 col = end_col - col;
8787 while (col--) /* clear chars in ScreenLines */
8788 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008789 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 ++off;
8791 }
8792 }
8793 did_delete = TRUE; /* the chars are cleared now */
8794 }
8795
8796 off = LineOffset[row] + start_col;
8797 c = c1;
8798 for (col = start_col; col < end_col; ++col)
8799 {
8800 if (ScreenLines[off] != c
8801#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008802 || (enc_utf8 && (int)ScreenLinesUC[off]
8803 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804#endif
8805 || ScreenAttrs[off] != attr
8806#if defined(FEAT_GUI) || defined(UNIX)
8807 || force_next
8808#endif
8809 )
8810 {
8811#if defined(FEAT_GUI) || defined(UNIX)
8812 /* The bold trick may make a single row of pixels appear in
8813 * the next character. When a bold character is removed, the
8814 * next character should be redrawn too. This happens for our
8815 * own GUI and for some xterms. */
8816 if (
8817# ifdef FEAT_GUI
8818 gui.in_use
8819# endif
8820# if defined(FEAT_GUI) && defined(UNIX)
8821 ||
8822# endif
8823# ifdef UNIX
8824 term_is_xterm
8825# endif
8826 )
8827 {
8828 if (ScreenLines[off] != ' '
8829 && (ScreenAttrs[off] > HL_ALL
8830 || ScreenAttrs[off] & HL_BOLD))
8831 force_next = TRUE;
8832 else
8833 force_next = FALSE;
8834 }
8835#endif
8836 ScreenLines[off] = c;
8837#ifdef FEAT_MBYTE
8838 if (enc_utf8)
8839 {
8840 if (c >= 0x80)
8841 {
8842 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008843 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 }
8845 else
8846 ScreenLinesUC[off] = 0;
8847 }
8848#endif
8849 ScreenAttrs[off] = attr;
8850 if (!did_delete || c != ' ')
8851 screen_char(off, row, col);
8852 }
8853 ++off;
8854 if (col == start_col)
8855 {
8856 if (did_delete)
8857 break;
8858 c = c2;
8859 }
8860 }
8861 if (end_col == Columns)
8862 LineWraps[row] = FALSE;
8863 if (row == Rows - 1) /* overwritten the command line */
8864 {
8865 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008866 if (start_col == 0 && end_col == Columns
8867 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008869 if (start_col == 0)
8870 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 }
8873}
8874
8875/*
8876 * Check if there should be a delay. Used before clearing or redrawing the
8877 * screen or the command line.
8878 */
8879 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008880check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8883 && !did_wait_return
8884 && emsg_silent == 0)
8885 {
8886 out_flush();
8887 ui_delay(1000L, TRUE);
8888 emsg_on_display = FALSE;
8889 if (check_msg_scroll)
8890 msg_scroll = FALSE;
8891 }
8892}
8893
8894/*
8895 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008896 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 * Returns TRUE if there is a valid screen to write to.
8898 * Returns FALSE when starting up and screen not initialized yet.
8899 */
8900 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008901screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008903 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 return (ScreenLines != NULL);
8905}
8906
8907/*
8908 * Resize the shell to Rows and Columns.
8909 * Allocate ScreenLines[] and associated items.
8910 *
8911 * There may be some time between setting Rows and Columns and (re)allocating
8912 * ScreenLines[]. This happens when starting up and when (manually) changing
8913 * the shell size. Always use screen_Rows and screen_Columns to access items
8914 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8915 * final size of the shell is needed.
8916 */
8917 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008918screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
8920 int new_row, old_row;
8921#ifdef FEAT_GUI
8922 int old_Rows;
8923#endif
8924 win_T *wp;
8925 int outofmem = FALSE;
8926 int len;
8927 schar_T *new_ScreenLines;
8928#ifdef FEAT_MBYTE
8929 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008932 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#endif
8934 sattr_T *new_ScreenAttrs;
8935 unsigned *new_LineOffset;
8936 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008937 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008938 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008940 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008941 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008943retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 /*
8945 * Allocation of the screen buffers is done only when the size changes and
8946 * when Rows and Columns have been set and we have started doing full
8947 * screen stuff.
8948 */
8949 if ((ScreenLines != NULL
8950 && Rows == screen_Rows
8951 && Columns == screen_Columns
8952#ifdef FEAT_MBYTE
8953 && enc_utf8 == (ScreenLinesUC != NULL)
8954 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008955 && p_mco == Screen_mco
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956#endif
8957 )
8958 || Rows == 0
8959 || Columns == 0
8960 || (!full_screen && ScreenLines == NULL))
8961 return;
8962
8963 /*
8964 * It's possible that we produce an out-of-memory message below, which
8965 * will cause this function to be called again. To break the loop, just
8966 * return here.
8967 */
8968 if (entered)
8969 return;
8970 entered = TRUE;
8971
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008972 /*
8973 * Note that the window sizes are updated before reallocating the arrays,
8974 * thus we must not redraw here!
8975 */
8976 ++RedrawingDisabled;
8977
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 win_new_shellsize(); /* fit the windows in the new sized shell */
8979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 comp_col(); /* recompute columns for shown command and ruler */
8981
8982 /*
8983 * We're changing the size of the screen.
8984 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8985 * - Move lines from the old arrays into the new arrays, clear extra
8986 * lines (unless the screen is going to be cleared).
8987 * - Free the old arrays.
8988 *
8989 * If anything fails, make ScreenLines NULL, so we don't do anything!
8990 * Continuing with the old ScreenLines may result in a crash, because the
8991 * size is wrong.
8992 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008993 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008995 if (aucmd_win != NULL)
8996 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
8998 new_ScreenLines = (schar_T *)lalloc((long_u)(
8999 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9000#ifdef FEAT_MBYTE
Bram Moolenaar216b7102010-03-23 13:56:59 +01009001 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 if (enc_utf8)
9003 {
9004 new_ScreenLinesUC = (u8char_T *)lalloc((long_u)(
9005 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009006 for (i = 0; i < p_mco; ++i)
Bram Moolenaar70c49c12010-03-23 15:36:35 +01009007 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear((long_u)(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 (Rows + 1) * Columns * sizeof(u8char_T)), FALSE);
9009 }
9010 if (enc_dbcs == DBCS_JPNU)
9011 new_ScreenLines2 = (schar_T *)lalloc((long_u)(
9012 (Rows + 1) * Columns * sizeof(schar_T)), FALSE);
9013#endif
9014 new_ScreenAttrs = (sattr_T *)lalloc((long_u)(
9015 (Rows + 1) * Columns * sizeof(sattr_T)), FALSE);
9016 new_LineOffset = (unsigned *)lalloc((long_u)(
9017 Rows * sizeof(unsigned)), FALSE);
9018 new_LineWraps = (char_u *)lalloc((long_u)(Rows * sizeof(char_u)), FALSE);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009019 new_TabPageIdxs = (short *)lalloc((long_u)(Columns * sizeof(short)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009021 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 {
9023 if (win_alloc_lines(wp) == FAIL)
9024 {
9025 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009026 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
9028 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009029 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9030 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009031 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009032give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009034#ifdef FEAT_MBYTE
9035 for (i = 0; i < p_mco; ++i)
9036 if (new_ScreenLinesC[i] == NULL)
9037 break;
9038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 if (new_ScreenLines == NULL
9040#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009041 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
9043#endif
9044 || new_ScreenAttrs == NULL
9045 || new_LineOffset == NULL
9046 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009047 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 || outofmem)
9049 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009050 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009051 {
9052 /* guess the size */
9053 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9054
9055 /* Remember we did this to avoid getting outofmem messages over
9056 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009057 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009058 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009059 VIM_CLEAR(new_ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +01009061 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009062 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009063 VIM_CLEAR(new_ScreenLinesC[i]);
9064 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01009066 VIM_CLEAR(new_ScreenAttrs);
9067 VIM_CLEAR(new_LineOffset);
9068 VIM_CLEAR(new_LineWraps);
9069 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 }
9071 else
9072 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009073 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009074
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 for (new_row = 0; new_row < Rows; ++new_row)
9076 {
9077 new_LineOffset[new_row] = new_row * Columns;
9078 new_LineWraps[new_row] = FALSE;
9079
9080 /*
9081 * If the screen is not going to be cleared, copy as much as
9082 * possible from the old screen to the new one and clear the rest
9083 * (used when resizing the window at the "--more--" prompt or when
9084 * executing an external command, for the GUI).
9085 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009086 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 {
9088 (void)vim_memset(new_ScreenLines + new_row * Columns,
9089 ' ', (size_t)Columns * sizeof(schar_T));
9090#ifdef FEAT_MBYTE
9091 if (enc_utf8)
9092 {
9093 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9094 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009095 for (i = 0; i < p_mco; ++i)
9096 (void)vim_memset(new_ScreenLinesC[i]
9097 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 0, (size_t)Columns * sizeof(u8char_T));
9099 }
9100 if (enc_dbcs == DBCS_JPNU)
9101 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9102 0, (size_t)Columns * sizeof(schar_T));
9103#endif
9104 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9105 0, (size_t)Columns * sizeof(sattr_T));
9106 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009107 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 {
9109 if (screen_Columns < Columns)
9110 len = screen_Columns;
9111 else
9112 len = Columns;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009113#ifdef FEAT_MBYTE
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009114 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009115 * may be invalid now. Also when p_mco changes. */
9116 if (!(enc_utf8 && ScreenLinesUC == NULL)
9117 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009118#endif
9119 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9120 ScreenLines + LineOffset[old_row],
9121 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009123 if (enc_utf8 && ScreenLinesUC != NULL
9124 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 {
9126 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9127 ScreenLinesUC + LineOffset[old_row],
9128 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009129 for (i = 0; i < p_mco; ++i)
9130 mch_memmove(new_ScreenLinesC[i]
9131 + new_LineOffset[new_row],
9132 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 (size_t)len * sizeof(u8char_T));
9134 }
9135 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9136 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9137 ScreenLines2 + LineOffset[old_row],
9138 (size_t)len * sizeof(schar_T));
9139#endif
9140 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9141 ScreenAttrs + LineOffset[old_row],
9142 (size_t)len * sizeof(sattr_T));
9143 }
9144 }
9145 }
9146 /* Use the last line of the screen for the current line. */
9147 current_ScreenLine = new_ScreenLines + Rows * Columns;
9148 }
9149
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009150 free_screenlines();
9151
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 ScreenLines = new_ScreenLines;
9153#ifdef FEAT_MBYTE
9154 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009155 for (i = 0; i < p_mco; ++i)
9156 ScreenLinesC[i] = new_ScreenLinesC[i];
9157 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 ScreenLines2 = new_ScreenLines2;
9159#endif
9160 ScreenAttrs = new_ScreenAttrs;
9161 LineOffset = new_LineOffset;
9162 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009163 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165 /* It's important that screen_Rows and screen_Columns reflect the actual
9166 * size of ScreenLines[]. Set them before calling anything. */
9167#ifdef FEAT_GUI
9168 old_Rows = screen_Rows;
9169#endif
9170 screen_Rows = Rows;
9171 screen_Columns = Columns;
9172
9173 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009174 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 screenclear2();
9176
9177#ifdef FEAT_GUI
9178 else if (gui.in_use
9179 && !gui.starting
9180 && ScreenLines != NULL
9181 && old_Rows != Rows)
9182 {
9183 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9184 /*
9185 * Adjust the position of the cursor, for when executing an external
9186 * command.
9187 */
9188 if (msg_row >= Rows) /* Rows got smaller */
9189 msg_row = Rows - 1; /* put cursor at last row */
9190 else if (Rows > old_Rows) /* Rows got bigger */
9191 msg_row += Rows - old_Rows; /* put cursor in same place */
9192 if (msg_col >= Columns) /* Columns got smaller */
9193 msg_col = Columns - 1; /* put cursor at last column */
9194 }
9195#endif
9196
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009198 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009199
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009200 /*
9201 * Do not apply autocommands more than 3 times to avoid an endless loop
9202 * in case applying autocommands always changes Rows or Columns.
9203 */
9204 if (starting == 0 && ++retry_count <= 3)
9205 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009206 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009207 /* In rare cases, autocommands may have altered Rows or Columns,
9208 * jump back to check if we need to allocate the screen again. */
9209 goto retry;
9210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211}
9212
9213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009214free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009215{
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009216#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009217 int i;
9218
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009219 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009220 for (i = 0; i < Screen_mco; ++i)
9221 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009222 vim_free(ScreenLines2);
9223#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009224 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009225 vim_free(ScreenAttrs);
9226 vim_free(LineOffset);
9227 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009228 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009229}
9230
9231 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009232screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 check_for_delay(FALSE);
9235 screenalloc(FALSE); /* allocate screen buffers if size changed */
9236 screenclear2(); /* clear the screen */
9237}
9238
9239 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009240screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241{
9242 int i;
9243
9244 if (starting == NO_SCREEN || ScreenLines == NULL
9245#ifdef FEAT_GUI
9246 || (gui.in_use && gui.starting)
9247#endif
9248 )
9249 return;
9250
9251#ifdef FEAT_GUI
9252 if (!gui.in_use)
9253#endif
9254 screen_attr = -1; /* force setting the Normal colors */
9255 screen_stop_highlight(); /* don't want highlighting here */
9256
9257#ifdef FEAT_CLIPBOARD
9258 /* disable selection without redrawing it */
9259 clip_scroll_selection(9999);
9260#endif
9261
9262 /* blank out ScreenLines */
9263 for (i = 0; i < Rows; ++i)
9264 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009265 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 LineWraps[i] = FALSE;
9267 }
9268
9269 if (can_clear(T_CL))
9270 {
9271 out_str(T_CL); /* clear the display */
9272 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009273 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 }
9275 else
9276 {
9277 /* can't clear the screen, mark all chars with invalid attributes */
9278 for (i = 0; i < Rows; ++i)
9279 lineinvalid(LineOffset[i], (int)Columns);
9280 clear_cmdline = TRUE;
9281 }
9282
9283 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9284
9285 win_rest_invalid(firstwin);
9286 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009287 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 if (must_redraw == CLEAR) /* no need to clear again */
9289 must_redraw = NOT_VALID;
9290 compute_cmdrow();
9291 msg_row = cmdline_row; /* put cursor on last line for messages */
9292 msg_col = 0;
9293 screen_start(); /* don't know where cursor is now */
9294 msg_scrolled = 0; /* can't scroll back */
9295 msg_didany = FALSE;
9296 msg_didout = FALSE;
9297}
9298
9299/*
9300 * Clear one line in ScreenLines.
9301 */
9302 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009303lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304{
9305 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
9306#ifdef FEAT_MBYTE
9307 if (enc_utf8)
9308 (void)vim_memset(ScreenLinesUC + off, 0,
9309 (size_t)width * sizeof(u8char_T));
9310#endif
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009311 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312}
9313
9314/*
9315 * Mark one line in ScreenLines invalid by setting the attributes to an
9316 * invalid value.
9317 */
9318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009319lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9322}
9323
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324/*
9325 * Copy part of a Screenline for vertically split window "wp".
9326 */
9327 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009328linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330 unsigned off_to = LineOffset[to] + wp->w_wincol;
9331 unsigned off_from = LineOffset[from] + wp->w_wincol;
9332
9333 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9334 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009335#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (enc_utf8)
9337 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009338 int i;
9339
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9341 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009342 for (i = 0; i < p_mco; ++i)
9343 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9344 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 }
9346 if (enc_dbcs == DBCS_JPNU)
9347 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9348 wp->w_width * sizeof(schar_T));
Bram Moolenaar4033c552017-09-16 20:54:51 +02009349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9351 wp->w_width * sizeof(sattr_T));
9352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
9354/*
9355 * Return TRUE if clearing with term string "p" would work.
9356 * It can't work when the string is empty or it won't set the right background.
9357 */
9358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009359can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360{
9361 return (*p != NUL && (t_colors <= 1
9362#ifdef FEAT_GUI
9363 || gui.in_use
9364#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009365#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009366 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009367 || (!p_tgc && cterm_normal_bg_color == 0)
9368#else
9369 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009370#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009371 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372}
9373
9374/*
9375 * Reset cursor position. Use whenever cursor was moved because of outputting
9376 * something directly to the screen (shell commands) or a terminal control
9377 * code.
9378 */
9379 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009380screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381{
9382 screen_cur_row = screen_cur_col = 9999;
9383}
9384
9385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 * Move the cursor to position "row","col" in the screen.
9387 * This tries to find the most efficient way to move, minimizing the number of
9388 * characters sent to the terminal.
9389 */
9390 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009391windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009393 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 int i;
9395 int plan;
9396 int cost;
9397 int wouldbe_col;
9398 int noinvcurs;
9399 char_u *bs;
9400 int goto_cost;
9401 int attr;
9402
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009403#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9405
9406#define PLAN_LE 1
9407#define PLAN_CR 2
9408#define PLAN_NL 3
9409#define PLAN_WRITE 4
9410 /* Can't use ScreenLines unless initialized */
9411 if (ScreenLines == NULL)
9412 return;
9413
9414 if (col != screen_cur_col || row != screen_cur_row)
9415 {
9416 /* Check for valid position. */
9417 if (row < 0) /* window without text lines? */
9418 row = 0;
9419 if (row >= screen_Rows)
9420 row = screen_Rows - 1;
9421 if (col >= screen_Columns)
9422 col = screen_Columns - 1;
9423
9424 /* check if no cursor movement is allowed in highlight mode */
9425 if (screen_attr && *T_MS == NUL)
9426 noinvcurs = HIGHL_COST;
9427 else
9428 noinvcurs = 0;
9429 goto_cost = GOTO_COST + noinvcurs;
9430
9431 /*
9432 * Plan how to do the positioning:
9433 * 1. Use CR to move it to column 0, same row.
9434 * 2. Use T_LE to move it a few columns to the left.
9435 * 3. Use NL to move a few lines down, column 0.
9436 * 4. Move a few columns to the right with T_ND or by writing chars.
9437 *
9438 * Don't do this if the cursor went beyond the last column, the cursor
9439 * position is unknown then (some terminals wrap, some don't )
9440 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009441 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 * characters to move the cursor to the right.
9443 */
9444 if (row >= screen_cur_row && screen_cur_col < Columns)
9445 {
9446 /*
9447 * If the cursor is in the same row, bigger col, we can use CR
9448 * or T_LE.
9449 */
9450 bs = NULL; /* init for GCC */
9451 attr = screen_attr;
9452 if (row == screen_cur_row && col < screen_cur_col)
9453 {
9454 /* "le" is preferred over "bc", because "bc" is obsolete */
9455 if (*T_LE)
9456 bs = T_LE; /* "cursor left" */
9457 else
9458 bs = T_BC; /* "backspace character (old) */
9459 if (*bs)
9460 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9461 else
9462 cost = 999;
9463 if (col + 1 < cost) /* using CR is less characters */
9464 {
9465 plan = PLAN_CR;
9466 wouldbe_col = 0;
9467 cost = 1; /* CR is just one character */
9468 }
9469 else
9470 {
9471 plan = PLAN_LE;
9472 wouldbe_col = col;
9473 }
9474 if (noinvcurs) /* will stop highlighting */
9475 {
9476 cost += noinvcurs;
9477 attr = 0;
9478 }
9479 }
9480
9481 /*
9482 * If the cursor is above where we want to be, we can use CR LF.
9483 */
9484 else if (row > screen_cur_row)
9485 {
9486 plan = PLAN_NL;
9487 wouldbe_col = 0;
9488 cost = (row - screen_cur_row) * 2; /* CR LF */
9489 if (noinvcurs) /* will stop highlighting */
9490 {
9491 cost += noinvcurs;
9492 attr = 0;
9493 }
9494 }
9495
9496 /*
9497 * If the cursor is in the same row, smaller col, just use write.
9498 */
9499 else
9500 {
9501 plan = PLAN_WRITE;
9502 wouldbe_col = screen_cur_col;
9503 cost = 0;
9504 }
9505
9506 /*
9507 * Check if any characters that need to be written have the
9508 * correct attributes. Also avoid UTF-8 characters.
9509 */
9510 i = col - wouldbe_col;
9511 if (i > 0)
9512 cost += i;
9513 if (cost < goto_cost && i > 0)
9514 {
9515 /*
9516 * Check if the attributes are correct without additionally
9517 * stopping highlighting.
9518 */
9519 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9520 while (i && *p++ == attr)
9521 --i;
9522 if (i != 0)
9523 {
9524 /*
9525 * Try if it works when highlighting is stopped here.
9526 */
9527 if (*--p == 0)
9528 {
9529 cost += noinvcurs;
9530 while (i && *p++ == 0)
9531 --i;
9532 }
9533 if (i != 0)
9534 cost = 999; /* different attributes, don't do it */
9535 }
9536#ifdef FEAT_MBYTE
9537 if (enc_utf8)
9538 {
9539 /* Don't use an UTF-8 char for positioning, it's slow. */
9540 for (i = wouldbe_col; i < col; ++i)
9541 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9542 {
9543 cost = 999;
9544 break;
9545 }
9546 }
9547#endif
9548 }
9549
9550 /*
9551 * We can do it without term_windgoto()!
9552 */
9553 if (cost < goto_cost)
9554 {
9555 if (plan == PLAN_LE)
9556 {
9557 if (noinvcurs)
9558 screen_stop_highlight();
9559 while (screen_cur_col > col)
9560 {
9561 out_str(bs);
9562 --screen_cur_col;
9563 }
9564 }
9565 else if (plan == PLAN_CR)
9566 {
9567 if (noinvcurs)
9568 screen_stop_highlight();
9569 out_char('\r');
9570 screen_cur_col = 0;
9571 }
9572 else if (plan == PLAN_NL)
9573 {
9574 if (noinvcurs)
9575 screen_stop_highlight();
9576 while (screen_cur_row < row)
9577 {
9578 out_char('\n');
9579 ++screen_cur_row;
9580 }
9581 screen_cur_col = 0;
9582 }
9583
9584 i = col - screen_cur_col;
9585 if (i > 0)
9586 {
9587 /*
9588 * Use cursor-right if it's one character only. Avoids
9589 * removing a line of pixels from the last bold char, when
9590 * using the bold trick in the GUI.
9591 */
9592 if (T_ND[0] != NUL && T_ND[1] == NUL)
9593 {
9594 while (i-- > 0)
9595 out_char(*T_ND);
9596 }
9597 else
9598 {
9599 int off;
9600
9601 off = LineOffset[row] + screen_cur_col;
9602 while (i-- > 0)
9603 {
9604 if (ScreenAttrs[off] != screen_attr)
9605 screen_stop_highlight();
9606#ifdef FEAT_MBYTE
9607 out_flush_check();
9608#endif
9609 out_char(ScreenLines[off]);
9610#ifdef FEAT_MBYTE
9611 if (enc_dbcs == DBCS_JPNU
9612 && ScreenLines[off] == 0x8e)
9613 out_char(ScreenLines2[off]);
9614#endif
9615 ++off;
9616 }
9617 }
9618 }
9619 }
9620 }
9621 else
9622 cost = 999;
9623
9624 if (cost >= goto_cost)
9625 {
9626 if (noinvcurs)
9627 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009628 if (row == screen_cur_row && (col > screen_cur_col)
9629 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 term_cursor_right(col - screen_cur_col);
9631 else
9632 term_windgoto(row, col);
9633 }
9634 screen_cur_row = row;
9635 screen_cur_col = col;
9636 }
9637}
9638
9639/*
9640 * Set cursor to its position in the current window.
9641 */
9642 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009643setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009645 setcursor_mayforce(FALSE);
9646}
9647
9648/*
9649 * Set cursor to its position in the current window.
9650 * When "force" is TRUE also when not redrawing.
9651 */
9652 void
9653setcursor_mayforce(int force)
9654{
9655 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 {
9657 validate_cursor();
9658 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009659 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009661 /* With 'rightleft' set and the cursor on a double-wide
9662 * character, position it on the leftmost column. */
Bram Moolenaar02631462017-09-22 15:20:32 +02009663 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol - (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664# ifdef FEAT_MBYTE
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009665 (has_mbyte
9666 && (*mb_ptr2cells)(ml_get_cursor()) == 2
9667 && vim_isprintc(gchar_cursor())) ? 2 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668# endif
9669 1)) :
9670#endif
9671 curwin->w_wcol));
9672 }
9673}
9674
9675
9676/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009677 * Insert 'line_count' lines at 'row' in window 'wp'.
9678 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9679 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 * scrolling.
9681 * Returns FAIL if the lines are not inserted, OK for success.
9682 */
9683 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009684win_ins_lines(
9685 win_T *wp,
9686 int row,
9687 int line_count,
9688 int invalid,
9689 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
9691 int did_delete;
9692 int nextrow;
9693 int lastrow;
9694 int retval;
9695
9696 if (invalid)
9697 wp->w_lines_valid = 0;
9698
9699 if (wp->w_height < 5)
9700 return FAIL;
9701
9702 if (line_count > wp->w_height - row)
9703 line_count = wp->w_height - row;
9704
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009705 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (retval != MAYBE)
9707 return retval;
9708
9709 /*
9710 * If there is a next window or a status line, we first try to delete the
9711 * lines at the bottom to avoid messing what is after the window.
9712 * If this fails and there are following windows, don't do anything to avoid
9713 * messing up those windows, better just redraw.
9714 */
9715 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (wp->w_next != NULL || wp->w_status_height)
9717 {
9718 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009719 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 did_delete = TRUE;
9721 else if (wp->w_next)
9722 return FAIL;
9723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 /*
9725 * if no lines deleted, blank the lines that will end up below the window
9726 */
9727 if (!did_delete)
9728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009731 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 lastrow = nextrow + line_count;
9733 if (lastrow > Rows)
9734 lastrow = Rows;
9735 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009736 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 ' ', ' ', 0);
9738 }
9739
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009740 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 == FAIL)
9742 {
9743 /* deletion will have messed up other windows */
9744 if (did_delete)
9745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 win_rest_invalid(W_NEXT(wp));
9748 }
9749 return FAIL;
9750 }
9751
9752 return OK;
9753}
9754
9755/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009756 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9758 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9759 * scrolling
9760 * Return OK for success, FAIL if the lines are not deleted.
9761 */
9762 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009763win_del_lines(
9764 win_T *wp,
9765 int row,
9766 int line_count,
9767 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009768 int mayclear,
9769 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 int retval;
9772
9773 if (invalid)
9774 wp->w_lines_valid = 0;
9775
9776 if (line_count > wp->w_height - row)
9777 line_count = wp->w_height - row;
9778
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009779 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (retval != MAYBE)
9781 return retval;
9782
9783 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009784 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 return FAIL;
9786
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 /*
9788 * If there are windows or status lines below, try to put them at the
9789 * correct place. If we can't do that, they have to be redrawn.
9790 */
9791 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9792 {
9793 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009794 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 {
9796 wp->w_redr_status = TRUE;
9797 win_rest_invalid(wp->w_next);
9798 }
9799 }
9800 /*
9801 * If this is the last window and there is no status line, redraw the
9802 * command line later.
9803 */
9804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 redraw_cmdline = TRUE;
9806 return OK;
9807}
9808
9809/*
9810 * Common code for win_ins_lines() and win_del_lines().
9811 * Returns OK or FAIL when the work has been done.
9812 * Returns MAYBE when not finished yet.
9813 */
9814 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009815win_do_lines(
9816 win_T *wp,
9817 int row,
9818 int line_count,
9819 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009820 int del,
9821 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822{
9823 int retval;
9824
9825 if (!redrawing() || line_count <= 0)
9826 return FAIL;
9827
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009828 /* When inserting lines would result in loss of command output, just redraw
9829 * the lines. */
9830 if (no_win_do_lines_ins && !del)
9831 return FAIL;
9832
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009834 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009836 if (!no_win_do_lines_ins)
9837 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 return FAIL;
9839 }
9840
9841 /*
9842 * Delete all remaining lines
9843 */
9844 if (row + line_count >= wp->w_height)
9845 {
9846 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009847 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 ' ', ' ', 0);
9849 return OK;
9850 }
9851
9852 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009853 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009855 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009857 if (!no_win_do_lines_ins)
9858 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
9860 /*
9861 * If the terminal can set a scroll region, use that.
9862 * Always do this in a vertically split window. This will redraw from
9863 * ScreenLines[] when t_CV isn't defined. That's faster than using
9864 * win_line().
9865 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009866 * a character in the lower right corner of the scroll region may cause a
9867 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009869 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 scroll_region_set(wp, row);
9873 if (del)
9874 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009875 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 else
9877 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009878 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 scroll_region_reset();
9881 return retval;
9882 }
9883
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9885 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
9887 return MAYBE;
9888}
9889
9890/*
9891 * window 'wp' and everything after it is messed up, mark it for redraw
9892 */
9893 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009894win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 {
9898 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 wp->w_redr_status = TRUE;
9900 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 }
9902 redraw_cmdline = TRUE;
9903}
9904
9905/*
9906 * The rest of the routines in this file perform screen manipulations. The
9907 * given operation is performed physically on the screen. The corresponding
9908 * change is also made to the internal screen image. In this way, the editor
9909 * anticipates the effect of editing changes on the appearance of the screen.
9910 * That way, when we call screenupdate a complete redraw isn't usually
9911 * necessary. Another advantage is that we can keep adding code to anticipate
9912 * screen changes, and in the meantime, everything still works.
9913 */
9914
9915/*
9916 * types for inserting or deleting lines
9917 */
9918#define USE_T_CAL 1
9919#define USE_T_CDL 2
9920#define USE_T_AL 3
9921#define USE_T_CE 4
9922#define USE_T_DL 5
9923#define USE_T_SR 6
9924#define USE_NL 7
9925#define USE_T_CD 8
9926#define USE_REDRAW 9
9927
9928/*
9929 * insert lines on the screen and update ScreenLines[]
9930 * 'end' is the line after the scrolled part. Normally it is Rows.
9931 * When scrolling region used 'off' is the offset from the top for the region.
9932 * 'row' and 'end' are relative to the start of the region.
9933 *
9934 * return FAIL for failure, OK for success.
9935 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009936 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009937screen_ins_lines(
9938 int off,
9939 int row,
9940 int line_count,
9941 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009942 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009943 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944{
9945 int i;
9946 int j;
9947 unsigned temp;
9948 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009949 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 int type;
9951 int result_empty;
9952 int can_ce = can_clear(T_CE);
9953
9954 /*
9955 * FAIL if
9956 * - there is no valid screen
9957 * - the screen has to be redrawn completely
9958 * - the line count is less than one
9959 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009960 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009962 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9963#ifdef FEAT_CLIPBOARD
9964 || (clip_star.state != SELECT_CLEARED
9965 && redrawing_for_callback > 0)
9966#endif
9967 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 return FAIL;
9969
9970 /*
9971 * There are seven ways to insert lines:
9972 * 0. When in a vertically split window and t_CV isn't set, redraw the
9973 * characters from ScreenLines[].
9974 * 1. Use T_CD (clear to end of display) if it exists and the result of
9975 * the insert is just empty lines
9976 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9977 * present or line_count > 1. It looks better if we do all the inserts
9978 * at once.
9979 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9980 * insert is just empty lines and T_CE is not present or line_count >
9981 * 1.
9982 * 4. Use T_AL (insert line) if it exists.
9983 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9984 * just empty lines.
9985 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9986 * just empty lines.
9987 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9988 * the 'da' flag is not set or we have clear line capability.
9989 * 8. redraw the characters from ScreenLines[].
9990 *
9991 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9992 * the scrollbar for the window. It does have insert line, use that if it
9993 * exists.
9994 */
9995 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9997 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009998 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 type = USE_T_CD;
10000 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10001 type = USE_T_CAL;
10002 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10003 type = USE_T_CDL;
10004 else if (*T_AL != NUL)
10005 type = USE_T_AL;
10006 else if (can_ce && result_empty)
10007 type = USE_T_CE;
10008 else if (*T_DL != NUL && result_empty)
10009 type = USE_T_DL;
10010 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10011 type = USE_T_SR;
10012 else
10013 return FAIL;
10014
10015 /*
10016 * For clearing the lines screen_del_lines() is used. This will also take
10017 * care of t_db if necessary.
10018 */
10019 if (type == USE_T_CD || type == USE_T_CDL ||
10020 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010021 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
10023 /*
10024 * If text is retained below the screen, first clear or delete as many
10025 * lines at the bottom of the window as are about to be inserted so that
10026 * the deleted lines won't later surface during a screen_del_lines.
10027 */
10028 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010029 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030
10031#ifdef FEAT_CLIPBOARD
10032 /* Remove a modeless selection when inserting lines halfway the screen
10033 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010034 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010035 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 else
10037 clip_scroll_selection(-line_count);
10038#endif
10039
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040#ifdef FEAT_GUI
10041 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10042 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010043 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#endif
10045
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010046 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10047 cursor_col = wp->w_wincol;
10048
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 if (*T_CCS != NUL) /* cursor relative to region */
10050 cursor_row = row;
10051 else
10052 cursor_row = row + off;
10053
10054 /*
10055 * Shift LineOffset[] line_count down to reflect the inserted lines.
10056 * Clear the inserted lines in ScreenLines[].
10057 */
10058 row += off;
10059 end += off;
10060 for (i = 0; i < line_count; ++i)
10061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (wp != NULL && wp->w_width != Columns)
10063 {
10064 /* need to copy part of a line */
10065 j = end - 1 - i;
10066 while ((j -= line_count) >= row)
10067 linecopy(j + line_count, j, wp);
10068 j += line_count;
10069 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010070 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10071 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 else
10073 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10074 LineWraps[j] = FALSE;
10075 }
10076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 {
10078 j = end - 1 - i;
10079 temp = LineOffset[j];
10080 while ((j -= line_count) >= row)
10081 {
10082 LineOffset[j + line_count] = LineOffset[j];
10083 LineWraps[j + line_count] = LineWraps[j];
10084 }
10085 LineOffset[j + line_count] = temp;
10086 LineWraps[j + line_count] = FALSE;
10087 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010088 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 else
10090 lineinvalid(temp, (int)Columns);
10091 }
10092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
10094 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010095 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010096 if (clear_attr != 0)
10097 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 /* redraw the characters */
10100 if (type == USE_REDRAW)
10101 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010102 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 {
10104 term_append_lines(line_count);
10105 screen_start(); /* don't know where cursor is now */
10106 }
10107 else
10108 {
10109 for (i = 0; i < line_count; i++)
10110 {
10111 if (type == USE_T_AL)
10112 {
10113 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010114 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 out_str(T_AL);
10116 }
10117 else /* type == USE_T_SR */
10118 out_str(T_SR);
10119 screen_start(); /* don't know where cursor is now */
10120 }
10121 }
10122
10123 /*
10124 * With scroll-reverse and 'da' flag set we need to clear the lines that
10125 * have been scrolled down into the region.
10126 */
10127 if (type == USE_T_SR && *T_DA)
10128 {
10129 for (i = 0; i < line_count; ++i)
10130 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010131 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 out_str(T_CE);
10133 screen_start(); /* don't know where cursor is now */
10134 }
10135 }
10136
10137#ifdef FEAT_GUI
10138 gui_can_update_cursor();
10139 if (gui.in_use)
10140 out_flush(); /* always flush after a scroll */
10141#endif
10142 return OK;
10143}
10144
10145/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010146 * Delete lines on the screen and update ScreenLines[].
10147 * "end" is the line after the scrolled part. Normally it is Rows.
10148 * When scrolling region used "off" is the offset from the top for the region.
10149 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 *
10151 * Return OK for success, FAIL if the lines are not deleted.
10152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010154screen_del_lines(
10155 int off,
10156 int row,
10157 int line_count,
10158 int end,
10159 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010160 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010161 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162{
10163 int j;
10164 int i;
10165 unsigned temp;
10166 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010167 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 int cursor_end;
10169 int result_empty; /* result is empty until end of region */
10170 int can_delete; /* deleting line codes can be used */
10171 int type;
10172
10173 /*
10174 * FAIL if
10175 * - there is no valid screen
10176 * - the screen has to be redrawn completely
10177 * - the line count is less than one
10178 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010179 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010181 if (!screen_valid(TRUE) || line_count <= 0
10182 || (!force && line_count > p_ttyscroll)
10183#ifdef FEAT_CLIPBOARD
10184 || (clip_star.state != SELECT_CLEARED
10185 && redrawing_for_callback > 0)
10186#endif
10187 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 return FAIL;
10189
10190 /*
10191 * Check if the rest of the current region will become empty.
10192 */
10193 result_empty = row + line_count >= end;
10194
10195 /*
10196 * We can delete lines only when 'db' flag not set or when 'ce' option
10197 * available.
10198 */
10199 can_delete = (*T_DB == NUL || can_clear(T_CE));
10200
10201 /*
10202 * There are six ways to delete lines:
10203 * 0. When in a vertically split window and t_CV isn't set, redraw the
10204 * characters from ScreenLines[].
10205 * 1. Use T_CD if it exists and the result is empty.
10206 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10207 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10208 * none of the other ways work.
10209 * 4. Use T_CE (erase line) if the result is empty.
10210 * 5. Use T_DL (delete line) if it exists.
10211 * 6. redraw the characters from ScreenLines[].
10212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10214 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010215 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 type = USE_T_CD;
10217#if defined(__BEOS__) && defined(BEOS_DR8)
10218 /*
10219 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10220 * its internal termcap... this works okay for tests which test *T_DB !=
10221 * NUL. It has the disadvantage that the user cannot use any :set t_*
10222 * command to get T_DB (back) to empty_option, only :set term=... will do
10223 * the trick...
10224 * Anyway, this hack will hopefully go away with the next OS release.
10225 * (Olaf Seibert)
10226 */
10227 else if (row == 0 && T_DB == empty_option
10228 && (line_count == 1 || *T_CDL == NUL))
10229#else
10230 else if (row == 0 && (
10231#ifndef AMIGA
10232 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10233 * up, so use delete-line command */
10234 line_count == 1 ||
10235#endif
10236 *T_CDL == NUL))
10237#endif
10238 type = USE_NL;
10239 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10240 type = USE_T_CDL;
10241 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010242 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243 type = USE_T_CE;
10244 else if (*T_DL != NUL && can_delete)
10245 type = USE_T_DL;
10246 else if (*T_CDL != NUL && can_delete)
10247 type = USE_T_CDL;
10248 else
10249 return FAIL;
10250
10251#ifdef FEAT_CLIPBOARD
10252 /* Remove a modeless selection when deleting lines halfway the screen or
10253 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010254 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010255 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 else
10257 clip_scroll_selection(line_count);
10258#endif
10259
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260#ifdef FEAT_GUI
10261 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10262 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010263 gui_dont_update_cursor(gui.cursor_row >= row + off
10264 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265#endif
10266
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010267 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10268 cursor_col = wp->w_wincol;
10269
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 if (*T_CCS != NUL) /* cursor relative to region */
10271 {
10272 cursor_row = row;
10273 cursor_end = end;
10274 }
10275 else
10276 {
10277 cursor_row = row + off;
10278 cursor_end = end + off;
10279 }
10280
10281 /*
10282 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10283 * Clear the inserted lines in ScreenLines[].
10284 */
10285 row += off;
10286 end += off;
10287 for (i = 0; i < line_count; ++i)
10288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 if (wp != NULL && wp->w_width != Columns)
10290 {
10291 /* need to copy part of a line */
10292 j = row + i;
10293 while ((j += line_count) <= end - 1)
10294 linecopy(j - line_count, j, wp);
10295 j -= line_count;
10296 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010297 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10298 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 else
10300 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10301 LineWraps[j] = FALSE;
10302 }
10303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 {
10305 /* whole width, moving the line pointers is faster */
10306 j = row + i;
10307 temp = LineOffset[j];
10308 while ((j += line_count) <= end - 1)
10309 {
10310 LineOffset[j - line_count] = LineOffset[j];
10311 LineWraps[j - line_count] = LineWraps[j];
10312 }
10313 LineOffset[j - line_count] = temp;
10314 LineWraps[j - line_count] = FALSE;
10315 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010316 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 else
10318 lineinvalid(temp, (int)Columns);
10319 }
10320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010322 if (screen_attr != clear_attr)
10323 screen_stop_highlight();
10324 if (clear_attr != 0)
10325 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 /* redraw the characters */
10328 if (type == USE_REDRAW)
10329 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010330 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010332 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 out_str(T_CD);
10334 screen_start(); /* don't know where cursor is now */
10335 }
10336 else if (type == USE_T_CDL)
10337 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010338 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 term_delete_lines(line_count);
10340 screen_start(); /* don't know where cursor is now */
10341 }
10342 /*
10343 * Deleting lines at top of the screen or scroll region: Just scroll
10344 * the whole screen (scroll region) up by outputting newlines on the
10345 * last line.
10346 */
10347 else if (type == USE_NL)
10348 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010349 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 for (i = line_count; --i >= 0; )
10351 out_char('\n'); /* cursor will remain on same line */
10352 }
10353 else
10354 {
10355 for (i = line_count; --i >= 0; )
10356 {
10357 if (type == USE_T_DL)
10358 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010359 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 out_str(T_DL); /* delete a line */
10361 }
10362 else /* type == USE_T_CE */
10363 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010364 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 out_str(T_CE); /* erase a line */
10366 }
10367 screen_start(); /* don't know where cursor is now */
10368 }
10369 }
10370
10371 /*
10372 * If the 'db' flag is set, we need to clear the lines that have been
10373 * scrolled up at the bottom of the region.
10374 */
10375 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10376 {
10377 for (i = line_count; i > 0; --i)
10378 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010379 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 out_str(T_CE); /* erase a line */
10381 screen_start(); /* don't know where cursor is now */
10382 }
10383 }
10384
10385#ifdef FEAT_GUI
10386 gui_can_update_cursor();
10387 if (gui.in_use)
10388 out_flush(); /* always flush after a scroll */
10389#endif
10390
10391 return OK;
10392}
10393
10394/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010395 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 *
10397 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10398 * If clear_cmdline is FALSE there may be a message there that needs to be
10399 * cleared only if a mode is shown.
10400 * Return the length of the message (0 if no message).
10401 */
10402 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010403showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404{
10405 int need_clear;
10406 int length = 0;
10407 int do_mode;
10408 int attr;
10409 int nwr_save;
10410#ifdef FEAT_INS_EXPAND
10411 int sub_attr;
10412#endif
10413
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010414 do_mode = ((p_smd && msg_silent == 0)
10415 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010416 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010417 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010418 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 {
10420 /*
10421 * Don't show mode right now, when not redrawing or inside a mapping.
10422 * Call char_avail() only when we are going to show something, because
10423 * it takes a bit of time.
10424 */
10425 if (!redrawing() || (char_avail() && !KeyTyped) || msg_silent != 0)
10426 {
10427 redraw_cmdline = TRUE; /* show mode later */
10428 return 0;
10429 }
10430
10431 nwr_save = need_wait_return;
10432
10433 /* wait a bit before overwriting an important message */
10434 check_for_delay(FALSE);
10435
10436 /* if the cmdline is more than one line high, erase top lines */
10437 need_clear = clear_cmdline;
10438 if (clear_cmdline && cmdline_row < Rows - 1)
10439 msg_clr_cmdline(); /* will reset clear_cmdline */
10440
10441 /* Position on the last line in the window, column 0 */
10442 msg_pos_mode();
10443 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010444 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 if (do_mode)
10446 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010447 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010449 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010450# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010451 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010452# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010453 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010454# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010455 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010456# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010457 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010459 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460# endif
10461#endif
10462#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10463 if (gui.in_use)
10464 {
10465 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010466 {
10467 /* HANGUL */
10468 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010469 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010470 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010471 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 }
10474#endif
10475#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010476 /* CTRL-X in Insert mode */
10477 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 {
10479 /* These messages can get long, avoid a wrap in a narrow
10480 * window. Prefer showing edit_submode_extra. */
10481 length = (Rows - msg_row) * Columns - 3;
10482 if (edit_submode_extra != NULL)
10483 length -= vim_strsize(edit_submode_extra);
10484 if (length > 0)
10485 {
10486 if (edit_submode_pre != NULL)
10487 length -= vim_strsize(edit_submode_pre);
10488 if (length - vim_strsize(edit_submode) > 0)
10489 {
10490 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010491 msg_puts_attr((char *)edit_submode_pre, attr);
10492 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 }
10494 if (edit_submode_extra != NULL)
10495 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010496 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010498 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 else
10500 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010501 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 }
10503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 }
10505 else
10506#endif
10507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010509 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010510 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010511 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 else if (State & INSERT)
10513 {
10514#ifdef FEAT_RIGHTLEFT
10515 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010516 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010518 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010520 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010521 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010523 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010525 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526#ifdef FEAT_RIGHTLEFT
10527 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010528 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529# ifdef FEAT_FKMAP
10530 if (p_fkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010531 msg_puts_attr(farsi_text_5, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532# endif
10533#endif
10534#ifdef FEAT_KEYMAP
10535 if (State & LANGMAP)
10536 {
10537# ifdef FEAT_ARABIC
10538 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010539 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 else
10541# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010542 if (get_keymap_str(curwin, (char_u *)" (%s)",
10543 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010544 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 }
10546#endif
10547 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010548 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 if (VIsual_active)
10551 {
10552 char *p;
10553
10554 /* Don't concatenate separate words to avoid translation
10555 * problems. */
10556 switch ((VIsual_select ? 4 : 0)
10557 + (VIsual_mode == Ctrl_V) * 2
10558 + (VIsual_mode == 'V'))
10559 {
10560 case 0: p = N_(" VISUAL"); break;
10561 case 1: p = N_(" VISUAL LINE"); break;
10562 case 2: p = N_(" VISUAL BLOCK"); break;
10563 case 4: p = N_(" SELECT"); break;
10564 case 5: p = N_(" SELECT LINE"); break;
10565 default: p = N_(" SELECT BLOCK"); break;
10566 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010567 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010569 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010571
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 need_clear = TRUE;
10573 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010574 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575#ifdef FEAT_INS_EXPAND
10576 && edit_submode == NULL /* otherwise it gets too long */
10577#endif
10578 )
10579 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010580 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 need_clear = TRUE;
10582 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010583
10584 mode_displayed = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 if (need_clear || clear_cmdline)
10586 msg_clr_eos();
10587 msg_didout = FALSE; /* overwrite this message */
10588 length = msg_col;
10589 msg_col = 0;
10590 need_wait_return = nwr_save; /* never ask for hit-return for this */
10591 }
10592 else if (clear_cmdline && msg_silent == 0)
10593 /* Clear the whole command line. Will reset "clear_cmdline". */
10594 msg_clr_cmdline();
10595
10596#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 /* In Visual mode the size of the selected area must be redrawn. */
10598 if (VIsual_active)
10599 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600
10601 /* If the last window has no status line, the ruler is after the mode
10602 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010603 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010604 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605#endif
10606 redraw_cmdline = FALSE;
10607 clear_cmdline = FALSE;
10608
10609 return length;
10610}
10611
10612/*
10613 * Position for a mode message.
10614 */
10615 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010616msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617{
10618 msg_col = 0;
10619 msg_row = Rows - 1;
10620}
10621
10622/*
10623 * Delete mode message. Used when ESC is typed which is expected to end
10624 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010625 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 */
10627 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010628unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629{
10630 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010631 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 */
10633 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10634 redraw_cmdline = TRUE; /* delete mode later */
10635 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010636 clearmode();
10637}
10638
10639/*
10640 * Clear the mode message.
10641 */
10642 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010643clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010644{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010645 int save_msg_row = msg_row;
10646 int save_msg_col = msg_col;
10647
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010648 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010649 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010650 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010651 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010652
10653 msg_col = save_msg_col;
10654 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655}
10656
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010657 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010658recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010659{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010660 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010661 if (!shortmess(SHM_RECORDING))
10662 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010663 char s[4];
10664
10665 sprintf(s, " @%c", reg_recording);
10666 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010667 }
10668}
10669
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010670/*
10671 * Draw the tab pages line at the top of the Vim window.
10672 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010673 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010674draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010675{
10676 int tabcount = 0;
10677 tabpage_T *tp;
10678 int tabwidth;
10679 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010680 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010681 int attr;
10682 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010683 win_T *cwp;
10684 int wincount;
10685 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010686 int c;
10687 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010688 int attr_sel = HL_ATTR(HLF_TPS);
10689 int attr_nosel = HL_ATTR(HLF_TP);
10690 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010691 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010692 int room;
10693 int use_sep_chars = (t_colors < 8
10694#ifdef FEAT_GUI
10695 && !gui.in_use
10696#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010697#ifdef FEAT_TERMGUICOLORS
10698 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010699#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010700 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010701
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010702 if (ScreenLines == NULL)
10703 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010704 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010705
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010706#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010707 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010708 if (gui_use_tabline())
10709 {
10710 gui_update_tabline();
10711 return;
10712 }
10713#endif
10714
10715 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010716 return;
10717
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010718#if defined(FEAT_STL_OPT)
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010719
10720 /* Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect. */
10721 for (scol = 0; scol < Columns; ++scol)
10722 TabPageIdxs[scol] = 0;
10723
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010724 /* Use the 'tabline' option if it's set. */
10725 if (*p_tal != NUL)
10726 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010727 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010728
10729 /* Check for an error. If there is one we would loop in redrawing the
10730 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010731 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010732 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010733 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010734 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010735 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010736 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010737 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010738 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010739#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010740 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010741 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010742 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010743
Bram Moolenaar238a5642006-02-21 22:12:05 +000010744 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10745 if (tabwidth < 6)
10746 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747
Bram Moolenaar238a5642006-02-21 22:12:05 +000010748 attr = attr_nosel;
10749 tabcount = 0;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010750 scol = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010751 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10752 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010753 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010754 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010755
Bram Moolenaar238a5642006-02-21 22:12:05 +000010756 if (tp->tp_topframe == topframe)
10757 attr = attr_sel;
10758 if (use_sep_chars && col > 0)
10759 screen_putchar('|', 0, col++, attr);
10760
10761 if (tp->tp_topframe != topframe)
10762 attr = attr_nosel;
10763
10764 screen_putchar(' ', 0, col++, attr);
10765
10766 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010767 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010768 cwp = curwin;
10769 wp = firstwin;
10770 }
10771 else
10772 {
10773 cwp = tp->tp_curwin;
10774 wp = tp->tp_firstwin;
10775 }
10776
10777 modified = FALSE;
10778 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10779 if (bufIsChanged(wp->w_buffer))
10780 modified = TRUE;
10781 if (modified || wincount > 1)
10782 {
10783 if (wincount > 1)
10784 {
10785 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010786 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010787 if (col + len >= Columns - 3)
10788 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010789 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010790#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010791 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010792#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010793 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010794#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010795 );
10796 col += len;
10797 }
10798 if (modified)
10799 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10800 screen_putchar(' ', 0, col++, attr);
10801 }
10802
10803 room = scol - col + tabwidth - 1;
10804 if (room > 0)
10805 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010806 /* Get buffer name in NameBuff[] */
10807 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010808 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 len = vim_strsize(NameBuff);
10810 p = NameBuff;
10811#ifdef FEAT_MBYTE
10812 if (has_mbyte)
10813 while (len > room)
10814 {
10815 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010816 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010817 }
10818 else
10819#endif
10820 if (len > room)
10821 {
10822 p += len - room;
10823 len = room;
10824 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010825 if (len > Columns - col - 1)
10826 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010827
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010828 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010829 col += len;
10830 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010831 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010832
10833 /* Store the tab page number in TabPageIdxs[], so that
10834 * jump_to_mouse() knows where each one is. */
10835 ++tabcount;
10836 while (scol < col)
10837 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010838 }
10839
Bram Moolenaar238a5642006-02-21 22:12:05 +000010840 if (use_sep_chars)
10841 c = '_';
10842 else
10843 c = ' ';
10844 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010845
10846 /* Put an "X" for closing the current tab if there are several. */
10847 if (first_tabpage->tp_next != NULL)
10848 {
10849 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10850 TabPageIdxs[Columns - 1] = -999;
10851 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010852 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010853
10854 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10855 * set. */
10856 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010857}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010858
10859/*
10860 * Get buffer name for "buf" into NameBuff[].
10861 * Takes care of special buffer names and translates special characters.
10862 */
10863 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010864get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010865{
10866 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010867 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010868 else
10869 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10870 trans_characters(NameBuff, MAXPATHL);
10871}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010872
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873/*
10874 * Get the character to use in a status line. Get its attributes in "*attr".
10875 */
10876 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010877fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878{
10879 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010880
10881#ifdef FEAT_TERMINAL
10882 if (bt_terminal(wp->w_buffer))
10883 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010884 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010885 {
10886 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010887 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010888 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010889 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010890 {
10891 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010892 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010893 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010894 }
10895 else
10896#endif
10897 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010899 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 fill = fill_stl;
10901 }
10902 else
10903 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010904 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 fill = fill_stlnc;
10906 }
10907 /* Use fill when there is highlighting, and highlighting of current
10908 * window differs, or the fillchars differ, or this is not the
10909 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010910 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010911 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 || (fill_stl != fill_stlnc)))
10913 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010914 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 return '^';
10916 return '=';
10917}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919/*
10920 * Get the character to use in a separator between vertically split windows.
10921 * Get its attributes in "*attr".
10922 */
10923 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010924fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010926 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 if (*attr == 0 && fill_vert == ' ')
10928 return '|';
10929 else
10930 return fill_vert;
10931}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932
10933/*
10934 * Return TRUE if redrawing should currently be done.
10935 */
10936 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010937redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010939#ifdef FEAT_EVAL
10940 if (disable_redraw_for_testing)
10941 return 0;
10942 else
10943#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010944 return ((!RedrawingDisabled
10945#ifdef FEAT_EVAL
10946 || ignore_redraw_flag_for_testing
10947#endif
10948 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949}
10950
10951/*
10952 * Return TRUE if printing messages should currently be done.
10953 */
10954 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010955messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956{
10957 return (!(p_lz && char_avail() && !KeyTyped));
10958}
10959
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010960#ifdef FEAT_MENU
10961/*
10962 * Draw the window toolbar.
10963 */
10964 static void
10965redraw_win_toolbar(win_T *wp)
10966{
10967 vimmenu_T *menu;
10968 int item_idx = 0;
10969 int item_count = 0;
10970 int col = 0;
10971 int next_col;
10972 int off = (int)(current_ScreenLine - ScreenLines);
10973 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10974 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10975
10976 vim_free(wp->w_winbar_items);
10977 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10978 ++item_count;
10979 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
10980 (unsigned)sizeof(winbar_item_T) * (item_count + 1));
10981
10982 /* TODO: use fewer spaces if there is not enough room */
10983 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010984 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010985 {
10986 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010987 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010988 break;
10989 if (col > 1)
10990 {
10991 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010992 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010993 break;
10994 }
10995
10996 wp->w_winbar_items[item_idx].wb_startcol = col;
10997 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010998 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010999 break;
11000
11001 next_col = text_to_screenline(wp, menu->name, col);
11002 while (col < next_col)
11003 {
11004 ScreenAttrs[off + col] = button_attr;
11005 ++col;
11006 }
11007 wp->w_winbar_items[item_idx].wb_endcol = col;
11008 wp->w_winbar_items[item_idx].wb_menu = menu;
11009 ++item_idx;
11010
Bram Moolenaar02631462017-09-22 15:20:32 +020011011 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011012 break;
11013 space_to_screenline(off + col, button_attr);
11014 ++col;
11015 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011016 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011017 {
11018 space_to_screenline(off + col, fill_attr);
11019 ++col;
11020 }
11021 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11022
Bram Moolenaar02631462017-09-22 15:20:32 +020011023 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
11024 (int)wp->w_width, FALSE);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011025}
11026#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011027
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028/*
11029 * Show current status info in ruler and various other places
11030 * If always is FALSE, only show ruler if position has changed.
11031 */
11032 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011033showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
11035 if (!always && !redrawing())
11036 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011037#ifdef FEAT_INS_EXPAND
11038 if (pum_visible())
11039 {
11040 /* Don't redraw right now, do it later. */
11041 curwin->w_redr_status = TRUE;
11042 return;
11043 }
11044#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011045#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011046 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar238a5642006-02-21 22:12:05 +000011047 {
Bram Moolenaar362f3562009-11-03 16:20:34 +000011048 redraw_custom_statusline(curwin);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 else
11051#endif
11052#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011053 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054#endif
11055
11056#ifdef FEAT_TITLE
11057 if (need_maketitle
11058# ifdef FEAT_STL_OPT
11059 || (p_icon && (stl_syntax & STL_IN_ICON))
11060 || (p_title && (stl_syntax & STL_IN_TITLE))
11061# endif
11062 )
11063 maketitle();
11064#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011065 /* Redraw the tab pages line if needed. */
11066 if (redraw_tabline)
11067 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068}
11069
11070#ifdef FEAT_CMDL_INFO
11071 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011072win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011074#define RULER_BUF_LEN 70
11075 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 int row;
11077 int fillchar;
11078 int attr;
11079 int empty_line = FALSE;
11080 colnr_T virtcol;
11081 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011082 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084 int this_ru_col;
11085 int off = 0;
11086 int width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087
11088 /* If 'ruler' off or redrawing disabled, don't do anything */
11089 if (!p_ru)
11090 return;
11091
11092 /*
11093 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11094 * after deleting lines, before cursor.lnum is corrected.
11095 */
11096 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11097 return;
11098
11099#ifdef FEAT_INS_EXPAND
11100 /* Don't draw the ruler while doing insert-completion, it might overwrite
11101 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 if (edit_submode != NULL)
11104 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011105 // Don't draw the ruler when the popup menu is visible, it may overlap.
11106 // Except when the popup menu will be redrawn anyway.
11107 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011108 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109#endif
11110
11111#ifdef FEAT_STL_OPT
11112 if (*p_ruf)
11113 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011114 int save_called_emsg = called_emsg;
11115
11116 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011118 if (called_emsg)
11119 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011120 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011121 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 return;
11123 }
11124#endif
11125
11126 /*
11127 * Check if not in Insert mode and the line is empty (will show "0-1").
11128 */
11129 if (!(State & INSERT)
11130 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11131 empty_line = TRUE;
11132
11133 /*
11134 * Only draw the ruler when something changed.
11135 */
11136 validate_virtcol_win(wp);
11137 if ( redraw_cmdline
11138 || always
11139 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11140 || wp->w_cursor.col != wp->w_ru_cursor.col
11141 || wp->w_virtcol != wp->w_ru_virtcol
11142#ifdef FEAT_VIRTUALEDIT
11143 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
11144#endif
11145 || wp->w_topline != wp->w_ru_topline
11146 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11147#ifdef FEAT_DIFF
11148 || wp->w_topfill != wp->w_ru_topfill
11149#endif
11150 || empty_line != wp->w_ru_empty)
11151 {
11152 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 if (wp->w_status_height)
11154 {
11155 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011156 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011157 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011158 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 }
11160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 {
11162 row = Rows - 1;
11163 fillchar = ' ';
11164 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 width = Columns;
11166 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167 }
11168
11169 /* In list mode virtcol needs to be recomputed */
11170 virtcol = wp->w_virtcol;
11171 if (wp->w_p_list && lcs_tab1 == NUL)
11172 {
11173 wp->w_p_list = FALSE;
11174 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11175 wp->w_p_list = TRUE;
11176 }
11177
11178 /*
11179 * Some sprintfs return the length, some return a pointer.
11180 * To avoid portability problems we use strlen() here.
11181 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011182 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11184 ? 0L
11185 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011186 len = STRLEN(buffer);
11187 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11189 (int)virtcol + 1);
11190
11191 /*
11192 * Add a "50%" if there is room for it.
11193 * On the last line, don't print in the last column (scrolls the
11194 * screen up on some terminals).
11195 */
11196 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011197 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 this_ru_col = ru_col - (Columns - width);
11202 if (this_ru_col < 0)
11203 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 /* Never use more than half the window/screen width, leave the other
11205 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011206 if (this_ru_col < (width + 1) / 2)
11207 this_ru_col = (width + 1) / 2;
11208 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011210 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011211 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 {
11213#ifdef FEAT_MBYTE
11214 if (has_mbyte)
11215 i += (*mb_char2bytes)(fillchar, buffer + i);
11216 else
11217#endif
11218 buffer[i++] = fillchar;
11219 ++o;
11220 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011221 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 }
11223 /* Truncate at window boundary. */
11224#ifdef FEAT_MBYTE
11225 if (has_mbyte)
11226 {
11227 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011228 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 {
11230 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011231 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 {
11233 buffer[i] = NUL;
11234 break;
11235 }
11236 }
11237 }
11238 else
11239#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011240 if (this_ru_col + (int)STRLEN(buffer) > width)
11241 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242
Bram Moolenaar4033c552017-09-16 20:54:51 +020011243 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 i = redraw_cmdline;
11245 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011246 this_ru_col + off + (int)STRLEN(buffer),
11247 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 fillchar, fillchar, attr);
11249 /* don't redraw the cmdline because of showing the ruler */
11250 redraw_cmdline = i;
11251 wp->w_ru_cursor = wp->w_cursor;
11252 wp->w_ru_virtcol = wp->w_virtcol;
11253 wp->w_ru_empty = empty_line;
11254 wp->w_ru_topline = wp->w_topline;
11255 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11256#ifdef FEAT_DIFF
11257 wp->w_ru_topfill = wp->w_topfill;
11258#endif
11259 }
11260}
11261#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011262
11263#if defined(FEAT_LINEBREAK) || defined(PROTO)
11264/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011265 * Return the width of the 'number' and 'relativenumber' column.
11266 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011267 * Otherwise it depends on 'numberwidth' and the line count.
11268 */
11269 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011270number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011271{
11272 int n;
11273 linenr_T lnum;
11274
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011275 if (wp->w_p_rnu && !wp->w_p_nu)
11276 /* cursor line shows "0" */
11277 lnum = wp->w_height;
11278 else
11279 /* cursor line shows absolute line number */
11280 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011281
Bram Moolenaar6b314672015-03-20 15:42:10 +010011282 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011283 return wp->w_nrwidth_width;
11284 wp->w_nrwidth_line_count = lnum;
11285
11286 n = 0;
11287 do
11288 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011289 lnum /= 10;
11290 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011291 } while (lnum > 0);
11292
11293 /* 'numberwidth' gives the minimal width plus one */
11294 if (n < wp->w_p_nuw - 1)
11295 n = wp->w_p_nuw - 1;
11296
11297 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011298 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011299 return n;
11300}
11301#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011302
Bram Moolenaar113e1072019-01-20 15:30:40 +010011303#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011304/*
11305 * Return the current cursor column. This is the actual position on the
11306 * screen. First column is 0.
11307 */
11308 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011309screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011310{
11311 return screen_cur_col;
11312}
11313
11314/*
11315 * Return the current cursor row. This is the actual position on the screen.
11316 * First row is 0.
11317 */
11318 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011319screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011320{
11321 return screen_cur_row;
11322}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011323#endif