blob: d5272236f44e269b605b24222adc10bf8aafb37a [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 Moolenaar51e14382019-05-25 20:21:28 +0200331 screenline = (schar_T *)lalloc(rows * cols * sizeof(schar_T), FALSE);
332 screenattr = (sattr_T *)lalloc(rows * cols * sizeof(sattr_T), FALSE);
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 {
337 screenlineUC = (u8char_T *)lalloc(
Bram Moolenaar51e14382019-05-25 20:21:28 +0200338 rows * cols * sizeof(u8char_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200339 if (screenlineUC == NULL)
340 ret = 2;
341 for (i = 0; i < p_mco; ++i)
342 {
343 screenlineC[i] = (u8char_T *)lalloc(
Bram Moolenaar51e14382019-05-25 20:21:28 +0200344 rows * cols * sizeof(u8char_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200345 if (screenlineC[i] == NULL)
346 ret = 2;
347 }
348 }
349 if (enc_dbcs == DBCS_JPNU)
350 {
Bram Moolenaar51e14382019-05-25 20:21:28 +0200351 screenline2 = (schar_T *)lalloc(rows * cols * sizeof(schar_T), FALSE);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200352 if (screenline2 == NULL)
353 ret = 2;
354 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200355
356 if (ret != 2)
357 {
358 /* Save the text displayed in the command line area. */
359 for (r = 0; r < rows; ++r)
360 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200361 mch_memmove(screenline + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200362 ScreenLines + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200363 (size_t)cols * sizeof(schar_T));
364 mch_memmove(screenattr + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200365 ScreenAttrs + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200366 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200367 if (enc_utf8)
368 {
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200369 mch_memmove(screenlineUC + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200370 ScreenLinesUC + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200371 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200372 for (i = 0; i < p_mco; ++i)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200373 mch_memmove(screenlineC[i] + r * cols,
374 ScreenLinesC[i] + LineOffset[cmdline_row + r],
375 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200376 }
377 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200378 mch_memmove(screenline2 + r * cols,
Bram Moolenaar2951b772013-07-03 12:45:31 +0200379 ScreenLines2 + LineOffset[cmdline_row + r],
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200380 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200381 }
382
383 update_screen(0);
384 ret = 3;
385
386 if (must_redraw == 0)
387 {
388 int off = (int)(current_ScreenLine - ScreenLines);
389
390 /* Restore the text displayed in the command line area. */
391 for (r = 0; r < rows; ++r)
392 {
393 mch_memmove(current_ScreenLine,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200394 screenline + r * cols,
395 (size_t)cols * sizeof(schar_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200396 mch_memmove(ScreenAttrs + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200397 screenattr + r * cols,
398 (size_t)cols * sizeof(sattr_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200399 if (enc_utf8)
400 {
401 mch_memmove(ScreenLinesUC + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200402 screenlineUC + r * cols,
403 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200404 for (i = 0; i < p_mco; ++i)
405 mch_memmove(ScreenLinesC[i] + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200406 screenlineC[i] + r * cols,
407 (size_t)cols * sizeof(u8char_T));
Bram Moolenaar2951b772013-07-03 12:45:31 +0200408 }
409 if (enc_dbcs == DBCS_JPNU)
410 mch_memmove(ScreenLines2 + off,
Bram Moolenaar5f95f282015-07-25 22:53:00 +0200411 screenline2 + r * cols,
412 (size_t)cols * sizeof(schar_T));
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200413 screen_line(cmdline_row + r, 0, cols, cols, 0);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200414 }
415 ret = 4;
416 }
Bram Moolenaar2951b772013-07-03 12:45:31 +0200417 }
418
419 vim_free(screenline);
420 vim_free(screenattr);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200421 if (enc_utf8)
422 {
423 vim_free(screenlineUC);
424 for (i = 0; i < p_mco; ++i)
425 vim_free(screenlineC[i]);
426 }
427 if (enc_dbcs == DBCS_JPNU)
428 vim_free(screenline2);
Bram Moolenaar2951b772013-07-03 12:45:31 +0200429
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200430 /* Show the intro message when appropriate. */
431 maybe_intro_message();
432
433 setcursor();
434
Bram Moolenaar2951b772013-07-03 12:45:31 +0200435 return ret;
436}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100437#endif
Bram Moolenaar2951b772013-07-03 12:45:31 +0200438
439/*
Bram Moolenaar975b5272016-03-15 23:10:59 +0100440 * Invoked after an asynchronous callback is called.
441 * If an echo command was used the cursor needs to be put back where
442 * it belongs. If highlighting was changed a redraw is needed.
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200443 * If "call_update_screen" is FALSE don't call update_screen() when at the
444 * command line.
Bram Moolenaar975b5272016-03-15 23:10:59 +0100445 */
446 void
Bram Moolenaar02e177d2017-08-26 23:43:28 +0200447redraw_after_callback(int call_update_screen)
Bram Moolenaar975b5272016-03-15 23:10:59 +0100448{
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200449 ++redrawing_for_callback;
450
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100451 if (State == HITRETURN || State == ASKMORE)
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200452 ; // do nothing
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100453 else if (State & CMDLINE)
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200454 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200455 // Don't redraw when in prompt_for_number().
456 if (cmdline_row > 0)
457 {
458 // Redrawing only works when the screen didn't scroll. Don't clear
459 // wildmenu entries.
460 if (msg_scrolled == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200461#ifdef FEAT_WILDMENU
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200462 && wild_menu_showing == 0
Bram Moolenaar85dad2c2017-07-12 21:12:43 +0200463#endif
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200464 && call_update_screen)
465 update_screen(0);
466
467 // Redraw in the same position, so that the user can continue
468 // editing the command.
469 redrawcmdline_ex(FALSE);
470 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200471 }
Bram Moolenaar1b9645d2017-09-17 23:03:31 +0200472 else if (State & (NORMAL | INSERT | TERMINAL))
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100473 {
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200474 // keep the command line if possible
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200475 update_screen(VALID_NO_UPDATE);
Bram Moolenaarbfb96c02016-03-19 17:05:20 +0100476 setcursor();
477 }
Bram Moolenaar975b5272016-03-15 23:10:59 +0100478 cursor_on();
Bram Moolenaar975b5272016-03-15 23:10:59 +0100479#ifdef FEAT_GUI
Bram Moolenaara338adc2018-01-31 20:51:47 +0100480 if (gui.in_use && !gui_mch_is_blink_off())
Bram Moolenaar4cbdf152018-08-26 21:23:07 +0200481 // Don't update the cursor when it is blinking and off to avoid
482 // flicker.
Bram Moolenaara338adc2018-01-31 20:51:47 +0100483 out_flush_cursor(FALSE, FALSE);
484 else
Bram Moolenaar975b5272016-03-15 23:10:59 +0100485#endif
Bram Moolenaaracda04f2018-02-08 09:57:28 +0100486 out_flush();
Bram Moolenaar80dd3f92017-07-19 12:51:52 +0200487
488 --redrawing_for_callback;
Bram Moolenaar975b5272016-03-15 23:10:59 +0100489}
490
491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 * Changed something in the current window, at buffer line "lnum", that
493 * requires that line and possibly other lines to be redrawn.
494 * Used when entering/leaving Insert mode with the cursor on a folded line.
495 * Used to remove the "$" from a change command.
496 * Note that when also inserting/deleting lines w_redraw_top and w_redraw_bot
497 * may become invalid and the whole window will have to be redrawn.
498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100500redrawWinline(
Bram Moolenaar90a99792018-09-12 21:52:18 +0200501 win_T *wp,
Bram Moolenaarae12f4b2019-01-09 20:51:04 +0100502 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503{
Bram Moolenaar90a99792018-09-12 21:52:18 +0200504 if (wp->w_redraw_top == 0 || wp->w_redraw_top > lnum)
505 wp->w_redraw_top = lnum;
506 if (wp->w_redraw_bot == 0 || wp->w_redraw_bot < lnum)
507 wp->w_redraw_bot = lnum;
508 redraw_win_later(wp, VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509}
510
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200511 void
512reset_updating_screen(int may_resize_shell UNUSED)
513{
514 updating_screen = FALSE;
515#ifdef FEAT_GUI
516 if (may_resize_shell)
517 gui_may_resize_shell();
518#endif
519#ifdef FEAT_TERMINAL
520 term_check_channel_closed_recently();
521#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200522
523#ifdef HAVE_DROP_FILE
524 // If handle_drop() was called while updating_screen was TRUE need to
525 // handle the drop now.
526 handle_any_postponed_drop();
527#endif
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200528}
529
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530/*
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200531 * Update all windows that are editing the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 */
533 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100534update_curbuf(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535{
536 redraw_curbuf_later(type);
537 update_screen(type);
538}
539
540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 * Based on the current value of curwin->w_topline, transfer a screenfull
542 * of stuff from Filemem to ScreenLines[], and update curwin->w_botline.
Bram Moolenaar072412e2017-09-13 22:11:35 +0200543 * Return OK when the screen was updated, FAIL if it was not done.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200545 int
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200546update_screen(int type_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200548 int type = type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 win_T *wp;
550 static int did_intro = FALSE;
551#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
552 int did_one;
553#endif
Bram Moolenaar144445d2016-07-08 21:41:54 +0200554#ifdef FEAT_GUI
Bram Moolenaar107abd22016-08-12 14:08:25 +0200555 int did_undraw = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +0200556 int gui_cursor_col = 0;
557 int gui_cursor_row = 0;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200558#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200559 int no_update = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000561 /* Don't do anything if the screen structures are (not yet) valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 if (!screen_valid(TRUE))
Bram Moolenaar072412e2017-09-13 22:11:35 +0200563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200565 if (type == VALID_NO_UPDATE)
566 {
567 no_update = TRUE;
568 type = 0;
569 }
570
Bram Moolenaara3347722019-05-11 21:14:24 +0200571#ifdef FEAT_EVAL
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200572 {
573 buf_T *buf;
574
575 // Before updating the screen, notify any listeners of changed text.
576 FOR_ALL_BUFFERS(buf)
577 invoke_listeners(buf);
578 }
Bram Moolenaara3347722019-05-11 21:14:24 +0200579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (must_redraw)
582 {
583 if (type < must_redraw) /* use maximal type */
584 type = must_redraw;
Bram Moolenaar943fae42007-07-30 20:00:38 +0000585
586 /* must_redraw is reset here, so that when we run into some weird
587 * reason to redraw while busy redrawing (e.g., asynchronous
588 * scrolling), or update_topline() in win_update() will cause a
589 * scroll, the screen will be redrawn later or in win_update(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 must_redraw = 0;
591 }
592
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200593 /* May need to update w_lines[]. */
594 if (curwin->w_lines_valid == 0 && type < NOT_VALID
595#ifdef FEAT_TERMINAL
596 && !term_do_update_window(curwin)
597#endif
598 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 type = NOT_VALID;
600
Bram Moolenaar19f990e2009-11-25 12:08:03 +0000601 /* Postpone the redrawing when it's not needed and when being called
602 * recursively. */
603 if (!redrawing() || updating_screen)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 redraw_later(type); /* remember type for next time */
606 must_redraw = type;
607 if (type > INVERTED_ALL)
608 curwin->w_lines_valid = 0; /* don't use w_lines[].wl_size now */
Bram Moolenaar072412e2017-09-13 22:11:35 +0200609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200611#ifdef FEAT_TEXT_PROP
612 // TODO: avoid redrawing everything when there is a popup window.
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200613 if (popup_any_visible())
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200614 type = NOT_VALID;
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617 updating_screen = TRUE;
618#ifdef FEAT_SYN_HL
619 ++display_tick; /* let syntax code know we're in a next round of
620 * display updating */
621#endif
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200622 if (no_update)
623 ++no_win_do_lines_ins;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /*
626 * if the screen was scrolled up when displaying a message, scroll it down
627 */
628 if (msg_scrolled)
629 {
630 clear_cmdline = TRUE;
631 if (msg_scrolled > Rows - 5) /* clearing is faster */
632 type = CLEAR;
633 else if (type != CLEAR)
634 {
635 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +0200636 if (screen_ins_lines(0, 0, msg_scrolled, (int)Rows, 0, NULL)
637 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 type = CLEAR;
639 FOR_ALL_WINDOWS(wp)
640 {
641 if (W_WINROW(wp) < msg_scrolled)
642 {
643 if (W_WINROW(wp) + wp->w_height > msg_scrolled
644 && wp->w_redr_type < REDRAW_TOP
645 && wp->w_lines_valid > 0
646 && wp->w_topline == wp->w_lines[0].wl_lnum)
647 {
648 wp->w_upd_rows = msg_scrolled - W_WINROW(wp);
649 wp->w_redr_type = REDRAW_TOP;
650 }
651 else
652 {
653 wp->w_redr_type = NOT_VALID;
Bram Moolenaare0de17d2017-09-24 16:24:34 +0200654 if (W_WINROW(wp) + wp->w_height + wp->w_status_height
655 <= msg_scrolled)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 }
659 }
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200660 if (!no_update)
661 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000662 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 msg_scrolled = 0;
665 need_wait_return = FALSE;
666 }
667
668 /* reset cmdline_row now (may have been changed temporarily) */
669 compute_cmdrow();
670
671 /* Check for changed highlighting */
672 if (need_highlight_changed)
673 highlight_changed();
674
675 if (type == CLEAR) /* first clear screen */
676 {
677 screenclear(); /* will reset clear_cmdline */
678 type = NOT_VALID;
Bram Moolenaar9f5f7bf2017-06-28 20:45:26 +0200679 /* must_redraw may be set indirectly, avoid another redraw later */
680 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682
683 if (clear_cmdline) /* going to clear cmdline (done below) */
684 check_for_delay(FALSE);
685
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000686#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +0200687 /* Force redraw when width of 'number' or 'relativenumber' column
688 * changes. */
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000689 if (curwin->w_redr_type < NOT_VALID
Bram Moolenaar64486672010-05-16 15:46:46 +0200690 && curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu)
691 ? number_width(curwin) : 0))
Bram Moolenaar592e0a22004-07-03 16:05:59 +0000692 curwin->w_redr_type = NOT_VALID;
693#endif
694
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 /*
696 * Only start redrawing if there is really something to do.
697 */
698 if (type == INVERTED)
699 update_curswant();
700 if (curwin->w_redr_type < type
701 && !((type == VALID
702 && curwin->w_lines[0].wl_valid
703#ifdef FEAT_DIFF
704 && curwin->w_topfill == curwin->w_old_topfill
705 && curwin->w_botfill == curwin->w_old_botfill
706#endif
707 && curwin->w_topline == curwin->w_lines[0].wl_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 || (type == INVERTED
Bram Moolenaarb0c9a852006-11-28 15:14:56 +0000709 && VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 && curwin->w_old_cursor_lnum == curwin->w_cursor.lnum
711 && curwin->w_old_visual_mode == VIsual_mode
712 && (curwin->w_valid & VALID_VIRTCOL)
713 && curwin->w_old_curswant == curwin->w_curswant)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 ))
715 curwin->w_redr_type = type;
716
Bram Moolenaar5a305422006-04-28 22:38:25 +0000717 /* Redraw the tab pages line if needed. */
718 if (redraw_tabline || type >= NOT_VALID)
719 draw_tabline();
Bram Moolenaar5a305422006-04-28 22:38:25 +0000720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_SYN_HL
722 /*
723 * Correct stored syntax highlighting info for changes in each displayed
724 * buffer. Each buffer must only be done once.
725 */
726 FOR_ALL_WINDOWS(wp)
727 {
728 if (wp->w_buffer->b_mod_set)
729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 win_T *wwp;
731
732 /* Check if we already did this buffer. */
733 for (wwp = firstwin; wwp != wp; wwp = wwp->w_next)
734 if (wwp->w_buffer == wp->w_buffer)
735 break;
Bram Moolenaar4033c552017-09-16 20:54:51 +0200736 if (wwp == wp && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 syn_stack_apply_changes(wp->w_buffer);
738 }
739 }
740#endif
741
742 /*
743 * Go from top to bottom through the windows, redrawing the ones that need
744 * it.
745 */
746#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
747 did_one = FALSE;
748#endif
749#ifdef FEAT_SEARCH_EXTRA
750 search_hl.rm.regprog = NULL;
751#endif
752 FOR_ALL_WINDOWS(wp)
753 {
754 if (wp->w_redr_type != 0)
755 {
756 cursor_off();
757#if defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CLIPBOARD)
758 if (!did_one)
759 {
760 did_one = TRUE;
761# ifdef FEAT_SEARCH_EXTRA
762 start_search_hl();
763# endif
764# ifdef FEAT_CLIPBOARD
765 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +0200766 if (clip_star.available && clip_isautosel_star())
767 clip_update_selection(&clip_star);
768 if (clip_plus.available && clip_isautosel_plus())
769 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770# endif
771#ifdef FEAT_GUI
772 /* Remove the cursor before starting to do anything, because
773 * scrolling may make it difficult to redraw the text under
774 * it. */
Bram Moolenaar107abd22016-08-12 14:08:25 +0200775 if (gui.in_use && wp == curwin)
Bram Moolenaar144445d2016-07-08 21:41:54 +0200776 {
777 gui_cursor_col = gui.cursor_col;
778 gui_cursor_row = gui.cursor_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 gui_undraw_cursor();
Bram Moolenaar107abd22016-08-12 14:08:25 +0200780 did_undraw = TRUE;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783 }
784#endif
785 win_update(wp);
786 }
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /* redraw status line after the window to minimize cursor movement */
789 if (wp->w_redr_status)
790 {
791 cursor_off();
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200792 win_redr_status(wp, TRUE); // any popup menu will be redrawn below
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795#if defined(FEAT_SEARCH_EXTRA)
796 end_search_hl();
797#endif
Bram Moolenaar51971b32013-02-13 12:16:05 +0100798#ifdef FEAT_INS_EXPAND
799 /* May need to redraw the popup menu. */
Bram Moolenaar491ac282018-06-17 14:47:55 +0200800 pum_may_redraw();
Bram Moolenaar51971b32013-02-13 12:16:05 +0100801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 /* Reset b_mod_set flags. Going through all windows is probably faster
804 * than going through all buffers (there could be many buffers). */
Bram Moolenaar29323592016-07-24 22:04:11 +0200805 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 wp->w_buffer->b_mod_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200808 reset_updating_screen(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Clear or redraw the command line. Done last, because scrolling may
811 * mess up the command line. */
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200812 if (clear_cmdline || redraw_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 showmode();
814
Bram Moolenaar29ae3772017-04-30 19:39:39 +0200815 if (no_update)
816 --no_win_do_lines_ins;
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* May put up an introductory message when not editing a file */
Bram Moolenaar249f0dd2013-07-04 22:31:03 +0200819 if (!did_intro)
820 maybe_intro_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 did_intro = TRUE;
822
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#ifdef FEAT_TEXT_PROP
824 // Display popup windows on top of the others.
825 update_popups();
826#endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_GUI
829 /* Redraw the cursor and update the scrollbars when all screen updating is
830 * done. */
831 if (gui.in_use)
832 {
Bram Moolenaar107abd22016-08-12 14:08:25 +0200833 if (did_undraw && !gui_mch_is_blink_off())
Bram Moolenaar144445d2016-07-08 21:41:54 +0200834 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100835 mch_disable_flush();
836 out_flush(); /* required before updating the cursor */
837 mch_enable_flush();
838
Bram Moolenaar144445d2016-07-08 21:41:54 +0200839 /* Put the GUI position where the cursor was, gui_update_cursor()
840 * uses that. */
841 gui.col = gui_cursor_col;
842 gui.row = gui_cursor_row;
Bram Moolenaar84dbd492016-10-02 23:09:31 +0200843 gui.col = mb_fix_col(gui.col, gui.row);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara338adc2018-01-31 20:51:47 +0100845 gui_may_flush();
Bram Moolenaar65549bd2016-07-08 22:52:37 +0200846 screen_cur_col = gui.col;
847 screen_cur_row = gui.row;
Bram Moolenaar144445d2016-07-08 21:41:54 +0200848 }
Bram Moolenaara338adc2018-01-31 20:51:47 +0100849 else
850 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 gui_update_scrollbars(FALSE);
852 }
853#endif
Bram Moolenaar072412e2017-09-13 22:11:35 +0200854 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855}
856
Bram Moolenaar1fa8fdd2019-02-25 05:41:15 +0100857#if defined(FEAT_NETBEANS_INTG) || defined(FEAT_GUI)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100858/*
859 * Prepare for updating one or more windows.
860 * Caller must check for "updating_screen" already set to avoid recursiveness.
861 */
862 static void
863update_prepare(void)
864{
865 cursor_off();
866 updating_screen = TRUE;
867#ifdef FEAT_GUI
868 /* Remove the cursor before starting to do anything, because scrolling may
869 * make it difficult to redraw the text under it. */
870 if (gui.in_use)
871 gui_undraw_cursor();
872#endif
873#ifdef FEAT_SEARCH_EXTRA
874 start_search_hl();
875#endif
876}
877
878/*
879 * Finish updating one or more windows.
880 */
881 static void
882update_finish(void)
883{
Bram Moolenaar4c25bd72019-04-20 23:38:07 +0200884 if (redraw_cmdline || redraw_mode)
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100885 showmode();
886
887# ifdef FEAT_SEARCH_EXTRA
888 end_search_hl();
889# endif
890
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200891 reset_updating_screen(TRUE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100892
893# ifdef FEAT_GUI
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100894 /* Redraw the cursor and update the scrollbars when all screen updating is
895 * done. */
896 if (gui.in_use)
897 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100898 out_flush_cursor(FALSE, FALSE);
Bram Moolenaarc10f0e72017-02-01 18:37:14 +0100899 gui_update_scrollbars(FALSE);
900 }
901# endif
902}
903#endif
904
Bram Moolenaar860cae12010-06-05 23:22:07 +0200905#if defined(FEAT_CONCEAL) || defined(PROTO)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200906/*
907 * Return TRUE if the cursor line in window "wp" may be concealed, according
908 * to the 'concealcursor' option.
909 */
910 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100911conceal_cursor_line(win_T *wp)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200912{
913 int c;
914
915 if (*wp->w_p_cocu == NUL)
916 return FALSE;
917 if (get_real_state() & VISUAL)
918 c = 'v';
919 else if (State & INSERT)
920 c = 'i';
921 else if (State & NORMAL)
922 c = 'n';
Bram Moolenaarca8c9862010-07-24 15:00:38 +0200923 else if (State & CMDLINE)
924 c = 'c';
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200925 else
926 return FALSE;
927 return vim_strchr(wp->w_p_cocu, c) != NULL;
928}
929
930/*
931 * Check if the cursor line needs to be redrawn because of 'concealcursor'.
932 */
933 void
Bram Moolenaarb9464822018-05-10 15:09:49 +0200934conceal_check_cursor_line(void)
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200935{
936 if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin))
937 {
938 need_cursor_line_redraw = TRUE;
939 /* Need to recompute cursor column, e.g., when starting Visual mode
940 * without concealing. */
941 curs_columns(TRUE);
942 }
943}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#endif
945
Bram Moolenaar113e1072019-01-20 15:30:40 +0100946#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100948update_debug_sign(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 win_T *wp;
951 int doit = FALSE;
952
953# ifdef FEAT_FOLDING
954 win_foldinfo.fi_level = 0;
955# endif
956
Bram Moolenaar27a472c2019-01-09 21:47:30 +0100957 // update/delete a specific sign
958 redraw_buf_line_later(buf, lnum);
959
960 // check if it resulted in the need to redraw a window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (wp->w_redr_type != 0)
963 doit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100965 /* Return when there is nothing to do, screen updating is already
Bram Moolenaar07920482017-08-01 20:53:30 +0200966 * happening (recursive call), messages on the screen or still starting up.
967 */
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100968 if (!doit || updating_screen
Bram Moolenaar07920482017-08-01 20:53:30 +0200969 || State == ASKMORE || State == HITRETURN
970 || msg_scrolled
Bram Moolenaar738f8fc2012-01-10 12:42:09 +0100971#ifdef FEAT_GUI
972 || gui.starting
973#endif
974 || starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 return;
976
977 /* update all windows that need updating */
978 update_prepare();
979
Bram Moolenaar29323592016-07-24 22:04:11 +0200980 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
982 if (wp->w_redr_type != 0)
983 win_update(wp);
984 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +0200985 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988 update_finish();
989}
990#endif
991
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200992#ifdef FEAT_TEXT_PROP
993 static void
994update_popups(void)
995{
996 win_T *wp;
997 win_T *lowest_wp;
998 int lowest_zindex;
999
1000 // Reset all the VALID_POPUP flags.
1001 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001002 wp->w_popup_flags &= ~POPF_REDRAWN;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001003 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001004 wp->w_popup_flags &= ~POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001005
1006 // TODO: don't redraw every popup every time.
1007 for (;;)
1008 {
1009 // Find the window with the lowest zindex that hasn't been updated yet,
1010 // so that the window with a higher zindex is drawn later, thus goes on
1011 // top.
1012 lowest_zindex = INT_MAX;
1013 lowest_wp = NULL;
1014 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001015 if ((wp->w_popup_flags & (POPF_REDRAWN|POPF_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001016 && wp->w_zindex < lowest_zindex)
1017 {
1018 lowest_zindex = wp->w_zindex;
1019 lowest_wp = wp;
1020 }
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001021 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001022 if ((wp->w_popup_flags & (POPF_REDRAWN|POPF_HIDDEN)) == 0
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001023 && wp->w_zindex < lowest_zindex)
1024 {
1025 lowest_zindex = wp->w_zindex;
1026 lowest_wp = wp;
1027 }
1028
1029 if (lowest_wp == NULL)
1030 break;
1031 win_update(lowest_wp);
Bram Moolenaarbf0ecb22019-05-27 10:04:40 +02001032 lowest_wp->w_popup_flags |= POPF_REDRAWN;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001033 }
1034}
1035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001037/*
1038 * Get 'wincolor' attribute for window "wp". If not set and "wp" is a popup
1039 * window then get the "Pmenu" highlight attribute.
1040 */
1041 static int
1042get_wcr_attr(win_T *wp)
1043{
1044 int wcr_attr = 0;
1045
1046 if (*wp->w_p_wcr != NUL)
1047 wcr_attr = syn_name2attr(wp->w_p_wcr);
1048#ifdef FEAT_TEXT_PROP
1049 if (bt_popup(wp->w_buffer) && wcr_attr == 0)
1050 wcr_attr = HL_ATTR(HLF_PNI);
1051#endif
1052 return wcr_attr;
1053}
1054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#if defined(FEAT_GUI) || defined(PROTO)
1056/*
1057 * Update a single window, its status line and maybe the command line msg.
1058 * Used for the GUI scrollbar.
1059 */
1060 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001061updateWindow(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
Bram Moolenaar19f990e2009-11-25 12:08:03 +00001063 /* return if already busy updating */
1064 if (updating_screen)
1065 return;
1066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 update_prepare();
1068
1069#ifdef FEAT_CLIPBOARD
1070 /* When Visual area changed, may have to update selection. */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001071 if (clip_star.available && clip_isautosel_star())
1072 clip_update_selection(&clip_star);
1073 if (clip_plus.available && clip_isautosel_plus())
1074 clip_update_selection(&clip_plus);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 win_update(wp);
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001078
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001079 /* When the screen was cleared redraw the tab pages line. */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001080 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001081 draw_tabline();
Bram Moolenaar4c7ed462006-02-15 22:18:42 +00001082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (wp->w_redr_status
1084# ifdef FEAT_CMDL_INFO
1085 || p_ru
1086# endif
1087# ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001088 || *p_stl != NUL || *wp->w_p_stl != NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089# endif
1090 )
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02001091 win_redr_status(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 update_finish();
1094}
1095#endif
1096
1097/*
1098 * Update a single window.
1099 *
1100 * This may cause the windows below it also to be redrawn (when clearing the
1101 * screen or scrolling lines).
1102 *
1103 * How the window is redrawn depends on wp->w_redr_type. Each type also
1104 * implies the one below it.
1105 * NOT_VALID redraw the whole window
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001106 * SOME_VALID redraw the whole window but do scroll when possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 * REDRAW_TOP redraw the top w_upd_rows window lines, otherwise like VALID
1108 * INVERTED redraw the changed part of the Visual area
1109 * INVERTED_ALL redraw the whole Visual area
1110 * VALID 1. scroll up/down to adjust for a changed w_topline
1111 * 2. update lines at the top when scrolled down
1112 * 3. redraw changed text:
Bram Moolenaar75c50c42005-06-04 22:06:24 +00001113 * - if wp->w_buffer->b_mod_set set, update lines between
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 * b_mod_top and b_mod_bot.
1115 * - if wp->w_redraw_top non-zero, redraw lines between
1116 * wp->w_redraw_top and wp->w_redr_bot.
1117 * - continue redrawing when syntax status is invalid.
1118 * 4. if scrolled up, update lines at the bottom.
1119 * This results in three areas that may need updating:
1120 * top: from first row to top_end (when scrolled down)
1121 * mid: from mid_start to mid_end (update inversion or changed text)
1122 * bot: from bot_start to last row (when scrolled up)
1123 */
1124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001125win_update(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127 buf_T *buf = wp->w_buffer;
1128 int type;
1129 int top_end = 0; /* Below last row of the top area that needs
1130 updating. 0 when no top area updating. */
1131 int mid_start = 999;/* first row of the mid area that needs
1132 updating. 999 when no mid area updating. */
1133 int mid_end = 0; /* Below last row of the mid area that needs
1134 updating. 0 when no mid area updating. */
1135 int bot_start = 999;/* first row of the bot area that needs
1136 updating. 999 when no bot area updating */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 int scrolled_down = FALSE; /* TRUE when scrolled down when
1138 w_topline got smaller a bit */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001140 matchitem_T *cur; /* points to the match list */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int top_to_mod = FALSE; /* redraw above mod_top */
1142#endif
1143
1144 int row; /* current window row to display */
1145 linenr_T lnum; /* current buffer lnum to display */
1146 int idx; /* current index in w_lines[] */
1147 int srow; /* starting row of the current line */
1148
1149 int eof = FALSE; /* if TRUE, we hit the end of the file */
1150 int didline = FALSE; /* if TRUE, we finished the last line */
1151 int i;
1152 long j;
1153 static int recursive = FALSE; /* being called recursively */
1154 int old_botline = wp->w_botline;
1155#ifdef FEAT_FOLDING
1156 long fold_count;
1157#endif
1158#ifdef FEAT_SYN_HL
1159 /* remember what happened to the previous line, to know if
1160 * check_visual_highlight() can be used */
1161#define DID_NONE 1 /* didn't update a line */
1162#define DID_LINE 2 /* updated a normal line */
1163#define DID_FOLD 3 /* updated a folded line */
1164 int did_update = DID_NONE;
1165 linenr_T syntax_last_parsed = 0; /* last parsed text line */
1166#endif
1167 linenr_T mod_top = 0;
1168 linenr_T mod_bot = 0;
1169#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1170 int save_got_int;
1171#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001172#ifdef SYN_TIME_LIMIT
1173 proftime_T syntax_tm;
1174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 type = wp->w_redr_type;
1177
1178 if (type == NOT_VALID)
1179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 wp->w_lines_valid = 0;
1182 }
1183
1184 /* Window is zero-height: nothing to draw. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001185 if (wp->w_height + WINBAR_HEIGHT(wp) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
1187 wp->w_redr_type = 0;
1188 return;
1189 }
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 /* Window is zero-width: Only need to draw the separator. */
1192 if (wp->w_width == 0)
1193 {
1194 /* draw the vertical separator right of this window */
1195 draw_vsep_win(wp, 0);
1196 wp->w_redr_type = 0;
1197 return;
1198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001200#ifdef FEAT_TERMINAL
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001201 // If this window contains a terminal, redraw works completely differently.
1202 if (term_do_update_window(wp))
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001203 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02001204 term_update_window(wp);
Bram Moolenaar181ca992018-02-13 21:19:21 +01001205# ifdef FEAT_MENU
1206 /* Draw the window toolbar, if there is one. */
1207 if (winbar_height(wp) > 0)
1208 redraw_win_toolbar(wp);
1209# endif
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02001210 wp->w_redr_type = 0;
1211 return;
1212 }
1213#endif
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02001216 init_search_hl(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
1218
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001219#ifdef FEAT_LINEBREAK
Bram Moolenaar64486672010-05-16 15:46:46 +02001220 /* Force redraw when width of 'number' or 'relativenumber' column
1221 * changes. */
1222 i = (wp->w_p_nu || wp->w_p_rnu) ? number_width(wp) : 0;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001223 if (wp->w_nrwidth != i)
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001224 {
1225 type = NOT_VALID;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00001226 wp->w_nrwidth = i;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00001227 }
1228 else
1229#endif
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (buf->b_mod_set && buf->b_mod_xlines != 0 && wp->w_redraw_top != 0)
1232 {
1233 /*
1234 * When there are both inserted/deleted lines and specific lines to be
1235 * redrawn, w_redraw_top and w_redraw_bot may be invalid, just redraw
1236 * everything (only happens when redrawing is off for while).
1237 */
1238 type = NOT_VALID;
1239 }
1240 else
1241 {
1242 /*
1243 * Set mod_top to the first line that needs displaying because of
1244 * changes. Set mod_bot to the first line after the changes.
1245 */
1246 mod_top = wp->w_redraw_top;
1247 if (wp->w_redraw_bot != 0)
1248 mod_bot = wp->w_redraw_bot + 1;
1249 else
1250 mod_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 if (buf->b_mod_set)
1252 {
1253 if (mod_top == 0 || mod_top > buf->b_mod_top)
1254 {
1255 mod_top = buf->b_mod_top;
1256#ifdef FEAT_SYN_HL
1257 /* Need to redraw lines above the change that may be included
1258 * in a pattern match. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259 if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 mod_top -= buf->b_s.b_syn_sync_linebreaks;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (mod_top < 1)
1263 mod_top = 1;
1264 }
1265#endif
1266 }
1267 if (mod_bot == 0 || mod_bot < buf->b_mod_bot)
1268 mod_bot = buf->b_mod_bot;
1269
1270#ifdef FEAT_SEARCH_EXTRA
1271 /* When 'hlsearch' is on and using a multi-line search pattern, a
1272 * change in one line may make the Search highlighting in a
1273 * previous line invalid. Simple solution: redraw all visible
1274 * lines above the change.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001275 * Same for a match pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 */
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001277 if (search_hl.rm.regprog != NULL
1278 && re_multiline(search_hl.rm.regprog))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 top_to_mod = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001280 else
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001281 {
1282 cur = wp->w_match_head;
1283 while (cur != NULL)
1284 {
1285 if (cur->match.regprog != NULL
1286 && re_multiline(cur->match.regprog))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001287 {
1288 top_to_mod = TRUE;
1289 break;
1290 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00001291 cur = cur->next;
1292 }
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294#endif
1295 }
1296#ifdef FEAT_FOLDING
1297 if (mod_top != 0 && hasAnyFolding(wp))
1298 {
1299 linenr_T lnumt, lnumb;
1300
1301 /*
1302 * A change in a line can cause lines above it to become folded or
1303 * unfolded. Find the top most buffer line that may be affected.
1304 * If the line was previously folded and displayed, get the first
1305 * line of that fold. If the line is folded now, get the first
1306 * folded line. Use the minimum of these two.
1307 */
1308
1309 /* Find last valid w_lines[] entry above mod_top. Set lnumt to
1310 * the line below it. If there is no valid entry, use w_topline.
1311 * Find the first valid w_lines[] entry below mod_bot. Set lnumb
1312 * to this line. If there is no valid entry, use MAXLNUM. */
1313 lnumt = wp->w_topline;
1314 lnumb = MAXLNUM;
1315 for (i = 0; i < wp->w_lines_valid; ++i)
1316 if (wp->w_lines[i].wl_valid)
1317 {
1318 if (wp->w_lines[i].wl_lastlnum < mod_top)
1319 lnumt = wp->w_lines[i].wl_lastlnum + 1;
1320 if (lnumb == MAXLNUM && wp->w_lines[i].wl_lnum >= mod_bot)
1321 {
1322 lnumb = wp->w_lines[i].wl_lnum;
1323 /* When there is a fold column it might need updating
1324 * in the next line ("J" just above an open fold). */
Bram Moolenaar1c934292015-01-27 16:39:29 +01001325 if (compute_foldcolumn(wp, 0) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 ++lnumb;
1327 }
1328 }
1329
1330 (void)hasFoldingWin(wp, mod_top, &mod_top, NULL, TRUE, NULL);
1331 if (mod_top > lnumt)
1332 mod_top = lnumt;
1333
1334 /* Now do the same for the bottom line (one above mod_bot). */
1335 --mod_bot;
1336 (void)hasFoldingWin(wp, mod_bot, NULL, &mod_bot, TRUE, NULL);
1337 ++mod_bot;
1338 if (mod_bot < lnumb)
1339 mod_bot = lnumb;
1340 }
1341#endif
1342
1343 /* When a change starts above w_topline and the end is below
1344 * w_topline, start redrawing at w_topline.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001345 * If the end of the change is above w_topline: do like no change was
1346 * made, but redraw the first line to find changes in syntax. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (mod_top != 0 && mod_top < wp->w_topline)
1348 {
1349 if (mod_bot > wp->w_topline)
1350 mod_top = wp->w_topline;
1351#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001352 else if (syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 top_end = 1;
1354#endif
1355 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001356
1357 /* When line numbers are displayed need to redraw all lines below
1358 * inserted/deleted lines. */
1359 if (mod_top != 0 && buf->b_mod_xlines != 0 && wp->w_p_nu)
1360 mod_bot = MAXLNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
Bram Moolenaar895d9662019-01-31 21:57:21 +01001362 wp->w_redraw_top = 0; // reset for next time
1363 wp->w_redraw_bot = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 /*
1366 * When only displaying the lines at the top, set top_end. Used when
1367 * window has scrolled down for msg_scrolled.
1368 */
1369 if (type == REDRAW_TOP)
1370 {
1371 j = 0;
1372 for (i = 0; i < wp->w_lines_valid; ++i)
1373 {
1374 j += wp->w_lines[i].wl_size;
1375 if (j >= wp->w_upd_rows)
1376 {
1377 top_end = j;
1378 break;
1379 }
1380 }
1381 if (top_end == 0)
1382 /* not found (cannot happen?): redraw everything */
1383 type = NOT_VALID;
1384 else
1385 /* top area defined, the rest is VALID */
1386 type = VALID;
1387 }
1388
Bram Moolenaar367329b2007-08-30 11:53:22 +00001389 /* Trick: we want to avoid clearing the screen twice. screenclear() will
Bram Moolenaar943fae42007-07-30 20:00:38 +00001390 * set "screen_cleared" to TRUE. The special value MAYBE (which is still
1391 * non-zero and thus not FALSE) will indicate that screenclear() was not
1392 * called. */
1393 if (screen_cleared)
1394 screen_cleared = MAYBE;
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 /*
1397 * If there are no changes on the screen that require a complete redraw,
1398 * handle three cases:
1399 * 1: we are off the top of the screen by a few lines: scroll down
1400 * 2: wp->w_topline is below wp->w_lines[0].wl_lnum: may scroll up
1401 * 3: wp->w_topline is wp->w_lines[0].wl_lnum: find first entry in
1402 * w_lines[] that needs updating.
1403 */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001404 if ((type == VALID || type == SOME_VALID
1405 || type == INVERTED || type == INVERTED_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406#ifdef FEAT_DIFF
1407 && !wp->w_botfill && !wp->w_old_botfill
1408#endif
1409 )
1410 {
1411 if (mod_top != 0 && wp->w_topline == mod_top)
1412 {
1413 /*
1414 * w_topline is the first changed line, the scrolling will be done
1415 * further down.
1416 */
1417 }
1418 else if (wp->w_lines[0].wl_valid
1419 && (wp->w_topline < wp->w_lines[0].wl_lnum
1420#ifdef FEAT_DIFF
1421 || (wp->w_topline == wp->w_lines[0].wl_lnum
1422 && wp->w_topfill > wp->w_old_topfill)
1423#endif
1424 ))
1425 {
1426 /*
1427 * New topline is above old topline: May scroll down.
1428 */
1429#ifdef FEAT_FOLDING
1430 if (hasAnyFolding(wp))
1431 {
1432 linenr_T ln;
1433
1434 /* count the number of lines we are off, counting a sequence
1435 * of folded lines as one */
1436 j = 0;
1437 for (ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ++ln)
1438 {
1439 ++j;
1440 if (j >= wp->w_height - 2)
1441 break;
1442 (void)hasFoldingWin(wp, ln, NULL, &ln, TRUE, NULL);
1443 }
1444 }
1445 else
1446#endif
1447 j = wp->w_lines[0].wl_lnum - wp->w_topline;
1448 if (j < wp->w_height - 2) /* not too far off */
1449 {
1450 i = plines_m_win(wp, wp->w_topline, wp->w_lines[0].wl_lnum - 1);
1451#ifdef FEAT_DIFF
1452 /* insert extra lines for previously invisible filler lines */
1453 if (wp->w_lines[0].wl_lnum != wp->w_topline)
1454 i += diff_check_fill(wp, wp->w_lines[0].wl_lnum)
1455 - wp->w_old_topfill;
1456#endif
1457 if (i < wp->w_height - 2) /* less than a screen off */
1458 {
1459 /*
1460 * Try to insert the correct number of lines.
1461 * If not the last window, delete the lines at the bottom.
1462 * win_ins_lines may fail when the terminal can't do it.
1463 */
1464 if (i > 0)
1465 check_for_delay(FALSE);
1466 if (win_ins_lines(wp, 0, i, FALSE, wp == firstwin) == OK)
1467 {
1468 if (wp->w_lines_valid != 0)
1469 {
1470 /* Need to update rows that are new, stop at the
1471 * first one that scrolled down. */
1472 top_end = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 scrolled_down = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475 /* Move the entries that were scrolled, disable
1476 * the entries for the lines to be redrawn. */
1477 if ((wp->w_lines_valid += j) > wp->w_height)
1478 wp->w_lines_valid = wp->w_height;
1479 for (idx = wp->w_lines_valid; idx - j >= 0; idx--)
1480 wp->w_lines[idx] = wp->w_lines[idx - j];
1481 while (idx >= 0)
1482 wp->w_lines[idx--].wl_valid = FALSE;
1483 }
1484 }
1485 else
1486 mid_start = 0; /* redraw all lines */
1487 }
1488 else
1489 mid_start = 0; /* redraw all lines */
1490 }
1491 else
1492 mid_start = 0; /* redraw all lines */
1493 }
1494 else
1495 {
1496 /*
1497 * New topline is at or below old topline: May scroll up.
1498 * When topline didn't change, find first entry in w_lines[] that
1499 * needs updating.
1500 */
1501
1502 /* try to find wp->w_topline in wp->w_lines[].wl_lnum */
1503 j = -1;
1504 row = 0;
1505 for (i = 0; i < wp->w_lines_valid; i++)
1506 {
1507 if (wp->w_lines[i].wl_valid
1508 && wp->w_lines[i].wl_lnum == wp->w_topline)
1509 {
1510 j = i;
1511 break;
1512 }
1513 row += wp->w_lines[i].wl_size;
1514 }
1515 if (j == -1)
1516 {
1517 /* if wp->w_topline is not in wp->w_lines[].wl_lnum redraw all
1518 * lines */
1519 mid_start = 0;
1520 }
1521 else
1522 {
1523 /*
1524 * Try to delete the correct number of lines.
1525 * wp->w_topline is at wp->w_lines[i].wl_lnum.
1526 */
1527#ifdef FEAT_DIFF
1528 /* If the topline didn't change, delete old filler lines,
1529 * otherwise delete filler lines of the new topline... */
1530 if (wp->w_lines[0].wl_lnum == wp->w_topline)
1531 row += wp->w_old_topfill;
1532 else
1533 row += diff_check_fill(wp, wp->w_topline);
1534 /* ... but don't delete new filler lines. */
1535 row -= wp->w_topfill;
1536#endif
1537 if (row > 0)
1538 {
1539 check_for_delay(FALSE);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001540 if (win_del_lines(wp, 0, row, FALSE, wp == firstwin, 0)
1541 == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 bot_start = wp->w_height - row;
1543 else
1544 mid_start = 0; /* redraw all lines */
1545 }
1546 if ((row == 0 || bot_start < 999) && wp->w_lines_valid != 0)
1547 {
1548 /*
1549 * Skip the lines (below the deleted lines) that are still
1550 * valid and don't need redrawing. Copy their info
1551 * upwards, to compensate for the deleted lines. Set
1552 * bot_start to the first row that needs redrawing.
1553 */
1554 bot_start = 0;
1555 idx = 0;
1556 for (;;)
1557 {
1558 wp->w_lines[idx] = wp->w_lines[j];
1559 /* stop at line that didn't fit, unless it is still
1560 * valid (no lines deleted) */
1561 if (row > 0 && bot_start + row
1562 + (int)wp->w_lines[j].wl_size > wp->w_height)
1563 {
1564 wp->w_lines_valid = idx + 1;
1565 break;
1566 }
1567 bot_start += wp->w_lines[idx++].wl_size;
1568
1569 /* stop at the last valid entry in w_lines[].wl_size */
1570 if (++j >= wp->w_lines_valid)
1571 {
1572 wp->w_lines_valid = idx;
1573 break;
1574 }
1575 }
1576#ifdef FEAT_DIFF
1577 /* Correct the first entry for filler lines at the top
1578 * when it won't get updated below. */
1579 if (wp->w_p_diff && bot_start > 0)
1580 wp->w_lines[0].wl_size =
1581 plines_win_nofill(wp, wp->w_topline, TRUE)
1582 + wp->w_topfill;
1583#endif
1584 }
1585 }
1586 }
1587
1588 /* When starting redraw in the first line, redraw all lines. When
1589 * there is only one window it's probably faster to clear the screen
1590 * first. */
1591 if (mid_start == 0)
1592 {
1593 mid_end = wp->w_height;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001594 if (ONE_WINDOW)
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001595 {
Bram Moolenaar943fae42007-07-30 20:00:38 +00001596 /* Clear the screen when it was not done by win_del_lines() or
1597 * win_ins_lines() above, "screen_cleared" is FALSE or MAYBE
1598 * then. */
1599 if (screen_cleared != TRUE)
1600 screenclear();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001601 /* The screen was cleared, redraw the tab pages line. */
1602 if (redraw_tabline)
1603 draw_tabline();
Bram Moolenaarbc1a7c32006-09-14 19:04:14 +00001604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
Bram Moolenaar943fae42007-07-30 20:00:38 +00001606
1607 /* When win_del_lines() or win_ins_lines() caused the screen to be
1608 * cleared (only happens for the first window) or when screenclear()
1609 * was called directly above, "must_redraw" will have been set to
1610 * NOT_VALID, need to reset it here to avoid redrawing twice. */
1611 if (screen_cleared == TRUE)
1612 must_redraw = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 }
1614 else
1615 {
1616 /* Not VALID or INVERTED: redraw all lines. */
1617 mid_start = 0;
1618 mid_end = wp->w_height;
1619 }
1620
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001621 if (type == SOME_VALID)
1622 {
1623 /* SOME_VALID: redraw all lines. */
1624 mid_start = 0;
1625 mid_end = wp->w_height;
1626 type = NOT_VALID;
1627 }
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 /* check if we are updating or removing the inverted part */
1630 if ((VIsual_active && buf == curwin->w_buffer)
1631 || (wp->w_old_cursor_lnum != 0 && type != NOT_VALID))
1632 {
1633 linenr_T from, to;
1634
1635 if (VIsual_active)
1636 {
1637 if (VIsual_active
1638 && (VIsual_mode != wp->w_old_visual_mode
1639 || type == INVERTED_ALL))
1640 {
1641 /*
1642 * If the type of Visual selection changed, redraw the whole
1643 * selection. Also when the ownership of the X selection is
1644 * gained or lost.
1645 */
1646 if (curwin->w_cursor.lnum < VIsual.lnum)
1647 {
1648 from = curwin->w_cursor.lnum;
1649 to = VIsual.lnum;
1650 }
1651 else
1652 {
1653 from = VIsual.lnum;
1654 to = curwin->w_cursor.lnum;
1655 }
1656 /* redraw more when the cursor moved as well */
1657 if (wp->w_old_cursor_lnum < from)
1658 from = wp->w_old_cursor_lnum;
1659 if (wp->w_old_cursor_lnum > to)
1660 to = wp->w_old_cursor_lnum;
1661 if (wp->w_old_visual_lnum < from)
1662 from = wp->w_old_visual_lnum;
1663 if (wp->w_old_visual_lnum > to)
1664 to = wp->w_old_visual_lnum;
1665 }
1666 else
1667 {
1668 /*
1669 * Find the line numbers that need to be updated: The lines
1670 * between the old cursor position and the current cursor
1671 * position. Also check if the Visual position changed.
1672 */
1673 if (curwin->w_cursor.lnum < wp->w_old_cursor_lnum)
1674 {
1675 from = curwin->w_cursor.lnum;
1676 to = wp->w_old_cursor_lnum;
1677 }
1678 else
1679 {
1680 from = wp->w_old_cursor_lnum;
1681 to = curwin->w_cursor.lnum;
1682 if (from == 0) /* Visual mode just started */
1683 from = to;
1684 }
1685
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001686 if (VIsual.lnum != wp->w_old_visual_lnum
1687 || VIsual.col != wp->w_old_visual_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
1689 if (wp->w_old_visual_lnum < from
1690 && wp->w_old_visual_lnum != 0)
1691 from = wp->w_old_visual_lnum;
1692 if (wp->w_old_visual_lnum > to)
1693 to = wp->w_old_visual_lnum;
1694 if (VIsual.lnum < from)
1695 from = VIsual.lnum;
1696 if (VIsual.lnum > to)
1697 to = VIsual.lnum;
1698 }
1699 }
1700
1701 /*
1702 * If in block mode and changed column or curwin->w_curswant:
1703 * update all lines.
1704 * First compute the actual start and end column.
1705 */
1706 if (VIsual_mode == Ctrl_V)
1707 {
Bram Moolenaar404406a2014-10-09 13:24:43 +02001708 colnr_T fromc, toc;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001709#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001710 int save_ve_flags = ve_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar404406a2014-10-09 13:24:43 +02001712 if (curwin->w_p_lbr)
1713 ve_flags = VE_ALL;
1714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 getvcols(wp, &VIsual, &curwin->w_cursor, &fromc, &toc);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001716#if defined(FEAT_LINEBREAK)
Bram Moolenaar404406a2014-10-09 13:24:43 +02001717 ve_flags = save_ve_flags;
1718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 ++toc;
1720 if (curwin->w_curswant == MAXCOL)
1721 toc = MAXCOL;
1722
1723 if (fromc != wp->w_old_cursor_fcol
1724 || toc != wp->w_old_cursor_lcol)
1725 {
1726 if (from > VIsual.lnum)
1727 from = VIsual.lnum;
1728 if (to < VIsual.lnum)
1729 to = VIsual.lnum;
1730 }
1731 wp->w_old_cursor_fcol = fromc;
1732 wp->w_old_cursor_lcol = toc;
1733 }
1734 }
1735 else
1736 {
1737 /* Use the line numbers of the old Visual area. */
1738 if (wp->w_old_cursor_lnum < wp->w_old_visual_lnum)
1739 {
1740 from = wp->w_old_cursor_lnum;
1741 to = wp->w_old_visual_lnum;
1742 }
1743 else
1744 {
1745 from = wp->w_old_visual_lnum;
1746 to = wp->w_old_cursor_lnum;
1747 }
1748 }
1749
1750 /*
1751 * There is no need to update lines above the top of the window.
1752 */
1753 if (from < wp->w_topline)
1754 from = wp->w_topline;
1755
1756 /*
1757 * If we know the value of w_botline, use it to restrict the update to
1758 * the lines that are visible in the window.
1759 */
1760 if (wp->w_valid & VALID_BOTLINE)
1761 {
1762 if (from >= wp->w_botline)
1763 from = wp->w_botline - 1;
1764 if (to >= wp->w_botline)
1765 to = wp->w_botline - 1;
1766 }
1767
1768 /*
1769 * Find the minimal part to be updated.
1770 * Watch out for scrolling that made entries in w_lines[] invalid.
1771 * E.g., CTRL-U makes the first half of w_lines[] invalid and sets
1772 * top_end; need to redraw from top_end to the "to" line.
1773 * A middle mouse click with a Visual selection may change the text
1774 * above the Visual area and reset wl_valid, do count these for
1775 * mid_end (in srow).
1776 */
1777 if (mid_start > 0)
1778 {
1779 lnum = wp->w_topline;
1780 idx = 0;
1781 srow = 0;
1782 if (scrolled_down)
1783 mid_start = top_end;
1784 else
1785 mid_start = 0;
1786 while (lnum < from && idx < wp->w_lines_valid) /* find start */
1787 {
1788 if (wp->w_lines[idx].wl_valid)
1789 mid_start += wp->w_lines[idx].wl_size;
1790 else if (!scrolled_down)
1791 srow += wp->w_lines[idx].wl_size;
1792 ++idx;
1793# ifdef FEAT_FOLDING
1794 if (idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid)
1795 lnum = wp->w_lines[idx].wl_lnum;
1796 else
1797# endif
1798 ++lnum;
1799 }
1800 srow += mid_start;
1801 mid_end = wp->w_height;
1802 for ( ; idx < wp->w_lines_valid; ++idx) /* find end */
1803 {
1804 if (wp->w_lines[idx].wl_valid
1805 && wp->w_lines[idx].wl_lnum >= to + 1)
1806 {
1807 /* Only update until first row of this line */
1808 mid_end = srow;
1809 break;
1810 }
1811 srow += wp->w_lines[idx].wl_size;
1812 }
1813 }
1814 }
1815
1816 if (VIsual_active && buf == curwin->w_buffer)
1817 {
1818 wp->w_old_visual_mode = VIsual_mode;
1819 wp->w_old_cursor_lnum = curwin->w_cursor.lnum;
1820 wp->w_old_visual_lnum = VIsual.lnum;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001821 wp->w_old_visual_col = VIsual.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 wp->w_old_curswant = curwin->w_curswant;
1823 }
1824 else
1825 {
1826 wp->w_old_visual_mode = 0;
1827 wp->w_old_cursor_lnum = 0;
1828 wp->w_old_visual_lnum = 0;
Bram Moolenaar6c131c42005-07-19 22:17:30 +00001829 wp->w_old_visual_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
1833 /* reset got_int, otherwise regexp won't work */
1834 save_got_int = got_int;
1835 got_int = 0;
1836#endif
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001837#ifdef SYN_TIME_LIMIT
1838 /* Set the time limit to 'redrawtime'. */
1839 profile_setlimit(p_rdt, &syntax_tm);
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02001840 syn_set_timeout(&syntax_tm);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02001841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#ifdef FEAT_FOLDING
1843 win_foldinfo.fi_level = 0;
1844#endif
1845
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02001846#ifdef FEAT_MENU
1847 /*
1848 * Draw the window toolbar, if there is one.
1849 * TODO: only when needed.
1850 */
1851 if (winbar_height(wp) > 0)
1852 redraw_win_toolbar(wp);
1853#endif
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 /*
1856 * Update all the window rows.
1857 */
1858 idx = 0; /* first entry in w_lines[].wl_size */
1859 row = 0;
1860 srow = 0;
1861 lnum = wp->w_topline; /* first line shown in window */
1862 for (;;)
1863 {
1864 /* stop updating when reached the end of the window (check for _past_
1865 * the end of the window is at the end of the loop) */
1866 if (row == wp->w_height)
1867 {
1868 didline = TRUE;
1869 break;
1870 }
1871
1872 /* stop updating when hit the end of the file */
1873 if (lnum > buf->b_ml.ml_line_count)
1874 {
1875 eof = TRUE;
1876 break;
1877 }
1878
1879 /* Remember the starting row of the line that is going to be dealt
1880 * with. It is used further down when the line doesn't fit. */
1881 srow = row;
1882
1883 /*
1884 * Update a line when it is in an area that needs updating, when it
1885 * has changes or w_lines[idx] is invalid.
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02001886 * "bot_start" may be halfway a wrapped line after using
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 * win_del_lines(), check if the current line includes it.
1888 * When syntax folding is being used, the saved syntax states will
1889 * already have been updated, we can't see where the syntax state is
1890 * the same again, just update until the end of the window.
1891 */
1892 if (row < top_end
1893 || (row >= mid_start && row < mid_end)
1894#ifdef FEAT_SEARCH_EXTRA
1895 || top_to_mod
1896#endif
1897 || idx >= wp->w_lines_valid
1898 || (row + wp->w_lines[idx].wl_size > bot_start)
1899 || (mod_top != 0
1900 && (lnum == mod_top
1901 || (lnum >= mod_top
1902 && (lnum < mod_bot
1903#ifdef FEAT_SYN_HL
1904 || did_update == DID_FOLD
1905 || (did_update == DID_LINE
Bram Moolenaar860cae12010-06-05 23:22:07 +02001906 && syntax_present(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 && (
1908# ifdef FEAT_FOLDING
1909 (foldmethodIsSyntax(wp)
1910 && hasAnyFolding(wp)) ||
1911# endif
1912 syntax_check_changed(lnum)))
1913#endif
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001914#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardab70c62014-07-02 17:16:58 +02001915 /* match in fixed position might need redraw
1916 * if lines were inserted or deleted */
1917 || (wp->w_match_head != NULL
1918 && buf->b_mod_xlines != 0)
Bram Moolenaar4ce239b2013-06-15 23:00:30 +02001919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 )))))
1921 {
1922#ifdef FEAT_SEARCH_EXTRA
1923 if (lnum == mod_top)
1924 top_to_mod = FALSE;
1925#endif
1926
1927 /*
1928 * When at start of changed lines: May scroll following lines
1929 * up or down to minimize redrawing.
1930 * Don't do this when the change continues until the end.
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001931 * Don't scroll when dollar_vcol >= 0, keep the "$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 */
1933 if (lnum == mod_top
1934 && mod_bot != MAXLNUM
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001935 && !(dollar_vcol >= 0 && mod_bot == mod_top + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 {
1937 int old_rows = 0;
1938 int new_rows = 0;
1939 int xtra_rows;
1940 linenr_T l;
1941
1942 /* Count the old number of window rows, using w_lines[], which
1943 * should still contain the sizes for the lines as they are
1944 * currently displayed. */
1945 for (i = idx; i < wp->w_lines_valid; ++i)
1946 {
1947 /* Only valid lines have a meaningful wl_lnum. Invalid
1948 * lines are part of the changed area. */
1949 if (wp->w_lines[i].wl_valid
1950 && wp->w_lines[i].wl_lnum == mod_bot)
1951 break;
1952 old_rows += wp->w_lines[i].wl_size;
1953#ifdef FEAT_FOLDING
1954 if (wp->w_lines[i].wl_valid
1955 && wp->w_lines[i].wl_lastlnum + 1 == mod_bot)
1956 {
1957 /* Must have found the last valid entry above mod_bot.
1958 * Add following invalid entries. */
1959 ++i;
1960 while (i < wp->w_lines_valid
1961 && !wp->w_lines[i].wl_valid)
1962 old_rows += wp->w_lines[i++].wl_size;
1963 break;
1964 }
1965#endif
1966 }
1967
1968 if (i >= wp->w_lines_valid)
1969 {
1970 /* We can't find a valid line below the changed lines,
1971 * need to redraw until the end of the window.
1972 * Inserting/deleting lines has no use. */
1973 bot_start = 0;
1974 }
1975 else
1976 {
1977 /* Able to count old number of rows: Count new window
1978 * rows, and may insert/delete lines */
1979 j = idx;
1980 for (l = lnum; l < mod_bot; ++l)
1981 {
1982#ifdef FEAT_FOLDING
1983 if (hasFoldingWin(wp, l, NULL, &l, TRUE, NULL))
1984 ++new_rows;
1985 else
1986#endif
1987#ifdef FEAT_DIFF
1988 if (l == wp->w_topline)
1989 new_rows += plines_win_nofill(wp, l, TRUE)
1990 + wp->w_topfill;
1991 else
1992#endif
1993 new_rows += plines_win(wp, l, TRUE);
1994 ++j;
1995 if (new_rows > wp->w_height - row - 2)
1996 {
1997 /* it's getting too much, must redraw the rest */
1998 new_rows = 9999;
1999 break;
2000 }
2001 }
2002 xtra_rows = new_rows - old_rows;
2003 if (xtra_rows < 0)
2004 {
2005 /* May scroll text up. If there is not enough
2006 * remaining text or scrolling fails, must redraw the
2007 * rest. If scrolling works, must redraw the text
2008 * below the scrolled text. */
2009 if (row - xtra_rows >= wp->w_height - 2)
2010 mod_bot = MAXLNUM;
2011 else
2012 {
2013 check_for_delay(FALSE);
2014 if (win_del_lines(wp, row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02002015 -xtra_rows, FALSE, FALSE, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 mod_bot = MAXLNUM;
2017 else
2018 bot_start = wp->w_height + xtra_rows;
2019 }
2020 }
2021 else if (xtra_rows > 0)
2022 {
2023 /* May scroll text down. If there is not enough
2024 * remaining text of scrolling fails, must redraw the
2025 * rest. */
2026 if (row + xtra_rows >= wp->w_height - 2)
2027 mod_bot = MAXLNUM;
2028 else
2029 {
2030 check_for_delay(FALSE);
2031 if (win_ins_lines(wp, row + old_rows,
2032 xtra_rows, FALSE, FALSE) == FAIL)
2033 mod_bot = MAXLNUM;
2034 else if (top_end > row + old_rows)
2035 /* Scrolled the part at the top that requires
2036 * updating down. */
2037 top_end += xtra_rows;
2038 }
2039 }
2040
2041 /* When not updating the rest, may need to move w_lines[]
2042 * entries. */
2043 if (mod_bot != MAXLNUM && i != j)
2044 {
2045 if (j < i)
2046 {
2047 int x = row + new_rows;
2048
2049 /* move entries in w_lines[] upwards */
2050 for (;;)
2051 {
2052 /* stop at last valid entry in w_lines[] */
2053 if (i >= wp->w_lines_valid)
2054 {
2055 wp->w_lines_valid = j;
2056 break;
2057 }
2058 wp->w_lines[j] = wp->w_lines[i];
2059 /* stop at a line that won't fit */
2060 if (x + (int)wp->w_lines[j].wl_size
2061 > wp->w_height)
2062 {
2063 wp->w_lines_valid = j + 1;
2064 break;
2065 }
2066 x += wp->w_lines[j++].wl_size;
2067 ++i;
2068 }
2069 if (bot_start > x)
2070 bot_start = x;
2071 }
2072 else /* j > i */
2073 {
2074 /* move entries in w_lines[] downwards */
2075 j -= i;
2076 wp->w_lines_valid += j;
2077 if (wp->w_lines_valid > wp->w_height)
2078 wp->w_lines_valid = wp->w_height;
2079 for (i = wp->w_lines_valid; i - j >= idx; --i)
2080 wp->w_lines[i] = wp->w_lines[i - j];
2081
2082 /* The w_lines[] entries for inserted lines are
2083 * now invalid, but wl_size may be used above.
2084 * Reset to zero. */
2085 while (i >= idx)
2086 {
2087 wp->w_lines[i].wl_size = 0;
2088 wp->w_lines[i--].wl_valid = FALSE;
2089 }
2090 }
2091 }
2092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
2094
2095#ifdef FEAT_FOLDING
2096 /*
2097 * When lines are folded, display one line for all of them.
2098 * Otherwise, display normally (can be several display lines when
2099 * 'wrap' is on).
2100 */
2101 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2102 if (fold_count != 0)
2103 {
2104 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
2105 ++row;
2106 --fold_count;
2107 wp->w_lines[idx].wl_folded = TRUE;
2108 wp->w_lines[idx].wl_lastlnum = lnum + fold_count;
2109# ifdef FEAT_SYN_HL
2110 did_update = DID_FOLD;
2111# endif
2112 }
2113 else
2114#endif
2115 if (idx < wp->w_lines_valid
2116 && wp->w_lines[idx].wl_valid
2117 && wp->w_lines[idx].wl_lnum == lnum
2118 && lnum > wp->w_topline
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002119 && !(dy_flags & (DY_LASTLINE | DY_TRUNCATE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 && srow + wp->w_lines[idx].wl_size > wp->w_height
2121#ifdef FEAT_DIFF
2122 && diff_check_fill(wp, lnum) == 0
2123#endif
2124 )
2125 {
2126 /* This line is not going to fit. Don't draw anything here,
2127 * will draw "@ " lines below. */
2128 row = wp->w_height + 1;
2129 }
2130 else
2131 {
2132#ifdef FEAT_SEARCH_EXTRA
2133 prepare_search_hl(wp, lnum);
2134#endif
2135#ifdef FEAT_SYN_HL
2136 /* Let the syntax stuff know we skipped a few lines. */
2137 if (syntax_last_parsed != 0 && syntax_last_parsed + 1 < lnum
Bram Moolenaar860cae12010-06-05 23:22:07 +02002138 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 syntax_end_parsing(syntax_last_parsed + 1);
2140#endif
2141
2142 /*
2143 * Display one line.
2144 */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002145 row = win_line(wp, lnum, srow, wp->w_height,
2146 mod_top == 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148#ifdef FEAT_FOLDING
2149 wp->w_lines[idx].wl_folded = FALSE;
2150 wp->w_lines[idx].wl_lastlnum = lnum;
2151#endif
2152#ifdef FEAT_SYN_HL
2153 did_update = DID_LINE;
2154 syntax_last_parsed = lnum;
2155#endif
2156 }
2157
2158 wp->w_lines[idx].wl_lnum = lnum;
2159 wp->w_lines[idx].wl_valid = TRUE;
Bram Moolenaar0e19fc02017-10-28 14:45:16 +02002160
2161 /* Past end of the window or end of the screen. Note that after
2162 * resizing wp->w_height may be end up too big. That's a problem
2163 * elsewhere, but prevent a crash here. */
2164 if (row > wp->w_height || row + wp->w_winrow >= Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 /* we may need the size of that too long line later on */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002167 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 wp->w_lines[idx].wl_size = plines_win(wp, lnum, TRUE);
2169 ++idx;
2170 break;
2171 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002172 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 wp->w_lines[idx].wl_size = row - srow;
2174 ++idx;
2175#ifdef FEAT_FOLDING
2176 lnum += fold_count + 1;
2177#else
2178 ++lnum;
2179#endif
2180 }
2181 else
2182 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002183 if (wp->w_p_rnu)
2184 {
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002185#ifdef FEAT_FOLDING
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002186 // 'relativenumber' set: The text doesn't need to be drawn, but
2187 // the number column nearly always does.
Bram Moolenaar7701f302018-10-02 21:20:32 +02002188 fold_count = foldedCount(wp, lnum, &win_foldinfo);
2189 if (fold_count != 0)
Bram Moolenaar7701f302018-10-02 21:20:32 +02002190 fold_line(wp, fold_count, &win_foldinfo, lnum, row);
Bram Moolenaar7701f302018-10-02 21:20:32 +02002191 else
Bram Moolenaar0e9deef2018-10-02 21:48:34 +02002192#endif
Bram Moolenaar7701f302018-10-02 21:20:32 +02002193 (void)win_line(wp, lnum, srow, wp->w_height, TRUE, TRUE);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02002194 }
2195
2196 // This line does not need to be drawn, advance to the next one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 row += wp->w_lines[idx++].wl_size;
2198 if (row > wp->w_height) /* past end of screen */
2199 break;
2200#ifdef FEAT_FOLDING
2201 lnum = wp->w_lines[idx - 1].wl_lastlnum + 1;
2202#else
2203 ++lnum;
2204#endif
2205#ifdef FEAT_SYN_HL
2206 did_update = DID_NONE;
2207#endif
2208 }
2209
2210 if (lnum > buf->b_ml.ml_line_count)
2211 {
2212 eof = TRUE;
2213 break;
2214 }
2215 }
2216 /*
2217 * End of loop over all window lines.
2218 */
2219
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002220#ifdef FEAT_VTP
2221 /* Rewrite the character at the end of the screen line. */
2222 if (use_vtp())
2223 {
2224 int i;
2225
2226 for (i = 0; i < Rows; ++i)
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002227 if (enc_utf8)
2228 if ((*mb_off2cells)(LineOffset[i] + Columns - 2,
2229 LineOffset[i] + screen_Columns) > 1)
2230 screen_draw_rectangle(i, Columns - 2, 1, 2, FALSE);
2231 else
2232 screen_draw_rectangle(i, Columns - 1, 1, 1, FALSE);
2233 else
Bram Moolenaarcafafb32018-02-22 21:07:09 +01002234 screen_char(LineOffset[i] + Columns - 1, i, Columns - 1);
2235 }
2236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
2238 if (idx > wp->w_lines_valid)
2239 wp->w_lines_valid = idx;
2240
2241#ifdef FEAT_SYN_HL
2242 /*
2243 * Let the syntax stuff know we stop parsing here.
2244 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02002245 if (syntax_last_parsed != 0 && syntax_present(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 syntax_end_parsing(syntax_last_parsed + 1);
2247#endif
2248
2249 /*
2250 * If we didn't hit the end of the file, and we didn't finish the last
2251 * line we were working on, then the line didn't fit.
2252 */
2253 wp->w_empty_rows = 0;
2254#ifdef FEAT_DIFF
2255 wp->w_filler_rows = 0;
2256#endif
2257 if (!eof && !didline)
2258 {
2259 if (lnum == wp->w_topline)
2260 {
2261 /*
2262 * Single line that does not fit!
2263 * Don't overwrite it, it can be edited.
2264 */
2265 wp->w_botline = lnum + 1;
2266 }
2267#ifdef FEAT_DIFF
2268 else if (diff_check_fill(wp, lnum) >= wp->w_height - srow)
2269 {
2270 /* Window ends in filler lines. */
2271 wp->w_botline = lnum;
2272 wp->w_filler_rows = wp->w_height - srow;
2273 }
2274#endif
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002275 else if (dy_flags & DY_TRUNCATE) /* 'display' has "truncate" */
2276 {
2277 int scr_row = W_WINROW(wp) + wp->w_height - 1;
2278
2279 /*
2280 * Last line isn't finished: Display "@@@" in the last screen line.
2281 */
Bram Moolenaar53f81742017-09-22 14:35:51 +02002282 screen_puts_len((char_u *)"@@", 2, scr_row, wp->w_wincol,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002283 HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002284 screen_fill(scr_row, scr_row + 1,
Bram Moolenaar53f81742017-09-22 14:35:51 +02002285 (int)wp->w_wincol + 2, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002286 '@', ' ', HL_ATTR(HLF_AT));
Bram Moolenaarad9c2a02016-07-27 23:26:04 +02002287 set_empty_rows(wp, srow);
2288 wp->w_botline = lnum;
2289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 else if (dy_flags & DY_LASTLINE) /* 'display' has "lastline" */
2291 {
2292 /*
2293 * Last line isn't finished: Display "@@@" at the end.
2294 */
2295 screen_fill(W_WINROW(wp) + wp->w_height - 1,
2296 W_WINROW(wp) + wp->w_height,
2297 (int)W_ENDCOL(wp) - 3, (int)W_ENDCOL(wp),
Bram Moolenaar8820b482017-03-16 17:23:31 +01002298 '@', '@', HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 set_empty_rows(wp, srow);
2300 wp->w_botline = lnum;
2301 }
2302 else
2303 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002304 win_draw_end(wp, '@', ' ', TRUE, srow, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 wp->w_botline = lnum;
2306 }
2307 }
2308 else
2309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (eof) /* we hit the end of the file */
2312 {
2313 wp->w_botline = buf->b_ml.ml_line_count + 1;
2314#ifdef FEAT_DIFF
2315 j = diff_check_fill(wp, wp->w_botline);
2316 if (j > 0 && !wp->w_botfill)
2317 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002318 // Display filler lines at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (char2cells(fill_diff) > 1)
2320 i = '-';
2321 else
2322 i = fill_diff;
2323 if (row + j > wp->w_height)
2324 j = wp->w_height - row;
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002325 win_draw_end(wp, i, i, TRUE, row, row + (int)j, HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 row += j;
2327 }
2328#endif
2329 }
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002330 else if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 wp->w_botline = lnum;
2332
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002333 // Make sure the rest of the screen is blank
2334 // put '~'s on rows that aren't part of the file.
2335 win_draw_end(wp, '~', ' ', FALSE, row, wp->w_height, HLF_EOB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02002338#ifdef SYN_TIME_LIMIT
2339 syn_set_timeout(NULL);
2340#endif
2341
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 /* Reset the type of redrawing required, the window has been updated. */
2343 wp->w_redr_type = 0;
2344#ifdef FEAT_DIFF
2345 wp->w_old_topfill = wp->w_topfill;
2346 wp->w_old_botfill = wp->w_botfill;
2347#endif
2348
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002349 if (dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
2351 /*
2352 * There is a trick with w_botline. If we invalidate it on each
2353 * change that might modify it, this will cause a lot of expensive
2354 * calls to plines() in update_topline() each time. Therefore the
2355 * value of w_botline is often approximated, and this value is used to
2356 * compute the value of w_topline. If the value of w_botline was
2357 * wrong, check that the value of w_topline is correct (cursor is on
2358 * the visible part of the text). If it's not, we need to redraw
2359 * again. Mostly this just means scrolling up a few lines, so it
2360 * doesn't look too bad. Only do this for the current window (where
2361 * changes are relevant).
2362 */
2363 wp->w_valid |= VALID_BOTLINE;
2364 if (wp == curwin && wp->w_botline != old_botline && !recursive)
2365 {
2366 recursive = TRUE;
2367 curwin->w_valid &= ~VALID_TOPLINE;
2368 update_topline(); /* may invalidate w_botline again */
2369 if (must_redraw != 0)
2370 {
2371 /* Don't update for changes in buffer again. */
2372 i = curbuf->b_mod_set;
2373 curbuf->b_mod_set = FALSE;
2374 win_update(curwin);
2375 must_redraw = 0;
2376 curbuf->b_mod_set = i;
2377 }
2378 recursive = FALSE;
2379 }
2380 }
2381
2382#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA)
2383 /* restore got_int, unless CTRL-C was hit while redrawing */
2384 if (!got_int)
2385 got_int = save_got_int;
2386#endif
2387}
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389/*
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002390 * Call screen_fill() with the columns adjusted for 'rightleft' if needed.
2391 * Return the new offset.
2392 */
2393 static int
2394screen_fill_end(
2395 win_T *wp,
2396 int c1,
2397 int c2,
2398 int off,
2399 int width,
2400 int row,
2401 int endrow,
2402 int attr)
2403{
2404 int nn = off + width;
2405
2406 if (nn > wp->w_width)
2407 nn = wp->w_width;
2408#ifdef FEAT_RIGHTLEFT
2409 if (wp->w_p_rl)
2410 {
2411 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2412 W_ENDCOL(wp) - nn, (int)W_ENDCOL(wp) - off,
2413 c1, c2, attr);
2414 }
2415 else
2416#endif
2417 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
2418 wp->w_wincol + off, (int)wp->w_wincol + nn,
2419 c1, c2, attr);
2420 return nn;
2421}
2422
2423/*
2424 * Clear lines near the end the window and mark the unused lines with "c1".
2425 * use "c2" as the filler character.
2426 * When "draw_margin" is TRUE then draw the sign, fold and number columns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 */
2428 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002429win_draw_end(
2430 win_T *wp,
2431 int c1,
2432 int c2,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002433 int draw_margin,
Bram Moolenaar05540972016-01-30 20:31:25 +01002434 int row,
2435 int endrow,
2436 hlf_T hl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 int n = 0;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002439 int attr = HL_ATTR(hl);
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002440 int wcr_attr = get_wcr_attr(wp);
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002441
Bram Moolenaar60cdb302019-05-27 21:54:10 +02002442 attr = hl_combine_attr(wcr_attr, attr);
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002443
2444 if (draw_margin)
2445 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01002446#ifdef FEAT_FOLDING
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002447 int fdc = compute_foldcolumn(wp, 0);
2448
2449 if (fdc > 0)
2450 // draw the fold column
2451 n = screen_fill_end(wp, ' ', ' ', n, fdc,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002452 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC)));
Bram Moolenaar1c934292015-01-27 16:39:29 +01002453#endif
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002454#ifdef FEAT_SIGNS
2455 if (signcolumn_on(wp))
2456 // draw the sign column
2457 n = screen_fill_end(wp, ' ', ' ', n, 2,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002458 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002459#endif
2460 if ((wp->w_p_nu || wp->w_p_rnu)
2461 && vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
2462 // draw the number column
2463 n = screen_fill_end(wp, ' ', ' ', n, number_width(wp) + 1,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002464 row, endrow, hl_combine_attr(wcr_attr, HL_ATTR(HLF_N)));
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467#ifdef FEAT_RIGHTLEFT
2468 if (wp->w_p_rl)
2469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002471 wp->w_wincol, W_ENDCOL(wp) - 1 - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002472 c2, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002474 W_ENDCOL(wp) - 1 - n, W_ENDCOL(wp) - n,
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002475 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477 else
2478#endif
2479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + endrow,
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002481 wp->w_wincol + n, (int)W_ENDCOL(wp),
Bram Moolenaar193ffd12019-05-25 22:57:30 +02002482 c1, c2, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01002484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 set_empty_rows(wp, row);
2486}
2487
Bram Moolenaar1a384422010-07-14 19:53:30 +02002488#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02002489/*
2490 * Advance **color_cols and return TRUE when there are columns to draw.
2491 */
2492 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002493advance_color_col(int vcol, int **color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02002494{
2495 while (**color_cols >= 0 && vcol > **color_cols)
2496 ++*color_cols;
2497 return (**color_cols >= 0);
2498}
2499#endif
2500
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002501#if defined(FEAT_MENU) || defined(FEAT_FOLDING)
2502/*
2503 * Copy "text" to ScreenLines using "attr".
2504 * Returns the next screen column.
2505 */
2506 static int
2507text_to_screenline(win_T *wp, char_u *text, int col)
2508{
2509 int off = (int)(current_ScreenLine - ScreenLines);
2510
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002511 if (has_mbyte)
2512 {
2513 int cells;
2514 int u8c, u8cc[MAX_MCO];
2515 int i;
2516 int idx;
2517 int c_len;
2518 char_u *p;
2519# ifdef FEAT_ARABIC
2520 int prev_c = 0; /* previous Arabic character */
2521 int prev_c1 = 0; /* first composing char for prev_c */
2522# endif
2523
2524# ifdef FEAT_RIGHTLEFT
2525 if (wp->w_p_rl)
2526 idx = off;
2527 else
2528# endif
2529 idx = off + col;
2530
2531 /* Store multibyte characters in ScreenLines[] et al. correctly. */
2532 for (p = text; *p != NUL; )
2533 {
2534 cells = (*mb_ptr2cells)(p);
2535 c_len = (*mb_ptr2len)(p);
Bram Moolenaar02631462017-09-22 15:20:32 +02002536 if (col + cells > wp->w_width
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002537# ifdef FEAT_RIGHTLEFT
2538 - (wp->w_p_rl ? col : 0)
2539# endif
2540 )
2541 break;
2542 ScreenLines[idx] = *p;
2543 if (enc_utf8)
2544 {
2545 u8c = utfc_ptr2char(p, u8cc);
2546 if (*p < 0x80 && u8cc[0] == 0)
2547 {
2548 ScreenLinesUC[idx] = 0;
2549#ifdef FEAT_ARABIC
2550 prev_c = u8c;
2551#endif
2552 }
2553 else
2554 {
2555#ifdef FEAT_ARABIC
2556 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
2557 {
2558 /* Do Arabic shaping. */
2559 int pc, pc1, nc;
2560 int pcc[MAX_MCO];
2561 int firstbyte = *p;
2562
2563 /* The idea of what is the previous and next
2564 * character depends on 'rightleft'. */
2565 if (wp->w_p_rl)
2566 {
2567 pc = prev_c;
2568 pc1 = prev_c1;
2569 nc = utf_ptr2char(p + c_len);
2570 prev_c1 = u8cc[0];
2571 }
2572 else
2573 {
2574 pc = utfc_ptr2char(p + c_len, pcc);
2575 nc = prev_c;
2576 pc1 = pcc[0];
2577 }
2578 prev_c = u8c;
2579
2580 u8c = arabic_shape(u8c, &firstbyte, &u8cc[0],
2581 pc, pc1, nc);
2582 ScreenLines[idx] = firstbyte;
2583 }
2584 else
2585 prev_c = u8c;
2586#endif
2587 /* Non-BMP character: display as ? or fullwidth ?. */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01002588 ScreenLinesUC[idx] = u8c;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002589 for (i = 0; i < Screen_mco; ++i)
2590 {
2591 ScreenLinesC[i][idx] = u8cc[i];
2592 if (u8cc[i] == 0)
2593 break;
2594 }
2595 }
2596 if (cells > 1)
2597 ScreenLines[idx + 1] = 0;
2598 }
2599 else if (enc_dbcs == DBCS_JPNU && *p == 0x8e)
2600 /* double-byte single width character */
2601 ScreenLines2[idx] = p[1];
2602 else if (cells > 1)
2603 /* double-width character */
2604 ScreenLines[idx + 1] = p[1];
2605 col += cells;
2606 idx += cells;
2607 p += c_len;
2608 }
2609 }
2610 else
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002611 {
2612 int len = (int)STRLEN(text);
2613
Bram Moolenaar02631462017-09-22 15:20:32 +02002614 if (len > wp->w_width - col)
2615 len = wp->w_width - col;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002616 if (len > 0)
2617 {
2618#ifdef FEAT_RIGHTLEFT
2619 if (wp->w_p_rl)
Bram Moolenaarc6663882019-02-22 19:14:54 +01002620 mch_memmove(current_ScreenLine, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002621 else
2622#endif
Bram Moolenaarc6663882019-02-22 19:14:54 +01002623 mch_memmove(current_ScreenLine + col, text, len);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002624 col += len;
2625 }
2626 }
2627 return col;
2628}
2629#endif
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631#ifdef FEAT_FOLDING
2632/*
Bram Moolenaar1c934292015-01-27 16:39:29 +01002633 * Compute the width of the foldcolumn. Based on 'foldcolumn' and how much
2634 * space is available for window "wp", minus "col".
2635 */
2636 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002637compute_foldcolumn(win_T *wp, int col)
Bram Moolenaar1c934292015-01-27 16:39:29 +01002638{
2639 int fdc = wp->w_p_fdc;
2640 int wmw = wp == curwin && p_wmw == 0 ? 1 : p_wmw;
Bram Moolenaar02631462017-09-22 15:20:32 +02002641 int wwidth = wp->w_width;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002642
2643 if (fdc > wwidth - (col + wmw))
2644 fdc = wwidth - (col + wmw);
2645 return fdc;
2646}
2647
2648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * Display one folded line.
2650 */
2651 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002652fold_line(
2653 win_T *wp,
2654 long fold_count,
2655 foldinfo_T *foldinfo,
2656 linenr_T lnum,
2657 int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
Bram Moolenaaree695f72016-08-03 22:08:45 +02002659 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 pos_T *top, *bot;
2661 linenr_T lnume = lnum + fold_count - 1;
2662 int len;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002663 char_u *text;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 int fdc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 int col;
2666 int txtcol;
2667 int off = (int)(current_ScreenLine - ScreenLines);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002668 int ri;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 /* Build the fold line:
2671 * 1. Add the cmdwin_type for the command-line window
2672 * 2. Add the 'foldcolumn'
Bram Moolenaar64486672010-05-16 15:46:46 +02002673 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 * 4. Compose the text
2675 * 5. Add the text
2676 * 6. set highlighting for the Visual area an other text
2677 */
2678 col = 0;
2679
2680 /*
2681 * 1. Add the cmdwin_type for the command-line window
2682 * Ignores 'rightleft', this window is never right-left.
2683 */
2684#ifdef FEAT_CMDWIN
2685 if (cmdwin_type != 0 && wp == curwin)
2686 {
2687 ScreenLines[off] = cmdwin_type;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002688 ScreenAttrs[off] = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (enc_utf8)
2690 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ++col;
2692 }
2693#endif
2694
2695 /*
2696 * 2. Add the 'foldcolumn'
Bram Moolenaar1c934292015-01-27 16:39:29 +01002697 * Reduce the width when there is not enough space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaar1c934292015-01-27 16:39:29 +01002699 fdc = compute_foldcolumn(wp, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (fdc > 0)
2701 {
2702 fill_foldcolumn(buf, wp, TRUE, lnum);
2703#ifdef FEAT_RIGHTLEFT
2704 if (wp->w_p_rl)
2705 {
2706 int i;
2707
Bram Moolenaar02631462017-09-22 15:20:32 +02002708 copy_text_attr(off + wp->w_width - fdc - col, buf, fdc,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002709 HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 /* reverse the fold column */
2711 for (i = 0; i < fdc; ++i)
Bram Moolenaar02631462017-09-22 15:20:32 +02002712 ScreenLines[off + wp->w_width - i - 1 - col] = buf[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
2714 else
2715#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002716 copy_text_attr(off + col, buf, fdc, HL_ATTR(HLF_FC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 col += fdc;
2718 }
2719
2720#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6f470022018-04-10 18:47:20 +02002721# define RL_MEMSET(p, v, l) \
2722 do { \
2723 if (wp->w_p_rl) \
2724 for (ri = 0; ri < l; ++ri) \
2725 ScreenAttrs[off + (wp->w_width - (p) - (l)) + ri] = v; \
2726 else \
2727 for (ri = 0; ri < l; ++ri) \
2728 ScreenAttrs[off + (p) + ri] = v; \
2729 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730#else
Bram Moolenaar6f470022018-04-10 18:47:20 +02002731# define RL_MEMSET(p, v, l) \
2732 do { \
2733 for (ri = 0; ri < l; ++ri) \
2734 ScreenAttrs[off + (p) + ri] = v; \
2735 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736#endif
2737
Bram Moolenaar64486672010-05-16 15:46:46 +02002738 /* Set all attributes of the 'number' or 'relativenumber' column and the
2739 * text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002740 RL_MEMSET(col, HL_ATTR(HLF_FL), wp->w_width - col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742#ifdef FEAT_SIGNS
2743 /* If signs are being displayed, add two spaces. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02002744 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002746 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (len > 0)
2748 {
2749 if (len > 2)
2750 len = 2;
2751# ifdef FEAT_RIGHTLEFT
2752 if (wp->w_p_rl)
2753 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002754 copy_text_attr(off + wp->w_width - len - col,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002755 (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 else
2757# endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002758 copy_text_attr(off + col, (char_u *)" ", len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 col += len;
2760 }
2761 }
2762#endif
2763
2764 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002765 * 3. Add the 'number' or 'relativenumber' column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 */
Bram Moolenaar64486672010-05-16 15:46:46 +02002767 if (wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar02631462017-09-22 15:20:32 +02002769 len = wp->w_width - col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (len > 0)
2771 {
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002772 int w = number_width(wp);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002773 long num;
2774 char *fmt = "%*ld ";
Bram Moolenaar592e0a22004-07-03 16:05:59 +00002775
2776 if (len > w + 1)
2777 len = w + 1;
Bram Moolenaar64486672010-05-16 15:46:46 +02002778
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002779 if (wp->w_p_nu && !wp->w_p_rnu)
2780 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02002781 num = (long)lnum;
2782 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01002783 {
Bram Moolenaar64486672010-05-16 15:46:46 +02002784 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01002785 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002786 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01002787 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02002788 /* 'number' + 'relativenumber': cursor line shows absolute
2789 * line number */
Bram Moolenaar700e7342013-01-30 12:31:36 +01002790 num = lnum;
2791 fmt = "%-*ld ";
2792 }
2793 }
Bram Moolenaar64486672010-05-16 15:46:46 +02002794
Bram Moolenaar700e7342013-01-30 12:31:36 +01002795 sprintf((char *)buf, fmt, w, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#ifdef FEAT_RIGHTLEFT
2797 if (wp->w_p_rl)
2798 /* the line number isn't reversed */
Bram Moolenaar02631462017-09-22 15:20:32 +02002799 copy_text_attr(off + wp->w_width - len - col, buf, len,
Bram Moolenaar8820b482017-03-16 17:23:31 +01002800 HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 else
2802#endif
Bram Moolenaar8820b482017-03-16 17:23:31 +01002803 copy_text_attr(off + col, buf, len, HL_ATTR(HLF_FL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 col += len;
2805 }
2806 }
2807
2808 /*
2809 * 4. Compose the folded-line string with 'foldtext', if set.
2810 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002811 text = get_foldtext(wp, lnum, lnume, foldinfo, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 txtcol = col; /* remember where text starts */
2814
2815 /*
2816 * 5. move the text to current_ScreenLine. Fill up with "fill_fold".
2817 * Right-left text is put in columns 0 - number-col, normal text is put
2818 * in columns number-col - window-width.
2819 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002820 col = text_to_screenline(wp, text, col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
2822 /* Fill the rest of the line with the fold filler */
2823#ifdef FEAT_RIGHTLEFT
2824 if (wp->w_p_rl)
2825 col -= txtcol;
2826#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02002827 while (col < wp->w_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828#ifdef FEAT_RIGHTLEFT
2829 - (wp->w_p_rl ? txtcol : 0)
2830#endif
2831 )
2832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 if (enc_utf8)
2834 {
2835 if (fill_fold >= 0x80)
2836 {
2837 ScreenLinesUC[off + col] = fill_fold;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002838 ScreenLinesC[0][off + col] = 0;
Bram Moolenaaracda04f2018-02-08 09:57:28 +01002839 ScreenLines[off + col] = 0x80; /* avoid storing zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841 else
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 ScreenLinesUC[off + col] = 0;
Bram Moolenaar8da1e6c2017-03-29 20:38:59 +02002844 ScreenLines[off + col] = fill_fold;
2845 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002846 col++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002848 else
Bram Moolenaarc6cd8402017-03-29 14:40:47 +02002849 ScreenLines[off + col++] = fill_fold;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851
2852 if (text != buf)
2853 vim_free(text);
2854
2855 /*
2856 * 6. set highlighting for the Visual area an other text.
2857 * If all folded lines are in the Visual area, highlight the line.
2858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
2860 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002861 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
2863 /* Visual is after curwin->w_cursor */
2864 top = &curwin->w_cursor;
2865 bot = &VIsual;
2866 }
2867 else
2868 {
2869 /* Visual is before curwin->w_cursor */
2870 top = &VIsual;
2871 bot = &curwin->w_cursor;
2872 }
2873 if (lnum >= top->lnum
2874 && lnume <= bot->lnum
2875 && (VIsual_mode != 'v'
2876 || ((lnum > top->lnum
2877 || (lnum == top->lnum
2878 && top->col == 0))
2879 && (lnume < bot->lnum
2880 || (lnume == bot->lnum
2881 && (bot->col - (*p_sel == 'e'))
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002882 >= (colnr_T)STRLEN(ml_get_buf(wp->w_buffer, lnume, FALSE)))))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 {
2884 if (VIsual_mode == Ctrl_V)
2885 {
2886 /* Visual block mode: highlight the chars part of the block */
Bram Moolenaar02631462017-09-22 15:20:32 +02002887 if (wp->w_old_cursor_fcol + txtcol < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar6c167c62011-09-02 14:07:36 +02002889 if (wp->w_old_cursor_lcol != MAXCOL
2890 && wp->w_old_cursor_lcol + txtcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002891 < (colnr_T)wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 len = wp->w_old_cursor_lcol;
2893 else
Bram Moolenaar02631462017-09-22 15:20:32 +02002894 len = wp->w_width - txtcol;
Bram Moolenaar8820b482017-03-16 17:23:31 +01002895 RL_MEMSET(wp->w_old_cursor_fcol + txtcol, HL_ATTR(HLF_V),
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002896 len - (int)wp->w_old_cursor_fcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
2899 else
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002900 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 /* Set all attributes of the text */
Bram Moolenaar02631462017-09-22 15:20:32 +02002902 RL_MEMSET(txtcol, HL_ATTR(HLF_V), wp->w_width - txtcol);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002907#ifdef FEAT_SYN_HL
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002908 /* Show colorcolumn in the fold line, but let cursorcolumn override it. */
2909 if (wp->w_p_cc_cols)
2910 {
2911 int i = 0;
2912 int j = wp->w_p_cc_cols[i];
2913 int old_txtcol = txtcol;
2914
2915 while (j > -1)
2916 {
2917 txtcol += j;
2918 if (wp->w_p_wrap)
2919 txtcol -= wp->w_skipcol;
2920 else
2921 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002922 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002923 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002924 ScreenAttrs[off + txtcol], HL_ATTR(HLF_MC));
Bram Moolenaarfbc25b22015-03-20 17:16:27 +01002925 txtcol = old_txtcol;
2926 j = wp->w_p_cc_cols[++i];
2927 }
2928 }
2929
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002930 /* Show 'cursorcolumn' in the fold line. */
Bram Moolenaar85595c52008-10-02 16:04:05 +00002931 if (wp->w_p_cuc)
2932 {
2933 txtcol += wp->w_virtcol;
2934 if (wp->w_p_wrap)
2935 txtcol -= wp->w_skipcol;
2936 else
2937 txtcol -= wp->w_leftcol;
Bram Moolenaar02631462017-09-22 15:20:32 +02002938 if (txtcol >= 0 && txtcol < wp->w_width)
Bram Moolenaar85595c52008-10-02 16:04:05 +00002939 ScreenAttrs[off + txtcol] = hl_combine_attr(
Bram Moolenaar8820b482017-03-16 17:23:31 +01002940 ScreenAttrs[off + txtcol], HL_ATTR(HLF_CUC));
Bram Moolenaar85595c52008-10-02 16:04:05 +00002941 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Bram Moolenaar02631462017-09-22 15:20:32 +02002944 screen_line(row + W_WINROW(wp), wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002945 (int)wp->w_width, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
2947 /*
2948 * Update w_cline_height and w_cline_folded if the cursor line was
2949 * updated (saves a call to plines() later).
2950 */
2951 if (wp == curwin
2952 && lnum <= curwin->w_cursor.lnum
2953 && lnume >= curwin->w_cursor.lnum)
2954 {
2955 curwin->w_cline_row = row;
2956 curwin->w_cline_height = 1;
2957 curwin->w_cline_folded = TRUE;
2958 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
2959 }
2960}
2961
2962/*
2963 * Copy "buf[len]" to ScreenLines["off"] and set attributes to "attr".
2964 */
2965 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002966copy_text_attr(
2967 int off,
2968 char_u *buf,
2969 int len,
2970 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002972 int i;
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 mch_memmove(ScreenLines + off, buf, (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (enc_utf8)
2976 vim_memset(ScreenLinesUC + off, 0, sizeof(u8char_T) * (size_t)len);
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002977 for (i = 0; i < len; ++i)
2978 ScreenAttrs[off + i] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
2981/*
2982 * Fill the foldcolumn at "p" for window "wp".
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002983 * Only to be called when 'foldcolumn' > 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 */
2985 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002986fill_foldcolumn(
2987 char_u *p,
2988 win_T *wp,
2989 int closed, /* TRUE of FALSE */
2990 linenr_T lnum) /* current line number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 int i = 0;
2993 int level;
2994 int first_level;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00002995 int empty;
Bram Moolenaar1c934292015-01-27 16:39:29 +01002996 int fdc = compute_foldcolumn(wp, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 /* Init to all spaces. */
Bram Moolenaar2536d4f2015-07-17 13:22:51 +02002999 vim_memset(p, ' ', (size_t)fdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 level = win_foldinfo.fi_level;
3002 if (level > 0)
3003 {
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003004 /* If there is only one column put more info in it. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003005 empty = (fdc == 1) ? 0 : 1;
Bram Moolenaar578b49e2005-09-10 19:22:57 +00003006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 /* If the column is too narrow, we start at the lowest level that
3008 * fits and use numbers to indicated the depth. */
Bram Moolenaar1c934292015-01-27 16:39:29 +01003009 first_level = level - fdc - closed + 1 + empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (first_level < 1)
3011 first_level = 1;
3012
Bram Moolenaar1c934292015-01-27 16:39:29 +01003013 for (i = 0; i + empty < fdc; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
3015 if (win_foldinfo.fi_lnum == lnum
3016 && first_level + i >= win_foldinfo.fi_low_level)
3017 p[i] = '-';
3018 else if (first_level == 1)
3019 p[i] = '|';
3020 else if (first_level + i <= 9)
3021 p[i] = '0' + first_level + i;
3022 else
3023 p[i] = '>';
3024 if (first_level + i == level)
3025 break;
3026 }
3027 }
3028 if (closed)
Bram Moolenaar1c934292015-01-27 16:39:29 +01003029 p[i >= fdc ? i - 1 : i] = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030}
3031#endif /* FEAT_FOLDING */
3032
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003033#ifdef FEAT_TEXT_PROP
3034static textprop_T *current_text_props = NULL;
3035static buf_T *current_buf = NULL;
3036
3037 static int
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01003038text_prop_compare(const void *s1, const void *s2)
3039{
3040 int idx1, idx2;
3041 proptype_T *pt1, *pt2;
3042 colnr_T col1, col2;
3043
3044 idx1 = *(int *)s1;
3045 idx2 = *(int *)s2;
3046 pt1 = text_prop_type_by_id(current_buf, current_text_props[idx1].tp_type);
3047 pt2 = text_prop_type_by_id(current_buf, current_text_props[idx2].tp_type);
3048 if (pt1 == pt2)
3049 return 0;
3050 if (pt1 == NULL)
3051 return -1;
3052 if (pt2 == NULL)
3053 return 1;
3054 if (pt1->pt_priority != pt2->pt_priority)
3055 return pt1->pt_priority > pt2->pt_priority ? 1 : -1;
3056 col1 = current_text_props[idx1].tp_col;
3057 col2 = current_text_props[idx2].tp_col;
3058 return col1 == col2 ? 0 : col1 > col2 ? 1 : -1;
3059}
3060#endif
3061
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062/*
3063 * Display line "lnum" of window 'wp' on the screen.
3064 * Start at row "startrow", stop when "endrow" is reached.
3065 * wp->w_virtcol needs to be valid.
3066 *
3067 * Return the number of last row the line occupies.
3068 */
3069 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003070win_line(
3071 win_T *wp,
3072 linenr_T lnum,
3073 int startrow,
3074 int endrow,
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003075 int nochange UNUSED, // not updating for changed text
3076 int number_only) // only update the number column
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003078 int col = 0; /* visual column on screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 unsigned off; /* offset in ScreenLines/ScreenAttrs */
3080 int c = 0; /* init for GCC */
3081 long vcol = 0; /* virtual column (for tabs) */
Bram Moolenaard574ea22015-01-14 19:35:14 +01003082#ifdef FEAT_LINEBREAK
3083 long vcol_sbr = -1; /* virtual column after showbreak */
3084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 long vcol_prev = -1; /* "vcol" of previous character */
3086 char_u *line; /* current line */
3087 char_u *ptr; /* current position in "line" */
3088 int row; /* row in the window, excl w_winrow */
3089 int screen_row; /* row on the screen, incl w_winrow */
3090
3091 char_u extra[18]; /* "%ld" and 'fdc' must fit in here */
3092 int n_extra = 0; /* number of extra chars */
Bram Moolenaara064ac82007-08-05 18:10:54 +00003093 char_u *p_extra = NULL; /* string of extra chars, plus NUL */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02003094 char_u *p_extra_free = NULL; /* p_extra needs to be freed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int c_extra = NUL; /* extra chars, all the same */
Bram Moolenaar83a52172019-01-16 22:41:54 +01003096 int c_final = NUL; /* final char, mandatory if set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int extra_attr = 0; /* attributes when n_extra != 0 */
3098 static char_u *at_end_str = (char_u *)""; /* used for p_extra when
3099 displaying lcs_eol at end-of-line */
3100 int lcs_eol_one = lcs_eol; /* lcs_eol until it's been used */
3101 int lcs_prec_todo = lcs_prec; /* lcs_prec until it's been used */
3102
3103 /* saved "extra" items for when draw_state becomes WL_LINE (again) */
3104 int saved_n_extra = 0;
3105 char_u *saved_p_extra = NULL;
3106 int saved_c_extra = 0;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003107 int saved_c_final = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 int saved_char_attr = 0;
3109
3110 int n_attr = 0; /* chars with special attr */
3111 int saved_attr2 = 0; /* char_attr saved for n_attr */
3112 int n_attr3 = 0; /* chars with overruling special attr */
3113 int saved_attr3 = 0; /* char_attr saved for n_attr3 */
3114
3115 int n_skip = 0; /* nr of chars to skip for 'nowrap' */
3116
3117 int fromcol, tocol; /* start/end of inverting */
3118 int fromcol_prev = -2; /* start of inverting after cursor */
3119 int noinvcur = FALSE; /* don't invert the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 pos_T *top, *bot;
Bram Moolenaar54ef7112009-02-21 20:11:41 +00003121 int lnum_in_visual_area = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 pos_T pos;
3123 long v;
3124
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003125 int char_attr = 0; // attributes for next character
3126 int attr_pri = FALSE; // char_attr has priority
3127 int area_highlighting = FALSE; // Visual or incsearch highlighting
3128 // in this line
3129 int vi_attr = 0; // attributes for Visual and incsearch
3130 // highlighting
3131 int wcr_attr = 0; // attributes from 'wincolor'
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003132 int win_attr = 0; // background for whole window, except
3133 // margins and "~" lines.
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003134 int area_attr = 0; // attributes desired by highlighting
3135 int search_attr = 0; // attributes desired by 'hlsearch'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136#ifdef FEAT_SYN_HL
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003137 int vcol_save_attr = 0; /* saved attr for 'cursorcolumn' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 int syntax_attr = 0; /* attributes desired by syntax */
3139 int has_syntax = FALSE; /* this buffer has syntax highl. */
3140 int save_did_emsg;
Bram Moolenaara443af82007-11-08 13:51:42 +00003141 int eol_hl_off = 0; /* 1 if highlighted char after EOL */
Bram Moolenaar1a384422010-07-14 19:53:30 +02003142 int draw_color_col = FALSE; /* highlight colorcolumn */
3143 int *color_cols = NULL; /* pointer to according columns array */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003144#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003145#ifdef FEAT_TEXT_PROP
3146 int text_prop_count;
3147 int text_prop_next = 0; // next text property to use
3148 textprop_T *text_props = NULL;
3149 int *text_prop_idxs = NULL;
3150 int text_props_active = 0;
3151 proptype_T *text_prop_type = NULL;
3152 int text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02003153 int text_prop_combine = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003154#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003155#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00003156 int has_spell = FALSE; /* this buffer has spell checking */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003157# define SPWORDLEN 150
3158 char_u nextline[SPWORDLEN * 2];/* text with start of the next line */
Bram Moolenaar3b506942005-06-23 22:36:45 +00003159 int nextlinecol = 0; /* column where nextline[] starts */
3160 int nextline_idx = 0; /* index in nextline[] where next line
Bram Moolenaar30abd282005-06-22 22:35:10 +00003161 starts */
Bram Moolenaar217ad922005-03-20 22:37:15 +00003162 int spell_attr = 0; /* attributes desired by spelling */
3163 int word_end = 0; /* last byte with same spell_attr */
Bram Moolenaard042c562005-06-30 22:04:15 +00003164 static linenr_T checked_lnum = 0; /* line number for "checked_col" */
3165 static int checked_col = 0; /* column in "checked_lnum" up to which
Bram Moolenaar30abd282005-06-22 22:35:10 +00003166 * there are no spell errors */
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003167 static int cap_col = -1; /* column to check for Cap word */
3168 static linenr_T capcol_lnum = 0; /* line number where "cap_col" used */
Bram Moolenaar30abd282005-06-22 22:35:10 +00003169 int cur_checked_col = 0; /* checked column for current line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003171 int extra_check = 0; // has extra highlighting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 int multi_attr = 0; /* attributes desired by multibyte */
3173 int mb_l = 1; /* multi-byte byte length */
3174 int mb_c = 0; /* decoded multi-byte character */
3175 int mb_utf8 = FALSE; /* screen char is UTF-8 char */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003176 int u8cc[MAX_MCO]; /* composing UTF-8 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#ifdef FEAT_DIFF
3178 int filler_lines; /* nr of filler lines to be drawn */
3179 int filler_todo; /* nr of filler lines still to do + 1 */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003180 hlf_T diff_hlf = (hlf_T)0; /* type of diff highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 int change_start = MAXCOL; /* first col of changed area */
3182 int change_end = -1; /* last col of changed area */
3183#endif
3184 colnr_T trailcol = MAXCOL; /* start of trailing spaces */
3185#ifdef FEAT_LINEBREAK
Bram Moolenaar6c896862016-11-17 19:46:51 +01003186 int need_showbreak = FALSE; /* overlong line, skipping first x
3187 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003189#if defined(FEAT_SIGNS) || defined(FEAT_QUICKFIX) \
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003190 || defined(FEAT_SYN_HL) || defined(FEAT_DIFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191# define LINE_ATTR
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003192 int line_attr = 0; /* attribute for the whole line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#endif
3194#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003195 matchitem_T *cur; /* points to the match list */
3196 match_T *shl; /* points to search_hl or a match */
3197 int shl_flag; /* flag to indicate whether search_hl
3198 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02003199 int pos_inprogress; /* marks that position match search is
3200 in progress */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003201 int prevcol_hl_flag; /* flag to indicate whether prevcol
3202 equals startcol of search_hl or one
3203 of the matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
3205#ifdef FEAT_ARABIC
3206 int prev_c = 0; /* previous Arabic character */
3207 int prev_c1 = 0; /* first composing char for prev_c */
3208#endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00003209#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00003210 int did_line_attr = 0;
3211#endif
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003212#ifdef FEAT_TERMINAL
3213 int get_term_attr = FALSE;
3214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
3216 /* draw_state: items that are drawn in sequence: */
3217#define WL_START 0 /* nothing done yet */
3218#ifdef FEAT_CMDWIN
3219# define WL_CMDLINE WL_START + 1 /* cmdline window column */
3220#else
3221# define WL_CMDLINE WL_START
3222#endif
3223#ifdef FEAT_FOLDING
3224# define WL_FOLD WL_CMDLINE + 1 /* 'foldcolumn' */
3225#else
3226# define WL_FOLD WL_CMDLINE
3227#endif
3228#ifdef FEAT_SIGNS
3229# define WL_SIGN WL_FOLD + 1 /* column for signs */
3230#else
3231# define WL_SIGN WL_FOLD /* column for signs */
3232#endif
3233#define WL_NR WL_SIGN + 1 /* line number */
Bram Moolenaar597a4222014-06-25 14:39:50 +02003234#ifdef FEAT_LINEBREAK
3235# define WL_BRI WL_NR + 1 /* 'breakindent' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236#else
Bram Moolenaar597a4222014-06-25 14:39:50 +02003237# define WL_BRI WL_NR
3238#endif
3239#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
3240# define WL_SBR WL_BRI + 1 /* 'showbreak' or 'diff' */
3241#else
3242# define WL_SBR WL_BRI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243#endif
3244#define WL_LINE WL_SBR + 1 /* text in the line */
3245 int draw_state = WL_START; /* what to draw next */
Bram Moolenaar9372a112005-12-06 19:59:18 +00003246#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 int feedback_col = 0;
3248 int feedback_old_attr = -1;
3249#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003250 int screen_line_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar860cae12010-06-05 23:22:07 +02003252#ifdef FEAT_CONCEAL
3253 int syntax_flags = 0;
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02003254 int syntax_seqnr = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02003255 int prev_syntax_id = 0;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003256 int conceal_attr = HL_ATTR(HLF_CONCEAL);
Bram Moolenaar860cae12010-06-05 23:22:07 +02003257 int is_concealing = FALSE;
3258 int boguscols = 0; /* nonexistent columns added to force
3259 wrapping */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003260 int vcol_off = 0; /* offset for concealed characters */
Bram Moolenaarf5963f72010-07-23 22:10:27 +02003261 int did_wcol = FALSE;
Bram Moolenaar4d585022016-04-14 19:50:22 +02003262 int match_conc = 0; /* cchar for match functions */
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003263 int old_boguscols = 0;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003264# define VCOL_HLC (vcol - vcol_off)
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003265# define FIX_FOR_BOGUSCOLS \
3266 { \
3267 n_extra += vcol_off; \
3268 vcol -= vcol_off; \
3269 vcol_off = 0; \
3270 col -= boguscols; \
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01003271 old_boguscols = boguscols; \
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02003272 boguscols = 0; \
3273 }
Bram Moolenaarac550fd2010-07-18 13:55:02 +02003274#else
3275# define VCOL_HLC (vcol)
Bram Moolenaar860cae12010-06-05 23:22:07 +02003276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278 if (startrow > endrow) /* past the end already! */
3279 return startrow;
3280
3281 row = startrow;
3282 screen_row = row + W_WINROW(wp);
3283
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003284 if (!number_only)
3285 {
3286 /*
3287 * To speed up the loop below, set extra_check when there is linebreak,
3288 * trailing white space and/or syntax processing to be done.
3289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290#ifdef FEAT_LINEBREAK
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003291 extra_check = wp->w_p_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#endif
3293#ifdef FEAT_SYN_HL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003294 if (syntax_present(wp) && !wp->w_s->b_syn_error
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003295# ifdef SYN_TIME_LIMIT
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003296 && !wp->w_s->b_syn_slow
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003297# endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003300 /* Prepare for syntax highlighting in this line. When there is an
3301 * error, stop syntax highlighting. */
3302 save_did_emsg = did_emsg;
3303 did_emsg = FALSE;
3304 syntax_start(wp, lnum);
3305 if (did_emsg)
3306 wp->w_s->b_syn_error = TRUE;
3307 else
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003308 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003309 did_emsg = save_did_emsg;
3310#ifdef SYN_TIME_LIMIT
3311 if (!wp->w_s->b_syn_slow)
3312#endif
3313 {
3314 has_syntax = TRUE;
3315 extra_check = TRUE;
3316 }
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
Bram Moolenaar1a384422010-07-14 19:53:30 +02003319
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003320 /* Check for columns to display for 'colorcolumn'. */
3321 color_cols = wp->w_p_cc_cols;
3322 if (color_cols != NULL)
3323 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003324#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00003325
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003326#ifdef FEAT_TERMINAL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003327 if (term_show_buffer(wp->w_buffer))
3328 {
3329 extra_check = TRUE;
3330 get_term_attr = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003331 win_attr = term_get_attr(wp->w_buffer, lnum, -1);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003332 }
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02003333#endif
3334
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003335#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003336 if (wp->w_p_spell
3337 && *wp->w_s->b_p_spl != NUL
3338 && wp->w_s->b_langp.ga_len > 0
3339 && *(char **)(wp->w_s->b_langp.ga_data) != NULL)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003340 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003341 /* Prepare for spell checking. */
3342 has_spell = TRUE;
3343 extra_check = TRUE;
3344
Bram Moolenaar7701f302018-10-02 21:20:32 +02003345 /* Get the start of the next line, so that words that wrap to the
3346 * next line are found too: "et<line-break>al.".
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003347 * Trick: skip a few chars for C/shell/Vim comments */
3348 nextline[SPWORDLEN] = NUL;
3349 if (lnum < wp->w_buffer->b_ml.ml_line_count)
3350 {
3351 line = ml_get_buf(wp->w_buffer, lnum + 1, FALSE);
3352 spell_cat_line(nextline + SPWORDLEN, line, SPWORDLEN);
3353 }
3354
Bram Moolenaar7701f302018-10-02 21:20:32 +02003355 /* When a word wrapped from the previous line the start of the
3356 * current line is valid. */
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003357 if (lnum == checked_lnum)
3358 cur_checked_col = checked_col;
3359 checked_lnum = 0;
3360
3361 /* When there was a sentence end in the previous line may require a
3362 * word starting with capital in this line. In line 1 always check
3363 * the first word. */
3364 if (lnum != capcol_lnum)
3365 cap_col = -1;
3366 if (lnum == 1)
3367 cap_col = 0;
3368 capcol_lnum = 0;
Bram Moolenaar30abd282005-06-22 22:35:10 +00003369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#endif
3371
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003372 /*
3373 * handle visual active in this window
3374 */
3375 fromcol = -10;
3376 tocol = MAXCOL;
3377 if (VIsual_active && wp->w_buffer == curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003379 /* Visual is after curwin->w_cursor */
3380 if (LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003382 top = &curwin->w_cursor;
3383 bot = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003385 else /* Visual is before curwin->w_cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003387 top = &VIsual;
3388 bot = &curwin->w_cursor;
3389 }
3390 lnum_in_visual_area = (lnum >= top->lnum && lnum <= bot->lnum);
3391 if (VIsual_mode == Ctrl_V) /* block mode */
3392 {
3393 if (lnum_in_visual_area)
3394 {
3395 fromcol = wp->w_old_cursor_fcol;
3396 tocol = wp->w_old_cursor_lcol;
3397 }
3398 }
3399 else /* non-block mode */
3400 {
3401 if (lnum > top->lnum && lnum <= bot->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 fromcol = 0;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003403 else if (lnum == top->lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003405 if (VIsual_mode == 'V') /* linewise */
3406 fromcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 else
3408 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003409 getvvcol(wp, top, (colnr_T *)&fromcol, NULL, NULL);
3410 if (gchar_pos(top) == NUL)
3411 tocol = fromcol + 1;
3412 }
3413 }
3414 if (VIsual_mode != 'V' && lnum == bot->lnum)
3415 {
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003416 if (*p_sel == 'e' && bot->col == 0 && bot->coladd == 0)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003417 {
3418 fromcol = -10;
3419 tocol = MAXCOL;
3420 }
3421 else if (bot->col == MAXCOL)
3422 tocol = MAXCOL;
3423 else
3424 {
3425 pos = *bot;
3426 if (*p_sel == 'e')
3427 getvvcol(wp, &pos, (colnr_T *)&tocol, NULL, NULL);
3428 else
3429 {
3430 getvvcol(wp, &pos, NULL, NULL, (colnr_T *)&tocol);
3431 ++tocol;
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 }
3435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003437 /* Check if the character under the cursor should not be inverted */
3438 if (!highlight_match && lnum == curwin->w_cursor.lnum && wp == curwin
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003439#ifdef FEAT_GUI
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003440 && !gui.in_use
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003441#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003442 )
3443 noinvcur = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003445 /* if inverting in this line set area_highlighting */
3446 if (fromcol >= 0)
3447 {
3448 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003449 vi_attr = HL_ATTR(HLF_V);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003451 if ((clip_star.available && !clip_star.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003452 && clip_isautosel_star())
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003453 || (clip_plus.available && !clip_plus.owned
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003454 && clip_isautosel_plus()))
3455 vi_attr = HL_ATTR(HLF_VNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#endif
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003460 /*
3461 * handle 'incsearch' and ":s///c" highlighting
3462 */
3463 else if (highlight_match
3464 && wp == curwin
3465 && lnum >= curwin->w_cursor.lnum
3466 && lnum <= curwin->w_cursor.lnum + search_match_lines)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003468 if (lnum == curwin->w_cursor.lnum)
3469 getvcol(curwin, &(curwin->w_cursor),
Bram Moolenaarc6663882019-02-22 19:14:54 +01003470 (colnr_T *)&fromcol, NULL, NULL);
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003471 else
3472 fromcol = 0;
3473 if (lnum == curwin->w_cursor.lnum + search_match_lines)
3474 {
3475 pos.lnum = lnum;
3476 pos.col = search_match_endcol;
3477 getvcol(curwin, &pos, (colnr_T *)&tocol, NULL, NULL);
3478 }
3479 else
3480 tocol = MAXCOL;
3481 /* do at least one character; happens when past end of line */
3482 if (fromcol == tocol)
3483 tocol = fromcol + 1;
3484 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003485 vi_attr = HL_ATTR(HLF_I);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488
3489#ifdef FEAT_DIFF
3490 filler_lines = diff_check(wp, lnum);
3491 if (filler_lines < 0)
3492 {
3493 if (filler_lines == -1)
3494 {
3495 if (diff_find_change(wp, lnum, &change_start, &change_end))
3496 diff_hlf = HLF_ADD; /* added line */
3497 else if (change_start == 0)
3498 diff_hlf = HLF_TXD; /* changed text */
3499 else
3500 diff_hlf = HLF_CHD; /* changed line */
3501 }
3502 else
3503 diff_hlf = HLF_ADD; /* added line */
3504 filler_lines = 0;
3505 area_highlighting = TRUE;
3506 }
3507 if (lnum == wp->w_topline)
3508 filler_lines = wp->w_topfill;
3509 filler_todo = filler_lines;
3510#endif
3511
3512#ifdef LINE_ATTR
3513# ifdef FEAT_SIGNS
3514 /* If this line has a sign with line highlighting set line_attr. */
3515 v = buf_getsigntype(wp->w_buffer, lnum, SIGN_LINEHL);
3516 if (v != 0)
3517 line_attr = sign_get_attr((int)v, TRUE);
3518# endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003519# if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 /* Highlight the current line in the quickfix window. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003521 if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum)
Bram Moolenaar21020352017-06-13 17:21:04 +02003522 line_attr = HL_ATTR(HLF_QFL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523# endif
3524 if (line_attr != 0)
3525 area_highlighting = TRUE;
3526#endif
3527
3528 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3529 ptr = line;
3530
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003531#ifdef FEAT_SPELL
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003532 if (has_spell && !number_only)
Bram Moolenaar30abd282005-06-22 22:35:10 +00003533 {
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003534 /* For checking first word with a capital skip white space. */
3535 if (cap_col == 0)
Bram Moolenaare2e69e42017-09-02 20:30:35 +02003536 cap_col = getwhitecols(line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00003537
Bram Moolenaar30abd282005-06-22 22:35:10 +00003538 /* To be able to spell-check over line boundaries copy the end of the
3539 * current line into nextline[]. Above the start of the next line was
3540 * copied to nextline[SPWORDLEN]. */
3541 if (nextline[SPWORDLEN] == NUL)
3542 {
3543 /* No next line or it is empty. */
3544 nextlinecol = MAXCOL;
3545 nextline_idx = 0;
3546 }
3547 else
3548 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003549 v = (long)STRLEN(line);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003550 if (v < SPWORDLEN)
3551 {
3552 /* Short line, use it completely and append the start of the
3553 * next line. */
3554 nextlinecol = 0;
3555 mch_memmove(nextline, line, (size_t)v);
Bram Moolenaar446cb832008-06-24 21:56:24 +00003556 STRMOVE(nextline + v, nextline + SPWORDLEN);
Bram Moolenaar30abd282005-06-22 22:35:10 +00003557 nextline_idx = v + 1;
3558 }
3559 else
3560 {
3561 /* Long line, use only the last SPWORDLEN bytes. */
3562 nextlinecol = v - SPWORDLEN;
3563 mch_memmove(nextline, line + nextlinecol, SPWORDLEN);
3564 nextline_idx = SPWORDLEN + 1;
3565 }
3566 }
3567 }
3568#endif
3569
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003570 if (wp->w_p_list)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar895d9662019-01-31 21:57:21 +01003572 if (lcs_space || lcs_trail || lcs_nbsp)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003573 extra_check = TRUE;
3574 /* find start of trailing whitespace */
3575 if (lcs_trail)
3576 {
3577 trailcol = (colnr_T)STRLEN(ptr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003578 while (trailcol > (colnr_T)0 && VIM_ISWHITE(ptr[trailcol - 1]))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01003579 --trailcol;
3580 trailcol += (colnr_T) (ptr - line);
3581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003584 wcr_attr = get_wcr_attr(wp);
3585 if (wcr_attr != 0)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003586 {
Bram Moolenaar60cdb302019-05-27 21:54:10 +02003587 win_attr = wcr_attr;
3588 area_highlighting = TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003589 }
3590#ifdef FEAT_TEXT_PROP
3591 if (bt_popup(wp->w_buffer))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003592 screen_line_flags |= SLF_POPUP;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003593#endif
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /*
3596 * 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
3597 * first character to be displayed.
3598 */
3599 if (wp->w_p_wrap)
3600 v = wp->w_skipcol;
3601 else
3602 v = wp->w_leftcol;
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02003603 if (v > 0 && !number_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u *prev_ptr = ptr;
Bram Moolenaara12a1612019-01-24 16:39:02 +01003606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 while (vcol < v && *ptr != NUL)
3608 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02003609 c = win_lbr_chartabsize(wp, line, ptr, (colnr_T)vcol, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 vcol += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 prev_ptr = ptr;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003612 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 }
3614
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003615 /* When:
3616 * - 'cuc' is set, or
Bram Moolenaar1a384422010-07-14 19:53:30 +02003617 * - 'colorcolumn' is set, or
Bram Moolenaarbb6a7052009-11-03 16:36:44 +00003618 * - 'virtualedit' is set, or
3619 * - the visual mode is active,
3620 * the end of the line may be before the start of the displayed part.
3621 */
3622 if (vcol < v && (
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003623#ifdef FEAT_SYN_HL
3624 wp->w_p_cuc || draw_color_col ||
3625#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003626 virtual_active() ||
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003627 (VIsual_active && wp->w_buffer == curwin->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 vcol = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 /* Handle a character that's not completely on the screen: Put ptr at
3631 * that character but skip the first few screen characters. */
3632 if (vcol > v)
3633 {
3634 vcol -= c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 ptr = prev_ptr;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01003636 /* If the character fits on the screen, don't need to skip it.
3637 * Except for a TAB. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01003638 if (( (*mb_ptr2cells)(ptr) >= c || *ptr == TAB) && col == 0)
Bram Moolenaar04e87b72017-02-01 21:23:10 +01003639 n_skip = v - vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641
3642 /*
3643 * Adjust for when the inverted text is before the screen,
3644 * and when the start of the inverted text is before the screen.
3645 */
3646 if (tocol <= vcol)
3647 fromcol = 0;
3648 else if (fromcol >= 0 && fromcol < vcol)
3649 fromcol = vcol;
3650
3651#ifdef FEAT_LINEBREAK
3652 /* When w_skipcol is non-zero, first line needs 'showbreak' */
3653 if (wp->w_p_wrap)
3654 need_showbreak = TRUE;
3655#endif
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003656#ifdef FEAT_SPELL
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003657 /* When spell checking a word we need to figure out the start of the
3658 * word and if it's badly spelled or not. */
3659 if (has_spell)
3660 {
3661 int len;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003662 colnr_T linecol = (colnr_T)(ptr - line);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003663 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003664
3665 pos = wp->w_cursor;
3666 wp->w_cursor.lnum = lnum;
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003667 wp->w_cursor.col = linecol;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003668 len = spell_move_to(wp, FORWARD, TRUE, TRUE, &spell_hlf);
Bram Moolenaar4ef9e492008-02-13 20:49:04 +00003669
3670 /* spell_move_to() may call ml_get() and make "line" invalid */
3671 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3672 ptr = line + linecol;
3673
Bram Moolenaar60a795a2005-09-16 21:55:43 +00003674 if (len == 0 || (int)wp->w_cursor.col > ptr - line)
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003675 {
3676 /* no bad word found at line start, don't check until end of a
3677 * word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003678 spell_hlf = HLF_COUNT;
Bram Moolenaar3b393a02012-06-06 19:05:50 +02003679 word_end = (int)(spell_to_word_end(ptr, wp) - line + 1);
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003680 }
3681 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003682 {
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003683 /* bad word found, use attributes until end of word */
3684 word_end = wp->w_cursor.col + len + 1;
3685
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003686 /* Turn index into actual attributes. */
3687 if (spell_hlf != HLF_COUNT)
3688 spell_attr = highlight_attr[spell_hlf];
3689 }
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003690 wp->w_cursor = pos;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003691
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003692# ifdef FEAT_SYN_HL
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003693 /* Need to restart syntax highlighting for this line. */
3694 if (has_syntax)
Bram Moolenaarf3d769a2017-09-22 13:44:56 +02003695 syntax_start(wp, lnum);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003696# endif
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00003697 }
3698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700
3701 /*
3702 * Correct highlighting for cursor that can't be disabled.
3703 * Avoids having to check this for each character.
3704 */
3705 if (fromcol >= 0)
3706 {
3707 if (noinvcur)
3708 {
3709 if ((colnr_T)fromcol == wp->w_virtcol)
3710 {
3711 /* highlighting starts at cursor, let it start just after the
3712 * cursor */
3713 fromcol_prev = fromcol;
3714 fromcol = -1;
3715 }
3716 else if ((colnr_T)fromcol < wp->w_virtcol)
3717 /* restart highlighting after the cursor */
3718 fromcol_prev = wp->w_virtcol;
3719 }
3720 if (fromcol >= tocol)
3721 fromcol = -1;
3722 }
3723
3724#ifdef FEAT_SEARCH_EXTRA
3725 /*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003726 * Handle highlighting the last used search pattern and matches.
3727 * Do this for both search_hl and the match list.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003728 * Not in a popup window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003730 cur = wp->w_match_head;
3731 shl_flag = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003732 while ((cur != NULL || shl_flag == FALSE) && !number_only
3733 && !(screen_line_flags & SLF_POPUP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003735 if (shl_flag == FALSE)
3736 {
3737 shl = &search_hl;
3738 shl_flag = TRUE;
3739 }
3740 else
3741 shl = &cur->hl;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003742 shl->startcol = MAXCOL;
3743 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 shl->attr_cur = 0;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02003745 shl->is_addpos = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02003746 v = (long)(ptr - line);
3747 if (cur != NULL)
3748 cur->pos.cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02003749 next_search_hl(wp, shl, lnum, (colnr_T)v,
3750 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02003751
3752 /* Need to get the line again, a multi-line regexp may have made it
3753 * invalid. */
3754 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
3755 ptr = line + v;
3756
3757 if (shl->lnum != 0 && shl->lnum <= lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003759 if (shl->lnum == lnum)
3760 shl->startcol = shl->rm.startpos[0].col;
3761 else
3762 shl->startcol = 0;
3763 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
3764 - shl->rm.startpos[0].lnum)
3765 shl->endcol = shl->rm.endpos[0].col;
3766 else
3767 shl->endcol = MAXCOL;
3768 /* Highlight one character for an empty match. */
3769 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02003771 if (has_mbyte && line[shl->endcol] != NUL)
3772 shl->endcol += (*mb_ptr2len)(line + shl->endcol);
3773 else
Bram Moolenaarb3414592014-06-17 17:48:32 +02003774 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02003776 if ((long)shl->startcol < v) /* match at leftcol */
3777 {
3778 shl->attr_cur = shl->attr;
3779 search_attr = shl->attr;
3780 }
3781 area_highlighting = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00003783 if (shl != &search_hl && cur != NULL)
3784 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786#endif
3787
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003788#ifdef FEAT_SYN_HL
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003789 // Cursor line highlighting for 'cursorline' in the current window.
3790 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003791 {
Bram Moolenaar8156ed32019-03-09 11:46:15 +01003792 // Do not show the cursor line when Visual mode is active, because it's
3793 // not clear what is selected then. Do update w_last_cursorline.
3794 if (!(wp == curwin && VIsual_active))
3795 {
3796 line_attr = HL_ATTR(HLF_CUL);
3797 area_highlighting = TRUE;
3798 }
Bram Moolenaarc07ff5c2019-01-30 21:41:14 +01003799 wp->w_last_cursorline = wp->w_cursor.lnum;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00003800 }
3801#endif
3802
Bram Moolenaar98aefe72018-12-13 22:20:09 +01003803#ifdef FEAT_TEXT_PROP
3804 {
3805 char_u *prop_start;
3806
3807 text_prop_count = get_text_props(wp->w_buffer, lnum,
3808 &prop_start, FALSE);
3809 if (text_prop_count > 0)
3810 {
3811 // Make a copy of the properties, so that they are properly
3812 // aligned.
3813 text_props = (textprop_T *)alloc(
3814 text_prop_count * sizeof(textprop_T));
3815 if (text_props != NULL)
3816 mch_memmove(text_props, prop_start,
3817 text_prop_count * sizeof(textprop_T));
3818
3819 // Allocate an array for the indexes.
3820 text_prop_idxs = (int *)alloc(text_prop_count * sizeof(int));
3821 area_highlighting = TRUE;
3822 extra_check = TRUE;
3823 }
3824 }
3825#endif
3826
Bram Moolenaar92d640f2005-09-05 22:11:52 +00003827 off = (unsigned)(current_ScreenLine - ScreenLines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 col = 0;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#ifdef FEAT_RIGHTLEFT
3831 if (wp->w_p_rl)
3832 {
3833 /* Rightleft window: process the text in the normal direction, but put
3834 * it in current_ScreenLine[] from right to left. Start at the
3835 * rightmost column of the window. */
Bram Moolenaar02631462017-09-22 15:20:32 +02003836 col = wp->w_width - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 off += col;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003838 screen_line_flags |= SLF_RIGHTLEFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840#endif
3841
3842 /*
3843 * Repeat for the whole displayed line.
3844 */
3845 for (;;)
3846 {
Bram Moolenaar6561d522015-07-21 15:48:27 +02003847#ifdef FEAT_CONCEAL
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02003848 int has_match_conc = 0; // match wants to conceal
3849 int did_decrement_ptr = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +02003850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 /* Skip this quickly when working on the text. */
3852 if (draw_state != WL_LINE)
3853 {
3854#ifdef FEAT_CMDWIN
3855 if (draw_state == WL_CMDLINE - 1 && n_extra == 0)
3856 {
3857 draw_state = WL_CMDLINE;
3858 if (cmdwin_type != 0 && wp == curwin)
3859 {
3860 /* Draw the cmdline character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 n_extra = 1;
Bram Moolenaara064ac82007-08-05 18:10:54 +00003862 c_extra = cmdwin_type;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003863 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003864 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_AT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 }
3867#endif
3868
3869#ifdef FEAT_FOLDING
3870 if (draw_state == WL_FOLD - 1 && n_extra == 0)
3871 {
Bram Moolenaar1c934292015-01-27 16:39:29 +01003872 int fdc = compute_foldcolumn(wp, 0);
3873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 draw_state = WL_FOLD;
Bram Moolenaar1c934292015-01-27 16:39:29 +01003875 if (fdc > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 {
Bram Moolenaar62706602016-12-09 19:28:48 +01003877 /* Draw the 'foldcolumn'. Allocate a buffer, "extra" may
Bram Moolenaarc695cec2017-01-08 20:00:04 +01003878 * already be in use. */
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01003879 vim_free(p_extra_free);
Bram Moolenaar62706602016-12-09 19:28:48 +01003880 p_extra_free = alloc(12 + 1);
3881
3882 if (p_extra_free != NULL)
3883 {
3884 fill_foldcolumn(p_extra_free, wp, FALSE, lnum);
3885 n_extra = fdc;
3886 p_extra_free[n_extra] = NUL;
3887 p_extra = p_extra_free;
3888 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003889 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003890 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_FC));
Bram Moolenaar62706602016-12-09 19:28:48 +01003891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894#endif
3895
3896#ifdef FEAT_SIGNS
3897 if (draw_state == WL_SIGN - 1 && n_extra == 0)
3898 {
3899 draw_state = WL_SIGN;
3900 /* Show the sign column when there are any signs in this
3901 * buffer or when using Netbeans. */
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02003902 if (signcolumn_on(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003904 int text_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905# ifdef FEAT_SIGN_ICONS
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01003906 int icon_sign;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907# endif
3908
3909 /* Draw two cells with the sign value or blank. */
3910 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01003911 c_final = NUL;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02003912 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_SC));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 n_extra = 2;
3914
Bram Moolenaarbc6cf6c2014-05-22 15:51:04 +02003915 if (row == startrow
3916#ifdef FEAT_DIFF
3917 + filler_lines && filler_todo <= 0
3918#endif
3919 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921 text_sign = buf_getsigntype(wp->w_buffer, lnum,
3922 SIGN_TEXT);
3923# ifdef FEAT_SIGN_ICONS
3924 icon_sign = buf_getsigntype(wp->w_buffer, lnum,
3925 SIGN_ICON);
3926 if (gui.in_use && icon_sign != 0)
3927 {
3928 /* Use the image in this position. */
3929 c_extra = SIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003930 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931# ifdef FEAT_NETBEANS_INTG
3932 if (buf_signcount(wp->w_buffer, lnum) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01003933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 c_extra = MULTISIGN_BYTE;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003935 c_final = NUL;
3936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937# endif
3938 char_attr = icon_sign;
3939 }
3940 else
3941# endif
3942 if (text_sign != 0)
3943 {
3944 p_extra = sign_get_text(text_sign);
3945 if (p_extra != NULL)
3946 {
3947 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01003948 c_final = NUL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003949 n_extra = (int)STRLEN(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 char_attr = sign_get_attr(text_sign, FALSE);
3952 }
3953 }
3954 }
3955 }
3956#endif
3957
3958 if (draw_state == WL_NR - 1 && n_extra == 0)
3959 {
3960 draw_state = WL_NR;
Bram Moolenaar64486672010-05-16 15:46:46 +02003961 /* Display the absolute or relative line number. After the
3962 * first fill with blanks when the 'n' flag isn't in 'cpo' */
3963 if ((wp->w_p_nu || wp->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 && (row == startrow
3965#ifdef FEAT_DIFF
3966 + filler_lines
3967#endif
3968 || vim_strchr(p_cpo, CPO_NUMCOL) == NULL))
3969 {
3970 /* Draw the line number (empty space after wrapping). */
3971 if (row == startrow
3972#ifdef FEAT_DIFF
3973 + filler_lines
3974#endif
3975 )
3976 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003977 long num;
Bram Moolenaar700e7342013-01-30 12:31:36 +01003978 char *fmt = "%*ld ";
Bram Moolenaar64486672010-05-16 15:46:46 +02003979
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003980 if (wp->w_p_nu && !wp->w_p_rnu)
3981 /* 'number' + 'norelativenumber' */
Bram Moolenaar64486672010-05-16 15:46:46 +02003982 num = (long)lnum;
3983 else
Bram Moolenaar700e7342013-01-30 12:31:36 +01003984 {
Bram Moolenaar64486672010-05-16 15:46:46 +02003985 /* 'relativenumber', don't use negative numbers */
Bram Moolenaar7eb46522010-12-30 14:57:08 +01003986 num = labs((long)get_cursor_rel_lnum(wp, lnum));
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003987 if (num == 0 && wp->w_p_nu && wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01003988 {
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +02003989 /* 'number' + 'relativenumber' */
Bram Moolenaar700e7342013-01-30 12:31:36 +01003990 num = lnum;
3991 fmt = "%-*ld ";
3992 }
3993 }
Bram Moolenaar64486672010-05-16 15:46:46 +02003994
Bram Moolenaar700e7342013-01-30 12:31:36 +01003995 sprintf((char *)extra, fmt,
Bram Moolenaar64486672010-05-16 15:46:46 +02003996 number_width(wp), num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 if (wp->w_skipcol > 0)
3998 for (p_extra = extra; *p_extra == ' '; ++p_extra)
3999 *p_extra = '-';
4000#ifdef FEAT_RIGHTLEFT
4001 if (wp->w_p_rl) /* reverse line numbers */
Bram Moolenaare73f9112019-03-29 18:29:54 +01004002 {
4003 char_u *p1, *p2;
4004 int t;
4005
4006 // like rl_mirror(), but keep the space at the end
4007 p2 = skiptowhite(extra) - 1;
4008 for (p1 = extra; p1 < p2; ++p1, --p2)
4009 {
4010 t = *p1;
4011 *p1 = *p2;
4012 *p2 = t;
4013 }
4014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
4016 p_extra = extra;
4017 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004018 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 c_extra = ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004023 c_final = NUL;
4024 }
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004025 n_extra = number_width(wp) + 1;
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004026 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_N));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004027#ifdef FEAT_SYN_HL
4028 /* When 'cursorline' is set highlight the line number of
Bram Moolenaar06ca5132012-03-23 16:25:17 +01004029 * the current line differently.
4030 * TODO: Can we use CursorLine instead of CursorLineNr
4031 * when CursorLineNr isn't set? */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004032 if ((wp->w_p_cul || wp->w_p_rnu)
Bram Moolenaar700e7342013-01-30 12:31:36 +01004033 && lnum == wp->w_cursor.lnum)
Bram Moolenaar193ffd12019-05-25 22:57:30 +02004034 char_attr = hl_combine_attr(wcr_attr, HL_ATTR(HLF_CLN));
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
4037 }
4038
Bram Moolenaar597a4222014-06-25 14:39:50 +02004039#ifdef FEAT_LINEBREAK
4040 if (wp->w_p_brisbr && draw_state == WL_BRI - 1
4041 && n_extra == 0 && *p_sbr != NUL)
4042 /* draw indent after showbreak value */
4043 draw_state = WL_BRI;
4044 else if (wp->w_p_brisbr && draw_state == WL_SBR && n_extra == 0)
4045 /* After the showbreak, draw the breakindent */
4046 draw_state = WL_BRI - 1;
4047
4048 /* draw 'breakindent': indent wrapped text accordingly */
4049 if (draw_state == WL_BRI - 1 && n_extra == 0)
4050 {
4051 draw_state = WL_BRI;
Bram Moolenaar6c896862016-11-17 19:46:51 +01004052 /* if need_showbreak is set, breakindent also applies */
4053 if (wp->w_p_bri && n_extra == 0
4054 && (row != startrow || need_showbreak)
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004055# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004056 && filler_lines == 0
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004057# endif
Bram Moolenaar597a4222014-06-25 14:39:50 +02004058 )
4059 {
Bram Moolenaar6c896862016-11-17 19:46:51 +01004060 char_attr = 0;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004061# ifdef FEAT_DIFF
Bram Moolenaar597a4222014-06-25 14:39:50 +02004062 if (diff_hlf != (hlf_T)0)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004063 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004064 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004065# ifdef FEAT_SYN_HL
Bram Moolenaare0f14822014-08-06 13:20:56 +02004066 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
4067 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004068 HL_ATTR(HLF_CUL));
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004069# endif
Bram Moolenaare0f14822014-08-06 13:20:56 +02004070 }
Bram Moolenaard710e0d2015-06-10 12:16:47 +02004071# endif
Bram Moolenaarb8b57462014-07-03 22:54:08 +02004072 p_extra = NULL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02004073 c_extra = ' ';
4074 n_extra = get_breakindent_win(wp,
4075 ml_get_buf(wp->w_buffer, lnum, FALSE));
4076 /* Correct end of highlighted area for 'breakindent',
4077 * required when 'linebreak' is also set. */
4078 if (tocol == vcol)
4079 tocol += n_extra;
4080 }
4081 }
4082#endif
4083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#if defined(FEAT_LINEBREAK) || defined(FEAT_DIFF)
4085 if (draw_state == WL_SBR - 1 && n_extra == 0)
4086 {
4087 draw_state = WL_SBR;
4088# ifdef FEAT_DIFF
4089 if (filler_todo > 0)
4090 {
4091 /* Draw "deleted" diff line(s). */
4092 if (char2cells(fill_diff) > 1)
Bram Moolenaar83a52172019-01-16 22:41:54 +01004093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 c_extra = '-';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004095 c_final = NUL;
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 else
Bram Moolenaar83a52172019-01-16 22:41:54 +01004098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 c_extra = fill_diff;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004100 c_final = NUL;
4101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102# ifdef FEAT_RIGHTLEFT
4103 if (wp->w_p_rl)
4104 n_extra = col + 1;
4105 else
4106# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004107 n_extra = wp->w_width - col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004108 char_attr = HL_ATTR(HLF_DED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110# endif
4111# ifdef FEAT_LINEBREAK
4112 if (*p_sbr != NUL && need_showbreak)
4113 {
4114 /* Draw 'showbreak' at the start of each broken line. */
4115 p_extra = p_sbr;
4116 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004117 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 n_extra = (int)STRLEN(p_sbr);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004119 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 need_showbreak = FALSE;
Bram Moolenaard574ea22015-01-14 19:35:14 +01004121 vcol_sbr = vcol + MB_CHARLEN(p_sbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 /* Correct end of highlighted area for 'showbreak',
4123 * required when 'linebreak' is also set. */
4124 if (tocol == vcol)
4125 tocol += n_extra;
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004126#ifdef FEAT_SYN_HL
4127 /* combine 'showbreak' with 'cursorline' */
Bram Moolenaarbd65c462013-07-01 20:18:33 +02004128 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaare0f14822014-08-06 13:20:56 +02004129 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01004130 HL_ATTR(HLF_CUL));
Bram Moolenaar5a4d51e2013-06-30 17:24:16 +02004131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133# endif
4134 }
4135#endif
4136
4137 if (draw_state == WL_LINE - 1 && n_extra == 0)
4138 {
4139 draw_state = WL_LINE;
4140 if (saved_n_extra)
4141 {
4142 /* Continue item from end of wrapped line. */
4143 n_extra = saved_n_extra;
4144 c_extra = saved_c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004145 c_final = saved_c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 p_extra = saved_p_extra;
4147 char_attr = saved_char_attr;
4148 }
4149 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004150 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 }
4153
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004154 // When still displaying '$' of change command, stop at cursor.
4155 // When only displaying the (relative) line number and that's done,
4156 // stop here.
4157 if ((dollar_vcol >= 0 && wp == curwin
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004158 && lnum == wp->w_cursor.lnum && vcol >= (long)wp->w_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#ifdef FEAT_DIFF
4160 && filler_todo <= 0
4161#endif
4162 )
Bram Moolenaarbd9a53c2018-09-12 23:15:48 +02004163 || (number_only && draw_state > WL_NR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar02631462017-09-22 15:20:32 +02004165 screen_line(screen_row, wp->w_wincol, col, -(int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004166 screen_line_flags);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004167 /* Pretend we have finished updating the window. Except when
4168 * 'cursorcolumn' is set. */
4169#ifdef FEAT_SYN_HL
4170 if (wp->w_p_cuc)
4171 row = wp->w_cline_row + wp->w_cline_height;
4172 else
4173#endif
4174 row = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 break;
4176 }
4177
Bram Moolenaar637532b2019-01-03 21:44:40 +01004178 if (draw_state == WL_LINE && (area_highlighting
4179#ifdef FEAT_SPELL
4180 || has_spell
4181#endif
4182 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
4184 /* handle Visual or match highlighting in this line */
4185 if (vcol == fromcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 || (has_mbyte && vcol + 1 == fromcol && n_extra == 0
4187 && (*mb_ptr2cells)(ptr) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 || ((int)vcol_prev == fromcol_prev
Bram Moolenaarfa363cd2009-02-21 20:23:59 +00004189 && vcol_prev < vcol /* not at margin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 && vcol < tocol))
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004191 area_attr = vi_attr; /* start highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 else if (area_attr != 0
4193 && (vcol == tocol
4194 || (noinvcur && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 area_attr = 0; /* stop highlighting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197#ifdef FEAT_SEARCH_EXTRA
4198 if (!n_extra)
4199 {
4200 /*
4201 * Check for start/end of search pattern match.
4202 * After end, check for start/end of next match.
4203 * When another match, have to check for start again.
4204 * Watch out for matching an empty string!
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004205 * Do this for 'search_hl' and the match list (ordered by
4206 * priority).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004208 v = (long)(ptr - line);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004209 cur = wp->w_match_head;
4210 shl_flag = FALSE;
4211 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004213 if (shl_flag == FALSE
4214 && ((cur != NULL
4215 && cur->priority > SEARCH_HL_PRIORITY)
4216 || cur == NULL))
4217 {
4218 shl = &search_hl;
4219 shl_flag = TRUE;
4220 }
4221 else
4222 shl = &cur->hl;
Bram Moolenaarb3414592014-06-17 17:48:32 +02004223 if (cur != NULL)
4224 cur->pos.cur = 0;
4225 pos_inprogress = TRUE;
4226 while (shl->rm.regprog != NULL
4227 || (cur != NULL && pos_inprogress))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004229 if (shl->startcol != MAXCOL
4230 && v >= (long)shl->startcol
4231 && v < (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004233 int tmp_col = v + MB_PTR2LEN(ptr);
4234
4235 if (shl->endcol < tmp_col)
4236 shl->endcol = tmp_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 shl->attr_cur = shl->attr;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004238#ifdef FEAT_CONCEAL
Bram Moolenaarab62c192019-03-30 16:39:05 +01004239 // Match with the "Conceal" group results in hiding
4240 // the match.
4241 if (cur != NULL
4242 && shl != &search_hl
4243 && syn_name2id((char_u *)"Conceal")
4244 == cur->hlg_id)
Bram Moolenaar6561d522015-07-21 15:48:27 +02004245 {
Bram Moolenaar4d585022016-04-14 19:50:22 +02004246 has_match_conc =
4247 v == (long)shl->startcol ? 2 : 1;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004248 match_conc = cur->conceal_char;
4249 }
4250 else
Bram Moolenaar4d585022016-04-14 19:50:22 +02004251 has_match_conc = match_conc = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02004252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
Bram Moolenaaraff5c3a2014-12-13 03:36:39 +01004254 else if (v == (long)shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
4256 shl->attr_cur = 0;
Bram Moolenaare17bdff2016-08-27 18:34:29 +02004257 next_search_hl(wp, shl, lnum, (colnr_T)v,
4258 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02004259 pos_inprogress = cur == NULL || cur->pos.cur == 0
Bram Moolenaar6561d522015-07-21 15:48:27 +02004260 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 /* Need to get the line again, a multi-line regexp
4263 * may have made it invalid. */
4264 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4265 ptr = line + v;
4266
4267 if (shl->lnum == lnum)
4268 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004269 shl->startcol = shl->rm.startpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 if (shl->rm.endpos[0].lnum == 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004271 shl->endcol = shl->rm.endpos[0].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004273 shl->endcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004275 if (shl->startcol == shl->endcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
4277 /* highlight empty match, try again after
4278 * it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004280 shl->endcol += (*mb_ptr2len)(line
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004281 + shl->endcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004283 ++shl->endcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 /* Loop to check if the match starts at the
4287 * current position */
4288 continue;
4289 }
4290 }
4291 break;
4292 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004293 if (shl != &search_hl && cur != NULL)
4294 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004296
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004297 /* Use attributes from match with highest priority among
4298 * 'search_hl' and the match list. */
4299 search_attr = search_hl.attr_cur;
4300 cur = wp->w_match_head;
4301 shl_flag = FALSE;
4302 while (cur != NULL || shl_flag == FALSE)
4303 {
4304 if (shl_flag == FALSE
4305 && ((cur != NULL
4306 && cur->priority > SEARCH_HL_PRIORITY)
4307 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004308 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004309 shl = &search_hl;
4310 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00004311 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004312 else
4313 shl = &cur->hl;
4314 if (shl->attr_cur != 0)
4315 search_attr = shl->attr_cur;
4316 if (shl != &search_hl && cur != NULL)
4317 cur = cur->next;
4318 }
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004319 /* Only highlight one character after the last column. */
Bram Moolenaar5ece3e32017-10-01 14:35:02 +02004320 if (*ptr == NUL && (did_line_attr >= 1
4321 || (wp->w_p_list && lcs_eol_one == -1)))
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02004322 search_attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324#endif
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326#ifdef FEAT_DIFF
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004327 if (diff_hlf != (hlf_T)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004329 if (diff_hlf == HLF_CHD && ptr - line >= change_start
4330 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 diff_hlf = HLF_TXD; /* changed text */
Bram Moolenaar4b80a512007-06-19 15:44:58 +00004332 if (diff_hlf == HLF_TXD && ptr - line > change_end
4333 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 diff_hlf = HLF_CHD; /* changed line */
Bram Moolenaar8820b482017-03-16 17:23:31 +01004335 line_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02004336 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
Bram Moolenaar8820b482017-03-16 17:23:31 +01004337 line_attr = hl_combine_attr(line_attr, HL_ATTR(HLF_CUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 }
4339#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004340
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004341#ifdef FEAT_TEXT_PROP
4342 if (text_props != NULL)
4343 {
4344 int pi;
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004345 int bcol = (int)(ptr - line);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004346
4347 // Check if any active property ends.
4348 for (pi = 0; pi < text_props_active; ++pi)
4349 {
4350 int tpi = text_prop_idxs[pi];
4351
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004352 if (bcol >= text_props[tpi].tp_col - 1
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004353 + text_props[tpi].tp_len)
4354 {
4355 if (pi + 1 < text_props_active)
4356 mch_memmove(text_prop_idxs + pi,
4357 text_prop_idxs + pi + 1,
4358 sizeof(int)
4359 * (text_props_active - (pi + 1)));
4360 --text_props_active;
4361 --pi;
4362 }
4363 }
4364
4365 // Add any text property that starts in this column.
4366 while (text_prop_next < text_prop_count
Bram Moolenaar5e53ac02019-01-01 20:31:31 +01004367 && bcol >= text_props[text_prop_next].tp_col - 1)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004368 text_prop_idxs[text_props_active++] = text_prop_next++;
4369
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004370 text_prop_attr = 0;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004371 text_prop_combine = FALSE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004372 if (text_props_active > 0)
4373 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004374 // Sort the properties on priority and/or starting last.
4375 // Then combine the attributes, highest priority last.
4376 current_text_props = text_props;
4377 current_buf = wp->w_buffer;
4378 qsort((void *)text_prop_idxs, (size_t)text_props_active,
4379 sizeof(int), text_prop_compare);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004380
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004381 for (pi = 0; pi < text_props_active; ++pi)
4382 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004383 int tpi = text_prop_idxs[pi];
Bram Moolenaarde24a872019-05-05 15:48:00 +02004384 proptype_T *pt = text_prop_type_by_id(
4385 wp->w_buffer, text_props[tpi].tp_type);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004386
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004387 if (pt != NULL)
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004388 {
Bram Moolenaarc6d86dc2018-12-31 13:57:36 +01004389 int pt_attr = syn_id2attr(pt->pt_hl_id);
4390
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004391 text_prop_type = pt;
Bram Moolenaarde24a872019-05-05 15:48:00 +02004392 text_prop_attr =
4393 hl_combine_attr(text_prop_attr, pt_attr);
4394 text_prop_combine = pt->pt_flags & PT_FLAG_COMBINE;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004395 }
4396 }
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004397 }
4398 }
4399#endif
4400
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004401 /* Decide which of the highlight attributes to use. */
4402 attr_pri = TRUE;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004403#ifdef LINE_ATTR
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004404 if (area_attr != 0)
4405 char_attr = hl_combine_attr(line_attr, area_attr);
4406 else if (search_attr != 0)
4407 char_attr = hl_combine_attr(line_attr, search_attr);
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004408# ifdef FEAT_TEXT_PROP
4409 else if (text_prop_type != NULL)
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004410 {
4411 char_attr = hl_combine_attr(
4412 line_attr != 0 ? line_attr : win_attr, text_prop_attr);
4413 }
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004414# endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004415 else if (line_attr != 0 && ((fromcol == -10 && tocol == MAXCOL)
Bram Moolenaarf837ef92009-03-11 16:47:21 +00004416 || vcol < fromcol || vcol_prev < fromcol_prev
4417 || vcol >= tocol))
Bram Moolenaarbfd45122019-05-17 13:05:07 +02004418 // Use line_attr when not in the Visual or 'incsearch' area
4419 // (area_attr may be 0 when "noinvcur" is set).
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004420 char_attr = line_attr;
Bram Moolenaar09deeb72015-03-24 18:22:41 +01004421#else
4422 if (area_attr != 0)
4423 char_attr = area_attr;
4424 else if (search_attr != 0)
4425 char_attr = search_attr;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004426#endif
4427 else
4428 {
4429 attr_pri = FALSE;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004430#ifdef FEAT_TEXT_PROP
4431 if (text_prop_type != NULL)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004432 {
4433 if (text_prop_combine)
4434 char_attr = hl_combine_attr(
4435 syntax_attr, text_prop_attr);
4436 else
Bram Moolenaar7a8d0272019-05-26 23:32:06 +02004437 char_attr = hl_combine_attr(
4438 win_attr, text_prop_attr);
Bram Moolenaarde24a872019-05-05 15:48:00 +02004439 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01004440 else
4441#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004442#ifdef FEAT_SYN_HL
4443 if (has_syntax)
4444 char_attr = syntax_attr;
4445 else
4446#endif
4447 char_attr = 0;
4448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004450 if (char_attr == 0)
4451 char_attr = win_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 /*
4454 * Get the next character to put on the screen.
4455 */
4456 /*
Bram Moolenaara064ac82007-08-05 18:10:54 +00004457 * The "p_extra" points to the extra stuff that is inserted to
4458 * represent special characters (non-printable stuff) and other
4459 * things. When all characters are the same, c_extra is used.
Bram Moolenaar83a52172019-01-16 22:41:54 +01004460 * If c_final is set, it will compulsorily be used at the end.
Bram Moolenaara064ac82007-08-05 18:10:54 +00004461 * "p_extra" must end in a NUL to avoid mb_ptr2len() reads past
4462 * "p_extra[n_extra]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 * For the '$' of the 'list' option, n_extra == 1, p_extra == "".
4464 */
4465 if (n_extra > 0)
4466 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004467 if (c_extra != NUL || (n_extra == 1 && c_final != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01004469 c = (n_extra == 1 && c_final != NUL) ? c_final : c_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 mb_c = c; /* doesn't handle non-utf-8 multi-byte! */
Bram Moolenaarace95982017-03-29 17:30:27 +02004471 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004474 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004475 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else
4478 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 else
4481 {
4482 c = *p_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (has_mbyte)
4484 {
4485 mb_c = c;
4486 if (enc_utf8)
4487 {
4488 /* If the UTF-8 character is more than one byte:
4489 * Decode it into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004490 mb_l = utfc_ptr2len(p_extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 mb_utf8 = FALSE;
4492 if (mb_l > n_extra)
4493 mb_l = 1;
4494 else if (mb_l > 1)
4495 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004496 mb_c = utfc_ptr2char(p_extra, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 mb_utf8 = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004498 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500 }
4501 else
4502 {
4503 /* if this is a DBCS character, put it in "mb_c" */
4504 mb_l = MB_BYTE2LEN(c);
4505 if (mb_l >= n_extra)
4506 mb_l = 1;
4507 else if (mb_l > 1)
4508 mb_c = (c << 8) + p_extra[1];
4509 }
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004510 if (mb_l == 0) /* at the NUL at end-of-line */
4511 mb_l = 1;
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 /* If a double-width char doesn't fit display a '>' in the
4514 * last column. */
Bram Moolenaar92d640f2005-09-05 22:11:52 +00004515 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516# ifdef FEAT_RIGHTLEFT
4517 wp->w_p_rl ? (col <= 0) :
4518# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004519 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 && (*mb_char2cells)(mb_c) == 2)
4521 {
4522 c = '>';
4523 mb_c = c;
4524 mb_l = 1;
4525 mb_utf8 = FALSE;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004526 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 /* put the pointer back to output the double-width
4528 * character at the start of the next line. */
4529 ++n_extra;
4530 --p_extra;
4531 }
4532 else
4533 {
4534 n_extra -= mb_l - 1;
4535 p_extra += mb_l - 1;
4536 }
4537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 ++p_extra;
4539 }
4540 --n_extra;
4541 }
4542 else
4543 {
Bram Moolenaar88e76882017-02-27 20:33:46 +01004544#ifdef FEAT_LINEBREAK
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004545 int c0;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004546#endif
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004547
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004548 if (p_extra_free != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004549 VIM_CLEAR(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 /*
4551 * Get a character from the line itself.
4552 */
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004553 c = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004554#ifdef FEAT_LINEBREAK
Bram Moolenaar10a8da02017-02-27 21:11:35 +01004555 c0 = *ptr;
Bram Moolenaar88e76882017-02-27 20:33:46 +01004556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (has_mbyte)
4558 {
4559 mb_c = c;
4560 if (enc_utf8)
4561 {
4562 /* If the UTF-8 character is more than one byte: Decode it
4563 * into "mb_c". */
Bram Moolenaarace95982017-03-29 17:30:27 +02004564 mb_l = utfc_ptr2len(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 mb_utf8 = FALSE;
4566 if (mb_l > 1)
4567 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004568 mb_c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /* Overlong encoded ASCII or ASCII with composing char
4570 * is displayed normally, except a NUL. */
4571 if (mb_c < 0x80)
Bram Moolenaar88e76882017-02-27 20:33:46 +01004572 {
4573 c = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004574#ifdef FEAT_LINEBREAK
Bram Moolenaar88e76882017-02-27 20:33:46 +01004575 c0 = mb_c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004576#endif
Bram Moolenaar88e76882017-02-27 20:33:46 +01004577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 mb_utf8 = TRUE;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004579
4580 /* At start of the line we can have a composing char.
4581 * Draw it as a space with a composing char. */
4582 if (utf_iscomposing(mb_c))
4583 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004584 int i;
4585
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004586 for (i = Screen_mco - 1; i > 0; --i)
4587 u8cc[i] = u8cc[i - 1];
4588 u8cc[0] = mb_c;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00004589 mb_c = ' ';
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592
4593 if ((mb_l == 1 && c >= 0x80)
4594 || (mb_l >= 1 && mb_c == 0)
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004595 || (mb_l > 1 && (!vim_isprintc(mb_c))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 {
4597 /*
4598 * Illegal UTF-8 byte: display as <xx>.
4599 * Non-BMP character : display as ? or fullwidth ?.
4600 */
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004601 transchar_hex(extra, mb_c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004602# ifdef FEAT_RIGHTLEFT
Bram Moolenaar9ba61172019-01-24 18:20:17 +01004603 if (wp->w_p_rl) /* reverse */
4604 rl_mirror(extra);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 p_extra = extra;
4607 c = *p_extra;
4608 mb_c = mb_ptr2char_adv(&p_extra);
4609 mb_utf8 = (c >= 0x80);
4610 n_extra = (int)STRLEN(p_extra);
4611 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004612 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (area_attr == 0 && search_attr == 0)
4614 {
4615 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004616 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 saved_attr2 = char_attr; /* save current attr */
4618 }
4619 }
4620 else if (mb_l == 0) /* at the NUL at end-of-line */
4621 mb_l = 1;
4622#ifdef FEAT_ARABIC
4623 else if (p_arshape && !p_tbidi && ARABIC_CHAR(mb_c))
4624 {
4625 /* Do Arabic shaping. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004626 int pc, pc1, nc;
4627 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629 /* The idea of what is the previous and next
4630 * character depends on 'rightleft'. */
4631 if (wp->w_p_rl)
4632 {
4633 pc = prev_c;
4634 pc1 = prev_c1;
4635 nc = utf_ptr2char(ptr + mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004636 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 }
4638 else
4639 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 pc = utfc_ptr2char(ptr + mb_l, pcc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 nc = prev_c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004642 pc1 = pcc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 prev_c = mb_c;
4645
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004646 mb_c = arabic_shape(mb_c, &c, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 else
4649 prev_c = mb_c;
4650#endif
4651 }
4652 else /* enc_dbcs */
4653 {
4654 mb_l = MB_BYTE2LEN(c);
4655 if (mb_l == 0) /* at the NUL at end-of-line */
4656 mb_l = 1;
4657 else if (mb_l > 1)
4658 {
4659 /* We assume a second byte below 32 is illegal.
4660 * Hopefully this is OK for all double-byte encodings!
4661 */
4662 if (ptr[1] >= 32)
4663 mb_c = (c << 8) + ptr[1];
4664 else
4665 {
4666 if (ptr[1] == NUL)
4667 {
4668 /* head byte at end of line */
4669 mb_l = 1;
4670 transchar_nonprint(extra, c);
4671 }
4672 else
4673 {
4674 /* illegal tail byte */
4675 mb_l = 2;
4676 STRCPY(extra, "XX");
4677 }
4678 p_extra = extra;
4679 n_extra = (int)STRLEN(extra) - 1;
4680 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004681 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 c = *p_extra++;
4683 if (area_attr == 0 && search_attr == 0)
4684 {
4685 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004686 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 saved_attr2 = char_attr; /* save current attr */
4688 }
4689 mb_c = c;
4690 }
4691 }
4692 }
4693 /* If a double-width char doesn't fit display a '>' in the
4694 * last column; the character is displayed at the start of the
4695 * next line. */
4696 if ((
4697# ifdef FEAT_RIGHTLEFT
4698 wp->w_p_rl ? (col <= 0) :
4699# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02004700 (col >= wp->w_width - 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 && (*mb_char2cells)(mb_c) == 2)
4702 {
4703 c = '>';
4704 mb_c = c;
4705 mb_utf8 = FALSE;
4706 mb_l = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004707 multi_attr = HL_ATTR(HLF_AT);
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004708 // Put pointer back so that the character will be
4709 // displayed at the start of the next line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 --ptr;
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02004711#ifdef FEAT_CONCEAL
4712 did_decrement_ptr = TRUE;
4713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715 else if (*ptr != NUL)
4716 ptr += mb_l - 1;
4717
4718 /* If a double-width char doesn't fit at the left side display
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004719 * a '<' in the first column. Don't do this for unprintable
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004720 * characters. */
Bram Moolenaar7ba6ed32010-08-07 16:38:13 +02004721 if (n_skip > 0 && mb_l > 1 && n_extra == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 n_extra = 1;
Bram Moolenaar5641f382012-06-13 18:06:36 +02004724 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01004725 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 c = ' ';
4727 if (area_attr == 0 && search_attr == 0)
4728 {
4729 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004730 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 saved_attr2 = char_attr; /* save current attr */
4732 }
4733 mb_c = c;
4734 mb_utf8 = FALSE;
4735 mb_l = 1;
4736 }
4737
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 ++ptr;
4740
4741 if (extra_check)
4742 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004743#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00004744 int can_spell = TRUE;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004745#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004746
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004747#ifdef FEAT_TERMINAL
4748 if (get_term_attr)
4749 {
Bram Moolenaar68c4bdd2017-07-30 13:57:41 +02004750 syntax_attr = term_get_attr(wp->w_buffer, lnum, vcol);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02004751
4752 if (!attr_pri)
4753 char_attr = syntax_attr;
4754 else
4755 char_attr = hl_combine_attr(syntax_attr, char_attr);
4756 }
4757#endif
4758
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004759#ifdef FEAT_SYN_HL
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004760 // Get syntax attribute, unless still at the start of the line
4761 // (double-wide char that doesn't fit).
Bram Moolenaar217ad922005-03-20 22:37:15 +00004762 v = (long)(ptr - line);
4763 if (has_syntax && v > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 /* Get the syntax attribute for the character. If there
4766 * is an error, disable syntax highlighting. */
4767 save_did_emsg = did_emsg;
4768 did_emsg = FALSE;
4769
Bram Moolenaar217ad922005-03-20 22:37:15 +00004770 syntax_attr = get_syntax_attr((colnr_T)v - 1,
Bram Moolenaar860cae12010-06-05 23:22:07 +02004771# ifdef FEAT_SPELL
4772 has_spell ? &can_spell :
4773# endif
4774 NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 if (did_emsg)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004777 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004778 wp->w_s->b_syn_error = TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004779 has_syntax = FALSE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004780 syntax_attr = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 did_emsg = save_did_emsg;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004784
4785 // combine syntax attribute with 'wincolor'
4786 if (win_attr != 0)
4787 syntax_attr = hl_combine_attr(win_attr, syntax_attr);
4788
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004789#ifdef SYN_TIME_LIMIT
4790 if (wp->w_s->b_syn_slow)
4791 has_syntax = FALSE;
4792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 /* Need to get the line again, a multi-line regexp may
4795 * have made it invalid. */
4796 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
4797 ptr = line + v;
4798
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004799# ifdef FEAT_TEXT_PROP
Bram Moolenaarde24a872019-05-05 15:48:00 +02004800 // Text properties overrule syntax highlighting or combine.
4801 if (text_prop_attr == 0 || text_prop_combine)
4802# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004803 {
Bram Moolenaarde24a872019-05-05 15:48:00 +02004804 int comb_attr = syntax_attr;
4805# ifdef FEAT_TEXT_PROP
4806 comb_attr = hl_combine_attr(text_prop_attr, comb_attr);
4807# endif
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004808 if (!attr_pri)
Bram Moolenaarde24a872019-05-05 15:48:00 +02004809 char_attr = comb_attr;
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004810 else
Bram Moolenaarde24a872019-05-05 15:48:00 +02004811 char_attr = hl_combine_attr(comb_attr, char_attr);
Bram Moolenaar48f88ac2018-12-26 00:25:20 +01004812 }
Bram Moolenaarc095b282010-07-20 22:33:34 +02004813# ifdef FEAT_CONCEAL
4814 /* no concealing past the end of the line, it interferes
4815 * with line highlighting */
4816 if (c == NUL)
4817 syntax_flags = 0;
Bram Moolenaar27c735b2010-07-22 22:16:29 +02004818 else
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02004819 syntax_flags = get_syntax_info(&syntax_seqnr);
Bram Moolenaarc095b282010-07-20 22:33:34 +02004820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004822#endif
Bram Moolenaar217ad922005-03-20 22:37:15 +00004823
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004824#ifdef FEAT_SPELL
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004825 /* Check spelling (unless at the end of the line).
Bram Moolenaarf3681cc2005-06-08 22:03:13 +00004826 * Only do this when there is no syntax highlighting, the
4827 * @Spell cluster is not used or the current syntax item
4828 * contains the @Spell cluster. */
Bram Moolenaar30abd282005-06-22 22:35:10 +00004829 if (has_spell && v >= word_end && v > cur_checked_col)
Bram Moolenaar217ad922005-03-20 22:37:15 +00004830 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00004831 spell_attr = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00004832 if (c != 0 && (
4833# ifdef FEAT_SYN_HL
4834 !has_syntax ||
4835# endif
4836 can_spell))
Bram Moolenaar217ad922005-03-20 22:37:15 +00004837 {
Bram Moolenaar30abd282005-06-22 22:35:10 +00004838 char_u *prev_ptr, *p;
4839 int len;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004840 hlf_T spell_hlf = HLF_COUNT;
Bram Moolenaare7566042005-06-17 22:00:15 +00004841 if (has_mbyte)
4842 {
4843 prev_ptr = ptr - mb_l;
4844 v -= mb_l - 1;
4845 }
4846 else
Bram Moolenaare7566042005-06-17 22:00:15 +00004847 prev_ptr = ptr - 1;
Bram Moolenaar30abd282005-06-22 22:35:10 +00004848
4849 /* Use nextline[] if possible, it has the start of the
4850 * next line concatenated. */
4851 if ((prev_ptr - line) - nextlinecol >= 0)
4852 p = nextline + (prev_ptr - line) - nextlinecol;
4853 else
4854 p = prev_ptr;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004855 cap_col -= (int)(prev_ptr - line);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004856 len = spell_check(wp, p, &spell_hlf, &cap_col,
4857 nochange);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004858 word_end = v + len;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004859
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004860 /* In Insert mode only highlight a word that
4861 * doesn't touch the cursor. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004862 if (spell_hlf != HLF_COUNT
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004863 && (State & INSERT) != 0
4864 && wp->w_cursor.lnum == lnum
4865 && wp->w_cursor.col >=
Bram Moolenaar217ad922005-03-20 22:37:15 +00004866 (colnr_T)(prev_ptr - line)
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004867 && wp->w_cursor.col < (colnr_T)word_end)
4868 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004869 spell_hlf = HLF_COUNT;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00004870 spell_redraw_lnum = lnum;
Bram Moolenaar217ad922005-03-20 22:37:15 +00004871 }
Bram Moolenaar30abd282005-06-22 22:35:10 +00004872
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004873 if (spell_hlf == HLF_COUNT && p != prev_ptr
Bram Moolenaar30abd282005-06-22 22:35:10 +00004874 && (p - nextline) + len > nextline_idx)
4875 {
4876 /* Remember that the good word continues at the
4877 * start of the next line. */
4878 checked_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004879 checked_col = (int)((p - nextline) + len - nextline_idx);
Bram Moolenaar30abd282005-06-22 22:35:10 +00004880 }
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004881
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004882 /* Turn index into actual attributes. */
4883 if (spell_hlf != HLF_COUNT)
4884 spell_attr = highlight_attr[spell_hlf];
4885
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004886 if (cap_col > 0)
4887 {
4888 if (p != prev_ptr
4889 && (p - nextline) + cap_col >= nextline_idx)
4890 {
4891 /* Remember that the word in the next line
4892 * must start with a capital. */
4893 capcol_lnum = lnum + 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004894 cap_col = (int)((p - nextline) + cap_col
4895 - nextline_idx);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004896 }
4897 else
4898 /* Compute the actual column. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004899 cap_col += (int)(prev_ptr - line);
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00004900 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004901 }
Bram Moolenaar217ad922005-03-20 22:37:15 +00004902 }
4903 if (spell_attr != 0)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004904 {
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004905 if (!attr_pri)
Bram Moolenaar30abd282005-06-22 22:35:10 +00004906 char_attr = hl_combine_attr(char_attr, spell_attr);
4907 else
4908 char_attr = hl_combine_attr(spell_attr, char_attr);
4909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#endif
4911#ifdef FEAT_LINEBREAK
4912 /*
Bram Moolenaar217ad922005-03-20 22:37:15 +00004913 * Found last space before word: check for line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaar38632fa2017-02-26 19:40:59 +01004915 if (wp->w_p_lbr && c0 == c
Bram Moolenaar977d0372017-03-12 21:31:58 +01004916 && VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaar4df70292015-03-21 14:20:16 +01004918 int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1) : 0;
Bram Moolenaara12a1612019-01-24 16:39:02 +01004919 char_u *p = ptr - (mb_off + 1);
Bram Moolenaar76feaf12015-03-20 15:58:52 +01004920
Bram Moolenaar597a4222014-06-25 14:39:50 +02004921 /* TODO: is passing p for start of the line OK? */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004922 n_extra = win_lbr_chartabsize(wp, line, p, (colnr_T)vcol,
Bram Moolenaar597a4222014-06-25 14:39:50 +02004923 NULL) - 1;
Bram Moolenaar02631462017-09-22 15:20:32 +02004924 if (c == TAB && n_extra + col > wp->w_width)
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004925# ifdef FEAT_VARTABS
Bram Moolenaara87b72c2018-06-25 21:24:51 +02004926 n_extra = tabstop_padding(vcol, wp->w_buffer->b_p_ts,
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004927 wp->w_buffer->b_p_vts_array) - 1;
4928# else
Bram Moolenaara3650912014-11-19 13:21:57 +01004929 n_extra = (int)wp->w_buffer->b_p_ts
4930 - vcol % (int)wp->w_buffer->b_p_ts - 1;
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02004931# endif
Bram Moolenaara3650912014-11-19 13:21:57 +01004932
Bram Moolenaar4df70292015-03-21 14:20:16 +01004933 c_extra = mb_off > 0 ? MB_FILLER_CHAR : ' ';
Bram Moolenaar83a52172019-01-16 22:41:54 +01004934 c_final = NUL;
Bram Moolenaar1c465442017-03-12 20:10:05 +01004935 if (VIM_ISWHITE(c))
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004936 {
4937#ifdef FEAT_CONCEAL
4938 if (c == TAB)
4939 /* See "Tab alignment" below. */
4940 FIX_FOR_BOGUSCOLS;
4941#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02004942 if (!wp->w_p_list)
4943 c = ' ';
Bram Moolenaar3ff9b182013-07-13 12:36:55 +02004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946#endif
4947
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004948 // 'list': Change char 160 to lcs_nbsp and space to lcs_space.
4949 // But not when the character is followed by a composing
4950 // character (use mb_l to check that).
4951 if (wp->w_p_list
4952 && ((((c == 160 && mb_l == 1)
4953 || (mb_utf8
4954 && ((mb_c == 160 && mb_l == 2)
4955 || (mb_c == 0x202f && mb_l == 3))))
4956 && lcs_nbsp)
4957 || (c == ' '
4958 && mb_l == 1
4959 && lcs_space
4960 && ptr - line <= trailcol)))
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004961 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004962 c = (c == ' ') ? lcs_space : lcs_nbsp;
4963 if (area_attr == 0 && search_attr == 0)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004964 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004965 n_attr = 1;
4966 extra_attr = HL_ATTR(HLF_8);
4967 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004968 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004969 mb_c = c;
4970 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004971 {
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004972 mb_utf8 = TRUE;
4973 u8cc[0] = 0;
4974 c = 0xc0;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004975 }
Bram Moolenaare5e4e222019-04-04 13:28:45 +02004976 else
4977 mb_utf8 = FALSE;
Bram Moolenaar9bc01eb2015-12-17 21:14:58 +01004978 }
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (trailcol != MAXCOL && ptr > line + trailcol && c == ' ')
4981 {
4982 c = lcs_trail;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004983 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
4985 n_attr = 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01004986 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 saved_attr2 = char_attr; /* save current attr */
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02004990 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004993 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004994 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 else
4997 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999 }
5000
5001 /*
5002 * Handling of non-printable characters.
5003 */
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01005004 if (!vim_isprintc(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 /*
5007 * when getting a character from the file, we may have to
5008 * turn it into something else on the way to putting it
5009 * into "ScreenLines".
5010 */
5011 if (c == TAB && (!wp->w_p_list || lcs_tab1))
5012 {
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005013 int tab_len = 0;
Bram Moolenaard574ea22015-01-14 19:35:14 +01005014 long vcol_adjusted = vcol; /* removed showbreak length */
5015#ifdef FEAT_LINEBREAK
5016 /* only adjust the tab_len, when at the first column
5017 * after the showbreak value was drawn */
5018 if (*p_sbr != NUL && vcol == vcol_sbr && wp->w_p_wrap)
5019 vcol_adjusted = vcol - MB_CHARLEN(p_sbr);
5020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 /* tab amount depends on current column */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005022#ifdef FEAT_VARTABS
5023 tab_len = tabstop_padding(vcol_adjusted,
5024 wp->w_buffer->b_p_ts,
5025 wp->w_buffer->b_p_vts_array) - 1;
5026#else
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005027 tab_len = (int)wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005028 - vcol_adjusted % (int)wp->w_buffer->b_p_ts - 1;
5029#endif
Bram Moolenaard574ea22015-01-14 19:35:14 +01005030
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005031#ifdef FEAT_LINEBREAK
Bram Moolenaarb81c85d2014-07-30 16:44:22 +02005032 if (!wp->w_p_lbr || !wp->w_p_list)
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005033#endif
5034 /* tab amount depends on current column */
5035 n_extra = tab_len;
5036#ifdef FEAT_LINEBREAK
5037 else
5038 {
5039 char_u *p;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005040 int len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005041 int i;
5042 int saved_nextra = n_extra;
5043
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005044#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005045 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005046 /* there are characters to conceal */
5047 tab_len += vcol_off;
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005048 /* boguscols before FIX_FOR_BOGUSCOLS macro from above
5049 */
5050 if (wp->w_p_list && lcs_tab1 && old_boguscols > 0
5051 && n_extra > tab_len)
5052 tab_len += n_extra - tab_len;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005053#endif
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005054
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005055 /* if n_extra > 0, it gives the number of chars, to
5056 * use for a tab, else we need to calculate the width
5057 * for a tab */
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005058 len = (tab_len * mb_char2len(lcs_tab2));
5059 if (n_extra > 0)
5060 len += n_extra - tab_len;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005061 c = lcs_tab1;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005062 p = alloc(len + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005063 vim_memset(p, ' ', len);
5064 p[len] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005065 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005066 p_extra_free = p;
5067 for (i = 0; i < tab_len; i++)
5068 {
Bram Moolenaar307ac5c2018-06-28 22:23:00 +02005069 if (*p == NUL)
5070 {
5071 tab_len = i;
5072 break;
5073 }
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005074 mb_char2bytes(lcs_tab2, p);
5075 p += mb_char2len(lcs_tab2);
5076 n_extra += mb_char2len(lcs_tab2)
5077 - (saved_nextra > 0 ? 1 : 0);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005078 }
5079 p_extra = p_extra_free;
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005080#ifdef FEAT_CONCEAL
5081 /* n_extra will be increased by FIX_FOX_BOGUSCOLS
5082 * macro below, so need to adjust for that here */
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005083 if (vcol_off > 0)
Bram Moolenaar49f9dd72014-08-29 12:08:43 +02005084 n_extra -= vcol_off;
5085#endif
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005086 }
5087#endif
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005088#ifdef FEAT_CONCEAL
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005089 {
5090 int vc_saved = vcol_off;
5091
5092 /* Tab alignment should be identical regardless of
5093 * 'conceallevel' value. So tab compensates of all
5094 * previous concealed characters, and thus resets
5095 * vcol_off and boguscols accumulated so far in the
5096 * line. Note that the tab can be longer than
5097 * 'tabstop' when there are concealed characters. */
5098 FIX_FOR_BOGUSCOLS;
5099
5100 /* Make sure, the highlighting for the tab char will be
5101 * correctly set further below (effectively reverts the
5102 * FIX_FOR_BOGSUCOLS macro */
5103 if (n_extra == tab_len + vc_saved && wp->w_p_list
Bram Moolenaar6a6028c2015-01-20 19:01:35 +01005104 && lcs_tab1)
Bram Moolenaar8fc6bc72015-02-17 17:26:10 +01005105 tab_len += vc_saved;
5106 }
Bram Moolenaar0f9d0862012-12-05 15:32:30 +01005107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (wp->w_p_list)
5110 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005111 c = (n_extra == 0 && lcs_tab3) ? lcs_tab3 : lcs_tab1;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005112#ifdef FEAT_LINEBREAK
5113 if (wp->w_p_lbr)
5114 c_extra = NUL; /* using p_extra from above */
5115 else
5116#endif
5117 c_extra = lcs_tab2;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005118 c_final = lcs_tab3;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005119 n_attr = tab_len + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005120 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 saved_attr2 = char_attr; /* save current attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005123 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005126 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005127 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 else
5131 {
Bram Moolenaar83a52172019-01-16 22:41:54 +01005132 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 c_extra = ' ';
5134 c = ' ';
5135 }
5136 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005137 else if (c == NUL
Bram Moolenaard59c0992015-05-04 16:52:01 +02005138 && (wp->w_p_list
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005139 || ((fromcol >= 0 || fromcol_prev >= 0)
5140 && tocol > vcol
5141 && VIsual_mode != Ctrl_V
5142 && (
5143# ifdef FEAT_RIGHTLEFT
5144 wp->w_p_rl ? (col >= 0) :
5145# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005146 (col < wp->w_width))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005147 && !(noinvcur
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005148 && lnum == wp->w_cursor.lnum
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005149 && (colnr_T)vcol == wp->w_virtcol)))
Bram Moolenaar0481fee2015-05-14 05:56:09 +02005150 && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005152 /* Display a '$' after the line or highlight an extra
5153 * character if the line break is included. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#if defined(FEAT_DIFF) || defined(LINE_ATTR)
5155 /* For a diff line the highlighting continues after the
5156 * "$". */
5157 if (
5158# ifdef FEAT_DIFF
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005159 diff_hlf == (hlf_T)0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160# ifdef LINE_ATTR
5161 &&
5162# endif
5163# endif
5164# ifdef LINE_ATTR
5165 line_attr == 0
5166# endif
5167 )
5168#endif
5169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 /* In virtualedit, visual selections may extend
5171 * beyond end of line. */
5172 if (area_highlighting && virtual_active()
5173 && tocol != MAXCOL && vcol < tocol)
5174 n_extra = 0;
5175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
5177 p_extra = at_end_str;
5178 n_extra = 1;
5179 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005180 c_final = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182 }
Bram Moolenaard59c0992015-05-04 16:52:01 +02005183 if (wp->w_p_list && lcs_eol > 0)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005184 c = lcs_eol;
5185 else
5186 c = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 lcs_eol_one = -1;
5188 --ptr; /* put it back at the NUL */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005189 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005191 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 n_attr = 1;
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005195 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005198 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005199 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 else
5202 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204 else if (c != NUL)
5205 {
5206 p_extra = transchar(c);
Bram Moolenaar5524aeb2014-07-16 17:29:51 +02005207 if (n_extra == 0)
5208 n_extra = byte2cells(c) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#ifdef FEAT_RIGHTLEFT
5210 if ((dy_flags & DY_UHEX) && wp->w_p_rl)
5211 rl_mirror(p_extra); /* reverse "<12>" */
5212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 c_extra = NUL;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005214 c_final = NUL;
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005215#ifdef FEAT_LINEBREAK
5216 if (wp->w_p_lbr)
5217 {
5218 char_u *p;
5219
5220 c = *p_extra;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005221 p = alloc(n_extra + 1);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005222 vim_memset(p, ' ', n_extra);
5223 STRNCPY(p, p_extra + 1, STRLEN(p_extra) - 1);
5224 p[n_extra] = NUL;
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01005225 vim_free(p_extra_free);
Bram Moolenaar86b17e92014-07-02 20:00:47 +02005226 p_extra_free = p_extra = p;
5227 }
5228 else
5229#endif
5230 {
5231 n_extra = byte2cells(c) - 1;
5232 c = *p_extra++;
5233 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005234 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 n_attr = n_extra + 1;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005237 extra_attr = HL_ATTR(HLF_8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 saved_attr2 = char_attr; /* save current attr */
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 else if (VIsual_active
5243 && (VIsual_mode == Ctrl_V
5244 || VIsual_mode == 'v')
5245 && virtual_active()
5246 && tocol != MAXCOL
5247 && vcol < tocol
5248 && (
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005249#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 wp->w_p_rl ? (col >= 0) :
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005251#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005252 (col < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 c = ' ';
5255 --ptr; /* put it back at the NUL */
5256 }
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005257#if defined(LINE_ATTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 else if ((
5259# ifdef FEAT_DIFF
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005260 diff_hlf != (hlf_T)0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005262# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005263 win_attr != 0 ||
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 line_attr != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ) && (
5267# ifdef FEAT_RIGHTLEFT
5268 wp->w_p_rl ? (col >= 0) :
5269# endif
Bram Moolenaar2a988a12010-08-13 15:24:39 +02005270 (col
5271# ifdef FEAT_CONCEAL
5272 - boguscols
5273# endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005274 < wp->w_width)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 /* Highlight until the right side of the window */
5277 c = ' ';
5278 --ptr; /* put it back at the NUL */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005279
5280 /* Remember we do the char for line highlighting. */
5281 ++did_line_attr;
5282
5283 /* don't do search HL for the rest of the line */
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005284 if (line_attr != 0 && char_attr == search_attr
5285 && (did_line_attr > 1
5286 || (wp->w_p_list && lcs_eol > 0)))
Bram Moolenaar91170f82006-05-05 21:15:17 +00005287 char_attr = line_attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288# ifdef FEAT_DIFF
5289 if (diff_hlf == HLF_TXD)
5290 {
5291 diff_hlf = HLF_CHD;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005292 if (vi_attr == 0 || char_attr != vi_attr)
Bram Moolenaare0f14822014-08-06 13:20:56 +02005293 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01005294 char_attr = HL_ATTR(diff_hlf);
Bram Moolenaare0f14822014-08-06 13:20:56 +02005295 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5296 char_attr = hl_combine_attr(char_attr,
Bram Moolenaar8820b482017-03-16 17:23:31 +01005297 HL_ATTR(HLF_CUL));
Bram Moolenaare0f14822014-08-06 13:20:56 +02005298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300# endif
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005301# ifdef FEAT_TERMINAL
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005302 if (win_attr != 0)
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005303 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005304 char_attr = win_attr;
Bram Moolenaar238d43b2017-09-11 22:00:51 +02005305 if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
5306 char_attr = hl_combine_attr(char_attr,
5307 HL_ATTR(HLF_CUL));
5308 }
5309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311#endif
5312 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005313
5314#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005315 if ( wp->w_p_cole > 0
5316 && (wp != curwin || lnum != wp->w_cursor.lnum ||
Bram Moolenaarab62c192019-03-30 16:39:05 +01005317 conceal_cursor_line(wp))
5318 && ((syntax_flags & HL_CONCEAL) != 0 || has_match_conc > 0)
Bram Moolenaare6dc5732010-07-24 23:52:26 +02005319 && !(lnum_in_visual_area
5320 && vim_strchr(wp->w_p_cocu, 'v') == NULL))
Bram Moolenaar860cae12010-06-05 23:22:07 +02005321 {
5322 char_attr = conceal_attr;
Bram Moolenaar4d585022016-04-14 19:50:22 +02005323 if ((prev_syntax_id != syntax_seqnr || has_match_conc > 1)
Bram Moolenaar6561d522015-07-21 15:48:27 +02005324 && (syn_get_sub_char() != NUL || match_conc
5325 || wp->w_p_cole == 1)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005326 && wp->w_p_cole != 3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005327 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005328 /* First time at this concealed item: display one
5329 * character. */
Bram Moolenaar6561d522015-07-21 15:48:27 +02005330 if (match_conc)
5331 c = match_conc;
5332 else if (syn_get_sub_char() != NUL)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005333 c = syn_get_sub_char();
5334 else if (lcs_conceal != NUL)
5335 c = lcs_conceal;
5336 else
5337 c = ' ';
5338
Bram Moolenaarffbbcb52010-07-24 17:29:03 +02005339 prev_syntax_id = syntax_seqnr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005340
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005341 if (n_extra > 0)
5342 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005343 vcol += n_extra;
5344 if (wp->w_p_wrap && n_extra > 0)
5345 {
5346# ifdef FEAT_RIGHTLEFT
5347 if (wp->w_p_rl)
5348 {
5349 col -= n_extra;
5350 boguscols -= n_extra;
5351 }
5352 else
5353# endif
5354 {
5355 boguscols += n_extra;
5356 col += n_extra;
5357 }
5358 }
5359 n_extra = 0;
5360 n_attr = 0;
5361 }
5362 else if (n_skip == 0)
5363 {
5364 is_concealing = TRUE;
5365 n_skip = 1;
5366 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005367 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005368 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005369 {
5370 mb_utf8 = TRUE;
5371 u8cc[0] = 0;
5372 c = 0xc0;
5373 }
5374 else
5375 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005376 }
5377 else
5378 {
Bram Moolenaar27c735b2010-07-22 22:16:29 +02005379 prev_syntax_id = 0;
Bram Moolenaarc400cb92010-07-19 19:52:13 +02005380 is_concealing = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005381 }
Bram Moolenaar0ebe12b2019-05-17 12:31:44 +02005382
5383 if (n_skip > 0 && did_decrement_ptr)
5384 // not showing the '>', put pointer back to avoid getting stuck
5385 ++ptr;
5386
5387#endif // FEAT_CONCEAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005390#ifdef FEAT_CONCEAL
5391 /* In the cursor line and we may be concealing characters: correct
5392 * the cursor column when we reach its position. */
Bram Moolenaarf691b842010-07-24 13:31:09 +02005393 if (!did_wcol && draw_state == WL_LINE
5394 && wp == curwin && lnum == wp->w_cursor.lnum
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005395 && conceal_cursor_line(wp)
5396 && (int)wp->w_virtcol <= vcol + n_skip)
5397 {
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005398# ifdef FEAT_RIGHTLEFT
5399 if (wp->w_p_rl)
Bram Moolenaar02631462017-09-22 15:20:32 +02005400 wp->w_wcol = wp->w_width - col + boguscols - 1;
Bram Moolenaare39b3d92016-01-15 22:52:22 +01005401 else
5402# endif
5403 wp->w_wcol = col - boguscols;
Bram Moolenaar72ada0f2010-07-24 17:39:52 +02005404 wp->w_wrow = row;
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005405 did_wcol = TRUE;
5406 }
5407#endif
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 /* Don't override visual selection highlighting. */
5410 if (n_attr > 0
5411 && draw_state == WL_LINE
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005412 && !attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 char_attr = extra_attr;
5414
Bram Moolenaar81695252004-12-29 20:58:21 +00005415#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 /* XIM don't send preedit_start and preedit_end, but they send
5417 * preedit_changed and commit. Thus Vim can't set "im_is_active", use
5418 * im_is_preediting() here. */
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02005419 if (p_imst == IM_ON_THE_SPOT
5420 && xic != NULL
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005421 && lnum == wp->w_cursor.lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 && (State & INSERT)
5423 && !p_imdisable
5424 && im_is_preediting()
5425 && draw_state == WL_LINE)
5426 {
5427 colnr_T tcol;
5428
5429 if (preedit_end_col == MAXCOL)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005430 getvcol(curwin, &(wp->w_cursor), &tcol, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 else
5432 tcol = preedit_end_col;
5433 if ((long)preedit_start_col <= vcol && vcol < (long)tcol)
5434 {
5435 if (feedback_old_attr < 0)
5436 {
5437 feedback_col = 0;
5438 feedback_old_attr = char_attr;
5439 }
5440 char_attr = im_get_feedback_attr(feedback_col);
5441 if (char_attr < 0)
5442 char_attr = feedback_old_attr;
5443 feedback_col++;
5444 }
5445 else if (feedback_old_attr >= 0)
5446 {
5447 char_attr = feedback_old_attr;
5448 feedback_old_attr = -1;
5449 feedback_col = 0;
5450 }
5451 }
5452#endif
5453 /*
5454 * Handle the case where we are in column 0 but not on the first
5455 * character of the line and the user wants us to show us a
5456 * special character (via 'listchars' option "precedes:<char>".
5457 */
5458 if (lcs_prec_todo != NUL
Bram Moolenaar7425b932014-10-10 15:28:46 +02005459 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 && (wp->w_p_wrap ? wp->w_skipcol > 0 : wp->w_leftcol > 0)
5461#ifdef FEAT_DIFF
5462 && filler_todo <= 0
5463#endif
5464 && draw_state > WL_NR
5465 && c != NUL)
5466 {
5467 c = lcs_prec;
5468 lcs_prec_todo = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005469 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5470 {
5471 /* Double-width character being overwritten by the "precedes"
5472 * character, need to fill up half the character. */
5473 c_extra = MB_FILLER_CHAR;
Bram Moolenaar83a52172019-01-16 22:41:54 +01005474 c_final = NUL;
Bram Moolenaar5641f382012-06-13 18:06:36 +02005475 n_extra = 1;
5476 n_attr = 2;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005477 extra_attr = HL_ATTR(HLF_AT);
Bram Moolenaar5641f382012-06-13 18:06:36 +02005478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005480 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005483 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005484 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486 else
5487 mb_utf8 = FALSE; /* don't draw as UTF-8 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005488 if (!attr_pri)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
5490 saved_attr3 = char_attr; /* save current attr */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005491 char_attr = HL_ATTR(HLF_AT); /* later copied to char_attr */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 n_attr3 = 1;
5493 }
5494 }
5495
5496 /*
Bram Moolenaar91170f82006-05-05 21:15:17 +00005497 * At end of the text line or just after the last character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499 if (c == NUL
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005500#if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005501 || did_line_attr == 1
5502#endif
5503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
Bram Moolenaar91170f82006-05-05 21:15:17 +00005505#ifdef FEAT_SEARCH_EXTRA
5506 long prevcol = (long)(ptr - line) - (c == NUL);
Bram Moolenaara443af82007-11-08 13:51:42 +00005507
5508 /* we're not really at that column when skipping some text */
Bram Moolenaar33741a02007-11-08 20:24:19 +00005509 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
Bram Moolenaara443af82007-11-08 13:51:42 +00005510 ++prevcol;
Bram Moolenaar91170f82006-05-05 21:15:17 +00005511#endif
5512
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005513 /* Invert at least one char, used for Visual and empty line or
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 * highlight match at end of line. If it's beyond the last
5515 * char on the screen, just overwrite that one (tricky!) Not
5516 * needed when a '$' was displayed for 'list'. */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005517#ifdef FEAT_SEARCH_EXTRA
5518 prevcol_hl_flag = FALSE;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005519 if (!search_hl.is_addpos && prevcol == (long)search_hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005520 prevcol_hl_flag = TRUE;
5521 else
5522 {
5523 cur = wp->w_match_head;
5524 while (cur != NULL)
5525 {
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005526 if (!cur->hl.is_addpos && prevcol == (long)cur->hl.startcol)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005527 {
5528 prevcol_hl_flag = TRUE;
5529 break;
5530 }
5531 cur = cur->next;
5532 }
5533 }
5534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 if (lcs_eol == lcs_eol_one
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005536 && ((area_attr != 0 && vcol == fromcol
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005537 && (VIsual_mode != Ctrl_V
5538 || lnum == VIsual.lnum
5539 || lnum == curwin->w_cursor.lnum)
Bram Moolenaarf3205d12009-03-18 18:09:03 +00005540 && c == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_SEARCH_EXTRA
5542 /* highlight 'hlsearch' match at end of line */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005543 || (prevcol_hl_flag == TRUE
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005544# ifdef FEAT_SYN_HL
5545 && !(wp->w_p_cul && lnum == wp->w_cursor.lnum
5546 && !(wp == curwin && VIsual_active))
5547# endif
5548# ifdef FEAT_DIFF
5549 && diff_hlf == (hlf_T)0
5550# endif
Bram Moolenaar6c60ea22006-07-11 20:36:45 +00005551# if defined(LINE_ATTR)
Bram Moolenaar91170f82006-05-05 21:15:17 +00005552 && did_line_attr <= 1
5553# endif
5554 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#endif
5556 ))
5557 {
5558 int n = 0;
5559
5560#ifdef FEAT_RIGHTLEFT
5561 if (wp->w_p_rl)
5562 {
5563 if (col < 0)
5564 n = 1;
5565 }
5566 else
5567#endif
5568 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005569 if (col >= wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 n = -1;
5571 }
5572 if (n != 0)
5573 {
5574 /* At the window boundary, highlight the last character
5575 * instead (better than nothing). */
5576 off += n;
5577 col += n;
5578 }
5579 else
5580 {
5581 /* Add a blank character to highlight. */
5582 ScreenLines[off] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (enc_utf8)
5584 ScreenLinesUC[off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
5586#ifdef FEAT_SEARCH_EXTRA
5587 if (area_attr == 0)
5588 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005589 /* Use attributes from match with highest priority among
5590 * 'search_hl' and the match list. */
5591 char_attr = search_hl.attr;
5592 cur = wp->w_match_head;
5593 shl_flag = FALSE;
5594 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005595 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005596 if (shl_flag == FALSE
5597 && ((cur != NULL
5598 && cur->priority > SEARCH_HL_PRIORITY)
5599 || cur == NULL))
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005600 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005601 shl = &search_hl;
5602 shl_flag = TRUE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005603 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005604 else
5605 shl = &cur->hl;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02005606 if ((ptr - line) - 1 == (long)shl->startcol
5607 && (shl == &search_hl || !shl->is_addpos))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005608 char_attr = shl->attr;
5609 if (shl != &search_hl && cur != NULL)
5610 cur = cur->next;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00005611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613#endif
5614 ScreenAttrs[off] = char_attr;
5615#ifdef FEAT_RIGHTLEFT
5616 if (wp->w_p_rl)
Bram Moolenaara443af82007-11-08 13:51:42 +00005617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 --col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005619 --off;
5620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 else
5622#endif
Bram Moolenaara443af82007-11-08 13:51:42 +00005623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 ++col;
Bram Moolenaara443af82007-11-08 13:51:42 +00005625 ++off;
5626 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005627 ++vcol;
Bram Moolenaara443af82007-11-08 13:51:42 +00005628#ifdef FEAT_SYN_HL
5629 eol_hl_off = 1;
5630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
Bram Moolenaar91170f82006-05-05 21:15:17 +00005632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaar91170f82006-05-05 21:15:17 +00005634 /*
5635 * At end of the text line.
5636 */
5637 if (c == NUL)
5638 {
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005639#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005640 /* Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. */
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00005641 if (wp->w_p_wrap)
5642 v = wp->w_skipcol;
5643 else
5644 v = wp->w_leftcol;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005645
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005646 /* check if line ends before left margin */
5647 if (vcol < v + col - win_col_off(wp))
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005648 vcol = v + col - win_col_off(wp);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005649#ifdef FEAT_CONCEAL
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01005650 // Get rid of the boguscols now, we want to draw until the right
5651 // edge for 'cursorcolumn'.
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005652 col -= boguscols;
5653 boguscols = 0;
5654#endif
Bram Moolenaar1a384422010-07-14 19:53:30 +02005655
5656 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005657 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005658
5659 if (((wp->w_p_cuc
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005660 && (int)wp->w_virtcol >= VCOL_HLC - eol_hl_off
5661 && (int)wp->w_virtcol <
Bram Moolenaar02631462017-09-22 15:20:32 +02005662 wp->w_width * (row - startrow + 1) + v
Bram Moolenaar1a384422010-07-14 19:53:30 +02005663 && lnum != wp->w_cursor.lnum)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005664 || draw_color_col
5665 || win_attr != 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005666# ifdef FEAT_RIGHTLEFT
5667 && !wp->w_p_rl
5668# endif
5669 )
5670 {
Bram Moolenaar1a384422010-07-14 19:53:30 +02005671 int rightmost_vcol = 0;
5672 int i;
5673
5674 if (wp->w_p_cuc)
5675 rightmost_vcol = wp->w_virtcol;
5676 if (draw_color_col)
5677 /* determine rightmost colorcolumn to possibly draw */
5678 for (i = 0; color_cols[i] >= 0; ++i)
5679 if (rightmost_vcol < color_cols[i])
5680 rightmost_vcol = color_cols[i];
5681
Bram Moolenaar02631462017-09-22 15:20:32 +02005682 while (col < wp->w_width)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005683 {
5684 ScreenLines[off] = ' ';
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005685 if (enc_utf8)
5686 ScreenLinesUC[off] = 0;
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005687 ++col;
Bram Moolenaar973bd472010-07-20 11:29:07 +02005688 if (draw_color_col)
5689 draw_color_col = advance_color_col(VCOL_HLC,
5690 &color_cols);
5691
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005692 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005693 ScreenAttrs[off++] = HL_ATTR(HLF_CUC);
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005694 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar8820b482017-03-16 17:23:31 +01005695 ScreenAttrs[off++] = HL_ATTR(HLF_MC);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005696 else
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005697 ScreenAttrs[off++] = win_attr;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005698
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005699 if (VCOL_HLC >= rightmost_vcol && win_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005700 break;
Bram Moolenaar1a384422010-07-14 19:53:30 +02005701
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005702 ++vcol;
5703 }
5704 }
5705#endif
5706
Bram Moolenaar53f81742017-09-22 14:35:51 +02005707 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005708 (int)wp->w_width, screen_line_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 row++;
5710
5711 /*
5712 * Update w_cline_height and w_cline_folded if the cursor line was
5713 * updated (saves a call to plines() later).
5714 */
5715 if (wp == curwin && lnum == curwin->w_cursor.lnum)
5716 {
5717 curwin->w_cline_row = startrow;
5718 curwin->w_cline_height = row - startrow;
5719#ifdef FEAT_FOLDING
5720 curwin->w_cline_folded = FALSE;
5721#endif
5722 curwin->w_valid |= (VALID_CHEIGHT|VALID_CROW);
5723 }
5724
5725 break;
5726 }
5727
Bram Moolenaara5c6a0b2019-05-08 20:20:46 +02005728 // Show "extends" character from 'listchars' if beyond the line end and
5729 // 'list' is set.
5730 if (lcs_ext != NUL
5731 && wp->w_p_list
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 && !wp->w_p_wrap
5733#ifdef FEAT_DIFF
5734 && filler_todo <= 0
5735#endif
5736 && (
5737#ifdef FEAT_RIGHTLEFT
5738 wp->w_p_rl ? col == 0 :
5739#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005740 col == wp->w_width - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 && (*ptr != NUL
Bram Moolenaar5bbc21d2008-03-09 13:30:56 +00005742 || (wp->w_p_list && lcs_eol_one > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 || (n_extra && (c_extra != NUL || *p_extra != NUL))))
5744 {
5745 c = lcs_ext;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005746 char_attr = HL_ATTR(HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 mb_c = c;
Bram Moolenaarace95982017-03-29 17:30:27 +02005748 if (enc_utf8 && utf_char2len(c) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
5750 mb_utf8 = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005751 u8cc[0] = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005752 c = 0xc0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754 else
5755 mb_utf8 = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005758#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02005759 /* advance to the next 'colorcolumn' */
5760 if (draw_color_col)
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005761 draw_color_col = advance_color_col(VCOL_HLC, &color_cols);
Bram Moolenaar1a384422010-07-14 19:53:30 +02005762
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005763 /* Highlight the cursor column if 'cursorcolumn' is set. But don't
Bram Moolenaar1a384422010-07-14 19:53:30 +02005764 * highlight the cursor position itself.
5765 * Also highlight the 'colorcolumn' if it is different than
5766 * 'cursorcolumn' */
5767 vcol_save_attr = -1;
Bram Moolenaar774e5a92017-06-25 18:03:37 +02005768 if (draw_state == WL_LINE && !lnum_in_visual_area
5769 && search_attr == 0 && area_attr == 0)
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005770 {
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005771 if (wp->w_p_cuc && VCOL_HLC == (long)wp->w_virtcol
Bram Moolenaar1a384422010-07-14 19:53:30 +02005772 && lnum != wp->w_cursor.lnum)
5773 {
5774 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005775 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_CUC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005776 }
Bram Moolenaard160c342010-07-18 23:30:34 +02005777 else if (draw_color_col && VCOL_HLC == *color_cols)
Bram Moolenaar1a384422010-07-14 19:53:30 +02005778 {
5779 vcol_save_attr = char_attr;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005780 char_attr = hl_combine_attr(char_attr, HL_ATTR(HLF_MC));
Bram Moolenaar1a384422010-07-14 19:53:30 +02005781 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005782 }
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005783#endif
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 /*
5786 * Store character to be displayed.
5787 * Skip characters that are left of the screen for 'nowrap'.
5788 */
5789 vcol_prev = vcol;
5790 if (draw_state < WL_LINE || n_skip <= 0)
5791 {
5792 /*
5793 * Store the character.
5794 */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005795#if defined(FEAT_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (has_mbyte && wp->w_p_rl && (*mb_char2cells)(mb_c) > 1)
5797 {
5798 /* A double-wide character is: put first halve in left cell. */
5799 --off;
5800 --col;
5801 }
5802#endif
5803 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar990bb662010-02-03 15:48:04 +01005805 {
5806 if ((mb_c & 0xff00) == 0x8e00)
5807 ScreenLines[off] = 0x8e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 ScreenLines2[off] = mb_c & 0xff;
Bram Moolenaar990bb662010-02-03 15:48:04 +01005809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 else if (enc_utf8)
5811 {
5812 if (mb_utf8)
5813 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00005814 int i;
5815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 ScreenLinesUC[off] = mb_c;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005817 if ((c & 0xff) == 0)
5818 ScreenLines[off] = 0x80; /* avoid storing zero */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005819 for (i = 0; i < Screen_mco; ++i)
5820 {
5821 ScreenLinesC[i][off] = u8cc[i];
5822 if (u8cc[i] == 0)
5823 break;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826 else
5827 ScreenLinesUC[off] = 0;
5828 }
5829 if (multi_attr)
5830 {
5831 ScreenAttrs[off] = multi_attr;
5832 multi_attr = 0;
5833 }
5834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 ScreenAttrs[off] = char_attr;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5838 {
5839 /* Need to fill two screen columns. */
5840 ++off;
5841 ++col;
5842 if (enc_utf8)
5843 /* UTF-8: Put a 0 in the second screen char. */
5844 ScreenLines[off] = 0;
5845 else
5846 /* DBCS: Put second byte in the second screen char. */
5847 ScreenLines[off] = mb_c & 0xff;
Bram Moolenaar32a214e2015-12-03 14:29:02 +01005848 if (draw_state > WL_NR
5849#ifdef FEAT_DIFF
5850 && filler_todo <= 0
5851#endif
5852 )
5853 ++vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 /* When "tocol" is halfway a character, set it to the end of
5855 * the character, otherwise highlighting won't stop. */
5856 if (tocol == vcol)
5857 ++tocol;
5858#ifdef FEAT_RIGHTLEFT
5859 if (wp->w_p_rl)
5860 {
5861 /* now it's time to backup one cell */
5862 --off;
5863 --col;
5864 }
5865#endif
5866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867#ifdef FEAT_RIGHTLEFT
5868 if (wp->w_p_rl)
5869 {
5870 --off;
5871 --col;
5872 }
5873 else
5874#endif
5875 {
5876 ++off;
5877 ++col;
5878 }
5879 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005880#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005881 else if (wp->w_p_cole > 0 && is_concealing)
Bram Moolenaar860cae12010-06-05 23:22:07 +02005882 {
5883 --n_skip;
Bram Moolenaarac550fd2010-07-18 13:55:02 +02005884 ++vcol_off;
5885 if (n_extra > 0)
5886 vcol_off += n_extra;
Bram Moolenaar860cae12010-06-05 23:22:07 +02005887 if (wp->w_p_wrap)
5888 {
5889 /*
5890 * Special voodoo required if 'wrap' is on.
5891 *
5892 * Advance the column indicator to force the line
5893 * drawing to wrap early. This will make the line
5894 * take up the same screen space when parts are concealed,
5895 * so that cursor line computations aren't messed up.
5896 *
5897 * To avoid the fictitious advance of 'col' causing
5898 * trailing junk to be written out of the screen line
5899 * we are building, 'boguscols' keeps track of the number
5900 * of bad columns we have advanced.
5901 */
5902 if (n_extra > 0)
5903 {
5904 vcol += n_extra;
5905# ifdef FEAT_RIGHTLEFT
5906 if (wp->w_p_rl)
5907 {
5908 col -= n_extra;
5909 boguscols -= n_extra;
5910 }
5911 else
5912# endif
5913 {
5914 col += n_extra;
5915 boguscols += n_extra;
5916 }
5917 n_extra = 0;
5918 n_attr = 0;
5919 }
5920
5921
Bram Moolenaar860cae12010-06-05 23:22:07 +02005922 if (has_mbyte && (*mb_char2cells)(mb_c) > 1)
5923 {
5924 /* Need to fill two screen columns. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01005925# ifdef FEAT_RIGHTLEFT
Bram Moolenaar860cae12010-06-05 23:22:07 +02005926 if (wp->w_p_rl)
5927 {
5928 --boguscols;
5929 --col;
5930 }
5931 else
Bram Moolenaara12a1612019-01-24 16:39:02 +01005932# endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02005933 {
5934 ++boguscols;
5935 ++col;
5936 }
5937 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02005938
5939# ifdef FEAT_RIGHTLEFT
5940 if (wp->w_p_rl)
5941 {
5942 --boguscols;
5943 --col;
5944 }
5945 else
5946# endif
5947 {
5948 ++boguscols;
5949 ++col;
5950 }
5951 }
5952 else
5953 {
5954 if (n_extra > 0)
5955 {
5956 vcol += n_extra;
5957 n_extra = 0;
5958 n_attr = 0;
5959 }
5960 }
5961
5962 }
5963#endif /* FEAT_CONCEAL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 else
5965 --n_skip;
5966
Bram Moolenaar64486672010-05-16 15:46:46 +02005967 /* Only advance the "vcol" when after the 'number' or 'relativenumber'
5968 * column. */
Bram Moolenaar1b636fa2009-03-18 15:28:08 +00005969 if (draw_state > WL_NR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970#ifdef FEAT_DIFF
5971 && filler_todo <= 0
5972#endif
5973 )
5974 ++vcol;
5975
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005976#ifdef FEAT_SYN_HL
5977 if (vcol_save_attr >= 0)
5978 char_attr = vcol_save_attr;
5979#endif
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 /* restore attributes after "predeces" in 'listchars' */
5982 if (draw_state > WL_NR && n_attr3 > 0 && --n_attr3 == 0)
5983 char_attr = saved_attr3;
5984
5985 /* restore attributes after last 'listchars' or 'number' char */
5986 if (n_attr > 0 && draw_state == WL_LINE && --n_attr == 0)
5987 char_attr = saved_attr2;
5988
5989 /*
5990 * At end of screen line and there is more to come: Display the line
Bram Moolenaar367329b2007-08-30 11:53:22 +00005991 * so far. If there is no more to display it is caught above.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 */
5993 if ((
5994#ifdef FEAT_RIGHTLEFT
5995 wp->w_p_rl ? (col < 0) :
5996#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02005997 (col >= wp->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 && (*ptr != NUL
5999#ifdef FEAT_DIFF
6000 || filler_todo > 0
6001#endif
Bram Moolenaare9d4b582011-03-22 13:29:24 +01006002 || (wp->w_p_list && lcs_eol != NUL && p_extra != at_end_str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 || (n_extra != 0 && (c_extra != NUL || *p_extra != NUL)))
6004 )
6005 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006006#ifdef FEAT_CONCEAL
Bram Moolenaar53f81742017-09-22 14:35:51 +02006007 screen_line(screen_row, wp->w_wincol, col - boguscols,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006008 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006009 boguscols = 0;
6010#else
Bram Moolenaar53f81742017-09-22 14:35:51 +02006011 screen_line(screen_row, wp->w_wincol, col,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006012 (int)wp->w_width, screen_line_flags);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 ++row;
6015 ++screen_row;
6016
6017 /* When not wrapping and finished diff lines, or when displayed
6018 * '$' and highlighting until last column, break here. */
6019 if ((!wp->w_p_wrap
6020#ifdef FEAT_DIFF
6021 && filler_todo <= 0
6022#endif
6023 ) || lcs_eol_one == -1)
6024 break;
6025
6026 /* When the window is too narrow draw all "@" lines. */
6027 if (draw_state != WL_LINE
6028#ifdef FEAT_DIFF
6029 && filler_todo <= 0
6030#endif
6031 )
6032 {
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01006033 win_draw_end(wp, '@', ' ', TRUE, row, wp->w_height, HLF_AT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 draw_vsep_win(wp, row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 row = endrow;
6036 }
6037
6038 /* When line got too long for screen break here. */
6039 if (row == endrow)
6040 {
6041 ++row;
6042 break;
6043 }
6044
6045 if (screen_cur_row == screen_row - 1
6046#ifdef FEAT_DIFF
6047 && filler_todo <= 0
6048#endif
Bram Moolenaar02631462017-09-22 15:20:32 +02006049 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {
6051 /* Remember that the line wraps, used for modeless copy. */
6052 LineWraps[screen_row - 1] = TRUE;
6053
6054 /*
6055 * Special trick to make copy/paste of wrapped lines work with
6056 * xterm/screen: write an extra character beyond the end of
6057 * the line. This will work with all terminal types
6058 * (regardless of the xn,am settings).
6059 * Only do this on a fast tty.
6060 * Only do this if the cursor is on the current line
6061 * (something has been written in it).
6062 * Don't do this for the GUI.
6063 * Don't do this for double-width characters.
6064 * Don't do this for a window not at the right screen border.
6065 */
6066 if (p_tf
6067#ifdef FEAT_GUI
6068 && !gui.in_use
6069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 && !(has_mbyte
Bram Moolenaar367329b2007-08-30 11:53:22 +00006071 && ((*mb_off2cells)(LineOffset[screen_row],
6072 LineOffset[screen_row] + screen_Columns)
6073 == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 || (*mb_off2cells)(LineOffset[screen_row - 1]
Bram Moolenaar367329b2007-08-30 11:53:22 +00006075 + (int)Columns - 2,
6076 LineOffset[screen_row] + screen_Columns)
Bram Moolenaara12a1612019-01-24 16:39:02 +01006077 == 2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
6079 /* First make sure we are at the end of the screen line,
6080 * then output the same character again to let the
6081 * terminal know about the wrap. If the terminal doesn't
6082 * auto-wrap, we overwrite the character. */
Bram Moolenaar02631462017-09-22 15:20:32 +02006083 if (screen_cur_col != wp->w_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 screen_char(LineOffset[screen_row - 1]
6085 + (unsigned)Columns - 1,
6086 screen_row - 1, (int)(Columns - 1));
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 /* When there is a multi-byte character, just output a
6089 * space to keep it simple. */
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00006090 if (has_mbyte && MB_BYTE2LEN(ScreenLines[LineOffset[
6091 screen_row - 1] + (Columns - 1)]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 out_char(' ');
6093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 out_char(ScreenLines[LineOffset[screen_row - 1]
6095 + (Columns - 1)]);
6096 /* force a redraw of the first char on the next line */
6097 ScreenAttrs[LineOffset[screen_row]] = (sattr_T)-1;
6098 screen_start(); /* don't know where cursor is now */
6099 }
6100 }
6101
6102 col = 0;
6103 off = (unsigned)(current_ScreenLine - ScreenLines);
6104#ifdef FEAT_RIGHTLEFT
6105 if (wp->w_p_rl)
6106 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006107 col = wp->w_width - 1; /* col is not used if breaking! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 off += col;
6109 }
6110#endif
6111
6112 /* reset the drawing state for the start of a wrapped line */
6113 draw_state = WL_START;
6114 saved_n_extra = n_extra;
6115 saved_p_extra = p_extra;
6116 saved_c_extra = c_extra;
Bram Moolenaar83a52172019-01-16 22:41:54 +01006117 saved_c_final = c_final;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 saved_char_attr = char_attr;
6119 n_extra = 0;
6120 lcs_prec_todo = lcs_prec;
6121#ifdef FEAT_LINEBREAK
6122# ifdef FEAT_DIFF
6123 if (filler_todo <= 0)
6124# endif
6125 need_showbreak = TRUE;
6126#endif
6127#ifdef FEAT_DIFF
6128 --filler_todo;
6129 /* When the filler lines are actually below the last line of the
6130 * file, don't draw the line itself, break here. */
6131 if (filler_todo == 0 && wp->w_botfill)
6132 break;
6133#endif
6134 }
6135
6136 } /* for every character in the line */
6137
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006138#ifdef FEAT_SPELL
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006139 /* After an empty line check first word for capital. */
6140 if (*skipwhite(line) == NUL)
6141 {
6142 capcol_lnum = lnum + 1;
6143 cap_col = 0;
6144 }
6145#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006146#ifdef FEAT_TEXT_PROP
6147 vim_free(text_props);
6148 vim_free(text_prop_idxs);
6149#endif
Bram Moolenaar0d9c26d2005-07-02 23:19:16 +00006150
Bram Moolenaarb031c4e2017-01-24 20:14:48 +01006151 vim_free(p_extra_free);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 return row;
6153}
6154
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006155/*
6156 * Return if the composing characters at "off_from" and "off_to" differ.
Bram Moolenaar70c49c12010-03-23 15:36:35 +01006157 * Only to be used when ScreenLinesUC[off_from] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006158 */
6159 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006160comp_char_differs(int off_from, int off_to)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006161{
6162 int i;
6163
6164 for (i = 0; i < Screen_mco; ++i)
6165 {
6166 if (ScreenLinesC[i][off_from] != ScreenLinesC[i][off_to])
6167 return TRUE;
6168 if (ScreenLinesC[i][off_from] == 0)
6169 break;
6170 }
6171 return FALSE;
6172}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006173
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174/*
6175 * Check whether the given character needs redrawing:
6176 * - the (first byte of the) character is different
6177 * - the attributes are different
6178 * - the character is multi-byte and the next byte is different
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006179 * - the character is two cells wide and the second cell differs.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 */
6181 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006182char_needs_redraw(int off_from, int off_to, int cols)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 if (cols > 0
6185 && ((ScreenLines[off_from] != ScreenLines[off_to]
6186 || ScreenAttrs[off_from] != ScreenAttrs[off_to])
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 || (enc_dbcs != 0
6188 && MB_BYTE2LEN(ScreenLines[off_from]) > 1
6189 && (enc_dbcs == DBCS_JPNU && ScreenLines[off_from] == 0x8e
6190 ? ScreenLines2[off_from] != ScreenLines2[off_to]
6191 : (cols > 1 && ScreenLines[off_from + 1]
6192 != ScreenLines[off_to + 1])))
6193 || (enc_utf8
6194 && (ScreenLinesUC[off_from] != ScreenLinesUC[off_to]
6195 || (ScreenLinesUC[off_from] != 0
Bram Moolenaar88f3d3a2008-06-21 12:14:30 +00006196 && comp_char_differs(off_from, off_to))
Bram Moolenaar451cf632012-08-23 18:58:14 +02006197 || ((*mb_off2cells)(off_from, off_from + cols) > 1
6198 && ScreenLines[off_from + 1]
Bram Moolenaara12a1612019-01-24 16:39:02 +01006199 != ScreenLines[off_to + 1])))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 return TRUE;
6201 return FALSE;
6202}
6203
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006204#if defined(FEAT_TERMINAL) || defined(PROTO)
6205/*
6206 * Return the index in ScreenLines[] for the current screen line.
6207 */
6208 int
6209screen_get_current_line_off()
6210{
6211 return (int)(current_ScreenLine - ScreenLines);
6212}
6213#endif
6214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215/*
6216 * Move one "cooked" screen line to the screen, but only the characters that
6217 * have actually changed. Handle insert/delete character.
6218 * "coloff" gives the first column on the screen for this line.
6219 * "endcol" gives the columns where valid characters are.
6220 * "clear_width" is the width of the window. It's > 0 if the rest of the line
6221 * needs to be cleared, negative otherwise.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006222 * "flags" can have bits:
6223 * SLF_POPUP popup window
6224 * SLF_RIGHTLEFT rightleft window:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 * When TRUE and "clear_width" > 0, clear columns 0 to "endcol"
6226 * When FALSE and "clear_width" > 0, clear columns "endcol" to "clear_width"
6227 */
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006228 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006229screen_line(
6230 int row,
6231 int coloff,
6232 int endcol,
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006233 int clear_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006234 int flags UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 unsigned off_from;
6237 unsigned off_to;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006238 unsigned max_off_from;
6239 unsigned max_off_to;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 int col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 int hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 int force = FALSE; /* force update rest of the line */
6243 int redraw_this /* bool: does character need redraw? */
6244#ifdef FEAT_GUI
6245 = TRUE /* For GUI when while-loop empty */
6246#endif
6247 ;
6248 int redraw_next; /* redraw_this for next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 int clear_next = FALSE;
6250 int char_cells; /* 1: normal char */
6251 /* 2: occupies two display cells */
6252# define CHAR_CELLS char_cells
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01006254 /* Check for illegal row and col, just in case. */
6255 if (row >= Rows)
6256 row = Rows - 1;
6257 if (endcol > Columns)
6258 endcol = Columns;
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260# ifdef FEAT_CLIPBOARD
6261 clip_may_clear_selection(row, row);
6262# endif
6263
6264 off_from = (unsigned)(current_ScreenLine - ScreenLines);
6265 off_to = LineOffset[row] + coloff;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006266 max_off_from = off_from + screen_Columns;
6267 max_off_to = LineOffset[row] + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268
6269#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006270 if (flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 {
6272 /* Clear rest first, because it's left of the text. */
6273 if (clear_width > 0)
6274 {
6275 while (col <= endcol && ScreenLines[off_to] == ' '
6276 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006277 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
6279 ++off_to;
6280 ++col;
6281 }
6282 if (col <= endcol)
6283 screen_fill(row, row + 1, col + coloff,
6284 endcol + coloff + 1, ' ', ' ', 0);
6285 }
6286 col = endcol + 1;
6287 off_to = LineOffset[row] + col + coloff;
6288 off_from += col;
6289 endcol = (clear_width > 0 ? clear_width : -clear_width);
6290 }
6291#endif /* FEAT_RIGHTLEFT */
6292
6293 redraw_next = char_needs_redraw(off_from, off_to, endcol - col);
6294
6295 while (col < endcol)
6296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (has_mbyte && (col + 1 < endcol))
Bram Moolenaar367329b2007-08-30 11:53:22 +00006298 char_cells = (*mb_off2cells)(off_from, max_off_from);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 else
6300 char_cells = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
6302 redraw_this = redraw_next;
6303 redraw_next = force || char_needs_redraw(off_from + CHAR_CELLS,
6304 off_to + CHAR_CELLS, endcol - col - CHAR_CELLS);
6305
6306#ifdef FEAT_GUI
6307 /* If the next character was bold, then redraw the current character to
6308 * remove any pixels that might have spilt over into us. This only
6309 * happens in the GUI.
6310 */
6311 if (redraw_next && gui.in_use)
6312 {
6313 hl = ScreenAttrs[off_to + CHAR_CELLS];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006314 if (hl > HL_ALL)
6315 hl = syn_attr2attr(hl);
6316 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 redraw_this = TRUE;
6318 }
6319#endif
6320
6321 if (redraw_this)
6322 {
6323 /*
6324 * Special handling when 'xs' termcap flag set (hpterm):
6325 * Attributes for characters are stored at the position where the
6326 * cursor is when writing the highlighting code. The
6327 * start-highlighting code must be written with the cursor on the
6328 * first highlighted character. The stop-highlighting code must
6329 * be written with the cursor just after the last highlighted
6330 * character.
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01006331 * Overwriting a character doesn't remove its highlighting. Need
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 * to clear the rest of the line, and force redrawing it
6333 * completely.
6334 */
6335 if ( p_wiv
6336 && !force
6337#ifdef FEAT_GUI
6338 && !gui.in_use
6339#endif
6340 && ScreenAttrs[off_to] != 0
6341 && ScreenAttrs[off_from] != ScreenAttrs[off_to])
6342 {
6343 /*
6344 * Need to remove highlighting attributes here.
6345 */
6346 windgoto(row, col + coloff);
6347 out_str(T_CE); /* clear rest of this screen line */
6348 screen_start(); /* don't know where cursor is now */
6349 force = TRUE; /* force redraw of rest of the line */
6350 redraw_next = TRUE; /* or else next char would miss out */
6351
6352 /*
6353 * If the previous character was highlighted, need to stop
6354 * highlighting at this character.
6355 */
6356 if (col + coloff > 0 && ScreenAttrs[off_to - 1] != 0)
6357 {
6358 screen_attr = ScreenAttrs[off_to - 1];
6359 term_windgoto(row, col + coloff);
6360 screen_stop_highlight();
6361 }
6362 else
6363 screen_attr = 0; /* highlighting has stopped */
6364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (enc_dbcs != 0)
6366 {
6367 /* Check if overwriting a double-byte with a single-byte or
6368 * the other way around requires another character to be
6369 * redrawn. For UTF-8 this isn't needed, because comparing
6370 * ScreenLinesUC[] is sufficient. */
6371 if (char_cells == 1
6372 && col + 1 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006373 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
6375 /* Writing a single-cell character over a double-cell
6376 * character: need to redraw the next cell. */
6377 ScreenLines[off_to + 1] = 0;
6378 redraw_next = TRUE;
6379 }
6380 else if (char_cells == 2
6381 && col + 2 < endcol
Bram Moolenaar367329b2007-08-30 11:53:22 +00006382 && (*mb_off2cells)(off_to, max_off_to) == 1
6383 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
6385 /* Writing the second half of a double-cell character over
6386 * a double-cell character: need to redraw the second
6387 * cell. */
6388 ScreenLines[off_to + 2] = 0;
6389 redraw_next = TRUE;
6390 }
6391
6392 if (enc_dbcs == DBCS_JPNU)
6393 ScreenLines2[off_to] = ScreenLines2[off_from];
6394 }
6395 /* When writing a single-width character over a double-width
6396 * character and at the end of the redrawn text, need to clear out
6397 * the right halve of the old character.
6398 * Also required when writing the right halve of a double-width
6399 * char over the left halve of an existing one. */
6400 if (has_mbyte && col + char_cells == endcol
6401 && ((char_cells == 1
Bram Moolenaar367329b2007-08-30 11:53:22 +00006402 && (*mb_off2cells)(off_to, max_off_to) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 || (char_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00006404 && (*mb_off2cells)(off_to, max_off_to) == 1
6405 && (*mb_off2cells)(off_to + 1, max_off_to) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 clear_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407
6408 ScreenLines[off_to] = ScreenLines[off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (enc_utf8)
6410 {
6411 ScreenLinesUC[off_to] = ScreenLinesUC[off_from];
6412 if (ScreenLinesUC[off_from] != 0)
6413 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006414 int i;
6415
6416 for (i = 0; i < Screen_mco; ++i)
6417 ScreenLinesC[i][off_to] = ScreenLinesC[i][off_from];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419 }
6420 if (char_cells == 2)
6421 ScreenLines[off_to + 1] = ScreenLines[off_from + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
6423#if defined(FEAT_GUI) || defined(UNIX)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00006424 /* The bold trick makes a single column of pixels appear in the
6425 * next character. When a bold character is removed, the next
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 * character should be redrawn too. This happens for our own GUI
6427 * and for some xterms. */
6428 if (
6429# ifdef FEAT_GUI
6430 gui.in_use
6431# endif
6432# if defined(FEAT_GUI) && defined(UNIX)
6433 ||
6434# endif
6435# ifdef UNIX
6436 term_is_xterm
6437# endif
6438 )
6439 {
6440 hl = ScreenAttrs[off_to];
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006441 if (hl > HL_ALL)
6442 hl = syn_attr2attr(hl);
6443 if (hl & HL_BOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 redraw_next = TRUE;
6445 }
6446#endif
6447 ScreenAttrs[off_to] = ScreenAttrs[off_from];
Bram Moolenaara12a1612019-01-24 16:39:02 +01006448
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006449 /* For simplicity set the attributes of second half of a
6450 * double-wide character equal to the first half. */
6451 if (char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 ScreenAttrs[off_to + 1] = ScreenAttrs[off_from];
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006453
6454 if (enc_dbcs != 0 && char_cells == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 screen_char_2(off_to, row, col + coloff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 screen_char(off_to, row, col + coloff);
6458 }
6459 else if ( p_wiv
6460#ifdef FEAT_GUI
6461 && !gui.in_use
6462#endif
6463 && col + coloff > 0)
6464 {
6465 if (ScreenAttrs[off_to] == ScreenAttrs[off_to - 1])
6466 {
6467 /*
6468 * Don't output stop-highlight when moving the cursor, it will
6469 * stop the highlighting when it should continue.
6470 */
6471 screen_attr = 0;
6472 }
6473 else if (screen_attr != 0)
6474 screen_stop_highlight();
6475 }
6476
6477 off_to += CHAR_CELLS;
6478 off_from += CHAR_CELLS;
6479 col += CHAR_CELLS;
6480 }
6481
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (clear_next)
6483 {
6484 /* Clear the second half of a double-wide character of which the left
6485 * half was overwritten with a single-wide character. */
6486 ScreenLines[off_to] = ' ';
6487 if (enc_utf8)
6488 ScreenLinesUC[off_to] = 0;
6489 screen_char(off_to, row, col + coloff);
6490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 if (clear_width > 0
6493#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006494 && !(flags & SLF_RIGHTLEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495#endif
6496 )
6497 {
6498#ifdef FEAT_GUI
6499 int startCol = col;
6500#endif
6501
6502 /* blank out the rest of the line */
6503 while (col < clear_width && ScreenLines[off_to] == ' '
6504 && ScreenAttrs[off_to] == 0
Bram Moolenaara12a1612019-01-24 16:39:02 +01006505 && (!enc_utf8 || ScreenLinesUC[off_to] == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 {
6507 ++off_to;
6508 ++col;
6509 }
6510 if (col < clear_width)
6511 {
6512#ifdef FEAT_GUI
6513 /*
6514 * In the GUI, clearing the rest of the line may leave pixels
6515 * behind if the first character cleared was bold. Some bold
6516 * fonts spill over the left. In this case we redraw the previous
6517 * character too. If we didn't skip any blanks above, then we
6518 * only redraw if the character wasn't already redrawn anyway.
6519 */
Bram Moolenaar9c697322006-10-09 20:11:17 +00006520 if (gui.in_use && (col > startCol || !redraw_this))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
6522 hl = ScreenAttrs[off_to];
6523 if (hl > HL_ALL || (hl & HL_BOLD))
Bram Moolenaar9c697322006-10-09 20:11:17 +00006524 {
6525 int prev_cells = 1;
Bram Moolenaara12a1612019-01-24 16:39:02 +01006526
Bram Moolenaar9c697322006-10-09 20:11:17 +00006527 if (enc_utf8)
6528 /* for utf-8, ScreenLines[char_offset + 1] == 0 means
6529 * that its width is 2. */
6530 prev_cells = ScreenLines[off_to - 1] == 0 ? 2 : 1;
6531 else if (enc_dbcs != 0)
6532 {
6533 /* find previous character by counting from first
6534 * column and get its width. */
6535 unsigned off = LineOffset[row];
Bram Moolenaar367329b2007-08-30 11:53:22 +00006536 unsigned max_off = LineOffset[row] + screen_Columns;
Bram Moolenaar9c697322006-10-09 20:11:17 +00006537
6538 while (off < off_to)
6539 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00006540 prev_cells = (*mb_off2cells)(off, max_off);
Bram Moolenaar9c697322006-10-09 20:11:17 +00006541 off += prev_cells;
6542 }
6543 }
6544
6545 if (enc_dbcs != 0 && prev_cells > 1)
6546 screen_char_2(off_to - prev_cells, row,
6547 col + coloff - prev_cells);
6548 else
Bram Moolenaar9c697322006-10-09 20:11:17 +00006549 screen_char(off_to - prev_cells, row,
6550 col + coloff - prev_cells);
6551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 }
6553#endif
6554 screen_fill(row, row + 1, col + coloff, clear_width + coloff,
6555 ' ', ' ', 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 off_to += clear_width - col;
6557 col = clear_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559 }
6560
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006561 if (clear_width > 0
6562#ifdef FEAT_TEXT_PROP
6563 && !(flags & SLF_POPUP) // no separator for popup window
6564#endif
6565 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006567 // For a window that has a right neighbor, draw the separator char
6568 // right of the window contents.
6569 if (coloff + col < Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {
6571 int c;
6572
6573 c = fillchar_vsep(&hl);
Bram Moolenaarc60c4f62015-01-07 19:04:28 +01006574 if (ScreenLines[off_to] != (schar_T)c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006575 || (enc_utf8 && (int)ScreenLinesUC[off_to]
6576 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 || ScreenAttrs[off_to] != hl)
6578 {
6579 ScreenLines[off_to] = c;
6580 ScreenAttrs[off_to] = hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 if (enc_utf8)
6582 {
6583 if (c >= 0x80)
6584 {
6585 ScreenLinesUC[off_to] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006586 ScreenLinesC[0][off_to] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 }
6588 else
6589 ScreenLinesUC[off_to] = 0;
6590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 screen_char(off_to, row, col + coloff);
6592 }
6593 }
6594 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 LineWraps[row] = FALSE;
6596 }
6597}
6598
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006599#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006601 * Mirror text "str" for right-left displaying.
6602 * Only works for single-byte characters (e.g., numbers).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006604 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006605rl_mirror(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 char_u *p1, *p2;
6608 int t;
6609
6610 for (p1 = str, p2 = str + STRLEN(str) - 1; p1 < p2; ++p1, --p2)
6611 {
6612 t = *p1;
6613 *p1 = *p2;
6614 *p2 = t;
6615 }
6616}
6617#endif
6618
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619/*
6620 * mark all status lines for redraw; used after first :cd
6621 */
6622 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006623status_redraw_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 win_T *wp;
6626
Bram Moolenaar29323592016-07-24 22:04:11 +02006627 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 if (wp->w_status_height)
6629 {
6630 wp->w_redr_status = TRUE;
6631 redraw_later(VALID);
6632 }
6633}
6634
6635/*
6636 * mark all status lines of the current buffer for redraw
6637 */
6638 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006639status_redraw_curbuf(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 win_T *wp;
6642
Bram Moolenaar29323592016-07-24 22:04:11 +02006643 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (wp->w_status_height != 0 && wp->w_buffer == curbuf)
6645 {
6646 wp->w_redr_status = TRUE;
6647 redraw_later(VALID);
6648 }
6649}
6650
6651/*
6652 * Redraw all status lines that need to be redrawn.
6653 */
6654 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006655redraw_statuslines(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 win_T *wp;
6658
Bram Moolenaar29323592016-07-24 22:04:11 +02006659 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if (wp->w_redr_status)
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006661 win_redr_status(wp, FALSE);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00006662 if (redraw_tabline)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00006663 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
Bram Moolenaar4033c552017-09-16 20:54:51 +02006666#if defined(FEAT_WILDMENU) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667/*
6668 * Redraw all status lines at the bottom of frame "frp".
6669 */
6670 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006671win_redraw_last_status(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 if (frp->fr_layout == FR_LEAF)
6674 frp->fr_win->w_redr_status = TRUE;
6675 else if (frp->fr_layout == FR_ROW)
6676 {
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006677 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 win_redraw_last_status(frp);
6679 }
6680 else /* frp->fr_layout == FR_COL */
6681 {
6682 frp = frp->fr_child;
6683 while (frp->fr_next != NULL)
6684 frp = frp->fr_next;
6685 win_redraw_last_status(frp);
6686 }
6687}
6688#endif
6689
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690/*
6691 * Draw the verticap separator right of window "wp" starting with line "row".
6692 */
6693 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006694draw_vsep_win(win_T *wp, int row)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
6696 int hl;
6697 int c;
6698
6699 if (wp->w_vsep_width)
6700 {
6701 /* draw the vertical separator right of this window */
6702 c = fillchar_vsep(&hl);
6703 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
6704 W_ENDCOL(wp), W_ENDCOL(wp) + 1,
6705 c, ' ', hl);
6706 }
6707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709#ifdef FEAT_WILDMENU
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006710static int skip_status_match_char(expand_T *xp, char_u *s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712/*
Bram Moolenaar367329b2007-08-30 11:53:22 +00006713 * Get the length of an item as it will be shown in the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 */
6715 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006716status_match_len(expand_T *xp, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 int len = 0;
6719
6720#ifdef FEAT_MENU
6721 int emenu = (xp->xp_context == EXPAND_MENUS
6722 || xp->xp_context == EXPAND_MENUNAMES);
6723
6724 /* Check for menu separators - replace with '|'. */
6725 if (emenu && menu_is_separator(s))
6726 return 1;
6727#endif
6728
6729 while (*s != NUL)
6730 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006731 s += skip_status_match_char(xp, s);
Bram Moolenaar81695252004-12-29 20:58:21 +00006732 len += ptr2cells(s);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006733 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 }
6735
6736 return len;
6737}
6738
6739/*
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006740 * Return the number of characters that should be skipped in a status match.
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006741 * These are backslashes used for escaping. Do show backslashes in help tags.
6742 */
6743 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006744skip_status_match_char(expand_T *xp, char_u *s)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006745{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006746 if ((rem_backslash(s) && xp->xp_context != EXPAND_HELP)
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006747#ifdef FEAT_MENU
6748 || ((xp->xp_context == EXPAND_MENUS
6749 || xp->xp_context == EXPAND_MENUNAMES)
6750 && (s[0] == '\t' || (s[0] == '\\' && s[1] != NUL)))
6751#endif
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006752 )
6753 {
6754#ifndef BACKSLASH_IN_FILENAME
6755 if (xp->xp_shell && csh_like_shell() && s[1] == '\\' && s[2] == '!')
6756 return 2;
6757#endif
6758 return 1;
6759 }
6760 return 0;
Bram Moolenaar35c54e52005-05-20 21:25:31 +00006761}
6762
6763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 * Show wildchar matches in the status line.
6765 * Show at least the "match" item.
6766 * We start at item 'first_match' in the list and show all matches that fit.
6767 *
6768 * If inversion is possible we use it. Else '=' characters are used.
6769 */
6770 void
Bram Moolenaar05540972016-01-30 20:31:25 +01006771win_redr_status_matches(
6772 expand_T *xp,
6773 int num_matches,
6774 char_u **matches, /* list of matches */
6775 int match,
6776 int showtail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778#define L_MATCH(m) (showtail ? sm_gettail(matches[m]) : matches[m])
6779 int row;
6780 char_u *buf;
6781 int len;
Bram Moolenaar367329b2007-08-30 11:53:22 +00006782 int clen; /* length in screen cells */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 int fillchar;
6784 int attr;
6785 int i;
6786 int highlight = TRUE;
6787 char_u *selstart = NULL;
6788 int selstart_col = 0;
6789 char_u *selend = NULL;
6790 static int first_match = 0;
6791 int add_left = FALSE;
6792 char_u *s;
6793#ifdef FEAT_MENU
6794 int emenu;
6795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798 if (matches == NULL) /* interrupted completion? */
6799 return;
6800
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006801 if (has_mbyte)
Bram Moolenaar964b3742019-05-24 18:54:09 +02006802 buf = alloc(Columns * MB_MAXBYTES + 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006803 else
Bram Moolenaar964b3742019-05-24 18:54:09 +02006804 buf = alloc(Columns + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (buf == NULL)
6806 return;
6807
6808 if (match == -1) /* don't show match but original text */
6809 {
6810 match = 0;
6811 highlight = FALSE;
6812 }
6813 /* count 1 for the ending ">" */
6814 clen = status_match_len(xp, L_MATCH(match)) + 3;
6815 if (match == 0)
6816 first_match = 0;
6817 else if (match < first_match)
6818 {
6819 /* jumping left, as far as we can go */
6820 first_match = match;
6821 add_left = TRUE;
6822 }
6823 else
6824 {
6825 /* check if match fits on the screen */
6826 for (i = first_match; i < match; ++i)
6827 clen += status_match_len(xp, L_MATCH(i)) + 2;
6828 if (first_match > 0)
6829 clen += 2;
6830 /* jumping right, put match at the left */
6831 if ((long)clen > Columns)
6832 {
6833 first_match = match;
6834 /* if showing the last match, we can add some on the left */
6835 clen = 2;
6836 for (i = match; i < num_matches; ++i)
6837 {
6838 clen += status_match_len(xp, L_MATCH(i)) + 2;
6839 if ((long)clen >= Columns)
6840 break;
6841 }
6842 if (i == num_matches)
6843 add_left = TRUE;
6844 }
6845 }
6846 if (add_left)
6847 while (first_match > 0)
6848 {
6849 clen += status_match_len(xp, L_MATCH(first_match - 1)) + 2;
6850 if ((long)clen >= Columns)
6851 break;
6852 --first_match;
6853 }
6854
Bram Moolenaar3633cf52017-07-31 22:29:35 +02006855 fillchar = fillchar_status(&attr, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
6857 if (first_match == 0)
6858 {
6859 *buf = NUL;
6860 len = 0;
6861 }
6862 else
6863 {
6864 STRCPY(buf, "< ");
6865 len = 2;
6866 }
6867 clen = len;
6868
6869 i = first_match;
6870 while ((long)(clen + status_match_len(xp, L_MATCH(i)) + 2) < Columns)
6871 {
6872 if (i == match)
6873 {
6874 selstart = buf + len;
6875 selstart_col = clen;
6876 }
6877
6878 s = L_MATCH(i);
6879 /* Check for menu separators - replace with '|' */
6880#ifdef FEAT_MENU
6881 emenu = (xp->xp_context == EXPAND_MENUS
6882 || xp->xp_context == EXPAND_MENUNAMES);
6883 if (emenu && menu_is_separator(s))
6884 {
6885 STRCPY(buf + len, transchar('|'));
6886 l = (int)STRLEN(buf + len);
6887 len += l;
6888 clen += l;
6889 }
6890 else
6891#endif
6892 for ( ; *s != NUL; ++s)
6893 {
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006894 s += skip_status_match_char(xp, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 clen += ptr2cells(s);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006896 if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {
6898 STRNCPY(buf + len, s, l);
6899 s += l - 1;
6900 len += l;
6901 }
6902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 {
6904 STRCPY(buf + len, transchar_byte(*s));
6905 len += (int)STRLEN(buf + len);
6906 }
6907 }
6908 if (i == match)
6909 selend = buf + len;
6910
6911 *(buf + len++) = ' ';
6912 *(buf + len++) = ' ';
6913 clen += 2;
6914 if (++i == num_matches)
6915 break;
6916 }
6917
6918 if (i != num_matches)
6919 {
6920 *(buf + len++) = '>';
6921 ++clen;
6922 }
6923
6924 buf[len] = NUL;
6925
6926 row = cmdline_row - 1;
6927 if (row >= 0)
6928 {
6929 if (wild_menu_showing == 0)
6930 {
6931 if (msg_scrolled > 0)
6932 {
6933 /* Put the wildmenu just above the command line. If there is
6934 * no room, scroll the screen one line up. */
6935 if (cmdline_row == Rows - 1)
6936 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02006937 screen_del_lines(0, 0, 1, (int)Rows, TRUE, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 ++msg_scrolled;
6939 }
6940 else
6941 {
6942 ++cmdline_row;
6943 ++row;
6944 }
6945 wild_menu_showing = WM_SCROLLED;
6946 }
6947 else
6948 {
6949 /* Create status line if needed by setting 'laststatus' to 2.
6950 * Set 'winminheight' to zero to avoid that the window is
6951 * resized. */
6952 if (lastwin->w_status_height == 0)
6953 {
6954 save_p_ls = p_ls;
6955 save_p_wmh = p_wmh;
6956 p_ls = 2;
6957 p_wmh = 0;
6958 last_status(FALSE);
6959 }
6960 wild_menu_showing = WM_SHOWN;
6961 }
6962 }
6963
6964 screen_puts(buf, row, 0, attr);
6965 if (selstart != NULL && highlight)
6966 {
6967 *selend = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01006968 screen_puts(selstart, row, selstart_col, HL_ATTR(HLF_WM));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 }
6970
6971 screen_fill(row, row + 1, clen, (int)Columns, fillchar, fillchar, attr);
6972 }
6973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 vim_free(buf);
6976}
6977#endif
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979/*
6980 * Redraw the status line of window wp.
6981 *
6982 * If inversion is possible we use it. Else '=' characters are used.
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006983 * If "ignore_pum" is TRUE, also redraw statusline when the popup menu is
6984 * displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 */
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02006986 static void
Bram Moolenaar829adb72018-06-24 19:24:03 +02006987win_redr_status(win_T *wp, int ignore_pum UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 int row;
6990 char_u *p;
6991 int len;
6992 int fillchar;
6993 int attr;
6994 int this_ru_col;
Bram Moolenaaradb09c22009-06-16 15:22:12 +00006995 static int busy = FALSE;
6996
6997 /* It's possible to get here recursively when 'statusline' (indirectly)
6998 * invokes ":redrawstatus". Simply ignore the call then. */
6999 if (busy)
7000 return;
7001 busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003 wp->w_redr_status = FALSE;
7004 if (wp->w_status_height == 0)
7005 {
7006 /* no status line, can only be last window */
7007 redraw_cmdline = TRUE;
7008 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007009 else if (!redrawing()
7010#ifdef FEAT_INS_EXPAND
Bram Moolenaar6ba3ec12018-06-16 15:32:38 +02007011 // don't update status line when popup menu is visible and may be
7012 // drawn over it, unless it will be redrawn later
7013 || (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00007014#endif
7015 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
7017 /* Don't redraw right now, do it later. */
7018 wp->w_redr_status = TRUE;
7019 }
7020#ifdef FEAT_STL_OPT
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007021 else if (*p_stl != NUL || *wp->w_p_stl != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
7023 /* redraw custom status line */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007024 redraw_custom_statusline(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
7026#endif
7027 else
7028 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007029 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007031 get_trans_bufname(wp->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 p = NameBuff;
7033 len = (int)STRLEN(p);
7034
Bram Moolenaard85f2712017-07-28 21:51:57 +02007035 if (bt_help(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036#ifdef FEAT_QUICKFIX
7037 || wp->w_p_pvw
7038#endif
7039 || bufIsChanged(wp->w_buffer)
7040 || wp->w_buffer->b_p_ro)
7041 *(p + len++) = ' ';
Bram Moolenaard85f2712017-07-28 21:51:57 +02007042 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007044 STRCPY(p + len, _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 len += (int)STRLEN(p + len);
7046 }
7047#ifdef FEAT_QUICKFIX
7048 if (wp->w_p_pvw)
7049 {
7050 STRCPY(p + len, _("[Preview]"));
7051 len += (int)STRLEN(p + len);
7052 }
7053#endif
Bram Moolenaar086d5352017-08-05 18:19:55 +02007054 if (bufIsChanged(wp->w_buffer)
7055#ifdef FEAT_TERMINAL
7056 && !bt_terminal(wp->w_buffer)
7057#endif
7058 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060 STRCPY(p + len, "[+]");
7061 len += 3;
7062 }
7063 if (wp->w_buffer->b_p_ro)
7064 {
Bram Moolenaar23584032013-06-07 20:17:11 +02007065 STRCPY(p + len, _("[RO]"));
Bram Moolenaar3457d292017-02-23 14:55:59 +01007066 len += (int)STRLEN(p + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 }
7068
Bram Moolenaar02631462017-09-22 15:20:32 +02007069 this_ru_col = ru_col - (Columns - wp->w_width);
7070 if (this_ru_col < (wp->w_width + 1) / 2)
7071 this_ru_col = (wp->w_width + 1) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (this_ru_col <= 1)
7073 {
7074 p = (char_u *)"<"; /* No room for file name! */
7075 len = 1;
7076 }
Bram Moolenaara12a1612019-01-24 16:39:02 +01007077 else if (has_mbyte)
7078 {
7079 int clen = 0, i;
7080
7081 /* Count total number of display cells. */
7082 clen = mb_string2cells(p, -1);
7083
7084 /* Find first character that will fit.
7085 * Going from start to end is much faster for DBCS. */
7086 for (i = 0; p[i] != NUL && clen >= this_ru_col - 1;
7087 i += (*mb_ptr2len)(p + i))
7088 clen -= (*mb_ptr2cells)(p + i);
7089 len = clen;
7090 if (i > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 {
Bram Moolenaara12a1612019-01-24 16:39:02 +01007092 p = p + i - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 *p = '<';
Bram Moolenaara12a1612019-01-24 16:39:02 +01007094 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 }
7096
Bram Moolenaara12a1612019-01-24 16:39:02 +01007097 }
7098 else if (len > this_ru_col - 1)
7099 {
7100 p += len - (this_ru_col - 1);
7101 *p = '<';
7102 len = this_ru_col - 1;
7103 }
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar53f81742017-09-22 14:35:51 +02007106 screen_puts(p, row, wp->w_wincol, attr);
7107 screen_fill(row, row + 1, len + wp->w_wincol,
7108 this_ru_col + wp->w_wincol, fillchar, fillchar, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007110 if (get_keymap_str(wp, (char_u *)"<%s>", NameBuff, MAXPATHL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 && (int)(this_ru_col - len) > (int)(STRLEN(NameBuff) + 1))
7112 screen_puts(NameBuff, row, (int)(this_ru_col - STRLEN(NameBuff)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007113 - 1 + wp->w_wincol), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
7115#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +02007116 win_redr_ruler(wp, TRUE, ignore_pum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117#endif
7118 }
7119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 /*
7121 * May need to draw the character below the vertical separator.
7122 */
7123 if (wp->w_vsep_width != 0 && wp->w_status_height != 0 && redrawing())
7124 {
7125 if (stl_connected(wp))
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007126 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 else
7128 fillchar = fillchar_vsep(&attr);
7129 screen_putchar(fillchar, W_WINROW(wp) + wp->w_height, W_ENDCOL(wp),
7130 attr);
7131 }
Bram Moolenaaradb09c22009-06-16 15:22:12 +00007132 busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133}
7134
Bram Moolenaar238a5642006-02-21 22:12:05 +00007135#ifdef FEAT_STL_OPT
7136/*
7137 * Redraw the status line according to 'statusline' and take care of any
7138 * errors encountered.
7139 */
7140 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007141redraw_custom_statusline(win_T *wp)
Bram Moolenaar238a5642006-02-21 22:12:05 +00007142{
Bram Moolenaar362f3562009-11-03 16:20:34 +00007143 static int entered = FALSE;
Bram Moolenaara742e082016-04-05 21:10:38 +02007144 int saved_did_emsg = did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007145
7146 /* When called recursively return. This can happen when the statusline
7147 * contains an expression that triggers a redraw. */
7148 if (entered)
7149 return;
7150 entered = TRUE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007151
Bram Moolenaara742e082016-04-05 21:10:38 +02007152 did_emsg = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007153 win_redr_custom(wp, FALSE);
Bram Moolenaara742e082016-04-05 21:10:38 +02007154 if (did_emsg)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007155 {
7156 /* When there is an error disable the statusline, otherwise the
7157 * display is messed up with errors and a redraw triggers the problem
7158 * again and again. */
Bram Moolenaar238a5642006-02-21 22:12:05 +00007159 set_string_option_direct((char_u *)"statusline", -1,
7160 (char_u *)"", OPT_FREE | (*wp->w_p_stl != NUL
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007161 ? OPT_LOCAL : OPT_GLOBAL), SID_ERROR);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007162 }
Bram Moolenaara742e082016-04-05 21:10:38 +02007163 did_emsg |= saved_did_emsg;
Bram Moolenaar362f3562009-11-03 16:20:34 +00007164 entered = FALSE;
Bram Moolenaar238a5642006-02-21 22:12:05 +00007165}
7166#endif
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168/*
7169 * Return TRUE if the status line of window "wp" is connected to the status
7170 * line of the window right of it. If not, then it's a vertical separator.
7171 * Only call if (wp->w_vsep_width != 0).
7172 */
7173 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007174stl_connected(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 frame_T *fr;
7177
7178 fr = wp->w_frame;
7179 while (fr->fr_parent != NULL)
7180 {
7181 if (fr->fr_parent->fr_layout == FR_COL)
7182 {
7183 if (fr->fr_next != NULL)
7184 break;
7185 }
7186 else
7187 {
7188 if (fr->fr_next != NULL)
7189 return TRUE;
7190 }
7191 fr = fr->fr_parent;
7192 }
7193 return FALSE;
7194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197/*
7198 * Get the value to show for the language mappings, active 'keymap'.
7199 */
7200 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007201get_keymap_str(
7202 win_T *wp,
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007203 char_u *fmt, /* format string containing one %s item */
Bram Moolenaar05540972016-01-30 20:31:25 +01007204 char_u *buf, /* buffer for the result */
7205 int len) /* length of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206{
7207 char_u *p;
7208
7209 if (wp->w_buffer->b_p_iminsert != B_IMODE_LMAP)
7210 return FALSE;
7211
7212 {
7213#ifdef FEAT_EVAL
7214 buf_T *old_curbuf = curbuf;
7215 win_T *old_curwin = curwin;
7216 char_u *s;
7217
7218 curbuf = wp->w_buffer;
7219 curwin = wp;
7220 STRCPY(buf, "b:keymap_name"); /* must be writable */
7221 ++emsg_skip;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007222 s = p = eval_to_string(buf, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 --emsg_skip;
7224 curbuf = old_curbuf;
7225 curwin = old_curwin;
7226 if (p == NULL || *p == NUL)
7227#endif
7228 {
7229#ifdef FEAT_KEYMAP
7230 if (wp->w_buffer->b_kmap_state & KEYMAP_LOADED)
7231 p = wp->w_buffer->b_p_keymap;
7232 else
7233#endif
7234 p = (char_u *)"lang";
7235 }
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02007236 if (vim_snprintf((char *)buf, len, (char *)fmt, p) > len - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 buf[0] = NUL;
7238#ifdef FEAT_EVAL
7239 vim_free(s);
7240#endif
7241 }
7242 return buf[0] != NUL;
7243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
7245#if defined(FEAT_STL_OPT) || defined(PROTO)
7246/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007247 * Redraw the status line or ruler of window "wp".
7248 * When "wp" is NULL redraw the tab pages line from 'tabline'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 */
7250 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007251win_redr_custom(
7252 win_T *wp,
7253 int draw_ruler) /* TRUE or FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
Bram Moolenaar1d633412013-12-11 15:52:01 +01007255 static int entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 int attr;
7257 int curattr;
7258 int row;
7259 int col = 0;
7260 int maxwidth;
7261 int width;
7262 int n;
7263 int len;
7264 int fillchar;
7265 char_u buf[MAXPATHL];
Bram Moolenaar362f3562009-11-03 16:20:34 +00007266 char_u *stl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 char_u *p;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007268 struct stl_hlrec hltab[STL_MAX_ITEM];
7269 struct stl_hlrec tabtab[STL_MAX_ITEM];
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007270 int use_sandbox = FALSE;
Bram Moolenaar61452852011-02-01 18:01:11 +01007271 win_T *ewp;
7272 int p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
Bram Moolenaar1d633412013-12-11 15:52:01 +01007274 /* There is a tiny chance that this gets called recursively: When
7275 * redrawing a status line triggers redrawing the ruler or tabline.
7276 * Avoid trouble by not allowing recursion. */
7277 if (entered)
7278 return;
7279 entered = TRUE;
7280
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 /* setup environment for the task at hand */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007282 if (wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007284 /* Use 'tabline'. Always at the first line of the screen. */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007285 stl = p_tal;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007286 row = 0;
Bram Moolenaar65c923a2006-03-03 22:56:30 +00007287 fillchar = ' ';
Bram Moolenaar8820b482017-03-16 17:23:31 +01007288 attr = HL_ATTR(HLF_TPF);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007289 maxwidth = Columns;
7290# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007291 use_sandbox = was_set_insecurely((char_u *)"tabline", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007292# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007294 else
7295 {
7296 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +02007297 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar02631462017-09-22 15:20:32 +02007298 maxwidth = wp->w_width;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007299
7300 if (draw_ruler)
7301 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007302 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007303 /* advance past any leading group spec - implicit in ru_col */
Bram Moolenaar362f3562009-11-03 16:20:34 +00007304 if (*stl == '%')
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007305 {
Bram Moolenaar362f3562009-11-03 16:20:34 +00007306 if (*++stl == '-')
7307 stl++;
7308 if (atoi((char *)stl))
7309 while (VIM_ISDIGIT(*stl))
7310 stl++;
7311 if (*stl++ != '(')
7312 stl = p_ruf;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007313 }
Bram Moolenaar02631462017-09-22 15:20:32 +02007314 col = ru_col - (Columns - wp->w_width);
7315 if (col < (wp->w_width + 1) / 2)
7316 col = (wp->w_width + 1) / 2;
7317 maxwidth = wp->w_width - col;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007318 if (!wp->w_status_height)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007319 {
7320 row = Rows - 1;
7321 --maxwidth; /* writing in last column may cause scrolling */
7322 fillchar = ' ';
7323 attr = 0;
7324 }
7325
7326# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007327 use_sandbox = was_set_insecurely((char_u *)"rulerformat", 0);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007328# endif
7329 }
7330 else
7331 {
7332 if (*wp->w_p_stl != NUL)
Bram Moolenaar362f3562009-11-03 16:20:34 +00007333 stl = wp->w_p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007334 else
Bram Moolenaar362f3562009-11-03 16:20:34 +00007335 stl = p_stl;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007336# ifdef FEAT_EVAL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007337 use_sandbox = was_set_insecurely((char_u *)"statusline",
7338 *wp->w_p_stl == NUL ? 0 : OPT_LOCAL);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007339# endif
7340 }
7341
Bram Moolenaar53f81742017-09-22 14:35:51 +02007342 col += wp->w_wincol;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007343 }
7344
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 if (maxwidth <= 0)
Bram Moolenaar1d633412013-12-11 15:52:01 +01007346 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
Bram Moolenaar61452852011-02-01 18:01:11 +01007348 /* Temporarily reset 'cursorbind', we don't want a side effect from moving
7349 * the cursor away and back. */
7350 ewp = wp == NULL ? curwin : wp;
7351 p_crb_save = ewp->w_p_crb;
7352 ewp->w_p_crb = FALSE;
7353
Bram Moolenaar362f3562009-11-03 16:20:34 +00007354 /* Make a copy, because the statusline may include a function call that
7355 * might change the option value and free the memory. */
7356 stl = vim_strsave(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007357 width = build_stl_str_hl(ewp, buf, sizeof(buf),
Bram Moolenaar362f3562009-11-03 16:20:34 +00007358 stl, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007359 fillchar, maxwidth, hltab, tabtab);
Bram Moolenaar362f3562009-11-03 16:20:34 +00007360 vim_free(stl);
Bram Moolenaar61452852011-02-01 18:01:11 +01007361 ewp->w_p_crb = p_crb_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
Bram Moolenaar7c5676b2010-12-08 19:56:58 +01007363 /* Make all characters printable. */
7364 p = transstr(buf);
7365 if (p != NULL)
7366 {
7367 vim_strncpy(buf, p, sizeof(buf) - 1);
7368 vim_free(p);
7369 }
7370
7371 /* fill up with "fillchar" */
7372 len = (int)STRLEN(buf);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007373 while (width < maxwidth && len < (int)sizeof(buf) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 len += (*mb_char2bytes)(fillchar, buf + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 ++width;
7377 }
7378 buf[len] = NUL;
7379
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007380 /*
7381 * Draw each snippet with the specified highlighting.
7382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 curattr = attr;
7384 p = buf;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007385 for (n = 0; hltab[n].start != NULL; n++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007387 len = (int)(hltab[n].start - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 screen_puts_len(p, len, row, col, curattr);
7389 col += vim_strnsize(p, len);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007390 p = hltab[n].start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007392 if (hltab[n].userhl == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 curattr = attr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007394 else if (hltab[n].userhl < 0)
7395 curattr = syn_id2attr(-hltab[n].userhl);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007396#ifdef FEAT_TERMINAL
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +02007397 else if (wp != NULL && wp != curwin && bt_terminal(wp->w_buffer)
7398 && wp->w_status_height != 0)
7399 curattr = highlight_stltermnc[hltab[n].userhl - 1];
Bram Moolenaarbce4f622017-08-13 21:37:43 +02007400 else if (wp != NULL && bt_terminal(wp->w_buffer)
7401 && wp->w_status_height != 0)
7402 curattr = highlight_stlterm[hltab[n].userhl - 1];
Bram Moolenaar4033c552017-09-16 20:54:51 +02007403#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +00007404 else if (wp != NULL && wp != curwin && wp->w_status_height != 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007405 curattr = highlight_stlnc[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007407 curattr = highlight_user[hltab[n].userhl - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
7409 screen_puts(p, row, col, curattr);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007410
7411 if (wp == NULL)
7412 {
7413 /* Fill the TabPageIdxs[] array for clicking in the tab pagesline. */
7414 col = 0;
7415 len = 0;
7416 p = buf;
7417 fillchar = 0;
7418 for (n = 0; tabtab[n].start != NULL; n++)
7419 {
7420 len += vim_strnsize(p, (int)(tabtab[n].start - p));
7421 while (col < len)
7422 TabPageIdxs[col++] = fillchar;
7423 p = tabtab[n].start;
7424 fillchar = tabtab[n].userhl;
7425 }
7426 while (col < Columns)
7427 TabPageIdxs[col++] = fillchar;
7428 }
Bram Moolenaar1d633412013-12-11 15:52:01 +01007429
7430theend:
7431 entered = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432}
7433
7434#endif /* FEAT_STL_OPT */
7435
7436/*
7437 * Output a single character directly to the screen and update ScreenLines.
7438 */
7439 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007440screen_putchar(int c, int row, int col, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 char_u buf[MB_MAXBYTES + 1];
7443
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007444 if (has_mbyte)
7445 buf[(*mb_char2bytes)(c, buf)] = NUL;
7446 else
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007447 {
7448 buf[0] = c;
7449 buf[1] = NUL;
7450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 screen_puts(buf, row, col, attr);
7452}
7453
7454/*
7455 * Get a single character directly from ScreenLines into "bytes[]".
7456 * Also return its attribute in *attrp;
7457 */
7458 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007459screen_getbytes(int row, int col, char_u *bytes, int *attrp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461 unsigned off;
7462
7463 /* safety check */
7464 if (ScreenLines != NULL && row < screen_Rows && col < screen_Columns)
7465 {
7466 off = LineOffset[row] + col;
7467 *attrp = ScreenAttrs[off];
7468 bytes[0] = ScreenLines[off];
7469 bytes[1] = NUL;
7470
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (enc_utf8 && ScreenLinesUC[off] != 0)
7472 bytes[utfc_char2bytes(off, bytes)] = NUL;
7473 else if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
7474 {
7475 bytes[0] = ScreenLines[off];
7476 bytes[1] = ScreenLines2[off];
7477 bytes[2] = NUL;
7478 }
7479 else if (enc_dbcs && MB_BYTE2LEN(bytes[0]) > 1)
7480 {
7481 bytes[1] = ScreenLines[off + 1];
7482 bytes[2] = NUL;
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485}
7486
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007487/*
7488 * Return TRUE if composing characters for screen posn "off" differs from
7489 * composing characters in "u8cc".
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007490 * Only to be used when ScreenLinesUC[off] != 0.
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007491 */
7492 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007493screen_comp_differs(int off, int *u8cc)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007494{
7495 int i;
7496
7497 for (i = 0; i < Screen_mco; ++i)
7498 {
7499 if (ScreenLinesC[i][off] != (u8char_T)u8cc[i])
7500 return TRUE;
7501 if (u8cc[i] == 0)
7502 break;
7503 }
7504 return FALSE;
7505}
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507/*
7508 * Put string '*text' on the screen at position 'row' and 'col', with
7509 * attributes 'attr', and update ScreenLines[] and ScreenAttrs[].
7510 * Note: only outputs within one row, message is truncated at screen boundary!
7511 * Note: if ScreenLines[], row and/or col is invalid, nothing is done.
7512 */
7513 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007514screen_puts(
7515 char_u *text,
7516 int row,
7517 int col,
7518 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
7520 screen_puts_len(text, -1, row, col, attr);
7521}
7522
7523/*
7524 * Like screen_puts(), but output "text[len]". When "len" is -1 output up to
7525 * a NUL.
7526 */
7527 void
Bram Moolenaar05540972016-01-30 20:31:25 +01007528screen_puts_len(
7529 char_u *text,
7530 int textlen,
7531 int row,
7532 int col,
7533 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534{
7535 unsigned off;
7536 char_u *ptr = text;
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007537 int len = textlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 int c;
Bram Moolenaar367329b2007-08-30 11:53:22 +00007539 unsigned max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 int mbyte_blen = 1;
7541 int mbyte_cells = 1;
7542 int u8c = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007543 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 int clear_next_cell = FALSE;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007545#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 int prev_c = 0; /* previous Arabic character */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007547 int pc, nc, nc1;
7548 int pcc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549#endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007550 int force_redraw_this;
7551 int force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007552 int need_redraw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 if (ScreenLines == NULL || row >= screen_Rows) /* safety check */
7555 return;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007556 off = LineOffset[row] + col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaarc236c162008-07-13 17:41:49 +00007558 /* When drawing over the right halve of a double-wide char clear out the
7559 * left halve. Only needed in a terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00007560 if (has_mbyte && col > 0 && col < screen_Columns
Bram Moolenaara12a1612019-01-24 16:39:02 +01007561#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00007562 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01007563#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007564 && mb_fix_col(col, row) != col)
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007565 {
7566 ScreenLines[off - 1] = ' ';
7567 ScreenAttrs[off - 1] = 0;
7568 if (enc_utf8)
7569 {
7570 ScreenLinesUC[off - 1] = 0;
7571 ScreenLinesC[0][off - 1] = 0;
7572 }
7573 /* redraw the previous cell, make it empty */
7574 screen_char(off - 1, row, col - 1);
7575 /* force the cell at "col" to be redrawn */
7576 force_redraw_next = TRUE;
7577 }
Bram Moolenaarc236c162008-07-13 17:41:49 +00007578
Bram Moolenaar367329b2007-08-30 11:53:22 +00007579 max_off = LineOffset[row] + screen_Columns;
Bram Moolenaara064ac82007-08-05 18:10:54 +00007580 while (col < screen_Columns
7581 && (len < 0 || (int)(ptr - text) < len)
7582 && *ptr != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 {
7584 c = *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 /* check if this is the first byte of a multibyte */
7586 if (has_mbyte)
7587 {
7588 if (enc_utf8 && len > 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007589 mbyte_blen = utfc_ptr2len_len(ptr, (int)((text + len) - ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007591 mbyte_blen = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7593 mbyte_cells = 1;
7594 else if (enc_dbcs != 0)
7595 mbyte_cells = mbyte_blen;
7596 else /* enc_utf8 */
7597 {
7598 if (len >= 0)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007599 u8c = utfc_ptr2char_len(ptr, u8cc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 (int)((text + len) - ptr));
7601 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007602 u8c = utfc_ptr2char(ptr, u8cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 mbyte_cells = utf_char2cells(u8c);
Bram Moolenaara12a1612019-01-24 16:39:02 +01007604#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (p_arshape && !p_tbidi && ARABIC_CHAR(u8c))
7606 {
7607 /* Do Arabic shaping. */
7608 if (len >= 0 && (int)(ptr - text) + mbyte_blen >= len)
7609 {
7610 /* Past end of string to be displayed. */
7611 nc = NUL;
7612 nc1 = NUL;
7613 }
7614 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007615 {
Bram Moolenaar54620182009-11-11 16:07:20 +00007616 nc = utfc_ptr2char_len(ptr + mbyte_blen, pcc,
7617 (int)((text + len) - ptr - mbyte_blen));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007618 nc1 = pcc[0];
7619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 pc = prev_c;
7621 prev_c = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007622 u8c = arabic_shape(u8c, &c, &u8cc[0], nc, nc1, pc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
7624 else
7625 prev_c = u8c;
Bram Moolenaara12a1612019-01-24 16:39:02 +01007626#endif
Bram Moolenaare4ebd292010-01-19 17:40:46 +01007627 if (col + mbyte_cells > screen_Columns)
7628 {
7629 /* Only 1 cell left, but character requires 2 cells:
7630 * display a '>' in the last column to avoid wrapping. */
7631 c = '>';
7632 mbyte_cells = 1;
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
7635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007637 force_redraw_this = force_redraw_next;
7638 force_redraw_next = FALSE;
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007639
7640 need_redraw = ScreenLines[off] != c
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 || (mbyte_cells == 2
7642 && ScreenLines[off + 1] != (enc_dbcs ? ptr[1] : 0))
7643 || (enc_dbcs == DBCS_JPNU
7644 && c == 0x8e
7645 && ScreenLines2[off] != ptr[1])
7646 || (enc_utf8
Bram Moolenaar70c49c12010-03-23 15:36:35 +01007647 && (ScreenLinesUC[off] !=
7648 (u8char_T)(c < 0x80 && u8cc[0] == 0 ? 0 : u8c)
7649 || (ScreenLinesUC[off] != 0
7650 && screen_comp_differs(off, u8cc))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 || ScreenAttrs[off] != attr
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007652 || exmode_active;
7653
Bram Moolenaara12a1612019-01-24 16:39:02 +01007654 if (need_redraw || force_redraw_this)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656#if defined(FEAT_GUI) || defined(UNIX)
7657 /* The bold trick makes a single row of pixels appear in the next
7658 * character. When a bold character is removed, the next
7659 * character should be redrawn too. This happens for our own GUI
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007660 * and for some xterms. */
7661 if (need_redraw && ScreenLines[off] != ' ' && (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662# ifdef FEAT_GUI
7663 gui.in_use
7664# endif
7665# if defined(FEAT_GUI) && defined(UNIX)
7666 ||
7667# endif
7668# ifdef UNIX
7669 term_is_xterm
7670# endif
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007671 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007673 int n = ScreenAttrs[off];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007675 if (n > HL_ALL)
7676 n = syn_attr2attr(n);
7677 if (n & HL_BOLD)
7678 force_redraw_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 /* When at the end of the text and overwriting a two-cell
7682 * character with a one-cell character, need to clear the next
7683 * cell. Also when overwriting the left halve of a two-cell char
7684 * with the right halve of a two-cell char. Do this only once
7685 * (mb_off2cells() may return 2 on the right halve). */
7686 if (clear_next_cell)
7687 clear_next_cell = FALSE;
7688 else if (has_mbyte
7689 && (len < 0 ? ptr[mbyte_blen] == NUL
7690 : ptr + mbyte_blen >= text + len)
Bram Moolenaar367329b2007-08-30 11:53:22 +00007691 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007693 && (*mb_off2cells)(off, max_off) == 1
7694 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 clear_next_cell = TRUE;
7696
7697 /* Make sure we never leave a second byte of a double-byte behind,
7698 * it confuses mb_off2cells(). */
7699 if (enc_dbcs
Bram Moolenaar367329b2007-08-30 11:53:22 +00007700 && ((mbyte_cells == 1 && (*mb_off2cells)(off, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 || (mbyte_cells == 2
Bram Moolenaar367329b2007-08-30 11:53:22 +00007702 && (*mb_off2cells)(off, max_off) == 1
7703 && (*mb_off2cells)(off + 1, max_off) > 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 ScreenLines[off + mbyte_blen] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 ScreenLines[off] = c;
7706 ScreenAttrs[off] = attr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 if (enc_utf8)
7708 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007709 if (c < 0x80 && u8cc[0] == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 ScreenLinesUC[off] = 0;
7711 else
7712 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007713 int i;
7714
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 ScreenLinesUC[off] = u8c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007716 for (i = 0; i < Screen_mco; ++i)
7717 {
7718 ScreenLinesC[i][off] = u8cc[i];
7719 if (u8cc[i] == 0)
7720 break;
7721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723 if (mbyte_cells == 2)
7724 {
7725 ScreenLines[off + 1] = 0;
7726 ScreenAttrs[off + 1] = attr;
7727 }
7728 screen_char(off, row, col);
7729 }
7730 else if (mbyte_cells == 2)
7731 {
7732 ScreenLines[off + 1] = ptr[1];
7733 ScreenAttrs[off + 1] = attr;
7734 screen_char_2(off, row, col);
7735 }
7736 else if (enc_dbcs == DBCS_JPNU && c == 0x8e)
7737 {
7738 ScreenLines2[off] = ptr[1];
7739 screen_char(off, row, col);
7740 }
7741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 screen_char(off, row, col);
7743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 if (has_mbyte)
7745 {
7746 off += mbyte_cells;
7747 col += mbyte_cells;
7748 ptr += mbyte_blen;
7749 if (clear_next_cell)
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007750 {
7751 /* This only happens at the end, display one space next. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 ptr = (char_u *)" ";
Bram Moolenaare4c21e62014-05-22 16:05:19 +02007753 len = -1;
7754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 ++off;
7759 ++col;
7760 ++ptr;
7761 }
7762 }
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007763
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007764 /* If we detected the next character needs to be redrawn, but the text
7765 * doesn't extend up to there, update the character here. */
7766 if (force_redraw_next && col < screen_Columns)
7767 {
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007768 if (enc_dbcs != 0 && dbcs_off2cells(off, max_off) > 1)
7769 screen_char_2(off, row, col);
7770 else
Bram Moolenaar2bea2912009-03-11 16:58:40 +00007771 screen_char(off, row, col);
7772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775#ifdef FEAT_SEARCH_EXTRA
7776/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007777 * Prepare for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 */
7779 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007780start_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 if (p_hls && !no_hlsearch)
7783 {
7784 last_pat_prog(&search_hl.rm);
Bram Moolenaar8820b482017-03-16 17:23:31 +01007785 search_hl.attr = HL_ATTR(HLF_L);
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007786# ifdef FEAT_RELTIME
7787 /* Set the time limit to 'redrawtime'. */
7788 profile_setlimit(p_rdt, &search_hl.tm);
7789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 }
7791}
7792
7793/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007794 * Clean up for 'hlsearch' highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 */
7796 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007797end_search_hl(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 if (search_hl.rm.regprog != NULL)
7800 {
Bram Moolenaar473de612013-06-08 18:19:48 +02007801 vim_regfree(search_hl.rm.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 search_hl.rm.regprog = NULL;
7803 }
7804}
7805
7806/*
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007807 * Init for calling prepare_search_hl().
7808 */
7809 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007810init_search_hl(win_T *wp)
Bram Moolenaar0af8ceb2010-07-05 22:22:57 +02007811{
7812 matchitem_T *cur;
7813
7814 /* Setup for match and 'hlsearch' highlighting. Disable any previous
7815 * match */
7816 cur = wp->w_match_head;
7817 while (cur != NULL)
7818 {
7819 cur->hl.rm = cur->match;
7820 if (cur->hlg_id == 0)
7821 cur->hl.attr = 0;
7822 else
7823 cur->hl.attr = syn_id2attr(cur->hlg_id);
7824 cur->hl.buf = wp->w_buffer;
7825 cur->hl.lnum = 0;
7826 cur->hl.first_lnum = 0;
7827# ifdef FEAT_RELTIME
7828 /* Set the time limit to 'redrawtime'. */
7829 profile_setlimit(p_rdt, &(cur->hl.tm));
7830# endif
7831 cur = cur->next;
7832 }
7833 search_hl.buf = wp->w_buffer;
7834 search_hl.lnum = 0;
7835 search_hl.first_lnum = 0;
7836 /* time limit is set at the toplevel, for all windows */
7837}
7838
7839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 * Advance to the match in window "wp" line "lnum" or past it.
7841 */
7842 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007843prepare_search_hl(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007845 matchitem_T *cur; /* points to the match list */
7846 match_T *shl; /* points to search_hl or a match */
7847 int shl_flag; /* flag to indicate whether search_hl
7848 has been processed or not */
Bram Moolenaarb3414592014-06-17 17:48:32 +02007849 int pos_inprogress; /* marks that position match search is
7850 in progress */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 int n;
7852
7853 /*
7854 * When using a multi-line pattern, start searching at the top
7855 * of the window or just after a closed fold.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007856 * Do this both for search_hl and the match list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007858 cur = wp->w_match_head;
7859 shl_flag = FALSE;
7860 while (cur != NULL || shl_flag == FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007862 if (shl_flag == FALSE)
7863 {
7864 shl = &search_hl;
7865 shl_flag = TRUE;
7866 }
7867 else
7868 shl = &cur->hl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (shl->rm.regprog != NULL
7870 && shl->lnum == 0
7871 && re_multiline(shl->rm.regprog))
7872 {
7873 if (shl->first_lnum == 0)
7874 {
7875# ifdef FEAT_FOLDING
7876 for (shl->first_lnum = lnum;
7877 shl->first_lnum > wp->w_topline; --shl->first_lnum)
7878 if (hasFoldingWin(wp, shl->first_lnum - 1,
7879 NULL, NULL, TRUE, NULL))
7880 break;
7881# else
7882 shl->first_lnum = wp->w_topline;
7883# endif
7884 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007885 if (cur != NULL)
7886 cur->pos.cur = 0;
7887 pos_inprogress = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 n = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007889 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
7890 || (cur != NULL && pos_inprogress)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
Bram Moolenaare17bdff2016-08-27 18:34:29 +02007892 next_search_hl(wp, shl, shl->first_lnum, (colnr_T)n,
7893 shl == &search_hl ? NULL : cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007894 pos_inprogress = cur == NULL || cur->pos.cur == 0
7895 ? FALSE : TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (shl->lnum != 0)
7897 {
7898 shl->first_lnum = shl->lnum
7899 + shl->rm.endpos[0].lnum
7900 - shl->rm.startpos[0].lnum;
7901 n = shl->rm.endpos[0].col;
7902 }
7903 else
7904 {
7905 ++shl->first_lnum;
7906 n = 0;
7907 }
7908 }
7909 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007910 if (shl != &search_hl && cur != NULL)
7911 cur = cur->next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 }
7913}
7914
7915/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007916 * Search for a next 'hlsearch' or match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 * Uses shl->buf.
7918 * Sets shl->lnum and shl->rm contents.
7919 * Note: Assumes a previous match is always before "lnum", unless
7920 * shl->lnum is zero.
7921 * Careful: Any pointers for buffer lines will become invalid.
7922 */
7923 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007924next_search_hl(
7925 win_T *win,
7926 match_T *shl, /* points to search_hl or a match */
7927 linenr_T lnum,
7928 colnr_T mincol, /* minimal column for a match */
7929 matchitem_T *cur) /* to retrieve match positions if any */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 linenr_T l;
7932 colnr_T matchcol;
7933 long nmatched;
Bram Moolenaarbcf94422018-06-23 14:21:42 +02007934 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
Bram Moolenaarb0acacd2018-08-11 16:40:43 +02007936 // for :{range}s/pat only highlight inside the range
7937 if (lnum < search_first_line || lnum > search_last_line)
7938 {
7939 shl->lnum = 0;
7940 return;
7941 }
7942
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (shl->lnum != 0)
7944 {
7945 /* Check for three situations:
7946 * 1. If the "lnum" is below a previous match, start a new search.
7947 * 2. If the previous match includes "mincol", use it.
7948 * 3. Continue after the previous match.
7949 */
7950 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
7951 if (lnum > l)
7952 shl->lnum = 0;
7953 else if (lnum < l || shl->rm.endpos[0].col > mincol)
7954 return;
7955 }
7956
7957 /*
7958 * Repeat searching for a match until one is found that includes "mincol"
7959 * or none is found in this line.
7960 */
7961 called_emsg = FALSE;
7962 for (;;)
7963 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00007964#ifdef FEAT_RELTIME
7965 /* Stop searching after passing the time limit. */
7966 if (profile_passed_limit(&(shl->tm)))
7967 {
7968 shl->lnum = 0; /* no match found in time */
7969 break;
7970 }
7971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 /* Three situations:
7973 * 1. No useful previous match: search from start of line.
7974 * 2. Not Vi compatible or empty match: continue at next character.
7975 * Break the loop if this is beyond the end of the line.
7976 * 3. Vi compatible searching: continue at end of previous match.
7977 */
7978 if (shl->lnum == 0)
7979 matchcol = 0;
7980 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
7981 || (shl->rm.endpos[0].lnum == 0
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007982 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007984 char_u *ml;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007985
7986 matchcol = shl->rm.startpos[0].col;
Bram Moolenaar5c8837f2006-02-25 21:52:33 +00007987 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007988 if (*ml == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007990 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 shl->lnum = 0;
7992 break;
7993 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007994 if (has_mbyte)
7995 matchcol += mb_ptr2len(ml);
7996 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007997 ++matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 }
7999 else
8000 matchcol = shl->rm.endpos[0].col;
8001
8002 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008003 if (shl->rm.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008005 /* Remember whether shl->rm is using a copy of the regprog in
8006 * cur->match. */
8007 int regprog_is_copy = (shl != &search_hl && cur != NULL
8008 && shl == &cur->hl
8009 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008010 int timed_out = FALSE;
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008011
Bram Moolenaarb3414592014-06-17 17:48:32 +02008012 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
8013 matchcol,
8014#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008015 &(shl->tm), &timed_out
Bram Moolenaarb3414592014-06-17 17:48:32 +02008016#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008017 NULL, NULL
Bram Moolenaarb3414592014-06-17 17:48:32 +02008018#endif
8019 );
Bram Moolenaarcbdf0a02014-11-27 13:37:10 +01008020 /* Copy the regprog, in case it got freed and recompiled. */
8021 if (regprog_is_copy)
8022 cur->match.regprog = cur->hl.rm.regprog;
8023
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008024 if (called_emsg || got_int || timed_out)
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008025 {
Bram Moolenaarb3414592014-06-17 17:48:32 +02008026 /* Error while handling regexp: stop using this regexp. */
8027 if (shl == &search_hl)
8028 {
8029 /* don't free regprog in the match list, it's a copy */
8030 vim_regfree(shl->rm.regprog);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008031 set_no_hlsearch(TRUE);
Bram Moolenaarb3414592014-06-17 17:48:32 +02008032 }
8033 shl->rm.regprog = NULL;
8034 shl->lnum = 0;
8035 got_int = FALSE; /* avoid the "Type :quit to exit Vim"
8036 message */
8037 break;
Bram Moolenaar0ddf0a72007-05-01 20:04:53 +00008038 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02008039 }
8040 else if (cur != NULL)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008041 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
Bram Moolenaardeae0f22014-06-18 21:20:11 +02008042 else
8043 nmatched = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 if (nmatched == 0)
8045 {
8046 shl->lnum = 0; /* no match found */
8047 break;
8048 }
8049 if (shl->rm.startpos[0].lnum > 0
8050 || shl->rm.startpos[0].col >= mincol
8051 || nmatched > 1
8052 || shl->rm.endpos[0].col > mincol)
8053 {
8054 shl->lnum += shl->rm.startpos[0].lnum;
8055 break; /* useful match found */
8056 }
8057 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +02008058
8059 // Restore called_emsg for assert_fails().
8060 called_emsg = save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062
Bram Moolenaar85077472016-10-16 14:35:48 +02008063/*
8064 * If there is a match fill "shl" and return one.
8065 * Return zero otherwise.
8066 */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008067 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008068next_search_hl_pos(
8069 match_T *shl, /* points to a match */
8070 linenr_T lnum,
8071 posmatch_T *posmatch, /* match positions */
8072 colnr_T mincol) /* minimal column for a match */
Bram Moolenaarb3414592014-06-17 17:48:32 +02008073{
8074 int i;
Bram Moolenaar85077472016-10-16 14:35:48 +02008075 int found = -1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008076
Bram Moolenaarb3414592014-06-17 17:48:32 +02008077 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
8078 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008079 llpos_T *pos = &posmatch->pos[i];
8080
8081 if (pos->lnum == 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008082 break;
Bram Moolenaar85077472016-10-16 14:35:48 +02008083 if (pos->len == 0 && pos->col < mincol)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008084 continue;
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008085 if (pos->lnum == lnum)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008086 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008087 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008089 /* if this match comes before the one at "found" then swap
8090 * them */
8091 if (pos->col < posmatch->pos[found].col)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008092 {
Bram Moolenaara6c27ee2016-10-15 14:56:30 +02008093 llpos_T tmp = *pos;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008094
Bram Moolenaar85077472016-10-16 14:35:48 +02008095 *pos = posmatch->pos[found];
8096 posmatch->pos[found] = tmp;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008097 }
8098 }
8099 else
Bram Moolenaar85077472016-10-16 14:35:48 +02008100 found = i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008101 }
8102 }
8103 posmatch->cur = 0;
Bram Moolenaar85077472016-10-16 14:35:48 +02008104 if (found >= 0)
Bram Moolenaarb3414592014-06-17 17:48:32 +02008105 {
Bram Moolenaar85077472016-10-16 14:35:48 +02008106 colnr_T start = posmatch->pos[found].col == 0
8107 ? 0 : posmatch->pos[found].col - 1;
8108 colnr_T end = posmatch->pos[found].col == 0
8109 ? MAXCOL : start + posmatch->pos[found].len;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008110
Bram Moolenaar85077472016-10-16 14:35:48 +02008111 shl->lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008112 shl->rm.startpos[0].lnum = 0;
8113 shl->rm.startpos[0].col = start;
8114 shl->rm.endpos[0].lnum = 0;
8115 shl->rm.endpos[0].col = end;
Bram Moolenaar4f416e42016-08-16 16:08:18 +02008116 shl->is_addpos = TRUE;
Bram Moolenaar85077472016-10-16 14:35:48 +02008117 posmatch->cur = found + 1;
8118 return 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008119 }
Bram Moolenaar85077472016-10-16 14:35:48 +02008120 return 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02008121}
Bram Moolenaarde993ea2014-06-17 23:18:01 +02008122#endif
Bram Moolenaarb3414592014-06-17 17:48:32 +02008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008125screen_start_highlight(int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126{
8127 attrentry_T *aep = NULL;
8128
8129 screen_attr = attr;
8130 if (full_screen
Bram Moolenaar4f974752019-02-17 17:44:42 +01008131#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 && termcap_active
8133#endif
8134 )
8135 {
8136#ifdef FEAT_GUI
8137 if (gui.in_use)
8138 {
8139 char buf[20];
8140
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008141 /* The GUI handles this internally. */
8142 sprintf(buf, IF_EB("\033|%dh", ESC_STR "|%dh"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 OUT_STR(buf);
8144 }
8145 else
8146#endif
8147 {
8148 if (attr > HL_ALL) /* special HL attr. */
8149 {
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008150 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 aep = syn_cterm_attr2entry(attr);
8152 else
8153 aep = syn_term_attr2entry(attr);
8154 if (aep == NULL) /* did ":syntax clear" */
8155 attr = 0;
8156 else
8157 attr = aep->ae_attr;
8158 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008159 if ((attr & HL_BOLD) && *T_MD != NUL) /* bold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 out_str(T_MD);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008161 else if (aep != NULL && cterm_normal_fg_bold && (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008162#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008163 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8164 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8165 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008166#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008167 t_colors > 1 && aep->ae_u.cterm.fg_color))
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008168 /* If the Normal FG color has BOLD attribute and the new HL
8169 * has a FG color defined, clear BOLD. */
8170 out_str(T_ME);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008171 if ((attr & HL_STANDOUT) && *T_SO != NUL) /* standout */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 out_str(T_SO);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008173 if ((attr & HL_UNDERCURL) && *T_UCS != NUL) /* undercurl */
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008174 out_str(T_UCS);
8175 if (((attr & HL_UNDERLINE) /* underline or undercurl */
Bram Moolenaar45a00002017-12-22 21:12:34 +01008176 || ((attr & HL_UNDERCURL) && *T_UCS == NUL))
8177 && *T_US != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 out_str(T_US);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008179 if ((attr & HL_ITALIC) && *T_CZH != NUL) /* italic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 out_str(T_CZH);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008181 if ((attr & HL_INVERSE) && *T_MR != NUL) /* inverse (reverse) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 out_str(T_MR);
Bram Moolenaar45a00002017-12-22 21:12:34 +01008183 if ((attr & HL_STRIKETHROUGH) && *T_STS != NUL) /* strike */
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008184 out_str(T_STS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185
8186 /*
8187 * Output the color or start string after bold etc., in case the
8188 * bold etc. override the color setting.
8189 */
8190 if (aep != NULL)
8191 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008192#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008193 /* When 'termguicolors' is set but fg or bg is unset,
8194 * fall back to the cterm colors. This helps for SpellBad,
8195 * where the GUI uses a red undercurl. */
8196 if (p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008198 if (aep->ae_u.cterm.fg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008199 term_fg_rgb_color(aep->ae_u.cterm.fg_rgb);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008200 }
8201 else
8202#endif
8203 if (t_colors > 1)
8204 {
8205 if (aep->ae_u.cterm.fg_color)
8206 term_fg_color(aep->ae_u.cterm.fg_color - 1);
8207 }
8208#ifdef FEAT_TERMGUICOLORS
8209 if (p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR)
8210 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008211 if (aep->ae_u.cterm.bg_rgb != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008212 term_bg_rgb_color(aep->ae_u.cterm.bg_rgb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 }
8214 else
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008215#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008216 if (t_colors > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008218 if (aep->ae_u.cterm.bg_color)
8219 term_bg_color(aep->ae_u.cterm.bg_color - 1);
8220 }
8221
Bram Moolenaarf708ac52018-03-12 21:48:32 +01008222 if (!IS_CTERM)
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008223 {
8224 if (aep->ae_u.term.start != NULL)
8225 out_str(aep->ae_u.term.start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227 }
8228 }
8229 }
8230}
8231
8232 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008233screen_stop_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235 int do_ME = FALSE; /* output T_ME code */
8236
8237 if (screen_attr != 0
Bram Moolenaar4f974752019-02-17 17:44:42 +01008238#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 && termcap_active
8240#endif
8241 )
8242 {
8243#ifdef FEAT_GUI
8244 if (gui.in_use)
8245 {
8246 char buf[20];
8247
8248 /* use internal GUI code */
8249 sprintf(buf, IF_EB("\033|%dH", ESC_STR "|%dH"), screen_attr);
8250 OUT_STR(buf);
8251 }
8252 else
8253#endif
8254 {
8255 if (screen_attr > HL_ALL) /* special HL attr. */
8256 {
8257 attrentry_T *aep;
8258
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008259 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
8261 /*
8262 * Assume that t_me restores the original colors!
8263 */
8264 aep = syn_cterm_attr2entry(screen_attr);
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008265 if (aep != NULL && ((
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008266#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008267 p_tgc && aep->ae_u.cterm.fg_rgb != CTERMCOLOR
8268 ? aep->ae_u.cterm.fg_rgb != INVALCOLOR
8269 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008270#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008271 aep->ae_u.cterm.fg_color) || (
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008272#ifdef FEAT_TERMGUICOLORS
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008273 p_tgc && aep->ae_u.cterm.bg_rgb != CTERMCOLOR
8274 ? aep->ae_u.cterm.bg_rgb != INVALCOLOR
8275 :
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008276#endif
Bram Moolenaard4fc5772018-02-27 14:39:03 +01008277 aep->ae_u.cterm.bg_color)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 do_ME = TRUE;
8279 }
8280 else
8281 {
8282 aep = syn_term_attr2entry(screen_attr);
8283 if (aep != NULL && aep->ae_u.term.stop != NULL)
8284 {
8285 if (STRCMP(aep->ae_u.term.stop, T_ME) == 0)
8286 do_ME = TRUE;
8287 else
8288 out_str(aep->ae_u.term.stop);
8289 }
8290 }
8291 if (aep == NULL) /* did ":syntax clear" */
8292 screen_attr = 0;
8293 else
8294 screen_attr = aep->ae_attr;
8295 }
8296
8297 /*
8298 * Often all ending-codes are equal to T_ME. Avoid outputting the
8299 * same sequence several times.
8300 */
8301 if (screen_attr & HL_STANDOUT)
8302 {
8303 if (STRCMP(T_SE, T_ME) == 0)
8304 do_ME = TRUE;
8305 else
8306 out_str(T_SE);
8307 }
Bram Moolenaar45a00002017-12-22 21:12:34 +01008308 if ((screen_attr & HL_UNDERCURL) && *T_UCE != NUL)
Bram Moolenaar8b9e20a2017-11-28 21:25:21 +01008309 {
8310 if (STRCMP(T_UCE, T_ME) == 0)
8311 do_ME = TRUE;
8312 else
8313 out_str(T_UCE);
8314 }
8315 if ((screen_attr & HL_UNDERLINE)
Bram Moolenaar45a00002017-12-22 21:12:34 +01008316 || ((screen_attr & HL_UNDERCURL) && *T_UCE == NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {
8318 if (STRCMP(T_UE, T_ME) == 0)
8319 do_ME = TRUE;
8320 else
8321 out_str(T_UE);
8322 }
8323 if (screen_attr & HL_ITALIC)
8324 {
8325 if (STRCMP(T_CZR, T_ME) == 0)
8326 do_ME = TRUE;
8327 else
8328 out_str(T_CZR);
8329 }
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02008330 if (screen_attr & HL_STRIKETHROUGH)
8331 {
8332 if (STRCMP(T_STE, T_ME) == 0)
8333 do_ME = TRUE;
8334 else
8335 out_str(T_STE);
8336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 if (do_ME || (screen_attr & (HL_BOLD | HL_INVERSE)))
8338 out_str(T_ME);
8339
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008340#ifdef FEAT_TERMGUICOLORS
8341 if (p_tgc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008343 if (cterm_normal_fg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008344 term_fg_rgb_color(cterm_normal_fg_gui_color);
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008345 if (cterm_normal_bg_gui_color != INVALCOLOR)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008346 term_bg_rgb_color(cterm_normal_bg_gui_color);
8347 }
8348 else
8349#endif
8350 {
8351 if (t_colors > 1)
8352 {
8353 /* set Normal cterm colors */
8354 if (cterm_normal_fg_color != 0)
8355 term_fg_color(cterm_normal_fg_color - 1);
8356 if (cterm_normal_bg_color != 0)
8357 term_bg_color(cterm_normal_bg_color - 1);
8358 if (cterm_normal_fg_bold)
8359 out_str(T_MD);
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 }
8362 }
8363 }
8364 screen_attr = 0;
8365}
8366
8367/*
8368 * Reset the colors for a cterm. Used when leaving Vim.
8369 * The machine specific code may override this again.
8370 */
8371 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008372reset_cterm_colors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373{
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008374 if (IS_CTERM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {
8376 /* set Normal cterm colors */
Bram Moolenaar61be73b2016-04-29 22:59:22 +02008377#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02008378 if (p_tgc ? (cterm_normal_fg_gui_color != INVALCOLOR
8379 || cterm_normal_bg_gui_color != INVALCOLOR)
8380 : (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0))
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008381#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 if (cterm_normal_fg_color > 0 || cterm_normal_bg_color > 0)
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
8385 out_str(T_OP);
8386 screen_attr = -1;
8387 }
8388 if (cterm_normal_fg_bold)
8389 {
8390 out_str(T_ME);
8391 screen_attr = -1;
8392 }
8393 }
8394}
8395
8396/*
8397 * Put character ScreenLines["off"] on the screen at position "row" and "col",
8398 * using the attributes from ScreenAttrs["off"].
8399 */
8400 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008401screen_char(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int attr;
8404
8405 /* Check for illegal values, just in case (could happen just after
8406 * resizing). */
8407 if (row >= screen_Rows || col >= screen_Columns)
8408 return;
8409
Bram Moolenaarae654382019-01-17 21:09:05 +01008410#ifdef FEAT_INS_EXPAND
8411 if (pum_under_menu(row, col))
8412 return;
8413#endif
Bram Moolenaar494838a2015-02-10 19:20:37 +01008414 /* Outputting a character in the last cell on the screen may scroll the
8415 * screen up. Only do it when the "xn" termcap property is set, otherwise
8416 * mark the character invalid (update it when scrolled up). */
8417 if (*T_XN == NUL
8418 && row == screen_Rows - 1 && col == screen_Columns - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419#ifdef FEAT_RIGHTLEFT
8420 /* account for first command-line character in rightleft mode */
8421 && !cmdmsg_rl
8422#endif
8423 )
8424 {
8425 ScreenAttrs[off] = (sattr_T)-1;
8426 return;
8427 }
8428
8429 /*
8430 * Stop highlighting first, so it's easier to move the cursor.
8431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (screen_char_attr != 0)
8433 attr = screen_char_attr;
8434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 attr = ScreenAttrs[off];
8436 if (screen_attr != attr)
8437 screen_stop_highlight();
8438
8439 windgoto(row, col);
8440
8441 if (screen_attr != attr)
8442 screen_start_highlight(attr);
8443
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 if (enc_utf8 && ScreenLinesUC[off] != 0)
8445 {
8446 char_u buf[MB_MAXBYTES + 1];
8447
Bram Moolenaarcb070082016-04-02 22:14:51 +02008448 if (utf_ambiguous_width(ScreenLinesUC[off]))
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008449 {
8450 if (*p_ambw == 'd'
Bram Moolenaara12a1612019-01-24 16:39:02 +01008451#ifdef FEAT_GUI
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008452 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008453#endif
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008454 )
8455 {
8456 /* Clear the two screen cells. If the character is actually
8457 * single width it won't change the second cell. */
8458 out_str((char_u *)" ");
8459 term_windgoto(row, col);
8460 }
8461 /* not sure where the cursor is after drawing the ambiguous width
8462 * character */
Bram Moolenaarcb070082016-04-02 22:14:51 +02008463 screen_cur_col = 9999;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008464 }
Bram Moolenaarcb070082016-04-02 22:14:51 +02008465 else if (utf_char2cells(ScreenLinesUC[off]) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 ++screen_cur_col;
Bram Moolenaarfae8ed12017-12-12 22:29:30 +01008467
8468 /* Convert the UTF-8 character to bytes and write it. */
8469 buf[utfc_char2bytes(off, buf)] = NUL;
8470 out_str(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 }
8472 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 /* double-byte character in single-width cell */
8477 if (enc_dbcs == DBCS_JPNU && ScreenLines[off] == 0x8e)
8478 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
8480
8481 screen_cur_col++;
8482}
8483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484/*
8485 * Used for enc_dbcs only: Put one double-wide character at ScreenLines["off"]
8486 * on the screen at position 'row' and 'col'.
8487 * The attributes of the first byte is used for all. This is required to
8488 * output the two bytes of a double-byte character with nothing in between.
8489 */
8490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008491screen_char_2(unsigned off, int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
8493 /* Check for illegal values (could be wrong when screen was resized). */
8494 if (off + 1 >= (unsigned)(screen_Rows * screen_Columns))
8495 return;
8496
8497 /* Outputting the last character on the screen may scrollup the screen.
8498 * Don't to it! Mark the character invalid (update it when scrolled up) */
8499 if (row == screen_Rows - 1 && col >= screen_Columns - 2)
8500 {
8501 ScreenAttrs[off] = (sattr_T)-1;
8502 return;
8503 }
8504
8505 /* Output the first byte normally (positions the cursor), then write the
8506 * second byte directly. */
8507 screen_char(off, row, col);
8508 out_char(ScreenLines[off + 1]);
8509 ++screen_cur_col;
8510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512/*
8513 * Draw a rectangle of the screen, inverted when "invert" is TRUE.
8514 * This uses the contents of ScreenLines[] and doesn't change it.
8515 */
8516 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008517screen_draw_rectangle(
8518 int row,
8519 int col,
8520 int height,
8521 int width,
8522 int invert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 int r, c;
8525 int off;
Bram Moolenaar367329b2007-08-30 11:53:22 +00008526 int max_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008528 /* Can't use ScreenLines unless initialized */
8529 if (ScreenLines == NULL)
8530 return;
8531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 if (invert)
8533 screen_char_attr = HL_INVERSE;
8534 for (r = row; r < row + height; ++r)
8535 {
8536 off = LineOffset[r];
Bram Moolenaar367329b2007-08-30 11:53:22 +00008537 max_off = off + screen_Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 for (c = col; c < col + width; ++c)
8539 {
Bram Moolenaar367329b2007-08-30 11:53:22 +00008540 if (enc_dbcs != 0 && dbcs_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
8542 screen_char_2(off + c, r, c);
8543 ++c;
8544 }
8545 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
8547 screen_char(off + c, r, c);
Bram Moolenaar367329b2007-08-30 11:53:22 +00008548 if (utf_off2cells(off + c, max_off) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 ++c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 }
8551 }
8552 }
8553 screen_char_attr = 0;
8554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556/*
8557 * Redraw the characters for a vertically split window.
8558 */
8559 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008560redraw_block(int row, int end, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 int col;
8563 int width;
8564
8565# ifdef FEAT_CLIPBOARD
8566 clip_may_clear_selection(row, end - 1);
8567# endif
8568
8569 if (wp == NULL)
8570 {
8571 col = 0;
8572 width = Columns;
8573 }
8574 else
8575 {
8576 col = wp->w_wincol;
8577 width = wp->w_width;
8578 }
8579 screen_draw_rectangle(row, col, end - row, width, FALSE);
8580}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008582 static void
8583space_to_screenline(int off, int attr)
8584{
8585 ScreenLines[off] = ' ';
8586 ScreenAttrs[off] = attr;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008587 if (enc_utf8)
8588 ScreenLinesUC[off] = 0;
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008589}
8590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591/*
8592 * Fill the screen from 'start_row' to 'end_row', from 'start_col' to 'end_col'
8593 * with character 'c1' in first column followed by 'c2' in the other columns.
8594 * Use attributes 'attr'.
8595 */
8596 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008597screen_fill(
8598 int start_row,
8599 int end_row,
8600 int start_col,
8601 int end_col,
8602 int c1,
8603 int c2,
8604 int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 int row;
8607 int col;
8608 int off;
8609 int end_off;
8610 int did_delete;
8611 int c;
8612 int norm_term;
8613#if defined(FEAT_GUI) || defined(UNIX)
8614 int force_next = FALSE;
8615#endif
8616
8617 if (end_row > screen_Rows) /* safety check */
8618 end_row = screen_Rows;
8619 if (end_col > screen_Columns) /* safety check */
8620 end_col = screen_Columns;
8621 if (ScreenLines == NULL
8622 || start_row >= end_row
8623 || start_col >= end_col) /* nothing to do */
8624 return;
8625
8626 /* it's a "normal" terminal when not in a GUI or cterm */
8627 norm_term = (
8628#ifdef FEAT_GUI
8629 !gui.in_use &&
8630#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +02008631 !IS_CTERM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 for (row = start_row; row < end_row; ++row)
8633 {
Bram Moolenaarc236c162008-07-13 17:41:49 +00008634 if (has_mbyte
Bram Moolenaara12a1612019-01-24 16:39:02 +01008635#ifdef FEAT_GUI
Bram Moolenaarc236c162008-07-13 17:41:49 +00008636 && !gui.in_use
Bram Moolenaara12a1612019-01-24 16:39:02 +01008637#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00008638 )
8639 {
8640 /* When drawing over the right halve of a double-wide char clear
8641 * out the left halve. When drawing over the left halve of a
8642 * double wide-char clear out the right halve. Only needed in a
8643 * terminal. */
Bram Moolenaar7693ec62008-07-24 18:29:37 +00008644 if (start_col > 0 && mb_fix_col(start_col, row) != start_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008645 screen_puts_len((char_u *)" ", 1, row, start_col - 1, 0);
Bram Moolenaara1aed622008-07-18 15:14:43 +00008646 if (end_col < screen_Columns && mb_fix_col(end_col, row) != end_col)
Bram Moolenaard91ffe92008-07-14 17:51:11 +00008647 screen_puts_len((char_u *)" ", 1, row, end_col, 0);
Bram Moolenaarc236c162008-07-13 17:41:49 +00008648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 /*
8650 * Try to use delete-line termcap code, when no attributes or in a
8651 * "normal" terminal, where a bold/italic space is just a
8652 * space.
8653 */
8654 did_delete = FALSE;
8655 if (c2 == ' '
8656 && end_col == Columns
8657 && can_clear(T_CE)
8658 && (attr == 0
8659 || (norm_term
8660 && attr <= HL_ALL
8661 && ((attr & ~(HL_BOLD | HL_ITALIC)) == 0))))
8662 {
8663 /*
8664 * check if we really need to clear something
8665 */
8666 col = start_col;
8667 if (c1 != ' ') /* don't clear first char */
8668 ++col;
8669
8670 off = LineOffset[row] + col;
8671 end_off = LineOffset[row] + end_col;
8672
8673 /* skip blanks (used often, keep it fast!) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 if (enc_utf8)
8675 while (off < end_off && ScreenLines[off] == ' '
8676 && ScreenAttrs[off] == 0 && ScreenLinesUC[off] == 0)
8677 ++off;
8678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 while (off < end_off && ScreenLines[off] == ' '
8680 && ScreenAttrs[off] == 0)
8681 ++off;
8682 if (off < end_off) /* something to be cleared */
8683 {
8684 col = off - LineOffset[row];
8685 screen_stop_highlight();
8686 term_windgoto(row, col);/* clear rest of this screen line */
8687 out_str(T_CE);
8688 screen_start(); /* don't know where cursor is now */
8689 col = end_col - col;
8690 while (col--) /* clear chars in ScreenLines */
8691 {
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008692 space_to_screenline(off, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 ++off;
8694 }
8695 }
8696 did_delete = TRUE; /* the chars are cleared now */
8697 }
8698
8699 off = LineOffset[row] + start_col;
8700 c = c1;
8701 for (col = start_col; col < end_col; ++col)
8702 {
8703 if (ScreenLines[off] != c
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008704 || (enc_utf8 && (int)ScreenLinesUC[off]
8705 != (c >= 0x80 ? c : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 || ScreenAttrs[off] != attr
8707#if defined(FEAT_GUI) || defined(UNIX)
8708 || force_next
8709#endif
8710 )
8711 {
8712#if defined(FEAT_GUI) || defined(UNIX)
8713 /* The bold trick may make a single row of pixels appear in
8714 * the next character. When a bold character is removed, the
8715 * next character should be redrawn too. This happens for our
8716 * own GUI and for some xterms. */
8717 if (
8718# ifdef FEAT_GUI
8719 gui.in_use
8720# endif
8721# if defined(FEAT_GUI) && defined(UNIX)
8722 ||
8723# endif
8724# ifdef UNIX
8725 term_is_xterm
8726# endif
8727 )
8728 {
8729 if (ScreenLines[off] != ' '
8730 && (ScreenAttrs[off] > HL_ALL
8731 || ScreenAttrs[off] & HL_BOLD))
8732 force_next = TRUE;
8733 else
8734 force_next = FALSE;
8735 }
8736#endif
8737 ScreenLines[off] = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 if (enc_utf8)
8739 {
8740 if (c >= 0x80)
8741 {
8742 ScreenLinesUC[off] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008743 ScreenLinesC[0][off] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 }
8745 else
8746 ScreenLinesUC[off] = 0;
8747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 ScreenAttrs[off] = attr;
8749 if (!did_delete || c != ' ')
8750 screen_char(off, row, col);
8751 }
8752 ++off;
8753 if (col == start_col)
8754 {
8755 if (did_delete)
8756 break;
8757 c = c2;
8758 }
8759 }
8760 if (end_col == Columns)
8761 LineWraps[row] = FALSE;
8762 if (row == Rows - 1) /* overwritten the command line */
8763 {
8764 redraw_cmdline = TRUE;
Bram Moolenaar5bab5552018-04-13 20:41:29 +02008765 if (start_col == 0 && end_col == Columns
8766 && c1 == ' ' && c2 == ' ' && attr == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 clear_cmdline = FALSE; /* command line has been cleared */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00008768 if (start_col == 0)
8769 mode_displayed = FALSE; /* mode cleared or overwritten */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 }
8771 }
8772}
8773
8774/*
8775 * Check if there should be a delay. Used before clearing or redrawing the
8776 * screen or the command line.
8777 */
8778 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008779check_for_delay(int check_msg_scroll)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 if ((emsg_on_display || (check_msg_scroll && msg_scroll))
8782 && !did_wait_return
8783 && emsg_silent == 0)
8784 {
8785 out_flush();
8786 ui_delay(1000L, TRUE);
8787 emsg_on_display = FALSE;
8788 if (check_msg_scroll)
8789 msg_scroll = FALSE;
8790 }
8791}
8792
8793/*
Bram Moolenaarca57ab52019-04-13 14:53:16 +02008794 * Init TabPageIdxs[] to zero: Clicking outside of tabs has no effect.
8795 */
8796 static void
8797clear_TabPageIdxs(void)
8798{
8799 int scol;
8800
8801 for (scol = 0; scol < Columns; ++scol)
8802 TabPageIdxs[scol] = 0;
8803}
8804
8805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 * screen_valid - allocate screen buffers if size changed
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008807 * If "doclear" is TRUE: clear screen if it has been resized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 * Returns TRUE if there is a valid screen to write to.
8809 * Returns FALSE when starting up and screen not initialized yet.
8810 */
8811 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008812screen_valid(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008814 screenalloc(doclear); /* allocate screen buffers if size changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 return (ScreenLines != NULL);
8816}
8817
8818/*
8819 * Resize the shell to Rows and Columns.
8820 * Allocate ScreenLines[] and associated items.
8821 *
8822 * There may be some time between setting Rows and Columns and (re)allocating
8823 * ScreenLines[]. This happens when starting up and when (manually) changing
8824 * the shell size. Always use screen_Rows and screen_Columns to access items
8825 * in ScreenLines[]. Use Rows and Columns for positioning text etc. where the
8826 * final size of the shell is needed.
8827 */
8828 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008829screenalloc(int doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 int new_row, old_row;
8832#ifdef FEAT_GUI
8833 int old_Rows;
8834#endif
8835 win_T *wp;
8836 int outofmem = FALSE;
8837 int len;
8838 schar_T *new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 u8char_T *new_ScreenLinesUC = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008840 u8char_T *new_ScreenLinesC[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 schar_T *new_ScreenLines2 = NULL;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008842 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 sattr_T *new_ScreenAttrs;
8844 unsigned *new_LineOffset;
8845 char_u *new_LineWraps;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008846 short *new_TabPageIdxs;
Bram Moolenaarf740b292006-02-16 22:11:02 +00008847 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 static int entered = FALSE; /* avoid recursiveness */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008849 static int done_outofmem_msg = FALSE; /* did outofmem message */
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008850 int retry_count = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851
Bram Moolenaar87e817c2009-02-22 20:13:39 +00008852retry:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 /*
8854 * Allocation of the screen buffers is done only when the size changes and
8855 * when Rows and Columns have been set and we have started doing full
8856 * screen stuff.
8857 */
8858 if ((ScreenLines != NULL
8859 && Rows == screen_Rows
8860 && Columns == screen_Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 && enc_utf8 == (ScreenLinesUC != NULL)
8862 && (enc_dbcs == DBCS_JPNU) == (ScreenLines2 != NULL)
Bram Moolenaara12a1612019-01-24 16:39:02 +01008863 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 || Rows == 0
8865 || Columns == 0
8866 || (!full_screen && ScreenLines == NULL))
8867 return;
8868
8869 /*
8870 * It's possible that we produce an out-of-memory message below, which
8871 * will cause this function to be called again. To break the loop, just
8872 * return here.
8873 */
8874 if (entered)
8875 return;
8876 entered = TRUE;
8877
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00008878 /*
8879 * Note that the window sizes are updated before reallocating the arrays,
8880 * thus we must not redraw here!
8881 */
8882 ++RedrawingDisabled;
8883
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 win_new_shellsize(); /* fit the windows in the new sized shell */
8885
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 comp_col(); /* recompute columns for shown command and ruler */
8887
8888 /*
8889 * We're changing the size of the screen.
8890 * - Allocate new arrays for ScreenLines and ScreenAttrs.
8891 * - Move lines from the old arrays into the new arrays, clear extra
8892 * lines (unless the screen is going to be cleared).
8893 * - Free the old arrays.
8894 *
8895 * If anything fails, make ScreenLines NULL, so we don't do anything!
8896 * Continuing with the old ScreenLines may result in a crash, because the
8897 * size is wrong.
8898 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00008899 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 win_free_lsize(wp);
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008901 if (aucmd_win != NULL)
8902 win_free_lsize(aucmd_win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008904 new_ScreenLines = (schar_T *)lalloc(
8905 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
Bram Moolenaar216b7102010-03-23 13:56:59 +01008906 vim_memset(new_ScreenLinesC, 0, sizeof(u8char_T *) * MAX_MCO);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (enc_utf8)
8908 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008909 new_ScreenLinesUC = (u8char_T *)lalloc(
8910 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008911 for (i = 0; i < p_mco; ++i)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008912 new_ScreenLinesC[i] = (u8char_T *)lalloc_clear(
8913 (Rows + 1) * Columns * sizeof(u8char_T), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 if (enc_dbcs == DBCS_JPNU)
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02008916 new_ScreenLines2 = (schar_T *)lalloc(
8917 (Rows + 1) * Columns * sizeof(schar_T), FALSE);
8918 new_ScreenAttrs = (sattr_T *)lalloc(
8919 (Rows + 1) * Columns * sizeof(sattr_T), FALSE);
8920 new_LineOffset = (unsigned *)lalloc(Rows * sizeof(unsigned), FALSE);
8921 new_LineWraps = (char_u *)lalloc(Rows * sizeof(char_u), FALSE);
8922 new_TabPageIdxs = (short *)lalloc(Columns * sizeof(short), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008924 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 {
8926 if (win_alloc_lines(wp) == FAIL)
8927 {
8928 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008929 goto give_up;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
8931 }
Bram Moolenaar5e9b4542009-07-29 14:24:36 +00008932 if (aucmd_win != NULL && aucmd_win->w_lines == NULL
8933 && win_alloc_lines(aucmd_win) == FAIL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008934 outofmem = TRUE;
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00008935give_up:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008937 for (i = 0; i < p_mco; ++i)
8938 if (new_ScreenLinesC[i] == NULL)
8939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 if (new_ScreenLines == NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008941 || (enc_utf8 && (new_ScreenLinesUC == NULL || i != p_mco))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 || (enc_dbcs == DBCS_JPNU && new_ScreenLines2 == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 || new_ScreenAttrs == NULL
8944 || new_LineOffset == NULL
8945 || new_LineWraps == NULL
Bram Moolenaarf740b292006-02-16 22:11:02 +00008946 || new_TabPageIdxs == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 || outofmem)
8948 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008949 if (ScreenLines != NULL || !done_outofmem_msg)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008950 {
8951 /* guess the size */
8952 do_outofmem_msg((long_u)((Rows + 1) * Columns));
8953
8954 /* Remember we did this to avoid getting outofmem messages over
8955 * and over again. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008956 done_outofmem_msg = TRUE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008957 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01008958 VIM_CLEAR(new_ScreenLines);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008959 VIM_CLEAR(new_ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008960 for (i = 0; i < p_mco; ++i)
Bram Moolenaard23a8232018-02-10 18:45:26 +01008961 VIM_CLEAR(new_ScreenLinesC[i]);
8962 VIM_CLEAR(new_ScreenLines2);
Bram Moolenaard23a8232018-02-10 18:45:26 +01008963 VIM_CLEAR(new_ScreenAttrs);
8964 VIM_CLEAR(new_LineOffset);
8965 VIM_CLEAR(new_LineWraps);
8966 VIM_CLEAR(new_TabPageIdxs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 }
8968 else
8969 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00008970 done_outofmem_msg = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 for (new_row = 0; new_row < Rows; ++new_row)
8973 {
8974 new_LineOffset[new_row] = new_row * Columns;
8975 new_LineWraps[new_row] = FALSE;
8976
8977 /*
8978 * If the screen is not going to be cleared, copy as much as
8979 * possible from the old screen to the new one and clear the rest
8980 * (used when resizing the window at the "--more--" prompt or when
8981 * executing an external command, for the GUI).
8982 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01008983 if (!doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
8985 (void)vim_memset(new_ScreenLines + new_row * Columns,
8986 ' ', (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 if (enc_utf8)
8988 {
8989 (void)vim_memset(new_ScreenLinesUC + new_row * Columns,
8990 0, (size_t)Columns * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008991 for (i = 0; i < p_mco; ++i)
8992 (void)vim_memset(new_ScreenLinesC[i]
8993 + new_row * Columns,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 0, (size_t)Columns * sizeof(u8char_T));
8995 }
8996 if (enc_dbcs == DBCS_JPNU)
8997 (void)vim_memset(new_ScreenLines2 + new_row * Columns,
8998 0, (size_t)Columns * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 (void)vim_memset(new_ScreenAttrs + new_row * Columns,
9000 0, (size_t)Columns * sizeof(sattr_T));
9001 old_row = new_row + (screen_Rows - Rows);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009002 if (old_row >= 0 && ScreenLines != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 {
9004 if (screen_Columns < Columns)
9005 len = screen_Columns;
9006 else
9007 len = Columns;
Bram Moolenaarf4d11452005-12-02 00:46:37 +00009008 /* When switching to utf-8 don't copy characters, they
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009009 * may be invalid now. Also when p_mco changes. */
9010 if (!(enc_utf8 && ScreenLinesUC == NULL)
9011 && p_mco == Screen_mco)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00009012 mch_memmove(new_ScreenLines + new_LineOffset[new_row],
9013 ScreenLines + LineOffset[old_row],
9014 (size_t)len * sizeof(schar_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009015 if (enc_utf8 && ScreenLinesUC != NULL
9016 && p_mco == Screen_mco)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 {
9018 mch_memmove(new_ScreenLinesUC + new_LineOffset[new_row],
9019 ScreenLinesUC + LineOffset[old_row],
9020 (size_t)len * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009021 for (i = 0; i < p_mco; ++i)
9022 mch_memmove(new_ScreenLinesC[i]
9023 + new_LineOffset[new_row],
9024 ScreenLinesC[i] + LineOffset[old_row],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 (size_t)len * sizeof(u8char_T));
9026 }
9027 if (enc_dbcs == DBCS_JPNU && ScreenLines2 != NULL)
9028 mch_memmove(new_ScreenLines2 + new_LineOffset[new_row],
9029 ScreenLines2 + LineOffset[old_row],
9030 (size_t)len * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 mch_memmove(new_ScreenAttrs + new_LineOffset[new_row],
9032 ScreenAttrs + LineOffset[old_row],
9033 (size_t)len * sizeof(sattr_T));
9034 }
9035 }
9036 }
9037 /* Use the last line of the screen for the current line. */
9038 current_ScreenLine = new_ScreenLines + Rows * Columns;
9039 }
9040
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009041 free_screenlines();
9042
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 ScreenLines = new_ScreenLines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 ScreenLinesUC = new_ScreenLinesUC;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009045 for (i = 0; i < p_mco; ++i)
9046 ScreenLinesC[i] = new_ScreenLinesC[i];
9047 Screen_mco = p_mco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 ScreenLines2 = new_ScreenLines2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 ScreenAttrs = new_ScreenAttrs;
9050 LineOffset = new_LineOffset;
9051 LineWraps = new_LineWraps;
Bram Moolenaarf740b292006-02-16 22:11:02 +00009052 TabPageIdxs = new_TabPageIdxs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 /* It's important that screen_Rows and screen_Columns reflect the actual
9055 * size of ScreenLines[]. Set them before calling anything. */
9056#ifdef FEAT_GUI
9057 old_Rows = screen_Rows;
9058#endif
9059 screen_Rows = Rows;
9060 screen_Columns = Columns;
9061
9062 must_redraw = CLEAR; /* need to clear the screen later */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01009063 if (doclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 screenclear2();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065#ifdef FEAT_GUI
9066 else if (gui.in_use
9067 && !gui.starting
9068 && ScreenLines != NULL
9069 && old_Rows != Rows)
9070 {
9071 (void)gui_redraw_block(0, 0, (int)Rows - 1, (int)Columns - 1, 0);
9072 /*
9073 * Adjust the position of the cursor, for when executing an external
9074 * command.
9075 */
9076 if (msg_row >= Rows) /* Rows got smaller */
9077 msg_row = Rows - 1; /* put cursor at last row */
9078 else if (Rows > old_Rows) /* Rows got bigger */
9079 msg_row += Rows - old_Rows; /* put cursor in same place */
9080 if (msg_col >= Columns) /* Columns got smaller */
9081 msg_col = Columns - 1; /* put cursor at last column */
9082 }
9083#endif
Bram Moolenaarca57ab52019-04-13 14:53:16 +02009084 clear_TabPageIdxs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 entered = FALSE;
Bram Moolenaara3f2ecd2006-07-11 21:01:01 +00009087 --RedrawingDisabled;
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009088
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009089 /*
9090 * Do not apply autocommands more than 3 times to avoid an endless loop
9091 * in case applying autocommands always changes Rows or Columns.
9092 */
9093 if (starting == 0 && ++retry_count <= 3)
9094 {
Bram Moolenaar7d47b6e2006-03-15 22:59:18 +00009095 apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar87e817c2009-02-22 20:13:39 +00009096 /* In rare cases, autocommands may have altered Rows or Columns,
9097 * jump back to check if we need to allocate the screen again. */
9098 goto retry;
9099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100}
9101
9102 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009103free_screenlines(void)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009104{
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009105 int i;
9106
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009107 vim_free(ScreenLinesUC);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009108 for (i = 0; i < Screen_mco; ++i)
9109 vim_free(ScreenLinesC[i]);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009110 vim_free(ScreenLines2);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009111 vim_free(ScreenLines);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009112 vim_free(ScreenAttrs);
9113 vim_free(LineOffset);
9114 vim_free(LineWraps);
Bram Moolenaarf740b292006-02-16 22:11:02 +00009115 vim_free(TabPageIdxs);
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00009116}
9117
9118 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009119screenclear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
9121 check_for_delay(FALSE);
9122 screenalloc(FALSE); /* allocate screen buffers if size changed */
9123 screenclear2(); /* clear the screen */
9124}
9125
9126 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009127screenclear2(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
9129 int i;
9130
9131 if (starting == NO_SCREEN || ScreenLines == NULL
9132#ifdef FEAT_GUI
9133 || (gui.in_use && gui.starting)
9134#endif
9135 )
9136 return;
9137
9138#ifdef FEAT_GUI
9139 if (!gui.in_use)
9140#endif
9141 screen_attr = -1; /* force setting the Normal colors */
9142 screen_stop_highlight(); /* don't want highlighting here */
9143
9144#ifdef FEAT_CLIPBOARD
9145 /* disable selection without redrawing it */
9146 clip_scroll_selection(9999);
9147#endif
9148
9149 /* blank out ScreenLines */
9150 for (i = 0; i < Rows; ++i)
9151 {
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009152 lineclear(LineOffset[i], (int)Columns, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 LineWraps[i] = FALSE;
9154 }
9155
9156 if (can_clear(T_CL))
9157 {
9158 out_str(T_CL); /* clear the display */
9159 clear_cmdline = FALSE;
Bram Moolenaard12f5c12006-01-25 22:10:52 +00009160 mode_displayed = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 }
9162 else
9163 {
9164 /* can't clear the screen, mark all chars with invalid attributes */
9165 for (i = 0; i < Rows; ++i)
9166 lineinvalid(LineOffset[i], (int)Columns);
9167 clear_cmdline = TRUE;
9168 }
9169
9170 screen_cleared = TRUE; /* can use contents of ScreenLines now */
9171
9172 win_rest_invalid(firstwin);
9173 redraw_cmdline = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00009174 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 if (must_redraw == CLEAR) /* no need to clear again */
9176 must_redraw = NOT_VALID;
9177 compute_cmdrow();
9178 msg_row = cmdline_row; /* put cursor on last line for messages */
9179 msg_col = 0;
9180 screen_start(); /* don't know where cursor is now */
9181 msg_scrolled = 0; /* can't scroll back */
9182 msg_didany = FALSE;
9183 msg_didout = FALSE;
9184}
9185
9186/*
9187 * Clear one line in ScreenLines.
9188 */
9189 static void
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009190lineclear(unsigned off, int width, int attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 (void)vim_memset(ScreenLines + off, ' ', (size_t)width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 if (enc_utf8)
9194 (void)vim_memset(ScreenLinesUC + off, 0,
9195 (size_t)width * sizeof(u8char_T));
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009196 (void)vim_memset(ScreenAttrs + off, attr, (size_t)width * sizeof(sattr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197}
9198
9199/*
9200 * Mark one line in ScreenLines invalid by setting the attributes to an
9201 * invalid value.
9202 */
9203 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009204lineinvalid(unsigned off, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
9206 (void)vim_memset(ScreenAttrs + off, -1, (size_t)width * sizeof(sattr_T));
9207}
9208
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209/*
9210 * Copy part of a Screenline for vertically split window "wp".
9211 */
9212 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009213linecopy(int to, int from, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 unsigned off_to = LineOffset[to] + wp->w_wincol;
9216 unsigned off_from = LineOffset[from] + wp->w_wincol;
9217
9218 mch_memmove(ScreenLines + off_to, ScreenLines + off_from,
9219 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 if (enc_utf8)
9221 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009222 int i;
9223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 mch_memmove(ScreenLinesUC + off_to, ScreenLinesUC + off_from,
9225 wp->w_width * sizeof(u8char_T));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009226 for (i = 0; i < p_mco; ++i)
9227 mch_memmove(ScreenLinesC[i] + off_to, ScreenLinesC[i] + off_from,
9228 wp->w_width * sizeof(u8char_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 }
9230 if (enc_dbcs == DBCS_JPNU)
9231 mch_memmove(ScreenLines2 + off_to, ScreenLines2 + off_from,
9232 wp->w_width * sizeof(schar_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 mch_memmove(ScreenAttrs + off_to, ScreenAttrs + off_from,
9234 wp->w_width * sizeof(sattr_T));
9235}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
9237/*
9238 * Return TRUE if clearing with term string "p" would work.
9239 * It can't work when the string is empty or it won't set the right background.
9240 */
9241 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009242can_clear(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243{
9244 return (*p != NUL && (t_colors <= 1
9245#ifdef FEAT_GUI
9246 || gui.in_use
9247#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +02009248#ifdef FEAT_TERMGUICOLORS
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02009249 || (p_tgc && cterm_normal_bg_gui_color == INVALCOLOR)
Bram Moolenaard18f6722016-06-17 13:18:49 +02009250 || (!p_tgc && cterm_normal_bg_color == 0)
9251#else
9252 || cterm_normal_bg_color == 0
Bram Moolenaar8a633e32016-04-21 21:10:14 +02009253#endif
Bram Moolenaard18f6722016-06-17 13:18:49 +02009254 || *T_UT != NUL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255}
9256
9257/*
9258 * Reset cursor position. Use whenever cursor was moved because of outputting
9259 * something directly to the screen (shell commands) or a terminal control
9260 * code.
9261 */
9262 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009263screen_start(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
9265 screen_cur_row = screen_cur_col = 9999;
9266}
9267
9268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 * Move the cursor to position "row","col" in the screen.
9270 * This tries to find the most efficient way to move, minimizing the number of
9271 * characters sent to the terminal.
9272 */
9273 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009274windgoto(int row, int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009276 sattr_T *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 int i;
9278 int plan;
9279 int cost;
9280 int wouldbe_col;
9281 int noinvcurs;
9282 char_u *bs;
9283 int goto_cost;
9284 int attr;
9285
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009286#define GOTO_COST 7 /* assume a term_windgoto() takes about 7 chars */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287#define HIGHL_COST 5 /* assume unhighlight takes 5 chars */
9288
9289#define PLAN_LE 1
9290#define PLAN_CR 2
9291#define PLAN_NL 3
9292#define PLAN_WRITE 4
9293 /* Can't use ScreenLines unless initialized */
9294 if (ScreenLines == NULL)
9295 return;
9296
9297 if (col != screen_cur_col || row != screen_cur_row)
9298 {
9299 /* Check for valid position. */
9300 if (row < 0) /* window without text lines? */
9301 row = 0;
9302 if (row >= screen_Rows)
9303 row = screen_Rows - 1;
9304 if (col >= screen_Columns)
9305 col = screen_Columns - 1;
9306
9307 /* check if no cursor movement is allowed in highlight mode */
9308 if (screen_attr && *T_MS == NUL)
9309 noinvcurs = HIGHL_COST;
9310 else
9311 noinvcurs = 0;
9312 goto_cost = GOTO_COST + noinvcurs;
9313
9314 /*
9315 * Plan how to do the positioning:
9316 * 1. Use CR to move it to column 0, same row.
9317 * 2. Use T_LE to move it a few columns to the left.
9318 * 3. Use NL to move a few lines down, column 0.
9319 * 4. Move a few columns to the right with T_ND or by writing chars.
9320 *
9321 * Don't do this if the cursor went beyond the last column, the cursor
9322 * position is unknown then (some terminals wrap, some don't )
9323 *
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00009324 * First check if the highlighting attributes allow us to write
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 * characters to move the cursor to the right.
9326 */
9327 if (row >= screen_cur_row && screen_cur_col < Columns)
9328 {
9329 /*
9330 * If the cursor is in the same row, bigger col, we can use CR
9331 * or T_LE.
9332 */
9333 bs = NULL; /* init for GCC */
9334 attr = screen_attr;
9335 if (row == screen_cur_row && col < screen_cur_col)
9336 {
9337 /* "le" is preferred over "bc", because "bc" is obsolete */
9338 if (*T_LE)
9339 bs = T_LE; /* "cursor left" */
9340 else
9341 bs = T_BC; /* "backspace character (old) */
9342 if (*bs)
9343 cost = (screen_cur_col - col) * (int)STRLEN(bs);
9344 else
9345 cost = 999;
9346 if (col + 1 < cost) /* using CR is less characters */
9347 {
9348 plan = PLAN_CR;
9349 wouldbe_col = 0;
9350 cost = 1; /* CR is just one character */
9351 }
9352 else
9353 {
9354 plan = PLAN_LE;
9355 wouldbe_col = col;
9356 }
9357 if (noinvcurs) /* will stop highlighting */
9358 {
9359 cost += noinvcurs;
9360 attr = 0;
9361 }
9362 }
9363
9364 /*
9365 * If the cursor is above where we want to be, we can use CR LF.
9366 */
9367 else if (row > screen_cur_row)
9368 {
9369 plan = PLAN_NL;
9370 wouldbe_col = 0;
9371 cost = (row - screen_cur_row) * 2; /* CR LF */
9372 if (noinvcurs) /* will stop highlighting */
9373 {
9374 cost += noinvcurs;
9375 attr = 0;
9376 }
9377 }
9378
9379 /*
9380 * If the cursor is in the same row, smaller col, just use write.
9381 */
9382 else
9383 {
9384 plan = PLAN_WRITE;
9385 wouldbe_col = screen_cur_col;
9386 cost = 0;
9387 }
9388
9389 /*
9390 * Check if any characters that need to be written have the
9391 * correct attributes. Also avoid UTF-8 characters.
9392 */
9393 i = col - wouldbe_col;
9394 if (i > 0)
9395 cost += i;
9396 if (cost < goto_cost && i > 0)
9397 {
9398 /*
9399 * Check if the attributes are correct without additionally
9400 * stopping highlighting.
9401 */
9402 p = ScreenAttrs + LineOffset[row] + wouldbe_col;
9403 while (i && *p++ == attr)
9404 --i;
9405 if (i != 0)
9406 {
9407 /*
9408 * Try if it works when highlighting is stopped here.
9409 */
9410 if (*--p == 0)
9411 {
9412 cost += noinvcurs;
9413 while (i && *p++ == 0)
9414 --i;
9415 }
9416 if (i != 0)
9417 cost = 999; /* different attributes, don't do it */
9418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 if (enc_utf8)
9420 {
9421 /* Don't use an UTF-8 char for positioning, it's slow. */
9422 for (i = wouldbe_col; i < col; ++i)
9423 if (ScreenLinesUC[LineOffset[row] + i] != 0)
9424 {
9425 cost = 999;
9426 break;
9427 }
9428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
9430
9431 /*
9432 * We can do it without term_windgoto()!
9433 */
9434 if (cost < goto_cost)
9435 {
9436 if (plan == PLAN_LE)
9437 {
9438 if (noinvcurs)
9439 screen_stop_highlight();
9440 while (screen_cur_col > col)
9441 {
9442 out_str(bs);
9443 --screen_cur_col;
9444 }
9445 }
9446 else if (plan == PLAN_CR)
9447 {
9448 if (noinvcurs)
9449 screen_stop_highlight();
9450 out_char('\r');
9451 screen_cur_col = 0;
9452 }
9453 else if (plan == PLAN_NL)
9454 {
9455 if (noinvcurs)
9456 screen_stop_highlight();
9457 while (screen_cur_row < row)
9458 {
9459 out_char('\n');
9460 ++screen_cur_row;
9461 }
9462 screen_cur_col = 0;
9463 }
9464
9465 i = col - screen_cur_col;
9466 if (i > 0)
9467 {
9468 /*
9469 * Use cursor-right if it's one character only. Avoids
9470 * removing a line of pixels from the last bold char, when
9471 * using the bold trick in the GUI.
9472 */
9473 if (T_ND[0] != NUL && T_ND[1] == NUL)
9474 {
9475 while (i-- > 0)
9476 out_char(*T_ND);
9477 }
9478 else
9479 {
9480 int off;
9481
9482 off = LineOffset[row] + screen_cur_col;
9483 while (i-- > 0)
9484 {
9485 if (ScreenAttrs[off] != screen_attr)
9486 screen_stop_highlight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 out_flush_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 out_char(ScreenLines[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 if (enc_dbcs == DBCS_JPNU
9490 && ScreenLines[off] == 0x8e)
9491 out_char(ScreenLines2[off]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 ++off;
9493 }
9494 }
9495 }
9496 }
9497 }
9498 else
9499 cost = 999;
9500
9501 if (cost >= goto_cost)
9502 {
9503 if (noinvcurs)
9504 screen_stop_highlight();
Bram Moolenaar597a4222014-06-25 14:39:50 +02009505 if (row == screen_cur_row && (col > screen_cur_col)
9506 && *T_CRI != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 term_cursor_right(col - screen_cur_col);
9508 else
9509 term_windgoto(row, col);
9510 }
9511 screen_cur_row = row;
9512 screen_cur_col = col;
9513 }
9514}
9515
9516/*
9517 * Set cursor to its position in the current window.
9518 */
9519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01009520setcursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521{
Bram Moolenaar987723e2018-03-06 11:43:04 +01009522 setcursor_mayforce(FALSE);
9523}
9524
9525/*
9526 * Set cursor to its position in the current window.
9527 * When "force" is TRUE also when not redrawing.
9528 */
9529 void
9530setcursor_mayforce(int force)
9531{
9532 if (force || redrawing())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
9534 validate_cursor();
9535 windgoto(W_WINROW(curwin) + curwin->w_wrow,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009536 curwin->w_wincol + (
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537#ifdef FEAT_RIGHTLEFT
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009538 /* With 'rightleft' set and the cursor on a double-wide
9539 * character, position it on the leftmost column. */
Bram Moolenaara12a1612019-01-24 16:39:02 +01009540 curwin->w_p_rl ? ((int)curwin->w_width - curwin->w_wcol
9541 - ((has_mbyte
Bram Moolenaar561f9db2008-02-20 13:16:29 +00009542 && (*mb_ptr2cells)(ml_get_cursor()) == 2
Bram Moolenaara12a1612019-01-24 16:39:02 +01009543 && vim_isprintc(gchar_cursor())) ? 2 : 1)) :
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544#endif
9545 curwin->w_wcol));
9546 }
9547}
9548
9549
9550/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009551 * Insert 'line_count' lines at 'row' in window 'wp'.
9552 * If 'invalid' is TRUE the wp->w_lines[].wl_lnum is invalidated.
9553 * If 'mayclear' is TRUE the screen will be cleared if it is faster than
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 * scrolling.
9555 * Returns FAIL if the lines are not inserted, OK for success.
9556 */
9557 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009558win_ins_lines(
9559 win_T *wp,
9560 int row,
9561 int line_count,
9562 int invalid,
9563 int mayclear)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 int did_delete;
9566 int nextrow;
9567 int lastrow;
9568 int retval;
9569
9570 if (invalid)
9571 wp->w_lines_valid = 0;
9572
9573 if (wp->w_height < 5)
9574 return FAIL;
9575
9576 if (line_count > wp->w_height - row)
9577 line_count = wp->w_height - row;
9578
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009579 retval = win_do_lines(wp, row, line_count, mayclear, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 if (retval != MAYBE)
9581 return retval;
9582
9583 /*
9584 * If there is a next window or a status line, we first try to delete the
9585 * lines at the bottom to avoid messing what is after the window.
9586 * If this fails and there are following windows, don't do anything to avoid
9587 * messing up those windows, better just redraw.
9588 */
9589 did_delete = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (wp->w_next != NULL || wp->w_status_height)
9591 {
9592 if (screen_del_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009593 line_count, (int)Rows, FALSE, 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 did_delete = TRUE;
9595 else if (wp->w_next)
9596 return FAIL;
9597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 /*
9599 * if no lines deleted, blank the lines that will end up below the window
9600 */
9601 if (!did_delete)
9602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 redraw_cmdline = TRUE;
Bram Moolenaare0de17d2017-09-24 16:24:34 +02009605 nextrow = W_WINROW(wp) + wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 lastrow = nextrow + line_count;
9607 if (lastrow > Rows)
9608 lastrow = Rows;
9609 screen_fill(nextrow - line_count, lastrow - line_count,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009610 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 ' ', ' ', 0);
9612 }
9613
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009614 if (screen_ins_lines(0, W_WINROW(wp) + row, line_count, (int)Rows, 0, NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 == FAIL)
9616 {
9617 /* deletion will have messed up other windows */
9618 if (did_delete)
9619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 win_rest_invalid(W_NEXT(wp));
9622 }
9623 return FAIL;
9624 }
9625
9626 return OK;
9627}
9628
9629/*
Bram Moolenaar86033562017-07-12 20:24:41 +02009630 * Delete "line_count" window lines at "row" in window "wp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 * If "invalid" is TRUE curwin->w_lines[] is invalidated.
9632 * If "mayclear" is TRUE the screen will be cleared if it is faster than
9633 * scrolling
9634 * Return OK for success, FAIL if the lines are not deleted.
9635 */
9636 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009637win_del_lines(
9638 win_T *wp,
9639 int row,
9640 int line_count,
9641 int invalid,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009642 int mayclear,
9643 int clear_attr) /* for clearing lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 int retval;
9646
9647 if (invalid)
9648 wp->w_lines_valid = 0;
9649
9650 if (line_count > wp->w_height - row)
9651 line_count = wp->w_height - row;
9652
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009653 retval = win_do_lines(wp, row, line_count, mayclear, TRUE, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (retval != MAYBE)
9655 return retval;
9656
9657 if (screen_del_lines(0, W_WINROW(wp) + row, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009658 (int)Rows, FALSE, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 return FAIL;
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661 /*
9662 * If there are windows or status lines below, try to put them at the
9663 * correct place. If we can't do that, they have to be redrawn.
9664 */
9665 if (wp->w_next || wp->w_status_height || cmdline_row < Rows - 1)
9666 {
9667 if (screen_ins_lines(0, W_WINROW(wp) + wp->w_height - line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009668 line_count, (int)Rows, clear_attr, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 {
9670 wp->w_redr_status = TRUE;
9671 win_rest_invalid(wp->w_next);
9672 }
9673 }
9674 /*
9675 * If this is the last window and there is no status line, redraw the
9676 * command line later.
9677 */
9678 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 redraw_cmdline = TRUE;
9680 return OK;
9681}
9682
9683/*
9684 * Common code for win_ins_lines() and win_del_lines().
9685 * Returns OK or FAIL when the work has been done.
9686 * Returns MAYBE when not finished yet.
9687 */
9688 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01009689win_do_lines(
9690 win_T *wp,
9691 int row,
9692 int line_count,
9693 int mayclear,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009694 int del,
9695 int clear_attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697 int retval;
9698
9699 if (!redrawing() || line_count <= 0)
9700 return FAIL;
9701
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009702 /* When inserting lines would result in loss of command output, just redraw
9703 * the lines. */
9704 if (no_win_do_lines_ins && !del)
9705 return FAIL;
9706
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 /* only a few lines left: redraw is faster */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009708 if (mayclear && Rows - line_count < 5 && wp->w_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 {
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009710 if (!no_win_do_lines_ins)
9711 screenclear(); /* will set wp->w_lines_valid to 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 return FAIL;
9713 }
9714
9715 /*
9716 * Delete all remaining lines
9717 */
9718 if (row + line_count >= wp->w_height)
9719 {
9720 screen_fill(W_WINROW(wp) + row, W_WINROW(wp) + wp->w_height,
Bram Moolenaar53f81742017-09-22 14:35:51 +02009721 wp->w_wincol, (int)W_ENDCOL(wp),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 ' ', ' ', 0);
9723 return OK;
9724 }
9725
9726 /*
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009727 * When scrolling, the message on the command line should be cleared,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 * otherwise it will stay there forever.
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009729 * Don't do this when avoiding to insert lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 */
Bram Moolenaar29ae3772017-04-30 19:39:39 +02009731 if (!no_win_do_lines_ins)
9732 clear_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733
9734 /*
9735 * If the terminal can set a scroll region, use that.
9736 * Always do this in a vertically split window. This will redraw from
9737 * ScreenLines[] when t_CV isn't defined. That's faster than using
9738 * win_line().
9739 * Don't use a scroll region when we are going to redraw the text, writing
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009740 * a character in the lower right corner of the scroll region may cause a
9741 * scroll-up .
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 */
Bram Moolenaar02631462017-09-22 15:20:32 +02009743 if (scroll_region || wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 scroll_region_set(wp, row);
9747 if (del)
9748 retval = screen_del_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009749 wp->w_height - row, FALSE, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 else
9751 retval = screen_ins_lines(W_WINROW(wp) + row, 0, line_count,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009752 wp->w_height - row, clear_attr, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 if (scroll_region && (wp->w_width == Columns || *T_CSV != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 scroll_region_reset();
9755 return retval;
9756 }
9757
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (wp->w_next != NULL && p_tf) /* don't delete/insert on fast terminal */
9759 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760
9761 return MAYBE;
9762}
9763
9764/*
9765 * window 'wp' and everything after it is messed up, mark it for redraw
9766 */
9767 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01009768win_rest_invalid(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 while (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 {
9772 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 wp->w_redr_status = TRUE;
9774 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 }
9776 redraw_cmdline = TRUE;
9777}
9778
9779/*
9780 * The rest of the routines in this file perform screen manipulations. The
9781 * given operation is performed physically on the screen. The corresponding
9782 * change is also made to the internal screen image. In this way, the editor
9783 * anticipates the effect of editing changes on the appearance of the screen.
9784 * That way, when we call screenupdate a complete redraw isn't usually
9785 * necessary. Another advantage is that we can keep adding code to anticipate
9786 * screen changes, and in the meantime, everything still works.
9787 */
9788
9789/*
9790 * types for inserting or deleting lines
9791 */
9792#define USE_T_CAL 1
9793#define USE_T_CDL 2
9794#define USE_T_AL 3
9795#define USE_T_CE 4
9796#define USE_T_DL 5
9797#define USE_T_SR 6
9798#define USE_NL 7
9799#define USE_T_CD 8
9800#define USE_REDRAW 9
9801
9802/*
9803 * insert lines on the screen and update ScreenLines[]
9804 * 'end' is the line after the scrolled part. Normally it is Rows.
9805 * When scrolling region used 'off' is the offset from the top for the region.
9806 * 'row' and 'end' are relative to the start of the region.
9807 *
9808 * return FAIL for failure, OK for success.
9809 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00009810 int
Bram Moolenaar05540972016-01-30 20:31:25 +01009811screen_ins_lines(
9812 int off,
9813 int row,
9814 int line_count,
9815 int end,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009816 int clear_attr,
Bram Moolenaar05540972016-01-30 20:31:25 +01009817 win_T *wp) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 int i;
9820 int j;
9821 unsigned temp;
9822 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009823 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 int type;
9825 int result_empty;
9826 int can_ce = can_clear(T_CE);
9827
9828 /*
9829 * FAIL if
9830 * - there is no valid screen
9831 * - the screen has to be redrawn completely
9832 * - the line count is less than one
9833 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009834 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +02009836 if (!screen_valid(TRUE) || line_count <= 0 || line_count > p_ttyscroll
9837#ifdef FEAT_CLIPBOARD
9838 || (clip_star.state != SELECT_CLEARED
9839 && redrawing_for_callback > 0)
9840#endif
9841 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 return FAIL;
9843
9844 /*
9845 * There are seven ways to insert lines:
9846 * 0. When in a vertically split window and t_CV isn't set, redraw the
9847 * characters from ScreenLines[].
9848 * 1. Use T_CD (clear to end of display) if it exists and the result of
9849 * the insert is just empty lines
9850 * 2. Use T_CAL (insert multiple lines) if it exists and T_AL is not
9851 * present or line_count > 1. It looks better if we do all the inserts
9852 * at once.
9853 * 3. Use T_CDL (delete multiple lines) if it exists and the result of the
9854 * insert is just empty lines and T_CE is not present or line_count >
9855 * 1.
9856 * 4. Use T_AL (insert line) if it exists.
9857 * 5. Use T_CE (erase line) if it exists and the result of the insert is
9858 * just empty lines.
9859 * 6. Use T_DL (delete line) if it exists and the result of the insert is
9860 * just empty lines.
9861 * 7. Use T_SR (scroll reverse) if it exists and inserting at row 0 and
9862 * the 'da' flag is not set or we have clear line capability.
9863 * 8. redraw the characters from ScreenLines[].
9864 *
9865 * Careful: In a hpterm scroll reverse doesn't work as expected, it moves
9866 * the scrollbar for the window. It does have insert line, use that if it
9867 * exists.
9868 */
9869 result_empty = (row + line_count >= end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
9871 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +02009872 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 type = USE_T_CD;
9874 else if (*T_CAL != NUL && (line_count > 1 || *T_AL == NUL))
9875 type = USE_T_CAL;
9876 else if (*T_CDL != NUL && result_empty && (line_count > 1 || !can_ce))
9877 type = USE_T_CDL;
9878 else if (*T_AL != NUL)
9879 type = USE_T_AL;
9880 else if (can_ce && result_empty)
9881 type = USE_T_CE;
9882 else if (*T_DL != NUL && result_empty)
9883 type = USE_T_DL;
9884 else if (*T_SR != NUL && row == 0 && (*T_DA == NUL || can_ce))
9885 type = USE_T_SR;
9886 else
9887 return FAIL;
9888
9889 /*
9890 * For clearing the lines screen_del_lines() is used. This will also take
9891 * care of t_db if necessary.
9892 */
9893 if (type == USE_T_CD || type == USE_T_CDL ||
9894 type == USE_T_CE || type == USE_T_DL)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009895 return screen_del_lines(off, row, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896
9897 /*
9898 * If text is retained below the screen, first clear or delete as many
9899 * lines at the bottom of the window as are about to be inserted so that
9900 * the deleted lines won't later surface during a screen_del_lines.
9901 */
9902 if (*T_DB)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009903 screen_del_lines(off, end - line_count, line_count, end, FALSE, 0, wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
9905#ifdef FEAT_CLIPBOARD
9906 /* Remove a modeless selection when inserting lines halfway the screen
9907 * or not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +02009908 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02009909 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 else
9911 clip_scroll_selection(-line_count);
9912#endif
9913
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914#ifdef FEAT_GUI
9915 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
9916 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02009917 gui_dont_update_cursor(row + off <= gui.cursor_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918#endif
9919
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009920 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
9921 cursor_col = wp->w_wincol;
9922
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923 if (*T_CCS != NUL) /* cursor relative to region */
9924 cursor_row = row;
9925 else
9926 cursor_row = row + off;
9927
9928 /*
9929 * Shift LineOffset[] line_count down to reflect the inserted lines.
9930 * Clear the inserted lines in ScreenLines[].
9931 */
9932 row += off;
9933 end += off;
9934 for (i = 0; i < line_count; ++i)
9935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (wp != NULL && wp->w_width != Columns)
9937 {
9938 /* need to copy part of a line */
9939 j = end - 1 - i;
9940 while ((j -= line_count) >= row)
9941 linecopy(j + line_count, j, wp);
9942 j += line_count;
9943 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009944 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
9945 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 else
9947 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
9948 LineWraps[j] = FALSE;
9949 }
9950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 {
9952 j = end - 1 - i;
9953 temp = LineOffset[j];
9954 while ((j -= line_count) >= row)
9955 {
9956 LineOffset[j + line_count] = LineOffset[j];
9957 LineWraps[j + line_count] = LineWraps[j];
9958 }
9959 LineOffset[j + line_count] = temp;
9960 LineWraps[j + line_count] = FALSE;
9961 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009962 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 else
9964 lineinvalid(temp, (int)Columns);
9965 }
9966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967
9968 screen_stop_highlight();
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009969 windgoto(cursor_row, cursor_col);
Bram Moolenaarcfce7172017-08-17 20:31:48 +02009970 if (clear_attr != 0)
9971 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 /* redraw the characters */
9974 if (type == USE_REDRAW)
9975 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +02009976 else if (type == USE_T_CAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 {
9978 term_append_lines(line_count);
9979 screen_start(); /* don't know where cursor is now */
9980 }
9981 else
9982 {
9983 for (i = 0; i < line_count; i++)
9984 {
9985 if (type == USE_T_AL)
9986 {
9987 if (i && cursor_row != 0)
Bram Moolenaarbfa42462018-06-16 16:20:52 +02009988 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 out_str(T_AL);
9990 }
9991 else /* type == USE_T_SR */
9992 out_str(T_SR);
9993 screen_start(); /* don't know where cursor is now */
9994 }
9995 }
9996
9997 /*
9998 * With scroll-reverse and 'da' flag set we need to clear the lines that
9999 * have been scrolled down into the region.
10000 */
10001 if (type == USE_T_SR && *T_DA)
10002 {
10003 for (i = 0; i < line_count; ++i)
10004 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010005 windgoto(off + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 out_str(T_CE);
10007 screen_start(); /* don't know where cursor is now */
10008 }
10009 }
10010
10011#ifdef FEAT_GUI
10012 gui_can_update_cursor();
10013 if (gui.in_use)
10014 out_flush(); /* always flush after a scroll */
10015#endif
10016 return OK;
10017}
10018
10019/*
Bram Moolenaar107abd22016-08-12 14:08:25 +020010020 * Delete lines on the screen and update ScreenLines[].
10021 * "end" is the line after the scrolled part. Normally it is Rows.
10022 * When scrolling region used "off" is the offset from the top for the region.
10023 * "row" and "end" are relative to the start of the region.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 *
10025 * Return OK for success, FAIL if the lines are not deleted.
10026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010028screen_del_lines(
10029 int off,
10030 int row,
10031 int line_count,
10032 int end,
10033 int force, /* even when line_count > p_ttyscroll */
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010034 int clear_attr, /* used for clearing lines */
Bram Moolenaar05540972016-01-30 20:31:25 +010010035 win_T *wp UNUSED) /* NULL or window to use width from */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 int j;
10038 int i;
10039 unsigned temp;
10040 int cursor_row;
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010041 int cursor_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 int cursor_end;
10043 int result_empty; /* result is empty until end of region */
10044 int can_delete; /* deleting line codes can be used */
10045 int type;
10046
10047 /*
10048 * FAIL if
10049 * - there is no valid screen
10050 * - the screen has to be redrawn completely
10051 * - the line count is less than one
10052 * - the line count is more than 'ttyscroll'
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010053 * - redrawing for a callback and there is a modeless selection
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 */
Bram Moolenaar80dd3f92017-07-19 12:51:52 +020010055 if (!screen_valid(TRUE) || line_count <= 0
10056 || (!force && line_count > p_ttyscroll)
10057#ifdef FEAT_CLIPBOARD
10058 || (clip_star.state != SELECT_CLEARED
10059 && redrawing_for_callback > 0)
10060#endif
10061 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 return FAIL;
10063
10064 /*
10065 * Check if the rest of the current region will become empty.
10066 */
10067 result_empty = row + line_count >= end;
10068
10069 /*
10070 * We can delete lines only when 'db' flag not set or when 'ce' option
10071 * available.
10072 */
10073 can_delete = (*T_DB == NUL || can_clear(T_CE));
10074
10075 /*
10076 * There are six ways to delete lines:
10077 * 0. When in a vertically split window and t_CV isn't set, redraw the
10078 * characters from ScreenLines[].
10079 * 1. Use T_CD if it exists and the result is empty.
10080 * 2. Use newlines if row == 0 and count == 1 or T_CDL does not exist.
10081 * 3. Use T_CDL (delete multiple lines) if it exists and line_count > 1 or
10082 * none of the other ways work.
10083 * 4. Use T_CE (erase line) if the result is empty.
10084 * 5. Use T_DL (delete line) if it exists.
10085 * 6. redraw the characters from ScreenLines[].
10086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 if (wp != NULL && wp->w_width != Columns && *T_CSV == NUL)
10088 type = USE_REDRAW;
Bram Moolenaar4033c552017-09-16 20:54:51 +020010089 else if (can_clear(T_CD) && result_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 type = USE_T_CD;
10091#if defined(__BEOS__) && defined(BEOS_DR8)
10092 /*
10093 * USE_NL does not seem to work in Terminal of DR8 so we set T_DB="" in
10094 * its internal termcap... this works okay for tests which test *T_DB !=
10095 * NUL. It has the disadvantage that the user cannot use any :set t_*
10096 * command to get T_DB (back) to empty_option, only :set term=... will do
10097 * the trick...
10098 * Anyway, this hack will hopefully go away with the next OS release.
10099 * (Olaf Seibert)
10100 */
10101 else if (row == 0 && T_DB == empty_option
10102 && (line_count == 1 || *T_CDL == NUL))
10103#else
10104 else if (row == 0 && (
10105#ifndef AMIGA
10106 /* On the Amiga, somehow '\n' on the last line doesn't always scroll
10107 * up, so use delete-line command */
10108 line_count == 1 ||
10109#endif
10110 *T_CDL == NUL))
10111#endif
10112 type = USE_NL;
10113 else if (*T_CDL != NUL && line_count > 1 && can_delete)
10114 type = USE_T_CDL;
10115 else if (can_clear(T_CE) && result_empty
Bram Moolenaar4033c552017-09-16 20:54:51 +020010116 && (wp == NULL || wp->w_width == Columns))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 type = USE_T_CE;
10118 else if (*T_DL != NUL && can_delete)
10119 type = USE_T_DL;
10120 else if (*T_CDL != NUL && can_delete)
10121 type = USE_T_CDL;
10122 else
10123 return FAIL;
10124
10125#ifdef FEAT_CLIPBOARD
10126 /* Remove a modeless selection when deleting lines halfway the screen or
10127 * not the full width of the screen. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010128 if (off + row > 0 || (wp != NULL && wp->w_width != Columns))
Bram Moolenaarc0885aa2012-07-10 16:49:23 +020010129 clip_clear_selection(&clip_star);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 else
10131 clip_scroll_selection(line_count);
10132#endif
10133
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134#ifdef FEAT_GUI
10135 /* Don't update the GUI cursor here, ScreenLines[] is invalid until the
10136 * scrolling is actually carried out. */
Bram Moolenaar107abd22016-08-12 14:08:25 +020010137 gui_dont_update_cursor(gui.cursor_row >= row + off
10138 && gui.cursor_row < end + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139#endif
10140
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010141 if (wp != NULL && wp->w_wincol != 0 && *T_CSV != NUL && *T_CCS == NUL)
10142 cursor_col = wp->w_wincol;
10143
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (*T_CCS != NUL) /* cursor relative to region */
10145 {
10146 cursor_row = row;
10147 cursor_end = end;
10148 }
10149 else
10150 {
10151 cursor_row = row + off;
10152 cursor_end = end + off;
10153 }
10154
10155 /*
10156 * Now shift LineOffset[] line_count up to reflect the deleted lines.
10157 * Clear the inserted lines in ScreenLines[].
10158 */
10159 row += off;
10160 end += off;
10161 for (i = 0; i < line_count; ++i)
10162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 if (wp != NULL && wp->w_width != Columns)
10164 {
10165 /* need to copy part of a line */
10166 j = row + i;
10167 while ((j += line_count) <= end - 1)
10168 linecopy(j - line_count, j, wp);
10169 j -= line_count;
10170 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010171 lineclear(LineOffset[j] + wp->w_wincol, wp->w_width,
10172 clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 else
10174 lineinvalid(LineOffset[j] + wp->w_wincol, wp->w_width);
10175 LineWraps[j] = FALSE;
10176 }
10177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 {
10179 /* whole width, moving the line pointers is faster */
10180 j = row + i;
10181 temp = LineOffset[j];
10182 while ((j += line_count) <= end - 1)
10183 {
10184 LineOffset[j - line_count] = LineOffset[j];
10185 LineWraps[j - line_count] = LineWraps[j];
10186 }
10187 LineOffset[j - line_count] = temp;
10188 LineWraps[j - line_count] = FALSE;
10189 if (can_clear((char_u *)" "))
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010190 lineclear(temp, (int)Columns, clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 else
10192 lineinvalid(temp, (int)Columns);
10193 }
10194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195
Bram Moolenaarcfce7172017-08-17 20:31:48 +020010196 if (screen_attr != clear_attr)
10197 screen_stop_highlight();
10198 if (clear_attr != 0)
10199 screen_start_highlight(clear_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 /* redraw the characters */
10202 if (type == USE_REDRAW)
10203 redraw_block(row, end, wp);
Bram Moolenaar4033c552017-09-16 20:54:51 +020010204 else if (type == USE_T_CD) /* delete the lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010206 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 out_str(T_CD);
10208 screen_start(); /* don't know where cursor is now */
10209 }
10210 else if (type == USE_T_CDL)
10211 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010212 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 term_delete_lines(line_count);
10214 screen_start(); /* don't know where cursor is now */
10215 }
10216 /*
10217 * Deleting lines at top of the screen or scroll region: Just scroll
10218 * the whole screen (scroll region) up by outputting newlines on the
10219 * last line.
10220 */
10221 else if (type == USE_NL)
10222 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010223 windgoto(cursor_end - 1, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 for (i = line_count; --i >= 0; )
10225 out_char('\n'); /* cursor will remain on same line */
10226 }
10227 else
10228 {
10229 for (i = line_count; --i >= 0; )
10230 {
10231 if (type == USE_T_DL)
10232 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010233 windgoto(cursor_row, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 out_str(T_DL); /* delete a line */
10235 }
10236 else /* type == USE_T_CE */
10237 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010238 windgoto(cursor_row + i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 out_str(T_CE); /* erase a line */
10240 }
10241 screen_start(); /* don't know where cursor is now */
10242 }
10243 }
10244
10245 /*
10246 * If the 'db' flag is set, we need to clear the lines that have been
10247 * scrolled up at the bottom of the region.
10248 */
10249 if (*T_DB && (type == USE_T_DL || type == USE_T_CDL))
10250 {
10251 for (i = line_count; i > 0; --i)
10252 {
Bram Moolenaarbfa42462018-06-16 16:20:52 +020010253 windgoto(cursor_end - i, cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 out_str(T_CE); /* erase a line */
10255 screen_start(); /* don't know where cursor is now */
10256 }
10257 }
10258
10259#ifdef FEAT_GUI
10260 gui_can_update_cursor();
10261 if (gui.in_use)
10262 out_flush(); /* always flush after a scroll */
10263#endif
10264
10265 return OK;
10266}
10267
10268/*
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010269 * Return TRUE when postponing displaying the mode message: when not redrawing
10270 * or inside a mapping.
10271 */
10272 int
10273skip_showmode()
10274{
10275 // Call char_avail() only when we are going to show something, because it
10276 // takes a bit of time. redrawing() may also call char_avail_avail().
10277 if (global_busy
10278 || msg_silent != 0
10279 || !redrawing()
10280 || (char_avail() && !KeyTyped))
10281 {
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010282 redraw_mode = TRUE; // show mode later
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010283 return TRUE;
10284 }
10285 return FALSE;
10286}
10287
10288/*
Bram Moolenaar81226e02018-02-20 21:44:45 +010010289 * Show the current mode and ruler.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 *
10291 * If clear_cmdline is TRUE, clear the rest of the cmdline.
10292 * If clear_cmdline is FALSE there may be a message there that needs to be
10293 * cleared only if a mode is shown.
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010294 * If redraw_mode is TRUE show or clear the mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 * Return the length of the message (0 if no message).
10296 */
10297 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010298showmode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299{
10300 int need_clear;
10301 int length = 0;
10302 int do_mode;
10303 int attr;
10304 int nwr_save;
10305#ifdef FEAT_INS_EXPAND
10306 int sub_attr;
10307#endif
10308
Bram Moolenaar7df351e2006-01-23 22:30:28 +000010309 do_mode = ((p_smd && msg_silent == 0)
10310 && ((State & INSERT)
Bram Moolenaar942b4542018-06-17 16:23:34 +020010311 || restart_edit != NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010010312 || VIsual_active));
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010313 if (do_mode || reg_recording != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 {
Bram Moolenaarcb574f42019-01-25 22:29:57 +010010315 if (skip_showmode())
10316 return 0; // show mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317
10318 nwr_save = need_wait_return;
10319
10320 /* wait a bit before overwriting an important message */
10321 check_for_delay(FALSE);
10322
10323 /* if the cmdline is more than one line high, erase top lines */
10324 need_clear = clear_cmdline;
10325 if (clear_cmdline && cmdline_row < Rows - 1)
10326 msg_clr_cmdline(); /* will reset clear_cmdline */
10327
10328 /* Position on the last line in the window, column 0 */
10329 msg_pos_mode();
10330 cursor_off();
Bram Moolenaar8820b482017-03-16 17:23:31 +010010331 attr = HL_ATTR(HLF_CM); /* Highlight mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 if (do_mode)
10333 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010334 msg_puts_attr("--", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335#if defined(FEAT_XIM)
Bram Moolenaarc236c162008-07-13 17:41:49 +000010336 if (
Bram Moolenaar09092152010-08-08 16:38:42 +020010337# ifdef FEAT_GUI_GTK
Bram Moolenaarc236c162008-07-13 17:41:49 +000010338 preedit_get_status()
Bram Moolenaar09092152010-08-08 16:38:42 +020010339# else
Bram Moolenaarc236c162008-07-13 17:41:49 +000010340 im_get_status()
Bram Moolenaarc236c162008-07-13 17:41:49 +000010341# endif
Bram Moolenaar09092152010-08-08 16:38:42 +020010342 )
Bram Moolenaar0eda7ac2010-06-26 05:38:18 +020010343# ifdef FEAT_GUI_GTK /* most of the time, it's not XIM being used */
Bram Moolenaar32526b32019-01-19 17:43:09 +010010344 msg_puts_attr(" IM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345# else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010346 msg_puts_attr(" XIM", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347# endif
10348#endif
10349#if defined(FEAT_HANGULIN) && defined(FEAT_GUI)
10350 if (gui.in_use)
10351 {
10352 if (hangul_input_state_get())
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010353 {
10354 /* HANGUL */
10355 if (enc_utf8)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010356 msg_puts_attr(" \355\225\234\352\270\200", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010357 else
Bram Moolenaar32526b32019-01-19 17:43:09 +010010358 msg_puts_attr(" \307\321\261\333", attr);
Bram Moolenaar72f4cc42015-11-10 14:35:18 +010010359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 }
10361#endif
10362#ifdef FEAT_INS_EXPAND
Bram Moolenaarea389e92014-05-28 21:40:52 +020010363 /* CTRL-X in Insert mode */
10364 if (edit_submode != NULL && !shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 {
10366 /* These messages can get long, avoid a wrap in a narrow
10367 * window. Prefer showing edit_submode_extra. */
10368 length = (Rows - msg_row) * Columns - 3;
10369 if (edit_submode_extra != NULL)
10370 length -= vim_strsize(edit_submode_extra);
10371 if (length > 0)
10372 {
10373 if (edit_submode_pre != NULL)
10374 length -= vim_strsize(edit_submode_pre);
10375 if (length - vim_strsize(edit_submode) > 0)
10376 {
10377 if (edit_submode_pre != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010378 msg_puts_attr((char *)edit_submode_pre, attr);
10379 msg_puts_attr((char *)edit_submode, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 }
10381 if (edit_submode_extra != NULL)
10382 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010383 msg_puts_attr(" ", attr); /* add a space in between */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 if ((int)edit_submode_highl < (int)HLF_COUNT)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010385 sub_attr = HL_ATTR(edit_submode_highl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386 else
10387 sub_attr = attr;
Bram Moolenaar32526b32019-01-19 17:43:09 +010010388 msg_puts_attr((char *)edit_submode_extra, sub_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 }
10390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 }
10392 else
10393#endif
10394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395 if (State & VREPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010396 msg_puts_attr(_(" VREPLACE"), attr);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +020010397 else if (State & REPLACE_FLAG)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010398 msg_puts_attr(_(" REPLACE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 else if (State & INSERT)
10400 {
10401#ifdef FEAT_RIGHTLEFT
10402 if (p_ri)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010403 msg_puts_attr(_(" REVERSE"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +010010405 msg_puts_attr(_(" INSERT"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406 }
Bram Moolenaar942b4542018-06-17 16:23:34 +020010407 else if (restart_edit == 'I' || restart_edit == 'A')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010408 msg_puts_attr(_(" (insert)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 else if (restart_edit == 'R')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010410 msg_puts_attr(_(" (replace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 else if (restart_edit == 'V')
Bram Moolenaar32526b32019-01-19 17:43:09 +010010412 msg_puts_attr(_(" (vreplace)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413#ifdef FEAT_RIGHTLEFT
10414 if (p_hkmap)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010415 msg_puts_attr(_(" Hebrew"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416#endif
10417#ifdef FEAT_KEYMAP
10418 if (State & LANGMAP)
10419 {
10420# ifdef FEAT_ARABIC
10421 if (curwin->w_p_arab)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010422 msg_puts_attr(_(" Arabic"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 else
10424# endif
Bram Moolenaar73ac0c42016-07-24 16:17:59 +020010425 if (get_keymap_str(curwin, (char_u *)" (%s)",
10426 NameBuff, MAXPATHL))
Bram Moolenaar32526b32019-01-19 17:43:09 +010010427 msg_puts_attr((char *)NameBuff, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429#endif
10430 if ((State & INSERT) && p_paste)
Bram Moolenaar32526b32019-01-19 17:43:09 +010010431 msg_puts_attr(_(" (paste)"), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 if (VIsual_active)
10434 {
10435 char *p;
10436
10437 /* Don't concatenate separate words to avoid translation
10438 * problems. */
10439 switch ((VIsual_select ? 4 : 0)
10440 + (VIsual_mode == Ctrl_V) * 2
10441 + (VIsual_mode == 'V'))
10442 {
10443 case 0: p = N_(" VISUAL"); break;
10444 case 1: p = N_(" VISUAL LINE"); break;
10445 case 2: p = N_(" VISUAL BLOCK"); break;
10446 case 4: p = N_(" SELECT"); break;
10447 case 5: p = N_(" SELECT LINE"); break;
10448 default: p = N_(" SELECT BLOCK"); break;
10449 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010450 msg_puts_attr(_(p), attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451 }
Bram Moolenaar32526b32019-01-19 17:43:09 +010010452 msg_puts_attr(" --", attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010454
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 need_clear = TRUE;
10456 }
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010457 if (reg_recording != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458#ifdef FEAT_INS_EXPAND
10459 && edit_submode == NULL /* otherwise it gets too long */
10460#endif
10461 )
10462 {
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010463 recording_mode(attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 need_clear = TRUE;
10465 }
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010466
10467 mode_displayed = TRUE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010468 if (need_clear || clear_cmdline || redraw_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 msg_clr_eos();
10470 msg_didout = FALSE; /* overwrite this message */
10471 length = msg_col;
10472 msg_col = 0;
10473 need_wait_return = nwr_save; /* never ask for hit-return for this */
10474 }
10475 else if (clear_cmdline && msg_silent == 0)
10476 /* Clear the whole command line. Will reset "clear_cmdline". */
10477 msg_clr_cmdline();
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010478 else if (redraw_mode)
10479 {
10480 msg_pos_mode();
10481 msg_clr_eos();
10482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483
10484#ifdef FEAT_CMDL_INFO
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 /* In Visual mode the size of the selected area must be redrawn. */
10486 if (VIsual_active)
10487 clear_showcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488
10489 /* If the last window has no status line, the ruler is after the mode
10490 * message and must be redrawn */
Bram Moolenaar4033c552017-09-16 20:54:51 +020010491 if (redrawing() && lastwin->w_status_height == 0)
Bram Moolenaar491ac282018-06-17 14:47:55 +020010492 win_redr_ruler(lastwin, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493#endif
10494 redraw_cmdline = FALSE;
Bram Moolenaar4c25bd72019-04-20 23:38:07 +020010495 redraw_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 clear_cmdline = FALSE;
10497
10498 return length;
10499}
10500
10501/*
10502 * Position for a mode message.
10503 */
10504 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010505msg_pos_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507 msg_col = 0;
10508 msg_row = Rows - 1;
10509}
10510
10511/*
10512 * Delete mode message. Used when ESC is typed which is expected to end
10513 * Insert mode (but Insert mode didn't end yet!).
Bram Moolenaard12f5c12006-01-25 22:10:52 +000010514 * Caller should check "mode_displayed".
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 */
10516 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010517unshowmode(int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518{
10519 /*
Bram Moolenaare4ebd292010-01-19 17:40:46 +010010520 * Don't delete it right now, when not redrawing or inside a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 */
10522 if (!redrawing() || (!force && char_avail() && !KeyTyped))
10523 redraw_cmdline = TRUE; /* delete mode later */
10524 else
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010525 clearmode();
10526}
10527
10528/*
10529 * Clear the mode message.
10530 */
10531 void
Bram Moolenaarcf089462016-06-12 21:18:43 +020010532clearmode(void)
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010533{
Bram Moolenaar2abad542018-05-19 14:43:45 +020010534 int save_msg_row = msg_row;
10535 int save_msg_col = msg_col;
10536
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010537 msg_pos_mode();
Bram Moolenaar0b6d9112018-05-22 20:35:17 +020010538 if (reg_recording != 0)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010539 recording_mode(HL_ATTR(HLF_CM));
Bram Moolenaarfd773e92016-04-02 19:39:16 +020010540 msg_clr_eos();
Bram Moolenaar2abad542018-05-19 14:43:45 +020010541
10542 msg_col = save_msg_col;
10543 msg_row = save_msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544}
10545
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010546 static void
Bram Moolenaar05540972016-01-30 20:31:25 +010010547recording_mode(int attr)
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010548{
Bram Moolenaar32526b32019-01-19 17:43:09 +010010549 msg_puts_attr(_("recording"), attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010550 if (!shortmess(SHM_RECORDING))
10551 {
Bram Moolenaar32526b32019-01-19 17:43:09 +010010552 char s[4];
10553
10554 sprintf(s, " @%c", reg_recording);
10555 msg_puts_attr(s, attr);
Bram Moolenaara0ed84a2015-11-19 17:56:13 +010010556 }
10557}
10558
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010559/*
10560 * Draw the tab pages line at the top of the Vim window.
10561 */
Bram Moolenaare12bab32019-01-08 22:02:56 +010010562 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010563draw_tabline(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010564{
10565 int tabcount = 0;
10566 tabpage_T *tp;
10567 int tabwidth;
10568 int col = 0;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010569 int scol = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010570 int attr;
10571 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010572 win_T *cwp;
10573 int wincount;
10574 int modified;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010575 int c;
10576 int len;
Bram Moolenaar8820b482017-03-16 17:23:31 +010010577 int attr_sel = HL_ATTR(HLF_TPS);
10578 int attr_nosel = HL_ATTR(HLF_TP);
10579 int attr_fill = HL_ATTR(HLF_TPF);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010580 char_u *p;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010581 int room;
10582 int use_sep_chars = (t_colors < 8
10583#ifdef FEAT_GUI
10584 && !gui.in_use
10585#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020010586#ifdef FEAT_TERMGUICOLORS
10587 && !p_tgc
Bram Moolenaar8a633e32016-04-21 21:10:14 +020010588#endif
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010589 );
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010590
Bram Moolenaarc695cec2017-01-08 20:00:04 +010010591 if (ScreenLines == NULL)
10592 return;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000010593 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010594
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010595#ifdef FEAT_GUI_TABLINE
Bram Moolenaardb552d602006-03-23 22:59:57 +000010596 /* Take care of a GUI tabline. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010597 if (gui_use_tabline())
10598 {
10599 gui_update_tabline();
10600 return;
10601 }
10602#endif
10603
10604 if (tabline_height() < 1)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010605 return;
10606
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010607#if defined(FEAT_STL_OPT)
Bram Moolenaarca57ab52019-04-13 14:53:16 +020010608 clear_TabPageIdxs();
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010609
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010610 /* Use the 'tabline' option if it's set. */
10611 if (*p_tal != NUL)
10612 {
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010613 int saved_did_emsg = did_emsg;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010614
10615 /* Check for an error. If there is one we would loop in redrawing the
10616 * screen. Avoid that by making 'tabline' empty. */
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010617 did_emsg = FALSE;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010618 win_redr_custom(NULL, FALSE);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010619 if (did_emsg)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010620 set_string_option_direct((char_u *)"tabline", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000010621 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaarf73d3bc2016-04-11 21:55:15 +020010622 did_emsg |= saved_did_emsg;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010623 }
Bram Moolenaar238a5642006-02-21 22:12:05 +000010624 else
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010625#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010626 {
Bram Moolenaar29323592016-07-24 22:04:11 +020010627 FOR_ALL_TABPAGES(tp)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010628 ++tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010629
Bram Moolenaar238a5642006-02-21 22:12:05 +000010630 tabwidth = (Columns - 1 + tabcount / 2) / tabcount;
10631 if (tabwidth < 6)
10632 tabwidth = 6;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010633
Bram Moolenaar238a5642006-02-21 22:12:05 +000010634 attr = attr_nosel;
10635 tabcount = 0;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010636 for (tp = first_tabpage; tp != NULL && col < Columns - 4;
10637 tp = tp->tp_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010638 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010639 scol = col;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010640
Bram Moolenaar238a5642006-02-21 22:12:05 +000010641 if (tp->tp_topframe == topframe)
10642 attr = attr_sel;
10643 if (use_sep_chars && col > 0)
10644 screen_putchar('|', 0, col++, attr);
10645
10646 if (tp->tp_topframe != topframe)
10647 attr = attr_nosel;
10648
10649 screen_putchar(' ', 0, col++, attr);
10650
10651 if (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +000010652 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010653 cwp = curwin;
10654 wp = firstwin;
10655 }
10656 else
10657 {
10658 cwp = tp->tp_curwin;
10659 wp = tp->tp_firstwin;
10660 }
10661
10662 modified = FALSE;
10663 for (wincount = 0; wp != NULL; wp = wp->w_next, ++wincount)
10664 if (bufIsChanged(wp->w_buffer))
10665 modified = TRUE;
10666 if (modified || wincount > 1)
10667 {
10668 if (wincount > 1)
10669 {
10670 vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010671 len = (int)STRLEN(NameBuff);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010672 if (col + len >= Columns - 3)
10673 break;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010674 screen_puts_len(NameBuff, len, 0, col,
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010675#if defined(FEAT_SYN_HL)
Bram Moolenaar8820b482017-03-16 17:23:31 +010010676 hl_combine_attr(attr, HL_ATTR(HLF_T))
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010677#else
Bram Moolenaare0f14822014-08-06 13:20:56 +020010678 attr
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000010679#endif
Bram Moolenaar238a5642006-02-21 22:12:05 +000010680 );
10681 col += len;
10682 }
10683 if (modified)
10684 screen_puts_len((char_u *)"+", 1, 0, col++, attr);
10685 screen_putchar(' ', 0, col++, attr);
10686 }
10687
10688 room = scol - col + tabwidth - 1;
10689 if (room > 0)
10690 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010691 /* Get buffer name in NameBuff[] */
10692 get_trans_bufname(cwp->w_buffer);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010693 shorten_dir(NameBuff);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010694 len = vim_strsize(NameBuff);
10695 p = NameBuff;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010696 if (has_mbyte)
10697 while (len > room)
10698 {
10699 len -= ptr2cells(p);
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010700 MB_PTR_ADV(p);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010701 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010010702 else if (len > room)
Bram Moolenaar238a5642006-02-21 22:12:05 +000010703 {
10704 p += len - room;
10705 len = room;
10706 }
Bram Moolenaarfd2ac762006-03-01 22:09:21 +000010707 if (len > Columns - col - 1)
10708 len = Columns - col - 1;
Bram Moolenaar238a5642006-02-21 22:12:05 +000010709
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010710 screen_puts_len(p, (int)STRLEN(p), 0, col, attr);
Bram Moolenaarf740b292006-02-16 22:11:02 +000010711 col += len;
10712 }
Bram Moolenaarf740b292006-02-16 22:11:02 +000010713 screen_putchar(' ', 0, col++, attr);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010714
10715 /* Store the tab page number in TabPageIdxs[], so that
10716 * jump_to_mouse() knows where each one is. */
10717 ++tabcount;
10718 while (scol < col)
10719 TabPageIdxs[scol++] = tabcount;
Bram Moolenaarf740b292006-02-16 22:11:02 +000010720 }
10721
Bram Moolenaar238a5642006-02-21 22:12:05 +000010722 if (use_sep_chars)
10723 c = '_';
10724 else
10725 c = ' ';
10726 screen_fill(0, 1, col, (int)Columns, c, c, attr_fill);
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010727
10728 /* Put an "X" for closing the current tab if there are several. */
10729 if (first_tabpage->tp_next != NULL)
10730 {
10731 screen_putchar('X', 0, (int)Columns - 1, attr_nosel);
10732 TabPageIdxs[Columns - 1] = -999;
10733 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010734 }
Bram Moolenaarb21e5842006-04-16 18:30:08 +000010735
10736 /* Reset the flag here again, in case evaluating 'tabline' causes it to be
10737 * set. */
10738 redraw_tabline = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010739}
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010740
10741/*
10742 * Get buffer name for "buf" into NameBuff[].
10743 * Takes care of special buffer names and translates special characters.
10744 */
10745 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010746get_trans_bufname(buf_T *buf)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010747{
10748 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +020010749 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000010750 else
10751 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
10752 trans_characters(NameBuff, MAXPATHL);
10753}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000010754
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755/*
10756 * Get the character to use in a status line. Get its attributes in "*attr".
10757 */
10758 static int
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010759fillchar_status(int *attr, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760{
10761 int fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010762
10763#ifdef FEAT_TERMINAL
10764 if (bt_terminal(wp->w_buffer))
10765 {
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010766 if (wp == curwin)
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010767 {
10768 *attr = HL_ATTR(HLF_ST);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010769 fill = fill_stl;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010770 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010771 else
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010772 {
10773 *attr = HL_ATTR(HLF_STNC);
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010774 fill = fill_stlnc;
Bram Moolenaar05fbfdc2017-08-14 22:35:08 +020010775 }
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010776 }
10777 else
10778#endif
10779 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010781 *attr = HL_ATTR(HLF_S);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782 fill = fill_stl;
10783 }
10784 else
10785 {
Bram Moolenaar8820b482017-03-16 17:23:31 +010010786 *attr = HL_ATTR(HLF_SNC);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 fill = fill_stlnc;
10788 }
10789 /* Use fill when there is highlighting, and highlighting of current
10790 * window differs, or the fillchars differ, or this is not the
10791 * current window */
Bram Moolenaar8820b482017-03-16 17:23:31 +010010792 if (*attr != 0 && ((HL_ATTR(HLF_S) != HL_ATTR(HLF_SNC)
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010793 || wp != curwin || ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 || (fill_stl != fill_stlnc)))
10795 return fill;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020010796 if (wp == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 return '^';
10798 return '=';
10799}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801/*
10802 * Get the character to use in a separator between vertically split windows.
10803 * Get its attributes in "*attr".
10804 */
10805 static int
Bram Moolenaar05540972016-01-30 20:31:25 +010010806fillchar_vsep(int *attr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807{
Bram Moolenaar8820b482017-03-16 17:23:31 +010010808 *attr = HL_ATTR(HLF_C);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 if (*attr == 0 && fill_vert == ' ')
10810 return '|';
10811 else
10812 return fill_vert;
10813}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
10815/*
10816 * Return TRUE if redrawing should currently be done.
10817 */
10818 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010819redrawing(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010010821#ifdef FEAT_EVAL
10822 if (disable_redraw_for_testing)
10823 return 0;
10824 else
10825#endif
Bram Moolenaared5a9d62018-09-06 13:14:43 +020010826 return ((!RedrawingDisabled
10827#ifdef FEAT_EVAL
10828 || ignore_redraw_flag_for_testing
10829#endif
10830 ) && !(p_lz && char_avail() && !KeyTyped && !do_redraw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831}
10832
10833/*
10834 * Return TRUE if printing messages should currently be done.
10835 */
10836 int
Bram Moolenaar05540972016-01-30 20:31:25 +010010837messaging(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
10839 return (!(p_lz && char_avail() && !KeyTyped));
10840}
10841
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010842#ifdef FEAT_MENU
10843/*
10844 * Draw the window toolbar.
10845 */
10846 static void
10847redraw_win_toolbar(win_T *wp)
10848{
10849 vimmenu_T *menu;
10850 int item_idx = 0;
10851 int item_count = 0;
10852 int col = 0;
10853 int next_col;
10854 int off = (int)(current_ScreenLine - ScreenLines);
10855 int fill_attr = syn_name2attr((char_u *)"ToolbarLine");
10856 int button_attr = syn_name2attr((char_u *)"ToolbarButton");
10857
10858 vim_free(wp->w_winbar_items);
10859 for (menu = wp->w_winbar->children; menu != NULL; menu = menu->next)
10860 ++item_count;
10861 wp->w_winbar_items = (winbar_item_T *)alloc_clear(
Bram Moolenaar18a4ba22019-05-24 19:39:03 +020010862 sizeof(winbar_item_T) * (item_count + 1));
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010863
10864 /* TODO: use fewer spaces if there is not enough room */
10865 for (menu = wp->w_winbar->children;
Bram Moolenaar02631462017-09-22 15:20:32 +020010866 menu != NULL && col < wp->w_width; menu = menu->next)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010867 {
10868 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010869 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010870 break;
10871 if (col > 1)
10872 {
10873 space_to_screenline(off + col, fill_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010874 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010875 break;
10876 }
10877
10878 wp->w_winbar_items[item_idx].wb_startcol = col;
10879 space_to_screenline(off + col, button_attr);
Bram Moolenaar02631462017-09-22 15:20:32 +020010880 if (++col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010881 break;
10882
10883 next_col = text_to_screenline(wp, menu->name, col);
10884 while (col < next_col)
10885 {
10886 ScreenAttrs[off + col] = button_attr;
10887 ++col;
10888 }
10889 wp->w_winbar_items[item_idx].wb_endcol = col;
10890 wp->w_winbar_items[item_idx].wb_menu = menu;
10891 ++item_idx;
10892
Bram Moolenaar02631462017-09-22 15:20:32 +020010893 if (col >= wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010894 break;
10895 space_to_screenline(off + col, button_attr);
10896 ++col;
10897 }
Bram Moolenaar02631462017-09-22 15:20:32 +020010898 while (col < wp->w_width)
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010899 {
10900 space_to_screenline(off + col, fill_attr);
10901 ++col;
10902 }
10903 wp->w_winbar_items[item_idx].wb_menu = NULL; /* end marker */
10904
Bram Moolenaar02631462017-09-22 15:20:32 +020010905 screen_line(wp->w_winrow, wp->w_wincol, (int)wp->w_width,
Bram Moolenaar4d784b22019-05-25 19:51:39 +020010906 (int)wp->w_width, 0);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +020010907}
10908#endif
Bram Moolenaar491ac282018-06-17 14:47:55 +020010909
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910/*
10911 * Show current status info in ruler and various other places
10912 * If always is FALSE, only show ruler if position has changed.
10913 */
10914 void
Bram Moolenaar05540972016-01-30 20:31:25 +010010915showruler(int always)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916{
10917 if (!always && !redrawing())
10918 return;
Bram Moolenaar9372a112005-12-06 19:59:18 +000010919#ifdef FEAT_INS_EXPAND
10920 if (pum_visible())
10921 {
10922 /* Don't redraw right now, do it later. */
10923 curwin->w_redr_status = TRUE;
10924 return;
10925 }
10926#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020010927#if defined(FEAT_STL_OPT)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +000010928 if ((*p_stl != NUL || *curwin->w_p_stl != NUL) && curwin->w_status_height)
Bram Moolenaar362f3562009-11-03 16:20:34 +000010929 redraw_custom_statusline(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 else
10931#endif
10932#ifdef FEAT_CMDL_INFO
Bram Moolenaar491ac282018-06-17 14:47:55 +020010933 win_redr_ruler(curwin, always, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#endif
10935
10936#ifdef FEAT_TITLE
10937 if (need_maketitle
10938# ifdef FEAT_STL_OPT
10939 || (p_icon && (stl_syntax & STL_IN_ICON))
10940 || (p_title && (stl_syntax & STL_IN_TITLE))
10941# endif
10942 )
10943 maketitle();
10944#endif
Bram Moolenaar497683b2008-05-28 17:02:46 +000010945 /* Redraw the tab pages line if needed. */
10946 if (redraw_tabline)
10947 draw_tabline();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948}
10949
10950#ifdef FEAT_CMDL_INFO
10951 static void
Bram Moolenaar491ac282018-06-17 14:47:55 +020010952win_redr_ruler(win_T *wp, int always, int ignore_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010954#define RULER_BUF_LEN 70
10955 char_u buffer[RULER_BUF_LEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 int row;
10957 int fillchar;
10958 int attr;
10959 int empty_line = FALSE;
10960 colnr_T virtcol;
10961 int i;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000010962 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 int o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 int this_ru_col;
10965 int off = 0;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +010010966 int width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967
10968 /* If 'ruler' off or redrawing disabled, don't do anything */
10969 if (!p_ru)
10970 return;
10971
10972 /*
10973 * Check if cursor.lnum is valid, since win_redr_ruler() may be called
10974 * after deleting lines, before cursor.lnum is corrected.
10975 */
10976 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
10977 return;
10978
10979#ifdef FEAT_INS_EXPAND
10980 /* Don't draw the ruler while doing insert-completion, it might overwrite
10981 * the (long) mode message. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (wp == lastwin && lastwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 if (edit_submode != NULL)
10984 return;
Bram Moolenaar491ac282018-06-17 14:47:55 +020010985 // Don't draw the ruler when the popup menu is visible, it may overlap.
10986 // Except when the popup menu will be redrawn anyway.
10987 if (!ignore_pum && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +000010988 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989#endif
10990
10991#ifdef FEAT_STL_OPT
10992 if (*p_ruf)
10993 {
Bram Moolenaar238a5642006-02-21 22:12:05 +000010994 int save_called_emsg = called_emsg;
10995
10996 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 win_redr_custom(wp, TRUE);
Bram Moolenaar238a5642006-02-21 22:12:05 +000010998 if (called_emsg)
10999 set_string_option_direct((char_u *)"rulerformat", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000011000 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaar238a5642006-02-21 22:12:05 +000011001 called_emsg |= save_called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 return;
11003 }
11004#endif
11005
11006 /*
11007 * Check if not in Insert mode and the line is empty (will show "0-1").
11008 */
11009 if (!(State & INSERT)
11010 && *ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE) == NUL)
11011 empty_line = TRUE;
11012
11013 /*
11014 * Only draw the ruler when something changed.
11015 */
11016 validate_virtcol_win(wp);
11017 if ( redraw_cmdline
11018 || always
11019 || wp->w_cursor.lnum != wp->w_ru_cursor.lnum
11020 || wp->w_cursor.col != wp->w_ru_cursor.col
11021 || wp->w_virtcol != wp->w_ru_virtcol
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 || wp->w_cursor.coladd != wp->w_ru_cursor.coladd
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 || wp->w_topline != wp->w_ru_topline
11024 || wp->w_buffer->b_ml.ml_line_count != wp->w_ru_line_count
11025#ifdef FEAT_DIFF
11026 || wp->w_topfill != wp->w_ru_topfill
11027#endif
11028 || empty_line != wp->w_ru_empty)
11029 {
11030 cursor_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 if (wp->w_status_height)
11032 {
11033 row = W_WINROW(wp) + wp->w_height;
Bram Moolenaar3633cf52017-07-31 22:29:35 +020011034 fillchar = fillchar_status(&attr, wp);
Bram Moolenaar53f81742017-09-22 14:35:51 +020011035 off = wp->w_wincol;
Bram Moolenaar02631462017-09-22 15:20:32 +020011036 width = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 }
11038 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 {
11040 row = Rows - 1;
11041 fillchar = ' ';
11042 attr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 width = Columns;
11044 off = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 }
11046
11047 /* In list mode virtcol needs to be recomputed */
11048 virtcol = wp->w_virtcol;
11049 if (wp->w_p_list && lcs_tab1 == NUL)
11050 {
11051 wp->w_p_list = FALSE;
11052 getvvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
11053 wp->w_p_list = TRUE;
11054 }
11055
11056 /*
11057 * Some sprintfs return the length, some return a pointer.
11058 * To avoid portability problems we use strlen() here.
11059 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011060 vim_snprintf((char *)buffer, RULER_BUF_LEN, "%ld,",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
11062 ? 0L
11063 : (long)(wp->w_cursor.lnum));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011064 len = STRLEN(buffer);
11065 col_print(buffer + len, RULER_BUF_LEN - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 empty_line ? 0 : (int)wp->w_cursor.col + 1,
11067 (int)virtcol + 1);
11068
11069 /*
11070 * Add a "50%" if there is room for it.
11071 * On the last line, don't print in the last column (scrolls the
11072 * screen up on some terminals).
11073 */
11074 i = (int)STRLEN(buffer);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011075 get_rel_pos(wp, buffer + i + 1, RULER_BUF_LEN - i - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 o = i + vim_strsize(buffer + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 if (wp->w_status_height == 0) /* can't use last char of screen */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 ++o;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 this_ru_col = ru_col - (Columns - width);
11080 if (this_ru_col < 0)
11081 this_ru_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 /* Never use more than half the window/screen width, leave the other
11083 * half for the filename. */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011084 if (this_ru_col < (width + 1) / 2)
11085 this_ru_col = (width + 1) / 2;
11086 if (this_ru_col + o < width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 {
Bram Moolenaar0027c212015-01-07 13:31:52 +010011088 /* need at least 3 chars left for get_rel_pos() + NUL */
Bram Moolenaar4033c552017-09-16 20:54:51 +020011089 while (this_ru_col + o < width && RULER_BUF_LEN > i + 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 if (has_mbyte)
11092 i += (*mb_char2bytes)(fillchar, buffer + i);
11093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094 buffer[i++] = fillchar;
11095 ++o;
11096 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000011097 get_rel_pos(wp, buffer + i, RULER_BUF_LEN - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 }
11099 /* Truncate at window boundary. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 if (has_mbyte)
11101 {
11102 o = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011103 for (i = 0; buffer[i] != NUL; i += (*mb_ptr2len)(buffer + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 {
11105 o += (*mb_ptr2cells)(buffer + i);
Bram Moolenaar4033c552017-09-16 20:54:51 +020011106 if (this_ru_col + o > width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 {
11108 buffer[i] = NUL;
11109 break;
11110 }
11111 }
11112 }
Bram Moolenaara12a1612019-01-24 16:39:02 +010011113 else if (this_ru_col + (int)STRLEN(buffer) > width)
Bram Moolenaar4033c552017-09-16 20:54:51 +020011114 buffer[width - this_ru_col] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115
Bram Moolenaar4033c552017-09-16 20:54:51 +020011116 screen_puts(buffer, row, this_ru_col + off, attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 i = redraw_cmdline;
11118 screen_fill(row, row + 1,
Bram Moolenaar4033c552017-09-16 20:54:51 +020011119 this_ru_col + off + (int)STRLEN(buffer),
11120 (int)(off + width),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 fillchar, fillchar, attr);
11122 /* don't redraw the cmdline because of showing the ruler */
11123 redraw_cmdline = i;
11124 wp->w_ru_cursor = wp->w_cursor;
11125 wp->w_ru_virtcol = wp->w_virtcol;
11126 wp->w_ru_empty = empty_line;
11127 wp->w_ru_topline = wp->w_topline;
11128 wp->w_ru_line_count = wp->w_buffer->b_ml.ml_line_count;
11129#ifdef FEAT_DIFF
11130 wp->w_ru_topfill = wp->w_topfill;
11131#endif
11132 }
11133}
11134#endif
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011135
11136#if defined(FEAT_LINEBREAK) || defined(PROTO)
11137/*
Bram Moolenaar64486672010-05-16 15:46:46 +020011138 * Return the width of the 'number' and 'relativenumber' column.
11139 * Caller may need to check if 'number' or 'relativenumber' is set.
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011140 * Otherwise it depends on 'numberwidth' and the line count.
11141 */
11142 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011143number_width(win_T *wp)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011144{
11145 int n;
11146 linenr_T lnum;
11147
Bram Moolenaar5ebc09b2013-06-04 22:13:50 +020011148 if (wp->w_p_rnu && !wp->w_p_nu)
11149 /* cursor line shows "0" */
11150 lnum = wp->w_height;
11151 else
11152 /* cursor line shows absolute line number */
11153 lnum = wp->w_buffer->b_ml.ml_line_count;
Bram Moolenaar64486672010-05-16 15:46:46 +020011154
Bram Moolenaar6b314672015-03-20 15:42:10 +010011155 if (lnum == wp->w_nrwidth_line_count && wp->w_nuw_cached == wp->w_p_nuw)
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011156 return wp->w_nrwidth_width;
11157 wp->w_nrwidth_line_count = lnum;
11158
11159 n = 0;
11160 do
11161 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000011162 lnum /= 10;
11163 ++n;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011164 } while (lnum > 0);
11165
11166 /* 'numberwidth' gives the minimal width plus one */
11167 if (n < wp->w_p_nuw - 1)
11168 n = wp->w_p_nuw - 1;
11169
11170 wp->w_nrwidth_width = n;
Bram Moolenaar6b314672015-03-20 15:42:10 +010011171 wp->w_nuw_cached = wp->w_p_nuw;
Bram Moolenaar592e0a22004-07-03 16:05:59 +000011172 return n;
11173}
11174#endif
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011175
Bram Moolenaar113e1072019-01-20 15:30:40 +010011176#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011177/*
11178 * Return the current cursor column. This is the actual position on the
11179 * screen. First column is 0.
11180 */
11181 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011182screen_screencol(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011183{
11184 return screen_cur_col;
11185}
11186
11187/*
11188 * Return the current cursor row. This is the actual position on the screen.
11189 * First row is 0.
11190 */
11191 int
Bram Moolenaar05540972016-01-30 20:31:25 +010011192screen_screenrow(void)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010011193{
11194 return screen_cur_row;
11195}
Bram Moolenaar113e1072019-01-20 15:30:40 +010011196#endif