blob: 8092fd2448851c750df64edcc8ff148b2b11c483 [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 Moolenaarca2f7032019-06-02 15:56:15 +0200616 {
617 if (type < NOT_VALID)
618 type = NOT_VALID;
619 FOR_ALL_WINDOWS(wp)
620 wp->w_redr_type = NOT_VALID;
621 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
624 updating_screen = TRUE;
625#ifdef FEAT_SYN_HL
626 ++display_tick; /* let syntax code know we're in a next round of
627 * display updating */
628#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200629 if (no_update)
630 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 /*
633 * if the screen was scrolled up when displaying a message, scroll it down
634 */
635 if (msg_scrolled)
636 {
637 clear_cmdline = TRUE;
638 if (msg_scrolled > Rows - 5) /* clearing is faster */
639 type = CLEAR;
640 else if (type != CLEAR)
641 {
642 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200643 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
644 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 type = CLEAR;
646 FOR_ALL_WINDOWS(wp)
647 {
Bram Moolenaar98fb65c2019-06-02 20:33:32 +0200648 if (wp->w_winrow < msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
650 if (W_WINROW(wp) + wp->w_height > msg_scrolled
651 && wp->w_redr_type < REDRAW_TOP
652 && wp->w_lines_valid > 0
653 && wp->w_topline == wp->w_lines[0].wl_lnum)
654 {
655 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
656 wp->w_redr_type = REDRAW_TOP;
657 }
658 else
659 {
660 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200661 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
662 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665 }
666 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200667 if (!no_update)
668 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000669 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
671 msg_scrolled = 0;
672 need_wait_return = FALSE;
673 }
674
675 /* reset cmdline_row now (may have been changed temporarily) */
676 compute_cmdrow();
677
678 /* Check for changed highlighting */
679 if (need_highlight_changed)
680 highlight_changed();
681
682 if (type == CLEAR) /* first clear screen */
683 {
684 screenclear(); /* will reset clear_cmdline */
685 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200686 /* must_redraw may be set indirectly, avoid another redraw later */
687 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689
690 if (clear_cmdline) /* going to clear cmdline (done below) */
691 check_for_delay(FALSE);
692
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000693#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200694 /* Force redraw when width of 'number' or 'relativenumber' column
695 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000696 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200697 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
698 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000699 curwin->w_redr_type = NOT_VALID;
700#endif
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 /*
703 * Only start redrawing if there is really something to do.
704 */
705 if (type == INVERTED)
706 update_curswant();
707 if (curwin->w_redr_type < type
708 && !((type == VALID
709 && curwin->w_lines[0].wl_valid
710#ifdef FEAT_DIFF
711 && curwin->w_topfill == curwin->w_old_topfill
712 && curwin->w_botfill == curwin->w_old_botfill
713#endif
714 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000716 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
718 && curwin->w_old_visual_mode == VIsual_mode
719 && (curwin->w_valid & VALID_VIRTCOL)
720 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 ))
722 curwin->w_redr_type = type;
723
Bram Moolenaar5a305422006-04-28 22:38:25 +0000724 /* Redraw the tab pages line if needed. */
725 if (redraw_tabline || type >= NOT_VALID)
726 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728#ifdef FEAT_SYN_HL
729 /*
730 * Correct stored syntax highlighting info for changes in each displayed
731 * buffer. Each buffer must only be done once.
732 */
733 FOR_ALL_WINDOWS(wp)
734 {
735 if (wp->w_buffer->b_mod_set)
736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 win_T *wwp;
738
739 /* Check if we already did this buffer. */
740 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
741 if (wwp->w_buffer == wp->w_buffer)
742 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200743 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 syn_stack_apply_changes(wp->w_buffer);
745 }
746 }
747#endif
748
749 /*
750 * Go from top to bottom through the windows, redrawing the ones that need
751 * it.
752 */
753#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
754 did_one = FALSE;
755#endif
756#ifdef FEAT_SEARCH_EXTRA
757 search_hl.rm.regprog = NULL;
758#endif
759 FOR_ALL_WINDOWS(wp)
760 {
761 if (wp->w_redr_type != 0)
762 {
763 cursor_off();
764#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
765 if (!did_one)
766 {
767 did_one = TRUE;
768# ifdef FEAT_SEARCH_EXTRA
769 start_search_hl();
770# endif
771# ifdef FEAT_CLIPBOARD
772 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200773 if (clip_star.available && clip_isautosel_star())
774 clip_update_selection(&clip_star);
775 if (clip_plus.available && clip_isautosel_plus())
776 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777# endif
778#ifdef FEAT_GUI
779 /* Remove the cursor before starting to do anything, because
780 * scrolling may make it difficult to redraw the text under
781 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200782 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200783 {
784 gui_cursor_col = gui.cursor_col;
785 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200787 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#endif
790 }
791#endif
792 win_update(wp);
793 }
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /* redraw status line after the window to minimize cursor movement */
796 if (wp->w_redr_status)
797 {
798 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200799 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
802#if defined(FEAT_SEARCH_EXTRA)
803 end_search_hl();
804#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100805#ifdef FEAT_INS_EXPAND
806 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200807 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 /* Reset b_mod_set flags. Going through all windows is probably faster
811 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200812 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200815 after_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /* Clear or redraw the command line. Done last, because scrolling may
818 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200819 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 showmode();
821
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200822 if (no_update)
823 --no_win_do_lines_ins;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200826 if (!did_intro)
827 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 did_intro = TRUE;
829
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200830#ifdef FEAT_TEXT_PROP
Bram Moolenaar988c4332019-06-02 14:12:11 +0200831 // Display popup windows on top of the windows.
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200832 update_popups();
833#endif
834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#ifdef FEAT_GUI
836 /* Redraw the cursor and update the scrollbars when all screen updating is
837 * done. */
838 if (gui.in_use)
839 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200840 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200841 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100842 mch_disable_flush();
843 out_flush(); /* required before updating the cursor */
844 mch_enable_flush();
845
Bram Moolenaar144445d2016-07-08 21:41:54 +0200846 /* Put the GUI position where the cursor was, gui_update_cursor()
847 * uses that. */
848 gui.col = gui_cursor_col;
849 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200850 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100852 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200853 screen_cur_col = gui.col;
854 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200855 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100856 else
857 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 gui_update_scrollbars(FALSE);
859 }
860#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862}
863
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100864#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100865/*
866 * Prepare for updating one or more windows.
867 * Caller must check for "updating_screen" already set to avoid recursiveness.
868 */
869 static void
870update_prepare(void)
871{
872 cursor_off();
873 updating_screen = TRUE;
874#ifdef FEAT_GUI
875 /* Remove the cursor before starting to do anything, because scrolling may
876 * make it difficult to redraw the text under it. */
877 if (gui.in_use)
878 gui_undraw_cursor();
879#endif
880#ifdef FEAT_SEARCH_EXTRA
881 start_search_hl();
882#endif
883}
884
885/*
886 * Finish updating one or more windows.
887 */
888 static void
889update_finish(void)
890{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200891 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892 showmode();
893
894# ifdef FEAT_SEARCH_EXTRA
895 end_search_hl();
896# endif
897
Bram Moolenaar68a4b042019-05-29 22:28:29 +0200898 after_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100899
900# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100901 /* Redraw the cursor and update the scrollbars when all screen updating is
902 * done. */
903 if (gui.in_use)
904 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100905 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100906 gui_update_scrollbars(FALSE);
907 }
908# endif
909}
910#endif
911
Bram Moolenaar860cae12010-06-05 23:22:07 +0200912#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200913/*
914 * Return TRUE if the cursor line in window "wp" may be concealed, according
915 * to the 'concealcursor' option.
916 */
917 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100918conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200919{
920 int c;
921
922 if (*wp->w_p_cocu == NUL)
923 return FALSE;
924 if (get_real_state() & VISUAL)
925 c = 'v';
926 else if (State & INSERT)
927 c = 'i';
928 else if (State & NORMAL)
929 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200930 else if (State & CMDLINE)
931 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200932 else
933 return FALSE;
934 return vim_strchr(wp->w_p_cocu, c) != NULL;
935}
936
937/*
938 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
939 */
940 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200941conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200942{
943 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
944 {
945 need_cursor_line_redraw = TRUE;
946 /* Need to recompute cursor column, e.g., when starting Visual mode
947 * without concealing. */
948 curs_columns(TRUE);
949 }
950}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951#endif
952
Bram Moolenaar113e1072019-01-20 15:30:40 +0100953#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100955update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956{
957 win_T *wp;
958 int doit = FALSE;
959
960# ifdef FEAT_FOLDING
961 win_foldinfo.fi_level = 0;
962# endif
963
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100964 // update/delete a specific sign
965 redraw_buf_line_later(buf, lnum);
966
967 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (wp->w_redr_type != 0)
970 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100972 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200973 * happening (recursive call), messages on the screen or still starting up.
974 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100975 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200976 || State == ASKMORE || State == HITRETURN
977 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100978#ifdef FEAT_GUI
979 || gui.starting
980#endif
981 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 return;
983
984 /* update all windows that need updating */
985 update_prepare();
986
Bram Moolenaar29323592016-07-24 22:04:11 +0200987 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
989 if (wp->w_redr_type != 0)
990 win_update(wp);
991 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200992 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Bram Moolenaar988c4332019-06-02 14:12:11 +0200995#ifdef FEAT_TEXT_PROP
996 // Display popup windows on top of the others.
997 update_popups();
998#endif
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 update_finish();
1001}
1002#endif
1003
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001004/*
1005 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1006 * window then get the "Pmenu" highlight attribute.
1007 */
1008 static int
1009get_wcr_attr(win_T *wp)
1010{
1011 int wcr_attr = 0;
1012
1013 if (*wp->w_p_wcr != NUL)
1014 wcr_attr = syn_name2attr(wp->w_p_wcr);
1015#ifdef FEAT_TEXT_PROP
1016 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1017 wcr_attr = HL_ATTR(HLF_PNI);
1018#endif
1019 return wcr_attr;
1020}
1021
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001022#ifdef FEAT_TEXT_PROP
1023/*
1024 * Return a string of "len" spaces in IObuff.
1025 */
1026 static char_u *
1027get_spaces(int len)
1028{
1029 vim_memset(IObuff, ' ', (size_t)len);
1030 IObuff[len] = NUL;
1031 return IObuff;
1032}
1033
1034 static void
1035update_popups(void)
1036{
1037 win_T *wp;
1038 int top_off;
1039 int left_off;
1040 int total_width;
1041 int total_height;
1042 int popup_attr;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001043 int border_attr[4];
Bram Moolenaar02e15072019-06-03 22:53:30 +02001044 int border_char[8];
Bram Moolenaar790498b2019-06-01 22:15:29 +02001045 char_u buf[MB_MAXBYTES];
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001046 int row;
Bram Moolenaar790498b2019-06-01 22:15:29 +02001047 int i;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001048
1049 // Find the window with the lowest zindex that hasn't been updated yet,
1050 // so that the window with a higher zindex is drawn later, thus goes on
1051 // top.
1052 // TODO: don't redraw every popup every time.
Bram Moolenaar3397f742019-06-02 18:40:06 +02001053 popup_visible = FALSE;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001054 popup_reset_handled();
1055 while ((wp = find_next_popup(TRUE)) != NULL)
1056 {
1057 // Recompute the position if the text changed.
1058 if (wp->w_popup_last_changedtick != CHANGEDTICK(wp->w_buffer))
1059 popup_adjust_position(wp);
1060
1061 // adjust w_winrow and w_wincol for border and padding, since
1062 // win_update() doesn't handle them.
1063 top_off = wp->w_popup_padding[0] + wp->w_popup_border[0];
1064 left_off = wp->w_popup_padding[3] + wp->w_popup_border[3];
1065 wp->w_winrow += top_off;
1066 wp->w_wincol += left_off;
1067
1068 // Draw the popup text.
1069 win_update(wp);
Bram Moolenaar3397f742019-06-02 18:40:06 +02001070 popup_visible = TRUE;
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001071
1072 wp->w_winrow -= top_off;
1073 wp->w_wincol -= left_off;
1074
1075 total_width = wp->w_popup_border[3] + wp->w_popup_padding[3]
1076 + wp->w_width + wp->w_popup_padding[1] + wp->w_popup_border[1];
1077 total_height = wp->w_popup_border[0] + wp->w_popup_padding[0]
1078 + wp->w_height + wp->w_popup_padding[2] + wp->w_popup_border[2];
1079 popup_attr = get_wcr_attr(wp);
1080
Bram Moolenaar3f6aeba2019-06-03 22:21:27 +02001081 // We can only use these line drawing characters when 'encoding' is
1082 // "utf-8" and 'ambiwidth' is "single".
Bram Moolenaar02e15072019-06-03 22:53:30 +02001083 if (enc_utf8 && *p_ambw == 's')
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001084 {
Bram Moolenaar790498b2019-06-01 22:15:29 +02001085 border_char[0] = border_char[2] = 0x2550;
1086 border_char[1] = border_char[3] = 0x2551;
1087 border_char[4] = 0x2554;
1088 border_char[5] = 0x2557;
1089 border_char[6] = 0x255d;
1090 border_char[7] = 0x255a;
1091 }
Bram Moolenaar02e15072019-06-03 22:53:30 +02001092 else
1093 {
1094 border_char[0] = border_char[2] = '-';
1095 border_char[1] = border_char[3] = '|';
1096 for (i = 4; i < 8; ++i)
1097 border_char[i] = '+';
1098 }
Bram Moolenaar790498b2019-06-01 22:15:29 +02001099 for (i = 0; i < 8; ++i)
1100 if (wp->w_border_char[i] != 0)
1101 border_char[i] = wp->w_border_char[i];
1102
1103 for (i = 0; i < 4; ++i)
1104 {
1105 border_attr[i] = popup_attr;
1106 if (wp->w_border_highlight[i] != NULL)
1107 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001108 }
1109
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001110 if (wp->w_popup_border[0] > 0)
1111 {
1112 // top border
1113 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1114 wp->w_wincol,
1115 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001116 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001117 ? border_char[4] : border_char[0],
1118 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001119 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001120 {
1121 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1122 screen_puts(buf, wp->w_winrow,
1123 wp->w_wincol + total_width - 1, border_attr[1]);
1124 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001125 }
1126
1127 if (wp->w_popup_padding[0] > 0)
1128 {
1129 // top padding
1130 row = wp->w_winrow + wp->w_popup_border[0];
1131 screen_fill(row, row + wp->w_popup_padding[0],
1132 wp->w_wincol + wp->w_popup_border[3],
1133 wp->w_wincol + total_width - wp->w_popup_border[1],
1134 ' ', ' ', popup_attr);
1135 }
1136
1137 for (row = wp->w_winrow + wp->w_popup_border[0];
1138 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1139 ++row)
1140 {
1141 // left border
1142 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001143 {
1144 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1145 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1146 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001147 // left padding
1148 if (wp->w_popup_padding[3] > 0)
1149 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1150 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1151 // right border
1152 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001153 {
1154 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1155 screen_puts(buf, row,
1156 wp->w_wincol + total_width - 1, border_attr[1]);
1157 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001158 // right padding
1159 if (wp->w_popup_padding[1] > 0)
1160 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1161 wp->w_wincol + wp->w_popup_border[3]
1162 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1163 }
1164
1165 if (wp->w_popup_padding[2] > 0)
1166 {
1167 // bottom padding
1168 row = wp->w_winrow + wp->w_popup_border[0]
1169 + wp->w_popup_padding[0] + wp->w_height;
1170 screen_fill(row, row + wp->w_popup_padding[2],
1171 wp->w_wincol + wp->w_popup_border[3],
1172 wp->w_wincol + total_width - wp->w_popup_border[1],
1173 ' ', ' ', popup_attr);
1174 }
1175
1176 if (wp->w_popup_border[2] > 0)
1177 {
1178 // bottom border
1179 row = wp->w_winrow + total_height - 1;
1180 screen_fill(row , row + 1,
1181 wp->w_wincol,
1182 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001183 wp->w_popup_border[3] != 0
1184 ? border_char[7] : border_char[2],
1185 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001186 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001187 {
1188 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1189 screen_puts(buf, row,
1190 wp->w_wincol + total_width - 1, border_attr[2]);
1191 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001192 }
1193 }
1194}
1195#endif
1196
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#if defined(FEAT_GUI) || defined(PROTO)
1198/*
1199 * Update a single window, its status line and maybe the command line msg.
1200 * Used for the GUI scrollbar.
1201 */
1202 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001203updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001205 /* return if already busy updating */
1206 if (updating_screen)
1207 return;
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 update_prepare();
1210
1211#ifdef FEAT_CLIPBOARD
1212 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001213 if (clip_star.available && clip_isautosel_star())
1214 clip_update_selection(&clip_star);
1215 if (clip_plus.available && clip_isautosel_plus())
1216 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001218
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001220
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001221 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001222 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001223 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (wp->w_redr_status
1226# ifdef FEAT_CMDL_INFO
1227 || p_ru
1228# endif
1229# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001230 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231# endif
1232 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001233 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
Bram Moolenaar988c4332019-06-02 14:12:11 +02001235#ifdef FEAT_TEXT_PROP
1236 // Display popup windows on top of everything.
1237 update_popups();
1238#endif
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 update_finish();
1241}
1242#endif
1243
1244/*
1245 * Update a single window.
1246 *
1247 * This may cause the windows below it also to be redrawn (when clearing the
1248 * screen or scrolling lines).
1249 *
1250 * How the window is redrawn depends on wp->w_redr_type. Each type also
1251 * implies the one below it.
1252 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001253 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1255 * INVERTED redraw the changed part of the Visual area
1256 * INVERTED_ALL redraw the whole Visual area
1257 * VALID 1. scroll up/down to adjust for a changed w_topline
1258 * 2. update lines at the top when scrolled down
1259 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001260 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 * b_mod_top and b_mod_bot.
1262 * - if wp->w_redraw_top non-zero, redraw lines between
1263 * wp->w_redraw_top and wp->w_redr_bot.
1264 * - continue redrawing when syntax status is invalid.
1265 * 4. if scrolled up, update lines at the bottom.
1266 * This results in three areas that may need updating:
1267 * top: from first row to top_end (when scrolled down)
1268 * mid: from mid_start to mid_end (update inversion or changed text)
1269 * bot: from bot_start to last row (when scrolled up)
1270 */
1271 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001272win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
1274 buf_T *buf = wp->w_buffer;
1275 int type;
1276 int top_end = 0; /* Below last row of the top area that needs
1277 updating. 0 when no top area updating. */
1278 int mid_start = 999;/* first row of the mid area that needs
1279 updating. 999 when no mid area updating. */
1280 int mid_end = 0; /* Below last row of the mid area that needs
1281 updating. 0 when no mid area updating. */
1282 int bot_start = 999;/* first row of the bot area that needs
1283 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 int scrolled_down = FALSE; /* TRUE when scrolled down when
1285 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001287 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 int top_to_mod = FALSE; /* redraw above mod_top */
1289#endif
1290
1291 int row; /* current window row to display */
1292 linenr_T lnum; /* current buffer lnum to display */
1293 int idx; /* current index in w_lines[] */
1294 int srow; /* starting row of the current line */
1295
1296 int eof = FALSE; /* if TRUE, we hit the end of the file */
1297 int didline = FALSE; /* if TRUE, we finished the last line */
1298 int i;
1299 long j;
1300 static int recursive = FALSE; /* being called recursively */
1301 int old_botline = wp->w_botline;
1302#ifdef FEAT_FOLDING
1303 long fold_count;
1304#endif
1305#ifdef FEAT_SYN_HL
1306 /* remember what happened to the previous line, to know if
1307 * check_visual_highlight() can be used */
1308#define DID_NONE 1 /* didn't update a line */
1309#define DID_LINE 2 /* updated a normal line */
1310#define DID_FOLD 3 /* updated a folded line */
1311 int did_update = DID_NONE;
1312 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1313#endif
1314 linenr_T mod_top = 0;
1315 linenr_T mod_bot = 0;
1316#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1317 int save_got_int;
1318#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001319#ifdef SYN_TIME_LIMIT
1320 proftime_T syntax_tm;
1321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323 type = wp->w_redr_type;
1324
1325 if (type == NOT_VALID)
1326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 wp->w_lines_valid = 0;
1329 }
1330
1331 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001332 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 wp->w_redr_type = 0;
1335 return;
1336 }
1337
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 /* Window is zero-width: Only need to draw the separator. */
1339 if (wp->w_width == 0)
1340 {
1341 /* draw the vertical separator right of this window */
1342 draw_vsep_win(wp, 0);
1343 wp->w_redr_type = 0;
1344 return;
1345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001347#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001348 // If this window contains a terminal, redraw works completely differently.
1349 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001350 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001351 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001352# ifdef FEAT_MENU
1353 /* Draw the window toolbar, if there is one. */
1354 if (winbar_height(wp) > 0)
1355 redraw_win_toolbar(wp);
1356# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001357 wp->w_redr_type = 0;
1358 return;
1359 }
1360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001363 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#endif
1365
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001366#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001367 /* Force redraw when width of 'number' or 'relativenumber' column
1368 * changes. */
1369 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001370 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001371 {
1372 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001373 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001374 }
1375 else
1376#endif
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1379 {
1380 /*
1381 * When there are both inserted/deleted lines and specific lines to be
1382 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1383 * everything (only happens when redrawing is off for while).
1384 */
1385 type = NOT_VALID;
1386 }
1387 else
1388 {
1389 /*
1390 * Set mod_top to the first line that needs displaying because of
1391 * changes. Set mod_bot to the first line after the changes.
1392 */
1393 mod_top = wp->w_redraw_top;
1394 if (wp->w_redraw_bot != 0)
1395 mod_bot = wp->w_redraw_bot + 1;
1396 else
1397 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (buf->b_mod_set)
1399 {
1400 if (mod_top == 0 || mod_top > buf->b_mod_top)
1401 {
1402 mod_top = buf->b_mod_top;
1403#ifdef FEAT_SYN_HL
1404 /* Need to redraw lines above the change that may be included
1405 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001406 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001408 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (mod_top < 1)
1410 mod_top = 1;
1411 }
1412#endif
1413 }
1414 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1415 mod_bot = buf->b_mod_bot;
1416
1417#ifdef FEAT_SEARCH_EXTRA
1418 /* When 'hlsearch' is on and using a multi-line search pattern, a
1419 * change in one line may make the Search highlighting in a
1420 * previous line invalid. Simple solution: redraw all visible
1421 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001422 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001424 if (search_hl.rm.regprog != NULL
1425 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001427 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001428 {
1429 cur = wp->w_match_head;
1430 while (cur != NULL)
1431 {
1432 if (cur->match.regprog != NULL
1433 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001434 {
1435 top_to_mod = TRUE;
1436 break;
1437 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001438 cur = cur->next;
1439 }
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#endif
1442 }
1443#ifdef FEAT_FOLDING
1444 if (mod_top != 0 && hasAnyFolding(wp))
1445 {
1446 linenr_T lnumt, lnumb;
1447
1448 /*
1449 * A change in a line can cause lines above it to become folded or
1450 * unfolded. Find the top most buffer line that may be affected.
1451 * If the line was previously folded and displayed, get the first
1452 * line of that fold. If the line is folded now, get the first
1453 * folded line. Use the minimum of these two.
1454 */
1455
1456 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1457 * the line below it. If there is no valid entry, use w_topline.
1458 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1459 * to this line. If there is no valid entry, use MAXLNUM. */
1460 lnumt = wp->w_topline;
1461 lnumb = MAXLNUM;
1462 for (i = 0; i < wp->w_lines_valid; ++i)
1463 if (wp->w_lines[i].wl_valid)
1464 {
1465 if (wp->w_lines[i].wl_lastlnum < mod_top)
1466 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1467 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1468 {
1469 lnumb = wp->w_lines[i].wl_lnum;
1470 /* When there is a fold column it might need updating
1471 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001472 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 ++lnumb;
1474 }
1475 }
1476
1477 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1478 if (mod_top > lnumt)
1479 mod_top = lnumt;
1480
1481 /* Now do the same for the bottom line (one above mod_bot). */
1482 --mod_bot;
1483 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1484 ++mod_bot;
1485 if (mod_bot < lnumb)
1486 mod_bot = lnumb;
1487 }
1488#endif
1489
1490 /* When a change starts above w_topline and the end is below
1491 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001492 * If the end of the change is above w_topline: do like no change was
1493 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (mod_top != 0 && mod_top < wp->w_topline)
1495 {
1496 if (mod_bot > wp->w_topline)
1497 mod_top = wp->w_topline;
1498#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001499 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 top_end = 1;
1501#endif
1502 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001503
1504 /* When line numbers are displayed need to redraw all lines below
1505 * inserted/deleted lines. */
1506 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1507 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001509 wp->w_redraw_top = 0; // reset for next time
1510 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
1512 /*
1513 * When only displaying the lines at the top, set top_end. Used when
1514 * window has scrolled down for msg_scrolled.
1515 */
1516 if (type == REDRAW_TOP)
1517 {
1518 j = 0;
1519 for (i = 0; i < wp->w_lines_valid; ++i)
1520 {
1521 j += wp->w_lines[i].wl_size;
1522 if (j >= wp->w_upd_rows)
1523 {
1524 top_end = j;
1525 break;
1526 }
1527 }
1528 if (top_end == 0)
1529 /* not found (cannot happen?): redraw everything */
1530 type = NOT_VALID;
1531 else
1532 /* top area defined, the rest is VALID */
1533 type = VALID;
1534 }
1535
Bram Moolenaar367329b2007-08-30 11:53:22 +00001536 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001537 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1538 * non-zero and thus not FALSE) will indicate that screenclear() was not
1539 * called. */
1540 if (screen_cleared)
1541 screen_cleared = MAYBE;
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /*
1544 * If there are no changes on the screen that require a complete redraw,
1545 * handle three cases:
1546 * 1: we are off the top of the screen by a few lines: scroll down
1547 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1548 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1549 * w_lines[] that needs updating.
1550 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001551 if ((type == VALID || type == SOME_VALID
1552 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#ifdef FEAT_DIFF
1554 && !wp->w_botfill && !wp->w_old_botfill
1555#endif
1556 )
1557 {
1558 if (mod_top != 0 && wp->w_topline == mod_top)
1559 {
1560 /*
1561 * w_topline is the first changed line, the scrolling will be done
1562 * further down.
1563 */
1564 }
1565 else if (wp->w_lines[0].wl_valid
1566 && (wp->w_topline < wp->w_lines[0].wl_lnum
1567#ifdef FEAT_DIFF
1568 || (wp->w_topline == wp->w_lines[0].wl_lnum
1569 && wp->w_topfill > wp->w_old_topfill)
1570#endif
1571 ))
1572 {
1573 /*
1574 * New topline is above old topline: May scroll down.
1575 */
1576#ifdef FEAT_FOLDING
1577 if (hasAnyFolding(wp))
1578 {
1579 linenr_T ln;
1580
1581 /* count the number of lines we are off, counting a sequence
1582 * of folded lines as one */
1583 j = 0;
1584 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1585 {
1586 ++j;
1587 if (j >= wp->w_height - 2)
1588 break;
1589 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1590 }
1591 }
1592 else
1593#endif
1594 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1595 if (j < wp->w_height - 2) /* not too far off */
1596 {
1597 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1598#ifdef FEAT_DIFF
1599 /* insert extra lines for previously invisible filler lines */
1600 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1601 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1602 - wp->w_old_topfill;
1603#endif
1604 if (i < wp->w_height - 2) /* less than a screen off */
1605 {
1606 /*
1607 * Try to insert the correct number of lines.
1608 * If not the last window, delete the lines at the bottom.
1609 * win_ins_lines may fail when the terminal can't do it.
1610 */
1611 if (i > 0)
1612 check_for_delay(FALSE);
1613 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1614 {
1615 if (wp->w_lines_valid != 0)
1616 {
1617 /* Need to update rows that are new, stop at the
1618 * first one that scrolled down. */
1619 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622 /* Move the entries that were scrolled, disable
1623 * the entries for the lines to be redrawn. */
1624 if ((wp->w_lines_valid += j) > wp->w_height)
1625 wp->w_lines_valid = wp->w_height;
1626 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1627 wp->w_lines[idx] = wp->w_lines[idx - j];
1628 while (idx >= 0)
1629 wp->w_lines[idx--].wl_valid = FALSE;
1630 }
1631 }
1632 else
1633 mid_start = 0; /* redraw all lines */
1634 }
1635 else
1636 mid_start = 0; /* redraw all lines */
1637 }
1638 else
1639 mid_start = 0; /* redraw all lines */
1640 }
1641 else
1642 {
1643 /*
1644 * New topline is at or below old topline: May scroll up.
1645 * When topline didn't change, find first entry in w_lines[] that
1646 * needs updating.
1647 */
1648
1649 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1650 j = -1;
1651 row = 0;
1652 for (i = 0; i < wp->w_lines_valid; i++)
1653 {
1654 if (wp->w_lines[i].wl_valid
1655 && wp->w_lines[i].wl_lnum == wp->w_topline)
1656 {
1657 j = i;
1658 break;
1659 }
1660 row += wp->w_lines[i].wl_size;
1661 }
1662 if (j == -1)
1663 {
1664 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1665 * lines */
1666 mid_start = 0;
1667 }
1668 else
1669 {
1670 /*
1671 * Try to delete the correct number of lines.
1672 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1673 */
1674#ifdef FEAT_DIFF
1675 /* If the topline didn't change, delete old filler lines,
1676 * otherwise delete filler lines of the new topline... */
1677 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1678 row += wp->w_old_topfill;
1679 else
1680 row += diff_check_fill(wp, wp->w_topline);
1681 /* ... but don't delete new filler lines. */
1682 row -= wp->w_topfill;
1683#endif
1684 if (row > 0)
1685 {
1686 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001687 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1688 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 bot_start = wp->w_height - row;
1690 else
1691 mid_start = 0; /* redraw all lines */
1692 }
1693 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1694 {
1695 /*
1696 * Skip the lines (below the deleted lines) that are still
1697 * valid and don't need redrawing. Copy their info
1698 * upwards, to compensate for the deleted lines. Set
1699 * bot_start to the first row that needs redrawing.
1700 */
1701 bot_start = 0;
1702 idx = 0;
1703 for (;;)
1704 {
1705 wp->w_lines[idx] = wp->w_lines[j];
1706 /* stop at line that didn't fit, unless it is still
1707 * valid (no lines deleted) */
1708 if (row > 0 && bot_start + row
1709 + (int)wp->w_lines[j].wl_size > wp->w_height)
1710 {
1711 wp->w_lines_valid = idx + 1;
1712 break;
1713 }
1714 bot_start += wp->w_lines[idx++].wl_size;
1715
1716 /* stop at the last valid entry in w_lines[].wl_size */
1717 if (++j >= wp->w_lines_valid)
1718 {
1719 wp->w_lines_valid = idx;
1720 break;
1721 }
1722 }
1723#ifdef FEAT_DIFF
1724 /* Correct the first entry for filler lines at the top
1725 * when it won't get updated below. */
1726 if (wp->w_p_diff && bot_start > 0)
1727 wp->w_lines[0].wl_size =
1728 plines_win_nofill(wp, wp->w_topline, TRUE)
1729 + wp->w_topfill;
1730#endif
1731 }
1732 }
1733 }
1734
1735 /* When starting redraw in the first line, redraw all lines. When
1736 * there is only one window it's probably faster to clear the screen
1737 * first. */
1738 if (mid_start == 0)
1739 {
1740 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001741 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001742 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001743 /* Clear the screen when it was not done by win_del_lines() or
1744 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1745 * then. */
1746 if (screen_cleared != TRUE)
1747 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001748 /* The screen was cleared, redraw the tab pages line. */
1749 if (redraw_tabline)
1750 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001753
1754 /* When win_del_lines() or win_ins_lines() caused the screen to be
1755 * cleared (only happens for the first window) or when screenclear()
1756 * was called directly above, "must_redraw" will have been set to
1757 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1758 if (screen_cleared == TRUE)
1759 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 else
1762 {
1763 /* Not VALID or INVERTED: redraw all lines. */
1764 mid_start = 0;
1765 mid_end = wp->w_height;
1766 }
1767
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001768 if (type == SOME_VALID)
1769 {
1770 /* SOME_VALID: redraw all lines. */
1771 mid_start = 0;
1772 mid_end = wp->w_height;
1773 type = NOT_VALID;
1774 }
1775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 /* check if we are updating or removing the inverted part */
1777 if ((VIsual_active && buf == curwin->w_buffer)
1778 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1779 {
1780 linenr_T from, to;
1781
1782 if (VIsual_active)
1783 {
1784 if (VIsual_active
1785 && (VIsual_mode != wp->w_old_visual_mode
1786 || type == INVERTED_ALL))
1787 {
1788 /*
1789 * If the type of Visual selection changed, redraw the whole
1790 * selection. Also when the ownership of the X selection is
1791 * gained or lost.
1792 */
1793 if (curwin->w_cursor.lnum < VIsual.lnum)
1794 {
1795 from = curwin->w_cursor.lnum;
1796 to = VIsual.lnum;
1797 }
1798 else
1799 {
1800 from = VIsual.lnum;
1801 to = curwin->w_cursor.lnum;
1802 }
1803 /* redraw more when the cursor moved as well */
1804 if (wp->w_old_cursor_lnum < from)
1805 from = wp->w_old_cursor_lnum;
1806 if (wp->w_old_cursor_lnum > to)
1807 to = wp->w_old_cursor_lnum;
1808 if (wp->w_old_visual_lnum < from)
1809 from = wp->w_old_visual_lnum;
1810 if (wp->w_old_visual_lnum > to)
1811 to = wp->w_old_visual_lnum;
1812 }
1813 else
1814 {
1815 /*
1816 * Find the line numbers that need to be updated: The lines
1817 * between the old cursor position and the current cursor
1818 * position. Also check if the Visual position changed.
1819 */
1820 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1821 {
1822 from = curwin->w_cursor.lnum;
1823 to = wp->w_old_cursor_lnum;
1824 }
1825 else
1826 {
1827 from = wp->w_old_cursor_lnum;
1828 to = curwin->w_cursor.lnum;
1829 if (from == 0) /* Visual mode just started */
1830 from = to;
1831 }
1832
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001833 if (VIsual.lnum != wp->w_old_visual_lnum
1834 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 if (wp->w_old_visual_lnum < from
1837 && wp->w_old_visual_lnum != 0)
1838 from = wp->w_old_visual_lnum;
1839 if (wp->w_old_visual_lnum > to)
1840 to = wp->w_old_visual_lnum;
1841 if (VIsual.lnum < from)
1842 from = VIsual.lnum;
1843 if (VIsual.lnum > to)
1844 to = VIsual.lnum;
1845 }
1846 }
1847
1848 /*
1849 * If in block mode and changed column or curwin->w_curswant:
1850 * update all lines.
1851 * First compute the actual start and end column.
1852 */
1853 if (VIsual_mode == Ctrl_V)
1854 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001855 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001856#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001857 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858
Bram Moolenaar404406a2014-10-09 13:24:43 +02001859 if (curwin->w_p_lbr)
1860 ve_flags = VE_ALL;
1861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001863#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001864 ve_flags = save_ve_flags;
1865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 ++toc;
1867 if (curwin->w_curswant == MAXCOL)
1868 toc = MAXCOL;
1869
1870 if (fromc != wp->w_old_cursor_fcol
1871 || toc != wp->w_old_cursor_lcol)
1872 {
1873 if (from > VIsual.lnum)
1874 from = VIsual.lnum;
1875 if (to < VIsual.lnum)
1876 to = VIsual.lnum;
1877 }
1878 wp->w_old_cursor_fcol = fromc;
1879 wp->w_old_cursor_lcol = toc;
1880 }
1881 }
1882 else
1883 {
1884 /* Use the line numbers of the old Visual area. */
1885 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1886 {
1887 from = wp->w_old_cursor_lnum;
1888 to = wp->w_old_visual_lnum;
1889 }
1890 else
1891 {
1892 from = wp->w_old_visual_lnum;
1893 to = wp->w_old_cursor_lnum;
1894 }
1895 }
1896
1897 /*
1898 * There is no need to update lines above the top of the window.
1899 */
1900 if (from < wp->w_topline)
1901 from = wp->w_topline;
1902
1903 /*
1904 * If we know the value of w_botline, use it to restrict the update to
1905 * the lines that are visible in the window.
1906 */
1907 if (wp->w_valid & VALID_BOTLINE)
1908 {
1909 if (from >= wp->w_botline)
1910 from = wp->w_botline - 1;
1911 if (to >= wp->w_botline)
1912 to = wp->w_botline - 1;
1913 }
1914
1915 /*
1916 * Find the minimal part to be updated.
1917 * Watch out for scrolling that made entries in w_lines[] invalid.
1918 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1919 * top_end; need to redraw from top_end to the "to" line.
1920 * A middle mouse click with a Visual selection may change the text
1921 * above the Visual area and reset wl_valid, do count these for
1922 * mid_end (in srow).
1923 */
1924 if (mid_start > 0)
1925 {
1926 lnum = wp->w_topline;
1927 idx = 0;
1928 srow = 0;
1929 if (scrolled_down)
1930 mid_start = top_end;
1931 else
1932 mid_start = 0;
1933 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1934 {
1935 if (wp->w_lines[idx].wl_valid)
1936 mid_start += wp->w_lines[idx].wl_size;
1937 else if (!scrolled_down)
1938 srow += wp->w_lines[idx].wl_size;
1939 ++idx;
1940# ifdef FEAT_FOLDING
1941 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1942 lnum = wp->w_lines[idx].wl_lnum;
1943 else
1944# endif
1945 ++lnum;
1946 }
1947 srow += mid_start;
1948 mid_end = wp->w_height;
1949 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1950 {
1951 if (wp->w_lines[idx].wl_valid
1952 && wp->w_lines[idx].wl_lnum >= to + 1)
1953 {
1954 /* Only update until first row of this line */
1955 mid_end = srow;
1956 break;
1957 }
1958 srow += wp->w_lines[idx].wl_size;
1959 }
1960 }
1961 }
1962
1963 if (VIsual_active && buf == curwin->w_buffer)
1964 {
1965 wp->w_old_visual_mode = VIsual_mode;
1966 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1967 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001968 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 wp->w_old_curswant = curwin->w_curswant;
1970 }
1971 else
1972 {
1973 wp->w_old_visual_mode = 0;
1974 wp->w_old_cursor_lnum = 0;
1975 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001976 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
1979#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1980 /* reset got_int, otherwise regexp won't work */
1981 save_got_int = got_int;
1982 got_int = 0;
1983#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001984#ifdef SYN_TIME_LIMIT
1985 /* Set the time limit to 'redrawtime'. */
1986 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001987 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989#ifdef FEAT_FOLDING
1990 win_foldinfo.fi_level = 0;
1991#endif
1992
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001993#ifdef FEAT_MENU
1994 /*
1995 * Draw the window toolbar, if there is one.
1996 * TODO: only when needed.
1997 */
1998 if (winbar_height(wp) > 0)
1999 redraw_win_toolbar(wp);
2000#endif
2001
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 /*
2003 * Update all the window rows.
2004 */
2005 idx = 0; /* first entry in w_lines[].wl_size */
2006 row = 0;
2007 srow = 0;
2008 lnum = wp->w_topline; /* first line shown in window */
2009 for (;;)
2010 {
2011 /* stop updating when reached the end of the window (check for _past_
2012 * the end of the window is at the end of the loop) */
2013 if (row == wp->w_height)
2014 {
2015 didline = TRUE;
2016 break;
2017 }
2018
2019 /* stop updating when hit the end of the file */
2020 if (lnum > buf->b_ml.ml_line_count)
2021 {
2022 eof = TRUE;
2023 break;
2024 }
2025
2026 /* Remember the starting row of the line that is going to be dealt
2027 * with. It is used further down when the line doesn't fit. */
2028 srow = row;
2029
2030 /*
2031 * Update a line when it is in an area that needs updating, when it
2032 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002033 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 * win_del_lines(), check if the current line includes it.
2035 * When syntax folding is being used, the saved syntax states will
2036 * already have been updated, we can't see where the syntax state is
2037 * the same again, just update until the end of the window.
2038 */
2039 if (row < top_end
2040 || (row >= mid_start && row < mid_end)
2041#ifdef FEAT_SEARCH_EXTRA
2042 || top_to_mod
2043#endif
2044 || idx >= wp->w_lines_valid
2045 || (row + wp->w_lines[idx].wl_size > bot_start)
2046 || (mod_top != 0
2047 && (lnum == mod_top
2048 || (lnum >= mod_top
2049 && (lnum < mod_bot
2050#ifdef FEAT_SYN_HL
2051 || did_update == DID_FOLD
2052 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002053 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 && (
2055# ifdef FEAT_FOLDING
2056 (foldmethodIsSyntax(wp)
2057 && hasAnyFolding(wp)) ||
2058# endif
2059 syntax_check_changed(lnum)))
2060#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002061#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002062 /* match in fixed position might need redraw
2063 * if lines were inserted or deleted */
2064 || (wp->w_match_head != NULL
2065 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 )))))
2068 {
2069#ifdef FEAT_SEARCH_EXTRA
2070 if (lnum == mod_top)
2071 top_to_mod = FALSE;
2072#endif
2073
2074 /*
2075 * When at start of changed lines: May scroll following lines
2076 * up or down to minimize redrawing.
2077 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002078 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 */
2080 if (lnum == mod_top
2081 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002082 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
2084 int old_rows = 0;
2085 int new_rows = 0;
2086 int xtra_rows;
2087 linenr_T l;
2088
2089 /* Count the old number of window rows, using w_lines[], which
2090 * should still contain the sizes for the lines as they are
2091 * currently displayed. */
2092 for (i = idx; i < wp->w_lines_valid; ++i)
2093 {
2094 /* Only valid lines have a meaningful wl_lnum. Invalid
2095 * lines are part of the changed area. */
2096 if (wp->w_lines[i].wl_valid
2097 && wp->w_lines[i].wl_lnum == mod_bot)
2098 break;
2099 old_rows += wp->w_lines[i].wl_size;
2100#ifdef FEAT_FOLDING
2101 if (wp->w_lines[i].wl_valid
2102 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2103 {
2104 /* Must have found the last valid entry above mod_bot.
2105 * Add following invalid entries. */
2106 ++i;
2107 while (i < wp->w_lines_valid
2108 && !wp->w_lines[i].wl_valid)
2109 old_rows += wp->w_lines[i++].wl_size;
2110 break;
2111 }
2112#endif
2113 }
2114
2115 if (i >= wp->w_lines_valid)
2116 {
2117 /* We can't find a valid line below the changed lines,
2118 * need to redraw until the end of the window.
2119 * Inserting/deleting lines has no use. */
2120 bot_start = 0;
2121 }
2122 else
2123 {
2124 /* Able to count old number of rows: Count new window
2125 * rows, and may insert/delete lines */
2126 j = idx;
2127 for (l = lnum; l < mod_bot; ++l)
2128 {
2129#ifdef FEAT_FOLDING
2130 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2131 ++new_rows;
2132 else
2133#endif
2134#ifdef FEAT_DIFF
2135 if (l == wp->w_topline)
2136 new_rows += plines_win_nofill(wp, l, TRUE)
2137 + wp->w_topfill;
2138 else
2139#endif
2140 new_rows += plines_win(wp, l, TRUE);
2141 ++j;
2142 if (new_rows > wp->w_height - row - 2)
2143 {
2144 /* it's getting too much, must redraw the rest */
2145 new_rows = 9999;
2146 break;
2147 }
2148 }
2149 xtra_rows = new_rows - old_rows;
2150 if (xtra_rows < 0)
2151 {
2152 /* May scroll text up. If there is not enough
2153 * remaining text or scrolling fails, must redraw the
2154 * rest. If scrolling works, must redraw the text
2155 * below the scrolled text. */
2156 if (row - xtra_rows >= wp->w_height - 2)
2157 mod_bot = MAXLNUM;
2158 else
2159 {
2160 check_for_delay(FALSE);
2161 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002162 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 mod_bot = MAXLNUM;
2164 else
2165 bot_start = wp->w_height + xtra_rows;
2166 }
2167 }
2168 else if (xtra_rows > 0)
2169 {
2170 /* May scroll text down. If there is not enough
2171 * remaining text of scrolling fails, must redraw the
2172 * rest. */
2173 if (row + xtra_rows >= wp->w_height - 2)
2174 mod_bot = MAXLNUM;
2175 else
2176 {
2177 check_for_delay(FALSE);
2178 if (win_ins_lines(wp, row + old_rows,
2179 xtra_rows, FALSE, FALSE) == FAIL)
2180 mod_bot = MAXLNUM;
2181 else if (top_end > row + old_rows)
2182 /* Scrolled the part at the top that requires
2183 * updating down. */
2184 top_end += xtra_rows;
2185 }
2186 }
2187
2188 /* When not updating the rest, may need to move w_lines[]
2189 * entries. */
2190 if (mod_bot != MAXLNUM && i != j)
2191 {
2192 if (j < i)
2193 {
2194 int x = row + new_rows;
2195
2196 /* move entries in w_lines[] upwards */
2197 for (;;)
2198 {
2199 /* stop at last valid entry in w_lines[] */
2200 if (i >= wp->w_lines_valid)
2201 {
2202 wp->w_lines_valid = j;
2203 break;
2204 }
2205 wp->w_lines[j] = wp->w_lines[i];
2206 /* stop at a line that won't fit */
2207 if (x + (int)wp->w_lines[j].wl_size
2208 > wp->w_height)
2209 {
2210 wp->w_lines_valid = j + 1;
2211 break;
2212 }
2213 x += wp->w_lines[j++].wl_size;
2214 ++i;
2215 }
2216 if (bot_start > x)
2217 bot_start = x;
2218 }
2219 else /* j > i */
2220 {
2221 /* move entries in w_lines[] downwards */
2222 j -= i;
2223 wp->w_lines_valid += j;
2224 if (wp->w_lines_valid > wp->w_height)
2225 wp->w_lines_valid = wp->w_height;
2226 for (i = wp->w_lines_valid; i - j >= idx; --i)
2227 wp->w_lines[i] = wp->w_lines[i - j];
2228
2229 /* The w_lines[] entries for inserted lines are
2230 * now invalid, but wl_size may be used above.
2231 * Reset to zero. */
2232 while (i >= idx)
2233 {
2234 wp->w_lines[i].wl_size = 0;
2235 wp->w_lines[i--].wl_valid = FALSE;
2236 }
2237 }
2238 }
2239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241
2242#ifdef FEAT_FOLDING
2243 /*
2244 * When lines are folded, display one line for all of them.
2245 * Otherwise, display normally (can be several display lines when
2246 * 'wrap' is on).
2247 */
2248 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2249 if (fold_count != 0)
2250 {
2251 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2252 ++row;
2253 --fold_count;
2254 wp->w_lines[idx].wl_folded = TRUE;
2255 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2256# ifdef FEAT_SYN_HL
2257 did_update = DID_FOLD;
2258# endif
2259 }
2260 else
2261#endif
2262 if (idx < wp->w_lines_valid
2263 && wp->w_lines[idx].wl_valid
2264 && wp->w_lines[idx].wl_lnum == lnum
2265 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002266 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002267#ifdef FEAT_TEXT_PROP
2268 && !bt_popup(wp->w_buffer)
2269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 && srow + wp->w_lines[idx].wl_size > wp->w_height
2271#ifdef FEAT_DIFF
2272 && diff_check_fill(wp, lnum) == 0
2273#endif
2274 )
2275 {
2276 /* This line is not going to fit. Don't draw anything here,
2277 * will draw "@ " lines below. */
2278 row = wp->w_height + 1;
2279 }
2280 else
2281 {
2282#ifdef FEAT_SEARCH_EXTRA
2283 prepare_search_hl(wp, lnum);
2284#endif
2285#ifdef FEAT_SYN_HL
2286 /* Let the syntax stuff know we skipped a few lines. */
2287 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002288 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 syntax_end_parsing(syntax_last_parsed + 1);
2290#endif
2291
2292 /*
2293 * Display one line.
2294 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002295 row = win_line(wp, lnum, srow, wp->w_height,
2296 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298#ifdef FEAT_FOLDING
2299 wp->w_lines[idx].wl_folded = FALSE;
2300 wp->w_lines[idx].wl_lastlnum = lnum;
2301#endif
2302#ifdef FEAT_SYN_HL
2303 did_update = DID_LINE;
2304 syntax_last_parsed = lnum;
2305#endif
2306 }
2307
2308 wp->w_lines[idx].wl_lnum = lnum;
2309 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002310
2311 /* Past end of the window or end of the screen. Note that after
2312 * resizing wp->w_height may be end up too big. That's a problem
2313 * elsewhere, but prevent a crash here. */
2314 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002317 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2319 ++idx;
2320 break;
2321 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002322 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 wp->w_lines[idx].wl_size = row - srow;
2324 ++idx;
2325#ifdef FEAT_FOLDING
2326 lnum += fold_count + 1;
2327#else
2328 ++lnum;
2329#endif
2330 }
2331 else
2332 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002333 if (wp->w_p_rnu)
2334 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002335#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002336 // 'relativenumber' set: The text doesn't need to be drawn, but
2337 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002338 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2339 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002340 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002341 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002342#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002343 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002344 }
2345
2346 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 row += wp->w_lines[idx++].wl_size;
2348 if (row > wp->w_height) /* past end of screen */
2349 break;
2350#ifdef FEAT_FOLDING
2351 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2352#else
2353 ++lnum;
2354#endif
2355#ifdef FEAT_SYN_HL
2356 did_update = DID_NONE;
2357#endif
2358 }
2359
2360 if (lnum > buf->b_ml.ml_line_count)
2361 {
2362 eof = TRUE;
2363 break;
2364 }
2365 }
2366 /*
2367 * End of loop over all window lines.
2368 */
2369
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002370#ifdef FEAT_VTP
2371 /* Rewrite the character at the end of the screen line. */
2372 if (use_vtp())
2373 {
2374 int i;
2375
2376 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002377 if (enc_utf8)
2378 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2379 LineOffset[i] + screen_Columns) > 1)
2380 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2381 else
2382 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2383 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002384 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2385 }
2386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 if (idx > wp->w_lines_valid)
2389 wp->w_lines_valid = idx;
2390
2391#ifdef FEAT_SYN_HL
2392 /*
2393 * Let the syntax stuff know we stop parsing here.
2394 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002395 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 syntax_end_parsing(syntax_last_parsed + 1);
2397#endif
2398
2399 /*
2400 * If we didn't hit the end of the file, and we didn't finish the last
2401 * line we were working on, then the line didn't fit.
2402 */
2403 wp->w_empty_rows = 0;
2404#ifdef FEAT_DIFF
2405 wp->w_filler_rows = 0;
2406#endif
2407 if (!eof && !didline)
2408 {
2409 if (lnum == wp->w_topline)
2410 {
2411 /*
2412 * Single line that does not fit!
2413 * Don't overwrite it, it can be edited.
2414 */
2415 wp->w_botline = lnum + 1;
2416 }
2417#ifdef FEAT_DIFF
2418 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2419 {
2420 /* Window ends in filler lines. */
2421 wp->w_botline = lnum;
2422 wp->w_filler_rows = wp->w_height - srow;
2423 }
2424#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002425#ifdef FEAT_TEXT_PROP
2426 else if (bt_popup(wp->w_buffer))
2427 {
2428 // popup line that doesn't fit is left as-is
2429 wp->w_botline = lnum;
2430 }
2431#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002432 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2433 {
2434 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2435
2436 /*
2437 * Last line isn't finished: Display "@@@" in the last screen line.
2438 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002439 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002440 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002441 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002442 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002443 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002444 set_empty_rows(wp, srow);
2445 wp->w_botline = lnum;
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2448 {
2449 /*
2450 * Last line isn't finished: Display "@@@" at the end.
2451 */
2452 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2453 W_WINROW(wp) + wp->w_height,
2454 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002455 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 set_empty_rows(wp, srow);
2457 wp->w_botline = lnum;
2458 }
2459 else
2460 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002461 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 wp->w_botline = lnum;
2463 }
2464 }
2465 else
2466 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (eof) /* we hit the end of the file */
2469 {
2470 wp->w_botline = buf->b_ml.ml_line_count + 1;
2471#ifdef FEAT_DIFF
2472 j = diff_check_fill(wp, wp->w_botline);
2473 if (j > 0 && !wp->w_botfill)
2474 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002475 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if (char2cells(fill_diff) > 1)
2477 i = '-';
2478 else
2479 i = fill_diff;
2480 if (row + j > wp->w_height)
2481 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002482 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 row += j;
2484 }
2485#endif
2486 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002487 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 wp->w_botline = lnum;
2489
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002490 // Make sure the rest of the screen is blank
2491 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002492 win_draw_end(wp,
2493#ifdef FEAT_TEXT_PROP
2494 bt_popup(wp->w_buffer) ? ' ' :
2495#endif
2496 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002499#ifdef SYN_TIME_LIMIT
2500 syn_set_timeout(NULL);
2501#endif
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /* Reset the type of redrawing required, the window has been updated. */
2504 wp->w_redr_type = 0;
2505#ifdef FEAT_DIFF
2506 wp->w_old_topfill = wp->w_topfill;
2507 wp->w_old_botfill = wp->w_botfill;
2508#endif
2509
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002510 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 {
2512 /*
2513 * There is a trick with w_botline. If we invalidate it on each
2514 * change that might modify it, this will cause a lot of expensive
2515 * calls to plines() in update_topline() each time. Therefore the
2516 * value of w_botline is often approximated, and this value is used to
2517 * compute the value of w_topline. If the value of w_botline was
2518 * wrong, check that the value of w_topline is correct (cursor is on
2519 * the visible part of the text). If it's not, we need to redraw
2520 * again. Mostly this just means scrolling up a few lines, so it
2521 * doesn't look too bad. Only do this for the current window (where
2522 * changes are relevant).
2523 */
2524 wp->w_valid |= VALID_BOTLINE;
2525 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2526 {
2527 recursive = TRUE;
2528 curwin->w_valid &= ~VALID_TOPLINE;
2529 update_topline(); /* may invalidate w_botline again */
2530 if (must_redraw != 0)
2531 {
2532 /* Don't update for changes in buffer again. */
2533 i = curbuf->b_mod_set;
2534 curbuf->b_mod_set = FALSE;
2535 win_update(curwin);
2536 must_redraw = 0;
2537 curbuf->b_mod_set = i;
2538 }
2539 recursive = FALSE;
2540 }
2541 }
2542
2543#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2544 /* restore got_int, unless CTRL-C was hit while redrawing */
2545 if (!got_int)
2546 got_int = save_got_int;
2547#endif
2548}
2549
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002551 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2552 * Return the new offset.
2553 */
2554 static int
2555screen_fill_end(
2556 win_T *wp,
2557 int c1,
2558 int c2,
2559 int off,
2560 int width,
2561 int row,
2562 int endrow,
2563 int attr)
2564{
2565 int nn = off + width;
2566
2567 if (nn > wp->w_width)
2568 nn = wp->w_width;
2569#ifdef FEAT_RIGHTLEFT
2570 if (wp->w_p_rl)
2571 {
2572 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2573 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2574 c1, c2, attr);
2575 }
2576 else
2577#endif
2578 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2579 wp->w_wincol + off, (int)wp->w_wincol + nn,
2580 c1, c2, attr);
2581 return nn;
2582}
2583
2584/*
2585 * Clear lines near the end the window and mark the unused lines with "c1".
2586 * use "c2" as the filler character.
2587 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 */
2589 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002590win_draw_end(
2591 win_T *wp,
2592 int c1,
2593 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002594 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002595 int row,
2596 int endrow,
2597 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002600 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002601 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002602
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002603 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002604
2605 if (draw_margin)
2606 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002607#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002608 int fdc = compute_foldcolumn(wp, 0);
2609
2610 if (fdc > 0)
2611 // draw the fold column
2612 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002613 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002614#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002615#ifdef FEAT_SIGNS
2616 if (signcolumn_on(wp))
2617 // draw the sign column
2618 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002619 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002620#endif
2621 if ((wp->w_p_nu || wp->w_p_rnu)
2622 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2623 // draw the number column
2624 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002625 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628#ifdef FEAT_RIGHTLEFT
2629 if (wp->w_p_rl)
2630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002632 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002633 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002635 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002636 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638 else
2639#endif
2640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002642 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002643 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 set_empty_rows(wp, row);
2647}
2648
Bram Moolenaar1a384422010-07-14 19:53:30 +02002649#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002650/*
2651 * Advance **color_cols and return TRUE when there are columns to draw.
2652 */
2653 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002654advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002655{
2656 while (**color_cols >= 0 && vcol > **color_cols)
2657 ++*color_cols;
2658 return (**color_cols >= 0);
2659}
2660#endif
2661
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002662#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2663/*
2664 * Copy "text" to ScreenLines using "attr".
2665 * Returns the next screen column.
2666 */
2667 static int
2668text_to_screenline(win_T *wp, char_u *text, int col)
2669{
2670 int off = (int)(current_ScreenLine - ScreenLines);
2671
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002672 if (has_mbyte)
2673 {
2674 int cells;
2675 int u8c, u8cc[MAX_MCO];
2676 int i;
2677 int idx;
2678 int c_len;
2679 char_u *p;
2680# ifdef FEAT_ARABIC
2681 int prev_c = 0; /* previous Arabic character */
2682 int prev_c1 = 0; /* first composing char for prev_c */
2683# endif
2684
2685# ifdef FEAT_RIGHTLEFT
2686 if (wp->w_p_rl)
2687 idx = off;
2688 else
2689# endif
2690 idx = off + col;
2691
2692 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2693 for (p = text; *p != NUL; )
2694 {
2695 cells = (*mb_ptr2cells)(p);
2696 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002697 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002698# ifdef FEAT_RIGHTLEFT
2699 - (wp->w_p_rl ? col : 0)
2700# endif
2701 )
2702 break;
2703 ScreenLines[idx] = *p;
2704 if (enc_utf8)
2705 {
2706 u8c = utfc_ptr2char(p, u8cc);
2707 if (*p < 0x80 && u8cc[0] == 0)
2708 {
2709 ScreenLinesUC[idx] = 0;
2710#ifdef FEAT_ARABIC
2711 prev_c = u8c;
2712#endif
2713 }
2714 else
2715 {
2716#ifdef FEAT_ARABIC
2717 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2718 {
2719 /* Do Arabic shaping. */
2720 int pc, pc1, nc;
2721 int pcc[MAX_MCO];
2722 int firstbyte = *p;
2723
2724 /* The idea of what is the previous and next
2725 * character depends on 'rightleft'. */
2726 if (wp->w_p_rl)
2727 {
2728 pc = prev_c;
2729 pc1 = prev_c1;
2730 nc = utf_ptr2char(p + c_len);
2731 prev_c1 = u8cc[0];
2732 }
2733 else
2734 {
2735 pc = utfc_ptr2char(p + c_len, pcc);
2736 nc = prev_c;
2737 pc1 = pcc[0];
2738 }
2739 prev_c = u8c;
2740
2741 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2742 pc, pc1, nc);
2743 ScreenLines[idx] = firstbyte;
2744 }
2745 else
2746 prev_c = u8c;
2747#endif
2748 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002749 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002750 for (i = 0; i < Screen_mco; ++i)
2751 {
2752 ScreenLinesC[i][idx] = u8cc[i];
2753 if (u8cc[i] == 0)
2754 break;
2755 }
2756 }
2757 if (cells > 1)
2758 ScreenLines[idx + 1] = 0;
2759 }
2760 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2761 /* double-byte single width character */
2762 ScreenLines2[idx] = p[1];
2763 else if (cells > 1)
2764 /* double-width character */
2765 ScreenLines[idx + 1] = p[1];
2766 col += cells;
2767 idx += cells;
2768 p += c_len;
2769 }
2770 }
2771 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002772 {
2773 int len = (int)STRLEN(text);
2774
Bram Moolenaar02631462017-09-22 15:20:32 +02002775 if (len > wp->w_width - col)
2776 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002777 if (len > 0)
2778 {
2779#ifdef FEAT_RIGHTLEFT
2780 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002781 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002782 else
2783#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002784 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002785 col += len;
2786 }
2787 }
2788 return col;
2789}
2790#endif
2791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792#ifdef FEAT_FOLDING
2793/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002794 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2795 * space is available for window "wp", minus "col".
2796 */
2797 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002798compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002799{
2800 int fdc = wp->w_p_fdc;
2801 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002802 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002803
2804 if (fdc > wwidth - (col + wmw))
2805 fdc = wwidth - (col + wmw);
2806 return fdc;
2807}
2808
2809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 * Display one folded line.
2811 */
2812 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002813fold_line(
2814 win_T *wp,
2815 long fold_count,
2816 foldinfo_T *foldinfo,
2817 linenr_T lnum,
2818 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002820 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 pos_T *top, *bot;
2822 linenr_T lnume = lnum + fold_count - 1;
2823 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002824 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 int col;
2827 int txtcol;
2828 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002829 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831 /* Build the fold line:
2832 * 1. Add the cmdwin_type for the command-line window
2833 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002834 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 * 4. Compose the text
2836 * 5. Add the text
2837 * 6. set highlighting for the Visual area an other text
2838 */
2839 col = 0;
2840
2841 /*
2842 * 1. Add the cmdwin_type for the command-line window
2843 * Ignores 'rightleft', this window is never right-left.
2844 */
2845#ifdef FEAT_CMDWIN
2846 if (cmdwin_type != 0 && wp == curwin)
2847 {
2848 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002849 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (enc_utf8)
2851 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 ++col;
2853 }
2854#endif
2855
2856 /*
2857 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002858 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002860 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (fdc > 0)
2862 {
2863 fill_foldcolumn(buf, wp, TRUE, lnum);
2864#ifdef FEAT_RIGHTLEFT
2865 if (wp->w_p_rl)
2866 {
2867 int i;
2868
Bram Moolenaar02631462017-09-22 15:20:32 +02002869 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002870 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 /* reverse the fold column */
2872 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002873 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
2875 else
2876#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002877 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 col += fdc;
2879 }
2880
2881#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002882# define RL_MEMSET(p, v, l) \
2883 do { \
2884 if (wp->w_p_rl) \
2885 for (ri = 0; ri < l; ++ri) \
2886 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2887 else \
2888 for (ri = 0; ri < l; ++ri) \
2889 ScreenAttrs[off + (p) + ri] = v; \
2890 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002892# define RL_MEMSET(p, v, l) \
2893 do { \
2894 for (ri = 0; ri < l; ++ri) \
2895 ScreenAttrs[off + (p) + ri] = v; \
2896 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#endif
2898
Bram Moolenaar64486672010-05-16 15:46:46 +02002899 /* Set all attributes of the 'number' or 'relativenumber' column and the
2900 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002901 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903#ifdef FEAT_SIGNS
2904 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002905 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002907 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 if (len > 0)
2909 {
2910 if (len > 2)
2911 len = 2;
2912# ifdef FEAT_RIGHTLEFT
2913 if (wp->w_p_rl)
2914 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002915 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002916 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else
2918# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002919 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 col += len;
2921 }
2922 }
2923#endif
2924
2925 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002926 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002928 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002930 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 if (len > 0)
2932 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002933 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002934 long num;
2935 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002936
2937 if (len > w + 1)
2938 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002939
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002940 if (wp->w_p_nu && !wp->w_p_rnu)
2941 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002942 num = (long)lnum;
2943 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002944 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002945 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002946 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002947 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002948 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002949 /* 'number' + 'relativenumber': cursor line shows absolute
2950 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002951 num = lnum;
2952 fmt = "%-*ld ";
2953 }
2954 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002955
Bram Moolenaar700e7342013-01-30 12:31:36 +01002956 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957#ifdef FEAT_RIGHTLEFT
2958 if (wp->w_p_rl)
2959 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002960 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002961 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else
2963#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002964 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 col += len;
2966 }
2967 }
2968
2969 /*
2970 * 4. Compose the folded-line string with 'foldtext', if set.
2971 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002972 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974 txtcol = col; /* remember where text starts */
2975
2976 /*
2977 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2978 * Right-left text is put in columns 0 - number-col, normal text is put
2979 * in columns number-col - window-width.
2980 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002981 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
2983 /* Fill the rest of the line with the fold filler */
2984#ifdef FEAT_RIGHTLEFT
2985 if (wp->w_p_rl)
2986 col -= txtcol;
2987#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002988 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989#ifdef FEAT_RIGHTLEFT
2990 - (wp->w_p_rl ? txtcol : 0)
2991#endif
2992 )
2993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (enc_utf8)
2995 {
2996 if (fill_fold >= 0x80)
2997 {
2998 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002999 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01003000 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02003005 ScreenLines[off + col] = fill_fold;
3006 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003007 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003009 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003010 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 }
3012
3013 if (text != buf)
3014 vim_free(text);
3015
3016 /*
3017 * 6. set highlighting for the Visual area an other text.
3018 * If all folded lines are in the Visual area, highlight the line.
3019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3021 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003022 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 {
3024 /* Visual is after curwin->w_cursor */
3025 top = &curwin->w_cursor;
3026 bot = &VIsual;
3027 }
3028 else
3029 {
3030 /* Visual is before curwin->w_cursor */
3031 top = &VIsual;
3032 bot = &curwin->w_cursor;
3033 }
3034 if (lnum >= top->lnum
3035 && lnume <= bot->lnum
3036 && (VIsual_mode != 'v'
3037 || ((lnum > top->lnum
3038 || (lnum == top->lnum
3039 && top->col == 0))
3040 && (lnume < bot->lnum
3041 || (lnume == bot->lnum
3042 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003043 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 if (VIsual_mode == Ctrl_V)
3046 {
3047 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003048 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003050 if (wp->w_old_cursor_lcol != MAXCOL
3051 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003052 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 len = wp->w_old_cursor_lcol;
3054 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003055 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003056 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003057 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059 }
3060 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003063 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 }
3066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003068#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003069 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3070 if (wp->w_p_cc_cols)
3071 {
3072 int i = 0;
3073 int j = wp->w_p_cc_cols[i];
3074 int old_txtcol = txtcol;
3075
3076 while (j > -1)
3077 {
3078 txtcol += j;
3079 if (wp->w_p_wrap)
3080 txtcol -= wp->w_skipcol;
3081 else
3082 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003083 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003084 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003085 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003086 txtcol = old_txtcol;
3087 j = wp->w_p_cc_cols[++i];
3088 }
3089 }
3090
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003091 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003092 if (wp->w_p_cuc)
3093 {
3094 txtcol += wp->w_virtcol;
3095 if (wp->w_p_wrap)
3096 txtcol -= wp->w_skipcol;
3097 else
3098 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003099 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003100 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003101 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003102 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Bram Moolenaar02631462017-09-22 15:20:32 +02003105 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003106 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108 /*
3109 * Update w_cline_height and w_cline_folded if the cursor line was
3110 * updated (saves a call to plines() later).
3111 */
3112 if (wp == curwin
3113 && lnum <= curwin->w_cursor.lnum
3114 && lnume >= curwin->w_cursor.lnum)
3115 {
3116 curwin->w_cline_row = row;
3117 curwin->w_cline_height = 1;
3118 curwin->w_cline_folded = TRUE;
3119 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3120 }
3121}
3122
3123/*
3124 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3125 */
3126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003127copy_text_attr(
3128 int off,
3129 char_u *buf,
3130 int len,
3131 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003133 int i;
3134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (enc_utf8)
3137 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003138 for (i = 0; i < len; ++i)
3139 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
3142/*
3143 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003144 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
3146 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003147fill_foldcolumn(
3148 char_u *p,
3149 win_T *wp,
3150 int closed, /* TRUE of FALSE */
3151 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 int i = 0;
3154 int level;
3155 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003156 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003157 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
3159 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003160 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 level = win_foldinfo.fi_level;
3163 if (level > 0)
3164 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003165 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003166 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003167
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 /* If the column is too narrow, we start at the lowest level that
3169 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003170 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 if (first_level < 1)
3172 first_level = 1;
3173
Bram Moolenaar1c934292015-01-27 16:39:29 +01003174 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 if (win_foldinfo.fi_lnum == lnum
3177 && first_level + i >= win_foldinfo.fi_low_level)
3178 p[i] = '-';
3179 else if (first_level == 1)
3180 p[i] = '|';
3181 else if (first_level + i <= 9)
3182 p[i] = '0' + first_level + i;
3183 else
3184 p[i] = '>';
3185 if (first_level + i == level)
3186 break;
3187 }
3188 }
3189 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003190 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191}
3192#endif /* FEAT_FOLDING */
3193
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003194#ifdef FEAT_TEXT_PROP
3195static textprop_T *current_text_props = NULL;
3196static buf_T *current_buf = NULL;
3197
3198 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003199text_prop_compare(const void *s1, const void *s2)
3200{
3201 int idx1, idx2;
3202 proptype_T *pt1, *pt2;
3203 colnr_T col1, col2;
3204
3205 idx1 = *(int *)s1;
3206 idx2 = *(int *)s2;
3207 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3208 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3209 if (pt1 == pt2)
3210 return 0;
3211 if (pt1 == NULL)
3212 return -1;
3213 if (pt2 == NULL)
3214 return 1;
3215 if (pt1->pt_priority != pt2->pt_priority)
3216 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3217 col1 = current_text_props[idx1].tp_col;
3218 col2 = current_text_props[idx2].tp_col;
3219 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3220}
3221#endif
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223/*
3224 * Display line "lnum" of window 'wp' on the screen.
3225 * Start at row "startrow", stop when "endrow" is reached.
3226 * wp->w_virtcol needs to be valid.
3227 *
3228 * Return the number of last row the line occupies.
3229 */
3230 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003231win_line(
3232 win_T *wp,
3233 linenr_T lnum,
3234 int startrow,
3235 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003236 int nochange UNUSED, // not updating for changed text
3237 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003239 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3241 int c = 0; /* init for GCC */
3242 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003243#ifdef FEAT_LINEBREAK
3244 long vcol_sbr = -1; /* virtual column after showbreak */
3245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 long vcol_prev = -1; /* "vcol" of previous character */
3247 char_u *line; /* current line */
3248 char_u *ptr; /* current position in "line" */
3249 int row; /* row in the window, excl w_winrow */
3250 int screen_row; /* row on the screen, incl w_winrow */
3251
3252 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3253 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003254 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003255 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003257 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 int extra_attr = 0; /* attributes when n_extra != 0 */
3259 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3260 displaying lcs_eol at end-of-line */
3261 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3262 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3263
3264 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3265 int saved_n_extra = 0;
3266 char_u *saved_p_extra = NULL;
3267 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003268 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 int saved_char_attr = 0;
3270
3271 int n_attr = 0; /* chars with special attr */
3272 int saved_attr2 = 0; /* char_attr saved for n_attr */
3273 int n_attr3 = 0; /* chars with overruling special attr */
3274 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3275
3276 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3277
3278 int fromcol, tocol; /* start/end of inverting */
3279 int fromcol_prev = -2; /* start of inverting after cursor */
3280 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003282 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 pos_T pos;
3284 long v;
3285
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003286 int char_attr = 0; // attributes for next character
3287 int attr_pri = FALSE; // char_attr has priority
3288 int area_highlighting = FALSE; // Visual or incsearch highlighting
3289 // in this line
3290 int vi_attr = 0; // attributes for Visual and incsearch
3291 // highlighting
3292 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003293 int win_attr = 0; // background for whole window, except
3294 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003295 int area_attr = 0; // attributes desired by highlighting
3296 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003298 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 int syntax_attr = 0; /* attributes desired by syntax */
3300 int has_syntax = FALSE; /* this buffer has syntax highl. */
3301 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003302 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003303 int draw_color_col = FALSE; /* highlight colorcolumn */
3304 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003305#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003306#ifdef FEAT_TEXT_PROP
3307 int text_prop_count;
3308 int text_prop_next = 0; // next text property to use
3309 textprop_T *text_props = NULL;
3310 int *text_prop_idxs = NULL;
3311 int text_props_active = 0;
3312 proptype_T *text_prop_type = NULL;
3313 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003314 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003315#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003316#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003317 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003318# define SPWORDLEN 150
3319 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003320 int nextlinecol = 0; /* column where nextline[] starts */
3321 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003322 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003323 int spell_attr = 0; /* attributes desired by spelling */
3324 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003325 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3326 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003327 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003328 static int cap_col = -1; /* column to check for Cap word */
3329 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003330 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003332 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 int multi_attr = 0; /* attributes desired by multibyte */
3334 int mb_l = 1; /* multi-byte byte length */
3335 int mb_c = 0; /* decoded multi-byte character */
3336 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003337 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#ifdef FEAT_DIFF
3339 int filler_lines; /* nr of filler lines to be drawn */
3340 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003341 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 int change_start = MAXCOL; /* first col of changed area */
3343 int change_end = -1; /* last col of changed area */
3344#endif
3345 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3346#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003347 int need_showbreak = FALSE; /* overlong line, skipping first x
3348 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003350#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003351 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003353 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#endif
3355#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003356 matchitem_T *cur; /* points to the match list */
3357 match_T *shl; /* points to search_hl or a match */
3358 int shl_flag; /* flag to indicate whether search_hl
3359 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003360 int pos_inprogress; /* marks that position match search is
3361 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003362 int prevcol_hl_flag; /* flag to indicate whether prevcol
3363 equals startcol of search_hl or one
3364 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#endif
3366#ifdef FEAT_ARABIC
3367 int prev_c = 0; /* previous Arabic character */
3368 int prev_c1 = 0; /* first composing char for prev_c */
3369#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003370#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003371 int did_line_attr = 0;
3372#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003373#ifdef FEAT_TERMINAL
3374 int get_term_attr = FALSE;
3375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
3377 /* draw_state: items that are drawn in sequence: */
3378#define WL_START 0 /* nothing done yet */
3379#ifdef FEAT_CMDWIN
3380# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3381#else
3382# define WL_CMDLINE WL_START
3383#endif
3384#ifdef FEAT_FOLDING
3385# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3386#else
3387# define WL_FOLD WL_CMDLINE
3388#endif
3389#ifdef FEAT_SIGNS
3390# define WL_SIGN WL_FOLD + 1 /* column for signs */
3391#else
3392# define WL_SIGN WL_FOLD /* column for signs */
3393#endif
3394#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003395#ifdef FEAT_LINEBREAK
3396# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003398# define WL_BRI WL_NR
3399#endif
3400#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3401# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3402#else
3403# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif
3405#define WL_LINE WL_SBR + 1 /* text in the line */
3406 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003407#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 int feedback_col = 0;
3409 int feedback_old_attr = -1;
3410#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003411 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
Bram Moolenaar860cae12010-06-05 23:22:07 +02003413#ifdef FEAT_CONCEAL
3414 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003415 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003416 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003417 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003418 int is_concealing = FALSE;
3419 int boguscols = 0; /* nonexistent columns added to force
3420 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003421 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003422 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003423 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003424 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003425# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003426# define FIX_FOR_BOGUSCOLS \
3427 { \
3428 n_extra += vcol_off; \
3429 vcol -= vcol_off; \
3430 vcol_off = 0; \
3431 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003432 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003433 boguscols = 0; \
3434 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003435#else
3436# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439 if (startrow > endrow) /* past the end already! */
3440 return startrow;
3441
3442 row = startrow;
3443 screen_row = row + W_WINROW(wp);
3444
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 if (!number_only)
3446 {
3447 /*
3448 * To speed up the loop below, set extra_check when there is linebreak,
3449 * trailing white space and/or syntax processing to be done.
3450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
3454#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003455 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003456# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003457 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003458# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003459 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003461 /* Prepare for syntax highlighting in this line. When there is an
3462 * error, stop syntax highlighting. */
3463 save_did_emsg = did_emsg;
3464 did_emsg = FALSE;
3465 syntax_start(wp, lnum);
3466 if (did_emsg)
3467 wp->w_s->b_syn_error = TRUE;
3468 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003469 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003470 did_emsg = save_did_emsg;
3471#ifdef SYN_TIME_LIMIT
3472 if (!wp->w_s->b_syn_slow)
3473#endif
3474 {
3475 has_syntax = TRUE;
3476 extra_check = TRUE;
3477 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003480
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003481 /* Check for columns to display for 'colorcolumn'. */
3482 color_cols = wp->w_p_cc_cols;
3483 if (color_cols != NULL)
3484 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003485#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003486
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003487#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003488 if (term_show_buffer(wp->w_buffer))
3489 {
3490 extra_check = TRUE;
3491 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003492 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003493 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003494#endif
3495
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003496#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003497 if (wp->w_p_spell
3498 && *wp->w_s->b_p_spl != NUL
3499 && wp->w_s->b_langp.ga_len > 0
3500 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003501 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003502 /* Prepare for spell checking. */
3503 has_spell = TRUE;
3504 extra_check = TRUE;
3505
Bram Moolenaar7701f302018-10-02 21:20:32 +02003506 /* Get the start of the next line, so that words that wrap to the
3507 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003508 * Trick: skip a few chars for C/shell/Vim comments */
3509 nextline[SPWORDLEN] = NUL;
3510 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3511 {
3512 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3513 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3514 }
3515
Bram Moolenaar7701f302018-10-02 21:20:32 +02003516 /* When a word wrapped from the previous line the start of the
3517 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003518 if (lnum == checked_lnum)
3519 cur_checked_col = checked_col;
3520 checked_lnum = 0;
3521
3522 /* When there was a sentence end in the previous line may require a
3523 * word starting with capital in this line. In line 1 always check
3524 * the first word. */
3525 if (lnum != capcol_lnum)
3526 cap_col = -1;
3527 if (lnum == 1)
3528 cap_col = 0;
3529 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531#endif
3532
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003533 /*
3534 * handle visual active in this window
3535 */
3536 fromcol = -10;
3537 tocol = MAXCOL;
3538 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003540 /* Visual is after curwin->w_cursor */
3541 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003543 top = &curwin->w_cursor;
3544 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003546 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003548 top = &VIsual;
3549 bot = &curwin->w_cursor;
3550 }
3551 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3552 if (VIsual_mode == Ctrl_V) /* block mode */
3553 {
3554 if (lnum_in_visual_area)
3555 {
3556 fromcol = wp->w_old_cursor_fcol;
3557 tocol = wp->w_old_cursor_lcol;
3558 }
3559 }
3560 else /* non-block mode */
3561 {
3562 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003564 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003566 if (VIsual_mode == 'V') /* linewise */
3567 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 else
3569 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003570 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3571 if (gchar_pos(top) == NUL)
3572 tocol = fromcol + 1;
3573 }
3574 }
3575 if (VIsual_mode != 'V' && lnum == bot->lnum)
3576 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003577 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003578 {
3579 fromcol = -10;
3580 tocol = MAXCOL;
3581 }
3582 else if (bot->col == MAXCOL)
3583 tocol = MAXCOL;
3584 else
3585 {
3586 pos = *bot;
3587 if (*p_sel == 'e')
3588 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3589 else
3590 {
3591 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3592 ++tocol;
3593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003598 /* Check if the character under the cursor should not be inverted */
3599 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003600#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003601 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003602#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003603 )
3604 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003606 /* if inverting in this line set area_highlighting */
3607 if (fromcol >= 0)
3608 {
3609 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003610 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003612 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003613 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003614 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003615 && clip_isautosel_plus()))
3616 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003621 /*
3622 * handle 'incsearch' and ":s///c" highlighting
3623 */
3624 else if (highlight_match
3625 && wp == curwin
3626 && lnum >= curwin->w_cursor.lnum
3627 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003629 if (lnum == curwin->w_cursor.lnum)
3630 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003631 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003632 else
3633 fromcol = 0;
3634 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3635 {
3636 pos.lnum = lnum;
3637 pos.col = search_match_endcol;
3638 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3639 }
3640 else
3641 tocol = MAXCOL;
3642 /* do at least one character; happens when past end of line */
3643 if (fromcol == tocol)
3644 tocol = fromcol + 1;
3645 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003646 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649
3650#ifdef FEAT_DIFF
3651 filler_lines = diff_check(wp, lnum);
3652 if (filler_lines < 0)
3653 {
3654 if (filler_lines == -1)
3655 {
3656 if (diff_find_change(wp, lnum, &change_start, &change_end))
3657 diff_hlf = HLF_ADD; /* added line */
3658 else if (change_start == 0)
3659 diff_hlf = HLF_TXD; /* changed text */
3660 else
3661 diff_hlf = HLF_CHD; /* changed line */
3662 }
3663 else
3664 diff_hlf = HLF_ADD; /* added line */
3665 filler_lines = 0;
3666 area_highlighting = TRUE;
3667 }
3668 if (lnum == wp->w_topline)
3669 filler_lines = wp->w_topfill;
3670 filler_todo = filler_lines;
3671#endif
3672
3673#ifdef LINE_ATTR
3674# ifdef FEAT_SIGNS
3675 /* If this line has a sign with line highlighting set line_attr. */
3676 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3677 if (v != 0)
3678 line_attr = sign_get_attr((int)v, TRUE);
3679# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003680# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003682 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003683 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684# endif
3685 if (line_attr != 0)
3686 area_highlighting = TRUE;
3687#endif
3688
3689 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3690 ptr = line;
3691
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003692#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003693 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003694 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003695 /* For checking first word with a capital skip white space. */
3696 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003697 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003698
Bram Moolenaar30abd282005-06-22 22:35:10 +00003699 /* To be able to spell-check over line boundaries copy the end of the
3700 * current line into nextline[]. Above the start of the next line was
3701 * copied to nextline[SPWORDLEN]. */
3702 if (nextline[SPWORDLEN] == NUL)
3703 {
3704 /* No next line or it is empty. */
3705 nextlinecol = MAXCOL;
3706 nextline_idx = 0;
3707 }
3708 else
3709 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003710 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003711 if (v < SPWORDLEN)
3712 {
3713 /* Short line, use it completely and append the start of the
3714 * next line. */
3715 nextlinecol = 0;
3716 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003717 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003718 nextline_idx = v + 1;
3719 }
3720 else
3721 {
3722 /* Long line, use only the last SPWORDLEN bytes. */
3723 nextlinecol = v - SPWORDLEN;
3724 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3725 nextline_idx = SPWORDLEN + 1;
3726 }
3727 }
3728 }
3729#endif
3730
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003731 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003733 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003734 extra_check = TRUE;
3735 /* find start of trailing whitespace */
3736 if (lcs_trail)
3737 {
3738 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003739 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003740 --trailcol;
3741 trailcol += (colnr_T) (ptr - line);
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003745 wcr_attr = get_wcr_attr(wp);
3746 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003747 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003748 win_attr = wcr_attr;
3749 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003750 }
3751#ifdef FEAT_TEXT_PROP
3752 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003753 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003754#endif
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 /*
3757 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3758 * first character to be displayed.
3759 */
3760 if (wp->w_p_wrap)
3761 v = wp->w_skipcol;
3762 else
3763 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003764 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 while (vcol < v && *ptr != NUL)
3769 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003770 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003773 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003776 /* When:
3777 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003778 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003779 * - 'virtualedit' is set, or
3780 * - the visual mode is active,
3781 * the end of the line may be before the start of the displayed part.
3782 */
3783 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003784#ifdef FEAT_SYN_HL
3785 wp->w_p_cuc || draw_color_col ||
3786#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003787 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003788 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 /* Handle a character that's not completely on the screen: Put ptr at
3792 * that character but skip the first few screen characters. */
3793 if (vcol > v)
3794 {
3795 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003797 /* If the character fits on the screen, don't need to skip it.
3798 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003799 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003800 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802
3803 /*
3804 * Adjust for when the inverted text is before the screen,
3805 * and when the start of the inverted text is before the screen.
3806 */
3807 if (tocol <= vcol)
3808 fromcol = 0;
3809 else if (fromcol >= 0 && fromcol < vcol)
3810 fromcol = vcol;
3811
3812#ifdef FEAT_LINEBREAK
3813 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3814 if (wp->w_p_wrap)
3815 need_showbreak = TRUE;
3816#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003817#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003818 /* When spell checking a word we need to figure out the start of the
3819 * word and if it's badly spelled or not. */
3820 if (has_spell)
3821 {
3822 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003823 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003824 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003825
3826 pos = wp->w_cursor;
3827 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003828 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003829 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003830
3831 /* spell_move_to() may call ml_get() and make "line" invalid */
3832 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3833 ptr = line + linecol;
3834
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003835 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003836 {
3837 /* no bad word found at line start, don't check until end of a
3838 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003839 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003840 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003841 }
3842 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003843 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003844 /* bad word found, use attributes until end of word */
3845 word_end = wp->w_cursor.col + len + 1;
3846
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003847 /* Turn index into actual attributes. */
3848 if (spell_hlf != HLF_COUNT)
3849 spell_attr = highlight_attr[spell_hlf];
3850 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003851 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003852
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003853# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003854 /* Need to restart syntax highlighting for this line. */
3855 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003856 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003857# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003858 }
3859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861
3862 /*
3863 * Correct highlighting for cursor that can't be disabled.
3864 * Avoids having to check this for each character.
3865 */
3866 if (fromcol >= 0)
3867 {
3868 if (noinvcur)
3869 {
3870 if ((colnr_T)fromcol == wp->w_virtcol)
3871 {
3872 /* highlighting starts at cursor, let it start just after the
3873 * cursor */
3874 fromcol_prev = fromcol;
3875 fromcol = -1;
3876 }
3877 else if ((colnr_T)fromcol < wp->w_virtcol)
3878 /* restart highlighting after the cursor */
3879 fromcol_prev = wp->w_virtcol;
3880 }
3881 if (fromcol >= tocol)
3882 fromcol = -1;
3883 }
3884
3885#ifdef FEAT_SEARCH_EXTRA
3886 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003887 * Handle highlighting the last used search pattern and matches.
3888 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003889 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003891 cur = wp->w_match_head;
3892 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003893 while ((cur != NULL || shl_flag == FALSE) && !number_only
3894 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003896 if (shl_flag == FALSE)
3897 {
3898 shl = &search_hl;
3899 shl_flag = TRUE;
3900 }
3901 else
3902 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003903 shl->startcol = MAXCOL;
3904 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003906 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003907 v = (long)(ptr - line);
3908 if (cur != NULL)
3909 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003910 next_search_hl(wp, shl, lnum, (colnr_T)v,
3911 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003912
3913 /* Need to get the line again, a multi-line regexp may have made it
3914 * invalid. */
3915 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3916 ptr = line + v;
3917
3918 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003920 if (shl->lnum == lnum)
3921 shl->startcol = shl->rm.startpos[0].col;
3922 else
3923 shl->startcol = 0;
3924 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3925 - shl->rm.startpos[0].lnum)
3926 shl->endcol = shl->rm.endpos[0].col;
3927 else
3928 shl->endcol = MAXCOL;
3929 /* Highlight one character for an empty match. */
3930 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003932 if (has_mbyte && line[shl->endcol] != NUL)
3933 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3934 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003935 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003937 if ((long)shl->startcol < v) /* match at leftcol */
3938 {
3939 shl->attr_cur = shl->attr;
3940 search_attr = shl->attr;
3941 }
3942 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003944 if (shl != &search_hl && cur != NULL)
3945 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947#endif
3948
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003949#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003950 // Cursor line highlighting for 'cursorline' in the current window.
3951 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003952 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003953 // Do not show the cursor line when Visual mode is active, because it's
3954 // not clear what is selected then. Do update w_last_cursorline.
3955 if (!(wp == curwin && VIsual_active))
3956 {
3957 line_attr = HL_ATTR(HLF_CUL);
3958 area_highlighting = TRUE;
3959 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003960 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003961 }
3962#endif
3963
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003964#ifdef FEAT_TEXT_PROP
3965 {
3966 char_u *prop_start;
3967
3968 text_prop_count = get_text_props(wp->w_buffer, lnum,
3969 &prop_start, FALSE);
3970 if (text_prop_count > 0)
3971 {
3972 // Make a copy of the properties, so that they are properly
3973 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003974 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003975 if (text_props != NULL)
3976 mch_memmove(text_props, prop_start,
3977 text_prop_count * sizeof(textprop_T));
3978
3979 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003980 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003981 area_highlighting = TRUE;
3982 extra_check = TRUE;
3983 }
3984 }
3985#endif
3986
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003987 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#ifdef FEAT_RIGHTLEFT
3991 if (wp->w_p_rl)
3992 {
3993 /* Rightleft window: process the text in the normal direction, but put
3994 * it in current_ScreenLine[] from right to left. Start at the
3995 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003996 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003998 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000#endif
4001
4002 /*
4003 * Repeat for the whole displayed line.
4004 */
4005 for (;;)
4006 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02004007#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004008 int has_match_conc = 0; // match wants to conceal
4009 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 /* Skip this quickly when working on the text. */
4012 if (draw_state != WL_LINE)
4013 {
4014#ifdef FEAT_CMDWIN
4015 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4016 {
4017 draw_state = WL_CMDLINE;
4018 if (cmdwin_type != 0 && wp == curwin)
4019 {
4020 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004022 c_extra = cmdwin_type;
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_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 }
4027#endif
4028
4029#ifdef FEAT_FOLDING
4030 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4031 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004032 int fdc = compute_foldcolumn(wp, 0);
4033
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004035 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004037 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004038 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004039 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004040 p_extra_free = alloc(12 + 1);
4041
4042 if (p_extra_free != NULL)
4043 {
4044 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4045 n_extra = fdc;
4046 p_extra_free[n_extra] = NUL;
4047 p_extra = p_extra_free;
4048 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004049 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004050 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053 }
4054#endif
4055
4056#ifdef FEAT_SIGNS
4057 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4058 {
4059 draw_state = WL_SIGN;
4060 /* Show the sign column when there are any signs in this
4061 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004062 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004064 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004066 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067# endif
4068
4069 /* Draw two cells with the sign value or blank. */
4070 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004071 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004072 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 n_extra = 2;
4074
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004075 if (row == startrow
4076#ifdef FEAT_DIFF
4077 + filler_lines && filler_todo <= 0
4078#endif
4079 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4082 SIGN_TEXT);
4083# ifdef FEAT_SIGN_ICONS
4084 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4085 SIGN_ICON);
4086 if (gui.in_use && icon_sign != 0)
4087 {
4088 /* Use the image in this position. */
4089 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004090 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091# ifdef FEAT_NETBEANS_INTG
4092 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004095 c_final = NUL;
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097# endif
4098 char_attr = icon_sign;
4099 }
4100 else
4101# endif
4102 if (text_sign != 0)
4103 {
4104 p_extra = sign_get_text(text_sign);
4105 if (p_extra != NULL)
4106 {
4107 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004108 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004109 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 char_attr = sign_get_attr(text_sign, FALSE);
4112 }
4113 }
4114 }
4115 }
4116#endif
4117
4118 if (draw_state == WL_NR - 1 && n_extra == 0)
4119 {
4120 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004121 /* Display the absolute or relative line number. After the
4122 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4123 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 && (row == startrow
4125#ifdef FEAT_DIFF
4126 + filler_lines
4127#endif
4128 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4129 {
4130 /* Draw the line number (empty space after wrapping). */
4131 if (row == startrow
4132#ifdef FEAT_DIFF
4133 + filler_lines
4134#endif
4135 )
4136 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004137 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004138 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004139
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004140 if (wp->w_p_nu && !wp->w_p_rnu)
4141 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004142 num = (long)lnum;
4143 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004144 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004145 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004146 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004147 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004148 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004149 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004150 num = lnum;
4151 fmt = "%-*ld ";
4152 }
4153 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004154
Bram Moolenaar700e7342013-01-30 12:31:36 +01004155 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004156 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 if (wp->w_skipcol > 0)
4158 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4159 *p_extra = '-';
4160#ifdef FEAT_RIGHTLEFT
4161 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004162 {
4163 char_u *p1, *p2;
4164 int t;
4165
4166 // like rl_mirror(), but keep the space at the end
4167 p2 = skiptowhite(extra) - 1;
4168 for (p1 = extra; p1 < p2; ++p1, --p2)
4169 {
4170 t = *p1;
4171 *p1 = *p2;
4172 *p2 = t;
4173 }
4174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175#endif
4176 p_extra = extra;
4177 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004178 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004183 c_final = NUL;
4184 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004185 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004186 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004187#ifdef FEAT_SYN_HL
4188 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004189 * the current line differently.
4190 * TODO: Can we use CursorLine instead of CursorLineNr
4191 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004192 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004193 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004194 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 }
4198
Bram Moolenaar597a4222014-06-25 14:39:50 +02004199#ifdef FEAT_LINEBREAK
4200 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4201 && n_extra == 0 && *p_sbr != NUL)
4202 /* draw indent after showbreak value */
4203 draw_state = WL_BRI;
4204 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4205 /* After the showbreak, draw the breakindent */
4206 draw_state = WL_BRI - 1;
4207
4208 /* draw 'breakindent': indent wrapped text accordingly */
4209 if (draw_state == WL_BRI - 1 && n_extra == 0)
4210 {
4211 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004212 /* if need_showbreak is set, breakindent also applies */
4213 if (wp->w_p_bri && n_extra == 0
4214 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004215# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004216 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004217# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004218 )
4219 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004220 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004221# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004222 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004223 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004224 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004225# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004226 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4227 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004228 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004229# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004230 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004231# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004232 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004233 c_extra = ' ';
4234 n_extra = get_breakindent_win(wp,
4235 ml_get_buf(wp->w_buffer, lnum, FALSE));
4236 /* Correct end of highlighted area for 'breakindent',
4237 * required when 'linebreak' is also set. */
4238 if (tocol == vcol)
4239 tocol += n_extra;
4240 }
4241 }
4242#endif
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4245 if (draw_state == WL_SBR - 1 && n_extra == 0)
4246 {
4247 draw_state = WL_SBR;
4248# ifdef FEAT_DIFF
4249 if (filler_todo > 0)
4250 {
4251 /* Draw "deleted" diff line(s). */
4252 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004255 c_final = NUL;
4256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004260 c_final = NUL;
4261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262# ifdef FEAT_RIGHTLEFT
4263 if (wp->w_p_rl)
4264 n_extra = col + 1;
4265 else
4266# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004267 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004268 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270# endif
4271# ifdef FEAT_LINEBREAK
4272 if (*p_sbr != NUL && need_showbreak)
4273 {
4274 /* Draw 'showbreak' at the start of each broken line. */
4275 p_extra = p_sbr;
4276 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004277 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004279 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004281 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 /* Correct end of highlighted area for 'showbreak',
4283 * required when 'linebreak' is also set. */
4284 if (tocol == vcol)
4285 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004286#ifdef FEAT_SYN_HL
4287 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004288 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004289 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004290 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293# endif
4294 }
4295#endif
4296
4297 if (draw_state == WL_LINE - 1 && n_extra == 0)
4298 {
4299 draw_state = WL_LINE;
4300 if (saved_n_extra)
4301 {
4302 /* Continue item from end of wrapped line. */
4303 n_extra = saved_n_extra;
4304 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004305 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 p_extra = saved_p_extra;
4307 char_attr = saved_char_attr;
4308 }
4309 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004310 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 }
4313
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004314 // When still displaying '$' of change command, stop at cursor.
4315 // When only displaying the (relative) line number and that's done,
4316 // stop here.
4317 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004318 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319#ifdef FEAT_DIFF
4320 && filler_todo <= 0
4321#endif
4322 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004323 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004325 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004326 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004327 /* Pretend we have finished updating the window. Except when
4328 * 'cursorcolumn' is set. */
4329#ifdef FEAT_SYN_HL
4330 if (wp->w_p_cuc)
4331 row = wp->w_cline_row + wp->w_cline_height;
4332 else
4333#endif
4334 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 break;
4336 }
4337
Bram Moolenaar637532b2019-01-03 21:44:40 +01004338 if (draw_state == WL_LINE && (area_highlighting
4339#ifdef FEAT_SPELL
4340 || has_spell
4341#endif
4342 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 {
4344 /* handle Visual or match highlighting in this line */
4345 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4347 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004349 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004351 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 else if (area_attr != 0
4353 && (vcol == tocol
4354 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357#ifdef FEAT_SEARCH_EXTRA
4358 if (!n_extra)
4359 {
4360 /*
4361 * Check for start/end of search pattern match.
4362 * After end, check for start/end of next match.
4363 * When another match, have to check for start again.
4364 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004365 * Do this for 'search_hl' and the match list (ordered by
4366 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004368 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004369 cur = wp->w_match_head;
4370 shl_flag = FALSE;
4371 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004373 if (shl_flag == FALSE
4374 && ((cur != NULL
4375 && cur->priority > SEARCH_HL_PRIORITY)
4376 || cur == NULL))
4377 {
4378 shl = &search_hl;
4379 shl_flag = TRUE;
4380 }
4381 else
4382 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004383 if (cur != NULL)
4384 cur->pos.cur = 0;
4385 pos_inprogress = TRUE;
4386 while (shl->rm.regprog != NULL
4387 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004389 if (shl->startcol != MAXCOL
4390 && v >= (long)shl->startcol
4391 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004393 int tmp_col = v + MB_PTR2LEN(ptr);
4394
4395 if (shl->endcol < tmp_col)
4396 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004398#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004399 // Match with the "Conceal" group results in hiding
4400 // the match.
4401 if (cur != NULL
4402 && shl != &search_hl
4403 && syn_name2id((char_u *)"Conceal")
4404 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004405 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004406 has_match_conc =
4407 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004408 match_conc = cur->conceal_char;
4409 }
4410 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004411 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004414 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004417 next_search_hl(wp, shl, lnum, (colnr_T)v,
4418 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004419 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004420 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 /* Need to get the line again, a multi-line regexp
4423 * may have made it invalid. */
4424 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4425 ptr = line + v;
4426
4427 if (shl->lnum == lnum)
4428 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004429 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004431 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004433 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004435 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 /* highlight empty match, try again after
4438 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004440 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004441 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004443 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445
4446 /* Loop to check if the match starts at the
4447 * current position */
4448 continue;
4449 }
4450 }
4451 break;
4452 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004453 if (shl != &search_hl && cur != NULL)
4454 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004456
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004457 /* Use attributes from match with highest priority among
4458 * 'search_hl' and the match list. */
4459 search_attr = search_hl.attr_cur;
4460 cur = wp->w_match_head;
4461 shl_flag = FALSE;
4462 while (cur != NULL || shl_flag == FALSE)
4463 {
4464 if (shl_flag == FALSE
4465 && ((cur != NULL
4466 && cur->priority > SEARCH_HL_PRIORITY)
4467 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004468 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004469 shl = &search_hl;
4470 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004471 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004472 else
4473 shl = &cur->hl;
4474 if (shl->attr_cur != 0)
4475 search_attr = shl->attr_cur;
4476 if (shl != &search_hl && cur != NULL)
4477 cur = cur->next;
4478 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004479 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004480 if (*ptr == NUL && (did_line_attr >= 1
4481 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004482 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484#endif
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004487 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004489 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4490 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004492 if (diff_hlf == HLF_TXD && ptr - line > change_end
4493 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004496 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004497 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004500
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004501#ifdef FEAT_TEXT_PROP
4502 if (text_props != NULL)
4503 {
4504 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004505 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004506
4507 // Check if any active property ends.
4508 for (pi = 0; pi < text_props_active; ++pi)
4509 {
4510 int tpi = text_prop_idxs[pi];
4511
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004512 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004513 + text_props[tpi].tp_len)
4514 {
4515 if (pi + 1 < text_props_active)
4516 mch_memmove(text_prop_idxs + pi,
4517 text_prop_idxs + pi + 1,
4518 sizeof(int)
4519 * (text_props_active - (pi + 1)));
4520 --text_props_active;
4521 --pi;
4522 }
4523 }
4524
4525 // Add any text property that starts in this column.
4526 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004527 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004528 text_prop_idxs[text_props_active++] = text_prop_next++;
4529
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004530 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004531 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004532 if (text_props_active > 0)
4533 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004534 // Sort the properties on priority and/or starting last.
4535 // Then combine the attributes, highest priority last.
4536 current_text_props = text_props;
4537 current_buf = wp->w_buffer;
4538 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4539 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004540
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004541 for (pi = 0; pi < text_props_active; ++pi)
4542 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004543 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004544 proptype_T *pt = text_prop_type_by_id(
4545 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004546
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004547 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004548 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004549 int pt_attr = syn_id2attr(pt->pt_hl_id);
4550
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004551 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004552 text_prop_attr =
4553 hl_combine_attr(text_prop_attr, pt_attr);
4554 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004555 }
4556 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004557 }
4558 }
4559#endif
4560
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004561 /* Decide which of the highlight attributes to use. */
4562 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004563#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004564 if (area_attr != 0)
4565 char_attr = hl_combine_attr(line_attr, area_attr);
4566 else if (search_attr != 0)
4567 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004568# ifdef FEAT_TEXT_PROP
4569 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004570 {
4571 char_attr = hl_combine_attr(
4572 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4573 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004574# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004575 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004576 || vcol < fromcol || vcol_prev < fromcol_prev
4577 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004578 // Use line_attr when not in the Visual or 'incsearch' area
4579 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004580 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004581#else
4582 if (area_attr != 0)
4583 char_attr = area_attr;
4584 else if (search_attr != 0)
4585 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004586#endif
4587 else
4588 {
4589 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004590#ifdef FEAT_TEXT_PROP
4591 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004592 {
4593 if (text_prop_combine)
4594 char_attr = hl_combine_attr(
4595 syntax_attr, text_prop_attr);
4596 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004597 char_attr = hl_combine_attr(
4598 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004599 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004600 else
4601#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004602#ifdef FEAT_SYN_HL
4603 if (has_syntax)
4604 char_attr = syntax_attr;
4605 else
4606#endif
4607 char_attr = 0;
4608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004610 if (char_attr == 0)
4611 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613 /*
4614 * Get the next character to put on the screen.
4615 */
4616 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004617 * The "p_extra" points to the extra stuff that is inserted to
4618 * represent special characters (non-printable stuff) and other
4619 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004620 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004621 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4622 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4624 */
4625 if (n_extra > 0)
4626 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004627 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004629 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004631 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
4633 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004634 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004635 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 }
4637 else
4638 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 else
4641 {
4642 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if (has_mbyte)
4644 {
4645 mb_c = c;
4646 if (enc_utf8)
4647 {
4648 /* If the UTF-8 character is more than one byte:
4649 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004650 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 mb_utf8 = FALSE;
4652 if (mb_l > n_extra)
4653 mb_l = 1;
4654 else if (mb_l > 1)
4655 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004656 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004658 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 }
4661 else
4662 {
4663 /* if this is a DBCS character, put it in "mb_c" */
4664 mb_l = MB_BYTE2LEN(c);
4665 if (mb_l >= n_extra)
4666 mb_l = 1;
4667 else if (mb_l > 1)
4668 mb_c = (c << 8) + p_extra[1];
4669 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004670 if (mb_l == 0) /* at the NUL at end-of-line */
4671 mb_l = 1;
4672
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 /* If a double-width char doesn't fit display a '>' in the
4674 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004675 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676# ifdef FEAT_RIGHTLEFT
4677 wp->w_p_rl ? (col <= 0) :
4678# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004679 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 && (*mb_char2cells)(mb_c) == 2)
4681 {
4682 c = '>';
4683 mb_c = c;
4684 mb_l = 1;
4685 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004686 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 /* put the pointer back to output the double-width
4688 * character at the start of the next line. */
4689 ++n_extra;
4690 --p_extra;
4691 }
4692 else
4693 {
4694 n_extra -= mb_l - 1;
4695 p_extra += mb_l - 1;
4696 }
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 ++p_extra;
4699 }
4700 --n_extra;
4701 }
4702 else
4703 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004704#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004705 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004706#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004707
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004708 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004709 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 /*
4711 * Get a character from the line itself.
4712 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004713 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004714#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004715 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (has_mbyte)
4718 {
4719 mb_c = c;
4720 if (enc_utf8)
4721 {
4722 /* If the UTF-8 character is more than one byte: Decode it
4723 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004724 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 mb_utf8 = FALSE;
4726 if (mb_l > 1)
4727 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004728 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 /* Overlong encoded ASCII or ASCII with composing char
4730 * is displayed normally, except a NUL. */
4731 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004732 {
4733 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004734#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004735 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004736#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004739
4740 /* At start of the line we can have a composing char.
4741 * Draw it as a space with a composing char. */
4742 if (utf_iscomposing(mb_c))
4743 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004744 int i;
4745
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004746 for (i = Screen_mco - 1; i > 0; --i)
4747 u8cc[i] = u8cc[i - 1];
4748 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004749 mb_c = ' ';
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752
4753 if ((mb_l == 1 && c >= 0x80)
4754 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004755 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
4757 /*
4758 * Illegal UTF-8 byte: display as <xx>.
4759 * Non-BMP character : display as ? or fullwidth ?.
4760 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004761 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004762# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004763 if (wp->w_p_rl) /* reverse */
4764 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 p_extra = extra;
4767 c = *p_extra;
4768 mb_c = mb_ptr2char_adv(&p_extra);
4769 mb_utf8 = (c >= 0x80);
4770 n_extra = (int)STRLEN(p_extra);
4771 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004772 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (area_attr == 0 && search_attr == 0)
4774 {
4775 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004776 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 saved_attr2 = char_attr; /* save current attr */
4778 }
4779 }
4780 else if (mb_l == 0) /* at the NUL at end-of-line */
4781 mb_l = 1;
4782#ifdef FEAT_ARABIC
4783 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4784 {
4785 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004786 int pc, pc1, nc;
4787 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
4789 /* The idea of what is the previous and next
4790 * character depends on 'rightleft'. */
4791 if (wp->w_p_rl)
4792 {
4793 pc = prev_c;
4794 pc1 = prev_c1;
4795 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004796 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 else
4799 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004800 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004802 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804 prev_c = mb_c;
4805
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004806 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 else
4809 prev_c = mb_c;
4810#endif
4811 }
4812 else /* enc_dbcs */
4813 {
4814 mb_l = MB_BYTE2LEN(c);
4815 if (mb_l == 0) /* at the NUL at end-of-line */
4816 mb_l = 1;
4817 else if (mb_l > 1)
4818 {
4819 /* We assume a second byte below 32 is illegal.
4820 * Hopefully this is OK for all double-byte encodings!
4821 */
4822 if (ptr[1] >= 32)
4823 mb_c = (c << 8) + ptr[1];
4824 else
4825 {
4826 if (ptr[1] == NUL)
4827 {
4828 /* head byte at end of line */
4829 mb_l = 1;
4830 transchar_nonprint(extra, c);
4831 }
4832 else
4833 {
4834 /* illegal tail byte */
4835 mb_l = 2;
4836 STRCPY(extra, "XX");
4837 }
4838 p_extra = extra;
4839 n_extra = (int)STRLEN(extra) - 1;
4840 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004841 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 c = *p_extra++;
4843 if (area_attr == 0 && search_attr == 0)
4844 {
4845 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004846 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 saved_attr2 = char_attr; /* save current attr */
4848 }
4849 mb_c = c;
4850 }
4851 }
4852 }
4853 /* If a double-width char doesn't fit display a '>' in the
4854 * last column; the character is displayed at the start of the
4855 * next line. */
4856 if ((
4857# ifdef FEAT_RIGHTLEFT
4858 wp->w_p_rl ? (col <= 0) :
4859# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004860 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 && (*mb_char2cells)(mb_c) == 2)
4862 {
4863 c = '>';
4864 mb_c = c;
4865 mb_utf8 = FALSE;
4866 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004867 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004868 // Put pointer back so that the character will be
4869 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004871#ifdef FEAT_CONCEAL
4872 did_decrement_ptr = TRUE;
4873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 else if (*ptr != NUL)
4876 ptr += mb_l - 1;
4877
4878 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004879 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004880 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004881 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004884 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004885 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 c = ' ';
4887 if (area_attr == 0 && search_attr == 0)
4888 {
4889 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004890 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 saved_attr2 = char_attr; /* save current attr */
4892 }
4893 mb_c = c;
4894 mb_utf8 = FALSE;
4895 mb_l = 1;
4896 }
4897
4898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 ++ptr;
4900
4901 if (extra_check)
4902 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004903#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004904 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004905#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004906
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004907#ifdef FEAT_TERMINAL
4908 if (get_term_attr)
4909 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004910 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004911
4912 if (!attr_pri)
4913 char_attr = syntax_attr;
4914 else
4915 char_attr = hl_combine_attr(syntax_attr, char_attr);
4916 }
4917#endif
4918
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004919#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004920 // Get syntax attribute, unless still at the start of the line
4921 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004922 v = (long)(ptr - line);
4923 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
4925 /* Get the syntax attribute for the character. If there
4926 * is an error, disable syntax highlighting. */
4927 save_did_emsg = did_emsg;
4928 did_emsg = FALSE;
4929
Bram Moolenaar217ad922005-03-20 22:37:15 +00004930 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004931# ifdef FEAT_SPELL
4932 has_spell ? &can_spell :
4933# endif
4934 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
4936 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004937 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004938 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004939 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004940 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 else
4943 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004944
4945 // combine syntax attribute with 'wincolor'
4946 if (win_attr != 0)
4947 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4948
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004949#ifdef SYN_TIME_LIMIT
4950 if (wp->w_s->b_syn_slow)
4951 has_syntax = FALSE;
4952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /* Need to get the line again, a multi-line regexp may
4955 * have made it invalid. */
4956 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4957 ptr = line + v;
4958
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004959# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004960 // Text properties overrule syntax highlighting or combine.
4961 if (text_prop_attr == 0 || text_prop_combine)
4962# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004963 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004964 int comb_attr = syntax_attr;
4965# ifdef FEAT_TEXT_PROP
4966 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4967# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004968 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004969 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004970 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004971 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004972 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004973# ifdef FEAT_CONCEAL
4974 /* no concealing past the end of the line, it interferes
4975 * with line highlighting */
4976 if (c == NUL)
4977 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004978 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004979 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004982#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004983
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004984#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004985 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004986 * Only do this when there is no syntax highlighting, the
4987 * @Spell cluster is not used or the current syntax item
4988 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004989 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004990 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004991 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004992 if (c != 0 && (
4993# ifdef FEAT_SYN_HL
4994 !has_syntax ||
4995# endif
4996 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004997 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004998 char_u *prev_ptr, *p;
4999 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005000 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00005001 if (has_mbyte)
5002 {
5003 prev_ptr = ptr - mb_l;
5004 v -= mb_l - 1;
5005 }
5006 else
Bram Moolenaare7566042005-06-17 22:00:15 +00005007 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00005008
5009 /* Use nextline[] if possible, it has the start of the
5010 * next line concatenated. */
5011 if ((prev_ptr - line) - nextlinecol >= 0)
5012 p = nextline + (prev_ptr - line) - nextlinecol;
5013 else
5014 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005015 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005016 len = spell_check(wp, p, &spell_hlf, &cap_col,
5017 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005018 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005019
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005020 /* In Insert mode only highlight a word that
5021 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005022 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005023 && (State & INSERT) != 0
5024 && wp->w_cursor.lnum == lnum
5025 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005026 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005027 && wp->w_cursor.col < (colnr_T)word_end)
5028 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005029 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005030 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005031 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005032
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005033 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005034 && (p - nextline) + len > nextline_idx)
5035 {
5036 /* Remember that the good word continues at the
5037 * start of the next line. */
5038 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005039 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005040 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005041
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005042 /* Turn index into actual attributes. */
5043 if (spell_hlf != HLF_COUNT)
5044 spell_attr = highlight_attr[spell_hlf];
5045
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005046 if (cap_col > 0)
5047 {
5048 if (p != prev_ptr
5049 && (p - nextline) + cap_col >= nextline_idx)
5050 {
5051 /* Remember that the word in the next line
5052 * must start with a capital. */
5053 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005054 cap_col = (int)((p - nextline) + cap_col
5055 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005056 }
5057 else
5058 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005059 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005060 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005061 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005062 }
5063 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005064 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005065 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005066 char_attr = hl_combine_attr(char_attr, spell_attr);
5067 else
5068 char_attr = hl_combine_attr(spell_attr, char_attr);
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070#endif
5071#ifdef FEAT_LINEBREAK
5072 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005073 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005075 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005076 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005078 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005079 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005080
Bram Moolenaar597a4222014-06-25 14:39:50 +02005081 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005082 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005083 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005084 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005085# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005086 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005087 wp->w_buffer->b_p_vts_array) - 1;
5088# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005089 n_extra = (int)wp->w_buffer->b_p_ts
5090 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005091# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005092
Bram Moolenaar4df70292015-03-21 14:20:16 +01005093 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005094 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005095 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005096 {
5097#ifdef FEAT_CONCEAL
5098 if (c == TAB)
5099 /* See "Tab alignment" below. */
5100 FIX_FOR_BOGUSCOLS;
5101#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005102 if (!wp->w_p_list)
5103 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106#endif
5107
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005108 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5109 // But not when the character is followed by a composing
5110 // character (use mb_l to check that).
5111 if (wp->w_p_list
5112 && ((((c == 160 && mb_l == 1)
5113 || (mb_utf8
5114 && ((mb_c == 160 && mb_l == 2)
5115 || (mb_c == 0x202f && mb_l == 3))))
5116 && lcs_nbsp)
5117 || (c == ' '
5118 && mb_l == 1
5119 && lcs_space
5120 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005121 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005122 c = (c == ' ') ? lcs_space : lcs_nbsp;
5123 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005124 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005125 n_attr = 1;
5126 extra_attr = HL_ATTR(HLF_8);
5127 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005128 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005129 mb_c = c;
5130 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005131 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005132 mb_utf8 = TRUE;
5133 u8cc[0] = 0;
5134 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005135 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005136 else
5137 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005138 }
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5141 {
5142 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005143 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005146 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 saved_attr2 = char_attr; /* save current attr */
5148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005150 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
5152 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005153 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005154 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156 else
5157 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 }
5160
5161 /*
5162 * Handling of non-printable characters.
5163 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005164 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 /*
5167 * when getting a character from the file, we may have to
5168 * turn it into something else on the way to putting it
5169 * into "ScreenLines".
5170 */
5171 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5172 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005173 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005174 long vcol_adjusted = vcol; /* removed showbreak length */
5175#ifdef FEAT_LINEBREAK
5176 /* only adjust the tab_len, when at the first column
5177 * after the showbreak value was drawn */
5178 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5179 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005182#ifdef FEAT_VARTABS
5183 tab_len = tabstop_padding(vcol_adjusted,
5184 wp->w_buffer->b_p_ts,
5185 wp->w_buffer->b_p_vts_array) - 1;
5186#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005187 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005188 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5189#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005190
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005191#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005192 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005193#endif
5194 /* tab amount depends on current column */
5195 n_extra = tab_len;
5196#ifdef FEAT_LINEBREAK
5197 else
5198 {
5199 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005200 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005201 int i;
5202 int saved_nextra = n_extra;
5203
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005204#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005205 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005206 /* there are characters to conceal */
5207 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005208 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5209 */
5210 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5211 && n_extra > tab_len)
5212 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005213#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005214
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005215 /* if n_extra > 0, it gives the number of chars, to
5216 * use for a tab, else we need to calculate the width
5217 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005218 len = (tab_len * mb_char2len(lcs_tab2));
5219 if (n_extra > 0)
5220 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005221 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005222 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005223 vim_memset(p, ' ', len);
5224 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005225 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005226 p_extra_free = p;
5227 for (i = 0; i < tab_len; i++)
5228 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005229 if (*p == NUL)
5230 {
5231 tab_len = i;
5232 break;
5233 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005234 mb_char2bytes(lcs_tab2, p);
5235 p += mb_char2len(lcs_tab2);
5236 n_extra += mb_char2len(lcs_tab2)
5237 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005238 }
5239 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005240#ifdef FEAT_CONCEAL
5241 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5242 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005243 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005244 n_extra -= vcol_off;
5245#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005246 }
5247#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005248#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005249 {
5250 int vc_saved = vcol_off;
5251
5252 /* Tab alignment should be identical regardless of
5253 * 'conceallevel' value. So tab compensates of all
5254 * previous concealed characters, and thus resets
5255 * vcol_off and boguscols accumulated so far in the
5256 * line. Note that the tab can be longer than
5257 * 'tabstop' when there are concealed characters. */
5258 FIX_FOR_BOGUSCOLS;
5259
5260 /* Make sure, the highlighting for the tab char will be
5261 * correctly set further below (effectively reverts the
5262 * FIX_FOR_BOGSUCOLS macro */
5263 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005264 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005265 tab_len += vc_saved;
5266 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 if (wp->w_p_list)
5270 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005271 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005272#ifdef FEAT_LINEBREAK
5273 if (wp->w_p_lbr)
5274 c_extra = NUL; /* using p_extra from above */
5275 else
5276#endif
5277 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005278 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005279 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005280 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005283 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005286 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005287 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290 else
5291 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005292 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 c_extra = ' ';
5294 c = ' ';
5295 }
5296 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005297 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005298 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005299 || ((fromcol >= 0 || fromcol_prev >= 0)
5300 && tocol > vcol
5301 && VIsual_mode != Ctrl_V
5302 && (
5303# ifdef FEAT_RIGHTLEFT
5304 wp->w_p_rl ? (col >= 0) :
5305# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005306 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005307 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005308 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005309 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005310 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005312 /* Display a '$' after the line or highlight an extra
5313 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5315 /* For a diff line the highlighting continues after the
5316 * "$". */
5317 if (
5318# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005319 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320# ifdef LINE_ATTR
5321 &&
5322# endif
5323# endif
5324# ifdef LINE_ATTR
5325 line_attr == 0
5326# endif
5327 )
5328#endif
5329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /* In virtualedit, visual selections may extend
5331 * beyond end of line. */
5332 if (area_highlighting && virtual_active()
5333 && tocol != MAXCOL && vcol < tocol)
5334 n_extra = 0;
5335 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
5337 p_extra = at_end_str;
5338 n_extra = 1;
5339 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005340 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005343 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005344 c = lcs_eol;
5345 else
5346 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 lcs_eol_one = -1;
5348 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005349 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005351 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 n_attr = 1;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005355 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005358 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005359 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
5361 else
5362 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364 else if (c != NUL)
5365 {
5366 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005367 if (n_extra == 0)
5368 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369#ifdef FEAT_RIGHTLEFT
5370 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5371 rl_mirror(p_extra); /* reverse "<12>" */
5372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005374 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005375#ifdef FEAT_LINEBREAK
5376 if (wp->w_p_lbr)
5377 {
5378 char_u *p;
5379
5380 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005381 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005382 vim_memset(p, ' ', n_extra);
5383 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5384 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005385 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005386 p_extra_free = p_extra = p;
5387 }
5388 else
5389#endif
5390 {
5391 n_extra = byte2cells(c) - 1;
5392 c = *p_extra++;
5393 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005394 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005397 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 saved_attr2 = char_attr; /* save current attr */
5399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 else if (VIsual_active
5403 && (VIsual_mode == Ctrl_V
5404 || VIsual_mode == 'v')
5405 && virtual_active()
5406 && tocol != MAXCOL
5407 && vcol < tocol
5408 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005409#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005411#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005412 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 c = ' ';
5415 --ptr; /* put it back at the NUL */
5416 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005417#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 else if ((
5419# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005420 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005422# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005423 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 ) && (
5427# ifdef FEAT_RIGHTLEFT
5428 wp->w_p_rl ? (col >= 0) :
5429# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005430 (col
5431# ifdef FEAT_CONCEAL
5432 - boguscols
5433# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005434 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 {
5436 /* Highlight until the right side of the window */
5437 c = ' ';
5438 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005439
5440 /* Remember we do the char for line highlighting. */
5441 ++did_line_attr;
5442
5443 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005444 if (line_attr != 0 && char_attr == search_attr
5445 && (did_line_attr > 1
5446 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005447 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448# ifdef FEAT_DIFF
5449 if (diff_hlf == HLF_TXD)
5450 {
5451 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005452 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005453 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005454 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005455 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5456 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005457 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005461# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005462 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005463 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005464 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005465 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5466 char_attr = hl_combine_attr(char_attr,
5467 HL_ATTR(HLF_CUL));
5468 }
5469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471#endif
5472 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005473
5474#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005475 if ( wp->w_p_cole > 0
5476 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005477 conceal_cursor_line(wp))
5478 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005479 && !(lnum_in_visual_area
5480 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005481 {
5482 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005483 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005484 && (syn_get_sub_char() != NUL || match_conc
5485 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005486 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005487 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005488 /* First time at this concealed item: display one
5489 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005490 if (match_conc)
5491 c = match_conc;
5492 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005493 c = syn_get_sub_char();
5494 else if (lcs_conceal != NUL)
5495 c = lcs_conceal;
5496 else
5497 c = ' ';
5498
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005499 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005500
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005501 if (n_extra > 0)
5502 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005503 vcol += n_extra;
5504 if (wp->w_p_wrap && n_extra > 0)
5505 {
5506# ifdef FEAT_RIGHTLEFT
5507 if (wp->w_p_rl)
5508 {
5509 col -= n_extra;
5510 boguscols -= n_extra;
5511 }
5512 else
5513# endif
5514 {
5515 boguscols += n_extra;
5516 col += n_extra;
5517 }
5518 }
5519 n_extra = 0;
5520 n_attr = 0;
5521 }
5522 else if (n_skip == 0)
5523 {
5524 is_concealing = TRUE;
5525 n_skip = 1;
5526 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005527 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005528 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005529 {
5530 mb_utf8 = TRUE;
5531 u8cc[0] = 0;
5532 c = 0xc0;
5533 }
5534 else
5535 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005536 }
5537 else
5538 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005539 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005540 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005541 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005542
5543 if (n_skip > 0 && did_decrement_ptr)
5544 // not showing the '>', put pointer back to avoid getting stuck
5545 ++ptr;
5546
5547#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
5549
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005550#ifdef FEAT_CONCEAL
5551 /* In the cursor line and we may be concealing characters: correct
5552 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005553 if (!did_wcol && draw_state == WL_LINE
5554 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005555 && conceal_cursor_line(wp)
5556 && (int)wp->w_virtcol <= vcol + n_skip)
5557 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005558# ifdef FEAT_RIGHTLEFT
5559 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005560 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005561 else
5562# endif
5563 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005564 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005565 did_wcol = TRUE;
5566 }
5567#endif
5568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 /* Don't override visual selection highlighting. */
5570 if (n_attr > 0
5571 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005572 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 char_attr = extra_attr;
5574
Bram Moolenaar81695252004-12-29 20:58:21 +00005575#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 /* XIM don't send preedit_start and preedit_end, but they send
5577 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5578 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005579 if (p_imst == IM_ON_THE_SPOT
5580 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005581 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 && (State & INSERT)
5583 && !p_imdisable
5584 && im_is_preediting()
5585 && draw_state == WL_LINE)
5586 {
5587 colnr_T tcol;
5588
5589 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005590 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 else
5592 tcol = preedit_end_col;
5593 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5594 {
5595 if (feedback_old_attr < 0)
5596 {
5597 feedback_col = 0;
5598 feedback_old_attr = char_attr;
5599 }
5600 char_attr = im_get_feedback_attr(feedback_col);
5601 if (char_attr < 0)
5602 char_attr = feedback_old_attr;
5603 feedback_col++;
5604 }
5605 else if (feedback_old_attr >= 0)
5606 {
5607 char_attr = feedback_old_attr;
5608 feedback_old_attr = -1;
5609 feedback_col = 0;
5610 }
5611 }
5612#endif
5613 /*
5614 * Handle the case where we are in column 0 but not on the first
5615 * character of the line and the user wants us to show us a
5616 * special character (via 'listchars' option "precedes:<char>".
5617 */
5618 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005619 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5621#ifdef FEAT_DIFF
5622 && filler_todo <= 0
5623#endif
5624 && draw_state > WL_NR
5625 && c != NUL)
5626 {
5627 c = lcs_prec;
5628 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005629 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5630 {
5631 /* Double-width character being overwritten by the "precedes"
5632 * character, need to fill up half the character. */
5633 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005634 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005635 n_extra = 1;
5636 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005637 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005640 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
5642 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005643 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005644 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646 else
5647 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005648 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
5650 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005651 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 n_attr3 = 1;
5653 }
5654 }
5655
5656 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005657 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005659 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005660#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005661 || did_line_attr == 1
5662#endif
5663 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005665#ifdef FEAT_SEARCH_EXTRA
5666 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005667
5668 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005669 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005670 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005671#endif
5672
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005673 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 * highlight match at end of line. If it's beyond the last
5675 * char on the screen, just overwrite that one (tricky!) Not
5676 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005677#ifdef FEAT_SEARCH_EXTRA
5678 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005679 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005680 prevcol_hl_flag = TRUE;
5681 else
5682 {
5683 cur = wp->w_match_head;
5684 while (cur != NULL)
5685 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005686 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005687 {
5688 prevcol_hl_flag = TRUE;
5689 break;
5690 }
5691 cur = cur->next;
5692 }
5693 }
5694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005696 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005697 && (VIsual_mode != Ctrl_V
5698 || lnum == VIsual.lnum
5699 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005700 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#ifdef FEAT_SEARCH_EXTRA
5702 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005703 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005704# ifdef FEAT_SYN_HL
5705 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5706 && !(wp == curwin && VIsual_active))
5707# endif
5708# ifdef FEAT_DIFF
5709 && diff_hlf == (hlf_T)0
5710# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005711# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005712 && did_line_attr <= 1
5713# endif
5714 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715#endif
5716 ))
5717 {
5718 int n = 0;
5719
5720#ifdef FEAT_RIGHTLEFT
5721 if (wp->w_p_rl)
5722 {
5723 if (col < 0)
5724 n = 1;
5725 }
5726 else
5727#endif
5728 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005729 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 n = -1;
5731 }
5732 if (n != 0)
5733 {
5734 /* At the window boundary, highlight the last character
5735 * instead (better than nothing). */
5736 off += n;
5737 col += n;
5738 }
5739 else
5740 {
5741 /* Add a blank character to highlight. */
5742 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 if (enc_utf8)
5744 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
5746#ifdef FEAT_SEARCH_EXTRA
5747 if (area_attr == 0)
5748 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005749 /* Use attributes from match with highest priority among
5750 * 'search_hl' and the match list. */
5751 char_attr = search_hl.attr;
5752 cur = wp->w_match_head;
5753 shl_flag = FALSE;
5754 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005755 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005756 if (shl_flag == FALSE
5757 && ((cur != NULL
5758 && cur->priority > SEARCH_HL_PRIORITY)
5759 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005760 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005761 shl = &search_hl;
5762 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005763 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005764 else
5765 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005766 if ((ptr - line) - 1 == (long)shl->startcol
5767 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005768 char_attr = shl->attr;
5769 if (shl != &search_hl && cur != NULL)
5770 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 }
5773#endif
5774 ScreenAttrs[off] = char_attr;
5775#ifdef FEAT_RIGHTLEFT
5776 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005779 --off;
5780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 else
5782#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005785 ++off;
5786 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005787 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005788#ifdef FEAT_SYN_HL
5789 eol_hl_off = 1;
5790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaar91170f82006-05-05 21:15:17 +00005794 /*
5795 * At end of the text line.
5796 */
5797 if (c == NUL)
5798 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005799#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005800 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005801 if (wp->w_p_wrap)
5802 v = wp->w_skipcol;
5803 else
5804 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005805
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005806 /* check if line ends before left margin */
5807 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005808 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005809#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005810 // Get rid of the boguscols now, we want to draw until the right
5811 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005812 col -= boguscols;
5813 boguscols = 0;
5814#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005815
5816 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005817 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005818
5819 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005820 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5821 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005822 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005823 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005824 || draw_color_col
5825 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005826# ifdef FEAT_RIGHTLEFT
5827 && !wp->w_p_rl
5828# endif
5829 )
5830 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005831 int rightmost_vcol = 0;
5832 int i;
5833
5834 if (wp->w_p_cuc)
5835 rightmost_vcol = wp->w_virtcol;
5836 if (draw_color_col)
5837 /* determine rightmost colorcolumn to possibly draw */
5838 for (i = 0; color_cols[i] >= 0; ++i)
5839 if (rightmost_vcol < color_cols[i])
5840 rightmost_vcol = color_cols[i];
5841
Bram Moolenaar02631462017-09-22 15:20:32 +02005842 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005843 {
5844 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005845 if (enc_utf8)
5846 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005847 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005848 if (draw_color_col)
5849 draw_color_col = advance_color_col(VCOL_HLC,
5850 &color_cols);
5851
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005852 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005853 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005854 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005855 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005856 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005857 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005858
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005859 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005860 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005861
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005862 ++vcol;
5863 }
5864 }
5865#endif
5866
Bram Moolenaar53f81742017-09-22 14:35:51 +02005867 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005868 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 row++;
5870
5871 /*
5872 * Update w_cline_height and w_cline_folded if the cursor line was
5873 * updated (saves a call to plines() later).
5874 */
5875 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5876 {
5877 curwin->w_cline_row = startrow;
5878 curwin->w_cline_height = row - startrow;
5879#ifdef FEAT_FOLDING
5880 curwin->w_cline_folded = FALSE;
5881#endif
5882 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5883 }
5884
5885 break;
5886 }
5887
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005888 // Show "extends" character from 'listchars' if beyond the line end and
5889 // 'list' is set.
5890 if (lcs_ext != NUL
5891 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 && !wp->w_p_wrap
5893#ifdef FEAT_DIFF
5894 && filler_todo <= 0
5895#endif
5896 && (
5897#ifdef FEAT_RIGHTLEFT
5898 wp->w_p_rl ? col == 0 :
5899#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005900 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005902 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5904 {
5905 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005906 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005908 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005911 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005912 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 else
5915 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005918#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005919 /* advance to the next 'colorcolumn' */
5920 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005921 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005922
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005923 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005924 * highlight the cursor position itself.
5925 * Also highlight the 'colorcolumn' if it is different than
5926 * 'cursorcolumn' */
5927 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005928 if (draw_state == WL_LINE && !lnum_in_visual_area
5929 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005930 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005931 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005932 && lnum != wp->w_cursor.lnum)
5933 {
5934 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005935 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005936 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005937 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005938 {
5939 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005940 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005941 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005942 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005943#endif
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 /*
5946 * Store character to be displayed.
5947 * Skip characters that are left of the screen for 'nowrap'.
5948 */
5949 vcol_prev = vcol;
5950 if (draw_state < WL_LINE || n_skip <= 0)
5951 {
5952 /*
5953 * Store the character.
5954 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005955#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5957 {
5958 /* A double-wide character is: put first halve in left cell. */
5959 --off;
5960 --col;
5961 }
5962#endif
5963 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005965 {
5966 if ((mb_c & 0xff00) == 0x8e00)
5967 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 else if (enc_utf8)
5971 {
5972 if (mb_utf8)
5973 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005974 int i;
5975
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005977 if ((c & 0xff) == 0)
5978 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005979 for (i = 0; i < Screen_mco; ++i)
5980 {
5981 ScreenLinesC[i][off] = u8cc[i];
5982 if (u8cc[i] == 0)
5983 break;
5984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986 else
5987 ScreenLinesUC[off] = 0;
5988 }
5989 if (multi_attr)
5990 {
5991 ScreenAttrs[off] = multi_attr;
5992 multi_attr = 0;
5993 }
5994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 ScreenAttrs[off] = char_attr;
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5998 {
5999 /* Need to fill two screen columns. */
6000 ++off;
6001 ++col;
6002 if (enc_utf8)
6003 /* UTF-8: Put a 0 in the second screen char. */
6004 ScreenLines[off] = 0;
6005 else
6006 /* DBCS: Put second byte in the second screen char. */
6007 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01006008 if (draw_state > WL_NR
6009#ifdef FEAT_DIFF
6010 && filler_todo <= 0
6011#endif
6012 )
6013 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 /* When "tocol" is halfway a character, set it to the end of
6015 * the character, otherwise highlighting won't stop. */
6016 if (tocol == vcol)
6017 ++tocol;
6018#ifdef FEAT_RIGHTLEFT
6019 if (wp->w_p_rl)
6020 {
6021 /* now it's time to backup one cell */
6022 --off;
6023 --col;
6024 }
6025#endif
6026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027#ifdef FEAT_RIGHTLEFT
6028 if (wp->w_p_rl)
6029 {
6030 --off;
6031 --col;
6032 }
6033 else
6034#endif
6035 {
6036 ++off;
6037 ++col;
6038 }
6039 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006040#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006041 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006042 {
6043 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006044 ++vcol_off;
6045 if (n_extra > 0)
6046 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006047 if (wp->w_p_wrap)
6048 {
6049 /*
6050 * Special voodoo required if 'wrap' is on.
6051 *
6052 * Advance the column indicator to force the line
6053 * drawing to wrap early. This will make the line
6054 * take up the same screen space when parts are concealed,
6055 * so that cursor line computations aren't messed up.
6056 *
6057 * To avoid the fictitious advance of 'col' causing
6058 * trailing junk to be written out of the screen line
6059 * we are building, 'boguscols' keeps track of the number
6060 * of bad columns we have advanced.
6061 */
6062 if (n_extra > 0)
6063 {
6064 vcol += n_extra;
6065# ifdef FEAT_RIGHTLEFT
6066 if (wp->w_p_rl)
6067 {
6068 col -= n_extra;
6069 boguscols -= n_extra;
6070 }
6071 else
6072# endif
6073 {
6074 col += n_extra;
6075 boguscols += n_extra;
6076 }
6077 n_extra = 0;
6078 n_attr = 0;
6079 }
6080
6081
Bram Moolenaar860cae12010-06-05 23:22:07 +02006082 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6083 {
6084 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006085# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006086 if (wp->w_p_rl)
6087 {
6088 --boguscols;
6089 --col;
6090 }
6091 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006092# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006093 {
6094 ++boguscols;
6095 ++col;
6096 }
6097 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006098
6099# ifdef FEAT_RIGHTLEFT
6100 if (wp->w_p_rl)
6101 {
6102 --boguscols;
6103 --col;
6104 }
6105 else
6106# endif
6107 {
6108 ++boguscols;
6109 ++col;
6110 }
6111 }
6112 else
6113 {
6114 if (n_extra > 0)
6115 {
6116 vcol += n_extra;
6117 n_extra = 0;
6118 n_attr = 0;
6119 }
6120 }
6121
6122 }
6123#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 else
6125 --n_skip;
6126
Bram Moolenaar64486672010-05-16 15:46:46 +02006127 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6128 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006129 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130#ifdef FEAT_DIFF
6131 && filler_todo <= 0
6132#endif
6133 )
6134 ++vcol;
6135
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006136#ifdef FEAT_SYN_HL
6137 if (vcol_save_attr >= 0)
6138 char_attr = vcol_save_attr;
6139#endif
6140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 /* restore attributes after "predeces" in 'listchars' */
6142 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6143 char_attr = saved_attr3;
6144
6145 /* restore attributes after last 'listchars' or 'number' char */
6146 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6147 char_attr = saved_attr2;
6148
6149 /*
6150 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006151 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 */
6153 if ((
6154#ifdef FEAT_RIGHTLEFT
6155 wp->w_p_rl ? (col < 0) :
6156#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006157 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 && (*ptr != NUL
6159#ifdef FEAT_DIFF
6160 || filler_todo > 0
6161#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006162 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6164 )
6165 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006166#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006167 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006168 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006169 boguscols = 0;
6170#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006171 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006172 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 ++row;
6175 ++screen_row;
6176
6177 /* When not wrapping and finished diff lines, or when displayed
6178 * '$' and highlighting until last column, break here. */
6179 if ((!wp->w_p_wrap
6180#ifdef FEAT_DIFF
6181 && filler_todo <= 0
6182#endif
6183 ) || lcs_eol_one == -1)
6184 break;
6185
6186 /* When the window is too narrow draw all "@" lines. */
6187 if (draw_state != WL_LINE
6188#ifdef FEAT_DIFF
6189 && filler_todo <= 0
6190#endif
6191 )
6192 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006193 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 row = endrow;
6196 }
6197
6198 /* When line got too long for screen break here. */
6199 if (row == endrow)
6200 {
6201 ++row;
6202 break;
6203 }
6204
6205 if (screen_cur_row == screen_row - 1
6206#ifdef FEAT_DIFF
6207 && filler_todo <= 0
6208#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006209 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 {
6211 /* Remember that the line wraps, used for modeless copy. */
6212 LineWraps[screen_row - 1] = TRUE;
6213
6214 /*
6215 * Special trick to make copy/paste of wrapped lines work with
6216 * xterm/screen: write an extra character beyond the end of
6217 * the line. This will work with all terminal types
6218 * (regardless of the xn,am settings).
6219 * Only do this on a fast tty.
6220 * Only do this if the cursor is on the current line
6221 * (something has been written in it).
6222 * Don't do this for the GUI.
6223 * Don't do this for double-width characters.
6224 * Don't do this for a window not at the right screen border.
6225 */
6226 if (p_tf
6227#ifdef FEAT_GUI
6228 && !gui.in_use
6229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006231 && ((*mb_off2cells)(LineOffset[screen_row],
6232 LineOffset[screen_row] + screen_Columns)
6233 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006235 + (int)Columns - 2,
6236 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006237 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
6239 /* First make sure we are at the end of the screen line,
6240 * then output the same character again to let the
6241 * terminal know about the wrap. If the terminal doesn't
6242 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006243 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 screen_char(LineOffset[screen_row - 1]
6245 + (unsigned)Columns - 1,
6246 screen_row - 1, (int)(Columns - 1));
6247
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 /* When there is a multi-byte character, just output a
6249 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006250 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6251 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 out_char(' ');
6253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 out_char(ScreenLines[LineOffset[screen_row - 1]
6255 + (Columns - 1)]);
6256 /* force a redraw of the first char on the next line */
6257 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6258 screen_start(); /* don't know where cursor is now */
6259 }
6260 }
6261
6262 col = 0;
6263 off = (unsigned)(current_ScreenLine - ScreenLines);
6264#ifdef FEAT_RIGHTLEFT
6265 if (wp->w_p_rl)
6266 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006267 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 off += col;
6269 }
6270#endif
6271
6272 /* reset the drawing state for the start of a wrapped line */
6273 draw_state = WL_START;
6274 saved_n_extra = n_extra;
6275 saved_p_extra = p_extra;
6276 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006277 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 saved_char_attr = char_attr;
6279 n_extra = 0;
6280 lcs_prec_todo = lcs_prec;
6281#ifdef FEAT_LINEBREAK
6282# ifdef FEAT_DIFF
6283 if (filler_todo <= 0)
6284# endif
6285 need_showbreak = TRUE;
6286#endif
6287#ifdef FEAT_DIFF
6288 --filler_todo;
6289 /* When the filler lines are actually below the last line of the
6290 * file, don't draw the line itself, break here. */
6291 if (filler_todo == 0 && wp->w_botfill)
6292 break;
6293#endif
6294 }
6295
6296 } /* for every character in the line */
6297
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006298#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006299 /* After an empty line check first word for capital. */
6300 if (*skipwhite(line) == NUL)
6301 {
6302 capcol_lnum = lnum + 1;
6303 cap_col = 0;
6304 }
6305#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006306#ifdef FEAT_TEXT_PROP
6307 vim_free(text_props);
6308 vim_free(text_prop_idxs);
6309#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006310
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006311 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 return row;
6313}
6314
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006315/*
6316 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006317 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006318 */
6319 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006320comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006321{
6322 int i;
6323
6324 for (i = 0; i < Screen_mco; ++i)
6325 {
6326 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6327 return TRUE;
6328 if (ScreenLinesC[i][off_from] == 0)
6329 break;
6330 }
6331 return FALSE;
6332}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334/*
6335 * Check whether the given character needs redrawing:
6336 * - the (first byte of the) character is different
6337 * - the attributes are different
6338 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006339 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 */
6341 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006342char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 if (cols > 0
6345 && ((ScreenLines[off_from] != ScreenLines[off_to]
6346 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 || (enc_dbcs != 0
6348 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6349 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6350 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6351 : (cols > 1 && ScreenLines[off_from + 1]
6352 != ScreenLines[off_to + 1])))
6353 || (enc_utf8
6354 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6355 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006356 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006357 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6358 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006359 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 return TRUE;
6361 return FALSE;
6362}
6363
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006364#if defined(FEAT_TERMINAL) || defined(PROTO)
6365/*
6366 * Return the index in ScreenLines[] for the current screen line.
6367 */
6368 int
6369screen_get_current_line_off()
6370{
6371 return (int)(current_ScreenLine - ScreenLines);
6372}
6373#endif
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375/*
6376 * Move one "cooked" screen line to the screen, but only the characters that
6377 * have actually changed. Handle insert/delete character.
6378 * "coloff" gives the first column on the screen for this line.
6379 * "endcol" gives the columns where valid characters are.
6380 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6381 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006382 * "flags" can have bits:
6383 * SLF_POPUP popup window
6384 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6386 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6387 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006388 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006389screen_line(
6390 int row,
6391 int coloff,
6392 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006393 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006394 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 unsigned off_from;
6397 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006398 unsigned max_off_from;
6399 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 int force = FALSE; /* force update rest of the line */
6403 int redraw_this /* bool: does character need redraw? */
6404#ifdef FEAT_GUI
6405 = TRUE /* For GUI when while-loop empty */
6406#endif
6407 ;
6408 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 int clear_next = FALSE;
6410 int char_cells; /* 1: normal char */
6411 /* 2: occupies two display cells */
6412# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006414 /* Check for illegal row and col, just in case. */
6415 if (row >= Rows)
6416 row = Rows - 1;
6417 if (endcol > Columns)
6418 endcol = Columns;
6419
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420# ifdef FEAT_CLIPBOARD
6421 clip_may_clear_selection(row, row);
6422# endif
6423
6424 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6425 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006426 max_off_from = off_from + screen_Columns;
6427 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006430 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
6432 /* Clear rest first, because it's left of the text. */
6433 if (clear_width > 0)
6434 {
6435 while (col <= endcol && ScreenLines[off_to] == ' '
6436 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006437 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 {
6439 ++off_to;
6440 ++col;
6441 }
6442 if (col <= endcol)
6443 screen_fill(row, row + 1, col + coloff,
6444 endcol + coloff + 1, ' ', ' ', 0);
6445 }
6446 col = endcol + 1;
6447 off_to = LineOffset[row] + col + coloff;
6448 off_from += col;
6449 endcol = (clear_width > 0 ? clear_width : -clear_width);
6450 }
6451#endif /* FEAT_RIGHTLEFT */
6452
6453 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6454
6455 while (col < endcol)
6456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006458 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 else
6460 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 redraw_this = redraw_next;
6463 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6464 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6465
6466#ifdef FEAT_GUI
6467 /* If the next character was bold, then redraw the current character to
6468 * remove any pixels that might have spilt over into us. This only
6469 * happens in the GUI.
6470 */
6471 if (redraw_next && gui.in_use)
6472 {
6473 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006474 if (hl > HL_ALL)
6475 hl = syn_attr2attr(hl);
6476 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 redraw_this = TRUE;
6478 }
6479#endif
6480
6481 if (redraw_this)
6482 {
6483 /*
6484 * Special handling when 'xs' termcap flag set (hpterm):
6485 * Attributes for characters are stored at the position where the
6486 * cursor is when writing the highlighting code. The
6487 * start-highlighting code must be written with the cursor on the
6488 * first highlighted character. The stop-highlighting code must
6489 * be written with the cursor just after the last highlighted
6490 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006491 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 * to clear the rest of the line, and force redrawing it
6493 * completely.
6494 */
6495 if ( p_wiv
6496 && !force
6497#ifdef FEAT_GUI
6498 && !gui.in_use
6499#endif
6500 && ScreenAttrs[off_to] != 0
6501 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6502 {
6503 /*
6504 * Need to remove highlighting attributes here.
6505 */
6506 windgoto(row, col + coloff);
6507 out_str(T_CE); /* clear rest of this screen line */
6508 screen_start(); /* don't know where cursor is now */
6509 force = TRUE; /* force redraw of rest of the line */
6510 redraw_next = TRUE; /* or else next char would miss out */
6511
6512 /*
6513 * If the previous character was highlighted, need to stop
6514 * highlighting at this character.
6515 */
6516 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6517 {
6518 screen_attr = ScreenAttrs[off_to - 1];
6519 term_windgoto(row, col + coloff);
6520 screen_stop_highlight();
6521 }
6522 else
6523 screen_attr = 0; /* highlighting has stopped */
6524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (enc_dbcs != 0)
6526 {
6527 /* Check if overwriting a double-byte with a single-byte or
6528 * the other way around requires another character to be
6529 * redrawn. For UTF-8 this isn't needed, because comparing
6530 * ScreenLinesUC[] is sufficient. */
6531 if (char_cells == 1
6532 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006533 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
6535 /* Writing a single-cell character over a double-cell
6536 * character: need to redraw the next cell. */
6537 ScreenLines[off_to + 1] = 0;
6538 redraw_next = TRUE;
6539 }
6540 else if (char_cells == 2
6541 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006542 && (*mb_off2cells)(off_to, max_off_to) == 1
6543 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
6545 /* Writing the second half of a double-cell character over
6546 * a double-cell character: need to redraw the second
6547 * cell. */
6548 ScreenLines[off_to + 2] = 0;
6549 redraw_next = TRUE;
6550 }
6551
6552 if (enc_dbcs == DBCS_JPNU)
6553 ScreenLines2[off_to] = ScreenLines2[off_from];
6554 }
6555 /* When writing a single-width character over a double-width
6556 * character and at the end of the redrawn text, need to clear out
6557 * the right halve of the old character.
6558 * Also required when writing the right halve of a double-width
6559 * char over the left halve of an existing one. */
6560 if (has_mbyte && col + char_cells == endcol
6561 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006562 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006564 && (*mb_off2cells)(off_to, max_off_to) == 1
6565 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 if (enc_utf8)
6570 {
6571 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6572 if (ScreenLinesUC[off_from] != 0)
6573 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006574 int i;
6575
6576 for (i = 0; i < Screen_mco; ++i)
6577 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 }
6579 }
6580 if (char_cells == 2)
6581 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
6583#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006584 /* The bold trick makes a single column of pixels appear in the
6585 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 * character should be redrawn too. This happens for our own GUI
6587 * and for some xterms. */
6588 if (
6589# ifdef FEAT_GUI
6590 gui.in_use
6591# endif
6592# if defined(FEAT_GUI) && defined(UNIX)
6593 ||
6594# endif
6595# ifdef UNIX
6596 term_is_xterm
6597# endif
6598 )
6599 {
6600 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006601 if (hl > HL_ALL)
6602 hl = syn_attr2attr(hl);
6603 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 redraw_next = TRUE;
6605 }
6606#endif
6607 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006608
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006609 /* For simplicity set the attributes of second half of a
6610 * double-wide character equal to the first half. */
6611 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006613
6614 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 screen_char(off_to, row, col + coloff);
6618 }
6619 else if ( p_wiv
6620#ifdef FEAT_GUI
6621 && !gui.in_use
6622#endif
6623 && col + coloff > 0)
6624 {
6625 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6626 {
6627 /*
6628 * Don't output stop-highlight when moving the cursor, it will
6629 * stop the highlighting when it should continue.
6630 */
6631 screen_attr = 0;
6632 }
6633 else if (screen_attr != 0)
6634 screen_stop_highlight();
6635 }
6636
6637 off_to += CHAR_CELLS;
6638 off_from += CHAR_CELLS;
6639 col += CHAR_CELLS;
6640 }
6641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 if (clear_next)
6643 {
6644 /* Clear the second half of a double-wide character of which the left
6645 * half was overwritten with a single-wide character. */
6646 ScreenLines[off_to] = ' ';
6647 if (enc_utf8)
6648 ScreenLinesUC[off_to] = 0;
6649 screen_char(off_to, row, col + coloff);
6650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 if (clear_width > 0
6653#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006654 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655#endif
6656 )
6657 {
6658#ifdef FEAT_GUI
6659 int startCol = col;
6660#endif
6661
6662 /* blank out the rest of the line */
6663 while (col < clear_width && ScreenLines[off_to] == ' '
6664 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006665 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
6667 ++off_to;
6668 ++col;
6669 }
6670 if (col < clear_width)
6671 {
6672#ifdef FEAT_GUI
6673 /*
6674 * In the GUI, clearing the rest of the line may leave pixels
6675 * behind if the first character cleared was bold. Some bold
6676 * fonts spill over the left. In this case we redraw the previous
6677 * character too. If we didn't skip any blanks above, then we
6678 * only redraw if the character wasn't already redrawn anyway.
6679 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006680 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
6682 hl = ScreenAttrs[off_to];
6683 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006684 {
6685 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006686
Bram Moolenaar9c697322006-10-09 20:11:17 +00006687 if (enc_utf8)
6688 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6689 * that its width is 2. */
6690 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6691 else if (enc_dbcs != 0)
6692 {
6693 /* find previous character by counting from first
6694 * column and get its width. */
6695 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006696 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006697
6698 while (off < off_to)
6699 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006700 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006701 off += prev_cells;
6702 }
6703 }
6704
6705 if (enc_dbcs != 0 && prev_cells > 1)
6706 screen_char_2(off_to - prev_cells, row,
6707 col + coloff - prev_cells);
6708 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006709 screen_char(off_to - prev_cells, row,
6710 col + coloff - prev_cells);
6711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 }
6713#endif
6714 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6715 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 off_to += clear_width - col;
6717 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 }
6719 }
6720
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006721 if (clear_width > 0
6722#ifdef FEAT_TEXT_PROP
6723 && !(flags & SLF_POPUP) // no separator for popup window
6724#endif
6725 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006727 // For a window that has a right neighbor, draw the separator char
6728 // right of the window contents.
6729 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
6731 int c;
6732
6733 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006734 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006735 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6736 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 || ScreenAttrs[off_to] != hl)
6738 {
6739 ScreenLines[off_to] = c;
6740 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 if (enc_utf8)
6742 {
6743 if (c >= 0x80)
6744 {
6745 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006746 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748 else
6749 ScreenLinesUC[off_to] = 0;
6750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 screen_char(off_to, row, col + coloff);
6752 }
6753 }
6754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 LineWraps[row] = FALSE;
6756 }
6757}
6758
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006759#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006761 * Mirror text "str" for right-left displaying.
6762 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006764 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006765rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 char_u *p1, *p2;
6768 int t;
6769
6770 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6771 {
6772 t = *p1;
6773 *p1 = *p2;
6774 *p2 = t;
6775 }
6776}
6777#endif
6778
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779/*
6780 * mark all status lines for redraw; used after first :cd
6781 */
6782 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006783status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 win_T *wp;
6786
Bram Moolenaar29323592016-07-24 22:04:11 +02006787 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (wp->w_status_height)
6789 {
6790 wp->w_redr_status = TRUE;
6791 redraw_later(VALID);
6792 }
6793}
6794
6795/*
6796 * mark all status lines of the current buffer for redraw
6797 */
6798 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006799status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800{
6801 win_T *wp;
6802
Bram Moolenaar29323592016-07-24 22:04:11 +02006803 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6805 {
6806 wp->w_redr_status = TRUE;
6807 redraw_later(VALID);
6808 }
6809}
6810
6811/*
6812 * Redraw all status lines that need to be redrawn.
6813 */
6814 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006815redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 win_T *wp;
6818
Bram Moolenaar29323592016-07-24 22:04:11 +02006819 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006821 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006822 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006823 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +02006824
6825#ifdef FEAT_TEXT_PROP
6826 // Display popup windows on top of the status lines.
6827 update_popups();
6828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
Bram Moolenaar4033c552017-09-16 20:54:51 +02006831#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832/*
6833 * Redraw all status lines at the bottom of frame "frp".
6834 */
6835 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006836win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 if (frp->fr_layout == FR_LEAF)
6839 frp->fr_win->w_redr_status = TRUE;
6840 else if (frp->fr_layout == FR_ROW)
6841 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006842 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 win_redraw_last_status(frp);
6844 }
6845 else /* frp->fr_layout == FR_COL */
6846 {
6847 frp = frp->fr_child;
6848 while (frp->fr_next != NULL)
6849 frp = frp->fr_next;
6850 win_redraw_last_status(frp);
6851 }
6852}
6853#endif
6854
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855/*
6856 * Draw the verticap separator right of window "wp" starting with line "row".
6857 */
6858 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006859draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
6861 int hl;
6862 int c;
6863
6864 if (wp->w_vsep_width)
6865 {
6866 /* draw the vertical separator right of this window */
6867 c = fillchar_vsep(&hl);
6868 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6869 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6870 c, ' ', hl);
6871 }
6872}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006875static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006878 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 */
6880 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006881status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 int len = 0;
6884
6885#ifdef FEAT_MENU
6886 int emenu = (xp->xp_context == EXPAND_MENUS
6887 || xp->xp_context == EXPAND_MENUNAMES);
6888
6889 /* Check for menu separators - replace with '|'. */
6890 if (emenu && menu_is_separator(s))
6891 return 1;
6892#endif
6893
6894 while (*s != NUL)
6895 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006896 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006897 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006898 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
6900
6901 return len;
6902}
6903
6904/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006905 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006906 * These are backslashes used for escaping. Do show backslashes in help tags.
6907 */
6908 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006909skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006910{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006911 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006912#ifdef FEAT_MENU
6913 || ((xp->xp_context == EXPAND_MENUS
6914 || xp->xp_context == EXPAND_MENUNAMES)
6915 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6916#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006917 )
6918 {
6919#ifndef BACKSLASH_IN_FILENAME
6920 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6921 return 2;
6922#endif
6923 return 1;
6924 }
6925 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006926}
6927
6928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 * Show wildchar matches in the status line.
6930 * Show at least the "match" item.
6931 * We start at item 'first_match' in the list and show all matches that fit.
6932 *
6933 * If inversion is possible we use it. Else '=' characters are used.
6934 */
6935 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006936win_redr_status_matches(
6937 expand_T *xp,
6938 int num_matches,
6939 char_u **matches, /* list of matches */
6940 int match,
6941 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6944 int row;
6945 char_u *buf;
6946 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006947 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 int fillchar;
6949 int attr;
6950 int i;
6951 int highlight = TRUE;
6952 char_u *selstart = NULL;
6953 int selstart_col = 0;
6954 char_u *selend = NULL;
6955 static int first_match = 0;
6956 int add_left = FALSE;
6957 char_u *s;
6958#ifdef FEAT_MENU
6959 int emenu;
6960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962
6963 if (matches == NULL) /* interrupted completion? */
6964 return;
6965
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006966 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006967 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006968 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006969 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (buf == NULL)
6971 return;
6972
6973 if (match == -1) /* don't show match but original text */
6974 {
6975 match = 0;
6976 highlight = FALSE;
6977 }
6978 /* count 1 for the ending ">" */
6979 clen = status_match_len(xp, L_MATCH(match)) + 3;
6980 if (match == 0)
6981 first_match = 0;
6982 else if (match < first_match)
6983 {
6984 /* jumping left, as far as we can go */
6985 first_match = match;
6986 add_left = TRUE;
6987 }
6988 else
6989 {
6990 /* check if match fits on the screen */
6991 for (i = first_match; i < match; ++i)
6992 clen += status_match_len(xp, L_MATCH(i)) + 2;
6993 if (first_match > 0)
6994 clen += 2;
6995 /* jumping right, put match at the left */
6996 if ((long)clen > Columns)
6997 {
6998 first_match = match;
6999 /* if showing the last match, we can add some on the left */
7000 clen = 2;
7001 for (i = match; i < num_matches; ++i)
7002 {
7003 clen += status_match_len(xp, L_MATCH(i)) + 2;
7004 if ((long)clen >= Columns)
7005 break;
7006 }
7007 if (i == num_matches)
7008 add_left = TRUE;
7009 }
7010 }
7011 if (add_left)
7012 while (first_match > 0)
7013 {
7014 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7015 if ((long)clen >= Columns)
7016 break;
7017 --first_match;
7018 }
7019
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007020 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 if (first_match == 0)
7023 {
7024 *buf = NUL;
7025 len = 0;
7026 }
7027 else
7028 {
7029 STRCPY(buf, "< ");
7030 len = 2;
7031 }
7032 clen = len;
7033
7034 i = first_match;
7035 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7036 {
7037 if (i == match)
7038 {
7039 selstart = buf + len;
7040 selstart_col = clen;
7041 }
7042
7043 s = L_MATCH(i);
7044 /* Check for menu separators - replace with '|' */
7045#ifdef FEAT_MENU
7046 emenu = (xp->xp_context == EXPAND_MENUS
7047 || xp->xp_context == EXPAND_MENUNAMES);
7048 if (emenu && menu_is_separator(s))
7049 {
7050 STRCPY(buf + len, transchar('|'));
7051 l = (int)STRLEN(buf + len);
7052 len += l;
7053 clen += l;
7054 }
7055 else
7056#endif
7057 for ( ; *s != NUL; ++s)
7058 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007059 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007061 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
7063 STRNCPY(buf + len, s, l);
7064 s += l - 1;
7065 len += l;
7066 }
7067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 {
7069 STRCPY(buf + len, transchar_byte(*s));
7070 len += (int)STRLEN(buf + len);
7071 }
7072 }
7073 if (i == match)
7074 selend = buf + len;
7075
7076 *(buf + len++) = ' ';
7077 *(buf + len++) = ' ';
7078 clen += 2;
7079 if (++i == num_matches)
7080 break;
7081 }
7082
7083 if (i != num_matches)
7084 {
7085 *(buf + len++) = '>';
7086 ++clen;
7087 }
7088
7089 buf[len] = NUL;
7090
7091 row = cmdline_row - 1;
7092 if (row >= 0)
7093 {
7094 if (wild_menu_showing == 0)
7095 {
7096 if (msg_scrolled > 0)
7097 {
7098 /* Put the wildmenu just above the command line. If there is
7099 * no room, scroll the screen one line up. */
7100 if (cmdline_row == Rows - 1)
7101 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007102 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 ++msg_scrolled;
7104 }
7105 else
7106 {
7107 ++cmdline_row;
7108 ++row;
7109 }
7110 wild_menu_showing = WM_SCROLLED;
7111 }
7112 else
7113 {
7114 /* Create status line if needed by setting 'laststatus' to 2.
7115 * Set 'winminheight' to zero to avoid that the window is
7116 * resized. */
7117 if (lastwin->w_status_height == 0)
7118 {
7119 save_p_ls = p_ls;
7120 save_p_wmh = p_wmh;
7121 p_ls = 2;
7122 p_wmh = 0;
7123 last_status(FALSE);
7124 }
7125 wild_menu_showing = WM_SHOWN;
7126 }
7127 }
7128
7129 screen_puts(buf, row, 0, attr);
7130 if (selstart != NULL && highlight)
7131 {
7132 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007133 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 }
7135
7136 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7137 }
7138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 vim_free(buf);
7141}
7142#endif
7143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144/*
7145 * Redraw the status line of window wp.
7146 *
7147 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007148 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7149 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007151 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007152win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 int row;
7155 char_u *p;
7156 int len;
7157 int fillchar;
7158 int attr;
7159 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007160 static int busy = FALSE;
7161
7162 /* It's possible to get here recursively when 'statusline' (indirectly)
7163 * invokes ":redrawstatus". Simply ignore the call then. */
7164 if (busy)
7165 return;
7166 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
7168 wp->w_redr_status = FALSE;
7169 if (wp->w_status_height == 0)
7170 {
7171 /* no status line, can only be last window */
7172 redraw_cmdline = TRUE;
7173 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007174 else if (!redrawing()
7175#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007176 // don't update status line when popup menu is visible and may be
7177 // drawn over it, unless it will be redrawn later
7178 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007179#endif
7180 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
7182 /* Don't redraw right now, do it later. */
7183 wp->w_redr_status = TRUE;
7184 }
7185#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007186 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007189 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191#endif
7192 else
7193 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007194 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007196 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 p = NameBuff;
7198 len = (int)STRLEN(p);
7199
Bram Moolenaard85f2712017-07-28 21:51:57 +02007200 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201#ifdef FEAT_QUICKFIX
7202 || wp->w_p_pvw
7203#endif
7204 || bufIsChanged(wp->w_buffer)
7205 || wp->w_buffer->b_p_ro)
7206 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007207 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007209 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 len += (int)STRLEN(p + len);
7211 }
7212#ifdef FEAT_QUICKFIX
7213 if (wp->w_p_pvw)
7214 {
7215 STRCPY(p + len, _("[Preview]"));
7216 len += (int)STRLEN(p + len);
7217 }
7218#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007219 if (bufIsChanged(wp->w_buffer)
7220#ifdef FEAT_TERMINAL
7221 && !bt_terminal(wp->w_buffer)
7222#endif
7223 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
7225 STRCPY(p + len, "[+]");
7226 len += 3;
7227 }
7228 if (wp->w_buffer->b_p_ro)
7229 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007230 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007231 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
7233
Bram Moolenaar02631462017-09-22 15:20:32 +02007234 this_ru_col = ru_col - (Columns - wp->w_width);
7235 if (this_ru_col < (wp->w_width + 1) / 2)
7236 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (this_ru_col <= 1)
7238 {
7239 p = (char_u *)"<"; /* No room for file name! */
7240 len = 1;
7241 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007242 else if (has_mbyte)
7243 {
7244 int clen = 0, i;
7245
7246 /* Count total number of display cells. */
7247 clen = mb_string2cells(p, -1);
7248
7249 /* Find first character that will fit.
7250 * Going from start to end is much faster for DBCS. */
7251 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7252 i += (*mb_ptr2len)(p + i))
7253 clen -= (*mb_ptr2cells)(p + i);
7254 len = clen;
7255 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007257 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007259 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261
Bram Moolenaara12a1612019-01-24 16:39:02 +01007262 }
7263 else if (len > this_ru_col - 1)
7264 {
7265 p += len - (this_ru_col - 1);
7266 *p = '<';
7267 len = this_ru_col - 1;
7268 }
7269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007271 screen_puts(p, row, wp->w_wincol, attr);
7272 screen_fill(row, row + 1, len + wp->w_wincol,
7273 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007275 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7277 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007278 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279
7280#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007281 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282#endif
7283 }
7284
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 /*
7286 * May need to draw the character below the vertical separator.
7287 */
7288 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7289 {
7290 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007291 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 else
7293 fillchar = fillchar_vsep(&attr);
7294 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7295 attr);
7296 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007297 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
7299
Bram Moolenaar238a5642006-02-21 22:12:05 +00007300#ifdef FEAT_STL_OPT
7301/*
7302 * Redraw the status line according to 'statusline' and take care of any
7303 * errors encountered.
7304 */
7305 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007306redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007307{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007308 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007309 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007310
7311 /* When called recursively return. This can happen when the statusline
7312 * contains an expression that triggers a redraw. */
7313 if (entered)
7314 return;
7315 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007316
Bram Moolenaara742e082016-04-05 21:10:38 +02007317 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007318 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007319 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007320 {
7321 /* When there is an error disable the statusline, otherwise the
7322 * display is messed up with errors and a redraw triggers the problem
7323 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007324 set_string_option_direct((char_u *)"statusline", -1,
7325 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007326 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007327 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007328 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007329 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007330}
7331#endif
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333/*
7334 * Return TRUE if the status line of window "wp" is connected to the status
7335 * line of the window right of it. If not, then it's a vertical separator.
7336 * Only call if (wp->w_vsep_width != 0).
7337 */
7338 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007339stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341 frame_T *fr;
7342
7343 fr = wp->w_frame;
7344 while (fr->fr_parent != NULL)
7345 {
7346 if (fr->fr_parent->fr_layout == FR_COL)
7347 {
7348 if (fr->fr_next != NULL)
7349 break;
7350 }
7351 else
7352 {
7353 if (fr->fr_next != NULL)
7354 return TRUE;
7355 }
7356 fr = fr->fr_parent;
7357 }
7358 return FALSE;
7359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362/*
7363 * Get the value to show for the language mappings, active 'keymap'.
7364 */
7365 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007366get_keymap_str(
7367 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007368 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007369 char_u *buf, /* buffer for the result */
7370 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
7372 char_u *p;
7373
7374 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7375 return FALSE;
7376
7377 {
7378#ifdef FEAT_EVAL
7379 buf_T *old_curbuf = curbuf;
7380 win_T *old_curwin = curwin;
7381 char_u *s;
7382
7383 curbuf = wp->w_buffer;
7384 curwin = wp;
7385 STRCPY(buf, "b:keymap_name"); /* must be writable */
7386 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007387 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 --emsg_skip;
7389 curbuf = old_curbuf;
7390 curwin = old_curwin;
7391 if (p == NULL || *p == NUL)
7392#endif
7393 {
7394#ifdef FEAT_KEYMAP
7395 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7396 p = wp->w_buffer->b_p_keymap;
7397 else
7398#endif
7399 p = (char_u *)"lang";
7400 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007401 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 buf[0] = NUL;
7403#ifdef FEAT_EVAL
7404 vim_free(s);
7405#endif
7406 }
7407 return buf[0] != NUL;
7408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410#if defined(FEAT_STL_OPT) || defined(PROTO)
7411/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007412 * Redraw the status line or ruler of window "wp".
7413 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 */
7415 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007416win_redr_custom(
7417 win_T *wp,
7418 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007420 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 int attr;
7422 int curattr;
7423 int row;
7424 int col = 0;
7425 int maxwidth;
7426 int width;
7427 int n;
7428 int len;
7429 int fillchar;
7430 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007431 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007433 struct stl_hlrec hltab[STL_MAX_ITEM];
7434 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007435 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007436 win_T *ewp;
7437 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438
Bram Moolenaar1d633412013-12-11 15:52:01 +01007439 /* There is a tiny chance that this gets called recursively: When
7440 * redrawing a status line triggers redrawing the ruler or tabline.
7441 * Avoid trouble by not allowing recursion. */
7442 if (entered)
7443 return;
7444 entered = TRUE;
7445
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007447 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007449 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007450 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007451 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007452 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007453 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007454 maxwidth = Columns;
7455# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007456 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007459 else
7460 {
7461 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007462 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007463 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007464
7465 if (draw_ruler)
7466 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007467 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007468 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007469 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007470 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007471 if (*++stl == '-')
7472 stl++;
7473 if (atoi((char *)stl))
7474 while (VIM_ISDIGIT(*stl))
7475 stl++;
7476 if (*stl++ != '(')
7477 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007478 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007479 col = ru_col - (Columns - wp->w_width);
7480 if (col < (wp->w_width + 1) / 2)
7481 col = (wp->w_width + 1) / 2;
7482 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007483 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007484 {
7485 row = Rows - 1;
7486 --maxwidth; /* writing in last column may cause scrolling */
7487 fillchar = ' ';
7488 attr = 0;
7489 }
7490
7491# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007492 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007493# endif
7494 }
7495 else
7496 {
7497 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007498 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007499 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007500 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007501# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007502 use_sandbox = was_set_insecurely((char_u *)"statusline",
7503 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007504# endif
7505 }
7506
Bram Moolenaar53f81742017-09-22 14:35:51 +02007507 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007508 }
7509
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007511 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
Bram Moolenaar61452852011-02-01 18:01:11 +01007513 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7514 * the cursor away and back. */
7515 ewp = wp == NULL ? curwin : wp;
7516 p_crb_save = ewp->w_p_crb;
7517 ewp->w_p_crb = FALSE;
7518
Bram Moolenaar362f3562009-11-03 16:20:34 +00007519 /* Make a copy, because the statusline may include a function call that
7520 * might change the option value and free the memory. */
7521 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007522 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007523 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007524 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007525 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007526 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007528 /* Make all characters printable. */
7529 p = transstr(buf);
7530 if (p != NULL)
7531 {
7532 vim_strncpy(buf, p, sizeof(buf) - 1);
7533 vim_free(p);
7534 }
7535
7536 /* fill up with "fillchar" */
7537 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007538 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 ++width;
7542 }
7543 buf[len] = NUL;
7544
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007545 /*
7546 * Draw each snippet with the specified highlighting.
7547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 curattr = attr;
7549 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007550 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007552 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 screen_puts_len(p, len, row, col, curattr);
7554 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007555 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007557 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007559 else if (hltab[n].userhl < 0)
7560 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007561#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007562 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7563 && wp->w_status_height != 0)
7564 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007565 else if (wp != NULL && bt_terminal(wp->w_buffer)
7566 && wp->w_status_height != 0)
7567 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007568#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007569 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007570 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007572 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 }
7574 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007575
7576 if (wp == NULL)
7577 {
7578 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7579 col = 0;
7580 len = 0;
7581 p = buf;
7582 fillchar = 0;
7583 for (n = 0; tabtab[n].start != NULL; n++)
7584 {
7585 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7586 while (col < len)
7587 TabPageIdxs[col++] = fillchar;
7588 p = tabtab[n].start;
7589 fillchar = tabtab[n].userhl;
7590 }
7591 while (col < Columns)
7592 TabPageIdxs[col++] = fillchar;
7593 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007594
7595theend:
7596 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597}
7598
7599#endif /* FEAT_STL_OPT */
7600
7601/*
7602 * Output a single character directly to the screen and update ScreenLines.
7603 */
7604 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007605screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 char_u buf[MB_MAXBYTES + 1];
7608
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007609 if (has_mbyte)
7610 buf[(*mb_char2bytes)(c, buf)] = NUL;
7611 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007612 {
7613 buf[0] = c;
7614 buf[1] = NUL;
7615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 screen_puts(buf, row, col, attr);
7617}
7618
7619/*
7620 * Get a single character directly from ScreenLines into "bytes[]".
7621 * Also return its attribute in *attrp;
7622 */
7623 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007624screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625{
7626 unsigned off;
7627
7628 /* safety check */
7629 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7630 {
7631 off = LineOffset[row] + col;
7632 *attrp = ScreenAttrs[off];
7633 bytes[0] = ScreenLines[off];
7634 bytes[1] = NUL;
7635
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 if (enc_utf8 && ScreenLinesUC[off] != 0)
7637 bytes[utfc_char2bytes(off, bytes)] = NUL;
7638 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7639 {
7640 bytes[0] = ScreenLines[off];
7641 bytes[1] = ScreenLines2[off];
7642 bytes[2] = NUL;
7643 }
7644 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7645 {
7646 bytes[1] = ScreenLines[off + 1];
7647 bytes[2] = NUL;
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 }
7650}
7651
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007652/*
7653 * Return TRUE if composing characters for screen posn "off" differs from
7654 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007655 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007656 */
7657 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007658screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007659{
7660 int i;
7661
7662 for (i = 0; i < Screen_mco; ++i)
7663 {
7664 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7665 return TRUE;
7666 if (u8cc[i] == 0)
7667 break;
7668 }
7669 return FALSE;
7670}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007671
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672/*
7673 * Put string '*text' on the screen at position 'row' and 'col', with
7674 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7675 * Note: only outputs within one row, message is truncated at screen boundary!
7676 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7677 */
7678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007679screen_puts(
7680 char_u *text,
7681 int row,
7682 int col,
7683 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 screen_puts_len(text, -1, row, col, attr);
7686}
7687
7688/*
7689 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7690 * a NUL.
7691 */
7692 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007693screen_puts_len(
7694 char_u *text,
7695 int textlen,
7696 int row,
7697 int col,
7698 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 unsigned off;
7701 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007702 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007704 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 int mbyte_blen = 1;
7706 int mbyte_cells = 1;
7707 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007708 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007710#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007712 int pc, nc, nc1;
7713 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007715 int force_redraw_this;
7716 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007717 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007719 // Safety check. The check for negative row and column is to fix issue
7720 // #4102. TODO: find out why row/col could be negative.
7721 if (ScreenLines == NULL
7722 || row >= screen_Rows || row < 0
7723 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007725 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726
Bram Moolenaarc236c162008-07-13 17:41:49 +00007727 /* When drawing over the right halve of a double-wide char clear out the
7728 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007729 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007730#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007731 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007732#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007733 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007734 {
7735 ScreenLines[off - 1] = ' ';
7736 ScreenAttrs[off - 1] = 0;
7737 if (enc_utf8)
7738 {
7739 ScreenLinesUC[off - 1] = 0;
7740 ScreenLinesC[0][off - 1] = 0;
7741 }
7742 /* redraw the previous cell, make it empty */
7743 screen_char(off - 1, row, col - 1);
7744 /* force the cell at "col" to be redrawn */
7745 force_redraw_next = TRUE;
7746 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007747
Bram Moolenaar367329b2007-08-30 11:53:22 +00007748 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007749 while (col < screen_Columns
7750 && (len < 0 || (int)(ptr - text) < len)
7751 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 {
7753 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 /* check if this is the first byte of a multibyte */
7755 if (has_mbyte)
7756 {
7757 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007758 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007760 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7762 mbyte_cells = 1;
7763 else if (enc_dbcs != 0)
7764 mbyte_cells = mbyte_blen;
7765 else /* enc_utf8 */
7766 {
7767 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007768 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 (int)((text + len) - ptr));
7770 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007771 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007773#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7775 {
7776 /* Do Arabic shaping. */
7777 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7778 {
7779 /* Past end of string to be displayed. */
7780 nc = NUL;
7781 nc1 = NUL;
7782 }
7783 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007784 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007785 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7786 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007787 nc1 = pcc[0];
7788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 pc = prev_c;
7790 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007791 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793 else
7794 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007795#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007796 if (col + mbyte_cells > screen_Columns)
7797 {
7798 /* Only 1 cell left, but character requires 2 cells:
7799 * display a '>' in the last column to avoid wrapping. */
7800 c = '>';
7801 mbyte_cells = 1;
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007806 force_redraw_this = force_redraw_next;
7807 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007808
7809 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 || (mbyte_cells == 2
7811 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7812 || (enc_dbcs == DBCS_JPNU
7813 && c == 0x8e
7814 && ScreenLines2[off] != ptr[1])
7815 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007816 && (ScreenLinesUC[off] !=
7817 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7818 || (ScreenLinesUC[off] != 0
7819 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007821 || exmode_active;
7822
Bram Moolenaara12a1612019-01-24 16:39:02 +01007823 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
7825#if defined(FEAT_GUI) || defined(UNIX)
7826 /* The bold trick makes a single row of pixels appear in the next
7827 * character. When a bold character is removed, the next
7828 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007829 * and for some xterms. */
7830 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831# ifdef FEAT_GUI
7832 gui.in_use
7833# endif
7834# if defined(FEAT_GUI) && defined(UNIX)
7835 ||
7836# endif
7837# ifdef UNIX
7838 term_is_xterm
7839# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007840 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007842 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007844 if (n > HL_ALL)
7845 n = syn_attr2attr(n);
7846 if (n & HL_BOLD)
7847 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 /* When at the end of the text and overwriting a two-cell
7851 * character with a one-cell character, need to clear the next
7852 * cell. Also when overwriting the left halve of a two-cell char
7853 * with the right halve of a two-cell char. Do this only once
7854 * (mb_off2cells() may return 2 on the right halve). */
7855 if (clear_next_cell)
7856 clear_next_cell = FALSE;
7857 else if (has_mbyte
7858 && (len < 0 ? ptr[mbyte_blen] == NUL
7859 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007860 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007862 && (*mb_off2cells)(off, max_off) == 1
7863 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 clear_next_cell = TRUE;
7865
7866 /* Make sure we never leave a second byte of a double-byte behind,
7867 * it confuses mb_off2cells(). */
7868 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007869 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007871 && (*mb_off2cells)(off, max_off) == 1
7872 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 ScreenLines[off] = c;
7875 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 if (enc_utf8)
7877 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007878 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 ScreenLinesUC[off] = 0;
7880 else
7881 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007882 int i;
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007885 for (i = 0; i < Screen_mco; ++i)
7886 {
7887 ScreenLinesC[i][off] = u8cc[i];
7888 if (u8cc[i] == 0)
7889 break;
7890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 if (mbyte_cells == 2)
7893 {
7894 ScreenLines[off + 1] = 0;
7895 ScreenAttrs[off + 1] = attr;
7896 }
7897 screen_char(off, row, col);
7898 }
7899 else if (mbyte_cells == 2)
7900 {
7901 ScreenLines[off + 1] = ptr[1];
7902 ScreenAttrs[off + 1] = attr;
7903 screen_char_2(off, row, col);
7904 }
7905 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7906 {
7907 ScreenLines2[off] = ptr[1];
7908 screen_char(off, row, col);
7909 }
7910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 screen_char(off, row, col);
7912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 if (has_mbyte)
7914 {
7915 off += mbyte_cells;
7916 col += mbyte_cells;
7917 ptr += mbyte_blen;
7918 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007919 {
7920 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007922 len = -1;
7923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {
7927 ++off;
7928 ++col;
7929 ++ptr;
7930 }
7931 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007932
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007933 /* If we detected the next character needs to be redrawn, but the text
7934 * doesn't extend up to there, update the character here. */
7935 if (force_redraw_next && col < screen_Columns)
7936 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007937 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7938 screen_char_2(off, row, col);
7939 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007940 screen_char(off, row, col);
7941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942}
7943
7944#ifdef FEAT_SEARCH_EXTRA
7945/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007946 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 */
7948 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007949start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950{
7951 if (p_hls && !no_hlsearch)
7952 {
7953 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007954 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007955# ifdef FEAT_RELTIME
7956 /* Set the time limit to 'redrawtime'. */
7957 profile_setlimit(p_rdt, &search_hl.tm);
7958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
7960}
7961
7962/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007963 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 */
7965 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007966end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967{
7968 if (search_hl.rm.regprog != NULL)
7969 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007970 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 search_hl.rm.regprog = NULL;
7972 }
7973}
7974
7975/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007976 * Init for calling prepare_search_hl().
7977 */
7978 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007979init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007980{
7981 matchitem_T *cur;
7982
7983 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7984 * match */
7985 cur = wp->w_match_head;
7986 while (cur != NULL)
7987 {
7988 cur->hl.rm = cur->match;
7989 if (cur->hlg_id == 0)
7990 cur->hl.attr = 0;
7991 else
7992 cur->hl.attr = syn_id2attr(cur->hlg_id);
7993 cur->hl.buf = wp->w_buffer;
7994 cur->hl.lnum = 0;
7995 cur->hl.first_lnum = 0;
7996# ifdef FEAT_RELTIME
7997 /* Set the time limit to 'redrawtime'. */
7998 profile_setlimit(p_rdt, &(cur->hl.tm));
7999# endif
8000 cur = cur->next;
8001 }
8002 search_hl.buf = wp->w_buffer;
8003 search_hl.lnum = 0;
8004 search_hl.first_lnum = 0;
8005 /* time limit is set at the toplevel, for all windows */
8006}
8007
8008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * Advance to the match in window "wp" line "lnum" or past it.
8010 */
8011 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008012prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008014 matchitem_T *cur; /* points to the match list */
8015 match_T *shl; /* points to search_hl or a match */
8016 int shl_flag; /* flag to indicate whether search_hl
8017 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018 int pos_inprogress; /* marks that position match search is
8019 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 int n;
8021
8022 /*
8023 * When using a multi-line pattern, start searching at the top
8024 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008025 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008027 cur = wp->w_match_head;
8028 shl_flag = FALSE;
8029 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008031 if (shl_flag == FALSE)
8032 {
8033 shl = &search_hl;
8034 shl_flag = TRUE;
8035 }
8036 else
8037 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 if (shl->rm.regprog != NULL
8039 && shl->lnum == 0
8040 && re_multiline(shl->rm.regprog))
8041 {
8042 if (shl->first_lnum == 0)
8043 {
8044# ifdef FEAT_FOLDING
8045 for (shl->first_lnum = lnum;
8046 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8047 if (hasFoldingWin(wp, shl->first_lnum - 1,
8048 NULL, NULL, TRUE, NULL))
8049 break;
8050# else
8051 shl->first_lnum = wp->w_topline;
8052# endif
8053 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008054 if (cur != NULL)
8055 cur->pos.cur = 0;
8056 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008058 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8059 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008061 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8062 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008063 pos_inprogress = cur == NULL || cur->pos.cur == 0
8064 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (shl->lnum != 0)
8066 {
8067 shl->first_lnum = shl->lnum
8068 + shl->rm.endpos[0].lnum
8069 - shl->rm.startpos[0].lnum;
8070 n = shl->rm.endpos[0].col;
8071 }
8072 else
8073 {
8074 ++shl->first_lnum;
8075 n = 0;
8076 }
8077 }
8078 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008079 if (shl != &search_hl && cur != NULL)
8080 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 }
8082}
8083
8084/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008085 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 * Uses shl->buf.
8087 * Sets shl->lnum and shl->rm contents.
8088 * Note: Assumes a previous match is always before "lnum", unless
8089 * shl->lnum is zero.
8090 * Careful: Any pointers for buffer lines will become invalid.
8091 */
8092 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008093next_search_hl(
8094 win_T *win,
8095 match_T *shl, /* points to search_hl or a match */
8096 linenr_T lnum,
8097 colnr_T mincol, /* minimal column for a match */
8098 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 linenr_T l;
8101 colnr_T matchcol;
8102 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008103 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008105 // for :{range}s/pat only highlight inside the range
8106 if (lnum < search_first_line || lnum > search_last_line)
8107 {
8108 shl->lnum = 0;
8109 return;
8110 }
8111
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (shl->lnum != 0)
8113 {
8114 /* Check for three situations:
8115 * 1. If the "lnum" is below a previous match, start a new search.
8116 * 2. If the previous match includes "mincol", use it.
8117 * 3. Continue after the previous match.
8118 */
8119 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8120 if (lnum > l)
8121 shl->lnum = 0;
8122 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8123 return;
8124 }
8125
8126 /*
8127 * Repeat searching for a match until one is found that includes "mincol"
8128 * or none is found in this line.
8129 */
8130 called_emsg = FALSE;
8131 for (;;)
8132 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008133#ifdef FEAT_RELTIME
8134 /* Stop searching after passing the time limit. */
8135 if (profile_passed_limit(&(shl->tm)))
8136 {
8137 shl->lnum = 0; /* no match found in time */
8138 break;
8139 }
8140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 /* Three situations:
8142 * 1. No useful previous match: search from start of line.
8143 * 2. Not Vi compatible or empty match: continue at next character.
8144 * Break the loop if this is beyond the end of the line.
8145 * 3. Vi compatible searching: continue at end of previous match.
8146 */
8147 if (shl->lnum == 0)
8148 matchcol = 0;
8149 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8150 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008151 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008153 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008154
8155 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008156 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008157 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008159 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 shl->lnum = 0;
8161 break;
8162 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008163 if (has_mbyte)
8164 matchcol += mb_ptr2len(ml);
8165 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008166 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 }
8168 else
8169 matchcol = shl->rm.endpos[0].col;
8170
8171 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008172 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008174 /* Remember whether shl->rm is using a copy of the regprog in
8175 * cur->match. */
8176 int regprog_is_copy = (shl != &search_hl && cur != NULL
8177 && shl == &cur->hl
8178 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008179 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008180
Bram Moolenaarb3414592014-06-17 17:48:32 +02008181 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8182 matchcol,
8183#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008184 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008185#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008186 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008187#endif
8188 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008189 /* Copy the regprog, in case it got freed and recompiled. */
8190 if (regprog_is_copy)
8191 cur->match.regprog = cur->hl.rm.regprog;
8192
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008193 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008194 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008195 /* Error while handling regexp: stop using this regexp. */
8196 if (shl == &search_hl)
8197 {
8198 /* don't free regprog in the match list, it's a copy */
8199 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008200 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008201 }
8202 shl->rm.regprog = NULL;
8203 shl->lnum = 0;
8204 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8205 message */
8206 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008207 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008208 }
8209 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008210 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008211 else
8212 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 if (nmatched == 0)
8214 {
8215 shl->lnum = 0; /* no match found */
8216 break;
8217 }
8218 if (shl->rm.startpos[0].lnum > 0
8219 || shl->rm.startpos[0].col >= mincol
8220 || nmatched > 1
8221 || shl->rm.endpos[0].col > mincol)
8222 {
8223 shl->lnum += shl->rm.startpos[0].lnum;
8224 break; /* useful match found */
8225 }
8226 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008227
8228 // Restore called_emsg for assert_fails().
8229 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
Bram Moolenaar85077472016-10-16 14:35:48 +02008232/*
8233 * If there is a match fill "shl" and return one.
8234 * Return zero otherwise.
8235 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008236 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008237next_search_hl_pos(
8238 match_T *shl, /* points to a match */
8239 linenr_T lnum,
8240 posmatch_T *posmatch, /* match positions */
8241 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008242{
8243 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008244 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008245
Bram Moolenaarb3414592014-06-17 17:48:32 +02008246 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8247 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008248 llpos_T *pos = &posmatch->pos[i];
8249
8250 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008251 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008252 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008253 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008254 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008255 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008256 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008257 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008258 /* if this match comes before the one at "found" then swap
8259 * them */
8260 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008261 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008262 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008263
Bram Moolenaar85077472016-10-16 14:35:48 +02008264 *pos = posmatch->pos[found];
8265 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008266 }
8267 }
8268 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008269 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008270 }
8271 }
8272 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008273 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008274 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008275 colnr_T start = posmatch->pos[found].col == 0
8276 ? 0 : posmatch->pos[found].col - 1;
8277 colnr_T end = posmatch->pos[found].col == 0
8278 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008279
Bram Moolenaar85077472016-10-16 14:35:48 +02008280 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008281 shl->rm.startpos[0].lnum = 0;
8282 shl->rm.startpos[0].col = start;
8283 shl->rm.endpos[0].lnum = 0;
8284 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008285 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008286 posmatch->cur = found + 1;
8287 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008288 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008289 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008290}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008291#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008292
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008294screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
8296 attrentry_T *aep = NULL;
8297
8298 screen_attr = attr;
8299 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008300#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 && termcap_active
8302#endif
8303 )
8304 {
8305#ifdef FEAT_GUI
8306 if (gui.in_use)
8307 {
8308 char buf[20];
8309
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008310 /* The GUI handles this internally. */
8311 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 OUT_STR(buf);
8313 }
8314 else
8315#endif
8316 {
8317 if (attr > HL_ALL) /* special HL attr. */
8318 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008319 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 aep = syn_cterm_attr2entry(attr);
8321 else
8322 aep = syn_term_attr2entry(attr);
8323 if (aep == NULL) /* did ":syntax clear" */
8324 attr = 0;
8325 else
8326 attr = aep->ae_attr;
8327 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008328 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008330 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008331#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008332 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8333 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8334 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008335#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008336 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008337 /* If the Normal FG color has BOLD attribute and the new HL
8338 * has a FG color defined, clear BOLD. */
8339 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008340 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008342 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008343 out_str(T_UCS);
8344 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008345 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8346 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008348 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008350 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008352 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008353 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354
8355 /*
8356 * Output the color or start string after bold etc., in case the
8357 * bold etc. override the color setting.
8358 */
8359 if (aep != NULL)
8360 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008361#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008362 /* When 'termguicolors' is set but fg or bg is unset,
8363 * fall back to the cterm colors. This helps for SpellBad,
8364 * where the GUI uses a red undercurl. */
8365 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008367 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008368 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008369 }
8370 else
8371#endif
8372 if (t_colors > 1)
8373 {
8374 if (aep->ae_u.cterm.fg_color)
8375 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8376 }
8377#ifdef FEAT_TERMGUICOLORS
8378 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8379 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008380 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008381 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008384#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008385 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008387 if (aep->ae_u.cterm.bg_color)
8388 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8389 }
8390
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008391 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008392 {
8393 if (aep->ae_u.term.start != NULL)
8394 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 }
8396 }
8397 }
8398 }
8399}
8400
8401 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008402screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 int do_ME = FALSE; /* output T_ME code */
8405
8406 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008407#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 && termcap_active
8409#endif
8410 )
8411 {
8412#ifdef FEAT_GUI
8413 if (gui.in_use)
8414 {
8415 char buf[20];
8416
8417 /* use internal GUI code */
8418 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8419 OUT_STR(buf);
8420 }
8421 else
8422#endif
8423 {
8424 if (screen_attr > HL_ALL) /* special HL attr. */
8425 {
8426 attrentry_T *aep;
8427
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008428 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {
8430 /*
8431 * Assume that t_me restores the original colors!
8432 */
8433 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008434 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008435#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008436 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8437 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8438 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008439#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008440 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008441#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008442 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8443 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8444 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008445#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008446 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 do_ME = TRUE;
8448 }
8449 else
8450 {
8451 aep = syn_term_attr2entry(screen_attr);
8452 if (aep != NULL && aep->ae_u.term.stop != NULL)
8453 {
8454 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8455 do_ME = TRUE;
8456 else
8457 out_str(aep->ae_u.term.stop);
8458 }
8459 }
8460 if (aep == NULL) /* did ":syntax clear" */
8461 screen_attr = 0;
8462 else
8463 screen_attr = aep->ae_attr;
8464 }
8465
8466 /*
8467 * Often all ending-codes are equal to T_ME. Avoid outputting the
8468 * same sequence several times.
8469 */
8470 if (screen_attr & HL_STANDOUT)
8471 {
8472 if (STRCMP(T_SE, T_ME) == 0)
8473 do_ME = TRUE;
8474 else
8475 out_str(T_SE);
8476 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008477 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008478 {
8479 if (STRCMP(T_UCE, T_ME) == 0)
8480 do_ME = TRUE;
8481 else
8482 out_str(T_UCE);
8483 }
8484 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008485 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {
8487 if (STRCMP(T_UE, T_ME) == 0)
8488 do_ME = TRUE;
8489 else
8490 out_str(T_UE);
8491 }
8492 if (screen_attr & HL_ITALIC)
8493 {
8494 if (STRCMP(T_CZR, T_ME) == 0)
8495 do_ME = TRUE;
8496 else
8497 out_str(T_CZR);
8498 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008499 if (screen_attr & HL_STRIKETHROUGH)
8500 {
8501 if (STRCMP(T_STE, T_ME) == 0)
8502 do_ME = TRUE;
8503 else
8504 out_str(T_STE);
8505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8507 out_str(T_ME);
8508
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008509#ifdef FEAT_TERMGUICOLORS
8510 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008512 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008513 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008514 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008515 term_bg_rgb_color(cterm_normal_bg_gui_color);
8516 }
8517 else
8518#endif
8519 {
8520 if (t_colors > 1)
8521 {
8522 /* set Normal cterm colors */
8523 if (cterm_normal_fg_color != 0)
8524 term_fg_color(cterm_normal_fg_color - 1);
8525 if (cterm_normal_bg_color != 0)
8526 term_bg_color(cterm_normal_bg_color - 1);
8527 if (cterm_normal_fg_bold)
8528 out_str(T_MD);
8529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 }
8531 }
8532 }
8533 screen_attr = 0;
8534}
8535
8536/*
8537 * Reset the colors for a cterm. Used when leaving Vim.
8538 * The machine specific code may override this again.
8539 */
8540 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008541reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008543 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
8545 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008546#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008547 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8548 || cterm_normal_bg_gui_color != INVALCOLOR)
8549 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008550#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {
8554 out_str(T_OP);
8555 screen_attr = -1;
8556 }
8557 if (cterm_normal_fg_bold)
8558 {
8559 out_str(T_ME);
8560 screen_attr = -1;
8561 }
8562 }
8563}
8564
8565/*
8566 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8567 * using the attributes from ScreenAttrs["off"].
8568 */
8569 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008570screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 int attr;
8573
8574 /* Check for illegal values, just in case (could happen just after
8575 * resizing). */
8576 if (row >= screen_Rows || col >= screen_Columns)
8577 return;
8578
Bram Moolenaarae654382019-01-17 21:09:05 +01008579#ifdef FEAT_INS_EXPAND
8580 if (pum_under_menu(row, col))
8581 return;
8582#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008583 /* Outputting a character in the last cell on the screen may scroll the
8584 * screen up. Only do it when the "xn" termcap property is set, otherwise
8585 * mark the character invalid (update it when scrolled up). */
8586 if (*T_XN == NUL
8587 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588#ifdef FEAT_RIGHTLEFT
8589 /* account for first command-line character in rightleft mode */
8590 && !cmdmsg_rl
8591#endif
8592 )
8593 {
8594 ScreenAttrs[off] = (sattr_T)-1;
8595 return;
8596 }
8597
8598 /*
8599 * Stop highlighting first, so it's easier to move the cursor.
8600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (screen_char_attr != 0)
8602 attr = screen_char_attr;
8603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 attr = ScreenAttrs[off];
8605 if (screen_attr != attr)
8606 screen_stop_highlight();
8607
8608 windgoto(row, col);
8609
8610 if (screen_attr != attr)
8611 screen_start_highlight(attr);
8612
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 if (enc_utf8 && ScreenLinesUC[off] != 0)
8614 {
8615 char_u buf[MB_MAXBYTES + 1];
8616
Bram Moolenaarcb070082016-04-02 22:14:51 +02008617 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008618 {
8619 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008620#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008621 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008622#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008623 )
8624 {
8625 /* Clear the two screen cells. If the character is actually
8626 * single width it won't change the second cell. */
8627 out_str((char_u *)" ");
8628 term_windgoto(row, col);
8629 }
8630 /* not sure where the cursor is after drawing the ambiguous width
8631 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008632 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008633 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008634 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008636
8637 /* Convert the UTF-8 character to bytes and write it. */
8638 buf[utfc_char2bytes(off, buf)] = NUL;
8639 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 /* double-byte character in single-width cell */
8646 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8647 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 }
8649
8650 screen_cur_col++;
8651}
8652
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653/*
8654 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8655 * on the screen at position 'row' and 'col'.
8656 * The attributes of the first byte is used for all. This is required to
8657 * output the two bytes of a double-byte character with nothing in between.
8658 */
8659 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008660screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
8662 /* Check for illegal values (could be wrong when screen was resized). */
8663 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8664 return;
8665
8666 /* Outputting the last character on the screen may scrollup the screen.
8667 * Don't to it! Mark the character invalid (update it when scrolled up) */
8668 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8669 {
8670 ScreenAttrs[off] = (sattr_T)-1;
8671 return;
8672 }
8673
8674 /* Output the first byte normally (positions the cursor), then write the
8675 * second byte directly. */
8676 screen_char(off, row, col);
8677 out_char(ScreenLines[off + 1]);
8678 ++screen_cur_col;
8679}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681/*
8682 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8683 * This uses the contents of ScreenLines[] and doesn't change it.
8684 */
8685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008686screen_draw_rectangle(
8687 int row,
8688 int col,
8689 int height,
8690 int width,
8691 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
8693 int r, c;
8694 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008695 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008697 /* Can't use ScreenLines unless initialized */
8698 if (ScreenLines == NULL)
8699 return;
8700
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 if (invert)
8702 screen_char_attr = HL_INVERSE;
8703 for (r = row; r < row + height; ++r)
8704 {
8705 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008706 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 for (c = col; c < col + width; ++c)
8708 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008709 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 {
8711 screen_char_2(off + c, r, c);
8712 ++c;
8713 }
8714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 {
8716 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008717 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 }
8720 }
8721 }
8722 screen_char_attr = 0;
8723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725/*
8726 * Redraw the characters for a vertically split window.
8727 */
8728 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008729redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 int col;
8732 int width;
8733
8734# ifdef FEAT_CLIPBOARD
8735 clip_may_clear_selection(row, end - 1);
8736# endif
8737
8738 if (wp == NULL)
8739 {
8740 col = 0;
8741 width = Columns;
8742 }
8743 else
8744 {
8745 col = wp->w_wincol;
8746 width = wp->w_width;
8747 }
8748 screen_draw_rectangle(row, col, end - row, width, FALSE);
8749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008751 static void
8752space_to_screenline(int off, int attr)
8753{
8754 ScreenLines[off] = ' ';
8755 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008756 if (enc_utf8)
8757 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008758}
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760/*
8761 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8762 * with character 'c1' in first column followed by 'c2' in the other columns.
8763 * Use attributes 'attr'.
8764 */
8765 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008766screen_fill(
8767 int start_row,
8768 int end_row,
8769 int start_col,
8770 int end_col,
8771 int c1,
8772 int c2,
8773 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 int row;
8776 int col;
8777 int off;
8778 int end_off;
8779 int did_delete;
8780 int c;
8781 int norm_term;
8782#if defined(FEAT_GUI) || defined(UNIX)
8783 int force_next = FALSE;
8784#endif
8785
8786 if (end_row > screen_Rows) /* safety check */
8787 end_row = screen_Rows;
8788 if (end_col > screen_Columns) /* safety check */
8789 end_col = screen_Columns;
8790 if (ScreenLines == NULL
8791 || start_row >= end_row
8792 || start_col >= end_col) /* nothing to do */
8793 return;
8794
8795 /* it's a "normal" terminal when not in a GUI or cterm */
8796 norm_term = (
8797#ifdef FEAT_GUI
8798 !gui.in_use &&
8799#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008800 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 for (row = start_row; row < end_row; ++row)
8802 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008803 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008804#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008805 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008806#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008807 )
8808 {
8809 /* When drawing over the right halve of a double-wide char clear
8810 * out the left halve. When drawing over the left halve of a
8811 * double wide-char clear out the right halve. Only needed in a
8812 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008813 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008814 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008815 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008816 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 /*
8819 * Try to use delete-line termcap code, when no attributes or in a
8820 * "normal" terminal, where a bold/italic space is just a
8821 * space.
8822 */
8823 did_delete = FALSE;
8824 if (c2 == ' '
8825 && end_col == Columns
8826 && can_clear(T_CE)
8827 && (attr == 0
8828 || (norm_term
8829 && attr <= HL_ALL
8830 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8831 {
8832 /*
8833 * check if we really need to clear something
8834 */
8835 col = start_col;
8836 if (c1 != ' ') /* don't clear first char */
8837 ++col;
8838
8839 off = LineOffset[row] + col;
8840 end_off = LineOffset[row] + end_col;
8841
8842 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (enc_utf8)
8844 while (off < end_off && ScreenLines[off] == ' '
8845 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8846 ++off;
8847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 while (off < end_off && ScreenLines[off] == ' '
8849 && ScreenAttrs[off] == 0)
8850 ++off;
8851 if (off < end_off) /* something to be cleared */
8852 {
8853 col = off - LineOffset[row];
8854 screen_stop_highlight();
8855 term_windgoto(row, col);/* clear rest of this screen line */
8856 out_str(T_CE);
8857 screen_start(); /* don't know where cursor is now */
8858 col = end_col - col;
8859 while (col--) /* clear chars in ScreenLines */
8860 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008861 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 ++off;
8863 }
8864 }
8865 did_delete = TRUE; /* the chars are cleared now */
8866 }
8867
8868 off = LineOffset[row] + start_col;
8869 c = c1;
8870 for (col = start_col; col < end_col; ++col)
8871 {
8872 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008873 || (enc_utf8 && (int)ScreenLinesUC[off]
8874 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 || ScreenAttrs[off] != attr
8876#if defined(FEAT_GUI) || defined(UNIX)
8877 || force_next
8878#endif
8879 )
8880 {
8881#if defined(FEAT_GUI) || defined(UNIX)
8882 /* The bold trick may make a single row of pixels appear in
8883 * the next character. When a bold character is removed, the
8884 * next character should be redrawn too. This happens for our
8885 * own GUI and for some xterms. */
8886 if (
8887# ifdef FEAT_GUI
8888 gui.in_use
8889# endif
8890# if defined(FEAT_GUI) && defined(UNIX)
8891 ||
8892# endif
8893# ifdef UNIX
8894 term_is_xterm
8895# endif
8896 )
8897 {
8898 if (ScreenLines[off] != ' '
8899 && (ScreenAttrs[off] > HL_ALL
8900 || ScreenAttrs[off] & HL_BOLD))
8901 force_next = TRUE;
8902 else
8903 force_next = FALSE;
8904 }
8905#endif
8906 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (enc_utf8)
8908 {
8909 if (c >= 0x80)
8910 {
8911 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008912 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 }
8914 else
8915 ScreenLinesUC[off] = 0;
8916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 ScreenAttrs[off] = attr;
8918 if (!did_delete || c != ' ')
8919 screen_char(off, row, col);
8920 }
8921 ++off;
8922 if (col == start_col)
8923 {
8924 if (did_delete)
8925 break;
8926 c = c2;
8927 }
8928 }
8929 if (end_col == Columns)
8930 LineWraps[row] = FALSE;
8931 if (row == Rows - 1) /* overwritten the command line */
8932 {
8933 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008934 if (start_col == 0 && end_col == Columns
8935 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008937 if (start_col == 0)
8938 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 }
8940 }
8941}
8942
8943/*
8944 * Check if there should be a delay. Used before clearing or redrawing the
8945 * screen or the command line.
8946 */
8947 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008948check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949{
8950 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8951 && !did_wait_return
8952 && emsg_silent == 0)
8953 {
8954 out_flush();
8955 ui_delay(1000L, TRUE);
8956 emsg_on_display = FALSE;
8957 if (check_msg_scroll)
8958 msg_scroll = FALSE;
8959 }
8960}
8961
8962/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008963 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8964 */
8965 static void
8966clear_TabPageIdxs(void)
8967{
8968 int scol;
8969
8970 for (scol = 0; scol < Columns; ++scol)
8971 TabPageIdxs[scol] = 0;
8972}
8973
8974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008976 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 * Returns TRUE if there is a valid screen to write to.
8978 * Returns FALSE when starting up and screen not initialized yet.
8979 */
8980 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008981screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008983 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 return (ScreenLines != NULL);
8985}
8986
8987/*
8988 * Resize the shell to Rows and Columns.
8989 * Allocate ScreenLines[] and associated items.
8990 *
8991 * There may be some time between setting Rows and Columns and (re)allocating
8992 * ScreenLines[]. This happens when starting up and when (manually) changing
8993 * the shell size. Always use screen_Rows and screen_Columns to access items
8994 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8995 * final size of the shell is needed.
8996 */
8997 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008998screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
9000 int new_row, old_row;
9001#ifdef FEAT_GUI
9002 int old_Rows;
9003#endif
9004 win_T *wp;
9005 int outofmem = FALSE;
9006 int len;
9007 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009011 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 sattr_T *new_ScreenAttrs;
9013 unsigned *new_LineOffset;
9014 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009015 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009016 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009018 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009019 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009021retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 /*
9023 * Allocation of the screen buffers is done only when the size changes and
9024 * when Rows and Columns have been set and we have started doing full
9025 * screen stuff.
9026 */
9027 if ((ScreenLines != NULL
9028 && Rows == screen_Rows
9029 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 && enc_utf8 == (ScreenLinesUC != NULL)
9031 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009032 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 || Rows == 0
9034 || Columns == 0
9035 || (!full_screen && ScreenLines == NULL))
9036 return;
9037
9038 /*
9039 * It's possible that we produce an out-of-memory message below, which
9040 * will cause this function to be called again. To break the loop, just
9041 * return here.
9042 */
9043 if (entered)
9044 return;
9045 entered = TRUE;
9046
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009047 /*
9048 * Note that the window sizes are updated before reallocating the arrays,
9049 * thus we must not redraw here!
9050 */
9051 ++RedrawingDisabled;
9052
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 win_new_shellsize(); /* fit the windows in the new sized shell */
9054
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 comp_col(); /* recompute columns for shown command and ruler */
9056
9057 /*
9058 * We're changing the size of the screen.
9059 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9060 * - Move lines from the old arrays into the new arrays, clear extra
9061 * lines (unless the screen is going to be cleared).
9062 * - Free the old arrays.
9063 *
9064 * If anything fails, make ScreenLines NULL, so we don't do anything!
9065 * Continuing with the old ScreenLines may result in a crash, because the
9066 * size is wrong.
9067 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009068 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009070 if (aucmd_win != NULL)
9071 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009072#ifdef FEAT_TEXT_PROP
9073 // global popup windows
9074 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9075 win_free_lsize(wp);
9076 // tab-local popup windows
9077 FOR_ALL_TABPAGES(tp)
9078 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9079 win_free_lsize(wp);
9080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009082 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009083 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 if (enc_utf8)
9085 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009086 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009087 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009088 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9089 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 }
9091 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009092 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9093 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9094 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9095 new_LineWraps = LALLOC_MULT(char_u, Rows);
9096 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009098 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 {
9100 if (win_alloc_lines(wp) == FAIL)
9101 {
9102 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009103 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 }
9105 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009106 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9107 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009108 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009109#ifdef FEAT_TEXT_PROP
9110 // global popup windows
9111 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9112 if (win_alloc_lines(wp) == FAIL)
9113 {
9114 outofmem = TRUE;
9115 goto give_up;
9116 }
9117 // tab-local popup windows
9118 FOR_ALL_TABPAGES(tp)
9119 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9120 if (win_alloc_lines(wp) == FAIL)
9121 {
9122 outofmem = TRUE;
9123 goto give_up;
9124 }
9125#endif
9126
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009127give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009129 for (i = 0; i < p_mco; ++i)
9130 if (new_ScreenLinesC[i] == NULL)
9131 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009133 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 || new_ScreenAttrs == NULL
9136 || new_LineOffset == NULL
9137 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009138 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 || outofmem)
9140 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009141 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009142 {
9143 /* guess the size */
9144 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9145
9146 /* Remember we did this to avoid getting outofmem messages over
9147 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009148 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009149 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009150 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009151 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009152 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009153 VIM_CLEAR(new_ScreenLinesC[i]);
9154 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009155 VIM_CLEAR(new_ScreenAttrs);
9156 VIM_CLEAR(new_LineOffset);
9157 VIM_CLEAR(new_LineWraps);
9158 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 }
9160 else
9161 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009162 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009163
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 for (new_row = 0; new_row < Rows; ++new_row)
9165 {
9166 new_LineOffset[new_row] = new_row * Columns;
9167 new_LineWraps[new_row] = FALSE;
9168
9169 /*
9170 * If the screen is not going to be cleared, copy as much as
9171 * possible from the old screen to the new one and clear the rest
9172 * (used when resizing the window at the "--more--" prompt or when
9173 * executing an external command, for the GUI).
9174 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009175 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
9177 (void)vim_memset(new_ScreenLines + new_row * Columns,
9178 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 if (enc_utf8)
9180 {
9181 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9182 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009183 for (i = 0; i < p_mco; ++i)
9184 (void)vim_memset(new_ScreenLinesC[i]
9185 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 0, (size_t)Columns * sizeof(u8char_T));
9187 }
9188 if (enc_dbcs == DBCS_JPNU)
9189 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9190 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9192 0, (size_t)Columns * sizeof(sattr_T));
9193 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009194 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 {
9196 if (screen_Columns < Columns)
9197 len = screen_Columns;
9198 else
9199 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009200 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009201 * may be invalid now. Also when p_mco changes. */
9202 if (!(enc_utf8 && ScreenLinesUC == NULL)
9203 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009204 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9205 ScreenLines + LineOffset[old_row],
9206 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009207 if (enc_utf8 && ScreenLinesUC != NULL
9208 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 {
9210 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9211 ScreenLinesUC + LineOffset[old_row],
9212 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009213 for (i = 0; i < p_mco; ++i)
9214 mch_memmove(new_ScreenLinesC[i]
9215 + new_LineOffset[new_row],
9216 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 (size_t)len * sizeof(u8char_T));
9218 }
9219 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9220 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9221 ScreenLines2 + LineOffset[old_row],
9222 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9224 ScreenAttrs + LineOffset[old_row],
9225 (size_t)len * sizeof(sattr_T));
9226 }
9227 }
9228 }
9229 /* Use the last line of the screen for the current line. */
9230 current_ScreenLine = new_ScreenLines + Rows * Columns;
9231 }
9232
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009233 free_screenlines();
9234
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009237 for (i = 0; i < p_mco; ++i)
9238 ScreenLinesC[i] = new_ScreenLinesC[i];
9239 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 ScreenAttrs = new_ScreenAttrs;
9242 LineOffset = new_LineOffset;
9243 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009244 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245
9246 /* It's important that screen_Rows and screen_Columns reflect the actual
9247 * size of ScreenLines[]. Set them before calling anything. */
9248#ifdef FEAT_GUI
9249 old_Rows = screen_Rows;
9250#endif
9251 screen_Rows = Rows;
9252 screen_Columns = Columns;
9253
9254 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009255 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257#ifdef FEAT_GUI
9258 else if (gui.in_use
9259 && !gui.starting
9260 && ScreenLines != NULL
9261 && old_Rows != Rows)
9262 {
9263 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9264 /*
9265 * Adjust the position of the cursor, for when executing an external
9266 * command.
9267 */
9268 if (msg_row >= Rows) /* Rows got smaller */
9269 msg_row = Rows - 1; /* put cursor at last row */
9270 else if (Rows > old_Rows) /* Rows got bigger */
9271 msg_row += Rows - old_Rows; /* put cursor in same place */
9272 if (msg_col >= Columns) /* Columns got smaller */
9273 msg_col = Columns - 1; /* put cursor at last column */
9274 }
9275#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009276 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009279 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009280
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009281 /*
9282 * Do not apply autocommands more than 3 times to avoid an endless loop
9283 * in case applying autocommands always changes Rows or Columns.
9284 */
9285 if (starting == 0 && ++retry_count <= 3)
9286 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009287 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009288 /* In rare cases, autocommands may have altered Rows or Columns,
9289 * jump back to check if we need to allocate the screen again. */
9290 goto retry;
9291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292}
9293
9294 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009295free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009296{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009297 int i;
9298
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009299 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009300 for (i = 0; i < Screen_mco; ++i)
9301 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009302 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009303 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009304 vim_free(ScreenAttrs);
9305 vim_free(LineOffset);
9306 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009307 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009308}
9309
9310 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009311screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 check_for_delay(FALSE);
9314 screenalloc(FALSE); /* allocate screen buffers if size changed */
9315 screenclear2(); /* clear the screen */
9316}
9317
9318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009319screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 int i;
9322
9323 if (starting == NO_SCREEN || ScreenLines == NULL
9324#ifdef FEAT_GUI
9325 || (gui.in_use && gui.starting)
9326#endif
9327 )
9328 return;
9329
9330#ifdef FEAT_GUI
9331 if (!gui.in_use)
9332#endif
9333 screen_attr = -1; /* force setting the Normal colors */
9334 screen_stop_highlight(); /* don't want highlighting here */
9335
9336#ifdef FEAT_CLIPBOARD
9337 /* disable selection without redrawing it */
9338 clip_scroll_selection(9999);
9339#endif
9340
9341 /* blank out ScreenLines */
9342 for (i = 0; i < Rows; ++i)
9343 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009344 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 LineWraps[i] = FALSE;
9346 }
9347
9348 if (can_clear(T_CL))
9349 {
9350 out_str(T_CL); /* clear the display */
9351 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009352 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 }
9354 else
9355 {
9356 /* can't clear the screen, mark all chars with invalid attributes */
9357 for (i = 0; i < Rows; ++i)
9358 lineinvalid(LineOffset[i], (int)Columns);
9359 clear_cmdline = TRUE;
9360 }
9361
9362 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9363
9364 win_rest_invalid(firstwin);
9365 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009366 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 if (must_redraw == CLEAR) /* no need to clear again */
9368 must_redraw = NOT_VALID;
9369 compute_cmdrow();
9370 msg_row = cmdline_row; /* put cursor on last line for messages */
9371 msg_col = 0;
9372 screen_start(); /* don't know where cursor is now */
9373 msg_scrolled = 0; /* can't scroll back */
9374 msg_didany = FALSE;
9375 msg_didout = FALSE;
9376}
9377
9378/*
9379 * Clear one line in ScreenLines.
9380 */
9381 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009382lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
9384 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if (enc_utf8)
9386 (void)vim_memset(ScreenLinesUC + off, 0,
9387 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009388 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389}
9390
9391/*
9392 * Mark one line in ScreenLines invalid by setting the attributes to an
9393 * invalid value.
9394 */
9395 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009396lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397{
9398 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9399}
9400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401/*
9402 * Copy part of a Screenline for vertically split window "wp".
9403 */
9404 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009405linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 unsigned off_to = LineOffset[to] + wp->w_wincol;
9408 unsigned off_from = LineOffset[from] + wp->w_wincol;
9409
9410 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9411 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 if (enc_utf8)
9413 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009414 int i;
9415
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9417 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009418 for (i = 0; i < p_mco; ++i)
9419 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9420 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 }
9422 if (enc_dbcs == DBCS_JPNU)
9423 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9424 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9426 wp->w_width * sizeof(sattr_T));
9427}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428
9429/*
9430 * Return TRUE if clearing with term string "p" would work.
9431 * It can't work when the string is empty or it won't set the right background.
9432 */
9433 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009434can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436 return (*p != NUL && (t_colors <= 1
9437#ifdef FEAT_GUI
9438 || gui.in_use
9439#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009440#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009441 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009442 || (!p_tgc && cterm_normal_bg_color == 0)
9443#else
9444 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009445#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009446 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447}
9448
9449/*
9450 * Reset cursor position. Use whenever cursor was moved because of outputting
9451 * something directly to the screen (shell commands) or a terminal control
9452 * code.
9453 */
9454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009455screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456{
9457 screen_cur_row = screen_cur_col = 9999;
9458}
9459
9460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 * Move the cursor to position "row","col" in the screen.
9462 * This tries to find the most efficient way to move, minimizing the number of
9463 * characters sent to the terminal.
9464 */
9465 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009466windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009468 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 int i;
9470 int plan;
9471 int cost;
9472 int wouldbe_col;
9473 int noinvcurs;
9474 char_u *bs;
9475 int goto_cost;
9476 int attr;
9477
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009478#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9480
9481#define PLAN_LE 1
9482#define PLAN_CR 2
9483#define PLAN_NL 3
9484#define PLAN_WRITE 4
9485 /* Can't use ScreenLines unless initialized */
9486 if (ScreenLines == NULL)
9487 return;
9488
9489 if (col != screen_cur_col || row != screen_cur_row)
9490 {
9491 /* Check for valid position. */
9492 if (row < 0) /* window without text lines? */
9493 row = 0;
9494 if (row >= screen_Rows)
9495 row = screen_Rows - 1;
9496 if (col >= screen_Columns)
9497 col = screen_Columns - 1;
9498
9499 /* check if no cursor movement is allowed in highlight mode */
9500 if (screen_attr && *T_MS == NUL)
9501 noinvcurs = HIGHL_COST;
9502 else
9503 noinvcurs = 0;
9504 goto_cost = GOTO_COST + noinvcurs;
9505
9506 /*
9507 * Plan how to do the positioning:
9508 * 1. Use CR to move it to column 0, same row.
9509 * 2. Use T_LE to move it a few columns to the left.
9510 * 3. Use NL to move a few lines down, column 0.
9511 * 4. Move a few columns to the right with T_ND or by writing chars.
9512 *
9513 * Don't do this if the cursor went beyond the last column, the cursor
9514 * position is unknown then (some terminals wrap, some don't )
9515 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009516 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 * characters to move the cursor to the right.
9518 */
9519 if (row >= screen_cur_row && screen_cur_col < Columns)
9520 {
9521 /*
9522 * If the cursor is in the same row, bigger col, we can use CR
9523 * or T_LE.
9524 */
9525 bs = NULL; /* init for GCC */
9526 attr = screen_attr;
9527 if (row == screen_cur_row && col < screen_cur_col)
9528 {
9529 /* "le" is preferred over "bc", because "bc" is obsolete */
9530 if (*T_LE)
9531 bs = T_LE; /* "cursor left" */
9532 else
9533 bs = T_BC; /* "backspace character (old) */
9534 if (*bs)
9535 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9536 else
9537 cost = 999;
9538 if (col + 1 < cost) /* using CR is less characters */
9539 {
9540 plan = PLAN_CR;
9541 wouldbe_col = 0;
9542 cost = 1; /* CR is just one character */
9543 }
9544 else
9545 {
9546 plan = PLAN_LE;
9547 wouldbe_col = col;
9548 }
9549 if (noinvcurs) /* will stop highlighting */
9550 {
9551 cost += noinvcurs;
9552 attr = 0;
9553 }
9554 }
9555
9556 /*
9557 * If the cursor is above where we want to be, we can use CR LF.
9558 */
9559 else if (row > screen_cur_row)
9560 {
9561 plan = PLAN_NL;
9562 wouldbe_col = 0;
9563 cost = (row - screen_cur_row) * 2; /* CR LF */
9564 if (noinvcurs) /* will stop highlighting */
9565 {
9566 cost += noinvcurs;
9567 attr = 0;
9568 }
9569 }
9570
9571 /*
9572 * If the cursor is in the same row, smaller col, just use write.
9573 */
9574 else
9575 {
9576 plan = PLAN_WRITE;
9577 wouldbe_col = screen_cur_col;
9578 cost = 0;
9579 }
9580
9581 /*
9582 * Check if any characters that need to be written have the
9583 * correct attributes. Also avoid UTF-8 characters.
9584 */
9585 i = col - wouldbe_col;
9586 if (i > 0)
9587 cost += i;
9588 if (cost < goto_cost && i > 0)
9589 {
9590 /*
9591 * Check if the attributes are correct without additionally
9592 * stopping highlighting.
9593 */
9594 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9595 while (i && *p++ == attr)
9596 --i;
9597 if (i != 0)
9598 {
9599 /*
9600 * Try if it works when highlighting is stopped here.
9601 */
9602 if (*--p == 0)
9603 {
9604 cost += noinvcurs;
9605 while (i && *p++ == 0)
9606 --i;
9607 }
9608 if (i != 0)
9609 cost = 999; /* different attributes, don't do it */
9610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 if (enc_utf8)
9612 {
9613 /* Don't use an UTF-8 char for positioning, it's slow. */
9614 for (i = wouldbe_col; i < col; ++i)
9615 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9616 {
9617 cost = 999;
9618 break;
9619 }
9620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 }
9622
9623 /*
9624 * We can do it without term_windgoto()!
9625 */
9626 if (cost < goto_cost)
9627 {
9628 if (plan == PLAN_LE)
9629 {
9630 if (noinvcurs)
9631 screen_stop_highlight();
9632 while (screen_cur_col > col)
9633 {
9634 out_str(bs);
9635 --screen_cur_col;
9636 }
9637 }
9638 else if (plan == PLAN_CR)
9639 {
9640 if (noinvcurs)
9641 screen_stop_highlight();
9642 out_char('\r');
9643 screen_cur_col = 0;
9644 }
9645 else if (plan == PLAN_NL)
9646 {
9647 if (noinvcurs)
9648 screen_stop_highlight();
9649 while (screen_cur_row < row)
9650 {
9651 out_char('\n');
9652 ++screen_cur_row;
9653 }
9654 screen_cur_col = 0;
9655 }
9656
9657 i = col - screen_cur_col;
9658 if (i > 0)
9659 {
9660 /*
9661 * Use cursor-right if it's one character only. Avoids
9662 * removing a line of pixels from the last bold char, when
9663 * using the bold trick in the GUI.
9664 */
9665 if (T_ND[0] != NUL && T_ND[1] == NUL)
9666 {
9667 while (i-- > 0)
9668 out_char(*T_ND);
9669 }
9670 else
9671 {
9672 int off;
9673
9674 off = LineOffset[row] + screen_cur_col;
9675 while (i-- > 0)
9676 {
9677 if (ScreenAttrs[off] != screen_attr)
9678 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 if (enc_dbcs == DBCS_JPNU
9682 && ScreenLines[off] == 0x8e)
9683 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 ++off;
9685 }
9686 }
9687 }
9688 }
9689 }
9690 else
9691 cost = 999;
9692
9693 if (cost >= goto_cost)
9694 {
9695 if (noinvcurs)
9696 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009697 if (row == screen_cur_row && (col > screen_cur_col)
9698 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 term_cursor_right(col - screen_cur_col);
9700 else
9701 term_windgoto(row, col);
9702 }
9703 screen_cur_row = row;
9704 screen_cur_col = col;
9705 }
9706}
9707
9708/*
9709 * Set cursor to its position in the current window.
9710 */
9711 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009712setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009714 setcursor_mayforce(FALSE);
9715}
9716
9717/*
9718 * Set cursor to its position in the current window.
9719 * When "force" is TRUE also when not redrawing.
9720 */
9721 void
9722setcursor_mayforce(int force)
9723{
9724 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 {
9726 validate_cursor();
9727 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009728 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009730 /* With 'rightleft' set and the cursor on a double-wide
9731 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009732 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9733 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009734 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009735 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736#endif
9737 curwin->w_wcol));
9738 }
9739}
9740
9741
9742/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009743 * Insert 'line_count' lines at 'row' in window 'wp'.
9744 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9745 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 * scrolling.
9747 * Returns FAIL if the lines are not inserted, OK for success.
9748 */
9749 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009750win_ins_lines(
9751 win_T *wp,
9752 int row,
9753 int line_count,
9754 int invalid,
9755 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757 int did_delete;
9758 int nextrow;
9759 int lastrow;
9760 int retval;
9761
9762 if (invalid)
9763 wp->w_lines_valid = 0;
9764
9765 if (wp->w_height < 5)
9766 return FAIL;
9767
9768 if (line_count > wp->w_height - row)
9769 line_count = wp->w_height - row;
9770
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009771 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 if (retval != MAYBE)
9773 return retval;
9774
9775 /*
9776 * If there is a next window or a status line, we first try to delete the
9777 * lines at the bottom to avoid messing what is after the window.
9778 * If this fails and there are following windows, don't do anything to avoid
9779 * messing up those windows, better just redraw.
9780 */
9781 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 if (wp->w_next != NULL || wp->w_status_height)
9783 {
9784 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009785 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 did_delete = TRUE;
9787 else if (wp->w_next)
9788 return FAIL;
9789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 /*
9791 * if no lines deleted, blank the lines that will end up below the window
9792 */
9793 if (!did_delete)
9794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009797 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 lastrow = nextrow + line_count;
9799 if (lastrow > Rows)
9800 lastrow = Rows;
9801 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009802 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 ' ', ' ', 0);
9804 }
9805
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009806 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 == FAIL)
9808 {
9809 /* deletion will have messed up other windows */
9810 if (did_delete)
9811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 win_rest_invalid(W_NEXT(wp));
9814 }
9815 return FAIL;
9816 }
9817
9818 return OK;
9819}
9820
9821/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009822 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9824 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9825 * scrolling
9826 * Return OK for success, FAIL if the lines are not deleted.
9827 */
9828 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009829win_del_lines(
9830 win_T *wp,
9831 int row,
9832 int line_count,
9833 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009834 int mayclear,
9835 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 int retval;
9838
9839 if (invalid)
9840 wp->w_lines_valid = 0;
9841
9842 if (line_count > wp->w_height - row)
9843 line_count = wp->w_height - row;
9844
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009845 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (retval != MAYBE)
9847 return retval;
9848
9849 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009850 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 return FAIL;
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 /*
9854 * If there are windows or status lines below, try to put them at the
9855 * correct place. If we can't do that, they have to be redrawn.
9856 */
9857 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9858 {
9859 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009860 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 {
9862 wp->w_redr_status = TRUE;
9863 win_rest_invalid(wp->w_next);
9864 }
9865 }
9866 /*
9867 * If this is the last window and there is no status line, redraw the
9868 * command line later.
9869 */
9870 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 redraw_cmdline = TRUE;
9872 return OK;
9873}
9874
9875/*
9876 * Common code for win_ins_lines() and win_del_lines().
9877 * Returns OK or FAIL when the work has been done.
9878 * Returns MAYBE when not finished yet.
9879 */
9880 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009881win_do_lines(
9882 win_T *wp,
9883 int row,
9884 int line_count,
9885 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009886 int del,
9887 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
9889 int retval;
9890
9891 if (!redrawing() || line_count <= 0)
9892 return FAIL;
9893
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009894 /* When inserting lines would result in loss of command output, just redraw
9895 * the lines. */
9896 if (no_win_do_lines_ins && !del)
9897 return FAIL;
9898
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009900 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009902 if (!no_win_do_lines_ins)
9903 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 return FAIL;
9905 }
9906
9907 /*
9908 * Delete all remaining lines
9909 */
9910 if (row + line_count >= wp->w_height)
9911 {
9912 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009913 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 ' ', ' ', 0);
9915 return OK;
9916 }
9917
9918 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009919 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009921 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009923 if (!no_win_do_lines_ins)
9924 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
9926 /*
9927 * If the terminal can set a scroll region, use that.
9928 * Always do this in a vertically split window. This will redraw from
9929 * ScreenLines[] when t_CV isn't defined. That's faster than using
9930 * win_line().
9931 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009932 * a character in the lower right corner of the scroll region may cause a
9933 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009935 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 scroll_region_set(wp, row);
9939 if (del)
9940 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009941 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 else
9943 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009944 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 scroll_region_reset();
9947 return retval;
9948 }
9949
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952
9953 return MAYBE;
9954}
9955
9956/*
9957 * window 'wp' and everything after it is messed up, mark it for redraw
9958 */
9959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009960win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 {
9964 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 wp->w_redr_status = TRUE;
9966 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 }
9968 redraw_cmdline = TRUE;
9969}
9970
9971/*
9972 * The rest of the routines in this file perform screen manipulations. The
9973 * given operation is performed physically on the screen. The corresponding
9974 * change is also made to the internal screen image. In this way, the editor
9975 * anticipates the effect of editing changes on the appearance of the screen.
9976 * That way, when we call screenupdate a complete redraw isn't usually
9977 * necessary. Another advantage is that we can keep adding code to anticipate
9978 * screen changes, and in the meantime, everything still works.
9979 */
9980
9981/*
9982 * types for inserting or deleting lines
9983 */
9984#define USE_T_CAL 1
9985#define USE_T_CDL 2
9986#define USE_T_AL 3
9987#define USE_T_CE 4
9988#define USE_T_DL 5
9989#define USE_T_SR 6
9990#define USE_NL 7
9991#define USE_T_CD 8
9992#define USE_REDRAW 9
9993
9994/*
9995 * insert lines on the screen and update ScreenLines[]
9996 * 'end' is the line after the scrolled part. Normally it is Rows.
9997 * When scrolling region used 'off' is the offset from the top for the region.
9998 * 'row' and 'end' are relative to the start of the region.
9999 *
10000 * return FAIL for failure, OK for success.
10001 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000010002 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010003screen_ins_lines(
10004 int off,
10005 int row,
10006 int line_count,
10007 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010008 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +010010009 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010{
10011 int i;
10012 int j;
10013 unsigned temp;
10014 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010015 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 int type;
10017 int result_empty;
10018 int can_ce = can_clear(T_CE);
10019
10020 /*
10021 * FAIL if
10022 * - there is no valid screen
10023 * - the screen has to be redrawn completely
10024 * - the line count is less than one
10025 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010026 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010028 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
10029#ifdef FEAT_CLIPBOARD
10030 || (clip_star.state != SELECT_CLEARED
10031 && redrawing_for_callback > 0)
10032#endif
10033 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 return FAIL;
10035
10036 /*
10037 * There are seven ways to insert lines:
10038 * 0. When in a vertically split window and t_CV isn't set, redraw the
10039 * characters from ScreenLines[].
10040 * 1. Use T_CD (clear to end of display) if it exists and the result of
10041 * the insert is just empty lines
10042 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10043 * present or line_count > 1. It looks better if we do all the inserts
10044 * at once.
10045 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10046 * insert is just empty lines and T_CE is not present or line_count >
10047 * 1.
10048 * 4. Use T_AL (insert line) if it exists.
10049 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10050 * just empty lines.
10051 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10052 * just empty lines.
10053 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10054 * the 'da' flag is not set or we have clear line capability.
10055 * 8. redraw the characters from ScreenLines[].
10056 *
10057 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10058 * the scrollbar for the window. It does have insert line, use that if it
10059 * exists.
10060 */
10061 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10063 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010064 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 type = USE_T_CD;
10066 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10067 type = USE_T_CAL;
10068 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10069 type = USE_T_CDL;
10070 else if (*T_AL != NUL)
10071 type = USE_T_AL;
10072 else if (can_ce && result_empty)
10073 type = USE_T_CE;
10074 else if (*T_DL != NUL && result_empty)
10075 type = USE_T_DL;
10076 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10077 type = USE_T_SR;
10078 else
10079 return FAIL;
10080
10081 /*
10082 * For clearing the lines screen_del_lines() is used. This will also take
10083 * care of t_db if necessary.
10084 */
10085 if (type == USE_T_CD || type == USE_T_CDL ||
10086 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010087 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088
10089 /*
10090 * If text is retained below the screen, first clear or delete as many
10091 * lines at the bottom of the window as are about to be inserted so that
10092 * the deleted lines won't later surface during a screen_del_lines.
10093 */
10094 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010095 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096
10097#ifdef FEAT_CLIPBOARD
10098 /* Remove a modeless selection when inserting lines halfway the screen
10099 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010100 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010101 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 else
10103 clip_scroll_selection(-line_count);
10104#endif
10105
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106#ifdef FEAT_GUI
10107 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10108 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010109 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110#endif
10111
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010112 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10113 cursor_col = wp->w_wincol;
10114
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 if (*T_CCS != NUL) /* cursor relative to region */
10116 cursor_row = row;
10117 else
10118 cursor_row = row + off;
10119
10120 /*
10121 * Shift LineOffset[] line_count down to reflect the inserted lines.
10122 * Clear the inserted lines in ScreenLines[].
10123 */
10124 row += off;
10125 end += off;
10126 for (i = 0; i < line_count; ++i)
10127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 if (wp != NULL && wp->w_width != Columns)
10129 {
10130 /* need to copy part of a line */
10131 j = end - 1 - i;
10132 while ((j -= line_count) >= row)
10133 linecopy(j + line_count, j, wp);
10134 j += line_count;
10135 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010136 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10137 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 else
10139 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10140 LineWraps[j] = FALSE;
10141 }
10142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 {
10144 j = end - 1 - i;
10145 temp = LineOffset[j];
10146 while ((j -= line_count) >= row)
10147 {
10148 LineOffset[j + line_count] = LineOffset[j];
10149 LineWraps[j + line_count] = LineWraps[j];
10150 }
10151 LineOffset[j + line_count] = temp;
10152 LineWraps[j + line_count] = FALSE;
10153 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010154 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 else
10156 lineinvalid(temp, (int)Columns);
10157 }
10158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159
10160 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010161 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010162 if (clear_attr != 0)
10163 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 /* redraw the characters */
10166 if (type == USE_REDRAW)
10167 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010168 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 {
10170 term_append_lines(line_count);
10171 screen_start(); /* don't know where cursor is now */
10172 }
10173 else
10174 {
10175 for (i = 0; i < line_count; i++)
10176 {
10177 if (type == USE_T_AL)
10178 {
10179 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010180 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 out_str(T_AL);
10182 }
10183 else /* type == USE_T_SR */
10184 out_str(T_SR);
10185 screen_start(); /* don't know where cursor is now */
10186 }
10187 }
10188
10189 /*
10190 * With scroll-reverse and 'da' flag set we need to clear the lines that
10191 * have been scrolled down into the region.
10192 */
10193 if (type == USE_T_SR && *T_DA)
10194 {
10195 for (i = 0; i < line_count; ++i)
10196 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010197 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 out_str(T_CE);
10199 screen_start(); /* don't know where cursor is now */
10200 }
10201 }
10202
10203#ifdef FEAT_GUI
10204 gui_can_update_cursor();
10205 if (gui.in_use)
10206 out_flush(); /* always flush after a scroll */
10207#endif
10208 return OK;
10209}
10210
10211/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010212 * Delete lines on the screen and update ScreenLines[].
10213 * "end" is the line after the scrolled part. Normally it is Rows.
10214 * When scrolling region used "off" is the offset from the top for the region.
10215 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 *
10217 * Return OK for success, FAIL if the lines are not deleted.
10218 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010220screen_del_lines(
10221 int off,
10222 int row,
10223 int line_count,
10224 int end,
10225 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010226 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010227 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228{
10229 int j;
10230 int i;
10231 unsigned temp;
10232 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010233 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 int cursor_end;
10235 int result_empty; /* result is empty until end of region */
10236 int can_delete; /* deleting line codes can be used */
10237 int type;
10238
10239 /*
10240 * FAIL if
10241 * - there is no valid screen
10242 * - the screen has to be redrawn completely
10243 * - the line count is less than one
10244 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010245 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010247 if (!screen_valid(TRUE) || line_count <= 0
10248 || (!force && line_count > p_ttyscroll)
10249#ifdef FEAT_CLIPBOARD
10250 || (clip_star.state != SELECT_CLEARED
10251 && redrawing_for_callback > 0)
10252#endif
10253 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 return FAIL;
10255
10256 /*
10257 * Check if the rest of the current region will become empty.
10258 */
10259 result_empty = row + line_count >= end;
10260
10261 /*
10262 * We can delete lines only when 'db' flag not set or when 'ce' option
10263 * available.
10264 */
10265 can_delete = (*T_DB == NUL || can_clear(T_CE));
10266
10267 /*
10268 * There are six ways to delete lines:
10269 * 0. When in a vertically split window and t_CV isn't set, redraw the
10270 * characters from ScreenLines[].
10271 * 1. Use T_CD if it exists and the result is empty.
10272 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10273 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10274 * none of the other ways work.
10275 * 4. Use T_CE (erase line) if the result is empty.
10276 * 5. Use T_DL (delete line) if it exists.
10277 * 6. redraw the characters from ScreenLines[].
10278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10280 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010281 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 type = USE_T_CD;
10283#if defined(__BEOS__) && defined(BEOS_DR8)
10284 /*
10285 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10286 * its internal termcap... this works okay for tests which test *T_DB !=
10287 * NUL. It has the disadvantage that the user cannot use any :set t_*
10288 * command to get T_DB (back) to empty_option, only :set term=... will do
10289 * the trick...
10290 * Anyway, this hack will hopefully go away with the next OS release.
10291 * (Olaf Seibert)
10292 */
10293 else if (row == 0 && T_DB == empty_option
10294 && (line_count == 1 || *T_CDL == NUL))
10295#else
10296 else if (row == 0 && (
10297#ifndef AMIGA
10298 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10299 * up, so use delete-line command */
10300 line_count == 1 ||
10301#endif
10302 *T_CDL == NUL))
10303#endif
10304 type = USE_NL;
10305 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10306 type = USE_T_CDL;
10307 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010308 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 type = USE_T_CE;
10310 else if (*T_DL != NUL && can_delete)
10311 type = USE_T_DL;
10312 else if (*T_CDL != NUL && can_delete)
10313 type = USE_T_CDL;
10314 else
10315 return FAIL;
10316
10317#ifdef FEAT_CLIPBOARD
10318 /* Remove a modeless selection when deleting lines halfway the screen or
10319 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010320 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010321 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 else
10323 clip_scroll_selection(line_count);
10324#endif
10325
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326#ifdef FEAT_GUI
10327 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10328 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010329 gui_dont_update_cursor(gui.cursor_row >= row + off
10330 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331#endif
10332
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010333 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10334 cursor_col = wp->w_wincol;
10335
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 if (*T_CCS != NUL) /* cursor relative to region */
10337 {
10338 cursor_row = row;
10339 cursor_end = end;
10340 }
10341 else
10342 {
10343 cursor_row = row + off;
10344 cursor_end = end + off;
10345 }
10346
10347 /*
10348 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10349 * Clear the inserted lines in ScreenLines[].
10350 */
10351 row += off;
10352 end += off;
10353 for (i = 0; i < line_count; ++i)
10354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 if (wp != NULL && wp->w_width != Columns)
10356 {
10357 /* need to copy part of a line */
10358 j = row + i;
10359 while ((j += line_count) <= end - 1)
10360 linecopy(j - line_count, j, wp);
10361 j -= line_count;
10362 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010363 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10364 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 else
10366 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10367 LineWraps[j] = FALSE;
10368 }
10369 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 {
10371 /* whole width, moving the line pointers is faster */
10372 j = row + i;
10373 temp = LineOffset[j];
10374 while ((j += line_count) <= end - 1)
10375 {
10376 LineOffset[j - line_count] = LineOffset[j];
10377 LineWraps[j - line_count] = LineWraps[j];
10378 }
10379 LineOffset[j - line_count] = temp;
10380 LineWraps[j - line_count] = FALSE;
10381 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010382 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 else
10384 lineinvalid(temp, (int)Columns);
10385 }
10386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010388 if (screen_attr != clear_attr)
10389 screen_stop_highlight();
10390 if (clear_attr != 0)
10391 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 /* redraw the characters */
10394 if (type == USE_REDRAW)
10395 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010396 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010398 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 out_str(T_CD);
10400 screen_start(); /* don't know where cursor is now */
10401 }
10402 else if (type == USE_T_CDL)
10403 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010404 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 term_delete_lines(line_count);
10406 screen_start(); /* don't know where cursor is now */
10407 }
10408 /*
10409 * Deleting lines at top of the screen or scroll region: Just scroll
10410 * the whole screen (scroll region) up by outputting newlines on the
10411 * last line.
10412 */
10413 else if (type == USE_NL)
10414 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010415 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 for (i = line_count; --i >= 0; )
10417 out_char('\n'); /* cursor will remain on same line */
10418 }
10419 else
10420 {
10421 for (i = line_count; --i >= 0; )
10422 {
10423 if (type == USE_T_DL)
10424 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010425 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 out_str(T_DL); /* delete a line */
10427 }
10428 else /* type == USE_T_CE */
10429 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010430 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 out_str(T_CE); /* erase a line */
10432 }
10433 screen_start(); /* don't know where cursor is now */
10434 }
10435 }
10436
10437 /*
10438 * If the 'db' flag is set, we need to clear the lines that have been
10439 * scrolled up at the bottom of the region.
10440 */
10441 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10442 {
10443 for (i = line_count; i > 0; --i)
10444 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010445 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 out_str(T_CE); /* erase a line */
10447 screen_start(); /* don't know where cursor is now */
10448 }
10449 }
10450
10451#ifdef FEAT_GUI
10452 gui_can_update_cursor();
10453 if (gui.in_use)
10454 out_flush(); /* always flush after a scroll */
10455#endif
10456
10457 return OK;
10458}
10459
10460/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010461 * Return TRUE when postponing displaying the mode message: when not redrawing
10462 * or inside a mapping.
10463 */
10464 int
10465skip_showmode()
10466{
10467 // Call char_avail() only when we are going to show something, because it
10468 // takes a bit of time. redrawing() may also call char_avail_avail().
10469 if (global_busy
10470 || msg_silent != 0
10471 || !redrawing()
10472 || (char_avail() && !KeyTyped))
10473 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010474 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010475 return TRUE;
10476 }
10477 return FALSE;
10478}
10479
10480/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010481 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482 *
10483 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10484 * If clear_cmdline is FALSE there may be a message there that needs to be
10485 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010486 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 * Return the length of the message (0 if no message).
10488 */
10489 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010490showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 int need_clear;
10493 int length = 0;
10494 int do_mode;
10495 int attr;
10496 int nwr_save;
10497#ifdef FEAT_INS_EXPAND
10498 int sub_attr;
10499#endif
10500
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010501 do_mode = ((p_smd && msg_silent == 0)
10502 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010503 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010504 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010505 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010507 if (skip_showmode())
10508 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509
10510 nwr_save = need_wait_return;
10511
10512 /* wait a bit before overwriting an important message */
10513 check_for_delay(FALSE);
10514
10515 /* if the cmdline is more than one line high, erase top lines */
10516 need_clear = clear_cmdline;
10517 if (clear_cmdline && cmdline_row < Rows - 1)
10518 msg_clr_cmdline(); /* will reset clear_cmdline */
10519
10520 /* Position on the last line in the window, column 0 */
10521 msg_pos_mode();
10522 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010523 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (do_mode)
10525 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010526 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010528 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010529# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010530 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010531# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010532 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010533# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010534 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010535# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010536 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010538 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539# endif
10540#endif
10541#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10542 if (gui.in_use)
10543 {
10544 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010545 {
10546 /* HANGUL */
10547 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010548 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010549 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010550 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 }
10553#endif
10554#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010555 /* CTRL-X in Insert mode */
10556 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 {
10558 /* These messages can get long, avoid a wrap in a narrow
10559 * window. Prefer showing edit_submode_extra. */
10560 length = (Rows - msg_row) * Columns - 3;
10561 if (edit_submode_extra != NULL)
10562 length -= vim_strsize(edit_submode_extra);
10563 if (length > 0)
10564 {
10565 if (edit_submode_pre != NULL)
10566 length -= vim_strsize(edit_submode_pre);
10567 if (length - vim_strsize(edit_submode) > 0)
10568 {
10569 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010570 msg_puts_attr((char *)edit_submode_pre, attr);
10571 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 }
10573 if (edit_submode_extra != NULL)
10574 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010575 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010577 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 else
10579 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010580 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 }
10582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 }
10584 else
10585#endif
10586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010588 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010589 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010590 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 else if (State & INSERT)
10592 {
10593#ifdef FEAT_RIGHTLEFT
10594 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010595 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010597 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010599 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010600 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010602 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010604 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605#ifdef FEAT_RIGHTLEFT
10606 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010607 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608#endif
10609#ifdef FEAT_KEYMAP
10610 if (State & LANGMAP)
10611 {
10612# ifdef FEAT_ARABIC
10613 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010614 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 else
10616# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010617 if (get_keymap_str(curwin, (char_u *)" (%s)",
10618 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010619 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 }
10621#endif
10622 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010623 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 if (VIsual_active)
10626 {
10627 char *p;
10628
10629 /* Don't concatenate separate words to avoid translation
10630 * problems. */
10631 switch ((VIsual_select ? 4 : 0)
10632 + (VIsual_mode == Ctrl_V) * 2
10633 + (VIsual_mode == 'V'))
10634 {
10635 case 0: p = N_(" VISUAL"); break;
10636 case 1: p = N_(" VISUAL LINE"); break;
10637 case 2: p = N_(" VISUAL BLOCK"); break;
10638 case 4: p = N_(" SELECT"); break;
10639 case 5: p = N_(" SELECT LINE"); break;
10640 default: p = N_(" SELECT BLOCK"); break;
10641 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010642 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010644 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010646
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 need_clear = TRUE;
10648 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010649 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650#ifdef FEAT_INS_EXPAND
10651 && edit_submode == NULL /* otherwise it gets too long */
10652#endif
10653 )
10654 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010655 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 need_clear = TRUE;
10657 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010658
10659 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010660 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 msg_clr_eos();
10662 msg_didout = FALSE; /* overwrite this message */
10663 length = msg_col;
10664 msg_col = 0;
10665 need_wait_return = nwr_save; /* never ask for hit-return for this */
10666 }
10667 else if (clear_cmdline && msg_silent == 0)
10668 /* Clear the whole command line. Will reset "clear_cmdline". */
10669 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010670 else if (redraw_mode)
10671 {
10672 msg_pos_mode();
10673 msg_clr_eos();
10674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675
10676#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677 /* In Visual mode the size of the selected area must be redrawn. */
10678 if (VIsual_active)
10679 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680
10681 /* If the last window has no status line, the ruler is after the mode
10682 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010683 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010684 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685#endif
10686 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010687 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 clear_cmdline = FALSE;
10689
10690 return length;
10691}
10692
10693/*
10694 * Position for a mode message.
10695 */
10696 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010697msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
10699 msg_col = 0;
10700 msg_row = Rows - 1;
10701}
10702
10703/*
10704 * Delete mode message. Used when ESC is typed which is expected to end
10705 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010706 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 */
10708 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010709unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
10711 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010712 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 */
10714 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10715 redraw_cmdline = TRUE; /* delete mode later */
10716 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010717 clearmode();
10718}
10719
10720/*
10721 * Clear the mode message.
10722 */
10723 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010724clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010725{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010726 int save_msg_row = msg_row;
10727 int save_msg_col = msg_col;
10728
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010729 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010730 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010731 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010732 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010733
10734 msg_col = save_msg_col;
10735 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736}
10737
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010738 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010739recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010740{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010741 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010742 if (!shortmess(SHM_RECORDING))
10743 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010744 char s[4];
10745
10746 sprintf(s, " @%c", reg_recording);
10747 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010748 }
10749}
10750
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010751/*
10752 * Draw the tab pages line at the top of the Vim window.
10753 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010754 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010755draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010756{
10757 int tabcount = 0;
10758 tabpage_T *tp;
10759 int tabwidth;
10760 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010761 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010762 int attr;
10763 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010764 win_T *cwp;
10765 int wincount;
10766 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010767 int c;
10768 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010769 int attr_sel = HL_ATTR(HLF_TPS);
10770 int attr_nosel = HL_ATTR(HLF_TP);
10771 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010772 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010773 int room;
10774 int use_sep_chars = (t_colors < 8
10775#ifdef FEAT_GUI
10776 && !gui.in_use
10777#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010778#ifdef FEAT_TERMGUICOLORS
10779 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010780#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010781 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010782
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010783 if (ScreenLines == NULL)
10784 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010785 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010786
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010787#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010788 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010789 if (gui_use_tabline())
10790 {
10791 gui_update_tabline();
10792 return;
10793 }
10794#endif
10795
10796 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010797 return;
10798
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010799#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010800 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010801
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010802 /* Use the 'tabline' option if it's set. */
10803 if (*p_tal != NUL)
10804 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010805 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010806
10807 /* Check for an error. If there is one we would loop in redrawing the
10808 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010809 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010810 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010811 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010812 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010813 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010814 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010815 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010816 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010817#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010818 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010819 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010820 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010821
Bram Moolenaar238a5642006-02-21 22:12:05 +000010822 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10823 if (tabwidth < 6)
10824 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010825
Bram Moolenaar238a5642006-02-21 22:12:05 +000010826 attr = attr_nosel;
10827 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010828 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10829 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010830 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010831 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010832
Bram Moolenaar238a5642006-02-21 22:12:05 +000010833 if (tp->tp_topframe == topframe)
10834 attr = attr_sel;
10835 if (use_sep_chars && col > 0)
10836 screen_putchar('|', 0, col++, attr);
10837
10838 if (tp->tp_topframe != topframe)
10839 attr = attr_nosel;
10840
10841 screen_putchar(' ', 0, col++, attr);
10842
10843 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010844 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010845 cwp = curwin;
10846 wp = firstwin;
10847 }
10848 else
10849 {
10850 cwp = tp->tp_curwin;
10851 wp = tp->tp_firstwin;
10852 }
10853
10854 modified = FALSE;
10855 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10856 if (bufIsChanged(wp->w_buffer))
10857 modified = TRUE;
10858 if (modified || wincount > 1)
10859 {
10860 if (wincount > 1)
10861 {
10862 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010863 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010864 if (col + len >= Columns - 3)
10865 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010866 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010867#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010868 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010869#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010870 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010871#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010872 );
10873 col += len;
10874 }
10875 if (modified)
10876 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10877 screen_putchar(' ', 0, col++, attr);
10878 }
10879
10880 room = scol - col + tabwidth - 1;
10881 if (room > 0)
10882 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010883 /* Get buffer name in NameBuff[] */
10884 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010885 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010886 len = vim_strsize(NameBuff);
10887 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010888 if (has_mbyte)
10889 while (len > room)
10890 {
10891 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010892 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010893 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010894 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010895 {
10896 p += len - room;
10897 len = room;
10898 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010899 if (len > Columns - col - 1)
10900 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010902 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010903 col += len;
10904 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010905 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010906
10907 /* Store the tab page number in TabPageIdxs[], so that
10908 * jump_to_mouse() knows where each one is. */
10909 ++tabcount;
10910 while (scol < col)
10911 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010912 }
10913
Bram Moolenaar238a5642006-02-21 22:12:05 +000010914 if (use_sep_chars)
10915 c = '_';
10916 else
10917 c = ' ';
10918 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010919
10920 /* Put an "X" for closing the current tab if there are several. */
10921 if (first_tabpage->tp_next != NULL)
10922 {
10923 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10924 TabPageIdxs[Columns - 1] = -999;
10925 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010926 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010927
10928 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10929 * set. */
10930 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010931}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010932
10933/*
10934 * Get buffer name for "buf" into NameBuff[].
10935 * Takes care of special buffer names and translates special characters.
10936 */
10937 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010938get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010939{
10940 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010941 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010942 else
10943 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10944 trans_characters(NameBuff, MAXPATHL);
10945}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010946
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947/*
10948 * Get the character to use in a status line. Get its attributes in "*attr".
10949 */
10950 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010951fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
10953 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010954
10955#ifdef FEAT_TERMINAL
10956 if (bt_terminal(wp->w_buffer))
10957 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010958 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010959 {
10960 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010961 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010962 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010963 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010964 {
10965 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010966 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010967 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010968 }
10969 else
10970#endif
10971 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010973 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 fill = fill_stl;
10975 }
10976 else
10977 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010978 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 fill = fill_stlnc;
10980 }
10981 /* Use fill when there is highlighting, and highlighting of current
10982 * window differs, or the fillchars differ, or this is not the
10983 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010984 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010985 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 || (fill_stl != fill_stlnc)))
10987 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010988 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 return '^';
10990 return '=';
10991}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993/*
10994 * Get the character to use in a separator between vertically split windows.
10995 * Get its attributes in "*attr".
10996 */
10997 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010998fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999{
Bram Moolenaar8820b482017-03-16 17:23:31 +010011000 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 if (*attr == 0 && fill_vert == ' ')
11002 return '|';
11003 else
11004 return fill_vert;
11005}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006
11007/*
11008 * Return TRUE if redrawing should currently be done.
11009 */
11010 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011011redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011013#ifdef FEAT_EVAL
11014 if (disable_redraw_for_testing)
11015 return 0;
11016 else
11017#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011018 return ((!RedrawingDisabled
11019#ifdef FEAT_EVAL
11020 || ignore_redraw_flag_for_testing
11021#endif
11022 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023}
11024
11025/*
11026 * Return TRUE if printing messages should currently be done.
11027 */
11028 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011029messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030{
11031 return (!(p_lz && char_avail() && !KeyTyped));
11032}
11033
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011034#ifdef FEAT_MENU
11035/*
11036 * Draw the window toolbar.
11037 */
11038 static void
11039redraw_win_toolbar(win_T *wp)
11040{
11041 vimmenu_T *menu;
11042 int item_idx = 0;
11043 int item_count = 0;
11044 int col = 0;
11045 int next_col;
11046 int off = (int)(current_ScreenLine - ScreenLines);
11047 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11048 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11049
11050 vim_free(wp->w_winbar_items);
11051 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11052 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011053 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011054
11055 /* TODO: use fewer spaces if there is not enough room */
11056 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011057 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011058 {
11059 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011060 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011061 break;
11062 if (col > 1)
11063 {
11064 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011065 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011066 break;
11067 }
11068
11069 wp->w_winbar_items[item_idx].wb_startcol = col;
11070 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011071 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011072 break;
11073
11074 next_col = text_to_screenline(wp, menu->name, col);
11075 while (col < next_col)
11076 {
11077 ScreenAttrs[off + col] = button_attr;
11078 ++col;
11079 }
11080 wp->w_winbar_items[item_idx].wb_endcol = col;
11081 wp->w_winbar_items[item_idx].wb_menu = menu;
11082 ++item_idx;
11083
Bram Moolenaar02631462017-09-22 15:20:32 +020011084 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011085 break;
11086 space_to_screenline(off + col, button_attr);
11087 ++col;
11088 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011089 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011090 {
11091 space_to_screenline(off + col, fill_attr);
11092 ++col;
11093 }
11094 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11095
Bram Moolenaar02631462017-09-22 15:20:32 +020011096 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011097 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011098}
11099#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011100
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101/*
11102 * Show current status info in ruler and various other places
11103 * If always is FALSE, only show ruler if position has changed.
11104 */
11105 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011106showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107{
11108 if (!always && !redrawing())
11109 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011110#ifdef FEAT_INS_EXPAND
11111 if (pum_visible())
11112 {
11113 /* Don't redraw right now, do it later. */
11114 curwin->w_redr_status = TRUE;
11115 return;
11116 }
11117#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011118#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011119 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011120 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 else
11122#endif
11123#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011124 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125#endif
11126
11127#ifdef FEAT_TITLE
11128 if (need_maketitle
11129# ifdef FEAT_STL_OPT
11130 || (p_icon && (stl_syntax & STL_IN_ICON))
11131 || (p_title && (stl_syntax & STL_IN_TITLE))
11132# endif
11133 )
11134 maketitle();
11135#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011136 /* Redraw the tab pages line if needed. */
11137 if (redraw_tabline)
11138 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +020011139
11140#ifdef FEAT_TEXT_PROP
11141 // Display popup windows on top of everything.
11142 update_popups();
11143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144}
11145
11146#ifdef FEAT_CMDL_INFO
11147 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011148win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011150#define RULER_BUF_LEN 70
11151 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 int row;
11153 int fillchar;
11154 int attr;
11155 int empty_line = FALSE;
11156 colnr_T virtcol;
11157 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011158 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 int this_ru_col;
11161 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011162 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163
11164 /* If 'ruler' off or redrawing disabled, don't do anything */
11165 if (!p_ru)
11166 return;
11167
11168 /*
11169 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11170 * after deleting lines, before cursor.lnum is corrected.
11171 */
11172 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11173 return;
11174
11175#ifdef FEAT_INS_EXPAND
11176 /* Don't draw the ruler while doing insert-completion, it might overwrite
11177 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 if (edit_submode != NULL)
11180 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011181 // Don't draw the ruler when the popup menu is visible, it may overlap.
11182 // Except when the popup menu will be redrawn anyway.
11183 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011184 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185#endif
11186
11187#ifdef FEAT_STL_OPT
11188 if (*p_ruf)
11189 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011190 int save_called_emsg = called_emsg;
11191
11192 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011194 if (called_emsg)
11195 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011196 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011197 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 return;
11199 }
11200#endif
11201
11202 /*
11203 * Check if not in Insert mode and the line is empty (will show "0-1").
11204 */
11205 if (!(State & INSERT)
11206 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11207 empty_line = TRUE;
11208
11209 /*
11210 * Only draw the ruler when something changed.
11211 */
11212 validate_virtcol_win(wp);
11213 if ( redraw_cmdline
11214 || always
11215 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11216 || wp->w_cursor.col != wp->w_ru_cursor.col
11217 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 || wp->w_topline != wp->w_ru_topline
11220 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11221#ifdef FEAT_DIFF
11222 || wp->w_topfill != wp->w_ru_topfill
11223#endif
11224 || empty_line != wp->w_ru_empty)
11225 {
11226 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 if (wp->w_status_height)
11228 {
11229 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011230 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011231 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011232 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 }
11234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 {
11236 row = Rows - 1;
11237 fillchar = ' ';
11238 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 width = Columns;
11240 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242
11243 /* In list mode virtcol needs to be recomputed */
11244 virtcol = wp->w_virtcol;
11245 if (wp->w_p_list && lcs_tab1 == NUL)
11246 {
11247 wp->w_p_list = FALSE;
11248 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11249 wp->w_p_list = TRUE;
11250 }
11251
11252 /*
11253 * Some sprintfs return the length, some return a pointer.
11254 * To avoid portability problems we use strlen() here.
11255 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011256 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11258 ? 0L
11259 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011260 len = STRLEN(buffer);
11261 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11263 (int)virtcol + 1);
11264
11265 /*
11266 * Add a "50%" if there is room for it.
11267 * On the last line, don't print in the last column (scrolls the
11268 * screen up on some terminals).
11269 */
11270 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011271 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011272 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 this_ru_col = ru_col - (Columns - width);
11276 if (this_ru_col < 0)
11277 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278 /* Never use more than half the window/screen width, leave the other
11279 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011280 if (this_ru_col < (width + 1) / 2)
11281 this_ru_col = (width + 1) / 2;
11282 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011284 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011285 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 if (has_mbyte)
11288 i += (*mb_char2bytes)(fillchar, buffer + i);
11289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 buffer[i++] = fillchar;
11291 ++o;
11292 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011293 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 }
11295 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 if (has_mbyte)
11297 {
11298 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011299 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 {
11301 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011302 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 {
11304 buffer[i] = NUL;
11305 break;
11306 }
11307 }
11308 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011309 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011310 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
Bram Moolenaar4033c552017-09-16 20:54:51 +020011312 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 i = redraw_cmdline;
11314 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011315 this_ru_col + off + (int)STRLEN(buffer),
11316 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 fillchar, fillchar, attr);
11318 /* don't redraw the cmdline because of showing the ruler */
11319 redraw_cmdline = i;
11320 wp->w_ru_cursor = wp->w_cursor;
11321 wp->w_ru_virtcol = wp->w_virtcol;
11322 wp->w_ru_empty = empty_line;
11323 wp->w_ru_topline = wp->w_topline;
11324 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11325#ifdef FEAT_DIFF
11326 wp->w_ru_topfill = wp->w_topfill;
11327#endif
11328 }
11329}
11330#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011331
11332#if defined(FEAT_LINEBREAK) || defined(PROTO)
11333/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011334 * Return the width of the 'number' and 'relativenumber' column.
11335 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011336 * Otherwise it depends on 'numberwidth' and the line count.
11337 */
11338 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011339number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011340{
11341 int n;
11342 linenr_T lnum;
11343
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011344 if (wp->w_p_rnu && !wp->w_p_nu)
11345 /* cursor line shows "0" */
11346 lnum = wp->w_height;
11347 else
11348 /* cursor line shows absolute line number */
11349 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011350
Bram Moolenaar6b314672015-03-20 15:42:10 +010011351 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011352 return wp->w_nrwidth_width;
11353 wp->w_nrwidth_line_count = lnum;
11354
11355 n = 0;
11356 do
11357 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011358 lnum /= 10;
11359 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011360 } while (lnum > 0);
11361
11362 /* 'numberwidth' gives the minimal width plus one */
11363 if (n < wp->w_p_nuw - 1)
11364 n = wp->w_p_nuw - 1;
11365
11366 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011367 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011368 return n;
11369}
11370#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011371
Bram Moolenaar113e1072019-01-20 15:30:40 +010011372#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011373/*
11374 * Return the current cursor column. This is the actual position on the
11375 * screen. First column is 0.
11376 */
11377 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011378screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011379{
11380 return screen_cur_col;
11381}
11382
11383/*
11384 * Return the current cursor row. This is the actual position on the screen.
11385 * First row is 0.
11386 */
11387 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011388screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011389{
11390 return screen_cur_row;
11391}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011392#endif