blob: d72d7792cc64704a818614481dd795a7bb324b2f [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];
1044 int border_char[8] = {'-', '|', '-', '|', '+', '+', '+', '+', };
1045 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".
1083 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 }
1092 for (i = 0; i < 8; ++i)
1093 if (wp->w_border_char[i] != 0)
1094 border_char[i] = wp->w_border_char[i];
1095
1096 for (i = 0; i < 4; ++i)
1097 {
1098 border_attr[i] = popup_attr;
1099 if (wp->w_border_highlight[i] != NULL)
1100 border_attr[i] = syn_name2attr(wp->w_border_highlight[i]);
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001101 }
1102
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001103 if (wp->w_popup_border[0] > 0)
1104 {
1105 // top border
1106 screen_fill(wp->w_winrow, wp->w_winrow + 1,
1107 wp->w_wincol,
1108 wp->w_wincol + total_width,
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +02001109 wp->w_popup_border[3] != 0
Bram Moolenaar790498b2019-06-01 22:15:29 +02001110 ? border_char[4] : border_char[0],
1111 border_char[0], border_attr[0]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001112 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001113 {
1114 buf[mb_char2bytes(border_char[5], buf)] = NUL;
1115 screen_puts(buf, wp->w_winrow,
1116 wp->w_wincol + total_width - 1, border_attr[1]);
1117 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001118 }
1119
1120 if (wp->w_popup_padding[0] > 0)
1121 {
1122 // top padding
1123 row = wp->w_winrow + wp->w_popup_border[0];
1124 screen_fill(row, row + wp->w_popup_padding[0],
1125 wp->w_wincol + wp->w_popup_border[3],
1126 wp->w_wincol + total_width - wp->w_popup_border[1],
1127 ' ', ' ', popup_attr);
1128 }
1129
1130 for (row = wp->w_winrow + wp->w_popup_border[0];
1131 row < wp->w_winrow + total_height - wp->w_popup_border[2];
1132 ++row)
1133 {
1134 // left border
1135 if (wp->w_popup_border[3] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001136 {
1137 buf[mb_char2bytes(border_char[3], buf)] = NUL;
1138 screen_puts(buf, row, wp->w_wincol, border_attr[3]);
1139 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001140 // left padding
1141 if (wp->w_popup_padding[3] > 0)
1142 screen_puts(get_spaces(wp->w_popup_padding[3]), row,
1143 wp->w_wincol + wp->w_popup_border[3], popup_attr);
1144 // right border
1145 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001146 {
1147 buf[mb_char2bytes(border_char[1], buf)] = NUL;
1148 screen_puts(buf, row,
1149 wp->w_wincol + total_width - 1, border_attr[1]);
1150 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001151 // right padding
1152 if (wp->w_popup_padding[1] > 0)
1153 screen_puts(get_spaces(wp->w_popup_padding[1]), row,
1154 wp->w_wincol + wp->w_popup_border[3]
1155 + wp->w_popup_padding[3] + wp->w_width, popup_attr);
1156 }
1157
1158 if (wp->w_popup_padding[2] > 0)
1159 {
1160 // bottom padding
1161 row = wp->w_winrow + wp->w_popup_border[0]
1162 + wp->w_popup_padding[0] + wp->w_height;
1163 screen_fill(row, row + wp->w_popup_padding[2],
1164 wp->w_wincol + wp->w_popup_border[3],
1165 wp->w_wincol + total_width - wp->w_popup_border[1],
1166 ' ', ' ', popup_attr);
1167 }
1168
1169 if (wp->w_popup_border[2] > 0)
1170 {
1171 // bottom border
1172 row = wp->w_winrow + total_height - 1;
1173 screen_fill(row , row + 1,
1174 wp->w_wincol,
1175 wp->w_wincol + total_width,
Bram Moolenaar790498b2019-06-01 22:15:29 +02001176 wp->w_popup_border[3] != 0
1177 ? border_char[7] : border_char[2],
1178 border_char[2], border_attr[2]);
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001179 if (wp->w_popup_border[1] > 0)
Bram Moolenaar790498b2019-06-01 22:15:29 +02001180 {
1181 buf[mb_char2bytes(border_char[6], buf)] = NUL;
1182 screen_puts(buf, row,
1183 wp->w_wincol + total_width - 1, border_attr[2]);
1184 }
Bram Moolenaar2fd8e352019-06-01 20:16:48 +02001185 }
1186 }
1187}
1188#endif
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190#if defined(FEAT_GUI) || defined(PROTO)
1191/*
1192 * Update a single window, its status line and maybe the command line msg.
1193 * Used for the GUI scrollbar.
1194 */
1195 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001196updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001198 /* return if already busy updating */
1199 if (updating_screen)
1200 return;
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 update_prepare();
1203
1204#ifdef FEAT_CLIPBOARD
1205 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001206 if (clip_star.available && clip_isautosel_star())
1207 clip_update_selection(&clip_star);
1208 if (clip_plus.available && clip_isautosel_plus())
1209 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001213
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001214 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001215 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001216 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (wp->w_redr_status
1219# ifdef FEAT_CMDL_INFO
1220 || p_ru
1221# endif
1222# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001223 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224# endif
1225 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001226 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
Bram Moolenaar988c4332019-06-02 14:12:11 +02001228#ifdef FEAT_TEXT_PROP
1229 // Display popup windows on top of everything.
1230 update_popups();
1231#endif
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 update_finish();
1234}
1235#endif
1236
1237/*
1238 * Update a single window.
1239 *
1240 * This may cause the windows below it also to be redrawn (when clearing the
1241 * screen or scrolling lines).
1242 *
1243 * How the window is redrawn depends on wp->w_redr_type. Each type also
1244 * implies the one below it.
1245 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001246 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1248 * INVERTED redraw the changed part of the Visual area
1249 * INVERTED_ALL redraw the whole Visual area
1250 * VALID 1. scroll up/down to adjust for a changed w_topline
1251 * 2. update lines at the top when scrolled down
1252 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001253 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 * b_mod_top and b_mod_bot.
1255 * - if wp->w_redraw_top non-zero, redraw lines between
1256 * wp->w_redraw_top and wp->w_redr_bot.
1257 * - continue redrawing when syntax status is invalid.
1258 * 4. if scrolled up, update lines at the bottom.
1259 * This results in three areas that may need updating:
1260 * top: from first row to top_end (when scrolled down)
1261 * mid: from mid_start to mid_end (update inversion or changed text)
1262 * bot: from bot_start to last row (when scrolled up)
1263 */
1264 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001265win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 buf_T *buf = wp->w_buffer;
1268 int type;
1269 int top_end = 0; /* Below last row of the top area that needs
1270 updating. 0 when no top area updating. */
1271 int mid_start = 999;/* first row of the mid area that needs
1272 updating. 999 when no mid area updating. */
1273 int mid_end = 0; /* Below last row of the mid area that needs
1274 updating. 0 when no mid area updating. */
1275 int bot_start = 999;/* first row of the bot area that needs
1276 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 int scrolled_down = FALSE; /* TRUE when scrolled down when
1278 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001280 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 int top_to_mod = FALSE; /* redraw above mod_top */
1282#endif
1283
1284 int row; /* current window row to display */
1285 linenr_T lnum; /* current buffer lnum to display */
1286 int idx; /* current index in w_lines[] */
1287 int srow; /* starting row of the current line */
1288
1289 int eof = FALSE; /* if TRUE, we hit the end of the file */
1290 int didline = FALSE; /* if TRUE, we finished the last line */
1291 int i;
1292 long j;
1293 static int recursive = FALSE; /* being called recursively */
1294 int old_botline = wp->w_botline;
1295#ifdef FEAT_FOLDING
1296 long fold_count;
1297#endif
1298#ifdef FEAT_SYN_HL
1299 /* remember what happened to the previous line, to know if
1300 * check_visual_highlight() can be used */
1301#define DID_NONE 1 /* didn't update a line */
1302#define DID_LINE 2 /* updated a normal line */
1303#define DID_FOLD 3 /* updated a folded line */
1304 int did_update = DID_NONE;
1305 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1306#endif
1307 linenr_T mod_top = 0;
1308 linenr_T mod_bot = 0;
1309#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1310 int save_got_int;
1311#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001312#ifdef SYN_TIME_LIMIT
1313 proftime_T syntax_tm;
1314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316 type = wp->w_redr_type;
1317
1318 if (type == NOT_VALID)
1319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 wp->w_lines_valid = 0;
1322 }
1323
1324 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001325 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
1327 wp->w_redr_type = 0;
1328 return;
1329 }
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 /* Window is zero-width: Only need to draw the separator. */
1332 if (wp->w_width == 0)
1333 {
1334 /* draw the vertical separator right of this window */
1335 draw_vsep_win(wp, 0);
1336 wp->w_redr_type = 0;
1337 return;
1338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001340#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001341 // If this window contains a terminal, redraw works completely differently.
1342 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001343 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001344 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001345# ifdef FEAT_MENU
1346 /* Draw the window toolbar, if there is one. */
1347 if (winbar_height(wp) > 0)
1348 redraw_win_toolbar(wp);
1349# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001350 wp->w_redr_type = 0;
1351 return;
1352 }
1353#endif
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001356 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#endif
1358
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001359#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001360 /* Force redraw when width of 'number' or 'relativenumber' column
1361 * changes. */
1362 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001363 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001364 {
1365 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001366 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001367 }
1368 else
1369#endif
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1372 {
1373 /*
1374 * When there are both inserted/deleted lines and specific lines to be
1375 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1376 * everything (only happens when redrawing is off for while).
1377 */
1378 type = NOT_VALID;
1379 }
1380 else
1381 {
1382 /*
1383 * Set mod_top to the first line that needs displaying because of
1384 * changes. Set mod_bot to the first line after the changes.
1385 */
1386 mod_top = wp->w_redraw_top;
1387 if (wp->w_redraw_bot != 0)
1388 mod_bot = wp->w_redraw_bot + 1;
1389 else
1390 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (buf->b_mod_set)
1392 {
1393 if (mod_top == 0 || mod_top > buf->b_mod_top)
1394 {
1395 mod_top = buf->b_mod_top;
1396#ifdef FEAT_SYN_HL
1397 /* Need to redraw lines above the change that may be included
1398 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001399 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001401 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (mod_top < 1)
1403 mod_top = 1;
1404 }
1405#endif
1406 }
1407 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1408 mod_bot = buf->b_mod_bot;
1409
1410#ifdef FEAT_SEARCH_EXTRA
1411 /* When 'hlsearch' is on and using a multi-line search pattern, a
1412 * change in one line may make the Search highlighting in a
1413 * previous line invalid. Simple solution: redraw all visible
1414 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001415 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001417 if (search_hl.rm.regprog != NULL
1418 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001420 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001421 {
1422 cur = wp->w_match_head;
1423 while (cur != NULL)
1424 {
1425 if (cur->match.regprog != NULL
1426 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001427 {
1428 top_to_mod = TRUE;
1429 break;
1430 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001431 cur = cur->next;
1432 }
1433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434#endif
1435 }
1436#ifdef FEAT_FOLDING
1437 if (mod_top != 0 && hasAnyFolding(wp))
1438 {
1439 linenr_T lnumt, lnumb;
1440
1441 /*
1442 * A change in a line can cause lines above it to become folded or
1443 * unfolded. Find the top most buffer line that may be affected.
1444 * If the line was previously folded and displayed, get the first
1445 * line of that fold. If the line is folded now, get the first
1446 * folded line. Use the minimum of these two.
1447 */
1448
1449 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1450 * the line below it. If there is no valid entry, use w_topline.
1451 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1452 * to this line. If there is no valid entry, use MAXLNUM. */
1453 lnumt = wp->w_topline;
1454 lnumb = MAXLNUM;
1455 for (i = 0; i < wp->w_lines_valid; ++i)
1456 if (wp->w_lines[i].wl_valid)
1457 {
1458 if (wp->w_lines[i].wl_lastlnum < mod_top)
1459 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1460 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1461 {
1462 lnumb = wp->w_lines[i].wl_lnum;
1463 /* When there is a fold column it might need updating
1464 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001465 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 ++lnumb;
1467 }
1468 }
1469
1470 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1471 if (mod_top > lnumt)
1472 mod_top = lnumt;
1473
1474 /* Now do the same for the bottom line (one above mod_bot). */
1475 --mod_bot;
1476 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1477 ++mod_bot;
1478 if (mod_bot < lnumb)
1479 mod_bot = lnumb;
1480 }
1481#endif
1482
1483 /* When a change starts above w_topline and the end is below
1484 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001485 * If the end of the change is above w_topline: do like no change was
1486 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (mod_top != 0 && mod_top < wp->w_topline)
1488 {
1489 if (mod_bot > wp->w_topline)
1490 mod_top = wp->w_topline;
1491#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001492 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 top_end = 1;
1494#endif
1495 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001496
1497 /* When line numbers are displayed need to redraw all lines below
1498 * inserted/deleted lines. */
1499 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1500 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001502 wp->w_redraw_top = 0; // reset for next time
1503 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 /*
1506 * When only displaying the lines at the top, set top_end. Used when
1507 * window has scrolled down for msg_scrolled.
1508 */
1509 if (type == REDRAW_TOP)
1510 {
1511 j = 0;
1512 for (i = 0; i < wp->w_lines_valid; ++i)
1513 {
1514 j += wp->w_lines[i].wl_size;
1515 if (j >= wp->w_upd_rows)
1516 {
1517 top_end = j;
1518 break;
1519 }
1520 }
1521 if (top_end == 0)
1522 /* not found (cannot happen?): redraw everything */
1523 type = NOT_VALID;
1524 else
1525 /* top area defined, the rest is VALID */
1526 type = VALID;
1527 }
1528
Bram Moolenaar367329b2007-08-30 11:53:22 +00001529 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001530 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1531 * non-zero and thus not FALSE) will indicate that screenclear() was not
1532 * called. */
1533 if (screen_cleared)
1534 screen_cleared = MAYBE;
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /*
1537 * If there are no changes on the screen that require a complete redraw,
1538 * handle three cases:
1539 * 1: we are off the top of the screen by a few lines: scroll down
1540 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1541 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1542 * w_lines[] that needs updating.
1543 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001544 if ((type == VALID || type == SOME_VALID
1545 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546#ifdef FEAT_DIFF
1547 && !wp->w_botfill && !wp->w_old_botfill
1548#endif
1549 )
1550 {
1551 if (mod_top != 0 && wp->w_topline == mod_top)
1552 {
1553 /*
1554 * w_topline is the first changed line, the scrolling will be done
1555 * further down.
1556 */
1557 }
1558 else if (wp->w_lines[0].wl_valid
1559 && (wp->w_topline < wp->w_lines[0].wl_lnum
1560#ifdef FEAT_DIFF
1561 || (wp->w_topline == wp->w_lines[0].wl_lnum
1562 && wp->w_topfill > wp->w_old_topfill)
1563#endif
1564 ))
1565 {
1566 /*
1567 * New topline is above old topline: May scroll down.
1568 */
1569#ifdef FEAT_FOLDING
1570 if (hasAnyFolding(wp))
1571 {
1572 linenr_T ln;
1573
1574 /* count the number of lines we are off, counting a sequence
1575 * of folded lines as one */
1576 j = 0;
1577 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1578 {
1579 ++j;
1580 if (j >= wp->w_height - 2)
1581 break;
1582 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1583 }
1584 }
1585 else
1586#endif
1587 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1588 if (j < wp->w_height - 2) /* not too far off */
1589 {
1590 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1591#ifdef FEAT_DIFF
1592 /* insert extra lines for previously invisible filler lines */
1593 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1594 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1595 - wp->w_old_topfill;
1596#endif
1597 if (i < wp->w_height - 2) /* less than a screen off */
1598 {
1599 /*
1600 * Try to insert the correct number of lines.
1601 * If not the last window, delete the lines at the bottom.
1602 * win_ins_lines may fail when the terminal can't do it.
1603 */
1604 if (i > 0)
1605 check_for_delay(FALSE);
1606 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1607 {
1608 if (wp->w_lines_valid != 0)
1609 {
1610 /* Need to update rows that are new, stop at the
1611 * first one that scrolled down. */
1612 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 /* Move the entries that were scrolled, disable
1616 * the entries for the lines to be redrawn. */
1617 if ((wp->w_lines_valid += j) > wp->w_height)
1618 wp->w_lines_valid = wp->w_height;
1619 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1620 wp->w_lines[idx] = wp->w_lines[idx - j];
1621 while (idx >= 0)
1622 wp->w_lines[idx--].wl_valid = FALSE;
1623 }
1624 }
1625 else
1626 mid_start = 0; /* redraw all lines */
1627 }
1628 else
1629 mid_start = 0; /* redraw all lines */
1630 }
1631 else
1632 mid_start = 0; /* redraw all lines */
1633 }
1634 else
1635 {
1636 /*
1637 * New topline is at or below old topline: May scroll up.
1638 * When topline didn't change, find first entry in w_lines[] that
1639 * needs updating.
1640 */
1641
1642 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1643 j = -1;
1644 row = 0;
1645 for (i = 0; i < wp->w_lines_valid; i++)
1646 {
1647 if (wp->w_lines[i].wl_valid
1648 && wp->w_lines[i].wl_lnum == wp->w_topline)
1649 {
1650 j = i;
1651 break;
1652 }
1653 row += wp->w_lines[i].wl_size;
1654 }
1655 if (j == -1)
1656 {
1657 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1658 * lines */
1659 mid_start = 0;
1660 }
1661 else
1662 {
1663 /*
1664 * Try to delete the correct number of lines.
1665 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1666 */
1667#ifdef FEAT_DIFF
1668 /* If the topline didn't change, delete old filler lines,
1669 * otherwise delete filler lines of the new topline... */
1670 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1671 row += wp->w_old_topfill;
1672 else
1673 row += diff_check_fill(wp, wp->w_topline);
1674 /* ... but don't delete new filler lines. */
1675 row -= wp->w_topfill;
1676#endif
1677 if (row > 0)
1678 {
1679 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001680 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1681 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 bot_start = wp->w_height - row;
1683 else
1684 mid_start = 0; /* redraw all lines */
1685 }
1686 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1687 {
1688 /*
1689 * Skip the lines (below the deleted lines) that are still
1690 * valid and don't need redrawing. Copy their info
1691 * upwards, to compensate for the deleted lines. Set
1692 * bot_start to the first row that needs redrawing.
1693 */
1694 bot_start = 0;
1695 idx = 0;
1696 for (;;)
1697 {
1698 wp->w_lines[idx] = wp->w_lines[j];
1699 /* stop at line that didn't fit, unless it is still
1700 * valid (no lines deleted) */
1701 if (row > 0 && bot_start + row
1702 + (int)wp->w_lines[j].wl_size > wp->w_height)
1703 {
1704 wp->w_lines_valid = idx + 1;
1705 break;
1706 }
1707 bot_start += wp->w_lines[idx++].wl_size;
1708
1709 /* stop at the last valid entry in w_lines[].wl_size */
1710 if (++j >= wp->w_lines_valid)
1711 {
1712 wp->w_lines_valid = idx;
1713 break;
1714 }
1715 }
1716#ifdef FEAT_DIFF
1717 /* Correct the first entry for filler lines at the top
1718 * when it won't get updated below. */
1719 if (wp->w_p_diff && bot_start > 0)
1720 wp->w_lines[0].wl_size =
1721 plines_win_nofill(wp, wp->w_topline, TRUE)
1722 + wp->w_topfill;
1723#endif
1724 }
1725 }
1726 }
1727
1728 /* When starting redraw in the first line, redraw all lines. When
1729 * there is only one window it's probably faster to clear the screen
1730 * first. */
1731 if (mid_start == 0)
1732 {
1733 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001734 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001735 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001736 /* Clear the screen when it was not done by win_del_lines() or
1737 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1738 * then. */
1739 if (screen_cleared != TRUE)
1740 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001741 /* The screen was cleared, redraw the tab pages line. */
1742 if (redraw_tabline)
1743 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001746
1747 /* When win_del_lines() or win_ins_lines() caused the screen to be
1748 * cleared (only happens for the first window) or when screenclear()
1749 * was called directly above, "must_redraw" will have been set to
1750 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1751 if (screen_cleared == TRUE)
1752 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
1754 else
1755 {
1756 /* Not VALID or INVERTED: redraw all lines. */
1757 mid_start = 0;
1758 mid_end = wp->w_height;
1759 }
1760
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001761 if (type == SOME_VALID)
1762 {
1763 /* SOME_VALID: redraw all lines. */
1764 mid_start = 0;
1765 mid_end = wp->w_height;
1766 type = NOT_VALID;
1767 }
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 /* check if we are updating or removing the inverted part */
1770 if ((VIsual_active && buf == curwin->w_buffer)
1771 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1772 {
1773 linenr_T from, to;
1774
1775 if (VIsual_active)
1776 {
1777 if (VIsual_active
1778 && (VIsual_mode != wp->w_old_visual_mode
1779 || type == INVERTED_ALL))
1780 {
1781 /*
1782 * If the type of Visual selection changed, redraw the whole
1783 * selection. Also when the ownership of the X selection is
1784 * gained or lost.
1785 */
1786 if (curwin->w_cursor.lnum < VIsual.lnum)
1787 {
1788 from = curwin->w_cursor.lnum;
1789 to = VIsual.lnum;
1790 }
1791 else
1792 {
1793 from = VIsual.lnum;
1794 to = curwin->w_cursor.lnum;
1795 }
1796 /* redraw more when the cursor moved as well */
1797 if (wp->w_old_cursor_lnum < from)
1798 from = wp->w_old_cursor_lnum;
1799 if (wp->w_old_cursor_lnum > to)
1800 to = wp->w_old_cursor_lnum;
1801 if (wp->w_old_visual_lnum < from)
1802 from = wp->w_old_visual_lnum;
1803 if (wp->w_old_visual_lnum > to)
1804 to = wp->w_old_visual_lnum;
1805 }
1806 else
1807 {
1808 /*
1809 * Find the line numbers that need to be updated: The lines
1810 * between the old cursor position and the current cursor
1811 * position. Also check if the Visual position changed.
1812 */
1813 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1814 {
1815 from = curwin->w_cursor.lnum;
1816 to = wp->w_old_cursor_lnum;
1817 }
1818 else
1819 {
1820 from = wp->w_old_cursor_lnum;
1821 to = curwin->w_cursor.lnum;
1822 if (from == 0) /* Visual mode just started */
1823 from = to;
1824 }
1825
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001826 if (VIsual.lnum != wp->w_old_visual_lnum
1827 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 if (wp->w_old_visual_lnum < from
1830 && wp->w_old_visual_lnum != 0)
1831 from = wp->w_old_visual_lnum;
1832 if (wp->w_old_visual_lnum > to)
1833 to = wp->w_old_visual_lnum;
1834 if (VIsual.lnum < from)
1835 from = VIsual.lnum;
1836 if (VIsual.lnum > to)
1837 to = VIsual.lnum;
1838 }
1839 }
1840
1841 /*
1842 * If in block mode and changed column or curwin->w_curswant:
1843 * update all lines.
1844 * First compute the actual start and end column.
1845 */
1846 if (VIsual_mode == Ctrl_V)
1847 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001848 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001849#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001850 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar404406a2014-10-09 13:24:43 +02001852 if (curwin->w_p_lbr)
1853 ve_flags = VE_ALL;
1854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001856#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001857 ve_flags = save_ve_flags;
1858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 ++toc;
1860 if (curwin->w_curswant == MAXCOL)
1861 toc = MAXCOL;
1862
1863 if (fromc != wp->w_old_cursor_fcol
1864 || toc != wp->w_old_cursor_lcol)
1865 {
1866 if (from > VIsual.lnum)
1867 from = VIsual.lnum;
1868 if (to < VIsual.lnum)
1869 to = VIsual.lnum;
1870 }
1871 wp->w_old_cursor_fcol = fromc;
1872 wp->w_old_cursor_lcol = toc;
1873 }
1874 }
1875 else
1876 {
1877 /* Use the line numbers of the old Visual area. */
1878 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1879 {
1880 from = wp->w_old_cursor_lnum;
1881 to = wp->w_old_visual_lnum;
1882 }
1883 else
1884 {
1885 from = wp->w_old_visual_lnum;
1886 to = wp->w_old_cursor_lnum;
1887 }
1888 }
1889
1890 /*
1891 * There is no need to update lines above the top of the window.
1892 */
1893 if (from < wp->w_topline)
1894 from = wp->w_topline;
1895
1896 /*
1897 * If we know the value of w_botline, use it to restrict the update to
1898 * the lines that are visible in the window.
1899 */
1900 if (wp->w_valid & VALID_BOTLINE)
1901 {
1902 if (from >= wp->w_botline)
1903 from = wp->w_botline - 1;
1904 if (to >= wp->w_botline)
1905 to = wp->w_botline - 1;
1906 }
1907
1908 /*
1909 * Find the minimal part to be updated.
1910 * Watch out for scrolling that made entries in w_lines[] invalid.
1911 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1912 * top_end; need to redraw from top_end to the "to" line.
1913 * A middle mouse click with a Visual selection may change the text
1914 * above the Visual area and reset wl_valid, do count these for
1915 * mid_end (in srow).
1916 */
1917 if (mid_start > 0)
1918 {
1919 lnum = wp->w_topline;
1920 idx = 0;
1921 srow = 0;
1922 if (scrolled_down)
1923 mid_start = top_end;
1924 else
1925 mid_start = 0;
1926 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1927 {
1928 if (wp->w_lines[idx].wl_valid)
1929 mid_start += wp->w_lines[idx].wl_size;
1930 else if (!scrolled_down)
1931 srow += wp->w_lines[idx].wl_size;
1932 ++idx;
1933# ifdef FEAT_FOLDING
1934 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1935 lnum = wp->w_lines[idx].wl_lnum;
1936 else
1937# endif
1938 ++lnum;
1939 }
1940 srow += mid_start;
1941 mid_end = wp->w_height;
1942 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1943 {
1944 if (wp->w_lines[idx].wl_valid
1945 && wp->w_lines[idx].wl_lnum >= to + 1)
1946 {
1947 /* Only update until first row of this line */
1948 mid_end = srow;
1949 break;
1950 }
1951 srow += wp->w_lines[idx].wl_size;
1952 }
1953 }
1954 }
1955
1956 if (VIsual_active && buf == curwin->w_buffer)
1957 {
1958 wp->w_old_visual_mode = VIsual_mode;
1959 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1960 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001961 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 wp->w_old_curswant = curwin->w_curswant;
1963 }
1964 else
1965 {
1966 wp->w_old_visual_mode = 0;
1967 wp->w_old_cursor_lnum = 0;
1968 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001969 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1973 /* reset got_int, otherwise regexp won't work */
1974 save_got_int = got_int;
1975 got_int = 0;
1976#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001977#ifdef SYN_TIME_LIMIT
1978 /* Set the time limit to 'redrawtime'. */
1979 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001980 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#ifdef FEAT_FOLDING
1983 win_foldinfo.fi_level = 0;
1984#endif
1985
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001986#ifdef FEAT_MENU
1987 /*
1988 * Draw the window toolbar, if there is one.
1989 * TODO: only when needed.
1990 */
1991 if (winbar_height(wp) > 0)
1992 redraw_win_toolbar(wp);
1993#endif
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 /*
1996 * Update all the window rows.
1997 */
1998 idx = 0; /* first entry in w_lines[].wl_size */
1999 row = 0;
2000 srow = 0;
2001 lnum = wp->w_topline; /* first line shown in window */
2002 for (;;)
2003 {
2004 /* stop updating when reached the end of the window (check for _past_
2005 * the end of the window is at the end of the loop) */
2006 if (row == wp->w_height)
2007 {
2008 didline = TRUE;
2009 break;
2010 }
2011
2012 /* stop updating when hit the end of the file */
2013 if (lnum > buf->b_ml.ml_line_count)
2014 {
2015 eof = TRUE;
2016 break;
2017 }
2018
2019 /* Remember the starting row of the line that is going to be dealt
2020 * with. It is used further down when the line doesn't fit. */
2021 srow = row;
2022
2023 /*
2024 * Update a line when it is in an area that needs updating, when it
2025 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002026 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 * win_del_lines(), check if the current line includes it.
2028 * When syntax folding is being used, the saved syntax states will
2029 * already have been updated, we can't see where the syntax state is
2030 * the same again, just update until the end of the window.
2031 */
2032 if (row < top_end
2033 || (row >= mid_start && row < mid_end)
2034#ifdef FEAT_SEARCH_EXTRA
2035 || top_to_mod
2036#endif
2037 || idx >= wp->w_lines_valid
2038 || (row + wp->w_lines[idx].wl_size > bot_start)
2039 || (mod_top != 0
2040 && (lnum == mod_top
2041 || (lnum >= mod_top
2042 && (lnum < mod_bot
2043#ifdef FEAT_SYN_HL
2044 || did_update == DID_FOLD
2045 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02002046 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 && (
2048# ifdef FEAT_FOLDING
2049 (foldmethodIsSyntax(wp)
2050 && hasAnyFolding(wp)) ||
2051# endif
2052 syntax_check_changed(lnum)))
2053#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002054#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02002055 /* match in fixed position might need redraw
2056 * if lines were inserted or deleted */
2057 || (wp->w_match_head != NULL
2058 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02002059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 )))))
2061 {
2062#ifdef FEAT_SEARCH_EXTRA
2063 if (lnum == mod_top)
2064 top_to_mod = FALSE;
2065#endif
2066
2067 /*
2068 * When at start of changed lines: May scroll following lines
2069 * up or down to minimize redrawing.
2070 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002071 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 */
2073 if (lnum == mod_top
2074 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002075 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
2077 int old_rows = 0;
2078 int new_rows = 0;
2079 int xtra_rows;
2080 linenr_T l;
2081
2082 /* Count the old number of window rows, using w_lines[], which
2083 * should still contain the sizes for the lines as they are
2084 * currently displayed. */
2085 for (i = idx; i < wp->w_lines_valid; ++i)
2086 {
2087 /* Only valid lines have a meaningful wl_lnum. Invalid
2088 * lines are part of the changed area. */
2089 if (wp->w_lines[i].wl_valid
2090 && wp->w_lines[i].wl_lnum == mod_bot)
2091 break;
2092 old_rows += wp->w_lines[i].wl_size;
2093#ifdef FEAT_FOLDING
2094 if (wp->w_lines[i].wl_valid
2095 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
2096 {
2097 /* Must have found the last valid entry above mod_bot.
2098 * Add following invalid entries. */
2099 ++i;
2100 while (i < wp->w_lines_valid
2101 && !wp->w_lines[i].wl_valid)
2102 old_rows += wp->w_lines[i++].wl_size;
2103 break;
2104 }
2105#endif
2106 }
2107
2108 if (i >= wp->w_lines_valid)
2109 {
2110 /* We can't find a valid line below the changed lines,
2111 * need to redraw until the end of the window.
2112 * Inserting/deleting lines has no use. */
2113 bot_start = 0;
2114 }
2115 else
2116 {
2117 /* Able to count old number of rows: Count new window
2118 * rows, and may insert/delete lines */
2119 j = idx;
2120 for (l = lnum; l < mod_bot; ++l)
2121 {
2122#ifdef FEAT_FOLDING
2123 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
2124 ++new_rows;
2125 else
2126#endif
2127#ifdef FEAT_DIFF
2128 if (l == wp->w_topline)
2129 new_rows += plines_win_nofill(wp, l, TRUE)
2130 + wp->w_topfill;
2131 else
2132#endif
2133 new_rows += plines_win(wp, l, TRUE);
2134 ++j;
2135 if (new_rows > wp->w_height - row - 2)
2136 {
2137 /* it's getting too much, must redraw the rest */
2138 new_rows = 9999;
2139 break;
2140 }
2141 }
2142 xtra_rows = new_rows - old_rows;
2143 if (xtra_rows < 0)
2144 {
2145 /* May scroll text up. If there is not enough
2146 * remaining text or scrolling fails, must redraw the
2147 * rest. If scrolling works, must redraw the text
2148 * below the scrolled text. */
2149 if (row - xtra_rows >= wp->w_height - 2)
2150 mod_bot = MAXLNUM;
2151 else
2152 {
2153 check_for_delay(FALSE);
2154 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002155 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 mod_bot = MAXLNUM;
2157 else
2158 bot_start = wp->w_height + xtra_rows;
2159 }
2160 }
2161 else if (xtra_rows > 0)
2162 {
2163 /* May scroll text down. If there is not enough
2164 * remaining text of scrolling fails, must redraw the
2165 * rest. */
2166 if (row + xtra_rows >= wp->w_height - 2)
2167 mod_bot = MAXLNUM;
2168 else
2169 {
2170 check_for_delay(FALSE);
2171 if (win_ins_lines(wp, row + old_rows,
2172 xtra_rows, FALSE, FALSE) == FAIL)
2173 mod_bot = MAXLNUM;
2174 else if (top_end > row + old_rows)
2175 /* Scrolled the part at the top that requires
2176 * updating down. */
2177 top_end += xtra_rows;
2178 }
2179 }
2180
2181 /* When not updating the rest, may need to move w_lines[]
2182 * entries. */
2183 if (mod_bot != MAXLNUM && i != j)
2184 {
2185 if (j < i)
2186 {
2187 int x = row + new_rows;
2188
2189 /* move entries in w_lines[] upwards */
2190 for (;;)
2191 {
2192 /* stop at last valid entry in w_lines[] */
2193 if (i >= wp->w_lines_valid)
2194 {
2195 wp->w_lines_valid = j;
2196 break;
2197 }
2198 wp->w_lines[j] = wp->w_lines[i];
2199 /* stop at a line that won't fit */
2200 if (x + (int)wp->w_lines[j].wl_size
2201 > wp->w_height)
2202 {
2203 wp->w_lines_valid = j + 1;
2204 break;
2205 }
2206 x += wp->w_lines[j++].wl_size;
2207 ++i;
2208 }
2209 if (bot_start > x)
2210 bot_start = x;
2211 }
2212 else /* j > i */
2213 {
2214 /* move entries in w_lines[] downwards */
2215 j -= i;
2216 wp->w_lines_valid += j;
2217 if (wp->w_lines_valid > wp->w_height)
2218 wp->w_lines_valid = wp->w_height;
2219 for (i = wp->w_lines_valid; i - j >= idx; --i)
2220 wp->w_lines[i] = wp->w_lines[i - j];
2221
2222 /* The w_lines[] entries for inserted lines are
2223 * now invalid, but wl_size may be used above.
2224 * Reset to zero. */
2225 while (i >= idx)
2226 {
2227 wp->w_lines[i].wl_size = 0;
2228 wp->w_lines[i--].wl_valid = FALSE;
2229 }
2230 }
2231 }
2232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
2235#ifdef FEAT_FOLDING
2236 /*
2237 * When lines are folded, display one line for all of them.
2238 * Otherwise, display normally (can be several display lines when
2239 * 'wrap' is on).
2240 */
2241 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2242 if (fold_count != 0)
2243 {
2244 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2245 ++row;
2246 --fold_count;
2247 wp->w_lines[idx].wl_folded = TRUE;
2248 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2249# ifdef FEAT_SYN_HL
2250 did_update = DID_FOLD;
2251# endif
2252 }
2253 else
2254#endif
2255 if (idx < wp->w_lines_valid
2256 && wp->w_lines[idx].wl_valid
2257 && wp->w_lines[idx].wl_lnum == lnum
2258 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002259 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar17146962019-05-30 00:12:11 +02002260#ifdef FEAT_TEXT_PROP
2261 && !bt_popup(wp->w_buffer)
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 && srow + wp->w_lines[idx].wl_size > wp->w_height
2264#ifdef FEAT_DIFF
2265 && diff_check_fill(wp, lnum) == 0
2266#endif
2267 )
2268 {
2269 /* This line is not going to fit. Don't draw anything here,
2270 * will draw "@ " lines below. */
2271 row = wp->w_height + 1;
2272 }
2273 else
2274 {
2275#ifdef FEAT_SEARCH_EXTRA
2276 prepare_search_hl(wp, lnum);
2277#endif
2278#ifdef FEAT_SYN_HL
2279 /* Let the syntax stuff know we skipped a few lines. */
2280 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002281 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 syntax_end_parsing(syntax_last_parsed + 1);
2283#endif
2284
2285 /*
2286 * Display one line.
2287 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002288 row = win_line(wp, lnum, srow, wp->w_height,
2289 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291#ifdef FEAT_FOLDING
2292 wp->w_lines[idx].wl_folded = FALSE;
2293 wp->w_lines[idx].wl_lastlnum = lnum;
2294#endif
2295#ifdef FEAT_SYN_HL
2296 did_update = DID_LINE;
2297 syntax_last_parsed = lnum;
2298#endif
2299 }
2300
2301 wp->w_lines[idx].wl_lnum = lnum;
2302 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002303
2304 /* Past end of the window or end of the screen. Note that after
2305 * resizing wp->w_height may be end up too big. That's a problem
2306 * elsewhere, but prevent a crash here. */
2307 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002310 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2312 ++idx;
2313 break;
2314 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002315 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 wp->w_lines[idx].wl_size = row - srow;
2317 ++idx;
2318#ifdef FEAT_FOLDING
2319 lnum += fold_count + 1;
2320#else
2321 ++lnum;
2322#endif
2323 }
2324 else
2325 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002326 if (wp->w_p_rnu)
2327 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002328#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002329 // 'relativenumber' set: The text doesn't need to be drawn, but
2330 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002331 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2332 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002333 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002334 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002335#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002336 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002337 }
2338
2339 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 row += wp->w_lines[idx++].wl_size;
2341 if (row > wp->w_height) /* past end of screen */
2342 break;
2343#ifdef FEAT_FOLDING
2344 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2345#else
2346 ++lnum;
2347#endif
2348#ifdef FEAT_SYN_HL
2349 did_update = DID_NONE;
2350#endif
2351 }
2352
2353 if (lnum > buf->b_ml.ml_line_count)
2354 {
2355 eof = TRUE;
2356 break;
2357 }
2358 }
2359 /*
2360 * End of loop over all window lines.
2361 */
2362
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002363#ifdef FEAT_VTP
2364 /* Rewrite the character at the end of the screen line. */
2365 if (use_vtp())
2366 {
2367 int i;
2368
2369 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002370 if (enc_utf8)
2371 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2372 LineOffset[i] + screen_Columns) > 1)
2373 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2374 else
2375 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2376 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002377 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2378 }
2379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 if (idx > wp->w_lines_valid)
2382 wp->w_lines_valid = idx;
2383
2384#ifdef FEAT_SYN_HL
2385 /*
2386 * Let the syntax stuff know we stop parsing here.
2387 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002388 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 syntax_end_parsing(syntax_last_parsed + 1);
2390#endif
2391
2392 /*
2393 * If we didn't hit the end of the file, and we didn't finish the last
2394 * line we were working on, then the line didn't fit.
2395 */
2396 wp->w_empty_rows = 0;
2397#ifdef FEAT_DIFF
2398 wp->w_filler_rows = 0;
2399#endif
2400 if (!eof && !didline)
2401 {
2402 if (lnum == wp->w_topline)
2403 {
2404 /*
2405 * Single line that does not fit!
2406 * Don't overwrite it, it can be edited.
2407 */
2408 wp->w_botline = lnum + 1;
2409 }
2410#ifdef FEAT_DIFF
2411 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2412 {
2413 /* Window ends in filler lines. */
2414 wp->w_botline = lnum;
2415 wp->w_filler_rows = wp->w_height - srow;
2416 }
2417#endif
Bram Moolenaar17146962019-05-30 00:12:11 +02002418#ifdef FEAT_TEXT_PROP
2419 else if (bt_popup(wp->w_buffer))
2420 {
2421 // popup line that doesn't fit is left as-is
2422 wp->w_botline = lnum;
2423 }
2424#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002425 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2426 {
2427 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2428
2429 /*
2430 * Last line isn't finished: Display "@@@" in the last screen line.
2431 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002432 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002433 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002434 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002435 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002436 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002437 set_empty_rows(wp, srow);
2438 wp->w_botline = lnum;
2439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2441 {
2442 /*
2443 * Last line isn't finished: Display "@@@" at the end.
2444 */
2445 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2446 W_WINROW(wp) + wp->w_height,
2447 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002448 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 set_empty_rows(wp, srow);
2450 wp->w_botline = lnum;
2451 }
2452 else
2453 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002454 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 wp->w_botline = lnum;
2456 }
2457 }
2458 else
2459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (eof) /* we hit the end of the file */
2462 {
2463 wp->w_botline = buf->b_ml.ml_line_count + 1;
2464#ifdef FEAT_DIFF
2465 j = diff_check_fill(wp, wp->w_botline);
2466 if (j > 0 && !wp->w_botfill)
2467 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002468 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (char2cells(fill_diff) > 1)
2470 i = '-';
2471 else
2472 i = fill_diff;
2473 if (row + j > wp->w_height)
2474 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002475 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 row += j;
2477 }
2478#endif
2479 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002480 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 wp->w_botline = lnum;
2482
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002483 // Make sure the rest of the screen is blank
2484 // put '~'s on rows that aren't part of the file.
Bram Moolenaar17146962019-05-30 00:12:11 +02002485 win_draw_end(wp,
2486#ifdef FEAT_TEXT_PROP
2487 bt_popup(wp->w_buffer) ? ' ' :
2488#endif
2489 '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002492#ifdef SYN_TIME_LIMIT
2493 syn_set_timeout(NULL);
2494#endif
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 /* Reset the type of redrawing required, the window has been updated. */
2497 wp->w_redr_type = 0;
2498#ifdef FEAT_DIFF
2499 wp->w_old_topfill = wp->w_topfill;
2500 wp->w_old_botfill = wp->w_botfill;
2501#endif
2502
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002503 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 /*
2506 * There is a trick with w_botline. If we invalidate it on each
2507 * change that might modify it, this will cause a lot of expensive
2508 * calls to plines() in update_topline() each time. Therefore the
2509 * value of w_botline is often approximated, and this value is used to
2510 * compute the value of w_topline. If the value of w_botline was
2511 * wrong, check that the value of w_topline is correct (cursor is on
2512 * the visible part of the text). If it's not, we need to redraw
2513 * again. Mostly this just means scrolling up a few lines, so it
2514 * doesn't look too bad. Only do this for the current window (where
2515 * changes are relevant).
2516 */
2517 wp->w_valid |= VALID_BOTLINE;
2518 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2519 {
2520 recursive = TRUE;
2521 curwin->w_valid &= ~VALID_TOPLINE;
2522 update_topline(); /* may invalidate w_botline again */
2523 if (must_redraw != 0)
2524 {
2525 /* Don't update for changes in buffer again. */
2526 i = curbuf->b_mod_set;
2527 curbuf->b_mod_set = FALSE;
2528 win_update(curwin);
2529 must_redraw = 0;
2530 curbuf->b_mod_set = i;
2531 }
2532 recursive = FALSE;
2533 }
2534 }
2535
2536#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2537 /* restore got_int, unless CTRL-C was hit while redrawing */
2538 if (!got_int)
2539 got_int = save_got_int;
2540#endif
2541}
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002544 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2545 * Return the new offset.
2546 */
2547 static int
2548screen_fill_end(
2549 win_T *wp,
2550 int c1,
2551 int c2,
2552 int off,
2553 int width,
2554 int row,
2555 int endrow,
2556 int attr)
2557{
2558 int nn = off + width;
2559
2560 if (nn > wp->w_width)
2561 nn = wp->w_width;
2562#ifdef FEAT_RIGHTLEFT
2563 if (wp->w_p_rl)
2564 {
2565 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2566 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2567 c1, c2, attr);
2568 }
2569 else
2570#endif
2571 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2572 wp->w_wincol + off, (int)wp->w_wincol + nn,
2573 c1, c2, attr);
2574 return nn;
2575}
2576
2577/*
2578 * Clear lines near the end the window and mark the unused lines with "c1".
2579 * use "c2" as the filler character.
2580 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
2582 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002583win_draw_end(
2584 win_T *wp,
2585 int c1,
2586 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002587 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002588 int row,
2589 int endrow,
2590 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002593 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002594 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002595
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002596 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002597
2598 if (draw_margin)
2599 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002600#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002601 int fdc = compute_foldcolumn(wp, 0);
2602
2603 if (fdc > 0)
2604 // draw the fold column
2605 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002606 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002607#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002608#ifdef FEAT_SIGNS
2609 if (signcolumn_on(wp))
2610 // draw the sign column
2611 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002612 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002613#endif
2614 if ((wp->w_p_nu || wp->w_p_rnu)
2615 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2616 // draw the number column
2617 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002618 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621#ifdef FEAT_RIGHTLEFT
2622 if (wp->w_p_rl)
2623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002625 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002626 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002628 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002629 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 }
2631 else
2632#endif
2633 {
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 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002636 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002638
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 set_empty_rows(wp, row);
2640}
2641
Bram Moolenaar1a384422010-07-14 19:53:30 +02002642#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002643/*
2644 * Advance **color_cols and return TRUE when there are columns to draw.
2645 */
2646 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002647advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002648{
2649 while (**color_cols >= 0 && vcol > **color_cols)
2650 ++*color_cols;
2651 return (**color_cols >= 0);
2652}
2653#endif
2654
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002655#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2656/*
2657 * Copy "text" to ScreenLines using "attr".
2658 * Returns the next screen column.
2659 */
2660 static int
2661text_to_screenline(win_T *wp, char_u *text, int col)
2662{
2663 int off = (int)(current_ScreenLine - ScreenLines);
2664
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002665 if (has_mbyte)
2666 {
2667 int cells;
2668 int u8c, u8cc[MAX_MCO];
2669 int i;
2670 int idx;
2671 int c_len;
2672 char_u *p;
2673# ifdef FEAT_ARABIC
2674 int prev_c = 0; /* previous Arabic character */
2675 int prev_c1 = 0; /* first composing char for prev_c */
2676# endif
2677
2678# ifdef FEAT_RIGHTLEFT
2679 if (wp->w_p_rl)
2680 idx = off;
2681 else
2682# endif
2683 idx = off + col;
2684
2685 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2686 for (p = text; *p != NUL; )
2687 {
2688 cells = (*mb_ptr2cells)(p);
2689 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002690 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002691# ifdef FEAT_RIGHTLEFT
2692 - (wp->w_p_rl ? col : 0)
2693# endif
2694 )
2695 break;
2696 ScreenLines[idx] = *p;
2697 if (enc_utf8)
2698 {
2699 u8c = utfc_ptr2char(p, u8cc);
2700 if (*p < 0x80 && u8cc[0] == 0)
2701 {
2702 ScreenLinesUC[idx] = 0;
2703#ifdef FEAT_ARABIC
2704 prev_c = u8c;
2705#endif
2706 }
2707 else
2708 {
2709#ifdef FEAT_ARABIC
2710 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2711 {
2712 /* Do Arabic shaping. */
2713 int pc, pc1, nc;
2714 int pcc[MAX_MCO];
2715 int firstbyte = *p;
2716
2717 /* The idea of what is the previous and next
2718 * character depends on 'rightleft'. */
2719 if (wp->w_p_rl)
2720 {
2721 pc = prev_c;
2722 pc1 = prev_c1;
2723 nc = utf_ptr2char(p + c_len);
2724 prev_c1 = u8cc[0];
2725 }
2726 else
2727 {
2728 pc = utfc_ptr2char(p + c_len, pcc);
2729 nc = prev_c;
2730 pc1 = pcc[0];
2731 }
2732 prev_c = u8c;
2733
2734 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2735 pc, pc1, nc);
2736 ScreenLines[idx] = firstbyte;
2737 }
2738 else
2739 prev_c = u8c;
2740#endif
2741 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002742 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002743 for (i = 0; i < Screen_mco; ++i)
2744 {
2745 ScreenLinesC[i][idx] = u8cc[i];
2746 if (u8cc[i] == 0)
2747 break;
2748 }
2749 }
2750 if (cells > 1)
2751 ScreenLines[idx + 1] = 0;
2752 }
2753 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2754 /* double-byte single width character */
2755 ScreenLines2[idx] = p[1];
2756 else if (cells > 1)
2757 /* double-width character */
2758 ScreenLines[idx + 1] = p[1];
2759 col += cells;
2760 idx += cells;
2761 p += c_len;
2762 }
2763 }
2764 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002765 {
2766 int len = (int)STRLEN(text);
2767
Bram Moolenaar02631462017-09-22 15:20:32 +02002768 if (len > wp->w_width - col)
2769 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002770 if (len > 0)
2771 {
2772#ifdef FEAT_RIGHTLEFT
2773 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002774 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002775 else
2776#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002777 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002778 col += len;
2779 }
2780 }
2781 return col;
2782}
2783#endif
2784
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#ifdef FEAT_FOLDING
2786/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002787 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2788 * space is available for window "wp", minus "col".
2789 */
2790 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002791compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002792{
2793 int fdc = wp->w_p_fdc;
2794 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002795 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002796
2797 if (fdc > wwidth - (col + wmw))
2798 fdc = wwidth - (col + wmw);
2799 return fdc;
2800}
2801
2802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 * Display one folded line.
2804 */
2805 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002806fold_line(
2807 win_T *wp,
2808 long fold_count,
2809 foldinfo_T *foldinfo,
2810 linenr_T lnum,
2811 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002813 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 pos_T *top, *bot;
2815 linenr_T lnume = lnum + fold_count - 1;
2816 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002817 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 int col;
2820 int txtcol;
2821 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002822 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824 /* Build the fold line:
2825 * 1. Add the cmdwin_type for the command-line window
2826 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002827 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 * 4. Compose the text
2829 * 5. Add the text
2830 * 6. set highlighting for the Visual area an other text
2831 */
2832 col = 0;
2833
2834 /*
2835 * 1. Add the cmdwin_type for the command-line window
2836 * Ignores 'rightleft', this window is never right-left.
2837 */
2838#ifdef FEAT_CMDWIN
2839 if (cmdwin_type != 0 && wp == curwin)
2840 {
2841 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002842 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (enc_utf8)
2844 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 ++col;
2846 }
2847#endif
2848
2849 /*
2850 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002851 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002853 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (fdc > 0)
2855 {
2856 fill_foldcolumn(buf, wp, TRUE, lnum);
2857#ifdef FEAT_RIGHTLEFT
2858 if (wp->w_p_rl)
2859 {
2860 int i;
2861
Bram Moolenaar02631462017-09-22 15:20:32 +02002862 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002863 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 /* reverse the fold column */
2865 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002866 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 else
2869#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002870 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 col += fdc;
2872 }
2873
2874#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002875# define RL_MEMSET(p, v, l) \
2876 do { \
2877 if (wp->w_p_rl) \
2878 for (ri = 0; ri < l; ++ri) \
2879 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2880 else \
2881 for (ri = 0; ri < l; ++ri) \
2882 ScreenAttrs[off + (p) + ri] = v; \
2883 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002885# define RL_MEMSET(p, v, l) \
2886 do { \
2887 for (ri = 0; ri < l; ++ri) \
2888 ScreenAttrs[off + (p) + ri] = v; \
2889 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890#endif
2891
Bram Moolenaar64486672010-05-16 15:46:46 +02002892 /* Set all attributes of the 'number' or 'relativenumber' column and the
2893 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002894 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896#ifdef FEAT_SIGNS
2897 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002898 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (len > 0)
2902 {
2903 if (len > 2)
2904 len = 2;
2905# ifdef FEAT_RIGHTLEFT
2906 if (wp->w_p_rl)
2907 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002908 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002909 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 else
2911# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002912 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 col += len;
2914 }
2915 }
2916#endif
2917
2918 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002919 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002921 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002923 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 if (len > 0)
2925 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002926 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002927 long num;
2928 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002929
2930 if (len > w + 1)
2931 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002932
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002933 if (wp->w_p_nu && !wp->w_p_rnu)
2934 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002935 num = (long)lnum;
2936 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002937 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002938 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002939 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002940 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002941 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002942 /* 'number' + 'relativenumber': cursor line shows absolute
2943 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002944 num = lnum;
2945 fmt = "%-*ld ";
2946 }
2947 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002948
Bram Moolenaar700e7342013-01-30 12:31:36 +01002949 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950#ifdef FEAT_RIGHTLEFT
2951 if (wp->w_p_rl)
2952 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002953 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002954 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 else
2956#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002957 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 col += len;
2959 }
2960 }
2961
2962 /*
2963 * 4. Compose the folded-line string with 'foldtext', if set.
2964 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002965 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966
2967 txtcol = col; /* remember where text starts */
2968
2969 /*
2970 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2971 * Right-left text is put in columns 0 - number-col, normal text is put
2972 * in columns number-col - window-width.
2973 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002974 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /* Fill the rest of the line with the fold filler */
2977#ifdef FEAT_RIGHTLEFT
2978 if (wp->w_p_rl)
2979 col -= txtcol;
2980#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002981 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982#ifdef FEAT_RIGHTLEFT
2983 - (wp->w_p_rl ? txtcol : 0)
2984#endif
2985 )
2986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 if (enc_utf8)
2988 {
2989 if (fill_fold >= 0x80)
2990 {
2991 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002992 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002993 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002998 ScreenLines[off + col] = fill_fold;
2999 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003000 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003002 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02003003 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
3005
3006 if (text != buf)
3007 vim_free(text);
3008
3009 /*
3010 * 6. set highlighting for the Visual area an other text.
3011 * If all folded lines are in the Visual area, highlight the line.
3012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
3014 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003015 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
3017 /* Visual is after curwin->w_cursor */
3018 top = &curwin->w_cursor;
3019 bot = &VIsual;
3020 }
3021 else
3022 {
3023 /* Visual is before curwin->w_cursor */
3024 top = &VIsual;
3025 bot = &curwin->w_cursor;
3026 }
3027 if (lnum >= top->lnum
3028 && lnume <= bot->lnum
3029 && (VIsual_mode != 'v'
3030 || ((lnum > top->lnum
3031 || (lnum == top->lnum
3032 && top->col == 0))
3033 && (lnume < bot->lnum
3034 || (lnume == bot->lnum
3035 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003036 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 if (VIsual_mode == Ctrl_V)
3039 {
3040 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02003041 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02003043 if (wp->w_old_cursor_lcol != MAXCOL
3044 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02003045 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 len = wp->w_old_cursor_lcol;
3047 else
Bram Moolenaar02631462017-09-22 15:20:32 +02003048 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003049 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00003050 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052 }
3053 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02003056 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003061#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003062 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
3063 if (wp->w_p_cc_cols)
3064 {
3065 int i = 0;
3066 int j = wp->w_p_cc_cols[i];
3067 int old_txtcol = txtcol;
3068
3069 while (j > -1)
3070 {
3071 txtcol += j;
3072 if (wp->w_p_wrap)
3073 txtcol -= wp->w_skipcol;
3074 else
3075 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003076 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003077 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003078 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01003079 txtcol = old_txtcol;
3080 j = wp->w_p_cc_cols[++i];
3081 }
3082 }
3083
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003084 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00003085 if (wp->w_p_cuc)
3086 {
3087 txtcol += wp->w_virtcol;
3088 if (wp->w_p_wrap)
3089 txtcol -= wp->w_skipcol;
3090 else
3091 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02003092 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00003093 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01003094 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00003095 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
Bram Moolenaar02631462017-09-22 15:20:32 +02003098 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003099 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 /*
3102 * Update w_cline_height and w_cline_folded if the cursor line was
3103 * updated (saves a call to plines() later).
3104 */
3105 if (wp == curwin
3106 && lnum <= curwin->w_cursor.lnum
3107 && lnume >= curwin->w_cursor.lnum)
3108 {
3109 curwin->w_cline_row = row;
3110 curwin->w_cline_height = 1;
3111 curwin->w_cline_folded = TRUE;
3112 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
3113 }
3114}
3115
3116/*
3117 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
3118 */
3119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003120copy_text_attr(
3121 int off,
3122 char_u *buf,
3123 int len,
3124 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003126 int i;
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (enc_utf8)
3130 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00003131 for (i = 0; i < len; ++i)
3132 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133}
3134
3135/*
3136 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003137 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 */
3139 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003140fill_foldcolumn(
3141 char_u *p,
3142 win_T *wp,
3143 int closed, /* TRUE of FALSE */
3144 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145{
3146 int i = 0;
3147 int level;
3148 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003149 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003150 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02003153 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 level = win_foldinfo.fi_level;
3156 if (level > 0)
3157 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003158 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003159 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 /* If the column is too narrow, we start at the lowest level that
3162 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003163 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (first_level < 1)
3165 first_level = 1;
3166
Bram Moolenaar1c934292015-01-27 16:39:29 +01003167 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 {
3169 if (win_foldinfo.fi_lnum == lnum
3170 && first_level + i >= win_foldinfo.fi_low_level)
3171 p[i] = '-';
3172 else if (first_level == 1)
3173 p[i] = '|';
3174 else if (first_level + i <= 9)
3175 p[i] = '0' + first_level + i;
3176 else
3177 p[i] = '>';
3178 if (first_level + i == level)
3179 break;
3180 }
3181 }
3182 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003183 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184}
3185#endif /* FEAT_FOLDING */
3186
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003187#ifdef FEAT_TEXT_PROP
3188static textprop_T *current_text_props = NULL;
3189static buf_T *current_buf = NULL;
3190
3191 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003192text_prop_compare(const void *s1, const void *s2)
3193{
3194 int idx1, idx2;
3195 proptype_T *pt1, *pt2;
3196 colnr_T col1, col2;
3197
3198 idx1 = *(int *)s1;
3199 idx2 = *(int *)s2;
3200 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3201 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3202 if (pt1 == pt2)
3203 return 0;
3204 if (pt1 == NULL)
3205 return -1;
3206 if (pt2 == NULL)
3207 return 1;
3208 if (pt1->pt_priority != pt2->pt_priority)
3209 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3210 col1 = current_text_props[idx1].tp_col;
3211 col2 = current_text_props[idx2].tp_col;
3212 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3213}
3214#endif
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216/*
3217 * Display line "lnum" of window 'wp' on the screen.
3218 * Start at row "startrow", stop when "endrow" is reached.
3219 * wp->w_virtcol needs to be valid.
3220 *
3221 * Return the number of last row the line occupies.
3222 */
3223 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003224win_line(
3225 win_T *wp,
3226 linenr_T lnum,
3227 int startrow,
3228 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003229 int nochange UNUSED, // not updating for changed text
3230 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003232 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3234 int c = 0; /* init for GCC */
3235 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003236#ifdef FEAT_LINEBREAK
3237 long vcol_sbr = -1; /* virtual column after showbreak */
3238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 long vcol_prev = -1; /* "vcol" of previous character */
3240 char_u *line; /* current line */
3241 char_u *ptr; /* current position in "line" */
3242 int row; /* row in the window, excl w_winrow */
3243 int screen_row; /* row on the screen, incl w_winrow */
3244
3245 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3246 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003247 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003248 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003250 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 int extra_attr = 0; /* attributes when n_extra != 0 */
3252 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3253 displaying lcs_eol at end-of-line */
3254 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3255 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3256
3257 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3258 int saved_n_extra = 0;
3259 char_u *saved_p_extra = NULL;
3260 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003261 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 int saved_char_attr = 0;
3263
3264 int n_attr = 0; /* chars with special attr */
3265 int saved_attr2 = 0; /* char_attr saved for n_attr */
3266 int n_attr3 = 0; /* chars with overruling special attr */
3267 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3268
3269 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3270
3271 int fromcol, tocol; /* start/end of inverting */
3272 int fromcol_prev = -2; /* start of inverting after cursor */
3273 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003275 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 pos_T pos;
3277 long v;
3278
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003279 int char_attr = 0; // attributes for next character
3280 int attr_pri = FALSE; // char_attr has priority
3281 int area_highlighting = FALSE; // Visual or incsearch highlighting
3282 // in this line
3283 int vi_attr = 0; // attributes for Visual and incsearch
3284 // highlighting
3285 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003286 int win_attr = 0; // background for whole window, except
3287 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003288 int area_attr = 0; // attributes desired by highlighting
3289 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003291 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 int syntax_attr = 0; /* attributes desired by syntax */
3293 int has_syntax = FALSE; /* this buffer has syntax highl. */
3294 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003295 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003296 int draw_color_col = FALSE; /* highlight colorcolumn */
3297 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003298#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003299#ifdef FEAT_TEXT_PROP
3300 int text_prop_count;
3301 int text_prop_next = 0; // next text property to use
3302 textprop_T *text_props = NULL;
3303 int *text_prop_idxs = NULL;
3304 int text_props_active = 0;
3305 proptype_T *text_prop_type = NULL;
3306 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003307 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003308#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003309#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003310 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003311# define SPWORDLEN 150
3312 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003313 int nextlinecol = 0; /* column where nextline[] starts */
3314 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003315 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003316 int spell_attr = 0; /* attributes desired by spelling */
3317 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003318 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3319 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003320 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003321 static int cap_col = -1; /* column to check for Cap word */
3322 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003323 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003325 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 int multi_attr = 0; /* attributes desired by multibyte */
3327 int mb_l = 1; /* multi-byte byte length */
3328 int mb_c = 0; /* decoded multi-byte character */
3329 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003330 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#ifdef FEAT_DIFF
3332 int filler_lines; /* nr of filler lines to be drawn */
3333 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003334 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 int change_start = MAXCOL; /* first col of changed area */
3336 int change_end = -1; /* last col of changed area */
3337#endif
3338 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3339#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003340 int need_showbreak = FALSE; /* overlong line, skipping first x
3341 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003343#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003344 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003346 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#endif
3348#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003349 matchitem_T *cur; /* points to the match list */
3350 match_T *shl; /* points to search_hl or a match */
3351 int shl_flag; /* flag to indicate whether search_hl
3352 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003353 int pos_inprogress; /* marks that position match search is
3354 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003355 int prevcol_hl_flag; /* flag to indicate whether prevcol
3356 equals startcol of search_hl or one
3357 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
3359#ifdef FEAT_ARABIC
3360 int prev_c = 0; /* previous Arabic character */
3361 int prev_c1 = 0; /* first composing char for prev_c */
3362#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003363#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003364 int did_line_attr = 0;
3365#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003366#ifdef FEAT_TERMINAL
3367 int get_term_attr = FALSE;
3368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370 /* draw_state: items that are drawn in sequence: */
3371#define WL_START 0 /* nothing done yet */
3372#ifdef FEAT_CMDWIN
3373# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3374#else
3375# define WL_CMDLINE WL_START
3376#endif
3377#ifdef FEAT_FOLDING
3378# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3379#else
3380# define WL_FOLD WL_CMDLINE
3381#endif
3382#ifdef FEAT_SIGNS
3383# define WL_SIGN WL_FOLD + 1 /* column for signs */
3384#else
3385# define WL_SIGN WL_FOLD /* column for signs */
3386#endif
3387#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003388#ifdef FEAT_LINEBREAK
3389# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003391# define WL_BRI WL_NR
3392#endif
3393#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3394# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3395#else
3396# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397#endif
3398#define WL_LINE WL_SBR + 1 /* text in the line */
3399 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003400#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 int feedback_col = 0;
3402 int feedback_old_attr = -1;
3403#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003404 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar860cae12010-06-05 23:22:07 +02003406#ifdef FEAT_CONCEAL
3407 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003408 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003409 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003410 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003411 int is_concealing = FALSE;
3412 int boguscols = 0; /* nonexistent columns added to force
3413 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003414 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003415 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003416 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003417 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003418# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003419# define FIX_FOR_BOGUSCOLS \
3420 { \
3421 n_extra += vcol_off; \
3422 vcol -= vcol_off; \
3423 vcol_off = 0; \
3424 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003425 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003426 boguscols = 0; \
3427 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003428#else
3429# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432 if (startrow > endrow) /* past the end already! */
3433 return startrow;
3434
3435 row = startrow;
3436 screen_row = row + W_WINROW(wp);
3437
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 if (!number_only)
3439 {
3440 /*
3441 * To speed up the loop below, set extra_check when there is linebreak,
3442 * trailing white space and/or syntax processing to be done.
3443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
3447#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003448 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003449# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003450 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003451# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003452 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003454 /* Prepare for syntax highlighting in this line. When there is an
3455 * error, stop syntax highlighting. */
3456 save_did_emsg = did_emsg;
3457 did_emsg = FALSE;
3458 syntax_start(wp, lnum);
3459 if (did_emsg)
3460 wp->w_s->b_syn_error = TRUE;
3461 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003462 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003463 did_emsg = save_did_emsg;
3464#ifdef SYN_TIME_LIMIT
3465 if (!wp->w_s->b_syn_slow)
3466#endif
3467 {
3468 has_syntax = TRUE;
3469 extra_check = TRUE;
3470 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003473
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003474 /* Check for columns to display for 'colorcolumn'. */
3475 color_cols = wp->w_p_cc_cols;
3476 if (color_cols != NULL)
3477 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003478#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003479
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003480#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003481 if (term_show_buffer(wp->w_buffer))
3482 {
3483 extra_check = TRUE;
3484 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003485 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003486 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003487#endif
3488
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003489#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003490 if (wp->w_p_spell
3491 && *wp->w_s->b_p_spl != NUL
3492 && wp->w_s->b_langp.ga_len > 0
3493 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003494 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003495 /* Prepare for spell checking. */
3496 has_spell = TRUE;
3497 extra_check = TRUE;
3498
Bram Moolenaar7701f302018-10-02 21:20:32 +02003499 /* Get the start of the next line, so that words that wrap to the
3500 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003501 * Trick: skip a few chars for C/shell/Vim comments */
3502 nextline[SPWORDLEN] = NUL;
3503 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3504 {
3505 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3506 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3507 }
3508
Bram Moolenaar7701f302018-10-02 21:20:32 +02003509 /* When a word wrapped from the previous line the start of the
3510 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003511 if (lnum == checked_lnum)
3512 cur_checked_col = checked_col;
3513 checked_lnum = 0;
3514
3515 /* When there was a sentence end in the previous line may require a
3516 * word starting with capital in this line. In line 1 always check
3517 * the first word. */
3518 if (lnum != capcol_lnum)
3519 cap_col = -1;
3520 if (lnum == 1)
3521 cap_col = 0;
3522 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524#endif
3525
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003526 /*
3527 * handle visual active in this window
3528 */
3529 fromcol = -10;
3530 tocol = MAXCOL;
3531 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003533 /* Visual is after curwin->w_cursor */
3534 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003536 top = &curwin->w_cursor;
3537 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003539 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003541 top = &VIsual;
3542 bot = &curwin->w_cursor;
3543 }
3544 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3545 if (VIsual_mode == Ctrl_V) /* block mode */
3546 {
3547 if (lnum_in_visual_area)
3548 {
3549 fromcol = wp->w_old_cursor_fcol;
3550 tocol = wp->w_old_cursor_lcol;
3551 }
3552 }
3553 else /* non-block mode */
3554 {
3555 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003557 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003559 if (VIsual_mode == 'V') /* linewise */
3560 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
3562 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003563 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3564 if (gchar_pos(top) == NUL)
3565 tocol = fromcol + 1;
3566 }
3567 }
3568 if (VIsual_mode != 'V' && lnum == bot->lnum)
3569 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003570 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003571 {
3572 fromcol = -10;
3573 tocol = MAXCOL;
3574 }
3575 else if (bot->col == MAXCOL)
3576 tocol = MAXCOL;
3577 else
3578 {
3579 pos = *bot;
3580 if (*p_sel == 'e')
3581 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3582 else
3583 {
3584 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3585 ++tocol;
3586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 }
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003591 /* Check if the character under the cursor should not be inverted */
3592 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003593#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003594 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003595#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003596 )
3597 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003599 /* if inverting in this line set area_highlighting */
3600 if (fromcol >= 0)
3601 {
3602 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003603 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003605 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003606 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003607 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003608 && clip_isautosel_plus()))
3609 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003614 /*
3615 * handle 'incsearch' and ":s///c" highlighting
3616 */
3617 else if (highlight_match
3618 && wp == curwin
3619 && lnum >= curwin->w_cursor.lnum
3620 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003622 if (lnum == curwin->w_cursor.lnum)
3623 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003624 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003625 else
3626 fromcol = 0;
3627 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3628 {
3629 pos.lnum = lnum;
3630 pos.col = search_match_endcol;
3631 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3632 }
3633 else
3634 tocol = MAXCOL;
3635 /* do at least one character; happens when past end of line */
3636 if (fromcol == tocol)
3637 tocol = fromcol + 1;
3638 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003639 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642
3643#ifdef FEAT_DIFF
3644 filler_lines = diff_check(wp, lnum);
3645 if (filler_lines < 0)
3646 {
3647 if (filler_lines == -1)
3648 {
3649 if (diff_find_change(wp, lnum, &change_start, &change_end))
3650 diff_hlf = HLF_ADD; /* added line */
3651 else if (change_start == 0)
3652 diff_hlf = HLF_TXD; /* changed text */
3653 else
3654 diff_hlf = HLF_CHD; /* changed line */
3655 }
3656 else
3657 diff_hlf = HLF_ADD; /* added line */
3658 filler_lines = 0;
3659 area_highlighting = TRUE;
3660 }
3661 if (lnum == wp->w_topline)
3662 filler_lines = wp->w_topfill;
3663 filler_todo = filler_lines;
3664#endif
3665
3666#ifdef LINE_ATTR
3667# ifdef FEAT_SIGNS
3668 /* If this line has a sign with line highlighting set line_attr. */
3669 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3670 if (v != 0)
3671 line_attr = sign_get_attr((int)v, TRUE);
3672# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003673# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003675 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003676 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677# endif
3678 if (line_attr != 0)
3679 area_highlighting = TRUE;
3680#endif
3681
3682 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3683 ptr = line;
3684
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003685#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003686 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003687 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003688 /* For checking first word with a capital skip white space. */
3689 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003690 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003691
Bram Moolenaar30abd282005-06-22 22:35:10 +00003692 /* To be able to spell-check over line boundaries copy the end of the
3693 * current line into nextline[]. Above the start of the next line was
3694 * copied to nextline[SPWORDLEN]. */
3695 if (nextline[SPWORDLEN] == NUL)
3696 {
3697 /* No next line or it is empty. */
3698 nextlinecol = MAXCOL;
3699 nextline_idx = 0;
3700 }
3701 else
3702 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003703 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003704 if (v < SPWORDLEN)
3705 {
3706 /* Short line, use it completely and append the start of the
3707 * next line. */
3708 nextlinecol = 0;
3709 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003710 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003711 nextline_idx = v + 1;
3712 }
3713 else
3714 {
3715 /* Long line, use only the last SPWORDLEN bytes. */
3716 nextlinecol = v - SPWORDLEN;
3717 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3718 nextline_idx = SPWORDLEN + 1;
3719 }
3720 }
3721 }
3722#endif
3723
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003724 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003726 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003727 extra_check = TRUE;
3728 /* find start of trailing whitespace */
3729 if (lcs_trail)
3730 {
3731 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003732 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003733 --trailcol;
3734 trailcol += (colnr_T) (ptr - line);
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003738 wcr_attr = get_wcr_attr(wp);
3739 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003740 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003741 win_attr = wcr_attr;
3742 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003743 }
3744#ifdef FEAT_TEXT_PROP
3745 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003746 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003747#endif
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 /*
3750 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3751 * first character to be displayed.
3752 */
3753 if (wp->w_p_wrap)
3754 v = wp->w_skipcol;
3755 else
3756 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003757 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 while (vcol < v && *ptr != NUL)
3762 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003763 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003766 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003769 /* When:
3770 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003771 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003772 * - 'virtualedit' is set, or
3773 * - the visual mode is active,
3774 * the end of the line may be before the start of the displayed part.
3775 */
3776 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003777#ifdef FEAT_SYN_HL
3778 wp->w_p_cuc || draw_color_col ||
3779#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003780 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003781 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 /* Handle a character that's not completely on the screen: Put ptr at
3785 * that character but skip the first few screen characters. */
3786 if (vcol > v)
3787 {
3788 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003790 /* If the character fits on the screen, don't need to skip it.
3791 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003792 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003793 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795
3796 /*
3797 * Adjust for when the inverted text is before the screen,
3798 * and when the start of the inverted text is before the screen.
3799 */
3800 if (tocol <= vcol)
3801 fromcol = 0;
3802 else if (fromcol >= 0 && fromcol < vcol)
3803 fromcol = vcol;
3804
3805#ifdef FEAT_LINEBREAK
3806 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3807 if (wp->w_p_wrap)
3808 need_showbreak = TRUE;
3809#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003810#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003811 /* When spell checking a word we need to figure out the start of the
3812 * word and if it's badly spelled or not. */
3813 if (has_spell)
3814 {
3815 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003816 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003817 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003818
3819 pos = wp->w_cursor;
3820 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003821 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003822 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003823
3824 /* spell_move_to() may call ml_get() and make "line" invalid */
3825 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3826 ptr = line + linecol;
3827
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003828 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003829 {
3830 /* no bad word found at line start, don't check until end of a
3831 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003832 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003833 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003834 }
3835 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003836 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003837 /* bad word found, use attributes until end of word */
3838 word_end = wp->w_cursor.col + len + 1;
3839
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003840 /* Turn index into actual attributes. */
3841 if (spell_hlf != HLF_COUNT)
3842 spell_attr = highlight_attr[spell_hlf];
3843 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003844 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003845
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003846# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003847 /* Need to restart syntax highlighting for this line. */
3848 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003849 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003850# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003851 }
3852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854
3855 /*
3856 * Correct highlighting for cursor that can't be disabled.
3857 * Avoids having to check this for each character.
3858 */
3859 if (fromcol >= 0)
3860 {
3861 if (noinvcur)
3862 {
3863 if ((colnr_T)fromcol == wp->w_virtcol)
3864 {
3865 /* highlighting starts at cursor, let it start just after the
3866 * cursor */
3867 fromcol_prev = fromcol;
3868 fromcol = -1;
3869 }
3870 else if ((colnr_T)fromcol < wp->w_virtcol)
3871 /* restart highlighting after the cursor */
3872 fromcol_prev = wp->w_virtcol;
3873 }
3874 if (fromcol >= tocol)
3875 fromcol = -1;
3876 }
3877
3878#ifdef FEAT_SEARCH_EXTRA
3879 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003880 * Handle highlighting the last used search pattern and matches.
3881 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003882 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003884 cur = wp->w_match_head;
3885 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003886 while ((cur != NULL || shl_flag == FALSE) && !number_only
3887 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003889 if (shl_flag == FALSE)
3890 {
3891 shl = &search_hl;
3892 shl_flag = TRUE;
3893 }
3894 else
3895 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003896 shl->startcol = MAXCOL;
3897 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003899 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003900 v = (long)(ptr - line);
3901 if (cur != NULL)
3902 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003903 next_search_hl(wp, shl, lnum, (colnr_T)v,
3904 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003905
3906 /* Need to get the line again, a multi-line regexp may have made it
3907 * invalid. */
3908 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3909 ptr = line + v;
3910
3911 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003913 if (shl->lnum == lnum)
3914 shl->startcol = shl->rm.startpos[0].col;
3915 else
3916 shl->startcol = 0;
3917 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3918 - shl->rm.startpos[0].lnum)
3919 shl->endcol = shl->rm.endpos[0].col;
3920 else
3921 shl->endcol = MAXCOL;
3922 /* Highlight one character for an empty match. */
3923 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003925 if (has_mbyte && line[shl->endcol] != NUL)
3926 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3927 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003928 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003930 if ((long)shl->startcol < v) /* match at leftcol */
3931 {
3932 shl->attr_cur = shl->attr;
3933 search_attr = shl->attr;
3934 }
3935 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003937 if (shl != &search_hl && cur != NULL)
3938 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 }
3940#endif
3941
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003942#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003943 // Cursor line highlighting for 'cursorline' in the current window.
3944 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003945 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003946 // Do not show the cursor line when Visual mode is active, because it's
3947 // not clear what is selected then. Do update w_last_cursorline.
3948 if (!(wp == curwin && VIsual_active))
3949 {
3950 line_attr = HL_ATTR(HLF_CUL);
3951 area_highlighting = TRUE;
3952 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003953 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003954 }
3955#endif
3956
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003957#ifdef FEAT_TEXT_PROP
3958 {
3959 char_u *prop_start;
3960
3961 text_prop_count = get_text_props(wp->w_buffer, lnum,
3962 &prop_start, FALSE);
3963 if (text_prop_count > 0)
3964 {
3965 // Make a copy of the properties, so that they are properly
3966 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003967 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003968 if (text_props != NULL)
3969 mch_memmove(text_props, prop_start,
3970 text_prop_count * sizeof(textprop_T));
3971
3972 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003973 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003974 area_highlighting = TRUE;
3975 extra_check = TRUE;
3976 }
3977 }
3978#endif
3979
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003980 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#ifdef FEAT_RIGHTLEFT
3984 if (wp->w_p_rl)
3985 {
3986 /* Rightleft window: process the text in the normal direction, but put
3987 * it in current_ScreenLine[] from right to left. Start at the
3988 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003989 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003991 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993#endif
3994
3995 /*
3996 * Repeat for the whole displayed line.
3997 */
3998 for (;;)
3999 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02004000#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004001 int has_match_conc = 0; // match wants to conceal
4002 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 /* Skip this quickly when working on the text. */
4005 if (draw_state != WL_LINE)
4006 {
4007#ifdef FEAT_CMDWIN
4008 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
4009 {
4010 draw_state = WL_CMDLINE;
4011 if (cmdwin_type != 0 && wp == curwin)
4012 {
4013 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00004015 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004016 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004017 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 }
4020#endif
4021
4022#ifdef FEAT_FOLDING
4023 if (draw_state == WL_FOLD - 1 && n_extra == 0)
4024 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01004025 int fdc = compute_foldcolumn(wp, 0);
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01004028 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
Bram Moolenaar62706602016-12-09 19:28:48 +01004030 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01004031 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01004032 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01004033 p_extra_free = alloc(12 + 1);
4034
4035 if (p_extra_free != NULL)
4036 {
4037 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
4038 n_extra = fdc;
4039 p_extra_free[n_extra] = NUL;
4040 p_extra = p_extra_free;
4041 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004042 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004043 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01004044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 }
4047#endif
4048
4049#ifdef FEAT_SIGNS
4050 if (draw_state == WL_SIGN - 1 && n_extra == 0)
4051 {
4052 draw_state = WL_SIGN;
4053 /* Show the sign column when there are any signs in this
4054 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02004055 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004057 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01004059 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060# endif
4061
4062 /* Draw two cells with the sign value or blank. */
4063 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004064 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004065 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 n_extra = 2;
4067
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02004068 if (row == startrow
4069#ifdef FEAT_DIFF
4070 + filler_lines && filler_todo <= 0
4071#endif
4072 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
4074 text_sign = buf_getsigntype(wp->w_buffer, lnum,
4075 SIGN_TEXT);
4076# ifdef FEAT_SIGN_ICONS
4077 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
4078 SIGN_ICON);
4079 if (gui.in_use && icon_sign != 0)
4080 {
4081 /* Use the image in this position. */
4082 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004083 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084# ifdef FEAT_NETBEANS_INTG
4085 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004088 c_final = NUL;
4089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090# endif
4091 char_attr = icon_sign;
4092 }
4093 else
4094# endif
4095 if (text_sign != 0)
4096 {
4097 p_extra = sign_get_text(text_sign);
4098 if (p_extra != NULL)
4099 {
4100 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004101 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004102 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 char_attr = sign_get_attr(text_sign, FALSE);
4105 }
4106 }
4107 }
4108 }
4109#endif
4110
4111 if (draw_state == WL_NR - 1 && n_extra == 0)
4112 {
4113 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02004114 /* Display the absolute or relative line number. After the
4115 * first fill with blanks when the 'n' flag isn't in 'cpo' */
4116 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 && (row == startrow
4118#ifdef FEAT_DIFF
4119 + filler_lines
4120#endif
4121 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
4122 {
4123 /* Draw the line number (empty space after wrapping). */
4124 if (row == startrow
4125#ifdef FEAT_DIFF
4126 + filler_lines
4127#endif
4128 )
4129 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004130 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01004131 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02004132
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004133 if (wp->w_p_nu && !wp->w_p_rnu)
4134 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02004135 num = (long)lnum;
4136 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01004137 {
Bram Moolenaar64486672010-05-16 15:46:46 +02004138 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01004139 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004140 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004141 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02004142 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01004143 num = lnum;
4144 fmt = "%-*ld ";
4145 }
4146 }
Bram Moolenaar64486672010-05-16 15:46:46 +02004147
Bram Moolenaar700e7342013-01-30 12:31:36 +01004148 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02004149 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (wp->w_skipcol > 0)
4151 for (p_extra = extra; *p_extra == ' '; ++p_extra)
4152 *p_extra = '-';
4153#ifdef FEAT_RIGHTLEFT
4154 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004155 {
4156 char_u *p1, *p2;
4157 int t;
4158
4159 // like rl_mirror(), but keep the space at the end
4160 p2 = skiptowhite(extra) - 1;
4161 for (p1 = extra; p1 < p2; ++p1, --p2)
4162 {
4163 t = *p1;
4164 *p1 = *p2;
4165 *p2 = t;
4166 }
4167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#endif
4169 p_extra = extra;
4170 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004171 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004176 c_final = NUL;
4177 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004178 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004179 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004180#ifdef FEAT_SYN_HL
4181 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004182 * the current line differently.
4183 * TODO: Can we use CursorLine instead of CursorLineNr
4184 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004185 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004186 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004187 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 }
4191
Bram Moolenaar597a4222014-06-25 14:39:50 +02004192#ifdef FEAT_LINEBREAK
4193 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4194 && n_extra == 0 && *p_sbr != NUL)
4195 /* draw indent after showbreak value */
4196 draw_state = WL_BRI;
4197 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4198 /* After the showbreak, draw the breakindent */
4199 draw_state = WL_BRI - 1;
4200
4201 /* draw 'breakindent': indent wrapped text accordingly */
4202 if (draw_state == WL_BRI - 1 && n_extra == 0)
4203 {
4204 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004205 /* if need_showbreak is set, breakindent also applies */
4206 if (wp->w_p_bri && n_extra == 0
4207 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004208# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004209 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004210# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004211 )
4212 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004213 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004214# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004215 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004216 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004217 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004218# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004219 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4220 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004221 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004222# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004223 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004224# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004225 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004226 c_extra = ' ';
4227 n_extra = get_breakindent_win(wp,
4228 ml_get_buf(wp->w_buffer, lnum, FALSE));
4229 /* Correct end of highlighted area for 'breakindent',
4230 * required when 'linebreak' is also set. */
4231 if (tocol == vcol)
4232 tocol += n_extra;
4233 }
4234 }
4235#endif
4236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4238 if (draw_state == WL_SBR - 1 && n_extra == 0)
4239 {
4240 draw_state = WL_SBR;
4241# ifdef FEAT_DIFF
4242 if (filler_todo > 0)
4243 {
4244 /* Draw "deleted" diff line(s). */
4245 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004248 c_final = NUL;
4249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004253 c_final = NUL;
4254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255# ifdef FEAT_RIGHTLEFT
4256 if (wp->w_p_rl)
4257 n_extra = col + 1;
4258 else
4259# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004260 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004261 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263# endif
4264# ifdef FEAT_LINEBREAK
4265 if (*p_sbr != NUL && need_showbreak)
4266 {
4267 /* Draw 'showbreak' at the start of each broken line. */
4268 p_extra = p_sbr;
4269 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004270 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004272 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004274 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 /* Correct end of highlighted area for 'showbreak',
4276 * required when 'linebreak' is also set. */
4277 if (tocol == vcol)
4278 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004279#ifdef FEAT_SYN_HL
4280 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004281 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004282 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004283 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286# endif
4287 }
4288#endif
4289
4290 if (draw_state == WL_LINE - 1 && n_extra == 0)
4291 {
4292 draw_state = WL_LINE;
4293 if (saved_n_extra)
4294 {
4295 /* Continue item from end of wrapped line. */
4296 n_extra = saved_n_extra;
4297 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004298 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 p_extra = saved_p_extra;
4300 char_attr = saved_char_attr;
4301 }
4302 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004303 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 }
4306
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004307 // When still displaying '$' of change command, stop at cursor.
4308 // When only displaying the (relative) line number and that's done,
4309 // stop here.
4310 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312#ifdef FEAT_DIFF
4313 && filler_todo <= 0
4314#endif
4315 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004316 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004318 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004319 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004320 /* Pretend we have finished updating the window. Except when
4321 * 'cursorcolumn' is set. */
4322#ifdef FEAT_SYN_HL
4323 if (wp->w_p_cuc)
4324 row = wp->w_cline_row + wp->w_cline_height;
4325 else
4326#endif
4327 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 break;
4329 }
4330
Bram Moolenaar637532b2019-01-03 21:44:40 +01004331 if (draw_state == WL_LINE && (area_highlighting
4332#ifdef FEAT_SPELL
4333 || has_spell
4334#endif
4335 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 /* handle Visual or match highlighting in this line */
4338 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4340 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004342 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004344 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 else if (area_attr != 0
4346 && (vcol == tocol
4347 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350#ifdef FEAT_SEARCH_EXTRA
4351 if (!n_extra)
4352 {
4353 /*
4354 * Check for start/end of search pattern match.
4355 * After end, check for start/end of next match.
4356 * When another match, have to check for start again.
4357 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004358 * Do this for 'search_hl' and the match list (ordered by
4359 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004361 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004362 cur = wp->w_match_head;
4363 shl_flag = FALSE;
4364 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004366 if (shl_flag == FALSE
4367 && ((cur != NULL
4368 && cur->priority > SEARCH_HL_PRIORITY)
4369 || cur == NULL))
4370 {
4371 shl = &search_hl;
4372 shl_flag = TRUE;
4373 }
4374 else
4375 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004376 if (cur != NULL)
4377 cur->pos.cur = 0;
4378 pos_inprogress = TRUE;
4379 while (shl->rm.regprog != NULL
4380 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004382 if (shl->startcol != MAXCOL
4383 && v >= (long)shl->startcol
4384 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004386 int tmp_col = v + MB_PTR2LEN(ptr);
4387
4388 if (shl->endcol < tmp_col)
4389 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004391#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004392 // Match with the "Conceal" group results in hiding
4393 // the match.
4394 if (cur != NULL
4395 && shl != &search_hl
4396 && syn_name2id((char_u *)"Conceal")
4397 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004398 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004399 has_match_conc =
4400 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004401 match_conc = cur->conceal_char;
4402 }
4403 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004404 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004407 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
4409 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004410 next_search_hl(wp, shl, lnum, (colnr_T)v,
4411 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004412 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004413 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 /* Need to get the line again, a multi-line regexp
4416 * may have made it invalid. */
4417 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4418 ptr = line + v;
4419
4420 if (shl->lnum == lnum)
4421 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004422 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004424 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004426 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004428 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
4430 /* highlight empty match, try again after
4431 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004433 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004434 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004436 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438
4439 /* Loop to check if the match starts at the
4440 * current position */
4441 continue;
4442 }
4443 }
4444 break;
4445 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004446 if (shl != &search_hl && cur != NULL)
4447 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004449
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004450 /* Use attributes from match with highest priority among
4451 * 'search_hl' and the match list. */
4452 search_attr = search_hl.attr_cur;
4453 cur = wp->w_match_head;
4454 shl_flag = FALSE;
4455 while (cur != NULL || shl_flag == FALSE)
4456 {
4457 if (shl_flag == FALSE
4458 && ((cur != NULL
4459 && cur->priority > SEARCH_HL_PRIORITY)
4460 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004461 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004462 shl = &search_hl;
4463 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004464 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004465 else
4466 shl = &cur->hl;
4467 if (shl->attr_cur != 0)
4468 search_attr = shl->attr_cur;
4469 if (shl != &search_hl && cur != NULL)
4470 cur = cur->next;
4471 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004472 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004473 if (*ptr == NUL && (did_line_attr >= 1
4474 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004475 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477#endif
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004480 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004482 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4483 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004485 if (diff_hlf == HLF_TXD && ptr - line > change_end
4486 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004488 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004489 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004490 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004493
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004494#ifdef FEAT_TEXT_PROP
4495 if (text_props != NULL)
4496 {
4497 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004498 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004499
4500 // Check if any active property ends.
4501 for (pi = 0; pi < text_props_active; ++pi)
4502 {
4503 int tpi = text_prop_idxs[pi];
4504
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004505 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004506 + text_props[tpi].tp_len)
4507 {
4508 if (pi + 1 < text_props_active)
4509 mch_memmove(text_prop_idxs + pi,
4510 text_prop_idxs + pi + 1,
4511 sizeof(int)
4512 * (text_props_active - (pi + 1)));
4513 --text_props_active;
4514 --pi;
4515 }
4516 }
4517
4518 // Add any text property that starts in this column.
4519 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004520 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004521 text_prop_idxs[text_props_active++] = text_prop_next++;
4522
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004523 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004524 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004525 if (text_props_active > 0)
4526 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004527 // Sort the properties on priority and/or starting last.
4528 // Then combine the attributes, highest priority last.
4529 current_text_props = text_props;
4530 current_buf = wp->w_buffer;
4531 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4532 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004533
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004534 for (pi = 0; pi < text_props_active; ++pi)
4535 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004536 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004537 proptype_T *pt = text_prop_type_by_id(
4538 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004539
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004540 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004541 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004542 int pt_attr = syn_id2attr(pt->pt_hl_id);
4543
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004544 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004545 text_prop_attr =
4546 hl_combine_attr(text_prop_attr, pt_attr);
4547 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004548 }
4549 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004550 }
4551 }
4552#endif
4553
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004554 /* Decide which of the highlight attributes to use. */
4555 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004556#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004557 if (area_attr != 0)
4558 char_attr = hl_combine_attr(line_attr, area_attr);
4559 else if (search_attr != 0)
4560 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004561# ifdef FEAT_TEXT_PROP
4562 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004563 {
4564 char_attr = hl_combine_attr(
4565 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4566 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004567# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004568 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004569 || vcol < fromcol || vcol_prev < fromcol_prev
4570 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004571 // Use line_attr when not in the Visual or 'incsearch' area
4572 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004573 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004574#else
4575 if (area_attr != 0)
4576 char_attr = area_attr;
4577 else if (search_attr != 0)
4578 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004579#endif
4580 else
4581 {
4582 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004583#ifdef FEAT_TEXT_PROP
4584 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004585 {
4586 if (text_prop_combine)
4587 char_attr = hl_combine_attr(
4588 syntax_attr, text_prop_attr);
4589 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004590 char_attr = hl_combine_attr(
4591 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004592 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004593 else
4594#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004595#ifdef FEAT_SYN_HL
4596 if (has_syntax)
4597 char_attr = syntax_attr;
4598 else
4599#endif
4600 char_attr = 0;
4601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004603 if (char_attr == 0)
4604 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 /*
4607 * Get the next character to put on the screen.
4608 */
4609 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004610 * The "p_extra" points to the extra stuff that is inserted to
4611 * represent special characters (non-printable stuff) and other
4612 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004613 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004614 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4615 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4617 */
4618 if (n_extra > 0)
4619 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004620 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004622 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004624 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
4626 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004627 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004628 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 else
4631 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
4633 else
4634 {
4635 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (has_mbyte)
4637 {
4638 mb_c = c;
4639 if (enc_utf8)
4640 {
4641 /* If the UTF-8 character is more than one byte:
4642 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004643 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 mb_utf8 = FALSE;
4645 if (mb_l > n_extra)
4646 mb_l = 1;
4647 else if (mb_l > 1)
4648 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004649 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004651 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 }
4654 else
4655 {
4656 /* if this is a DBCS character, put it in "mb_c" */
4657 mb_l = MB_BYTE2LEN(c);
4658 if (mb_l >= n_extra)
4659 mb_l = 1;
4660 else if (mb_l > 1)
4661 mb_c = (c << 8) + p_extra[1];
4662 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004663 if (mb_l == 0) /* at the NUL at end-of-line */
4664 mb_l = 1;
4665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 /* If a double-width char doesn't fit display a '>' in the
4667 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004668 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669# ifdef FEAT_RIGHTLEFT
4670 wp->w_p_rl ? (col <= 0) :
4671# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004672 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 && (*mb_char2cells)(mb_c) == 2)
4674 {
4675 c = '>';
4676 mb_c = c;
4677 mb_l = 1;
4678 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004679 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 /* put the pointer back to output the double-width
4681 * character at the start of the next line. */
4682 ++n_extra;
4683 --p_extra;
4684 }
4685 else
4686 {
4687 n_extra -= mb_l - 1;
4688 p_extra += mb_l - 1;
4689 }
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 ++p_extra;
4692 }
4693 --n_extra;
4694 }
4695 else
4696 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004697#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004698 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004699#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004700
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004701 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004702 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
4704 * Get a character from the line itself.
4705 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004706 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004707#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004708 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (has_mbyte)
4711 {
4712 mb_c = c;
4713 if (enc_utf8)
4714 {
4715 /* If the UTF-8 character is more than one byte: Decode it
4716 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004717 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 mb_utf8 = FALSE;
4719 if (mb_l > 1)
4720 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004721 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 /* Overlong encoded ASCII or ASCII with composing char
4723 * is displayed normally, except a NUL. */
4724 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004725 {
4726 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004727#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004728 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004729#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004732
4733 /* At start of the line we can have a composing char.
4734 * Draw it as a space with a composing char. */
4735 if (utf_iscomposing(mb_c))
4736 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004737 int i;
4738
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004739 for (i = Screen_mco - 1; i > 0; --i)
4740 u8cc[i] = u8cc[i - 1];
4741 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004742 mb_c = ' ';
4743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745
4746 if ((mb_l == 1 && c >= 0x80)
4747 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004748 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 /*
4751 * Illegal UTF-8 byte: display as <xx>.
4752 * Non-BMP character : display as ? or fullwidth ?.
4753 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004754 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004755# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004756 if (wp->w_p_rl) /* reverse */
4757 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 p_extra = extra;
4760 c = *p_extra;
4761 mb_c = mb_ptr2char_adv(&p_extra);
4762 mb_utf8 = (c >= 0x80);
4763 n_extra = (int)STRLEN(p_extra);
4764 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004765 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (area_attr == 0 && search_attr == 0)
4767 {
4768 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004769 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 saved_attr2 = char_attr; /* save current attr */
4771 }
4772 }
4773 else if (mb_l == 0) /* at the NUL at end-of-line */
4774 mb_l = 1;
4775#ifdef FEAT_ARABIC
4776 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4777 {
4778 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004779 int pc, pc1, nc;
4780 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 /* The idea of what is the previous and next
4783 * character depends on 'rightleft'. */
4784 if (wp->w_p_rl)
4785 {
4786 pc = prev_c;
4787 pc1 = prev_c1;
4788 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004789 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 else
4792 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004793 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004795 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 prev_c = mb_c;
4798
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004799 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801 else
4802 prev_c = mb_c;
4803#endif
4804 }
4805 else /* enc_dbcs */
4806 {
4807 mb_l = MB_BYTE2LEN(c);
4808 if (mb_l == 0) /* at the NUL at end-of-line */
4809 mb_l = 1;
4810 else if (mb_l > 1)
4811 {
4812 /* We assume a second byte below 32 is illegal.
4813 * Hopefully this is OK for all double-byte encodings!
4814 */
4815 if (ptr[1] >= 32)
4816 mb_c = (c << 8) + ptr[1];
4817 else
4818 {
4819 if (ptr[1] == NUL)
4820 {
4821 /* head byte at end of line */
4822 mb_l = 1;
4823 transchar_nonprint(extra, c);
4824 }
4825 else
4826 {
4827 /* illegal tail byte */
4828 mb_l = 2;
4829 STRCPY(extra, "XX");
4830 }
4831 p_extra = extra;
4832 n_extra = (int)STRLEN(extra) - 1;
4833 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004834 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 c = *p_extra++;
4836 if (area_attr == 0 && search_attr == 0)
4837 {
4838 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004839 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 saved_attr2 = char_attr; /* save current attr */
4841 }
4842 mb_c = c;
4843 }
4844 }
4845 }
4846 /* If a double-width char doesn't fit display a '>' in the
4847 * last column; the character is displayed at the start of the
4848 * next line. */
4849 if ((
4850# ifdef FEAT_RIGHTLEFT
4851 wp->w_p_rl ? (col <= 0) :
4852# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004853 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 && (*mb_char2cells)(mb_c) == 2)
4855 {
4856 c = '>';
4857 mb_c = c;
4858 mb_utf8 = FALSE;
4859 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004860 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004861 // Put pointer back so that the character will be
4862 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004864#ifdef FEAT_CONCEAL
4865 did_decrement_ptr = TRUE;
4866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 else if (*ptr != NUL)
4869 ptr += mb_l - 1;
4870
4871 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004872 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004873 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004874 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004877 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004878 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 c = ' ';
4880 if (area_attr == 0 && search_attr == 0)
4881 {
4882 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004883 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 saved_attr2 = char_attr; /* save current attr */
4885 }
4886 mb_c = c;
4887 mb_utf8 = FALSE;
4888 mb_l = 1;
4889 }
4890
4891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 ++ptr;
4893
4894 if (extra_check)
4895 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004896#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004897 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004898#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004899
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004900#ifdef FEAT_TERMINAL
4901 if (get_term_attr)
4902 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004903 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004904
4905 if (!attr_pri)
4906 char_attr = syntax_attr;
4907 else
4908 char_attr = hl_combine_attr(syntax_attr, char_attr);
4909 }
4910#endif
4911
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004912#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004913 // Get syntax attribute, unless still at the start of the line
4914 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004915 v = (long)(ptr - line);
4916 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 /* Get the syntax attribute for the character. If there
4919 * is an error, disable syntax highlighting. */
4920 save_did_emsg = did_emsg;
4921 did_emsg = FALSE;
4922
Bram Moolenaar217ad922005-03-20 22:37:15 +00004923 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004924# ifdef FEAT_SPELL
4925 has_spell ? &can_spell :
4926# endif
4927 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004930 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004931 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004932 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004933 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 else
4936 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004937
4938 // combine syntax attribute with 'wincolor'
4939 if (win_attr != 0)
4940 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4941
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004942#ifdef SYN_TIME_LIMIT
4943 if (wp->w_s->b_syn_slow)
4944 has_syntax = FALSE;
4945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947 /* Need to get the line again, a multi-line regexp may
4948 * have made it invalid. */
4949 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4950 ptr = line + v;
4951
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004952# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004953 // Text properties overrule syntax highlighting or combine.
4954 if (text_prop_attr == 0 || text_prop_combine)
4955# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004956 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004957 int comb_attr = syntax_attr;
4958# ifdef FEAT_TEXT_PROP
4959 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4960# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004961 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004962 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004963 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004964 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004965 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004966# ifdef FEAT_CONCEAL
4967 /* no concealing past the end of the line, it interferes
4968 * with line highlighting */
4969 if (c == NUL)
4970 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004971 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004972 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004975#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004976
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004977#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004978 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004979 * Only do this when there is no syntax highlighting, the
4980 * @Spell cluster is not used or the current syntax item
4981 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004982 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004983 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004984 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004985 if (c != 0 && (
4986# ifdef FEAT_SYN_HL
4987 !has_syntax ||
4988# endif
4989 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004990 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004991 char_u *prev_ptr, *p;
4992 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004993 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004994 if (has_mbyte)
4995 {
4996 prev_ptr = ptr - mb_l;
4997 v -= mb_l - 1;
4998 }
4999 else
Bram Moolenaare7566042005-06-17 22:00:15 +00005000 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00005001
5002 /* Use nextline[] if possible, it has the start of the
5003 * next line concatenated. */
5004 if ((prev_ptr - line) - nextlinecol >= 0)
5005 p = nextline + (prev_ptr - line) - nextlinecol;
5006 else
5007 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005008 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005009 len = spell_check(wp, p, &spell_hlf, &cap_col,
5010 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005011 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005012
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005013 /* In Insert mode only highlight a word that
5014 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005015 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005016 && (State & INSERT) != 0
5017 && wp->w_cursor.lnum == lnum
5018 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00005019 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005020 && wp->w_cursor.col < (colnr_T)word_end)
5021 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005022 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005023 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00005024 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00005025
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005026 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00005027 && (p - nextline) + len > nextline_idx)
5028 {
5029 /* Remember that the good word continues at the
5030 * start of the next line. */
5031 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005032 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00005033 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005034
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005035 /* Turn index into actual attributes. */
5036 if (spell_hlf != HLF_COUNT)
5037 spell_attr = highlight_attr[spell_hlf];
5038
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005039 if (cap_col > 0)
5040 {
5041 if (p != prev_ptr
5042 && (p - nextline) + cap_col >= nextline_idx)
5043 {
5044 /* Remember that the word in the next line
5045 * must start with a capital. */
5046 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005047 cap_col = (int)((p - nextline) + cap_col
5048 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005049 }
5050 else
5051 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005052 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00005053 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005054 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00005055 }
5056 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005057 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005058 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00005059 char_attr = hl_combine_attr(char_attr, spell_attr);
5060 else
5061 char_attr = hl_combine_attr(spell_attr, char_attr);
5062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063#endif
5064#ifdef FEAT_LINEBREAK
5065 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00005066 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01005068 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01005069 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01005071 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01005072 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01005073
Bram Moolenaar597a4222014-06-25 14:39:50 +02005074 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005075 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02005076 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02005077 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005078# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02005079 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005080 wp->w_buffer->b_p_vts_array) - 1;
5081# else
Bram Moolenaara3650912014-11-19 13:21:57 +01005082 n_extra = (int)wp->w_buffer->b_p_ts
5083 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005084# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01005085
Bram Moolenaar4df70292015-03-21 14:20:16 +01005086 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01005087 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01005088 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005089 {
5090#ifdef FEAT_CONCEAL
5091 if (c == TAB)
5092 /* See "Tab alignment" below. */
5093 FIX_FOR_BOGUSCOLS;
5094#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005095 if (!wp->w_p_list)
5096 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02005097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099#endif
5100
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005101 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
5102 // But not when the character is followed by a composing
5103 // character (use mb_l to check that).
5104 if (wp->w_p_list
5105 && ((((c == 160 && mb_l == 1)
5106 || (mb_utf8
5107 && ((mb_c == 160 && mb_l == 2)
5108 || (mb_c == 0x202f && mb_l == 3))))
5109 && lcs_nbsp)
5110 || (c == ' '
5111 && mb_l == 1
5112 && lcs_space
5113 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005114 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005115 c = (c == ' ') ? lcs_space : lcs_nbsp;
5116 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005117 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005118 n_attr = 1;
5119 extra_attr = HL_ATTR(HLF_8);
5120 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005121 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005122 mb_c = c;
5123 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005124 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005125 mb_utf8 = TRUE;
5126 u8cc[0] = 0;
5127 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005128 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02005129 else
5130 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01005131 }
5132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
5134 {
5135 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005136 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005139 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 saved_attr2 = char_attr; /* save current attr */
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005143 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005146 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005147 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149 else
5150 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152 }
5153
5154 /*
5155 * Handling of non-printable characters.
5156 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005157 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
5159 /*
5160 * when getting a character from the file, we may have to
5161 * turn it into something else on the way to putting it
5162 * into "ScreenLines".
5163 */
5164 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5165 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005166 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005167 long vcol_adjusted = vcol; /* removed showbreak length */
5168#ifdef FEAT_LINEBREAK
5169 /* only adjust the tab_len, when at the first column
5170 * after the showbreak value was drawn */
5171 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5172 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005175#ifdef FEAT_VARTABS
5176 tab_len = tabstop_padding(vcol_adjusted,
5177 wp->w_buffer->b_p_ts,
5178 wp->w_buffer->b_p_vts_array) - 1;
5179#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005180 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005181 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5182#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005183
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005184#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005185 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005186#endif
5187 /* tab amount depends on current column */
5188 n_extra = tab_len;
5189#ifdef FEAT_LINEBREAK
5190 else
5191 {
5192 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005193 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005194 int i;
5195 int saved_nextra = n_extra;
5196
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005197#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005198 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005199 /* there are characters to conceal */
5200 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005201 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5202 */
5203 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5204 && n_extra > tab_len)
5205 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005206#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005207
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005208 /* if n_extra > 0, it gives the number of chars, to
5209 * use for a tab, else we need to calculate the width
5210 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005211 len = (tab_len * mb_char2len(lcs_tab2));
5212 if (n_extra > 0)
5213 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005214 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005215 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005216 vim_memset(p, ' ', len);
5217 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005218 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005219 p_extra_free = p;
5220 for (i = 0; i < tab_len; i++)
5221 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005222 if (*p == NUL)
5223 {
5224 tab_len = i;
5225 break;
5226 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005227 mb_char2bytes(lcs_tab2, p);
5228 p += mb_char2len(lcs_tab2);
5229 n_extra += mb_char2len(lcs_tab2)
5230 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005231 }
5232 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005233#ifdef FEAT_CONCEAL
5234 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5235 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005236 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005237 n_extra -= vcol_off;
5238#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005239 }
5240#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005241#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005242 {
5243 int vc_saved = vcol_off;
5244
5245 /* Tab alignment should be identical regardless of
5246 * 'conceallevel' value. So tab compensates of all
5247 * previous concealed characters, and thus resets
5248 * vcol_off and boguscols accumulated so far in the
5249 * line. Note that the tab can be longer than
5250 * 'tabstop' when there are concealed characters. */
5251 FIX_FOR_BOGUSCOLS;
5252
5253 /* Make sure, the highlighting for the tab char will be
5254 * correctly set further below (effectively reverts the
5255 * FIX_FOR_BOGSUCOLS macro */
5256 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005257 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005258 tab_len += vc_saved;
5259 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (wp->w_p_list)
5263 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005264 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005265#ifdef FEAT_LINEBREAK
5266 if (wp->w_p_lbr)
5267 c_extra = NUL; /* using p_extra from above */
5268 else
5269#endif
5270 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005271 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005272 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005273 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005276 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005279 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005280 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283 else
5284 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005285 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 c_extra = ' ';
5287 c = ' ';
5288 }
5289 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005290 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005291 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005292 || ((fromcol >= 0 || fromcol_prev >= 0)
5293 && tocol > vcol
5294 && VIsual_mode != Ctrl_V
5295 && (
5296# ifdef FEAT_RIGHTLEFT
5297 wp->w_p_rl ? (col >= 0) :
5298# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005299 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005300 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005301 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005302 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005303 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005305 /* Display a '$' after the line or highlight an extra
5306 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5308 /* For a diff line the highlighting continues after the
5309 * "$". */
5310 if (
5311# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005312 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313# ifdef LINE_ATTR
5314 &&
5315# endif
5316# endif
5317# ifdef LINE_ATTR
5318 line_attr == 0
5319# endif
5320 )
5321#endif
5322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 /* In virtualedit, visual selections may extend
5324 * beyond end of line. */
5325 if (area_highlighting && virtual_active()
5326 && tocol != MAXCOL && vcol < tocol)
5327 n_extra = 0;
5328 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
5330 p_extra = at_end_str;
5331 n_extra = 1;
5332 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005333 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005336 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005337 c = lcs_eol;
5338 else
5339 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 lcs_eol_one = -1;
5341 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005342 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005344 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 n_attr = 1;
5346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005348 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005351 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005352 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354 else
5355 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357 else if (c != NUL)
5358 {
5359 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005360 if (n_extra == 0)
5361 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362#ifdef FEAT_RIGHTLEFT
5363 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5364 rl_mirror(p_extra); /* reverse "<12>" */
5365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005367 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005368#ifdef FEAT_LINEBREAK
5369 if (wp->w_p_lbr)
5370 {
5371 char_u *p;
5372
5373 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005374 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005375 vim_memset(p, ' ', n_extra);
5376 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5377 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005378 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005379 p_extra_free = p_extra = p;
5380 }
5381 else
5382#endif
5383 {
5384 n_extra = byte2cells(c) - 1;
5385 c = *p_extra++;
5386 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005387 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
5389 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005390 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 saved_attr2 = char_attr; /* save current attr */
5392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else if (VIsual_active
5396 && (VIsual_mode == Ctrl_V
5397 || VIsual_mode == 'v')
5398 && virtual_active()
5399 && tocol != MAXCOL
5400 && vcol < tocol
5401 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005402#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005404#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005405 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 c = ' ';
5408 --ptr; /* put it back at the NUL */
5409 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005410#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 else if ((
5412# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005413 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005415# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005416 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 ) && (
5420# ifdef FEAT_RIGHTLEFT
5421 wp->w_p_rl ? (col >= 0) :
5422# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005423 (col
5424# ifdef FEAT_CONCEAL
5425 - boguscols
5426# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005427 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
5429 /* Highlight until the right side of the window */
5430 c = ' ';
5431 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005432
5433 /* Remember we do the char for line highlighting. */
5434 ++did_line_attr;
5435
5436 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005437 if (line_attr != 0 && char_attr == search_attr
5438 && (did_line_attr > 1
5439 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005440 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441# ifdef FEAT_DIFF
5442 if (diff_hlf == HLF_TXD)
5443 {
5444 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005445 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005446 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005447 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005448 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5449 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005450 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005454# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005455 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005456 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005457 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005458 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5459 char_attr = hl_combine_attr(char_attr,
5460 HL_ATTR(HLF_CUL));
5461 }
5462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464#endif
5465 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005466
5467#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005468 if ( wp->w_p_cole > 0
5469 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005470 conceal_cursor_line(wp))
5471 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005472 && !(lnum_in_visual_area
5473 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005474 {
5475 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005476 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005477 && (syn_get_sub_char() != NUL || match_conc
5478 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005479 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005480 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005481 /* First time at this concealed item: display one
5482 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005483 if (match_conc)
5484 c = match_conc;
5485 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005486 c = syn_get_sub_char();
5487 else if (lcs_conceal != NUL)
5488 c = lcs_conceal;
5489 else
5490 c = ' ';
5491
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005492 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005493
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005494 if (n_extra > 0)
5495 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005496 vcol += n_extra;
5497 if (wp->w_p_wrap && n_extra > 0)
5498 {
5499# ifdef FEAT_RIGHTLEFT
5500 if (wp->w_p_rl)
5501 {
5502 col -= n_extra;
5503 boguscols -= n_extra;
5504 }
5505 else
5506# endif
5507 {
5508 boguscols += n_extra;
5509 col += n_extra;
5510 }
5511 }
5512 n_extra = 0;
5513 n_attr = 0;
5514 }
5515 else if (n_skip == 0)
5516 {
5517 is_concealing = TRUE;
5518 n_skip = 1;
5519 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005520 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005521 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005522 {
5523 mb_utf8 = TRUE;
5524 u8cc[0] = 0;
5525 c = 0xc0;
5526 }
5527 else
5528 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005529 }
5530 else
5531 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005532 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005533 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005534 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005535
5536 if (n_skip > 0 && did_decrement_ptr)
5537 // not showing the '>', put pointer back to avoid getting stuck
5538 ++ptr;
5539
5540#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005543#ifdef FEAT_CONCEAL
5544 /* In the cursor line and we may be concealing characters: correct
5545 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005546 if (!did_wcol && draw_state == WL_LINE
5547 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005548 && conceal_cursor_line(wp)
5549 && (int)wp->w_virtcol <= vcol + n_skip)
5550 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005551# ifdef FEAT_RIGHTLEFT
5552 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005553 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005554 else
5555# endif
5556 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005557 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005558 did_wcol = TRUE;
5559 }
5560#endif
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 /* Don't override visual selection highlighting. */
5563 if (n_attr > 0
5564 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005565 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 char_attr = extra_attr;
5567
Bram Moolenaar81695252004-12-29 20:58:21 +00005568#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 /* XIM don't send preedit_start and preedit_end, but they send
5570 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5571 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005572 if (p_imst == IM_ON_THE_SPOT
5573 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005574 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 && (State & INSERT)
5576 && !p_imdisable
5577 && im_is_preediting()
5578 && draw_state == WL_LINE)
5579 {
5580 colnr_T tcol;
5581
5582 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005583 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 else
5585 tcol = preedit_end_col;
5586 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5587 {
5588 if (feedback_old_attr < 0)
5589 {
5590 feedback_col = 0;
5591 feedback_old_attr = char_attr;
5592 }
5593 char_attr = im_get_feedback_attr(feedback_col);
5594 if (char_attr < 0)
5595 char_attr = feedback_old_attr;
5596 feedback_col++;
5597 }
5598 else if (feedback_old_attr >= 0)
5599 {
5600 char_attr = feedback_old_attr;
5601 feedback_old_attr = -1;
5602 feedback_col = 0;
5603 }
5604 }
5605#endif
5606 /*
5607 * Handle the case where we are in column 0 but not on the first
5608 * character of the line and the user wants us to show us a
5609 * special character (via 'listchars' option "precedes:<char>".
5610 */
5611 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005612 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5614#ifdef FEAT_DIFF
5615 && filler_todo <= 0
5616#endif
5617 && draw_state > WL_NR
5618 && c != NUL)
5619 {
5620 c = lcs_prec;
5621 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005622 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5623 {
5624 /* Double-width character being overwritten by the "precedes"
5625 * character, need to fill up half the character. */
5626 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005627 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005628 n_extra = 1;
5629 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005630 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005633 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
5635 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005636 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005637 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639 else
5640 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005641 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 {
5643 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005644 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 n_attr3 = 1;
5646 }
5647 }
5648
5649 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005650 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005652 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005653#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005654 || did_line_attr == 1
5655#endif
5656 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005658#ifdef FEAT_SEARCH_EXTRA
5659 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005660
5661 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005662 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005663 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005664#endif
5665
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005666 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 * highlight match at end of line. If it's beyond the last
5668 * char on the screen, just overwrite that one (tricky!) Not
5669 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005670#ifdef FEAT_SEARCH_EXTRA
5671 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005672 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005673 prevcol_hl_flag = TRUE;
5674 else
5675 {
5676 cur = wp->w_match_head;
5677 while (cur != NULL)
5678 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005679 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005680 {
5681 prevcol_hl_flag = TRUE;
5682 break;
5683 }
5684 cur = cur->next;
5685 }
5686 }
5687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005689 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005690 && (VIsual_mode != Ctrl_V
5691 || lnum == VIsual.lnum
5692 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005693 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694#ifdef FEAT_SEARCH_EXTRA
5695 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005696 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005697# ifdef FEAT_SYN_HL
5698 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5699 && !(wp == curwin && VIsual_active))
5700# endif
5701# ifdef FEAT_DIFF
5702 && diff_hlf == (hlf_T)0
5703# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005704# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005705 && did_line_attr <= 1
5706# endif
5707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#endif
5709 ))
5710 {
5711 int n = 0;
5712
5713#ifdef FEAT_RIGHTLEFT
5714 if (wp->w_p_rl)
5715 {
5716 if (col < 0)
5717 n = 1;
5718 }
5719 else
5720#endif
5721 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005722 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 n = -1;
5724 }
5725 if (n != 0)
5726 {
5727 /* At the window boundary, highlight the last character
5728 * instead (better than nothing). */
5729 off += n;
5730 col += n;
5731 }
5732 else
5733 {
5734 /* Add a blank character to highlight. */
5735 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (enc_utf8)
5737 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739#ifdef FEAT_SEARCH_EXTRA
5740 if (area_attr == 0)
5741 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005742 /* Use attributes from match with highest priority among
5743 * 'search_hl' and the match list. */
5744 char_attr = search_hl.attr;
5745 cur = wp->w_match_head;
5746 shl_flag = FALSE;
5747 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005748 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005749 if (shl_flag == FALSE
5750 && ((cur != NULL
5751 && cur->priority > SEARCH_HL_PRIORITY)
5752 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005753 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005754 shl = &search_hl;
5755 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005756 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005757 else
5758 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005759 if ((ptr - line) - 1 == (long)shl->startcol
5760 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005761 char_attr = shl->attr;
5762 if (shl != &search_hl && cur != NULL)
5763 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766#endif
5767 ScreenAttrs[off] = char_attr;
5768#ifdef FEAT_RIGHTLEFT
5769 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005772 --off;
5773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 else
5775#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005776 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005778 ++off;
5779 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005780 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005781#ifdef FEAT_SYN_HL
5782 eol_hl_off = 1;
5783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
Bram Moolenaar91170f82006-05-05 21:15:17 +00005787 /*
5788 * At end of the text line.
5789 */
5790 if (c == NUL)
5791 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005792#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005793 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005794 if (wp->w_p_wrap)
5795 v = wp->w_skipcol;
5796 else
5797 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005798
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005799 /* check if line ends before left margin */
5800 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005801 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005802#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005803 // Get rid of the boguscols now, we want to draw until the right
5804 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005805 col -= boguscols;
5806 boguscols = 0;
5807#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005808
5809 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005810 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005811
5812 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005813 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5814 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005815 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005816 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005817 || draw_color_col
5818 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005819# ifdef FEAT_RIGHTLEFT
5820 && !wp->w_p_rl
5821# endif
5822 )
5823 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005824 int rightmost_vcol = 0;
5825 int i;
5826
5827 if (wp->w_p_cuc)
5828 rightmost_vcol = wp->w_virtcol;
5829 if (draw_color_col)
5830 /* determine rightmost colorcolumn to possibly draw */
5831 for (i = 0; color_cols[i] >= 0; ++i)
5832 if (rightmost_vcol < color_cols[i])
5833 rightmost_vcol = color_cols[i];
5834
Bram Moolenaar02631462017-09-22 15:20:32 +02005835 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005836 {
5837 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005838 if (enc_utf8)
5839 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005840 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005841 if (draw_color_col)
5842 draw_color_col = advance_color_col(VCOL_HLC,
5843 &color_cols);
5844
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005845 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005846 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005847 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005848 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005849 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005850 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005851
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005852 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005853 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005854
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005855 ++vcol;
5856 }
5857 }
5858#endif
5859
Bram Moolenaar53f81742017-09-22 14:35:51 +02005860 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005861 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 row++;
5863
5864 /*
5865 * Update w_cline_height and w_cline_folded if the cursor line was
5866 * updated (saves a call to plines() later).
5867 */
5868 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5869 {
5870 curwin->w_cline_row = startrow;
5871 curwin->w_cline_height = row - startrow;
5872#ifdef FEAT_FOLDING
5873 curwin->w_cline_folded = FALSE;
5874#endif
5875 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5876 }
5877
5878 break;
5879 }
5880
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005881 // Show "extends" character from 'listchars' if beyond the line end and
5882 // 'list' is set.
5883 if (lcs_ext != NUL
5884 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 && !wp->w_p_wrap
5886#ifdef FEAT_DIFF
5887 && filler_todo <= 0
5888#endif
5889 && (
5890#ifdef FEAT_RIGHTLEFT
5891 wp->w_p_rl ? col == 0 :
5892#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005893 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005895 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5897 {
5898 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005899 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005901 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
5903 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005904 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005905 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907 else
5908 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
5910
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005911#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005912 /* advance to the next 'colorcolumn' */
5913 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005914 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005915
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005916 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005917 * highlight the cursor position itself.
5918 * Also highlight the 'colorcolumn' if it is different than
5919 * 'cursorcolumn' */
5920 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005921 if (draw_state == WL_LINE && !lnum_in_visual_area
5922 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005923 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005924 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005925 && lnum != wp->w_cursor.lnum)
5926 {
5927 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005928 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005929 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005930 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005931 {
5932 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005933 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005934 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005935 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005936#endif
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 /*
5939 * Store character to be displayed.
5940 * Skip characters that are left of the screen for 'nowrap'.
5941 */
5942 vcol_prev = vcol;
5943 if (draw_state < WL_LINE || n_skip <= 0)
5944 {
5945 /*
5946 * Store the character.
5947 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005948#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5950 {
5951 /* A double-wide character is: put first halve in left cell. */
5952 --off;
5953 --col;
5954 }
5955#endif
5956 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005958 {
5959 if ((mb_c & 0xff00) == 0x8e00)
5960 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 else if (enc_utf8)
5964 {
5965 if (mb_utf8)
5966 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005967 int i;
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005970 if ((c & 0xff) == 0)
5971 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005972 for (i = 0; i < Screen_mco; ++i)
5973 {
5974 ScreenLinesC[i][off] = u8cc[i];
5975 if (u8cc[i] == 0)
5976 break;
5977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
5979 else
5980 ScreenLinesUC[off] = 0;
5981 }
5982 if (multi_attr)
5983 {
5984 ScreenAttrs[off] = multi_attr;
5985 multi_attr = 0;
5986 }
5987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 ScreenAttrs[off] = char_attr;
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5991 {
5992 /* Need to fill two screen columns. */
5993 ++off;
5994 ++col;
5995 if (enc_utf8)
5996 /* UTF-8: Put a 0 in the second screen char. */
5997 ScreenLines[off] = 0;
5998 else
5999 /* DBCS: Put second byte in the second screen char. */
6000 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01006001 if (draw_state > WL_NR
6002#ifdef FEAT_DIFF
6003 && filler_todo <= 0
6004#endif
6005 )
6006 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 /* When "tocol" is halfway a character, set it to the end of
6008 * the character, otherwise highlighting won't stop. */
6009 if (tocol == vcol)
6010 ++tocol;
6011#ifdef FEAT_RIGHTLEFT
6012 if (wp->w_p_rl)
6013 {
6014 /* now it's time to backup one cell */
6015 --off;
6016 --col;
6017 }
6018#endif
6019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020#ifdef FEAT_RIGHTLEFT
6021 if (wp->w_p_rl)
6022 {
6023 --off;
6024 --col;
6025 }
6026 else
6027#endif
6028 {
6029 ++off;
6030 ++col;
6031 }
6032 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006033#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006034 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02006035 {
6036 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02006037 ++vcol_off;
6038 if (n_extra > 0)
6039 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02006040 if (wp->w_p_wrap)
6041 {
6042 /*
6043 * Special voodoo required if 'wrap' is on.
6044 *
6045 * Advance the column indicator to force the line
6046 * drawing to wrap early. This will make the line
6047 * take up the same screen space when parts are concealed,
6048 * so that cursor line computations aren't messed up.
6049 *
6050 * To avoid the fictitious advance of 'col' causing
6051 * trailing junk to be written out of the screen line
6052 * we are building, 'boguscols' keeps track of the number
6053 * of bad columns we have advanced.
6054 */
6055 if (n_extra > 0)
6056 {
6057 vcol += n_extra;
6058# ifdef FEAT_RIGHTLEFT
6059 if (wp->w_p_rl)
6060 {
6061 col -= n_extra;
6062 boguscols -= n_extra;
6063 }
6064 else
6065# endif
6066 {
6067 col += n_extra;
6068 boguscols += n_extra;
6069 }
6070 n_extra = 0;
6071 n_attr = 0;
6072 }
6073
6074
Bram Moolenaar860cae12010-06-05 23:22:07 +02006075 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
6076 {
6077 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01006078# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02006079 if (wp->w_p_rl)
6080 {
6081 --boguscols;
6082 --col;
6083 }
6084 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01006085# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02006086 {
6087 ++boguscols;
6088 ++col;
6089 }
6090 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02006091
6092# ifdef FEAT_RIGHTLEFT
6093 if (wp->w_p_rl)
6094 {
6095 --boguscols;
6096 --col;
6097 }
6098 else
6099# endif
6100 {
6101 ++boguscols;
6102 ++col;
6103 }
6104 }
6105 else
6106 {
6107 if (n_extra > 0)
6108 {
6109 vcol += n_extra;
6110 n_extra = 0;
6111 n_attr = 0;
6112 }
6113 }
6114
6115 }
6116#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 else
6118 --n_skip;
6119
Bram Moolenaar64486672010-05-16 15:46:46 +02006120 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
6121 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00006122 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123#ifdef FEAT_DIFF
6124 && filler_todo <= 0
6125#endif
6126 )
6127 ++vcol;
6128
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006129#ifdef FEAT_SYN_HL
6130 if (vcol_save_attr >= 0)
6131 char_attr = vcol_save_attr;
6132#endif
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 /* restore attributes after "predeces" in 'listchars' */
6135 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
6136 char_attr = saved_attr3;
6137
6138 /* restore attributes after last 'listchars' or 'number' char */
6139 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
6140 char_attr = saved_attr2;
6141
6142 /*
6143 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00006144 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 */
6146 if ((
6147#ifdef FEAT_RIGHTLEFT
6148 wp->w_p_rl ? (col < 0) :
6149#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006150 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 && (*ptr != NUL
6152#ifdef FEAT_DIFF
6153 || filler_todo > 0
6154#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006155 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6157 )
6158 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006159#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006160 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006161 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006162 boguscols = 0;
6163#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006164 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006165 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 ++row;
6168 ++screen_row;
6169
6170 /* When not wrapping and finished diff lines, or when displayed
6171 * '$' and highlighting until last column, break here. */
6172 if ((!wp->w_p_wrap
6173#ifdef FEAT_DIFF
6174 && filler_todo <= 0
6175#endif
6176 ) || lcs_eol_one == -1)
6177 break;
6178
6179 /* When the window is too narrow draw all "@" lines. */
6180 if (draw_state != WL_LINE
6181#ifdef FEAT_DIFF
6182 && filler_todo <= 0
6183#endif
6184 )
6185 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006186 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 row = endrow;
6189 }
6190
6191 /* When line got too long for screen break here. */
6192 if (row == endrow)
6193 {
6194 ++row;
6195 break;
6196 }
6197
6198 if (screen_cur_row == screen_row - 1
6199#ifdef FEAT_DIFF
6200 && filler_todo <= 0
6201#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006202 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 /* Remember that the line wraps, used for modeless copy. */
6205 LineWraps[screen_row - 1] = TRUE;
6206
6207 /*
6208 * Special trick to make copy/paste of wrapped lines work with
6209 * xterm/screen: write an extra character beyond the end of
6210 * the line. This will work with all terminal types
6211 * (regardless of the xn,am settings).
6212 * Only do this on a fast tty.
6213 * Only do this if the cursor is on the current line
6214 * (something has been written in it).
6215 * Don't do this for the GUI.
6216 * Don't do this for double-width characters.
6217 * Don't do this for a window not at the right screen border.
6218 */
6219 if (p_tf
6220#ifdef FEAT_GUI
6221 && !gui.in_use
6222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006224 && ((*mb_off2cells)(LineOffset[screen_row],
6225 LineOffset[screen_row] + screen_Columns)
6226 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006228 + (int)Columns - 2,
6229 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006230 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 /* First make sure we are at the end of the screen line,
6233 * then output the same character again to let the
6234 * terminal know about the wrap. If the terminal doesn't
6235 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006236 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 screen_char(LineOffset[screen_row - 1]
6238 + (unsigned)Columns - 1,
6239 screen_row - 1, (int)(Columns - 1));
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 /* When there is a multi-byte character, just output a
6242 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006243 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6244 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 out_char(' ');
6246 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 out_char(ScreenLines[LineOffset[screen_row - 1]
6248 + (Columns - 1)]);
6249 /* force a redraw of the first char on the next line */
6250 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6251 screen_start(); /* don't know where cursor is now */
6252 }
6253 }
6254
6255 col = 0;
6256 off = (unsigned)(current_ScreenLine - ScreenLines);
6257#ifdef FEAT_RIGHTLEFT
6258 if (wp->w_p_rl)
6259 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006260 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 off += col;
6262 }
6263#endif
6264
6265 /* reset the drawing state for the start of a wrapped line */
6266 draw_state = WL_START;
6267 saved_n_extra = n_extra;
6268 saved_p_extra = p_extra;
6269 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006270 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 saved_char_attr = char_attr;
6272 n_extra = 0;
6273 lcs_prec_todo = lcs_prec;
6274#ifdef FEAT_LINEBREAK
6275# ifdef FEAT_DIFF
6276 if (filler_todo <= 0)
6277# endif
6278 need_showbreak = TRUE;
6279#endif
6280#ifdef FEAT_DIFF
6281 --filler_todo;
6282 /* When the filler lines are actually below the last line of the
6283 * file, don't draw the line itself, break here. */
6284 if (filler_todo == 0 && wp->w_botfill)
6285 break;
6286#endif
6287 }
6288
6289 } /* for every character in the line */
6290
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006291#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006292 /* After an empty line check first word for capital. */
6293 if (*skipwhite(line) == NUL)
6294 {
6295 capcol_lnum = lnum + 1;
6296 cap_col = 0;
6297 }
6298#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006299#ifdef FEAT_TEXT_PROP
6300 vim_free(text_props);
6301 vim_free(text_prop_idxs);
6302#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006303
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006304 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 return row;
6306}
6307
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006308/*
6309 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006310 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006311 */
6312 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006313comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006314{
6315 int i;
6316
6317 for (i = 0; i < Screen_mco; ++i)
6318 {
6319 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6320 return TRUE;
6321 if (ScreenLinesC[i][off_from] == 0)
6322 break;
6323 }
6324 return FALSE;
6325}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006326
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327/*
6328 * Check whether the given character needs redrawing:
6329 * - the (first byte of the) character is different
6330 * - the attributes are different
6331 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006332 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 */
6334 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006335char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 if (cols > 0
6338 && ((ScreenLines[off_from] != ScreenLines[off_to]
6339 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 || (enc_dbcs != 0
6341 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6342 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6343 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6344 : (cols > 1 && ScreenLines[off_from + 1]
6345 != ScreenLines[off_to + 1])))
6346 || (enc_utf8
6347 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6348 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006349 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006350 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6351 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006352 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return TRUE;
6354 return FALSE;
6355}
6356
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006357#if defined(FEAT_TERMINAL) || defined(PROTO)
6358/*
6359 * Return the index in ScreenLines[] for the current screen line.
6360 */
6361 int
6362screen_get_current_line_off()
6363{
6364 return (int)(current_ScreenLine - ScreenLines);
6365}
6366#endif
6367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368/*
6369 * Move one "cooked" screen line to the screen, but only the characters that
6370 * have actually changed. Handle insert/delete character.
6371 * "coloff" gives the first column on the screen for this line.
6372 * "endcol" gives the columns where valid characters are.
6373 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6374 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006375 * "flags" can have bits:
6376 * SLF_POPUP popup window
6377 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6379 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6380 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006381 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006382screen_line(
6383 int row,
6384 int coloff,
6385 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006386 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006387 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 unsigned off_from;
6390 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006391 unsigned max_off_from;
6392 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 int force = FALSE; /* force update rest of the line */
6396 int redraw_this /* bool: does character need redraw? */
6397#ifdef FEAT_GUI
6398 = TRUE /* For GUI when while-loop empty */
6399#endif
6400 ;
6401 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 int clear_next = FALSE;
6403 int char_cells; /* 1: normal char */
6404 /* 2: occupies two display cells */
6405# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006407 /* Check for illegal row and col, just in case. */
6408 if (row >= Rows)
6409 row = Rows - 1;
6410 if (endcol > Columns)
6411 endcol = Columns;
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413# ifdef FEAT_CLIPBOARD
6414 clip_may_clear_selection(row, row);
6415# endif
6416
6417 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6418 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006419 max_off_from = off_from + screen_Columns;
6420 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006423 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
6425 /* Clear rest first, because it's left of the text. */
6426 if (clear_width > 0)
6427 {
6428 while (col <= endcol && ScreenLines[off_to] == ' '
6429 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006430 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
6432 ++off_to;
6433 ++col;
6434 }
6435 if (col <= endcol)
6436 screen_fill(row, row + 1, col + coloff,
6437 endcol + coloff + 1, ' ', ' ', 0);
6438 }
6439 col = endcol + 1;
6440 off_to = LineOffset[row] + col + coloff;
6441 off_from += col;
6442 endcol = (clear_width > 0 ? clear_width : -clear_width);
6443 }
6444#endif /* FEAT_RIGHTLEFT */
6445
6446 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6447
6448 while (col < endcol)
6449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006451 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 else
6453 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 redraw_this = redraw_next;
6456 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6457 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6458
6459#ifdef FEAT_GUI
6460 /* If the next character was bold, then redraw the current character to
6461 * remove any pixels that might have spilt over into us. This only
6462 * happens in the GUI.
6463 */
6464 if (redraw_next && gui.in_use)
6465 {
6466 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006467 if (hl > HL_ALL)
6468 hl = syn_attr2attr(hl);
6469 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 redraw_this = TRUE;
6471 }
6472#endif
6473
6474 if (redraw_this)
6475 {
6476 /*
6477 * Special handling when 'xs' termcap flag set (hpterm):
6478 * Attributes for characters are stored at the position where the
6479 * cursor is when writing the highlighting code. The
6480 * start-highlighting code must be written with the cursor on the
6481 * first highlighted character. The stop-highlighting code must
6482 * be written with the cursor just after the last highlighted
6483 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006484 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 * to clear the rest of the line, and force redrawing it
6486 * completely.
6487 */
6488 if ( p_wiv
6489 && !force
6490#ifdef FEAT_GUI
6491 && !gui.in_use
6492#endif
6493 && ScreenAttrs[off_to] != 0
6494 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6495 {
6496 /*
6497 * Need to remove highlighting attributes here.
6498 */
6499 windgoto(row, col + coloff);
6500 out_str(T_CE); /* clear rest of this screen line */
6501 screen_start(); /* don't know where cursor is now */
6502 force = TRUE; /* force redraw of rest of the line */
6503 redraw_next = TRUE; /* or else next char would miss out */
6504
6505 /*
6506 * If the previous character was highlighted, need to stop
6507 * highlighting at this character.
6508 */
6509 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6510 {
6511 screen_attr = ScreenAttrs[off_to - 1];
6512 term_windgoto(row, col + coloff);
6513 screen_stop_highlight();
6514 }
6515 else
6516 screen_attr = 0; /* highlighting has stopped */
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (enc_dbcs != 0)
6519 {
6520 /* Check if overwriting a double-byte with a single-byte or
6521 * the other way around requires another character to be
6522 * redrawn. For UTF-8 this isn't needed, because comparing
6523 * ScreenLinesUC[] is sufficient. */
6524 if (char_cells == 1
6525 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006526 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
6528 /* Writing a single-cell character over a double-cell
6529 * character: need to redraw the next cell. */
6530 ScreenLines[off_to + 1] = 0;
6531 redraw_next = TRUE;
6532 }
6533 else if (char_cells == 2
6534 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006535 && (*mb_off2cells)(off_to, max_off_to) == 1
6536 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
6538 /* Writing the second half of a double-cell character over
6539 * a double-cell character: need to redraw the second
6540 * cell. */
6541 ScreenLines[off_to + 2] = 0;
6542 redraw_next = TRUE;
6543 }
6544
6545 if (enc_dbcs == DBCS_JPNU)
6546 ScreenLines2[off_to] = ScreenLines2[off_from];
6547 }
6548 /* When writing a single-width character over a double-width
6549 * character and at the end of the redrawn text, need to clear out
6550 * the right halve of the old character.
6551 * Also required when writing the right halve of a double-width
6552 * char over the left halve of an existing one. */
6553 if (has_mbyte && col + char_cells == endcol
6554 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006555 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006557 && (*mb_off2cells)(off_to, max_off_to) == 1
6558 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (enc_utf8)
6563 {
6564 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6565 if (ScreenLinesUC[off_from] != 0)
6566 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006567 int i;
6568
6569 for (i = 0; i < Screen_mco; ++i)
6570 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572 }
6573 if (char_cells == 2)
6574 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
6576#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006577 /* The bold trick makes a single column of pixels appear in the
6578 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 * character should be redrawn too. This happens for our own GUI
6580 * and for some xterms. */
6581 if (
6582# ifdef FEAT_GUI
6583 gui.in_use
6584# endif
6585# if defined(FEAT_GUI) && defined(UNIX)
6586 ||
6587# endif
6588# ifdef UNIX
6589 term_is_xterm
6590# endif
6591 )
6592 {
6593 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006594 if (hl > HL_ALL)
6595 hl = syn_attr2attr(hl);
6596 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 redraw_next = TRUE;
6598 }
6599#endif
6600 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006601
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006602 /* For simplicity set the attributes of second half of a
6603 * double-wide character equal to the first half. */
6604 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006606
6607 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 screen_char(off_to, row, col + coloff);
6611 }
6612 else if ( p_wiv
6613#ifdef FEAT_GUI
6614 && !gui.in_use
6615#endif
6616 && col + coloff > 0)
6617 {
6618 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6619 {
6620 /*
6621 * Don't output stop-highlight when moving the cursor, it will
6622 * stop the highlighting when it should continue.
6623 */
6624 screen_attr = 0;
6625 }
6626 else if (screen_attr != 0)
6627 screen_stop_highlight();
6628 }
6629
6630 off_to += CHAR_CELLS;
6631 off_from += CHAR_CELLS;
6632 col += CHAR_CELLS;
6633 }
6634
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 if (clear_next)
6636 {
6637 /* Clear the second half of a double-wide character of which the left
6638 * half was overwritten with a single-wide character. */
6639 ScreenLines[off_to] = ' ';
6640 if (enc_utf8)
6641 ScreenLinesUC[off_to] = 0;
6642 screen_char(off_to, row, col + coloff);
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 if (clear_width > 0
6646#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006647 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648#endif
6649 )
6650 {
6651#ifdef FEAT_GUI
6652 int startCol = col;
6653#endif
6654
6655 /* blank out the rest of the line */
6656 while (col < clear_width && ScreenLines[off_to] == ' '
6657 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006658 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 {
6660 ++off_to;
6661 ++col;
6662 }
6663 if (col < clear_width)
6664 {
6665#ifdef FEAT_GUI
6666 /*
6667 * In the GUI, clearing the rest of the line may leave pixels
6668 * behind if the first character cleared was bold. Some bold
6669 * fonts spill over the left. In this case we redraw the previous
6670 * character too. If we didn't skip any blanks above, then we
6671 * only redraw if the character wasn't already redrawn anyway.
6672 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006673 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 hl = ScreenAttrs[off_to];
6676 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006677 {
6678 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006679
Bram Moolenaar9c697322006-10-09 20:11:17 +00006680 if (enc_utf8)
6681 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6682 * that its width is 2. */
6683 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6684 else if (enc_dbcs != 0)
6685 {
6686 /* find previous character by counting from first
6687 * column and get its width. */
6688 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006689 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006690
6691 while (off < off_to)
6692 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006693 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006694 off += prev_cells;
6695 }
6696 }
6697
6698 if (enc_dbcs != 0 && prev_cells > 1)
6699 screen_char_2(off_to - prev_cells, row,
6700 col + coloff - prev_cells);
6701 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006702 screen_char(off_to - prev_cells, row,
6703 col + coloff - prev_cells);
6704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 }
6706#endif
6707 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6708 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 off_to += clear_width - col;
6710 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712 }
6713
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006714 if (clear_width > 0
6715#ifdef FEAT_TEXT_PROP
6716 && !(flags & SLF_POPUP) // no separator for popup window
6717#endif
6718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006720 // For a window that has a right neighbor, draw the separator char
6721 // right of the window contents.
6722 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
6724 int c;
6725
6726 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006727 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006728 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6729 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 || ScreenAttrs[off_to] != hl)
6731 {
6732 ScreenLines[off_to] = c;
6733 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (enc_utf8)
6735 {
6736 if (c >= 0x80)
6737 {
6738 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006739 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 else
6742 ScreenLinesUC[off_to] = 0;
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 screen_char(off_to, row, col + coloff);
6745 }
6746 }
6747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 LineWraps[row] = FALSE;
6749 }
6750}
6751
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006752#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006754 * Mirror text "str" for right-left displaying.
6755 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006757 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006758rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 char_u *p1, *p2;
6761 int t;
6762
6763 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6764 {
6765 t = *p1;
6766 *p1 = *p2;
6767 *p2 = t;
6768 }
6769}
6770#endif
6771
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772/*
6773 * mark all status lines for redraw; used after first :cd
6774 */
6775 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006776status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 win_T *wp;
6779
Bram Moolenaar29323592016-07-24 22:04:11 +02006780 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (wp->w_status_height)
6782 {
6783 wp->w_redr_status = TRUE;
6784 redraw_later(VALID);
6785 }
6786}
6787
6788/*
6789 * mark all status lines of the current buffer for redraw
6790 */
6791 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006792status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 win_T *wp;
6795
Bram Moolenaar29323592016-07-24 22:04:11 +02006796 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6798 {
6799 wp->w_redr_status = TRUE;
6800 redraw_later(VALID);
6801 }
6802}
6803
6804/*
6805 * Redraw all status lines that need to be redrawn.
6806 */
6807 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006808redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809{
6810 win_T *wp;
6811
Bram Moolenaar29323592016-07-24 22:04:11 +02006812 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006814 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006815 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006816 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +02006817
6818#ifdef FEAT_TEXT_PROP
6819 // Display popup windows on top of the status lines.
6820 update_popups();
6821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaar4033c552017-09-16 20:54:51 +02006824#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825/*
6826 * Redraw all status lines at the bottom of frame "frp".
6827 */
6828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006829win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 if (frp->fr_layout == FR_LEAF)
6832 frp->fr_win->w_redr_status = TRUE;
6833 else if (frp->fr_layout == FR_ROW)
6834 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006835 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 win_redraw_last_status(frp);
6837 }
6838 else /* frp->fr_layout == FR_COL */
6839 {
6840 frp = frp->fr_child;
6841 while (frp->fr_next != NULL)
6842 frp = frp->fr_next;
6843 win_redraw_last_status(frp);
6844 }
6845}
6846#endif
6847
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848/*
6849 * Draw the verticap separator right of window "wp" starting with line "row".
6850 */
6851 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006852draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 int hl;
6855 int c;
6856
6857 if (wp->w_vsep_width)
6858 {
6859 /* draw the vertical separator right of this window */
6860 c = fillchar_vsep(&hl);
6861 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6862 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6863 c, ' ', hl);
6864 }
6865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866
6867#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006868static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869
6870/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006871 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 */
6873 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006874status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 int len = 0;
6877
6878#ifdef FEAT_MENU
6879 int emenu = (xp->xp_context == EXPAND_MENUS
6880 || xp->xp_context == EXPAND_MENUNAMES);
6881
6882 /* Check for menu separators - replace with '|'. */
6883 if (emenu && menu_is_separator(s))
6884 return 1;
6885#endif
6886
6887 while (*s != NUL)
6888 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006889 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006890 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006891 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893
6894 return len;
6895}
6896
6897/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006898 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006899 * These are backslashes used for escaping. Do show backslashes in help tags.
6900 */
6901 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006902skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006903{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006904 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006905#ifdef FEAT_MENU
6906 || ((xp->xp_context == EXPAND_MENUS
6907 || xp->xp_context == EXPAND_MENUNAMES)
6908 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6909#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006910 )
6911 {
6912#ifndef BACKSLASH_IN_FILENAME
6913 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6914 return 2;
6915#endif
6916 return 1;
6917 }
6918 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006919}
6920
6921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 * Show wildchar matches in the status line.
6923 * Show at least the "match" item.
6924 * We start at item 'first_match' in the list and show all matches that fit.
6925 *
6926 * If inversion is possible we use it. Else '=' characters are used.
6927 */
6928 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006929win_redr_status_matches(
6930 expand_T *xp,
6931 int num_matches,
6932 char_u **matches, /* list of matches */
6933 int match,
6934 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6937 int row;
6938 char_u *buf;
6939 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006940 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 int fillchar;
6942 int attr;
6943 int i;
6944 int highlight = TRUE;
6945 char_u *selstart = NULL;
6946 int selstart_col = 0;
6947 char_u *selend = NULL;
6948 static int first_match = 0;
6949 int add_left = FALSE;
6950 char_u *s;
6951#ifdef FEAT_MENU
6952 int emenu;
6953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 if (matches == NULL) /* interrupted completion? */
6957 return;
6958
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006959 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006960 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006961 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006962 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (buf == NULL)
6964 return;
6965
6966 if (match == -1) /* don't show match but original text */
6967 {
6968 match = 0;
6969 highlight = FALSE;
6970 }
6971 /* count 1 for the ending ">" */
6972 clen = status_match_len(xp, L_MATCH(match)) + 3;
6973 if (match == 0)
6974 first_match = 0;
6975 else if (match < first_match)
6976 {
6977 /* jumping left, as far as we can go */
6978 first_match = match;
6979 add_left = TRUE;
6980 }
6981 else
6982 {
6983 /* check if match fits on the screen */
6984 for (i = first_match; i < match; ++i)
6985 clen += status_match_len(xp, L_MATCH(i)) + 2;
6986 if (first_match > 0)
6987 clen += 2;
6988 /* jumping right, put match at the left */
6989 if ((long)clen > Columns)
6990 {
6991 first_match = match;
6992 /* if showing the last match, we can add some on the left */
6993 clen = 2;
6994 for (i = match; i < num_matches; ++i)
6995 {
6996 clen += status_match_len(xp, L_MATCH(i)) + 2;
6997 if ((long)clen >= Columns)
6998 break;
6999 }
7000 if (i == num_matches)
7001 add_left = TRUE;
7002 }
7003 }
7004 if (add_left)
7005 while (first_match > 0)
7006 {
7007 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
7008 if ((long)clen >= Columns)
7009 break;
7010 --first_match;
7011 }
7012
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007013 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015 if (first_match == 0)
7016 {
7017 *buf = NUL;
7018 len = 0;
7019 }
7020 else
7021 {
7022 STRCPY(buf, "< ");
7023 len = 2;
7024 }
7025 clen = len;
7026
7027 i = first_match;
7028 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
7029 {
7030 if (i == match)
7031 {
7032 selstart = buf + len;
7033 selstart_col = clen;
7034 }
7035
7036 s = L_MATCH(i);
7037 /* Check for menu separators - replace with '|' */
7038#ifdef FEAT_MENU
7039 emenu = (xp->xp_context == EXPAND_MENUS
7040 || xp->xp_context == EXPAND_MENUNAMES);
7041 if (emenu && menu_is_separator(s))
7042 {
7043 STRCPY(buf + len, transchar('|'));
7044 l = (int)STRLEN(buf + len);
7045 len += l;
7046 clen += l;
7047 }
7048 else
7049#endif
7050 for ( ; *s != NUL; ++s)
7051 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007052 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007054 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 STRNCPY(buf + len, s, l);
7057 s += l - 1;
7058 len += l;
7059 }
7060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
7062 STRCPY(buf + len, transchar_byte(*s));
7063 len += (int)STRLEN(buf + len);
7064 }
7065 }
7066 if (i == match)
7067 selend = buf + len;
7068
7069 *(buf + len++) = ' ';
7070 *(buf + len++) = ' ';
7071 clen += 2;
7072 if (++i == num_matches)
7073 break;
7074 }
7075
7076 if (i != num_matches)
7077 {
7078 *(buf + len++) = '>';
7079 ++clen;
7080 }
7081
7082 buf[len] = NUL;
7083
7084 row = cmdline_row - 1;
7085 if (row >= 0)
7086 {
7087 if (wild_menu_showing == 0)
7088 {
7089 if (msg_scrolled > 0)
7090 {
7091 /* Put the wildmenu just above the command line. If there is
7092 * no room, scroll the screen one line up. */
7093 if (cmdline_row == Rows - 1)
7094 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02007095 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 ++msg_scrolled;
7097 }
7098 else
7099 {
7100 ++cmdline_row;
7101 ++row;
7102 }
7103 wild_menu_showing = WM_SCROLLED;
7104 }
7105 else
7106 {
7107 /* Create status line if needed by setting 'laststatus' to 2.
7108 * Set 'winminheight' to zero to avoid that the window is
7109 * resized. */
7110 if (lastwin->w_status_height == 0)
7111 {
7112 save_p_ls = p_ls;
7113 save_p_wmh = p_wmh;
7114 p_ls = 2;
7115 p_wmh = 0;
7116 last_status(FALSE);
7117 }
7118 wild_menu_showing = WM_SHOWN;
7119 }
7120 }
7121
7122 screen_puts(buf, row, 0, attr);
7123 if (selstart != NULL && highlight)
7124 {
7125 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01007126 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 }
7128
7129 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
7130 }
7131
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 vim_free(buf);
7134}
7135#endif
7136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137/*
7138 * Redraw the status line of window wp.
7139 *
7140 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007141 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
7142 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007144 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02007145win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147 int row;
7148 char_u *p;
7149 int len;
7150 int fillchar;
7151 int attr;
7152 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007153 static int busy = FALSE;
7154
7155 /* It's possible to get here recursively when 'statusline' (indirectly)
7156 * invokes ":redrawstatus". Simply ignore the call then. */
7157 if (busy)
7158 return;
7159 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
7161 wp->w_redr_status = FALSE;
7162 if (wp->w_status_height == 0)
7163 {
7164 /* no status line, can only be last window */
7165 redraw_cmdline = TRUE;
7166 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007167 else if (!redrawing()
7168#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007169 // don't update status line when popup menu is visible and may be
7170 // drawn over it, unless it will be redrawn later
7171 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007172#endif
7173 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {
7175 /* Don't redraw right now, do it later. */
7176 wp->w_redr_status = TRUE;
7177 }
7178#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007179 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 {
7181 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007182 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184#endif
7185 else
7186 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007187 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007189 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 p = NameBuff;
7191 len = (int)STRLEN(p);
7192
Bram Moolenaard85f2712017-07-28 21:51:57 +02007193 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194#ifdef FEAT_QUICKFIX
7195 || wp->w_p_pvw
7196#endif
7197 || bufIsChanged(wp->w_buffer)
7198 || wp->w_buffer->b_p_ro)
7199 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007200 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007202 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 len += (int)STRLEN(p + len);
7204 }
7205#ifdef FEAT_QUICKFIX
7206 if (wp->w_p_pvw)
7207 {
7208 STRCPY(p + len, _("[Preview]"));
7209 len += (int)STRLEN(p + len);
7210 }
7211#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007212 if (bufIsChanged(wp->w_buffer)
7213#ifdef FEAT_TERMINAL
7214 && !bt_terminal(wp->w_buffer)
7215#endif
7216 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
7218 STRCPY(p + len, "[+]");
7219 len += 3;
7220 }
7221 if (wp->w_buffer->b_p_ro)
7222 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007223 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007224 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226
Bram Moolenaar02631462017-09-22 15:20:32 +02007227 this_ru_col = ru_col - (Columns - wp->w_width);
7228 if (this_ru_col < (wp->w_width + 1) / 2)
7229 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (this_ru_col <= 1)
7231 {
7232 p = (char_u *)"<"; /* No room for file name! */
7233 len = 1;
7234 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007235 else if (has_mbyte)
7236 {
7237 int clen = 0, i;
7238
7239 /* Count total number of display cells. */
7240 clen = mb_string2cells(p, -1);
7241
7242 /* Find first character that will fit.
7243 * Going from start to end is much faster for DBCS. */
7244 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7245 i += (*mb_ptr2len)(p + i))
7246 clen -= (*mb_ptr2cells)(p + i);
7247 len = clen;
7248 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007250 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007252 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254
Bram Moolenaara12a1612019-01-24 16:39:02 +01007255 }
7256 else if (len > this_ru_col - 1)
7257 {
7258 p += len - (this_ru_col - 1);
7259 *p = '<';
7260 len = this_ru_col - 1;
7261 }
7262
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007264 screen_puts(p, row, wp->w_wincol, attr);
7265 screen_fill(row, row + 1, len + wp->w_wincol,
7266 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007268 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7270 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007271 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
7273#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007274 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#endif
7276 }
7277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 /*
7279 * May need to draw the character below the vertical separator.
7280 */
7281 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7282 {
7283 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007284 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 else
7286 fillchar = fillchar_vsep(&attr);
7287 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7288 attr);
7289 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007290 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291}
7292
Bram Moolenaar238a5642006-02-21 22:12:05 +00007293#ifdef FEAT_STL_OPT
7294/*
7295 * Redraw the status line according to 'statusline' and take care of any
7296 * errors encountered.
7297 */
7298 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007299redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007300{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007301 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007302 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007303
7304 /* When called recursively return. This can happen when the statusline
7305 * contains an expression that triggers a redraw. */
7306 if (entered)
7307 return;
7308 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007309
Bram Moolenaara742e082016-04-05 21:10:38 +02007310 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007311 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007312 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007313 {
7314 /* When there is an error disable the statusline, otherwise the
7315 * display is messed up with errors and a redraw triggers the problem
7316 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007317 set_string_option_direct((char_u *)"statusline", -1,
7318 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007319 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007320 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007321 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007322 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007323}
7324#endif
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326/*
7327 * Return TRUE if the status line of window "wp" is connected to the status
7328 * line of the window right of it. If not, then it's a vertical separator.
7329 * Only call if (wp->w_vsep_width != 0).
7330 */
7331 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007332stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
7334 frame_T *fr;
7335
7336 fr = wp->w_frame;
7337 while (fr->fr_parent != NULL)
7338 {
7339 if (fr->fr_parent->fr_layout == FR_COL)
7340 {
7341 if (fr->fr_next != NULL)
7342 break;
7343 }
7344 else
7345 {
7346 if (fr->fr_next != NULL)
7347 return TRUE;
7348 }
7349 fr = fr->fr_parent;
7350 }
7351 return FALSE;
7352}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355/*
7356 * Get the value to show for the language mappings, active 'keymap'.
7357 */
7358 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007359get_keymap_str(
7360 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007361 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007362 char_u *buf, /* buffer for the result */
7363 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 char_u *p;
7366
7367 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7368 return FALSE;
7369
7370 {
7371#ifdef FEAT_EVAL
7372 buf_T *old_curbuf = curbuf;
7373 win_T *old_curwin = curwin;
7374 char_u *s;
7375
7376 curbuf = wp->w_buffer;
7377 curwin = wp;
7378 STRCPY(buf, "b:keymap_name"); /* must be writable */
7379 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007380 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 --emsg_skip;
7382 curbuf = old_curbuf;
7383 curwin = old_curwin;
7384 if (p == NULL || *p == NUL)
7385#endif
7386 {
7387#ifdef FEAT_KEYMAP
7388 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7389 p = wp->w_buffer->b_p_keymap;
7390 else
7391#endif
7392 p = (char_u *)"lang";
7393 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007394 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 buf[0] = NUL;
7396#ifdef FEAT_EVAL
7397 vim_free(s);
7398#endif
7399 }
7400 return buf[0] != NUL;
7401}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403#if defined(FEAT_STL_OPT) || defined(PROTO)
7404/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007405 * Redraw the status line or ruler of window "wp".
7406 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 */
7408 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007409win_redr_custom(
7410 win_T *wp,
7411 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007413 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 int attr;
7415 int curattr;
7416 int row;
7417 int col = 0;
7418 int maxwidth;
7419 int width;
7420 int n;
7421 int len;
7422 int fillchar;
7423 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007424 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007426 struct stl_hlrec hltab[STL_MAX_ITEM];
7427 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007428 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007429 win_T *ewp;
7430 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
Bram Moolenaar1d633412013-12-11 15:52:01 +01007432 /* There is a tiny chance that this gets called recursively: When
7433 * redrawing a status line triggers redrawing the ruler or tabline.
7434 * Avoid trouble by not allowing recursion. */
7435 if (entered)
7436 return;
7437 entered = TRUE;
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007440 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007442 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007443 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007444 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007445 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007446 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007447 maxwidth = Columns;
7448# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007449 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007452 else
7453 {
7454 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007455 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007456 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007457
7458 if (draw_ruler)
7459 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007460 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007461 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007462 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007463 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007464 if (*++stl == '-')
7465 stl++;
7466 if (atoi((char *)stl))
7467 while (VIM_ISDIGIT(*stl))
7468 stl++;
7469 if (*stl++ != '(')
7470 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007471 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007472 col = ru_col - (Columns - wp->w_width);
7473 if (col < (wp->w_width + 1) / 2)
7474 col = (wp->w_width + 1) / 2;
7475 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007476 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007477 {
7478 row = Rows - 1;
7479 --maxwidth; /* writing in last column may cause scrolling */
7480 fillchar = ' ';
7481 attr = 0;
7482 }
7483
7484# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007485 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007486# endif
7487 }
7488 else
7489 {
7490 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007491 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007492 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007493 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007494# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007495 use_sandbox = was_set_insecurely((char_u *)"statusline",
7496 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007497# endif
7498 }
7499
Bram Moolenaar53f81742017-09-22 14:35:51 +02007500 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007501 }
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007504 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
Bram Moolenaar61452852011-02-01 18:01:11 +01007506 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7507 * the cursor away and back. */
7508 ewp = wp == NULL ? curwin : wp;
7509 p_crb_save = ewp->w_p_crb;
7510 ewp->w_p_crb = FALSE;
7511
Bram Moolenaar362f3562009-11-03 16:20:34 +00007512 /* Make a copy, because the statusline may include a function call that
7513 * might change the option value and free the memory. */
7514 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007515 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007516 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007517 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007518 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007519 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007521 /* Make all characters printable. */
7522 p = transstr(buf);
7523 if (p != NULL)
7524 {
7525 vim_strncpy(buf, p, sizeof(buf) - 1);
7526 vim_free(p);
7527 }
7528
7529 /* fill up with "fillchar" */
7530 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007531 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 ++width;
7535 }
7536 buf[len] = NUL;
7537
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007538 /*
7539 * Draw each snippet with the specified highlighting.
7540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 curattr = attr;
7542 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007543 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007545 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 screen_puts_len(p, len, row, col, curattr);
7547 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007548 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007550 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007552 else if (hltab[n].userhl < 0)
7553 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007554#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007555 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7556 && wp->w_status_height != 0)
7557 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007558 else if (wp != NULL && bt_terminal(wp->w_buffer)
7559 && wp->w_status_height != 0)
7560 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007561#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007562 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007563 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007565 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 }
7567 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007568
7569 if (wp == NULL)
7570 {
7571 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7572 col = 0;
7573 len = 0;
7574 p = buf;
7575 fillchar = 0;
7576 for (n = 0; tabtab[n].start != NULL; n++)
7577 {
7578 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7579 while (col < len)
7580 TabPageIdxs[col++] = fillchar;
7581 p = tabtab[n].start;
7582 fillchar = tabtab[n].userhl;
7583 }
7584 while (col < Columns)
7585 TabPageIdxs[col++] = fillchar;
7586 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007587
7588theend:
7589 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590}
7591
7592#endif /* FEAT_STL_OPT */
7593
7594/*
7595 * Output a single character directly to the screen and update ScreenLines.
7596 */
7597 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007598screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 char_u buf[MB_MAXBYTES + 1];
7601
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007602 if (has_mbyte)
7603 buf[(*mb_char2bytes)(c, buf)] = NUL;
7604 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007605 {
7606 buf[0] = c;
7607 buf[1] = NUL;
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 screen_puts(buf, row, col, attr);
7610}
7611
7612/*
7613 * Get a single character directly from ScreenLines into "bytes[]".
7614 * Also return its attribute in *attrp;
7615 */
7616 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007617screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
7619 unsigned off;
7620
7621 /* safety check */
7622 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7623 {
7624 off = LineOffset[row] + col;
7625 *attrp = ScreenAttrs[off];
7626 bytes[0] = ScreenLines[off];
7627 bytes[1] = NUL;
7628
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (enc_utf8 && ScreenLinesUC[off] != 0)
7630 bytes[utfc_char2bytes(off, bytes)] = NUL;
7631 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7632 {
7633 bytes[0] = ScreenLines[off];
7634 bytes[1] = ScreenLines2[off];
7635 bytes[2] = NUL;
7636 }
7637 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7638 {
7639 bytes[1] = ScreenLines[off + 1];
7640 bytes[2] = NUL;
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 }
7643}
7644
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007645/*
7646 * Return TRUE if composing characters for screen posn "off" differs from
7647 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007648 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007649 */
7650 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007651screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007652{
7653 int i;
7654
7655 for (i = 0; i < Screen_mco; ++i)
7656 {
7657 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7658 return TRUE;
7659 if (u8cc[i] == 0)
7660 break;
7661 }
7662 return FALSE;
7663}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665/*
7666 * Put string '*text' on the screen at position 'row' and 'col', with
7667 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7668 * Note: only outputs within one row, message is truncated at screen boundary!
7669 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7670 */
7671 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007672screen_puts(
7673 char_u *text,
7674 int row,
7675 int col,
7676 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 screen_puts_len(text, -1, row, col, attr);
7679}
7680
7681/*
7682 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7683 * a NUL.
7684 */
7685 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007686screen_puts_len(
7687 char_u *text,
7688 int textlen,
7689 int row,
7690 int col,
7691 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 unsigned off;
7694 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007695 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007697 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 int mbyte_blen = 1;
7699 int mbyte_cells = 1;
7700 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007701 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007703#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007705 int pc, nc, nc1;
7706 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007708 int force_redraw_this;
7709 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007710 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711
Bram Moolenaar0b4c9ed2019-06-03 22:04:23 +02007712 // Safety check. The check for negative row and column is to fix issue
7713 // #4102. TODO: find out why row/col could be negative.
7714 if (ScreenLines == NULL
7715 || row >= screen_Rows || row < 0
7716 || col >= screen_Columns || col < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007718 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719
Bram Moolenaarc236c162008-07-13 17:41:49 +00007720 /* When drawing over the right halve of a double-wide char clear out the
7721 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007722 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007723#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007724 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007725#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007726 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007727 {
7728 ScreenLines[off - 1] = ' ';
7729 ScreenAttrs[off - 1] = 0;
7730 if (enc_utf8)
7731 {
7732 ScreenLinesUC[off - 1] = 0;
7733 ScreenLinesC[0][off - 1] = 0;
7734 }
7735 /* redraw the previous cell, make it empty */
7736 screen_char(off - 1, row, col - 1);
7737 /* force the cell at "col" to be redrawn */
7738 force_redraw_next = TRUE;
7739 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007740
Bram Moolenaar367329b2007-08-30 11:53:22 +00007741 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007742 while (col < screen_Columns
7743 && (len < 0 || (int)(ptr - text) < len)
7744 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {
7746 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 /* check if this is the first byte of a multibyte */
7748 if (has_mbyte)
7749 {
7750 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007751 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007753 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7755 mbyte_cells = 1;
7756 else if (enc_dbcs != 0)
7757 mbyte_cells = mbyte_blen;
7758 else /* enc_utf8 */
7759 {
7760 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007761 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 (int)((text + len) - ptr));
7763 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007764 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007766#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7768 {
7769 /* Do Arabic shaping. */
7770 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7771 {
7772 /* Past end of string to be displayed. */
7773 nc = NUL;
7774 nc1 = NUL;
7775 }
7776 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007777 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007778 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7779 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007780 nc1 = pcc[0];
7781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 pc = prev_c;
7783 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007784 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
7786 else
7787 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007788#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007789 if (col + mbyte_cells > screen_Columns)
7790 {
7791 /* Only 1 cell left, but character requires 2 cells:
7792 * display a '>' in the last column to avoid wrapping. */
7793 c = '>';
7794 mbyte_cells = 1;
7795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007799 force_redraw_this = force_redraw_next;
7800 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007801
7802 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 || (mbyte_cells == 2
7804 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7805 || (enc_dbcs == DBCS_JPNU
7806 && c == 0x8e
7807 && ScreenLines2[off] != ptr[1])
7808 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007809 && (ScreenLinesUC[off] !=
7810 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7811 || (ScreenLinesUC[off] != 0
7812 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007814 || exmode_active;
7815
Bram Moolenaara12a1612019-01-24 16:39:02 +01007816 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 {
7818#if defined(FEAT_GUI) || defined(UNIX)
7819 /* The bold trick makes a single row of pixels appear in the next
7820 * character. When a bold character is removed, the next
7821 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007822 * and for some xterms. */
7823 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824# ifdef FEAT_GUI
7825 gui.in_use
7826# endif
7827# if defined(FEAT_GUI) && defined(UNIX)
7828 ||
7829# endif
7830# ifdef UNIX
7831 term_is_xterm
7832# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007833 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007835 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007837 if (n > HL_ALL)
7838 n = syn_attr2attr(n);
7839 if (n & HL_BOLD)
7840 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 /* When at the end of the text and overwriting a two-cell
7844 * character with a one-cell character, need to clear the next
7845 * cell. Also when overwriting the left halve of a two-cell char
7846 * with the right halve of a two-cell char. Do this only once
7847 * (mb_off2cells() may return 2 on the right halve). */
7848 if (clear_next_cell)
7849 clear_next_cell = FALSE;
7850 else if (has_mbyte
7851 && (len < 0 ? ptr[mbyte_blen] == NUL
7852 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007853 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007855 && (*mb_off2cells)(off, max_off) == 1
7856 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 clear_next_cell = TRUE;
7858
7859 /* Make sure we never leave a second byte of a double-byte behind,
7860 * it confuses mb_off2cells(). */
7861 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007862 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007864 && (*mb_off2cells)(off, max_off) == 1
7865 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 ScreenLines[off] = c;
7868 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (enc_utf8)
7870 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007871 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 ScreenLinesUC[off] = 0;
7873 else
7874 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007875 int i;
7876
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007878 for (i = 0; i < Screen_mco; ++i)
7879 {
7880 ScreenLinesC[i][off] = u8cc[i];
7881 if (u8cc[i] == 0)
7882 break;
7883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 if (mbyte_cells == 2)
7886 {
7887 ScreenLines[off + 1] = 0;
7888 ScreenAttrs[off + 1] = attr;
7889 }
7890 screen_char(off, row, col);
7891 }
7892 else if (mbyte_cells == 2)
7893 {
7894 ScreenLines[off + 1] = ptr[1];
7895 ScreenAttrs[off + 1] = attr;
7896 screen_char_2(off, row, col);
7897 }
7898 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7899 {
7900 ScreenLines2[off] = ptr[1];
7901 screen_char(off, row, col);
7902 }
7903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 screen_char(off, row, col);
7905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 if (has_mbyte)
7907 {
7908 off += mbyte_cells;
7909 col += mbyte_cells;
7910 ptr += mbyte_blen;
7911 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007912 {
7913 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007915 len = -1;
7916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 }
7918 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {
7920 ++off;
7921 ++col;
7922 ++ptr;
7923 }
7924 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007925
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007926 /* If we detected the next character needs to be redrawn, but the text
7927 * doesn't extend up to there, update the character here. */
7928 if (force_redraw_next && col < screen_Columns)
7929 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007930 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7931 screen_char_2(off, row, col);
7932 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007933 screen_char(off, row, col);
7934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935}
7936
7937#ifdef FEAT_SEARCH_EXTRA
7938/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007939 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 */
7941 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007942start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 if (p_hls && !no_hlsearch)
7945 {
7946 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007947 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007948# ifdef FEAT_RELTIME
7949 /* Set the time limit to 'redrawtime'. */
7950 profile_setlimit(p_rdt, &search_hl.tm);
7951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
7953}
7954
7955/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007956 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 */
7958 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007959end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961 if (search_hl.rm.regprog != NULL)
7962 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007963 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 search_hl.rm.regprog = NULL;
7965 }
7966}
7967
7968/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007969 * Init for calling prepare_search_hl().
7970 */
7971 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007972init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007973{
7974 matchitem_T *cur;
7975
7976 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7977 * match */
7978 cur = wp->w_match_head;
7979 while (cur != NULL)
7980 {
7981 cur->hl.rm = cur->match;
7982 if (cur->hlg_id == 0)
7983 cur->hl.attr = 0;
7984 else
7985 cur->hl.attr = syn_id2attr(cur->hlg_id);
7986 cur->hl.buf = wp->w_buffer;
7987 cur->hl.lnum = 0;
7988 cur->hl.first_lnum = 0;
7989# ifdef FEAT_RELTIME
7990 /* Set the time limit to 'redrawtime'. */
7991 profile_setlimit(p_rdt, &(cur->hl.tm));
7992# endif
7993 cur = cur->next;
7994 }
7995 search_hl.buf = wp->w_buffer;
7996 search_hl.lnum = 0;
7997 search_hl.first_lnum = 0;
7998 /* time limit is set at the toplevel, for all windows */
7999}
8000
8001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 * Advance to the match in window "wp" line "lnum" or past it.
8003 */
8004 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008005prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008007 matchitem_T *cur; /* points to the match list */
8008 match_T *shl; /* points to search_hl or a match */
8009 int shl_flag; /* flag to indicate whether search_hl
8010 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008011 int pos_inprogress; /* marks that position match search is
8012 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 int n;
8014
8015 /*
8016 * When using a multi-line pattern, start searching at the top
8017 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008018 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008020 cur = wp->w_match_head;
8021 shl_flag = FALSE;
8022 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008024 if (shl_flag == FALSE)
8025 {
8026 shl = &search_hl;
8027 shl_flag = TRUE;
8028 }
8029 else
8030 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (shl->rm.regprog != NULL
8032 && shl->lnum == 0
8033 && re_multiline(shl->rm.regprog))
8034 {
8035 if (shl->first_lnum == 0)
8036 {
8037# ifdef FEAT_FOLDING
8038 for (shl->first_lnum = lnum;
8039 shl->first_lnum > wp->w_topline; --shl->first_lnum)
8040 if (hasFoldingWin(wp, shl->first_lnum - 1,
8041 NULL, NULL, TRUE, NULL))
8042 break;
8043# else
8044 shl->first_lnum = wp->w_topline;
8045# endif
8046 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008047 if (cur != NULL)
8048 cur->pos.cur = 0;
8049 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008051 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
8052 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02008054 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
8055 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008056 pos_inprogress = cur == NULL || cur->pos.cur == 0
8057 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 if (shl->lnum != 0)
8059 {
8060 shl->first_lnum = shl->lnum
8061 + shl->rm.endpos[0].lnum
8062 - shl->rm.startpos[0].lnum;
8063 n = shl->rm.endpos[0].col;
8064 }
8065 else
8066 {
8067 ++shl->first_lnum;
8068 n = 0;
8069 }
8070 }
8071 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008072 if (shl != &search_hl && cur != NULL)
8073 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 }
8075}
8076
8077/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008078 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 * Uses shl->buf.
8080 * Sets shl->lnum and shl->rm contents.
8081 * Note: Assumes a previous match is always before "lnum", unless
8082 * shl->lnum is zero.
8083 * Careful: Any pointers for buffer lines will become invalid.
8084 */
8085 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008086next_search_hl(
8087 win_T *win,
8088 match_T *shl, /* points to search_hl or a match */
8089 linenr_T lnum,
8090 colnr_T mincol, /* minimal column for a match */
8091 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 linenr_T l;
8094 colnr_T matchcol;
8095 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008096 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02008098 // for :{range}s/pat only highlight inside the range
8099 if (lnum < search_first_line || lnum > search_last_line)
8100 {
8101 shl->lnum = 0;
8102 return;
8103 }
8104
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 if (shl->lnum != 0)
8106 {
8107 /* Check for three situations:
8108 * 1. If the "lnum" is below a previous match, start a new search.
8109 * 2. If the previous match includes "mincol", use it.
8110 * 3. Continue after the previous match.
8111 */
8112 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
8113 if (lnum > l)
8114 shl->lnum = 0;
8115 else if (lnum < l || shl->rm.endpos[0].col > mincol)
8116 return;
8117 }
8118
8119 /*
8120 * Repeat searching for a match until one is found that includes "mincol"
8121 * or none is found in this line.
8122 */
8123 called_emsg = FALSE;
8124 for (;;)
8125 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00008126#ifdef FEAT_RELTIME
8127 /* Stop searching after passing the time limit. */
8128 if (profile_passed_limit(&(shl->tm)))
8129 {
8130 shl->lnum = 0; /* no match found in time */
8131 break;
8132 }
8133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 /* Three situations:
8135 * 1. No useful previous match: search from start of line.
8136 * 2. Not Vi compatible or empty match: continue at next character.
8137 * Break the loop if this is beyond the end of the line.
8138 * 3. Vi compatible searching: continue at end of previous match.
8139 */
8140 if (shl->lnum == 0)
8141 matchcol = 0;
8142 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
8143 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008144 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008146 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008147
8148 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00008149 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008150 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008152 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 shl->lnum = 0;
8154 break;
8155 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008156 if (has_mbyte)
8157 matchcol += mb_ptr2len(ml);
8158 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00008159 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 }
8161 else
8162 matchcol = shl->rm.endpos[0].col;
8163
8164 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008165 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008167 /* Remember whether shl->rm is using a copy of the regprog in
8168 * cur->match. */
8169 int regprog_is_copy = (shl != &search_hl && cur != NULL
8170 && shl == &cur->hl
8171 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008172 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008173
Bram Moolenaarb3414592014-06-17 17:48:32 +02008174 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8175 matchcol,
8176#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008177 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008178#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008179 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008180#endif
8181 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008182 /* Copy the regprog, in case it got freed and recompiled. */
8183 if (regprog_is_copy)
8184 cur->match.regprog = cur->hl.rm.regprog;
8185
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008186 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008187 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008188 /* Error while handling regexp: stop using this regexp. */
8189 if (shl == &search_hl)
8190 {
8191 /* don't free regprog in the match list, it's a copy */
8192 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008193 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008194 }
8195 shl->rm.regprog = NULL;
8196 shl->lnum = 0;
8197 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8198 message */
8199 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008200 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008201 }
8202 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008203 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008204 else
8205 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 if (nmatched == 0)
8207 {
8208 shl->lnum = 0; /* no match found */
8209 break;
8210 }
8211 if (shl->rm.startpos[0].lnum > 0
8212 || shl->rm.startpos[0].col >= mincol
8213 || nmatched > 1
8214 || shl->rm.endpos[0].col > mincol)
8215 {
8216 shl->lnum += shl->rm.startpos[0].lnum;
8217 break; /* useful match found */
8218 }
8219 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008220
8221 // Restore called_emsg for assert_fails().
8222 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
Bram Moolenaar85077472016-10-16 14:35:48 +02008225/*
8226 * If there is a match fill "shl" and return one.
8227 * Return zero otherwise.
8228 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008229 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008230next_search_hl_pos(
8231 match_T *shl, /* points to a match */
8232 linenr_T lnum,
8233 posmatch_T *posmatch, /* match positions */
8234 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008235{
8236 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008237 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008238
Bram Moolenaarb3414592014-06-17 17:48:32 +02008239 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8240 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008241 llpos_T *pos = &posmatch->pos[i];
8242
8243 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008244 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008245 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008246 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008247 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008248 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008249 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008250 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008251 /* if this match comes before the one at "found" then swap
8252 * them */
8253 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008254 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008255 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008256
Bram Moolenaar85077472016-10-16 14:35:48 +02008257 *pos = posmatch->pos[found];
8258 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008259 }
8260 }
8261 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008262 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008263 }
8264 }
8265 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008266 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008267 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008268 colnr_T start = posmatch->pos[found].col == 0
8269 ? 0 : posmatch->pos[found].col - 1;
8270 colnr_T end = posmatch->pos[found].col == 0
8271 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008272
Bram Moolenaar85077472016-10-16 14:35:48 +02008273 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008274 shl->rm.startpos[0].lnum = 0;
8275 shl->rm.startpos[0].col = start;
8276 shl->rm.endpos[0].lnum = 0;
8277 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008278 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008279 posmatch->cur = found + 1;
8280 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008281 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008282 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008283}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008284#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008285
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008287screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 attrentry_T *aep = NULL;
8290
8291 screen_attr = attr;
8292 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008293#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 && termcap_active
8295#endif
8296 )
8297 {
8298#ifdef FEAT_GUI
8299 if (gui.in_use)
8300 {
8301 char buf[20];
8302
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008303 /* The GUI handles this internally. */
8304 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 OUT_STR(buf);
8306 }
8307 else
8308#endif
8309 {
8310 if (attr > HL_ALL) /* special HL attr. */
8311 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008312 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 aep = syn_cterm_attr2entry(attr);
8314 else
8315 aep = syn_term_attr2entry(attr);
8316 if (aep == NULL) /* did ":syntax clear" */
8317 attr = 0;
8318 else
8319 attr = aep->ae_attr;
8320 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008321 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008323 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008324#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008325 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8326 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8327 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008328#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008329 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008330 /* If the Normal FG color has BOLD attribute and the new HL
8331 * has a FG color defined, clear BOLD. */
8332 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008333 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008335 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008336 out_str(T_UCS);
8337 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008338 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8339 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008341 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008343 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008345 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008346 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347
8348 /*
8349 * Output the color or start string after bold etc., in case the
8350 * bold etc. override the color setting.
8351 */
8352 if (aep != NULL)
8353 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008354#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008355 /* When 'termguicolors' is set but fg or bg is unset,
8356 * fall back to the cterm colors. This helps for SpellBad,
8357 * where the GUI uses a red undercurl. */
8358 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008360 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008361 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008362 }
8363 else
8364#endif
8365 if (t_colors > 1)
8366 {
8367 if (aep->ae_u.cterm.fg_color)
8368 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8369 }
8370#ifdef FEAT_TERMGUICOLORS
8371 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8372 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008373 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008374 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 }
8376 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008377#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008378 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008380 if (aep->ae_u.cterm.bg_color)
8381 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8382 }
8383
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008384 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008385 {
8386 if (aep->ae_u.term.start != NULL)
8387 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 }
8389 }
8390 }
8391 }
8392}
8393
8394 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008395screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 int do_ME = FALSE; /* output T_ME code */
8398
8399 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008400#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 && termcap_active
8402#endif
8403 )
8404 {
8405#ifdef FEAT_GUI
8406 if (gui.in_use)
8407 {
8408 char buf[20];
8409
8410 /* use internal GUI code */
8411 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8412 OUT_STR(buf);
8413 }
8414 else
8415#endif
8416 {
8417 if (screen_attr > HL_ALL) /* special HL attr. */
8418 {
8419 attrentry_T *aep;
8420
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008421 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {
8423 /*
8424 * Assume that t_me restores the original colors!
8425 */
8426 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008427 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008428#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008429 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8430 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8431 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008432#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008433 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008434#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008435 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8436 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8437 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008438#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008439 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 do_ME = TRUE;
8441 }
8442 else
8443 {
8444 aep = syn_term_attr2entry(screen_attr);
8445 if (aep != NULL && aep->ae_u.term.stop != NULL)
8446 {
8447 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8448 do_ME = TRUE;
8449 else
8450 out_str(aep->ae_u.term.stop);
8451 }
8452 }
8453 if (aep == NULL) /* did ":syntax clear" */
8454 screen_attr = 0;
8455 else
8456 screen_attr = aep->ae_attr;
8457 }
8458
8459 /*
8460 * Often all ending-codes are equal to T_ME. Avoid outputting the
8461 * same sequence several times.
8462 */
8463 if (screen_attr & HL_STANDOUT)
8464 {
8465 if (STRCMP(T_SE, T_ME) == 0)
8466 do_ME = TRUE;
8467 else
8468 out_str(T_SE);
8469 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008470 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008471 {
8472 if (STRCMP(T_UCE, T_ME) == 0)
8473 do_ME = TRUE;
8474 else
8475 out_str(T_UCE);
8476 }
8477 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008478 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
8480 if (STRCMP(T_UE, T_ME) == 0)
8481 do_ME = TRUE;
8482 else
8483 out_str(T_UE);
8484 }
8485 if (screen_attr & HL_ITALIC)
8486 {
8487 if (STRCMP(T_CZR, T_ME) == 0)
8488 do_ME = TRUE;
8489 else
8490 out_str(T_CZR);
8491 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008492 if (screen_attr & HL_STRIKETHROUGH)
8493 {
8494 if (STRCMP(T_STE, T_ME) == 0)
8495 do_ME = TRUE;
8496 else
8497 out_str(T_STE);
8498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8500 out_str(T_ME);
8501
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008502#ifdef FEAT_TERMGUICOLORS
8503 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008505 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008506 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008507 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008508 term_bg_rgb_color(cterm_normal_bg_gui_color);
8509 }
8510 else
8511#endif
8512 {
8513 if (t_colors > 1)
8514 {
8515 /* set Normal cterm colors */
8516 if (cterm_normal_fg_color != 0)
8517 term_fg_color(cterm_normal_fg_color - 1);
8518 if (cterm_normal_bg_color != 0)
8519 term_bg_color(cterm_normal_bg_color - 1);
8520 if (cterm_normal_fg_bold)
8521 out_str(T_MD);
8522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 }
8524 }
8525 }
8526 screen_attr = 0;
8527}
8528
8529/*
8530 * Reset the colors for a cterm. Used when leaving Vim.
8531 * The machine specific code may override this again.
8532 */
8533 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008534reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008536 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {
8538 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008539#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008540 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8541 || cterm_normal_bg_gui_color != INVALCOLOR)
8542 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008543#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
8547 out_str(T_OP);
8548 screen_attr = -1;
8549 }
8550 if (cterm_normal_fg_bold)
8551 {
8552 out_str(T_ME);
8553 screen_attr = -1;
8554 }
8555 }
8556}
8557
8558/*
8559 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8560 * using the attributes from ScreenAttrs["off"].
8561 */
8562 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008563screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 int attr;
8566
8567 /* Check for illegal values, just in case (could happen just after
8568 * resizing). */
8569 if (row >= screen_Rows || col >= screen_Columns)
8570 return;
8571
Bram Moolenaarae654382019-01-17 21:09:05 +01008572#ifdef FEAT_INS_EXPAND
8573 if (pum_under_menu(row, col))
8574 return;
8575#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008576 /* Outputting a character in the last cell on the screen may scroll the
8577 * screen up. Only do it when the "xn" termcap property is set, otherwise
8578 * mark the character invalid (update it when scrolled up). */
8579 if (*T_XN == NUL
8580 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581#ifdef FEAT_RIGHTLEFT
8582 /* account for first command-line character in rightleft mode */
8583 && !cmdmsg_rl
8584#endif
8585 )
8586 {
8587 ScreenAttrs[off] = (sattr_T)-1;
8588 return;
8589 }
8590
8591 /*
8592 * Stop highlighting first, so it's easier to move the cursor.
8593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 if (screen_char_attr != 0)
8595 attr = screen_char_attr;
8596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 attr = ScreenAttrs[off];
8598 if (screen_attr != attr)
8599 screen_stop_highlight();
8600
8601 windgoto(row, col);
8602
8603 if (screen_attr != attr)
8604 screen_start_highlight(attr);
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (enc_utf8 && ScreenLinesUC[off] != 0)
8607 {
8608 char_u buf[MB_MAXBYTES + 1];
8609
Bram Moolenaarcb070082016-04-02 22:14:51 +02008610 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008611 {
8612 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008613#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008614 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008615#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008616 )
8617 {
8618 /* Clear the two screen cells. If the character is actually
8619 * single width it won't change the second cell. */
8620 out_str((char_u *)" ");
8621 term_windgoto(row, col);
8622 }
8623 /* not sure where the cursor is after drawing the ambiguous width
8624 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008625 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008626 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008627 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008629
8630 /* Convert the UTF-8 character to bytes and write it. */
8631 buf[utfc_char2bytes(off, buf)] = NUL;
8632 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 }
8634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 /* double-byte character in single-width cell */
8639 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8640 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642
8643 screen_cur_col++;
8644}
8645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646/*
8647 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8648 * on the screen at position 'row' and 'col'.
8649 * The attributes of the first byte is used for all. This is required to
8650 * output the two bytes of a double-byte character with nothing in between.
8651 */
8652 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008653screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
8655 /* Check for illegal values (could be wrong when screen was resized). */
8656 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8657 return;
8658
8659 /* Outputting the last character on the screen may scrollup the screen.
8660 * Don't to it! Mark the character invalid (update it when scrolled up) */
8661 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8662 {
8663 ScreenAttrs[off] = (sattr_T)-1;
8664 return;
8665 }
8666
8667 /* Output the first byte normally (positions the cursor), then write the
8668 * second byte directly. */
8669 screen_char(off, row, col);
8670 out_char(ScreenLines[off + 1]);
8671 ++screen_cur_col;
8672}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674/*
8675 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8676 * This uses the contents of ScreenLines[] and doesn't change it.
8677 */
8678 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008679screen_draw_rectangle(
8680 int row,
8681 int col,
8682 int height,
8683 int width,
8684 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 int r, c;
8687 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008688 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008690 /* Can't use ScreenLines unless initialized */
8691 if (ScreenLines == NULL)
8692 return;
8693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 if (invert)
8695 screen_char_attr = HL_INVERSE;
8696 for (r = row; r < row + height; ++r)
8697 {
8698 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008699 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 for (c = col; c < col + width; ++c)
8701 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008702 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 {
8704 screen_char_2(off + c, r, c);
8705 ++c;
8706 }
8707 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
8709 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008710 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713 }
8714 }
8715 screen_char_attr = 0;
8716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718/*
8719 * Redraw the characters for a vertically split window.
8720 */
8721 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008722redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 int col;
8725 int width;
8726
8727# ifdef FEAT_CLIPBOARD
8728 clip_may_clear_selection(row, end - 1);
8729# endif
8730
8731 if (wp == NULL)
8732 {
8733 col = 0;
8734 width = Columns;
8735 }
8736 else
8737 {
8738 col = wp->w_wincol;
8739 width = wp->w_width;
8740 }
8741 screen_draw_rectangle(row, col, end - row, width, FALSE);
8742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008744 static void
8745space_to_screenline(int off, int attr)
8746{
8747 ScreenLines[off] = ' ';
8748 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008749 if (enc_utf8)
8750 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008751}
8752
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753/*
8754 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8755 * with character 'c1' in first column followed by 'c2' in the other columns.
8756 * Use attributes 'attr'.
8757 */
8758 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008759screen_fill(
8760 int start_row,
8761 int end_row,
8762 int start_col,
8763 int end_col,
8764 int c1,
8765 int c2,
8766 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
8768 int row;
8769 int col;
8770 int off;
8771 int end_off;
8772 int did_delete;
8773 int c;
8774 int norm_term;
8775#if defined(FEAT_GUI) || defined(UNIX)
8776 int force_next = FALSE;
8777#endif
8778
8779 if (end_row > screen_Rows) /* safety check */
8780 end_row = screen_Rows;
8781 if (end_col > screen_Columns) /* safety check */
8782 end_col = screen_Columns;
8783 if (ScreenLines == NULL
8784 || start_row >= end_row
8785 || start_col >= end_col) /* nothing to do */
8786 return;
8787
8788 /* it's a "normal" terminal when not in a GUI or cterm */
8789 norm_term = (
8790#ifdef FEAT_GUI
8791 !gui.in_use &&
8792#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008793 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 for (row = start_row; row < end_row; ++row)
8795 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008796 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008797#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008798 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008799#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008800 )
8801 {
8802 /* When drawing over the right halve of a double-wide char clear
8803 * out the left halve. When drawing over the left halve of a
8804 * double wide-char clear out the right halve. Only needed in a
8805 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008806 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008807 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008808 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008809 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 /*
8812 * Try to use delete-line termcap code, when no attributes or in a
8813 * "normal" terminal, where a bold/italic space is just a
8814 * space.
8815 */
8816 did_delete = FALSE;
8817 if (c2 == ' '
8818 && end_col == Columns
8819 && can_clear(T_CE)
8820 && (attr == 0
8821 || (norm_term
8822 && attr <= HL_ALL
8823 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8824 {
8825 /*
8826 * check if we really need to clear something
8827 */
8828 col = start_col;
8829 if (c1 != ' ') /* don't clear first char */
8830 ++col;
8831
8832 off = LineOffset[row] + col;
8833 end_off = LineOffset[row] + end_col;
8834
8835 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (enc_utf8)
8837 while (off < end_off && ScreenLines[off] == ' '
8838 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8839 ++off;
8840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 while (off < end_off && ScreenLines[off] == ' '
8842 && ScreenAttrs[off] == 0)
8843 ++off;
8844 if (off < end_off) /* something to be cleared */
8845 {
8846 col = off - LineOffset[row];
8847 screen_stop_highlight();
8848 term_windgoto(row, col);/* clear rest of this screen line */
8849 out_str(T_CE);
8850 screen_start(); /* don't know where cursor is now */
8851 col = end_col - col;
8852 while (col--) /* clear chars in ScreenLines */
8853 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008854 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 ++off;
8856 }
8857 }
8858 did_delete = TRUE; /* the chars are cleared now */
8859 }
8860
8861 off = LineOffset[row] + start_col;
8862 c = c1;
8863 for (col = start_col; col < end_col; ++col)
8864 {
8865 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008866 || (enc_utf8 && (int)ScreenLinesUC[off]
8867 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 || ScreenAttrs[off] != attr
8869#if defined(FEAT_GUI) || defined(UNIX)
8870 || force_next
8871#endif
8872 )
8873 {
8874#if defined(FEAT_GUI) || defined(UNIX)
8875 /* The bold trick may make a single row of pixels appear in
8876 * the next character. When a bold character is removed, the
8877 * next character should be redrawn too. This happens for our
8878 * own GUI and for some xterms. */
8879 if (
8880# ifdef FEAT_GUI
8881 gui.in_use
8882# endif
8883# if defined(FEAT_GUI) && defined(UNIX)
8884 ||
8885# endif
8886# ifdef UNIX
8887 term_is_xterm
8888# endif
8889 )
8890 {
8891 if (ScreenLines[off] != ' '
8892 && (ScreenAttrs[off] > HL_ALL
8893 || ScreenAttrs[off] & HL_BOLD))
8894 force_next = TRUE;
8895 else
8896 force_next = FALSE;
8897 }
8898#endif
8899 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 if (enc_utf8)
8901 {
8902 if (c >= 0x80)
8903 {
8904 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008905 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 }
8907 else
8908 ScreenLinesUC[off] = 0;
8909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 ScreenAttrs[off] = attr;
8911 if (!did_delete || c != ' ')
8912 screen_char(off, row, col);
8913 }
8914 ++off;
8915 if (col == start_col)
8916 {
8917 if (did_delete)
8918 break;
8919 c = c2;
8920 }
8921 }
8922 if (end_col == Columns)
8923 LineWraps[row] = FALSE;
8924 if (row == Rows - 1) /* overwritten the command line */
8925 {
8926 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008927 if (start_col == 0 && end_col == Columns
8928 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008930 if (start_col == 0)
8931 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 }
8933 }
8934}
8935
8936/*
8937 * Check if there should be a delay. Used before clearing or redrawing the
8938 * screen or the command line.
8939 */
8940 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008941check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8944 && !did_wait_return
8945 && emsg_silent == 0)
8946 {
8947 out_flush();
8948 ui_delay(1000L, TRUE);
8949 emsg_on_display = FALSE;
8950 if (check_msg_scroll)
8951 msg_scroll = FALSE;
8952 }
8953}
8954
8955/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008956 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8957 */
8958 static void
8959clear_TabPageIdxs(void)
8960{
8961 int scol;
8962
8963 for (scol = 0; scol < Columns; ++scol)
8964 TabPageIdxs[scol] = 0;
8965}
8966
8967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008969 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 * Returns TRUE if there is a valid screen to write to.
8971 * Returns FALSE when starting up and screen not initialized yet.
8972 */
8973 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008974screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008976 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 return (ScreenLines != NULL);
8978}
8979
8980/*
8981 * Resize the shell to Rows and Columns.
8982 * Allocate ScreenLines[] and associated items.
8983 *
8984 * There may be some time between setting Rows and Columns and (re)allocating
8985 * ScreenLines[]. This happens when starting up and when (manually) changing
8986 * the shell size. Always use screen_Rows and screen_Columns to access items
8987 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8988 * final size of the shell is needed.
8989 */
8990 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008991screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992{
8993 int new_row, old_row;
8994#ifdef FEAT_GUI
8995 int old_Rows;
8996#endif
8997 win_T *wp;
8998 int outofmem = FALSE;
8999 int len;
9000 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009002 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009004 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 sattr_T *new_ScreenAttrs;
9006 unsigned *new_LineOffset;
9007 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009008 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009009 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009011 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009012 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009014retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 /*
9016 * Allocation of the screen buffers is done only when the size changes and
9017 * when Rows and Columns have been set and we have started doing full
9018 * screen stuff.
9019 */
9020 if ((ScreenLines != NULL
9021 && Rows == screen_Rows
9022 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 && enc_utf8 == (ScreenLinesUC != NULL)
9024 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01009025 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 || Rows == 0
9027 || Columns == 0
9028 || (!full_screen && ScreenLines == NULL))
9029 return;
9030
9031 /*
9032 * It's possible that we produce an out-of-memory message below, which
9033 * will cause this function to be called again. To break the loop, just
9034 * return here.
9035 */
9036 if (entered)
9037 return;
9038 entered = TRUE;
9039
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009040 /*
9041 * Note that the window sizes are updated before reallocating the arrays,
9042 * thus we must not redraw here!
9043 */
9044 ++RedrawingDisabled;
9045
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 win_new_shellsize(); /* fit the windows in the new sized shell */
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 comp_col(); /* recompute columns for shown command and ruler */
9049
9050 /*
9051 * We're changing the size of the screen.
9052 * - Allocate new arrays for ScreenLines and ScreenAttrs.
9053 * - Move lines from the old arrays into the new arrays, clear extra
9054 * lines (unless the screen is going to be cleared).
9055 * - Free the old arrays.
9056 *
9057 * If anything fails, make ScreenLines NULL, so we don't do anything!
9058 * Continuing with the old ScreenLines may result in a crash, because the
9059 * size is wrong.
9060 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00009061 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009063 if (aucmd_win != NULL)
9064 win_free_lsize(aucmd_win);
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009065#ifdef FEAT_TEXT_PROP
9066 // global popup windows
9067 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9068 win_free_lsize(wp);
9069 // tab-local popup windows
9070 FOR_ALL_TABPAGES(tp)
9071 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9072 win_free_lsize(wp);
9073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009075 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01009076 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 if (enc_utf8)
9078 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009079 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009080 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009081 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
9082 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
9084 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009085 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
9086 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
9087 new_LineOffset = LALLOC_MULT(unsigned, Rows);
9088 new_LineWraps = LALLOC_MULT(char_u, Rows);
9089 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00009091 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 {
9093 if (win_alloc_lines(wp) == FAIL)
9094 {
9095 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009096 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 }
9098 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00009099 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
9100 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009101 outofmem = TRUE;
Bram Moolenaar8caaf822019-06-01 18:11:22 +02009102#ifdef FEAT_TEXT_PROP
9103 // global popup windows
9104 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
9105 if (win_alloc_lines(wp) == FAIL)
9106 {
9107 outofmem = TRUE;
9108 goto give_up;
9109 }
9110 // tab-local popup windows
9111 FOR_ALL_TABPAGES(tp)
9112 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
9113 if (win_alloc_lines(wp) == FAIL)
9114 {
9115 outofmem = TRUE;
9116 goto give_up;
9117 }
9118#endif
9119
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00009120give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009122 for (i = 0; i < p_mco; ++i)
9123 if (new_ScreenLinesC[i] == NULL)
9124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009126 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 || new_ScreenAttrs == NULL
9129 || new_LineOffset == NULL
9130 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00009131 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 || outofmem)
9133 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009134 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009135 {
9136 /* guess the size */
9137 do_outofmem_msg((long_u)((Rows + 1) * Columns));
9138
9139 /* Remember we did this to avoid getting outofmem messages over
9140 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00009141 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009142 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01009143 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009144 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009145 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01009146 VIM_CLEAR(new_ScreenLinesC[i]);
9147 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01009148 VIM_CLEAR(new_ScreenAttrs);
9149 VIM_CLEAR(new_LineOffset);
9150 VIM_CLEAR(new_LineWraps);
9151 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 }
9153 else
9154 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009155 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 for (new_row = 0; new_row < Rows; ++new_row)
9158 {
9159 new_LineOffset[new_row] = new_row * Columns;
9160 new_LineWraps[new_row] = FALSE;
9161
9162 /*
9163 * If the screen is not going to be cleared, copy as much as
9164 * possible from the old screen to the new one and clear the rest
9165 * (used when resizing the window at the "--more--" prompt or when
9166 * executing an external command, for the GUI).
9167 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009168 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 {
9170 (void)vim_memset(new_ScreenLines + new_row * Columns,
9171 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 if (enc_utf8)
9173 {
9174 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
9175 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009176 for (i = 0; i < p_mco; ++i)
9177 (void)vim_memset(new_ScreenLinesC[i]
9178 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 0, (size_t)Columns * sizeof(u8char_T));
9180 }
9181 if (enc_dbcs == DBCS_JPNU)
9182 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
9183 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9185 0, (size_t)Columns * sizeof(sattr_T));
9186 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009187 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 {
9189 if (screen_Columns < Columns)
9190 len = screen_Columns;
9191 else
9192 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009193 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009194 * may be invalid now. Also when p_mco changes. */
9195 if (!(enc_utf8 && ScreenLinesUC == NULL)
9196 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009197 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9198 ScreenLines + LineOffset[old_row],
9199 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009200 if (enc_utf8 && ScreenLinesUC != NULL
9201 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
9203 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9204 ScreenLinesUC + LineOffset[old_row],
9205 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009206 for (i = 0; i < p_mco; ++i)
9207 mch_memmove(new_ScreenLinesC[i]
9208 + new_LineOffset[new_row],
9209 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 (size_t)len * sizeof(u8char_T));
9211 }
9212 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9213 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9214 ScreenLines2 + LineOffset[old_row],
9215 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9217 ScreenAttrs + LineOffset[old_row],
9218 (size_t)len * sizeof(sattr_T));
9219 }
9220 }
9221 }
9222 /* Use the last line of the screen for the current line. */
9223 current_ScreenLine = new_ScreenLines + Rows * Columns;
9224 }
9225
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009226 free_screenlines();
9227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009230 for (i = 0; i < p_mco; ++i)
9231 ScreenLinesC[i] = new_ScreenLinesC[i];
9232 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 ScreenAttrs = new_ScreenAttrs;
9235 LineOffset = new_LineOffset;
9236 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009237 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
9239 /* It's important that screen_Rows and screen_Columns reflect the actual
9240 * size of ScreenLines[]. Set them before calling anything. */
9241#ifdef FEAT_GUI
9242 old_Rows = screen_Rows;
9243#endif
9244 screen_Rows = Rows;
9245 screen_Columns = Columns;
9246
9247 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009248 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250#ifdef FEAT_GUI
9251 else if (gui.in_use
9252 && !gui.starting
9253 && ScreenLines != NULL
9254 && old_Rows != Rows)
9255 {
9256 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9257 /*
9258 * Adjust the position of the cursor, for when executing an external
9259 * command.
9260 */
9261 if (msg_row >= Rows) /* Rows got smaller */
9262 msg_row = Rows - 1; /* put cursor at last row */
9263 else if (Rows > old_Rows) /* Rows got bigger */
9264 msg_row += Rows - old_Rows; /* put cursor in same place */
9265 if (msg_col >= Columns) /* Columns got smaller */
9266 msg_col = Columns - 1; /* put cursor at last column */
9267 }
9268#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009269 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009272 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009273
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009274 /*
9275 * Do not apply autocommands more than 3 times to avoid an endless loop
9276 * in case applying autocommands always changes Rows or Columns.
9277 */
9278 if (starting == 0 && ++retry_count <= 3)
9279 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009280 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009281 /* In rare cases, autocommands may have altered Rows or Columns,
9282 * jump back to check if we need to allocate the screen again. */
9283 goto retry;
9284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285}
9286
9287 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009288free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009289{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009290 int i;
9291
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009292 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009293 for (i = 0; i < Screen_mco; ++i)
9294 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009295 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009296 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009297 vim_free(ScreenAttrs);
9298 vim_free(LineOffset);
9299 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009300 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009301}
9302
9303 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009304screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 check_for_delay(FALSE);
9307 screenalloc(FALSE); /* allocate screen buffers if size changed */
9308 screenclear2(); /* clear the screen */
9309}
9310
9311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009312screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 int i;
9315
9316 if (starting == NO_SCREEN || ScreenLines == NULL
9317#ifdef FEAT_GUI
9318 || (gui.in_use && gui.starting)
9319#endif
9320 )
9321 return;
9322
9323#ifdef FEAT_GUI
9324 if (!gui.in_use)
9325#endif
9326 screen_attr = -1; /* force setting the Normal colors */
9327 screen_stop_highlight(); /* don't want highlighting here */
9328
9329#ifdef FEAT_CLIPBOARD
9330 /* disable selection without redrawing it */
9331 clip_scroll_selection(9999);
9332#endif
9333
9334 /* blank out ScreenLines */
9335 for (i = 0; i < Rows; ++i)
9336 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009337 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 LineWraps[i] = FALSE;
9339 }
9340
9341 if (can_clear(T_CL))
9342 {
9343 out_str(T_CL); /* clear the display */
9344 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009345 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 }
9347 else
9348 {
9349 /* can't clear the screen, mark all chars with invalid attributes */
9350 for (i = 0; i < Rows; ++i)
9351 lineinvalid(LineOffset[i], (int)Columns);
9352 clear_cmdline = TRUE;
9353 }
9354
9355 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9356
9357 win_rest_invalid(firstwin);
9358 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009359 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 if (must_redraw == CLEAR) /* no need to clear again */
9361 must_redraw = NOT_VALID;
9362 compute_cmdrow();
9363 msg_row = cmdline_row; /* put cursor on last line for messages */
9364 msg_col = 0;
9365 screen_start(); /* don't know where cursor is now */
9366 msg_scrolled = 0; /* can't scroll back */
9367 msg_didany = FALSE;
9368 msg_didout = FALSE;
9369}
9370
9371/*
9372 * Clear one line in ScreenLines.
9373 */
9374 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009375lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376{
9377 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 if (enc_utf8)
9379 (void)vim_memset(ScreenLinesUC + off, 0,
9380 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009381 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382}
9383
9384/*
9385 * Mark one line in ScreenLines invalid by setting the attributes to an
9386 * invalid value.
9387 */
9388 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009389lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9392}
9393
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394/*
9395 * Copy part of a Screenline for vertically split window "wp".
9396 */
9397 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009398linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
9400 unsigned off_to = LineOffset[to] + wp->w_wincol;
9401 unsigned off_from = LineOffset[from] + wp->w_wincol;
9402
9403 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9404 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 if (enc_utf8)
9406 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009407 int i;
9408
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9410 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009411 for (i = 0; i < p_mco; ++i)
9412 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9413 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 }
9415 if (enc_dbcs == DBCS_JPNU)
9416 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9417 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9419 wp->w_width * sizeof(sattr_T));
9420}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421
9422/*
9423 * Return TRUE if clearing with term string "p" would work.
9424 * It can't work when the string is empty or it won't set the right background.
9425 */
9426 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009427can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
9429 return (*p != NUL && (t_colors <= 1
9430#ifdef FEAT_GUI
9431 || gui.in_use
9432#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009433#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009434 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009435 || (!p_tgc && cterm_normal_bg_color == 0)
9436#else
9437 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009438#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009439 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440}
9441
9442/*
9443 * Reset cursor position. Use whenever cursor was moved because of outputting
9444 * something directly to the screen (shell commands) or a terminal control
9445 * code.
9446 */
9447 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009448screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 screen_cur_row = screen_cur_col = 9999;
9451}
9452
9453/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 * Move the cursor to position "row","col" in the screen.
9455 * This tries to find the most efficient way to move, minimizing the number of
9456 * characters sent to the terminal.
9457 */
9458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009459windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009461 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 int i;
9463 int plan;
9464 int cost;
9465 int wouldbe_col;
9466 int noinvcurs;
9467 char_u *bs;
9468 int goto_cost;
9469 int attr;
9470
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009471#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9473
9474#define PLAN_LE 1
9475#define PLAN_CR 2
9476#define PLAN_NL 3
9477#define PLAN_WRITE 4
9478 /* Can't use ScreenLines unless initialized */
9479 if (ScreenLines == NULL)
9480 return;
9481
9482 if (col != screen_cur_col || row != screen_cur_row)
9483 {
9484 /* Check for valid position. */
9485 if (row < 0) /* window without text lines? */
9486 row = 0;
9487 if (row >= screen_Rows)
9488 row = screen_Rows - 1;
9489 if (col >= screen_Columns)
9490 col = screen_Columns - 1;
9491
9492 /* check if no cursor movement is allowed in highlight mode */
9493 if (screen_attr && *T_MS == NUL)
9494 noinvcurs = HIGHL_COST;
9495 else
9496 noinvcurs = 0;
9497 goto_cost = GOTO_COST + noinvcurs;
9498
9499 /*
9500 * Plan how to do the positioning:
9501 * 1. Use CR to move it to column 0, same row.
9502 * 2. Use T_LE to move it a few columns to the left.
9503 * 3. Use NL to move a few lines down, column 0.
9504 * 4. Move a few columns to the right with T_ND or by writing chars.
9505 *
9506 * Don't do this if the cursor went beyond the last column, the cursor
9507 * position is unknown then (some terminals wrap, some don't )
9508 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009509 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 * characters to move the cursor to the right.
9511 */
9512 if (row >= screen_cur_row && screen_cur_col < Columns)
9513 {
9514 /*
9515 * If the cursor is in the same row, bigger col, we can use CR
9516 * or T_LE.
9517 */
9518 bs = NULL; /* init for GCC */
9519 attr = screen_attr;
9520 if (row == screen_cur_row && col < screen_cur_col)
9521 {
9522 /* "le" is preferred over "bc", because "bc" is obsolete */
9523 if (*T_LE)
9524 bs = T_LE; /* "cursor left" */
9525 else
9526 bs = T_BC; /* "backspace character (old) */
9527 if (*bs)
9528 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9529 else
9530 cost = 999;
9531 if (col + 1 < cost) /* using CR is less characters */
9532 {
9533 plan = PLAN_CR;
9534 wouldbe_col = 0;
9535 cost = 1; /* CR is just one character */
9536 }
9537 else
9538 {
9539 plan = PLAN_LE;
9540 wouldbe_col = col;
9541 }
9542 if (noinvcurs) /* will stop highlighting */
9543 {
9544 cost += noinvcurs;
9545 attr = 0;
9546 }
9547 }
9548
9549 /*
9550 * If the cursor is above where we want to be, we can use CR LF.
9551 */
9552 else if (row > screen_cur_row)
9553 {
9554 plan = PLAN_NL;
9555 wouldbe_col = 0;
9556 cost = (row - screen_cur_row) * 2; /* CR LF */
9557 if (noinvcurs) /* will stop highlighting */
9558 {
9559 cost += noinvcurs;
9560 attr = 0;
9561 }
9562 }
9563
9564 /*
9565 * If the cursor is in the same row, smaller col, just use write.
9566 */
9567 else
9568 {
9569 plan = PLAN_WRITE;
9570 wouldbe_col = screen_cur_col;
9571 cost = 0;
9572 }
9573
9574 /*
9575 * Check if any characters that need to be written have the
9576 * correct attributes. Also avoid UTF-8 characters.
9577 */
9578 i = col - wouldbe_col;
9579 if (i > 0)
9580 cost += i;
9581 if (cost < goto_cost && i > 0)
9582 {
9583 /*
9584 * Check if the attributes are correct without additionally
9585 * stopping highlighting.
9586 */
9587 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9588 while (i && *p++ == attr)
9589 --i;
9590 if (i != 0)
9591 {
9592 /*
9593 * Try if it works when highlighting is stopped here.
9594 */
9595 if (*--p == 0)
9596 {
9597 cost += noinvcurs;
9598 while (i && *p++ == 0)
9599 --i;
9600 }
9601 if (i != 0)
9602 cost = 999; /* different attributes, don't do it */
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 if (enc_utf8)
9605 {
9606 /* Don't use an UTF-8 char for positioning, it's slow. */
9607 for (i = wouldbe_col; i < col; ++i)
9608 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9609 {
9610 cost = 999;
9611 break;
9612 }
9613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 }
9615
9616 /*
9617 * We can do it without term_windgoto()!
9618 */
9619 if (cost < goto_cost)
9620 {
9621 if (plan == PLAN_LE)
9622 {
9623 if (noinvcurs)
9624 screen_stop_highlight();
9625 while (screen_cur_col > col)
9626 {
9627 out_str(bs);
9628 --screen_cur_col;
9629 }
9630 }
9631 else if (plan == PLAN_CR)
9632 {
9633 if (noinvcurs)
9634 screen_stop_highlight();
9635 out_char('\r');
9636 screen_cur_col = 0;
9637 }
9638 else if (plan == PLAN_NL)
9639 {
9640 if (noinvcurs)
9641 screen_stop_highlight();
9642 while (screen_cur_row < row)
9643 {
9644 out_char('\n');
9645 ++screen_cur_row;
9646 }
9647 screen_cur_col = 0;
9648 }
9649
9650 i = col - screen_cur_col;
9651 if (i > 0)
9652 {
9653 /*
9654 * Use cursor-right if it's one character only. Avoids
9655 * removing a line of pixels from the last bold char, when
9656 * using the bold trick in the GUI.
9657 */
9658 if (T_ND[0] != NUL && T_ND[1] == NUL)
9659 {
9660 while (i-- > 0)
9661 out_char(*T_ND);
9662 }
9663 else
9664 {
9665 int off;
9666
9667 off = LineOffset[row] + screen_cur_col;
9668 while (i-- > 0)
9669 {
9670 if (ScreenAttrs[off] != screen_attr)
9671 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 if (enc_dbcs == DBCS_JPNU
9675 && ScreenLines[off] == 0x8e)
9676 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 ++off;
9678 }
9679 }
9680 }
9681 }
9682 }
9683 else
9684 cost = 999;
9685
9686 if (cost >= goto_cost)
9687 {
9688 if (noinvcurs)
9689 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009690 if (row == screen_cur_row && (col > screen_cur_col)
9691 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 term_cursor_right(col - screen_cur_col);
9693 else
9694 term_windgoto(row, col);
9695 }
9696 screen_cur_row = row;
9697 screen_cur_col = col;
9698 }
9699}
9700
9701/*
9702 * Set cursor to its position in the current window.
9703 */
9704 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009705setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009707 setcursor_mayforce(FALSE);
9708}
9709
9710/*
9711 * Set cursor to its position in the current window.
9712 * When "force" is TRUE also when not redrawing.
9713 */
9714 void
9715setcursor_mayforce(int force)
9716{
9717 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 {
9719 validate_cursor();
9720 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009721 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009723 /* With 'rightleft' set and the cursor on a double-wide
9724 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009725 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9726 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009727 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009728 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729#endif
9730 curwin->w_wcol));
9731 }
9732}
9733
9734
9735/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009736 * Insert 'line_count' lines at 'row' in window 'wp'.
9737 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9738 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 * scrolling.
9740 * Returns FAIL if the lines are not inserted, OK for success.
9741 */
9742 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009743win_ins_lines(
9744 win_T *wp,
9745 int row,
9746 int line_count,
9747 int invalid,
9748 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 int did_delete;
9751 int nextrow;
9752 int lastrow;
9753 int retval;
9754
9755 if (invalid)
9756 wp->w_lines_valid = 0;
9757
9758 if (wp->w_height < 5)
9759 return FAIL;
9760
9761 if (line_count > wp->w_height - row)
9762 line_count = wp->w_height - row;
9763
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009764 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 if (retval != MAYBE)
9766 return retval;
9767
9768 /*
9769 * If there is a next window or a status line, we first try to delete the
9770 * lines at the bottom to avoid messing what is after the window.
9771 * If this fails and there are following windows, don't do anything to avoid
9772 * messing up those windows, better just redraw.
9773 */
9774 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 if (wp->w_next != NULL || wp->w_status_height)
9776 {
9777 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009778 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 did_delete = TRUE;
9780 else if (wp->w_next)
9781 return FAIL;
9782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 /*
9784 * if no lines deleted, blank the lines that will end up below the window
9785 */
9786 if (!did_delete)
9787 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009790 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 lastrow = nextrow + line_count;
9792 if (lastrow > Rows)
9793 lastrow = Rows;
9794 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009795 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 ' ', ' ', 0);
9797 }
9798
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009799 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 == FAIL)
9801 {
9802 /* deletion will have messed up other windows */
9803 if (did_delete)
9804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 win_rest_invalid(W_NEXT(wp));
9807 }
9808 return FAIL;
9809 }
9810
9811 return OK;
9812}
9813
9814/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009815 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9817 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9818 * scrolling
9819 * Return OK for success, FAIL if the lines are not deleted.
9820 */
9821 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009822win_del_lines(
9823 win_T *wp,
9824 int row,
9825 int line_count,
9826 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009827 int mayclear,
9828 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829{
9830 int retval;
9831
9832 if (invalid)
9833 wp->w_lines_valid = 0;
9834
9835 if (line_count > wp->w_height - row)
9836 line_count = wp->w_height - row;
9837
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009838 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 if (retval != MAYBE)
9840 return retval;
9841
9842 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009843 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 return FAIL;
9845
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 /*
9847 * If there are windows or status lines below, try to put them at the
9848 * correct place. If we can't do that, they have to be redrawn.
9849 */
9850 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9851 {
9852 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009853 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 {
9855 wp->w_redr_status = TRUE;
9856 win_rest_invalid(wp->w_next);
9857 }
9858 }
9859 /*
9860 * If this is the last window and there is no status line, redraw the
9861 * command line later.
9862 */
9863 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 redraw_cmdline = TRUE;
9865 return OK;
9866}
9867
9868/*
9869 * Common code for win_ins_lines() and win_del_lines().
9870 * Returns OK or FAIL when the work has been done.
9871 * Returns MAYBE when not finished yet.
9872 */
9873 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009874win_do_lines(
9875 win_T *wp,
9876 int row,
9877 int line_count,
9878 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009879 int del,
9880 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881{
9882 int retval;
9883
9884 if (!redrawing() || line_count <= 0)
9885 return FAIL;
9886
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009887 /* When inserting lines would result in loss of command output, just redraw
9888 * the lines. */
9889 if (no_win_do_lines_ins && !del)
9890 return FAIL;
9891
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009893 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009895 if (!no_win_do_lines_ins)
9896 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 return FAIL;
9898 }
9899
9900 /*
9901 * Delete all remaining lines
9902 */
9903 if (row + line_count >= wp->w_height)
9904 {
9905 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009906 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 ' ', ' ', 0);
9908 return OK;
9909 }
9910
9911 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009912 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009914 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009916 if (!no_win_do_lines_ins)
9917 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
9919 /*
9920 * If the terminal can set a scroll region, use that.
9921 * Always do this in a vertically split window. This will redraw from
9922 * ScreenLines[] when t_CV isn't defined. That's faster than using
9923 * win_line().
9924 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009925 * a character in the lower right corner of the scroll region may cause a
9926 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009928 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 scroll_region_set(wp, row);
9932 if (del)
9933 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009934 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 else
9936 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009937 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 scroll_region_reset();
9940 return retval;
9941 }
9942
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
9946 return MAYBE;
9947}
9948
9949/*
9950 * window 'wp' and everything after it is messed up, mark it for redraw
9951 */
9952 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009953win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
9957 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 wp->w_redr_status = TRUE;
9959 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 }
9961 redraw_cmdline = TRUE;
9962}
9963
9964/*
9965 * The rest of the routines in this file perform screen manipulations. The
9966 * given operation is performed physically on the screen. The corresponding
9967 * change is also made to the internal screen image. In this way, the editor
9968 * anticipates the effect of editing changes on the appearance of the screen.
9969 * That way, when we call screenupdate a complete redraw isn't usually
9970 * necessary. Another advantage is that we can keep adding code to anticipate
9971 * screen changes, and in the meantime, everything still works.
9972 */
9973
9974/*
9975 * types for inserting or deleting lines
9976 */
9977#define USE_T_CAL 1
9978#define USE_T_CDL 2
9979#define USE_T_AL 3
9980#define USE_T_CE 4
9981#define USE_T_DL 5
9982#define USE_T_SR 6
9983#define USE_NL 7
9984#define USE_T_CD 8
9985#define USE_REDRAW 9
9986
9987/*
9988 * insert lines on the screen and update ScreenLines[]
9989 * 'end' is the line after the scrolled part. Normally it is Rows.
9990 * When scrolling region used 'off' is the offset from the top for the region.
9991 * 'row' and 'end' are relative to the start of the region.
9992 *
9993 * return FAIL for failure, OK for success.
9994 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009995 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009996screen_ins_lines(
9997 int off,
9998 int row,
9999 int line_count,
10000 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010001 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +010010002 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004 int i;
10005 int j;
10006 unsigned temp;
10007 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010008 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 int type;
10010 int result_empty;
10011 int can_ce = can_clear(T_CE);
10012
10013 /*
10014 * FAIL if
10015 * - there is no valid screen
10016 * - the screen has to be redrawn completely
10017 * - the line count is less than one
10018 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010019 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010021 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
10022#ifdef FEAT_CLIPBOARD
10023 || (clip_star.state != SELECT_CLEARED
10024 && redrawing_for_callback > 0)
10025#endif
10026 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 return FAIL;
10028
10029 /*
10030 * There are seven ways to insert lines:
10031 * 0. When in a vertically split window and t_CV isn't set, redraw the
10032 * characters from ScreenLines[].
10033 * 1. Use T_CD (clear to end of display) if it exists and the result of
10034 * the insert is just empty lines
10035 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
10036 * present or line_count > 1. It looks better if we do all the inserts
10037 * at once.
10038 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
10039 * insert is just empty lines and T_CE is not present or line_count >
10040 * 1.
10041 * 4. Use T_AL (insert line) if it exists.
10042 * 5. Use T_CE (erase line) if it exists and the result of the insert is
10043 * just empty lines.
10044 * 6. Use T_DL (delete line) if it exists and the result of the insert is
10045 * just empty lines.
10046 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
10047 * the 'da' flag is not set or we have clear line capability.
10048 * 8. redraw the characters from ScreenLines[].
10049 *
10050 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
10051 * the scrollbar for the window. It does have insert line, use that if it
10052 * exists.
10053 */
10054 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10056 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010057 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 type = USE_T_CD;
10059 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
10060 type = USE_T_CAL;
10061 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
10062 type = USE_T_CDL;
10063 else if (*T_AL != NUL)
10064 type = USE_T_AL;
10065 else if (can_ce && result_empty)
10066 type = USE_T_CE;
10067 else if (*T_DL != NUL && result_empty)
10068 type = USE_T_DL;
10069 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
10070 type = USE_T_SR;
10071 else
10072 return FAIL;
10073
10074 /*
10075 * For clearing the lines screen_del_lines() is used. This will also take
10076 * care of t_db if necessary.
10077 */
10078 if (type == USE_T_CD || type == USE_T_CDL ||
10079 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010080 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
10082 /*
10083 * If text is retained below the screen, first clear or delete as many
10084 * lines at the bottom of the window as are about to be inserted so that
10085 * the deleted lines won't later surface during a screen_del_lines.
10086 */
10087 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010088 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
10090#ifdef FEAT_CLIPBOARD
10091 /* Remove a modeless selection when inserting lines halfway the screen
10092 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010093 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010094 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 else
10096 clip_scroll_selection(-line_count);
10097#endif
10098
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099#ifdef FEAT_GUI
10100 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10101 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010102 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103#endif
10104
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010105 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10106 cursor_col = wp->w_wincol;
10107
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 if (*T_CCS != NUL) /* cursor relative to region */
10109 cursor_row = row;
10110 else
10111 cursor_row = row + off;
10112
10113 /*
10114 * Shift LineOffset[] line_count down to reflect the inserted lines.
10115 * Clear the inserted lines in ScreenLines[].
10116 */
10117 row += off;
10118 end += off;
10119 for (i = 0; i < line_count; ++i)
10120 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 if (wp != NULL && wp->w_width != Columns)
10122 {
10123 /* need to copy part of a line */
10124 j = end - 1 - i;
10125 while ((j -= line_count) >= row)
10126 linecopy(j + line_count, j, wp);
10127 j += line_count;
10128 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010129 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10130 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 else
10132 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10133 LineWraps[j] = FALSE;
10134 }
10135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 {
10137 j = end - 1 - i;
10138 temp = LineOffset[j];
10139 while ((j -= line_count) >= row)
10140 {
10141 LineOffset[j + line_count] = LineOffset[j];
10142 LineWraps[j + line_count] = LineWraps[j];
10143 }
10144 LineOffset[j + line_count] = temp;
10145 LineWraps[j + line_count] = FALSE;
10146 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010147 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 else
10149 lineinvalid(temp, (int)Columns);
10150 }
10151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152
10153 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010154 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010155 if (clear_attr != 0)
10156 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 /* redraw the characters */
10159 if (type == USE_REDRAW)
10160 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010161 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 {
10163 term_append_lines(line_count);
10164 screen_start(); /* don't know where cursor is now */
10165 }
10166 else
10167 {
10168 for (i = 0; i < line_count; i++)
10169 {
10170 if (type == USE_T_AL)
10171 {
10172 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010173 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 out_str(T_AL);
10175 }
10176 else /* type == USE_T_SR */
10177 out_str(T_SR);
10178 screen_start(); /* don't know where cursor is now */
10179 }
10180 }
10181
10182 /*
10183 * With scroll-reverse and 'da' flag set we need to clear the lines that
10184 * have been scrolled down into the region.
10185 */
10186 if (type == USE_T_SR && *T_DA)
10187 {
10188 for (i = 0; i < line_count; ++i)
10189 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010190 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 out_str(T_CE);
10192 screen_start(); /* don't know where cursor is now */
10193 }
10194 }
10195
10196#ifdef FEAT_GUI
10197 gui_can_update_cursor();
10198 if (gui.in_use)
10199 out_flush(); /* always flush after a scroll */
10200#endif
10201 return OK;
10202}
10203
10204/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010205 * Delete lines on the screen and update ScreenLines[].
10206 * "end" is the line after the scrolled part. Normally it is Rows.
10207 * When scrolling region used "off" is the offset from the top for the region.
10208 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 *
10210 * Return OK for success, FAIL if the lines are not deleted.
10211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010213screen_del_lines(
10214 int off,
10215 int row,
10216 int line_count,
10217 int end,
10218 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010219 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010220 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
10222 int j;
10223 int i;
10224 unsigned temp;
10225 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010226 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 int cursor_end;
10228 int result_empty; /* result is empty until end of region */
10229 int can_delete; /* deleting line codes can be used */
10230 int type;
10231
10232 /*
10233 * FAIL if
10234 * - there is no valid screen
10235 * - the screen has to be redrawn completely
10236 * - the line count is less than one
10237 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010238 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010240 if (!screen_valid(TRUE) || line_count <= 0
10241 || (!force && line_count > p_ttyscroll)
10242#ifdef FEAT_CLIPBOARD
10243 || (clip_star.state != SELECT_CLEARED
10244 && redrawing_for_callback > 0)
10245#endif
10246 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 return FAIL;
10248
10249 /*
10250 * Check if the rest of the current region will become empty.
10251 */
10252 result_empty = row + line_count >= end;
10253
10254 /*
10255 * We can delete lines only when 'db' flag not set or when 'ce' option
10256 * available.
10257 */
10258 can_delete = (*T_DB == NUL || can_clear(T_CE));
10259
10260 /*
10261 * There are six ways to delete lines:
10262 * 0. When in a vertically split window and t_CV isn't set, redraw the
10263 * characters from ScreenLines[].
10264 * 1. Use T_CD if it exists and the result is empty.
10265 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10266 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10267 * none of the other ways work.
10268 * 4. Use T_CE (erase line) if the result is empty.
10269 * 5. Use T_DL (delete line) if it exists.
10270 * 6. redraw the characters from ScreenLines[].
10271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10273 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010274 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 type = USE_T_CD;
10276#if defined(__BEOS__) && defined(BEOS_DR8)
10277 /*
10278 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10279 * its internal termcap... this works okay for tests which test *T_DB !=
10280 * NUL. It has the disadvantage that the user cannot use any :set t_*
10281 * command to get T_DB (back) to empty_option, only :set term=... will do
10282 * the trick...
10283 * Anyway, this hack will hopefully go away with the next OS release.
10284 * (Olaf Seibert)
10285 */
10286 else if (row == 0 && T_DB == empty_option
10287 && (line_count == 1 || *T_CDL == NUL))
10288#else
10289 else if (row == 0 && (
10290#ifndef AMIGA
10291 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10292 * up, so use delete-line command */
10293 line_count == 1 ||
10294#endif
10295 *T_CDL == NUL))
10296#endif
10297 type = USE_NL;
10298 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10299 type = USE_T_CDL;
10300 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010301 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 type = USE_T_CE;
10303 else if (*T_DL != NUL && can_delete)
10304 type = USE_T_DL;
10305 else if (*T_CDL != NUL && can_delete)
10306 type = USE_T_CDL;
10307 else
10308 return FAIL;
10309
10310#ifdef FEAT_CLIPBOARD
10311 /* Remove a modeless selection when deleting lines halfway the screen or
10312 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010313 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010314 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 else
10316 clip_scroll_selection(line_count);
10317#endif
10318
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319#ifdef FEAT_GUI
10320 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10321 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010322 gui_dont_update_cursor(gui.cursor_row >= row + off
10323 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324#endif
10325
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010326 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10327 cursor_col = wp->w_wincol;
10328
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 if (*T_CCS != NUL) /* cursor relative to region */
10330 {
10331 cursor_row = row;
10332 cursor_end = end;
10333 }
10334 else
10335 {
10336 cursor_row = row + off;
10337 cursor_end = end + off;
10338 }
10339
10340 /*
10341 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10342 * Clear the inserted lines in ScreenLines[].
10343 */
10344 row += off;
10345 end += off;
10346 for (i = 0; i < line_count; ++i)
10347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 if (wp != NULL && wp->w_width != Columns)
10349 {
10350 /* need to copy part of a line */
10351 j = row + i;
10352 while ((j += line_count) <= end - 1)
10353 linecopy(j - line_count, j, wp);
10354 j -= line_count;
10355 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010356 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10357 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 else
10359 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10360 LineWraps[j] = FALSE;
10361 }
10362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 {
10364 /* whole width, moving the line pointers is faster */
10365 j = row + i;
10366 temp = LineOffset[j];
10367 while ((j += line_count) <= end - 1)
10368 {
10369 LineOffset[j - line_count] = LineOffset[j];
10370 LineWraps[j - line_count] = LineWraps[j];
10371 }
10372 LineOffset[j - line_count] = temp;
10373 LineWraps[j - line_count] = FALSE;
10374 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010375 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 else
10377 lineinvalid(temp, (int)Columns);
10378 }
10379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010381 if (screen_attr != clear_attr)
10382 screen_stop_highlight();
10383 if (clear_attr != 0)
10384 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 /* redraw the characters */
10387 if (type == USE_REDRAW)
10388 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010389 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010391 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 out_str(T_CD);
10393 screen_start(); /* don't know where cursor is now */
10394 }
10395 else if (type == USE_T_CDL)
10396 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010397 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 term_delete_lines(line_count);
10399 screen_start(); /* don't know where cursor is now */
10400 }
10401 /*
10402 * Deleting lines at top of the screen or scroll region: Just scroll
10403 * the whole screen (scroll region) up by outputting newlines on the
10404 * last line.
10405 */
10406 else if (type == USE_NL)
10407 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010408 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 for (i = line_count; --i >= 0; )
10410 out_char('\n'); /* cursor will remain on same line */
10411 }
10412 else
10413 {
10414 for (i = line_count; --i >= 0; )
10415 {
10416 if (type == USE_T_DL)
10417 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010418 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 out_str(T_DL); /* delete a line */
10420 }
10421 else /* type == USE_T_CE */
10422 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010423 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 out_str(T_CE); /* erase a line */
10425 }
10426 screen_start(); /* don't know where cursor is now */
10427 }
10428 }
10429
10430 /*
10431 * If the 'db' flag is set, we need to clear the lines that have been
10432 * scrolled up at the bottom of the region.
10433 */
10434 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10435 {
10436 for (i = line_count; i > 0; --i)
10437 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010438 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 out_str(T_CE); /* erase a line */
10440 screen_start(); /* don't know where cursor is now */
10441 }
10442 }
10443
10444#ifdef FEAT_GUI
10445 gui_can_update_cursor();
10446 if (gui.in_use)
10447 out_flush(); /* always flush after a scroll */
10448#endif
10449
10450 return OK;
10451}
10452
10453/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010454 * Return TRUE when postponing displaying the mode message: when not redrawing
10455 * or inside a mapping.
10456 */
10457 int
10458skip_showmode()
10459{
10460 // Call char_avail() only when we are going to show something, because it
10461 // takes a bit of time. redrawing() may also call char_avail_avail().
10462 if (global_busy
10463 || msg_silent != 0
10464 || !redrawing()
10465 || (char_avail() && !KeyTyped))
10466 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010467 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010468 return TRUE;
10469 }
10470 return FALSE;
10471}
10472
10473/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010474 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 *
10476 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10477 * If clear_cmdline is FALSE there may be a message there that needs to be
10478 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010479 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 * Return the length of the message (0 if no message).
10481 */
10482 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010483showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484{
10485 int need_clear;
10486 int length = 0;
10487 int do_mode;
10488 int attr;
10489 int nwr_save;
10490#ifdef FEAT_INS_EXPAND
10491 int sub_attr;
10492#endif
10493
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010494 do_mode = ((p_smd && msg_silent == 0)
10495 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010496 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010497 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010498 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010500 if (skip_showmode())
10501 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502
10503 nwr_save = need_wait_return;
10504
10505 /* wait a bit before overwriting an important message */
10506 check_for_delay(FALSE);
10507
10508 /* if the cmdline is more than one line high, erase top lines */
10509 need_clear = clear_cmdline;
10510 if (clear_cmdline && cmdline_row < Rows - 1)
10511 msg_clr_cmdline(); /* will reset clear_cmdline */
10512
10513 /* Position on the last line in the window, column 0 */
10514 msg_pos_mode();
10515 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010516 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 if (do_mode)
10518 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010519 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010521 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010522# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010523 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010524# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010525 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010526# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010527 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010528# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010529 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010531 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532# endif
10533#endif
10534#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10535 if (gui.in_use)
10536 {
10537 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010538 {
10539 /* HANGUL */
10540 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010541 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010542 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010543 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 }
10546#endif
10547#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010548 /* CTRL-X in Insert mode */
10549 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 {
10551 /* These messages can get long, avoid a wrap in a narrow
10552 * window. Prefer showing edit_submode_extra. */
10553 length = (Rows - msg_row) * Columns - 3;
10554 if (edit_submode_extra != NULL)
10555 length -= vim_strsize(edit_submode_extra);
10556 if (length > 0)
10557 {
10558 if (edit_submode_pre != NULL)
10559 length -= vim_strsize(edit_submode_pre);
10560 if (length - vim_strsize(edit_submode) > 0)
10561 {
10562 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010563 msg_puts_attr((char *)edit_submode_pre, attr);
10564 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 }
10566 if (edit_submode_extra != NULL)
10567 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010568 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010570 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 else
10572 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010573 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 }
10575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 }
10577 else
10578#endif
10579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010581 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010582 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010583 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 else if (State & INSERT)
10585 {
10586#ifdef FEAT_RIGHTLEFT
10587 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010588 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010590 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010592 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010593 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010595 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010597 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598#ifdef FEAT_RIGHTLEFT
10599 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010600 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601#endif
10602#ifdef FEAT_KEYMAP
10603 if (State & LANGMAP)
10604 {
10605# ifdef FEAT_ARABIC
10606 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010607 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 else
10609# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010610 if (get_keymap_str(curwin, (char_u *)" (%s)",
10611 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010612 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 }
10614#endif
10615 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010616 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 if (VIsual_active)
10619 {
10620 char *p;
10621
10622 /* Don't concatenate separate words to avoid translation
10623 * problems. */
10624 switch ((VIsual_select ? 4 : 0)
10625 + (VIsual_mode == Ctrl_V) * 2
10626 + (VIsual_mode == 'V'))
10627 {
10628 case 0: p = N_(" VISUAL"); break;
10629 case 1: p = N_(" VISUAL LINE"); break;
10630 case 2: p = N_(" VISUAL BLOCK"); break;
10631 case 4: p = N_(" SELECT"); break;
10632 case 5: p = N_(" SELECT LINE"); break;
10633 default: p = N_(" SELECT BLOCK"); break;
10634 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010635 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010637 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010639
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 need_clear = TRUE;
10641 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010642 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643#ifdef FEAT_INS_EXPAND
10644 && edit_submode == NULL /* otherwise it gets too long */
10645#endif
10646 )
10647 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010648 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 need_clear = TRUE;
10650 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010651
10652 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010653 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 msg_clr_eos();
10655 msg_didout = FALSE; /* overwrite this message */
10656 length = msg_col;
10657 msg_col = 0;
10658 need_wait_return = nwr_save; /* never ask for hit-return for this */
10659 }
10660 else if (clear_cmdline && msg_silent == 0)
10661 /* Clear the whole command line. Will reset "clear_cmdline". */
10662 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010663 else if (redraw_mode)
10664 {
10665 msg_pos_mode();
10666 msg_clr_eos();
10667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668
10669#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 /* In Visual mode the size of the selected area must be redrawn. */
10671 if (VIsual_active)
10672 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673
10674 /* If the last window has no status line, the ruler is after the mode
10675 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010676 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010677 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678#endif
10679 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010680 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 clear_cmdline = FALSE;
10682
10683 return length;
10684}
10685
10686/*
10687 * Position for a mode message.
10688 */
10689 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010690msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
10692 msg_col = 0;
10693 msg_row = Rows - 1;
10694}
10695
10696/*
10697 * Delete mode message. Used when ESC is typed which is expected to end
10698 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010699 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 */
10701 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010702unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010705 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 */
10707 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10708 redraw_cmdline = TRUE; /* delete mode later */
10709 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010710 clearmode();
10711}
10712
10713/*
10714 * Clear the mode message.
10715 */
10716 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010717clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010718{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010719 int save_msg_row = msg_row;
10720 int save_msg_col = msg_col;
10721
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010722 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010723 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010724 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010725 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010726
10727 msg_col = save_msg_col;
10728 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729}
10730
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010731 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010732recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010733{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010734 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010735 if (!shortmess(SHM_RECORDING))
10736 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010737 char s[4];
10738
10739 sprintf(s, " @%c", reg_recording);
10740 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010741 }
10742}
10743
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010744/*
10745 * Draw the tab pages line at the top of the Vim window.
10746 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010747 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010748draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010749{
10750 int tabcount = 0;
10751 tabpage_T *tp;
10752 int tabwidth;
10753 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010754 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010755 int attr;
10756 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010757 win_T *cwp;
10758 int wincount;
10759 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010760 int c;
10761 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010762 int attr_sel = HL_ATTR(HLF_TPS);
10763 int attr_nosel = HL_ATTR(HLF_TP);
10764 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010765 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010766 int room;
10767 int use_sep_chars = (t_colors < 8
10768#ifdef FEAT_GUI
10769 && !gui.in_use
10770#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010771#ifdef FEAT_TERMGUICOLORS
10772 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010773#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010774 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010775
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010776 if (ScreenLines == NULL)
10777 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010778 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010779
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010780#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010781 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010782 if (gui_use_tabline())
10783 {
10784 gui_update_tabline();
10785 return;
10786 }
10787#endif
10788
10789 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010790 return;
10791
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010792#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010793 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010794
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010795 /* Use the 'tabline' option if it's set. */
10796 if (*p_tal != NUL)
10797 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010798 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010799
10800 /* Check for an error. If there is one we would loop in redrawing the
10801 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010802 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010803 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010804 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010805 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010806 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010807 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010808 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010809 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010810#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010811 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010812 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010813 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010814
Bram Moolenaar238a5642006-02-21 22:12:05 +000010815 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10816 if (tabwidth < 6)
10817 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010818
Bram Moolenaar238a5642006-02-21 22:12:05 +000010819 attr = attr_nosel;
10820 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010821 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10822 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010823 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010824 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010825
Bram Moolenaar238a5642006-02-21 22:12:05 +000010826 if (tp->tp_topframe == topframe)
10827 attr = attr_sel;
10828 if (use_sep_chars && col > 0)
10829 screen_putchar('|', 0, col++, attr);
10830
10831 if (tp->tp_topframe != topframe)
10832 attr = attr_nosel;
10833
10834 screen_putchar(' ', 0, col++, attr);
10835
10836 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010837 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010838 cwp = curwin;
10839 wp = firstwin;
10840 }
10841 else
10842 {
10843 cwp = tp->tp_curwin;
10844 wp = tp->tp_firstwin;
10845 }
10846
10847 modified = FALSE;
10848 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10849 if (bufIsChanged(wp->w_buffer))
10850 modified = TRUE;
10851 if (modified || wincount > 1)
10852 {
10853 if (wincount > 1)
10854 {
10855 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010856 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010857 if (col + len >= Columns - 3)
10858 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010859 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010860#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010861 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010862#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010863 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010864#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010865 );
10866 col += len;
10867 }
10868 if (modified)
10869 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10870 screen_putchar(' ', 0, col++, attr);
10871 }
10872
10873 room = scol - col + tabwidth - 1;
10874 if (room > 0)
10875 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010876 /* Get buffer name in NameBuff[] */
10877 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010878 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010879 len = vim_strsize(NameBuff);
10880 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010881 if (has_mbyte)
10882 while (len > room)
10883 {
10884 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010885 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010886 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010887 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010888 {
10889 p += len - room;
10890 len = room;
10891 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010892 if (len > Columns - col - 1)
10893 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010894
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010895 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010896 col += len;
10897 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010898 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010899
10900 /* Store the tab page number in TabPageIdxs[], so that
10901 * jump_to_mouse() knows where each one is. */
10902 ++tabcount;
10903 while (scol < col)
10904 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010905 }
10906
Bram Moolenaar238a5642006-02-21 22:12:05 +000010907 if (use_sep_chars)
10908 c = '_';
10909 else
10910 c = ' ';
10911 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010912
10913 /* Put an "X" for closing the current tab if there are several. */
10914 if (first_tabpage->tp_next != NULL)
10915 {
10916 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10917 TabPageIdxs[Columns - 1] = -999;
10918 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010919 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010920
10921 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10922 * set. */
10923 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010924}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010925
10926/*
10927 * Get buffer name for "buf" into NameBuff[].
10928 * Takes care of special buffer names and translates special characters.
10929 */
10930 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010931get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010932{
10933 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010934 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010935 else
10936 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10937 trans_characters(NameBuff, MAXPATHL);
10938}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010939
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940/*
10941 * Get the character to use in a status line. Get its attributes in "*attr".
10942 */
10943 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010944fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010947
10948#ifdef FEAT_TERMINAL
10949 if (bt_terminal(wp->w_buffer))
10950 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010951 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010952 {
10953 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010954 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010955 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010956 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010957 {
10958 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010959 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010960 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010961 }
10962 else
10963#endif
10964 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010966 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 fill = fill_stl;
10968 }
10969 else
10970 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010971 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 fill = fill_stlnc;
10973 }
10974 /* Use fill when there is highlighting, and highlighting of current
10975 * window differs, or the fillchars differ, or this is not the
10976 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010977 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010978 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 || (fill_stl != fill_stlnc)))
10980 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010981 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 return '^';
10983 return '=';
10984}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986/*
10987 * Get the character to use in a separator between vertically split windows.
10988 * Get its attributes in "*attr".
10989 */
10990 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010991fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010993 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 if (*attr == 0 && fill_vert == ' ')
10995 return '|';
10996 else
10997 return fill_vert;
10998}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999
11000/*
11001 * Return TRUE if redrawing should currently be done.
11002 */
11003 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011004redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010011006#ifdef FEAT_EVAL
11007 if (disable_redraw_for_testing)
11008 return 0;
11009 else
11010#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020011011 return ((!RedrawingDisabled
11012#ifdef FEAT_EVAL
11013 || ignore_redraw_flag_for_testing
11014#endif
11015 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016}
11017
11018/*
11019 * Return TRUE if printing messages should currently be done.
11020 */
11021 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011022messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024 return (!(p_lz && char_avail() && !KeyTyped));
11025}
11026
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011027#ifdef FEAT_MENU
11028/*
11029 * Draw the window toolbar.
11030 */
11031 static void
11032redraw_win_toolbar(win_T *wp)
11033{
11034 vimmenu_T *menu;
11035 int item_idx = 0;
11036 int item_count = 0;
11037 int col = 0;
11038 int next_col;
11039 int off = (int)(current_ScreenLine - ScreenLines);
11040 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
11041 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
11042
11043 vim_free(wp->w_winbar_items);
11044 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
11045 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011046 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011047
11048 /* TODO: use fewer spaces if there is not enough room */
11049 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020011050 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011051 {
11052 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011053 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011054 break;
11055 if (col > 1)
11056 {
11057 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011058 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011059 break;
11060 }
11061
11062 wp->w_winbar_items[item_idx].wb_startcol = col;
11063 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020011064 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011065 break;
11066
11067 next_col = text_to_screenline(wp, menu->name, col);
11068 while (col < next_col)
11069 {
11070 ScreenAttrs[off + col] = button_attr;
11071 ++col;
11072 }
11073 wp->w_winbar_items[item_idx].wb_endcol = col;
11074 wp->w_winbar_items[item_idx].wb_menu = menu;
11075 ++item_idx;
11076
Bram Moolenaar02631462017-09-22 15:20:32 +020011077 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011078 break;
11079 space_to_screenline(off + col, button_attr);
11080 ++col;
11081 }
Bram Moolenaar02631462017-09-22 15:20:32 +020011082 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011083 {
11084 space_to_screenline(off + col, fill_attr);
11085 ++col;
11086 }
11087 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
11088
Bram Moolenaar02631462017-09-22 15:20:32 +020011089 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011090 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020011091}
11092#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020011093
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094/*
11095 * Show current status info in ruler and various other places
11096 * If always is FALSE, only show ruler if position has changed.
11097 */
11098 void
Bram Moolenaar05540972016-01-30 20:31:25 +010011099showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100{
11101 if (!always && !redrawing())
11102 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000011103#ifdef FEAT_INS_EXPAND
11104 if (pum_visible())
11105 {
11106 /* Don't redraw right now, do it later. */
11107 curwin->w_redr_status = TRUE;
11108 return;
11109 }
11110#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020011111#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000011112 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000011113 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 else
11115#endif
11116#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020011117 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118#endif
11119
11120#ifdef FEAT_TITLE
11121 if (need_maketitle
11122# ifdef FEAT_STL_OPT
11123 || (p_icon && (stl_syntax & STL_IN_ICON))
11124 || (p_title && (stl_syntax & STL_IN_TITLE))
11125# endif
11126 )
11127 maketitle();
11128#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000011129 /* Redraw the tab pages line if needed. */
11130 if (redraw_tabline)
11131 draw_tabline();
Bram Moolenaar988c4332019-06-02 14:12:11 +020011132
11133#ifdef FEAT_TEXT_PROP
11134 // Display popup windows on top of everything.
11135 update_popups();
11136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137}
11138
11139#ifdef FEAT_CMDL_INFO
11140 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020011141win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011143#define RULER_BUF_LEN 70
11144 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 int row;
11146 int fillchar;
11147 int attr;
11148 int empty_line = FALSE;
11149 colnr_T virtcol;
11150 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011151 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 int this_ru_col;
11154 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010011155 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156
11157 /* If 'ruler' off or redrawing disabled, don't do anything */
11158 if (!p_ru)
11159 return;
11160
11161 /*
11162 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
11163 * after deleting lines, before cursor.lnum is corrected.
11164 */
11165 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
11166 return;
11167
11168#ifdef FEAT_INS_EXPAND
11169 /* Don't draw the ruler while doing insert-completion, it might overwrite
11170 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 if (edit_submode != NULL)
11173 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020011174 // Don't draw the ruler when the popup menu is visible, it may overlap.
11175 // Except when the popup menu will be redrawn anyway.
11176 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000011177 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178#endif
11179
11180#ifdef FEAT_STL_OPT
11181 if (*p_ruf)
11182 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000011183 int save_called_emsg = called_emsg;
11184
11185 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011187 if (called_emsg)
11188 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011189 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011190 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 return;
11192 }
11193#endif
11194
11195 /*
11196 * Check if not in Insert mode and the line is empty (will show "0-1").
11197 */
11198 if (!(State & INSERT)
11199 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11200 empty_line = TRUE;
11201
11202 /*
11203 * Only draw the ruler when something changed.
11204 */
11205 validate_virtcol_win(wp);
11206 if ( redraw_cmdline
11207 || always
11208 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11209 || wp->w_cursor.col != wp->w_ru_cursor.col
11210 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212 || wp->w_topline != wp->w_ru_topline
11213 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11214#ifdef FEAT_DIFF
11215 || wp->w_topfill != wp->w_ru_topfill
11216#endif
11217 || empty_line != wp->w_ru_empty)
11218 {
11219 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 if (wp->w_status_height)
11221 {
11222 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011223 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011224 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011225 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 }
11227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 {
11229 row = Rows - 1;
11230 fillchar = ' ';
11231 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011232 width = Columns;
11233 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 }
11235
11236 /* In list mode virtcol needs to be recomputed */
11237 virtcol = wp->w_virtcol;
11238 if (wp->w_p_list && lcs_tab1 == NUL)
11239 {
11240 wp->w_p_list = FALSE;
11241 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11242 wp->w_p_list = TRUE;
11243 }
11244
11245 /*
11246 * Some sprintfs return the length, some return a pointer.
11247 * To avoid portability problems we use strlen() here.
11248 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011249 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11251 ? 0L
11252 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011253 len = STRLEN(buffer);
11254 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11256 (int)virtcol + 1);
11257
11258 /*
11259 * Add a "50%" if there is room for it.
11260 * On the last line, don't print in the last column (scrolls the
11261 * screen up on some terminals).
11262 */
11263 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011264 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 this_ru_col = ru_col - (Columns - width);
11269 if (this_ru_col < 0)
11270 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 /* Never use more than half the window/screen width, leave the other
11272 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011273 if (this_ru_col < (width + 1) / 2)
11274 this_ru_col = (width + 1) / 2;
11275 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011277 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011278 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 if (has_mbyte)
11281 i += (*mb_char2bytes)(fillchar, buffer + i);
11282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 buffer[i++] = fillchar;
11284 ++o;
11285 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011286 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 }
11288 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 if (has_mbyte)
11290 {
11291 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011292 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 {
11294 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011295 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 {
11297 buffer[i] = NUL;
11298 break;
11299 }
11300 }
11301 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011302 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011303 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304
Bram Moolenaar4033c552017-09-16 20:54:51 +020011305 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 i = redraw_cmdline;
11307 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011308 this_ru_col + off + (int)STRLEN(buffer),
11309 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 fillchar, fillchar, attr);
11311 /* don't redraw the cmdline because of showing the ruler */
11312 redraw_cmdline = i;
11313 wp->w_ru_cursor = wp->w_cursor;
11314 wp->w_ru_virtcol = wp->w_virtcol;
11315 wp->w_ru_empty = empty_line;
11316 wp->w_ru_topline = wp->w_topline;
11317 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11318#ifdef FEAT_DIFF
11319 wp->w_ru_topfill = wp->w_topfill;
11320#endif
11321 }
11322}
11323#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011324
11325#if defined(FEAT_LINEBREAK) || defined(PROTO)
11326/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011327 * Return the width of the 'number' and 'relativenumber' column.
11328 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011329 * Otherwise it depends on 'numberwidth' and the line count.
11330 */
11331 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011332number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011333{
11334 int n;
11335 linenr_T lnum;
11336
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011337 if (wp->w_p_rnu && !wp->w_p_nu)
11338 /* cursor line shows "0" */
11339 lnum = wp->w_height;
11340 else
11341 /* cursor line shows absolute line number */
11342 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011343
Bram Moolenaar6b314672015-03-20 15:42:10 +010011344 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011345 return wp->w_nrwidth_width;
11346 wp->w_nrwidth_line_count = lnum;
11347
11348 n = 0;
11349 do
11350 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011351 lnum /= 10;
11352 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011353 } while (lnum > 0);
11354
11355 /* 'numberwidth' gives the minimal width plus one */
11356 if (n < wp->w_p_nuw - 1)
11357 n = wp->w_p_nuw - 1;
11358
11359 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011360 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011361 return n;
11362}
11363#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011364
Bram Moolenaar113e1072019-01-20 15:30:40 +010011365#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011366/*
11367 * Return the current cursor column. This is the actual position on the
11368 * screen. First column is 0.
11369 */
11370 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011371screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011372{
11373 return screen_cur_col;
11374}
11375
11376/*
11377 * Return the current cursor row. This is the actual position on the screen.
11378 * First row is 0.
11379 */
11380 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011381screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011382{
11383 return screen_cur_row;
11384}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011385#endif