blob: d20bd2a7b62e9d2c5e59e48f65af296cbde7dde7 [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 Moolenaar0cb8ac72018-05-11 22:01:51 +0200509 void
510reset_updating_screen(int may_resize_shell UNUSED)
511{
512 updating_screen = FALSE;
513#ifdef FEAT_GUI
514 if (may_resize_shell)
515 gui_may_resize_shell();
516#endif
517#ifdef FEAT_TERMINAL
518 term_check_channel_closed_recently();
519#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200520
521#ifdef HAVE_DROP_FILE
522 // If handle_drop() was called while updating_screen was TRUE need to
523 // handle the drop now.
524 handle_any_postponed_drop();
525#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200526}
527
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200529 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
531 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100532update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 redraw_curbuf_later(type);
535 update_screen(type);
536}
537
538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 * Based on the current value of curwin->w_topline, transfer a screenfull
540 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200541 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200544update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 win_T *wp;
548 static int did_intro = FALSE;
549#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
550 int did_one;
551#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200552#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200553 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200554 int gui_cursor_col = 0;
555 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200556#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200557 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000559 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200563 if (type == VALID_NO_UPDATE)
564 {
565 no_update = TRUE;
566 type = 0;
567 }
568
Bram Moolenaara3347722019-05-11 21:14:24 +0200569#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200570 {
571 buf_T *buf;
572
573 // Before updating the screen, notify any listeners of changed text.
574 FOR_ALL_BUFFERS(buf)
575 invoke_listeners(buf);
576 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 if (must_redraw)
580 {
581 if (type < must_redraw) /* use maximal type */
582 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000583
584 /* must_redraw is reset here, so that when we run into some weird
585 * reason to redraw while busy redrawing (e.g., asynchronous
586 * scrolling), or update_topline() in win_update() will cause a
587 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 must_redraw = 0;
589 }
590
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200591 /* May need to update w_lines[]. */
592 if (curwin->w_lines_valid == 0 && type < NOT_VALID
593#ifdef FEAT_TERMINAL
594 && !term_do_update_window(curwin)
595#endif
596 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 type = NOT_VALID;
598
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000599 /* Postpone the redrawing when it's not needed and when being called
600 * recursively. */
601 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
603 redraw_later(type); /* remember type for next time */
604 must_redraw = type;
605 if (type > INVERTED_ALL)
606 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200607 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200609#ifdef FEAT_TEXT_PROP
610 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200611 if (popup_any_visible())
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200612 type = NOT_VALID;
613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
615 updating_screen = TRUE;
616#ifdef FEAT_SYN_HL
617 ++display_tick; /* let syntax code know we're in a next round of
618 * display updating */
619#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200620 if (no_update)
621 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623 /*
624 * if the screen was scrolled up when displaying a message, scroll it down
625 */
626 if (msg_scrolled)
627 {
628 clear_cmdline = TRUE;
629 if (msg_scrolled > Rows - 5) /* clearing is faster */
630 type = CLEAR;
631 else if (type != CLEAR)
632 {
633 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200634 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
635 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 type = CLEAR;
637 FOR_ALL_WINDOWS(wp)
638 {
639 if (W_WINROW(wp) < msg_scrolled)
640 {
641 if (W_WINROW(wp) + wp->w_height > msg_scrolled
642 && wp->w_redr_type < REDRAW_TOP
643 && wp->w_lines_valid > 0
644 && wp->w_topline == wp->w_lines[0].wl_lnum)
645 {
646 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
647 wp->w_redr_type = REDRAW_TOP;
648 }
649 else
650 {
651 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200652 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
653 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 }
657 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200658 if (!no_update)
659 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000660 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 msg_scrolled = 0;
663 need_wait_return = FALSE;
664 }
665
666 /* reset cmdline_row now (may have been changed temporarily) */
667 compute_cmdrow();
668
669 /* Check for changed highlighting */
670 if (need_highlight_changed)
671 highlight_changed();
672
673 if (type == CLEAR) /* first clear screen */
674 {
675 screenclear(); /* will reset clear_cmdline */
676 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200677 /* must_redraw may be set indirectly, avoid another redraw later */
678 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680
681 if (clear_cmdline) /* going to clear cmdline (done below) */
682 check_for_delay(FALSE);
683
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000684#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200685 /* Force redraw when width of 'number' or 'relativenumber' column
686 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000687 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200688 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
689 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000690 curwin->w_redr_type = NOT_VALID;
691#endif
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 /*
694 * Only start redrawing if there is really something to do.
695 */
696 if (type == INVERTED)
697 update_curswant();
698 if (curwin->w_redr_type < type
699 && !((type == VALID
700 && curwin->w_lines[0].wl_valid
701#ifdef FEAT_DIFF
702 && curwin->w_topfill == curwin->w_old_topfill
703 && curwin->w_botfill == curwin->w_old_botfill
704#endif
705 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000707 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
709 && curwin->w_old_visual_mode == VIsual_mode
710 && (curwin->w_valid & VALID_VIRTCOL)
711 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 ))
713 curwin->w_redr_type = type;
714
Bram Moolenaar5a305422006-04-28 22:38:25 +0000715 /* Redraw the tab pages line if needed. */
716 if (redraw_tabline || type >= NOT_VALID)
717 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef FEAT_SYN_HL
720 /*
721 * Correct stored syntax highlighting info for changes in each displayed
722 * buffer. Each buffer must only be done once.
723 */
724 FOR_ALL_WINDOWS(wp)
725 {
726 if (wp->w_buffer->b_mod_set)
727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 win_T *wwp;
729
730 /* Check if we already did this buffer. */
731 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
732 if (wwp->w_buffer == wp->w_buffer)
733 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200734 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 syn_stack_apply_changes(wp->w_buffer);
736 }
737 }
738#endif
739
740 /*
741 * Go from top to bottom through the windows, redrawing the ones that need
742 * it.
743 */
744#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
745 did_one = FALSE;
746#endif
747#ifdef FEAT_SEARCH_EXTRA
748 search_hl.rm.regprog = NULL;
749#endif
750 FOR_ALL_WINDOWS(wp)
751 {
752 if (wp->w_redr_type != 0)
753 {
754 cursor_off();
755#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
756 if (!did_one)
757 {
758 did_one = TRUE;
759# ifdef FEAT_SEARCH_EXTRA
760 start_search_hl();
761# endif
762# ifdef FEAT_CLIPBOARD
763 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200764 if (clip_star.available && clip_isautosel_star())
765 clip_update_selection(&clip_star);
766 if (clip_plus.available && clip_isautosel_plus())
767 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768# endif
769#ifdef FEAT_GUI
770 /* Remove the cursor before starting to do anything, because
771 * scrolling may make it difficult to redraw the text under
772 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200773 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200774 {
775 gui_cursor_col = gui.cursor_col;
776 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200778 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 }
782#endif
783 win_update(wp);
784 }
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* redraw status line after the window to minimize cursor movement */
787 if (wp->w_redr_status)
788 {
789 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200790 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793#if defined(FEAT_SEARCH_EXTRA)
794 end_search_hl();
795#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100796#ifdef FEAT_INS_EXPAND
797 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200798 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /* Reset b_mod_set flags. Going through all windows is probably faster
802 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200803 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200806 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
808 /* Clear or redraw the command line. Done last, because scrolling may
809 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200810 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 showmode();
812
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200813 if (no_update)
814 --no_win_do_lines_ins;
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200817 if (!did_intro)
818 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 did_intro = TRUE;
820
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200821#ifdef FEAT_TEXT_PROP
822 // Display popup windows on top of the others.
823 update_popups();
824#endif
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826#ifdef FEAT_GUI
827 /* Redraw the cursor and update the scrollbars when all screen updating is
828 * done. */
829 if (gui.in_use)
830 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200831 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200832 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100833 mch_disable_flush();
834 out_flush(); /* required before updating the cursor */
835 mch_enable_flush();
836
Bram Moolenaar144445d2016-07-08 21:41:54 +0200837 /* Put the GUI position where the cursor was, gui_update_cursor()
838 * uses that. */
839 gui.col = gui_cursor_col;
840 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200841 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100843 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200844 screen_cur_col = gui.col;
845 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200846 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100847 else
848 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 gui_update_scrollbars(FALSE);
850 }
851#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200852 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100855#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100856/*
857 * Prepare for updating one or more windows.
858 * Caller must check for "updating_screen" already set to avoid recursiveness.
859 */
860 static void
861update_prepare(void)
862{
863 cursor_off();
864 updating_screen = TRUE;
865#ifdef FEAT_GUI
866 /* Remove the cursor before starting to do anything, because scrolling may
867 * make it difficult to redraw the text under it. */
868 if (gui.in_use)
869 gui_undraw_cursor();
870#endif
871#ifdef FEAT_SEARCH_EXTRA
872 start_search_hl();
873#endif
874}
875
876/*
877 * Finish updating one or more windows.
878 */
879 static void
880update_finish(void)
881{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200882 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100883 showmode();
884
885# ifdef FEAT_SEARCH_EXTRA
886 end_search_hl();
887# endif
888
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200889 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100890
891# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892 /* Redraw the cursor and update the scrollbars when all screen updating is
893 * done. */
894 if (gui.in_use)
895 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100896 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100897 gui_update_scrollbars(FALSE);
898 }
899# endif
900}
901#endif
902
Bram Moolenaar860cae12010-06-05 23:22:07 +0200903#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200904/*
905 * Return TRUE if the cursor line in window "wp" may be concealed, according
906 * to the 'concealcursor' option.
907 */
908 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100909conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200910{
911 int c;
912
913 if (*wp->w_p_cocu == NUL)
914 return FALSE;
915 if (get_real_state() & VISUAL)
916 c = 'v';
917 else if (State & INSERT)
918 c = 'i';
919 else if (State & NORMAL)
920 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200921 else if (State & CMDLINE)
922 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200923 else
924 return FALSE;
925 return vim_strchr(wp->w_p_cocu, c) != NULL;
926}
927
928/*
929 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
930 */
931 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200932conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200933{
934 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
935 {
936 need_cursor_line_redraw = TRUE;
937 /* Need to recompute cursor column, e.g., when starting Visual mode
938 * without concealing. */
939 curs_columns(TRUE);
940 }
941}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942#endif
943
Bram Moolenaar113e1072019-01-20 15:30:40 +0100944#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100946update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947{
948 win_T *wp;
949 int doit = FALSE;
950
951# ifdef FEAT_FOLDING
952 win_foldinfo.fi_level = 0;
953# endif
954
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100955 // update/delete a specific sign
956 redraw_buf_line_later(buf, lnum);
957
958 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 if (wp->w_redr_type != 0)
961 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100963 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200964 * happening (recursive call), messages on the screen or still starting up.
965 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100966 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200967 || State == ASKMORE || State == HITRETURN
968 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100969#ifdef FEAT_GUI
970 || gui.starting
971#endif
972 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 return;
974
975 /* update all windows that need updating */
976 update_prepare();
977
Bram Moolenaar29323592016-07-24 22:04:11 +0200978 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 {
980 if (wp->w_redr_type != 0)
981 win_update(wp);
982 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200983 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 update_finish();
987}
988#endif
989
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200990#ifdef FEAT_TEXT_PROP
991 static void
992update_popups(void)
993{
994 win_T *wp;
995 win_T *lowest_wp;
996 int lowest_zindex;
997
998 // Reset all the VALID_POPUP flags.
999 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001000 wp->w_popup_flags &= ~POPF_REDRAWN;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001001 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001002 wp->w_popup_flags &= ~POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001003
1004 // TODO: don't redraw every popup every time.
1005 for (;;)
1006 {
1007 // Find the window with the lowest zindex that hasn't been updated yet,
1008 // so that the window with a higher zindex is drawn later, thus goes on
1009 // top.
1010 lowest_zindex = INT_MAX;
1011 lowest_wp = NULL;
1012 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001013 if ((wp->w_popup_flags & (POPF_REDRAWN|POPF_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001014 && wp->w_zindex < lowest_zindex)
1015 {
1016 lowest_zindex = wp->w_zindex;
1017 lowest_wp = wp;
1018 }
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001019 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001020 if ((wp->w_popup_flags & (POPF_REDRAWN|POPF_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001021 && wp->w_zindex < lowest_zindex)
1022 {
1023 lowest_zindex = wp->w_zindex;
1024 lowest_wp = wp;
1025 }
1026
1027 if (lowest_wp == NULL)
1028 break;
1029 win_update(lowest_wp);
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001030 lowest_wp->w_popup_flags |= POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001031 }
1032}
1033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001035/*
1036 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1037 * window then get the "Pmenu" highlight attribute.
1038 */
1039 static int
1040get_wcr_attr(win_T *wp)
1041{
1042 int wcr_attr = 0;
1043
1044 if (*wp->w_p_wcr != NUL)
1045 wcr_attr = syn_name2attr(wp->w_p_wcr);
1046#ifdef FEAT_TEXT_PROP
1047 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1048 wcr_attr = HL_ATTR(HLF_PNI);
1049#endif
1050 return wcr_attr;
1051}
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#if defined(FEAT_GUI) || defined(PROTO)
1054/*
1055 * Update a single window, its status line and maybe the command line msg.
1056 * Used for the GUI scrollbar.
1057 */
1058 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001059updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001061 /* return if already busy updating */
1062 if (updating_screen)
1063 return;
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 update_prepare();
1066
1067#ifdef FEAT_CLIPBOARD
1068 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001069 if (clip_star.available && clip_isautosel_star())
1070 clip_update_selection(&clip_star);
1071 if (clip_plus.available && clip_isautosel_plus())
1072 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001076
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001077 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001078 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001079 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (wp->w_redr_status
1082# ifdef FEAT_CMDL_INFO
1083 || p_ru
1084# endif
1085# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001086 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087# endif
1088 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001089 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 update_finish();
1092}
1093#endif
1094
1095/*
1096 * Update a single window.
1097 *
1098 * This may cause the windows below it also to be redrawn (when clearing the
1099 * screen or scrolling lines).
1100 *
1101 * How the window is redrawn depends on wp->w_redr_type. Each type also
1102 * implies the one below it.
1103 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001104 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1106 * INVERTED redraw the changed part of the Visual area
1107 * INVERTED_ALL redraw the whole Visual area
1108 * VALID 1. scroll up/down to adjust for a changed w_topline
1109 * 2. update lines at the top when scrolled down
1110 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001111 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 * b_mod_top and b_mod_bot.
1113 * - if wp->w_redraw_top non-zero, redraw lines between
1114 * wp->w_redraw_top and wp->w_redr_bot.
1115 * - continue redrawing when syntax status is invalid.
1116 * 4. if scrolled up, update lines at the bottom.
1117 * This results in three areas that may need updating:
1118 * top: from first row to top_end (when scrolled down)
1119 * mid: from mid_start to mid_end (update inversion or changed text)
1120 * bot: from bot_start to last row (when scrolled up)
1121 */
1122 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001123win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
1125 buf_T *buf = wp->w_buffer;
1126 int type;
1127 int top_end = 0; /* Below last row of the top area that needs
1128 updating. 0 when no top area updating. */
1129 int mid_start = 999;/* first row of the mid area that needs
1130 updating. 999 when no mid area updating. */
1131 int mid_end = 0; /* Below last row of the mid area that needs
1132 updating. 0 when no mid area updating. */
1133 int bot_start = 999;/* first row of the bot area that needs
1134 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 int scrolled_down = FALSE; /* TRUE when scrolled down when
1136 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001138 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 int top_to_mod = FALSE; /* redraw above mod_top */
1140#endif
1141
1142 int row; /* current window row to display */
1143 linenr_T lnum; /* current buffer lnum to display */
1144 int idx; /* current index in w_lines[] */
1145 int srow; /* starting row of the current line */
1146
1147 int eof = FALSE; /* if TRUE, we hit the end of the file */
1148 int didline = FALSE; /* if TRUE, we finished the last line */
1149 int i;
1150 long j;
1151 static int recursive = FALSE; /* being called recursively */
1152 int old_botline = wp->w_botline;
1153#ifdef FEAT_FOLDING
1154 long fold_count;
1155#endif
1156#ifdef FEAT_SYN_HL
1157 /* remember what happened to the previous line, to know if
1158 * check_visual_highlight() can be used */
1159#define DID_NONE 1 /* didn't update a line */
1160#define DID_LINE 2 /* updated a normal line */
1161#define DID_FOLD 3 /* updated a folded line */
1162 int did_update = DID_NONE;
1163 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1164#endif
1165 linenr_T mod_top = 0;
1166 linenr_T mod_bot = 0;
1167#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1168 int save_got_int;
1169#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001170#ifdef SYN_TIME_LIMIT
1171 proftime_T syntax_tm;
1172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 type = wp->w_redr_type;
1175
1176 if (type == NOT_VALID)
1177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 wp->w_lines_valid = 0;
1180 }
1181
1182 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001183 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
1185 wp->w_redr_type = 0;
1186 return;
1187 }
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 /* Window is zero-width: Only need to draw the separator. */
1190 if (wp->w_width == 0)
1191 {
1192 /* draw the vertical separator right of this window */
1193 draw_vsep_win(wp, 0);
1194 wp->w_redr_type = 0;
1195 return;
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001198#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001199 // If this window contains a terminal, redraw works completely differently.
1200 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001201 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001202 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001203# ifdef FEAT_MENU
1204 /* Draw the window toolbar, if there is one. */
1205 if (winbar_height(wp) > 0)
1206 redraw_win_toolbar(wp);
1207# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001208 wp->w_redr_type = 0;
1209 return;
1210 }
1211#endif
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001214 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#endif
1216
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001217#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001218 /* Force redraw when width of 'number' or 'relativenumber' column
1219 * changes. */
1220 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001221 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001222 {
1223 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001224 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001225 }
1226 else
1227#endif
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1230 {
1231 /*
1232 * When there are both inserted/deleted lines and specific lines to be
1233 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1234 * everything (only happens when redrawing is off for while).
1235 */
1236 type = NOT_VALID;
1237 }
1238 else
1239 {
1240 /*
1241 * Set mod_top to the first line that needs displaying because of
1242 * changes. Set mod_bot to the first line after the changes.
1243 */
1244 mod_top = wp->w_redraw_top;
1245 if (wp->w_redraw_bot != 0)
1246 mod_bot = wp->w_redraw_bot + 1;
1247 else
1248 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (buf->b_mod_set)
1250 {
1251 if (mod_top == 0 || mod_top > buf->b_mod_top)
1252 {
1253 mod_top = buf->b_mod_top;
1254#ifdef FEAT_SYN_HL
1255 /* Need to redraw lines above the change that may be included
1256 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (mod_top < 1)
1261 mod_top = 1;
1262 }
1263#endif
1264 }
1265 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1266 mod_bot = buf->b_mod_bot;
1267
1268#ifdef FEAT_SEARCH_EXTRA
1269 /* When 'hlsearch' is on and using a multi-line search pattern, a
1270 * change in one line may make the Search highlighting in a
1271 * previous line invalid. Simple solution: redraw all visible
1272 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001273 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001275 if (search_hl.rm.regprog != NULL
1276 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001278 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001279 {
1280 cur = wp->w_match_head;
1281 while (cur != NULL)
1282 {
1283 if (cur->match.regprog != NULL
1284 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001285 {
1286 top_to_mod = TRUE;
1287 break;
1288 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001289 cur = cur->next;
1290 }
1291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292#endif
1293 }
1294#ifdef FEAT_FOLDING
1295 if (mod_top != 0 && hasAnyFolding(wp))
1296 {
1297 linenr_T lnumt, lnumb;
1298
1299 /*
1300 * A change in a line can cause lines above it to become folded or
1301 * unfolded. Find the top most buffer line that may be affected.
1302 * If the line was previously folded and displayed, get the first
1303 * line of that fold. If the line is folded now, get the first
1304 * folded line. Use the minimum of these two.
1305 */
1306
1307 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1308 * the line below it. If there is no valid entry, use w_topline.
1309 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1310 * to this line. If there is no valid entry, use MAXLNUM. */
1311 lnumt = wp->w_topline;
1312 lnumb = MAXLNUM;
1313 for (i = 0; i < wp->w_lines_valid; ++i)
1314 if (wp->w_lines[i].wl_valid)
1315 {
1316 if (wp->w_lines[i].wl_lastlnum < mod_top)
1317 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1318 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1319 {
1320 lnumb = wp->w_lines[i].wl_lnum;
1321 /* When there is a fold column it might need updating
1322 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001323 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 ++lnumb;
1325 }
1326 }
1327
1328 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1329 if (mod_top > lnumt)
1330 mod_top = lnumt;
1331
1332 /* Now do the same for the bottom line (one above mod_bot). */
1333 --mod_bot;
1334 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1335 ++mod_bot;
1336 if (mod_bot < lnumb)
1337 mod_bot = lnumb;
1338 }
1339#endif
1340
1341 /* When a change starts above w_topline and the end is below
1342 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001343 * If the end of the change is above w_topline: do like no change was
1344 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 if (mod_top != 0 && mod_top < wp->w_topline)
1346 {
1347 if (mod_bot > wp->w_topline)
1348 mod_top = wp->w_topline;
1349#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001350 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 top_end = 1;
1352#endif
1353 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001354
1355 /* When line numbers are displayed need to redraw all lines below
1356 * inserted/deleted lines. */
1357 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1358 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001360 wp->w_redraw_top = 0; // reset for next time
1361 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
1363 /*
1364 * When only displaying the lines at the top, set top_end. Used when
1365 * window has scrolled down for msg_scrolled.
1366 */
1367 if (type == REDRAW_TOP)
1368 {
1369 j = 0;
1370 for (i = 0; i < wp->w_lines_valid; ++i)
1371 {
1372 j += wp->w_lines[i].wl_size;
1373 if (j >= wp->w_upd_rows)
1374 {
1375 top_end = j;
1376 break;
1377 }
1378 }
1379 if (top_end == 0)
1380 /* not found (cannot happen?): redraw everything */
1381 type = NOT_VALID;
1382 else
1383 /* top area defined, the rest is VALID */
1384 type = VALID;
1385 }
1386
Bram Moolenaar367329b2007-08-30 11:53:22 +00001387 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001388 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1389 * non-zero and thus not FALSE) will indicate that screenclear() was not
1390 * called. */
1391 if (screen_cleared)
1392 screen_cleared = MAYBE;
1393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 /*
1395 * If there are no changes on the screen that require a complete redraw,
1396 * handle three cases:
1397 * 1: we are off the top of the screen by a few lines: scroll down
1398 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1399 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1400 * w_lines[] that needs updating.
1401 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001402 if ((type == VALID || type == SOME_VALID
1403 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_DIFF
1405 && !wp->w_botfill && !wp->w_old_botfill
1406#endif
1407 )
1408 {
1409 if (mod_top != 0 && wp->w_topline == mod_top)
1410 {
1411 /*
1412 * w_topline is the first changed line, the scrolling will be done
1413 * further down.
1414 */
1415 }
1416 else if (wp->w_lines[0].wl_valid
1417 && (wp->w_topline < wp->w_lines[0].wl_lnum
1418#ifdef FEAT_DIFF
1419 || (wp->w_topline == wp->w_lines[0].wl_lnum
1420 && wp->w_topfill > wp->w_old_topfill)
1421#endif
1422 ))
1423 {
1424 /*
1425 * New topline is above old topline: May scroll down.
1426 */
1427#ifdef FEAT_FOLDING
1428 if (hasAnyFolding(wp))
1429 {
1430 linenr_T ln;
1431
1432 /* count the number of lines we are off, counting a sequence
1433 * of folded lines as one */
1434 j = 0;
1435 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1436 {
1437 ++j;
1438 if (j >= wp->w_height - 2)
1439 break;
1440 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1441 }
1442 }
1443 else
1444#endif
1445 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1446 if (j < wp->w_height - 2) /* not too far off */
1447 {
1448 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1449#ifdef FEAT_DIFF
1450 /* insert extra lines for previously invisible filler lines */
1451 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1452 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1453 - wp->w_old_topfill;
1454#endif
1455 if (i < wp->w_height - 2) /* less than a screen off */
1456 {
1457 /*
1458 * Try to insert the correct number of lines.
1459 * If not the last window, delete the lines at the bottom.
1460 * win_ins_lines may fail when the terminal can't do it.
1461 */
1462 if (i > 0)
1463 check_for_delay(FALSE);
1464 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1465 {
1466 if (wp->w_lines_valid != 0)
1467 {
1468 /* Need to update rows that are new, stop at the
1469 * first one that scrolled down. */
1470 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 /* Move the entries that were scrolled, disable
1474 * the entries for the lines to be redrawn. */
1475 if ((wp->w_lines_valid += j) > wp->w_height)
1476 wp->w_lines_valid = wp->w_height;
1477 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1478 wp->w_lines[idx] = wp->w_lines[idx - j];
1479 while (idx >= 0)
1480 wp->w_lines[idx--].wl_valid = FALSE;
1481 }
1482 }
1483 else
1484 mid_start = 0; /* redraw all lines */
1485 }
1486 else
1487 mid_start = 0; /* redraw all lines */
1488 }
1489 else
1490 mid_start = 0; /* redraw all lines */
1491 }
1492 else
1493 {
1494 /*
1495 * New topline is at or below old topline: May scroll up.
1496 * When topline didn't change, find first entry in w_lines[] that
1497 * needs updating.
1498 */
1499
1500 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1501 j = -1;
1502 row = 0;
1503 for (i = 0; i < wp->w_lines_valid; i++)
1504 {
1505 if (wp->w_lines[i].wl_valid
1506 && wp->w_lines[i].wl_lnum == wp->w_topline)
1507 {
1508 j = i;
1509 break;
1510 }
1511 row += wp->w_lines[i].wl_size;
1512 }
1513 if (j == -1)
1514 {
1515 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1516 * lines */
1517 mid_start = 0;
1518 }
1519 else
1520 {
1521 /*
1522 * Try to delete the correct number of lines.
1523 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1524 */
1525#ifdef FEAT_DIFF
1526 /* If the topline didn't change, delete old filler lines,
1527 * otherwise delete filler lines of the new topline... */
1528 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1529 row += wp->w_old_topfill;
1530 else
1531 row += diff_check_fill(wp, wp->w_topline);
1532 /* ... but don't delete new filler lines. */
1533 row -= wp->w_topfill;
1534#endif
1535 if (row > 0)
1536 {
1537 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001538 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1539 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 bot_start = wp->w_height - row;
1541 else
1542 mid_start = 0; /* redraw all lines */
1543 }
1544 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1545 {
1546 /*
1547 * Skip the lines (below the deleted lines) that are still
1548 * valid and don't need redrawing. Copy their info
1549 * upwards, to compensate for the deleted lines. Set
1550 * bot_start to the first row that needs redrawing.
1551 */
1552 bot_start = 0;
1553 idx = 0;
1554 for (;;)
1555 {
1556 wp->w_lines[idx] = wp->w_lines[j];
1557 /* stop at line that didn't fit, unless it is still
1558 * valid (no lines deleted) */
1559 if (row > 0 && bot_start + row
1560 + (int)wp->w_lines[j].wl_size > wp->w_height)
1561 {
1562 wp->w_lines_valid = idx + 1;
1563 break;
1564 }
1565 bot_start += wp->w_lines[idx++].wl_size;
1566
1567 /* stop at the last valid entry in w_lines[].wl_size */
1568 if (++j >= wp->w_lines_valid)
1569 {
1570 wp->w_lines_valid = idx;
1571 break;
1572 }
1573 }
1574#ifdef FEAT_DIFF
1575 /* Correct the first entry for filler lines at the top
1576 * when it won't get updated below. */
1577 if (wp->w_p_diff && bot_start > 0)
1578 wp->w_lines[0].wl_size =
1579 plines_win_nofill(wp, wp->w_topline, TRUE)
1580 + wp->w_topfill;
1581#endif
1582 }
1583 }
1584 }
1585
1586 /* When starting redraw in the first line, redraw all lines. When
1587 * there is only one window it's probably faster to clear the screen
1588 * first. */
1589 if (mid_start == 0)
1590 {
1591 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001592 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001593 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001594 /* Clear the screen when it was not done by win_del_lines() or
1595 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1596 * then. */
1597 if (screen_cleared != TRUE)
1598 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001599 /* The screen was cleared, redraw the tab pages line. */
1600 if (redraw_tabline)
1601 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001604
1605 /* When win_del_lines() or win_ins_lines() caused the screen to be
1606 * cleared (only happens for the first window) or when screenclear()
1607 * was called directly above, "must_redraw" will have been set to
1608 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1609 if (screen_cleared == TRUE)
1610 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 else
1613 {
1614 /* Not VALID or INVERTED: redraw all lines. */
1615 mid_start = 0;
1616 mid_end = wp->w_height;
1617 }
1618
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001619 if (type == SOME_VALID)
1620 {
1621 /* SOME_VALID: redraw all lines. */
1622 mid_start = 0;
1623 mid_end = wp->w_height;
1624 type = NOT_VALID;
1625 }
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 /* check if we are updating or removing the inverted part */
1628 if ((VIsual_active && buf == curwin->w_buffer)
1629 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1630 {
1631 linenr_T from, to;
1632
1633 if (VIsual_active)
1634 {
1635 if (VIsual_active
1636 && (VIsual_mode != wp->w_old_visual_mode
1637 || type == INVERTED_ALL))
1638 {
1639 /*
1640 * If the type of Visual selection changed, redraw the whole
1641 * selection. Also when the ownership of the X selection is
1642 * gained or lost.
1643 */
1644 if (curwin->w_cursor.lnum < VIsual.lnum)
1645 {
1646 from = curwin->w_cursor.lnum;
1647 to = VIsual.lnum;
1648 }
1649 else
1650 {
1651 from = VIsual.lnum;
1652 to = curwin->w_cursor.lnum;
1653 }
1654 /* redraw more when the cursor moved as well */
1655 if (wp->w_old_cursor_lnum < from)
1656 from = wp->w_old_cursor_lnum;
1657 if (wp->w_old_cursor_lnum > to)
1658 to = wp->w_old_cursor_lnum;
1659 if (wp->w_old_visual_lnum < from)
1660 from = wp->w_old_visual_lnum;
1661 if (wp->w_old_visual_lnum > to)
1662 to = wp->w_old_visual_lnum;
1663 }
1664 else
1665 {
1666 /*
1667 * Find the line numbers that need to be updated: The lines
1668 * between the old cursor position and the current cursor
1669 * position. Also check if the Visual position changed.
1670 */
1671 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1672 {
1673 from = curwin->w_cursor.lnum;
1674 to = wp->w_old_cursor_lnum;
1675 }
1676 else
1677 {
1678 from = wp->w_old_cursor_lnum;
1679 to = curwin->w_cursor.lnum;
1680 if (from == 0) /* Visual mode just started */
1681 from = to;
1682 }
1683
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001684 if (VIsual.lnum != wp->w_old_visual_lnum
1685 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 if (wp->w_old_visual_lnum < from
1688 && wp->w_old_visual_lnum != 0)
1689 from = wp->w_old_visual_lnum;
1690 if (wp->w_old_visual_lnum > to)
1691 to = wp->w_old_visual_lnum;
1692 if (VIsual.lnum < from)
1693 from = VIsual.lnum;
1694 if (VIsual.lnum > to)
1695 to = VIsual.lnum;
1696 }
1697 }
1698
1699 /*
1700 * If in block mode and changed column or curwin->w_curswant:
1701 * update all lines.
1702 * First compute the actual start and end column.
1703 */
1704 if (VIsual_mode == Ctrl_V)
1705 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001706 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001707#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar404406a2014-10-09 13:24:43 +02001710 if (curwin->w_p_lbr)
1711 ve_flags = VE_ALL;
1712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001714#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001715 ve_flags = save_ve_flags;
1716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 ++toc;
1718 if (curwin->w_curswant == MAXCOL)
1719 toc = MAXCOL;
1720
1721 if (fromc != wp->w_old_cursor_fcol
1722 || toc != wp->w_old_cursor_lcol)
1723 {
1724 if (from > VIsual.lnum)
1725 from = VIsual.lnum;
1726 if (to < VIsual.lnum)
1727 to = VIsual.lnum;
1728 }
1729 wp->w_old_cursor_fcol = fromc;
1730 wp->w_old_cursor_lcol = toc;
1731 }
1732 }
1733 else
1734 {
1735 /* Use the line numbers of the old Visual area. */
1736 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1737 {
1738 from = wp->w_old_cursor_lnum;
1739 to = wp->w_old_visual_lnum;
1740 }
1741 else
1742 {
1743 from = wp->w_old_visual_lnum;
1744 to = wp->w_old_cursor_lnum;
1745 }
1746 }
1747
1748 /*
1749 * There is no need to update lines above the top of the window.
1750 */
1751 if (from < wp->w_topline)
1752 from = wp->w_topline;
1753
1754 /*
1755 * If we know the value of w_botline, use it to restrict the update to
1756 * the lines that are visible in the window.
1757 */
1758 if (wp->w_valid & VALID_BOTLINE)
1759 {
1760 if (from >= wp->w_botline)
1761 from = wp->w_botline - 1;
1762 if (to >= wp->w_botline)
1763 to = wp->w_botline - 1;
1764 }
1765
1766 /*
1767 * Find the minimal part to be updated.
1768 * Watch out for scrolling that made entries in w_lines[] invalid.
1769 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1770 * top_end; need to redraw from top_end to the "to" line.
1771 * A middle mouse click with a Visual selection may change the text
1772 * above the Visual area and reset wl_valid, do count these for
1773 * mid_end (in srow).
1774 */
1775 if (mid_start > 0)
1776 {
1777 lnum = wp->w_topline;
1778 idx = 0;
1779 srow = 0;
1780 if (scrolled_down)
1781 mid_start = top_end;
1782 else
1783 mid_start = 0;
1784 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1785 {
1786 if (wp->w_lines[idx].wl_valid)
1787 mid_start += wp->w_lines[idx].wl_size;
1788 else if (!scrolled_down)
1789 srow += wp->w_lines[idx].wl_size;
1790 ++idx;
1791# ifdef FEAT_FOLDING
1792 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1793 lnum = wp->w_lines[idx].wl_lnum;
1794 else
1795# endif
1796 ++lnum;
1797 }
1798 srow += mid_start;
1799 mid_end = wp->w_height;
1800 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1801 {
1802 if (wp->w_lines[idx].wl_valid
1803 && wp->w_lines[idx].wl_lnum >= to + 1)
1804 {
1805 /* Only update until first row of this line */
1806 mid_end = srow;
1807 break;
1808 }
1809 srow += wp->w_lines[idx].wl_size;
1810 }
1811 }
1812 }
1813
1814 if (VIsual_active && buf == curwin->w_buffer)
1815 {
1816 wp->w_old_visual_mode = VIsual_mode;
1817 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1818 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001819 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 wp->w_old_curswant = curwin->w_curswant;
1821 }
1822 else
1823 {
1824 wp->w_old_visual_mode = 0;
1825 wp->w_old_cursor_lnum = 0;
1826 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001827 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1831 /* reset got_int, otherwise regexp won't work */
1832 save_got_int = got_int;
1833 got_int = 0;
1834#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001835#ifdef SYN_TIME_LIMIT
1836 /* Set the time limit to 'redrawtime'. */
1837 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001838 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_FOLDING
1841 win_foldinfo.fi_level = 0;
1842#endif
1843
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001844#ifdef FEAT_MENU
1845 /*
1846 * Draw the window toolbar, if there is one.
1847 * TODO: only when needed.
1848 */
1849 if (winbar_height(wp) > 0)
1850 redraw_win_toolbar(wp);
1851#endif
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 /*
1854 * Update all the window rows.
1855 */
1856 idx = 0; /* first entry in w_lines[].wl_size */
1857 row = 0;
1858 srow = 0;
1859 lnum = wp->w_topline; /* first line shown in window */
1860 for (;;)
1861 {
1862 /* stop updating when reached the end of the window (check for _past_
1863 * the end of the window is at the end of the loop) */
1864 if (row == wp->w_height)
1865 {
1866 didline = TRUE;
1867 break;
1868 }
1869
1870 /* stop updating when hit the end of the file */
1871 if (lnum > buf->b_ml.ml_line_count)
1872 {
1873 eof = TRUE;
1874 break;
1875 }
1876
1877 /* Remember the starting row of the line that is going to be dealt
1878 * with. It is used further down when the line doesn't fit. */
1879 srow = row;
1880
1881 /*
1882 * Update a line when it is in an area that needs updating, when it
1883 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001884 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 * win_del_lines(), check if the current line includes it.
1886 * When syntax folding is being used, the saved syntax states will
1887 * already have been updated, we can't see where the syntax state is
1888 * the same again, just update until the end of the window.
1889 */
1890 if (row < top_end
1891 || (row >= mid_start && row < mid_end)
1892#ifdef FEAT_SEARCH_EXTRA
1893 || top_to_mod
1894#endif
1895 || idx >= wp->w_lines_valid
1896 || (row + wp->w_lines[idx].wl_size > bot_start)
1897 || (mod_top != 0
1898 && (lnum == mod_top
1899 || (lnum >= mod_top
1900 && (lnum < mod_bot
1901#ifdef FEAT_SYN_HL
1902 || did_update == DID_FOLD
1903 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001904 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 && (
1906# ifdef FEAT_FOLDING
1907 (foldmethodIsSyntax(wp)
1908 && hasAnyFolding(wp)) ||
1909# endif
1910 syntax_check_changed(lnum)))
1911#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001912#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001913 /* match in fixed position might need redraw
1914 * if lines were inserted or deleted */
1915 || (wp->w_match_head != NULL
1916 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 )))))
1919 {
1920#ifdef FEAT_SEARCH_EXTRA
1921 if (lnum == mod_top)
1922 top_to_mod = FALSE;
1923#endif
1924
1925 /*
1926 * When at start of changed lines: May scroll following lines
1927 * up or down to minimize redrawing.
1928 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001929 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 if (lnum == mod_top
1932 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001933 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
1935 int old_rows = 0;
1936 int new_rows = 0;
1937 int xtra_rows;
1938 linenr_T l;
1939
1940 /* Count the old number of window rows, using w_lines[], which
1941 * should still contain the sizes for the lines as they are
1942 * currently displayed. */
1943 for (i = idx; i < wp->w_lines_valid; ++i)
1944 {
1945 /* Only valid lines have a meaningful wl_lnum. Invalid
1946 * lines are part of the changed area. */
1947 if (wp->w_lines[i].wl_valid
1948 && wp->w_lines[i].wl_lnum == mod_bot)
1949 break;
1950 old_rows += wp->w_lines[i].wl_size;
1951#ifdef FEAT_FOLDING
1952 if (wp->w_lines[i].wl_valid
1953 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1954 {
1955 /* Must have found the last valid entry above mod_bot.
1956 * Add following invalid entries. */
1957 ++i;
1958 while (i < wp->w_lines_valid
1959 && !wp->w_lines[i].wl_valid)
1960 old_rows += wp->w_lines[i++].wl_size;
1961 break;
1962 }
1963#endif
1964 }
1965
1966 if (i >= wp->w_lines_valid)
1967 {
1968 /* We can't find a valid line below the changed lines,
1969 * need to redraw until the end of the window.
1970 * Inserting/deleting lines has no use. */
1971 bot_start = 0;
1972 }
1973 else
1974 {
1975 /* Able to count old number of rows: Count new window
1976 * rows, and may insert/delete lines */
1977 j = idx;
1978 for (l = lnum; l < mod_bot; ++l)
1979 {
1980#ifdef FEAT_FOLDING
1981 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1982 ++new_rows;
1983 else
1984#endif
1985#ifdef FEAT_DIFF
1986 if (l == wp->w_topline)
1987 new_rows += plines_win_nofill(wp, l, TRUE)
1988 + wp->w_topfill;
1989 else
1990#endif
1991 new_rows += plines_win(wp, l, TRUE);
1992 ++j;
1993 if (new_rows > wp->w_height - row - 2)
1994 {
1995 /* it's getting too much, must redraw the rest */
1996 new_rows = 9999;
1997 break;
1998 }
1999 }
2000 xtra_rows = new_rows - old_rows;
2001 if (xtra_rows < 0)
2002 {
2003 /* May scroll text up. If there is not enough
2004 * remaining text or scrolling fails, must redraw the
2005 * rest. If scrolling works, must redraw the text
2006 * below the scrolled text. */
2007 if (row - xtra_rows >= wp->w_height - 2)
2008 mod_bot = MAXLNUM;
2009 else
2010 {
2011 check_for_delay(FALSE);
2012 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002013 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 mod_bot = MAXLNUM;
2015 else
2016 bot_start = wp->w_height + xtra_rows;
2017 }
2018 }
2019 else if (xtra_rows > 0)
2020 {
2021 /* May scroll text down. If there is not enough
2022 * remaining text of scrolling fails, must redraw the
2023 * rest. */
2024 if (row + xtra_rows >= wp->w_height - 2)
2025 mod_bot = MAXLNUM;
2026 else
2027 {
2028 check_for_delay(FALSE);
2029 if (win_ins_lines(wp, row + old_rows,
2030 xtra_rows, FALSE, FALSE) == FAIL)
2031 mod_bot = MAXLNUM;
2032 else if (top_end > row + old_rows)
2033 /* Scrolled the part at the top that requires
2034 * updating down. */
2035 top_end += xtra_rows;
2036 }
2037 }
2038
2039 /* When not updating the rest, may need to move w_lines[]
2040 * entries. */
2041 if (mod_bot != MAXLNUM && i != j)
2042 {
2043 if (j < i)
2044 {
2045 int x = row + new_rows;
2046
2047 /* move entries in w_lines[] upwards */
2048 for (;;)
2049 {
2050 /* stop at last valid entry in w_lines[] */
2051 if (i >= wp->w_lines_valid)
2052 {
2053 wp->w_lines_valid = j;
2054 break;
2055 }
2056 wp->w_lines[j] = wp->w_lines[i];
2057 /* stop at a line that won't fit */
2058 if (x + (int)wp->w_lines[j].wl_size
2059 > wp->w_height)
2060 {
2061 wp->w_lines_valid = j + 1;
2062 break;
2063 }
2064 x += wp->w_lines[j++].wl_size;
2065 ++i;
2066 }
2067 if (bot_start > x)
2068 bot_start = x;
2069 }
2070 else /* j > i */
2071 {
2072 /* move entries in w_lines[] downwards */
2073 j -= i;
2074 wp->w_lines_valid += j;
2075 if (wp->w_lines_valid > wp->w_height)
2076 wp->w_lines_valid = wp->w_height;
2077 for (i = wp->w_lines_valid; i - j >= idx; --i)
2078 wp->w_lines[i] = wp->w_lines[i - j];
2079
2080 /* The w_lines[] entries for inserted lines are
2081 * now invalid, but wl_size may be used above.
2082 * Reset to zero. */
2083 while (i >= idx)
2084 {
2085 wp->w_lines[i].wl_size = 0;
2086 wp->w_lines[i--].wl_valid = FALSE;
2087 }
2088 }
2089 }
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092
2093#ifdef FEAT_FOLDING
2094 /*
2095 * When lines are folded, display one line for all of them.
2096 * Otherwise, display normally (can be several display lines when
2097 * 'wrap' is on).
2098 */
2099 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2100 if (fold_count != 0)
2101 {
2102 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2103 ++row;
2104 --fold_count;
2105 wp->w_lines[idx].wl_folded = TRUE;
2106 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2107# ifdef FEAT_SYN_HL
2108 did_update = DID_FOLD;
2109# endif
2110 }
2111 else
2112#endif
2113 if (idx < wp->w_lines_valid
2114 && wp->w_lines[idx].wl_valid
2115 && wp->w_lines[idx].wl_lnum == lnum
2116 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002117 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 && srow + wp->w_lines[idx].wl_size > wp->w_height
2119#ifdef FEAT_DIFF
2120 && diff_check_fill(wp, lnum) == 0
2121#endif
2122 )
2123 {
2124 /* This line is not going to fit. Don't draw anything here,
2125 * will draw "@ " lines below. */
2126 row = wp->w_height + 1;
2127 }
2128 else
2129 {
2130#ifdef FEAT_SEARCH_EXTRA
2131 prepare_search_hl(wp, lnum);
2132#endif
2133#ifdef FEAT_SYN_HL
2134 /* Let the syntax stuff know we skipped a few lines. */
2135 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002136 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 syntax_end_parsing(syntax_last_parsed + 1);
2138#endif
2139
2140 /*
2141 * Display one line.
2142 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002143 row = win_line(wp, lnum, srow, wp->w_height,
2144 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146#ifdef FEAT_FOLDING
2147 wp->w_lines[idx].wl_folded = FALSE;
2148 wp->w_lines[idx].wl_lastlnum = lnum;
2149#endif
2150#ifdef FEAT_SYN_HL
2151 did_update = DID_LINE;
2152 syntax_last_parsed = lnum;
2153#endif
2154 }
2155
2156 wp->w_lines[idx].wl_lnum = lnum;
2157 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002158
2159 /* Past end of the window or end of the screen. Note that after
2160 * resizing wp->w_height may be end up too big. That's a problem
2161 * elsewhere, but prevent a crash here. */
2162 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002165 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2167 ++idx;
2168 break;
2169 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002170 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 wp->w_lines[idx].wl_size = row - srow;
2172 ++idx;
2173#ifdef FEAT_FOLDING
2174 lnum += fold_count + 1;
2175#else
2176 ++lnum;
2177#endif
2178 }
2179 else
2180 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002181 if (wp->w_p_rnu)
2182 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002183#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002184 // 'relativenumber' set: The text doesn't need to be drawn, but
2185 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002186 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2187 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002188 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002189 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002190#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002191 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002192 }
2193
2194 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 row += wp->w_lines[idx++].wl_size;
2196 if (row > wp->w_height) /* past end of screen */
2197 break;
2198#ifdef FEAT_FOLDING
2199 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2200#else
2201 ++lnum;
2202#endif
2203#ifdef FEAT_SYN_HL
2204 did_update = DID_NONE;
2205#endif
2206 }
2207
2208 if (lnum > buf->b_ml.ml_line_count)
2209 {
2210 eof = TRUE;
2211 break;
2212 }
2213 }
2214 /*
2215 * End of loop over all window lines.
2216 */
2217
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002218#ifdef FEAT_VTP
2219 /* Rewrite the character at the end of the screen line. */
2220 if (use_vtp())
2221 {
2222 int i;
2223
2224 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002225 if (enc_utf8)
2226 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2227 LineOffset[i] + screen_Columns) > 1)
2228 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2229 else
2230 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2231 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002232 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2233 }
2234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 if (idx > wp->w_lines_valid)
2237 wp->w_lines_valid = idx;
2238
2239#ifdef FEAT_SYN_HL
2240 /*
2241 * Let the syntax stuff know we stop parsing here.
2242 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002243 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 syntax_end_parsing(syntax_last_parsed + 1);
2245#endif
2246
2247 /*
2248 * If we didn't hit the end of the file, and we didn't finish the last
2249 * line we were working on, then the line didn't fit.
2250 */
2251 wp->w_empty_rows = 0;
2252#ifdef FEAT_DIFF
2253 wp->w_filler_rows = 0;
2254#endif
2255 if (!eof && !didline)
2256 {
2257 if (lnum == wp->w_topline)
2258 {
2259 /*
2260 * Single line that does not fit!
2261 * Don't overwrite it, it can be edited.
2262 */
2263 wp->w_botline = lnum + 1;
2264 }
2265#ifdef FEAT_DIFF
2266 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2267 {
2268 /* Window ends in filler lines. */
2269 wp->w_botline = lnum;
2270 wp->w_filler_rows = wp->w_height - srow;
2271 }
2272#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002273 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2274 {
2275 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2276
2277 /*
2278 * Last line isn't finished: Display "@@@" in the last screen line.
2279 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002280 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002281 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002282 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002283 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002284 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002285 set_empty_rows(wp, srow);
2286 wp->w_botline = lnum;
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2289 {
2290 /*
2291 * Last line isn't finished: Display "@@@" at the end.
2292 */
2293 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2294 W_WINROW(wp) + wp->w_height,
2295 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002296 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 set_empty_rows(wp, srow);
2298 wp->w_botline = lnum;
2299 }
2300 else
2301 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002302 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 wp->w_botline = lnum;
2304 }
2305 }
2306 else
2307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (eof) /* we hit the end of the file */
2310 {
2311 wp->w_botline = buf->b_ml.ml_line_count + 1;
2312#ifdef FEAT_DIFF
2313 j = diff_check_fill(wp, wp->w_botline);
2314 if (j > 0 && !wp->w_botfill)
2315 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002316 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (char2cells(fill_diff) > 1)
2318 i = '-';
2319 else
2320 i = fill_diff;
2321 if (row + j > wp->w_height)
2322 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002323 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 row += j;
2325 }
2326#endif
2327 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002328 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 wp->w_botline = lnum;
2330
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002331 // Make sure the rest of the screen is blank
2332 // put '~'s on rows that aren't part of the file.
2333 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002336#ifdef SYN_TIME_LIMIT
2337 syn_set_timeout(NULL);
2338#endif
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 /* Reset the type of redrawing required, the window has been updated. */
2341 wp->w_redr_type = 0;
2342#ifdef FEAT_DIFF
2343 wp->w_old_topfill = wp->w_topfill;
2344 wp->w_old_botfill = wp->w_botfill;
2345#endif
2346
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002347 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 /*
2350 * There is a trick with w_botline. If we invalidate it on each
2351 * change that might modify it, this will cause a lot of expensive
2352 * calls to plines() in update_topline() each time. Therefore the
2353 * value of w_botline is often approximated, and this value is used to
2354 * compute the value of w_topline. If the value of w_botline was
2355 * wrong, check that the value of w_topline is correct (cursor is on
2356 * the visible part of the text). If it's not, we need to redraw
2357 * again. Mostly this just means scrolling up a few lines, so it
2358 * doesn't look too bad. Only do this for the current window (where
2359 * changes are relevant).
2360 */
2361 wp->w_valid |= VALID_BOTLINE;
2362 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2363 {
2364 recursive = TRUE;
2365 curwin->w_valid &= ~VALID_TOPLINE;
2366 update_topline(); /* may invalidate w_botline again */
2367 if (must_redraw != 0)
2368 {
2369 /* Don't update for changes in buffer again. */
2370 i = curbuf->b_mod_set;
2371 curbuf->b_mod_set = FALSE;
2372 win_update(curwin);
2373 must_redraw = 0;
2374 curbuf->b_mod_set = i;
2375 }
2376 recursive = FALSE;
2377 }
2378 }
2379
2380#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2381 /* restore got_int, unless CTRL-C was hit while redrawing */
2382 if (!got_int)
2383 got_int = save_got_int;
2384#endif
2385}
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002388 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2389 * Return the new offset.
2390 */
2391 static int
2392screen_fill_end(
2393 win_T *wp,
2394 int c1,
2395 int c2,
2396 int off,
2397 int width,
2398 int row,
2399 int endrow,
2400 int attr)
2401{
2402 int nn = off + width;
2403
2404 if (nn > wp->w_width)
2405 nn = wp->w_width;
2406#ifdef FEAT_RIGHTLEFT
2407 if (wp->w_p_rl)
2408 {
2409 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2410 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2411 c1, c2, attr);
2412 }
2413 else
2414#endif
2415 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2416 wp->w_wincol + off, (int)wp->w_wincol + nn,
2417 c1, c2, attr);
2418 return nn;
2419}
2420
2421/*
2422 * Clear lines near the end the window and mark the unused lines with "c1".
2423 * use "c2" as the filler character.
2424 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 */
2426 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002427win_draw_end(
2428 win_T *wp,
2429 int c1,
2430 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002431 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002432 int row,
2433 int endrow,
2434 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002437 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002438 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002439
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002440 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002441
2442 if (draw_margin)
2443 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002444#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002445 int fdc = compute_foldcolumn(wp, 0);
2446
2447 if (fdc > 0)
2448 // draw the fold column
2449 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002450 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002451#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002452#ifdef FEAT_SIGNS
2453 if (signcolumn_on(wp))
2454 // draw the sign column
2455 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002456 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002457#endif
2458 if ((wp->w_p_nu || wp->w_p_rnu)
2459 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2460 // draw the number column
2461 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002462 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465#ifdef FEAT_RIGHTLEFT
2466 if (wp->w_p_rl)
2467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002469 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002470 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002472 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002473 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475 else
2476#endif
2477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002479 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002480 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 set_empty_rows(wp, row);
2484}
2485
Bram Moolenaar1a384422010-07-14 19:53:30 +02002486#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002487/*
2488 * Advance **color_cols and return TRUE when there are columns to draw.
2489 */
2490 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002491advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002492{
2493 while (**color_cols >= 0 && vcol > **color_cols)
2494 ++*color_cols;
2495 return (**color_cols >= 0);
2496}
2497#endif
2498
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002499#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2500/*
2501 * Copy "text" to ScreenLines using "attr".
2502 * Returns the next screen column.
2503 */
2504 static int
2505text_to_screenline(win_T *wp, char_u *text, int col)
2506{
2507 int off = (int)(current_ScreenLine - ScreenLines);
2508
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002509 if (has_mbyte)
2510 {
2511 int cells;
2512 int u8c, u8cc[MAX_MCO];
2513 int i;
2514 int idx;
2515 int c_len;
2516 char_u *p;
2517# ifdef FEAT_ARABIC
2518 int prev_c = 0; /* previous Arabic character */
2519 int prev_c1 = 0; /* first composing char for prev_c */
2520# endif
2521
2522# ifdef FEAT_RIGHTLEFT
2523 if (wp->w_p_rl)
2524 idx = off;
2525 else
2526# endif
2527 idx = off + col;
2528
2529 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2530 for (p = text; *p != NUL; )
2531 {
2532 cells = (*mb_ptr2cells)(p);
2533 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002534 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002535# ifdef FEAT_RIGHTLEFT
2536 - (wp->w_p_rl ? col : 0)
2537# endif
2538 )
2539 break;
2540 ScreenLines[idx] = *p;
2541 if (enc_utf8)
2542 {
2543 u8c = utfc_ptr2char(p, u8cc);
2544 if (*p < 0x80 && u8cc[0] == 0)
2545 {
2546 ScreenLinesUC[idx] = 0;
2547#ifdef FEAT_ARABIC
2548 prev_c = u8c;
2549#endif
2550 }
2551 else
2552 {
2553#ifdef FEAT_ARABIC
2554 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2555 {
2556 /* Do Arabic shaping. */
2557 int pc, pc1, nc;
2558 int pcc[MAX_MCO];
2559 int firstbyte = *p;
2560
2561 /* The idea of what is the previous and next
2562 * character depends on 'rightleft'. */
2563 if (wp->w_p_rl)
2564 {
2565 pc = prev_c;
2566 pc1 = prev_c1;
2567 nc = utf_ptr2char(p + c_len);
2568 prev_c1 = u8cc[0];
2569 }
2570 else
2571 {
2572 pc = utfc_ptr2char(p + c_len, pcc);
2573 nc = prev_c;
2574 pc1 = pcc[0];
2575 }
2576 prev_c = u8c;
2577
2578 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2579 pc, pc1, nc);
2580 ScreenLines[idx] = firstbyte;
2581 }
2582 else
2583 prev_c = u8c;
2584#endif
2585 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002586 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002587 for (i = 0; i < Screen_mco; ++i)
2588 {
2589 ScreenLinesC[i][idx] = u8cc[i];
2590 if (u8cc[i] == 0)
2591 break;
2592 }
2593 }
2594 if (cells > 1)
2595 ScreenLines[idx + 1] = 0;
2596 }
2597 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2598 /* double-byte single width character */
2599 ScreenLines2[idx] = p[1];
2600 else if (cells > 1)
2601 /* double-width character */
2602 ScreenLines[idx + 1] = p[1];
2603 col += cells;
2604 idx += cells;
2605 p += c_len;
2606 }
2607 }
2608 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002609 {
2610 int len = (int)STRLEN(text);
2611
Bram Moolenaar02631462017-09-22 15:20:32 +02002612 if (len > wp->w_width - col)
2613 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002614 if (len > 0)
2615 {
2616#ifdef FEAT_RIGHTLEFT
2617 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002618 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002619 else
2620#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002621 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002622 col += len;
2623 }
2624 }
2625 return col;
2626}
2627#endif
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#ifdef FEAT_FOLDING
2630/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002631 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2632 * space is available for window "wp", minus "col".
2633 */
2634 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002635compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002636{
2637 int fdc = wp->w_p_fdc;
2638 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002639 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002640
2641 if (fdc > wwidth - (col + wmw))
2642 fdc = wwidth - (col + wmw);
2643 return fdc;
2644}
2645
2646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 * Display one folded line.
2648 */
2649 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002650fold_line(
2651 win_T *wp,
2652 long fold_count,
2653 foldinfo_T *foldinfo,
2654 linenr_T lnum,
2655 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002657 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 pos_T *top, *bot;
2659 linenr_T lnume = lnum + fold_count - 1;
2660 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002661 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 int col;
2664 int txtcol;
2665 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002666 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 /* Build the fold line:
2669 * 1. Add the cmdwin_type for the command-line window
2670 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002671 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 * 4. Compose the text
2673 * 5. Add the text
2674 * 6. set highlighting for the Visual area an other text
2675 */
2676 col = 0;
2677
2678 /*
2679 * 1. Add the cmdwin_type for the command-line window
2680 * Ignores 'rightleft', this window is never right-left.
2681 */
2682#ifdef FEAT_CMDWIN
2683 if (cmdwin_type != 0 && wp == curwin)
2684 {
2685 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002686 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (enc_utf8)
2688 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 ++col;
2690 }
2691#endif
2692
2693 /*
2694 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002695 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002697 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (fdc > 0)
2699 {
2700 fill_foldcolumn(buf, wp, TRUE, lnum);
2701#ifdef FEAT_RIGHTLEFT
2702 if (wp->w_p_rl)
2703 {
2704 int i;
2705
Bram Moolenaar02631462017-09-22 15:20:32 +02002706 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002707 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 /* reverse the fold column */
2709 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002710 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712 else
2713#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002714 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 col += fdc;
2716 }
2717
2718#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002719# define RL_MEMSET(p, v, l) \
2720 do { \
2721 if (wp->w_p_rl) \
2722 for (ri = 0; ri < l; ++ri) \
2723 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2724 else \
2725 for (ri = 0; ri < l; ++ri) \
2726 ScreenAttrs[off + (p) + ri] = v; \
2727 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002729# define RL_MEMSET(p, v, l) \
2730 do { \
2731 for (ri = 0; ri < l; ++ri) \
2732 ScreenAttrs[off + (p) + ri] = v; \
2733 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734#endif
2735
Bram Moolenaar64486672010-05-16 15:46:46 +02002736 /* Set all attributes of the 'number' or 'relativenumber' column and the
2737 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002738 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739
2740#ifdef FEAT_SIGNS
2741 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002742 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002744 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (len > 0)
2746 {
2747 if (len > 2)
2748 len = 2;
2749# ifdef FEAT_RIGHTLEFT
2750 if (wp->w_p_rl)
2751 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002752 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002753 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 else
2755# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002756 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 col += len;
2758 }
2759 }
2760#endif
2761
2762 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002763 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002765 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002767 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (len > 0)
2769 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002770 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002771 long num;
2772 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002773
2774 if (len > w + 1)
2775 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002776
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002777 if (wp->w_p_nu && !wp->w_p_rnu)
2778 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002779 num = (long)lnum;
2780 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002781 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002782 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002783 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002784 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002785 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002786 /* 'number' + 'relativenumber': cursor line shows absolute
2787 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002788 num = lnum;
2789 fmt = "%-*ld ";
2790 }
2791 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002792
Bram Moolenaar700e7342013-01-30 12:31:36 +01002793 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794#ifdef FEAT_RIGHTLEFT
2795 if (wp->w_p_rl)
2796 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002797 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002798 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 else
2800#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002801 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 col += len;
2803 }
2804 }
2805
2806 /*
2807 * 4. Compose the folded-line string with 'foldtext', if set.
2808 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002809 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811 txtcol = col; /* remember where text starts */
2812
2813 /*
2814 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2815 * Right-left text is put in columns 0 - number-col, normal text is put
2816 * in columns number-col - window-width.
2817 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002818 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820 /* Fill the rest of the line with the fold filler */
2821#ifdef FEAT_RIGHTLEFT
2822 if (wp->w_p_rl)
2823 col -= txtcol;
2824#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002825 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826#ifdef FEAT_RIGHTLEFT
2827 - (wp->w_p_rl ? txtcol : 0)
2828#endif
2829 )
2830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 if (enc_utf8)
2832 {
2833 if (fill_fold >= 0x80)
2834 {
2835 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002836 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002837 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002842 ScreenLines[off + col] = fill_fold;
2843 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002844 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002846 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002847 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
2850 if (text != buf)
2851 vim_free(text);
2852
2853 /*
2854 * 6. set highlighting for the Visual area an other text.
2855 * If all folded lines are in the Visual area, highlight the line.
2856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2858 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002859 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 {
2861 /* Visual is after curwin->w_cursor */
2862 top = &curwin->w_cursor;
2863 bot = &VIsual;
2864 }
2865 else
2866 {
2867 /* Visual is before curwin->w_cursor */
2868 top = &VIsual;
2869 bot = &curwin->w_cursor;
2870 }
2871 if (lnum >= top->lnum
2872 && lnume <= bot->lnum
2873 && (VIsual_mode != 'v'
2874 || ((lnum > top->lnum
2875 || (lnum == top->lnum
2876 && top->col == 0))
2877 && (lnume < bot->lnum
2878 || (lnume == bot->lnum
2879 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002880 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
2882 if (VIsual_mode == Ctrl_V)
2883 {
2884 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002885 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002887 if (wp->w_old_cursor_lcol != MAXCOL
2888 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 len = wp->w_old_cursor_lcol;
2891 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002892 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002893 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002894 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 }
2897 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002900 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002905#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002906 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2907 if (wp->w_p_cc_cols)
2908 {
2909 int i = 0;
2910 int j = wp->w_p_cc_cols[i];
2911 int old_txtcol = txtcol;
2912
2913 while (j > -1)
2914 {
2915 txtcol += j;
2916 if (wp->w_p_wrap)
2917 txtcol -= wp->w_skipcol;
2918 else
2919 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002920 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002921 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002922 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002923 txtcol = old_txtcol;
2924 j = wp->w_p_cc_cols[++i];
2925 }
2926 }
2927
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002928 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002929 if (wp->w_p_cuc)
2930 {
2931 txtcol += wp->w_virtcol;
2932 if (wp->w_p_wrap)
2933 txtcol -= wp->w_skipcol;
2934 else
2935 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002936 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002937 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002938 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002939 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
Bram Moolenaar02631462017-09-22 15:20:32 +02002942 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002943 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944
2945 /*
2946 * Update w_cline_height and w_cline_folded if the cursor line was
2947 * updated (saves a call to plines() later).
2948 */
2949 if (wp == curwin
2950 && lnum <= curwin->w_cursor.lnum
2951 && lnume >= curwin->w_cursor.lnum)
2952 {
2953 curwin->w_cline_row = row;
2954 curwin->w_cline_height = 1;
2955 curwin->w_cline_folded = TRUE;
2956 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2957 }
2958}
2959
2960/*
2961 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2962 */
2963 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002964copy_text_attr(
2965 int off,
2966 char_u *buf,
2967 int len,
2968 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002970 int i;
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (enc_utf8)
2974 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002975 for (i = 0; i < len; ++i)
2976 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977}
2978
2979/*
2980 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002981 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 */
2983 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002984fill_foldcolumn(
2985 char_u *p,
2986 win_T *wp,
2987 int closed, /* TRUE of FALSE */
2988 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
2990 int i = 0;
2991 int level;
2992 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002993 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002994 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995
2996 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002997 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999 level = win_foldinfo.fi_level;
3000 if (level > 0)
3001 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003002 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003003 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 /* If the column is too narrow, we start at the lowest level that
3006 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003007 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (first_level < 1)
3009 first_level = 1;
3010
Bram Moolenaar1c934292015-01-27 16:39:29 +01003011 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 if (win_foldinfo.fi_lnum == lnum
3014 && first_level + i >= win_foldinfo.fi_low_level)
3015 p[i] = '-';
3016 else if (first_level == 1)
3017 p[i] = '|';
3018 else if (first_level + i <= 9)
3019 p[i] = '0' + first_level + i;
3020 else
3021 p[i] = '>';
3022 if (first_level + i == level)
3023 break;
3024 }
3025 }
3026 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003027 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028}
3029#endif /* FEAT_FOLDING */
3030
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003031#ifdef FEAT_TEXT_PROP
3032static textprop_T *current_text_props = NULL;
3033static buf_T *current_buf = NULL;
3034
3035 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003036text_prop_compare(const void *s1, const void *s2)
3037{
3038 int idx1, idx2;
3039 proptype_T *pt1, *pt2;
3040 colnr_T col1, col2;
3041
3042 idx1 = *(int *)s1;
3043 idx2 = *(int *)s2;
3044 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3045 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3046 if (pt1 == pt2)
3047 return 0;
3048 if (pt1 == NULL)
3049 return -1;
3050 if (pt2 == NULL)
3051 return 1;
3052 if (pt1->pt_priority != pt2->pt_priority)
3053 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3054 col1 = current_text_props[idx1].tp_col;
3055 col2 = current_text_props[idx2].tp_col;
3056 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3057}
3058#endif
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060/*
3061 * Display line "lnum" of window 'wp' on the screen.
3062 * Start at row "startrow", stop when "endrow" is reached.
3063 * wp->w_virtcol needs to be valid.
3064 *
3065 * Return the number of last row the line occupies.
3066 */
3067 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003068win_line(
3069 win_T *wp,
3070 linenr_T lnum,
3071 int startrow,
3072 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003073 int nochange UNUSED, // not updating for changed text
3074 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003076 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3078 int c = 0; /* init for GCC */
3079 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003080#ifdef FEAT_LINEBREAK
3081 long vcol_sbr = -1; /* virtual column after showbreak */
3082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 long vcol_prev = -1; /* "vcol" of previous character */
3084 char_u *line; /* current line */
3085 char_u *ptr; /* current position in "line" */
3086 int row; /* row in the window, excl w_winrow */
3087 int screen_row; /* row on the screen, incl w_winrow */
3088
3089 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3090 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003091 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003092 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003094 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int extra_attr = 0; /* attributes when n_extra != 0 */
3096 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3097 displaying lcs_eol at end-of-line */
3098 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3099 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3100
3101 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3102 int saved_n_extra = 0;
3103 char_u *saved_p_extra = NULL;
3104 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003105 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 int saved_char_attr = 0;
3107
3108 int n_attr = 0; /* chars with special attr */
3109 int saved_attr2 = 0; /* char_attr saved for n_attr */
3110 int n_attr3 = 0; /* chars with overruling special attr */
3111 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3112
3113 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3114
3115 int fromcol, tocol; /* start/end of inverting */
3116 int fromcol_prev = -2; /* start of inverting after cursor */
3117 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003119 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 pos_T pos;
3121 long v;
3122
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003123 int char_attr = 0; // attributes for next character
3124 int attr_pri = FALSE; // char_attr has priority
3125 int area_highlighting = FALSE; // Visual or incsearch highlighting
3126 // in this line
3127 int vi_attr = 0; // attributes for Visual and incsearch
3128 // highlighting
3129 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003130 int win_attr = 0; // background for whole window, except
3131 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003132 int area_attr = 0; // attributes desired by highlighting
3133 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003135 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 int syntax_attr = 0; /* attributes desired by syntax */
3137 int has_syntax = FALSE; /* this buffer has syntax highl. */
3138 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003139 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003140 int draw_color_col = FALSE; /* highlight colorcolumn */
3141 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003142#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003143#ifdef FEAT_TEXT_PROP
3144 int text_prop_count;
3145 int text_prop_next = 0; // next text property to use
3146 textprop_T *text_props = NULL;
3147 int *text_prop_idxs = NULL;
3148 int text_props_active = 0;
3149 proptype_T *text_prop_type = NULL;
3150 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003151 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003152#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003153#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003154 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003155# define SPWORDLEN 150
3156 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003157 int nextlinecol = 0; /* column where nextline[] starts */
3158 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003159 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003160 int spell_attr = 0; /* attributes desired by spelling */
3161 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003162 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3163 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003164 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003165 static int cap_col = -1; /* column to check for Cap word */
3166 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003167 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003169 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 int multi_attr = 0; /* attributes desired by multibyte */
3171 int mb_l = 1; /* multi-byte byte length */
3172 int mb_c = 0; /* decoded multi-byte character */
3173 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003174 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175#ifdef FEAT_DIFF
3176 int filler_lines; /* nr of filler lines to be drawn */
3177 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003178 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 int change_start = MAXCOL; /* first col of changed area */
3180 int change_end = -1; /* last col of changed area */
3181#endif
3182 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3183#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003184 int need_showbreak = FALSE; /* overlong line, skipping first x
3185 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003187#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003188 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003190 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191#endif
3192#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003193 matchitem_T *cur; /* points to the match list */
3194 match_T *shl; /* points to search_hl or a match */
3195 int shl_flag; /* flag to indicate whether search_hl
3196 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003197 int pos_inprogress; /* marks that position match search is
3198 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003199 int prevcol_hl_flag; /* flag to indicate whether prevcol
3200 equals startcol of search_hl or one
3201 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#endif
3203#ifdef FEAT_ARABIC
3204 int prev_c = 0; /* previous Arabic character */
3205 int prev_c1 = 0; /* first composing char for prev_c */
3206#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003207#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003208 int did_line_attr = 0;
3209#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003210#ifdef FEAT_TERMINAL
3211 int get_term_attr = FALSE;
3212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 /* draw_state: items that are drawn in sequence: */
3215#define WL_START 0 /* nothing done yet */
3216#ifdef FEAT_CMDWIN
3217# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3218#else
3219# define WL_CMDLINE WL_START
3220#endif
3221#ifdef FEAT_FOLDING
3222# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3223#else
3224# define WL_FOLD WL_CMDLINE
3225#endif
3226#ifdef FEAT_SIGNS
3227# define WL_SIGN WL_FOLD + 1 /* column for signs */
3228#else
3229# define WL_SIGN WL_FOLD /* column for signs */
3230#endif
3231#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003232#ifdef FEAT_LINEBREAK
3233# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003235# define WL_BRI WL_NR
3236#endif
3237#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3238# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3239#else
3240# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241#endif
3242#define WL_LINE WL_SBR + 1 /* text in the line */
3243 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003244#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 int feedback_col = 0;
3246 int feedback_old_attr = -1;
3247#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003248 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaar860cae12010-06-05 23:22:07 +02003250#ifdef FEAT_CONCEAL
3251 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003252 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003253 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003254 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003255 int is_concealing = FALSE;
3256 int boguscols = 0; /* nonexistent columns added to force
3257 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003258 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003259 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003260 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003261 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003262# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003263# define FIX_FOR_BOGUSCOLS \
3264 { \
3265 n_extra += vcol_off; \
3266 vcol -= vcol_off; \
3267 vcol_off = 0; \
3268 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003269 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003270 boguscols = 0; \
3271 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003272#else
3273# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 if (startrow > endrow) /* past the end already! */
3277 return startrow;
3278
3279 row = startrow;
3280 screen_row = row + W_WINROW(wp);
3281
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003282 if (!number_only)
3283 {
3284 /*
3285 * To speed up the loop below, set extra_check when there is linebreak,
3286 * trailing white space and/or syntax processing to be done.
3287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003289 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#endif
3291#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003292 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003293# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003294 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003295# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003296 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003298 /* Prepare for syntax highlighting in this line. When there is an
3299 * error, stop syntax highlighting. */
3300 save_did_emsg = did_emsg;
3301 did_emsg = FALSE;
3302 syntax_start(wp, lnum);
3303 if (did_emsg)
3304 wp->w_s->b_syn_error = TRUE;
3305 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003306 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003307 did_emsg = save_did_emsg;
3308#ifdef SYN_TIME_LIMIT
3309 if (!wp->w_s->b_syn_slow)
3310#endif
3311 {
3312 has_syntax = TRUE;
3313 extra_check = TRUE;
3314 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003317
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003318 /* Check for columns to display for 'colorcolumn'. */
3319 color_cols = wp->w_p_cc_cols;
3320 if (color_cols != NULL)
3321 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003322#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003323
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003324#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003325 if (term_show_buffer(wp->w_buffer))
3326 {
3327 extra_check = TRUE;
3328 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003329 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003330 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003331#endif
3332
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003333#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003334 if (wp->w_p_spell
3335 && *wp->w_s->b_p_spl != NUL
3336 && wp->w_s->b_langp.ga_len > 0
3337 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003338 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003339 /* Prepare for spell checking. */
3340 has_spell = TRUE;
3341 extra_check = TRUE;
3342
Bram Moolenaar7701f302018-10-02 21:20:32 +02003343 /* Get the start of the next line, so that words that wrap to the
3344 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003345 * Trick: skip a few chars for C/shell/Vim comments */
3346 nextline[SPWORDLEN] = NUL;
3347 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3348 {
3349 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3350 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3351 }
3352
Bram Moolenaar7701f302018-10-02 21:20:32 +02003353 /* When a word wrapped from the previous line the start of the
3354 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003355 if (lnum == checked_lnum)
3356 cur_checked_col = checked_col;
3357 checked_lnum = 0;
3358
3359 /* When there was a sentence end in the previous line may require a
3360 * word starting with capital in this line. In line 1 always check
3361 * the first word. */
3362 if (lnum != capcol_lnum)
3363 cap_col = -1;
3364 if (lnum == 1)
3365 cap_col = 0;
3366 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#endif
3369
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003370 /*
3371 * handle visual active in this window
3372 */
3373 fromcol = -10;
3374 tocol = MAXCOL;
3375 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003377 /* Visual is after curwin->w_cursor */
3378 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003380 top = &curwin->w_cursor;
3381 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003383 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 top = &VIsual;
3386 bot = &curwin->w_cursor;
3387 }
3388 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3389 if (VIsual_mode == Ctrl_V) /* block mode */
3390 {
3391 if (lnum_in_visual_area)
3392 {
3393 fromcol = wp->w_old_cursor_fcol;
3394 tocol = wp->w_old_cursor_lcol;
3395 }
3396 }
3397 else /* non-block mode */
3398 {
3399 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003401 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 if (VIsual_mode == 'V') /* linewise */
3404 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else
3406 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003407 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3408 if (gchar_pos(top) == NUL)
3409 tocol = fromcol + 1;
3410 }
3411 }
3412 if (VIsual_mode != 'V' && lnum == bot->lnum)
3413 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003414 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003415 {
3416 fromcol = -10;
3417 tocol = MAXCOL;
3418 }
3419 else if (bot->col == MAXCOL)
3420 tocol = MAXCOL;
3421 else
3422 {
3423 pos = *bot;
3424 if (*p_sel == 'e')
3425 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3426 else
3427 {
3428 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3429 ++tocol;
3430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 }
3432 }
3433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003435 /* Check if the character under the cursor should not be inverted */
3436 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003437#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003438 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003439#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003440 )
3441 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003443 /* if inverting in this line set area_highlighting */
3444 if (fromcol >= 0)
3445 {
3446 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003447 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003449 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003450 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003451 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003452 && clip_isautosel_plus()))
3453 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003458 /*
3459 * handle 'incsearch' and ":s///c" highlighting
3460 */
3461 else if (highlight_match
3462 && wp == curwin
3463 && lnum >= curwin->w_cursor.lnum
3464 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003466 if (lnum == curwin->w_cursor.lnum)
3467 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003468 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003469 else
3470 fromcol = 0;
3471 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3472 {
3473 pos.lnum = lnum;
3474 pos.col = search_match_endcol;
3475 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3476 }
3477 else
3478 tocol = MAXCOL;
3479 /* do at least one character; happens when past end of line */
3480 if (fromcol == tocol)
3481 tocol = fromcol + 1;
3482 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003483 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486
3487#ifdef FEAT_DIFF
3488 filler_lines = diff_check(wp, lnum);
3489 if (filler_lines < 0)
3490 {
3491 if (filler_lines == -1)
3492 {
3493 if (diff_find_change(wp, lnum, &change_start, &change_end))
3494 diff_hlf = HLF_ADD; /* added line */
3495 else if (change_start == 0)
3496 diff_hlf = HLF_TXD; /* changed text */
3497 else
3498 diff_hlf = HLF_CHD; /* changed line */
3499 }
3500 else
3501 diff_hlf = HLF_ADD; /* added line */
3502 filler_lines = 0;
3503 area_highlighting = TRUE;
3504 }
3505 if (lnum == wp->w_topline)
3506 filler_lines = wp->w_topfill;
3507 filler_todo = filler_lines;
3508#endif
3509
3510#ifdef LINE_ATTR
3511# ifdef FEAT_SIGNS
3512 /* If this line has a sign with line highlighting set line_attr. */
3513 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3514 if (v != 0)
3515 line_attr = sign_get_attr((int)v, TRUE);
3516# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003517# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003519 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003520 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521# endif
3522 if (line_attr != 0)
3523 area_highlighting = TRUE;
3524#endif
3525
3526 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3527 ptr = line;
3528
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003529#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003530 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003531 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003532 /* For checking first word with a capital skip white space. */
3533 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003534 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003535
Bram Moolenaar30abd282005-06-22 22:35:10 +00003536 /* To be able to spell-check over line boundaries copy the end of the
3537 * current line into nextline[]. Above the start of the next line was
3538 * copied to nextline[SPWORDLEN]. */
3539 if (nextline[SPWORDLEN] == NUL)
3540 {
3541 /* No next line or it is empty. */
3542 nextlinecol = MAXCOL;
3543 nextline_idx = 0;
3544 }
3545 else
3546 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003547 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003548 if (v < SPWORDLEN)
3549 {
3550 /* Short line, use it completely and append the start of the
3551 * next line. */
3552 nextlinecol = 0;
3553 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003554 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003555 nextline_idx = v + 1;
3556 }
3557 else
3558 {
3559 /* Long line, use only the last SPWORDLEN bytes. */
3560 nextlinecol = v - SPWORDLEN;
3561 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3562 nextline_idx = SPWORDLEN + 1;
3563 }
3564 }
3565 }
3566#endif
3567
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003568 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003570 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003571 extra_check = TRUE;
3572 /* find start of trailing whitespace */
3573 if (lcs_trail)
3574 {
3575 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003576 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003577 --trailcol;
3578 trailcol += (colnr_T) (ptr - line);
3579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003582 wcr_attr = get_wcr_attr(wp);
3583 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003584 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003585 win_attr = wcr_attr;
3586 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003587 }
3588#ifdef FEAT_TEXT_PROP
3589 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003590 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003591#endif
3592
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 /*
3594 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3595 * first character to be displayed.
3596 */
3597 if (wp->w_p_wrap)
3598 v = wp->w_skipcol;
3599 else
3600 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003601 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 while (vcol < v && *ptr != NUL)
3606 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003607 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003610 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003613 /* When:
3614 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003615 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003616 * - 'virtualedit' is set, or
3617 * - the visual mode is active,
3618 * the end of the line may be before the start of the displayed part.
3619 */
3620 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003621#ifdef FEAT_SYN_HL
3622 wp->w_p_cuc || draw_color_col ||
3623#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003624 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003625 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
3628 /* Handle a character that's not completely on the screen: Put ptr at
3629 * that character but skip the first few screen characters. */
3630 if (vcol > v)
3631 {
3632 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003634 /* If the character fits on the screen, don't need to skip it.
3635 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003636 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003637 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639
3640 /*
3641 * Adjust for when the inverted text is before the screen,
3642 * and when the start of the inverted text is before the screen.
3643 */
3644 if (tocol <= vcol)
3645 fromcol = 0;
3646 else if (fromcol >= 0 && fromcol < vcol)
3647 fromcol = vcol;
3648
3649#ifdef FEAT_LINEBREAK
3650 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3651 if (wp->w_p_wrap)
3652 need_showbreak = TRUE;
3653#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003654#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003655 /* When spell checking a word we need to figure out the start of the
3656 * word and if it's badly spelled or not. */
3657 if (has_spell)
3658 {
3659 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003660 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003661 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003662
3663 pos = wp->w_cursor;
3664 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003665 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003666 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003667
3668 /* spell_move_to() may call ml_get() and make "line" invalid */
3669 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3670 ptr = line + linecol;
3671
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003672 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003673 {
3674 /* no bad word found at line start, don't check until end of a
3675 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003676 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003677 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003678 }
3679 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003680 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003681 /* bad word found, use attributes until end of word */
3682 word_end = wp->w_cursor.col + len + 1;
3683
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003684 /* Turn index into actual attributes. */
3685 if (spell_hlf != HLF_COUNT)
3686 spell_attr = highlight_attr[spell_hlf];
3687 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003688 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003689
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003690# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003691 /* Need to restart syntax highlighting for this line. */
3692 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003693 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003694# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003695 }
3696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698
3699 /*
3700 * Correct highlighting for cursor that can't be disabled.
3701 * Avoids having to check this for each character.
3702 */
3703 if (fromcol >= 0)
3704 {
3705 if (noinvcur)
3706 {
3707 if ((colnr_T)fromcol == wp->w_virtcol)
3708 {
3709 /* highlighting starts at cursor, let it start just after the
3710 * cursor */
3711 fromcol_prev = fromcol;
3712 fromcol = -1;
3713 }
3714 else if ((colnr_T)fromcol < wp->w_virtcol)
3715 /* restart highlighting after the cursor */
3716 fromcol_prev = wp->w_virtcol;
3717 }
3718 if (fromcol >= tocol)
3719 fromcol = -1;
3720 }
3721
3722#ifdef FEAT_SEARCH_EXTRA
3723 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003724 * Handle highlighting the last used search pattern and matches.
3725 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003726 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003728 cur = wp->w_match_head;
3729 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003730 while ((cur != NULL || shl_flag == FALSE) && !number_only
3731 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003733 if (shl_flag == FALSE)
3734 {
3735 shl = &search_hl;
3736 shl_flag = TRUE;
3737 }
3738 else
3739 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003740 shl->startcol = MAXCOL;
3741 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003743 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003744 v = (long)(ptr - line);
3745 if (cur != NULL)
3746 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003747 next_search_hl(wp, shl, lnum, (colnr_T)v,
3748 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003749
3750 /* Need to get the line again, a multi-line regexp may have made it
3751 * invalid. */
3752 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3753 ptr = line + v;
3754
3755 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003757 if (shl->lnum == lnum)
3758 shl->startcol = shl->rm.startpos[0].col;
3759 else
3760 shl->startcol = 0;
3761 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3762 - shl->rm.startpos[0].lnum)
3763 shl->endcol = shl->rm.endpos[0].col;
3764 else
3765 shl->endcol = MAXCOL;
3766 /* Highlight one character for an empty match. */
3767 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003769 if (has_mbyte && line[shl->endcol] != NUL)
3770 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3771 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003772 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003774 if ((long)shl->startcol < v) /* match at leftcol */
3775 {
3776 shl->attr_cur = shl->attr;
3777 search_attr = shl->attr;
3778 }
3779 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003781 if (shl != &search_hl && cur != NULL)
3782 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784#endif
3785
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003786#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003787 // Cursor line highlighting for 'cursorline' in the current window.
3788 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003789 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003790 // Do not show the cursor line when Visual mode is active, because it's
3791 // not clear what is selected then. Do update w_last_cursorline.
3792 if (!(wp == curwin && VIsual_active))
3793 {
3794 line_attr = HL_ATTR(HLF_CUL);
3795 area_highlighting = TRUE;
3796 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003797 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003798 }
3799#endif
3800
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003801#ifdef FEAT_TEXT_PROP
3802 {
3803 char_u *prop_start;
3804
3805 text_prop_count = get_text_props(wp->w_buffer, lnum,
3806 &prop_start, FALSE);
3807 if (text_prop_count > 0)
3808 {
3809 // Make a copy of the properties, so that they are properly
3810 // aligned.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003811 text_props = ALLOC_MULT(textprop_T, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003812 if (text_props != NULL)
3813 mch_memmove(text_props, prop_start,
3814 text_prop_count * sizeof(textprop_T));
3815
3816 // Allocate an array for the indexes.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003817 text_prop_idxs = ALLOC_MULT(int, text_prop_count);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003818 area_highlighting = TRUE;
3819 extra_check = TRUE;
3820 }
3821 }
3822#endif
3823
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003824 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827#ifdef FEAT_RIGHTLEFT
3828 if (wp->w_p_rl)
3829 {
3830 /* Rightleft window: process the text in the normal direction, but put
3831 * it in current_ScreenLine[] from right to left. Start at the
3832 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003833 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003835 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837#endif
3838
3839 /*
3840 * Repeat for the whole displayed line.
3841 */
3842 for (;;)
3843 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003844#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003845 int has_match_conc = 0; // match wants to conceal
3846 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 /* Skip this quickly when working on the text. */
3849 if (draw_state != WL_LINE)
3850 {
3851#ifdef FEAT_CMDWIN
3852 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3853 {
3854 draw_state = WL_CMDLINE;
3855 if (cmdwin_type != 0 && wp == curwin)
3856 {
3857 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003859 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003860 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003861 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 }
3864#endif
3865
3866#ifdef FEAT_FOLDING
3867 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3868 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003869 int fdc = compute_foldcolumn(wp, 0);
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003872 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003874 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003875 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003876 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003877 p_extra_free = alloc(12 + 1);
3878
3879 if (p_extra_free != NULL)
3880 {
3881 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3882 n_extra = fdc;
3883 p_extra_free[n_extra] = NUL;
3884 p_extra = p_extra_free;
3885 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003886 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003887 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
3890 }
3891#endif
3892
3893#ifdef FEAT_SIGNS
3894 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3895 {
3896 draw_state = WL_SIGN;
3897 /* Show the sign column when there are any signs in this
3898 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003899 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003901 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003903 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904# endif
3905
3906 /* Draw two cells with the sign value or blank. */
3907 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003908 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003909 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 n_extra = 2;
3911
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003912 if (row == startrow
3913#ifdef FEAT_DIFF
3914 + filler_lines && filler_todo <= 0
3915#endif
3916 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
3918 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3919 SIGN_TEXT);
3920# ifdef FEAT_SIGN_ICONS
3921 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3922 SIGN_ICON);
3923 if (gui.in_use && icon_sign != 0)
3924 {
3925 /* Use the image in this position. */
3926 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003927 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928# ifdef FEAT_NETBEANS_INTG
3929 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003932 c_final = NUL;
3933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934# endif
3935 char_attr = icon_sign;
3936 }
3937 else
3938# endif
3939 if (text_sign != 0)
3940 {
3941 p_extra = sign_get_text(text_sign);
3942 if (p_extra != NULL)
3943 {
3944 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003945 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 char_attr = sign_get_attr(text_sign, FALSE);
3949 }
3950 }
3951 }
3952 }
3953#endif
3954
3955 if (draw_state == WL_NR - 1 && n_extra == 0)
3956 {
3957 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003958 /* Display the absolute or relative line number. After the
3959 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3960 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 && (row == startrow
3962#ifdef FEAT_DIFF
3963 + filler_lines
3964#endif
3965 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3966 {
3967 /* Draw the line number (empty space after wrapping). */
3968 if (row == startrow
3969#ifdef FEAT_DIFF
3970 + filler_lines
3971#endif
3972 )
3973 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003974 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003975 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003976
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003977 if (wp->w_p_nu && !wp->w_p_rnu)
3978 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003979 num = (long)lnum;
3980 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003981 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003982 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003983 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003984 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003985 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003986 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003987 num = lnum;
3988 fmt = "%-*ld ";
3989 }
3990 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003991
Bram Moolenaar700e7342013-01-30 12:31:36 +01003992 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003993 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (wp->w_skipcol > 0)
3995 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3996 *p_extra = '-';
3997#ifdef FEAT_RIGHTLEFT
3998 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01003999 {
4000 char_u *p1, *p2;
4001 int t;
4002
4003 // like rl_mirror(), but keep the space at the end
4004 p2 = skiptowhite(extra) - 1;
4005 for (p1 = extra; p1 < p2; ++p1, --p2)
4006 {
4007 t = *p1;
4008 *p1 = *p2;
4009 *p2 = t;
4010 }
4011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012#endif
4013 p_extra = extra;
4014 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004015 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004020 c_final = NUL;
4021 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004022 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004023 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004024#ifdef FEAT_SYN_HL
4025 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004026 * the current line differently.
4027 * TODO: Can we use CursorLine instead of CursorLineNr
4028 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004029 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004030 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004031 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 }
4035
Bram Moolenaar597a4222014-06-25 14:39:50 +02004036#ifdef FEAT_LINEBREAK
4037 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4038 && n_extra == 0 && *p_sbr != NUL)
4039 /* draw indent after showbreak value */
4040 draw_state = WL_BRI;
4041 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4042 /* After the showbreak, draw the breakindent */
4043 draw_state = WL_BRI - 1;
4044
4045 /* draw 'breakindent': indent wrapped text accordingly */
4046 if (draw_state == WL_BRI - 1 && n_extra == 0)
4047 {
4048 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004049 /* if need_showbreak is set, breakindent also applies */
4050 if (wp->w_p_bri && n_extra == 0
4051 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004052# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004053 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004054# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004055 )
4056 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004057 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004058# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004059 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004060 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004061 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004062# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004063 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4064 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004065 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004066# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004067 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004068# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004069 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004070 c_extra = ' ';
4071 n_extra = get_breakindent_win(wp,
4072 ml_get_buf(wp->w_buffer, lnum, FALSE));
4073 /* Correct end of highlighted area for 'breakindent',
4074 * required when 'linebreak' is also set. */
4075 if (tocol == vcol)
4076 tocol += n_extra;
4077 }
4078 }
4079#endif
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4082 if (draw_state == WL_SBR - 1 && n_extra == 0)
4083 {
4084 draw_state = WL_SBR;
4085# ifdef FEAT_DIFF
4086 if (filler_todo > 0)
4087 {
4088 /* Draw "deleted" diff line(s). */
4089 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004092 c_final = NUL;
4093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004097 c_final = NUL;
4098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099# ifdef FEAT_RIGHTLEFT
4100 if (wp->w_p_rl)
4101 n_extra = col + 1;
4102 else
4103# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004104 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004105 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107# endif
4108# ifdef FEAT_LINEBREAK
4109 if (*p_sbr != NUL && need_showbreak)
4110 {
4111 /* Draw 'showbreak' at the start of each broken line. */
4112 p_extra = p_sbr;
4113 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004114 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004116 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004118 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 /* Correct end of highlighted area for 'showbreak',
4120 * required when 'linebreak' is also set. */
4121 if (tocol == vcol)
4122 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004123#ifdef FEAT_SYN_HL
4124 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004125 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004126 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004127 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130# endif
4131 }
4132#endif
4133
4134 if (draw_state == WL_LINE - 1 && n_extra == 0)
4135 {
4136 draw_state = WL_LINE;
4137 if (saved_n_extra)
4138 {
4139 /* Continue item from end of wrapped line. */
4140 n_extra = saved_n_extra;
4141 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004142 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 p_extra = saved_p_extra;
4144 char_attr = saved_char_attr;
4145 }
4146 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004147 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149 }
4150
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004151 // When still displaying '$' of change command, stop at cursor.
4152 // When only displaying the (relative) line number and that's done,
4153 // stop here.
4154 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004155 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#ifdef FEAT_DIFF
4157 && filler_todo <= 0
4158#endif
4159 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004160 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004162 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004163 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004164 /* Pretend we have finished updating the window. Except when
4165 * 'cursorcolumn' is set. */
4166#ifdef FEAT_SYN_HL
4167 if (wp->w_p_cuc)
4168 row = wp->w_cline_row + wp->w_cline_height;
4169 else
4170#endif
4171 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 break;
4173 }
4174
Bram Moolenaar637532b2019-01-03 21:44:40 +01004175 if (draw_state == WL_LINE && (area_highlighting
4176#ifdef FEAT_SPELL
4177 || has_spell
4178#endif
4179 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 /* handle Visual or match highlighting in this line */
4182 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4184 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004186 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004188 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 else if (area_attr != 0
4190 && (vcol == tocol
4191 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
4194#ifdef FEAT_SEARCH_EXTRA
4195 if (!n_extra)
4196 {
4197 /*
4198 * Check for start/end of search pattern match.
4199 * After end, check for start/end of next match.
4200 * When another match, have to check for start again.
4201 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004202 * Do this for 'search_hl' and the match list (ordered by
4203 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004205 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004206 cur = wp->w_match_head;
4207 shl_flag = FALSE;
4208 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004210 if (shl_flag == FALSE
4211 && ((cur != NULL
4212 && cur->priority > SEARCH_HL_PRIORITY)
4213 || cur == NULL))
4214 {
4215 shl = &search_hl;
4216 shl_flag = TRUE;
4217 }
4218 else
4219 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004220 if (cur != NULL)
4221 cur->pos.cur = 0;
4222 pos_inprogress = TRUE;
4223 while (shl->rm.regprog != NULL
4224 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004226 if (shl->startcol != MAXCOL
4227 && v >= (long)shl->startcol
4228 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004230 int tmp_col = v + MB_PTR2LEN(ptr);
4231
4232 if (shl->endcol < tmp_col)
4233 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004235#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004236 // Match with the "Conceal" group results in hiding
4237 // the match.
4238 if (cur != NULL
4239 && shl != &search_hl
4240 && syn_name2id((char_u *)"Conceal")
4241 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004242 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004243 has_match_conc =
4244 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004245 match_conc = cur->conceal_char;
4246 }
4247 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004248 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004251 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004254 next_search_hl(wp, shl, lnum, (colnr_T)v,
4255 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004256 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004257 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 /* Need to get the line again, a multi-line regexp
4260 * may have made it invalid. */
4261 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4262 ptr = line + v;
4263
4264 if (shl->lnum == lnum)
4265 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004266 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004268 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004270 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004272 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 /* highlight empty match, try again after
4275 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004277 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004278 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004280 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 /* Loop to check if the match starts at the
4284 * current position */
4285 continue;
4286 }
4287 }
4288 break;
4289 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004290 if (shl != &search_hl && cur != NULL)
4291 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004293
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004294 /* Use attributes from match with highest priority among
4295 * 'search_hl' and the match list. */
4296 search_attr = search_hl.attr_cur;
4297 cur = wp->w_match_head;
4298 shl_flag = FALSE;
4299 while (cur != NULL || shl_flag == FALSE)
4300 {
4301 if (shl_flag == FALSE
4302 && ((cur != NULL
4303 && cur->priority > SEARCH_HL_PRIORITY)
4304 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004305 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004306 shl = &search_hl;
4307 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004308 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004309 else
4310 shl = &cur->hl;
4311 if (shl->attr_cur != 0)
4312 search_attr = shl->attr_cur;
4313 if (shl != &search_hl && cur != NULL)
4314 cur = cur->next;
4315 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004316 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004317 if (*ptr == NUL && (did_line_attr >= 1
4318 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004319 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321#endif
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004324 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004326 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4327 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004329 if (diff_hlf == HLF_TXD && ptr - line > change_end
4330 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004332 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004333 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004334 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004337
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004338#ifdef FEAT_TEXT_PROP
4339 if (text_props != NULL)
4340 {
4341 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004342 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004343
4344 // Check if any active property ends.
4345 for (pi = 0; pi < text_props_active; ++pi)
4346 {
4347 int tpi = text_prop_idxs[pi];
4348
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004349 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004350 + text_props[tpi].tp_len)
4351 {
4352 if (pi + 1 < text_props_active)
4353 mch_memmove(text_prop_idxs + pi,
4354 text_prop_idxs + pi + 1,
4355 sizeof(int)
4356 * (text_props_active - (pi + 1)));
4357 --text_props_active;
4358 --pi;
4359 }
4360 }
4361
4362 // Add any text property that starts in this column.
4363 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004364 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004365 text_prop_idxs[text_props_active++] = text_prop_next++;
4366
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004367 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004368 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004369 if (text_props_active > 0)
4370 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004371 // Sort the properties on priority and/or starting last.
4372 // Then combine the attributes, highest priority last.
4373 current_text_props = text_props;
4374 current_buf = wp->w_buffer;
4375 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4376 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004377
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004378 for (pi = 0; pi < text_props_active; ++pi)
4379 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004380 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004381 proptype_T *pt = text_prop_type_by_id(
4382 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004383
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004384 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004385 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004386 int pt_attr = syn_id2attr(pt->pt_hl_id);
4387
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004388 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004389 text_prop_attr =
4390 hl_combine_attr(text_prop_attr, pt_attr);
4391 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004392 }
4393 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004394 }
4395 }
4396#endif
4397
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004398 /* Decide which of the highlight attributes to use. */
4399 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004400#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004401 if (area_attr != 0)
4402 char_attr = hl_combine_attr(line_attr, area_attr);
4403 else if (search_attr != 0)
4404 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004405# ifdef FEAT_TEXT_PROP
4406 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004407 {
4408 char_attr = hl_combine_attr(
4409 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4410 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004411# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004412 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004413 || vcol < fromcol || vcol_prev < fromcol_prev
4414 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004415 // Use line_attr when not in the Visual or 'incsearch' area
4416 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004417 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004418#else
4419 if (area_attr != 0)
4420 char_attr = area_attr;
4421 else if (search_attr != 0)
4422 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004423#endif
4424 else
4425 {
4426 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004427#ifdef FEAT_TEXT_PROP
4428 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004429 {
4430 if (text_prop_combine)
4431 char_attr = hl_combine_attr(
4432 syntax_attr, text_prop_attr);
4433 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004434 char_attr = hl_combine_attr(
4435 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004436 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004437 else
4438#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004439#ifdef FEAT_SYN_HL
4440 if (has_syntax)
4441 char_attr = syntax_attr;
4442 else
4443#endif
4444 char_attr = 0;
4445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004447 if (char_attr == 0)
4448 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 /*
4451 * Get the next character to put on the screen.
4452 */
4453 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004454 * The "p_extra" points to the extra stuff that is inserted to
4455 * represent special characters (non-printable stuff) and other
4456 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004457 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004458 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4459 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4461 */
4462 if (n_extra > 0)
4463 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004464 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004466 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004468 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
4470 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004472 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 else
4475 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else
4478 {
4479 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (has_mbyte)
4481 {
4482 mb_c = c;
4483 if (enc_utf8)
4484 {
4485 /* If the UTF-8 character is more than one byte:
4486 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004487 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 mb_utf8 = FALSE;
4489 if (mb_l > n_extra)
4490 mb_l = 1;
4491 else if (mb_l > 1)
4492 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004493 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004495 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 }
4498 else
4499 {
4500 /* if this is a DBCS character, put it in "mb_c" */
4501 mb_l = MB_BYTE2LEN(c);
4502 if (mb_l >= n_extra)
4503 mb_l = 1;
4504 else if (mb_l > 1)
4505 mb_c = (c << 8) + p_extra[1];
4506 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004507 if (mb_l == 0) /* at the NUL at end-of-line */
4508 mb_l = 1;
4509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 /* If a double-width char doesn't fit display a '>' in the
4511 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004512 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513# ifdef FEAT_RIGHTLEFT
4514 wp->w_p_rl ? (col <= 0) :
4515# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004516 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 && (*mb_char2cells)(mb_c) == 2)
4518 {
4519 c = '>';
4520 mb_c = c;
4521 mb_l = 1;
4522 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004523 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 /* put the pointer back to output the double-width
4525 * character at the start of the next line. */
4526 ++n_extra;
4527 --p_extra;
4528 }
4529 else
4530 {
4531 n_extra -= mb_l - 1;
4532 p_extra += mb_l - 1;
4533 }
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 ++p_extra;
4536 }
4537 --n_extra;
4538 }
4539 else
4540 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004541#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004542 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004543#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004544
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004545 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004546 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 /*
4548 * Get a character from the line itself.
4549 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004550 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004551#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004552 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (has_mbyte)
4555 {
4556 mb_c = c;
4557 if (enc_utf8)
4558 {
4559 /* If the UTF-8 character is more than one byte: Decode it
4560 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004561 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 mb_utf8 = FALSE;
4563 if (mb_l > 1)
4564 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004565 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 /* Overlong encoded ASCII or ASCII with composing char
4567 * is displayed normally, except a NUL. */
4568 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004569 {
4570 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004571#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004572 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004573#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004576
4577 /* At start of the line we can have a composing char.
4578 * Draw it as a space with a composing char. */
4579 if (utf_iscomposing(mb_c))
4580 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004581 int i;
4582
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004583 for (i = Screen_mco - 1; i > 0; --i)
4584 u8cc[i] = u8cc[i - 1];
4585 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004586 mb_c = ' ';
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589
4590 if ((mb_l == 1 && c >= 0x80)
4591 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004592 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 /*
4595 * Illegal UTF-8 byte: display as <xx>.
4596 * Non-BMP character : display as ? or fullwidth ?.
4597 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004598 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004599# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004600 if (wp->w_p_rl) /* reverse */
4601 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004602# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 p_extra = extra;
4604 c = *p_extra;
4605 mb_c = mb_ptr2char_adv(&p_extra);
4606 mb_utf8 = (c >= 0x80);
4607 n_extra = (int)STRLEN(p_extra);
4608 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004609 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 if (area_attr == 0 && search_attr == 0)
4611 {
4612 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004613 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 saved_attr2 = char_attr; /* save current attr */
4615 }
4616 }
4617 else if (mb_l == 0) /* at the NUL at end-of-line */
4618 mb_l = 1;
4619#ifdef FEAT_ARABIC
4620 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4621 {
4622 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004623 int pc, pc1, nc;
4624 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626 /* The idea of what is the previous and next
4627 * character depends on 'rightleft'. */
4628 if (wp->w_p_rl)
4629 {
4630 pc = prev_c;
4631 pc1 = prev_c1;
4632 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004633 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 else
4636 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004637 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004639 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 prev_c = mb_c;
4642
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004643 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 else
4646 prev_c = mb_c;
4647#endif
4648 }
4649 else /* enc_dbcs */
4650 {
4651 mb_l = MB_BYTE2LEN(c);
4652 if (mb_l == 0) /* at the NUL at end-of-line */
4653 mb_l = 1;
4654 else if (mb_l > 1)
4655 {
4656 /* We assume a second byte below 32 is illegal.
4657 * Hopefully this is OK for all double-byte encodings!
4658 */
4659 if (ptr[1] >= 32)
4660 mb_c = (c << 8) + ptr[1];
4661 else
4662 {
4663 if (ptr[1] == NUL)
4664 {
4665 /* head byte at end of line */
4666 mb_l = 1;
4667 transchar_nonprint(extra, c);
4668 }
4669 else
4670 {
4671 /* illegal tail byte */
4672 mb_l = 2;
4673 STRCPY(extra, "XX");
4674 }
4675 p_extra = extra;
4676 n_extra = (int)STRLEN(extra) - 1;
4677 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004678 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 c = *p_extra++;
4680 if (area_attr == 0 && search_attr == 0)
4681 {
4682 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004683 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 saved_attr2 = char_attr; /* save current attr */
4685 }
4686 mb_c = c;
4687 }
4688 }
4689 }
4690 /* If a double-width char doesn't fit display a '>' in the
4691 * last column; the character is displayed at the start of the
4692 * next line. */
4693 if ((
4694# ifdef FEAT_RIGHTLEFT
4695 wp->w_p_rl ? (col <= 0) :
4696# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004697 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 && (*mb_char2cells)(mb_c) == 2)
4699 {
4700 c = '>';
4701 mb_c = c;
4702 mb_utf8 = FALSE;
4703 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004704 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004705 // Put pointer back so that the character will be
4706 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004708#ifdef FEAT_CONCEAL
4709 did_decrement_ptr = TRUE;
4710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712 else if (*ptr != NUL)
4713 ptr += mb_l - 1;
4714
4715 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004716 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004717 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004718 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004721 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004722 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 c = ' ';
4724 if (area_attr == 0 && search_attr == 0)
4725 {
4726 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004727 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 saved_attr2 = char_attr; /* save current attr */
4729 }
4730 mb_c = c;
4731 mb_utf8 = FALSE;
4732 mb_l = 1;
4733 }
4734
4735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 ++ptr;
4737
4738 if (extra_check)
4739 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004740#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004741 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004742#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004743
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004744#ifdef FEAT_TERMINAL
4745 if (get_term_attr)
4746 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004747 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004748
4749 if (!attr_pri)
4750 char_attr = syntax_attr;
4751 else
4752 char_attr = hl_combine_attr(syntax_attr, char_attr);
4753 }
4754#endif
4755
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004756#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004757 // Get syntax attribute, unless still at the start of the line
4758 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004759 v = (long)(ptr - line);
4760 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 /* Get the syntax attribute for the character. If there
4763 * is an error, disable syntax highlighting. */
4764 save_did_emsg = did_emsg;
4765 did_emsg = FALSE;
4766
Bram Moolenaar217ad922005-03-20 22:37:15 +00004767 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004768# ifdef FEAT_SPELL
4769 has_spell ? &can_spell :
4770# endif
4771 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004774 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004775 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004776 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004777 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004781
4782 // combine syntax attribute with 'wincolor'
4783 if (win_attr != 0)
4784 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4785
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004786#ifdef SYN_TIME_LIMIT
4787 if (wp->w_s->b_syn_slow)
4788 has_syntax = FALSE;
4789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 /* Need to get the line again, a multi-line regexp may
4792 * have made it invalid. */
4793 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4794 ptr = line + v;
4795
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004796# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004797 // Text properties overrule syntax highlighting or combine.
4798 if (text_prop_attr == 0 || text_prop_combine)
4799# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004800 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004801 int comb_attr = syntax_attr;
4802# ifdef FEAT_TEXT_PROP
4803 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4804# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004805 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004806 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004807 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004808 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004809 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004810# ifdef FEAT_CONCEAL
4811 /* no concealing past the end of the line, it interferes
4812 * with line highlighting */
4813 if (c == NUL)
4814 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004815 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004816 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004819#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004820
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004821#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004822 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004823 * Only do this when there is no syntax highlighting, the
4824 * @Spell cluster is not used or the current syntax item
4825 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004826 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004827 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004828 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004829 if (c != 0 && (
4830# ifdef FEAT_SYN_HL
4831 !has_syntax ||
4832# endif
4833 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004834 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004835 char_u *prev_ptr, *p;
4836 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004837 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004838 if (has_mbyte)
4839 {
4840 prev_ptr = ptr - mb_l;
4841 v -= mb_l - 1;
4842 }
4843 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004844 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004845
4846 /* Use nextline[] if possible, it has the start of the
4847 * next line concatenated. */
4848 if ((prev_ptr - line) - nextlinecol >= 0)
4849 p = nextline + (prev_ptr - line) - nextlinecol;
4850 else
4851 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004852 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004853 len = spell_check(wp, p, &spell_hlf, &cap_col,
4854 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004855 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004856
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004857 /* In Insert mode only highlight a word that
4858 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004859 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004860 && (State & INSERT) != 0
4861 && wp->w_cursor.lnum == lnum
4862 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004863 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004864 && wp->w_cursor.col < (colnr_T)word_end)
4865 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004866 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004867 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004868 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004869
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004870 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004871 && (p - nextline) + len > nextline_idx)
4872 {
4873 /* Remember that the good word continues at the
4874 * start of the next line. */
4875 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004876 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004877 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004878
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004879 /* Turn index into actual attributes. */
4880 if (spell_hlf != HLF_COUNT)
4881 spell_attr = highlight_attr[spell_hlf];
4882
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004883 if (cap_col > 0)
4884 {
4885 if (p != prev_ptr
4886 && (p - nextline) + cap_col >= nextline_idx)
4887 {
4888 /* Remember that the word in the next line
4889 * must start with a capital. */
4890 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004891 cap_col = (int)((p - nextline) + cap_col
4892 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004893 }
4894 else
4895 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004896 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004897 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004898 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004899 }
4900 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004901 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004902 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004903 char_attr = hl_combine_attr(char_attr, spell_attr);
4904 else
4905 char_attr = hl_combine_attr(spell_attr, char_attr);
4906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#endif
4908#ifdef FEAT_LINEBREAK
4909 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004910 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004912 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004913 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004915 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004916 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004917
Bram Moolenaar597a4222014-06-25 14:39:50 +02004918 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004919 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004920 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004921 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004922# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004923 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004924 wp->w_buffer->b_p_vts_array) - 1;
4925# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004926 n_extra = (int)wp->w_buffer->b_p_ts
4927 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004928# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004929
Bram Moolenaar4df70292015-03-21 14:20:16 +01004930 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004931 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004932 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004933 {
4934#ifdef FEAT_CONCEAL
4935 if (c == TAB)
4936 /* See "Tab alignment" below. */
4937 FIX_FOR_BOGUSCOLS;
4938#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004939 if (!wp->w_p_list)
4940 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943#endif
4944
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004945 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4946 // But not when the character is followed by a composing
4947 // character (use mb_l to check that).
4948 if (wp->w_p_list
4949 && ((((c == 160 && mb_l == 1)
4950 || (mb_utf8
4951 && ((mb_c == 160 && mb_l == 2)
4952 || (mb_c == 0x202f && mb_l == 3))))
4953 && lcs_nbsp)
4954 || (c == ' '
4955 && mb_l == 1
4956 && lcs_space
4957 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004958 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004959 c = (c == ' ') ? lcs_space : lcs_nbsp;
4960 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004961 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004962 n_attr = 1;
4963 extra_attr = HL_ATTR(HLF_8);
4964 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004965 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004966 mb_c = c;
4967 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004968 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004969 mb_utf8 = TRUE;
4970 u8cc[0] = 0;
4971 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004972 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004973 else
4974 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004975 }
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4978 {
4979 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004980 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004983 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 saved_attr2 = char_attr; /* save current attr */
4985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004987 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004990 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004991 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 else
4994 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 }
4997
4998 /*
4999 * Handling of non-printable characters.
5000 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005001 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
5003 /*
5004 * when getting a character from the file, we may have to
5005 * turn it into something else on the way to putting it
5006 * into "ScreenLines".
5007 */
5008 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5009 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005010 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005011 long vcol_adjusted = vcol; /* removed showbreak length */
5012#ifdef FEAT_LINEBREAK
5013 /* only adjust the tab_len, when at the first column
5014 * after the showbreak value was drawn */
5015 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5016 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005019#ifdef FEAT_VARTABS
5020 tab_len = tabstop_padding(vcol_adjusted,
5021 wp->w_buffer->b_p_ts,
5022 wp->w_buffer->b_p_vts_array) - 1;
5023#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005024 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005025 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5026#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005027
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005028#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005029 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005030#endif
5031 /* tab amount depends on current column */
5032 n_extra = tab_len;
5033#ifdef FEAT_LINEBREAK
5034 else
5035 {
5036 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005037 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005038 int i;
5039 int saved_nextra = n_extra;
5040
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005041#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005042 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005043 /* there are characters to conceal */
5044 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005045 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5046 */
5047 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5048 && n_extra > tab_len)
5049 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005050#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005051
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005052 /* if n_extra > 0, it gives the number of chars, to
5053 * use for a tab, else we need to calculate the width
5054 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005055 len = (tab_len * mb_char2len(lcs_tab2));
5056 if (n_extra > 0)
5057 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005058 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005059 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005060 vim_memset(p, ' ', len);
5061 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005062 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005063 p_extra_free = p;
5064 for (i = 0; i < tab_len; i++)
5065 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005066 if (*p == NUL)
5067 {
5068 tab_len = i;
5069 break;
5070 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005071 mb_char2bytes(lcs_tab2, p);
5072 p += mb_char2len(lcs_tab2);
5073 n_extra += mb_char2len(lcs_tab2)
5074 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005075 }
5076 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005077#ifdef FEAT_CONCEAL
5078 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5079 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005080 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005081 n_extra -= vcol_off;
5082#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005083 }
5084#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005085#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005086 {
5087 int vc_saved = vcol_off;
5088
5089 /* Tab alignment should be identical regardless of
5090 * 'conceallevel' value. So tab compensates of all
5091 * previous concealed characters, and thus resets
5092 * vcol_off and boguscols accumulated so far in the
5093 * line. Note that the tab can be longer than
5094 * 'tabstop' when there are concealed characters. */
5095 FIX_FOR_BOGUSCOLS;
5096
5097 /* Make sure, the highlighting for the tab char will be
5098 * correctly set further below (effectively reverts the
5099 * FIX_FOR_BOGSUCOLS macro */
5100 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005101 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005102 tab_len += vc_saved;
5103 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (wp->w_p_list)
5107 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005108 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005109#ifdef FEAT_LINEBREAK
5110 if (wp->w_p_lbr)
5111 c_extra = NUL; /* using p_extra from above */
5112 else
5113#endif
5114 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005115 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005116 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005117 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005120 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005123 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005124 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 else
5128 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005129 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 c_extra = ' ';
5131 c = ' ';
5132 }
5133 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005134 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005135 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005136 || ((fromcol >= 0 || fromcol_prev >= 0)
5137 && tocol > vcol
5138 && VIsual_mode != Ctrl_V
5139 && (
5140# ifdef FEAT_RIGHTLEFT
5141 wp->w_p_rl ? (col >= 0) :
5142# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005143 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005144 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005145 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005146 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005147 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005149 /* Display a '$' after the line or highlight an extra
5150 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5152 /* For a diff line the highlighting continues after the
5153 * "$". */
5154 if (
5155# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005156 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157# ifdef LINE_ATTR
5158 &&
5159# endif
5160# endif
5161# ifdef LINE_ATTR
5162 line_attr == 0
5163# endif
5164 )
5165#endif
5166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 /* In virtualedit, visual selections may extend
5168 * beyond end of line. */
5169 if (area_highlighting && virtual_active()
5170 && tocol != MAXCOL && vcol < tocol)
5171 n_extra = 0;
5172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 p_extra = at_end_str;
5175 n_extra = 1;
5176 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005177 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005180 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005181 c = lcs_eol;
5182 else
5183 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 lcs_eol_one = -1;
5185 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005186 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005188 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 n_attr = 1;
5190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005192 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
5194 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005195 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005196 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 else
5199 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 else if (c != NUL)
5202 {
5203 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005204 if (n_extra == 0)
5205 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206#ifdef FEAT_RIGHTLEFT
5207 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5208 rl_mirror(p_extra); /* reverse "<12>" */
5209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005211 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005212#ifdef FEAT_LINEBREAK
5213 if (wp->w_p_lbr)
5214 {
5215 char_u *p;
5216
5217 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005218 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005219 vim_memset(p, ' ', n_extra);
5220 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5221 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005222 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005223 p_extra_free = p_extra = p;
5224 }
5225 else
5226#endif
5227 {
5228 n_extra = byte2cells(c) - 1;
5229 c = *p_extra++;
5230 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005231 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005234 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 saved_attr2 = char_attr; /* save current attr */
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 else if (VIsual_active
5240 && (VIsual_mode == Ctrl_V
5241 || VIsual_mode == 'v')
5242 && virtual_active()
5243 && tocol != MAXCOL
5244 && vcol < tocol
5245 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005246#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005248#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005249 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 c = ' ';
5252 --ptr; /* put it back at the NUL */
5253 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005254#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 else if ((
5256# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005257 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005259# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005260 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 ) && (
5264# ifdef FEAT_RIGHTLEFT
5265 wp->w_p_rl ? (col >= 0) :
5266# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005267 (col
5268# ifdef FEAT_CONCEAL
5269 - boguscols
5270# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005271 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 {
5273 /* Highlight until the right side of the window */
5274 c = ' ';
5275 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005276
5277 /* Remember we do the char for line highlighting. */
5278 ++did_line_attr;
5279
5280 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005281 if (line_attr != 0 && char_attr == search_attr
5282 && (did_line_attr > 1
5283 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005284 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285# ifdef FEAT_DIFF
5286 if (diff_hlf == HLF_TXD)
5287 {
5288 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005289 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005290 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005291 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005292 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5293 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005294 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
5297# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005298# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005299 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005300 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005301 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005302 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5303 char_attr = hl_combine_attr(char_attr,
5304 HL_ATTR(HLF_CUL));
5305 }
5306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308#endif
5309 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005310
5311#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005312 if ( wp->w_p_cole > 0
5313 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005314 conceal_cursor_line(wp))
5315 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005316 && !(lnum_in_visual_area
5317 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005318 {
5319 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005320 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005321 && (syn_get_sub_char() != NUL || match_conc
5322 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005323 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005324 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005325 /* First time at this concealed item: display one
5326 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005327 if (match_conc)
5328 c = match_conc;
5329 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005330 c = syn_get_sub_char();
5331 else if (lcs_conceal != NUL)
5332 c = lcs_conceal;
5333 else
5334 c = ' ';
5335
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005336 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005337
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005338 if (n_extra > 0)
5339 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005340 vcol += n_extra;
5341 if (wp->w_p_wrap && n_extra > 0)
5342 {
5343# ifdef FEAT_RIGHTLEFT
5344 if (wp->w_p_rl)
5345 {
5346 col -= n_extra;
5347 boguscols -= n_extra;
5348 }
5349 else
5350# endif
5351 {
5352 boguscols += n_extra;
5353 col += n_extra;
5354 }
5355 }
5356 n_extra = 0;
5357 n_attr = 0;
5358 }
5359 else if (n_skip == 0)
5360 {
5361 is_concealing = TRUE;
5362 n_skip = 1;
5363 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005364 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005365 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005366 {
5367 mb_utf8 = TRUE;
5368 u8cc[0] = 0;
5369 c = 0xc0;
5370 }
5371 else
5372 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005373 }
5374 else
5375 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005376 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005377 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005378 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005379
5380 if (n_skip > 0 && did_decrement_ptr)
5381 // not showing the '>', put pointer back to avoid getting stuck
5382 ++ptr;
5383
5384#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
5386
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005387#ifdef FEAT_CONCEAL
5388 /* In the cursor line and we may be concealing characters: correct
5389 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005390 if (!did_wcol && draw_state == WL_LINE
5391 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005392 && conceal_cursor_line(wp)
5393 && (int)wp->w_virtcol <= vcol + n_skip)
5394 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005395# ifdef FEAT_RIGHTLEFT
5396 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005397 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005398 else
5399# endif
5400 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005401 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005402 did_wcol = TRUE;
5403 }
5404#endif
5405
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 /* Don't override visual selection highlighting. */
5407 if (n_attr > 0
5408 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005409 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 char_attr = extra_attr;
5411
Bram Moolenaar81695252004-12-29 20:58:21 +00005412#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 /* XIM don't send preedit_start and preedit_end, but they send
5414 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5415 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005416 if (p_imst == IM_ON_THE_SPOT
5417 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005418 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 && (State & INSERT)
5420 && !p_imdisable
5421 && im_is_preediting()
5422 && draw_state == WL_LINE)
5423 {
5424 colnr_T tcol;
5425
5426 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005427 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 else
5429 tcol = preedit_end_col;
5430 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5431 {
5432 if (feedback_old_attr < 0)
5433 {
5434 feedback_col = 0;
5435 feedback_old_attr = char_attr;
5436 }
5437 char_attr = im_get_feedback_attr(feedback_col);
5438 if (char_attr < 0)
5439 char_attr = feedback_old_attr;
5440 feedback_col++;
5441 }
5442 else if (feedback_old_attr >= 0)
5443 {
5444 char_attr = feedback_old_attr;
5445 feedback_old_attr = -1;
5446 feedback_col = 0;
5447 }
5448 }
5449#endif
5450 /*
5451 * Handle the case where we are in column 0 but not on the first
5452 * character of the line and the user wants us to show us a
5453 * special character (via 'listchars' option "precedes:<char>".
5454 */
5455 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005456 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5458#ifdef FEAT_DIFF
5459 && filler_todo <= 0
5460#endif
5461 && draw_state > WL_NR
5462 && c != NUL)
5463 {
5464 c = lcs_prec;
5465 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005466 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5467 {
5468 /* Double-width character being overwritten by the "precedes"
5469 * character, need to fill up half the character. */
5470 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005471 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005472 n_extra = 1;
5473 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005474 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005477 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 {
5479 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005480 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005481 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483 else
5484 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005485 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005488 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 n_attr3 = 1;
5490 }
5491 }
5492
5493 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005494 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005496 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005497#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005498 || did_line_attr == 1
5499#endif
5500 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005502#ifdef FEAT_SEARCH_EXTRA
5503 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005504
5505 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005506 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005507 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005508#endif
5509
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005510 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 * highlight match at end of line. If it's beyond the last
5512 * char on the screen, just overwrite that one (tricky!) Not
5513 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005514#ifdef FEAT_SEARCH_EXTRA
5515 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005516 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005517 prevcol_hl_flag = TRUE;
5518 else
5519 {
5520 cur = wp->w_match_head;
5521 while (cur != NULL)
5522 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005523 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005524 {
5525 prevcol_hl_flag = TRUE;
5526 break;
5527 }
5528 cur = cur->next;
5529 }
5530 }
5531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005533 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005534 && (VIsual_mode != Ctrl_V
5535 || lnum == VIsual.lnum
5536 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005537 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#ifdef FEAT_SEARCH_EXTRA
5539 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005540 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005541# ifdef FEAT_SYN_HL
5542 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5543 && !(wp == curwin && VIsual_active))
5544# endif
5545# ifdef FEAT_DIFF
5546 && diff_hlf == (hlf_T)0
5547# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005548# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005549 && did_line_attr <= 1
5550# endif
5551 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552#endif
5553 ))
5554 {
5555 int n = 0;
5556
5557#ifdef FEAT_RIGHTLEFT
5558 if (wp->w_p_rl)
5559 {
5560 if (col < 0)
5561 n = 1;
5562 }
5563 else
5564#endif
5565 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005566 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 n = -1;
5568 }
5569 if (n != 0)
5570 {
5571 /* At the window boundary, highlight the last character
5572 * instead (better than nothing). */
5573 off += n;
5574 col += n;
5575 }
5576 else
5577 {
5578 /* Add a blank character to highlight. */
5579 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 if (enc_utf8)
5581 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583#ifdef FEAT_SEARCH_EXTRA
5584 if (area_attr == 0)
5585 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005586 /* Use attributes from match with highest priority among
5587 * 'search_hl' and the match list. */
5588 char_attr = search_hl.attr;
5589 cur = wp->w_match_head;
5590 shl_flag = FALSE;
5591 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005592 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005593 if (shl_flag == FALSE
5594 && ((cur != NULL
5595 && cur->priority > SEARCH_HL_PRIORITY)
5596 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005597 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005598 shl = &search_hl;
5599 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005600 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005601 else
5602 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005603 if ((ptr - line) - 1 == (long)shl->startcol
5604 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005605 char_attr = shl->attr;
5606 if (shl != &search_hl && cur != NULL)
5607 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
5610#endif
5611 ScreenAttrs[off] = char_attr;
5612#ifdef FEAT_RIGHTLEFT
5613 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005616 --off;
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 else
5619#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005620 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005622 ++off;
5623 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005624 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005625#ifdef FEAT_SYN_HL
5626 eol_hl_off = 1;
5627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaar91170f82006-05-05 21:15:17 +00005631 /*
5632 * At end of the text line.
5633 */
5634 if (c == NUL)
5635 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005636#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005637 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005638 if (wp->w_p_wrap)
5639 v = wp->w_skipcol;
5640 else
5641 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005642
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005643 /* check if line ends before left margin */
5644 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005645 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005646#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005647 // Get rid of the boguscols now, we want to draw until the right
5648 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005649 col -= boguscols;
5650 boguscols = 0;
5651#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005652
5653 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005654 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005655
5656 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005657 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5658 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005659 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005660 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005661 || draw_color_col
5662 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005663# ifdef FEAT_RIGHTLEFT
5664 && !wp->w_p_rl
5665# endif
5666 )
5667 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005668 int rightmost_vcol = 0;
5669 int i;
5670
5671 if (wp->w_p_cuc)
5672 rightmost_vcol = wp->w_virtcol;
5673 if (draw_color_col)
5674 /* determine rightmost colorcolumn to possibly draw */
5675 for (i = 0; color_cols[i] >= 0; ++i)
5676 if (rightmost_vcol < color_cols[i])
5677 rightmost_vcol = color_cols[i];
5678
Bram Moolenaar02631462017-09-22 15:20:32 +02005679 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005680 {
5681 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005682 if (enc_utf8)
5683 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005684 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005685 if (draw_color_col)
5686 draw_color_col = advance_color_col(VCOL_HLC,
5687 &color_cols);
5688
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005689 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005690 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005691 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005692 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005693 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005694 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005695
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005696 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005697 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005698
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005699 ++vcol;
5700 }
5701 }
5702#endif
5703
Bram Moolenaar53f81742017-09-22 14:35:51 +02005704 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005705 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 row++;
5707
5708 /*
5709 * Update w_cline_height and w_cline_folded if the cursor line was
5710 * updated (saves a call to plines() later).
5711 */
5712 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5713 {
5714 curwin->w_cline_row = startrow;
5715 curwin->w_cline_height = row - startrow;
5716#ifdef FEAT_FOLDING
5717 curwin->w_cline_folded = FALSE;
5718#endif
5719 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5720 }
5721
5722 break;
5723 }
5724
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005725 // Show "extends" character from 'listchars' if beyond the line end and
5726 // 'list' is set.
5727 if (lcs_ext != NUL
5728 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 && !wp->w_p_wrap
5730#ifdef FEAT_DIFF
5731 && filler_todo <= 0
5732#endif
5733 && (
5734#ifdef FEAT_RIGHTLEFT
5735 wp->w_p_rl ? col == 0 :
5736#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005737 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005739 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5741 {
5742 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005743 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005745 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
5747 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005748 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005749 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751 else
5752 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005755#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005756 /* advance to the next 'colorcolumn' */
5757 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005758 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005759
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005760 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005761 * highlight the cursor position itself.
5762 * Also highlight the 'colorcolumn' if it is different than
5763 * 'cursorcolumn' */
5764 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005765 if (draw_state == WL_LINE && !lnum_in_visual_area
5766 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005767 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005768 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005769 && lnum != wp->w_cursor.lnum)
5770 {
5771 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005772 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005773 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005774 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005775 {
5776 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005777 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005778 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005779 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005780#endif
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 /*
5783 * Store character to be displayed.
5784 * Skip characters that are left of the screen for 'nowrap'.
5785 */
5786 vcol_prev = vcol;
5787 if (draw_state < WL_LINE || n_skip <= 0)
5788 {
5789 /*
5790 * Store the character.
5791 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005792#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5794 {
5795 /* A double-wide character is: put first halve in left cell. */
5796 --off;
5797 --col;
5798 }
5799#endif
5800 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005802 {
5803 if ((mb_c & 0xff00) == 0x8e00)
5804 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 else if (enc_utf8)
5808 {
5809 if (mb_utf8)
5810 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005811 int i;
5812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005814 if ((c & 0xff) == 0)
5815 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005816 for (i = 0; i < Screen_mco; ++i)
5817 {
5818 ScreenLinesC[i][off] = u8cc[i];
5819 if (u8cc[i] == 0)
5820 break;
5821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
5823 else
5824 ScreenLinesUC[off] = 0;
5825 }
5826 if (multi_attr)
5827 {
5828 ScreenAttrs[off] = multi_attr;
5829 multi_attr = 0;
5830 }
5831 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 ScreenAttrs[off] = char_attr;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5835 {
5836 /* Need to fill two screen columns. */
5837 ++off;
5838 ++col;
5839 if (enc_utf8)
5840 /* UTF-8: Put a 0 in the second screen char. */
5841 ScreenLines[off] = 0;
5842 else
5843 /* DBCS: Put second byte in the second screen char. */
5844 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005845 if (draw_state > WL_NR
5846#ifdef FEAT_DIFF
5847 && filler_todo <= 0
5848#endif
5849 )
5850 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 /* When "tocol" is halfway a character, set it to the end of
5852 * the character, otherwise highlighting won't stop. */
5853 if (tocol == vcol)
5854 ++tocol;
5855#ifdef FEAT_RIGHTLEFT
5856 if (wp->w_p_rl)
5857 {
5858 /* now it's time to backup one cell */
5859 --off;
5860 --col;
5861 }
5862#endif
5863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#ifdef FEAT_RIGHTLEFT
5865 if (wp->w_p_rl)
5866 {
5867 --off;
5868 --col;
5869 }
5870 else
5871#endif
5872 {
5873 ++off;
5874 ++col;
5875 }
5876 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005877#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005878 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005879 {
5880 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005881 ++vcol_off;
5882 if (n_extra > 0)
5883 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005884 if (wp->w_p_wrap)
5885 {
5886 /*
5887 * Special voodoo required if 'wrap' is on.
5888 *
5889 * Advance the column indicator to force the line
5890 * drawing to wrap early. This will make the line
5891 * take up the same screen space when parts are concealed,
5892 * so that cursor line computations aren't messed up.
5893 *
5894 * To avoid the fictitious advance of 'col' causing
5895 * trailing junk to be written out of the screen line
5896 * we are building, 'boguscols' keeps track of the number
5897 * of bad columns we have advanced.
5898 */
5899 if (n_extra > 0)
5900 {
5901 vcol += n_extra;
5902# ifdef FEAT_RIGHTLEFT
5903 if (wp->w_p_rl)
5904 {
5905 col -= n_extra;
5906 boguscols -= n_extra;
5907 }
5908 else
5909# endif
5910 {
5911 col += n_extra;
5912 boguscols += n_extra;
5913 }
5914 n_extra = 0;
5915 n_attr = 0;
5916 }
5917
5918
Bram Moolenaar860cae12010-06-05 23:22:07 +02005919 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5920 {
5921 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005922# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005923 if (wp->w_p_rl)
5924 {
5925 --boguscols;
5926 --col;
5927 }
5928 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005929# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005930 {
5931 ++boguscols;
5932 ++col;
5933 }
5934 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005935
5936# ifdef FEAT_RIGHTLEFT
5937 if (wp->w_p_rl)
5938 {
5939 --boguscols;
5940 --col;
5941 }
5942 else
5943# endif
5944 {
5945 ++boguscols;
5946 ++col;
5947 }
5948 }
5949 else
5950 {
5951 if (n_extra > 0)
5952 {
5953 vcol += n_extra;
5954 n_extra = 0;
5955 n_attr = 0;
5956 }
5957 }
5958
5959 }
5960#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 else
5962 --n_skip;
5963
Bram Moolenaar64486672010-05-16 15:46:46 +02005964 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5965 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005966 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967#ifdef FEAT_DIFF
5968 && filler_todo <= 0
5969#endif
5970 )
5971 ++vcol;
5972
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005973#ifdef FEAT_SYN_HL
5974 if (vcol_save_attr >= 0)
5975 char_attr = vcol_save_attr;
5976#endif
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 /* restore attributes after "predeces" in 'listchars' */
5979 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5980 char_attr = saved_attr3;
5981
5982 /* restore attributes after last 'listchars' or 'number' char */
5983 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5984 char_attr = saved_attr2;
5985
5986 /*
5987 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005988 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 */
5990 if ((
5991#ifdef FEAT_RIGHTLEFT
5992 wp->w_p_rl ? (col < 0) :
5993#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005994 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 && (*ptr != NUL
5996#ifdef FEAT_DIFF
5997 || filler_todo > 0
5998#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01005999 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6001 )
6002 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006003#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006004 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006005 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006006 boguscols = 0;
6007#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006008 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006009 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 ++row;
6012 ++screen_row;
6013
6014 /* When not wrapping and finished diff lines, or when displayed
6015 * '$' and highlighting until last column, break here. */
6016 if ((!wp->w_p_wrap
6017#ifdef FEAT_DIFF
6018 && filler_todo <= 0
6019#endif
6020 ) || lcs_eol_one == -1)
6021 break;
6022
6023 /* When the window is too narrow draw all "@" lines. */
6024 if (draw_state != WL_LINE
6025#ifdef FEAT_DIFF
6026 && filler_todo <= 0
6027#endif
6028 )
6029 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006030 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 row = endrow;
6033 }
6034
6035 /* When line got too long for screen break here. */
6036 if (row == endrow)
6037 {
6038 ++row;
6039 break;
6040 }
6041
6042 if (screen_cur_row == screen_row - 1
6043#ifdef FEAT_DIFF
6044 && filler_todo <= 0
6045#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006046 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 /* Remember that the line wraps, used for modeless copy. */
6049 LineWraps[screen_row - 1] = TRUE;
6050
6051 /*
6052 * Special trick to make copy/paste of wrapped lines work with
6053 * xterm/screen: write an extra character beyond the end of
6054 * the line. This will work with all terminal types
6055 * (regardless of the xn,am settings).
6056 * Only do this on a fast tty.
6057 * Only do this if the cursor is on the current line
6058 * (something has been written in it).
6059 * Don't do this for the GUI.
6060 * Don't do this for double-width characters.
6061 * Don't do this for a window not at the right screen border.
6062 */
6063 if (p_tf
6064#ifdef FEAT_GUI
6065 && !gui.in_use
6066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006068 && ((*mb_off2cells)(LineOffset[screen_row],
6069 LineOffset[screen_row] + screen_Columns)
6070 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006072 + (int)Columns - 2,
6073 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006074 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
6076 /* First make sure we are at the end of the screen line,
6077 * then output the same character again to let the
6078 * terminal know about the wrap. If the terminal doesn't
6079 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006080 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 screen_char(LineOffset[screen_row - 1]
6082 + (unsigned)Columns - 1,
6083 screen_row - 1, (int)(Columns - 1));
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 /* When there is a multi-byte character, just output a
6086 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006087 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6088 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 out_char(' ');
6090 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 out_char(ScreenLines[LineOffset[screen_row - 1]
6092 + (Columns - 1)]);
6093 /* force a redraw of the first char on the next line */
6094 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6095 screen_start(); /* don't know where cursor is now */
6096 }
6097 }
6098
6099 col = 0;
6100 off = (unsigned)(current_ScreenLine - ScreenLines);
6101#ifdef FEAT_RIGHTLEFT
6102 if (wp->w_p_rl)
6103 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006104 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 off += col;
6106 }
6107#endif
6108
6109 /* reset the drawing state for the start of a wrapped line */
6110 draw_state = WL_START;
6111 saved_n_extra = n_extra;
6112 saved_p_extra = p_extra;
6113 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006114 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 saved_char_attr = char_attr;
6116 n_extra = 0;
6117 lcs_prec_todo = lcs_prec;
6118#ifdef FEAT_LINEBREAK
6119# ifdef FEAT_DIFF
6120 if (filler_todo <= 0)
6121# endif
6122 need_showbreak = TRUE;
6123#endif
6124#ifdef FEAT_DIFF
6125 --filler_todo;
6126 /* When the filler lines are actually below the last line of the
6127 * file, don't draw the line itself, break here. */
6128 if (filler_todo == 0 && wp->w_botfill)
6129 break;
6130#endif
6131 }
6132
6133 } /* for every character in the line */
6134
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006135#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006136 /* After an empty line check first word for capital. */
6137 if (*skipwhite(line) == NUL)
6138 {
6139 capcol_lnum = lnum + 1;
6140 cap_col = 0;
6141 }
6142#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006143#ifdef FEAT_TEXT_PROP
6144 vim_free(text_props);
6145 vim_free(text_prop_idxs);
6146#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006147
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006148 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 return row;
6150}
6151
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006152/*
6153 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006154 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006155 */
6156 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006157comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006158{
6159 int i;
6160
6161 for (i = 0; i < Screen_mco; ++i)
6162 {
6163 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6164 return TRUE;
6165 if (ScreenLinesC[i][off_from] == 0)
6166 break;
6167 }
6168 return FALSE;
6169}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006170
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171/*
6172 * Check whether the given character needs redrawing:
6173 * - the (first byte of the) character is different
6174 * - the attributes are different
6175 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006176 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 */
6178 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006179char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 if (cols > 0
6182 && ((ScreenLines[off_from] != ScreenLines[off_to]
6183 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 || (enc_dbcs != 0
6185 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6186 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6187 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6188 : (cols > 1 && ScreenLines[off_from + 1]
6189 != ScreenLines[off_to + 1])))
6190 || (enc_utf8
6191 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6192 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006193 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006194 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6195 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006196 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 return TRUE;
6198 return FALSE;
6199}
6200
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006201#if defined(FEAT_TERMINAL) || defined(PROTO)
6202/*
6203 * Return the index in ScreenLines[] for the current screen line.
6204 */
6205 int
6206screen_get_current_line_off()
6207{
6208 return (int)(current_ScreenLine - ScreenLines);
6209}
6210#endif
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212/*
6213 * Move one "cooked" screen line to the screen, but only the characters that
6214 * have actually changed. Handle insert/delete character.
6215 * "coloff" gives the first column on the screen for this line.
6216 * "endcol" gives the columns where valid characters are.
6217 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6218 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006219 * "flags" can have bits:
6220 * SLF_POPUP popup window
6221 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6223 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6224 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006225 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006226screen_line(
6227 int row,
6228 int coloff,
6229 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006230 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006231 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
6233 unsigned off_from;
6234 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006235 unsigned max_off_from;
6236 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 int force = FALSE; /* force update rest of the line */
6240 int redraw_this /* bool: does character need redraw? */
6241#ifdef FEAT_GUI
6242 = TRUE /* For GUI when while-loop empty */
6243#endif
6244 ;
6245 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 int clear_next = FALSE;
6247 int char_cells; /* 1: normal char */
6248 /* 2: occupies two display cells */
6249# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006251 /* Check for illegal row and col, just in case. */
6252 if (row >= Rows)
6253 row = Rows - 1;
6254 if (endcol > Columns)
6255 endcol = Columns;
6256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257# ifdef FEAT_CLIPBOARD
6258 clip_may_clear_selection(row, row);
6259# endif
6260
6261 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6262 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006263 max_off_from = off_from + screen_Columns;
6264 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006267 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
6269 /* Clear rest first, because it's left of the text. */
6270 if (clear_width > 0)
6271 {
6272 while (col <= endcol && ScreenLines[off_to] == ' '
6273 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006274 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
6276 ++off_to;
6277 ++col;
6278 }
6279 if (col <= endcol)
6280 screen_fill(row, row + 1, col + coloff,
6281 endcol + coloff + 1, ' ', ' ', 0);
6282 }
6283 col = endcol + 1;
6284 off_to = LineOffset[row] + col + coloff;
6285 off_from += col;
6286 endcol = (clear_width > 0 ? clear_width : -clear_width);
6287 }
6288#endif /* FEAT_RIGHTLEFT */
6289
6290 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6291
6292 while (col < endcol)
6293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006295 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 else
6297 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 redraw_this = redraw_next;
6300 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6301 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6302
6303#ifdef FEAT_GUI
6304 /* If the next character was bold, then redraw the current character to
6305 * remove any pixels that might have spilt over into us. This only
6306 * happens in the GUI.
6307 */
6308 if (redraw_next && gui.in_use)
6309 {
6310 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006311 if (hl > HL_ALL)
6312 hl = syn_attr2attr(hl);
6313 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 redraw_this = TRUE;
6315 }
6316#endif
6317
6318 if (redraw_this)
6319 {
6320 /*
6321 * Special handling when 'xs' termcap flag set (hpterm):
6322 * Attributes for characters are stored at the position where the
6323 * cursor is when writing the highlighting code. The
6324 * start-highlighting code must be written with the cursor on the
6325 * first highlighted character. The stop-highlighting code must
6326 * be written with the cursor just after the last highlighted
6327 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006328 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 * to clear the rest of the line, and force redrawing it
6330 * completely.
6331 */
6332 if ( p_wiv
6333 && !force
6334#ifdef FEAT_GUI
6335 && !gui.in_use
6336#endif
6337 && ScreenAttrs[off_to] != 0
6338 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6339 {
6340 /*
6341 * Need to remove highlighting attributes here.
6342 */
6343 windgoto(row, col + coloff);
6344 out_str(T_CE); /* clear rest of this screen line */
6345 screen_start(); /* don't know where cursor is now */
6346 force = TRUE; /* force redraw of rest of the line */
6347 redraw_next = TRUE; /* or else next char would miss out */
6348
6349 /*
6350 * If the previous character was highlighted, need to stop
6351 * highlighting at this character.
6352 */
6353 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6354 {
6355 screen_attr = ScreenAttrs[off_to - 1];
6356 term_windgoto(row, col + coloff);
6357 screen_stop_highlight();
6358 }
6359 else
6360 screen_attr = 0; /* highlighting has stopped */
6361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 if (enc_dbcs != 0)
6363 {
6364 /* Check if overwriting a double-byte with a single-byte or
6365 * the other way around requires another character to be
6366 * redrawn. For UTF-8 this isn't needed, because comparing
6367 * ScreenLinesUC[] is sufficient. */
6368 if (char_cells == 1
6369 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006370 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372 /* Writing a single-cell character over a double-cell
6373 * character: need to redraw the next cell. */
6374 ScreenLines[off_to + 1] = 0;
6375 redraw_next = TRUE;
6376 }
6377 else if (char_cells == 2
6378 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006379 && (*mb_off2cells)(off_to, max_off_to) == 1
6380 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 {
6382 /* Writing the second half of a double-cell character over
6383 * a double-cell character: need to redraw the second
6384 * cell. */
6385 ScreenLines[off_to + 2] = 0;
6386 redraw_next = TRUE;
6387 }
6388
6389 if (enc_dbcs == DBCS_JPNU)
6390 ScreenLines2[off_to] = ScreenLines2[off_from];
6391 }
6392 /* When writing a single-width character over a double-width
6393 * character and at the end of the redrawn text, need to clear out
6394 * the right halve of the old character.
6395 * Also required when writing the right halve of a double-width
6396 * char over the left halve of an existing one. */
6397 if (has_mbyte && col + char_cells == endcol
6398 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006399 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006401 && (*mb_off2cells)(off_to, max_off_to) == 1
6402 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (enc_utf8)
6407 {
6408 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6409 if (ScreenLinesUC[off_from] != 0)
6410 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006411 int i;
6412
6413 for (i = 0; i < Screen_mco; ++i)
6414 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416 }
6417 if (char_cells == 2)
6418 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419
6420#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006421 /* The bold trick makes a single column of pixels appear in the
6422 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 * character should be redrawn too. This happens for our own GUI
6424 * and for some xterms. */
6425 if (
6426# ifdef FEAT_GUI
6427 gui.in_use
6428# endif
6429# if defined(FEAT_GUI) && defined(UNIX)
6430 ||
6431# endif
6432# ifdef UNIX
6433 term_is_xterm
6434# endif
6435 )
6436 {
6437 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006438 if (hl > HL_ALL)
6439 hl = syn_attr2attr(hl);
6440 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 redraw_next = TRUE;
6442 }
6443#endif
6444 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006445
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006446 /* For simplicity set the attributes of second half of a
6447 * double-wide character equal to the first half. */
6448 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006450
6451 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 screen_char(off_to, row, col + coloff);
6455 }
6456 else if ( p_wiv
6457#ifdef FEAT_GUI
6458 && !gui.in_use
6459#endif
6460 && col + coloff > 0)
6461 {
6462 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6463 {
6464 /*
6465 * Don't output stop-highlight when moving the cursor, it will
6466 * stop the highlighting when it should continue.
6467 */
6468 screen_attr = 0;
6469 }
6470 else if (screen_attr != 0)
6471 screen_stop_highlight();
6472 }
6473
6474 off_to += CHAR_CELLS;
6475 off_from += CHAR_CELLS;
6476 col += CHAR_CELLS;
6477 }
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (clear_next)
6480 {
6481 /* Clear the second half of a double-wide character of which the left
6482 * half was overwritten with a single-wide character. */
6483 ScreenLines[off_to] = ' ';
6484 if (enc_utf8)
6485 ScreenLinesUC[off_to] = 0;
6486 screen_char(off_to, row, col + coloff);
6487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
6489 if (clear_width > 0
6490#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006491 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492#endif
6493 )
6494 {
6495#ifdef FEAT_GUI
6496 int startCol = col;
6497#endif
6498
6499 /* blank out the rest of the line */
6500 while (col < clear_width && ScreenLines[off_to] == ' '
6501 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006502 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
6504 ++off_to;
6505 ++col;
6506 }
6507 if (col < clear_width)
6508 {
6509#ifdef FEAT_GUI
6510 /*
6511 * In the GUI, clearing the rest of the line may leave pixels
6512 * behind if the first character cleared was bold. Some bold
6513 * fonts spill over the left. In this case we redraw the previous
6514 * character too. If we didn't skip any blanks above, then we
6515 * only redraw if the character wasn't already redrawn anyway.
6516 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006517 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
6519 hl = ScreenAttrs[off_to];
6520 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006521 {
6522 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006523
Bram Moolenaar9c697322006-10-09 20:11:17 +00006524 if (enc_utf8)
6525 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6526 * that its width is 2. */
6527 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6528 else if (enc_dbcs != 0)
6529 {
6530 /* find previous character by counting from first
6531 * column and get its width. */
6532 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006533 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006534
6535 while (off < off_to)
6536 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006537 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006538 off += prev_cells;
6539 }
6540 }
6541
6542 if (enc_dbcs != 0 && prev_cells > 1)
6543 screen_char_2(off_to - prev_cells, row,
6544 col + coloff - prev_cells);
6545 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006546 screen_char(off_to - prev_cells, row,
6547 col + coloff - prev_cells);
6548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550#endif
6551 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6552 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 off_to += clear_width - col;
6554 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556 }
6557
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006558 if (clear_width > 0
6559#ifdef FEAT_TEXT_PROP
6560 && !(flags & SLF_POPUP) // no separator for popup window
6561#endif
6562 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006564 // For a window that has a right neighbor, draw the separator char
6565 // right of the window contents.
6566 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 {
6568 int c;
6569
6570 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006571 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006572 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6573 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 || ScreenAttrs[off_to] != hl)
6575 {
6576 ScreenLines[off_to] = c;
6577 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (enc_utf8)
6579 {
6580 if (c >= 0x80)
6581 {
6582 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006583 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 }
6585 else
6586 ScreenLinesUC[off_to] = 0;
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 screen_char(off_to, row, col + coloff);
6589 }
6590 }
6591 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 LineWraps[row] = FALSE;
6593 }
6594}
6595
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006596#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006598 * Mirror text "str" for right-left displaying.
6599 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006601 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006602rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 char_u *p1, *p2;
6605 int t;
6606
6607 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6608 {
6609 t = *p1;
6610 *p1 = *p2;
6611 *p2 = t;
6612 }
6613}
6614#endif
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616/*
6617 * mark all status lines for redraw; used after first :cd
6618 */
6619 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006620status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 win_T *wp;
6623
Bram Moolenaar29323592016-07-24 22:04:11 +02006624 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 if (wp->w_status_height)
6626 {
6627 wp->w_redr_status = TRUE;
6628 redraw_later(VALID);
6629 }
6630}
6631
6632/*
6633 * mark all status lines of the current buffer for redraw
6634 */
6635 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006636status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 win_T *wp;
6639
Bram Moolenaar29323592016-07-24 22:04:11 +02006640 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6642 {
6643 wp->w_redr_status = TRUE;
6644 redraw_later(VALID);
6645 }
6646}
6647
6648/*
6649 * Redraw all status lines that need to be redrawn.
6650 */
6651 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006652redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 win_T *wp;
6655
Bram Moolenaar29323592016-07-24 22:04:11 +02006656 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006658 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006659 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006660 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
Bram Moolenaar4033c552017-09-16 20:54:51 +02006663#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664/*
6665 * Redraw all status lines at the bottom of frame "frp".
6666 */
6667 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006668win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 if (frp->fr_layout == FR_LEAF)
6671 frp->fr_win->w_redr_status = TRUE;
6672 else if (frp->fr_layout == FR_ROW)
6673 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006674 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 win_redraw_last_status(frp);
6676 }
6677 else /* frp->fr_layout == FR_COL */
6678 {
6679 frp = frp->fr_child;
6680 while (frp->fr_next != NULL)
6681 frp = frp->fr_next;
6682 win_redraw_last_status(frp);
6683 }
6684}
6685#endif
6686
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687/*
6688 * Draw the verticap separator right of window "wp" starting with line "row".
6689 */
6690 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006691draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 int hl;
6694 int c;
6695
6696 if (wp->w_vsep_width)
6697 {
6698 /* draw the vertical separator right of this window */
6699 c = fillchar_vsep(&hl);
6700 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6701 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6702 c, ' ', hl);
6703 }
6704}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006707static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006710 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 */
6712 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006713status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 int len = 0;
6716
6717#ifdef FEAT_MENU
6718 int emenu = (xp->xp_context == EXPAND_MENUS
6719 || xp->xp_context == EXPAND_MENUNAMES);
6720
6721 /* Check for menu separators - replace with '|'. */
6722 if (emenu && menu_is_separator(s))
6723 return 1;
6724#endif
6725
6726 while (*s != NUL)
6727 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006728 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006729 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006730 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732
6733 return len;
6734}
6735
6736/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006737 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006738 * These are backslashes used for escaping. Do show backslashes in help tags.
6739 */
6740 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006741skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006742{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006743 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006744#ifdef FEAT_MENU
6745 || ((xp->xp_context == EXPAND_MENUS
6746 || xp->xp_context == EXPAND_MENUNAMES)
6747 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6748#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006749 )
6750 {
6751#ifndef BACKSLASH_IN_FILENAME
6752 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6753 return 2;
6754#endif
6755 return 1;
6756 }
6757 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006758}
6759
6760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 * Show wildchar matches in the status line.
6762 * Show at least the "match" item.
6763 * We start at item 'first_match' in the list and show all matches that fit.
6764 *
6765 * If inversion is possible we use it. Else '=' characters are used.
6766 */
6767 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006768win_redr_status_matches(
6769 expand_T *xp,
6770 int num_matches,
6771 char_u **matches, /* list of matches */
6772 int match,
6773 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6776 int row;
6777 char_u *buf;
6778 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006779 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 int fillchar;
6781 int attr;
6782 int i;
6783 int highlight = TRUE;
6784 char_u *selstart = NULL;
6785 int selstart_col = 0;
6786 char_u *selend = NULL;
6787 static int first_match = 0;
6788 int add_left = FALSE;
6789 char_u *s;
6790#ifdef FEAT_MENU
6791 int emenu;
6792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
6795 if (matches == NULL) /* interrupted completion? */
6796 return;
6797
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006798 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006799 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006800 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006801 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 if (buf == NULL)
6803 return;
6804
6805 if (match == -1) /* don't show match but original text */
6806 {
6807 match = 0;
6808 highlight = FALSE;
6809 }
6810 /* count 1 for the ending ">" */
6811 clen = status_match_len(xp, L_MATCH(match)) + 3;
6812 if (match == 0)
6813 first_match = 0;
6814 else if (match < first_match)
6815 {
6816 /* jumping left, as far as we can go */
6817 first_match = match;
6818 add_left = TRUE;
6819 }
6820 else
6821 {
6822 /* check if match fits on the screen */
6823 for (i = first_match; i < match; ++i)
6824 clen += status_match_len(xp, L_MATCH(i)) + 2;
6825 if (first_match > 0)
6826 clen += 2;
6827 /* jumping right, put match at the left */
6828 if ((long)clen > Columns)
6829 {
6830 first_match = match;
6831 /* if showing the last match, we can add some on the left */
6832 clen = 2;
6833 for (i = match; i < num_matches; ++i)
6834 {
6835 clen += status_match_len(xp, L_MATCH(i)) + 2;
6836 if ((long)clen >= Columns)
6837 break;
6838 }
6839 if (i == num_matches)
6840 add_left = TRUE;
6841 }
6842 }
6843 if (add_left)
6844 while (first_match > 0)
6845 {
6846 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6847 if ((long)clen >= Columns)
6848 break;
6849 --first_match;
6850 }
6851
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006852 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854 if (first_match == 0)
6855 {
6856 *buf = NUL;
6857 len = 0;
6858 }
6859 else
6860 {
6861 STRCPY(buf, "< ");
6862 len = 2;
6863 }
6864 clen = len;
6865
6866 i = first_match;
6867 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6868 {
6869 if (i == match)
6870 {
6871 selstart = buf + len;
6872 selstart_col = clen;
6873 }
6874
6875 s = L_MATCH(i);
6876 /* Check for menu separators - replace with '|' */
6877#ifdef FEAT_MENU
6878 emenu = (xp->xp_context == EXPAND_MENUS
6879 || xp->xp_context == EXPAND_MENUNAMES);
6880 if (emenu && menu_is_separator(s))
6881 {
6882 STRCPY(buf + len, transchar('|'));
6883 l = (int)STRLEN(buf + len);
6884 len += l;
6885 clen += l;
6886 }
6887 else
6888#endif
6889 for ( ; *s != NUL; ++s)
6890 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006891 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006893 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 {
6895 STRNCPY(buf + len, s, l);
6896 s += l - 1;
6897 len += l;
6898 }
6899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
6901 STRCPY(buf + len, transchar_byte(*s));
6902 len += (int)STRLEN(buf + len);
6903 }
6904 }
6905 if (i == match)
6906 selend = buf + len;
6907
6908 *(buf + len++) = ' ';
6909 *(buf + len++) = ' ';
6910 clen += 2;
6911 if (++i == num_matches)
6912 break;
6913 }
6914
6915 if (i != num_matches)
6916 {
6917 *(buf + len++) = '>';
6918 ++clen;
6919 }
6920
6921 buf[len] = NUL;
6922
6923 row = cmdline_row - 1;
6924 if (row >= 0)
6925 {
6926 if (wild_menu_showing == 0)
6927 {
6928 if (msg_scrolled > 0)
6929 {
6930 /* Put the wildmenu just above the command line. If there is
6931 * no room, scroll the screen one line up. */
6932 if (cmdline_row == Rows - 1)
6933 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006934 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 ++msg_scrolled;
6936 }
6937 else
6938 {
6939 ++cmdline_row;
6940 ++row;
6941 }
6942 wild_menu_showing = WM_SCROLLED;
6943 }
6944 else
6945 {
6946 /* Create status line if needed by setting 'laststatus' to 2.
6947 * Set 'winminheight' to zero to avoid that the window is
6948 * resized. */
6949 if (lastwin->w_status_height == 0)
6950 {
6951 save_p_ls = p_ls;
6952 save_p_wmh = p_wmh;
6953 p_ls = 2;
6954 p_wmh = 0;
6955 last_status(FALSE);
6956 }
6957 wild_menu_showing = WM_SHOWN;
6958 }
6959 }
6960
6961 screen_puts(buf, row, 0, attr);
6962 if (selstart != NULL && highlight)
6963 {
6964 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006965 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
6967
6968 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6969 }
6970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 vim_free(buf);
6973}
6974#endif
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976/*
6977 * Redraw the status line of window wp.
6978 *
6979 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006980 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6981 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006983 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006984win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 int row;
6987 char_u *p;
6988 int len;
6989 int fillchar;
6990 int attr;
6991 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006992 static int busy = FALSE;
6993
6994 /* It's possible to get here recursively when 'statusline' (indirectly)
6995 * invokes ":redrawstatus". Simply ignore the call then. */
6996 if (busy)
6997 return;
6998 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 wp->w_redr_status = FALSE;
7001 if (wp->w_status_height == 0)
7002 {
7003 /* no status line, can only be last window */
7004 redraw_cmdline = TRUE;
7005 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007006 else if (!redrawing()
7007#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007008 // don't update status line when popup menu is visible and may be
7009 // drawn over it, unless it will be redrawn later
7010 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007011#endif
7012 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
7014 /* Don't redraw right now, do it later. */
7015 wp->w_redr_status = TRUE;
7016 }
7017#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007018 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 {
7020 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007021 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 }
7023#endif
7024 else
7025 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007026 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007028 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 p = NameBuff;
7030 len = (int)STRLEN(p);
7031
Bram Moolenaard85f2712017-07-28 21:51:57 +02007032 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033#ifdef FEAT_QUICKFIX
7034 || wp->w_p_pvw
7035#endif
7036 || bufIsChanged(wp->w_buffer)
7037 || wp->w_buffer->b_p_ro)
7038 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007039 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007041 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 len += (int)STRLEN(p + len);
7043 }
7044#ifdef FEAT_QUICKFIX
7045 if (wp->w_p_pvw)
7046 {
7047 STRCPY(p + len, _("[Preview]"));
7048 len += (int)STRLEN(p + len);
7049 }
7050#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007051 if (bufIsChanged(wp->w_buffer)
7052#ifdef FEAT_TERMINAL
7053 && !bt_terminal(wp->w_buffer)
7054#endif
7055 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
7057 STRCPY(p + len, "[+]");
7058 len += 3;
7059 }
7060 if (wp->w_buffer->b_p_ro)
7061 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007062 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007063 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065
Bram Moolenaar02631462017-09-22 15:20:32 +02007066 this_ru_col = ru_col - (Columns - wp->w_width);
7067 if (this_ru_col < (wp->w_width + 1) / 2)
7068 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if (this_ru_col <= 1)
7070 {
7071 p = (char_u *)"<"; /* No room for file name! */
7072 len = 1;
7073 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007074 else if (has_mbyte)
7075 {
7076 int clen = 0, i;
7077
7078 /* Count total number of display cells. */
7079 clen = mb_string2cells(p, -1);
7080
7081 /* Find first character that will fit.
7082 * Going from start to end is much faster for DBCS. */
7083 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7084 i += (*mb_ptr2len)(p + i))
7085 clen -= (*mb_ptr2cells)(p + i);
7086 len = clen;
7087 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007089 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007091 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093
Bram Moolenaara12a1612019-01-24 16:39:02 +01007094 }
7095 else if (len > this_ru_col - 1)
7096 {
7097 p += len - (this_ru_col - 1);
7098 *p = '<';
7099 len = this_ru_col - 1;
7100 }
7101
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007103 screen_puts(p, row, wp->w_wincol, attr);
7104 screen_fill(row, row + 1, len + wp->w_wincol,
7105 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007107 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7109 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007110 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111
7112#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007113 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114#endif
7115 }
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 /*
7118 * May need to draw the character below the vertical separator.
7119 */
7120 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7121 {
7122 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007123 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 else
7125 fillchar = fillchar_vsep(&attr);
7126 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7127 attr);
7128 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007129 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130}
7131
Bram Moolenaar238a5642006-02-21 22:12:05 +00007132#ifdef FEAT_STL_OPT
7133/*
7134 * Redraw the status line according to 'statusline' and take care of any
7135 * errors encountered.
7136 */
7137 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007138redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007139{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007140 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007141 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007142
7143 /* When called recursively return. This can happen when the statusline
7144 * contains an expression that triggers a redraw. */
7145 if (entered)
7146 return;
7147 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007148
Bram Moolenaara742e082016-04-05 21:10:38 +02007149 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007150 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007151 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007152 {
7153 /* When there is an error disable the statusline, otherwise the
7154 * display is messed up with errors and a redraw triggers the problem
7155 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007156 set_string_option_direct((char_u *)"statusline", -1,
7157 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007158 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007159 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007160 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007161 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007162}
7163#endif
7164
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165/*
7166 * Return TRUE if the status line of window "wp" is connected to the status
7167 * line of the window right of it. If not, then it's a vertical separator.
7168 * Only call if (wp->w_vsep_width != 0).
7169 */
7170 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007171stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 frame_T *fr;
7174
7175 fr = wp->w_frame;
7176 while (fr->fr_parent != NULL)
7177 {
7178 if (fr->fr_parent->fr_layout == FR_COL)
7179 {
7180 if (fr->fr_next != NULL)
7181 break;
7182 }
7183 else
7184 {
7185 if (fr->fr_next != NULL)
7186 return TRUE;
7187 }
7188 fr = fr->fr_parent;
7189 }
7190 return FALSE;
7191}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194/*
7195 * Get the value to show for the language mappings, active 'keymap'.
7196 */
7197 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007198get_keymap_str(
7199 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007200 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007201 char_u *buf, /* buffer for the result */
7202 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 char_u *p;
7205
7206 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7207 return FALSE;
7208
7209 {
7210#ifdef FEAT_EVAL
7211 buf_T *old_curbuf = curbuf;
7212 win_T *old_curwin = curwin;
7213 char_u *s;
7214
7215 curbuf = wp->w_buffer;
7216 curwin = wp;
7217 STRCPY(buf, "b:keymap_name"); /* must be writable */
7218 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007219 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 --emsg_skip;
7221 curbuf = old_curbuf;
7222 curwin = old_curwin;
7223 if (p == NULL || *p == NUL)
7224#endif
7225 {
7226#ifdef FEAT_KEYMAP
7227 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7228 p = wp->w_buffer->b_p_keymap;
7229 else
7230#endif
7231 p = (char_u *)"lang";
7232 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007233 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 buf[0] = NUL;
7235#ifdef FEAT_EVAL
7236 vim_free(s);
7237#endif
7238 }
7239 return buf[0] != NUL;
7240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
7242#if defined(FEAT_STL_OPT) || defined(PROTO)
7243/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007244 * Redraw the status line or ruler of window "wp".
7245 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 */
7247 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007248win_redr_custom(
7249 win_T *wp,
7250 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007252 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 int attr;
7254 int curattr;
7255 int row;
7256 int col = 0;
7257 int maxwidth;
7258 int width;
7259 int n;
7260 int len;
7261 int fillchar;
7262 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007263 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007265 struct stl_hlrec hltab[STL_MAX_ITEM];
7266 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007267 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007268 win_T *ewp;
7269 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaar1d633412013-12-11 15:52:01 +01007271 /* There is a tiny chance that this gets called recursively: When
7272 * redrawing a status line triggers redrawing the ruler or tabline.
7273 * Avoid trouble by not allowing recursion. */
7274 if (entered)
7275 return;
7276 entered = TRUE;
7277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007279 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007281 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007282 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007283 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007284 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007285 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007286 maxwidth = Columns;
7287# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007288 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007291 else
7292 {
7293 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007294 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007295 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007296
7297 if (draw_ruler)
7298 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007299 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007300 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007301 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007302 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007303 if (*++stl == '-')
7304 stl++;
7305 if (atoi((char *)stl))
7306 while (VIM_ISDIGIT(*stl))
7307 stl++;
7308 if (*stl++ != '(')
7309 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007310 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007311 col = ru_col - (Columns - wp->w_width);
7312 if (col < (wp->w_width + 1) / 2)
7313 col = (wp->w_width + 1) / 2;
7314 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007315 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007316 {
7317 row = Rows - 1;
7318 --maxwidth; /* writing in last column may cause scrolling */
7319 fillchar = ' ';
7320 attr = 0;
7321 }
7322
7323# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007324 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007325# endif
7326 }
7327 else
7328 {
7329 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007330 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007331 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007332 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007333# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007334 use_sandbox = was_set_insecurely((char_u *)"statusline",
7335 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336# endif
7337 }
7338
Bram Moolenaar53f81742017-09-22 14:35:51 +02007339 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007340 }
7341
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007343 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
Bram Moolenaar61452852011-02-01 18:01:11 +01007345 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7346 * the cursor away and back. */
7347 ewp = wp == NULL ? curwin : wp;
7348 p_crb_save = ewp->w_p_crb;
7349 ewp->w_p_crb = FALSE;
7350
Bram Moolenaar362f3562009-11-03 16:20:34 +00007351 /* Make a copy, because the statusline may include a function call that
7352 * might change the option value and free the memory. */
7353 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007354 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007355 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007356 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007357 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007358 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007360 /* Make all characters printable. */
7361 p = transstr(buf);
7362 if (p != NULL)
7363 {
7364 vim_strncpy(buf, p, sizeof(buf) - 1);
7365 vim_free(p);
7366 }
7367
7368 /* fill up with "fillchar" */
7369 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007370 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 ++width;
7374 }
7375 buf[len] = NUL;
7376
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007377 /*
7378 * Draw each snippet with the specified highlighting.
7379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 curattr = attr;
7381 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007382 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007384 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 screen_puts_len(p, len, row, col, curattr);
7386 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007387 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007389 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007391 else if (hltab[n].userhl < 0)
7392 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007393#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007394 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7395 && wp->w_status_height != 0)
7396 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007397 else if (wp != NULL && bt_terminal(wp->w_buffer)
7398 && wp->w_status_height != 0)
7399 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007400#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007401 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007402 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007404 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007407
7408 if (wp == NULL)
7409 {
7410 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7411 col = 0;
7412 len = 0;
7413 p = buf;
7414 fillchar = 0;
7415 for (n = 0; tabtab[n].start != NULL; n++)
7416 {
7417 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7418 while (col < len)
7419 TabPageIdxs[col++] = fillchar;
7420 p = tabtab[n].start;
7421 fillchar = tabtab[n].userhl;
7422 }
7423 while (col < Columns)
7424 TabPageIdxs[col++] = fillchar;
7425 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007426
7427theend:
7428 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429}
7430
7431#endif /* FEAT_STL_OPT */
7432
7433/*
7434 * Output a single character directly to the screen and update ScreenLines.
7435 */
7436 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007437screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 char_u buf[MB_MAXBYTES + 1];
7440
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007441 if (has_mbyte)
7442 buf[(*mb_char2bytes)(c, buf)] = NUL;
7443 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007444 {
7445 buf[0] = c;
7446 buf[1] = NUL;
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 screen_puts(buf, row, col, attr);
7449}
7450
7451/*
7452 * Get a single character directly from ScreenLines into "bytes[]".
7453 * Also return its attribute in *attrp;
7454 */
7455 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007456screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
7458 unsigned off;
7459
7460 /* safety check */
7461 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7462 {
7463 off = LineOffset[row] + col;
7464 *attrp = ScreenAttrs[off];
7465 bytes[0] = ScreenLines[off];
7466 bytes[1] = NUL;
7467
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (enc_utf8 && ScreenLinesUC[off] != 0)
7469 bytes[utfc_char2bytes(off, bytes)] = NUL;
7470 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7471 {
7472 bytes[0] = ScreenLines[off];
7473 bytes[1] = ScreenLines2[off];
7474 bytes[2] = NUL;
7475 }
7476 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7477 {
7478 bytes[1] = ScreenLines[off + 1];
7479 bytes[2] = NUL;
7480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482}
7483
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007484/*
7485 * Return TRUE if composing characters for screen posn "off" differs from
7486 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007487 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007488 */
7489 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007490screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491{
7492 int i;
7493
7494 for (i = 0; i < Screen_mco; ++i)
7495 {
7496 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7497 return TRUE;
7498 if (u8cc[i] == 0)
7499 break;
7500 }
7501 return FALSE;
7502}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504/*
7505 * Put string '*text' on the screen at position 'row' and 'col', with
7506 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7507 * Note: only outputs within one row, message is truncated at screen boundary!
7508 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7509 */
7510 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007511screen_puts(
7512 char_u *text,
7513 int row,
7514 int col,
7515 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 screen_puts_len(text, -1, row, col, attr);
7518}
7519
7520/*
7521 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7522 * a NUL.
7523 */
7524 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007525screen_puts_len(
7526 char_u *text,
7527 int textlen,
7528 int row,
7529 int col,
7530 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531{
7532 unsigned off;
7533 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007534 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007536 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 int mbyte_blen = 1;
7538 int mbyte_cells = 1;
7539 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007540 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007542#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007544 int pc, nc, nc1;
7545 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007547 int force_redraw_this;
7548 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007549 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
7551 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7552 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007553 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
Bram Moolenaarc236c162008-07-13 17:41:49 +00007555 /* When drawing over the right halve of a double-wide char clear out the
7556 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007557 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007558#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007559 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007560#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007561 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007562 {
7563 ScreenLines[off - 1] = ' ';
7564 ScreenAttrs[off - 1] = 0;
7565 if (enc_utf8)
7566 {
7567 ScreenLinesUC[off - 1] = 0;
7568 ScreenLinesC[0][off - 1] = 0;
7569 }
7570 /* redraw the previous cell, make it empty */
7571 screen_char(off - 1, row, col - 1);
7572 /* force the cell at "col" to be redrawn */
7573 force_redraw_next = TRUE;
7574 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007575
Bram Moolenaar367329b2007-08-30 11:53:22 +00007576 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007577 while (col < screen_Columns
7578 && (len < 0 || (int)(ptr - text) < len)
7579 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 {
7581 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 /* check if this is the first byte of a multibyte */
7583 if (has_mbyte)
7584 {
7585 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007586 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007588 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7590 mbyte_cells = 1;
7591 else if (enc_dbcs != 0)
7592 mbyte_cells = mbyte_blen;
7593 else /* enc_utf8 */
7594 {
7595 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007596 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 (int)((text + len) - ptr));
7598 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007601#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7603 {
7604 /* Do Arabic shaping. */
7605 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7606 {
7607 /* Past end of string to be displayed. */
7608 nc = NUL;
7609 nc1 = NUL;
7610 }
7611 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007612 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007613 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7614 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007615 nc1 = pcc[0];
7616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 pc = prev_c;
7618 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007619 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 }
7621 else
7622 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007623#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007624 if (col + mbyte_cells > screen_Columns)
7625 {
7626 /* Only 1 cell left, but character requires 2 cells:
7627 * display a '>' in the last column to avoid wrapping. */
7628 c = '>';
7629 mbyte_cells = 1;
7630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007634 force_redraw_this = force_redraw_next;
7635 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007636
7637 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 || (mbyte_cells == 2
7639 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7640 || (enc_dbcs == DBCS_JPNU
7641 && c == 0x8e
7642 && ScreenLines2[off] != ptr[1])
7643 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007644 && (ScreenLinesUC[off] !=
7645 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7646 || (ScreenLinesUC[off] != 0
7647 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007649 || exmode_active;
7650
Bram Moolenaara12a1612019-01-24 16:39:02 +01007651 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
7653#if defined(FEAT_GUI) || defined(UNIX)
7654 /* The bold trick makes a single row of pixels appear in the next
7655 * character. When a bold character is removed, the next
7656 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007657 * and for some xterms. */
7658 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659# ifdef FEAT_GUI
7660 gui.in_use
7661# endif
7662# if defined(FEAT_GUI) && defined(UNIX)
7663 ||
7664# endif
7665# ifdef UNIX
7666 term_is_xterm
7667# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007668 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007670 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007672 if (n > HL_ALL)
7673 n = syn_attr2attr(n);
7674 if (n & HL_BOLD)
7675 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 }
7677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 /* When at the end of the text and overwriting a two-cell
7679 * character with a one-cell character, need to clear the next
7680 * cell. Also when overwriting the left halve of a two-cell char
7681 * with the right halve of a two-cell char. Do this only once
7682 * (mb_off2cells() may return 2 on the right halve). */
7683 if (clear_next_cell)
7684 clear_next_cell = FALSE;
7685 else if (has_mbyte
7686 && (len < 0 ? ptr[mbyte_blen] == NUL
7687 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007688 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007690 && (*mb_off2cells)(off, max_off) == 1
7691 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 clear_next_cell = TRUE;
7693
7694 /* Make sure we never leave a second byte of a double-byte behind,
7695 * it confuses mb_off2cells(). */
7696 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007697 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007699 && (*mb_off2cells)(off, max_off) == 1
7700 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 ScreenLines[off] = c;
7703 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 if (enc_utf8)
7705 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007706 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 ScreenLinesUC[off] = 0;
7708 else
7709 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007710 int i;
7711
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007713 for (i = 0; i < Screen_mco; ++i)
7714 {
7715 ScreenLinesC[i][off] = u8cc[i];
7716 if (u8cc[i] == 0)
7717 break;
7718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 }
7720 if (mbyte_cells == 2)
7721 {
7722 ScreenLines[off + 1] = 0;
7723 ScreenAttrs[off + 1] = attr;
7724 }
7725 screen_char(off, row, col);
7726 }
7727 else if (mbyte_cells == 2)
7728 {
7729 ScreenLines[off + 1] = ptr[1];
7730 ScreenAttrs[off + 1] = attr;
7731 screen_char_2(off, row, col);
7732 }
7733 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7734 {
7735 ScreenLines2[off] = ptr[1];
7736 screen_char(off, row, col);
7737 }
7738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 screen_char(off, row, col);
7740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (has_mbyte)
7742 {
7743 off += mbyte_cells;
7744 col += mbyte_cells;
7745 ptr += mbyte_blen;
7746 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007747 {
7748 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007750 len = -1;
7751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {
7755 ++off;
7756 ++col;
7757 ++ptr;
7758 }
7759 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007760
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007761 /* If we detected the next character needs to be redrawn, but the text
7762 * doesn't extend up to there, update the character here. */
7763 if (force_redraw_next && col < screen_Columns)
7764 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007765 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7766 screen_char_2(off, row, col);
7767 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007768 screen_char(off, row, col);
7769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770}
7771
7772#ifdef FEAT_SEARCH_EXTRA
7773/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007774 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 */
7776 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007777start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 if (p_hls && !no_hlsearch)
7780 {
7781 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007782 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007783# ifdef FEAT_RELTIME
7784 /* Set the time limit to 'redrawtime'. */
7785 profile_setlimit(p_rdt, &search_hl.tm);
7786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
7788}
7789
7790/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007791 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 */
7793 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007794end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 if (search_hl.rm.regprog != NULL)
7797 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007798 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 search_hl.rm.regprog = NULL;
7800 }
7801}
7802
7803/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007804 * Init for calling prepare_search_hl().
7805 */
7806 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007807init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007808{
7809 matchitem_T *cur;
7810
7811 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7812 * match */
7813 cur = wp->w_match_head;
7814 while (cur != NULL)
7815 {
7816 cur->hl.rm = cur->match;
7817 if (cur->hlg_id == 0)
7818 cur->hl.attr = 0;
7819 else
7820 cur->hl.attr = syn_id2attr(cur->hlg_id);
7821 cur->hl.buf = wp->w_buffer;
7822 cur->hl.lnum = 0;
7823 cur->hl.first_lnum = 0;
7824# ifdef FEAT_RELTIME
7825 /* Set the time limit to 'redrawtime'. */
7826 profile_setlimit(p_rdt, &(cur->hl.tm));
7827# endif
7828 cur = cur->next;
7829 }
7830 search_hl.buf = wp->w_buffer;
7831 search_hl.lnum = 0;
7832 search_hl.first_lnum = 0;
7833 /* time limit is set at the toplevel, for all windows */
7834}
7835
7836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 * Advance to the match in window "wp" line "lnum" or past it.
7838 */
7839 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007840prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007842 matchitem_T *cur; /* points to the match list */
7843 match_T *shl; /* points to search_hl or a match */
7844 int shl_flag; /* flag to indicate whether search_hl
7845 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007846 int pos_inprogress; /* marks that position match search is
7847 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 int n;
7849
7850 /*
7851 * When using a multi-line pattern, start searching at the top
7852 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007853 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007855 cur = wp->w_match_head;
7856 shl_flag = FALSE;
7857 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007859 if (shl_flag == FALSE)
7860 {
7861 shl = &search_hl;
7862 shl_flag = TRUE;
7863 }
7864 else
7865 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 if (shl->rm.regprog != NULL
7867 && shl->lnum == 0
7868 && re_multiline(shl->rm.regprog))
7869 {
7870 if (shl->first_lnum == 0)
7871 {
7872# ifdef FEAT_FOLDING
7873 for (shl->first_lnum = lnum;
7874 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7875 if (hasFoldingWin(wp, shl->first_lnum - 1,
7876 NULL, NULL, TRUE, NULL))
7877 break;
7878# else
7879 shl->first_lnum = wp->w_topline;
7880# endif
7881 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007882 if (cur != NULL)
7883 cur->pos.cur = 0;
7884 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007886 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7887 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007889 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7890 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007891 pos_inprogress = cur == NULL || cur->pos.cur == 0
7892 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 if (shl->lnum != 0)
7894 {
7895 shl->first_lnum = shl->lnum
7896 + shl->rm.endpos[0].lnum
7897 - shl->rm.startpos[0].lnum;
7898 n = shl->rm.endpos[0].col;
7899 }
7900 else
7901 {
7902 ++shl->first_lnum;
7903 n = 0;
7904 }
7905 }
7906 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007907 if (shl != &search_hl && cur != NULL)
7908 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910}
7911
7912/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007913 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 * Uses shl->buf.
7915 * Sets shl->lnum and shl->rm contents.
7916 * Note: Assumes a previous match is always before "lnum", unless
7917 * shl->lnum is zero.
7918 * Careful: Any pointers for buffer lines will become invalid.
7919 */
7920 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007921next_search_hl(
7922 win_T *win,
7923 match_T *shl, /* points to search_hl or a match */
7924 linenr_T lnum,
7925 colnr_T mincol, /* minimal column for a match */
7926 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 linenr_T l;
7929 colnr_T matchcol;
7930 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007931 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007933 // for :{range}s/pat only highlight inside the range
7934 if (lnum < search_first_line || lnum > search_last_line)
7935 {
7936 shl->lnum = 0;
7937 return;
7938 }
7939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (shl->lnum != 0)
7941 {
7942 /* Check for three situations:
7943 * 1. If the "lnum" is below a previous match, start a new search.
7944 * 2. If the previous match includes "mincol", use it.
7945 * 3. Continue after the previous match.
7946 */
7947 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7948 if (lnum > l)
7949 shl->lnum = 0;
7950 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7951 return;
7952 }
7953
7954 /*
7955 * Repeat searching for a match until one is found that includes "mincol"
7956 * or none is found in this line.
7957 */
7958 called_emsg = FALSE;
7959 for (;;)
7960 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007961#ifdef FEAT_RELTIME
7962 /* Stop searching after passing the time limit. */
7963 if (profile_passed_limit(&(shl->tm)))
7964 {
7965 shl->lnum = 0; /* no match found in time */
7966 break;
7967 }
7968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 /* Three situations:
7970 * 1. No useful previous match: search from start of line.
7971 * 2. Not Vi compatible or empty match: continue at next character.
7972 * Break the loop if this is beyond the end of the line.
7973 * 3. Vi compatible searching: continue at end of previous match.
7974 */
7975 if (shl->lnum == 0)
7976 matchcol = 0;
7977 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7978 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007979 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007981 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982
7983 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007984 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007985 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007987 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 shl->lnum = 0;
7989 break;
7990 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007991 if (has_mbyte)
7992 matchcol += mb_ptr2len(ml);
7993 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007994 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 }
7996 else
7997 matchcol = shl->rm.endpos[0].col;
7998
7999 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008000 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008002 /* Remember whether shl->rm is using a copy of the regprog in
8003 * cur->match. */
8004 int regprog_is_copy = (shl != &search_hl && cur != NULL
8005 && shl == &cur->hl
8006 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008007 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008008
Bram Moolenaarb3414592014-06-17 17:48:32 +02008009 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8010 matchcol,
8011#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008012 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008013#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008014 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008015#endif
8016 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008017 /* Copy the regprog, in case it got freed and recompiled. */
8018 if (regprog_is_copy)
8019 cur->match.regprog = cur->hl.rm.regprog;
8020
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008021 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008022 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008023 /* Error while handling regexp: stop using this regexp. */
8024 if (shl == &search_hl)
8025 {
8026 /* don't free regprog in the match list, it's a copy */
8027 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008028 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008029 }
8030 shl->rm.regprog = NULL;
8031 shl->lnum = 0;
8032 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8033 message */
8034 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008035 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008036 }
8037 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008038 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008039 else
8040 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (nmatched == 0)
8042 {
8043 shl->lnum = 0; /* no match found */
8044 break;
8045 }
8046 if (shl->rm.startpos[0].lnum > 0
8047 || shl->rm.startpos[0].col >= mincol
8048 || nmatched > 1
8049 || shl->rm.endpos[0].col > mincol)
8050 {
8051 shl->lnum += shl->rm.startpos[0].lnum;
8052 break; /* useful match found */
8053 }
8054 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008055
8056 // Restore called_emsg for assert_fails().
8057 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
Bram Moolenaar85077472016-10-16 14:35:48 +02008060/*
8061 * If there is a match fill "shl" and return one.
8062 * Return zero otherwise.
8063 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008064 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008065next_search_hl_pos(
8066 match_T *shl, /* points to a match */
8067 linenr_T lnum,
8068 posmatch_T *posmatch, /* match positions */
8069 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008070{
8071 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008072 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008073
Bram Moolenaarb3414592014-06-17 17:48:32 +02008074 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8075 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008076 llpos_T *pos = &posmatch->pos[i];
8077
8078 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008079 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008080 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008081 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008082 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008083 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008084 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008085 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008086 /* if this match comes before the one at "found" then swap
8087 * them */
8088 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008089 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008090 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008091
Bram Moolenaar85077472016-10-16 14:35:48 +02008092 *pos = posmatch->pos[found];
8093 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008094 }
8095 }
8096 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008097 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008098 }
8099 }
8100 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008101 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008102 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008103 colnr_T start = posmatch->pos[found].col == 0
8104 ? 0 : posmatch->pos[found].col - 1;
8105 colnr_T end = posmatch->pos[found].col == 0
8106 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008107
Bram Moolenaar85077472016-10-16 14:35:48 +02008108 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008109 shl->rm.startpos[0].lnum = 0;
8110 shl->rm.startpos[0].col = start;
8111 shl->rm.endpos[0].lnum = 0;
8112 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008113 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008114 posmatch->cur = found + 1;
8115 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008116 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008117 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008118}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008119#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008120
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008122screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 attrentry_T *aep = NULL;
8125
8126 screen_attr = attr;
8127 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008128#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 && termcap_active
8130#endif
8131 )
8132 {
8133#ifdef FEAT_GUI
8134 if (gui.in_use)
8135 {
8136 char buf[20];
8137
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008138 /* The GUI handles this internally. */
8139 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 OUT_STR(buf);
8141 }
8142 else
8143#endif
8144 {
8145 if (attr > HL_ALL) /* special HL attr. */
8146 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008147 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 aep = syn_cterm_attr2entry(attr);
8149 else
8150 aep = syn_term_attr2entry(attr);
8151 if (aep == NULL) /* did ":syntax clear" */
8152 attr = 0;
8153 else
8154 attr = aep->ae_attr;
8155 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008156 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008158 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008159#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008160 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8161 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8162 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008163#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008164 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008165 /* If the Normal FG color has BOLD attribute and the new HL
8166 * has a FG color defined, clear BOLD. */
8167 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008168 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008170 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008171 out_str(T_UCS);
8172 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008173 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8174 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008176 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008178 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008180 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008181 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182
8183 /*
8184 * Output the color or start string after bold etc., in case the
8185 * bold etc. override the color setting.
8186 */
8187 if (aep != NULL)
8188 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008189#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008190 /* When 'termguicolors' is set but fg or bg is unset,
8191 * fall back to the cterm colors. This helps for SpellBad,
8192 * where the GUI uses a red undercurl. */
8193 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008195 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008196 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008197 }
8198 else
8199#endif
8200 if (t_colors > 1)
8201 {
8202 if (aep->ae_u.cterm.fg_color)
8203 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8204 }
8205#ifdef FEAT_TERMGUICOLORS
8206 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8207 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008208 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008209 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 }
8211 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008212#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008213 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008215 if (aep->ae_u.cterm.bg_color)
8216 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8217 }
8218
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008219 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008220 {
8221 if (aep->ae_u.term.start != NULL)
8222 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
8224 }
8225 }
8226 }
8227}
8228
8229 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008230screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231{
8232 int do_ME = FALSE; /* output T_ME code */
8233
8234 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008235#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 && termcap_active
8237#endif
8238 )
8239 {
8240#ifdef FEAT_GUI
8241 if (gui.in_use)
8242 {
8243 char buf[20];
8244
8245 /* use internal GUI code */
8246 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8247 OUT_STR(buf);
8248 }
8249 else
8250#endif
8251 {
8252 if (screen_attr > HL_ALL) /* special HL attr. */
8253 {
8254 attrentry_T *aep;
8255
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008256 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
8258 /*
8259 * Assume that t_me restores the original colors!
8260 */
8261 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008262 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008263#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008264 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8265 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8266 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008267#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008268 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008269#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008270 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8271 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8272 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008273#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008274 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 do_ME = TRUE;
8276 }
8277 else
8278 {
8279 aep = syn_term_attr2entry(screen_attr);
8280 if (aep != NULL && aep->ae_u.term.stop != NULL)
8281 {
8282 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8283 do_ME = TRUE;
8284 else
8285 out_str(aep->ae_u.term.stop);
8286 }
8287 }
8288 if (aep == NULL) /* did ":syntax clear" */
8289 screen_attr = 0;
8290 else
8291 screen_attr = aep->ae_attr;
8292 }
8293
8294 /*
8295 * Often all ending-codes are equal to T_ME. Avoid outputting the
8296 * same sequence several times.
8297 */
8298 if (screen_attr & HL_STANDOUT)
8299 {
8300 if (STRCMP(T_SE, T_ME) == 0)
8301 do_ME = TRUE;
8302 else
8303 out_str(T_SE);
8304 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008305 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008306 {
8307 if (STRCMP(T_UCE, T_ME) == 0)
8308 do_ME = TRUE;
8309 else
8310 out_str(T_UCE);
8311 }
8312 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008313 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
8315 if (STRCMP(T_UE, T_ME) == 0)
8316 do_ME = TRUE;
8317 else
8318 out_str(T_UE);
8319 }
8320 if (screen_attr & HL_ITALIC)
8321 {
8322 if (STRCMP(T_CZR, T_ME) == 0)
8323 do_ME = TRUE;
8324 else
8325 out_str(T_CZR);
8326 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008327 if (screen_attr & HL_STRIKETHROUGH)
8328 {
8329 if (STRCMP(T_STE, T_ME) == 0)
8330 do_ME = TRUE;
8331 else
8332 out_str(T_STE);
8333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8335 out_str(T_ME);
8336
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008337#ifdef FEAT_TERMGUICOLORS
8338 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008340 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008341 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008342 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008343 term_bg_rgb_color(cterm_normal_bg_gui_color);
8344 }
8345 else
8346#endif
8347 {
8348 if (t_colors > 1)
8349 {
8350 /* set Normal cterm colors */
8351 if (cterm_normal_fg_color != 0)
8352 term_fg_color(cterm_normal_fg_color - 1);
8353 if (cterm_normal_bg_color != 0)
8354 term_bg_color(cterm_normal_bg_color - 1);
8355 if (cterm_normal_fg_bold)
8356 out_str(T_MD);
8357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 }
8359 }
8360 }
8361 screen_attr = 0;
8362}
8363
8364/*
8365 * Reset the colors for a cterm. Used when leaving Vim.
8366 * The machine specific code may override this again.
8367 */
8368 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008369reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008371 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {
8373 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008374#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008375 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8376 || cterm_normal_bg_gui_color != INVALCOLOR)
8377 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008378#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {
8382 out_str(T_OP);
8383 screen_attr = -1;
8384 }
8385 if (cterm_normal_fg_bold)
8386 {
8387 out_str(T_ME);
8388 screen_attr = -1;
8389 }
8390 }
8391}
8392
8393/*
8394 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8395 * using the attributes from ScreenAttrs["off"].
8396 */
8397 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008398screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 int attr;
8401
8402 /* Check for illegal values, just in case (could happen just after
8403 * resizing). */
8404 if (row >= screen_Rows || col >= screen_Columns)
8405 return;
8406
Bram Moolenaarae654382019-01-17 21:09:05 +01008407#ifdef FEAT_INS_EXPAND
8408 if (pum_under_menu(row, col))
8409 return;
8410#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008411 /* Outputting a character in the last cell on the screen may scroll the
8412 * screen up. Only do it when the "xn" termcap property is set, otherwise
8413 * mark the character invalid (update it when scrolled up). */
8414 if (*T_XN == NUL
8415 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#ifdef FEAT_RIGHTLEFT
8417 /* account for first command-line character in rightleft mode */
8418 && !cmdmsg_rl
8419#endif
8420 )
8421 {
8422 ScreenAttrs[off] = (sattr_T)-1;
8423 return;
8424 }
8425
8426 /*
8427 * Stop highlighting first, so it's easier to move the cursor.
8428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 if (screen_char_attr != 0)
8430 attr = screen_char_attr;
8431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 attr = ScreenAttrs[off];
8433 if (screen_attr != attr)
8434 screen_stop_highlight();
8435
8436 windgoto(row, col);
8437
8438 if (screen_attr != attr)
8439 screen_start_highlight(attr);
8440
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 if (enc_utf8 && ScreenLinesUC[off] != 0)
8442 {
8443 char_u buf[MB_MAXBYTES + 1];
8444
Bram Moolenaarcb070082016-04-02 22:14:51 +02008445 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008446 {
8447 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008448#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008449 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008450#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008451 )
8452 {
8453 /* Clear the two screen cells. If the character is actually
8454 * single width it won't change the second cell. */
8455 out_str((char_u *)" ");
8456 term_windgoto(row, col);
8457 }
8458 /* not sure where the cursor is after drawing the ambiguous width
8459 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008460 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008461 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008462 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008464
8465 /* Convert the UTF-8 character to bytes and write it. */
8466 buf[utfc_char2bytes(off, buf)] = NUL;
8467 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 }
8469 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 /* double-byte character in single-width cell */
8474 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8475 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 }
8477
8478 screen_cur_col++;
8479}
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481/*
8482 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8483 * on the screen at position 'row' and 'col'.
8484 * The attributes of the first byte is used for all. This is required to
8485 * output the two bytes of a double-byte character with nothing in between.
8486 */
8487 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008488screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489{
8490 /* Check for illegal values (could be wrong when screen was resized). */
8491 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8492 return;
8493
8494 /* Outputting the last character on the screen may scrollup the screen.
8495 * Don't to it! Mark the character invalid (update it when scrolled up) */
8496 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8497 {
8498 ScreenAttrs[off] = (sattr_T)-1;
8499 return;
8500 }
8501
8502 /* Output the first byte normally (positions the cursor), then write the
8503 * second byte directly. */
8504 screen_char(off, row, col);
8505 out_char(ScreenLines[off + 1]);
8506 ++screen_cur_col;
8507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509/*
8510 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8511 * This uses the contents of ScreenLines[] and doesn't change it.
8512 */
8513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008514screen_draw_rectangle(
8515 int row,
8516 int col,
8517 int height,
8518 int width,
8519 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520{
8521 int r, c;
8522 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008523 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008525 /* Can't use ScreenLines unless initialized */
8526 if (ScreenLines == NULL)
8527 return;
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (invert)
8530 screen_char_attr = HL_INVERSE;
8531 for (r = row; r < row + height; ++r)
8532 {
8533 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008534 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 for (c = col; c < col + width; ++c)
8536 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008537 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {
8539 screen_char_2(off + c, r, c);
8540 ++c;
8541 }
8542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {
8544 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008545 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548 }
8549 }
8550 screen_char_attr = 0;
8551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553/*
8554 * Redraw the characters for a vertically split window.
8555 */
8556 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008557redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 int col;
8560 int width;
8561
8562# ifdef FEAT_CLIPBOARD
8563 clip_may_clear_selection(row, end - 1);
8564# endif
8565
8566 if (wp == NULL)
8567 {
8568 col = 0;
8569 width = Columns;
8570 }
8571 else
8572 {
8573 col = wp->w_wincol;
8574 width = wp->w_width;
8575 }
8576 screen_draw_rectangle(row, col, end - row, width, FALSE);
8577}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008579 static void
8580space_to_screenline(int off, int attr)
8581{
8582 ScreenLines[off] = ' ';
8583 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008584 if (enc_utf8)
8585 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008586}
8587
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588/*
8589 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8590 * with character 'c1' in first column followed by 'c2' in the other columns.
8591 * Use attributes 'attr'.
8592 */
8593 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008594screen_fill(
8595 int start_row,
8596 int end_row,
8597 int start_col,
8598 int end_col,
8599 int c1,
8600 int c2,
8601 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 int row;
8604 int col;
8605 int off;
8606 int end_off;
8607 int did_delete;
8608 int c;
8609 int norm_term;
8610#if defined(FEAT_GUI) || defined(UNIX)
8611 int force_next = FALSE;
8612#endif
8613
8614 if (end_row > screen_Rows) /* safety check */
8615 end_row = screen_Rows;
8616 if (end_col > screen_Columns) /* safety check */
8617 end_col = screen_Columns;
8618 if (ScreenLines == NULL
8619 || start_row >= end_row
8620 || start_col >= end_col) /* nothing to do */
8621 return;
8622
8623 /* it's a "normal" terminal when not in a GUI or cterm */
8624 norm_term = (
8625#ifdef FEAT_GUI
8626 !gui.in_use &&
8627#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008628 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 for (row = start_row; row < end_row; ++row)
8630 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008631 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008632#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008633 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008634#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008635 )
8636 {
8637 /* When drawing over the right halve of a double-wide char clear
8638 * out the left halve. When drawing over the left halve of a
8639 * double wide-char clear out the right halve. Only needed in a
8640 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008641 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008642 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008643 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008644 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 /*
8647 * Try to use delete-line termcap code, when no attributes or in a
8648 * "normal" terminal, where a bold/italic space is just a
8649 * space.
8650 */
8651 did_delete = FALSE;
8652 if (c2 == ' '
8653 && end_col == Columns
8654 && can_clear(T_CE)
8655 && (attr == 0
8656 || (norm_term
8657 && attr <= HL_ALL
8658 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8659 {
8660 /*
8661 * check if we really need to clear something
8662 */
8663 col = start_col;
8664 if (c1 != ' ') /* don't clear first char */
8665 ++col;
8666
8667 off = LineOffset[row] + col;
8668 end_off = LineOffset[row] + end_col;
8669
8670 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 if (enc_utf8)
8672 while (off < end_off && ScreenLines[off] == ' '
8673 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8674 ++off;
8675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 while (off < end_off && ScreenLines[off] == ' '
8677 && ScreenAttrs[off] == 0)
8678 ++off;
8679 if (off < end_off) /* something to be cleared */
8680 {
8681 col = off - LineOffset[row];
8682 screen_stop_highlight();
8683 term_windgoto(row, col);/* clear rest of this screen line */
8684 out_str(T_CE);
8685 screen_start(); /* don't know where cursor is now */
8686 col = end_col - col;
8687 while (col--) /* clear chars in ScreenLines */
8688 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008689 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 ++off;
8691 }
8692 }
8693 did_delete = TRUE; /* the chars are cleared now */
8694 }
8695
8696 off = LineOffset[row] + start_col;
8697 c = c1;
8698 for (col = start_col; col < end_col; ++col)
8699 {
8700 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008701 || (enc_utf8 && (int)ScreenLinesUC[off]
8702 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 || ScreenAttrs[off] != attr
8704#if defined(FEAT_GUI) || defined(UNIX)
8705 || force_next
8706#endif
8707 )
8708 {
8709#if defined(FEAT_GUI) || defined(UNIX)
8710 /* The bold trick may make a single row of pixels appear in
8711 * the next character. When a bold character is removed, the
8712 * next character should be redrawn too. This happens for our
8713 * own GUI and for some xterms. */
8714 if (
8715# ifdef FEAT_GUI
8716 gui.in_use
8717# endif
8718# if defined(FEAT_GUI) && defined(UNIX)
8719 ||
8720# endif
8721# ifdef UNIX
8722 term_is_xterm
8723# endif
8724 )
8725 {
8726 if (ScreenLines[off] != ' '
8727 && (ScreenAttrs[off] > HL_ALL
8728 || ScreenAttrs[off] & HL_BOLD))
8729 force_next = TRUE;
8730 else
8731 force_next = FALSE;
8732 }
8733#endif
8734 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 if (enc_utf8)
8736 {
8737 if (c >= 0x80)
8738 {
8739 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008740 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 }
8742 else
8743 ScreenLinesUC[off] = 0;
8744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 ScreenAttrs[off] = attr;
8746 if (!did_delete || c != ' ')
8747 screen_char(off, row, col);
8748 }
8749 ++off;
8750 if (col == start_col)
8751 {
8752 if (did_delete)
8753 break;
8754 c = c2;
8755 }
8756 }
8757 if (end_col == Columns)
8758 LineWraps[row] = FALSE;
8759 if (row == Rows - 1) /* overwritten the command line */
8760 {
8761 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008762 if (start_col == 0 && end_col == Columns
8763 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008765 if (start_col == 0)
8766 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 }
8768 }
8769}
8770
8771/*
8772 * Check if there should be a delay. Used before clearing or redrawing the
8773 * screen or the command line.
8774 */
8775 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008776check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777{
8778 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8779 && !did_wait_return
8780 && emsg_silent == 0)
8781 {
8782 out_flush();
8783 ui_delay(1000L, TRUE);
8784 emsg_on_display = FALSE;
8785 if (check_msg_scroll)
8786 msg_scroll = FALSE;
8787 }
8788}
8789
8790/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008791 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8792 */
8793 static void
8794clear_TabPageIdxs(void)
8795{
8796 int scol;
8797
8798 for (scol = 0; scol < Columns; ++scol)
8799 TabPageIdxs[scol] = 0;
8800}
8801
8802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008804 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 * Returns TRUE if there is a valid screen to write to.
8806 * Returns FALSE when starting up and screen not initialized yet.
8807 */
8808 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008809screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008811 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 return (ScreenLines != NULL);
8813}
8814
8815/*
8816 * Resize the shell to Rows and Columns.
8817 * Allocate ScreenLines[] and associated items.
8818 *
8819 * There may be some time between setting Rows and Columns and (re)allocating
8820 * ScreenLines[]. This happens when starting up and when (manually) changing
8821 * the shell size. Always use screen_Rows and screen_Columns to access items
8822 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8823 * final size of the shell is needed.
8824 */
8825 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008826screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 int new_row, old_row;
8829#ifdef FEAT_GUI
8830 int old_Rows;
8831#endif
8832 win_T *wp;
8833 int outofmem = FALSE;
8834 int len;
8835 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008837 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008839 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 sattr_T *new_ScreenAttrs;
8841 unsigned *new_LineOffset;
8842 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008843 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008844 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008846 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008847 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008849retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 /*
8851 * Allocation of the screen buffers is done only when the size changes and
8852 * when Rows and Columns have been set and we have started doing full
8853 * screen stuff.
8854 */
8855 if ((ScreenLines != NULL
8856 && Rows == screen_Rows
8857 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 && enc_utf8 == (ScreenLinesUC != NULL)
8859 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008860 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 || Rows == 0
8862 || Columns == 0
8863 || (!full_screen && ScreenLines == NULL))
8864 return;
8865
8866 /*
8867 * It's possible that we produce an out-of-memory message below, which
8868 * will cause this function to be called again. To break the loop, just
8869 * return here.
8870 */
8871 if (entered)
8872 return;
8873 entered = TRUE;
8874
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008875 /*
8876 * Note that the window sizes are updated before reallocating the arrays,
8877 * thus we must not redraw here!
8878 */
8879 ++RedrawingDisabled;
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 win_new_shellsize(); /* fit the windows in the new sized shell */
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 comp_col(); /* recompute columns for shown command and ruler */
8884
8885 /*
8886 * We're changing the size of the screen.
8887 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8888 * - Move lines from the old arrays into the new arrays, clear extra
8889 * lines (unless the screen is going to be cleared).
8890 * - Free the old arrays.
8891 *
8892 * If anything fails, make ScreenLines NULL, so we don't do anything!
8893 * Continuing with the old ScreenLines may result in a crash, because the
8894 * size is wrong.
8895 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008896 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008898 if (aucmd_win != NULL)
8899 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008901 new_ScreenLines = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008902 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (enc_utf8)
8904 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008905 new_ScreenLinesUC = LALLOC_MULT(u8char_T, (Rows + 1) * Columns);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008906 for (i = 0; i < p_mco; ++i)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008907 new_ScreenLinesC[i] = LALLOC_CLEAR_MULT(u8char_T,
8908 (Rows + 1) * Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 }
8910 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008911 new_ScreenLines2 = LALLOC_MULT(schar_T, (Rows + 1) * Columns);
8912 new_ScreenAttrs = LALLOC_MULT(sattr_T, (Rows + 1) * Columns);
8913 new_LineOffset = LALLOC_MULT(unsigned, Rows);
8914 new_LineWraps = LALLOC_MULT(char_u, Rows);
8915 new_TabPageIdxs = LALLOC_MULT(short, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008917 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
8919 if (win_alloc_lines(wp) == FAIL)
8920 {
8921 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008922 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 }
8924 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008925 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8926 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008927 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008928give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 for (i = 0; i < p_mco; ++i)
8931 if (new_ScreenLinesC[i] == NULL)
8932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008934 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 || new_ScreenAttrs == NULL
8937 || new_LineOffset == NULL
8938 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008939 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 || outofmem)
8941 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008942 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008943 {
8944 /* guess the size */
8945 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8946
8947 /* Remember we did this to avoid getting outofmem messages over
8948 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008949 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008950 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008951 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008952 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008953 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008954 VIM_CLEAR(new_ScreenLinesC[i]);
8955 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008956 VIM_CLEAR(new_ScreenAttrs);
8957 VIM_CLEAR(new_LineOffset);
8958 VIM_CLEAR(new_LineWraps);
8959 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 }
8961 else
8962 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008963 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 for (new_row = 0; new_row < Rows; ++new_row)
8966 {
8967 new_LineOffset[new_row] = new_row * Columns;
8968 new_LineWraps[new_row] = FALSE;
8969
8970 /*
8971 * If the screen is not going to be cleared, copy as much as
8972 * possible from the old screen to the new one and clear the rest
8973 * (used when resizing the window at the "--more--" prompt or when
8974 * executing an external command, for the GUI).
8975 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008976 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 {
8978 (void)vim_memset(new_ScreenLines + new_row * Columns,
8979 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 if (enc_utf8)
8981 {
8982 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8983 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008984 for (i = 0; i < p_mco; ++i)
8985 (void)vim_memset(new_ScreenLinesC[i]
8986 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 0, (size_t)Columns * sizeof(u8char_T));
8988 }
8989 if (enc_dbcs == DBCS_JPNU)
8990 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8991 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
8993 0, (size_t)Columns * sizeof(sattr_T));
8994 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008995 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 {
8997 if (screen_Columns < Columns)
8998 len = screen_Columns;
8999 else
9000 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009001 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009002 * may be invalid now. Also when p_mco changes. */
9003 if (!(enc_utf8 && ScreenLinesUC == NULL)
9004 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009005 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9006 ScreenLines + LineOffset[old_row],
9007 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009008 if (enc_utf8 && ScreenLinesUC != NULL
9009 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 {
9011 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9012 ScreenLinesUC + LineOffset[old_row],
9013 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009014 for (i = 0; i < p_mco; ++i)
9015 mch_memmove(new_ScreenLinesC[i]
9016 + new_LineOffset[new_row],
9017 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 (size_t)len * sizeof(u8char_T));
9019 }
9020 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9021 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9022 ScreenLines2 + LineOffset[old_row],
9023 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9025 ScreenAttrs + LineOffset[old_row],
9026 (size_t)len * sizeof(sattr_T));
9027 }
9028 }
9029 }
9030 /* Use the last line of the screen for the current line. */
9031 current_ScreenLine = new_ScreenLines + Rows * Columns;
9032 }
9033
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009034 free_screenlines();
9035
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009038 for (i = 0; i < p_mco; ++i)
9039 ScreenLinesC[i] = new_ScreenLinesC[i];
9040 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 ScreenAttrs = new_ScreenAttrs;
9043 LineOffset = new_LineOffset;
9044 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009045 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046
9047 /* It's important that screen_Rows and screen_Columns reflect the actual
9048 * size of ScreenLines[]. Set them before calling anything. */
9049#ifdef FEAT_GUI
9050 old_Rows = screen_Rows;
9051#endif
9052 screen_Rows = Rows;
9053 screen_Columns = Columns;
9054
9055 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009056 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058#ifdef FEAT_GUI
9059 else if (gui.in_use
9060 && !gui.starting
9061 && ScreenLines != NULL
9062 && old_Rows != Rows)
9063 {
9064 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9065 /*
9066 * Adjust the position of the cursor, for when executing an external
9067 * command.
9068 */
9069 if (msg_row >= Rows) /* Rows got smaller */
9070 msg_row = Rows - 1; /* put cursor at last row */
9071 else if (Rows > old_Rows) /* Rows got bigger */
9072 msg_row += Rows - old_Rows; /* put cursor in same place */
9073 if (msg_col >= Columns) /* Columns got smaller */
9074 msg_col = Columns - 1; /* put cursor at last column */
9075 }
9076#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009077 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009080 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009081
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009082 /*
9083 * Do not apply autocommands more than 3 times to avoid an endless loop
9084 * in case applying autocommands always changes Rows or Columns.
9085 */
9086 if (starting == 0 && ++retry_count <= 3)
9087 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009088 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009089 /* In rare cases, autocommands may have altered Rows or Columns,
9090 * jump back to check if we need to allocate the screen again. */
9091 goto retry;
9092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093}
9094
9095 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009096free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009097{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009098 int i;
9099
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009100 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009101 for (i = 0; i < Screen_mco; ++i)
9102 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009103 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009104 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009105 vim_free(ScreenAttrs);
9106 vim_free(LineOffset);
9107 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009108 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009109}
9110
9111 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009112screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113{
9114 check_for_delay(FALSE);
9115 screenalloc(FALSE); /* allocate screen buffers if size changed */
9116 screenclear2(); /* clear the screen */
9117}
9118
9119 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009120screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121{
9122 int i;
9123
9124 if (starting == NO_SCREEN || ScreenLines == NULL
9125#ifdef FEAT_GUI
9126 || (gui.in_use && gui.starting)
9127#endif
9128 )
9129 return;
9130
9131#ifdef FEAT_GUI
9132 if (!gui.in_use)
9133#endif
9134 screen_attr = -1; /* force setting the Normal colors */
9135 screen_stop_highlight(); /* don't want highlighting here */
9136
9137#ifdef FEAT_CLIPBOARD
9138 /* disable selection without redrawing it */
9139 clip_scroll_selection(9999);
9140#endif
9141
9142 /* blank out ScreenLines */
9143 for (i = 0; i < Rows; ++i)
9144 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009145 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 LineWraps[i] = FALSE;
9147 }
9148
9149 if (can_clear(T_CL))
9150 {
9151 out_str(T_CL); /* clear the display */
9152 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009153 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 }
9155 else
9156 {
9157 /* can't clear the screen, mark all chars with invalid attributes */
9158 for (i = 0; i < Rows; ++i)
9159 lineinvalid(LineOffset[i], (int)Columns);
9160 clear_cmdline = TRUE;
9161 }
9162
9163 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9164
9165 win_rest_invalid(firstwin);
9166 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009167 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 if (must_redraw == CLEAR) /* no need to clear again */
9169 must_redraw = NOT_VALID;
9170 compute_cmdrow();
9171 msg_row = cmdline_row; /* put cursor on last line for messages */
9172 msg_col = 0;
9173 screen_start(); /* don't know where cursor is now */
9174 msg_scrolled = 0; /* can't scroll back */
9175 msg_didany = FALSE;
9176 msg_didout = FALSE;
9177}
9178
9179/*
9180 * Clear one line in ScreenLines.
9181 */
9182 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009183lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 if (enc_utf8)
9187 (void)vim_memset(ScreenLinesUC + off, 0,
9188 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009189 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
9193 * Mark one line in ScreenLines invalid by setting the attributes to an
9194 * invalid value.
9195 */
9196 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009197lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9200}
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202/*
9203 * Copy part of a Screenline for vertically split window "wp".
9204 */
9205 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009206linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
9208 unsigned off_to = LineOffset[to] + wp->w_wincol;
9209 unsigned off_from = LineOffset[from] + wp->w_wincol;
9210
9211 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9212 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 if (enc_utf8)
9214 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009215 int i;
9216
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9218 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009219 for (i = 0; i < p_mco; ++i)
9220 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9221 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 }
9223 if (enc_dbcs == DBCS_JPNU)
9224 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9225 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9227 wp->w_width * sizeof(sattr_T));
9228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
9230/*
9231 * Return TRUE if clearing with term string "p" would work.
9232 * It can't work when the string is empty or it won't set the right background.
9233 */
9234 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009235can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236{
9237 return (*p != NUL && (t_colors <= 1
9238#ifdef FEAT_GUI
9239 || gui.in_use
9240#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009241#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009242 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009243 || (!p_tgc && cterm_normal_bg_color == 0)
9244#else
9245 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009246#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009247 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248}
9249
9250/*
9251 * Reset cursor position. Use whenever cursor was moved because of outputting
9252 * something directly to the screen (shell commands) or a terminal control
9253 * code.
9254 */
9255 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009256screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257{
9258 screen_cur_row = screen_cur_col = 9999;
9259}
9260
9261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 * Move the cursor to position "row","col" in the screen.
9263 * This tries to find the most efficient way to move, minimizing the number of
9264 * characters sent to the terminal.
9265 */
9266 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009267windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009269 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 int i;
9271 int plan;
9272 int cost;
9273 int wouldbe_col;
9274 int noinvcurs;
9275 char_u *bs;
9276 int goto_cost;
9277 int attr;
9278
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009279#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9281
9282#define PLAN_LE 1
9283#define PLAN_CR 2
9284#define PLAN_NL 3
9285#define PLAN_WRITE 4
9286 /* Can't use ScreenLines unless initialized */
9287 if (ScreenLines == NULL)
9288 return;
9289
9290 if (col != screen_cur_col || row != screen_cur_row)
9291 {
9292 /* Check for valid position. */
9293 if (row < 0) /* window without text lines? */
9294 row = 0;
9295 if (row >= screen_Rows)
9296 row = screen_Rows - 1;
9297 if (col >= screen_Columns)
9298 col = screen_Columns - 1;
9299
9300 /* check if no cursor movement is allowed in highlight mode */
9301 if (screen_attr && *T_MS == NUL)
9302 noinvcurs = HIGHL_COST;
9303 else
9304 noinvcurs = 0;
9305 goto_cost = GOTO_COST + noinvcurs;
9306
9307 /*
9308 * Plan how to do the positioning:
9309 * 1. Use CR to move it to column 0, same row.
9310 * 2. Use T_LE to move it a few columns to the left.
9311 * 3. Use NL to move a few lines down, column 0.
9312 * 4. Move a few columns to the right with T_ND or by writing chars.
9313 *
9314 * Don't do this if the cursor went beyond the last column, the cursor
9315 * position is unknown then (some terminals wrap, some don't )
9316 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009317 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 * characters to move the cursor to the right.
9319 */
9320 if (row >= screen_cur_row && screen_cur_col < Columns)
9321 {
9322 /*
9323 * If the cursor is in the same row, bigger col, we can use CR
9324 * or T_LE.
9325 */
9326 bs = NULL; /* init for GCC */
9327 attr = screen_attr;
9328 if (row == screen_cur_row && col < screen_cur_col)
9329 {
9330 /* "le" is preferred over "bc", because "bc" is obsolete */
9331 if (*T_LE)
9332 bs = T_LE; /* "cursor left" */
9333 else
9334 bs = T_BC; /* "backspace character (old) */
9335 if (*bs)
9336 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9337 else
9338 cost = 999;
9339 if (col + 1 < cost) /* using CR is less characters */
9340 {
9341 plan = PLAN_CR;
9342 wouldbe_col = 0;
9343 cost = 1; /* CR is just one character */
9344 }
9345 else
9346 {
9347 plan = PLAN_LE;
9348 wouldbe_col = col;
9349 }
9350 if (noinvcurs) /* will stop highlighting */
9351 {
9352 cost += noinvcurs;
9353 attr = 0;
9354 }
9355 }
9356
9357 /*
9358 * If the cursor is above where we want to be, we can use CR LF.
9359 */
9360 else if (row > screen_cur_row)
9361 {
9362 plan = PLAN_NL;
9363 wouldbe_col = 0;
9364 cost = (row - screen_cur_row) * 2; /* CR LF */
9365 if (noinvcurs) /* will stop highlighting */
9366 {
9367 cost += noinvcurs;
9368 attr = 0;
9369 }
9370 }
9371
9372 /*
9373 * If the cursor is in the same row, smaller col, just use write.
9374 */
9375 else
9376 {
9377 plan = PLAN_WRITE;
9378 wouldbe_col = screen_cur_col;
9379 cost = 0;
9380 }
9381
9382 /*
9383 * Check if any characters that need to be written have the
9384 * correct attributes. Also avoid UTF-8 characters.
9385 */
9386 i = col - wouldbe_col;
9387 if (i > 0)
9388 cost += i;
9389 if (cost < goto_cost && i > 0)
9390 {
9391 /*
9392 * Check if the attributes are correct without additionally
9393 * stopping highlighting.
9394 */
9395 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9396 while (i && *p++ == attr)
9397 --i;
9398 if (i != 0)
9399 {
9400 /*
9401 * Try if it works when highlighting is stopped here.
9402 */
9403 if (*--p == 0)
9404 {
9405 cost += noinvcurs;
9406 while (i && *p++ == 0)
9407 --i;
9408 }
9409 if (i != 0)
9410 cost = 999; /* different attributes, don't do it */
9411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 if (enc_utf8)
9413 {
9414 /* Don't use an UTF-8 char for positioning, it's slow. */
9415 for (i = wouldbe_col; i < col; ++i)
9416 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9417 {
9418 cost = 999;
9419 break;
9420 }
9421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 }
9423
9424 /*
9425 * We can do it without term_windgoto()!
9426 */
9427 if (cost < goto_cost)
9428 {
9429 if (plan == PLAN_LE)
9430 {
9431 if (noinvcurs)
9432 screen_stop_highlight();
9433 while (screen_cur_col > col)
9434 {
9435 out_str(bs);
9436 --screen_cur_col;
9437 }
9438 }
9439 else if (plan == PLAN_CR)
9440 {
9441 if (noinvcurs)
9442 screen_stop_highlight();
9443 out_char('\r');
9444 screen_cur_col = 0;
9445 }
9446 else if (plan == PLAN_NL)
9447 {
9448 if (noinvcurs)
9449 screen_stop_highlight();
9450 while (screen_cur_row < row)
9451 {
9452 out_char('\n');
9453 ++screen_cur_row;
9454 }
9455 screen_cur_col = 0;
9456 }
9457
9458 i = col - screen_cur_col;
9459 if (i > 0)
9460 {
9461 /*
9462 * Use cursor-right if it's one character only. Avoids
9463 * removing a line of pixels from the last bold char, when
9464 * using the bold trick in the GUI.
9465 */
9466 if (T_ND[0] != NUL && T_ND[1] == NUL)
9467 {
9468 while (i-- > 0)
9469 out_char(*T_ND);
9470 }
9471 else
9472 {
9473 int off;
9474
9475 off = LineOffset[row] + screen_cur_col;
9476 while (i-- > 0)
9477 {
9478 if (ScreenAttrs[off] != screen_attr)
9479 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 if (enc_dbcs == DBCS_JPNU
9483 && ScreenLines[off] == 0x8e)
9484 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 ++off;
9486 }
9487 }
9488 }
9489 }
9490 }
9491 else
9492 cost = 999;
9493
9494 if (cost >= goto_cost)
9495 {
9496 if (noinvcurs)
9497 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009498 if (row == screen_cur_row && (col > screen_cur_col)
9499 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 term_cursor_right(col - screen_cur_col);
9501 else
9502 term_windgoto(row, col);
9503 }
9504 screen_cur_row = row;
9505 screen_cur_col = col;
9506 }
9507}
9508
9509/*
9510 * Set cursor to its position in the current window.
9511 */
9512 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009513setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009515 setcursor_mayforce(FALSE);
9516}
9517
9518/*
9519 * Set cursor to its position in the current window.
9520 * When "force" is TRUE also when not redrawing.
9521 */
9522 void
9523setcursor_mayforce(int force)
9524{
9525 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 {
9527 validate_cursor();
9528 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009529 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009531 /* With 'rightleft' set and the cursor on a double-wide
9532 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009533 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9534 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009535 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009536 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#endif
9538 curwin->w_wcol));
9539 }
9540}
9541
9542
9543/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009544 * Insert 'line_count' lines at 'row' in window 'wp'.
9545 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9546 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 * scrolling.
9548 * Returns FAIL if the lines are not inserted, OK for success.
9549 */
9550 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009551win_ins_lines(
9552 win_T *wp,
9553 int row,
9554 int line_count,
9555 int invalid,
9556 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 int did_delete;
9559 int nextrow;
9560 int lastrow;
9561 int retval;
9562
9563 if (invalid)
9564 wp->w_lines_valid = 0;
9565
9566 if (wp->w_height < 5)
9567 return FAIL;
9568
9569 if (line_count > wp->w_height - row)
9570 line_count = wp->w_height - row;
9571
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009572 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 if (retval != MAYBE)
9574 return retval;
9575
9576 /*
9577 * If there is a next window or a status line, we first try to delete the
9578 * lines at the bottom to avoid messing what is after the window.
9579 * If this fails and there are following windows, don't do anything to avoid
9580 * messing up those windows, better just redraw.
9581 */
9582 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 if (wp->w_next != NULL || wp->w_status_height)
9584 {
9585 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009586 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 did_delete = TRUE;
9588 else if (wp->w_next)
9589 return FAIL;
9590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 /*
9592 * if no lines deleted, blank the lines that will end up below the window
9593 */
9594 if (!did_delete)
9595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009598 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 lastrow = nextrow + line_count;
9600 if (lastrow > Rows)
9601 lastrow = Rows;
9602 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009603 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 ' ', ' ', 0);
9605 }
9606
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009607 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 == FAIL)
9609 {
9610 /* deletion will have messed up other windows */
9611 if (did_delete)
9612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 win_rest_invalid(W_NEXT(wp));
9615 }
9616 return FAIL;
9617 }
9618
9619 return OK;
9620}
9621
9622/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009623 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9625 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9626 * scrolling
9627 * Return OK for success, FAIL if the lines are not deleted.
9628 */
9629 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009630win_del_lines(
9631 win_T *wp,
9632 int row,
9633 int line_count,
9634 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009635 int mayclear,
9636 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638 int retval;
9639
9640 if (invalid)
9641 wp->w_lines_valid = 0;
9642
9643 if (line_count > wp->w_height - row)
9644 line_count = wp->w_height - row;
9645
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009646 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 if (retval != MAYBE)
9648 return retval;
9649
9650 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009651 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 return FAIL;
9653
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 /*
9655 * If there are windows or status lines below, try to put them at the
9656 * correct place. If we can't do that, they have to be redrawn.
9657 */
9658 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9659 {
9660 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009661 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
9663 wp->w_redr_status = TRUE;
9664 win_rest_invalid(wp->w_next);
9665 }
9666 }
9667 /*
9668 * If this is the last window and there is no status line, redraw the
9669 * command line later.
9670 */
9671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 redraw_cmdline = TRUE;
9673 return OK;
9674}
9675
9676/*
9677 * Common code for win_ins_lines() and win_del_lines().
9678 * Returns OK or FAIL when the work has been done.
9679 * Returns MAYBE when not finished yet.
9680 */
9681 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009682win_do_lines(
9683 win_T *wp,
9684 int row,
9685 int line_count,
9686 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009687 int del,
9688 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689{
9690 int retval;
9691
9692 if (!redrawing() || line_count <= 0)
9693 return FAIL;
9694
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009695 /* When inserting lines would result in loss of command output, just redraw
9696 * the lines. */
9697 if (no_win_do_lines_ins && !del)
9698 return FAIL;
9699
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009701 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009703 if (!no_win_do_lines_ins)
9704 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 return FAIL;
9706 }
9707
9708 /*
9709 * Delete all remaining lines
9710 */
9711 if (row + line_count >= wp->w_height)
9712 {
9713 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009714 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 ' ', ' ', 0);
9716 return OK;
9717 }
9718
9719 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009720 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009722 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009724 if (!no_win_do_lines_ins)
9725 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
9727 /*
9728 * If the terminal can set a scroll region, use that.
9729 * Always do this in a vertically split window. This will redraw from
9730 * ScreenLines[] when t_CV isn't defined. That's faster than using
9731 * win_line().
9732 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009733 * a character in the lower right corner of the scroll region may cause a
9734 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009736 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 scroll_region_set(wp, row);
9740 if (del)
9741 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009742 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 else
9744 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009745 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 scroll_region_reset();
9748 return retval;
9749 }
9750
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753
9754 return MAYBE;
9755}
9756
9757/*
9758 * window 'wp' and everything after it is messed up, mark it for redraw
9759 */
9760 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009761win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 {
9765 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 wp->w_redr_status = TRUE;
9767 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 }
9769 redraw_cmdline = TRUE;
9770}
9771
9772/*
9773 * The rest of the routines in this file perform screen manipulations. The
9774 * given operation is performed physically on the screen. The corresponding
9775 * change is also made to the internal screen image. In this way, the editor
9776 * anticipates the effect of editing changes on the appearance of the screen.
9777 * That way, when we call screenupdate a complete redraw isn't usually
9778 * necessary. Another advantage is that we can keep adding code to anticipate
9779 * screen changes, and in the meantime, everything still works.
9780 */
9781
9782/*
9783 * types for inserting or deleting lines
9784 */
9785#define USE_T_CAL 1
9786#define USE_T_CDL 2
9787#define USE_T_AL 3
9788#define USE_T_CE 4
9789#define USE_T_DL 5
9790#define USE_T_SR 6
9791#define USE_NL 7
9792#define USE_T_CD 8
9793#define USE_REDRAW 9
9794
9795/*
9796 * insert lines on the screen and update ScreenLines[]
9797 * 'end' is the line after the scrolled part. Normally it is Rows.
9798 * When scrolling region used 'off' is the offset from the top for the region.
9799 * 'row' and 'end' are relative to the start of the region.
9800 *
9801 * return FAIL for failure, OK for success.
9802 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009803 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009804screen_ins_lines(
9805 int off,
9806 int row,
9807 int line_count,
9808 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009809 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009810 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811{
9812 int i;
9813 int j;
9814 unsigned temp;
9815 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009816 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 int type;
9818 int result_empty;
9819 int can_ce = can_clear(T_CE);
9820
9821 /*
9822 * FAIL if
9823 * - there is no valid screen
9824 * - the screen has to be redrawn completely
9825 * - the line count is less than one
9826 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009827 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009829 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9830#ifdef FEAT_CLIPBOARD
9831 || (clip_star.state != SELECT_CLEARED
9832 && redrawing_for_callback > 0)
9833#endif
9834 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 return FAIL;
9836
9837 /*
9838 * There are seven ways to insert lines:
9839 * 0. When in a vertically split window and t_CV isn't set, redraw the
9840 * characters from ScreenLines[].
9841 * 1. Use T_CD (clear to end of display) if it exists and the result of
9842 * the insert is just empty lines
9843 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9844 * present or line_count > 1. It looks better if we do all the inserts
9845 * at once.
9846 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9847 * insert is just empty lines and T_CE is not present or line_count >
9848 * 1.
9849 * 4. Use T_AL (insert line) if it exists.
9850 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9851 * just empty lines.
9852 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9853 * just empty lines.
9854 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9855 * the 'da' flag is not set or we have clear line capability.
9856 * 8. redraw the characters from ScreenLines[].
9857 *
9858 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9859 * the scrollbar for the window. It does have insert line, use that if it
9860 * exists.
9861 */
9862 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9864 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009865 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 type = USE_T_CD;
9867 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9868 type = USE_T_CAL;
9869 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9870 type = USE_T_CDL;
9871 else if (*T_AL != NUL)
9872 type = USE_T_AL;
9873 else if (can_ce && result_empty)
9874 type = USE_T_CE;
9875 else if (*T_DL != NUL && result_empty)
9876 type = USE_T_DL;
9877 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9878 type = USE_T_SR;
9879 else
9880 return FAIL;
9881
9882 /*
9883 * For clearing the lines screen_del_lines() is used. This will also take
9884 * care of t_db if necessary.
9885 */
9886 if (type == USE_T_CD || type == USE_T_CDL ||
9887 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009888 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
9890 /*
9891 * If text is retained below the screen, first clear or delete as many
9892 * lines at the bottom of the window as are about to be inserted so that
9893 * the deleted lines won't later surface during a screen_del_lines.
9894 */
9895 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009896 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898#ifdef FEAT_CLIPBOARD
9899 /* Remove a modeless selection when inserting lines halfway the screen
9900 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009901 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009902 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 else
9904 clip_scroll_selection(-line_count);
9905#endif
9906
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907#ifdef FEAT_GUI
9908 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9909 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009910 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911#endif
9912
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009913 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9914 cursor_col = wp->w_wincol;
9915
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 if (*T_CCS != NUL) /* cursor relative to region */
9917 cursor_row = row;
9918 else
9919 cursor_row = row + off;
9920
9921 /*
9922 * Shift LineOffset[] line_count down to reflect the inserted lines.
9923 * Clear the inserted lines in ScreenLines[].
9924 */
9925 row += off;
9926 end += off;
9927 for (i = 0; i < line_count; ++i)
9928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 if (wp != NULL && wp->w_width != Columns)
9930 {
9931 /* need to copy part of a line */
9932 j = end - 1 - i;
9933 while ((j -= line_count) >= row)
9934 linecopy(j + line_count, j, wp);
9935 j += line_count;
9936 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009937 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9938 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 else
9940 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9941 LineWraps[j] = FALSE;
9942 }
9943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 {
9945 j = end - 1 - i;
9946 temp = LineOffset[j];
9947 while ((j -= line_count) >= row)
9948 {
9949 LineOffset[j + line_count] = LineOffset[j];
9950 LineWraps[j + line_count] = LineWraps[j];
9951 }
9952 LineOffset[j + line_count] = temp;
9953 LineWraps[j + line_count] = FALSE;
9954 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009955 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 else
9957 lineinvalid(temp, (int)Columns);
9958 }
9959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
9961 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009962 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009963 if (clear_attr != 0)
9964 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 /* redraw the characters */
9967 if (type == USE_REDRAW)
9968 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009969 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 {
9971 term_append_lines(line_count);
9972 screen_start(); /* don't know where cursor is now */
9973 }
9974 else
9975 {
9976 for (i = 0; i < line_count; i++)
9977 {
9978 if (type == USE_T_AL)
9979 {
9980 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009981 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 out_str(T_AL);
9983 }
9984 else /* type == USE_T_SR */
9985 out_str(T_SR);
9986 screen_start(); /* don't know where cursor is now */
9987 }
9988 }
9989
9990 /*
9991 * With scroll-reverse and 'da' flag set we need to clear the lines that
9992 * have been scrolled down into the region.
9993 */
9994 if (type == USE_T_SR && *T_DA)
9995 {
9996 for (i = 0; i < line_count; ++i)
9997 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009998 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 out_str(T_CE);
10000 screen_start(); /* don't know where cursor is now */
10001 }
10002 }
10003
10004#ifdef FEAT_GUI
10005 gui_can_update_cursor();
10006 if (gui.in_use)
10007 out_flush(); /* always flush after a scroll */
10008#endif
10009 return OK;
10010}
10011
10012/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010013 * Delete lines on the screen and update ScreenLines[].
10014 * "end" is the line after the scrolled part. Normally it is Rows.
10015 * When scrolling region used "off" is the offset from the top for the region.
10016 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 *
10018 * Return OK for success, FAIL if the lines are not deleted.
10019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010021screen_del_lines(
10022 int off,
10023 int row,
10024 int line_count,
10025 int end,
10026 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010027 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010028 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029{
10030 int j;
10031 int i;
10032 unsigned temp;
10033 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010034 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 int cursor_end;
10036 int result_empty; /* result is empty until end of region */
10037 int can_delete; /* deleting line codes can be used */
10038 int type;
10039
10040 /*
10041 * FAIL if
10042 * - there is no valid screen
10043 * - the screen has to be redrawn completely
10044 * - the line count is less than one
10045 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010046 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010048 if (!screen_valid(TRUE) || line_count <= 0
10049 || (!force && line_count > p_ttyscroll)
10050#ifdef FEAT_CLIPBOARD
10051 || (clip_star.state != SELECT_CLEARED
10052 && redrawing_for_callback > 0)
10053#endif
10054 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055 return FAIL;
10056
10057 /*
10058 * Check if the rest of the current region will become empty.
10059 */
10060 result_empty = row + line_count >= end;
10061
10062 /*
10063 * We can delete lines only when 'db' flag not set or when 'ce' option
10064 * available.
10065 */
10066 can_delete = (*T_DB == NUL || can_clear(T_CE));
10067
10068 /*
10069 * There are six ways to delete lines:
10070 * 0. When in a vertically split window and t_CV isn't set, redraw the
10071 * characters from ScreenLines[].
10072 * 1. Use T_CD if it exists and the result is empty.
10073 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10074 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10075 * none of the other ways work.
10076 * 4. Use T_CE (erase line) if the result is empty.
10077 * 5. Use T_DL (delete line) if it exists.
10078 * 6. redraw the characters from ScreenLines[].
10079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10081 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010082 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 type = USE_T_CD;
10084#if defined(__BEOS__) && defined(BEOS_DR8)
10085 /*
10086 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10087 * its internal termcap... this works okay for tests which test *T_DB !=
10088 * NUL. It has the disadvantage that the user cannot use any :set t_*
10089 * command to get T_DB (back) to empty_option, only :set term=... will do
10090 * the trick...
10091 * Anyway, this hack will hopefully go away with the next OS release.
10092 * (Olaf Seibert)
10093 */
10094 else if (row == 0 && T_DB == empty_option
10095 && (line_count == 1 || *T_CDL == NUL))
10096#else
10097 else if (row == 0 && (
10098#ifndef AMIGA
10099 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10100 * up, so use delete-line command */
10101 line_count == 1 ||
10102#endif
10103 *T_CDL == NUL))
10104#endif
10105 type = USE_NL;
10106 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10107 type = USE_T_CDL;
10108 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010109 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 type = USE_T_CE;
10111 else if (*T_DL != NUL && can_delete)
10112 type = USE_T_DL;
10113 else if (*T_CDL != NUL && can_delete)
10114 type = USE_T_CDL;
10115 else
10116 return FAIL;
10117
10118#ifdef FEAT_CLIPBOARD
10119 /* Remove a modeless selection when deleting lines halfway the screen or
10120 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010121 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010122 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 else
10124 clip_scroll_selection(line_count);
10125#endif
10126
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127#ifdef FEAT_GUI
10128 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10129 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010130 gui_dont_update_cursor(gui.cursor_row >= row + off
10131 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#endif
10133
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010134 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10135 cursor_col = wp->w_wincol;
10136
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 if (*T_CCS != NUL) /* cursor relative to region */
10138 {
10139 cursor_row = row;
10140 cursor_end = end;
10141 }
10142 else
10143 {
10144 cursor_row = row + off;
10145 cursor_end = end + off;
10146 }
10147
10148 /*
10149 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10150 * Clear the inserted lines in ScreenLines[].
10151 */
10152 row += off;
10153 end += off;
10154 for (i = 0; i < line_count; ++i)
10155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 if (wp != NULL && wp->w_width != Columns)
10157 {
10158 /* need to copy part of a line */
10159 j = row + i;
10160 while ((j += line_count) <= end - 1)
10161 linecopy(j - line_count, j, wp);
10162 j -= line_count;
10163 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010164 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10165 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 else
10167 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10168 LineWraps[j] = FALSE;
10169 }
10170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 {
10172 /* whole width, moving the line pointers is faster */
10173 j = row + i;
10174 temp = LineOffset[j];
10175 while ((j += line_count) <= end - 1)
10176 {
10177 LineOffset[j - line_count] = LineOffset[j];
10178 LineWraps[j - line_count] = LineWraps[j];
10179 }
10180 LineOffset[j - line_count] = temp;
10181 LineWraps[j - line_count] = FALSE;
10182 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010183 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 else
10185 lineinvalid(temp, (int)Columns);
10186 }
10187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010189 if (screen_attr != clear_attr)
10190 screen_stop_highlight();
10191 if (clear_attr != 0)
10192 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 /* redraw the characters */
10195 if (type == USE_REDRAW)
10196 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010197 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010199 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 out_str(T_CD);
10201 screen_start(); /* don't know where cursor is now */
10202 }
10203 else if (type == USE_T_CDL)
10204 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010205 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 term_delete_lines(line_count);
10207 screen_start(); /* don't know where cursor is now */
10208 }
10209 /*
10210 * Deleting lines at top of the screen or scroll region: Just scroll
10211 * the whole screen (scroll region) up by outputting newlines on the
10212 * last line.
10213 */
10214 else if (type == USE_NL)
10215 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010216 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 for (i = line_count; --i >= 0; )
10218 out_char('\n'); /* cursor will remain on same line */
10219 }
10220 else
10221 {
10222 for (i = line_count; --i >= 0; )
10223 {
10224 if (type == USE_T_DL)
10225 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010226 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227 out_str(T_DL); /* delete a line */
10228 }
10229 else /* type == USE_T_CE */
10230 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010231 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 out_str(T_CE); /* erase a line */
10233 }
10234 screen_start(); /* don't know where cursor is now */
10235 }
10236 }
10237
10238 /*
10239 * If the 'db' flag is set, we need to clear the lines that have been
10240 * scrolled up at the bottom of the region.
10241 */
10242 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10243 {
10244 for (i = line_count; i > 0; --i)
10245 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010246 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 out_str(T_CE); /* erase a line */
10248 screen_start(); /* don't know where cursor is now */
10249 }
10250 }
10251
10252#ifdef FEAT_GUI
10253 gui_can_update_cursor();
10254 if (gui.in_use)
10255 out_flush(); /* always flush after a scroll */
10256#endif
10257
10258 return OK;
10259}
10260
10261/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010262 * Return TRUE when postponing displaying the mode message: when not redrawing
10263 * or inside a mapping.
10264 */
10265 int
10266skip_showmode()
10267{
10268 // Call char_avail() only when we are going to show something, because it
10269 // takes a bit of time. redrawing() may also call char_avail_avail().
10270 if (global_busy
10271 || msg_silent != 0
10272 || !redrawing()
10273 || (char_avail() && !KeyTyped))
10274 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010275 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010276 return TRUE;
10277 }
10278 return FALSE;
10279}
10280
10281/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010282 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 *
10284 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10285 * If clear_cmdline is FALSE there may be a message there that needs to be
10286 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010287 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * Return the length of the message (0 if no message).
10289 */
10290 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010291showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
10293 int need_clear;
10294 int length = 0;
10295 int do_mode;
10296 int attr;
10297 int nwr_save;
10298#ifdef FEAT_INS_EXPAND
10299 int sub_attr;
10300#endif
10301
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010302 do_mode = ((p_smd && msg_silent == 0)
10303 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010304 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010305 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010306 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010308 if (skip_showmode())
10309 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310
10311 nwr_save = need_wait_return;
10312
10313 /* wait a bit before overwriting an important message */
10314 check_for_delay(FALSE);
10315
10316 /* if the cmdline is more than one line high, erase top lines */
10317 need_clear = clear_cmdline;
10318 if (clear_cmdline && cmdline_row < Rows - 1)
10319 msg_clr_cmdline(); /* will reset clear_cmdline */
10320
10321 /* Position on the last line in the window, column 0 */
10322 msg_pos_mode();
10323 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010324 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 if (do_mode)
10326 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010327 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010329 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010330# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010331 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010332# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010333 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010334# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010335 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010336# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010337 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010339 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340# endif
10341#endif
10342#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10343 if (gui.in_use)
10344 {
10345 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010346 {
10347 /* HANGUL */
10348 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010349 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010350 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010351 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
10354#endif
10355#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010356 /* CTRL-X in Insert mode */
10357 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 {
10359 /* These messages can get long, avoid a wrap in a narrow
10360 * window. Prefer showing edit_submode_extra. */
10361 length = (Rows - msg_row) * Columns - 3;
10362 if (edit_submode_extra != NULL)
10363 length -= vim_strsize(edit_submode_extra);
10364 if (length > 0)
10365 {
10366 if (edit_submode_pre != NULL)
10367 length -= vim_strsize(edit_submode_pre);
10368 if (length - vim_strsize(edit_submode) > 0)
10369 {
10370 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010371 msg_puts_attr((char *)edit_submode_pre, attr);
10372 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 }
10374 if (edit_submode_extra != NULL)
10375 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010376 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010378 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 else
10380 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010381 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 }
10383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 }
10385 else
10386#endif
10387 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010389 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010390 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010391 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392 else if (State & INSERT)
10393 {
10394#ifdef FEAT_RIGHTLEFT
10395 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010396 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010398 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010400 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010401 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010403 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010405 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406#ifdef FEAT_RIGHTLEFT
10407 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010408 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409#endif
10410#ifdef FEAT_KEYMAP
10411 if (State & LANGMAP)
10412 {
10413# ifdef FEAT_ARABIC
10414 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010415 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 else
10417# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010418 if (get_keymap_str(curwin, (char_u *)" (%s)",
10419 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010420 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 }
10422#endif
10423 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010424 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 if (VIsual_active)
10427 {
10428 char *p;
10429
10430 /* Don't concatenate separate words to avoid translation
10431 * problems. */
10432 switch ((VIsual_select ? 4 : 0)
10433 + (VIsual_mode == Ctrl_V) * 2
10434 + (VIsual_mode == 'V'))
10435 {
10436 case 0: p = N_(" VISUAL"); break;
10437 case 1: p = N_(" VISUAL LINE"); break;
10438 case 2: p = N_(" VISUAL BLOCK"); break;
10439 case 4: p = N_(" SELECT"); break;
10440 case 5: p = N_(" SELECT LINE"); break;
10441 default: p = N_(" SELECT BLOCK"); break;
10442 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010443 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010445 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010447
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 need_clear = TRUE;
10449 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010450 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451#ifdef FEAT_INS_EXPAND
10452 && edit_submode == NULL /* otherwise it gets too long */
10453#endif
10454 )
10455 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010456 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 need_clear = TRUE;
10458 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010459
10460 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010461 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 msg_clr_eos();
10463 msg_didout = FALSE; /* overwrite this message */
10464 length = msg_col;
10465 msg_col = 0;
10466 need_wait_return = nwr_save; /* never ask for hit-return for this */
10467 }
10468 else if (clear_cmdline && msg_silent == 0)
10469 /* Clear the whole command line. Will reset "clear_cmdline". */
10470 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010471 else if (redraw_mode)
10472 {
10473 msg_pos_mode();
10474 msg_clr_eos();
10475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476
10477#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 /* In Visual mode the size of the selected area must be redrawn. */
10479 if (VIsual_active)
10480 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481
10482 /* If the last window has no status line, the ruler is after the mode
10483 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010484 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010485 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486#endif
10487 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010488 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 clear_cmdline = FALSE;
10490
10491 return length;
10492}
10493
10494/*
10495 * Position for a mode message.
10496 */
10497 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010498msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499{
10500 msg_col = 0;
10501 msg_row = Rows - 1;
10502}
10503
10504/*
10505 * Delete mode message. Used when ESC is typed which is expected to end
10506 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010507 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 */
10509 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010510unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511{
10512 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010513 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 */
10515 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10516 redraw_cmdline = TRUE; /* delete mode later */
10517 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010518 clearmode();
10519}
10520
10521/*
10522 * Clear the mode message.
10523 */
10524 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010525clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010526{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010527 int save_msg_row = msg_row;
10528 int save_msg_col = msg_col;
10529
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010530 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010531 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010532 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010533 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010534
10535 msg_col = save_msg_col;
10536 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537}
10538
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010539 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010540recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010541{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010542 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010543 if (!shortmess(SHM_RECORDING))
10544 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010545 char s[4];
10546
10547 sprintf(s, " @%c", reg_recording);
10548 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010549 }
10550}
10551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010552/*
10553 * Draw the tab pages line at the top of the Vim window.
10554 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010555 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010556draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010557{
10558 int tabcount = 0;
10559 tabpage_T *tp;
10560 int tabwidth;
10561 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010562 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010563 int attr;
10564 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010565 win_T *cwp;
10566 int wincount;
10567 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010568 int c;
10569 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010570 int attr_sel = HL_ATTR(HLF_TPS);
10571 int attr_nosel = HL_ATTR(HLF_TP);
10572 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010573 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010574 int room;
10575 int use_sep_chars = (t_colors < 8
10576#ifdef FEAT_GUI
10577 && !gui.in_use
10578#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010579#ifdef FEAT_TERMGUICOLORS
10580 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010581#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010582 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010583
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010584 if (ScreenLines == NULL)
10585 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010586 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010587
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010588#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010589 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010590 if (gui_use_tabline())
10591 {
10592 gui_update_tabline();
10593 return;
10594 }
10595#endif
10596
10597 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010598 return;
10599
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010600#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010601 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010602
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010603 /* Use the 'tabline' option if it's set. */
10604 if (*p_tal != NUL)
10605 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010606 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010607
10608 /* Check for an error. If there is one we would loop in redrawing the
10609 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010610 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010611 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010612 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010613 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010614 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010615 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010616 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010617 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010618#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010619 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010620 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010621 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010622
Bram Moolenaar238a5642006-02-21 22:12:05 +000010623 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10624 if (tabwidth < 6)
10625 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626
Bram Moolenaar238a5642006-02-21 22:12:05 +000010627 attr = attr_nosel;
10628 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010629 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10630 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010631 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010632 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010633
Bram Moolenaar238a5642006-02-21 22:12:05 +000010634 if (tp->tp_topframe == topframe)
10635 attr = attr_sel;
10636 if (use_sep_chars && col > 0)
10637 screen_putchar('|', 0, col++, attr);
10638
10639 if (tp->tp_topframe != topframe)
10640 attr = attr_nosel;
10641
10642 screen_putchar(' ', 0, col++, attr);
10643
10644 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010645 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010646 cwp = curwin;
10647 wp = firstwin;
10648 }
10649 else
10650 {
10651 cwp = tp->tp_curwin;
10652 wp = tp->tp_firstwin;
10653 }
10654
10655 modified = FALSE;
10656 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10657 if (bufIsChanged(wp->w_buffer))
10658 modified = TRUE;
10659 if (modified || wincount > 1)
10660 {
10661 if (wincount > 1)
10662 {
10663 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010664 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010665 if (col + len >= Columns - 3)
10666 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010667 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010668#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010669 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010670#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010671 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010672#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010673 );
10674 col += len;
10675 }
10676 if (modified)
10677 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10678 screen_putchar(' ', 0, col++, attr);
10679 }
10680
10681 room = scol - col + tabwidth - 1;
10682 if (room > 0)
10683 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010684 /* Get buffer name in NameBuff[] */
10685 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010686 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010687 len = vim_strsize(NameBuff);
10688 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010689 if (has_mbyte)
10690 while (len > room)
10691 {
10692 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010693 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010695 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010696 {
10697 p += len - room;
10698 len = room;
10699 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010700 if (len > Columns - col - 1)
10701 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010702
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010703 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010704 col += len;
10705 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010706 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010707
10708 /* Store the tab page number in TabPageIdxs[], so that
10709 * jump_to_mouse() knows where each one is. */
10710 ++tabcount;
10711 while (scol < col)
10712 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010713 }
10714
Bram Moolenaar238a5642006-02-21 22:12:05 +000010715 if (use_sep_chars)
10716 c = '_';
10717 else
10718 c = ' ';
10719 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010720
10721 /* Put an "X" for closing the current tab if there are several. */
10722 if (first_tabpage->tp_next != NULL)
10723 {
10724 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10725 TabPageIdxs[Columns - 1] = -999;
10726 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010727 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010728
10729 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10730 * set. */
10731 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010732}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010733
10734/*
10735 * Get buffer name for "buf" into NameBuff[].
10736 * Takes care of special buffer names and translates special characters.
10737 */
10738 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010739get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010740{
10741 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010742 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010743 else
10744 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10745 trans_characters(NameBuff, MAXPATHL);
10746}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010747
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748/*
10749 * Get the character to use in a status line. Get its attributes in "*attr".
10750 */
10751 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010752fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753{
10754 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010755
10756#ifdef FEAT_TERMINAL
10757 if (bt_terminal(wp->w_buffer))
10758 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010760 {
10761 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010763 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010764 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010765 {
10766 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010767 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010768 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010769 }
10770 else
10771#endif
10772 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010774 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 fill = fill_stl;
10776 }
10777 else
10778 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010779 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 fill = fill_stlnc;
10781 }
10782 /* Use fill when there is highlighting, and highlighting of current
10783 * window differs, or the fillchars differ, or this is not the
10784 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010785 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010786 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 || (fill_stl != fill_stlnc)))
10788 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010789 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 return '^';
10791 return '=';
10792}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794/*
10795 * Get the character to use in a separator between vertically split windows.
10796 * Get its attributes in "*attr".
10797 */
10798 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010799fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010801 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 if (*attr == 0 && fill_vert == ' ')
10803 return '|';
10804 else
10805 return fill_vert;
10806}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807
10808/*
10809 * Return TRUE if redrawing should currently be done.
10810 */
10811 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010812redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010814#ifdef FEAT_EVAL
10815 if (disable_redraw_for_testing)
10816 return 0;
10817 else
10818#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010819 return ((!RedrawingDisabled
10820#ifdef FEAT_EVAL
10821 || ignore_redraw_flag_for_testing
10822#endif
10823 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825
10826/*
10827 * Return TRUE if printing messages should currently be done.
10828 */
10829 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010830messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832 return (!(p_lz && char_avail() && !KeyTyped));
10833}
10834
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010835#ifdef FEAT_MENU
10836/*
10837 * Draw the window toolbar.
10838 */
10839 static void
10840redraw_win_toolbar(win_T *wp)
10841{
10842 vimmenu_T *menu;
10843 int item_idx = 0;
10844 int item_count = 0;
10845 int col = 0;
10846 int next_col;
10847 int off = (int)(current_ScreenLine - ScreenLines);
10848 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10849 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10850
10851 vim_free(wp->w_winbar_items);
10852 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10853 ++item_count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +020010854 wp->w_winbar_items = ALLOC_CLEAR_MULT(winbar_item_T, item_count + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010855
10856 /* TODO: use fewer spaces if there is not enough room */
10857 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010858 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010859 {
10860 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010861 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010862 break;
10863 if (col > 1)
10864 {
10865 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010866 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010867 break;
10868 }
10869
10870 wp->w_winbar_items[item_idx].wb_startcol = col;
10871 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010872 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010873 break;
10874
10875 next_col = text_to_screenline(wp, menu->name, col);
10876 while (col < next_col)
10877 {
10878 ScreenAttrs[off + col] = button_attr;
10879 ++col;
10880 }
10881 wp->w_winbar_items[item_idx].wb_endcol = col;
10882 wp->w_winbar_items[item_idx].wb_menu = menu;
10883 ++item_idx;
10884
Bram Moolenaar02631462017-09-22 15:20:32 +020010885 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010886 break;
10887 space_to_screenline(off + col, button_attr);
10888 ++col;
10889 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010890 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010891 {
10892 space_to_screenline(off + col, fill_attr);
10893 ++col;
10894 }
10895 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10896
Bram Moolenaar02631462017-09-22 15:20:32 +020010897 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010898 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010899}
10900#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010901
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902/*
10903 * Show current status info in ruler and various other places
10904 * If always is FALSE, only show ruler if position has changed.
10905 */
10906 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010907showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908{
10909 if (!always && !redrawing())
10910 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010911#ifdef FEAT_INS_EXPAND
10912 if (pum_visible())
10913 {
10914 /* Don't redraw right now, do it later. */
10915 curwin->w_redr_status = TRUE;
10916 return;
10917 }
10918#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010919#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010920 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010921 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 else
10923#endif
10924#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010925 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926#endif
10927
10928#ifdef FEAT_TITLE
10929 if (need_maketitle
10930# ifdef FEAT_STL_OPT
10931 || (p_icon && (stl_syntax & STL_IN_ICON))
10932 || (p_title && (stl_syntax & STL_IN_TITLE))
10933# endif
10934 )
10935 maketitle();
10936#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010937 /* Redraw the tab pages line if needed. */
10938 if (redraw_tabline)
10939 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940}
10941
10942#ifdef FEAT_CMDL_INFO
10943 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010944win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010946#define RULER_BUF_LEN 70
10947 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 int row;
10949 int fillchar;
10950 int attr;
10951 int empty_line = FALSE;
10952 colnr_T virtcol;
10953 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010954 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 int this_ru_col;
10957 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010958 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959
10960 /* If 'ruler' off or redrawing disabled, don't do anything */
10961 if (!p_ru)
10962 return;
10963
10964 /*
10965 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10966 * after deleting lines, before cursor.lnum is corrected.
10967 */
10968 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10969 return;
10970
10971#ifdef FEAT_INS_EXPAND
10972 /* Don't draw the ruler while doing insert-completion, it might overwrite
10973 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 if (edit_submode != NULL)
10976 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010977 // Don't draw the ruler when the popup menu is visible, it may overlap.
10978 // Except when the popup menu will be redrawn anyway.
10979 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010980 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981#endif
10982
10983#ifdef FEAT_STL_OPT
10984 if (*p_ruf)
10985 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010986 int save_called_emsg = called_emsg;
10987
10988 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010990 if (called_emsg)
10991 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010992 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010993 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 return;
10995 }
10996#endif
10997
10998 /*
10999 * Check if not in Insert mode and the line is empty (will show "0-1").
11000 */
11001 if (!(State & INSERT)
11002 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11003 empty_line = TRUE;
11004
11005 /*
11006 * Only draw the ruler when something changed.
11007 */
11008 validate_virtcol_win(wp);
11009 if ( redraw_cmdline
11010 || always
11011 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11012 || wp->w_cursor.col != wp->w_ru_cursor.col
11013 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 || wp->w_topline != wp->w_ru_topline
11016 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11017#ifdef FEAT_DIFF
11018 || wp->w_topfill != wp->w_ru_topfill
11019#endif
11020 || empty_line != wp->w_ru_empty)
11021 {
11022 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 if (wp->w_status_height)
11024 {
11025 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011026 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011027 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011028 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 }
11030 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 {
11032 row = Rows - 1;
11033 fillchar = ' ';
11034 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 width = Columns;
11036 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 }
11038
11039 /* In list mode virtcol needs to be recomputed */
11040 virtcol = wp->w_virtcol;
11041 if (wp->w_p_list && lcs_tab1 == NUL)
11042 {
11043 wp->w_p_list = FALSE;
11044 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11045 wp->w_p_list = TRUE;
11046 }
11047
11048 /*
11049 * Some sprintfs return the length, some return a pointer.
11050 * To avoid portability problems we use strlen() here.
11051 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011052 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11054 ? 0L
11055 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011056 len = STRLEN(buffer);
11057 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11059 (int)virtcol + 1);
11060
11061 /*
11062 * Add a "50%" if there is room for it.
11063 * On the last line, don't print in the last column (scrolls the
11064 * screen up on some terminals).
11065 */
11066 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011067 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 this_ru_col = ru_col - (Columns - width);
11072 if (this_ru_col < 0)
11073 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 /* Never use more than half the window/screen width, leave the other
11075 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011076 if (this_ru_col < (width + 1) / 2)
11077 this_ru_col = (width + 1) / 2;
11078 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011080 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011081 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 if (has_mbyte)
11084 i += (*mb_char2bytes)(fillchar, buffer + i);
11085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 buffer[i++] = fillchar;
11087 ++o;
11088 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011089 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 }
11091 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 if (has_mbyte)
11093 {
11094 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011095 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 {
11097 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011098 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 {
11100 buffer[i] = NUL;
11101 break;
11102 }
11103 }
11104 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011105 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011106 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107
Bram Moolenaar4033c552017-09-16 20:54:51 +020011108 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 i = redraw_cmdline;
11110 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011111 this_ru_col + off + (int)STRLEN(buffer),
11112 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113 fillchar, fillchar, attr);
11114 /* don't redraw the cmdline because of showing the ruler */
11115 redraw_cmdline = i;
11116 wp->w_ru_cursor = wp->w_cursor;
11117 wp->w_ru_virtcol = wp->w_virtcol;
11118 wp->w_ru_empty = empty_line;
11119 wp->w_ru_topline = wp->w_topline;
11120 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11121#ifdef FEAT_DIFF
11122 wp->w_ru_topfill = wp->w_topfill;
11123#endif
11124 }
11125}
11126#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011127
11128#if defined(FEAT_LINEBREAK) || defined(PROTO)
11129/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011130 * Return the width of the 'number' and 'relativenumber' column.
11131 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011132 * Otherwise it depends on 'numberwidth' and the line count.
11133 */
11134 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011135number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011136{
11137 int n;
11138 linenr_T lnum;
11139
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011140 if (wp->w_p_rnu && !wp->w_p_nu)
11141 /* cursor line shows "0" */
11142 lnum = wp->w_height;
11143 else
11144 /* cursor line shows absolute line number */
11145 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011146
Bram Moolenaar6b314672015-03-20 15:42:10 +010011147 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011148 return wp->w_nrwidth_width;
11149 wp->w_nrwidth_line_count = lnum;
11150
11151 n = 0;
11152 do
11153 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011154 lnum /= 10;
11155 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011156 } while (lnum > 0);
11157
11158 /* 'numberwidth' gives the minimal width plus one */
11159 if (n < wp->w_p_nuw - 1)
11160 n = wp->w_p_nuw - 1;
11161
11162 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011163 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011164 return n;
11165}
11166#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011167
Bram Moolenaar113e1072019-01-20 15:30:40 +010011168#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011169/*
11170 * Return the current cursor column. This is the actual position on the
11171 * screen. First column is 0.
11172 */
11173 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011174screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011175{
11176 return screen_cur_col;
11177}
11178
11179/*
11180 * Return the current cursor row. This is the actual position on the screen.
11181 * First row is 0.
11182 */
11183 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011184screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011185{
11186 return screen_cur_row;
11187}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011188#endif