blob: 387c398650a6217ea6e2efb1bb163956c9889cf9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * screen.c: code for displaying on the screen
12 *
13 * Output to the screen (console, terminal emulator or GUI window) is minimized
14 * by remembering what is already on the screen, and only updating the parts
15 * that changed.
16 *
17 * ScreenLines[off] Contains a copy of the whole screen, as it is currently
18 * displayed (excluding text written by external commands).
19 * ScreenAttrs[off] Contains the associated attributes.
20 * LineOffset[row] Contains the offset into ScreenLines*[] and ScreenAttrs[]
21 * for each line.
22 * LineWraps[row] Flag for each line whether it wraps to the next line.
23 *
24 * For double-byte characters, two consecutive bytes in ScreenLines[] can form
25 * one character which occupies two display cells.
26 * For UTF-8 a multi-byte character is converted to Unicode and stored in
27 * ScreenLinesUC[]. ScreenLines[] contains the first byte only. For an ASCII
Bram Moolenaar70c49c12010-03-23 15:36:35 +010028 * character without composing chars ScreenLinesUC[] will be 0 and
29 * ScreenLinesC[][] is not used. When the character occupies two display
30 * cells the next byte in ScreenLines[] is 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +000031 * ScreenLinesC[][] contain up to 'maxcombine' composing characters
Bram Moolenaar70c49c12010-03-23 15:36:35 +010032 * (drawn on top of the first character). There is 0 after the last one used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * ScreenLines2[] is only used for euc-jp to store the second byte if the
34 * first byte is 0x8e (single-width character).
35 *
36 * The screen_*() functions write to the screen and handle updating
37 * ScreenLines[].
38 *
39 * update_screen() is the function that updates all windows and status lines.
40 * It is called form the main loop when must_redraw is non-zero. It may be
Bram Moolenaar2c7a7632007-05-10 18:19:11 +000041 * called from other places when an immediate screen update is needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 *
43 * The part of the buffer that is displayed in a window is set with:
44 * - w_topline (first buffer line in window)
Bram Moolenaarea389e92014-05-28 21:40:52 +020045 * - w_topfill (filler lines above the first line)
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 * - w_leftcol (leftmost window cell in window),
47 * - w_skipcol (skipped window cells of first line)
48 *
49 * Commands that only move the cursor around in a window, do not need to take
50 * action to update the display. The main loop will check if w_topline is
51 * valid and update it (scroll the window) when needed.
52 *
53 * Commands that scroll a window change w_topline and must call
54 * check_cursor() to move the cursor into the visible part of the window, and
55 * call redraw_later(VALID) to have the window displayed by update_screen()
56 * later.
57 *
58 * Commands that change text in the buffer must call changed_bytes() or
59 * changed_lines() to mark the area that changed and will require updating
60 * later. The main loop will call update_screen(), which will update each
61 * window that shows the changed buffer. This assumes text above the change
62 * can remain displayed as it is. Text after the change may need updating for
63 * scrolling, folding and syntax highlighting.
64 *
65 * Commands that change how a window is displayed (e.g., setting 'list') or
66 * invalidate the contents of a window in another way (e.g., change fold
67 * settings), must call redraw_later(NOT_VALID) to have the whole window
68 * redisplayed by update_screen() later.
69 *
70 * Commands that change how a buffer is displayed (e.g., setting 'tabstop')
71 * must call redraw_curbuf_later(NOT_VALID) to have all the windows for the
72 * buffer redisplayed by update_screen() later.
73 *
Bram Moolenaar600dddc2006-03-12 22:05:10 +000074 * Commands that change highlighting and possibly cause a scroll too must call
75 * redraw_later(SOME_VALID) to update the whole window but still use scrolling
76 * to avoid redrawing everything. But the length of displayed lines must not
77 * change, use NOT_VALID then.
78 *
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 * Commands that move the window position must call redraw_later(NOT_VALID).
80 * TODO: should minimize redrawing by scrolling when possible.
81 *
82 * Commands that change everything (e.g., resizing the screen) must call
83 * redraw_all_later(NOT_VALID) or redraw_all_later(CLEAR).
84 *
85 * Things that are handled indirectly:
86 * - When messages scroll the screen up, msg_scrolled will be set and
87 * update_screen() called to redraw.
88 */
89
90#include "vim.h"
91
Bram Moolenaar5641f382012-06-13 18:06:36 +020092#define MB_FILLER_CHAR '<' /* character used when a double-width character
93 * doesn't fit. */
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * The attributes that are actually active for writing to the screen.
97 */
98static int screen_attr = 0;
99
100/*
101 * Positioning the cursor is reduced by remembering the last position.
102 * Mostly used by windgoto() and screen_char().
103 */
104static int screen_cur_row, screen_cur_col; /* last known cursor position */
105
106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static match_T search_hl; /* used for 'hlsearch' highlight matching */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109
110#ifdef FEAT_FOLDING
111static foldinfo_T win_foldinfo; /* info for 'foldcolumn' */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100112static int compute_foldcolumn(win_T *wp, int col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#endif
114
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200115/* Flag that is set when drawing for a callback, not from the main command
116 * loop. */
117static int redrawing_for_callback = 0;
118
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119/*
120 * Buffer for one screen line (characters and attributes).
121 */
122static schar_T *current_ScreenLine;
123
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200124#ifdef FEAT_TEXT_PROP
125static void update_popups(void);
126#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100127static void win_update(win_T *wp);
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200128static void win_redr_status(win_T *wp, int ignore_pum);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100129static void win_draw_end(win_T *wp, int c1, int c2, int draw_margin, int row, int endrow, hlf_T hl);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifdef FEAT_FOLDING
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100131static void fold_line(win_T *wp, long fold_count, foldinfo_T *foldinfo, linenr_T lnum, int row);
132static void fill_foldcolumn(char_u *p, win_T *wp, int closed, linenr_T lnum);
133static void copy_text_attr(int off, char_u *buf, int len, int attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +0200135static int win_line(win_T *, linenr_T, int, int, int nochange, int number_only);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100136static void draw_vsep_win(win_T *wp, int row);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000137#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100138static void redraw_custom_statusline(win_T *wp);
Bram Moolenaar238a5642006-02-21 22:12:05 +0000139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarde993ea2014-06-17 23:18:01 +0200141# define SEARCH_HL_PRIORITY 0
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100142static void start_search_hl(void);
143static void end_search_hl(void);
144static void init_search_hl(win_T *wp);
145static void prepare_search_hl(win_T *wp, linenr_T lnum);
146static void next_search_hl(win_T *win, match_T *shl, linenr_T lnum, colnr_T mincol, matchitem_T *cur);
147static int next_search_hl_pos(match_T *shl, linenr_T lnum, posmatch_T *pos, colnr_T mincol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100149static void screen_char(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100150static void screen_char_2(unsigned off, int row, int col);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100151static void screenclear2(void);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200152static void lineclear(unsigned off, int width, int attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100153static void lineinvalid(unsigned off, int width);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200154static int win_do_lines(win_T *wp, int row, int line_count, int mayclear, int del, int clear_attr);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100155static void win_rest_invalid(win_T *wp);
156static void msg_pos_mode(void);
157static void recording_mode(int attr);
Bram Moolenaar3633cf52017-07-31 22:29:35 +0200158static int fillchar_status(int *attr, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100159static int fillchar_vsep(int *attr);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200160#ifdef FEAT_MENU
161static void redraw_win_toolbar(win_T *wp);
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#ifdef FEAT_STL_OPT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100164static void win_redr_custom(win_T *wp, int draw_ruler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165#endif
166#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +0200167static void win_redr_ruler(win_T *wp, int always, int ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170/* Ugly global: overrule attribute used by screen_char() */
171static int screen_char_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
Bram Moolenaar06f1ed22017-06-18 22:41:03 +0200173#if defined(FEAT_SYN_HL) && defined(FEAT_RELTIME)
174/* Can limit syntax highlight time to 'redrawtime'. */
175# define SYN_TIME_LIMIT 1
176#endif
177
Bram Moolenaarc0aa4822017-07-16 14:04:29 +0200178#ifdef FEAT_RIGHTLEFT
179# define HAS_RIGHTLEFT(x) x
180#else
181# define HAS_RIGHTLEFT(x) FALSE
182#endif
183
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200184// flags for screen_line()
185#define SLF_RIGHTLEFT 1
186#define SLF_POPUP 2
187
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188/*
189 * Redraw the current window later, with update_screen(type).
190 * Set must_redraw only if not already set to a higher value.
Bram Moolenaarae654382019-01-17 21:09:05 +0100191 * E.g. if must_redraw is CLEAR, type NOT_VALID will do nothing.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100194redraw_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
196 redraw_win_later(curwin, type);
197}
198
199 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100200redraw_win_later(
201 win_T *wp,
202 int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203{
Bram Moolenaar4f198282017-10-23 21:53:30 +0200204 if (!exiting && wp->w_redr_type < type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 {
206 wp->w_redr_type = type;
207 if (type >= NOT_VALID)
208 wp->w_lines_valid = 0;
209 if (must_redraw < type) /* must_redraw is the maximum of all windows */
210 must_redraw = type;
211 }
212}
213
214/*
215 * Force a complete redraw later. Also resets the highlighting. To be used
216 * after executing a shell command that messes up the screen.
217 */
218 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100219redraw_later_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
221 redraw_all_later(CLEAR);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000222#ifdef FEAT_GUI
223 if (gui.in_use)
224 /* Use a code that will reset gui.highlight_mask in
225 * gui_stop_highlight(). */
226 screen_attr = HL_ALL + 1;
227 else
228#endif
229 /* Use attributes that is very unlikely to appear in text. */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +0200230 screen_attr = HL_BOLD | HL_UNDERLINE | HL_INVERSE | HL_STRIKETHROUGH;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233/*
234 * Mark all windows to be redrawn later.
235 */
236 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100237redraw_all_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 win_T *wp;
240
241 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 redraw_win_later(wp, type);
Bram Moolenaar04b4e1a2019-01-06 22:22:07 +0100243 // This may be needed when switching tabs.
244 if (must_redraw < type)
245 must_redraw = type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248/*
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000249 * Mark all windows that are editing the current buffer to be updated later.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100252redraw_curbuf_later(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 redraw_buf_later(curbuf, type);
255}
256
257 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100258redraw_buf_later(buf_T *buf, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259{
260 win_T *wp;
261
262 FOR_ALL_WINDOWS(wp)
263 {
264 if (wp->w_buffer == buf)
265 redraw_win_later(wp, type);
266 }
267}
268
Bram Moolenaar113e1072019-01-20 15:30:40 +0100269#if defined(FEAT_SIGNS) || defined(PROTO)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200270 void
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100271redraw_buf_line_later(buf_T *buf, linenr_T lnum)
272{
273 win_T *wp;
274
275 FOR_ALL_WINDOWS(wp)
276 if (wp->w_buffer == buf && lnum >= wp->w_topline
277 && lnum < wp->w_botline)
278 redrawWinline(wp, lnum);
279}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100280#endif
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100281
Bram Moolenaar113e1072019-01-20 15:30:40 +0100282#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100283 void
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200284redraw_buf_and_status_later(buf_T *buf, int type)
285{
286 win_T *wp;
287
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200288#ifdef FEAT_WILDMENU
Bram Moolenaar86033562017-07-12 20:24:41 +0200289 if (wild_menu_showing != 0)
290 /* Don't redraw while the command line completion is displayed, it
291 * would disappear. */
292 return;
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200293#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200294 FOR_ALL_WINDOWS(wp)
295 {
296 if (wp->w_buffer == buf)
297 {
298 redraw_win_later(wp, type);
299 wp->w_redr_status = TRUE;
300 }
301 }
302}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100303#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200304
Bram Moolenaar113e1072019-01-20 15:30:40 +0100305#if defined(FEAT_TERMRESPONSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306/*
Bram Moolenaar2951b772013-07-03 12:45:31 +0200307 * Redraw as soon as possible. When the command line is not scrolled redraw
308 * right away and restore what was on the command line.
309 * Return a code indicating what happened.
310 */
311 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100312redraw_asap(int type)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200313{
314 int rows;
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200315 int cols = screen_Columns;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200316 int r;
317 int ret = 0;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200318 schar_T *screenline; /* copy from ScreenLines[] */
319 sattr_T *screenattr; /* copy from ScreenAttrs[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200320 int i;
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200321 u8char_T *screenlineUC = NULL; /* copy from ScreenLinesUC[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200322 u8char_T *screenlineC[MAX_MCO]; /* copy from ScreenLinesC[][] */
Bram Moolenaare1fc4e22013-07-03 13:29:58 +0200323 schar_T *screenline2 = NULL; /* copy from ScreenLines2[] */
Bram Moolenaar2951b772013-07-03 12:45:31 +0200324
325 redraw_later(type);
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200326 if (msg_scrolled || (State != NORMAL && State != NORMAL_BUSY) || exiting)
Bram Moolenaar2951b772013-07-03 12:45:31 +0200327 return ret;
328
329 /* Allocate space to save the text displayed in the command line area. */
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200330 rows = screen_Rows - cmdline_row;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200331 screenline = LALLOC_MULT(schar_T, rows * cols);
332 screenattr = LALLOC_MULT(sattr_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200333 if (screenline == NULL || screenattr == NULL)
334 ret = 2;
Bram Moolenaar2951b772013-07-03 12:45:31 +0200335 if (enc_utf8)
336 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200337 screenlineUC = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200338 if (screenlineUC == NULL)
339 ret = 2;
340 for (i = 0; i < p_mco; ++i)
341 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200342 screenlineC[i] = LALLOC_MULT(u8char_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200343 if (screenlineC[i] == NULL)
344 ret = 2;
345 }
346 }
347 if (enc_dbcs == DBCS_JPNU)
348 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200349 screenline2 = LALLOC_MULT(schar_T, rows * cols);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200350 if (screenline2 == NULL)
351 ret = 2;
352 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200353
354 if (ret != 2)
355 {
356 /* Save the text displayed in the command line area. */
357 for (r = 0; r < rows; ++r)
358 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200359 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200360 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 (size_t)cols * sizeof(schar_T));
362 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200363 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200364 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 if (enc_utf8)
366 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200367 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200368 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 mch_memmove(screenlineC[i] + r * cols,
372 ScreenLinesC[i] + LineOffset[cmdline_row + r],
373 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200374 }
375 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200376 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200377 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 }
380
381 update_screen(0);
382 ret = 3;
383
384 if (must_redraw == 0)
385 {
386 int off = (int)(current_ScreenLine - ScreenLines);
387
388 /* Restore the text displayed in the command line area. */
389 for (r = 0; r < rows; ++r)
390 {
391 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200392 screenline + r * cols,
393 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200394 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200395 screenattr + r * cols,
396 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200397 if (enc_utf8)
398 {
399 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200400 screenlineUC + r * cols,
401 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200402 for (i = 0; i < p_mco; ++i)
403 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200404 screenlineC[i] + r * cols,
405 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200406 }
407 if (enc_dbcs == DBCS_JPNU)
408 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200409 screenline2 + r * cols,
410 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200411 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200412 }
413 ret = 4;
414 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200415 }
416
417 vim_free(screenline);
418 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200419 if (enc_utf8)
420 {
421 vim_free(screenlineUC);
422 for (i = 0; i < p_mco; ++i)
423 vim_free(screenlineC[i]);
424 }
425 if (enc_dbcs == DBCS_JPNU)
426 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200427
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200428 /* Show the intro message when appropriate. */
429 maybe_intro_message();
430
431 setcursor();
432
Bram Moolenaar2951b772013-07-03 12:45:31 +0200433 return ret;
434}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100435#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200436
437/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100438 * Invoked after an asynchronous callback is called.
439 * If an echo command was used the cursor needs to be put back where
440 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200441 * If "call_update_screen" is FALSE don't call update_screen() when at the
442 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100443 */
444 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200445redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100446{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200447 ++redrawing_for_callback;
448
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100449 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200450 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200452 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200453 // Don't redraw when in prompt_for_number().
454 if (cmdline_row > 0)
455 {
456 // Redrawing only works when the screen didn't scroll. Don't clear
457 // wildmenu entries.
458 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200459#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200460 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && call_update_screen)
463 update_screen(0);
464
465 // Redraw in the same position, so that the user can continue
466 // editing the command.
467 redrawcmdline_ex(FALSE);
468 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200469 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200470 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100471 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200472 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200473 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100474 setcursor();
475 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100476 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100477#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100478 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200479 // Don't update the cursor when it is blinking and off to avoid
480 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100481 out_flush_cursor(FALSE, FALSE);
482 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100483#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100484 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200485
486 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100487}
488
489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * Changed something in the current window, at buffer line "lnum", that
491 * requires that line and possibly other lines to be redrawn.
492 * Used when entering/leaving Insert mode with the cursor on a folded line.
493 * Used to remove the "$" from a change command.
494 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
495 * may become invalid and the whole window will have to be redrawn.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100498redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200499 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100500 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200502 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
503 wp->w_redraw_top = lnum;
504 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
505 wp->w_redraw_bot = lnum;
506 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507}
508
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200509/*
510 * To be called when "updating_screen" was set before and now the postponed
511 * side effects may take place.
512 */
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200513 void
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200514after_updating_screen(int may_resize_shell UNUSED)
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200515{
516 updating_screen = FALSE;
517#ifdef FEAT_GUI
518 if (may_resize_shell)
519 gui_may_resize_shell();
520#endif
521#ifdef FEAT_TERMINAL
522 term_check_channel_closed_recently();
523#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200524
525#ifdef HAVE_DROP_FILE
526 // If handle_drop() was called while updating_screen was TRUE need to
527 // handle the drop now.
528 handle_any_postponed_drop();
529#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200530}
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200533 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 */
535 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100536update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 redraw_curbuf_later(type);
539 update_screen(type);
540}
541
542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 * Based on the current value of curwin->w_topline, transfer a screenfull
544 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200547 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200550 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 win_T *wp;
552 static int did_intro = FALSE;
553#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
554 int did_one;
555#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200557 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200558 int gui_cursor_col = 0;
559 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200560#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200561 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000563 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200565 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200567 if (type == VALID_NO_UPDATE)
568 {
569 no_update = TRUE;
570 type = 0;
571 }
572
Bram Moolenaara3347722019-05-11 21:14:24 +0200573#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200574 {
575 buf_T *buf;
576
577 // Before updating the screen, notify any listeners of changed text.
578 FOR_ALL_BUFFERS(buf)
579 invoke_listeners(buf);
580 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (must_redraw)
584 {
585 if (type < must_redraw) /* use maximal type */
586 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000587
588 /* must_redraw is reset here, so that when we run into some weird
589 * reason to redraw while busy redrawing (e.g., asynchronous
590 * scrolling), or update_topline() in win_update() will cause a
591 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 must_redraw = 0;
593 }
594
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200595 /* May need to update w_lines[]. */
596 if (curwin->w_lines_valid == 0 && type < NOT_VALID
597#ifdef FEAT_TERMINAL
598 && !term_do_update_window(curwin)
599#endif
600 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 type = NOT_VALID;
602
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000603 /* Postpone the redrawing when it's not needed and when being called
604 * recursively. */
605 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 redraw_later(type); /* remember type for next time */
608 must_redraw = type;
609 if (type > INVERTED_ALL)
610 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200613#ifdef FEAT_TEXT_PROP
614 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200615 if (popup_any_visible())
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200616 type = NOT_VALID;
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
619 updating_screen = TRUE;
620#ifdef FEAT_SYN_HL
621 ++display_tick; /* let syntax code know we're in a next round of
622 * display updating */
623#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200624 if (no_update)
625 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * if the screen was scrolled up when displaying a message, scroll it down
629 */
630 if (msg_scrolled)
631 {
632 clear_cmdline = TRUE;
633 if (msg_scrolled > Rows - 5) /* clearing is faster */
634 type = CLEAR;
635 else if (type != CLEAR)
636 {
637 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200638 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
639 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 type = CLEAR;
641 FOR_ALL_WINDOWS(wp)
642 {
643 if (W_WINROW(wp) < msg_scrolled)
644 {
645 if (W_WINROW(wp) + wp->w_height > msg_scrolled
646 && wp->w_redr_type < REDRAW_TOP
647 && wp->w_lines_valid > 0
648 && wp->w_topline == wp->w_lines[0].wl_lnum)
649 {
650 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
651 wp->w_redr_type = REDRAW_TOP;
652 }
653 else
654 {
655 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200656 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
657 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
660 }
661 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200662 if (!no_update)
663 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000664 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 msg_scrolled = 0;
667 need_wait_return = FALSE;
668 }
669
670 /* reset cmdline_row now (may have been changed temporarily) */
671 compute_cmdrow();
672
673 /* Check for changed highlighting */
674 if (need_highlight_changed)
675 highlight_changed();
676
677 if (type == CLEAR) /* first clear screen */
678 {
679 screenclear(); /* will reset clear_cmdline */
680 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200681 /* must_redraw may be set indirectly, avoid another redraw later */
682 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684
685 if (clear_cmdline) /* going to clear cmdline (done below) */
686 check_for_delay(FALSE);
687
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000688#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200689 /* Force redraw when width of 'number' or 'relativenumber' column
690 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000691 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200692 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
693 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000694 curwin->w_redr_type = NOT_VALID;
695#endif
696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 /*
698 * Only start redrawing if there is really something to do.
699 */
700 if (type == INVERTED)
701 update_curswant();
702 if (curwin->w_redr_type < type
703 && !((type == VALID
704 && curwin->w_lines[0].wl_valid
705#ifdef FEAT_DIFF
706 && curwin->w_topfill == curwin->w_old_topfill
707 && curwin->w_botfill == curwin->w_old_botfill
708#endif
709 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000711 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
713 && curwin->w_old_visual_mode == VIsual_mode
714 && (curwin->w_valid & VALID_VIRTCOL)
715 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 ))
717 curwin->w_redr_type = type;
718
Bram Moolenaar5a305422006-04-28 22:38:25 +0000719 /* Redraw the tab pages line if needed. */
720 if (redraw_tabline || type >= NOT_VALID)
721 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#ifdef FEAT_SYN_HL
724 /*
725 * Correct stored syntax highlighting info for changes in each displayed
726 * buffer. Each buffer must only be done once.
727 */
728 FOR_ALL_WINDOWS(wp)
729 {
730 if (wp->w_buffer->b_mod_set)
731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 win_T *wwp;
733
734 /* Check if we already did this buffer. */
735 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
736 if (wwp->w_buffer == wp->w_buffer)
737 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200738 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 syn_stack_apply_changes(wp->w_buffer);
740 }
741 }
742#endif
743
744 /*
745 * Go from top to bottom through the windows, redrawing the ones that need
746 * it.
747 */
748#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
749 did_one = FALSE;
750#endif
751#ifdef FEAT_SEARCH_EXTRA
752 search_hl.rm.regprog = NULL;
753#endif
754 FOR_ALL_WINDOWS(wp)
755 {
756 if (wp->w_redr_type != 0)
757 {
758 cursor_off();
759#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
760 if (!did_one)
761 {
762 did_one = TRUE;
763# ifdef FEAT_SEARCH_EXTRA
764 start_search_hl();
765# endif
766# ifdef FEAT_CLIPBOARD
767 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200768 if (clip_star.available && clip_isautosel_star())
769 clip_update_selection(&clip_star);
770 if (clip_plus.available && clip_isautosel_plus())
771 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772# endif
773#ifdef FEAT_GUI
774 /* Remove the cursor before starting to do anything, because
775 * scrolling may make it difficult to redraw the text under
776 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200777 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200778 {
779 gui_cursor_col = gui.cursor_col;
780 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200782 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#endif
785 }
786#endif
787 win_update(wp);
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* redraw status line after the window to minimize cursor movement */
791 if (wp->w_redr_status)
792 {
793 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200794 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797#if defined(FEAT_SEARCH_EXTRA)
798 end_search_hl();
799#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100800#ifdef FEAT_INS_EXPAND
801 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200802 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /* Reset b_mod_set flags. Going through all windows is probably faster
806 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200807 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200810 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 /* Clear or redraw the command line. Done last, because scrolling may
813 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200814 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 showmode();
816
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200817 if (no_update)
818 --no_win_do_lines_ins;
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200821 if (!did_intro)
822 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 did_intro = TRUE;
824
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200825#ifdef FEAT_TEXT_PROP
826 // Display popup windows on top of the others.
827 update_popups();
828#endif
829
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_GUI
831 /* Redraw the cursor and update the scrollbars when all screen updating is
832 * done. */
833 if (gui.in_use)
834 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200835 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200836 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100837 mch_disable_flush();
838 out_flush(); /* required before updating the cursor */
839 mch_enable_flush();
840
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 /* Put the GUI position where the cursor was, gui_update_cursor()
842 * uses that. */
843 gui.col = gui_cursor_col;
844 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200845 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100847 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200848 screen_cur_col = gui.col;
849 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200850 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100851 else
852 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 gui_update_scrollbars(FALSE);
854 }
855#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200856 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857}
858
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100859#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100860/*
861 * Prepare for updating one or more windows.
862 * Caller must check for "updating_screen" already set to avoid recursiveness.
863 */
864 static void
865update_prepare(void)
866{
867 cursor_off();
868 updating_screen = TRUE;
869#ifdef FEAT_GUI
870 /* Remove the cursor before starting to do anything, because scrolling may
871 * make it difficult to redraw the text under it. */
872 if (gui.in_use)
873 gui_undraw_cursor();
874#endif
875#ifdef FEAT_SEARCH_EXTRA
876 start_search_hl();
877#endif
878}
879
880/*
881 * Finish updating one or more windows.
882 */
883 static void
884update_finish(void)
885{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200886 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100887 showmode();
888
889# ifdef FEAT_SEARCH_EXTRA
890 end_search_hl();
891# endif
892
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200893 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894
895# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100896 /* Redraw the cursor and update the scrollbars when all screen updating is
897 * done. */
898 if (gui.in_use)
899 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100900 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100901 gui_update_scrollbars(FALSE);
902 }
903# endif
904}
905#endif
906
Bram Moolenaar860cae12010-06-05 23:22:07 +0200907#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200908/*
909 * Return TRUE if the cursor line in window "wp" may be concealed, according
910 * to the 'concealcursor' option.
911 */
912 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100913conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200914{
915 int c;
916
917 if (*wp->w_p_cocu == NUL)
918 return FALSE;
919 if (get_real_state() & VISUAL)
920 c = 'v';
921 else if (State & INSERT)
922 c = 'i';
923 else if (State & NORMAL)
924 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200925 else if (State & CMDLINE)
926 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200927 else
928 return FALSE;
929 return vim_strchr(wp->w_p_cocu, c) != NULL;
930}
931
932/*
933 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
934 */
935 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200936conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200937{
938 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
939 {
940 need_cursor_line_redraw = TRUE;
941 /* Need to recompute cursor column, e.g., when starting Visual mode
942 * without concealing. */
943 curs_columns(TRUE);
944 }
945}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#endif
947
Bram Moolenaar113e1072019-01-20 15:30:40 +0100948#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100950update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951{
952 win_T *wp;
953 int doit = FALSE;
954
955# ifdef FEAT_FOLDING
956 win_foldinfo.fi_level = 0;
957# endif
958
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100959 // update/delete a specific sign
960 redraw_buf_line_later(buf, lnum);
961
962 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (wp->w_redr_type != 0)
965 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100967 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200968 * happening (recursive call), messages on the screen or still starting up.
969 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100970 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200971 || State == ASKMORE || State == HITRETURN
972 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100973#ifdef FEAT_GUI
974 || gui.starting
975#endif
976 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 return;
978
979 /* update all windows that need updating */
980 update_prepare();
981
Bram Moolenaar29323592016-07-24 22:04:11 +0200982 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
984 if (wp->w_redr_type != 0)
985 win_update(wp);
986 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200987 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 update_finish();
991}
992#endif
993
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200994/*
995 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
996 * window then get the "Pmenu" highlight attribute.
997 */
998 static int
999get_wcr_attr(win_T *wp)
1000{
1001 int wcr_attr = 0;
1002
1003 if (*wp->w_p_wcr != NUL)
1004 wcr_attr = syn_name2attr(wp->w_p_wcr);
1005#ifdef FEAT_TEXT_PROP
1006 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1007 wcr_attr = HL_ATTR(HLF_PNI);
1008#endif
1009 return wcr_attr;
1010}
1011
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001012#ifdef FEAT_TEXT_PROP
1013/*
1014 * Return a string of "len" spaces in IObuff.
1015 */
1016 static char_u *
1017get_spaces(int len)
1018{
1019 vim_memset(IObuff, ' ', (size_t)len);
1020 IObuff[len] = NUL;
1021 return IObuff;
1022}
1023
1024 static void
1025update_popups(void)
1026{
1027 win_T *wp;
1028 int top_off;
1029 int left_off;
1030 int total_width;
1031 int total_height;
1032 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001033 int border_attr[4];
1034 int border_char[8] = {'-', '|', '-', '|', '+', '+', '+', '+', };
1035 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001036 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001037 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001038
1039 // Find the window with the lowest zindex that hasn't been updated yet,
1040 // so that the window with a higher zindex is drawn later, thus goes on
1041 // top.
1042 // TODO: don't redraw every popup every time.
1043 popup_reset_handled();
1044 while ((wp = find_next_popup(TRUE)) != NULL)
1045 {
1046 // Recompute the position if the text changed.
1047 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1048 popup_adjust_position(wp);
1049
1050 // adjust w_winrow and w_wincol for border and padding, since
1051 // win_update() doesn't handle them.
1052 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1053 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1054 wp->w_winrow += top_off;
1055 wp->w_wincol += left_off;
1056
1057 // Draw the popup text.
1058 win_update(wp);
1059
1060 wp->w_winrow -= top_off;
1061 wp->w_wincol -= left_off;
1062
1063 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1064 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1065 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1066 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1067 popup_attr = get_wcr_attr(wp);
1068
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001069 if (enc_utf8)
1070 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001071 border_char[0] = border_char[2] = 0x2550;
1072 border_char[1] = border_char[3] = 0x2551;
1073 border_char[4] = 0x2554;
1074 border_char[5] = 0x2557;
1075 border_char[6] = 0x255d;
1076 border_char[7] = 0x255a;
1077 }
1078 for (i = 0; i < 8; ++i)
1079 if (wp->w_border_char[i] != 0)
1080 border_char[i] = wp->w_border_char[i];
1081
1082 for (i = 0; i < 4; ++i)
1083 {
1084 border_attr[i] = popup_attr;
1085 if (wp->w_border_highlight[i] != NULL)
1086 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001087 }
1088
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001089 if (wp->w_popup_border[0] > 0)
1090 {
1091 // top border
1092 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1093 wp->w_wincol,
1094 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001095 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001096 ? border_char[4] : border_char[0],
1097 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001098 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001099 {
1100 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1101 screen_puts(buf, wp->w_winrow,
1102 wp->w_wincol + total_width - 1, border_attr[1]);
1103 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001104 }
1105
1106 if (wp->w_popup_padding[0] > 0)
1107 {
1108 // top padding
1109 row = wp->w_winrow + wp->w_popup_border[0];
1110 screen_fill(row, row + wp->w_popup_padding[0],
1111 wp->w_wincol + wp->w_popup_border[3],
1112 wp->w_wincol + total_width - wp->w_popup_border[1],
1113 ' ', ' ', popup_attr);
1114 }
1115
1116 for (row = wp->w_winrow + wp->w_popup_border[0];
1117 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1118 ++row)
1119 {
1120 // left border
1121 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001122 {
1123 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1124 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1125 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001126 // left padding
1127 if (wp->w_popup_padding[3] > 0)
1128 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1129 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1130 // right border
1131 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001132 {
1133 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1134 screen_puts(buf, row,
1135 wp->w_wincol + total_width - 1, border_attr[1]);
1136 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001137 // right padding
1138 if (wp->w_popup_padding[1] > 0)
1139 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1140 wp->w_wincol + wp->w_popup_border[3]
1141 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1142 }
1143
1144 if (wp->w_popup_padding[2] > 0)
1145 {
1146 // bottom padding
1147 row = wp->w_winrow + wp->w_popup_border[0]
1148 + wp->w_popup_padding[0] + wp->w_height;
1149 screen_fill(row, row + wp->w_popup_padding[2],
1150 wp->w_wincol + wp->w_popup_border[3],
1151 wp->w_wincol + total_width - wp->w_popup_border[1],
1152 ' ', ' ', popup_attr);
1153 }
1154
1155 if (wp->w_popup_border[2] > 0)
1156 {
1157 // bottom border
1158 row = wp->w_winrow + total_height - 1;
1159 screen_fill(row , row + 1,
1160 wp->w_wincol,
1161 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001162 wp->w_popup_border[3] != 0
1163 ? border_char[7] : border_char[2],
1164 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001165 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001166 {
1167 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1168 screen_puts(buf, row,
1169 wp->w_wincol + total_width - 1, border_attr[2]);
1170 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001171 }
1172 }
1173}
1174#endif
1175
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#if defined(FEAT_GUI) || defined(PROTO)
1177/*
1178 * Update a single window, its status line and maybe the command line msg.
1179 * Used for the GUI scrollbar.
1180 */
1181 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001182updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001184 /* return if already busy updating */
1185 if (updating_screen)
1186 return;
1187
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 update_prepare();
1189
1190#ifdef FEAT_CLIPBOARD
1191 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001192 if (clip_star.available && clip_isautosel_star())
1193 clip_update_selection(&clip_star);
1194 if (clip_plus.available && clip_isautosel_plus())
1195 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001199
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001200 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001201 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001202 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (wp->w_redr_status
1205# ifdef FEAT_CMDL_INFO
1206 || p_ru
1207# endif
1208# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001209 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210# endif
1211 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001212 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214 update_finish();
1215}
1216#endif
1217
1218/*
1219 * Update a single window.
1220 *
1221 * This may cause the windows below it also to be redrawn (when clearing the
1222 * screen or scrolling lines).
1223 *
1224 * How the window is redrawn depends on wp->w_redr_type. Each type also
1225 * implies the one below it.
1226 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001227 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1229 * INVERTED redraw the changed part of the Visual area
1230 * INVERTED_ALL redraw the whole Visual area
1231 * VALID 1. scroll up/down to adjust for a changed w_topline
1232 * 2. update lines at the top when scrolled down
1233 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001234 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 * b_mod_top and b_mod_bot.
1236 * - if wp->w_redraw_top non-zero, redraw lines between
1237 * wp->w_redraw_top and wp->w_redr_bot.
1238 * - continue redrawing when syntax status is invalid.
1239 * 4. if scrolled up, update lines at the bottom.
1240 * This results in three areas that may need updating:
1241 * top: from first row to top_end (when scrolled down)
1242 * mid: from mid_start to mid_end (update inversion or changed text)
1243 * bot: from bot_start to last row (when scrolled up)
1244 */
1245 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001246win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247{
1248 buf_T *buf = wp->w_buffer;
1249 int type;
1250 int top_end = 0; /* Below last row of the top area that needs
1251 updating. 0 when no top area updating. */
1252 int mid_start = 999;/* first row of the mid area that needs
1253 updating. 999 when no mid area updating. */
1254 int mid_end = 0; /* Below last row of the mid area that needs
1255 updating. 0 when no mid area updating. */
1256 int bot_start = 999;/* first row of the bot area that needs
1257 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 int scrolled_down = FALSE; /* TRUE when scrolled down when
1259 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001261 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 int top_to_mod = FALSE; /* redraw above mod_top */
1263#endif
1264
1265 int row; /* current window row to display */
1266 linenr_T lnum; /* current buffer lnum to display */
1267 int idx; /* current index in w_lines[] */
1268 int srow; /* starting row of the current line */
1269
1270 int eof = FALSE; /* if TRUE, we hit the end of the file */
1271 int didline = FALSE; /* if TRUE, we finished the last line */
1272 int i;
1273 long j;
1274 static int recursive = FALSE; /* being called recursively */
1275 int old_botline = wp->w_botline;
1276#ifdef FEAT_FOLDING
1277 long fold_count;
1278#endif
1279#ifdef FEAT_SYN_HL
1280 /* remember what happened to the previous line, to know if
1281 * check_visual_highlight() can be used */
1282#define DID_NONE 1 /* didn't update a line */
1283#define DID_LINE 2 /* updated a normal line */
1284#define DID_FOLD 3 /* updated a folded line */
1285 int did_update = DID_NONE;
1286 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1287#endif
1288 linenr_T mod_top = 0;
1289 linenr_T mod_bot = 0;
1290#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1291 int save_got_int;
1292#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001293#ifdef SYN_TIME_LIMIT
1294 proftime_T syntax_tm;
1295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 type = wp->w_redr_type;
1298
1299 if (type == NOT_VALID)
1300 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 wp->w_lines_valid = 0;
1303 }
1304
1305 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001306 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 wp->w_redr_type = 0;
1309 return;
1310 }
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 /* Window is zero-width: Only need to draw the separator. */
1313 if (wp->w_width == 0)
1314 {
1315 /* draw the vertical separator right of this window */
1316 draw_vsep_win(wp, 0);
1317 wp->w_redr_type = 0;
1318 return;
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001321#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001322 // If this window contains a terminal, redraw works completely differently.
1323 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001324 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001325 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001326# ifdef FEAT_MENU
1327 /* Draw the window toolbar, if there is one. */
1328 if (winbar_height(wp) > 0)
1329 redraw_win_toolbar(wp);
1330# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001331 wp->w_redr_type = 0;
1332 return;
1333 }
1334#endif
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001337 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
1339
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001340#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001341 /* Force redraw when width of 'number' or 'relativenumber' column
1342 * changes. */
1343 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001344 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001345 {
1346 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001347 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001348 }
1349 else
1350#endif
1351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1353 {
1354 /*
1355 * When there are both inserted/deleted lines and specific lines to be
1356 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1357 * everything (only happens when redrawing is off for while).
1358 */
1359 type = NOT_VALID;
1360 }
1361 else
1362 {
1363 /*
1364 * Set mod_top to the first line that needs displaying because of
1365 * changes. Set mod_bot to the first line after the changes.
1366 */
1367 mod_top = wp->w_redraw_top;
1368 if (wp->w_redraw_bot != 0)
1369 mod_bot = wp->w_redraw_bot + 1;
1370 else
1371 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (buf->b_mod_set)
1373 {
1374 if (mod_top == 0 || mod_top > buf->b_mod_top)
1375 {
1376 mod_top = buf->b_mod_top;
1377#ifdef FEAT_SYN_HL
1378 /* Need to redraw lines above the change that may be included
1379 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001380 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001382 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (mod_top < 1)
1384 mod_top = 1;
1385 }
1386#endif
1387 }
1388 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1389 mod_bot = buf->b_mod_bot;
1390
1391#ifdef FEAT_SEARCH_EXTRA
1392 /* When 'hlsearch' is on and using a multi-line search pattern, a
1393 * change in one line may make the Search highlighting in a
1394 * previous line invalid. Simple solution: redraw all visible
1395 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001396 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001398 if (search_hl.rm.regprog != NULL
1399 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001401 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001402 {
1403 cur = wp->w_match_head;
1404 while (cur != NULL)
1405 {
1406 if (cur->match.regprog != NULL
1407 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001408 {
1409 top_to_mod = TRUE;
1410 break;
1411 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001412 cur = cur->next;
1413 }
1414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415#endif
1416 }
1417#ifdef FEAT_FOLDING
1418 if (mod_top != 0 && hasAnyFolding(wp))
1419 {
1420 linenr_T lnumt, lnumb;
1421
1422 /*
1423 * A change in a line can cause lines above it to become folded or
1424 * unfolded. Find the top most buffer line that may be affected.
1425 * If the line was previously folded and displayed, get the first
1426 * line of that fold. If the line is folded now, get the first
1427 * folded line. Use the minimum of these two.
1428 */
1429
1430 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1431 * the line below it. If there is no valid entry, use w_topline.
1432 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1433 * to this line. If there is no valid entry, use MAXLNUM. */
1434 lnumt = wp->w_topline;
1435 lnumb = MAXLNUM;
1436 for (i = 0; i < wp->w_lines_valid; ++i)
1437 if (wp->w_lines[i].wl_valid)
1438 {
1439 if (wp->w_lines[i].wl_lastlnum < mod_top)
1440 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1441 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1442 {
1443 lnumb = wp->w_lines[i].wl_lnum;
1444 /* When there is a fold column it might need updating
1445 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001446 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 ++lnumb;
1448 }
1449 }
1450
1451 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1452 if (mod_top > lnumt)
1453 mod_top = lnumt;
1454
1455 /* Now do the same for the bottom line (one above mod_bot). */
1456 --mod_bot;
1457 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1458 ++mod_bot;
1459 if (mod_bot < lnumb)
1460 mod_bot = lnumb;
1461 }
1462#endif
1463
1464 /* When a change starts above w_topline and the end is below
1465 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001466 * If the end of the change is above w_topline: do like no change was
1467 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (mod_top != 0 && mod_top < wp->w_topline)
1469 {
1470 if (mod_bot > wp->w_topline)
1471 mod_top = wp->w_topline;
1472#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001473 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 top_end = 1;
1475#endif
1476 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001477
1478 /* When line numbers are displayed need to redraw all lines below
1479 * inserted/deleted lines. */
1480 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1481 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001483 wp->w_redraw_top = 0; // reset for next time
1484 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 /*
1487 * When only displaying the lines at the top, set top_end. Used when
1488 * window has scrolled down for msg_scrolled.
1489 */
1490 if (type == REDRAW_TOP)
1491 {
1492 j = 0;
1493 for (i = 0; i < wp->w_lines_valid; ++i)
1494 {
1495 j += wp->w_lines[i].wl_size;
1496 if (j >= wp->w_upd_rows)
1497 {
1498 top_end = j;
1499 break;
1500 }
1501 }
1502 if (top_end == 0)
1503 /* not found (cannot happen?): redraw everything */
1504 type = NOT_VALID;
1505 else
1506 /* top area defined, the rest is VALID */
1507 type = VALID;
1508 }
1509
Bram Moolenaar367329b2007-08-30 11:53:22 +00001510 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001511 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1512 * non-zero and thus not FALSE) will indicate that screenclear() was not
1513 * called. */
1514 if (screen_cleared)
1515 screen_cleared = MAYBE;
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 /*
1518 * If there are no changes on the screen that require a complete redraw,
1519 * handle three cases:
1520 * 1: we are off the top of the screen by a few lines: scroll down
1521 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1522 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1523 * w_lines[] that needs updating.
1524 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001525 if ((type == VALID || type == SOME_VALID
1526 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#ifdef FEAT_DIFF
1528 && !wp->w_botfill && !wp->w_old_botfill
1529#endif
1530 )
1531 {
1532 if (mod_top != 0 && wp->w_topline == mod_top)
1533 {
1534 /*
1535 * w_topline is the first changed line, the scrolling will be done
1536 * further down.
1537 */
1538 }
1539 else if (wp->w_lines[0].wl_valid
1540 && (wp->w_topline < wp->w_lines[0].wl_lnum
1541#ifdef FEAT_DIFF
1542 || (wp->w_topline == wp->w_lines[0].wl_lnum
1543 && wp->w_topfill > wp->w_old_topfill)
1544#endif
1545 ))
1546 {
1547 /*
1548 * New topline is above old topline: May scroll down.
1549 */
1550#ifdef FEAT_FOLDING
1551 if (hasAnyFolding(wp))
1552 {
1553 linenr_T ln;
1554
1555 /* count the number of lines we are off, counting a sequence
1556 * of folded lines as one */
1557 j = 0;
1558 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1559 {
1560 ++j;
1561 if (j >= wp->w_height - 2)
1562 break;
1563 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1564 }
1565 }
1566 else
1567#endif
1568 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1569 if (j < wp->w_height - 2) /* not too far off */
1570 {
1571 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1572#ifdef FEAT_DIFF
1573 /* insert extra lines for previously invisible filler lines */
1574 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1575 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1576 - wp->w_old_topfill;
1577#endif
1578 if (i < wp->w_height - 2) /* less than a screen off */
1579 {
1580 /*
1581 * Try to insert the correct number of lines.
1582 * If not the last window, delete the lines at the bottom.
1583 * win_ins_lines may fail when the terminal can't do it.
1584 */
1585 if (i > 0)
1586 check_for_delay(FALSE);
1587 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1588 {
1589 if (wp->w_lines_valid != 0)
1590 {
1591 /* Need to update rows that are new, stop at the
1592 * first one that scrolled down. */
1593 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596 /* Move the entries that were scrolled, disable
1597 * the entries for the lines to be redrawn. */
1598 if ((wp->w_lines_valid += j) > wp->w_height)
1599 wp->w_lines_valid = wp->w_height;
1600 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1601 wp->w_lines[idx] = wp->w_lines[idx - j];
1602 while (idx >= 0)
1603 wp->w_lines[idx--].wl_valid = FALSE;
1604 }
1605 }
1606 else
1607 mid_start = 0; /* redraw all lines */
1608 }
1609 else
1610 mid_start = 0; /* redraw all lines */
1611 }
1612 else
1613 mid_start = 0; /* redraw all lines */
1614 }
1615 else
1616 {
1617 /*
1618 * New topline is at or below old topline: May scroll up.
1619 * When topline didn't change, find first entry in w_lines[] that
1620 * needs updating.
1621 */
1622
1623 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1624 j = -1;
1625 row = 0;
1626 for (i = 0; i < wp->w_lines_valid; i++)
1627 {
1628 if (wp->w_lines[i].wl_valid
1629 && wp->w_lines[i].wl_lnum == wp->w_topline)
1630 {
1631 j = i;
1632 break;
1633 }
1634 row += wp->w_lines[i].wl_size;
1635 }
1636 if (j == -1)
1637 {
1638 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1639 * lines */
1640 mid_start = 0;
1641 }
1642 else
1643 {
1644 /*
1645 * Try to delete the correct number of lines.
1646 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1647 */
1648#ifdef FEAT_DIFF
1649 /* If the topline didn't change, delete old filler lines,
1650 * otherwise delete filler lines of the new topline... */
1651 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1652 row += wp->w_old_topfill;
1653 else
1654 row += diff_check_fill(wp, wp->w_topline);
1655 /* ... but don't delete new filler lines. */
1656 row -= wp->w_topfill;
1657#endif
1658 if (row > 0)
1659 {
1660 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001661 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1662 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 bot_start = wp->w_height - row;
1664 else
1665 mid_start = 0; /* redraw all lines */
1666 }
1667 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1668 {
1669 /*
1670 * Skip the lines (below the deleted lines) that are still
1671 * valid and don't need redrawing. Copy their info
1672 * upwards, to compensate for the deleted lines. Set
1673 * bot_start to the first row that needs redrawing.
1674 */
1675 bot_start = 0;
1676 idx = 0;
1677 for (;;)
1678 {
1679 wp->w_lines[idx] = wp->w_lines[j];
1680 /* stop at line that didn't fit, unless it is still
1681 * valid (no lines deleted) */
1682 if (row > 0 && bot_start + row
1683 + (int)wp->w_lines[j].wl_size > wp->w_height)
1684 {
1685 wp->w_lines_valid = idx + 1;
1686 break;
1687 }
1688 bot_start += wp->w_lines[idx++].wl_size;
1689
1690 /* stop at the last valid entry in w_lines[].wl_size */
1691 if (++j >= wp->w_lines_valid)
1692 {
1693 wp->w_lines_valid = idx;
1694 break;
1695 }
1696 }
1697#ifdef FEAT_DIFF
1698 /* Correct the first entry for filler lines at the top
1699 * when it won't get updated below. */
1700 if (wp->w_p_diff && bot_start > 0)
1701 wp->w_lines[0].wl_size =
1702 plines_win_nofill(wp, wp->w_topline, TRUE)
1703 + wp->w_topfill;
1704#endif
1705 }
1706 }
1707 }
1708
1709 /* When starting redraw in the first line, redraw all lines. When
1710 * there is only one window it's probably faster to clear the screen
1711 * first. */
1712 if (mid_start == 0)
1713 {
1714 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001715 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001716 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001717 /* Clear the screen when it was not done by win_del_lines() or
1718 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1719 * then. */
1720 if (screen_cleared != TRUE)
1721 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001722 /* The screen was cleared, redraw the tab pages line. */
1723 if (redraw_tabline)
1724 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001727
1728 /* When win_del_lines() or win_ins_lines() caused the screen to be
1729 * cleared (only happens for the first window) or when screenclear()
1730 * was called directly above, "must_redraw" will have been set to
1731 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1732 if (screen_cleared == TRUE)
1733 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735 else
1736 {
1737 /* Not VALID or INVERTED: redraw all lines. */
1738 mid_start = 0;
1739 mid_end = wp->w_height;
1740 }
1741
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001742 if (type == SOME_VALID)
1743 {
1744 /* SOME_VALID: redraw all lines. */
1745 mid_start = 0;
1746 mid_end = wp->w_height;
1747 type = NOT_VALID;
1748 }
1749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /* check if we are updating or removing the inverted part */
1751 if ((VIsual_active && buf == curwin->w_buffer)
1752 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1753 {
1754 linenr_T from, to;
1755
1756 if (VIsual_active)
1757 {
1758 if (VIsual_active
1759 && (VIsual_mode != wp->w_old_visual_mode
1760 || type == INVERTED_ALL))
1761 {
1762 /*
1763 * If the type of Visual selection changed, redraw the whole
1764 * selection. Also when the ownership of the X selection is
1765 * gained or lost.
1766 */
1767 if (curwin->w_cursor.lnum < VIsual.lnum)
1768 {
1769 from = curwin->w_cursor.lnum;
1770 to = VIsual.lnum;
1771 }
1772 else
1773 {
1774 from = VIsual.lnum;
1775 to = curwin->w_cursor.lnum;
1776 }
1777 /* redraw more when the cursor moved as well */
1778 if (wp->w_old_cursor_lnum < from)
1779 from = wp->w_old_cursor_lnum;
1780 if (wp->w_old_cursor_lnum > to)
1781 to = wp->w_old_cursor_lnum;
1782 if (wp->w_old_visual_lnum < from)
1783 from = wp->w_old_visual_lnum;
1784 if (wp->w_old_visual_lnum > to)
1785 to = wp->w_old_visual_lnum;
1786 }
1787 else
1788 {
1789 /*
1790 * Find the line numbers that need to be updated: The lines
1791 * between the old cursor position and the current cursor
1792 * position. Also check if the Visual position changed.
1793 */
1794 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1795 {
1796 from = curwin->w_cursor.lnum;
1797 to = wp->w_old_cursor_lnum;
1798 }
1799 else
1800 {
1801 from = wp->w_old_cursor_lnum;
1802 to = curwin->w_cursor.lnum;
1803 if (from == 0) /* Visual mode just started */
1804 from = to;
1805 }
1806
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001807 if (VIsual.lnum != wp->w_old_visual_lnum
1808 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 if (wp->w_old_visual_lnum < from
1811 && wp->w_old_visual_lnum != 0)
1812 from = wp->w_old_visual_lnum;
1813 if (wp->w_old_visual_lnum > to)
1814 to = wp->w_old_visual_lnum;
1815 if (VIsual.lnum < from)
1816 from = VIsual.lnum;
1817 if (VIsual.lnum > to)
1818 to = VIsual.lnum;
1819 }
1820 }
1821
1822 /*
1823 * If in block mode and changed column or curwin->w_curswant:
1824 * update all lines.
1825 * First compute the actual start and end column.
1826 */
1827 if (VIsual_mode == Ctrl_V)
1828 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001829 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001830#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001831 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar404406a2014-10-09 13:24:43 +02001833 if (curwin->w_p_lbr)
1834 ve_flags = VE_ALL;
1835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001837#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001838 ve_flags = save_ve_flags;
1839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 ++toc;
1841 if (curwin->w_curswant == MAXCOL)
1842 toc = MAXCOL;
1843
1844 if (fromc != wp->w_old_cursor_fcol
1845 || toc != wp->w_old_cursor_lcol)
1846 {
1847 if (from > VIsual.lnum)
1848 from = VIsual.lnum;
1849 if (to < VIsual.lnum)
1850 to = VIsual.lnum;
1851 }
1852 wp->w_old_cursor_fcol = fromc;
1853 wp->w_old_cursor_lcol = toc;
1854 }
1855 }
1856 else
1857 {
1858 /* Use the line numbers of the old Visual area. */
1859 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1860 {
1861 from = wp->w_old_cursor_lnum;
1862 to = wp->w_old_visual_lnum;
1863 }
1864 else
1865 {
1866 from = wp->w_old_visual_lnum;
1867 to = wp->w_old_cursor_lnum;
1868 }
1869 }
1870
1871 /*
1872 * There is no need to update lines above the top of the window.
1873 */
1874 if (from < wp->w_topline)
1875 from = wp->w_topline;
1876
1877 /*
1878 * If we know the value of w_botline, use it to restrict the update to
1879 * the lines that are visible in the window.
1880 */
1881 if (wp->w_valid & VALID_BOTLINE)
1882 {
1883 if (from >= wp->w_botline)
1884 from = wp->w_botline - 1;
1885 if (to >= wp->w_botline)
1886 to = wp->w_botline - 1;
1887 }
1888
1889 /*
1890 * Find the minimal part to be updated.
1891 * Watch out for scrolling that made entries in w_lines[] invalid.
1892 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1893 * top_end; need to redraw from top_end to the "to" line.
1894 * A middle mouse click with a Visual selection may change the text
1895 * above the Visual area and reset wl_valid, do count these for
1896 * mid_end (in srow).
1897 */
1898 if (mid_start > 0)
1899 {
1900 lnum = wp->w_topline;
1901 idx = 0;
1902 srow = 0;
1903 if (scrolled_down)
1904 mid_start = top_end;
1905 else
1906 mid_start = 0;
1907 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1908 {
1909 if (wp->w_lines[idx].wl_valid)
1910 mid_start += wp->w_lines[idx].wl_size;
1911 else if (!scrolled_down)
1912 srow += wp->w_lines[idx].wl_size;
1913 ++idx;
1914# ifdef FEAT_FOLDING
1915 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1916 lnum = wp->w_lines[idx].wl_lnum;
1917 else
1918# endif
1919 ++lnum;
1920 }
1921 srow += mid_start;
1922 mid_end = wp->w_height;
1923 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1924 {
1925 if (wp->w_lines[idx].wl_valid
1926 && wp->w_lines[idx].wl_lnum >= to + 1)
1927 {
1928 /* Only update until first row of this line */
1929 mid_end = srow;
1930 break;
1931 }
1932 srow += wp->w_lines[idx].wl_size;
1933 }
1934 }
1935 }
1936
1937 if (VIsual_active && buf == curwin->w_buffer)
1938 {
1939 wp->w_old_visual_mode = VIsual_mode;
1940 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1941 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001942 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 wp->w_old_curswant = curwin->w_curswant;
1944 }
1945 else
1946 {
1947 wp->w_old_visual_mode = 0;
1948 wp->w_old_cursor_lnum = 0;
1949 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001950 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1954 /* reset got_int, otherwise regexp won't work */
1955 save_got_int = got_int;
1956 got_int = 0;
1957#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001958#ifdef SYN_TIME_LIMIT
1959 /* Set the time limit to 'redrawtime'. */
1960 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001961 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963#ifdef FEAT_FOLDING
1964 win_foldinfo.fi_level = 0;
1965#endif
1966
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001967#ifdef FEAT_MENU
1968 /*
1969 * Draw the window toolbar, if there is one.
1970 * TODO: only when needed.
1971 */
1972 if (winbar_height(wp) > 0)
1973 redraw_win_toolbar(wp);
1974#endif
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 /*
1977 * Update all the window rows.
1978 */
1979 idx = 0; /* first entry in w_lines[].wl_size */
1980 row = 0;
1981 srow = 0;
1982 lnum = wp->w_topline; /* first line shown in window */
1983 for (;;)
1984 {
1985 /* stop updating when reached the end of the window (check for _past_
1986 * the end of the window is at the end of the loop) */
1987 if (row == wp->w_height)
1988 {
1989 didline = TRUE;
1990 break;
1991 }
1992
1993 /* stop updating when hit the end of the file */
1994 if (lnum > buf->b_ml.ml_line_count)
1995 {
1996 eof = TRUE;
1997 break;
1998 }
1999
2000 /* Remember the starting row of the line that is going to be dealt
2001 * with. It is used further down when the line doesn't fit. */
2002 srow = row;
2003
2004 /*
2005 * Update a line when it is in an area that needs updating, when it
2006 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002007 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 * win_del_lines(), check if the current line includes it.
2009 * When syntax folding is being used, the saved syntax states will
2010 * already have been updated, we can't see where the syntax state is
2011 * the same again, just update until the end of the window.
2012 */
2013 if (row < top_end
2014 || (row >= mid_start && row < mid_end)
2015#ifdef FEAT_SEARCH_EXTRA
2016 || top_to_mod
2017#endif
2018 || idx >= wp->w_lines_valid
2019 || (row + wp->w_lines[idx].wl_size > bot_start)
2020 || (mod_top != 0
2021 && (lnum == mod_top
2022 || (lnum >= mod_top
2023 && (lnum < mod_bot
2024#ifdef FEAT_SYN_HL
2025 || did_update == DID_FOLD
2026 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002027 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 && (
2029# ifdef FEAT_FOLDING
2030 (foldmethodIsSyntax(wp)
2031 && hasAnyFolding(wp)) ||
2032# endif
2033 syntax_check_changed(lnum)))
2034#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002035#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002036 /* match in fixed position might need redraw
2037 * if lines were inserted or deleted */
2038 || (wp->w_match_head != NULL
2039 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 )))))
2042 {
2043#ifdef FEAT_SEARCH_EXTRA
2044 if (lnum == mod_top)
2045 top_to_mod = FALSE;
2046#endif
2047
2048 /*
2049 * When at start of changed lines: May scroll following lines
2050 * up or down to minimize redrawing.
2051 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002052 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
2054 if (lnum == mod_top
2055 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002056 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 int old_rows = 0;
2059 int new_rows = 0;
2060 int xtra_rows;
2061 linenr_T l;
2062
2063 /* Count the old number of window rows, using w_lines[], which
2064 * should still contain the sizes for the lines as they are
2065 * currently displayed. */
2066 for (i = idx; i < wp->w_lines_valid; ++i)
2067 {
2068 /* Only valid lines have a meaningful wl_lnum. Invalid
2069 * lines are part of the changed area. */
2070 if (wp->w_lines[i].wl_valid
2071 && wp->w_lines[i].wl_lnum == mod_bot)
2072 break;
2073 old_rows += wp->w_lines[i].wl_size;
2074#ifdef FEAT_FOLDING
2075 if (wp->w_lines[i].wl_valid
2076 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2077 {
2078 /* Must have found the last valid entry above mod_bot.
2079 * Add following invalid entries. */
2080 ++i;
2081 while (i < wp->w_lines_valid
2082 && !wp->w_lines[i].wl_valid)
2083 old_rows += wp->w_lines[i++].wl_size;
2084 break;
2085 }
2086#endif
2087 }
2088
2089 if (i >= wp->w_lines_valid)
2090 {
2091 /* We can't find a valid line below the changed lines,
2092 * need to redraw until the end of the window.
2093 * Inserting/deleting lines has no use. */
2094 bot_start = 0;
2095 }
2096 else
2097 {
2098 /* Able to count old number of rows: Count new window
2099 * rows, and may insert/delete lines */
2100 j = idx;
2101 for (l = lnum; l < mod_bot; ++l)
2102 {
2103#ifdef FEAT_FOLDING
2104 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2105 ++new_rows;
2106 else
2107#endif
2108#ifdef FEAT_DIFF
2109 if (l == wp->w_topline)
2110 new_rows += plines_win_nofill(wp, l, TRUE)
2111 + wp->w_topfill;
2112 else
2113#endif
2114 new_rows += plines_win(wp, l, TRUE);
2115 ++j;
2116 if (new_rows > wp->w_height - row - 2)
2117 {
2118 /* it's getting too much, must redraw the rest */
2119 new_rows = 9999;
2120 break;
2121 }
2122 }
2123 xtra_rows = new_rows - old_rows;
2124 if (xtra_rows < 0)
2125 {
2126 /* May scroll text up. If there is not enough
2127 * remaining text or scrolling fails, must redraw the
2128 * rest. If scrolling works, must redraw the text
2129 * below the scrolled text. */
2130 if (row - xtra_rows >= wp->w_height - 2)
2131 mod_bot = MAXLNUM;
2132 else
2133 {
2134 check_for_delay(FALSE);
2135 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002136 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 mod_bot = MAXLNUM;
2138 else
2139 bot_start = wp->w_height + xtra_rows;
2140 }
2141 }
2142 else if (xtra_rows > 0)
2143 {
2144 /* May scroll text down. If there is not enough
2145 * remaining text of scrolling fails, must redraw the
2146 * rest. */
2147 if (row + xtra_rows >= wp->w_height - 2)
2148 mod_bot = MAXLNUM;
2149 else
2150 {
2151 check_for_delay(FALSE);
2152 if (win_ins_lines(wp, row + old_rows,
2153 xtra_rows, FALSE, FALSE) == FAIL)
2154 mod_bot = MAXLNUM;
2155 else if (top_end > row + old_rows)
2156 /* Scrolled the part at the top that requires
2157 * updating down. */
2158 top_end += xtra_rows;
2159 }
2160 }
2161
2162 /* When not updating the rest, may need to move w_lines[]
2163 * entries. */
2164 if (mod_bot != MAXLNUM && i != j)
2165 {
2166 if (j < i)
2167 {
2168 int x = row + new_rows;
2169
2170 /* move entries in w_lines[] upwards */
2171 for (;;)
2172 {
2173 /* stop at last valid entry in w_lines[] */
2174 if (i >= wp->w_lines_valid)
2175 {
2176 wp->w_lines_valid = j;
2177 break;
2178 }
2179 wp->w_lines[j] = wp->w_lines[i];
2180 /* stop at a line that won't fit */
2181 if (x + (int)wp->w_lines[j].wl_size
2182 > wp->w_height)
2183 {
2184 wp->w_lines_valid = j + 1;
2185 break;
2186 }
2187 x += wp->w_lines[j++].wl_size;
2188 ++i;
2189 }
2190 if (bot_start > x)
2191 bot_start = x;
2192 }
2193 else /* j > i */
2194 {
2195 /* move entries in w_lines[] downwards */
2196 j -= i;
2197 wp->w_lines_valid += j;
2198 if (wp->w_lines_valid > wp->w_height)
2199 wp->w_lines_valid = wp->w_height;
2200 for (i = wp->w_lines_valid; i - j >= idx; --i)
2201 wp->w_lines[i] = wp->w_lines[i - j];
2202
2203 /* The w_lines[] entries for inserted lines are
2204 * now invalid, but wl_size may be used above.
2205 * Reset to zero. */
2206 while (i >= idx)
2207 {
2208 wp->w_lines[i].wl_size = 0;
2209 wp->w_lines[i--].wl_valid = FALSE;
2210 }
2211 }
2212 }
2213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
2215
2216#ifdef FEAT_FOLDING
2217 /*
2218 * When lines are folded, display one line for all of them.
2219 * Otherwise, display normally (can be several display lines when
2220 * 'wrap' is on).
2221 */
2222 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2223 if (fold_count != 0)
2224 {
2225 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2226 ++row;
2227 --fold_count;
2228 wp->w_lines[idx].wl_folded = TRUE;
2229 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2230# ifdef FEAT_SYN_HL
2231 did_update = DID_FOLD;
2232# endif
2233 }
2234 else
2235#endif
2236 if (idx < wp->w_lines_valid
2237 && wp->w_lines[idx].wl_valid
2238 && wp->w_lines[idx].wl_lnum == lnum
2239 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002240 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002241#ifdef FEAT_TEXT_PROP
2242 && !bt_popup(wp->w_buffer)
2243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 && srow + wp->w_lines[idx].wl_size > wp->w_height
2245#ifdef FEAT_DIFF
2246 && diff_check_fill(wp, lnum) == 0
2247#endif
2248 )
2249 {
2250 /* This line is not going to fit. Don't draw anything here,
2251 * will draw "@ " lines below. */
2252 row = wp->w_height + 1;
2253 }
2254 else
2255 {
2256#ifdef FEAT_SEARCH_EXTRA
2257 prepare_search_hl(wp, lnum);
2258#endif
2259#ifdef FEAT_SYN_HL
2260 /* Let the syntax stuff know we skipped a few lines. */
2261 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002262 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 syntax_end_parsing(syntax_last_parsed + 1);
2264#endif
2265
2266 /*
2267 * Display one line.
2268 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002269 row = win_line(wp, lnum, srow, wp->w_height,
2270 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
2272#ifdef FEAT_FOLDING
2273 wp->w_lines[idx].wl_folded = FALSE;
2274 wp->w_lines[idx].wl_lastlnum = lnum;
2275#endif
2276#ifdef FEAT_SYN_HL
2277 did_update = DID_LINE;
2278 syntax_last_parsed = lnum;
2279#endif
2280 }
2281
2282 wp->w_lines[idx].wl_lnum = lnum;
2283 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002284
2285 /* Past end of the window or end of the screen. Note that after
2286 * resizing wp->w_height may be end up too big. That's a problem
2287 * elsewhere, but prevent a crash here. */
2288 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002291 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2293 ++idx;
2294 break;
2295 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002296 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 wp->w_lines[idx].wl_size = row - srow;
2298 ++idx;
2299#ifdef FEAT_FOLDING
2300 lnum += fold_count + 1;
2301#else
2302 ++lnum;
2303#endif
2304 }
2305 else
2306 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002307 if (wp->w_p_rnu)
2308 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002309#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002310 // 'relativenumber' set: The text doesn't need to be drawn, but
2311 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002312 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2313 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002314 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002315 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002316#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002317 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002318 }
2319
2320 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 row += wp->w_lines[idx++].wl_size;
2322 if (row > wp->w_height) /* past end of screen */
2323 break;
2324#ifdef FEAT_FOLDING
2325 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2326#else
2327 ++lnum;
2328#endif
2329#ifdef FEAT_SYN_HL
2330 did_update = DID_NONE;
2331#endif
2332 }
2333
2334 if (lnum > buf->b_ml.ml_line_count)
2335 {
2336 eof = TRUE;
2337 break;
2338 }
2339 }
2340 /*
2341 * End of loop over all window lines.
2342 */
2343
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002344#ifdef FEAT_VTP
2345 /* Rewrite the character at the end of the screen line. */
2346 if (use_vtp())
2347 {
2348 int i;
2349
2350 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002351 if (enc_utf8)
2352 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2353 LineOffset[i] + screen_Columns) > 1)
2354 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2355 else
2356 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2357 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002358 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2359 }
2360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 if (idx > wp->w_lines_valid)
2363 wp->w_lines_valid = idx;
2364
2365#ifdef FEAT_SYN_HL
2366 /*
2367 * Let the syntax stuff know we stop parsing here.
2368 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002369 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 syntax_end_parsing(syntax_last_parsed + 1);
2371#endif
2372
2373 /*
2374 * If we didn't hit the end of the file, and we didn't finish the last
2375 * line we were working on, then the line didn't fit.
2376 */
2377 wp->w_empty_rows = 0;
2378#ifdef FEAT_DIFF
2379 wp->w_filler_rows = 0;
2380#endif
2381 if (!eof && !didline)
2382 {
2383 if (lnum == wp->w_topline)
2384 {
2385 /*
2386 * Single line that does not fit!
2387 * Don't overwrite it, it can be edited.
2388 */
2389 wp->w_botline = lnum + 1;
2390 }
2391#ifdef FEAT_DIFF
2392 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2393 {
2394 /* Window ends in filler lines. */
2395 wp->w_botline = lnum;
2396 wp->w_filler_rows = wp->w_height - srow;
2397 }
2398#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002399#ifdef FEAT_TEXT_PROP
2400 else if (bt_popup(wp->w_buffer))
2401 {
2402 // popup line that doesn't fit is left as-is
2403 wp->w_botline = lnum;
2404 }
2405#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002406 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2407 {
2408 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2409
2410 /*
2411 * Last line isn't finished: Display "@@@" in the last screen line.
2412 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002413 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002414 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002415 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002416 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002417 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002418 set_empty_rows(wp, srow);
2419 wp->w_botline = lnum;
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2422 {
2423 /*
2424 * Last line isn't finished: Display "@@@" at the end.
2425 */
2426 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2427 W_WINROW(wp) + wp->w_height,
2428 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002429 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 set_empty_rows(wp, srow);
2431 wp->w_botline = lnum;
2432 }
2433 else
2434 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002435 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 wp->w_botline = lnum;
2437 }
2438 }
2439 else
2440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 if (eof) /* we hit the end of the file */
2443 {
2444 wp->w_botline = buf->b_ml.ml_line_count + 1;
2445#ifdef FEAT_DIFF
2446 j = diff_check_fill(wp, wp->w_botline);
2447 if (j > 0 && !wp->w_botfill)
2448 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002449 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 if (char2cells(fill_diff) > 1)
2451 i = '-';
2452 else
2453 i = fill_diff;
2454 if (row + j > wp->w_height)
2455 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002456 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 row += j;
2458 }
2459#endif
2460 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002461 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 wp->w_botline = lnum;
2463
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002464 // Make sure the rest of the screen is blank
2465 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002466 win_draw_end(wp,
2467#ifdef FEAT_TEXT_PROP
2468 bt_popup(wp->w_buffer) ? ' ' :
2469#endif
2470 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
2472
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002473#ifdef SYN_TIME_LIMIT
2474 syn_set_timeout(NULL);
2475#endif
2476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 /* Reset the type of redrawing required, the window has been updated. */
2478 wp->w_redr_type = 0;
2479#ifdef FEAT_DIFF
2480 wp->w_old_topfill = wp->w_topfill;
2481 wp->w_old_botfill = wp->w_botfill;
2482#endif
2483
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002484 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
2487 * There is a trick with w_botline. If we invalidate it on each
2488 * change that might modify it, this will cause a lot of expensive
2489 * calls to plines() in update_topline() each time. Therefore the
2490 * value of w_botline is often approximated, and this value is used to
2491 * compute the value of w_topline. If the value of w_botline was
2492 * wrong, check that the value of w_topline is correct (cursor is on
2493 * the visible part of the text). If it's not, we need to redraw
2494 * again. Mostly this just means scrolling up a few lines, so it
2495 * doesn't look too bad. Only do this for the current window (where
2496 * changes are relevant).
2497 */
2498 wp->w_valid |= VALID_BOTLINE;
2499 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2500 {
2501 recursive = TRUE;
2502 curwin->w_valid &= ~VALID_TOPLINE;
2503 update_topline(); /* may invalidate w_botline again */
2504 if (must_redraw != 0)
2505 {
2506 /* Don't update for changes in buffer again. */
2507 i = curbuf->b_mod_set;
2508 curbuf->b_mod_set = FALSE;
2509 win_update(curwin);
2510 must_redraw = 0;
2511 curbuf->b_mod_set = i;
2512 }
2513 recursive = FALSE;
2514 }
2515 }
2516
2517#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2518 /* restore got_int, unless CTRL-C was hit while redrawing */
2519 if (!got_int)
2520 got_int = save_got_int;
2521#endif
2522}
2523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002525 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2526 * Return the new offset.
2527 */
2528 static int
2529screen_fill_end(
2530 win_T *wp,
2531 int c1,
2532 int c2,
2533 int off,
2534 int width,
2535 int row,
2536 int endrow,
2537 int attr)
2538{
2539 int nn = off + width;
2540
2541 if (nn > wp->w_width)
2542 nn = wp->w_width;
2543#ifdef FEAT_RIGHTLEFT
2544 if (wp->w_p_rl)
2545 {
2546 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2547 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2548 c1, c2, attr);
2549 }
2550 else
2551#endif
2552 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2553 wp->w_wincol + off, (int)wp->w_wincol + nn,
2554 c1, c2, attr);
2555 return nn;
2556}
2557
2558/*
2559 * Clear lines near the end the window and mark the unused lines with "c1".
2560 * use "c2" as the filler character.
2561 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 */
2563 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002564win_draw_end(
2565 win_T *wp,
2566 int c1,
2567 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002568 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002569 int row,
2570 int endrow,
2571 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002574 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002575 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002576
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002577 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002578
2579 if (draw_margin)
2580 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002581#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002582 int fdc = compute_foldcolumn(wp, 0);
2583
2584 if (fdc > 0)
2585 // draw the fold column
2586 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002587 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002588#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002589#ifdef FEAT_SIGNS
2590 if (signcolumn_on(wp))
2591 // draw the sign column
2592 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002593 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002594#endif
2595 if ((wp->w_p_nu || wp->w_p_rnu)
2596 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2597 // draw the number column
2598 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002599 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602#ifdef FEAT_RIGHTLEFT
2603 if (wp->w_p_rl)
2604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002606 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002607 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002609 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002610 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
2612 else
2613#endif
2614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002616 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002617 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 set_empty_rows(wp, row);
2621}
2622
Bram Moolenaar1a384422010-07-14 19:53:30 +02002623#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002624/*
2625 * Advance **color_cols and return TRUE when there are columns to draw.
2626 */
2627 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002628advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002629{
2630 while (**color_cols >= 0 && vcol > **color_cols)
2631 ++*color_cols;
2632 return (**color_cols >= 0);
2633}
2634#endif
2635
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002636#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2637/*
2638 * Copy "text" to ScreenLines using "attr".
2639 * Returns the next screen column.
2640 */
2641 static int
2642text_to_screenline(win_T *wp, char_u *text, int col)
2643{
2644 int off = (int)(current_ScreenLine - ScreenLines);
2645
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002646 if (has_mbyte)
2647 {
2648 int cells;
2649 int u8c, u8cc[MAX_MCO];
2650 int i;
2651 int idx;
2652 int c_len;
2653 char_u *p;
2654# ifdef FEAT_ARABIC
2655 int prev_c = 0; /* previous Arabic character */
2656 int prev_c1 = 0; /* first composing char for prev_c */
2657# endif
2658
2659# ifdef FEAT_RIGHTLEFT
2660 if (wp->w_p_rl)
2661 idx = off;
2662 else
2663# endif
2664 idx = off + col;
2665
2666 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2667 for (p = text; *p != NUL; )
2668 {
2669 cells = (*mb_ptr2cells)(p);
2670 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002671 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002672# ifdef FEAT_RIGHTLEFT
2673 - (wp->w_p_rl ? col : 0)
2674# endif
2675 )
2676 break;
2677 ScreenLines[idx] = *p;
2678 if (enc_utf8)
2679 {
2680 u8c = utfc_ptr2char(p, u8cc);
2681 if (*p < 0x80 && u8cc[0] == 0)
2682 {
2683 ScreenLinesUC[idx] = 0;
2684#ifdef FEAT_ARABIC
2685 prev_c = u8c;
2686#endif
2687 }
2688 else
2689 {
2690#ifdef FEAT_ARABIC
2691 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2692 {
2693 /* Do Arabic shaping. */
2694 int pc, pc1, nc;
2695 int pcc[MAX_MCO];
2696 int firstbyte = *p;
2697
2698 /* The idea of what is the previous and next
2699 * character depends on 'rightleft'. */
2700 if (wp->w_p_rl)
2701 {
2702 pc = prev_c;
2703 pc1 = prev_c1;
2704 nc = utf_ptr2char(p + c_len);
2705 prev_c1 = u8cc[0];
2706 }
2707 else
2708 {
2709 pc = utfc_ptr2char(p + c_len, pcc);
2710 nc = prev_c;
2711 pc1 = pcc[0];
2712 }
2713 prev_c = u8c;
2714
2715 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2716 pc, pc1, nc);
2717 ScreenLines[idx] = firstbyte;
2718 }
2719 else
2720 prev_c = u8c;
2721#endif
2722 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002723 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002724 for (i = 0; i < Screen_mco; ++i)
2725 {
2726 ScreenLinesC[i][idx] = u8cc[i];
2727 if (u8cc[i] == 0)
2728 break;
2729 }
2730 }
2731 if (cells > 1)
2732 ScreenLines[idx + 1] = 0;
2733 }
2734 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2735 /* double-byte single width character */
2736 ScreenLines2[idx] = p[1];
2737 else if (cells > 1)
2738 /* double-width character */
2739 ScreenLines[idx + 1] = p[1];
2740 col += cells;
2741 idx += cells;
2742 p += c_len;
2743 }
2744 }
2745 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002746 {
2747 int len = (int)STRLEN(text);
2748
Bram Moolenaar02631462017-09-22 15:20:32 +02002749 if (len > wp->w_width - col)
2750 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002751 if (len > 0)
2752 {
2753#ifdef FEAT_RIGHTLEFT
2754 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002755 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002756 else
2757#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002758 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002759 col += len;
2760 }
2761 }
2762 return col;
2763}
2764#endif
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766#ifdef FEAT_FOLDING
2767/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002768 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2769 * space is available for window "wp", minus "col".
2770 */
2771 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002772compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002773{
2774 int fdc = wp->w_p_fdc;
2775 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002776 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002777
2778 if (fdc > wwidth - (col + wmw))
2779 fdc = wwidth - (col + wmw);
2780 return fdc;
2781}
2782
2783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 * Display one folded line.
2785 */
2786 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002787fold_line(
2788 win_T *wp,
2789 long fold_count,
2790 foldinfo_T *foldinfo,
2791 linenr_T lnum,
2792 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002794 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 pos_T *top, *bot;
2796 linenr_T lnume = lnum + fold_count - 1;
2797 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002798 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 int col;
2801 int txtcol;
2802 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002803 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 /* Build the fold line:
2806 * 1. Add the cmdwin_type for the command-line window
2807 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002808 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 * 4. Compose the text
2810 * 5. Add the text
2811 * 6. set highlighting for the Visual area an other text
2812 */
2813 col = 0;
2814
2815 /*
2816 * 1. Add the cmdwin_type for the command-line window
2817 * Ignores 'rightleft', this window is never right-left.
2818 */
2819#ifdef FEAT_CMDWIN
2820 if (cmdwin_type != 0 && wp == curwin)
2821 {
2822 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002823 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (enc_utf8)
2825 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 ++col;
2827 }
2828#endif
2829
2830 /*
2831 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002832 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002834 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if (fdc > 0)
2836 {
2837 fill_foldcolumn(buf, wp, TRUE, lnum);
2838#ifdef FEAT_RIGHTLEFT
2839 if (wp->w_p_rl)
2840 {
2841 int i;
2842
Bram Moolenaar02631462017-09-22 15:20:32 +02002843 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002844 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 /* reverse the fold column */
2846 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002847 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849 else
2850#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002851 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 col += fdc;
2853 }
2854
2855#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002856# define RL_MEMSET(p, v, l) \
2857 do { \
2858 if (wp->w_p_rl) \
2859 for (ri = 0; ri < l; ++ri) \
2860 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2861 else \
2862 for (ri = 0; ri < l; ++ri) \
2863 ScreenAttrs[off + (p) + ri] = v; \
2864 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002866# define RL_MEMSET(p, v, l) \
2867 do { \
2868 for (ri = 0; ri < l; ++ri) \
2869 ScreenAttrs[off + (p) + ri] = v; \
2870 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871#endif
2872
Bram Moolenaar64486672010-05-16 15:46:46 +02002873 /* Set all attributes of the 'number' or 'relativenumber' column and the
2874 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002875 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
2877#ifdef FEAT_SIGNS
2878 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002879 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002881 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (len > 0)
2883 {
2884 if (len > 2)
2885 len = 2;
2886# ifdef FEAT_RIGHTLEFT
2887 if (wp->w_p_rl)
2888 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002890 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 else
2892# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002893 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 col += len;
2895 }
2896 }
2897#endif
2898
2899 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002900 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002902 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002904 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 if (len > 0)
2906 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002907 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002908 long num;
2909 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002910
2911 if (len > w + 1)
2912 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002913
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002914 if (wp->w_p_nu && !wp->w_p_rnu)
2915 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002916 num = (long)lnum;
2917 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002918 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002919 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002920 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002921 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002922 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002923 /* 'number' + 'relativenumber': cursor line shows absolute
2924 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002925 num = lnum;
2926 fmt = "%-*ld ";
2927 }
2928 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002929
Bram Moolenaar700e7342013-01-30 12:31:36 +01002930 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931#ifdef FEAT_RIGHTLEFT
2932 if (wp->w_p_rl)
2933 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002934 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002935 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 else
2937#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002938 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 col += len;
2940 }
2941 }
2942
2943 /*
2944 * 4. Compose the folded-line string with 'foldtext', if set.
2945 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002946 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
2948 txtcol = col; /* remember where text starts */
2949
2950 /*
2951 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2952 * Right-left text is put in columns 0 - number-col, normal text is put
2953 * in columns number-col - window-width.
2954 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002955 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 /* Fill the rest of the line with the fold filler */
2958#ifdef FEAT_RIGHTLEFT
2959 if (wp->w_p_rl)
2960 col -= txtcol;
2961#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002962 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963#ifdef FEAT_RIGHTLEFT
2964 - (wp->w_p_rl ? txtcol : 0)
2965#endif
2966 )
2967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (enc_utf8)
2969 {
2970 if (fill_fold >= 0x80)
2971 {
2972 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002973 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002974 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
2976 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002979 ScreenLines[off + col] = fill_fold;
2980 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002981 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002983 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002984 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
2986
2987 if (text != buf)
2988 vim_free(text);
2989
2990 /*
2991 * 6. set highlighting for the Visual area an other text.
2992 * If all folded lines are in the Visual area, highlight the line.
2993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2995 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002996 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 {
2998 /* Visual is after curwin->w_cursor */
2999 top = &curwin->w_cursor;
3000 bot = &VIsual;
3001 }
3002 else
3003 {
3004 /* Visual is before curwin->w_cursor */
3005 top = &VIsual;
3006 bot = &curwin->w_cursor;
3007 }
3008 if (lnum >= top->lnum
3009 && lnume <= bot->lnum
3010 && (VIsual_mode != 'v'
3011 || ((lnum > top->lnum
3012 || (lnum == top->lnum
3013 && top->col == 0))
3014 && (lnume < bot->lnum
3015 || (lnume == bot->lnum
3016 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003017 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
3019 if (VIsual_mode == Ctrl_V)
3020 {
3021 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003022 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003024 if (wp->w_old_cursor_lcol != MAXCOL
3025 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003026 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 len = wp->w_old_cursor_lcol;
3028 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003029 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003030 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003031 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033 }
3034 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003037 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 }
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003042#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003043 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3044 if (wp->w_p_cc_cols)
3045 {
3046 int i = 0;
3047 int j = wp->w_p_cc_cols[i];
3048 int old_txtcol = txtcol;
3049
3050 while (j > -1)
3051 {
3052 txtcol += j;
3053 if (wp->w_p_wrap)
3054 txtcol -= wp->w_skipcol;
3055 else
3056 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003057 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003058 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003059 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003060 txtcol = old_txtcol;
3061 j = wp->w_p_cc_cols[++i];
3062 }
3063 }
3064
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003065 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003066 if (wp->w_p_cuc)
3067 {
3068 txtcol += wp->w_virtcol;
3069 if (wp->w_p_wrap)
3070 txtcol -= wp->w_skipcol;
3071 else
3072 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003073 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003074 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003075 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003076 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaar02631462017-09-22 15:20:32 +02003079 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003080 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081
3082 /*
3083 * Update w_cline_height and w_cline_folded if the cursor line was
3084 * updated (saves a call to plines() later).
3085 */
3086 if (wp == curwin
3087 && lnum <= curwin->w_cursor.lnum
3088 && lnume >= curwin->w_cursor.lnum)
3089 {
3090 curwin->w_cline_row = row;
3091 curwin->w_cline_height = 1;
3092 curwin->w_cline_folded = TRUE;
3093 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3094 }
3095}
3096
3097/*
3098 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3099 */
3100 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003101copy_text_attr(
3102 int off,
3103 char_u *buf,
3104 int len,
3105 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003107 int i;
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 if (enc_utf8)
3111 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003112 for (i = 0; i < len; ++i)
3113 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114}
3115
3116/*
3117 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003118 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 */
3120 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003121fill_foldcolumn(
3122 char_u *p,
3123 win_T *wp,
3124 int closed, /* TRUE of FALSE */
3125 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 int i = 0;
3128 int level;
3129 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003130 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003131 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132
3133 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003134 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 level = win_foldinfo.fi_level;
3137 if (level > 0)
3138 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003139 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003140 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003141
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 /* If the column is too narrow, we start at the lowest level that
3143 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003144 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (first_level < 1)
3146 first_level = 1;
3147
Bram Moolenaar1c934292015-01-27 16:39:29 +01003148 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 {
3150 if (win_foldinfo.fi_lnum == lnum
3151 && first_level + i >= win_foldinfo.fi_low_level)
3152 p[i] = '-';
3153 else if (first_level == 1)
3154 p[i] = '|';
3155 else if (first_level + i <= 9)
3156 p[i] = '0' + first_level + i;
3157 else
3158 p[i] = '>';
3159 if (first_level + i == level)
3160 break;
3161 }
3162 }
3163 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003164 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165}
3166#endif /* FEAT_FOLDING */
3167
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003168#ifdef FEAT_TEXT_PROP
3169static textprop_T *current_text_props = NULL;
3170static buf_T *current_buf = NULL;
3171
3172 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003173text_prop_compare(const void *s1, const void *s2)
3174{
3175 int idx1, idx2;
3176 proptype_T *pt1, *pt2;
3177 colnr_T col1, col2;
3178
3179 idx1 = *(int *)s1;
3180 idx2 = *(int *)s2;
3181 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3182 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3183 if (pt1 == pt2)
3184 return 0;
3185 if (pt1 == NULL)
3186 return -1;
3187 if (pt2 == NULL)
3188 return 1;
3189 if (pt1->pt_priority != pt2->pt_priority)
3190 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3191 col1 = current_text_props[idx1].tp_col;
3192 col2 = current_text_props[idx2].tp_col;
3193 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3194}
3195#endif
3196
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197/*
3198 * Display line "lnum" of window 'wp' on the screen.
3199 * Start at row "startrow", stop when "endrow" is reached.
3200 * wp->w_virtcol needs to be valid.
3201 *
3202 * Return the number of last row the line occupies.
3203 */
3204 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003205win_line(
3206 win_T *wp,
3207 linenr_T lnum,
3208 int startrow,
3209 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003210 int nochange UNUSED, // not updating for changed text
3211 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003213 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3215 int c = 0; /* init for GCC */
3216 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003217#ifdef FEAT_LINEBREAK
3218 long vcol_sbr = -1; /* virtual column after showbreak */
3219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 long vcol_prev = -1; /* "vcol" of previous character */
3221 char_u *line; /* current line */
3222 char_u *ptr; /* current position in "line" */
3223 int row; /* row in the window, excl w_winrow */
3224 int screen_row; /* row on the screen, incl w_winrow */
3225
3226 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3227 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003228 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003229 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003231 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 int extra_attr = 0; /* attributes when n_extra != 0 */
3233 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3234 displaying lcs_eol at end-of-line */
3235 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3236 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3237
3238 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3239 int saved_n_extra = 0;
3240 char_u *saved_p_extra = NULL;
3241 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003242 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 int saved_char_attr = 0;
3244
3245 int n_attr = 0; /* chars with special attr */
3246 int saved_attr2 = 0; /* char_attr saved for n_attr */
3247 int n_attr3 = 0; /* chars with overruling special attr */
3248 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3249
3250 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3251
3252 int fromcol, tocol; /* start/end of inverting */
3253 int fromcol_prev = -2; /* start of inverting after cursor */
3254 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003256 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 pos_T pos;
3258 long v;
3259
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003260 int char_attr = 0; // attributes for next character
3261 int attr_pri = FALSE; // char_attr has priority
3262 int area_highlighting = FALSE; // Visual or incsearch highlighting
3263 // in this line
3264 int vi_attr = 0; // attributes for Visual and incsearch
3265 // highlighting
3266 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003267 int win_attr = 0; // background for whole window, except
3268 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003269 int area_attr = 0; // attributes desired by highlighting
3270 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003272 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 int syntax_attr = 0; /* attributes desired by syntax */
3274 int has_syntax = FALSE; /* this buffer has syntax highl. */
3275 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003276 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003277 int draw_color_col = FALSE; /* highlight colorcolumn */
3278 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003279#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003280#ifdef FEAT_TEXT_PROP
3281 int text_prop_count;
3282 int text_prop_next = 0; // next text property to use
3283 textprop_T *text_props = NULL;
3284 int *text_prop_idxs = NULL;
3285 int text_props_active = 0;
3286 proptype_T *text_prop_type = NULL;
3287 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003288 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003289#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003290#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003291 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003292# define SPWORDLEN 150
3293 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003294 int nextlinecol = 0; /* column where nextline[] starts */
3295 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003296 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003297 int spell_attr = 0; /* attributes desired by spelling */
3298 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003299 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3300 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003301 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003302 static int cap_col = -1; /* column to check for Cap word */
3303 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003304 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003306 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 int multi_attr = 0; /* attributes desired by multibyte */
3308 int mb_l = 1; /* multi-byte byte length */
3309 int mb_c = 0; /* decoded multi-byte character */
3310 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003311 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312#ifdef FEAT_DIFF
3313 int filler_lines; /* nr of filler lines to be drawn */
3314 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003315 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 int change_start = MAXCOL; /* first col of changed area */
3317 int change_end = -1; /* last col of changed area */
3318#endif
3319 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3320#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003321 int need_showbreak = FALSE; /* overlong line, skipping first x
3322 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003324#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003325 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003327 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
3329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003330 matchitem_T *cur; /* points to the match list */
3331 match_T *shl; /* points to search_hl or a match */
3332 int shl_flag; /* flag to indicate whether search_hl
3333 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003334 int pos_inprogress; /* marks that position match search is
3335 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003336 int prevcol_hl_flag; /* flag to indicate whether prevcol
3337 equals startcol of search_hl or one
3338 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#endif
3340#ifdef FEAT_ARABIC
3341 int prev_c = 0; /* previous Arabic character */
3342 int prev_c1 = 0; /* first composing char for prev_c */
3343#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003344#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003345 int did_line_attr = 0;
3346#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003347#ifdef FEAT_TERMINAL
3348 int get_term_attr = FALSE;
3349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 /* draw_state: items that are drawn in sequence: */
3352#define WL_START 0 /* nothing done yet */
3353#ifdef FEAT_CMDWIN
3354# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3355#else
3356# define WL_CMDLINE WL_START
3357#endif
3358#ifdef FEAT_FOLDING
3359# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3360#else
3361# define WL_FOLD WL_CMDLINE
3362#endif
3363#ifdef FEAT_SIGNS
3364# define WL_SIGN WL_FOLD + 1 /* column for signs */
3365#else
3366# define WL_SIGN WL_FOLD /* column for signs */
3367#endif
3368#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003369#ifdef FEAT_LINEBREAK
3370# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003372# define WL_BRI WL_NR
3373#endif
3374#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3375# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3376#else
3377# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#endif
3379#define WL_LINE WL_SBR + 1 /* text in the line */
3380 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003381#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 int feedback_col = 0;
3383 int feedback_old_attr = -1;
3384#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003385 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar860cae12010-06-05 23:22:07 +02003387#ifdef FEAT_CONCEAL
3388 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003389 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003390 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003391 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003392 int is_concealing = FALSE;
3393 int boguscols = 0; /* nonexistent columns added to force
3394 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003395 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003396 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003397 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003398 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003399# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003400# define FIX_FOR_BOGUSCOLS \
3401 { \
3402 n_extra += vcol_off; \
3403 vcol -= vcol_off; \
3404 vcol_off = 0; \
3405 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003406 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003407 boguscols = 0; \
3408 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003409#else
3410# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 if (startrow > endrow) /* past the end already! */
3414 return startrow;
3415
3416 row = startrow;
3417 screen_row = row + W_WINROW(wp);
3418
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003419 if (!number_only)
3420 {
3421 /*
3422 * To speed up the loop below, set extra_check when there is linebreak,
3423 * trailing white space and/or syntax processing to be done.
3424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003426 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427#endif
3428#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003429 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003430# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003431 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003432# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003433 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003435 /* Prepare for syntax highlighting in this line. When there is an
3436 * error, stop syntax highlighting. */
3437 save_did_emsg = did_emsg;
3438 did_emsg = FALSE;
3439 syntax_start(wp, lnum);
3440 if (did_emsg)
3441 wp->w_s->b_syn_error = TRUE;
3442 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003443 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003444 did_emsg = save_did_emsg;
3445#ifdef SYN_TIME_LIMIT
3446 if (!wp->w_s->b_syn_slow)
3447#endif
3448 {
3449 has_syntax = TRUE;
3450 extra_check = TRUE;
3451 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003454
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003455 /* Check for columns to display for 'colorcolumn'. */
3456 color_cols = wp->w_p_cc_cols;
3457 if (color_cols != NULL)
3458 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003459#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003460
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003461#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003462 if (term_show_buffer(wp->w_buffer))
3463 {
3464 extra_check = TRUE;
3465 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003466 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003467 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003468#endif
3469
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003470#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003471 if (wp->w_p_spell
3472 && *wp->w_s->b_p_spl != NUL
3473 && wp->w_s->b_langp.ga_len > 0
3474 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003475 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003476 /* Prepare for spell checking. */
3477 has_spell = TRUE;
3478 extra_check = TRUE;
3479
Bram Moolenaar7701f302018-10-02 21:20:32 +02003480 /* Get the start of the next line, so that words that wrap to the
3481 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003482 * Trick: skip a few chars for C/shell/Vim comments */
3483 nextline[SPWORDLEN] = NUL;
3484 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3485 {
3486 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3487 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3488 }
3489
Bram Moolenaar7701f302018-10-02 21:20:32 +02003490 /* When a word wrapped from the previous line the start of the
3491 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003492 if (lnum == checked_lnum)
3493 cur_checked_col = checked_col;
3494 checked_lnum = 0;
3495
3496 /* When there was a sentence end in the previous line may require a
3497 * word starting with capital in this line. In line 1 always check
3498 * the first word. */
3499 if (lnum != capcol_lnum)
3500 cap_col = -1;
3501 if (lnum == 1)
3502 cap_col = 0;
3503 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#endif
3506
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003507 /*
3508 * handle visual active in this window
3509 */
3510 fromcol = -10;
3511 tocol = MAXCOL;
3512 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003514 /* Visual is after curwin->w_cursor */
3515 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003517 top = &curwin->w_cursor;
3518 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003520 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003522 top = &VIsual;
3523 bot = &curwin->w_cursor;
3524 }
3525 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3526 if (VIsual_mode == Ctrl_V) /* block mode */
3527 {
3528 if (lnum_in_visual_area)
3529 {
3530 fromcol = wp->w_old_cursor_fcol;
3531 tocol = wp->w_old_cursor_lcol;
3532 }
3533 }
3534 else /* non-block mode */
3535 {
3536 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003538 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003540 if (VIsual_mode == 'V') /* linewise */
3541 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else
3543 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003544 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3545 if (gchar_pos(top) == NUL)
3546 tocol = fromcol + 1;
3547 }
3548 }
3549 if (VIsual_mode != 'V' && lnum == bot->lnum)
3550 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003551 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003552 {
3553 fromcol = -10;
3554 tocol = MAXCOL;
3555 }
3556 else if (bot->col == MAXCOL)
3557 tocol = MAXCOL;
3558 else
3559 {
3560 pos = *bot;
3561 if (*p_sel == 'e')
3562 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3563 else
3564 {
3565 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3566 ++tocol;
3567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569 }
3570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003572 /* Check if the character under the cursor should not be inverted */
3573 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003574#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003575 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003576#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003577 )
3578 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003580 /* if inverting in this line set area_highlighting */
3581 if (fromcol >= 0)
3582 {
3583 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003584 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003586 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003587 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003588 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003589 && clip_isautosel_plus()))
3590 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003595 /*
3596 * handle 'incsearch' and ":s///c" highlighting
3597 */
3598 else if (highlight_match
3599 && wp == curwin
3600 && lnum >= curwin->w_cursor.lnum
3601 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003603 if (lnum == curwin->w_cursor.lnum)
3604 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003605 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003606 else
3607 fromcol = 0;
3608 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3609 {
3610 pos.lnum = lnum;
3611 pos.col = search_match_endcol;
3612 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3613 }
3614 else
3615 tocol = MAXCOL;
3616 /* do at least one character; happens when past end of line */
3617 if (fromcol == tocol)
3618 tocol = fromcol + 1;
3619 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003620 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623
3624#ifdef FEAT_DIFF
3625 filler_lines = diff_check(wp, lnum);
3626 if (filler_lines < 0)
3627 {
3628 if (filler_lines == -1)
3629 {
3630 if (diff_find_change(wp, lnum, &change_start, &change_end))
3631 diff_hlf = HLF_ADD; /* added line */
3632 else if (change_start == 0)
3633 diff_hlf = HLF_TXD; /* changed text */
3634 else
3635 diff_hlf = HLF_CHD; /* changed line */
3636 }
3637 else
3638 diff_hlf = HLF_ADD; /* added line */
3639 filler_lines = 0;
3640 area_highlighting = TRUE;
3641 }
3642 if (lnum == wp->w_topline)
3643 filler_lines = wp->w_topfill;
3644 filler_todo = filler_lines;
3645#endif
3646
3647#ifdef LINE_ATTR
3648# ifdef FEAT_SIGNS
3649 /* If this line has a sign with line highlighting set line_attr. */
3650 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3651 if (v != 0)
3652 line_attr = sign_get_attr((int)v, TRUE);
3653# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003654# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003656 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003657 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658# endif
3659 if (line_attr != 0)
3660 area_highlighting = TRUE;
3661#endif
3662
3663 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3664 ptr = line;
3665
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003666#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003667 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003668 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003669 /* For checking first word with a capital skip white space. */
3670 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003671 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003672
Bram Moolenaar30abd282005-06-22 22:35:10 +00003673 /* To be able to spell-check over line boundaries copy the end of the
3674 * current line into nextline[]. Above the start of the next line was
3675 * copied to nextline[SPWORDLEN]. */
3676 if (nextline[SPWORDLEN] == NUL)
3677 {
3678 /* No next line or it is empty. */
3679 nextlinecol = MAXCOL;
3680 nextline_idx = 0;
3681 }
3682 else
3683 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003684 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003685 if (v < SPWORDLEN)
3686 {
3687 /* Short line, use it completely and append the start of the
3688 * next line. */
3689 nextlinecol = 0;
3690 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003691 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003692 nextline_idx = v + 1;
3693 }
3694 else
3695 {
3696 /* Long line, use only the last SPWORDLEN bytes. */
3697 nextlinecol = v - SPWORDLEN;
3698 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3699 nextline_idx = SPWORDLEN + 1;
3700 }
3701 }
3702 }
3703#endif
3704
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003705 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003707 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003708 extra_check = TRUE;
3709 /* find start of trailing whitespace */
3710 if (lcs_trail)
3711 {
3712 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003713 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003714 --trailcol;
3715 trailcol += (colnr_T) (ptr - line);
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003719 wcr_attr = get_wcr_attr(wp);
3720 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003721 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003722 win_attr = wcr_attr;
3723 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003724 }
3725#ifdef FEAT_TEXT_PROP
3726 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003727 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003728#endif
3729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 /*
3731 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3732 * first character to be displayed.
3733 */
3734 if (wp->w_p_wrap)
3735 v = wp->w_skipcol;
3736 else
3737 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003738 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 while (vcol < v && *ptr != NUL)
3743 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003744 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003747 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003750 /* When:
3751 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003752 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003753 * - 'virtualedit' is set, or
3754 * - the visual mode is active,
3755 * the end of the line may be before the start of the displayed part.
3756 */
3757 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003758#ifdef FEAT_SYN_HL
3759 wp->w_p_cuc || draw_color_col ||
3760#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003761 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003762 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /* Handle a character that's not completely on the screen: Put ptr at
3766 * that character but skip the first few screen characters. */
3767 if (vcol > v)
3768 {
3769 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003771 /* If the character fits on the screen, don't need to skip it.
3772 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003773 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003774 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
3776
3777 /*
3778 * Adjust for when the inverted text is before the screen,
3779 * and when the start of the inverted text is before the screen.
3780 */
3781 if (tocol <= vcol)
3782 fromcol = 0;
3783 else if (fromcol >= 0 && fromcol < vcol)
3784 fromcol = vcol;
3785
3786#ifdef FEAT_LINEBREAK
3787 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3788 if (wp->w_p_wrap)
3789 need_showbreak = TRUE;
3790#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003791#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003792 /* When spell checking a word we need to figure out the start of the
3793 * word and if it's badly spelled or not. */
3794 if (has_spell)
3795 {
3796 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003797 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003798 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003799
3800 pos = wp->w_cursor;
3801 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003802 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003803 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003804
3805 /* spell_move_to() may call ml_get() and make "line" invalid */
3806 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3807 ptr = line + linecol;
3808
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003809 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003810 {
3811 /* no bad word found at line start, don't check until end of a
3812 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003813 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003814 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003815 }
3816 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003817 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003818 /* bad word found, use attributes until end of word */
3819 word_end = wp->w_cursor.col + len + 1;
3820
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003821 /* Turn index into actual attributes. */
3822 if (spell_hlf != HLF_COUNT)
3823 spell_attr = highlight_attr[spell_hlf];
3824 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003825 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003826
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003827# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003828 /* Need to restart syntax highlighting for this line. */
3829 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003830 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003831# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003832 }
3833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835
3836 /*
3837 * Correct highlighting for cursor that can't be disabled.
3838 * Avoids having to check this for each character.
3839 */
3840 if (fromcol >= 0)
3841 {
3842 if (noinvcur)
3843 {
3844 if ((colnr_T)fromcol == wp->w_virtcol)
3845 {
3846 /* highlighting starts at cursor, let it start just after the
3847 * cursor */
3848 fromcol_prev = fromcol;
3849 fromcol = -1;
3850 }
3851 else if ((colnr_T)fromcol < wp->w_virtcol)
3852 /* restart highlighting after the cursor */
3853 fromcol_prev = wp->w_virtcol;
3854 }
3855 if (fromcol >= tocol)
3856 fromcol = -1;
3857 }
3858
3859#ifdef FEAT_SEARCH_EXTRA
3860 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003861 * Handle highlighting the last used search pattern and matches.
3862 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003863 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003865 cur = wp->w_match_head;
3866 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003867 while ((cur != NULL || shl_flag == FALSE) && !number_only
3868 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003870 if (shl_flag == FALSE)
3871 {
3872 shl = &search_hl;
3873 shl_flag = TRUE;
3874 }
3875 else
3876 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003877 shl->startcol = MAXCOL;
3878 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003880 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003881 v = (long)(ptr - line);
3882 if (cur != NULL)
3883 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003884 next_search_hl(wp, shl, lnum, (colnr_T)v,
3885 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003886
3887 /* Need to get the line again, a multi-line regexp may have made it
3888 * invalid. */
3889 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3890 ptr = line + v;
3891
3892 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003894 if (shl->lnum == lnum)
3895 shl->startcol = shl->rm.startpos[0].col;
3896 else
3897 shl->startcol = 0;
3898 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3899 - shl->rm.startpos[0].lnum)
3900 shl->endcol = shl->rm.endpos[0].col;
3901 else
3902 shl->endcol = MAXCOL;
3903 /* Highlight one character for an empty match. */
3904 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003906 if (has_mbyte && line[shl->endcol] != NUL)
3907 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3908 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003909 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003911 if ((long)shl->startcol < v) /* match at leftcol */
3912 {
3913 shl->attr_cur = shl->attr;
3914 search_attr = shl->attr;
3915 }
3916 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003918 if (shl != &search_hl && cur != NULL)
3919 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921#endif
3922
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003923#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003924 // Cursor line highlighting for 'cursorline' in the current window.
3925 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003926 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003927 // Do not show the cursor line when Visual mode is active, because it's
3928 // not clear what is selected then. Do update w_last_cursorline.
3929 if (!(wp == curwin && VIsual_active))
3930 {
3931 line_attr = HL_ATTR(HLF_CUL);
3932 area_highlighting = TRUE;
3933 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003934 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003935 }
3936#endif
3937
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003938#ifdef FEAT_TEXT_PROP
3939 {
3940 char_u *prop_start;
3941
3942 text_prop_count = get_text_props(wp->w_buffer, lnum,
3943 &prop_start, FALSE);
3944 if (text_prop_count > 0)
3945 {
3946 // Make a copy of the properties, so that they are properly
3947 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003948 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003949 if (text_props != NULL)
3950 mch_memmove(text_props, prop_start,
3951 text_prop_count * sizeof(textprop_T));
3952
3953 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003954 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003955 area_highlighting = TRUE;
3956 extra_check = TRUE;
3957 }
3958 }
3959#endif
3960
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003961 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#ifdef FEAT_RIGHTLEFT
3965 if (wp->w_p_rl)
3966 {
3967 /* Rightleft window: process the text in the normal direction, but put
3968 * it in current_ScreenLine[] from right to left. Start at the
3969 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003970 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003972 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974#endif
3975
3976 /*
3977 * Repeat for the whole displayed line.
3978 */
3979 for (;;)
3980 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003981#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003982 int has_match_conc = 0; // match wants to conceal
3983 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 /* Skip this quickly when working on the text. */
3986 if (draw_state != WL_LINE)
3987 {
3988#ifdef FEAT_CMDWIN
3989 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3990 {
3991 draw_state = WL_CMDLINE;
3992 if (cmdwin_type != 0 && wp == curwin)
3993 {
3994 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003996 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003997 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003998 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 }
4001#endif
4002
4003#ifdef FEAT_FOLDING
4004 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4005 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004006 int fdc = compute_foldcolumn(wp, 0);
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004009 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004011 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004012 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004013 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004014 p_extra_free = alloc(12 + 1);
4015
4016 if (p_extra_free != NULL)
4017 {
4018 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4019 n_extra = fdc;
4020 p_extra_free[n_extra] = NUL;
4021 p_extra = p_extra_free;
4022 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004023 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004024 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 }
4028#endif
4029
4030#ifdef FEAT_SIGNS
4031 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4032 {
4033 draw_state = WL_SIGN;
4034 /* Show the sign column when there are any signs in this
4035 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004036 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004038 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004040 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041# endif
4042
4043 /* Draw two cells with the sign value or blank. */
4044 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004045 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004046 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 n_extra = 2;
4048
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004049 if (row == startrow
4050#ifdef FEAT_DIFF
4051 + filler_lines && filler_todo <= 0
4052#endif
4053 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4056 SIGN_TEXT);
4057# ifdef FEAT_SIGN_ICONS
4058 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4059 SIGN_ICON);
4060 if (gui.in_use && icon_sign != 0)
4061 {
4062 /* Use the image in this position. */
4063 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004064 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065# ifdef FEAT_NETBEANS_INTG
4066 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004067 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004069 c_final = NUL;
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071# endif
4072 char_attr = icon_sign;
4073 }
4074 else
4075# endif
4076 if (text_sign != 0)
4077 {
4078 p_extra = sign_get_text(text_sign);
4079 if (p_extra != NULL)
4080 {
4081 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004082 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004083 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 char_attr = sign_get_attr(text_sign, FALSE);
4086 }
4087 }
4088 }
4089 }
4090#endif
4091
4092 if (draw_state == WL_NR - 1 && n_extra == 0)
4093 {
4094 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004095 /* Display the absolute or relative line number. After the
4096 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4097 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 && (row == startrow
4099#ifdef FEAT_DIFF
4100 + filler_lines
4101#endif
4102 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4103 {
4104 /* Draw the line number (empty space after wrapping). */
4105 if (row == startrow
4106#ifdef FEAT_DIFF
4107 + filler_lines
4108#endif
4109 )
4110 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004111 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004112 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004113
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004114 if (wp->w_p_nu && !wp->w_p_rnu)
4115 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004116 num = (long)lnum;
4117 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004118 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004119 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004120 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004121 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004122 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004123 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004124 num = lnum;
4125 fmt = "%-*ld ";
4126 }
4127 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004128
Bram Moolenaar700e7342013-01-30 12:31:36 +01004129 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004130 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 if (wp->w_skipcol > 0)
4132 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4133 *p_extra = '-';
4134#ifdef FEAT_RIGHTLEFT
4135 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004136 {
4137 char_u *p1, *p2;
4138 int t;
4139
4140 // like rl_mirror(), but keep the space at the end
4141 p2 = skiptowhite(extra) - 1;
4142 for (p1 = extra; p1 < p2; ++p1, --p2)
4143 {
4144 t = *p1;
4145 *p1 = *p2;
4146 *p2 = t;
4147 }
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#endif
4150 p_extra = extra;
4151 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004152 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004157 c_final = NUL;
4158 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004159 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004160 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004161#ifdef FEAT_SYN_HL
4162 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004163 * the current line differently.
4164 * TODO: Can we use CursorLine instead of CursorLineNr
4165 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004166 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004167 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004168 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171 }
4172
Bram Moolenaar597a4222014-06-25 14:39:50 +02004173#ifdef FEAT_LINEBREAK
4174 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4175 && n_extra == 0 && *p_sbr != NUL)
4176 /* draw indent after showbreak value */
4177 draw_state = WL_BRI;
4178 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4179 /* After the showbreak, draw the breakindent */
4180 draw_state = WL_BRI - 1;
4181
4182 /* draw 'breakindent': indent wrapped text accordingly */
4183 if (draw_state == WL_BRI - 1 && n_extra == 0)
4184 {
4185 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004186 /* if need_showbreak is set, breakindent also applies */
4187 if (wp->w_p_bri && n_extra == 0
4188 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004189# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004190 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004191# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004192 )
4193 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004194 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004195# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004196 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004197 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004198 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004199# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004200 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4201 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004202 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004203# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004204 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004205# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004206 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004207 c_extra = ' ';
4208 n_extra = get_breakindent_win(wp,
4209 ml_get_buf(wp->w_buffer, lnum, FALSE));
4210 /* Correct end of highlighted area for 'breakindent',
4211 * required when 'linebreak' is also set. */
4212 if (tocol == vcol)
4213 tocol += n_extra;
4214 }
4215 }
4216#endif
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4219 if (draw_state == WL_SBR - 1 && n_extra == 0)
4220 {
4221 draw_state = WL_SBR;
4222# ifdef FEAT_DIFF
4223 if (filler_todo > 0)
4224 {
4225 /* Draw "deleted" diff line(s). */
4226 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004229 c_final = NUL;
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004234 c_final = NUL;
4235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236# ifdef FEAT_RIGHTLEFT
4237 if (wp->w_p_rl)
4238 n_extra = col + 1;
4239 else
4240# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004241 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004242 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244# endif
4245# ifdef FEAT_LINEBREAK
4246 if (*p_sbr != NUL && need_showbreak)
4247 {
4248 /* Draw 'showbreak' at the start of each broken line. */
4249 p_extra = p_sbr;
4250 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004251 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004253 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004255 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 /* Correct end of highlighted area for 'showbreak',
4257 * required when 'linebreak' is also set. */
4258 if (tocol == vcol)
4259 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004260#ifdef FEAT_SYN_HL
4261 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004262 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004263 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004264 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267# endif
4268 }
4269#endif
4270
4271 if (draw_state == WL_LINE - 1 && n_extra == 0)
4272 {
4273 draw_state = WL_LINE;
4274 if (saved_n_extra)
4275 {
4276 /* Continue item from end of wrapped line. */
4277 n_extra = saved_n_extra;
4278 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004279 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 p_extra = saved_p_extra;
4281 char_attr = saved_char_attr;
4282 }
4283 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004284 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004288 // When still displaying '$' of change command, stop at cursor.
4289 // When only displaying the (relative) line number and that's done,
4290 // stop here.
4291 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004292 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293#ifdef FEAT_DIFF
4294 && filler_todo <= 0
4295#endif
4296 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004297 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004299 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004300 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004301 /* Pretend we have finished updating the window. Except when
4302 * 'cursorcolumn' is set. */
4303#ifdef FEAT_SYN_HL
4304 if (wp->w_p_cuc)
4305 row = wp->w_cline_row + wp->w_cline_height;
4306 else
4307#endif
4308 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 break;
4310 }
4311
Bram Moolenaar637532b2019-01-03 21:44:40 +01004312 if (draw_state == WL_LINE && (area_highlighting
4313#ifdef FEAT_SPELL
4314 || has_spell
4315#endif
4316 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
4318 /* handle Visual or match highlighting in this line */
4319 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4321 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004323 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004325 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 else if (area_attr != 0
4327 && (vcol == tocol
4328 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331#ifdef FEAT_SEARCH_EXTRA
4332 if (!n_extra)
4333 {
4334 /*
4335 * Check for start/end of search pattern match.
4336 * After end, check for start/end of next match.
4337 * When another match, have to check for start again.
4338 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004339 * Do this for 'search_hl' and the match list (ordered by
4340 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004342 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004343 cur = wp->w_match_head;
4344 shl_flag = FALSE;
4345 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004347 if (shl_flag == FALSE
4348 && ((cur != NULL
4349 && cur->priority > SEARCH_HL_PRIORITY)
4350 || cur == NULL))
4351 {
4352 shl = &search_hl;
4353 shl_flag = TRUE;
4354 }
4355 else
4356 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004357 if (cur != NULL)
4358 cur->pos.cur = 0;
4359 pos_inprogress = TRUE;
4360 while (shl->rm.regprog != NULL
4361 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004363 if (shl->startcol != MAXCOL
4364 && v >= (long)shl->startcol
4365 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004367 int tmp_col = v + MB_PTR2LEN(ptr);
4368
4369 if (shl->endcol < tmp_col)
4370 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004372#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004373 // Match with the "Conceal" group results in hiding
4374 // the match.
4375 if (cur != NULL
4376 && shl != &search_hl
4377 && syn_name2id((char_u *)"Conceal")
4378 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004379 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004380 has_match_conc =
4381 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004382 match_conc = cur->conceal_char;
4383 }
4384 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004385 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004388 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004391 next_search_hl(wp, shl, lnum, (colnr_T)v,
4392 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004393 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004394 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /* Need to get the line again, a multi-line regexp
4397 * may have made it invalid. */
4398 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4399 ptr = line + v;
4400
4401 if (shl->lnum == lnum)
4402 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004403 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004405 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004407 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004409 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
4411 /* highlight empty match, try again after
4412 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004414 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004415 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004417 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419
4420 /* Loop to check if the match starts at the
4421 * current position */
4422 continue;
4423 }
4424 }
4425 break;
4426 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004427 if (shl != &search_hl && cur != NULL)
4428 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004430
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004431 /* Use attributes from match with highest priority among
4432 * 'search_hl' and the match list. */
4433 search_attr = search_hl.attr_cur;
4434 cur = wp->w_match_head;
4435 shl_flag = FALSE;
4436 while (cur != NULL || shl_flag == FALSE)
4437 {
4438 if (shl_flag == FALSE
4439 && ((cur != NULL
4440 && cur->priority > SEARCH_HL_PRIORITY)
4441 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004442 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004443 shl = &search_hl;
4444 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004445 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004446 else
4447 shl = &cur->hl;
4448 if (shl->attr_cur != 0)
4449 search_attr = shl->attr_cur;
4450 if (shl != &search_hl && cur != NULL)
4451 cur = cur->next;
4452 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004453 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004454 if (*ptr == NUL && (did_line_attr >= 1
4455 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004456 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458#endif
4459
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004461 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004463 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4464 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004466 if (diff_hlf == HLF_TXD && ptr - line > change_end
4467 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004469 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004470 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004471 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004474
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004475#ifdef FEAT_TEXT_PROP
4476 if (text_props != NULL)
4477 {
4478 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004479 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004480
4481 // Check if any active property ends.
4482 for (pi = 0; pi < text_props_active; ++pi)
4483 {
4484 int tpi = text_prop_idxs[pi];
4485
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004486 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004487 + text_props[tpi].tp_len)
4488 {
4489 if (pi + 1 < text_props_active)
4490 mch_memmove(text_prop_idxs + pi,
4491 text_prop_idxs + pi + 1,
4492 sizeof(int)
4493 * (text_props_active - (pi + 1)));
4494 --text_props_active;
4495 --pi;
4496 }
4497 }
4498
4499 // Add any text property that starts in this column.
4500 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004501 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004502 text_prop_idxs[text_props_active++] = text_prop_next++;
4503
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004504 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004505 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004506 if (text_props_active > 0)
4507 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004508 // Sort the properties on priority and/or starting last.
4509 // Then combine the attributes, highest priority last.
4510 current_text_props = text_props;
4511 current_buf = wp->w_buffer;
4512 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4513 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004514
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004515 for (pi = 0; pi < text_props_active; ++pi)
4516 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004517 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004518 proptype_T *pt = text_prop_type_by_id(
4519 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004520
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004521 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004522 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004523 int pt_attr = syn_id2attr(pt->pt_hl_id);
4524
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004525 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004526 text_prop_attr =
4527 hl_combine_attr(text_prop_attr, pt_attr);
4528 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004529 }
4530 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004531 }
4532 }
4533#endif
4534
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004535 /* Decide which of the highlight attributes to use. */
4536 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004537#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004538 if (area_attr != 0)
4539 char_attr = hl_combine_attr(line_attr, area_attr);
4540 else if (search_attr != 0)
4541 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004542# ifdef FEAT_TEXT_PROP
4543 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004544 {
4545 char_attr = hl_combine_attr(
4546 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4547 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004548# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004549 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004550 || vcol < fromcol || vcol_prev < fromcol_prev
4551 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004552 // Use line_attr when not in the Visual or 'incsearch' area
4553 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004554 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004555#else
4556 if (area_attr != 0)
4557 char_attr = area_attr;
4558 else if (search_attr != 0)
4559 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004560#endif
4561 else
4562 {
4563 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004564#ifdef FEAT_TEXT_PROP
4565 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004566 {
4567 if (text_prop_combine)
4568 char_attr = hl_combine_attr(
4569 syntax_attr, text_prop_attr);
4570 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004571 char_attr = hl_combine_attr(
4572 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004573 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004574 else
4575#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004576#ifdef FEAT_SYN_HL
4577 if (has_syntax)
4578 char_attr = syntax_attr;
4579 else
4580#endif
4581 char_attr = 0;
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004584 if (char_attr == 0)
4585 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 /*
4588 * Get the next character to put on the screen.
4589 */
4590 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004591 * The "p_extra" points to the extra stuff that is inserted to
4592 * represent special characters (non-printable stuff) and other
4593 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004594 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004595 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4596 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4598 */
4599 if (n_extra > 0)
4600 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004601 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004603 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004605 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004608 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004609 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 else
4612 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 }
4614 else
4615 {
4616 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (has_mbyte)
4618 {
4619 mb_c = c;
4620 if (enc_utf8)
4621 {
4622 /* If the UTF-8 character is more than one byte:
4623 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004624 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 mb_utf8 = FALSE;
4626 if (mb_l > n_extra)
4627 mb_l = 1;
4628 else if (mb_l > 1)
4629 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004632 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634 }
4635 else
4636 {
4637 /* if this is a DBCS character, put it in "mb_c" */
4638 mb_l = MB_BYTE2LEN(c);
4639 if (mb_l >= n_extra)
4640 mb_l = 1;
4641 else if (mb_l > 1)
4642 mb_c = (c << 8) + p_extra[1];
4643 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004644 if (mb_l == 0) /* at the NUL at end-of-line */
4645 mb_l = 1;
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 /* If a double-width char doesn't fit display a '>' in the
4648 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004649 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650# ifdef FEAT_RIGHTLEFT
4651 wp->w_p_rl ? (col <= 0) :
4652# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004653 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 && (*mb_char2cells)(mb_c) == 2)
4655 {
4656 c = '>';
4657 mb_c = c;
4658 mb_l = 1;
4659 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004660 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 /* put the pointer back to output the double-width
4662 * character at the start of the next line. */
4663 ++n_extra;
4664 --p_extra;
4665 }
4666 else
4667 {
4668 n_extra -= mb_l - 1;
4669 p_extra += mb_l - 1;
4670 }
4671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 ++p_extra;
4673 }
4674 --n_extra;
4675 }
4676 else
4677 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004678#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004679 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004680#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004681
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004682 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004683 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 /*
4685 * Get a character from the line itself.
4686 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004687 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004688#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004689 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (has_mbyte)
4692 {
4693 mb_c = c;
4694 if (enc_utf8)
4695 {
4696 /* If the UTF-8 character is more than one byte: Decode it
4697 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004698 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 mb_utf8 = FALSE;
4700 if (mb_l > 1)
4701 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004702 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /* Overlong encoded ASCII or ASCII with composing char
4704 * is displayed normally, except a NUL. */
4705 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004706 {
4707 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004708#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004709 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004710#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004713
4714 /* At start of the line we can have a composing char.
4715 * Draw it as a space with a composing char. */
4716 if (utf_iscomposing(mb_c))
4717 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004718 int i;
4719
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004720 for (i = Screen_mco - 1; i > 0; --i)
4721 u8cc[i] = u8cc[i - 1];
4722 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004723 mb_c = ' ';
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726
4727 if ((mb_l == 1 && c >= 0x80)
4728 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004729 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 /*
4732 * Illegal UTF-8 byte: display as <xx>.
4733 * Non-BMP character : display as ? or fullwidth ?.
4734 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004735 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004736# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004737 if (wp->w_p_rl) /* reverse */
4738 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 p_extra = extra;
4741 c = *p_extra;
4742 mb_c = mb_ptr2char_adv(&p_extra);
4743 mb_utf8 = (c >= 0x80);
4744 n_extra = (int)STRLEN(p_extra);
4745 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004746 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (area_attr == 0 && search_attr == 0)
4748 {
4749 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004750 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 saved_attr2 = char_attr; /* save current attr */
4752 }
4753 }
4754 else if (mb_l == 0) /* at the NUL at end-of-line */
4755 mb_l = 1;
4756#ifdef FEAT_ARABIC
4757 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4758 {
4759 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004760 int pc, pc1, nc;
4761 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 /* The idea of what is the previous and next
4764 * character depends on 'rightleft'. */
4765 if (wp->w_p_rl)
4766 {
4767 pc = prev_c;
4768 pc1 = prev_c1;
4769 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004770 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 else
4773 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004774 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004776 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 prev_c = mb_c;
4779
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004780 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782 else
4783 prev_c = mb_c;
4784#endif
4785 }
4786 else /* enc_dbcs */
4787 {
4788 mb_l = MB_BYTE2LEN(c);
4789 if (mb_l == 0) /* at the NUL at end-of-line */
4790 mb_l = 1;
4791 else if (mb_l > 1)
4792 {
4793 /* We assume a second byte below 32 is illegal.
4794 * Hopefully this is OK for all double-byte encodings!
4795 */
4796 if (ptr[1] >= 32)
4797 mb_c = (c << 8) + ptr[1];
4798 else
4799 {
4800 if (ptr[1] == NUL)
4801 {
4802 /* head byte at end of line */
4803 mb_l = 1;
4804 transchar_nonprint(extra, c);
4805 }
4806 else
4807 {
4808 /* illegal tail byte */
4809 mb_l = 2;
4810 STRCPY(extra, "XX");
4811 }
4812 p_extra = extra;
4813 n_extra = (int)STRLEN(extra) - 1;
4814 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004815 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 c = *p_extra++;
4817 if (area_attr == 0 && search_attr == 0)
4818 {
4819 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004820 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 saved_attr2 = char_attr; /* save current attr */
4822 }
4823 mb_c = c;
4824 }
4825 }
4826 }
4827 /* If a double-width char doesn't fit display a '>' in the
4828 * last column; the character is displayed at the start of the
4829 * next line. */
4830 if ((
4831# ifdef FEAT_RIGHTLEFT
4832 wp->w_p_rl ? (col <= 0) :
4833# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004834 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 && (*mb_char2cells)(mb_c) == 2)
4836 {
4837 c = '>';
4838 mb_c = c;
4839 mb_utf8 = FALSE;
4840 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004841 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004842 // Put pointer back so that the character will be
4843 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004845#ifdef FEAT_CONCEAL
4846 did_decrement_ptr = TRUE;
4847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 else if (*ptr != NUL)
4850 ptr += mb_l - 1;
4851
4852 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004853 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004854 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004855 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004858 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004859 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 c = ' ';
4861 if (area_attr == 0 && search_attr == 0)
4862 {
4863 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004864 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 saved_attr2 = char_attr; /* save current attr */
4866 }
4867 mb_c = c;
4868 mb_utf8 = FALSE;
4869 mb_l = 1;
4870 }
4871
4872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 ++ptr;
4874
4875 if (extra_check)
4876 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004877#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004878 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004879#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004880
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004881#ifdef FEAT_TERMINAL
4882 if (get_term_attr)
4883 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004884 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004885
4886 if (!attr_pri)
4887 char_attr = syntax_attr;
4888 else
4889 char_attr = hl_combine_attr(syntax_attr, char_attr);
4890 }
4891#endif
4892
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004893#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004894 // Get syntax attribute, unless still at the start of the line
4895 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004896 v = (long)(ptr - line);
4897 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
4899 /* Get the syntax attribute for the character. If there
4900 * is an error, disable syntax highlighting. */
4901 save_did_emsg = did_emsg;
4902 did_emsg = FALSE;
4903
Bram Moolenaar217ad922005-03-20 22:37:15 +00004904 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004905# ifdef FEAT_SPELL
4906 has_spell ? &can_spell :
4907# endif
4908 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004911 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004912 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004913 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004914 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else
4917 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004918
4919 // combine syntax attribute with 'wincolor'
4920 if (win_attr != 0)
4921 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4922
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004923#ifdef SYN_TIME_LIMIT
4924 if (wp->w_s->b_syn_slow)
4925 has_syntax = FALSE;
4926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 /* Need to get the line again, a multi-line regexp may
4929 * have made it invalid. */
4930 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4931 ptr = line + v;
4932
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004933# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004934 // Text properties overrule syntax highlighting or combine.
4935 if (text_prop_attr == 0 || text_prop_combine)
4936# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004937 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004938 int comb_attr = syntax_attr;
4939# ifdef FEAT_TEXT_PROP
4940 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4941# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004942 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004943 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004944 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004945 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004946 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004947# ifdef FEAT_CONCEAL
4948 /* no concealing past the end of the line, it interferes
4949 * with line highlighting */
4950 if (c == NUL)
4951 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004952 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004953 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004956#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004957
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004958#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004959 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004960 * Only do this when there is no syntax highlighting, the
4961 * @Spell cluster is not used or the current syntax item
4962 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004963 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004964 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004965 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004966 if (c != 0 && (
4967# ifdef FEAT_SYN_HL
4968 !has_syntax ||
4969# endif
4970 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004971 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004972 char_u *prev_ptr, *p;
4973 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004974 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004975 if (has_mbyte)
4976 {
4977 prev_ptr = ptr - mb_l;
4978 v -= mb_l - 1;
4979 }
4980 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004981 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004982
4983 /* Use nextline[] if possible, it has the start of the
4984 * next line concatenated. */
4985 if ((prev_ptr - line) - nextlinecol >= 0)
4986 p = nextline + (prev_ptr - line) - nextlinecol;
4987 else
4988 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004989 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004990 len = spell_check(wp, p, &spell_hlf, &cap_col,
4991 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004992 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004993
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004994 /* In Insert mode only highlight a word that
4995 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004996 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004997 && (State & INSERT) != 0
4998 && wp->w_cursor.lnum == lnum
4999 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005000 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005001 && wp->w_cursor.col < (colnr_T)word_end)
5002 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005003 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005004 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005005 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005006
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005007 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005008 && (p - nextline) + len > nextline_idx)
5009 {
5010 /* Remember that the good word continues at the
5011 * start of the next line. */
5012 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005013 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005014 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005015
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005016 /* Turn index into actual attributes. */
5017 if (spell_hlf != HLF_COUNT)
5018 spell_attr = highlight_attr[spell_hlf];
5019
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005020 if (cap_col > 0)
5021 {
5022 if (p != prev_ptr
5023 && (p - nextline) + cap_col >= nextline_idx)
5024 {
5025 /* Remember that the word in the next line
5026 * must start with a capital. */
5027 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005028 cap_col = (int)((p - nextline) + cap_col
5029 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005030 }
5031 else
5032 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005033 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005034 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005035 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005036 }
5037 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005038 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005039 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005040 char_attr = hl_combine_attr(char_attr, spell_attr);
5041 else
5042 char_attr = hl_combine_attr(spell_attr, char_attr);
5043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#endif
5045#ifdef FEAT_LINEBREAK
5046 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005047 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005049 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005050 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005052 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005053 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005054
Bram Moolenaar597a4222014-06-25 14:39:50 +02005055 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005056 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005057 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005058 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005059# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005060 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005061 wp->w_buffer->b_p_vts_array) - 1;
5062# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005063 n_extra = (int)wp->w_buffer->b_p_ts
5064 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005065# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005066
Bram Moolenaar4df70292015-03-21 14:20:16 +01005067 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005068 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005069 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005070 {
5071#ifdef FEAT_CONCEAL
5072 if (c == TAB)
5073 /* See "Tab alignment" below. */
5074 FIX_FOR_BOGUSCOLS;
5075#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005076 if (!wp->w_p_list)
5077 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080#endif
5081
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005082 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5083 // But not when the character is followed by a composing
5084 // character (use mb_l to check that).
5085 if (wp->w_p_list
5086 && ((((c == 160 && mb_l == 1)
5087 || (mb_utf8
5088 && ((mb_c == 160 && mb_l == 2)
5089 || (mb_c == 0x202f && mb_l == 3))))
5090 && lcs_nbsp)
5091 || (c == ' '
5092 && mb_l == 1
5093 && lcs_space
5094 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005095 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005096 c = (c == ' ') ? lcs_space : lcs_nbsp;
5097 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005098 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005099 n_attr = 1;
5100 extra_attr = HL_ATTR(HLF_8);
5101 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005102 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005103 mb_c = c;
5104 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005105 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005106 mb_utf8 = TRUE;
5107 u8cc[0] = 0;
5108 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005109 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005110 else
5111 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005112 }
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5115 {
5116 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005117 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005120 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 saved_attr2 = char_attr; /* save current attr */
5122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005124 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
5126 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005127 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005128 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 else
5131 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 }
5134
5135 /*
5136 * Handling of non-printable characters.
5137 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005138 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 /*
5141 * when getting a character from the file, we may have to
5142 * turn it into something else on the way to putting it
5143 * into "ScreenLines".
5144 */
5145 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5146 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005147 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005148 long vcol_adjusted = vcol; /* removed showbreak length */
5149#ifdef FEAT_LINEBREAK
5150 /* only adjust the tab_len, when at the first column
5151 * after the showbreak value was drawn */
5152 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5153 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005156#ifdef FEAT_VARTABS
5157 tab_len = tabstop_padding(vcol_adjusted,
5158 wp->w_buffer->b_p_ts,
5159 wp->w_buffer->b_p_vts_array) - 1;
5160#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005161 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005162 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5163#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005164
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005165#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005166 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005167#endif
5168 /* tab amount depends on current column */
5169 n_extra = tab_len;
5170#ifdef FEAT_LINEBREAK
5171 else
5172 {
5173 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005174 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005175 int i;
5176 int saved_nextra = n_extra;
5177
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005178#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005179 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005180 /* there are characters to conceal */
5181 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005182 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5183 */
5184 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5185 && n_extra > tab_len)
5186 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005187#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005188
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005189 /* if n_extra > 0, it gives the number of chars, to
5190 * use for a tab, else we need to calculate the width
5191 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005192 len = (tab_len * mb_char2len(lcs_tab2));
5193 if (n_extra > 0)
5194 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005195 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005196 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005197 vim_memset(p, ' ', len);
5198 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005199 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005200 p_extra_free = p;
5201 for (i = 0; i < tab_len; i++)
5202 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005203 if (*p == NUL)
5204 {
5205 tab_len = i;
5206 break;
5207 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005208 mb_char2bytes(lcs_tab2, p);
5209 p += mb_char2len(lcs_tab2);
5210 n_extra += mb_char2len(lcs_tab2)
5211 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005212 }
5213 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005214#ifdef FEAT_CONCEAL
5215 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5216 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005217 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005218 n_extra -= vcol_off;
5219#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005220 }
5221#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005222#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005223 {
5224 int vc_saved = vcol_off;
5225
5226 /* Tab alignment should be identical regardless of
5227 * 'conceallevel' value. So tab compensates of all
5228 * previous concealed characters, and thus resets
5229 * vcol_off and boguscols accumulated so far in the
5230 * line. Note that the tab can be longer than
5231 * 'tabstop' when there are concealed characters. */
5232 FIX_FOR_BOGUSCOLS;
5233
5234 /* Make sure, the highlighting for the tab char will be
5235 * correctly set further below (effectively reverts the
5236 * FIX_FOR_BOGSUCOLS macro */
5237 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005238 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005239 tab_len += vc_saved;
5240 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 if (wp->w_p_list)
5244 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005245 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005246#ifdef FEAT_LINEBREAK
5247 if (wp->w_p_lbr)
5248 c_extra = NUL; /* using p_extra from above */
5249 else
5250#endif
5251 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005252 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005253 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005254 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005257 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
5259 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005260 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005261 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 else
5265 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005266 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 c_extra = ' ';
5268 c = ' ';
5269 }
5270 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005271 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005272 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005273 || ((fromcol >= 0 || fromcol_prev >= 0)
5274 && tocol > vcol
5275 && VIsual_mode != Ctrl_V
5276 && (
5277# ifdef FEAT_RIGHTLEFT
5278 wp->w_p_rl ? (col >= 0) :
5279# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005280 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005281 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005282 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005283 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005284 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005286 /* Display a '$' after the line or highlight an extra
5287 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5289 /* For a diff line the highlighting continues after the
5290 * "$". */
5291 if (
5292# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005293 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294# ifdef LINE_ATTR
5295 &&
5296# endif
5297# endif
5298# ifdef LINE_ATTR
5299 line_attr == 0
5300# endif
5301 )
5302#endif
5303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 /* In virtualedit, visual selections may extend
5305 * beyond end of line. */
5306 if (area_highlighting && virtual_active()
5307 && tocol != MAXCOL && vcol < tocol)
5308 n_extra = 0;
5309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
5311 p_extra = at_end_str;
5312 n_extra = 1;
5313 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005314 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005317 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005318 c = lcs_eol;
5319 else
5320 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 lcs_eol_one = -1;
5322 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005323 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005325 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 n_attr = 1;
5327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005329 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005332 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005333 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 else
5336 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 else if (c != NUL)
5339 {
5340 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005341 if (n_extra == 0)
5342 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343#ifdef FEAT_RIGHTLEFT
5344 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5345 rl_mirror(p_extra); /* reverse "<12>" */
5346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005348 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005349#ifdef FEAT_LINEBREAK
5350 if (wp->w_p_lbr)
5351 {
5352 char_u *p;
5353
5354 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005355 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005356 vim_memset(p, ' ', n_extra);
5357 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5358 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005359 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005360 p_extra_free = p_extra = p;
5361 }
5362 else
5363#endif
5364 {
5365 n_extra = byte2cells(c) - 1;
5366 c = *p_extra++;
5367 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005368 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
5370 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005371 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 saved_attr2 = char_attr; /* save current attr */
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 else if (VIsual_active
5377 && (VIsual_mode == Ctrl_V
5378 || VIsual_mode == 'v')
5379 && virtual_active()
5380 && tocol != MAXCOL
5381 && vcol < tocol
5382 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005383#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005385#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005386 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
5388 c = ' ';
5389 --ptr; /* put it back at the NUL */
5390 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005391#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 else if ((
5393# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005394 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005396# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005397 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 ) && (
5401# ifdef FEAT_RIGHTLEFT
5402 wp->w_p_rl ? (col >= 0) :
5403# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005404 (col
5405# ifdef FEAT_CONCEAL
5406 - boguscols
5407# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005408 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 /* Highlight until the right side of the window */
5411 c = ' ';
5412 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005413
5414 /* Remember we do the char for line highlighting. */
5415 ++did_line_attr;
5416
5417 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005418 if (line_attr != 0 && char_attr == search_attr
5419 && (did_line_attr > 1
5420 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005421 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422# ifdef FEAT_DIFF
5423 if (diff_hlf == HLF_TXD)
5424 {
5425 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005426 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005427 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005428 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005429 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5430 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005431 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005435# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005436 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005437 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005438 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005439 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5440 char_attr = hl_combine_attr(char_attr,
5441 HL_ATTR(HLF_CUL));
5442 }
5443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445#endif
5446 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005447
5448#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005449 if ( wp->w_p_cole > 0
5450 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005451 conceal_cursor_line(wp))
5452 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005453 && !(lnum_in_visual_area
5454 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005455 {
5456 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005457 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005458 && (syn_get_sub_char() != NUL || match_conc
5459 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005460 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005461 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005462 /* First time at this concealed item: display one
5463 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005464 if (match_conc)
5465 c = match_conc;
5466 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005467 c = syn_get_sub_char();
5468 else if (lcs_conceal != NUL)
5469 c = lcs_conceal;
5470 else
5471 c = ' ';
5472
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005473 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005474
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005475 if (n_extra > 0)
5476 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005477 vcol += n_extra;
5478 if (wp->w_p_wrap && n_extra > 0)
5479 {
5480# ifdef FEAT_RIGHTLEFT
5481 if (wp->w_p_rl)
5482 {
5483 col -= n_extra;
5484 boguscols -= n_extra;
5485 }
5486 else
5487# endif
5488 {
5489 boguscols += n_extra;
5490 col += n_extra;
5491 }
5492 }
5493 n_extra = 0;
5494 n_attr = 0;
5495 }
5496 else if (n_skip == 0)
5497 {
5498 is_concealing = TRUE;
5499 n_skip = 1;
5500 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005501 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005502 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005503 {
5504 mb_utf8 = TRUE;
5505 u8cc[0] = 0;
5506 c = 0xc0;
5507 }
5508 else
5509 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005510 }
5511 else
5512 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005513 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005514 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005515 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005516
5517 if (n_skip > 0 && did_decrement_ptr)
5518 // not showing the '>', put pointer back to avoid getting stuck
5519 ++ptr;
5520
5521#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
5523
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005524#ifdef FEAT_CONCEAL
5525 /* In the cursor line and we may be concealing characters: correct
5526 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005527 if (!did_wcol && draw_state == WL_LINE
5528 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005529 && conceal_cursor_line(wp)
5530 && (int)wp->w_virtcol <= vcol + n_skip)
5531 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005532# ifdef FEAT_RIGHTLEFT
5533 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005534 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005535 else
5536# endif
5537 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005538 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005539 did_wcol = TRUE;
5540 }
5541#endif
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 /* Don't override visual selection highlighting. */
5544 if (n_attr > 0
5545 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005546 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 char_attr = extra_attr;
5548
Bram Moolenaar81695252004-12-29 20:58:21 +00005549#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 /* XIM don't send preedit_start and preedit_end, but they send
5551 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5552 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005553 if (p_imst == IM_ON_THE_SPOT
5554 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005555 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 && (State & INSERT)
5557 && !p_imdisable
5558 && im_is_preediting()
5559 && draw_state == WL_LINE)
5560 {
5561 colnr_T tcol;
5562
5563 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005564 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 else
5566 tcol = preedit_end_col;
5567 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5568 {
5569 if (feedback_old_attr < 0)
5570 {
5571 feedback_col = 0;
5572 feedback_old_attr = char_attr;
5573 }
5574 char_attr = im_get_feedback_attr(feedback_col);
5575 if (char_attr < 0)
5576 char_attr = feedback_old_attr;
5577 feedback_col++;
5578 }
5579 else if (feedback_old_attr >= 0)
5580 {
5581 char_attr = feedback_old_attr;
5582 feedback_old_attr = -1;
5583 feedback_col = 0;
5584 }
5585 }
5586#endif
5587 /*
5588 * Handle the case where we are in column 0 but not on the first
5589 * character of the line and the user wants us to show us a
5590 * special character (via 'listchars' option "precedes:<char>".
5591 */
5592 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005593 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5595#ifdef FEAT_DIFF
5596 && filler_todo <= 0
5597#endif
5598 && draw_state > WL_NR
5599 && c != NUL)
5600 {
5601 c = lcs_prec;
5602 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005603 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5604 {
5605 /* Double-width character being overwritten by the "precedes"
5606 * character, need to fill up half the character. */
5607 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005608 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005609 n_extra = 1;
5610 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005611 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005614 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
5616 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005617 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005618 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620 else
5621 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005622 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
5624 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005625 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 n_attr3 = 1;
5627 }
5628 }
5629
5630 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005631 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005633 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005634#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005635 || did_line_attr == 1
5636#endif
5637 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005639#ifdef FEAT_SEARCH_EXTRA
5640 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005641
5642 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005643 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005644 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005645#endif
5646
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005647 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 * highlight match at end of line. If it's beyond the last
5649 * char on the screen, just overwrite that one (tricky!) Not
5650 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005651#ifdef FEAT_SEARCH_EXTRA
5652 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005653 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005654 prevcol_hl_flag = TRUE;
5655 else
5656 {
5657 cur = wp->w_match_head;
5658 while (cur != NULL)
5659 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005660 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005661 {
5662 prevcol_hl_flag = TRUE;
5663 break;
5664 }
5665 cur = cur->next;
5666 }
5667 }
5668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005670 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005671 && (VIsual_mode != Ctrl_V
5672 || lnum == VIsual.lnum
5673 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005674 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675#ifdef FEAT_SEARCH_EXTRA
5676 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005677 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005678# ifdef FEAT_SYN_HL
5679 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5680 && !(wp == curwin && VIsual_active))
5681# endif
5682# ifdef FEAT_DIFF
5683 && diff_hlf == (hlf_T)0
5684# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005685# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005686 && did_line_attr <= 1
5687# endif
5688 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689#endif
5690 ))
5691 {
5692 int n = 0;
5693
5694#ifdef FEAT_RIGHTLEFT
5695 if (wp->w_p_rl)
5696 {
5697 if (col < 0)
5698 n = 1;
5699 }
5700 else
5701#endif
5702 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005703 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 n = -1;
5705 }
5706 if (n != 0)
5707 {
5708 /* At the window boundary, highlight the last character
5709 * instead (better than nothing). */
5710 off += n;
5711 col += n;
5712 }
5713 else
5714 {
5715 /* Add a blank character to highlight. */
5716 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (enc_utf8)
5718 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720#ifdef FEAT_SEARCH_EXTRA
5721 if (area_attr == 0)
5722 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005723 /* Use attributes from match with highest priority among
5724 * 'search_hl' and the match list. */
5725 char_attr = search_hl.attr;
5726 cur = wp->w_match_head;
5727 shl_flag = FALSE;
5728 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005729 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005730 if (shl_flag == FALSE
5731 && ((cur != NULL
5732 && cur->priority > SEARCH_HL_PRIORITY)
5733 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005734 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005735 shl = &search_hl;
5736 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005737 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005738 else
5739 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005740 if ((ptr - line) - 1 == (long)shl->startcol
5741 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005742 char_attr = shl->attr;
5743 if (shl != &search_hl && cur != NULL)
5744 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747#endif
5748 ScreenAttrs[off] = char_attr;
5749#ifdef FEAT_RIGHTLEFT
5750 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005753 --off;
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 else
5756#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005759 ++off;
5760 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005761 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005762#ifdef FEAT_SYN_HL
5763 eol_hl_off = 1;
5764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
Bram Moolenaar91170f82006-05-05 21:15:17 +00005768 /*
5769 * At end of the text line.
5770 */
5771 if (c == NUL)
5772 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005773#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005774 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005775 if (wp->w_p_wrap)
5776 v = wp->w_skipcol;
5777 else
5778 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005779
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005780 /* check if line ends before left margin */
5781 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005782 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005783#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005784 // Get rid of the boguscols now, we want to draw until the right
5785 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005786 col -= boguscols;
5787 boguscols = 0;
5788#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005789
5790 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005791 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005792
5793 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005794 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5795 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005796 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005797 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005798 || draw_color_col
5799 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005800# ifdef FEAT_RIGHTLEFT
5801 && !wp->w_p_rl
5802# endif
5803 )
5804 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005805 int rightmost_vcol = 0;
5806 int i;
5807
5808 if (wp->w_p_cuc)
5809 rightmost_vcol = wp->w_virtcol;
5810 if (draw_color_col)
5811 /* determine rightmost colorcolumn to possibly draw */
5812 for (i = 0; color_cols[i] >= 0; ++i)
5813 if (rightmost_vcol < color_cols[i])
5814 rightmost_vcol = color_cols[i];
5815
Bram Moolenaar02631462017-09-22 15:20:32 +02005816 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005817 {
5818 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005819 if (enc_utf8)
5820 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005821 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005822 if (draw_color_col)
5823 draw_color_col = advance_color_col(VCOL_HLC,
5824 &color_cols);
5825
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005826 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005827 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005828 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005829 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005830 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005831 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005832
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005833 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005834 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005835
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005836 ++vcol;
5837 }
5838 }
5839#endif
5840
Bram Moolenaar53f81742017-09-22 14:35:51 +02005841 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005842 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 row++;
5844
5845 /*
5846 * Update w_cline_height and w_cline_folded if the cursor line was
5847 * updated (saves a call to plines() later).
5848 */
5849 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5850 {
5851 curwin->w_cline_row = startrow;
5852 curwin->w_cline_height = row - startrow;
5853#ifdef FEAT_FOLDING
5854 curwin->w_cline_folded = FALSE;
5855#endif
5856 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5857 }
5858
5859 break;
5860 }
5861
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005862 // Show "extends" character from 'listchars' if beyond the line end and
5863 // 'list' is set.
5864 if (lcs_ext != NUL
5865 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 && !wp->w_p_wrap
5867#ifdef FEAT_DIFF
5868 && filler_todo <= 0
5869#endif
5870 && (
5871#ifdef FEAT_RIGHTLEFT
5872 wp->w_p_rl ? col == 0 :
5873#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005874 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005876 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5878 {
5879 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005880 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005882 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
5884 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005885 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005886 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
5888 else
5889 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005892#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005893 /* advance to the next 'colorcolumn' */
5894 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005895 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005896
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005897 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005898 * highlight the cursor position itself.
5899 * Also highlight the 'colorcolumn' if it is different than
5900 * 'cursorcolumn' */
5901 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005902 if (draw_state == WL_LINE && !lnum_in_visual_area
5903 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005904 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005905 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005906 && lnum != wp->w_cursor.lnum)
5907 {
5908 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005909 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005910 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005911 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005912 {
5913 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005914 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005915 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005916 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 /*
5920 * Store character to be displayed.
5921 * Skip characters that are left of the screen for 'nowrap'.
5922 */
5923 vcol_prev = vcol;
5924 if (draw_state < WL_LINE || n_skip <= 0)
5925 {
5926 /*
5927 * Store the character.
5928 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005929#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5931 {
5932 /* A double-wide character is: put first halve in left cell. */
5933 --off;
5934 --col;
5935 }
5936#endif
5937 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005939 {
5940 if ((mb_c & 0xff00) == 0x8e00)
5941 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 else if (enc_utf8)
5945 {
5946 if (mb_utf8)
5947 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005948 int i;
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005951 if ((c & 0xff) == 0)
5952 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005953 for (i = 0; i < Screen_mco; ++i)
5954 {
5955 ScreenLinesC[i][off] = u8cc[i];
5956 if (u8cc[i] == 0)
5957 break;
5958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 }
5960 else
5961 ScreenLinesUC[off] = 0;
5962 }
5963 if (multi_attr)
5964 {
5965 ScreenAttrs[off] = multi_attr;
5966 multi_attr = 0;
5967 }
5968 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 ScreenAttrs[off] = char_attr;
5970
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5972 {
5973 /* Need to fill two screen columns. */
5974 ++off;
5975 ++col;
5976 if (enc_utf8)
5977 /* UTF-8: Put a 0 in the second screen char. */
5978 ScreenLines[off] = 0;
5979 else
5980 /* DBCS: Put second byte in the second screen char. */
5981 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005982 if (draw_state > WL_NR
5983#ifdef FEAT_DIFF
5984 && filler_todo <= 0
5985#endif
5986 )
5987 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 /* When "tocol" is halfway a character, set it to the end of
5989 * the character, otherwise highlighting won't stop. */
5990 if (tocol == vcol)
5991 ++tocol;
5992#ifdef FEAT_RIGHTLEFT
5993 if (wp->w_p_rl)
5994 {
5995 /* now it's time to backup one cell */
5996 --off;
5997 --col;
5998 }
5999#endif
6000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001#ifdef FEAT_RIGHTLEFT
6002 if (wp->w_p_rl)
6003 {
6004 --off;
6005 --col;
6006 }
6007 else
6008#endif
6009 {
6010 ++off;
6011 ++col;
6012 }
6013 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006014#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006015 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006016 {
6017 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006018 ++vcol_off;
6019 if (n_extra > 0)
6020 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006021 if (wp->w_p_wrap)
6022 {
6023 /*
6024 * Special voodoo required if 'wrap' is on.
6025 *
6026 * Advance the column indicator to force the line
6027 * drawing to wrap early. This will make the line
6028 * take up the same screen space when parts are concealed,
6029 * so that cursor line computations aren't messed up.
6030 *
6031 * To avoid the fictitious advance of 'col' causing
6032 * trailing junk to be written out of the screen line
6033 * we are building, 'boguscols' keeps track of the number
6034 * of bad columns we have advanced.
6035 */
6036 if (n_extra > 0)
6037 {
6038 vcol += n_extra;
6039# ifdef FEAT_RIGHTLEFT
6040 if (wp->w_p_rl)
6041 {
6042 col -= n_extra;
6043 boguscols -= n_extra;
6044 }
6045 else
6046# endif
6047 {
6048 col += n_extra;
6049 boguscols += n_extra;
6050 }
6051 n_extra = 0;
6052 n_attr = 0;
6053 }
6054
6055
Bram Moolenaar860cae12010-06-05 23:22:07 +02006056 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6057 {
6058 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006059# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006060 if (wp->w_p_rl)
6061 {
6062 --boguscols;
6063 --col;
6064 }
6065 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006066# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006067 {
6068 ++boguscols;
6069 ++col;
6070 }
6071 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006072
6073# ifdef FEAT_RIGHTLEFT
6074 if (wp->w_p_rl)
6075 {
6076 --boguscols;
6077 --col;
6078 }
6079 else
6080# endif
6081 {
6082 ++boguscols;
6083 ++col;
6084 }
6085 }
6086 else
6087 {
6088 if (n_extra > 0)
6089 {
6090 vcol += n_extra;
6091 n_extra = 0;
6092 n_attr = 0;
6093 }
6094 }
6095
6096 }
6097#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 else
6099 --n_skip;
6100
Bram Moolenaar64486672010-05-16 15:46:46 +02006101 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6102 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006103 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#ifdef FEAT_DIFF
6105 && filler_todo <= 0
6106#endif
6107 )
6108 ++vcol;
6109
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006110#ifdef FEAT_SYN_HL
6111 if (vcol_save_attr >= 0)
6112 char_attr = vcol_save_attr;
6113#endif
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* restore attributes after "predeces" in 'listchars' */
6116 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6117 char_attr = saved_attr3;
6118
6119 /* restore attributes after last 'listchars' or 'number' char */
6120 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6121 char_attr = saved_attr2;
6122
6123 /*
6124 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006125 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 */
6127 if ((
6128#ifdef FEAT_RIGHTLEFT
6129 wp->w_p_rl ? (col < 0) :
6130#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006131 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 && (*ptr != NUL
6133#ifdef FEAT_DIFF
6134 || filler_todo > 0
6135#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006136 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6138 )
6139 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006140#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006141 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006142 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006143 boguscols = 0;
6144#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006145 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006146 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ++row;
6149 ++screen_row;
6150
6151 /* When not wrapping and finished diff lines, or when displayed
6152 * '$' and highlighting until last column, break here. */
6153 if ((!wp->w_p_wrap
6154#ifdef FEAT_DIFF
6155 && filler_todo <= 0
6156#endif
6157 ) || lcs_eol_one == -1)
6158 break;
6159
6160 /* When the window is too narrow draw all "@" lines. */
6161 if (draw_state != WL_LINE
6162#ifdef FEAT_DIFF
6163 && filler_todo <= 0
6164#endif
6165 )
6166 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006167 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 row = endrow;
6170 }
6171
6172 /* When line got too long for screen break here. */
6173 if (row == endrow)
6174 {
6175 ++row;
6176 break;
6177 }
6178
6179 if (screen_cur_row == screen_row - 1
6180#ifdef FEAT_DIFF
6181 && filler_todo <= 0
6182#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006183 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
6185 /* Remember that the line wraps, used for modeless copy. */
6186 LineWraps[screen_row - 1] = TRUE;
6187
6188 /*
6189 * Special trick to make copy/paste of wrapped lines work with
6190 * xterm/screen: write an extra character beyond the end of
6191 * the line. This will work with all terminal types
6192 * (regardless of the xn,am settings).
6193 * Only do this on a fast tty.
6194 * Only do this if the cursor is on the current line
6195 * (something has been written in it).
6196 * Don't do this for the GUI.
6197 * Don't do this for double-width characters.
6198 * Don't do this for a window not at the right screen border.
6199 */
6200 if (p_tf
6201#ifdef FEAT_GUI
6202 && !gui.in_use
6203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006205 && ((*mb_off2cells)(LineOffset[screen_row],
6206 LineOffset[screen_row] + screen_Columns)
6207 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006209 + (int)Columns - 2,
6210 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006211 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
6213 /* First make sure we are at the end of the screen line,
6214 * then output the same character again to let the
6215 * terminal know about the wrap. If the terminal doesn't
6216 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006217 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 screen_char(LineOffset[screen_row - 1]
6219 + (unsigned)Columns - 1,
6220 screen_row - 1, (int)(Columns - 1));
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 /* When there is a multi-byte character, just output a
6223 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006224 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6225 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 out_char(' ');
6227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 out_char(ScreenLines[LineOffset[screen_row - 1]
6229 + (Columns - 1)]);
6230 /* force a redraw of the first char on the next line */
6231 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6232 screen_start(); /* don't know where cursor is now */
6233 }
6234 }
6235
6236 col = 0;
6237 off = (unsigned)(current_ScreenLine - ScreenLines);
6238#ifdef FEAT_RIGHTLEFT
6239 if (wp->w_p_rl)
6240 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006241 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 off += col;
6243 }
6244#endif
6245
6246 /* reset the drawing state for the start of a wrapped line */
6247 draw_state = WL_START;
6248 saved_n_extra = n_extra;
6249 saved_p_extra = p_extra;
6250 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006251 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 saved_char_attr = char_attr;
6253 n_extra = 0;
6254 lcs_prec_todo = lcs_prec;
6255#ifdef FEAT_LINEBREAK
6256# ifdef FEAT_DIFF
6257 if (filler_todo <= 0)
6258# endif
6259 need_showbreak = TRUE;
6260#endif
6261#ifdef FEAT_DIFF
6262 --filler_todo;
6263 /* When the filler lines are actually below the last line of the
6264 * file, don't draw the line itself, break here. */
6265 if (filler_todo == 0 && wp->w_botfill)
6266 break;
6267#endif
6268 }
6269
6270 } /* for every character in the line */
6271
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006272#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006273 /* After an empty line check first word for capital. */
6274 if (*skipwhite(line) == NUL)
6275 {
6276 capcol_lnum = lnum + 1;
6277 cap_col = 0;
6278 }
6279#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006280#ifdef FEAT_TEXT_PROP
6281 vim_free(text_props);
6282 vim_free(text_prop_idxs);
6283#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006284
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006285 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 return row;
6287}
6288
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006289/*
6290 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006291 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006292 */
6293 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006294comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006295{
6296 int i;
6297
6298 for (i = 0; i < Screen_mco; ++i)
6299 {
6300 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6301 return TRUE;
6302 if (ScreenLinesC[i][off_from] == 0)
6303 break;
6304 }
6305 return FALSE;
6306}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308/*
6309 * Check whether the given character needs redrawing:
6310 * - the (first byte of the) character is different
6311 * - the attributes are different
6312 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006313 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 */
6315 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006316char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 if (cols > 0
6319 && ((ScreenLines[off_from] != ScreenLines[off_to]
6320 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 || (enc_dbcs != 0
6322 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6323 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6324 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6325 : (cols > 1 && ScreenLines[off_from + 1]
6326 != ScreenLines[off_to + 1])))
6327 || (enc_utf8
6328 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6329 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006330 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006331 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6332 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006333 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 return TRUE;
6335 return FALSE;
6336}
6337
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006338#if defined(FEAT_TERMINAL) || defined(PROTO)
6339/*
6340 * Return the index in ScreenLines[] for the current screen line.
6341 */
6342 int
6343screen_get_current_line_off()
6344{
6345 return (int)(current_ScreenLine - ScreenLines);
6346}
6347#endif
6348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349/*
6350 * Move one "cooked" screen line to the screen, but only the characters that
6351 * have actually changed. Handle insert/delete character.
6352 * "coloff" gives the first column on the screen for this line.
6353 * "endcol" gives the columns where valid characters are.
6354 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6355 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006356 * "flags" can have bits:
6357 * SLF_POPUP popup window
6358 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6360 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6361 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006362 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006363screen_line(
6364 int row,
6365 int coloff,
6366 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006367 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006368 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 unsigned off_from;
6371 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006372 unsigned max_off_from;
6373 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 int force = FALSE; /* force update rest of the line */
6377 int redraw_this /* bool: does character need redraw? */
6378#ifdef FEAT_GUI
6379 = TRUE /* For GUI when while-loop empty */
6380#endif
6381 ;
6382 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 int clear_next = FALSE;
6384 int char_cells; /* 1: normal char */
6385 /* 2: occupies two display cells */
6386# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006388 /* Check for illegal row and col, just in case. */
6389 if (row >= Rows)
6390 row = Rows - 1;
6391 if (endcol > Columns)
6392 endcol = Columns;
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394# ifdef FEAT_CLIPBOARD
6395 clip_may_clear_selection(row, row);
6396# endif
6397
6398 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6399 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006400 max_off_from = off_from + screen_Columns;
6401 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402
6403#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006404 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 /* Clear rest first, because it's left of the text. */
6407 if (clear_width > 0)
6408 {
6409 while (col <= endcol && ScreenLines[off_to] == ' '
6410 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006411 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 {
6413 ++off_to;
6414 ++col;
6415 }
6416 if (col <= endcol)
6417 screen_fill(row, row + 1, col + coloff,
6418 endcol + coloff + 1, ' ', ' ', 0);
6419 }
6420 col = endcol + 1;
6421 off_to = LineOffset[row] + col + coloff;
6422 off_from += col;
6423 endcol = (clear_width > 0 ? clear_width : -clear_width);
6424 }
6425#endif /* FEAT_RIGHTLEFT */
6426
6427 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6428
6429 while (col < endcol)
6430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006432 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 else
6434 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436 redraw_this = redraw_next;
6437 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6438 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6439
6440#ifdef FEAT_GUI
6441 /* If the next character was bold, then redraw the current character to
6442 * remove any pixels that might have spilt over into us. This only
6443 * happens in the GUI.
6444 */
6445 if (redraw_next && gui.in_use)
6446 {
6447 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006448 if (hl > HL_ALL)
6449 hl = syn_attr2attr(hl);
6450 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 redraw_this = TRUE;
6452 }
6453#endif
6454
6455 if (redraw_this)
6456 {
6457 /*
6458 * Special handling when 'xs' termcap flag set (hpterm):
6459 * Attributes for characters are stored at the position where the
6460 * cursor is when writing the highlighting code. The
6461 * start-highlighting code must be written with the cursor on the
6462 * first highlighted character. The stop-highlighting code must
6463 * be written with the cursor just after the last highlighted
6464 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006465 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 * to clear the rest of the line, and force redrawing it
6467 * completely.
6468 */
6469 if ( p_wiv
6470 && !force
6471#ifdef FEAT_GUI
6472 && !gui.in_use
6473#endif
6474 && ScreenAttrs[off_to] != 0
6475 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6476 {
6477 /*
6478 * Need to remove highlighting attributes here.
6479 */
6480 windgoto(row, col + coloff);
6481 out_str(T_CE); /* clear rest of this screen line */
6482 screen_start(); /* don't know where cursor is now */
6483 force = TRUE; /* force redraw of rest of the line */
6484 redraw_next = TRUE; /* or else next char would miss out */
6485
6486 /*
6487 * If the previous character was highlighted, need to stop
6488 * highlighting at this character.
6489 */
6490 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6491 {
6492 screen_attr = ScreenAttrs[off_to - 1];
6493 term_windgoto(row, col + coloff);
6494 screen_stop_highlight();
6495 }
6496 else
6497 screen_attr = 0; /* highlighting has stopped */
6498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (enc_dbcs != 0)
6500 {
6501 /* Check if overwriting a double-byte with a single-byte or
6502 * the other way around requires another character to be
6503 * redrawn. For UTF-8 this isn't needed, because comparing
6504 * ScreenLinesUC[] is sufficient. */
6505 if (char_cells == 1
6506 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006507 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 {
6509 /* Writing a single-cell character over a double-cell
6510 * character: need to redraw the next cell. */
6511 ScreenLines[off_to + 1] = 0;
6512 redraw_next = TRUE;
6513 }
6514 else if (char_cells == 2
6515 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006516 && (*mb_off2cells)(off_to, max_off_to) == 1
6517 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
6519 /* Writing the second half of a double-cell character over
6520 * a double-cell character: need to redraw the second
6521 * cell. */
6522 ScreenLines[off_to + 2] = 0;
6523 redraw_next = TRUE;
6524 }
6525
6526 if (enc_dbcs == DBCS_JPNU)
6527 ScreenLines2[off_to] = ScreenLines2[off_from];
6528 }
6529 /* When writing a single-width character over a double-width
6530 * character and at the end of the redrawn text, need to clear out
6531 * the right halve of the old character.
6532 * Also required when writing the right halve of a double-width
6533 * char over the left halve of an existing one. */
6534 if (has_mbyte && col + char_cells == endcol
6535 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006536 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006538 && (*mb_off2cells)(off_to, max_off_to) == 1
6539 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (enc_utf8)
6544 {
6545 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6546 if (ScreenLinesUC[off_from] != 0)
6547 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006548 int i;
6549
6550 for (i = 0; i < Screen_mco; ++i)
6551 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
6553 }
6554 if (char_cells == 2)
6555 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006558 /* The bold trick makes a single column of pixels appear in the
6559 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 * character should be redrawn too. This happens for our own GUI
6561 * and for some xterms. */
6562 if (
6563# ifdef FEAT_GUI
6564 gui.in_use
6565# endif
6566# if defined(FEAT_GUI) && defined(UNIX)
6567 ||
6568# endif
6569# ifdef UNIX
6570 term_is_xterm
6571# endif
6572 )
6573 {
6574 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006575 if (hl > HL_ALL)
6576 hl = syn_attr2attr(hl);
6577 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 redraw_next = TRUE;
6579 }
6580#endif
6581 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006582
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006583 /* For simplicity set the attributes of second half of a
6584 * double-wide character equal to the first half. */
6585 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006587
6588 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 screen_char(off_to, row, col + coloff);
6592 }
6593 else if ( p_wiv
6594#ifdef FEAT_GUI
6595 && !gui.in_use
6596#endif
6597 && col + coloff > 0)
6598 {
6599 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6600 {
6601 /*
6602 * Don't output stop-highlight when moving the cursor, it will
6603 * stop the highlighting when it should continue.
6604 */
6605 screen_attr = 0;
6606 }
6607 else if (screen_attr != 0)
6608 screen_stop_highlight();
6609 }
6610
6611 off_to += CHAR_CELLS;
6612 off_from += CHAR_CELLS;
6613 col += CHAR_CELLS;
6614 }
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (clear_next)
6617 {
6618 /* Clear the second half of a double-wide character of which the left
6619 * half was overwritten with a single-wide character. */
6620 ScreenLines[off_to] = ' ';
6621 if (enc_utf8)
6622 ScreenLinesUC[off_to] = 0;
6623 screen_char(off_to, row, col + coloff);
6624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626 if (clear_width > 0
6627#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006628 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#endif
6630 )
6631 {
6632#ifdef FEAT_GUI
6633 int startCol = col;
6634#endif
6635
6636 /* blank out the rest of the line */
6637 while (col < clear_width && ScreenLines[off_to] == ' '
6638 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006639 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 {
6641 ++off_to;
6642 ++col;
6643 }
6644 if (col < clear_width)
6645 {
6646#ifdef FEAT_GUI
6647 /*
6648 * In the GUI, clearing the rest of the line may leave pixels
6649 * behind if the first character cleared was bold. Some bold
6650 * fonts spill over the left. In this case we redraw the previous
6651 * character too. If we didn't skip any blanks above, then we
6652 * only redraw if the character wasn't already redrawn anyway.
6653 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006654 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
6656 hl = ScreenAttrs[off_to];
6657 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006658 {
6659 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006660
Bram Moolenaar9c697322006-10-09 20:11:17 +00006661 if (enc_utf8)
6662 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6663 * that its width is 2. */
6664 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6665 else if (enc_dbcs != 0)
6666 {
6667 /* find previous character by counting from first
6668 * column and get its width. */
6669 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006670 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006671
6672 while (off < off_to)
6673 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006674 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006675 off += prev_cells;
6676 }
6677 }
6678
6679 if (enc_dbcs != 0 && prev_cells > 1)
6680 screen_char_2(off_to - prev_cells, row,
6681 col + coloff - prev_cells);
6682 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006683 screen_char(off_to - prev_cells, row,
6684 col + coloff - prev_cells);
6685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 }
6687#endif
6688 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6689 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 off_to += clear_width - col;
6691 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693 }
6694
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006695 if (clear_width > 0
6696#ifdef FEAT_TEXT_PROP
6697 && !(flags & SLF_POPUP) // no separator for popup window
6698#endif
6699 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006701 // For a window that has a right neighbor, draw the separator char
6702 // right of the window contents.
6703 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
6705 int c;
6706
6707 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006708 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006709 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6710 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 || ScreenAttrs[off_to] != hl)
6712 {
6713 ScreenLines[off_to] = c;
6714 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (enc_utf8)
6716 {
6717 if (c >= 0x80)
6718 {
6719 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006720 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722 else
6723 ScreenLinesUC[off_to] = 0;
6724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 screen_char(off_to, row, col + coloff);
6726 }
6727 }
6728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 LineWraps[row] = FALSE;
6730 }
6731}
6732
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006733#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006735 * Mirror text "str" for right-left displaying.
6736 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006739rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 char_u *p1, *p2;
6742 int t;
6743
6744 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6745 {
6746 t = *p1;
6747 *p1 = *p2;
6748 *p2 = t;
6749 }
6750}
6751#endif
6752
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753/*
6754 * mark all status lines for redraw; used after first :cd
6755 */
6756 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006757status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
6759 win_T *wp;
6760
Bram Moolenaar29323592016-07-24 22:04:11 +02006761 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (wp->w_status_height)
6763 {
6764 wp->w_redr_status = TRUE;
6765 redraw_later(VALID);
6766 }
6767}
6768
6769/*
6770 * mark all status lines of the current buffer for redraw
6771 */
6772 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006773status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 win_T *wp;
6776
Bram Moolenaar29323592016-07-24 22:04:11 +02006777 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6779 {
6780 wp->w_redr_status = TRUE;
6781 redraw_later(VALID);
6782 }
6783}
6784
6785/*
6786 * Redraw all status lines that need to be redrawn.
6787 */
6788 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006789redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 win_T *wp;
6792
Bram Moolenaar29323592016-07-24 22:04:11 +02006793 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006795 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006796 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006797 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
Bram Moolenaar4033c552017-09-16 20:54:51 +02006800#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801/*
6802 * Redraw all status lines at the bottom of frame "frp".
6803 */
6804 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006805win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 if (frp->fr_layout == FR_LEAF)
6808 frp->fr_win->w_redr_status = TRUE;
6809 else if (frp->fr_layout == FR_ROW)
6810 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006811 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 win_redraw_last_status(frp);
6813 }
6814 else /* frp->fr_layout == FR_COL */
6815 {
6816 frp = frp->fr_child;
6817 while (frp->fr_next != NULL)
6818 frp = frp->fr_next;
6819 win_redraw_last_status(frp);
6820 }
6821}
6822#endif
6823
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824/*
6825 * Draw the verticap separator right of window "wp" starting with line "row".
6826 */
6827 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006828draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829{
6830 int hl;
6831 int c;
6832
6833 if (wp->w_vsep_width)
6834 {
6835 /* draw the vertical separator right of this window */
6836 c = fillchar_vsep(&hl);
6837 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6838 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6839 c, ' ', hl);
6840 }
6841}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842
6843#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006844static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845
6846/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006847 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 */
6849 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006850status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 int len = 0;
6853
6854#ifdef FEAT_MENU
6855 int emenu = (xp->xp_context == EXPAND_MENUS
6856 || xp->xp_context == EXPAND_MENUNAMES);
6857
6858 /* Check for menu separators - replace with '|'. */
6859 if (emenu && menu_is_separator(s))
6860 return 1;
6861#endif
6862
6863 while (*s != NUL)
6864 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006865 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006866 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006867 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869
6870 return len;
6871}
6872
6873/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006874 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006875 * These are backslashes used for escaping. Do show backslashes in help tags.
6876 */
6877 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006878skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006879{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006880 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006881#ifdef FEAT_MENU
6882 || ((xp->xp_context == EXPAND_MENUS
6883 || xp->xp_context == EXPAND_MENUNAMES)
6884 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6885#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006886 )
6887 {
6888#ifndef BACKSLASH_IN_FILENAME
6889 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6890 return 2;
6891#endif
6892 return 1;
6893 }
6894 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006895}
6896
6897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 * Show wildchar matches in the status line.
6899 * Show at least the "match" item.
6900 * We start at item 'first_match' in the list and show all matches that fit.
6901 *
6902 * If inversion is possible we use it. Else '=' characters are used.
6903 */
6904 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006905win_redr_status_matches(
6906 expand_T *xp,
6907 int num_matches,
6908 char_u **matches, /* list of matches */
6909 int match,
6910 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6913 int row;
6914 char_u *buf;
6915 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006916 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 int fillchar;
6918 int attr;
6919 int i;
6920 int highlight = TRUE;
6921 char_u *selstart = NULL;
6922 int selstart_col = 0;
6923 char_u *selend = NULL;
6924 static int first_match = 0;
6925 int add_left = FALSE;
6926 char_u *s;
6927#ifdef FEAT_MENU
6928 int emenu;
6929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
6932 if (matches == NULL) /* interrupted completion? */
6933 return;
6934
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006935 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006936 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006937 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006938 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 if (buf == NULL)
6940 return;
6941
6942 if (match == -1) /* don't show match but original text */
6943 {
6944 match = 0;
6945 highlight = FALSE;
6946 }
6947 /* count 1 for the ending ">" */
6948 clen = status_match_len(xp, L_MATCH(match)) + 3;
6949 if (match == 0)
6950 first_match = 0;
6951 else if (match < first_match)
6952 {
6953 /* jumping left, as far as we can go */
6954 first_match = match;
6955 add_left = TRUE;
6956 }
6957 else
6958 {
6959 /* check if match fits on the screen */
6960 for (i = first_match; i < match; ++i)
6961 clen += status_match_len(xp, L_MATCH(i)) + 2;
6962 if (first_match > 0)
6963 clen += 2;
6964 /* jumping right, put match at the left */
6965 if ((long)clen > Columns)
6966 {
6967 first_match = match;
6968 /* if showing the last match, we can add some on the left */
6969 clen = 2;
6970 for (i = match; i < num_matches; ++i)
6971 {
6972 clen += status_match_len(xp, L_MATCH(i)) + 2;
6973 if ((long)clen >= Columns)
6974 break;
6975 }
6976 if (i == num_matches)
6977 add_left = TRUE;
6978 }
6979 }
6980 if (add_left)
6981 while (first_match > 0)
6982 {
6983 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6984 if ((long)clen >= Columns)
6985 break;
6986 --first_match;
6987 }
6988
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006989 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 if (first_match == 0)
6992 {
6993 *buf = NUL;
6994 len = 0;
6995 }
6996 else
6997 {
6998 STRCPY(buf, "< ");
6999 len = 2;
7000 }
7001 clen = len;
7002
7003 i = first_match;
7004 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7005 {
7006 if (i == match)
7007 {
7008 selstart = buf + len;
7009 selstart_col = clen;
7010 }
7011
7012 s = L_MATCH(i);
7013 /* Check for menu separators - replace with '|' */
7014#ifdef FEAT_MENU
7015 emenu = (xp->xp_context == EXPAND_MENUS
7016 || xp->xp_context == EXPAND_MENUNAMES);
7017 if (emenu && menu_is_separator(s))
7018 {
7019 STRCPY(buf + len, transchar('|'));
7020 l = (int)STRLEN(buf + len);
7021 len += l;
7022 clen += l;
7023 }
7024 else
7025#endif
7026 for ( ; *s != NUL; ++s)
7027 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007028 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007030 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032 STRNCPY(buf + len, s, l);
7033 s += l - 1;
7034 len += l;
7035 }
7036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 STRCPY(buf + len, transchar_byte(*s));
7039 len += (int)STRLEN(buf + len);
7040 }
7041 }
7042 if (i == match)
7043 selend = buf + len;
7044
7045 *(buf + len++) = ' ';
7046 *(buf + len++) = ' ';
7047 clen += 2;
7048 if (++i == num_matches)
7049 break;
7050 }
7051
7052 if (i != num_matches)
7053 {
7054 *(buf + len++) = '>';
7055 ++clen;
7056 }
7057
7058 buf[len] = NUL;
7059
7060 row = cmdline_row - 1;
7061 if (row >= 0)
7062 {
7063 if (wild_menu_showing == 0)
7064 {
7065 if (msg_scrolled > 0)
7066 {
7067 /* Put the wildmenu just above the command line. If there is
7068 * no room, scroll the screen one line up. */
7069 if (cmdline_row == Rows - 1)
7070 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007071 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 ++msg_scrolled;
7073 }
7074 else
7075 {
7076 ++cmdline_row;
7077 ++row;
7078 }
7079 wild_menu_showing = WM_SCROLLED;
7080 }
7081 else
7082 {
7083 /* Create status line if needed by setting 'laststatus' to 2.
7084 * Set 'winminheight' to zero to avoid that the window is
7085 * resized. */
7086 if (lastwin->w_status_height == 0)
7087 {
7088 save_p_ls = p_ls;
7089 save_p_wmh = p_wmh;
7090 p_ls = 2;
7091 p_wmh = 0;
7092 last_status(FALSE);
7093 }
7094 wild_menu_showing = WM_SHOWN;
7095 }
7096 }
7097
7098 screen_puts(buf, row, 0, attr);
7099 if (selstart != NULL && highlight)
7100 {
7101 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007102 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 }
7104
7105 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7106 }
7107
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 vim_free(buf);
7110}
7111#endif
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113/*
7114 * Redraw the status line of window wp.
7115 *
7116 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007117 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7118 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007120 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007121win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 int row;
7124 char_u *p;
7125 int len;
7126 int fillchar;
7127 int attr;
7128 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007129 static int busy = FALSE;
7130
7131 /* It's possible to get here recursively when 'statusline' (indirectly)
7132 * invokes ":redrawstatus". Simply ignore the call then. */
7133 if (busy)
7134 return;
7135 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136
7137 wp->w_redr_status = FALSE;
7138 if (wp->w_status_height == 0)
7139 {
7140 /* no status line, can only be last window */
7141 redraw_cmdline = TRUE;
7142 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007143 else if (!redrawing()
7144#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007145 // don't update status line when popup menu is visible and may be
7146 // drawn over it, unless it will be redrawn later
7147 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007148#endif
7149 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
7151 /* Don't redraw right now, do it later. */
7152 wp->w_redr_status = TRUE;
7153 }
7154#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007155 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {
7157 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007158 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 }
7160#endif
7161 else
7162 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007163 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007165 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 p = NameBuff;
7167 len = (int)STRLEN(p);
7168
Bram Moolenaard85f2712017-07-28 21:51:57 +02007169 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170#ifdef FEAT_QUICKFIX
7171 || wp->w_p_pvw
7172#endif
7173 || bufIsChanged(wp->w_buffer)
7174 || wp->w_buffer->b_p_ro)
7175 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007176 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007178 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 len += (int)STRLEN(p + len);
7180 }
7181#ifdef FEAT_QUICKFIX
7182 if (wp->w_p_pvw)
7183 {
7184 STRCPY(p + len, _("[Preview]"));
7185 len += (int)STRLEN(p + len);
7186 }
7187#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007188 if (bufIsChanged(wp->w_buffer)
7189#ifdef FEAT_TERMINAL
7190 && !bt_terminal(wp->w_buffer)
7191#endif
7192 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
7194 STRCPY(p + len, "[+]");
7195 len += 3;
7196 }
7197 if (wp->w_buffer->b_p_ro)
7198 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007199 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007200 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202
Bram Moolenaar02631462017-09-22 15:20:32 +02007203 this_ru_col = ru_col - (Columns - wp->w_width);
7204 if (this_ru_col < (wp->w_width + 1) / 2)
7205 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 if (this_ru_col <= 1)
7207 {
7208 p = (char_u *)"<"; /* No room for file name! */
7209 len = 1;
7210 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007211 else if (has_mbyte)
7212 {
7213 int clen = 0, i;
7214
7215 /* Count total number of display cells. */
7216 clen = mb_string2cells(p, -1);
7217
7218 /* Find first character that will fit.
7219 * Going from start to end is much faster for DBCS. */
7220 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7221 i += (*mb_ptr2len)(p + i))
7222 clen -= (*mb_ptr2cells)(p + i);
7223 len = clen;
7224 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007226 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007228 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
7230
Bram Moolenaara12a1612019-01-24 16:39:02 +01007231 }
7232 else if (len > this_ru_col - 1)
7233 {
7234 p += len - (this_ru_col - 1);
7235 *p = '<';
7236 len = this_ru_col - 1;
7237 }
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007240 screen_puts(p, row, wp->w_wincol, attr);
7241 screen_fill(row, row + 1, len + wp->w_wincol,
7242 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007244 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7246 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007247 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007250 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251#endif
7252 }
7253
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 /*
7255 * May need to draw the character below the vertical separator.
7256 */
7257 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7258 {
7259 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007260 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 else
7262 fillchar = fillchar_vsep(&attr);
7263 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7264 attr);
7265 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007266 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267}
7268
Bram Moolenaar238a5642006-02-21 22:12:05 +00007269#ifdef FEAT_STL_OPT
7270/*
7271 * Redraw the status line according to 'statusline' and take care of any
7272 * errors encountered.
7273 */
7274 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007275redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007276{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007277 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007278 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007279
7280 /* When called recursively return. This can happen when the statusline
7281 * contains an expression that triggers a redraw. */
7282 if (entered)
7283 return;
7284 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007285
Bram Moolenaara742e082016-04-05 21:10:38 +02007286 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007287 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007288 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007289 {
7290 /* When there is an error disable the statusline, otherwise the
7291 * display is messed up with errors and a redraw triggers the problem
7292 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007293 set_string_option_direct((char_u *)"statusline", -1,
7294 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007295 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007296 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007297 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007298 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007299}
7300#endif
7301
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302/*
7303 * Return TRUE if the status line of window "wp" is connected to the status
7304 * line of the window right of it. If not, then it's a vertical separator.
7305 * Only call if (wp->w_vsep_width != 0).
7306 */
7307 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007308stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
7310 frame_T *fr;
7311
7312 fr = wp->w_frame;
7313 while (fr->fr_parent != NULL)
7314 {
7315 if (fr->fr_parent->fr_layout == FR_COL)
7316 {
7317 if (fr->fr_next != NULL)
7318 break;
7319 }
7320 else
7321 {
7322 if (fr->fr_next != NULL)
7323 return TRUE;
7324 }
7325 fr = fr->fr_parent;
7326 }
7327 return FALSE;
7328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331/*
7332 * Get the value to show for the language mappings, active 'keymap'.
7333 */
7334 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007335get_keymap_str(
7336 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007337 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007338 char_u *buf, /* buffer for the result */
7339 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341 char_u *p;
7342
7343 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7344 return FALSE;
7345
7346 {
7347#ifdef FEAT_EVAL
7348 buf_T *old_curbuf = curbuf;
7349 win_T *old_curwin = curwin;
7350 char_u *s;
7351
7352 curbuf = wp->w_buffer;
7353 curwin = wp;
7354 STRCPY(buf, "b:keymap_name"); /* must be writable */
7355 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007356 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 --emsg_skip;
7358 curbuf = old_curbuf;
7359 curwin = old_curwin;
7360 if (p == NULL || *p == NUL)
7361#endif
7362 {
7363#ifdef FEAT_KEYMAP
7364 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7365 p = wp->w_buffer->b_p_keymap;
7366 else
7367#endif
7368 p = (char_u *)"lang";
7369 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007370 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 buf[0] = NUL;
7372#ifdef FEAT_EVAL
7373 vim_free(s);
7374#endif
7375 }
7376 return buf[0] != NUL;
7377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378
7379#if defined(FEAT_STL_OPT) || defined(PROTO)
7380/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007381 * Redraw the status line or ruler of window "wp".
7382 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 */
7384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007385win_redr_custom(
7386 win_T *wp,
7387 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007389 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 int attr;
7391 int curattr;
7392 int row;
7393 int col = 0;
7394 int maxwidth;
7395 int width;
7396 int n;
7397 int len;
7398 int fillchar;
7399 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007400 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007402 struct stl_hlrec hltab[STL_MAX_ITEM];
7403 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007404 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007405 win_T *ewp;
7406 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
Bram Moolenaar1d633412013-12-11 15:52:01 +01007408 /* There is a tiny chance that this gets called recursively: When
7409 * redrawing a status line triggers redrawing the ruler or tabline.
7410 * Avoid trouble by not allowing recursion. */
7411 if (entered)
7412 return;
7413 entered = TRUE;
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007416 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007418 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007419 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007420 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007421 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007422 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007423 maxwidth = Columns;
7424# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007425 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007428 else
7429 {
7430 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007431 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007432 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007433
7434 if (draw_ruler)
7435 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007436 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007437 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007438 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007439 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007440 if (*++stl == '-')
7441 stl++;
7442 if (atoi((char *)stl))
7443 while (VIM_ISDIGIT(*stl))
7444 stl++;
7445 if (*stl++ != '(')
7446 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007447 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007448 col = ru_col - (Columns - wp->w_width);
7449 if (col < (wp->w_width + 1) / 2)
7450 col = (wp->w_width + 1) / 2;
7451 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007452 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007453 {
7454 row = Rows - 1;
7455 --maxwidth; /* writing in last column may cause scrolling */
7456 fillchar = ' ';
7457 attr = 0;
7458 }
7459
7460# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007461 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007462# endif
7463 }
7464 else
7465 {
7466 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007467 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007468 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007469 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007470# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007471 use_sandbox = was_set_insecurely((char_u *)"statusline",
7472 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007473# endif
7474 }
7475
Bram Moolenaar53f81742017-09-22 14:35:51 +02007476 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007477 }
7478
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007480 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
Bram Moolenaar61452852011-02-01 18:01:11 +01007482 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7483 * the cursor away and back. */
7484 ewp = wp == NULL ? curwin : wp;
7485 p_crb_save = ewp->w_p_crb;
7486 ewp->w_p_crb = FALSE;
7487
Bram Moolenaar362f3562009-11-03 16:20:34 +00007488 /* Make a copy, because the statusline may include a function call that
7489 * might change the option value and free the memory. */
7490 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007491 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007492 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007493 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007494 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007495 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007497 /* Make all characters printable. */
7498 p = transstr(buf);
7499 if (p != NULL)
7500 {
7501 vim_strncpy(buf, p, sizeof(buf) - 1);
7502 vim_free(p);
7503 }
7504
7505 /* fill up with "fillchar" */
7506 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007507 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 ++width;
7511 }
7512 buf[len] = NUL;
7513
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007514 /*
7515 * Draw each snippet with the specified highlighting.
7516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 curattr = attr;
7518 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007519 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007521 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 screen_puts_len(p, len, row, col, curattr);
7523 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007524 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007526 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007528 else if (hltab[n].userhl < 0)
7529 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007530#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007531 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7532 && wp->w_status_height != 0)
7533 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007534 else if (wp != NULL && bt_terminal(wp->w_buffer)
7535 && wp->w_status_height != 0)
7536 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007537#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007538 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007539 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007541 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 }
7543 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007544
7545 if (wp == NULL)
7546 {
7547 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7548 col = 0;
7549 len = 0;
7550 p = buf;
7551 fillchar = 0;
7552 for (n = 0; tabtab[n].start != NULL; n++)
7553 {
7554 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7555 while (col < len)
7556 TabPageIdxs[col++] = fillchar;
7557 p = tabtab[n].start;
7558 fillchar = tabtab[n].userhl;
7559 }
7560 while (col < Columns)
7561 TabPageIdxs[col++] = fillchar;
7562 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007563
7564theend:
7565 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566}
7567
7568#endif /* FEAT_STL_OPT */
7569
7570/*
7571 * Output a single character directly to the screen and update ScreenLines.
7572 */
7573 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007574screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 char_u buf[MB_MAXBYTES + 1];
7577
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007578 if (has_mbyte)
7579 buf[(*mb_char2bytes)(c, buf)] = NUL;
7580 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007581 {
7582 buf[0] = c;
7583 buf[1] = NUL;
7584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 screen_puts(buf, row, col, attr);
7586}
7587
7588/*
7589 * Get a single character directly from ScreenLines into "bytes[]".
7590 * Also return its attribute in *attrp;
7591 */
7592 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007593screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
7595 unsigned off;
7596
7597 /* safety check */
7598 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7599 {
7600 off = LineOffset[row] + col;
7601 *attrp = ScreenAttrs[off];
7602 bytes[0] = ScreenLines[off];
7603 bytes[1] = NUL;
7604
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (enc_utf8 && ScreenLinesUC[off] != 0)
7606 bytes[utfc_char2bytes(off, bytes)] = NUL;
7607 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7608 {
7609 bytes[0] = ScreenLines[off];
7610 bytes[1] = ScreenLines2[off];
7611 bytes[2] = NUL;
7612 }
7613 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7614 {
7615 bytes[1] = ScreenLines[off + 1];
7616 bytes[2] = NUL;
7617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 }
7619}
7620
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007621/*
7622 * Return TRUE if composing characters for screen posn "off" differs from
7623 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007624 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007625 */
7626 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007627screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007628{
7629 int i;
7630
7631 for (i = 0; i < Screen_mco; ++i)
7632 {
7633 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7634 return TRUE;
7635 if (u8cc[i] == 0)
7636 break;
7637 }
7638 return FALSE;
7639}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641/*
7642 * Put string '*text' on the screen at position 'row' and 'col', with
7643 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7644 * Note: only outputs within one row, message is truncated at screen boundary!
7645 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7646 */
7647 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007648screen_puts(
7649 char_u *text,
7650 int row,
7651 int col,
7652 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
7654 screen_puts_len(text, -1, row, col, attr);
7655}
7656
7657/*
7658 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7659 * a NUL.
7660 */
7661 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007662screen_puts_len(
7663 char_u *text,
7664 int textlen,
7665 int row,
7666 int col,
7667 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668{
7669 unsigned off;
7670 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007671 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007673 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 int mbyte_blen = 1;
7675 int mbyte_cells = 1;
7676 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007677 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007679#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007681 int pc, nc, nc1;
7682 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007684 int force_redraw_this;
7685 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007686 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687
7688 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7689 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007690 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691
Bram Moolenaarc236c162008-07-13 17:41:49 +00007692 /* When drawing over the right halve of a double-wide char clear out the
7693 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007694 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007695#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007696 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007697#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007698 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007699 {
7700 ScreenLines[off - 1] = ' ';
7701 ScreenAttrs[off - 1] = 0;
7702 if (enc_utf8)
7703 {
7704 ScreenLinesUC[off - 1] = 0;
7705 ScreenLinesC[0][off - 1] = 0;
7706 }
7707 /* redraw the previous cell, make it empty */
7708 screen_char(off - 1, row, col - 1);
7709 /* force the cell at "col" to be redrawn */
7710 force_redraw_next = TRUE;
7711 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007712
Bram Moolenaar367329b2007-08-30 11:53:22 +00007713 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007714 while (col < screen_Columns
7715 && (len < 0 || (int)(ptr - text) < len)
7716 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {
7718 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 /* check if this is the first byte of a multibyte */
7720 if (has_mbyte)
7721 {
7722 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007723 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007725 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7727 mbyte_cells = 1;
7728 else if (enc_dbcs != 0)
7729 mbyte_cells = mbyte_blen;
7730 else /* enc_utf8 */
7731 {
7732 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007733 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 (int)((text + len) - ptr));
7735 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007736 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007738#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7740 {
7741 /* Do Arabic shaping. */
7742 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7743 {
7744 /* Past end of string to be displayed. */
7745 nc = NUL;
7746 nc1 = NUL;
7747 }
7748 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007749 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007750 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7751 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007752 nc1 = pcc[0];
7753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 pc = prev_c;
7755 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007756 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758 else
7759 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007760#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007761 if (col + mbyte_cells > screen_Columns)
7762 {
7763 /* Only 1 cell left, but character requires 2 cells:
7764 * display a '>' in the last column to avoid wrapping. */
7765 c = '>';
7766 mbyte_cells = 1;
7767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007771 force_redraw_this = force_redraw_next;
7772 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007773
7774 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 || (mbyte_cells == 2
7776 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7777 || (enc_dbcs == DBCS_JPNU
7778 && c == 0x8e
7779 && ScreenLines2[off] != ptr[1])
7780 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007781 && (ScreenLinesUC[off] !=
7782 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7783 || (ScreenLinesUC[off] != 0
7784 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007786 || exmode_active;
7787
Bram Moolenaara12a1612019-01-24 16:39:02 +01007788 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {
7790#if defined(FEAT_GUI) || defined(UNIX)
7791 /* The bold trick makes a single row of pixels appear in the next
7792 * character. When a bold character is removed, the next
7793 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007794 * and for some xterms. */
7795 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796# ifdef FEAT_GUI
7797 gui.in_use
7798# endif
7799# if defined(FEAT_GUI) && defined(UNIX)
7800 ||
7801# endif
7802# ifdef UNIX
7803 term_is_xterm
7804# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007805 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007807 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007809 if (n > HL_ALL)
7810 n = syn_attr2attr(n);
7811 if (n & HL_BOLD)
7812 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 /* When at the end of the text and overwriting a two-cell
7816 * character with a one-cell character, need to clear the next
7817 * cell. Also when overwriting the left halve of a two-cell char
7818 * with the right halve of a two-cell char. Do this only once
7819 * (mb_off2cells() may return 2 on the right halve). */
7820 if (clear_next_cell)
7821 clear_next_cell = FALSE;
7822 else if (has_mbyte
7823 && (len < 0 ? ptr[mbyte_blen] == NUL
7824 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007825 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007827 && (*mb_off2cells)(off, max_off) == 1
7828 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 clear_next_cell = TRUE;
7830
7831 /* Make sure we never leave a second byte of a double-byte behind,
7832 * it confuses mb_off2cells(). */
7833 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007834 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007836 && (*mb_off2cells)(off, max_off) == 1
7837 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 ScreenLines[off] = c;
7840 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 if (enc_utf8)
7842 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007843 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 ScreenLinesUC[off] = 0;
7845 else
7846 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007847 int i;
7848
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007850 for (i = 0; i < Screen_mco; ++i)
7851 {
7852 ScreenLinesC[i][off] = u8cc[i];
7853 if (u8cc[i] == 0)
7854 break;
7855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857 if (mbyte_cells == 2)
7858 {
7859 ScreenLines[off + 1] = 0;
7860 ScreenAttrs[off + 1] = attr;
7861 }
7862 screen_char(off, row, col);
7863 }
7864 else if (mbyte_cells == 2)
7865 {
7866 ScreenLines[off + 1] = ptr[1];
7867 ScreenAttrs[off + 1] = attr;
7868 screen_char_2(off, row, col);
7869 }
7870 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7871 {
7872 ScreenLines2[off] = ptr[1];
7873 screen_char(off, row, col);
7874 }
7875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 screen_char(off, row, col);
7877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (has_mbyte)
7879 {
7880 off += mbyte_cells;
7881 col += mbyte_cells;
7882 ptr += mbyte_blen;
7883 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007884 {
7885 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007887 len = -1;
7888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
7892 ++off;
7893 ++col;
7894 ++ptr;
7895 }
7896 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007897
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007898 /* If we detected the next character needs to be redrawn, but the text
7899 * doesn't extend up to there, update the character here. */
7900 if (force_redraw_next && col < screen_Columns)
7901 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007902 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7903 screen_char_2(off, row, col);
7904 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007905 screen_char(off, row, col);
7906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907}
7908
7909#ifdef FEAT_SEARCH_EXTRA
7910/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007911 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 */
7913 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007914start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 if (p_hls && !no_hlsearch)
7917 {
7918 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007919 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007920# ifdef FEAT_RELTIME
7921 /* Set the time limit to 'redrawtime'. */
7922 profile_setlimit(p_rdt, &search_hl.tm);
7923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925}
7926
7927/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007928 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 */
7930 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007931end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 if (search_hl.rm.regprog != NULL)
7934 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007935 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 search_hl.rm.regprog = NULL;
7937 }
7938}
7939
7940/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007941 * Init for calling prepare_search_hl().
7942 */
7943 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007944init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007945{
7946 matchitem_T *cur;
7947
7948 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7949 * match */
7950 cur = wp->w_match_head;
7951 while (cur != NULL)
7952 {
7953 cur->hl.rm = cur->match;
7954 if (cur->hlg_id == 0)
7955 cur->hl.attr = 0;
7956 else
7957 cur->hl.attr = syn_id2attr(cur->hlg_id);
7958 cur->hl.buf = wp->w_buffer;
7959 cur->hl.lnum = 0;
7960 cur->hl.first_lnum = 0;
7961# ifdef FEAT_RELTIME
7962 /* Set the time limit to 'redrawtime'. */
7963 profile_setlimit(p_rdt, &(cur->hl.tm));
7964# endif
7965 cur = cur->next;
7966 }
7967 search_hl.buf = wp->w_buffer;
7968 search_hl.lnum = 0;
7969 search_hl.first_lnum = 0;
7970 /* time limit is set at the toplevel, for all windows */
7971}
7972
7973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 * Advance to the match in window "wp" line "lnum" or past it.
7975 */
7976 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007977prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007979 matchitem_T *cur; /* points to the match list */
7980 match_T *shl; /* points to search_hl or a match */
7981 int shl_flag; /* flag to indicate whether search_hl
7982 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007983 int pos_inprogress; /* marks that position match search is
7984 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 int n;
7986
7987 /*
7988 * When using a multi-line pattern, start searching at the top
7989 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007990 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007992 cur = wp->w_match_head;
7993 shl_flag = FALSE;
7994 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007996 if (shl_flag == FALSE)
7997 {
7998 shl = &search_hl;
7999 shl_flag = TRUE;
8000 }
8001 else
8002 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 if (shl->rm.regprog != NULL
8004 && shl->lnum == 0
8005 && re_multiline(shl->rm.regprog))
8006 {
8007 if (shl->first_lnum == 0)
8008 {
8009# ifdef FEAT_FOLDING
8010 for (shl->first_lnum = lnum;
8011 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8012 if (hasFoldingWin(wp, shl->first_lnum - 1,
8013 NULL, NULL, TRUE, NULL))
8014 break;
8015# else
8016 shl->first_lnum = wp->w_topline;
8017# endif
8018 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008019 if (cur != NULL)
8020 cur->pos.cur = 0;
8021 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008023 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8024 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008026 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8027 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008028 pos_inprogress = cur == NULL || cur->pos.cur == 0
8029 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (shl->lnum != 0)
8031 {
8032 shl->first_lnum = shl->lnum
8033 + shl->rm.endpos[0].lnum
8034 - shl->rm.startpos[0].lnum;
8035 n = shl->rm.endpos[0].col;
8036 }
8037 else
8038 {
8039 ++shl->first_lnum;
8040 n = 0;
8041 }
8042 }
8043 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008044 if (shl != &search_hl && cur != NULL)
8045 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047}
8048
8049/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008050 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 * Uses shl->buf.
8052 * Sets shl->lnum and shl->rm contents.
8053 * Note: Assumes a previous match is always before "lnum", unless
8054 * shl->lnum is zero.
8055 * Careful: Any pointers for buffer lines will become invalid.
8056 */
8057 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008058next_search_hl(
8059 win_T *win,
8060 match_T *shl, /* points to search_hl or a match */
8061 linenr_T lnum,
8062 colnr_T mincol, /* minimal column for a match */
8063 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064{
8065 linenr_T l;
8066 colnr_T matchcol;
8067 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008068 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008070 // for :{range}s/pat only highlight inside the range
8071 if (lnum < search_first_line || lnum > search_last_line)
8072 {
8073 shl->lnum = 0;
8074 return;
8075 }
8076
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 if (shl->lnum != 0)
8078 {
8079 /* Check for three situations:
8080 * 1. If the "lnum" is below a previous match, start a new search.
8081 * 2. If the previous match includes "mincol", use it.
8082 * 3. Continue after the previous match.
8083 */
8084 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8085 if (lnum > l)
8086 shl->lnum = 0;
8087 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8088 return;
8089 }
8090
8091 /*
8092 * Repeat searching for a match until one is found that includes "mincol"
8093 * or none is found in this line.
8094 */
8095 called_emsg = FALSE;
8096 for (;;)
8097 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008098#ifdef FEAT_RELTIME
8099 /* Stop searching after passing the time limit. */
8100 if (profile_passed_limit(&(shl->tm)))
8101 {
8102 shl->lnum = 0; /* no match found in time */
8103 break;
8104 }
8105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 /* Three situations:
8107 * 1. No useful previous match: search from start of line.
8108 * 2. Not Vi compatible or empty match: continue at next character.
8109 * Break the loop if this is beyond the end of the line.
8110 * 3. Vi compatible searching: continue at end of previous match.
8111 */
8112 if (shl->lnum == 0)
8113 matchcol = 0;
8114 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8115 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008116 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008118 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008119
8120 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008121 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008122 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008124 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 shl->lnum = 0;
8126 break;
8127 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008128 if (has_mbyte)
8129 matchcol += mb_ptr2len(ml);
8130 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008131 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 }
8133 else
8134 matchcol = shl->rm.endpos[0].col;
8135
8136 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008137 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008139 /* Remember whether shl->rm is using a copy of the regprog in
8140 * cur->match. */
8141 int regprog_is_copy = (shl != &search_hl && cur != NULL
8142 && shl == &cur->hl
8143 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008144 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008145
Bram Moolenaarb3414592014-06-17 17:48:32 +02008146 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8147 matchcol,
8148#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008149 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008150#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008151 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008152#endif
8153 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008154 /* Copy the regprog, in case it got freed and recompiled. */
8155 if (regprog_is_copy)
8156 cur->match.regprog = cur->hl.rm.regprog;
8157
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008158 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008159 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008160 /* Error while handling regexp: stop using this regexp. */
8161 if (shl == &search_hl)
8162 {
8163 /* don't free regprog in the match list, it's a copy */
8164 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008165 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008166 }
8167 shl->rm.regprog = NULL;
8168 shl->lnum = 0;
8169 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8170 message */
8171 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008172 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008173 }
8174 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008175 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008176 else
8177 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (nmatched == 0)
8179 {
8180 shl->lnum = 0; /* no match found */
8181 break;
8182 }
8183 if (shl->rm.startpos[0].lnum > 0
8184 || shl->rm.startpos[0].col >= mincol
8185 || nmatched > 1
8186 || shl->rm.endpos[0].col > mincol)
8187 {
8188 shl->lnum += shl->rm.startpos[0].lnum;
8189 break; /* useful match found */
8190 }
8191 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008192
8193 // Restore called_emsg for assert_fails().
8194 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196
Bram Moolenaar85077472016-10-16 14:35:48 +02008197/*
8198 * If there is a match fill "shl" and return one.
8199 * Return zero otherwise.
8200 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008201 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008202next_search_hl_pos(
8203 match_T *shl, /* points to a match */
8204 linenr_T lnum,
8205 posmatch_T *posmatch, /* match positions */
8206 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008207{
8208 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008209 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008210
Bram Moolenaarb3414592014-06-17 17:48:32 +02008211 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8212 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008213 llpos_T *pos = &posmatch->pos[i];
8214
8215 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008216 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008217 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008218 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008219 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008220 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008221 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008222 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008223 /* if this match comes before the one at "found" then swap
8224 * them */
8225 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008226 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008227 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008228
Bram Moolenaar85077472016-10-16 14:35:48 +02008229 *pos = posmatch->pos[found];
8230 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008231 }
8232 }
8233 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008234 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008235 }
8236 }
8237 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008238 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008239 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008240 colnr_T start = posmatch->pos[found].col == 0
8241 ? 0 : posmatch->pos[found].col - 1;
8242 colnr_T end = posmatch->pos[found].col == 0
8243 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008244
Bram Moolenaar85077472016-10-16 14:35:48 +02008245 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008246 shl->rm.startpos[0].lnum = 0;
8247 shl->rm.startpos[0].col = start;
8248 shl->rm.endpos[0].lnum = 0;
8249 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008250 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008251 posmatch->cur = found + 1;
8252 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008253 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008254 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008255}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008256#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008257
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008259screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 attrentry_T *aep = NULL;
8262
8263 screen_attr = attr;
8264 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008265#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 && termcap_active
8267#endif
8268 )
8269 {
8270#ifdef FEAT_GUI
8271 if (gui.in_use)
8272 {
8273 char buf[20];
8274
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008275 /* The GUI handles this internally. */
8276 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 OUT_STR(buf);
8278 }
8279 else
8280#endif
8281 {
8282 if (attr > HL_ALL) /* special HL attr. */
8283 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008284 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 aep = syn_cterm_attr2entry(attr);
8286 else
8287 aep = syn_term_attr2entry(attr);
8288 if (aep == NULL) /* did ":syntax clear" */
8289 attr = 0;
8290 else
8291 attr = aep->ae_attr;
8292 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008293 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008295 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008296#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008297 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8298 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8299 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008300#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008301 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008302 /* If the Normal FG color has BOLD attribute and the new HL
8303 * has a FG color defined, clear BOLD. */
8304 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008305 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008307 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008308 out_str(T_UCS);
8309 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008310 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8311 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008313 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008315 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008317 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008318 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
8320 /*
8321 * Output the color or start string after bold etc., in case the
8322 * bold etc. override the color setting.
8323 */
8324 if (aep != NULL)
8325 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008326#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008327 /* When 'termguicolors' is set but fg or bg is unset,
8328 * fall back to the cterm colors. This helps for SpellBad,
8329 * where the GUI uses a red undercurl. */
8330 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008332 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008333 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008334 }
8335 else
8336#endif
8337 if (t_colors > 1)
8338 {
8339 if (aep->ae_u.cterm.fg_color)
8340 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8341 }
8342#ifdef FEAT_TERMGUICOLORS
8343 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8344 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008345 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008346 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 }
8348 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008349#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008350 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008352 if (aep->ae_u.cterm.bg_color)
8353 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8354 }
8355
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008356 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008357 {
8358 if (aep->ae_u.term.start != NULL)
8359 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 }
8361 }
8362 }
8363 }
8364}
8365
8366 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008367screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368{
8369 int do_ME = FALSE; /* output T_ME code */
8370
8371 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008372#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 && termcap_active
8374#endif
8375 )
8376 {
8377#ifdef FEAT_GUI
8378 if (gui.in_use)
8379 {
8380 char buf[20];
8381
8382 /* use internal GUI code */
8383 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8384 OUT_STR(buf);
8385 }
8386 else
8387#endif
8388 {
8389 if (screen_attr > HL_ALL) /* special HL attr. */
8390 {
8391 attrentry_T *aep;
8392
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008393 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {
8395 /*
8396 * Assume that t_me restores the original colors!
8397 */
8398 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008399 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008400#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008401 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8402 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8403 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008404#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008405 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008406#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008407 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8408 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8409 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008410#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008411 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 do_ME = TRUE;
8413 }
8414 else
8415 {
8416 aep = syn_term_attr2entry(screen_attr);
8417 if (aep != NULL && aep->ae_u.term.stop != NULL)
8418 {
8419 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8420 do_ME = TRUE;
8421 else
8422 out_str(aep->ae_u.term.stop);
8423 }
8424 }
8425 if (aep == NULL) /* did ":syntax clear" */
8426 screen_attr = 0;
8427 else
8428 screen_attr = aep->ae_attr;
8429 }
8430
8431 /*
8432 * Often all ending-codes are equal to T_ME. Avoid outputting the
8433 * same sequence several times.
8434 */
8435 if (screen_attr & HL_STANDOUT)
8436 {
8437 if (STRCMP(T_SE, T_ME) == 0)
8438 do_ME = TRUE;
8439 else
8440 out_str(T_SE);
8441 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008442 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008443 {
8444 if (STRCMP(T_UCE, T_ME) == 0)
8445 do_ME = TRUE;
8446 else
8447 out_str(T_UCE);
8448 }
8449 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008450 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {
8452 if (STRCMP(T_UE, T_ME) == 0)
8453 do_ME = TRUE;
8454 else
8455 out_str(T_UE);
8456 }
8457 if (screen_attr & HL_ITALIC)
8458 {
8459 if (STRCMP(T_CZR, T_ME) == 0)
8460 do_ME = TRUE;
8461 else
8462 out_str(T_CZR);
8463 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008464 if (screen_attr & HL_STRIKETHROUGH)
8465 {
8466 if (STRCMP(T_STE, T_ME) == 0)
8467 do_ME = TRUE;
8468 else
8469 out_str(T_STE);
8470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8472 out_str(T_ME);
8473
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008474#ifdef FEAT_TERMGUICOLORS
8475 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008477 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008478 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008479 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008480 term_bg_rgb_color(cterm_normal_bg_gui_color);
8481 }
8482 else
8483#endif
8484 {
8485 if (t_colors > 1)
8486 {
8487 /* set Normal cterm colors */
8488 if (cterm_normal_fg_color != 0)
8489 term_fg_color(cterm_normal_fg_color - 1);
8490 if (cterm_normal_bg_color != 0)
8491 term_bg_color(cterm_normal_bg_color - 1);
8492 if (cterm_normal_fg_bold)
8493 out_str(T_MD);
8494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 }
8496 }
8497 }
8498 screen_attr = 0;
8499}
8500
8501/*
8502 * Reset the colors for a cterm. Used when leaving Vim.
8503 * The machine specific code may override this again.
8504 */
8505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008506reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008508 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {
8510 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008511#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008512 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8513 || cterm_normal_bg_gui_color != INVALCOLOR)
8514 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008515#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {
8519 out_str(T_OP);
8520 screen_attr = -1;
8521 }
8522 if (cterm_normal_fg_bold)
8523 {
8524 out_str(T_ME);
8525 screen_attr = -1;
8526 }
8527 }
8528}
8529
8530/*
8531 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8532 * using the attributes from ScreenAttrs["off"].
8533 */
8534 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008535screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 int attr;
8538
8539 /* Check for illegal values, just in case (could happen just after
8540 * resizing). */
8541 if (row >= screen_Rows || col >= screen_Columns)
8542 return;
8543
Bram Moolenaarae654382019-01-17 21:09:05 +01008544#ifdef FEAT_INS_EXPAND
8545 if (pum_under_menu(row, col))
8546 return;
8547#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008548 /* Outputting a character in the last cell on the screen may scroll the
8549 * screen up. Only do it when the "xn" termcap property is set, otherwise
8550 * mark the character invalid (update it when scrolled up). */
8551 if (*T_XN == NUL
8552 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553#ifdef FEAT_RIGHTLEFT
8554 /* account for first command-line character in rightleft mode */
8555 && !cmdmsg_rl
8556#endif
8557 )
8558 {
8559 ScreenAttrs[off] = (sattr_T)-1;
8560 return;
8561 }
8562
8563 /*
8564 * Stop highlighting first, so it's easier to move the cursor.
8565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 if (screen_char_attr != 0)
8567 attr = screen_char_attr;
8568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 attr = ScreenAttrs[off];
8570 if (screen_attr != attr)
8571 screen_stop_highlight();
8572
8573 windgoto(row, col);
8574
8575 if (screen_attr != attr)
8576 screen_start_highlight(attr);
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 if (enc_utf8 && ScreenLinesUC[off] != 0)
8579 {
8580 char_u buf[MB_MAXBYTES + 1];
8581
Bram Moolenaarcb070082016-04-02 22:14:51 +02008582 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008583 {
8584 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008585#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008586 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008587#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008588 )
8589 {
8590 /* Clear the two screen cells. If the character is actually
8591 * single width it won't change the second cell. */
8592 out_str((char_u *)" ");
8593 term_windgoto(row, col);
8594 }
8595 /* not sure where the cursor is after drawing the ambiguous width
8596 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008597 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008598 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008599 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008601
8602 /* Convert the UTF-8 character to bytes and write it. */
8603 buf[utfc_char2bytes(off, buf)] = NUL;
8604 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 }
8606 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 /* double-byte character in single-width cell */
8611 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8612 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614
8615 screen_cur_col++;
8616}
8617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618/*
8619 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8620 * on the screen at position 'row' and 'col'.
8621 * The attributes of the first byte is used for all. This is required to
8622 * output the two bytes of a double-byte character with nothing in between.
8623 */
8624 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008625screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 /* Check for illegal values (could be wrong when screen was resized). */
8628 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8629 return;
8630
8631 /* Outputting the last character on the screen may scrollup the screen.
8632 * Don't to it! Mark the character invalid (update it when scrolled up) */
8633 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8634 {
8635 ScreenAttrs[off] = (sattr_T)-1;
8636 return;
8637 }
8638
8639 /* Output the first byte normally (positions the cursor), then write the
8640 * second byte directly. */
8641 screen_char(off, row, col);
8642 out_char(ScreenLines[off + 1]);
8643 ++screen_cur_col;
8644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646/*
8647 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8648 * This uses the contents of ScreenLines[] and doesn't change it.
8649 */
8650 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008651screen_draw_rectangle(
8652 int row,
8653 int col,
8654 int height,
8655 int width,
8656 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657{
8658 int r, c;
8659 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008660 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008662 /* Can't use ScreenLines unless initialized */
8663 if (ScreenLines == NULL)
8664 return;
8665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 if (invert)
8667 screen_char_attr = HL_INVERSE;
8668 for (r = row; r < row + height; ++r)
8669 {
8670 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008671 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 for (c = col; c < col + width; ++c)
8673 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008674 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 {
8676 screen_char_2(off + c, r, c);
8677 ++c;
8678 }
8679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {
8681 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008682 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685 }
8686 }
8687 screen_char_attr = 0;
8688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690/*
8691 * Redraw the characters for a vertically split window.
8692 */
8693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008694redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
8696 int col;
8697 int width;
8698
8699# ifdef FEAT_CLIPBOARD
8700 clip_may_clear_selection(row, end - 1);
8701# endif
8702
8703 if (wp == NULL)
8704 {
8705 col = 0;
8706 width = Columns;
8707 }
8708 else
8709 {
8710 col = wp->w_wincol;
8711 width = wp->w_width;
8712 }
8713 screen_draw_rectangle(row, col, end - row, width, FALSE);
8714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008716 static void
8717space_to_screenline(int off, int attr)
8718{
8719 ScreenLines[off] = ' ';
8720 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008721 if (enc_utf8)
8722 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008723}
8724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725/*
8726 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8727 * with character 'c1' in first column followed by 'c2' in the other columns.
8728 * Use attributes 'attr'.
8729 */
8730 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008731screen_fill(
8732 int start_row,
8733 int end_row,
8734 int start_col,
8735 int end_col,
8736 int c1,
8737 int c2,
8738 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739{
8740 int row;
8741 int col;
8742 int off;
8743 int end_off;
8744 int did_delete;
8745 int c;
8746 int norm_term;
8747#if defined(FEAT_GUI) || defined(UNIX)
8748 int force_next = FALSE;
8749#endif
8750
8751 if (end_row > screen_Rows) /* safety check */
8752 end_row = screen_Rows;
8753 if (end_col > screen_Columns) /* safety check */
8754 end_col = screen_Columns;
8755 if (ScreenLines == NULL
8756 || start_row >= end_row
8757 || start_col >= end_col) /* nothing to do */
8758 return;
8759
8760 /* it's a "normal" terminal when not in a GUI or cterm */
8761 norm_term = (
8762#ifdef FEAT_GUI
8763 !gui.in_use &&
8764#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008765 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 for (row = start_row; row < end_row; ++row)
8767 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008768 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008769#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008770 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008771#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008772 )
8773 {
8774 /* When drawing over the right halve of a double-wide char clear
8775 * out the left halve. When drawing over the left halve of a
8776 * double wide-char clear out the right halve. Only needed in a
8777 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008778 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008779 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008780 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008781 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 /*
8784 * Try to use delete-line termcap code, when no attributes or in a
8785 * "normal" terminal, where a bold/italic space is just a
8786 * space.
8787 */
8788 did_delete = FALSE;
8789 if (c2 == ' '
8790 && end_col == Columns
8791 && can_clear(T_CE)
8792 && (attr == 0
8793 || (norm_term
8794 && attr <= HL_ALL
8795 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8796 {
8797 /*
8798 * check if we really need to clear something
8799 */
8800 col = start_col;
8801 if (c1 != ' ') /* don't clear first char */
8802 ++col;
8803
8804 off = LineOffset[row] + col;
8805 end_off = LineOffset[row] + end_col;
8806
8807 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 if (enc_utf8)
8809 while (off < end_off && ScreenLines[off] == ' '
8810 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8811 ++off;
8812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 while (off < end_off && ScreenLines[off] == ' '
8814 && ScreenAttrs[off] == 0)
8815 ++off;
8816 if (off < end_off) /* something to be cleared */
8817 {
8818 col = off - LineOffset[row];
8819 screen_stop_highlight();
8820 term_windgoto(row, col);/* clear rest of this screen line */
8821 out_str(T_CE);
8822 screen_start(); /* don't know where cursor is now */
8823 col = end_col - col;
8824 while (col--) /* clear chars in ScreenLines */
8825 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008826 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 ++off;
8828 }
8829 }
8830 did_delete = TRUE; /* the chars are cleared now */
8831 }
8832
8833 off = LineOffset[row] + start_col;
8834 c = c1;
8835 for (col = start_col; col < end_col; ++col)
8836 {
8837 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008838 || (enc_utf8 && (int)ScreenLinesUC[off]
8839 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 || ScreenAttrs[off] != attr
8841#if defined(FEAT_GUI) || defined(UNIX)
8842 || force_next
8843#endif
8844 )
8845 {
8846#if defined(FEAT_GUI) || defined(UNIX)
8847 /* The bold trick may make a single row of pixels appear in
8848 * the next character. When a bold character is removed, the
8849 * next character should be redrawn too. This happens for our
8850 * own GUI and for some xterms. */
8851 if (
8852# ifdef FEAT_GUI
8853 gui.in_use
8854# endif
8855# if defined(FEAT_GUI) && defined(UNIX)
8856 ||
8857# endif
8858# ifdef UNIX
8859 term_is_xterm
8860# endif
8861 )
8862 {
8863 if (ScreenLines[off] != ' '
8864 && (ScreenAttrs[off] > HL_ALL
8865 || ScreenAttrs[off] & HL_BOLD))
8866 force_next = TRUE;
8867 else
8868 force_next = FALSE;
8869 }
8870#endif
8871 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 if (enc_utf8)
8873 {
8874 if (c >= 0x80)
8875 {
8876 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008877 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879 else
8880 ScreenLinesUC[off] = 0;
8881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 ScreenAttrs[off] = attr;
8883 if (!did_delete || c != ' ')
8884 screen_char(off, row, col);
8885 }
8886 ++off;
8887 if (col == start_col)
8888 {
8889 if (did_delete)
8890 break;
8891 c = c2;
8892 }
8893 }
8894 if (end_col == Columns)
8895 LineWraps[row] = FALSE;
8896 if (row == Rows - 1) /* overwritten the command line */
8897 {
8898 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008899 if (start_col == 0 && end_col == Columns
8900 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008902 if (start_col == 0)
8903 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905 }
8906}
8907
8908/*
8909 * Check if there should be a delay. Used before clearing or redrawing the
8910 * screen or the command line.
8911 */
8912 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008913check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914{
8915 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8916 && !did_wait_return
8917 && emsg_silent == 0)
8918 {
8919 out_flush();
8920 ui_delay(1000L, TRUE);
8921 emsg_on_display = FALSE;
8922 if (check_msg_scroll)
8923 msg_scroll = FALSE;
8924 }
8925}
8926
8927/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008928 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8929 */
8930 static void
8931clear_TabPageIdxs(void)
8932{
8933 int scol;
8934
8935 for (scol = 0; scol < Columns; ++scol)
8936 TabPageIdxs[scol] = 0;
8937}
8938
8939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008941 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 * Returns TRUE if there is a valid screen to write to.
8943 * Returns FALSE when starting up and screen not initialized yet.
8944 */
8945 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008946screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008948 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 return (ScreenLines != NULL);
8950}
8951
8952/*
8953 * Resize the shell to Rows and Columns.
8954 * Allocate ScreenLines[] and associated items.
8955 *
8956 * There may be some time between setting Rows and Columns and (re)allocating
8957 * ScreenLines[]. This happens when starting up and when (manually) changing
8958 * the shell size. Always use screen_Rows and screen_Columns to access items
8959 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8960 * final size of the shell is needed.
8961 */
8962 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008963screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 int new_row, old_row;
8966#ifdef FEAT_GUI
8967 int old_Rows;
8968#endif
8969 win_T *wp;
8970 int outofmem = FALSE;
8971 int len;
8972 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008974 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008976 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 sattr_T *new_ScreenAttrs;
8978 unsigned *new_LineOffset;
8979 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008980 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008981 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008983 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008984 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008986retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 /*
8988 * Allocation of the screen buffers is done only when the size changes and
8989 * when Rows and Columns have been set and we have started doing full
8990 * screen stuff.
8991 */
8992 if ((ScreenLines != NULL
8993 && Rows == screen_Rows
8994 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 && enc_utf8 == (ScreenLinesUC != NULL)
8996 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008997 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 || Rows == 0
8999 || Columns == 0
9000 || (!full_screen && ScreenLines == NULL))
9001 return;
9002
9003 /*
9004 * It's possible that we produce an out-of-memory message below, which
9005 * will cause this function to be called again. To break the loop, just
9006 * return here.
9007 */
9008 if (entered)
9009 return;
9010 entered = TRUE;
9011
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009012 /*
9013 * Note that the window sizes are updated before reallocating the arrays,
9014 * thus we must not redraw here!
9015 */
9016 ++RedrawingDisabled;
9017
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 win_new_shellsize(); /* fit the windows in the new sized shell */
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 comp_col(); /* recompute columns for shown command and ruler */
9021
9022 /*
9023 * We're changing the size of the screen.
9024 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9025 * - Move lines from the old arrays into the new arrays, clear extra
9026 * lines (unless the screen is going to be cleared).
9027 * - Free the old arrays.
9028 *
9029 * If anything fails, make ScreenLines NULL, so we don't do anything!
9030 * Continuing with the old ScreenLines may result in a crash, because the
9031 * size is wrong.
9032 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009033 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009035 if (aucmd_win != NULL)
9036 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009037#ifdef FEAT_TEXT_PROP
9038 // global popup windows
9039 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9040 win_free_lsize(wp);
9041 // tab-local popup windows
9042 FOR_ALL_TABPAGES(tp)
9043 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9044 win_free_lsize(wp);
9045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009047 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009048 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 if (enc_utf8)
9050 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009051 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009052 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009053 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9054 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 }
9056 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009057 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9058 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9059 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9060 new_LineWraps = LALLOC_MULT(char_u, Rows);
9061 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009063 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 {
9065 if (win_alloc_lines(wp) == FAIL)
9066 {
9067 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009068 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 }
9070 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009071 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9072 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009073 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009074#ifdef FEAT_TEXT_PROP
9075 // global popup windows
9076 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9077 if (win_alloc_lines(wp) == FAIL)
9078 {
9079 outofmem = TRUE;
9080 goto give_up;
9081 }
9082 // tab-local popup windows
9083 FOR_ALL_TABPAGES(tp)
9084 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9085 if (win_alloc_lines(wp) == FAIL)
9086 {
9087 outofmem = TRUE;
9088 goto give_up;
9089 }
9090#endif
9091
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009092give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009094 for (i = 0; i < p_mco; ++i)
9095 if (new_ScreenLinesC[i] == NULL)
9096 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009098 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 || new_ScreenAttrs == NULL
9101 || new_LineOffset == NULL
9102 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009103 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 || outofmem)
9105 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009106 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009107 {
9108 /* guess the size */
9109 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9110
9111 /* Remember we did this to avoid getting outofmem messages over
9112 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009113 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009114 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009115 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009116 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009117 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009118 VIM_CLEAR(new_ScreenLinesC[i]);
9119 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009120 VIM_CLEAR(new_ScreenAttrs);
9121 VIM_CLEAR(new_LineOffset);
9122 VIM_CLEAR(new_LineWraps);
9123 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 }
9125 else
9126 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009127 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 for (new_row = 0; new_row < Rows; ++new_row)
9130 {
9131 new_LineOffset[new_row] = new_row * Columns;
9132 new_LineWraps[new_row] = FALSE;
9133
9134 /*
9135 * If the screen is not going to be cleared, copy as much as
9136 * possible from the old screen to the new one and clear the rest
9137 * (used when resizing the window at the "--more--" prompt or when
9138 * executing an external command, for the GUI).
9139 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009140 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
9142 (void)vim_memset(new_ScreenLines + new_row * Columns,
9143 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 if (enc_utf8)
9145 {
9146 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9147 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009148 for (i = 0; i < p_mco; ++i)
9149 (void)vim_memset(new_ScreenLinesC[i]
9150 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 0, (size_t)Columns * sizeof(u8char_T));
9152 }
9153 if (enc_dbcs == DBCS_JPNU)
9154 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9155 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9157 0, (size_t)Columns * sizeof(sattr_T));
9158 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009159 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 {
9161 if (screen_Columns < Columns)
9162 len = screen_Columns;
9163 else
9164 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009165 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009166 * may be invalid now. Also when p_mco changes. */
9167 if (!(enc_utf8 && ScreenLinesUC == NULL)
9168 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009169 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9170 ScreenLines + LineOffset[old_row],
9171 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009172 if (enc_utf8 && ScreenLinesUC != NULL
9173 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 {
9175 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9176 ScreenLinesUC + LineOffset[old_row],
9177 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009178 for (i = 0; i < p_mco; ++i)
9179 mch_memmove(new_ScreenLinesC[i]
9180 + new_LineOffset[new_row],
9181 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 (size_t)len * sizeof(u8char_T));
9183 }
9184 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9185 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9186 ScreenLines2 + LineOffset[old_row],
9187 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9189 ScreenAttrs + LineOffset[old_row],
9190 (size_t)len * sizeof(sattr_T));
9191 }
9192 }
9193 }
9194 /* Use the last line of the screen for the current line. */
9195 current_ScreenLine = new_ScreenLines + Rows * Columns;
9196 }
9197
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009198 free_screenlines();
9199
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009202 for (i = 0; i < p_mco; ++i)
9203 ScreenLinesC[i] = new_ScreenLinesC[i];
9204 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 ScreenAttrs = new_ScreenAttrs;
9207 LineOffset = new_LineOffset;
9208 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009209 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
9211 /* It's important that screen_Rows and screen_Columns reflect the actual
9212 * size of ScreenLines[]. Set them before calling anything. */
9213#ifdef FEAT_GUI
9214 old_Rows = screen_Rows;
9215#endif
9216 screen_Rows = Rows;
9217 screen_Columns = Columns;
9218
9219 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009220 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222#ifdef FEAT_GUI
9223 else if (gui.in_use
9224 && !gui.starting
9225 && ScreenLines != NULL
9226 && old_Rows != Rows)
9227 {
9228 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9229 /*
9230 * Adjust the position of the cursor, for when executing an external
9231 * command.
9232 */
9233 if (msg_row >= Rows) /* Rows got smaller */
9234 msg_row = Rows - 1; /* put cursor at last row */
9235 else if (Rows > old_Rows) /* Rows got bigger */
9236 msg_row += Rows - old_Rows; /* put cursor in same place */
9237 if (msg_col >= Columns) /* Columns got smaller */
9238 msg_col = Columns - 1; /* put cursor at last column */
9239 }
9240#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009241 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009244 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009245
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009246 /*
9247 * Do not apply autocommands more than 3 times to avoid an endless loop
9248 * in case applying autocommands always changes Rows or Columns.
9249 */
9250 if (starting == 0 && ++retry_count <= 3)
9251 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009252 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009253 /* In rare cases, autocommands may have altered Rows or Columns,
9254 * jump back to check if we need to allocate the screen again. */
9255 goto retry;
9256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257}
9258
9259 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009260free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009261{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009262 int i;
9263
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009264 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009265 for (i = 0; i < Screen_mco; ++i)
9266 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009267 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009268 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009269 vim_free(ScreenAttrs);
9270 vim_free(LineOffset);
9271 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009272 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009273}
9274
9275 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009276screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277{
9278 check_for_delay(FALSE);
9279 screenalloc(FALSE); /* allocate screen buffers if size changed */
9280 screenclear2(); /* clear the screen */
9281}
9282
9283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009284screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 int i;
9287
9288 if (starting == NO_SCREEN || ScreenLines == NULL
9289#ifdef FEAT_GUI
9290 || (gui.in_use && gui.starting)
9291#endif
9292 )
9293 return;
9294
9295#ifdef FEAT_GUI
9296 if (!gui.in_use)
9297#endif
9298 screen_attr = -1; /* force setting the Normal colors */
9299 screen_stop_highlight(); /* don't want highlighting here */
9300
9301#ifdef FEAT_CLIPBOARD
9302 /* disable selection without redrawing it */
9303 clip_scroll_selection(9999);
9304#endif
9305
9306 /* blank out ScreenLines */
9307 for (i = 0; i < Rows; ++i)
9308 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009309 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 LineWraps[i] = FALSE;
9311 }
9312
9313 if (can_clear(T_CL))
9314 {
9315 out_str(T_CL); /* clear the display */
9316 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009317 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 }
9319 else
9320 {
9321 /* can't clear the screen, mark all chars with invalid attributes */
9322 for (i = 0; i < Rows; ++i)
9323 lineinvalid(LineOffset[i], (int)Columns);
9324 clear_cmdline = TRUE;
9325 }
9326
9327 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9328
9329 win_rest_invalid(firstwin);
9330 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009331 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 if (must_redraw == CLEAR) /* no need to clear again */
9333 must_redraw = NOT_VALID;
9334 compute_cmdrow();
9335 msg_row = cmdline_row; /* put cursor on last line for messages */
9336 msg_col = 0;
9337 screen_start(); /* don't know where cursor is now */
9338 msg_scrolled = 0; /* can't scroll back */
9339 msg_didany = FALSE;
9340 msg_didout = FALSE;
9341}
9342
9343/*
9344 * Clear one line in ScreenLines.
9345 */
9346 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009347lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 if (enc_utf8)
9351 (void)vim_memset(ScreenLinesUC + off, 0,
9352 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009353 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354}
9355
9356/*
9357 * Mark one line in ScreenLines invalid by setting the attributes to an
9358 * invalid value.
9359 */
9360 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009361lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362{
9363 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9364}
9365
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366/*
9367 * Copy part of a Screenline for vertically split window "wp".
9368 */
9369 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009370linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
9372 unsigned off_to = LineOffset[to] + wp->w_wincol;
9373 unsigned off_from = LineOffset[from] + wp->w_wincol;
9374
9375 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9376 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 if (enc_utf8)
9378 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009379 int i;
9380
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9382 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009383 for (i = 0; i < p_mco; ++i)
9384 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9385 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 }
9387 if (enc_dbcs == DBCS_JPNU)
9388 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9389 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9391 wp->w_width * sizeof(sattr_T));
9392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393
9394/*
9395 * Return TRUE if clearing with term string "p" would work.
9396 * It can't work when the string is empty or it won't set the right background.
9397 */
9398 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009399can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401 return (*p != NUL && (t_colors <= 1
9402#ifdef FEAT_GUI
9403 || gui.in_use
9404#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009405#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009406 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009407 || (!p_tgc && cterm_normal_bg_color == 0)
9408#else
9409 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009410#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009411 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412}
9413
9414/*
9415 * Reset cursor position. Use whenever cursor was moved because of outputting
9416 * something directly to the screen (shell commands) or a terminal control
9417 * code.
9418 */
9419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009420screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
9422 screen_cur_row = screen_cur_col = 9999;
9423}
9424
9425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 * Move the cursor to position "row","col" in the screen.
9427 * This tries to find the most efficient way to move, minimizing the number of
9428 * characters sent to the terminal.
9429 */
9430 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009431windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009433 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 int i;
9435 int plan;
9436 int cost;
9437 int wouldbe_col;
9438 int noinvcurs;
9439 char_u *bs;
9440 int goto_cost;
9441 int attr;
9442
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009443#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9445
9446#define PLAN_LE 1
9447#define PLAN_CR 2
9448#define PLAN_NL 3
9449#define PLAN_WRITE 4
9450 /* Can't use ScreenLines unless initialized */
9451 if (ScreenLines == NULL)
9452 return;
9453
9454 if (col != screen_cur_col || row != screen_cur_row)
9455 {
9456 /* Check for valid position. */
9457 if (row < 0) /* window without text lines? */
9458 row = 0;
9459 if (row >= screen_Rows)
9460 row = screen_Rows - 1;
9461 if (col >= screen_Columns)
9462 col = screen_Columns - 1;
9463
9464 /* check if no cursor movement is allowed in highlight mode */
9465 if (screen_attr && *T_MS == NUL)
9466 noinvcurs = HIGHL_COST;
9467 else
9468 noinvcurs = 0;
9469 goto_cost = GOTO_COST + noinvcurs;
9470
9471 /*
9472 * Plan how to do the positioning:
9473 * 1. Use CR to move it to column 0, same row.
9474 * 2. Use T_LE to move it a few columns to the left.
9475 * 3. Use NL to move a few lines down, column 0.
9476 * 4. Move a few columns to the right with T_ND or by writing chars.
9477 *
9478 * Don't do this if the cursor went beyond the last column, the cursor
9479 * position is unknown then (some terminals wrap, some don't )
9480 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009481 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 * characters to move the cursor to the right.
9483 */
9484 if (row >= screen_cur_row && screen_cur_col < Columns)
9485 {
9486 /*
9487 * If the cursor is in the same row, bigger col, we can use CR
9488 * or T_LE.
9489 */
9490 bs = NULL; /* init for GCC */
9491 attr = screen_attr;
9492 if (row == screen_cur_row && col < screen_cur_col)
9493 {
9494 /* "le" is preferred over "bc", because "bc" is obsolete */
9495 if (*T_LE)
9496 bs = T_LE; /* "cursor left" */
9497 else
9498 bs = T_BC; /* "backspace character (old) */
9499 if (*bs)
9500 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9501 else
9502 cost = 999;
9503 if (col + 1 < cost) /* using CR is less characters */
9504 {
9505 plan = PLAN_CR;
9506 wouldbe_col = 0;
9507 cost = 1; /* CR is just one character */
9508 }
9509 else
9510 {
9511 plan = PLAN_LE;
9512 wouldbe_col = col;
9513 }
9514 if (noinvcurs) /* will stop highlighting */
9515 {
9516 cost += noinvcurs;
9517 attr = 0;
9518 }
9519 }
9520
9521 /*
9522 * If the cursor is above where we want to be, we can use CR LF.
9523 */
9524 else if (row > screen_cur_row)
9525 {
9526 plan = PLAN_NL;
9527 wouldbe_col = 0;
9528 cost = (row - screen_cur_row) * 2; /* CR LF */
9529 if (noinvcurs) /* will stop highlighting */
9530 {
9531 cost += noinvcurs;
9532 attr = 0;
9533 }
9534 }
9535
9536 /*
9537 * If the cursor is in the same row, smaller col, just use write.
9538 */
9539 else
9540 {
9541 plan = PLAN_WRITE;
9542 wouldbe_col = screen_cur_col;
9543 cost = 0;
9544 }
9545
9546 /*
9547 * Check if any characters that need to be written have the
9548 * correct attributes. Also avoid UTF-8 characters.
9549 */
9550 i = col - wouldbe_col;
9551 if (i > 0)
9552 cost += i;
9553 if (cost < goto_cost && i > 0)
9554 {
9555 /*
9556 * Check if the attributes are correct without additionally
9557 * stopping highlighting.
9558 */
9559 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9560 while (i && *p++ == attr)
9561 --i;
9562 if (i != 0)
9563 {
9564 /*
9565 * Try if it works when highlighting is stopped here.
9566 */
9567 if (*--p == 0)
9568 {
9569 cost += noinvcurs;
9570 while (i && *p++ == 0)
9571 --i;
9572 }
9573 if (i != 0)
9574 cost = 999; /* different attributes, don't do it */
9575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 if (enc_utf8)
9577 {
9578 /* Don't use an UTF-8 char for positioning, it's slow. */
9579 for (i = wouldbe_col; i < col; ++i)
9580 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9581 {
9582 cost = 999;
9583 break;
9584 }
9585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 }
9587
9588 /*
9589 * We can do it without term_windgoto()!
9590 */
9591 if (cost < goto_cost)
9592 {
9593 if (plan == PLAN_LE)
9594 {
9595 if (noinvcurs)
9596 screen_stop_highlight();
9597 while (screen_cur_col > col)
9598 {
9599 out_str(bs);
9600 --screen_cur_col;
9601 }
9602 }
9603 else if (plan == PLAN_CR)
9604 {
9605 if (noinvcurs)
9606 screen_stop_highlight();
9607 out_char('\r');
9608 screen_cur_col = 0;
9609 }
9610 else if (plan == PLAN_NL)
9611 {
9612 if (noinvcurs)
9613 screen_stop_highlight();
9614 while (screen_cur_row < row)
9615 {
9616 out_char('\n');
9617 ++screen_cur_row;
9618 }
9619 screen_cur_col = 0;
9620 }
9621
9622 i = col - screen_cur_col;
9623 if (i > 0)
9624 {
9625 /*
9626 * Use cursor-right if it's one character only. Avoids
9627 * removing a line of pixels from the last bold char, when
9628 * using the bold trick in the GUI.
9629 */
9630 if (T_ND[0] != NUL && T_ND[1] == NUL)
9631 {
9632 while (i-- > 0)
9633 out_char(*T_ND);
9634 }
9635 else
9636 {
9637 int off;
9638
9639 off = LineOffset[row] + screen_cur_col;
9640 while (i-- > 0)
9641 {
9642 if (ScreenAttrs[off] != screen_attr)
9643 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (enc_dbcs == DBCS_JPNU
9647 && ScreenLines[off] == 0x8e)
9648 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 ++off;
9650 }
9651 }
9652 }
9653 }
9654 }
9655 else
9656 cost = 999;
9657
9658 if (cost >= goto_cost)
9659 {
9660 if (noinvcurs)
9661 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009662 if (row == screen_cur_row && (col > screen_cur_col)
9663 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 term_cursor_right(col - screen_cur_col);
9665 else
9666 term_windgoto(row, col);
9667 }
9668 screen_cur_row = row;
9669 screen_cur_col = col;
9670 }
9671}
9672
9673/*
9674 * Set cursor to its position in the current window.
9675 */
9676 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009677setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009679 setcursor_mayforce(FALSE);
9680}
9681
9682/*
9683 * Set cursor to its position in the current window.
9684 * When "force" is TRUE also when not redrawing.
9685 */
9686 void
9687setcursor_mayforce(int force)
9688{
9689 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 {
9691 validate_cursor();
9692 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009693 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009695 /* With 'rightleft' set and the cursor on a double-wide
9696 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009697 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9698 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009699 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009700 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#endif
9702 curwin->w_wcol));
9703 }
9704}
9705
9706
9707/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009708 * Insert 'line_count' lines at 'row' in window 'wp'.
9709 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9710 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 * scrolling.
9712 * Returns FAIL if the lines are not inserted, OK for success.
9713 */
9714 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009715win_ins_lines(
9716 win_T *wp,
9717 int row,
9718 int line_count,
9719 int invalid,
9720 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 int did_delete;
9723 int nextrow;
9724 int lastrow;
9725 int retval;
9726
9727 if (invalid)
9728 wp->w_lines_valid = 0;
9729
9730 if (wp->w_height < 5)
9731 return FAIL;
9732
9733 if (line_count > wp->w_height - row)
9734 line_count = wp->w_height - row;
9735
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009736 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 if (retval != MAYBE)
9738 return retval;
9739
9740 /*
9741 * If there is a next window or a status line, we first try to delete the
9742 * lines at the bottom to avoid messing what is after the window.
9743 * If this fails and there are following windows, don't do anything to avoid
9744 * messing up those windows, better just redraw.
9745 */
9746 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 if (wp->w_next != NULL || wp->w_status_height)
9748 {
9749 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009750 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 did_delete = TRUE;
9752 else if (wp->w_next)
9753 return FAIL;
9754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 /*
9756 * if no lines deleted, blank the lines that will end up below the window
9757 */
9758 if (!did_delete)
9759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009762 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 lastrow = nextrow + line_count;
9764 if (lastrow > Rows)
9765 lastrow = Rows;
9766 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009767 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 ' ', ' ', 0);
9769 }
9770
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009771 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 == FAIL)
9773 {
9774 /* deletion will have messed up other windows */
9775 if (did_delete)
9776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 win_rest_invalid(W_NEXT(wp));
9779 }
9780 return FAIL;
9781 }
9782
9783 return OK;
9784}
9785
9786/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009787 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9789 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9790 * scrolling
9791 * Return OK for success, FAIL if the lines are not deleted.
9792 */
9793 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009794win_del_lines(
9795 win_T *wp,
9796 int row,
9797 int line_count,
9798 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009799 int mayclear,
9800 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
9802 int retval;
9803
9804 if (invalid)
9805 wp->w_lines_valid = 0;
9806
9807 if (line_count > wp->w_height - row)
9808 line_count = wp->w_height - row;
9809
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009810 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 if (retval != MAYBE)
9812 return retval;
9813
9814 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009815 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 return FAIL;
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 /*
9819 * If there are windows or status lines below, try to put them at the
9820 * correct place. If we can't do that, they have to be redrawn.
9821 */
9822 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9823 {
9824 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009825 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
9827 wp->w_redr_status = TRUE;
9828 win_rest_invalid(wp->w_next);
9829 }
9830 }
9831 /*
9832 * If this is the last window and there is no status line, redraw the
9833 * command line later.
9834 */
9835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 redraw_cmdline = TRUE;
9837 return OK;
9838}
9839
9840/*
9841 * Common code for win_ins_lines() and win_del_lines().
9842 * Returns OK or FAIL when the work has been done.
9843 * Returns MAYBE when not finished yet.
9844 */
9845 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009846win_do_lines(
9847 win_T *wp,
9848 int row,
9849 int line_count,
9850 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009851 int del,
9852 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853{
9854 int retval;
9855
9856 if (!redrawing() || line_count <= 0)
9857 return FAIL;
9858
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009859 /* When inserting lines would result in loss of command output, just redraw
9860 * the lines. */
9861 if (no_win_do_lines_ins && !del)
9862 return FAIL;
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009865 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009867 if (!no_win_do_lines_ins)
9868 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 return FAIL;
9870 }
9871
9872 /*
9873 * Delete all remaining lines
9874 */
9875 if (row + line_count >= wp->w_height)
9876 {
9877 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009878 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 ' ', ' ', 0);
9880 return OK;
9881 }
9882
9883 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009884 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009886 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009888 if (!no_win_do_lines_ins)
9889 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890
9891 /*
9892 * If the terminal can set a scroll region, use that.
9893 * Always do this in a vertically split window. This will redraw from
9894 * ScreenLines[] when t_CV isn't defined. That's faster than using
9895 * win_line().
9896 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009897 * a character in the lower right corner of the scroll region may cause a
9898 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009900 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 scroll_region_set(wp, row);
9904 if (del)
9905 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009906 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 else
9908 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009909 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 scroll_region_reset();
9912 return retval;
9913 }
9914
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917
9918 return MAYBE;
9919}
9920
9921/*
9922 * window 'wp' and everything after it is messed up, mark it for redraw
9923 */
9924 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009925win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 {
9929 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 wp->w_redr_status = TRUE;
9931 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 }
9933 redraw_cmdline = TRUE;
9934}
9935
9936/*
9937 * The rest of the routines in this file perform screen manipulations. The
9938 * given operation is performed physically on the screen. The corresponding
9939 * change is also made to the internal screen image. In this way, the editor
9940 * anticipates the effect of editing changes on the appearance of the screen.
9941 * That way, when we call screenupdate a complete redraw isn't usually
9942 * necessary. Another advantage is that we can keep adding code to anticipate
9943 * screen changes, and in the meantime, everything still works.
9944 */
9945
9946/*
9947 * types for inserting or deleting lines
9948 */
9949#define USE_T_CAL 1
9950#define USE_T_CDL 2
9951#define USE_T_AL 3
9952#define USE_T_CE 4
9953#define USE_T_DL 5
9954#define USE_T_SR 6
9955#define USE_NL 7
9956#define USE_T_CD 8
9957#define USE_REDRAW 9
9958
9959/*
9960 * insert lines on the screen and update ScreenLines[]
9961 * 'end' is the line after the scrolled part. Normally it is Rows.
9962 * When scrolling region used 'off' is the offset from the top for the region.
9963 * 'row' and 'end' are relative to the start of the region.
9964 *
9965 * return FAIL for failure, OK for success.
9966 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009967 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009968screen_ins_lines(
9969 int off,
9970 int row,
9971 int line_count,
9972 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009973 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009974 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
9976 int i;
9977 int j;
9978 unsigned temp;
9979 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009980 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 int type;
9982 int result_empty;
9983 int can_ce = can_clear(T_CE);
9984
9985 /*
9986 * FAIL if
9987 * - there is no valid screen
9988 * - the screen has to be redrawn completely
9989 * - the line count is less than one
9990 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009991 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009993 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9994#ifdef FEAT_CLIPBOARD
9995 || (clip_star.state != SELECT_CLEARED
9996 && redrawing_for_callback > 0)
9997#endif
9998 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 return FAIL;
10000
10001 /*
10002 * There are seven ways to insert lines:
10003 * 0. When in a vertically split window and t_CV isn't set, redraw the
10004 * characters from ScreenLines[].
10005 * 1. Use T_CD (clear to end of display) if it exists and the result of
10006 * the insert is just empty lines
10007 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10008 * present or line_count > 1. It looks better if we do all the inserts
10009 * at once.
10010 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10011 * insert is just empty lines and T_CE is not present or line_count >
10012 * 1.
10013 * 4. Use T_AL (insert line) if it exists.
10014 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10015 * just empty lines.
10016 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10017 * just empty lines.
10018 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10019 * the 'da' flag is not set or we have clear line capability.
10020 * 8. redraw the characters from ScreenLines[].
10021 *
10022 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10023 * the scrollbar for the window. It does have insert line, use that if it
10024 * exists.
10025 */
10026 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10028 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010029 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 type = USE_T_CD;
10031 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10032 type = USE_T_CAL;
10033 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10034 type = USE_T_CDL;
10035 else if (*T_AL != NUL)
10036 type = USE_T_AL;
10037 else if (can_ce && result_empty)
10038 type = USE_T_CE;
10039 else if (*T_DL != NUL && result_empty)
10040 type = USE_T_DL;
10041 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10042 type = USE_T_SR;
10043 else
10044 return FAIL;
10045
10046 /*
10047 * For clearing the lines screen_del_lines() is used. This will also take
10048 * care of t_db if necessary.
10049 */
10050 if (type == USE_T_CD || type == USE_T_CDL ||
10051 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010052 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054 /*
10055 * If text is retained below the screen, first clear or delete as many
10056 * lines at the bottom of the window as are about to be inserted so that
10057 * the deleted lines won't later surface during a screen_del_lines.
10058 */
10059 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010060 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
10062#ifdef FEAT_CLIPBOARD
10063 /* Remove a modeless selection when inserting lines halfway the screen
10064 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010065 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010066 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 else
10068 clip_scroll_selection(-line_count);
10069#endif
10070
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071#ifdef FEAT_GUI
10072 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10073 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010074 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075#endif
10076
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010077 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10078 cursor_col = wp->w_wincol;
10079
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 if (*T_CCS != NUL) /* cursor relative to region */
10081 cursor_row = row;
10082 else
10083 cursor_row = row + off;
10084
10085 /*
10086 * Shift LineOffset[] line_count down to reflect the inserted lines.
10087 * Clear the inserted lines in ScreenLines[].
10088 */
10089 row += off;
10090 end += off;
10091 for (i = 0; i < line_count; ++i)
10092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 if (wp != NULL && wp->w_width != Columns)
10094 {
10095 /* need to copy part of a line */
10096 j = end - 1 - i;
10097 while ((j -= line_count) >= row)
10098 linecopy(j + line_count, j, wp);
10099 j += line_count;
10100 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010101 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10102 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else
10104 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10105 LineWraps[j] = FALSE;
10106 }
10107 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 {
10109 j = end - 1 - i;
10110 temp = LineOffset[j];
10111 while ((j -= line_count) >= row)
10112 {
10113 LineOffset[j + line_count] = LineOffset[j];
10114 LineWraps[j + line_count] = LineWraps[j];
10115 }
10116 LineOffset[j + line_count] = temp;
10117 LineWraps[j + line_count] = FALSE;
10118 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010119 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 else
10121 lineinvalid(temp, (int)Columns);
10122 }
10123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
10125 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010126 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010127 if (clear_attr != 0)
10128 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 /* redraw the characters */
10131 if (type == USE_REDRAW)
10132 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010133 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 {
10135 term_append_lines(line_count);
10136 screen_start(); /* don't know where cursor is now */
10137 }
10138 else
10139 {
10140 for (i = 0; i < line_count; i++)
10141 {
10142 if (type == USE_T_AL)
10143 {
10144 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010145 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 out_str(T_AL);
10147 }
10148 else /* type == USE_T_SR */
10149 out_str(T_SR);
10150 screen_start(); /* don't know where cursor is now */
10151 }
10152 }
10153
10154 /*
10155 * With scroll-reverse and 'da' flag set we need to clear the lines that
10156 * have been scrolled down into the region.
10157 */
10158 if (type == USE_T_SR && *T_DA)
10159 {
10160 for (i = 0; i < line_count; ++i)
10161 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010162 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 out_str(T_CE);
10164 screen_start(); /* don't know where cursor is now */
10165 }
10166 }
10167
10168#ifdef FEAT_GUI
10169 gui_can_update_cursor();
10170 if (gui.in_use)
10171 out_flush(); /* always flush after a scroll */
10172#endif
10173 return OK;
10174}
10175
10176/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010177 * Delete lines on the screen and update ScreenLines[].
10178 * "end" is the line after the scrolled part. Normally it is Rows.
10179 * When scrolling region used "off" is the offset from the top for the region.
10180 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 *
10182 * Return OK for success, FAIL if the lines are not deleted.
10183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010185screen_del_lines(
10186 int off,
10187 int row,
10188 int line_count,
10189 int end,
10190 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010191 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010192 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193{
10194 int j;
10195 int i;
10196 unsigned temp;
10197 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010198 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 int cursor_end;
10200 int result_empty; /* result is empty until end of region */
10201 int can_delete; /* deleting line codes can be used */
10202 int type;
10203
10204 /*
10205 * FAIL if
10206 * - there is no valid screen
10207 * - the screen has to be redrawn completely
10208 * - the line count is less than one
10209 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010210 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010212 if (!screen_valid(TRUE) || line_count <= 0
10213 || (!force && line_count > p_ttyscroll)
10214#ifdef FEAT_CLIPBOARD
10215 || (clip_star.state != SELECT_CLEARED
10216 && redrawing_for_callback > 0)
10217#endif
10218 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 return FAIL;
10220
10221 /*
10222 * Check if the rest of the current region will become empty.
10223 */
10224 result_empty = row + line_count >= end;
10225
10226 /*
10227 * We can delete lines only when 'db' flag not set or when 'ce' option
10228 * available.
10229 */
10230 can_delete = (*T_DB == NUL || can_clear(T_CE));
10231
10232 /*
10233 * There are six ways to delete lines:
10234 * 0. When in a vertically split window and t_CV isn't set, redraw the
10235 * characters from ScreenLines[].
10236 * 1. Use T_CD if it exists and the result is empty.
10237 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10238 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10239 * none of the other ways work.
10240 * 4. Use T_CE (erase line) if the result is empty.
10241 * 5. Use T_DL (delete line) if it exists.
10242 * 6. redraw the characters from ScreenLines[].
10243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10245 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010246 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 type = USE_T_CD;
10248#if defined(__BEOS__) && defined(BEOS_DR8)
10249 /*
10250 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10251 * its internal termcap... this works okay for tests which test *T_DB !=
10252 * NUL. It has the disadvantage that the user cannot use any :set t_*
10253 * command to get T_DB (back) to empty_option, only :set term=... will do
10254 * the trick...
10255 * Anyway, this hack will hopefully go away with the next OS release.
10256 * (Olaf Seibert)
10257 */
10258 else if (row == 0 && T_DB == empty_option
10259 && (line_count == 1 || *T_CDL == NUL))
10260#else
10261 else if (row == 0 && (
10262#ifndef AMIGA
10263 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10264 * up, so use delete-line command */
10265 line_count == 1 ||
10266#endif
10267 *T_CDL == NUL))
10268#endif
10269 type = USE_NL;
10270 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10271 type = USE_T_CDL;
10272 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010273 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 type = USE_T_CE;
10275 else if (*T_DL != NUL && can_delete)
10276 type = USE_T_DL;
10277 else if (*T_CDL != NUL && can_delete)
10278 type = USE_T_CDL;
10279 else
10280 return FAIL;
10281
10282#ifdef FEAT_CLIPBOARD
10283 /* Remove a modeless selection when deleting lines halfway the screen or
10284 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010285 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010286 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 else
10288 clip_scroll_selection(line_count);
10289#endif
10290
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291#ifdef FEAT_GUI
10292 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10293 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010294 gui_dont_update_cursor(gui.cursor_row >= row + off
10295 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296#endif
10297
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010298 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10299 cursor_col = wp->w_wincol;
10300
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 if (*T_CCS != NUL) /* cursor relative to region */
10302 {
10303 cursor_row = row;
10304 cursor_end = end;
10305 }
10306 else
10307 {
10308 cursor_row = row + off;
10309 cursor_end = end + off;
10310 }
10311
10312 /*
10313 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10314 * Clear the inserted lines in ScreenLines[].
10315 */
10316 row += off;
10317 end += off;
10318 for (i = 0; i < line_count; ++i)
10319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 if (wp != NULL && wp->w_width != Columns)
10321 {
10322 /* need to copy part of a line */
10323 j = row + i;
10324 while ((j += line_count) <= end - 1)
10325 linecopy(j - line_count, j, wp);
10326 j -= line_count;
10327 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010328 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10329 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 else
10331 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10332 LineWraps[j] = FALSE;
10333 }
10334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 {
10336 /* whole width, moving the line pointers is faster */
10337 j = row + i;
10338 temp = LineOffset[j];
10339 while ((j += line_count) <= end - 1)
10340 {
10341 LineOffset[j - line_count] = LineOffset[j];
10342 LineWraps[j - line_count] = LineWraps[j];
10343 }
10344 LineOffset[j - line_count] = temp;
10345 LineWraps[j - line_count] = FALSE;
10346 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010347 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 else
10349 lineinvalid(temp, (int)Columns);
10350 }
10351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010353 if (screen_attr != clear_attr)
10354 screen_stop_highlight();
10355 if (clear_attr != 0)
10356 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 /* redraw the characters */
10359 if (type == USE_REDRAW)
10360 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010361 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010363 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 out_str(T_CD);
10365 screen_start(); /* don't know where cursor is now */
10366 }
10367 else if (type == USE_T_CDL)
10368 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010369 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 term_delete_lines(line_count);
10371 screen_start(); /* don't know where cursor is now */
10372 }
10373 /*
10374 * Deleting lines at top of the screen or scroll region: Just scroll
10375 * the whole screen (scroll region) up by outputting newlines on the
10376 * last line.
10377 */
10378 else if (type == USE_NL)
10379 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010380 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 for (i = line_count; --i >= 0; )
10382 out_char('\n'); /* cursor will remain on same line */
10383 }
10384 else
10385 {
10386 for (i = line_count; --i >= 0; )
10387 {
10388 if (type == USE_T_DL)
10389 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010390 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 out_str(T_DL); /* delete a line */
10392 }
10393 else /* type == USE_T_CE */
10394 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010395 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 out_str(T_CE); /* erase a line */
10397 }
10398 screen_start(); /* don't know where cursor is now */
10399 }
10400 }
10401
10402 /*
10403 * If the 'db' flag is set, we need to clear the lines that have been
10404 * scrolled up at the bottom of the region.
10405 */
10406 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10407 {
10408 for (i = line_count; i > 0; --i)
10409 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010410 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 out_str(T_CE); /* erase a line */
10412 screen_start(); /* don't know where cursor is now */
10413 }
10414 }
10415
10416#ifdef FEAT_GUI
10417 gui_can_update_cursor();
10418 if (gui.in_use)
10419 out_flush(); /* always flush after a scroll */
10420#endif
10421
10422 return OK;
10423}
10424
10425/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010426 * Return TRUE when postponing displaying the mode message: when not redrawing
10427 * or inside a mapping.
10428 */
10429 int
10430skip_showmode()
10431{
10432 // Call char_avail() only when we are going to show something, because it
10433 // takes a bit of time. redrawing() may also call char_avail_avail().
10434 if (global_busy
10435 || msg_silent != 0
10436 || !redrawing()
10437 || (char_avail() && !KeyTyped))
10438 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010439 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010440 return TRUE;
10441 }
10442 return FALSE;
10443}
10444
10445/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010446 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 *
10448 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10449 * If clear_cmdline is FALSE there may be a message there that needs to be
10450 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010451 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 * Return the length of the message (0 if no message).
10453 */
10454 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010455showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456{
10457 int need_clear;
10458 int length = 0;
10459 int do_mode;
10460 int attr;
10461 int nwr_save;
10462#ifdef FEAT_INS_EXPAND
10463 int sub_attr;
10464#endif
10465
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010466 do_mode = ((p_smd && msg_silent == 0)
10467 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010468 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010469 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010470 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010472 if (skip_showmode())
10473 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474
10475 nwr_save = need_wait_return;
10476
10477 /* wait a bit before overwriting an important message */
10478 check_for_delay(FALSE);
10479
10480 /* if the cmdline is more than one line high, erase top lines */
10481 need_clear = clear_cmdline;
10482 if (clear_cmdline && cmdline_row < Rows - 1)
10483 msg_clr_cmdline(); /* will reset clear_cmdline */
10484
10485 /* Position on the last line in the window, column 0 */
10486 msg_pos_mode();
10487 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010488 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 if (do_mode)
10490 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010491 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010493 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010494# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010495 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010496# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010497 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010498# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010499 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010500# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010501 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010503 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504# endif
10505#endif
10506#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10507 if (gui.in_use)
10508 {
10509 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010510 {
10511 /* HANGUL */
10512 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010513 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010514 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010515 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 }
10518#endif
10519#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010520 /* CTRL-X in Insert mode */
10521 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 {
10523 /* These messages can get long, avoid a wrap in a narrow
10524 * window. Prefer showing edit_submode_extra. */
10525 length = (Rows - msg_row) * Columns - 3;
10526 if (edit_submode_extra != NULL)
10527 length -= vim_strsize(edit_submode_extra);
10528 if (length > 0)
10529 {
10530 if (edit_submode_pre != NULL)
10531 length -= vim_strsize(edit_submode_pre);
10532 if (length - vim_strsize(edit_submode) > 0)
10533 {
10534 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010535 msg_puts_attr((char *)edit_submode_pre, attr);
10536 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538 if (edit_submode_extra != NULL)
10539 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010540 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010542 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 else
10544 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010545 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 }
10547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 }
10549 else
10550#endif
10551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010553 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010554 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010555 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 else if (State & INSERT)
10557 {
10558#ifdef FEAT_RIGHTLEFT
10559 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010560 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010562 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010564 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010565 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010567 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010569 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570#ifdef FEAT_RIGHTLEFT
10571 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010572 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573#endif
10574#ifdef FEAT_KEYMAP
10575 if (State & LANGMAP)
10576 {
10577# ifdef FEAT_ARABIC
10578 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010579 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 else
10581# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010582 if (get_keymap_str(curwin, (char_u *)" (%s)",
10583 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010584 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
10586#endif
10587 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010588 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 if (VIsual_active)
10591 {
10592 char *p;
10593
10594 /* Don't concatenate separate words to avoid translation
10595 * problems. */
10596 switch ((VIsual_select ? 4 : 0)
10597 + (VIsual_mode == Ctrl_V) * 2
10598 + (VIsual_mode == 'V'))
10599 {
10600 case 0: p = N_(" VISUAL"); break;
10601 case 1: p = N_(" VISUAL LINE"); break;
10602 case 2: p = N_(" VISUAL BLOCK"); break;
10603 case 4: p = N_(" SELECT"); break;
10604 case 5: p = N_(" SELECT LINE"); break;
10605 default: p = N_(" SELECT BLOCK"); break;
10606 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010607 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010609 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010611
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 need_clear = TRUE;
10613 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010614 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615#ifdef FEAT_INS_EXPAND
10616 && edit_submode == NULL /* otherwise it gets too long */
10617#endif
10618 )
10619 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010620 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621 need_clear = TRUE;
10622 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010623
10624 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010625 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 msg_clr_eos();
10627 msg_didout = FALSE; /* overwrite this message */
10628 length = msg_col;
10629 msg_col = 0;
10630 need_wait_return = nwr_save; /* never ask for hit-return for this */
10631 }
10632 else if (clear_cmdline && msg_silent == 0)
10633 /* Clear the whole command line. Will reset "clear_cmdline". */
10634 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010635 else if (redraw_mode)
10636 {
10637 msg_pos_mode();
10638 msg_clr_eos();
10639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
10641#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 /* In Visual mode the size of the selected area must be redrawn. */
10643 if (VIsual_active)
10644 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645
10646 /* If the last window has no status line, the ruler is after the mode
10647 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010648 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010649 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650#endif
10651 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010652 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653 clear_cmdline = FALSE;
10654
10655 return length;
10656}
10657
10658/*
10659 * Position for a mode message.
10660 */
10661 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010662msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663{
10664 msg_col = 0;
10665 msg_row = Rows - 1;
10666}
10667
10668/*
10669 * Delete mode message. Used when ESC is typed which is expected to end
10670 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010671 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 */
10673 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010674unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675{
10676 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010677 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 */
10679 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10680 redraw_cmdline = TRUE; /* delete mode later */
10681 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010682 clearmode();
10683}
10684
10685/*
10686 * Clear the mode message.
10687 */
10688 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010689clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010690{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010691 int save_msg_row = msg_row;
10692 int save_msg_col = msg_col;
10693
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010694 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010695 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010696 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010697 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010698
10699 msg_col = save_msg_col;
10700 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701}
10702
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010703 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010704recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010705{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010706 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010707 if (!shortmess(SHM_RECORDING))
10708 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010709 char s[4];
10710
10711 sprintf(s, " @%c", reg_recording);
10712 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010713 }
10714}
10715
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010716/*
10717 * Draw the tab pages line at the top of the Vim window.
10718 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010719 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010720draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010721{
10722 int tabcount = 0;
10723 tabpage_T *tp;
10724 int tabwidth;
10725 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010726 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010727 int attr;
10728 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010729 win_T *cwp;
10730 int wincount;
10731 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010732 int c;
10733 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010734 int attr_sel = HL_ATTR(HLF_TPS);
10735 int attr_nosel = HL_ATTR(HLF_TP);
10736 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010737 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010738 int room;
10739 int use_sep_chars = (t_colors < 8
10740#ifdef FEAT_GUI
10741 && !gui.in_use
10742#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010743#ifdef FEAT_TERMGUICOLORS
10744 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010745#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010746 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010748 if (ScreenLines == NULL)
10749 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010750 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010752#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010753 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010754 if (gui_use_tabline())
10755 {
10756 gui_update_tabline();
10757 return;
10758 }
10759#endif
10760
10761 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010762 return;
10763
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010764#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010765 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010766
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010767 /* Use the 'tabline' option if it's set. */
10768 if (*p_tal != NUL)
10769 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010770 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010771
10772 /* Check for an error. If there is one we would loop in redrawing the
10773 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010774 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010775 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010776 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010777 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010778 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010779 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010780 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010781 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010782#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010783 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010784 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010785 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010786
Bram Moolenaar238a5642006-02-21 22:12:05 +000010787 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10788 if (tabwidth < 6)
10789 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010790
Bram Moolenaar238a5642006-02-21 22:12:05 +000010791 attr = attr_nosel;
10792 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010793 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10794 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010795 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010796 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010797
Bram Moolenaar238a5642006-02-21 22:12:05 +000010798 if (tp->tp_topframe == topframe)
10799 attr = attr_sel;
10800 if (use_sep_chars && col > 0)
10801 screen_putchar('|', 0, col++, attr);
10802
10803 if (tp->tp_topframe != topframe)
10804 attr = attr_nosel;
10805
10806 screen_putchar(' ', 0, col++, attr);
10807
10808 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010809 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010810 cwp = curwin;
10811 wp = firstwin;
10812 }
10813 else
10814 {
10815 cwp = tp->tp_curwin;
10816 wp = tp->tp_firstwin;
10817 }
10818
10819 modified = FALSE;
10820 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10821 if (bufIsChanged(wp->w_buffer))
10822 modified = TRUE;
10823 if (modified || wincount > 1)
10824 {
10825 if (wincount > 1)
10826 {
10827 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010828 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010829 if (col + len >= Columns - 3)
10830 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010831 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010832#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010833 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010834#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010835 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010836#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010837 );
10838 col += len;
10839 }
10840 if (modified)
10841 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10842 screen_putchar(' ', 0, col++, attr);
10843 }
10844
10845 room = scol - col + tabwidth - 1;
10846 if (room > 0)
10847 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010848 /* Get buffer name in NameBuff[] */
10849 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010850 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010851 len = vim_strsize(NameBuff);
10852 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010853 if (has_mbyte)
10854 while (len > room)
10855 {
10856 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010857 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010858 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010859 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010860 {
10861 p += len - room;
10862 len = room;
10863 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010864 if (len > Columns - col - 1)
10865 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010866
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010867 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010868 col += len;
10869 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010870 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010871
10872 /* Store the tab page number in TabPageIdxs[], so that
10873 * jump_to_mouse() knows where each one is. */
10874 ++tabcount;
10875 while (scol < col)
10876 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010877 }
10878
Bram Moolenaar238a5642006-02-21 22:12:05 +000010879 if (use_sep_chars)
10880 c = '_';
10881 else
10882 c = ' ';
10883 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010884
10885 /* Put an "X" for closing the current tab if there are several. */
10886 if (first_tabpage->tp_next != NULL)
10887 {
10888 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10889 TabPageIdxs[Columns - 1] = -999;
10890 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010891 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010892
10893 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10894 * set. */
10895 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010896}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010897
10898/*
10899 * Get buffer name for "buf" into NameBuff[].
10900 * Takes care of special buffer names and translates special characters.
10901 */
10902 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010903get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010904{
10905 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010906 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010907 else
10908 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10909 trans_characters(NameBuff, MAXPATHL);
10910}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010911
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912/*
10913 * Get the character to use in a status line. Get its attributes in "*attr".
10914 */
10915 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010916fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917{
10918 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010919
10920#ifdef FEAT_TERMINAL
10921 if (bt_terminal(wp->w_buffer))
10922 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010923 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010924 {
10925 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010926 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010927 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010928 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010929 {
10930 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010931 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010932 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010933 }
10934 else
10935#endif
10936 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010938 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 fill = fill_stl;
10940 }
10941 else
10942 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010943 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 fill = fill_stlnc;
10945 }
10946 /* Use fill when there is highlighting, and highlighting of current
10947 * window differs, or the fillchars differ, or this is not the
10948 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010949 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010950 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 || (fill_stl != fill_stlnc)))
10952 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010953 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 return '^';
10955 return '=';
10956}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958/*
10959 * Get the character to use in a separator between vertically split windows.
10960 * Get its attributes in "*attr".
10961 */
10962 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010963fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010965 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 if (*attr == 0 && fill_vert == ' ')
10967 return '|';
10968 else
10969 return fill_vert;
10970}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971
10972/*
10973 * Return TRUE if redrawing should currently be done.
10974 */
10975 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010976redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010978#ifdef FEAT_EVAL
10979 if (disable_redraw_for_testing)
10980 return 0;
10981 else
10982#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010983 return ((!RedrawingDisabled
10984#ifdef FEAT_EVAL
10985 || ignore_redraw_flag_for_testing
10986#endif
10987 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
10991 * Return TRUE if printing messages should currently be done.
10992 */
10993 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010994messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995{
10996 return (!(p_lz && char_avail() && !KeyTyped));
10997}
10998
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010999#ifdef FEAT_MENU
11000/*
11001 * Draw the window toolbar.
11002 */
11003 static void
11004redraw_win_toolbar(win_T *wp)
11005{
11006 vimmenu_T *menu;
11007 int item_idx = 0;
11008 int item_count = 0;
11009 int col = 0;
11010 int next_col;
11011 int off = (int)(current_ScreenLine - ScreenLines);
11012 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11013 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11014
11015 vim_free(wp->w_winbar_items);
11016 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11017 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011018 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011019
11020 /* TODO: use fewer spaces if there is not enough room */
11021 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011022 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011023 {
11024 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011025 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011026 break;
11027 if (col > 1)
11028 {
11029 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011030 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011031 break;
11032 }
11033
11034 wp->w_winbar_items[item_idx].wb_startcol = col;
11035 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011036 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011037 break;
11038
11039 next_col = text_to_screenline(wp, menu->name, col);
11040 while (col < next_col)
11041 {
11042 ScreenAttrs[off + col] = button_attr;
11043 ++col;
11044 }
11045 wp->w_winbar_items[item_idx].wb_endcol = col;
11046 wp->w_winbar_items[item_idx].wb_menu = menu;
11047 ++item_idx;
11048
Bram Moolenaar02631462017-09-22 15:20:32 +020011049 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011050 break;
11051 space_to_screenline(off + col, button_attr);
11052 ++col;
11053 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011054 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011055 {
11056 space_to_screenline(off + col, fill_attr);
11057 ++col;
11058 }
11059 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11060
Bram Moolenaar02631462017-09-22 15:20:32 +020011061 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011062 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011063}
11064#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011065
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066/*
11067 * Show current status info in ruler and various other places
11068 * If always is FALSE, only show ruler if position has changed.
11069 */
11070 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011071showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072{
11073 if (!always && !redrawing())
11074 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011075#ifdef FEAT_INS_EXPAND
11076 if (pum_visible())
11077 {
11078 /* Don't redraw right now, do it later. */
11079 curwin->w_redr_status = TRUE;
11080 return;
11081 }
11082#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011083#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011084 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011085 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 else
11087#endif
11088#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011089 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090#endif
11091
11092#ifdef FEAT_TITLE
11093 if (need_maketitle
11094# ifdef FEAT_STL_OPT
11095 || (p_icon && (stl_syntax & STL_IN_ICON))
11096 || (p_title && (stl_syntax & STL_IN_TITLE))
11097# endif
11098 )
11099 maketitle();
11100#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011101 /* Redraw the tab pages line if needed. */
11102 if (redraw_tabline)
11103 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104}
11105
11106#ifdef FEAT_CMDL_INFO
11107 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011108win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011110#define RULER_BUF_LEN 70
11111 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 int row;
11113 int fillchar;
11114 int attr;
11115 int empty_line = FALSE;
11116 colnr_T virtcol;
11117 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011118 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 int this_ru_col;
11121 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011122 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123
11124 /* If 'ruler' off or redrawing disabled, don't do anything */
11125 if (!p_ru)
11126 return;
11127
11128 /*
11129 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11130 * after deleting lines, before cursor.lnum is corrected.
11131 */
11132 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11133 return;
11134
11135#ifdef FEAT_INS_EXPAND
11136 /* Don't draw the ruler while doing insert-completion, it might overwrite
11137 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139 if (edit_submode != NULL)
11140 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011141 // Don't draw the ruler when the popup menu is visible, it may overlap.
11142 // Except when the popup menu will be redrawn anyway.
11143 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011144 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145#endif
11146
11147#ifdef FEAT_STL_OPT
11148 if (*p_ruf)
11149 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011150 int save_called_emsg = called_emsg;
11151
11152 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011154 if (called_emsg)
11155 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011156 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011157 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 return;
11159 }
11160#endif
11161
11162 /*
11163 * Check if not in Insert mode and the line is empty (will show "0-1").
11164 */
11165 if (!(State & INSERT)
11166 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11167 empty_line = TRUE;
11168
11169 /*
11170 * Only draw the ruler when something changed.
11171 */
11172 validate_virtcol_win(wp);
11173 if ( redraw_cmdline
11174 || always
11175 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11176 || wp->w_cursor.col != wp->w_ru_cursor.col
11177 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 || wp->w_topline != wp->w_ru_topline
11180 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11181#ifdef FEAT_DIFF
11182 || wp->w_topfill != wp->w_ru_topfill
11183#endif
11184 || empty_line != wp->w_ru_empty)
11185 {
11186 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 if (wp->w_status_height)
11188 {
11189 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011190 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011191 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011192 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 }
11194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 {
11196 row = Rows - 1;
11197 fillchar = ' ';
11198 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 width = Columns;
11200 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201 }
11202
11203 /* In list mode virtcol needs to be recomputed */
11204 virtcol = wp->w_virtcol;
11205 if (wp->w_p_list && lcs_tab1 == NUL)
11206 {
11207 wp->w_p_list = FALSE;
11208 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11209 wp->w_p_list = TRUE;
11210 }
11211
11212 /*
11213 * Some sprintfs return the length, some return a pointer.
11214 * To avoid portability problems we use strlen() here.
11215 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011216 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11218 ? 0L
11219 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011220 len = STRLEN(buffer);
11221 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11223 (int)virtcol + 1);
11224
11225 /*
11226 * Add a "50%" if there is room for it.
11227 * On the last line, don't print in the last column (scrolls the
11228 * screen up on some terminals).
11229 */
11230 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011231 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 this_ru_col = ru_col - (Columns - width);
11236 if (this_ru_col < 0)
11237 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 /* Never use more than half the window/screen width, leave the other
11239 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011240 if (this_ru_col < (width + 1) / 2)
11241 this_ru_col = (width + 1) / 2;
11242 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011244 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011245 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 if (has_mbyte)
11248 i += (*mb_char2bytes)(fillchar, buffer + i);
11249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 buffer[i++] = fillchar;
11251 ++o;
11252 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011253 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 }
11255 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 if (has_mbyte)
11257 {
11258 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011259 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 {
11261 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011262 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 {
11264 buffer[i] = NUL;
11265 break;
11266 }
11267 }
11268 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011269 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011270 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271
Bram Moolenaar4033c552017-09-16 20:54:51 +020011272 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 i = redraw_cmdline;
11274 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011275 this_ru_col + off + (int)STRLEN(buffer),
11276 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 fillchar, fillchar, attr);
11278 /* don't redraw the cmdline because of showing the ruler */
11279 redraw_cmdline = i;
11280 wp->w_ru_cursor = wp->w_cursor;
11281 wp->w_ru_virtcol = wp->w_virtcol;
11282 wp->w_ru_empty = empty_line;
11283 wp->w_ru_topline = wp->w_topline;
11284 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11285#ifdef FEAT_DIFF
11286 wp->w_ru_topfill = wp->w_topfill;
11287#endif
11288 }
11289}
11290#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011291
11292#if defined(FEAT_LINEBREAK) || defined(PROTO)
11293/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011294 * Return the width of the 'number' and 'relativenumber' column.
11295 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011296 * Otherwise it depends on 'numberwidth' and the line count.
11297 */
11298 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011299number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011300{
11301 int n;
11302 linenr_T lnum;
11303
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011304 if (wp->w_p_rnu && !wp->w_p_nu)
11305 /* cursor line shows "0" */
11306 lnum = wp->w_height;
11307 else
11308 /* cursor line shows absolute line number */
11309 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011310
Bram Moolenaar6b314672015-03-20 15:42:10 +010011311 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011312 return wp->w_nrwidth_width;
11313 wp->w_nrwidth_line_count = lnum;
11314
11315 n = 0;
11316 do
11317 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011318 lnum /= 10;
11319 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011320 } while (lnum > 0);
11321
11322 /* 'numberwidth' gives the minimal width plus one */
11323 if (n < wp->w_p_nuw - 1)
11324 n = wp->w_p_nuw - 1;
11325
11326 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011327 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011328 return n;
11329}
11330#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011331
Bram Moolenaar113e1072019-01-20 15:30:40 +010011332#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011333/*
11334 * Return the current cursor column. This is the actual position on the
11335 * screen. First column is 0.
11336 */
11337 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011338screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011339{
11340 return screen_cur_col;
11341}
11342
11343/*
11344 * Return the current cursor row. This is the actual position on the screen.
11345 * First row is 0.
11346 */
11347 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011348screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011349{
11350 return screen_cur_row;
11351}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011352#endif